use of android.content.pm.PackageStats in project android_packages_apps_Settings by LineageOS.
the class InstalledAppDetails method refreshSizeInfo.
/*
* Private method to handle get size info notification from observer when
* the async operation from PackageManager is complete. The current user data
* info has to be refreshed in the manage applications screen as well as the current screen.
*/
private void refreshSizeInfo(Message msg) {
boolean changed = false;
PackageStats newPs = msg.getData().getParcelable(ATTR_PACKAGE_STATS);
long newTot = newPs.cacheSize + newPs.codeSize + newPs.dataSize;
if (mSizeInfo == null) {
mSizeInfo = newPs;
String str = getSizeStr(newTot);
mTotalSize.setText(str);
mAppSize.setText(getSizeStr(newPs.codeSize));
mDataSize.setText(getSizeStr(newPs.dataSize));
mCacheSize.setText(getSizeStr(newPs.cacheSize));
} else {
long oldTot = mSizeInfo.cacheSize + mSizeInfo.codeSize + mSizeInfo.dataSize;
if (newTot != oldTot) {
String str = getSizeStr(newTot);
mTotalSize.setText(str);
changed = true;
}
if (newPs.codeSize != mSizeInfo.codeSize) {
mAppSize.setText(getSizeStr(newPs.codeSize));
changed = true;
}
if (newPs.dataSize != mSizeInfo.dataSize) {
mDataSize.setText(getSizeStr(newPs.dataSize));
changed = true;
}
if (newPs.cacheSize != mSizeInfo.cacheSize) {
mCacheSize.setText(getSizeStr(newPs.cacheSize));
changed = true;
}
if (changed) {
mSizeInfo = newPs;
}
}
// If data size is zero disable clear data button
if (newPs.dataSize == 0) {
mClearDataButton.setEnabled(false);
}
long data = mSizeInfo.dataSize;
refreshCacheInfo(newPs.cacheSize);
}
use of android.content.pm.PackageStats in project superCleanMaster by joyoyao.
the class Utils method getPkgSize.
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;
}
use of android.content.pm.PackageStats in project superCleanMaster by joyoyao.
the class SoftwareManageFragment method fillData.
private void fillData() {
if (position == 0) {
topText.setText("");
} else {
topText.setText(R.string.Attention);
}
task = new AsyncTask<Void, Integer, List<AppInfo>>() {
private int mAppCount = 0;
@Override
protected List<AppInfo> doInBackground(Void... params) {
PackageManager pm = mContext.getPackageManager();
List<PackageInfo> packInfos = pm.getInstalledPackages(0);
publishProgress(0, packInfos.size());
List<AppInfo> appinfos = new ArrayList<AppInfo>();
for (PackageInfo packInfo : packInfos) {
publishProgress(++mAppCount, packInfos.size());
final AppInfo appInfo = new AppInfo();
Drawable appIcon = packInfo.applicationInfo.loadIcon(pm);
appInfo.setAppIcon(appIcon);
int flags = packInfo.applicationInfo.flags;
int uid = packInfo.applicationInfo.uid;
appInfo.setUid(uid);
if ((flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
// 系统应用
appInfo.setUserApp(false);
} else {
// 用户应用
appInfo.setUserApp(true);
}
if ((flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
appInfo.setInRom(false);
} else {
appInfo.setInRom(true);
}
String appName = packInfo.applicationInfo.loadLabel(pm).toString();
appInfo.setAppName(appName);
String packname = packInfo.packageName;
appInfo.setPackname(packname);
String version = packInfo.versionName;
appInfo.setVersion(version);
try {
mGetPackageSizeInfoMethod.invoke(mContext.getPackageManager(), new Object[] { packname, new IPackageStatsObserver.Stub() {
@Override
public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException {
synchronized (appInfo) {
appInfo.setPkgSize(pStats.cacheSize + pStats.codeSize + pStats.dataSize);
}
}
} });
} catch (Exception e) {
}
appinfos.add(appInfo);
}
return appinfos;
}
@Override
protected void onProgressUpdate(Integer... values) {
try {
mProgressBarText.setText(getString(R.string.scanning_m_of_n, values[0], values[1]));
} catch (Exception e) {
}
}
@Override
protected void onPreExecute() {
try {
showProgressBar(true);
mProgressBarText.setText(R.string.scanning);
} catch (Exception e) {
}
// loading.setVisibility(View.VISIBLE);
super.onPreExecute();
}
@Override
protected void onPostExecute(List<AppInfo> result) {
super.onPostExecute(result);
try {
showProgressBar(false);
userAppInfos = new ArrayList<>();
systemAppInfos = new ArrayList<>();
long allSize = 0;
for (AppInfo a : result) {
if (a.isUserApp()) {
allSize += a.getPkgSize();
userAppInfos.add(a);
} else {
systemAppInfos.add(a);
}
}
if (position == 0) {
topText.setText(getString(R.string.software_top_text, userAppInfos.size(), StorageUtil.convertStorage(allSize)));
mAutoStartAdapter = new SoftwareAdapter(mContext, userAppInfos);
listview.setAdapter(mAutoStartAdapter);
} else {
mAutoStartAdapter = new SoftwareAdapter(mContext, systemAppInfos);
listview.setAdapter(mAutoStartAdapter);
}
} catch (Exception e) {
}
}
};
task.execute();
}
use of android.content.pm.PackageStats in project robolectric by robolectric.
the class ShadowApplicationPackageManager method getPackageSizeInfoAsUser.
@Implementation(minSdk = N)
protected void getPackageSizeInfoAsUser(Object pkgName, Object uid, final Object observer) {
final PackageStats packageStats = packageStatsMap.get((String) pkgName);
new Handler(Looper.getMainLooper()).post(() -> {
try {
((IPackageStatsObserver) observer).onGetStatsCompleted(packageStats, packageStats != null);
} catch (RemoteException remoteException) {
remoteException.rethrowFromSystemServer();
}
});
}
use of android.content.pm.PackageStats in project robolectric by robolectric.
the class ShadowApplicationPackageManager method getPackageSizeInfo.
@Implementation(minSdk = JELLY_BEAN_MR1, maxSdk = M)
protected void getPackageSizeInfo(Object pkgName, Object uid, final Object observer) {
final PackageStats packageStats = packageStatsMap.get((String) pkgName);
new Handler(Looper.getMainLooper()).post(() -> {
try {
((IPackageStatsObserver) observer).onGetStatsCompleted(packageStats, packageStats != null);
} catch (RemoteException remoteException) {
remoteException.rethrowFromSystemServer();
}
});
}
Aggregations