diff options
author | Jeff Sharkey <jsharkey@android.com> | 2015-07-07 17:27:05 -0700 |
---|---|---|
committer | Jeff Sharkey <jsharkey@android.com> | 2015-07-08 13:43:28 -0700 |
commit | 40fef25a59cc7a36b99857b756befc36e6996845 (patch) | |
tree | 62d1357174a537583dbf4154f7c2c0bac630c8cd /src/com/android/providers | |
parent | 4015136f163a249dbde15d8c93e2b03eae5d7996 (diff) | |
download | android_packages_providers_MediaProvider-40fef25a59cc7a36b99857b756befc36e6996845.tar.gz android_packages_providers_MediaProvider-40fef25a59cc7a36b99857b756befc36e6996845.tar.bz2 android_packages_providers_MediaProvider-40fef25a59cc7a36b99857b756befc36e6996845.zip |
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: Ia419c37f87c7c0aa59ef0de530fd219c82988f6c
Diffstat (limited to 'src/com/android/providers')
-rwxr-xr-x | src/com/android/providers/media/MediaProvider.java | 22 | ||||
-rw-r--r-- | src/com/android/providers/media/MtpService.java | 17 |
2 files changed, 22 insertions, 17 deletions
diff --git a/src/com/android/providers/media/MediaProvider.java b/src/com/android/providers/media/MediaProvider.java index f4e98e0c..e3e3a036 100755 --- a/src/com/android/providers/media/MediaProvider.java +++ b/src/com/android/providers/media/MediaProvider.java @@ -1623,7 +1623,7 @@ public class MediaProvider extends ContentProvider { // Add column for MTP storage ID db.execSQL("ALTER TABLE files ADD COLUMN storage_id INTEGER;"); // Anything in the database before this upgrade step will be in the primary storage - db.execSQL("UPDATE files SET storage_id=" + MtpStorage.getStorageIdForIndex(0) + ";"); + db.execSQL("UPDATE files SET storage_id=" + StorageVolume.STORAGE_ID_PRIMARY + ";"); } if (fromVersion < 403 && !internal) { @@ -3015,17 +3015,14 @@ public class MediaProvider extends ContentProvider { } private int getStorageId(String path) { - for (int i = 0; i < mExternalStoragePaths.length; i++) { - String test = mExternalStoragePaths[i]; - if (path.startsWith(test)) { - int length = test.length(); - if (path.length() == length || path.charAt(length) == '/') { - return MtpStorage.getStorageIdForIndex(i); - } - } + final StorageManager storage = getContext().getSystemService(StorageManager.class); + final StorageVolume vol = storage.getStorageVolume(new File(path)); + if (vol != null) { + return vol.getStorageId(); + } else { + Log.w(TAG, "Missing volume for " + path + "; assuming invalid"); + return StorageVolume.STORAGE_ID_INVALID; } - // default to primary storage - return MtpStorage.getStorageIdForIndex(0); } private long insertFile(DatabaseHelper helper, Uri uri, ContentValues initialValues, int mediaType, @@ -5364,7 +5361,8 @@ public class MediaProvider extends ContentProvider { // for devices without removable storage, and in that case we need to convert // to this new convention File dbFile = context.getDatabasePath(EXTERNAL_DATABASE_NAME); - if (!dbFile.exists()) { + if (!dbFile.exists() + && android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) { // find the most recent external database and rename it to // EXTERNAL_DATABASE_NAME, and delete any other older // external database files diff --git a/src/com/android/providers/media/MtpService.java b/src/com/android/providers/media/MtpService.java index 32e7f17c..2fef7ec4 100644 --- a/src/com/android/providers/media/MtpService.java +++ b/src/com/android/providers/media/MtpService.java @@ -221,10 +221,16 @@ public class MtpService extends Service { private void addStorageLocked(StorageVolume volume) { MtpStorage storage = new MtpStorage(volume, getApplicationContext()); - String path = storage.getPath(); - mStorageMap.put(path, storage); + mStorageMap.put(storage.getPath(), storage); + + if (storage.getStorageId() == StorageVolume.STORAGE_ID_INVALID) { + Log.w(TAG, "Ignoring volume with invalid MTP storage ID: " + storage); + return; + } else { + Log.d(TAG, "Adding MTP storage 0x" + Integer.toHexString(storage.getStorageId()) + + " at " + storage.getPath()); + } - Log.d(TAG, "addStorageLocked " + storage.getStorageId() + " " + path); if (mDatabase != null) { mDatabase.addStorage(storage); } @@ -236,11 +242,12 @@ public class MtpService extends Service { private void removeStorageLocked(StorageVolume volume) { MtpStorage storage = mStorageMap.remove(volume.getPath()); if (storage == null) { - Log.e(TAG, "no MtpStorage for " + volume.getPath()); + Log.e(TAG, "Missing MtpStorage for " + volume.getPath()); return; } - Log.d(TAG, "removeStorageLocked " + storage.getStorageId() + " " + storage.getPath()); + Log.d(TAG, "Removing MTP storage " + Integer.toHexString(storage.getStorageId()) + " at " + + storage.getPath()); if (mDatabase != null) { mDatabase.removeStorage(storage); } |