From 88818fe7b2c9b1b52c80e1416347953b7504108a Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Thu, 28 May 2020 19:18:38 -0600 Subject: [PATCH] Support getStorageVolume() for synthetic volumes. The MediaStore.VOLUME_EXTERNAL volume is a synthetic collection of all currently mounted storage volumes, so if someone wants to find the underlying StorageVolume for one of these Uris, we need to query to resolve the real volume name. Bug: 153664437 Test: atest android.provider.cts.MediaStoreTest Change-Id: I7b28ba3bfd5b7a4cc577830940de4a2e1cbd1932 --- core/java/android/os/storage/StorageManager.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/core/java/android/os/storage/StorageManager.java b/core/java/android/os/storage/StorageManager.java index 66b8cabe03c3e..4ca48cb3e57cc 100644 --- a/core/java/android/os/storage/StorageManager.java +++ b/core/java/android/os/storage/StorageManager.java @@ -56,6 +56,7 @@ import android.content.pm.IPackageMoveObserver; import android.content.pm.PackageManager; import android.content.res.ObbInfo; import android.content.res.ObbScanner; +import android.database.Cursor; import android.net.Uri; import android.os.Binder; import android.os.Environment; @@ -1201,7 +1202,19 @@ public class StorageManager { * {@link MediaStore} item. */ public @NonNull StorageVolume getStorageVolume(@NonNull Uri uri) { - final String volumeName = MediaStore.getVolumeName(uri); + String volumeName = MediaStore.getVolumeName(uri); + + // When Uri is pointing at a synthetic volume, we're willing to query to + // resolve the actual volume name + if (Objects.equals(volumeName, MediaStore.VOLUME_EXTERNAL)) { + try (Cursor c = mContext.getContentResolver().query(uri, + new String[] { MediaStore.MediaColumns.VOLUME_NAME }, null, null)) { + if (c.moveToFirst()) { + volumeName = c.getString(0); + } + } + } + switch (volumeName) { case MediaStore.VOLUME_EXTERNAL_PRIMARY: return getPrimaryStorageVolume();