Include user identifier in external storage paths.
When building external storage paths, always include user in path
to enable cross-user paths and aid debugging.
Bug: 7131382
Change-Id: I85f9eeed418a4fcced6633c579642e947fba65d7
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index d582830..34b6a94 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -2,7 +2,7 @@
package="com.android.providers.media"
android:sharedUserId="android.media"
android:sharedUserLabel="@string/uid_label"
- android:versionCode="509">
+ android:versionCode="510">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
diff --git a/src/com/android/providers/media/MediaProvider.java b/src/com/android/providers/media/MediaProvider.java
index 0f7ee55..b23effd 100644
--- a/src/com/android/providers/media/MediaProvider.java
+++ b/src/com/android/providers/media/MediaProvider.java
@@ -83,6 +83,7 @@
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.util.Log;
+import android.util.Slog;
import java.io.File;
import java.io.FileDescriptor;
@@ -643,6 +644,11 @@
return true;
}
+ private static final String TABLE_FILES = "files";
+ private static final String TABLE_ALBUM_ART = "album_art";
+ private static final String TABLE_THUMBNAILS = "thumbnails";
+ private static final String TABLE_VIDEO_THUMBNAILS = "videothumbnails";
+
private static final String IMAGE_COLUMNS =
"_data,_size,_display_name,mime_type,title,date_added," +
"date_modified,description,picasa_id,isprivate,latitude,longitude," +
@@ -1727,6 +1733,30 @@
if (fromVersion < 509) {
db.execSQL("CREATE TABLE IF NOT EXISTS log (time DATETIME PRIMARY KEY, message TEXT);");
}
+
+ // Emulated external storage moved to user-specific paths
+ if (fromVersion < 510 && Environment.isExternalStorageEmulated()) {
+ db.execSQL("DROP TRIGGER IF EXISTS files_cleanup");
+
+ // File.fixSlashes() removes any trailing slashes
+ final String externalStorage = Environment.getExternalStorageDirectory().toString();
+ Slog.d(TAG, "Adjusting external storage paths to: " + externalStorage);
+
+ final String[] tables = {
+ TABLE_FILES, TABLE_ALBUM_ART, TABLE_THUMBNAILS, TABLE_VIDEO_THUMBNAILS };
+ for (String table : tables) {
+ db.execSQL("UPDATE " + table + " SET " + "_data='" + externalStorage
+ + "'||SUBSTR(_data,17) WHERE _data LIKE '/storage/sdcard0/%';");
+ }
+
+ if (!internal) {
+ db.execSQL("CREATE TRIGGER IF NOT EXISTS files_cleanup DELETE ON files " +
+ "BEGIN " +
+ "SELECT _OBJECT_REMOVED(old._id);" +
+ "END");
+ }
+ }
+
sanityCheck(db, fromVersion);
long elapsedSeconds = (SystemClock.currentTimeMicro() - startTime) / 1000000;
logToDb(db, "Database upgraded from version " + fromVersion + " to " + toVersion
diff --git a/src/com/android/providers/media/MtpService.java b/src/com/android/providers/media/MtpService.java
index 545fcde..2d36b20 100644
--- a/src/com/android/providers/media/MtpService.java
+++ b/src/com/android/providers/media/MtpService.java
@@ -27,12 +27,10 @@
import android.mtp.MtpServer;
import android.mtp.MtpStorage;
import android.os.Environment;
-import android.os.Handler;
import android.os.IBinder;
import android.os.storage.StorageEventListener;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
-import android.provider.Settings;
import android.util.Log;
import java.io.File;
@@ -50,11 +48,12 @@
private void addStorageDevicesLocked() {
if (mPtpMode) {
// In PTP mode we support only primary storage
- String path = mVolumes[0].getPath();
+ final StorageVolume primary = StorageManager.getPrimaryVolume(mVolumes);
+ String path = primary.getPath();
if (path != null) {
String state = mStorageManager.getVolumeState(path);
if (Environment.MEDIA_MOUNTED.equals(state)) {
- addStorageLocked(mVolumeMap.get(mVolumes[0].getPath()));
+ addStorageLocked(mVolumeMap.get(primary.getPath()));
}
}
} else {
@@ -147,8 +146,9 @@
subdirs[i] = file.getPath();
}
}
+ final StorageVolume primary = StorageManager.getPrimaryVolume(mVolumes);
mDatabase = new MtpDatabase(this, MediaProvider.EXTERNAL_VOLUME,
- mVolumes[0].getPath(), subdirs);
+ primary.getPath(), subdirs);
mServer = new MtpServer(mDatabase, mPtpMode);
if (!mMtpDisabled) {
addStorageDevicesLocked();
@@ -199,7 +199,7 @@
mVolumeMap.put(path, volume);
if (!mMtpDisabled) {
// In PTP mode we support only primary storage
- if (i == 0 || !mPtpMode) {
+ if (volume.isPrimary() || !mPtpMode) {
addStorageLocked(volume);
}
}