下面列出了怎么用android.app.usage.StorageStatsManager的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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;
}
/**
* 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;
}
}
/**
* 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;
}
}
/**
* 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;
}
}
@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);
}
}
@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);
}
}
/**
* 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));
}
BackgroundHandler(Looper looper, @NonNull VolumeInfo volume,
PackageManager pm, UserManager um, StorageStatsManager storageStatsManager) {
super(looper);
mVolume = volume;
mPm = pm;
mUm = um;
mStorageStatsManager = storageStatsManager;
}
/**
* 获取应用的大小
*/
@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);
}
}
}
@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);
}
}
@Override
public StorageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
IStorageStatsManager service = IStorageStatsManager.Stub.asInterface(
ServiceManager.getServiceOrThrow(Context.STORAGE_STATS_SERVICE));
return new StorageStatsManager(ctx, service);
}