1. 程式人生 > >狀態列不彈出“已連線USB”的解決辦法

狀態列不彈出“已連線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沒有異常。

  1. /**
  2.      * Update the state of the USB mass storage notification
  3.      */
  4.     void updateUsbMassStorageNotification(boolean available) {
  5.         boolean isStorageCanShared = isAbleToShare();
  6.         Slog.d(TAG, "updateUsbMassStorageNotification - isStorageCanShared=" + isStorageCanShared + ",available=" + available);
  7.         if( !mStorageManager.isUsbMassStorageEnabled() || mLastState.equals(Environment.MEDIA_BAD_REMOVAL) ) {
  8.             /* Show "USB Connected" notification, if the system want it and there is more than one storage can be shared. */
  9.             /* Like SHARED SD, there is an internal storage, but that can not be shared. So don't show notification. */
  10.             if (available && isStorageCanShared) {   // available 與 isStorageCanShared 的值都為false
  11.                 Slog.d(TAG, "updateUsbMassStorageNotification - [true]");
  12.             Intent intent = new Intent();
  13.             intent.setClass(mContext, com.android.systemui.usb.UsbStorageActivity.class);
  14.             intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  15.             PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0);
  16.             setUsbStorageNotification(
  17.                     com.android.internal.R.string.usb_storage_notification_title,
  18.                     com.android.internal.R.string.usb_storage_notification_message,
  19.                     com.android.internal.R.drawable.stat_sys_data_usb,
  20.                     false, true, pi);
  21.             } else if(!available && !isStorageCanShared || !mUmsAvailable) {
  22.                 /* Cancel "USB Connected" notification, if the system want to cancel it and there is no storage can be shared. */
  23.                 /* Like SD hot-plug, remove the external SD card, but still one storage can be shared. So don't cancel the notification. */
  24.                 Slog.d(TAG, "updateUsbMassStorageNotification - [false]");
  25.                 setUsbStorageNotification(0, 0, 0, false, false, null);
  26.             } else {
  27.                 Slog.d(TAG, "updateUsbMassStorageNotification - Cannot as your wish!");
  28.                 /* When there is no partition to share (NOFS/BAD/...), invisible this notification */
  29.             Intent intent = new Intent();
  30.             intent.setClass(mContext, com.android.systemui.usb.UsbStorageActivity.class);
  31.             PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0);
  32.             setUsbStorageNotification(
  33.                     com.android.internal.R.string.usb_storage_stop_notification_title,
  34.                     com.android.internal.R.string.usb_storage_stop_notification_message,
  35.                     com.android.internal.R.drawable.stat_sys_warning, false, false, pi);
  36.             }
  37.             mLastConnected = available;
  38.         } else {
  39.             Slog.d(TAG, "updateUsbMassStorageNotification - UMS Enabled");
  40.         }
  41.     }
  1.     private void onUsbMassStorageConnectionChangedAsync(boolean connected) {
  2.         mUmsAvailable = connected;
  3.         /*
  4.          * Even though we may have a UMS host connected, we the SD card
  5.          * may not be in a state for export.
  6.          */
  7.         int allowedShareNum = 0;
  8.         String st = "";
  9.         String path = "";
  10.         StorageVolume[] volumes = mStorageManager.getVolumeList();
  11.         if (volumes != null) {
  12.             for (int i = 0; i < volumes.length; i++) {
  13.                 if (volumes[i].allowMassStorage() && !volumes[i].isEmulated()) {
  14.                     path = volumes[i].getPath();
  15.                     st = mStorageManager.getVolumeState(path);
  16.                     if (!(st.equals(Environment.MEDIA_REMOVED) || st.equals(Environment.MEDIA_CHECKING)|| st.equals(Environment.MEDIA_BAD_REMOVAL))) {
  17.                         /* got a truly sharable volume */
  18.                         allowedShareNum++;
  19.                     }
  20.                 }
  21.             }
  22.         }
  23.         if(connected && allowedShareNum == 0){
  24.             /* only changed connceted here */
  25.             Slog.d(TAG, "change connected from true -> false");
  26.             connected = false;
  27.         }
  28.         if (st != null) {
  29.             if (DEBUG) Log.i(TAG, String.format("UMS connection changed to %s (media state %s), (path %s)",
  30.                     connected, st, path));
  31.             Slog.d(TAG, "onUsbMassStorageConnectionChangedAsync - mLastState: " + mLastState + ", st: " + st + ", mLastConnected: " + mLastConnected+ ", connected: " + connected + ", path: " + path);
  32.             if (!connected) {
  33.                 mUsbNotifications.clear();
  34.                 updateUsbMassStorageNotification(connected);
  35.                 Slog.d(TAG, "onUsbMassStorageConnectionChangedAsync - Disconnect");
  36.             } else {
  37.                 String mCurrentFunctions = SystemProperties.get("sys.usb.config", "none");
  38.                 if (containsFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_MASS_STORAGE)) {
  39.                     Slog.d(TAG, "onUsbMassStorageConnectionChangedAsync - Connect - UMS");
  40.                     if (mLastState.equals(st) && mLastConnected == connected && !mAlarmBootOff) {
  41.                         Slog.d(TAG, "onUsbMassStorageConnectionChangedAsync - Connect - UMS - Ignore");
  42.                         return;
  43.                     }
  44.                     updateUsbMassStorageNotification(connected);
  45.                 } else {
  46.                     Slog.d(TAG, "onUsbMassStorageConnectionChangedAsync - Connect - MTP");
  47.                     setUsbStorageNotification(0, 0, 0, false, false, null);
  48.                     mLastConnected = connected;
  49.                 }
  50.             }
  51.         }
  52.         mLastConnected = connected;
  53.         Slog.d(TAG, "onUsbMassStorageConnectionChangedAsync - mLastConnected: " + mLastConnected);
  54.     }

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儲存裝置” 模式不可用的問題。