Merge "Access removable volumes through /mnt/media_rw" into pi-dev
am: 1e62b676d9

Change-Id: Ibc2cf0e4243adb0f69f2ba7ac3ca7526bb492b38
diff --git a/core/java/android/os/storage/StorageVolume.java b/core/java/android/os/storage/StorageVolume.java
index 839a8bf..fd5a22a 100644
--- a/core/java/android/os/storage/StorageVolume.java
+++ b/core/java/android/os/storage/StorageVolume.java
@@ -78,6 +78,7 @@
 
     private final String mId;
     private final File mPath;
+    private final File mInternalPath;
     private final String mDescription;
     private final boolean mPrimary;
     private final boolean mRemovable;
@@ -118,11 +119,12 @@
     public static final int STORAGE_ID_PRIMARY = 0x00010001;
 
     /** {@hide} */
-    public StorageVolume(String id, File path, String description, boolean primary,
-            boolean removable, boolean emulated, boolean allowMassStorage,
+    public StorageVolume(String id, File path, File internalPath, String description,
+            boolean primary, boolean removable, boolean emulated, boolean allowMassStorage,
             long maxFileSize, UserHandle owner, String fsUuid, String state) {
         mId = Preconditions.checkNotNull(id);
         mPath = Preconditions.checkNotNull(path);
+        mInternalPath = Preconditions.checkNotNull(internalPath);
         mDescription = Preconditions.checkNotNull(description);
         mPrimary = primary;
         mRemovable = removable;
@@ -137,6 +139,7 @@
     private StorageVolume(Parcel in) {
         mId = in.readString();
         mPath = new File(in.readString());
+        mInternalPath = new File(in.readString());
         mDescription = in.readString();
         mPrimary = in.readInt() != 0;
         mRemovable = in.readInt() != 0;
@@ -163,6 +166,16 @@
         return mPath.toString();
     }
 
+    /**
+     * Returns the path of the underlying filesystem.
+     *
+     * @return the internal path
+     * @hide
+     */
+    public String getInternalPath() {
+        return mInternalPath.toString();
+    }
+
     /** {@hide} */
     public File getPathFile() {
         return mPath;
@@ -351,6 +364,7 @@
         pw.increaseIndent();
         pw.printPair("mId", mId);
         pw.printPair("mPath", mPath);
+        pw.printPair("mInternalPath", mInternalPath);
         pw.printPair("mDescription", mDescription);
         pw.printPair("mPrimary", mPrimary);
         pw.printPair("mRemovable", mRemovable);
@@ -384,6 +398,7 @@
     public void writeToParcel(Parcel parcel, int flags) {
         parcel.writeString(mId);
         parcel.writeString(mPath.toString());
+        parcel.writeString(mInternalPath.toString());
         parcel.writeString(mDescription);
         parcel.writeInt(mPrimary ? 1 : 0);
         parcel.writeInt(mRemovable ? 1 : 0);
diff --git a/core/java/android/os/storage/VolumeInfo.java b/core/java/android/os/storage/VolumeInfo.java
index 5c99f6c..576ea52 100644
--- a/core/java/android/os/storage/VolumeInfo.java
+++ b/core/java/android/os/storage/VolumeInfo.java
@@ -333,6 +333,10 @@
         if (userPath == null) {
             userPath = new File("/dev/null");
         }
+        File internalPath = getInternalPathForUser(userId);
+        if (internalPath == null) {
+            internalPath = new File("/dev/null");
+        }
 
         String description = null;
         String derivedFsUuid = fsUuid;
@@ -371,7 +375,7 @@
             description = context.getString(android.R.string.unknownName);
         }
 
-        return new StorageVolume(id, userPath, description, isPrimary(), removable,
+        return new StorageVolume(id, userPath, internalPath, description, isPrimary(), removable,
                 emulated, allowMassStorage, maxFileSize, new UserHandle(userId),
                 derivedFsUuid, envState);
     }
diff --git a/media/java/android/mtp/MtpStorage.java b/media/java/android/mtp/MtpStorage.java
index c72b827..2625e0c 100644
--- a/media/java/android/mtp/MtpStorage.java
+++ b/media/java/android/mtp/MtpStorage.java
@@ -16,7 +16,6 @@
 
 package android.mtp;
 
-import android.content.Context;
 import android.os.storage.StorageVolume;
 
 /**
@@ -36,7 +35,7 @@
 
     public MtpStorage(StorageVolume volume, int storageId) {
         mStorageId = storageId;
-        mPath = volume.getPath();
+        mPath = volume.getInternalPath();
         mDescription = volume.getDescription(null);
         mRemovable = volume.isRemovable();
         mMaxFileSize = volume.getMaxFileSize();
diff --git a/media/java/android/mtp/MtpStorageManager.java b/media/java/android/mtp/MtpStorageManager.java
index bdc8741..a36d88d 100644
--- a/media/java/android/mtp/MtpStorageManager.java
+++ b/media/java/android/mtp/MtpStorageManager.java
@@ -399,8 +399,8 @@
      */
     public synchronized MtpStorage addMtpStorage(StorageVolume volume) {
         int storageId = ((getNextStorageId() & 0x0000FFFF) << 16) + 1;
-        MtpObject root = new MtpObject(volume.getPath(), storageId, null, true);
         MtpStorage storage = new MtpStorage(volume, storageId);
+        MtpObject root = new MtpObject(storage.getPath(), storageId, null, true);
         mRoots.put(storageId, root);
         return storage;
     }
diff --git a/services/core/java/com/android/server/StorageManagerService.java b/services/core/java/com/android/server/StorageManagerService.java
index 00302b2..183be9b 100644
--- a/services/core/java/com/android/server/StorageManagerService.java
+++ b/services/core/java/com/android/server/StorageManagerService.java
@@ -2795,7 +2795,7 @@
             final String uuid = null;
             final String state = Environment.MEDIA_REMOVED;
 
-            res.add(0, new StorageVolume(id, path,
+            res.add(0, new StorageVolume(id, path, path,
                     description, primary, removable, emulated,
                     allowMassStorage, maxFileSize, owner, uuid, state));
         }