@Override public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException { String packageName = pStats.packageName; long cacheSize = pStats.cacheSize; long codeSize = pStats.codeSize; long dataSize = pStats.dataSize; cacheS += cacheSize; // sb.delete(0, sb.length()); if (cacheSize > 0) { sb.append("") .append("") .append("") .append("") ; //Log.e("aaaa", sb.toString()); } }
@Override public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException { //获取缓存 long cache = pStats.cacheSize; if(cache>0){ try { CacheInfo cacheInfo = new CacheInfo(); PackageManager pm = getPackageManager(); //添加缓存信息到ui界面 //获取包名 cacheInfo.packname = pStats.packageName; PackageInfo packInfo = pm.getPackageInfo(cacheInfo.packname, 0); cacheInfo.cachesize = cache; cacheInfo.appname = packInfo.applicationInfo.loadLabel(pm).toString(); cacheInfo.icon = packInfo.applicationInfo.loadIcon(pm); Message msg = Message.obtain(); msg.obj = cacheInfo; handler.sendMessage(msg); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } } }
@Override public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException { long cacheSize = pStats.cacheSize; if (cacheSize > 0) { CacheInfo cacheInfo = new CacheInfo(); String appName = packageInfo.applicationInfo.loadLabel(pm) .toString(); if (appName.isEmpty()) { cacheInfo.appName = packageInfo.packageName; } else { cacheInfo.appName = appName; } Drawable icon = packageInfo.applicationInfo.loadIcon(pm); if (icon == null) { cacheInfo.icon = getResources().getDrawable( R.drawable.ic_launcher); } else { cacheInfo.icon = icon; } cacheInfo.cacheSize = cacheSize; cacheInfos.add(cacheInfo); } }
/** * 返回用户已安装应用列表 */ public List<ApkModel> getAppList(Context context) { PackageManager pm = context.getPackageManager(); List<PackageInfo> packageInfos = pm.getInstalledPackages(0); List<ApkModel> appInfos = new ArrayList<>(); for (PackageInfo packageInfo : packageInfos) { ApplicationInfo app = packageInfo.applicationInfo; if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { // 非系统应用 PackageStats stats = new PackageStats(packageInfo.packageName); ApkModel appInfo = new ApkModel(); appInfo.packageName = packageInfo.packageName; appInfo.versionCode = packageInfo.versionCode; appInfo.versionName = packageInfo.versionName; appInfo.icon = app.loadIcon(pm); appInfo.name = app.loadLabel(pm).toString(); appInfo.cacheSize = stats.cacheSize; appInfo.dataSize = stats.dataSize; appInfos.add(appInfo); } } return appInfos; }
/*** �ص������� * @param pStatus ,�������ݷ�װ��PackageStats������ * @param succeeded ����ص��ɹ� */ @Override public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException { cachesize = pStats.cacheSize ; //�����С datasize = pStats.dataSize ; //���ݴ�С codesize = pStats.codeSize ; //Ӧ�ó����С totalsize = cachesize + datasize + codesize ; Log.i(TAG, "cachesize--->"+cachesize+" datasize---->"+datasize+ " codeSize---->"+codesize); Log.i(TAG, "Ŀǰ���߳�����" + Thread.currentThread().getName()); // ��ȡ��������ʾAlertDialog // ���Եõ�����δ�����߳���Binder_1,2,3�����ȣ�����Ҫ�������̵߳���showAlertDialog() runOnUiThread(new Runnable() { @Override public void run() { showAlertDialog(); } }); }
/** * 根据包名获取应用程序的体积信息 注意: 这个方法是一个异步的方法 程序的体积要花一定时间才能获取到. * * @param packname */ private void setAppSize(final String packname) { try { Method method = PackageManager.class.getMethod("getPackageSizeInfo", new Class[] { String.class, IPackageStatsObserver.class }); method.invoke(getPackageManager(), new Object[] { packname, new IPackageStatsObserver.Stub() { public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException { // 注意这个操作是一个异步的操作 long codesize = pStats.codeSize; long datasize = pStats.dataSize; Logger.i(TAG, packname); CacheInfo info = maps.get(packname); info.setDataSize(Formatter.formatFileSize(mApp, datasize)); info.setCodeSize(Formatter.formatFileSize(mApp, codesize)); maps.put(packname, info); } } }); } catch (Exception e) { e.printStackTrace(); } }
@Override public boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException { switch (code) { case INTERFACE_TRANSACTION: { reply.writeString(DESCRIPTOR); return true; } case FIRST_CALL_TRANSACTION: { data.enforceInterface(DESCRIPTOR); final PackageStats _arg0; if ((0 != data.readInt())) { _arg0 = PackageStats.CREATOR.createFromParcel(data); } else { _arg0 = null; } final boolean _arg1 = (0 != data.readInt()); this.onGetStatsCompleted(_arg0, _arg1); return true; } } return true; }
public FileSystemPackage( String name, String pkg, PackageStats stats, int flags, Long hackApkSize, int blockSize) { super(null, name); this.pkg = pkg; this.cacheSize = stats.cacheSize; this.dataSize = stats.dataSize; this.flags = flags | (hackApkSize != null ? SDCARD_FLAG : 0); this.dalvikCacheSize = guessDalvikCacheSize(); if (onSD()) { if (hackApkSize != null) { this.codeSize = hackApkSize.intValue(); } else { this.codeSize = stats.codeSize; } } else { this.codeSize = stats.codeSize - this.dalvikCacheSize; } }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override public void onGetStatsCompleted(PackageStats packageStats, boolean succeeded) throws RemoteException { mScanCount++; if (packageStats == null || !succeeded) { } else { mCallBack.onScanProgress((int)(mScanCount * 100 / mTotalCount)); CacheInfo cacheInfo = new CacheInfo(); cacheInfo.setPackageName(packageStats.packageName); cacheInfo.setCacheSize(packageStats.cacheSize); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { cacheInfo.setCacheSize(packageStats.cacheSize + packageStats.externalCacheSize); } cacheInfo.setChecked(true); ArrayList<CacheInfo> caches = CacheLab.get(mAppContext).getCaches(); if (cacheInfo.getCacheSize() != 0) { caches.add(cacheInfo); } } if (mScanCount == mTotalCount) { mCallBack.onScanFinish(); } }
public void onGetStatsCompleted( PackageStats pStats, boolean succeeded ) throws RemoteException { AppInfoHolder holder = appCache.appLookup.get( pStats.packageName ); if ( holder != null ) { synchronized ( appCache ) { holder.size = Formatter.formatFileSize( ac, pStats.codeSize ) + " + " //$NON-NLS-1$ + Formatter.formatFileSize( ac, pStats.dataSize ) + " (" //$NON-NLS-1$ + Formatter.formatFileSize( ac, pStats.cacheSize ) + ')'; holder.codeSize = pStats.codeSize; holder.dataSize = pStats.dataSize; holder.cacheSize = pStats.cacheSize; } } count.countDown( ); }
@Override public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException { String packageName = pStats.packageName; long cacheSize = pStats.cacheSize; long codeSize = pStats.codeSize; long dataSize = pStats.dataSize; // sb.delete(0, sb.length()); if (cacheSize > 0) { } }
private void getPackageInfo(Context context, String pkg) { PackageManager pm = context.getPackageManager(); try { Method method_getPackageSizeInfo = pm.getClass().getMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class); method_getPackageSizeInfo.invoke(pm, pkg, new IPackageStatsObserver.Stub() { @Override public IBinder asBinder() { log.d("asBinder"); return super.asBinder(); } @Override public void onGetStatsCompleted(PackageStats packageStats, boolean b) throws RemoteException { final StringBuilder sb = new StringBuilder("onGetStatsCompleted\n"); sb.append("packageName:").append(packageStats.packageName).append("\n"); sb.append("cacheSize:").append(packageStats.cacheSize).append("\n"); sb.append("dataSize:").append(packageStats.dataSize).append("\n"); sb.append("externalDataSize:").append(packageStats.externalDataSize).append("\n"); log.i(sb.toString()); mHandler.post(new Runnable() { @Override public void run() { mDataBinding.txtContent.setText(sb); } }); } }); } catch (Exception e) { e.printStackTrace(); } }
@Override public void onGetStatsCompleted(PackageStats pStats,boolean successd) throws RemoteException { long cachesize = pStats.cacheSize; if (cachesize >= 0) { CacheInfo cacheinfo = new CacheInfo(); cacheinfo.setPackagename(info.packageName); cacheinfo.setCachesize(cachesize); cacheinfo.setAppname(info.applicationInfo.loadLabel(packageManager).toString()); cacheinfo.setAppicon(info.applicationInfo.loadIcon(packageManager)); cacheInfos.add(cacheinfo); cacheMemory += cachesize; } }
public void onGetStatsCompleted(PackageStats stats, boolean succeeded) { long cachesize = stats.cacheSize;//缓存大小 /*long codesize = stats.codeSize;//应用程序的大小 long datasize = stats.dataSize;//数据大小 */ if (cachesize > 0) { String cache = Formatter.formatFileSize(ClearCacheActivity.this, cachesize); list.add(new CachInfo(stats.packageName, cache)); } /*String code = Formatter.formatFileSize(getActivity(), codesize); String data = Formatter.formatFileSize(getActivity(), datasize);*/ // System.out.println(stats.packageName+"cachesize:"+cache +" codesize:"+code+" datasize:"+data); }
/** * 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(); } }
/** * 返回用户已安装应用列表 */ public static List<AppEntity> getAppList(Context context) { PackageManager pm = context.getPackageManager(); List<PackageInfo> packageInfos = pm.getInstalledPackages(0); List<AppEntity> appInfos = new ArrayList<AppEntity>(); for (PackageInfo packageInfo : packageInfos) { ApplicationInfo app = packageInfo.applicationInfo; if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { // 非系统应用 File apkfile = new File(app.sourceDir); PackageStats stats = new PackageStats(packageInfo.packageName); AppEntity appInfo = new AppEntity(app.sourceDir); appInfo.setPackageName(packageInfo.packageName); appInfo.setVersionCode(packageInfo.versionCode); appInfo.setVersionName(packageInfo.versionName); appInfo.setUid(app.uid); appInfo.setIcon(app.loadIcon(pm)); appInfo.setAppName(app.loadLabel(pm).toString()); appInfo.setCacheSize(stats.cacheSize); appInfo.setDataSize(stats.dataSize); appInfos.add(appInfo); } } return appInfos; }
public static long getPkgSize(final Context context, String pkgName) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { long pkgSize=0; // getPackageSizeInfo是PackageManager中的一个private方法,所以需要通过反射的机制来调用 Method method = PackageManager.class.getMethod("getPackageSizeInfo", new Class[]{String.class, IPackageStatsObserver.class}); // 调用 getPackageSizeInfo 方法,需要两个参数:1、需要检测的应用包名;2、回调 method.invoke(context.getPackageManager(), new Object[]{ pkgName, new IPackageStatsObserver.Stub() { @Override public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException { // 子线程中默认无法处理消息循环,自然也就不能显示Toast,所以需要手动Looper一下 Looper.prepare(); // 从pStats中提取各个所需数据 // pkgSize= pStats.cacheSize+pStats.dataSize+pStats.codeSize; // Toast.makeText(context, // "缓存大小=" + Formatter.formatFileSize(context, pStats.cacheSize) + // "\n数据大小=" + Formatter.formatFileSize(context, pStats.dataSize) + // "\n程序大小=" + Formatter.formatFileSize(context, pStats.codeSize), // Toast.LENGTH_LONG).show(); // 遍历一次消息队列,弹出Toast Looper.loop(); } } }); return pkgSize; }
@Override public void onGetStatsCompleted(final PackageStats pStats, final boolean success) throws RemoteException { Timber.v("onGetStatsCompleted(): %s", success); App.HANDLER.post(new Runnable() { @Override public void run() { packageStatsListener.onPackageStats(pStats); } }); }
private long addPackage(List<AppsListItem> apps, PackageStats pStats, boolean succeeded) { long cacheSize = 0; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { cacheSize += pStats.cacheSize; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { cacheSize += pStats.externalCacheSize; } if (!succeeded || cacheSize <= 0) { return 0; } try { PackageManager packageManager = getPackageManager(); ApplicationInfo info = packageManager.getApplicationInfo(pStats.packageName, PackageManager.GET_META_DATA); apps.add(new AppsListItem(pStats.packageName, packageManager.getApplicationLabel(info).toString(), packageManager.getApplicationIcon(pStats.packageName), cacheSize)); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return cacheSize; }
@Override public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException { long cacheSize = pStats.cacheSize;//得到缓存大小 if (cacheSize > 0) {//没有缓存的就不处理了 String appName = installPackage.applicationInfo.loadLabel(packageManager).toString();//得到应用名称 Drawable icon = installPackage.applicationInfo.loadIcon(packageManager);//得到应用LOGO String packageName = installPackage.applicationInfo.packageName;//应用包名 CacheInfos cacheInfo = new CacheInfos(cacheSize, appName, packageName, icon); cacheInfos.add(cacheInfo);//增加到集合中 } }
public OCSSoftwares(Context ctx) { OCSLog ocslog = OCSLog.getInstance(); softs = new ArrayList<>(); PackageManager pm = ctx.getPackageManager(); List<PackageInfo> pis = ctx.getPackageManager().getInstalledPackages( PackageManager.GET_ACTIVITIES | PackageManager.GET_PROVIDERS); for (PackageInfo pi : pis) { // Exclude systeme softwares i required if (OCSSettings.getInstance(ctx).isSysHide() && (pi.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1) { continue; } OCSSoftware oSoft = new OCSSoftware(); try { PackageInfo lpInfo = pm.getPackageInfo(pi.packageName, PackageManager.GET_ACTIVITIES | PackageManager.GET_PROVIDERS); ocslog.debug("PKG name " + lpInfo.packageName); ocslog.debug("PKG version " + String.valueOf(lpInfo.versionCode)); ocslog.debug("PKG version name " + lpInfo.versionName); oSoft.setVersion(lpInfo.versionName); oSoft.setPublisher(lpInfo.packageName); } catch (NameNotFoundException e) { ocslog.error("Error :" + e.getMessage()); } PackageStats stats = new PackageStats(pi.packageName); ocslog.debug("PKG size " + String.valueOf(stats.codeSize)); ocslog.debug("PKG folder " + pi.applicationInfo.dataDir); oSoft.setFilesize(String.valueOf(stats.codeSize)); oSoft.setFolder(pi.applicationInfo.dataDir); if (pi.applicationInfo.name != null) { oSoft.setName(pi.applicationInfo.name); } else if (pi.applicationInfo.className != null) { oSoft.setName(pi.applicationInfo.className); } else { String v[] = oSoft.getPublisher().split("\\."); if (v.length > 0) { oSoft.setName(v[v.length - 1]); } else { oSoft.setName(oSoft.getPublisher()); } } ocslog.debug("PKG appname " + oSoft.getName()); if (Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO) { String datei = (String) DateFormat.format("MM/dd/yy mm:ss", pi.firstInstallTime); ocslog.debug("PKG INSTALL :" + datei); oSoft.setInstallDate(datei); } ProviderInfo[] provsi = pi.providers; if (provsi != null) { for (ProviderInfo aProvsi : provsi) { ocslog.debug("PKG Provider " + aProvsi.authority); if (aProvsi.descriptionRes != 0) { ocslog.debug("PKG Desc " + String.valueOf(aProvsi.descriptionRes)); } } if (provsi.length > 0) { oSoft.setPublisher(provsi[0].authority); } } softs.add(oSoft); } Properties sp = System.getProperties(); OCSSoftware jsoft = new OCSSoftware(); jsoft.setName(sp.getProperty("java.vm.name")); jsoft.setVersion(sp.getProperty("java.vm.version")); jsoft.setFolder(sp.getProperty("java.home")); jsoft.setPublisher(sp.getProperty("java.vm.vendor")); jsoft.setFilesize("n.a"); jsoft.setInstallDate("n.a."); softs.add(jsoft); }
public AppStatsImpl(PackageStats packageStats) { this.packageStats = packageStats; }
@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); }
void onPackageStats(PackageStats packageStats);
void onPackageStats(final PackageStats packageStats);