diff options
author | Jeff Sharkey <jsharkey@android.com> | 2015-07-07 17:26:59 -0700 |
---|---|---|
committer | Jeff Sharkey <jsharkey@android.com> | 2015-07-07 17:46:16 -0700 |
commit | 5af1835d678031d4a6615edc96ba58c82304b31d (patch) | |
tree | 686e1a7e1a11374d4b760ac91ba66812fcfd631f /core | |
parent | 7a788a865e72da4205b5cf4e0a6f08ccb6f4bdbd (diff) | |
download | frameworks_base-5af1835d678031d4a6615edc96ba58c82304b31d.zip frameworks_base-5af1835d678031d4a6615edc96ba58c82304b31d.tar.gz frameworks_base-5af1835d678031d4a6615edc96ba58c82304b31d.tar.bz2 |
Generate stable MTP storage IDs.
It ends up that MediaProvider is persisting MTP storage IDs in its
database, so we need to make sure we generate stable IDs over time,
otherwise we can end up looking into a black hole.
Bug: 22256092
Change-Id: I6a75c239aac1b71fd5f6df0df69b24971079a086
Diffstat (limited to 'core')
-rw-r--r-- | core/java/android/os/storage/StorageVolume.java | 3 | ||||
-rw-r--r-- | core/java/android/os/storage/VolumeInfo.java | 42 |
2 files changed, 36 insertions, 9 deletions
diff --git a/core/java/android/os/storage/StorageVolume.java b/core/java/android/os/storage/StorageVolume.java index d66e228..1408202 100644 --- a/core/java/android/os/storage/StorageVolume.java +++ b/core/java/android/os/storage/StorageVolume.java @@ -58,6 +58,9 @@ public class StorageVolume implements Parcelable { // ACTION_MEDIA_BAD_REMOVAL, ACTION_MEDIA_UNMOUNTABLE and ACTION_MEDIA_EJECT broadcasts. public static final String EXTRA_STORAGE_VOLUME = "storage_volume"; + public static final int STORAGE_ID_INVALID = 0x00000000; + public static final int STORAGE_ID_PRIMARY = 0x00010001; + public StorageVolume(String id, int storageId, File path, String description, boolean primary, boolean removable, boolean emulated, long mtpReserveSize, boolean allowMassStorage, long maxFileSize, UserHandle owner, String fsUuid, String state) { diff --git a/core/java/android/os/storage/VolumeInfo.java b/core/java/android/os/storage/VolumeInfo.java index 91cb944..a4ee8b7 100644 --- a/core/java/android/os/storage/VolumeInfo.java +++ b/core/java/android/os/storage/VolumeInfo.java @@ -147,15 +147,11 @@ public class VolumeInfo implements Parcelable { public String path; public String internalPath; - /** Framework state */ - public final int mtpIndex; - - public VolumeInfo(String id, int type, DiskInfo disk, String partGuid, int mtpIndex) { + public VolumeInfo(String id, int type, DiskInfo disk, String partGuid) { this.id = Preconditions.checkNotNull(id); this.type = type; this.disk = disk; this.partGuid = partGuid; - this.mtpIndex = mtpIndex; } public VolumeInfo(Parcel parcel) { @@ -175,7 +171,6 @@ public class VolumeInfo implements Parcelable { fsLabel = parcel.readString(); path = parcel.readString(); internalPath = parcel.readString(); - mtpIndex = parcel.readInt(); } public static @NonNull String getEnvironmentForState(int state) { @@ -308,7 +303,6 @@ public class VolumeInfo implements Parcelable { final boolean removable; final boolean emulated; final boolean allowMassStorage = false; - final int mtpStorageId = MtpStorage.getStorageIdForIndex(mtpIndex); final String envState = getEnvironmentForState(state); File userPath = getPathForUser(userId); @@ -326,9 +320,15 @@ public class VolumeInfo implements Parcelable { long mtpReserveSize = 0; long maxFileSize = 0; + int mtpStorageId = StorageVolume.STORAGE_ID_INVALID; if (type == TYPE_EMULATED) { emulated = true; + + if (isPrimary()) { + mtpStorageId = StorageVolume.STORAGE_ID_PRIMARY; + } + mtpReserveSize = StorageManager.from(context).getStorageLowBytes(userPath); if (ID_EMULATED_INTERNAL.equals(id)) { @@ -341,6 +341,14 @@ public class VolumeInfo implements Parcelable { emulated = false; removable = true; + if (isPrimary()) { + mtpStorageId = StorageVolume.STORAGE_ID_PRIMARY; + } else { + // Since MediaProvider currently persists this value, we need a + // value that is stable over time. + mtpStorageId = buildStableMtpStorageId(fsUuid); + } + if ("vfat".equals(fsType)) { maxFileSize = 4294967295L; } @@ -354,6 +362,24 @@ public class VolumeInfo implements Parcelable { fsUuid, envState); } + public static int buildStableMtpStorageId(String fsUuid) { + if (TextUtils.isEmpty(fsUuid)) { + return StorageVolume.STORAGE_ID_INVALID; + } else { + int hash = 0; + for (int i = 0; i < fsUuid.length(); ++i) { + hash = 31 * hash + fsUuid.charAt(i); + } + hash = (hash ^ (hash << 16)) & 0xffff0000; + // Work around values that the spec doesn't allow, or that we've + // reserved for primary + if (hash == 0x00000000) hash = 0x00020000; + if (hash == 0x00010000) hash = 0x00020000; + if (hash == 0xffff0000) hash = 0xfffe0000; + return hash | 0x0001; + } + } + // TODO: avoid this layering violation private static final String DOCUMENT_AUTHORITY = "com.android.externalstorage.documents"; private static final String DOCUMENT_ROOT_PRIMARY_EMULATED = "primary"; @@ -402,7 +428,6 @@ public class VolumeInfo implements Parcelable { pw.println(); pw.printPair("path", path); pw.printPair("internalPath", internalPath); - pw.printPair("mtpIndex", mtpIndex); pw.decreaseIndent(); pw.println(); } @@ -469,6 +494,5 @@ public class VolumeInfo implements Parcelable { parcel.writeString(fsLabel); parcel.writeString(path); parcel.writeString(internalPath); - parcel.writeInt(mtpIndex); } } |