Loading core/java/android/os/storage/StorageManager.java +17 −0 Original line number Diff line number Diff line Loading @@ -20,6 +20,7 @@ import static android.net.TrafficStats.MB_IN_BYTES; import android.content.ContentResolver; import android.content.Context; import android.hardware.usb.UsbManager; import android.os.Environment; import android.os.Handler; import android.os.Looper; Loading Loading @@ -380,6 +381,22 @@ public class StorageManager { } } /** * Switch USB Mass Storage (UMS) on the device. * * @hide */ public void setUsbMassStorageEnabled(boolean enbale) { if (enbale && UsbManager.USB_FUNCTION_MASS_STORAGE .equals(new UsbManager(null, null).getDefaultFunction())) { if(!isUsbMassStorageEnabled()) { enableUsbMassStorage(); } } else if (isUsbMassStorageEnabled()) { disableUsbMassStorage(); } } /** * Enables USB Mass Storage (UMS) on the device. * Loading packages/SystemUI/src/com/android/systemui/usb/StorageNotification.java 100644 → 100755 +22 −0 Original line number Diff line number Diff line Loading @@ -28,8 +28,10 @@ import android.os.HandlerThread; import android.os.UserHandle; import android.os.storage.StorageEventListener; import android.os.storage.StorageManager; import android.os.SystemProperties; import android.provider.Settings; import android.util.Log; import android.hardware.usb.UsbManager; import com.android.systemui.SystemUI; Loading Loading @@ -117,13 +119,24 @@ public class StorageNotification extends SystemUI { */ connected = false; } //once UMS connected and SD card mounted, enable UMS if (connected && st.equals(Environment.MEDIA_MOUNTED)) { mStorageManager.setUsbMassStorageEnabled(true); } updateUsbMassStorageNotification(connected); } private void onStorageStateChangedAsync(String path, String oldState, String newState) { if (DEBUG) Log.i(TAG, String.format( "Media {%s} state changed from {%s} -> {%s}", path, oldState, newState)); if (newState.equals(Environment.MEDIA_SHARED)) { String usbMode = new UsbManager(null, null).getDefaultFunction(); final boolean isUmsMode = UsbManager.USB_FUNCTION_MASS_STORAGE.equals(usbMode); if (!isUmsMode) { mStorageManager.disableUsbMassStorage(); } /* * Storage is now shared. Modify the UMS notification * for stopping UMS. Loading Loading @@ -272,6 +285,15 @@ public class StorageNotification extends SystemUI { */ private synchronized void setUsbStorageNotification(int titleId, int messageId, int icon, boolean sound, boolean visible, PendingIntent pi) { // force to show UsbSettings screen to select usb mode if property is true if (SystemProperties.getBoolean("persist.sys.ums", true)) { titleId = 0; messageId = 0; icon = 0; sound = false; visible = false; pi = null; } if (!visible && mUsbStorageNotification == null) { return; Loading services/java/com/android/server/MountService.java 100644 → 100755 +67 −47 Original line number Diff line number Diff line Loading @@ -1134,10 +1134,14 @@ class MountService extends IMountService.Stub mSendUmsConnectedOnBoot = avail; } final StorageVolume primary = getPrimaryPhysicalVolume(); if (avail == false && primary != null && Environment.MEDIA_SHARED.equals(getVolumeState(primary.getPath()))) { final String path = primary.getPath(); final ArrayList<String> volumes = getShareableVolumes(); boolean mediaShared = false; for (String path : volumes) { if (getVolumeState(path).equals(Environment.MEDIA_SHARED)) mediaShared = true; } if (avail == false && mediaShared) { /* * USB mass storage disconnected while enabled */ Loading @@ -1147,12 +1151,17 @@ class MountService extends IMountService.Stub try { int rc; Slog.w(TAG, "Disabling UMS after cable disconnect"); for (String path : volumes) { if (getVolumeState(path).equals(Environment.MEDIA_SHARED)) { doShareUnshareVolume(path, "ums", false); if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) { rc = doMountVolume(path); if (rc != StorageResultCode.OperationSucceeded) { Slog.e(TAG, String.format( "Failed to remount {%s} on UMS enabled-disconnect (%d)", path, rc)); } } } } catch (Exception ex) { Slog.w(TAG, "Failed to mount media on UMS enabled-disconnect", ex); } Loading Loading @@ -1357,7 +1366,9 @@ class MountService extends IMountService.Stub // Watch for USB changes on primary volume final StorageVolume primary = getPrimaryPhysicalVolume(); if (primary != null && primary.allowMassStorage()) { if ((primary != null && primary.allowMassStorage()) || //ignore primary config, force to register if property is true SystemProperties.getBoolean("persist.sys.ums", true)) { mContext.registerReceiver( mUsbReceiver, new IntentFilter(UsbManager.ACTION_USB_STATE), null, mHandler); } Loading Loading @@ -1490,6 +1501,19 @@ class MountService extends IMountService.Stub } } private ArrayList<String> getShareableVolumes() { // Sharable volumes have android:allowMassStorage="true" in storage_list.xml ArrayList<String> volumesToMount = new ArrayList<String>(); synchronized (mVolumes) { for (StorageVolume v : mVolumes) { if (v.allowMassStorage()) { volumesToMount.add(v.getPath()); } } } return volumesToMount; } public boolean isUsbMassStorageConnected() { waitForReady(); Loading @@ -1505,15 +1529,11 @@ class MountService extends IMountService.Stub waitForReady(); validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); final StorageVolume primary = getPrimaryPhysicalVolume(); if (primary == null) return; // TODO: Add support for multiple share methods for (String path : getShareableVolumes()) { /* * If the volume is mounted and we're enabling then unmount it */ String path = primary.getPath(); String vs = getVolumeState(path); String method = "ums"; if (enable && vs.equals(Environment.MEDIA_MOUNTED)) { Loading @@ -1540,16 +1560,16 @@ class MountService extends IMountService.Stub } } } } public boolean isUsbMassStorageEnabled() { waitForReady(); final StorageVolume primary = getPrimaryPhysicalVolume(); if (primary != null) { return doGetVolumeShared(primary.getPath(), "ums"); } else { return false; for (String path : getShareableVolumes()) { if (doGetVolumeShared(path, "ums")) return true; } // no volume is shared return false; } /** Loading services/java/com/android/server/usb/UsbDeviceManager.java +2 −0 Original line number Diff line number Diff line Loading @@ -594,6 +594,8 @@ public class UsbDeviceManager { case MSG_UPDATE_STATE: mConnected = (msg.arg1 == 1); mConfigured = (msg.arg2 == 1); StorageManager storageManager = StorageManager.from(mContext); storageManager.setUsbMassStorageEnabled(mConnected); updateUsbNotification(); updateAdbNotification(); if (containsFunction(mCurrentFunctions, Loading Loading
core/java/android/os/storage/StorageManager.java +17 −0 Original line number Diff line number Diff line Loading @@ -20,6 +20,7 @@ import static android.net.TrafficStats.MB_IN_BYTES; import android.content.ContentResolver; import android.content.Context; import android.hardware.usb.UsbManager; import android.os.Environment; import android.os.Handler; import android.os.Looper; Loading Loading @@ -380,6 +381,22 @@ public class StorageManager { } } /** * Switch USB Mass Storage (UMS) on the device. * * @hide */ public void setUsbMassStorageEnabled(boolean enbale) { if (enbale && UsbManager.USB_FUNCTION_MASS_STORAGE .equals(new UsbManager(null, null).getDefaultFunction())) { if(!isUsbMassStorageEnabled()) { enableUsbMassStorage(); } } else if (isUsbMassStorageEnabled()) { disableUsbMassStorage(); } } /** * Enables USB Mass Storage (UMS) on the device. * Loading
packages/SystemUI/src/com/android/systemui/usb/StorageNotification.java 100644 → 100755 +22 −0 Original line number Diff line number Diff line Loading @@ -28,8 +28,10 @@ import android.os.HandlerThread; import android.os.UserHandle; import android.os.storage.StorageEventListener; import android.os.storage.StorageManager; import android.os.SystemProperties; import android.provider.Settings; import android.util.Log; import android.hardware.usb.UsbManager; import com.android.systemui.SystemUI; Loading Loading @@ -117,13 +119,24 @@ public class StorageNotification extends SystemUI { */ connected = false; } //once UMS connected and SD card mounted, enable UMS if (connected && st.equals(Environment.MEDIA_MOUNTED)) { mStorageManager.setUsbMassStorageEnabled(true); } updateUsbMassStorageNotification(connected); } private void onStorageStateChangedAsync(String path, String oldState, String newState) { if (DEBUG) Log.i(TAG, String.format( "Media {%s} state changed from {%s} -> {%s}", path, oldState, newState)); if (newState.equals(Environment.MEDIA_SHARED)) { String usbMode = new UsbManager(null, null).getDefaultFunction(); final boolean isUmsMode = UsbManager.USB_FUNCTION_MASS_STORAGE.equals(usbMode); if (!isUmsMode) { mStorageManager.disableUsbMassStorage(); } /* * Storage is now shared. Modify the UMS notification * for stopping UMS. Loading Loading @@ -272,6 +285,15 @@ public class StorageNotification extends SystemUI { */ private synchronized void setUsbStorageNotification(int titleId, int messageId, int icon, boolean sound, boolean visible, PendingIntent pi) { // force to show UsbSettings screen to select usb mode if property is true if (SystemProperties.getBoolean("persist.sys.ums", true)) { titleId = 0; messageId = 0; icon = 0; sound = false; visible = false; pi = null; } if (!visible && mUsbStorageNotification == null) { return; Loading
services/java/com/android/server/MountService.java 100644 → 100755 +67 −47 Original line number Diff line number Diff line Loading @@ -1134,10 +1134,14 @@ class MountService extends IMountService.Stub mSendUmsConnectedOnBoot = avail; } final StorageVolume primary = getPrimaryPhysicalVolume(); if (avail == false && primary != null && Environment.MEDIA_SHARED.equals(getVolumeState(primary.getPath()))) { final String path = primary.getPath(); final ArrayList<String> volumes = getShareableVolumes(); boolean mediaShared = false; for (String path : volumes) { if (getVolumeState(path).equals(Environment.MEDIA_SHARED)) mediaShared = true; } if (avail == false && mediaShared) { /* * USB mass storage disconnected while enabled */ Loading @@ -1147,12 +1151,17 @@ class MountService extends IMountService.Stub try { int rc; Slog.w(TAG, "Disabling UMS after cable disconnect"); for (String path : volumes) { if (getVolumeState(path).equals(Environment.MEDIA_SHARED)) { doShareUnshareVolume(path, "ums", false); if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) { rc = doMountVolume(path); if (rc != StorageResultCode.OperationSucceeded) { Slog.e(TAG, String.format( "Failed to remount {%s} on UMS enabled-disconnect (%d)", path, rc)); } } } } catch (Exception ex) { Slog.w(TAG, "Failed to mount media on UMS enabled-disconnect", ex); } Loading Loading @@ -1357,7 +1366,9 @@ class MountService extends IMountService.Stub // Watch for USB changes on primary volume final StorageVolume primary = getPrimaryPhysicalVolume(); if (primary != null && primary.allowMassStorage()) { if ((primary != null && primary.allowMassStorage()) || //ignore primary config, force to register if property is true SystemProperties.getBoolean("persist.sys.ums", true)) { mContext.registerReceiver( mUsbReceiver, new IntentFilter(UsbManager.ACTION_USB_STATE), null, mHandler); } Loading Loading @@ -1490,6 +1501,19 @@ class MountService extends IMountService.Stub } } private ArrayList<String> getShareableVolumes() { // Sharable volumes have android:allowMassStorage="true" in storage_list.xml ArrayList<String> volumesToMount = new ArrayList<String>(); synchronized (mVolumes) { for (StorageVolume v : mVolumes) { if (v.allowMassStorage()) { volumesToMount.add(v.getPath()); } } } return volumesToMount; } public boolean isUsbMassStorageConnected() { waitForReady(); Loading @@ -1505,15 +1529,11 @@ class MountService extends IMountService.Stub waitForReady(); validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); final StorageVolume primary = getPrimaryPhysicalVolume(); if (primary == null) return; // TODO: Add support for multiple share methods for (String path : getShareableVolumes()) { /* * If the volume is mounted and we're enabling then unmount it */ String path = primary.getPath(); String vs = getVolumeState(path); String method = "ums"; if (enable && vs.equals(Environment.MEDIA_MOUNTED)) { Loading @@ -1540,16 +1560,16 @@ class MountService extends IMountService.Stub } } } } public boolean isUsbMassStorageEnabled() { waitForReady(); final StorageVolume primary = getPrimaryPhysicalVolume(); if (primary != null) { return doGetVolumeShared(primary.getPath(), "ums"); } else { return false; for (String path : getShareableVolumes()) { if (doGetVolumeShared(path, "ums")) return true; } // no volume is shared return false; } /** Loading
services/java/com/android/server/usb/UsbDeviceManager.java +2 −0 Original line number Diff line number Diff line Loading @@ -594,6 +594,8 @@ public class UsbDeviceManager { case MSG_UPDATE_STATE: mConnected = (msg.arg1 == 1); mConfigured = (msg.arg2 == 1); StorageManager storageManager = StorageManager.from(mContext); storageManager.setUsbMassStorageEnabled(mConnected); updateUsbNotification(); updateAdbNotification(); if (containsFunction(mCurrentFunctions, Loading