Java Code Examples for android.os.storage.StorageManager#getWritablePrivateVolumes()

The following examples show how to use android.os.storage.StorageManager#getWritablePrivateVolumes() . 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: UserDataPreparer.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Prepare storage areas for given user on all mounted devices.
 */
void prepareUserData(int userId, int userSerial, int flags) {
    synchronized (mInstallLock) {
        final StorageManager storage = mContext.getSystemService(StorageManager.class);
        for (VolumeInfo vol : storage.getWritablePrivateVolumes()) {
            final String volumeUuid = vol.getFsUuid();
            prepareUserDataLI(volumeUuid, userId, userSerial, flags, true);
        }
    }
}
 
Example 2
Source File: UserDataPreparer.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Destroy storage areas for given user on all mounted devices.
 */
void destroyUserData(int userId, int flags) {
    synchronized (mInstallLock) {
        final StorageManager storage = mContext.getSystemService(StorageManager.class);
        for (VolumeInfo vol : storage.getWritablePrivateVolumes()) {
            final String volumeUuid = vol.getFsUuid();
            destroyUserDataLI(volumeUuid, userId, flags);
        }
    }
}
 
Example 3
Source File: DeviceStorageMonitorService.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Core logic that checks the storage state of every mounted private volume.
 * Since this can do heavy I/O, callers should invoke indirectly using
 * {@link #MSG_CHECK}.
 */
@WorkerThread
private void check() {
    final StorageManager storage = getContext().getSystemService(StorageManager.class);
    final int seq = mSeq.get();

    // Check every mounted private volume to see if they're low on space
    for (VolumeInfo vol : storage.getWritablePrivateVolumes()) {
        final File file = vol.getPath();
        final long fullBytes = storage.getStorageFullBytes(file);
        final long lowBytes = storage.getStorageLowBytes(file);

        // Automatically trim cached data when nearing the low threshold;
        // when it's within 150% of the threshold, we try trimming usage
        // back to 200% of the threshold.
        if (file.getUsableSpace() < (lowBytes * 3) / 2) {
            final PackageManagerService pms = (PackageManagerService) ServiceManager
                    .getService("package");
            try {
                pms.freeStorage(vol.getFsUuid(), lowBytes * 2, 0);
            } catch (IOException e) {
                Slog.w(TAG, e);
            }
        }

        // Send relevant broadcasts and show notifications based on any
        // recently noticed state transitions.
        final UUID uuid = StorageManager.convert(vol.getFsUuid());
        final State state = findOrCreateState(uuid);
        final long totalBytes = file.getTotalSpace();
        final long usableBytes = file.getUsableSpace();

        int oldLevel = state.level;
        int newLevel;
        if (mForceLevel != State.LEVEL_UNKNOWN) {
            // When in testing mode, use unknown old level to force sending
            // of any relevant broadcasts.
            oldLevel = State.LEVEL_UNKNOWN;
            newLevel = mForceLevel;
        } else if (usableBytes <= fullBytes) {
            newLevel = State.LEVEL_FULL;
        } else if (usableBytes <= lowBytes) {
            newLevel = State.LEVEL_LOW;
        } else if (StorageManager.UUID_DEFAULT.equals(uuid) && !isBootImageOnDisk()
                && usableBytes < BOOT_IMAGE_STORAGE_REQUIREMENT) {
            newLevel = State.LEVEL_LOW;
        } else {
            newLevel = State.LEVEL_NORMAL;
        }

        // Log whenever we notice drastic storage changes
        if ((Math.abs(state.lastUsableBytes - usableBytes) > DEFAULT_LOG_DELTA_BYTES)
                || oldLevel != newLevel) {
            EventLogTags.writeStorageState(uuid.toString(), oldLevel, newLevel,
                    usableBytes, totalBytes);
            state.lastUsableBytes = usableBytes;
        }

        updateNotifications(vol, oldLevel, newLevel);
        updateBroadcasts(vol, oldLevel, newLevel, seq);

        state.level = newLevel;
    }

    // Loop around to check again in future; we don't remove messages since
    // there might be an immediate request pending.
    if (!mHandler.hasMessages(MSG_CHECK)) {
        mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_CHECK),
                DEFAULT_CHECK_INTERVAL);
    }
}