diff options
| -rw-r--r-- | core/java/android/os/storage/StorageManager.java | 46 |
1 files changed, 30 insertions, 16 deletions
diff --git a/core/java/android/os/storage/StorageManager.java b/core/java/android/os/storage/StorageManager.java index d45a17f7194e..91ad22f51345 100644 --- a/core/java/android/os/storage/StorageManager.java +++ b/core/java/android/os/storage/StorageManager.java @@ -120,6 +120,8 @@ import java.util.concurrent.Executor; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * StorageManager is the interface to the systems storage service. The storage @@ -2512,6 +2514,9 @@ public class StorageManager { return userId * PER_USER_RANGE + projectId; } + private static final Pattern PATTERN_USER_ID = Pattern.compile( + "(?i)^/storage/emulated/([0-9]+)"); + /** * Let StorageManager know that the quota type for a file on external storage should * be updated. Android tracks quotas for various media types. Consequently, this should be @@ -2541,26 +2546,35 @@ public class StorageManager { @SystemApi public void updateExternalStorageFileQuotaType(@NonNull File path, @QuotaType int quotaType) throws IOException { + if (!path.exists()) return; + long projectId; final String filePath = path.getCanonicalPath(); - int volFlags = FLAG_REAL_STATE | FLAG_INCLUDE_INVISIBLE; - // If caller has MANAGE_EXTERNAL_STORAGE permission, results from User Profile(s) are also - // returned by enabling FLAG_INCLUDE_SHARED_PROFILE. - if (mContext.checkSelfPermission(MANAGE_EXTERNAL_STORAGE) == PERMISSION_GRANTED) { - volFlags |= FLAG_INCLUDE_SHARED_PROFILE; - } - final StorageVolume[] availableVolumes = getVolumeList(mContext.getUserId(), volFlags); - final StorageVolume volume = getStorageVolume(availableVolumes, path); - if (volume == null) { - Log.w(TAG, "Failed to update quota type for " + filePath); - return; - } - if (!volume.isEmulated()) { - // We only support quota tracking on emulated filesystems - return; + + final int userId; + final Matcher matcher = PATTERN_USER_ID.matcher(filePath); + if (matcher.find()) { + userId = Integer.parseInt(matcher.group(1)); + } else { // fallback + int volFlags = FLAG_REAL_STATE | FLAG_INCLUDE_INVISIBLE; + // If caller has MANAGE_EXTERNAL_STORAGE permission, results from User Profile(s) are + // also returned by enabling FLAG_INCLUDE_SHARED_PROFILE. + if (mContext.checkSelfPermission(MANAGE_EXTERNAL_STORAGE) == PERMISSION_GRANTED) { + volFlags |= FLAG_INCLUDE_SHARED_PROFILE; + } + final StorageVolume[] availableVolumes = getVolumeList(mContext.getUserId(), volFlags); + final StorageVolume volume = getStorageVolume(availableVolumes, path); + if (volume == null) { + Log.w(TAG, "Failed to update quota type for " + filePath); + return; + } + if (!volume.isEmulated()) { + // We only support quota tracking on emulated filesystems + return; + } + userId = volume.getOwner().getIdentifier(); } - final int userId = volume.getOwner().getIdentifier(); if (userId < 0) { throw new IllegalStateException("Failed to update quota type for " + filePath); } |