MTP/PTP: disable erasing/unmount operation for SD card while MTP/PTP is active

Check the USB connection status and MTP/PTP setting, if USB connected and MTP/PTP enabled,
disable the erasing and umount operation.
Judge SD card Mounting status and keep summary string.

Change-Id: Iff3e0f25b581bc462c88fff59eb93d765baaddd7
Author: Li Gang <gang.g.li@intel.com>
Signed-off-by: Xiaokang Qin <xiaokang.qin@intel.com>
Signed-off-by: Bruce Beare <bruce.j.beare@intel.com>
Signed-off-by: Jack Ren <jack.ren@intel.com>
Author-tracking-BZ: 47043
diff --git a/res/values-zh-rCN/strings.xml b/res/values-zh-rCN/strings.xml
index c6fc4d4..3f5b978 100644
--- a/res/values-zh-rCN/strings.xml
+++ b/res/values-zh-rCN/strings.xml
@@ -771,6 +771,7 @@
     <string name="sd_format" product="default" msgid="2576054280507119870">"格式化 SD 卡"</string>
     <string name="sd_format_summary" product="nosdcard" msgid="6331905044907914603">"清除内部 USB 存储设备中的全部数据,例如音乐和照片"</string>
     <string name="sd_format_summary" product="default" msgid="212703692181793109">"清除 SD 卡中的全部数据,例如音乐和照片"</string>
+    <string name="mtp_ptp_mode_summary">正在使用媒体设备(MTP)或相机(PTP)传输模式</string>
     <string name="read_only" msgid="6702420168629076340">" (只读)"</string>
     <string name="dlg_confirm_unmount_title" product="nosdcard" msgid="3077285629197874055">"要卸载 USB 存储设备吗?"</string>
     <string name="dlg_confirm_unmount_title" product="default" msgid="3634502237262534381">"要卸载 SD 卡吗?"</string>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 5a774f0..7015988 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -1898,6 +1898,10 @@
     <!-- SD card & phone storage settings item title that will result in the phone unmounting the SD card.   [CHAR LIMIT=80] -->
     <string name="sd_format_summary" product="default">Erases all data on the SD card, such as music and photos</string>
     <!-- SD card status when it is mounted as read only. Will be appended to size, starts with an unbreakable space -->
+
+    <!-- SD card & phone storage settings item summary that will result in the phone connected to PC and MTP/PTP enabled.   [CHAR LIMIT=80] -->
+    <string name="mtp_ptp_mode_summary">MTP or PTP function is active</string>
+
     <string name="read_only">\u0020(Read-only)</string>
     <!-- SD card eject confirmation dialog title   [CHAR LIMIT=25] -->
     <string name="dlg_confirm_unmount_title" product="nosdcard">Unmount USB storage?</string>
diff --git a/src/com/android/settings/deviceinfo/Memory.java b/src/com/android/settings/deviceinfo/Memory.java
index cb344bf..d84d219 100644
--- a/src/com/android/settings/deviceinfo/Memory.java
+++ b/src/com/android/settings/deviceinfo/Memory.java
@@ -24,6 +24,7 @@
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.res.Resources;
+import android.hardware.usb.UsbManager;
 import android.os.Bundle;
 import android.os.Environment;
 import android.os.IBinder;
@@ -65,6 +66,8 @@
 
     private StorageManager mStorageManager = null;
 
+    private UsbManager mUsbManager = null;
+
     private StorageVolumePreferenceCategory mInternalStorageVolumePreferenceCategory;
     private StorageVolumePreferenceCategory[] mStorageVolumePreferenceCategories;
 
@@ -72,6 +75,8 @@
     public void onCreate(Bundle icicle) {
         super.onCreate(icicle);
 
+        mUsbManager = (UsbManager)getSystemService(Context.USB_SERVICE);
+
         if (mStorageManager == null) {
             mStorageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
             mStorageManager.registerListener(mStorageListener);
@@ -119,6 +124,10 @@
         intentFilter.addDataScheme("file");
         getActivity().registerReceiver(mMediaScannerReceiver, intentFilter);
 
+        intentFilter = new IntentFilter();
+        intentFilter.addAction(UsbManager.ACTION_USB_STATE);
+        getActivity().registerReceiver(mMediaScannerReceiver, intentFilter);
+
         if (mInternalStorageVolumePreferenceCategory != null) {
             mInternalStorageVolumePreferenceCategory.onResume();
         }
@@ -237,9 +246,18 @@
     private final BroadcastReceiver mMediaScannerReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
-            // mInternalStorageVolumePreferenceCategory is not affected by the media scanner
-            for (int i = 0; i < mStorageVolumePreferenceCategories.length; i++) {
-                mStorageVolumePreferenceCategories[i].onMediaScannerFinished();
+            String action = intent.getAction();
+            if (action.equals(UsbManager.ACTION_USB_STATE)) {
+               boolean isUsbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
+               String usbFunction = mUsbManager.getDefaultFunction();
+               for (int i = 0; i < mStorageVolumePreferenceCategories.length; i++) {
+                   mStorageVolumePreferenceCategories[i].onUsbStateChanged(isUsbConnected, usbFunction);
+               }
+            } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_FINISHED)) {
+                // mInternalStorageVolumePreferenceCategory is not affected by the media scanner
+                for (int i = 0; i < mStorageVolumePreferenceCategories.length; i++) {
+                    mStorageVolumePreferenceCategories[i].onMediaScannerFinished();
+                }
             }
         }
     };
diff --git a/src/com/android/settings/deviceinfo/StorageVolumePreferenceCategory.java b/src/com/android/settings/deviceinfo/StorageVolumePreferenceCategory.java
index 0211c77..156184a 100644
--- a/src/com/android/settings/deviceinfo/StorageVolumePreferenceCategory.java
+++ b/src/com/android/settings/deviceinfo/StorageVolumePreferenceCategory.java
@@ -24,6 +24,7 @@
 import android.content.res.Resources;
 import android.graphics.drawable.ShapeDrawable;
 import android.graphics.drawable.shapes.RectShape;
+import android.hardware.usb.UsbManager;
 import android.os.Bundle;
 import android.os.Environment;
 import android.os.Handler;
@@ -69,6 +70,9 @@
 
     private boolean mAllowFormat;
 
+    private boolean mUsbConnected;
+    private String mUsbFunction;
+
     static class CategoryInfo {
         final int mTitle;
         final int mColor;
@@ -305,6 +309,23 @@
                 removePreference(mFormatPreference);
             }
         }
+
+        if (mUsbConnected && (UsbManager.USB_FUNCTION_MTP.equals(mUsbFunction) ||
+                UsbManager.USB_FUNCTION_PTP.equals(mUsbFunction))) {
+            mMountTogglePreference.setEnabled(false);
+            if (Environment.MEDIA_MOUNTED.equals(state)) {
+                mMountTogglePreference.setSummary(mResources.getString(R.string.mtp_ptp_mode_summary));
+            }
+
+            if (mFormatPreference != null) {
+                mFormatPreference.setEnabled(false);
+                mFormatPreference.setSummary(mResources.getString(R.string.mtp_ptp_mode_summary));
+            }
+        } else if (mFormatPreference != null) {
+            mFormatPreference.setEnabled(true);
+            mFormatPreference.setSummary(mResources.getString(R.string.sd_format_summary));
+        }
+
     }
 
     public void updateApproximate(long totalSize, long availSize) {
@@ -376,6 +397,11 @@
         measure();
     }
 
+    public void onUsbStateChanged(boolean isUsbConnected, String usbFunction) {
+        mUsbConnected = isUsbConnected;
+        mUsbFunction = usbFunction;
+        measure();
+    }
     public void onMediaScannerFinished() {
         measure();
     }