android.app.usage.StorageStats Java Examples

The following examples show how to use android.app.usage.StorageStats. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AppCollector.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void handleMessage(Message msg) {
    switch (msg.what) {
        case MSG_START_LOADING_SIZES: {
            List<PackageStats> stats = new ArrayList<>();
            List<UserInfo> users = mUm.getUsers();
            for (int userCount = 0, userSize = users.size();
                    userCount < userSize; userCount++) {
                UserInfo user = users.get(userCount);
                final List<ApplicationInfo> apps = mPm.getInstalledApplicationsAsUser(
                        PackageManager.MATCH_DISABLED_COMPONENTS, user.id);

                for (int appCount = 0, size = apps.size(); appCount < size; appCount++) {
                    ApplicationInfo app = apps.get(appCount);
                    if (!Objects.equals(app.volumeUuid, mVolume.getFsUuid())) {
                        continue;
                    }

                    try {
                        StorageStats storageStats =
                                mStorageStatsManager.queryStatsForPackage(app.storageUuid,
                                        app.packageName, user.getUserHandle());
                        PackageStats packageStats = new PackageStats(app.packageName,
                                user.id);
                        packageStats.cacheSize = storageStats.getCacheBytes();
                        packageStats.codeSize = storageStats.getAppBytes();
                        packageStats.dataSize = storageStats.getDataBytes();
                        stats.add(packageStats);
                    } catch (NameNotFoundException | IOException e) {
                        Log.e(TAG, "An exception occurred while fetching app size", e);
                    }
                }
            }

            mStats.complete(stats);
        }
    }
}
 
Example #2
Source File: AppSizeUtil.java    From AndroidGodEye with Apache License 2.0 5 votes vote down vote up
/**
 * 获取应用的大小
 */
@RequiresApi(api = Build.VERSION_CODES.O)
private static void getAppSizeAboveO(Context context, @NonNull OnGetSizeListener listener) {
    StorageStatsManager storageStatsManager = (StorageStatsManager) context
            .getSystemService(Context.STORAGE_STATS_SERVICE);
    StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
    // 获取所有应用的StorageVolume列表
    List<StorageVolume> storageVolumes = storageManager.getStorageVolumes();
    for (StorageVolume item : storageVolumes) {
        String uuidStr = item.getUuid();
        UUID uuid;
        if (uuidStr == null) {
            uuid = StorageManager.UUID_DEFAULT;
        } else {
            uuid = UUID.fromString(uuidStr);
        }
        int uid = getUid(context, context.getPackageName());
        // 通过包名获取uid
        StorageStats storageStats;
        try {
            storageStats = storageStatsManager.queryStatsForUid(uuid, uid);
            AppSizeInfo ctAppSizeInfo = new AppSizeInfo();
            ctAppSizeInfo.cacheSize = storageStats.getCacheBytes();
            ctAppSizeInfo.dataSize = storageStats.getDataBytes();
            ctAppSizeInfo.codeSize = storageStats.getAppBytes();
            listener.onGetSize(ctAppSizeInfo);
        } catch (IOException e) {
            listener.onError(e);
        }
    }
}