状态栏不弹出“已连接USB”的解决办法
我们将Android5.1设备与PC(计算机/电脑)连接时,USB计算机连接方式有:USB 存储设备、媒体设备(MTP)、相机(PTP)、只充电。比如,我要使用 “USB存储设备” 这个USB连接方式:
那么,状态栏会弹出,“已连接USB” 的通知,并且你可以点击进去,打开USB存储设备:
将Android设备与PC连接设置为 “USB存储设备” 模式,就是将Android设备的内部存储器挂载到PC,当做U盘使用。
---------------------------------------------分割线--------------------------------------------
现在在我们的工业平板上遇到状态栏不弹出“已连接USB”,那么就不能打开USB存储设备:
虽然,不使用“USB存储设备” 模式作为Android设备与PC连接方式,“媒体设备(MTP)”模式也可以在Android与PC之间进行数据传输;但是,有时候应用程序需要使用“USB存储设备”这个模式,APP新建的文件/文件夹可以实时更新到PC,其他模式却不行。因此,需要“USB存储设备” 模式能够正常使用!
排查问题过程:
1. 查找关键字
通过查找关键字“已连接USB”,追踪到frameworks/base/packages/SystemUI/src/com/android/systemui/usb/StorageNotification.java文件。在updateUsbMassStorageNotification方法中,通过打印日志发现 available 和 isStorageCanShared 值为false,导致“已连接USB”通知弹不出来!
2. 文件对比
available 和 isStorageCanShared 值 与 volumes.getPath() 和 mStorageManager.getVolumeState(path); 相关,通过对比能够正常使用“USB存储设备”的StorageNotification.java文件,StorageNotification.java没有异常。
3. 检查配置文件
需要检查配置文件是ProjectConfig.mk、<project>_defconfig,通过与正常的配置文件进行对比检查,发现如下异常:
在ProjectConfig.mk文件中:
异常:MTK_MULTI_PARTITION_MOUNT_ONLY_SUPPORT = yes
MTK_OWNER_SDCARD_ONLY_SUPPORT = yes
MTK_SHARED_SDCARD = yes
正常:MTK_MULTI_PARTITION_MOUNT_ONLY_SUPPORT = no
MTK_OWNER_SDCARD_ONLY_SUPPORT = no
MTK_SHARED_SDCARD = no
在<project>_defconfig文件中:
异常:CONFIG_MTK_SHARED_SDCARD=y
CONFIG_MTK_MULTI_PARTITION_MOUNT_ONLY_SUPPORT=y
正常:# CONFIG_MTK_SHARED_SDCARD is not set
# CONFIG_MTK_MULTI_PARTITION_MOUNT_ONLY_SUPPORT is not set
4. 问题处理
将上述ProjectConfig.mk、<project>_defconfig文件中出现的问题进行修改:在ProjectConfig.mk文件中:
MTK_MULTI_PARTITION_MOUNT_ONLY_SUPPORT = yes --> MTK_MULTI_PARTITION_MOUNT_ONLY_SUPPORT = no
MTK_OWNER_SDCARD_ONLY_SUPPORT = yes --> MTK_OWNER_SDCARD_ONLY_SUPPORT = no
MTK_SHARED_SDCARD = yes --> MTK_SHARED_SDCARD = no
在<project>_defconfig文件中:
CONFIG_MTK_SHARED_SDCARD=y --> # CONFIG_MTK_SHARED_SDCARD is not se
CONFIG_MTK_MULTI_PARTITION_MOUNT_ONLY_SUPPORT=y --> # CONFIG_MTK_MULTI_PARTITION_MOUNT_ONLY_SUPPORT is not set
修改完成之后make clean,并且全部重新编译Android5.1源码,将编译好的源码,更新到我们的工业平板上,即可解决“USB存储设备” 模式不可用的问题。
那么,状态栏会弹出,“已连接USB” 的通知,并且你可以点击进去,打开USB存储设备:
将Android设备与PC连接设置为 “USB存储设备” 模式,就是将Android设备的内部存储器挂载到PC,当做U盘使用。
---------------------------------------------分割线--------------------------------------------
现在在我们的工业平板上遇到状态栏不弹出“已连接USB”,那么就不能打开USB存储设备:
虽然,不使用“USB存储设备” 模式作为Android设备与PC连接方式,“媒体设备(MTP)”模式也可以在Android与PC之间进行数据传输;但是,有时候应用程序需要使用“USB存储设备”这个模式,APP新建的文件/文件夹可以实时更新到PC,其他模式却不行。因此,需要“USB存储设备” 模式能够正常使用!
排查问题过程:
1. 查找关键字
通过查找关键字“已连接USB”,追踪到frameworks/base/packages/SystemUI/src/com/android/systemui/usb/StorageNotification.java文件。在updateUsbMassStorageNotification方法中,通过打印日志发现 available 和 isStorageCanShared 值为false,导致“已连接USB”通知弹不出来!
2. 文件对比
available 和 isStorageCanShared 值 与 volumes.getPath() 和 mStorageManager.getVolumeState(path); 相关,通过对比能够正常使用“USB存储设备”的StorageNotification.java文件,StorageNotification.java没有异常。
- /**
- * Update the state of the USB mass storage notification
- */
- void updateUsbMassStorageNotification(boolean available) {
- boolean isStorageCanShared = isAbleToShare();
- Slog.d(TAG, "updateUsbMassStorageNotification - isStorageCanShared=" + isStorageCanShared + ",available=" + available);
- if( !mStorageManager.isUsbMassStorageEnabled() || mLastState.equals(Environment.MEDIA_BAD_REMOVAL) ) {
- /* Show "USB Connected" notification, if the system want it and there is more than one storage can be shared. */
- /* Like SHARED SD, there is an internal storage, but that can not be shared. So don't show notification. */
- if (available && isStorageCanShared) { // available 与 isStorageCanShared 的值都为false
- Slog.d(TAG, "updateUsbMassStorageNotification - [true]");
- Intent intent = new Intent();
- intent.setClass(mContext, com.android.systemui.usb.UsbStorageActivity.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0);
- setUsbStorageNotification(
- com.android.internal.R.string.usb_storage_notification_title,
- com.android.internal.R.string.usb_storage_notification_message,
- com.android.internal.R.drawable.stat_sys_data_usb,
- false, true, pi);
- } else if(!available && !isStorageCanShared || !mUmsAvailable) {
- /* Cancel "USB Connected" notification, if the system want to cancel it and there is no storage can be shared. */
- /* Like SD hot-plug, remove the external SD card, but still one storage can be shared. So don't cancel the notification. */
- Slog.d(TAG, "updateUsbMassStorageNotification - [false]");
- setUsbStorageNotification(0, 0, 0, false, false, null);
- } else {
- Slog.d(TAG, "updateUsbMassStorageNotification - Cannot as your wish!");
- /* When there is no partition to share (NOFS/BAD/...), invisible this notification */
- Intent intent = new Intent();
- intent.setClass(mContext, com.android.systemui.usb.UsbStorageActivity.class);
- PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0);
- setUsbStorageNotification(
- com.android.internal.R.string.usb_storage_stop_notification_title,
- com.android.internal.R.string.usb_storage_stop_notification_message,
- com.android.internal.R.drawable.stat_sys_warning, false, false, pi);
- }
- mLastConnected = available;
- } else {
- Slog.d(TAG, "updateUsbMassStorageNotification - UMS Enabled");
- }
- }
- private void onUsbMassStorageConnectionChangedAsync(boolean connected) {
- mUmsAvailable = connected;
- /*
- * Even though we may have a UMS host connected, we the SD card
- * may not be in a state for export.
- */
- int allowedShareNum = 0;
- String st = "";
- String path = "";
- StorageVolume[] volumes = mStorageManager.getVolumeList();
- if (volumes != null) {
- for (int i = 0; i < volumes.length; i++) {
- if (volumes[i].allowMassStorage() && !volumes[i].isEmulated()) {
- path = volumes[i].getPath();
- st = mStorageManager.getVolumeState(path);
- if (!(st.equals(Environment.MEDIA_REMOVED) || st.equals(Environment.MEDIA_CHECKING)|| st.equals(Environment.MEDIA_BAD_REMOVAL))) {
- /* got a truly sharable volume */
- allowedShareNum++;
- }
- }
- }
- }
- if(connected && allowedShareNum == 0){
- /* only changed connceted here */
- Slog.d(TAG, "change connected from true -> false");
- connected = false;
- }
- if (st != null) {
- if (DEBUG) Log.i(TAG, String.format("UMS connection changed to %s (media state %s), (path %s)",
- connected, st, path));
- Slog.d(TAG, "onUsbMassStorageConnectionChangedAsync - mLastState: " + mLastState + ", st: " + st + ", mLastConnected: " + mLastConnected+ ", connected: " + connected + ", path: " + path);
- if (!connected) {
- mUsbNotifications.clear();
- updateUsbMassStorageNotification(connected);
- Slog.d(TAG, "onUsbMassStorageConnectionChangedAsync - Disconnect");
- } else {
- String mCurrentFunctions = SystemProperties.get("sys.usb.config", "none");
- if (containsFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_MASS_STORAGE)) {
- Slog.d(TAG, "onUsbMassStorageConnectionChangedAsync - Connect - UMS");
- if (mLastState.equals(st) && mLastConnected == connected && !mAlarmBootOff) {
- Slog.d(TAG, "onUsbMassStorageConnectionChangedAsync - Connect - UMS - Ignore");
- return;
- }
- updateUsbMassStorageNotification(connected);
- } else {
- Slog.d(TAG, "onUsbMassStorageConnectionChangedAsync - Connect - MTP");
- setUsbStorageNotification(0, 0, 0, false, false, null);
- mLastConnected = connected;
- }
- }
- }
- mLastConnected = connected;
- Slog.d(TAG, "onUsbMassStorageConnectionChangedAsync - mLastConnected: " + mLastConnected);
- }
3. 检查配置文件
需要检查配置文件是ProjectConfig.mk、<project>_defconfig,通过与正常的配置文件进行对比检查,发现如下异常:
在ProjectConfig.mk文件中:
异常:MTK_MULTI_PARTITION_MOUNT_ONLY_SUPPORT = yes
MTK_OWNER_SDCARD_ONLY_SUPPORT = yes
MTK_SHARED_SDCARD = yes
正常:MTK_MULTI_PARTITION_MOUNT_ONLY_SUPPORT = no
MTK_OWNER_SDCARD_ONLY_SUPPORT = no
MTK_SHARED_SDCARD = no
在<project>_defconfig文件中:
异常:CONFIG_MTK_SHARED_SDCARD=y
CONFIG_MTK_MULTI_PARTITION_MOUNT_ONLY_SUPPORT=y
正常:# CONFIG_MTK_SHARED_SDCARD is not set
# CONFIG_MTK_MULTI_PARTITION_MOUNT_ONLY_SUPPORT is not set
4. 问题处理
将上述ProjectConfig.mk、<project>_defconfig文件中出现的问题进行修改:在ProjectConfig.mk文件中:
MTK_MULTI_PARTITION_MOUNT_ONLY_SUPPORT = yes --> MTK_MULTI_PARTITION_MOUNT_ONLY_SUPPORT = no
MTK_OWNER_SDCARD_ONLY_SUPPORT = yes --> MTK_OWNER_SDCARD_ONLY_SUPPORT = no
MTK_SHARED_SDCARD = yes --> MTK_SHARED_SDCARD = no
在<project>_defconfig文件中:
CONFIG_MTK_SHARED_SDCARD=y --> # CONFIG_MTK_SHARED_SDCARD is not se
CONFIG_MTK_MULTI_PARTITION_MOUNT_ONLY_SUPPORT=y --> # CONFIG_MTK_MULTI_PARTITION_MOUNT_ONLY_SUPPORT is not set
修改完成之后make clean,并且全部重新编译Android5.1源码,将编译好的源码,更新到我们的工业平板上,即可解决“USB存储设备” 模式不可用的问题。