Search in sources :

Example 16 with PackageStats

use of android.content.pm.PackageStats in project android_frameworks_base by crdroidandroid.

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 17 with PackageStats

use of android.content.pm.PackageStats in project android_frameworks_base by crdroidandroid.

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 18 with PackageStats

use of android.content.pm.PackageStats in project android_frameworks_base by crdroidandroid.

the class DiskStatsLoggingServiceTest method testPopulatedLogTask.

@Test
public void testPopulatedLogTask() throws Exception {
    // Write data to directories.
    writeDataToFile(mDownloads.newFile(), "lol");
    writeDataToFile(mRootDirectory.newFile("test.jpg"), "1234");
    writeDataToFile(mRootDirectory.newFile("test.mp4"), "12345");
    writeDataToFile(mRootDirectory.newFile("test.mp3"), "123456");
    writeDataToFile(mRootDirectory.newFile("test.whatever"), "1234567");
    // Write apps.
    ArrayList<PackageStats> apps = new ArrayList<>();
    PackageStats testApp = new PackageStats("com.test.app");
    testApp.dataSize = 5L;
    testApp.cacheSize = 55L;
    testApp.codeSize = 10L;
    apps.add(testApp);
    when(mCollector.getPackageStats(anyInt())).thenReturn(apps);
    LogRunnable task = new LogRunnable();
    task.setAppCollector(mCollector);
    task.setDownloadsDirectory(mDownloads.getRoot());
    task.setRootDirectory(mRootDirectory.getRoot());
    task.setLogOutputFile(mInputFile);
    task.setSystemSize(10L);
    task.run();
    JSONObject json = getJsonOutput();
    assertThat(json.getLong(DiskStatsFileLogger.PHOTOS_KEY)).isEqualTo(4L);
    assertThat(json.getLong(DiskStatsFileLogger.VIDEOS_KEY)).isEqualTo(5L);
    assertThat(json.getLong(DiskStatsFileLogger.AUDIO_KEY)).isEqualTo(6L);
    assertThat(json.getLong(DiskStatsFileLogger.DOWNLOADS_KEY)).isEqualTo(3L);
    assertThat(json.getLong(DiskStatsFileLogger.SYSTEM_KEY)).isEqualTo(10L);
    assertThat(json.getLong(DiskStatsFileLogger.MISC_KEY)).isEqualTo(7L);
    assertThat(json.getLong(DiskStatsFileLogger.APP_SIZE_AGG_KEY)).isEqualTo(15L);
    assertThat(json.getLong(DiskStatsFileLogger.APP_CACHE_AGG_KEY)).isEqualTo(55L);
    assertThat(json.getJSONArray(DiskStatsFileLogger.PACKAGE_NAMES_KEY).length()).isEqualTo(1L);
    assertThat(json.getJSONArray(DiskStatsFileLogger.APP_SIZES_KEY).length()).isEqualTo(1L);
    assertThat(json.getJSONArray(DiskStatsFileLogger.APP_CACHES_KEY).length()).isEqualTo(1L);
}
Also used : JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) PackageStats(android.content.pm.PackageStats) LogRunnable(com.android.server.storage.DiskStatsLoggingService.LogRunnable) Test(org.junit.Test)

Example 19 with PackageStats

use of android.content.pm.PackageStats in project android_frameworks_base by crdroidandroid.

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 20 with PackageStats

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("卸载下列软件,会影响正常使用");
    }
    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();
}
Also used : PackageInfo(android.content.pm.PackageInfo) Drawable(android.graphics.drawable.Drawable) ArrayList(java.util.ArrayList) RemoteException(android.os.RemoteException) AppInfo(com.yzy.supercleanmaster.model.AppInfo) SoftwareAdapter(com.yzy.supercleanmaster.adapter.SoftwareAdapter) PackageManager(android.content.pm.PackageManager) PackageStats(android.content.pm.PackageStats) ArrayList(java.util.ArrayList) List(java.util.List)

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