Search in sources :

Example 26 with PackageStats

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);
}
Also used : JSONArray(org.json.JSONArray) PackageStats(android.content.pm.PackageStats) Map(java.util.Map) ArrayMap(android.util.ArrayMap)

Example 27 with PackageStats

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;
}
Also used : PackageStats(android.content.pm.PackageStats) ArrayMap(android.util.ArrayMap)

Example 28 with PackageStats

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);
}
Also used : JSONObject(org.json.JSONObject) PackageStats(android.content.pm.PackageStats) JSONArray(org.json.JSONArray) Test(org.junit.Test)

Example 29 with PackageStats

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);
}
Also used : JSONObject(org.json.JSONObject) PackageStats(android.content.pm.PackageStats) JSONArray(org.json.JSONArray) Test(org.junit.Test)

Example 30 with PackageStats

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;
}
Also used : PackageStats(android.content.pm.PackageStats) ArrayMap(android.util.ArrayMap)

Aggregations

PackageStats (android.content.pm.PackageStats)43 Test (org.junit.Test)20 IPackageStatsObserver (android.content.pm.IPackageStatsObserver)13 JSONArray (org.json.JSONArray)12 JSONObject (org.json.JSONObject)12 ArrayList (java.util.ArrayList)11 RemoteException (android.os.RemoteException)8 VolumeInfo (android.os.storage.VolumeInfo)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 PackageManager (android.content.pm.PackageManager)6 ArrayMap (android.util.ArrayMap)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 ApplicationInfo (android.content.pm.ApplicationInfo)5 Message (android.os.Message)5 PackageInfo (android.content.pm.PackageInfo)4 UserInfo (android.content.pm.UserInfo)3 Handler (android.os.Handler)3 ArraySet (android.util.ArraySet)3 LogRunnable (com.android.server.storage.DiskStatsLoggingService.LogRunnable)3 Method (java.lang.reflect.Method)3