Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 88818fe7 authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

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
parent 7912eb5e
Loading
Loading
Loading
Loading
+14 −1
Original line number Diff line number Diff line
@@ -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();