diff options
author | android-build-team Robot <android-build-team-robot@google.com> | 2017-08-28 07:35:54 +0000 |
---|---|---|
committer | android-build-team Robot <android-build-team-robot@google.com> | 2017-08-28 07:35:54 +0000 |
commit | 790f0d421d92054dabfc8d085400c77acb616007 (patch) | |
tree | d8e537fde0e0a47864eabb730bcfc8c6037b0360 | |
parent | 37c000af29dac42ad6af44de303f26cbe3acca78 (diff) | |
parent | 08bae5c84fd7f2e3f876e60082b37e2865f5ab8d (diff) | |
download | MediaProvider-790f0d421d92054dabfc8d085400c77acb616007.tar.gz |
release-request-c924aaac-f0a2-4215-8dc4-e314f22460d9-for-git_oc-mr1-release-4301796 snap-temp-L23200000097143969
Change-Id: Icccb547e5dfa7094b714e37aa9e803b89f03f3c1
-rw-r--r-- | AndroidManifest.xml | 7 | ||||
-rw-r--r-- | src/com/android/providers/media/MtpReceiver.java | 3 | ||||
-rw-r--r-- | src/com/android/providers/media/MtpService.java | 89 |
3 files changed, 50 insertions, 49 deletions
diff --git a/AndroidManifest.xml b/AndroidManifest.xml index c38ea610a..b4c9e64c8 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -1,4 +1,5 @@ <manifest xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:androidprv="http://schemas.android.com/apk/prv/res/android" package="com.android.providers.media" android:sharedUserId="android.media" android:sharedUserLabel="@string/uid_label" @@ -72,7 +73,8 @@ </intent-filter> </service> - <receiver android:name=".MtpReceiver"> + <receiver android:name=".MtpReceiver" + androidprv:systemUserOnly="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> @@ -81,7 +83,8 @@ </intent-filter> </receiver> - <service android:name="MtpService" /> + <service android:name="MtpService" + androidprv:systemUserOnly="true"/> <activity android:name="RingtonePickerActivity" android:theme="@style/PickerDialogTheme" diff --git a/src/com/android/providers/media/MtpReceiver.java b/src/com/android/providers/media/MtpReceiver.java index 5f3801814..9841528c2 100644 --- a/src/com/android/providers/media/MtpReceiver.java +++ b/src/com/android/providers/media/MtpReceiver.java @@ -58,11 +58,8 @@ public class MtpReceiver extends BroadcastReceiver { boolean ptpEnabled = extras.getBoolean(UsbManager.USB_FUNCTION_PTP); boolean unlocked = extras.getBoolean(UsbManager.USB_DATA_UNLOCKED); boolean configChanged = extras.getBoolean(UsbManager.USB_CONFIG_CHANGED); - boolean isCurrentUser = UserHandle.myUserId() == ActivityManager.getCurrentUser(); if ((configChanged || (connected && !configured)) && (mtpEnabled || ptpEnabled)) { - if (!isCurrentUser) - return; MtpServer.configure(ptpEnabled); // tell MediaProvider MTP is configured so it can bind to the service context.getContentResolver().insert(Uri.parse( diff --git a/src/com/android/providers/media/MtpService.java b/src/com/android/providers/media/MtpService.java index b9ea3ed42..47841800e 100644 --- a/src/com/android/providers/media/MtpService.java +++ b/src/com/android/providers/media/MtpService.java @@ -20,6 +20,7 @@ import android.annotation.NonNull; import android.app.ActivityManager; import android.app.Service; import android.content.Intent; +import android.content.pm.PackageManager; import android.hardware.usb.UsbManager; import android.mtp.MtpDatabase; import android.mtp.MtpServer; @@ -55,9 +56,11 @@ public class MtpService extends Service { final StorageVolume primary = StorageManager.getPrimaryVolume(mVolumes); final String path = primary.getPath(); if (path != null) { - String state = mStorageManager.getVolumeState(path); + String state = primary.getState(); if (Environment.MEDIA_MOUNTED.equals(state)) { addStorageLocked(mVolumeMap.get(path)); + } else { + Log.e(TAG, "Couldn't add primary storage " + path + " in state " + state); } } } else { @@ -70,7 +73,7 @@ public class MtpService extends Service { private final StorageEventListener mStorageEventListener = new StorageEventListener() { @Override public void onStorageStateChanged(String path, String oldState, String newState) { - synchronized (MtpService.this) { + synchronized (this) { Log.d(TAG, "onStorageStateChanged " + path + " " + oldState + " -> " + newState); if (Environment.MEDIA_MOUNTED.equals(newState)) { volumeMountedLocked(path); @@ -84,16 +87,8 @@ public class MtpService extends Service { } }; - /** - * Static state of MtpServer. MtpServer opens FD for MTP driver internally and we cannot open - * multiple MtpServer at the same time. The static field used to handle the case where MtpServer - * lives beyond the lifetime of MtpService. - * - * Lock MtpService.this before locking MtpService.class if needed. Otherwise it goes to - * deadlock. - */ - @GuardedBy("MtpService.class") - private static ServerHolder sServerHolder; + @GuardedBy("this") + private ServerHolder sServerHolder; private StorageManager mStorageManager; @@ -106,73 +101,84 @@ public class MtpService extends Service { private boolean mPtpMode; @GuardedBy("this") - private final HashMap<String, StorageVolume> mVolumeMap = new HashMap<String, StorageVolume>(); + private HashMap<String, StorageVolume> mVolumeMap; @GuardedBy("this") - private final HashMap<String, MtpStorage> mStorageMap = new HashMap<String, MtpStorage>(); + private HashMap<String, MtpStorage> mStorageMap; @GuardedBy("this") private StorageVolume[] mVolumes; @Override public void onCreate() { - mStorageManager = StorageManager.from(this); + mStorageManager = this.getSystemService(StorageManager.class); + mStorageManager.registerListener(mStorageEventListener); + } + + @Override + public void onDestroy() { + mStorageManager.unregisterListener(mStorageEventListener); + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + UserHandle user = new UserHandle(ActivityManager.getCurrentUser()); synchronized (this) { - updateDisabledStateLocked(); - mStorageManager.registerListener(mStorageEventListener); - StorageVolume[] volumes = mStorageManager.getVolumeList(); + mVolumeMap = new HashMap<>(); + mStorageMap = new HashMap<>(); + StorageVolume[] volumes = mStorageManager.getVolumeList(user.getIdentifier(), 0); mVolumes = volumes; for (int i = 0; i < volumes.length; i++) { String path = volumes[i].getPath(); - String state = mStorageManager.getVolumeState(path); + String state = volumes[i].getState(); if (Environment.MEDIA_MOUNTED.equals(state)) { volumeMountedLocked(path); + } else { + Log.e(TAG, "StorageVolume not mounted " + path); } } } - } - @Override - public int onStartCommand(Intent intent, int flags, int startId) { synchronized (this) { mUnlocked = intent.getBooleanExtra(UsbManager.USB_DATA_UNLOCKED, false); - if (LOGD) { Log.d(TAG, "onStartCommand intent=" + intent + " mUnlocked=" + mUnlocked); } updateDisabledStateLocked(); mPtpMode = (intent == null ? false : intent.getBooleanExtra(UsbManager.USB_FUNCTION_PTP, false)); String[] subdirs = null; if (mPtpMode) { + Environment.UserEnvironment env = new Environment.UserEnvironment( + user.getIdentifier()); int count = PTP_DIRECTORIES.length; subdirs = new String[count]; for (int i = 0; i < count; i++) { - File file = - Environment.getExternalStoragePublicDirectory(PTP_DIRECTORIES[i]); + File file = env.buildExternalStoragePublicDirs(PTP_DIRECTORIES[i])[0]; // make sure this directory exists file.mkdirs(); subdirs[i] = file.getPath(); } } final StorageVolume primary = StorageManager.getPrimaryVolume(mVolumes); - manageServiceLocked(primary, subdirs); + try { + manageServiceLocked(primary, subdirs, user); + } catch (PackageManager.NameNotFoundException e) { + Log.e(TAG, "Couldn't find the current user!: " + e.getMessage()); + } } return START_REDELIVER_INTENT; } private void updateDisabledStateLocked() { - final boolean isCurrentUser = UserHandle.myUserId() == ActivityManager.getCurrentUser(); - mMtpDisabled = !mUnlocked || !isCurrentUser; + mMtpDisabled = !mUnlocked; if (LOGD) { - Log.d(TAG, "updating state; isCurrentUser=" + isCurrentUser + ", mMtpLocked=" - + mMtpDisabled); + Log.d(TAG, "updating state; mMtpLocked=" + mMtpDisabled); } } /** * Manage {@link #mServer}, creating only when running as the current user. */ - private void manageServiceLocked(StorageVolume primary, String[] subdirs) { - final boolean isCurrentUser = UserHandle.myUserId() == ActivityManager.getCurrentUser(); - - synchronized (MtpService.class) { + private void manageServiceLocked(StorageVolume primary, String[] subdirs, UserHandle user) + throws PackageManager.NameNotFoundException { + synchronized (this) { if (sServerHolder != null) { if (LOGD) { Log.d(TAG, "Cannot launch second MTP server."); @@ -182,12 +188,12 @@ public class MtpService extends Service { // intent after that when UsbDeviceManager configures USB with new state. return; } - if (!isCurrentUser) { - return; - } - Log.d(TAG, "starting MTP server in " + (mPtpMode ? "PTP mode" : "MTP mode")); - final MtpDatabase database = new MtpDatabase(this, MediaProvider.EXTERNAL_VOLUME, + Log.d(TAG, "starting MTP server in " + (mPtpMode ? "PTP mode" : "MTP mode") + + " with storage " + primary.getPath() + (mMtpDisabled ? " disabled" : "")); + final MtpDatabase database = new MtpDatabase(this, + createPackageContextAsUser(this.getPackageName(), 0, user), + MediaProvider.EXTERNAL_VOLUME, primary.getPath(), subdirs); String deviceSerialNumber = Build.SERIAL; if (Build.UNKNOWN.equals(deviceSerialNumber)) { @@ -215,11 +221,6 @@ public class MtpService extends Service { } } - @Override - public void onDestroy() { - mStorageManager.unregisterListener(mStorageEventListener); - } - private final IMtpService.Stub mBinder = new IMtpService.Stub() { @Override |