From 8218d3ba180505a52998caa1fd9f6a1647984e7f Mon Sep 17 00:00:00 2001 From: Li Gang Date: Fri, 20 Jan 2012 11:48:47 +0800 Subject: 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 Signed-off-by: Xiaokang Qin Signed-off-by: Bruce Beare Signed-off-by: Jack Ren Author-tracking-BZ: 47043 --- res/values-zh-rCN/strings.xml | 1 + res/values/strings.xml | 4 ++++ src/com/android/settings/deviceinfo/Memory.java | 24 +++++++++++++++++--- .../StorageVolumePreferenceCategory.java | 26 ++++++++++++++++++++++ 4 files changed, 52 insertions(+), 3 deletions(-) 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 @@ "格式化 SD 卡" "清除内部 USB 存储设备中的全部数据,例如音乐和照片" "清除 SD 卡中的全部数据,例如音乐和照片" + 正在使用媒体设备(MTP)或相机(PTP)传输模式 " (只读)" "要卸载 USB 存储设备吗?" "要卸载 SD 卡吗?" 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 @@ Erases all data on the SD card, such as music and photos + + + MTP or PTP function is active + \u0020(Read-only) Unmount USB storage? 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.DialogInterface; 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 @@ public class Memory extends SettingsPreferenceFragment { private StorageManager mStorageManager = null; + private UsbManager mUsbManager = null; + private StorageVolumePreferenceCategory mInternalStorageVolumePreferenceCategory; private StorageVolumePreferenceCategory[] mStorageVolumePreferenceCategories; @@ -72,6 +75,8 @@ public class Memory extends SettingsPreferenceFragment { 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 @@ public class Memory extends SettingsPreferenceFragment { 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 @@ public class Memory extends SettingsPreferenceFragment { 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.pm.IPackageManager; 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 @@ public class StorageVolumePreferenceCategory extends PreferenceCategory implemen private boolean mAllowFormat; + private boolean mUsbConnected; + private String mUsbFunction; + static class CategoryInfo { final int mTitle; final int mColor; @@ -305,6 +309,23 @@ public class StorageVolumePreferenceCategory extends PreferenceCategory implemen 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 @@ public class StorageVolumePreferenceCategory extends PreferenceCategory implemen measure(); } + public void onUsbStateChanged(boolean isUsbConnected, String usbFunction) { + mUsbConnected = isUsbConnected; + mUsbFunction = usbFunction; + measure(); + } public void onMediaScannerFinished() { measure(); } -- cgit v1.1