Merge "Better volume name argument checking."

This commit is contained in:
Jeff Sharkey
2019-01-20 21:52:00 +00:00
committed by Android (Google) Code Review

View File

@@ -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();