MountService: If USB cable is unplugged while UMS is active, clean up and remount

Change-Id: I9c20e7bfc215549cd8a6e14625e5d005bb53f8fa
Signed-off-by: San Mehat <san@google.com>
diff --git a/services/java/com/android/server/MountService.java b/services/java/com/android/server/MountService.java
index 713358d..d4d5336 100644
--- a/services/java/com/android/server/MountService.java
+++ b/services/java/com/android/server/MountService.java
@@ -566,6 +566,7 @@
         Intent in = null;
 
         if (oldState == VolumeState.Shared && newState != oldState) {
+            if (LOCAL_LOGD) Log.d(TAG, "Sending ACTION_MEDIA_UNSHARED intent");
             mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_UNSHARED,
                                                 Uri.parse("file://" + path)));
         }
@@ -607,6 +608,7 @@
 
             updatePublicVolumeState(path, Environment.MEDIA_SHARED);
             in = new Intent(Intent.ACTION_MEDIA_SHARED, Uri.parse("file://" + path));
+            if (LOCAL_LOGD) Log.d(TAG, "Sending ACTION_MEDIA_SHARED intent");
         } else if (newState == VolumeState.SharedMnt) {
             Log.e(TAG, "Live shared mounts not supported yet!");
             return;
@@ -798,6 +800,29 @@
         } else {
             mSendUmsConnectedOnBoot = avail;
         }
+
+        final String path = Environment.getExternalStorageDirectory().getPath();
+        if (avail == false && getVolumeState(path).equals(Environment.MEDIA_SHARED)) {
+            /*
+             * USB mass storage disconnected while enabled
+             */
+            new Thread() {
+                public void run() {
+                    try {
+                        int rc;
+                        Log.w(TAG, "Disabling UMS after cable disconnect");
+                        doShareUnshareVolume(path, "ums", false);
+                        if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
+                            Log.e(TAG, String.format(
+                                    "Failed to remount {%s} on UMS enabled-disconnect (%d)",
+                                            path, rc));
+                        }
+                    } catch (Exception ex) {
+                        Log.w(TAG, "Failed to mount media on UMS enabled-disconnect", ex);
+                    }
+                }
+            }.start();
+        }
     }
 
     private void sendUmsIntent(boolean c) {