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
This commit is contained in:
Li Gang
2012-01-20 11:48:47 +08:00
committed by Xiaokang Qin
parent 5d72b27267
commit 8218d3ba18
4 changed files with 52 additions and 3 deletions

View File

@@ -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>

View File

@@ -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>

View File

@@ -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,11 +246,20 @@ public class Memory extends SettingsPreferenceFragment {
private final BroadcastReceiver mMediaScannerReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
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();
}
}
}
};
@Override

View File

@@ -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();
}