1. 程式人生 > >【定製Android系統】Android 7.1 預設的 USB 配置模式,預設的 usb debug 配置

【定製Android系統】Android 7.1 預設的 USB 配置模式,預設的 usb debug 配置

需求:在 user 版中,插入 usb 連線電腦時,預設只有 mtp (傳檔案)功能,關閉 adb 功能。在 eng 版中,插入 usb 連線電腦時,預設開啟 adb 功能,且兼具 mtp (傳檔案)功能。
提示:

  • Android 4.4 之後貌似修改了整個 usb 模式配置的 方案,從 DatabaseHelper.java 中配置預設功能是行不通的了。
  • 試圖在 SystemServer.java 中,Android 第一次啟動時 setSystemProperties 也是行不通的。
  • 注意,儘量不要使用 .mk 的方法新增 PRODUCT_PROPERTIES_OVERRIDES persist.sys.usb.config 的方案來配置,這種適合配置無論 eng 還是 user 均使用同一模式的情況。但是,同樣不建議這麼做,因為這麼做之後,下面我將要說的這個方案就會不起作用了。

解決方案:

一種比較合理的解決方案是,儘量少地改動原始碼。從 build 目錄下進行修改。
首先確認一下,build/core/main.mk 中關於 ro.debuggable 的配置原始碼沒有被晶片供應商改動過。其原始碼應為:

420 ifeq (true,$(strip $(enable_target_debugging)))
421   # Target is more debuggable and adbd is on by default
422   ADDITIONAL_DEFAULT_PROPERTIES += ro.debuggable=1
423   # Enable Dalvik lock contention logging.
424 ADDITIONAL_BUILD_PROPERTIES += dalvik.vm.lockprof.threshold=500 425 # Include the debugging/testing OTA keys in this build. 426 INCLUDE_TEST_OTA_KEYS := true 427 else # !enable_target_debugging 428 # Target is less debuggable and adbd is off by default 429 ADDITIONAL_DEFAULT_PROPERTIES += ro.debuggable=0
430 endif # !enable_target_debugging

其大致意思是, 如果編譯的是 eng 版本,那麼就把 ro.debuggable 屬性置為 1,否則,置為0。

緊接著,在 build/tools/post_process_props.py 中,對相關預設配置進行修改,其原始碼本為:

28  # Put the modifications that you need to make into the /system/build.prop into this
29  # function. The prop object has get(name) and put(name,value) methods.
30  def mangle_build_prop(prop):
31      pass
32
33  # Put the modifications that you need to make into the /default.prop into this
34  # function. The prop object has get(name) and put(name,value) methods.
35  def mangle_default_prop(prop):
36      # If ro.debuggable is 1, then enable adb on USB by default
37      # (this is for userdebug builds)
38      if prop.get("ro.debuggable") == "1":
39          val = prop.get("persist.sys.usb.config")
40      if "adb" not in val:
41          if val == "":
42              val = "adb"
43          else:
44              val = val + ",adb"
45          prop.put("persist.sys.usb.config", val)
46  # UsbDeviceManager expects a value here.  If it doesn't get it, it will
47  # default to "adb". That might not the right policy there, but it's better
48  # to be explicit.
49  if not prop.get("persist.sys.usb.config"):
50      prop.put("persist.sys.usb.config", "none");

其大致意思是,如果 ro.debuggable == 1,即 eng 版本,那麼先獲取一下 persist.sys.usb.config 的值,如果 “adb” 不存在於這個值裡面,就把 “adb” 加進去。然後再設定一下 persist.sys.usb.config 這個值。如果 ro.debuggable == 0 的話,就不操作,如果無法獲取 persist.sys.usb.config 的值,就把它置為 “none”。
因此,如果之前在 .mk 檔案中設定過 persist.sys.usb.config 的屬性,這裡也可以把 “adb” 從原屬性中去掉,但是會顯得很混亂,因此是不建議從 .mk 檔案中配置預設的 USB 連線配置的。
根據本文的需求,可以將上述原始碼做如下修改:

28  # Put the modifications that you need to make into the /system/build.prop into this
29  # function. The prop object has get(name) and put(name,value) methods.
30  def mangle_build_prop(prop):
31      pass
32
33  # Put the modifications that you need to make into the /default.prop into this
34  # function. The prop object has get(name) and put(name,value) methods.
35  def mangle_default_prop(prop):
36      # If ro.debuggable is 1, then enable adb on USB by default
37      # (this is for userdebug builds)
38      if prop.get("ro.debuggable") == "1":
39          val = prop.get("persist.sys.usb.config")
40      if "adb" not in val:
41          if val == "":
42              val = "mtp,adb"
43          else:
44              val = val + ",adb"
45          prop.put("persist.sys.usb.config", val)
46  # UsbDeviceManager expects a value here.  If it doesn't get it, it will
47  # default to "adb". That might not the right policy there, but it's better
48  # to be explicit.
49  if not prop.get("persist.sys.usb.config"):
50      prop.put("persist.sys.usb.config", "mtp");

如此,即可達到,eng 版預設為 mtp、adb 模式,即 預設打開了 usb debug。而 user 版預設為 mtp 模式。
但是,剛剛在寫這篇文章時發現一個問題,如果之前配置過 persist.sys.usb.config 屬性,但不是 mtp,則會出現 未知屬性 val 和 adb 的模式。
此外,貌似,如果在 device/ 目錄下使用 .mk 檔案僅對 mtp 進行配置時,即只寫入 persist.sys.usb.config=mtp 的話,同樣無法滿足需求。這種情況我記不清具體的現象了。