use of android.content.pm.PackageStats in project android_frameworks_base by DirtyUnicorns.
the class DiskStatsFileLogger method addAppsToJson.
private void addAppsToJson(JSONObject json) throws JSONException {
JSONArray names = new JSONArray();
JSONArray appSizeList = new JSONArray();
JSONArray cacheSizeList = new JSONArray();
long appSizeSum = 0L;
long cacheSizeSum = 0L;
boolean isExternal = Environment.isExternalStorageEmulated();
for (Map.Entry<String, PackageStats> entry : mergePackagesAcrossUsers().entrySet()) {
PackageStats stat = entry.getValue();
long appSize = stat.codeSize + stat.dataSize;
long cacheSize = stat.cacheSize;
if (isExternal) {
appSize += stat.externalCodeSize + stat.externalDataSize;
cacheSize += stat.externalCacheSize;
}
appSizeSum += appSize;
cacheSizeSum += cacheSize;
names.put(stat.packageName);
appSizeList.put(appSize);
cacheSizeList.put(cacheSize);
}
json.put(PACKAGE_NAMES_KEY, names);
json.put(APP_SIZES_KEY, appSizeList);
json.put(APP_CACHES_KEY, cacheSizeList);
json.put(APP_SIZE_AGG_KEY, appSizeSum);
json.put(APP_CACHE_AGG_KEY, cacheSizeSum);
}
use of android.content.pm.PackageStats in project android_frameworks_base by DirtyUnicorns.
the class DiskStatsFileLogger method mergePackagesAcrossUsers.
/**
* A given package may exist for multiple users with distinct sizes. This function merges
* the duplicated packages together and sums up their sizes to get the actual totals for the
* package.
* @return A mapping of package name to merged package stats.
*/
private ArrayMap<String, PackageStats> mergePackagesAcrossUsers() {
ArrayMap<String, PackageStats> packageMap = new ArrayMap<>();
for (PackageStats stat : mPackageStats) {
PackageStats existingStats = packageMap.get(stat.packageName);
if (existingStats != null) {
existingStats.cacheSize += stat.cacheSize;
existingStats.codeSize += stat.codeSize;
existingStats.dataSize += stat.dataSize;
existingStats.externalCacheSize += stat.externalCacheSize;
existingStats.externalCodeSize += stat.externalCodeSize;
existingStats.externalDataSize += stat.externalDataSize;
} else {
packageMap.put(stat.packageName, new PackageStats(stat));
}
}
return packageMap;
}
use of android.content.pm.PackageStats in project android_frameworks_base by DirtyUnicorns.
the class DiskStatsFileLoggerTest method testDuplicatePackageNameIsMergedAcrossMultipleUsers.
@Test
public void testDuplicatePackageNameIsMergedAcrossMultipleUsers() throws Exception {
PackageStats app = new PackageStats("com.test.app");
app.dataSize = 1000;
app.externalDataSize = 1000;
app.cacheSize = 20;
app.userHandle = 0;
mPackages.add(app);
PackageStats secondApp = new PackageStats("com.test.app");
secondApp.dataSize = 100;
secondApp.externalDataSize = 100;
secondApp.cacheSize = 2;
secondApp.userHandle = 1;
mPackages.add(secondApp);
DiskStatsFileLogger logger = new DiskStatsFileLogger(mMainResult, mDownloadsResult, mPackages, 0L);
logger.dumpToFile(mOutputFile);
JSONObject output = getOutputFileAsJson();
assertThat(output.getLong(DiskStatsFileLogger.APP_SIZE_AGG_KEY)).isEqualTo(2200);
assertThat(output.getLong(DiskStatsFileLogger.APP_CACHE_AGG_KEY)).isEqualTo(22);
JSONArray packageNames = output.getJSONArray(DiskStatsFileLogger.PACKAGE_NAMES_KEY);
assertThat(packageNames.length()).isEqualTo(1);
assertThat(packageNames.getString(0)).isEqualTo("com.test.app");
JSONArray appSizes = output.getJSONArray(DiskStatsFileLogger.APP_SIZES_KEY);
assertThat(appSizes.length()).isEqualTo(1);
assertThat(appSizes.getLong(0)).isEqualTo(2200);
JSONArray cacheSizes = output.getJSONArray(DiskStatsFileLogger.APP_CACHES_KEY);
assertThat(cacheSizes.length()).isEqualTo(1);
assertThat(cacheSizes.getLong(0)).isEqualTo(22);
}
use of android.content.pm.PackageStats in project android_frameworks_base by DirtyUnicorns.
the class DiskStatsFileLoggerTest method testEmulatedExternalStorageCounted.
@Test
public void testEmulatedExternalStorageCounted() throws Exception {
PackageStats app = new PackageStats("com.test.app");
app.dataSize = 1000;
app.externalDataSize = 1000;
app.cacheSize = 20;
mPackages.add(app);
DiskStatsFileLogger logger = new DiskStatsFileLogger(mMainResult, mDownloadsResult, mPackages, 0L);
logger.dumpToFile(mOutputFile);
JSONObject output = getOutputFileAsJson();
JSONArray appSizes = output.getJSONArray(DiskStatsFileLogger.APP_SIZES_KEY);
assertThat(appSizes.length()).isEqualTo(1);
assertThat(appSizes.getLong(0)).isEqualTo(2000);
}
use of android.content.pm.PackageStats in project android_frameworks_base by ResurrectionRemix.
the class DiskStatsFileLogger method mergePackagesAcrossUsers.
/**
* A given package may exist for multiple users with distinct sizes. This function merges
* the duplicated packages together and sums up their sizes to get the actual totals for the
* package.
* @return A mapping of package name to merged package stats.
*/
private ArrayMap<String, PackageStats> mergePackagesAcrossUsers() {
ArrayMap<String, PackageStats> packageMap = new ArrayMap<>();
for (PackageStats stat : mPackageStats) {
PackageStats existingStats = packageMap.get(stat.packageName);
if (existingStats != null) {
existingStats.cacheSize += stat.cacheSize;
existingStats.codeSize += stat.codeSize;
existingStats.dataSize += stat.dataSize;
existingStats.externalCacheSize += stat.externalCacheSize;
existingStats.externalCodeSize += stat.externalCodeSize;
existingStats.externalDataSize += stat.externalDataSize;
} else {
packageMap.put(stat.packageName, new PackageStats(stat));
}
}
return packageMap;
}
Aggregations