android.app.usage.StorageStatsManager Java Examples

The following examples show how to use android.app.usage.StorageStatsManager. 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: FileCollector.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the file categorization result for the primary internal storage UUID.
 *
 * @param context
 */
public static MeasurementResult getMeasurementResult(Context context) {
    MeasurementResult result = new MeasurementResult();
    StorageStatsManager ssm =
            (StorageStatsManager) context.getSystemService(Context.STORAGE_STATS_SERVICE);
    ExternalStorageStats stats = null;
    try {
        stats =
                ssm.queryExternalStatsForUser(
                        StorageManager.UUID_PRIVATE_INTERNAL,
                        UserHandle.of(context.getUserId()));
        result.imagesSize = stats.getImageBytes();
        result.videosSize = stats.getVideoBytes();
        result.audioSize = stats.getAudioBytes();
        result.miscSize =
                stats.getTotalBytes()
                        - result.imagesSize
                        - result.videosSize
                        - result.audioSize;
    } catch (IOException e) {
        throw new IllegalStateException("Could not query storage");
    }

    return result;
}
 
Example #2
Source File: MemoryInfo.java    From MobileInfo with Apache License 2.0 6 votes vote down vote up
/**
 * API 26 android O
 * 获取总共容量大小,包括系统大小
 */
@SuppressLint("NewApi")
public static long getTotalSize(Context context, String fsUuid) {
    try {
        UUID id;
        if (fsUuid == null) {
            id = StorageManager.UUID_DEFAULT;
        } else {
            id = UUID.fromString(fsUuid);
        }
        StorageStatsManager stats = context.getSystemService(StorageStatsManager.class);
        return stats.getTotalBytes(id);
    } catch (NoSuchFieldError | NoClassDefFoundError | NullPointerException | IOException e) {
        e.printStackTrace();
        return -1;
    }
}
 
Example #3
Source File: StorageActivity.java    From AndroidDemo with MIT License 6 votes vote down vote up
/**
 * API 26 android O
 * 获取总共容量大小,包括系统大小
 */
@RequiresApi(api = 26)
public long getTotalSize(String fsUuid) {
    try {
        UUID id;
        if (fsUuid == null) {
            id = StorageManager.UUID_DEFAULT;
        } else {
            id = UUID.fromString(fsUuid);
        }
        StorageStatsManager stats = (StorageStatsManager) getSystemService(Context.STORAGE_STATS_SERVICE);
        return stats.getTotalBytes(id);
    } catch (NoSuchFieldError | NoClassDefFoundError | NullPointerException | IOException e) {
        e.printStackTrace();
        ToastUtils.show(this, "获取存储大小失败");
        return -1;
    }
}
 
Example #4
Source File: StorageQueryUtil.java    From AndroidDemo with MIT License 6 votes vote down vote up
/**
 * API 26 android O
 * 获取总共容量大小,包括系统大小
 */
@RequiresApi(Build.VERSION_CODES.O)
public static long getTotalSize(Context context, String fsUuid) {
    try {
        UUID id;
        if (fsUuid == null) {
            id = StorageManager.UUID_DEFAULT;
        } else {
            id = UUID.fromString(fsUuid);
        }
        StorageStatsManager stats = context.getSystemService(StorageStatsManager.class);
        return stats.getTotalBytes(id);
    } catch (NoSuchFieldError | NoClassDefFoundError | NullPointerException | IOException e) {
        e.printStackTrace();
        return -1;
    }
}
 
Example #5
Source File: StorageManagerService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public long getCacheQuotaBytes(String volumeUuid, int uid) {
    if (uid != Binder.getCallingUid()) {
        mContext.enforceCallingPermission(android.Manifest.permission.STORAGE_INTERNAL, TAG);
    }
    final long token = Binder.clearCallingIdentity();
    final StorageStatsManager stats = mContext.getSystemService(StorageStatsManager.class);
    try {
        return stats.getCacheQuotaBytes(volumeUuid, uid);
    } finally {
        Binder.restoreCallingIdentity(token);
    }
}
 
Example #6
Source File: StorageManagerService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public long getCacheSizeBytes(String volumeUuid, int uid) {
    if (uid != Binder.getCallingUid()) {
        mContext.enforceCallingPermission(android.Manifest.permission.STORAGE_INTERNAL, TAG);
    }
    final long token = Binder.clearCallingIdentity();
    try {
        return mContext.getSystemService(StorageStatsManager.class)
                .queryStatsForUid(volumeUuid, uid).getCacheBytes();
    } catch (IOException e) {
        throw new ParcelableException(e);
    } finally {
        Binder.restoreCallingIdentity(token);
    }
}
 
Example #7
Source File: AppCollector.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Constrcuts a new AppCollector which runs on the provided volume.
 * @param context Android context used to get
 * @param volume Volume to check for apps.
 */
public AppCollector(Context context, @NonNull VolumeInfo volume) {
    Preconditions.checkNotNull(volume);

    mBackgroundHandler = new BackgroundHandler(BackgroundThread.get().getLooper(),
            volume,
            context.getPackageManager(),
            (UserManager) context.getSystemService(Context.USER_SERVICE),
            (StorageStatsManager) context.getSystemService(Context.STORAGE_STATS_SERVICE));
}
 
Example #8
Source File: AppCollector.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
BackgroundHandler(Looper looper, @NonNull VolumeInfo volume,
        PackageManager pm, UserManager um, StorageStatsManager storageStatsManager) {
    super(looper);
    mVolume = volume;
    mPm = pm;
    mUm = um;
    mStorageStatsManager = storageStatsManager;
}
 
Example #9
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);
        }
    }
}
 
Example #10
Source File: StorageManagerService.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public long getAllocatableBytes(String volumeUuid, int flags, String callingPackage) {
    flags = adjustAllocateFlags(flags, Binder.getCallingUid(), callingPackage);

    final StorageManager storage = mContext.getSystemService(StorageManager.class);
    final StorageStatsManager stats = mContext.getSystemService(StorageStatsManager.class);
    final long token = Binder.clearCallingIdentity();
    try {
        // In general, apps can allocate as much space as they want, except
        // we never let them eat into either the minimum cache space or into
        // the low disk warning space. To avoid user confusion, this logic
        // should be kept in sync with getFreeBytes().
        final File path = storage.findPathForUuid(volumeUuid);

        final long usable = path.getUsableSpace();
        final long lowReserved = storage.getStorageLowBytes(path);
        final long fullReserved = storage.getStorageFullBytes(path);

        if (stats.isQuotaSupported(volumeUuid)) {
            final long cacheTotal = stats.getCacheBytes(volumeUuid);
            final long cacheReserved = storage.getStorageCacheBytes(path, flags);
            final long cacheClearable = Math.max(0, cacheTotal - cacheReserved);

            if ((flags & StorageManager.FLAG_ALLOCATE_AGGRESSIVE) != 0) {
                return Math.max(0, (usable + cacheClearable) - fullReserved);
            } else {
                return Math.max(0, (usable + cacheClearable) - lowReserved);
            }
        } else {
            // When we don't have fast quota information, we ignore cached
            // data and only consider unused bytes.
            if ((flags & StorageManager.FLAG_ALLOCATE_AGGRESSIVE) != 0) {
                return Math.max(0, usable - fullReserved);
            } else {
                return Math.max(0, usable - lowReserved);
            }
        }
    } catch (IOException e) {
        throw new ParcelableException(e);
    } finally {
        Binder.restoreCallingIdentity(token);
    }
}
 
Example #11
Source File: SystemServiceRegistry.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public StorageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    IStorageStatsManager service = IStorageStatsManager.Stub.asInterface(
            ServiceManager.getServiceOrThrow(Context.STORAGE_STATS_SERVICE));
    return new StorageStatsManager(ctx, service);
}