summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Jeff Sharkey <jsharkey@google.com> 2019-01-20 21:52:00 +0000
committer Android (Google) Code Review <android-gerrit@google.com> 2019-01-20 21:52:00 +0000
commitef033dada9fea6658749364ec3ae4346cfac00b7 (patch)
tree3c2d62664c29c40e78b45879b713140ba4c0e48f
parentf4246a1c175e00312767686fdcbf19a35efa34ee (diff)
parent8559e6569494adc60252712ab77dc57930e5ebde (diff)
Merge "Better volume name argument checking."
-rw-r--r--core/java/android/provider/MediaStore.java57
1 files changed, 45 insertions, 12 deletions
diff --git a/core/java/android/provider/MediaStore.java b/core/java/android/provider/MediaStore.java
index 7a83ac57390a..e36a1c21f592 100644
--- a/core/java/android/provider/MediaStore.java
+++ b/core/java/android/provider/MediaStore.java
@@ -46,6 +46,7 @@ import android.net.Uri;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.os.Environment;
+import android.os.FileUtils;
import android.os.OperationCanceledException;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
@@ -86,8 +87,10 @@ public final class MediaStore {
/** A content:// style uri to the authority for the media provider */
public static final Uri AUTHORITY_URI = Uri.parse("content://" + AUTHORITY);
- private static final String VOLUME_INTERNAL = "internal";
- private static final String VOLUME_EXTERNAL = "external";
+ /** {@hide} */
+ public static final String VOLUME_INTERNAL = "internal";
+ /** {@hide} */
+ public static final String VOLUME_EXTERNAL = "external";
/**
* The method name used by the media scanner and mtp to tell the media provider to
@@ -1213,15 +1216,18 @@ public final class MediaStore {
/** {@hide} */
public static @NonNull String getVolumeName(@NonNull File path) {
- final StorageManager sm = AppGlobals.getInitialApplication()
- .getSystemService(StorageManager.class);
- final StorageVolume sv = sm.getStorageVolume(path);
- if (sv != null) {
- if (sv.isPrimary()) {
- return VOLUME_EXTERNAL;
- } else {
- return sv.getUuid();
+ if (FileUtils.contains(Environment.getStorageDirectory(), path)) {
+ final StorageManager sm = AppGlobals.getInitialApplication()
+ .getSystemService(StorageManager.class);
+ final StorageVolume sv = sm.getStorageVolume(path);
+ if (sv != null) {
+ if (sv.isPrimary()) {
+ return VOLUME_EXTERNAL;
+ } else {
+ return checkArgumentVolumeName(sv.getUuid());
+ }
}
+ throw new IllegalStateException("Unknown volume at " + path);
} else {
return VOLUME_INTERNAL;
}
@@ -2928,14 +2934,41 @@ public final class MediaStore {
if (uri.getAuthority().equals(AUTHORITY) && segments != null && segments.size() > 0) {
return segments.get(0);
} else {
- throw new IllegalArgumentException("Not a media Uri: " + uri);
+ throw new IllegalArgumentException("Missing volume name: " + uri);
+ }
+ }
+
+ /** {@hide} */
+ public static @NonNull String checkArgumentVolumeName(@NonNull String volumeName) {
+ if (TextUtils.isEmpty(volumeName)) {
+ throw new IllegalArgumentException();
+ }
+
+ if (VOLUME_INTERNAL.equals(volumeName)) {
+ return volumeName;
+ } else if (VOLUME_EXTERNAL.equals(volumeName)) {
+ return volumeName;
}
+
+ // When not one of the well-known values above, it must be a hex UUID
+ for (int i = 0; i < volumeName.length(); i++) {
+ final char c = volumeName.charAt(i);
+ if (('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
+ || ('0' <= c && c <= '9') || (c == '-')) {
+ continue;
+ } else {
+ throw new IllegalArgumentException("Invalid volume name: " + volumeName);
+ }
+ }
+ return volumeName;
}
/** {@hide} */
public static @NonNull File getVolumePath(@NonNull String volumeName)
throws FileNotFoundException {
- Objects.requireNonNull(volumeName);
+ if (TextUtils.isEmpty(volumeName)) {
+ throw new IllegalArgumentException();
+ }
if (VOLUME_INTERNAL.equals(volumeName)) {
return Environment.getDataDirectory();