当前位置:   article > 正文

Android11编译第二弹:USB连接MTP模式+USB调试+USB信任_aosp给设置usb权限白名单

aosp给设置usb权限白名单

一、为什么需要开启USB信任和ADB调试

问题1:原始的AOSP,如果通过USB连接设备以后,会弹窗提示用户选择连接模式:MTP,大容量磁盘,照片等模式;

问题2:USB连接设备以后,需要开启USB调试模式,才方便操作adb调试;

问题3:USB设备连接以后,电脑会弹窗是否信任设备,需要点击信任当前设备,否则设备连接不成功。

使用场景:我们是自己的系统,需要支持设备USB连接,以及adb远程调试(USB调试和TCP调回),因此,系统需要默认支持USB连接,支持USB调试,默认信任连接的设备。

二、集成步骤

2.1 关闭USB调试弹窗

frameworks/base/packages/SystemUI/src/com/android/systemui/usb/UsbDebuggingActivity.java

  1. @Override
  2. public void onCreate(Bundle icicle) {
  3. ...(省略)
  4. /**
  5. final AlertController.AlertParams ap = mAlertParams;
  6. ap.mTitle = getString(R.string.usb_debugging_title);
  7. ap.mMessage = getString(R.string.usb_debugging_message, fingerprints);
  8. ap.mPositiveButtonText = getString(R.string.usb_debugging_allow);
  9. ap.mNegativeButtonText = getString(android.R.string.cancel);
  10. ap.mPositiveButtonListener = this;
  11. ap.mNegativeButtonListener = this;
  12. // add "always allow" checkbox
  13. LayoutInflater inflater = LayoutInflater.from(ap.mContext);
  14. View checkbox = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null);
  15. mAlwaysAllow = (CheckBox)checkbox.findViewById(com.android.internal.R.id.alwaysUse);
  16. mAlwaysAllow.setText(getString(R.string.usb_debugging_always));
  17. ap.mView = checkbox;
  18. window.setCloseOnTouchOutside(false);
  19. setupAlert();
  20. **/
  21. notifyService(true,true);
  22. finish();
  23. }

默认只要USB连接设备,默认就开启USB调试。关键方法是notifyService

  1. /**
  2. * Notifies the ADB service as to whether the current ADB request should be allowed, and if
  3. * subsequent requests from this key should be allowed without user consent.
  4. *
  5. * @param allow whether the connection should be allowed
  6. * @param alwaysAllow whether subsequent requests from this key should be allowed without user
  7. * consent
  8. */
  9. private void notifyService(boolean allow, boolean alwaysAllow) {
  10. try {
  11. IBinder b = ServiceManager.getService(ADB_SERVICE);
  12. IAdbManager service = IAdbManager.Stub.asInterface(b);
  13. if (allow) {
  14. service.allowDebugging(alwaysAllow, mKey);
  15. } else {
  16. service.denyDebugging();
  17. }
  18. mServiceNotified = true;
  19. } catch (Exception e) {
  20. Log.e(TAG, "Unable to notify Usb service", e);
  21. }
  22. }

这个方法很简单,通过ADB服务,设置允许调试。

mKey是什么呢?就是设备指纹,即我们常说的设备SN。

2.2 授权ADB调试5555端口

device/qcom/lahaina/init.target.rc

  1. #Allow access to memory hotplug device attributes
  2. chown system system /sys/kernel/mem-offline/anon_migrate
  3. # ==== modify start===== zhouronghua open adb debug port 5555
  4. setprop service.adb.tcp.port 5555
  5. # ==== modify end=====

2.3 接收到USB连接默认开启USB调试

frameworks/base/packages/SystemUI/src/com/android/systemui/usb/UsbDebuggingActivity.java

在其中有监听USB连接和断开的广播接收器UsbDisconnectedReceiver,

  1. private class UsbDisconnectedReceiver extends BroadcastReceiver {
  2. ...(省略)
  3. @Override
  4. public void onReceive(Context content, Intent intent) {
  5. String action = intent.getAction();
  6. if (!UsbManager.ACTION_USB_STATE.equals(action)) {
  7. return;
  8. }
  9. // ====== modify begin ==========
  10. // fix: zhouronghua usb debug default auth
  11. // boolean connected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
  12. boolean connected = false;
  13. if (!connected) {
  14. Log.d(TAG, "USB disconnected, notifying service");
  15. // notifyService(false);
  16. mActivity.finish();
  17. }
  18. // open adb debugging
  19. notifyService(true, true);
  20. // =========== modify end ==========
  21. }
  22. }

开启USB调试,直接开启ADB调试即可,不需要单独开启USB调试,TCP调试。

2.4 USB授权默认信息

frameworks/base/packages/SystemUI/src/com/android/systemui/usb/UsbPermissionActivity.java

  1. @Override
  2. public void onCreate(Bundle icicle) {
  3. ...(省略)
  4. setupAlert();
  5. // ======= modify begin =========
  6. // fix: zhouronghua usb default trust devices
  7. Log.d(TAG, "grant usb permission automatically");
  8. mPermissionGranted = true;
  9. finish();
  10. // ======= modify end =======
  11. }

默认同意授权USB连接。

 2.5 USB连接默认为MTP模式

USB管理器接收到USB连接监听

frameworks/base/services/usb/java/com/android/server/usb/UsbDeviceManager.java

USB连接默认采用MTP模式,且开启ADB调试。

  1. @Override
  2. public void handleMessage(Message msg) {
  3. ...(省略)
  4. if (mBootCompleted) {
  5. if (!mConnected && !hasMessages(MSG_ACCESSORY_MODE_ENTER_TIMEOUT)
  6. && !hasMessages(MSG_FUNCTION_SWITCH_TIMEOUT)) {
  7. // restore defaults when USB is disconnected
  8. // fix: zhouronghua usb debug mode
  9. //if (!mScreenLocked
  10. // && mScreenUnlockedFunctions != UsbManager.FUNCTION_NONE) {
  11. // setScreenUnlockedFunctions();
  12. //} else {
  13. //setEnabledFunctions(UsbManager.FUNCTION_NONE, false);
  14. // USB connect enable
  15. setEnabledFunctions(UsbManager.USB_FUNCTION_MTP, false);
  16. // adb enable
  17. setAdbEnabled(true)
  18. //}
  19. }
  20. updateUsbFunctions();
  21. }
  22. protected void finishBoot() {
  23. if (mBootCompleted && mCurrentUsbFunctionsReceived && mSystemReady) {
  24. if (mPendingBootBroadcast) {
  25. updateUsbStateBroadcastIfNeeded(getAppliedFunctions(mCurrentFunctions));
  26. mPendingBootBroadcast = false;
  27. }
  28. // ========== modify start ===========
  29. //if (!mScreenLocked
  30. // && mScreenUnlockedFunctions != UsbManager.FUNCTION_NONE) {
  31. // setScreenUnlockedFunctions();
  32. //} else {
  33. // setEnabledFunctions(UsbManager.FUNCTION_NONE, false);
  34. //}
  35. // fix: zhouronghua USB enable
  36. setEnabledFunctions(UsbManager.USB_FUNCTION_MTP, false);
  37. // adb enable
  38. setAdbEnabled(true);
  39. // ======== modify end =================

2.6 Release默认开启ADB调试

build/make/core/main.mk

  1. build/make/core/main.mk
  2. ifeq (true,$(strip $(enable_target_debugging)))
  3. # Target is more debuggable and adbd is on by default
  4. ADDITIONAL_DEFAULT_PROPERTIES += ro.debuggable=1
  5. # Enable Dalvik lock contention logging.
  6. ADDITIONAL_BUILD_PROPERTIES += dalvik.vm.lockprof.threshold=500
  7. else # !enable_target_debugging
  8. # Target is less debuggable and adbd is off by default
  9. # ==== mofify begin ===== zhouronghua fix: adb debug mode
  10. ADDITIONAL_DEFAULT_PROPERTIES += ro.debuggable=1
  11. # ==== modify end ====
  12. endif # !enable_target_debugging

在Release版本也允许进行ADB调试

2.7 post_process_props.py开启adb调试

build/make/tools/post_process_props.py

  1. # 1. 注释掉if prop.get("ro.debuggable") == "1"判断,Release也允许调试
  2. # ==== modify begin fix: zhoronghua open adb debug mode
  3. # if prop.get("ro.debuggable") == "1":
  4. # ==== modify end ====
  5. # 因为注释掉判断,判断体重的内容整体往前缩进
  6. val = prop.get("persist.sys.usb.config")
  7. if "adb" not in val:
  8. if val == "":
  9. val = "adb"
  10. else:
  11. val = val + ",adb"
  12. prop.put("persist.sys.usb.config", val)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/448139
推荐阅读
相关标签
  

闽ICP备14008679号