Search in sources :

Example 1 with PackageStats

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

the class PackageManagerService method getPackageSizeInfo.

public void getPackageSizeInfo(final String packageName, int userHandle, final IPackageStatsObserver observer) {
    mContext.enforceCallingOrSelfPermission(android.Manifest.permission.GET_PACKAGE_SIZE, null);
    PackageStats stats = new PackageStats(packageName, userHandle);
    /*
         * Queue up an async operation since the package measurement may take a
         * little while.
         */
    Message msg = mHandler.obtainMessage(INIT_COPY);
    msg.obj = new MeasureParams(stats, observer);
    mHandler.sendMessage(msg);
}
Also used : Message(android.os.Message) PackageStats(android.content.pm.PackageStats)

Example 2 with PackageStats

use of android.content.pm.PackageStats in project superCleanMaster by joyoyao.

the class CacheInfoProvider method initDataSize.

/**
	 * 通过AIDL的方法来获取到应用的缓存信息,getPackageSizeInfo是PackageManager里面的一个私有方法来的
	 * 我们通过反射就可以调用到它的了,但是这个方法里面会传递一个IPackageStatsObserver.Stub的对象
	 * 里面就可能通过AIDL来获取我们想要的信息了
	 * 
	 * 因为这样的调用是异步的,所以当我们完成获取完这些信息之后,我们就通过handler来发送一个消息
	 * 来通知我们的应用,通过getCacheInfos来获取到我们的Vector
	 * 
	 * 为什么要用Vector呢,因为下面的方法是异步的,也就是有可能是多线程操作,所以我们就用了线程安全的Vector
	 * 
	 * @param cacheInfo
	 * @param position
	 */
private void initDataSize(final CacheInfo cacheInfo, final int position) {
    try {
        Method method = PackageManager.class.getMethod("getPackageSizeInfo", new Class[] { String.class, IPackageStatsObserver.class });
        method.invoke(packageManager, new Object[] { cacheInfo.getPackageName(), new IPackageStatsObserver.Stub() {

            @Override
            public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException {
                System.out.println("onGetStatsCompleted" + position);
                long cacheSize = pStats.cacheSize;
                long codeSize = pStats.codeSize;
                long dataSize = pStats.dataSize;
                cacheInfo.setCacheSize(TextFormater.dataSizeFormat(cacheSize));
                cacheInfo.setCodeSize(TextFormater.dataSizeFormat(codeSize));
                cacheInfo.setDataSize(TextFormater.dataSizeFormat(dataSize));
                cacheInfos.add(cacheInfo);
                if (position == (size - 1)) {
                    // 当完全获取完信息之后,发送一个成功的消息
                    // 1对应的就是CacheClearActivity里面的FINISH
                    handler.sendEmptyMessage(1);
                }
            }
        } });
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : IPackageStatsObserver(android.content.pm.IPackageStatsObserver) PackageStats(android.content.pm.PackageStats) Method(java.lang.reflect.Method) RemoteException(android.os.RemoteException) RemoteException(android.os.RemoteException)

Example 3 with PackageStats

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

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

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

the class DiskStatsFileLoggerTest method testAppsReported.

@Test
public void testAppsReported() throws Exception {
    PackageStats firstPackage = new PackageStats("com.test.app");
    firstPackage.codeSize = 100;
    firstPackage.dataSize = 1000;
    firstPackage.cacheSize = 20;
    mPackages.add(firstPackage);
    PackageStats secondPackage = new PackageStats("com.test.app2");
    secondPackage.codeSize = 10;
    secondPackage.dataSize = 1;
    secondPackage.cacheSize = 2;
    mPackages.add(secondPackage);
    DiskStatsFileLogger logger = new DiskStatsFileLogger(mMainResult, mDownloadsResult, mPackages, 0L);
    logger.dumpToFile(mOutputFile);
    JSONObject output = getOutputFileAsJson();
    assertThat(output.getLong(DiskStatsFileLogger.APP_SIZE_AGG_KEY)).isEqualTo(1111);
    assertThat(output.getLong(DiskStatsFileLogger.APP_CACHE_AGG_KEY)).isEqualTo(22);
    JSONArray packageNames = output.getJSONArray(DiskStatsFileLogger.PACKAGE_NAMES_KEY);
    assertThat(packageNames.length()).isEqualTo(2);
    JSONArray appSizes = output.getJSONArray(DiskStatsFileLogger.APP_SIZES_KEY);
    assertThat(appSizes.length()).isEqualTo(2);
    JSONArray cacheSizes = output.getJSONArray(DiskStatsFileLogger.APP_CACHES_KEY);
    assertThat(cacheSizes.length()).isEqualTo(2);
    // We need to do this crazy Set over this because the DiskStatsFileLogger provides no
    // guarantee of the ordering of the apps in its output. By using a set, we avoid any order
    // problems.
    ArraySet<AppSizeGrouping> apps = new ArraySet<>();
    for (int i = 0; i < packageNames.length(); i++) {
        AppSizeGrouping app = new AppSizeGrouping(packageNames.getString(i), appSizes.getLong(i), cacheSizes.getLong(i));
        apps.add(app);
    }
    assertThat(apps).containsAllOf(new AppSizeGrouping("com.test.app", 1100, 20), new AppSizeGrouping("com.test.app2", 11, 2));
}
Also used : ArraySet(android.util.ArraySet) JSONObject(org.json.JSONObject) PackageStats(android.content.pm.PackageStats) JSONArray(org.json.JSONArray) Test(org.junit.Test)

Example 5 with PackageStats

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

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)

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