This tutorial is about getting internal cache size for all applications. There is no such API is available in android because of Security reasons.
So we will get cache size of all apps with Reflection.
Here we start:-
1. Create Project.
2. Create package in root folder with name android.content.pm.
3. Create aidl file int this package with name IPackageStatsObserver.aidl.
4. Paste below code to IPackageStatsObserver.aidl.
package android.content.pm;
import android.content.pm.PackageStats;
oneway interface IPackageStatsObserver {
void onGetStatsCompleted(in android.content.pm.PackageStats pStats, boolean succeeded);
}
5. Create PackageStats.aidl in same package.
6. Write below code to this file.
package android.content.pm;
parcelable PackageStats;
7. Create IDataStatus.aidl in same package.
8. Write below code to this file.
public interface IDataStatus {
public void onStatusListner(String msg);
}
9. Create model class with name App.
10. Create instance variable to App.
private String name;
private Drawable icon;
private String packageName;
private boolean enable;
private String apkSize;
private String versionName;
private long cacheSize;
private long dataSize;
11. Write Getter/Setter for same.
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Drawable getIcon() {
return icon;
}
public void setIcon(Drawable icon) {
this.icon = icon;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public boolean getEnable() {
return enable;
}
public void setEnable(boolean enable) {
this.enable = enable;
}
public String getApkSize() {
return apkSize;
}
public void setApkSize(String apkSize) {
this.apkSize = apkSize;
}
public String getVersionName() {
return versionName;
}
public void setVersionName(String versionName) {
this.versionName = versionName;
}
public long getCacheSize() {
return cacheSize;
}
public void setCacheSize(long cacheSize) {
this.cacheSize = cacheSize;
}
public long getDataSize() {
return dataSize;
}
public void setDataSize(long dataSize) {
this.dataSize = dataSize;
}
12. Open Activity class.
13. Write below Method to get installed Apps.
private ArrayList<App> getInstalledApps(boolean getSysPackages) {
ArrayList<App> applist = new ArrayList<App>();
List<PackageInfo> packs = mActivity.getPackageManager()
.getInstalledPackages(0);
try {
app_labels = new String[packs.size()];
} catch (Exception e) {
Toast.makeText(mActivity.getApplicationContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
if ((!getSysPackages) && (p.versionName == null)) {
continue;
}
App newInfo = new App();
newInfo.appname = p.applicationInfo.loadLabel(
mActivity.getPackageManager()).toString();
newInfo.pname = p.packageName;
newInfo.datadir = p.applicationInfo.dataDir;
newInfo.versionName = p.versionName;
newInfo.versionCode = p.versionCode;
newInfo.icon = p.applicationInfo.loadIcon(mActivity
.getPackageManager());
applist.add(newInfo);
}
return applist;
}
14. Write instance variable to activity.
private ArrayList<AppDetails.PackageInfoStruct> res;
public static final int FETCH_PACKAGE_SIZE_COMPLETED = 100;
public static final int ALL_PACAGE_SIZE_COMPLETED = 200;
15. Create inner class in your activity class.
public class cachePackState extends IPackageStatsObserver.Stub {
@Override
public void onGetStatsCompleted(PackageStats pStats, boolean succeeded)
throws RemoteException {
App app = new App();
app.setPackageName(pStats.packageName);
app.setDataSize(pStats.dataSize);
app.setCacheSize(pStats.cacheSize);
// app object has cache and data size of app with package name
Log.d("Package Size", pStats.packageName + "");
Log.i("Cache Size", pStats.packageName + " " + pStats.cacheSize + "");
Log.w("Data Size", pStats.dataSize + "");
packageSize = packageSize + pStats.cacheSize;
Log.v("Total Cache Size", " " + pStats.cacheSize);
handle.sendEmptyMessage(FETCH_PACKAGE_SIZE_COMPLETED);
}
}
16. Create initCache() and code the below.:-
for (int m = 0; m < getPackages().size(); m++) {
PackageManager pm = getPackageManager();
Method getPackageSizeInfo;
try {
getPackageSizeInfo = pm.getClass().getMethod(
"getPackageSizeInfo", String.class,
IPackageStatsObserver.class);
getPackageSizeInfo.invoke(pm, res.get(m).pname,
new cachePackState());
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
handle.sendEmptyMessage(ALL_PACAGE_SIZE_COMPLETED);
17. Create Handler and initialize it.
private Handler handle = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case FETCH_PACKAGE_SIZE_COMPLETED:
break;
case ALL_PACAGE_SIZE_COMPLETED:
// All apps cache fetched.
break;
default:
break;
}
}
};
18. Call initCache() from your activity onCreate().
Happy Coding :D
0 Comment(s)