diff options
-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); } |