android.content.pm.IPackageStatsObserver Java Examples

The following examples show how to use android.content.pm.IPackageStatsObserver. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SysCacheScanTask.java    From CleanExpert with MIT License 6 votes vote down vote up
@Override
protected Void doInBackground(Void... params) {
    mCallback.onBegin();
    PackageManager pm = MyApplication.getInstance().getPackageManager();
    List<ApplicationInfo> installedPackages = pm.getInstalledApplications(PackageManager.GET_GIDS);

    IPackageStatsObserver.Stub observer = new PackageStatsObserver();

    mScanCount = 0;
    mTotalCount = installedPackages.size();
    mSysCaches = new ArrayList<>();
    mAppNames = new HashMap<>();

    for (int i = 0; i < mTotalCount; i++) {
        ApplicationInfo info = installedPackages.get(i);
        mAppNames.put(info.packageName, pm.getApplicationLabel(info).toString());
        getPackageInfo(info.packageName, observer);
    }

    return null;
}
 
Example #2
Source File: SysCacheScanTask.java    From FileManager with Apache License 2.0 6 votes vote down vote up
@Override
protected Void doInBackground(Void... params) {
    mCallBack.onBegin();

    if (isCancelled()) {
        mCallBack.onCancel();
        return null;
    }

    PackageManager pm = App.getAppContext().getPackageManager();
    List<ApplicationInfo> installedPackages = pm.getInstalledApplications(PackageManager.GET_GIDS);

    IPackageStatsObserver.Stub observer = new PackageStatsObserver();
    mTotalCount = installedPackages.size();
    mSysCaches = new ArrayList<>();
    mAppNames = new HashMap<>();

    for (int i = 0; i < mTotalCount; i++) {
        ApplicationInfo info = installedPackages.get(i);
        mAppNames.put(info.packageName, pm.getApplicationLabel(info).toString());
        getPackageInfo(info.packageName, observer);
    }

    mIsOverTime = false;
    return null;
}
 
Example #3
Source File: CleanerService.java    From MemoryCleaner with Apache License 2.0 6 votes vote down vote up
@Override public void onCreate() {
    try {
        mGetPackageSizeInfoMethod = getPackageManager().getClass()
                                                       .getMethod(
                                                               "getPackageSizeInfo",
                                                               String.class,
                                                               IPackageStatsObserver.class);

        mFreeStorageAndNotifyMethod = getPackageManager().getClass()
                                                         .getMethod(
                                                                 "freeStorageAndNotify",
                                                                 long.class,
                                                                 IPackageDataObserver.class);
        mDeleteApplicationCacheFiles = getPackageManager().getClass()
                                                          .getMethod(
                                                                  "deleteApplicationCacheFiles",
                                                                  String.class,
                                                                  IPackageDataObserver.class);
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
}
 
Example #4
Source File: AppSizeUtil.java    From AndroidGodEye with Apache License 2.0 6 votes vote down vote up
/**
 * 获取应用大小8.0以下
 */
@SuppressWarnings("JavaReflectionMemberAccess")
private static void getAppSizeLowerO(Context context, @NonNull final OnGetSizeListener listener) {
    try {
        Method method = PackageManager.class.getMethod("getPackageSizeInfo", String.class,
                IPackageStatsObserver.class);
        // 调用 getPackageSizeInfo 方法,需要两个参数:1、需要检测的应用包名;2、回调
        method.invoke(context.getPackageManager(), context.getPackageName(), new IPackageStatsObserver.Stub() {
            @Override
            public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) {
                AppSizeInfo ctAppSizeInfo = new AppSizeInfo();
                ctAppSizeInfo.cacheSize = pStats.cacheSize;
                ctAppSizeInfo.dataSize = pStats.dataSize;
                ctAppSizeInfo.codeSize = pStats.codeSize;
                listener.onGetSize(ctAppSizeInfo);
            }
        });
    } catch (Throwable e) {
        listener.onError(e);
    }
}
 
Example #5
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 6 votes vote down vote up
@Override
@UnsupportedAppUsage
public void getPackageSizeInfoAsUser(String packageName, int userHandle,
        IPackageStatsObserver observer) {
    final String msg = "Shame on you for calling the hidden API "
            + "getPackageSizeInfoAsUser(). Shame!";
    if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
        throw new UnsupportedOperationException(msg);
    } else if (observer != null) {
        Log.d(TAG, msg);
        try {
            observer.onGetStatsCompleted(null, false);
        } catch (RemoteException ignored) {
        }
    }
}
 
Example #6
Source File: StorageActivity.java    From AndroidDemo with MIT License 5 votes vote down vote up
private void getAppSize() {
        boolean flag = false;
        try {
            PackageManager.class.getMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class);
            flag = true;
        } catch (NoSuchMethodException e) {
            try {
                PackageManager.class.getMethod("getPackageSizeInfo", String.class, int.class, IPackageStatsObserver.class);
                flag = true;
            } catch (NoSuchMethodException ee) {
                flag = false;
            }
        }
//        Method[] methods = PackageManager.class.getMethods();
//        for (Method m : methods) {
//            if ("getPackageSizeInfo".equals(m.getName())) {
//                Class<?>[] classes = m.getParameterTypes();
//                if (classes.length == 2 || classes.length == 3) {
//                    flag = true;
//                    break;
//                }
//            }
//        }
        if (flag) {
            useIPackageStatsObserver();
        } else {
            if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
                getStorageWith_7_1();
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                getStorageWith_8();
            }
        }
    }
 
Example #7
Source File: CleanerService.java    From android-cache-cleaner with MIT License 5 votes vote down vote up
@Override
public void onCreate() {
    try {
        mGetPackageSizeInfoMethod = getPackageManager().getClass().getMethod(
                "getPackageSizeInfo", String.class, IPackageStatsObserver.class);

        mFreeStorageAndNotifyMethod = getPackageManager().getClass().getMethod(
                "freeStorageAndNotify", long.class, IPackageDataObserver.class);
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
}
 
Example #8
Source File: AppManagerEngine.java    From MobileGuard with MIT License 5 votes vote down vote up
/**
 * get app size by package name
 * @param context
 * @param packageName package name
 * @param listener it will be call when success to get size
 */
public static void getAppSize(Context context, String packageName, final AppSizeInfoListener listener) {
    // check argument
    if(null == listener) {
        throw new NullPointerException("listener can't be null");
    }
    if(TextUtils.isEmpty(packageName)) {
        throw  new IllegalArgumentException("packageName can't be empty");
    }

    // get pm
    PackageManager pm = context.getPackageManager();
    Method getPackageSizeInfo = null;
    try {
        // get method getPackageSizeInfo
        getPackageSizeInfo = pm.getClass().getMethod(
                "getPackageSizeInfo",
                String.class, IPackageStatsObserver.class);
        // call method
        getPackageSizeInfo.invoke(pm, packageName,
                new IPackageStatsObserver.Stub() {
                    @Override
                    public void onGetStatsCompleted(PackageStats pStats, boolean succeeded)
                            throws RemoteException {
                        // call listener
                        listener.onGetSizeInfoCompleted(
                                new AppSizeInfo(pStats.cacheSize, pStats.dataSize, pStats.codeSize));
                    }
                });
    } catch (Exception e) {
        e.printStackTrace();
    }

}
 
Example #9
Source File: AppsPresenter.java    From MemoryCleaner with Apache License 2.0 5 votes vote down vote up
@Override public void onCreate(Bundle savedInstanceState) {
    //position = mContext.getArguments().getInt(ARG_POSITION);
    try {
        mGetPackageSizeInfoMethod = mContext.getPackageManager()
                                            .getClass()
                                            .getMethod("getPackageSizeInfo",
                                                    String.class,
                                                    IPackageStatsObserver.class);
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
    //mTaskScanApps = new TaskScanApps();
    //mAppsView.initViews(mAppsListAdapter,mContext,);
}
 
Example #10
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@Override
public void getPackageSizeInfoAsUser(String packageName, int userHandle,
        IPackageStatsObserver observer) {
    try {
        mPM.getPackageSizeInfo(packageName, userHandle, observer);
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
}
 
Example #11
Source File: MainActivity.java    From stynico with MIT License 5 votes vote down vote up
private void getCaches(){
    // scan
    pm = getPackageManager();
    List<PackageInfo> packages = pm.getInstalledPackages(0);

    int max = packages.size();
    int current = 0;
    sb.delete(0, sb.length());
    sb.append("");
    sb.append("本机一共安装了:" + max+"个应用");
    tvShowCaches.setText(sb.toString());
    for (PackageInfo pinfo : packages) {
        String packageName = pinfo.packageName;
        try {

            Method getPackageSizeInfo = PackageManager.class
                    .getDeclaredMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class);
            getPackageSizeInfo.invoke(pm, packageName, new MyPackageStateObserver());
            current++;
        } catch (Exception e) {
            current++;
            e.printStackTrace();
        }

    }
    //===到这里,数据准备完成
    mHadler.postDelayed(new Runnable() {
        @Override
        public void run() {
btnClearAll.setVisibility(View.VISIBLE);
            Toast.makeText(getApplicationContext(),"已读取全部缓存",Toast.LENGTH_SHORT).show();
            sbCache.append(Formatter.formatFileSize(getApplicationContext(),cacheS)+"\n");
            tvShowCaches.setText(sb.toString());
            tvAppCache.setText(sbCache.toString());
            sbCache.delete(0,sbCache.length());
        }
    }, 1000);
    //ok,所有应用程序信息显示完成
}
 
Example #12
Source File: MainActivity.java    From styT with Apache License 2.0 5 votes vote down vote up
private void getCaches() {
    // scan
    pm = getPackageManager();
    List<PackageInfo> packages = pm.getInstalledPackages(0);

    int max = packages.size();
    int current = 0;
    sb.delete(0, sb.length());
    sb.append("");
    sb.append("本机一共安装了:").append(max).append("个应用");
    tvShowCaches.setText(sb.toString());
    for (PackageInfo pinfo : packages) {
        String packageName = pinfo.packageName;
        try {

            Method getPackageSizeInfo = PackageManager.class
                    .getDeclaredMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class);
            getPackageSizeInfo.invoke(pm, packageName, new MyPackageStateObserver());
            current++;
        } catch (Exception e) {
            current++;
            e.printStackTrace();
        }

    }
    //===到这里,数据准备完成
    mHadler.postDelayed(new Runnable() {
        @Override
        public void run() {
            btnClearAll.setVisibility(View.VISIBLE);
            Toast.makeText(getApplicationContext(), "已读取全部缓存", Toast.LENGTH_SHORT).show();
            sbCache.append(Formatter.formatFileSize(getApplicationContext(), cacheS)).append("\n");
            tvShowCaches.setText(sb.toString());
            tvAppCache.setText(sbCache.toString());
            sbCache.delete(0, sbCache.length());
        }
    }, 1000);
    //ok,所有应用程序信息显示完成
}
 
Example #13
Source File: ApplicationPackageManager.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void getPackageSizeInfoAsUser(String packageName, int userHandle,
        IPackageStatsObserver observer) {
    final String msg = "Shame on you for calling the hidden API "
            + "getPackageSizeInfoAsUser(). Shame!";
    if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
        throw new UnsupportedOperationException(msg);
    } else if (observer != null) {
        Log.d(TAG, msg);
        try {
            observer.onGetStatsCompleted(null, false);
        } catch (RemoteException ignored) {
        }
    }
}
 
Example #14
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@Override
public void getPackageSizeInfoAsUser(String packageName, int userHandle,
        IPackageStatsObserver observer) {
    try {
        mPM.getPackageSizeInfo(packageName, userHandle, observer);
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
}
 
Example #15
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@Override
public void getPackageSizeInfoAsUser(String packageName, int userHandle,
        IPackageStatsObserver observer) {
    final String msg = "Shame on you for calling the hidden API "
            + "getPackageSizeInfoAsUser(). Shame!";
    if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
        throw new UnsupportedOperationException(msg);
    } else if (observer != null) {
        Log.d(TAG, msg);
        try {
            observer.onGetStatsCompleted(null, false);
        } catch (RemoteException ignored) {
        }
    }
}
 
Example #16
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@Override
public void getPackageSizeInfo(String packageName, int userHandle,
        IPackageStatsObserver observer) {
    try {
        mPM.getPackageSizeInfo(packageName, userHandle, observer);
    } catch (RemoteException e) {
        // Should never happen!
    }
}
 
Example #17
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@Override
public void getPackageSizeInfo(String packageName,
                               IPackageStatsObserver observer) {
    try {
        mPM.getPackageSizeInfo(packageName, observer);
    } catch (RemoteException e) {
        // Should never happen!
    }
}
 
Example #18
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@Override
public void getPackageSizeInfoAsUser(String packageName, int userHandle,
        IPackageStatsObserver observer) {
    final String msg = "Shame on you for calling the hidden API "
            + "getPackageSizeInfoAsUser(). Shame!";
    if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
        throw new UnsupportedOperationException(msg);
    } else if (observer != null) {
        Log.d(TAG, msg);
        try {
            observer.onGetStatsCompleted(null, false);
        } catch (RemoteException ignored) {
        }
    }
}
 
Example #19
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@Override
public void getPackageSizeInfoAsUser(String packageName, int userHandle,
        IPackageStatsObserver observer) {
    final String msg = "Shame on you for calling the hidden API "
            + "getPackageSizeInfoAsUser(). Shame!";
    if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
        throw new UnsupportedOperationException(msg);
    } else if (observer != null) {
        Log.d(TAG, msg);
        try {
            observer.onGetStatsCompleted(null, false);
        } catch (RemoteException ignored) {
        }
    }
}
 
Example #20
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@Override
public void getPackageSizeInfo(String packageName, int userHandle,
        IPackageStatsObserver observer) {
    try {
        mPM.getPackageSizeInfo(packageName, userHandle, observer);
    } catch (RemoteException e) {
        // Should never happen!
    }
}
 
Example #21
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@Override
public void getPackageSizeInfoAsUser(String packageName, int userHandle,
        IPackageStatsObserver observer) {
    final String msg = "Shame on you for calling the hidden API "
            + "getPackageSizeInfoAsUser(). Shame!";
    if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
        throw new UnsupportedOperationException(msg);
    } else if (observer != null) {
        Log.d(TAG, msg);
        try {
            observer.onGetStatsCompleted(null, false);
        } catch (RemoteException ignored) {
        }
    }
}
 
Example #22
Source File: PackageManagerWrapper.java    From condom with Apache License 2.0 4 votes vote down vote up
/** @hide */
@Override public void getPackageSizeInfoAsUser(String packageName, int userId, IPackageStatsObserver observer) {
	mBase.getPackageSizeInfoAsUser(packageName, userId, observer);
}
 
Example #23
Source File: PackageManagerWrapper.java    From condom with Apache License 2.0 4 votes vote down vote up
/** @hide */
@Override public void getPackageSizeInfo(String packageName, IPackageStatsObserver observer) {
	mBase.getPackageSizeInfo(packageName, observer);
}
 
Example #24
Source File: PackageManagerWrapper.java    From MiPushFramework with GNU General Public License v3.0 4 votes vote down vote up
/** @hide */
@Override public void getPackageSizeInfoAsUser(String packageName, int userId, IPackageStatsObserver observer) {
	mBase.getPackageSizeInfoAsUser(packageName, userId, observer);
}
 
Example #25
Source File: PackageManagerWrapper.java    From MiPushFramework with GNU General Public License v3.0 4 votes vote down vote up
/** @hide */
@Override public void getPackageSizeInfo(String packageName, IPackageStatsObserver observer) {
	mBase.getPackageSizeInfo(packageName, observer);
}
 
Example #26
Source File: CleanerService.java    From android-cache-cleaner with MIT License 4 votes vote down vote up
@Override
protected List<AppsListItem> doInBackground(Void... params) {
    mCacheSize = 0;

    final List<ApplicationInfo> packages = getPackageManager().getInstalledApplications(
            PackageManager.GET_META_DATA);

    publishProgress(0, packages.size());

    final CountDownLatch countDownLatch = new CountDownLatch(packages.size());

    final List<AppsListItem> apps = new ArrayList<>();

    try {
        for (ApplicationInfo pkg : packages) {
            mGetPackageSizeInfoMethod.invoke(getPackageManager(), pkg.packageName,
                    new IPackageStatsObserver.Stub() {

                        @Override
                        public void onGetStatsCompleted(PackageStats pStats,
                                                        boolean succeeded)
                                throws RemoteException {
                            synchronized (apps) {
                                publishProgress(++mAppCount, packages.size());

                                mCacheSize += addPackage(apps, pStats, succeeded);
                            }

                            synchronized (countDownLatch) {
                                countDownLatch.countDown();
                            }
                        }
                    }
            );
        }

        countDownLatch.await();
    } catch (InvocationTargetException | InterruptedException | IllegalAccessException e) {
        e.printStackTrace();
    }

    return new ArrayList<>(apps);
}