Java Code Examples for android.os.storage.StorageManager#FLAG_ALLOCATE_AGGRESSIVE

The following examples show how to use android.os.storage.StorageManager#FLAG_ALLOCATE_AGGRESSIVE . 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: StorageManagerService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private int adjustAllocateFlags(int flags, int callingUid, String callingPackage) {
    // Require permission to allocate aggressively
    if ((flags & StorageManager.FLAG_ALLOCATE_AGGRESSIVE) != 0) {
        mContext.enforceCallingOrSelfPermission(
                android.Manifest.permission.ALLOCATE_AGGRESSIVE, TAG);
    }

    // Apps normally can't directly defy reserved space
    flags &= ~StorageManager.FLAG_ALLOCATE_DEFY_ALL_RESERVED;
    flags &= ~StorageManager.FLAG_ALLOCATE_DEFY_HALF_RESERVED;

    // However, if app is actively using the camera, then we're willing to
    // clear up to half of the reserved cache space, since the user might be
    // trying to capture an important memory.
    final AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class);
    final long token = Binder.clearCallingIdentity();
    try {
        if (appOps.isOperationActive(AppOpsManager.OP_CAMERA, callingUid, callingPackage)) {
            Slog.d(TAG, "UID " + callingUid + " is actively using camera;"
                    + " letting them defy reserved cached data");
            flags |= StorageManager.FLAG_ALLOCATE_DEFY_HALF_RESERVED;
        }
    } finally {
        Binder.restoreCallingIdentity(token);
    }

    return flags;
}
 
Example 2
Source File: StorageManagerService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void allocateBytes(String volumeUuid, long bytes, int flags, String callingPackage) {
    flags = adjustAllocateFlags(flags, Binder.getCallingUid(), callingPackage);

    final long allocatableBytes = getAllocatableBytes(volumeUuid, flags, callingPackage);
    if (bytes > allocatableBytes) {
        throw new ParcelableException(new IOException("Failed to allocate " + bytes
                + " because only " + allocatableBytes + " allocatable"));
    }

    final StorageManager storage = mContext.getSystemService(StorageManager.class);
    final long token = Binder.clearCallingIdentity();
    try {
        // Free up enough disk space to satisfy both the requested allocation
        // and our low disk warning space.
        final File path = storage.findPathForUuid(volumeUuid);
        if ((flags & StorageManager.FLAG_ALLOCATE_AGGRESSIVE) != 0) {
            bytes += storage.getStorageFullBytes(path);
        } else {
            bytes += storage.getStorageLowBytes(path);
        }

        mPms.freeStorage(volumeUuid, bytes, flags);
    } catch (IOException e) {
        throw new ParcelableException(e);
    } finally {
        Binder.restoreCallingIdentity(token);
    }
}
 
Example 3
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);
    }
}