[RK3399][Android7.1] 调试笔记 --- USB连接后默认是充电模式

Platform: RK3399
OS: Android 7.1
Kernel: v4.4.83

现象:

开机连接USB调试线之后,默认是充电模式而不是MTP模式,如下图:
[RK3399][Android7.1] 调试笔记 --- USB连接后默认是充电模式_第1张图片


原因:

因为默认MTP开机有bug,google源码把默认使用MTP选项给去掉了。

Clean up persistent usb state on boot.
b/31814300 was fixed, but mtp can still stick around in the
persistent config even after flashing. This block of code
will only run once, but will ensure that mtp is not in
the config after the update.
Bug: 31814300
Test: Manual
Change-Id: Icf02be38c9e1f769412ac963ed6afc14e6092bfb
(cherry-picked from commit a45dac0e83f4f907b6b42f453181a7d5c01f65f3)

diff --git a/services/usb/java/com/android/server/usb/UsbDeviceManager.java b/services/usb/java/com/android/server/usb/UsbDeviceManager.java
index fb2477b..6f68bbb 100644
--- a/services/usb/java/com/android/server/usb/UsbDeviceManager.java
+++ b/services/usb/java/com/android/server/usb/UsbDeviceManager.java
@@ -340,6 +340,16 @@
                 mAdbEnabled = UsbManager.containsFunction(getDefaultFunctions(),
                         UsbManager.USB_FUNCTION_ADB);
 
+                /**
+                 * Remove MTP from persistent config, to bring usb to a good state
+                 * after fixes to b/31814300. This block can be removed after the update
+                 */
+                String persisted = SystemProperties.get(USB_PERSISTENT_CONFIG_PROPERTY);
+                if (UsbManager.containsFunction(persisted, UsbManager.USB_FUNCTION_MTP)) {
+                    SystemProperties.set(USB_PERSISTENT_CONFIG_PROPERTY,
+                            UsbManager.removeFunction(persisted, UsbManager.USB_FUNCTION_MTP));
+                }
+
                 setEnabledFunctions(null, false, false);
 
                 String state = FileUtils.readTextFile(new File(STATE_PATH), 0, null).trim();
@@ -444,17 +454,13 @@
                             USB_PERSISTENT_CONFIG_PROPERTY, UsbManager.USB_FUNCTION_NONE));
                 SystemProperties.set(USB_PERSISTENT_CONFIG_PROPERTY, newFunction);
 
-                // Changing the persistent config also changes the normal
-                // config. Wait for this to happen before changing again.
-                waitForState(newFunction);
-
                 // Remove mtp from the config if file transfer is not enabled
                 if (oldFunctions.equals(UsbManager.USB_FUNCTION_MTP) &&
                         !mUsbDataUnlocked && enable) {
                     oldFunctions = UsbManager.USB_FUNCTION_NONE;
                 }
 
-                setEnabledFunctions(oldFunctions, false, mUsbDataUnlocked);
+                setEnabledFunctions(oldFunctions, true, mUsbDataUnlocked);
                 updateAdbNotification();
             }

因此,即使在build.prop中设置了persist.sys.usb.config=mtp,adb,系统起来之后,MTP选项还是会被去掉。


参考:

https://android.googlesource.com/platform/frameworks/base/+/dfacad026dfc1582504969f8b268e7c3970c4496^!/
Android USB相關流程解析(android4.4)
解剖Android USB MTP 的激活流程
Android 连接USB 默认选中MTP

你可能感兴趣的:(RK3399,子类__Android)