在去除SystemUI后,连接ADB调试时,无法弹出授权确认框,因而无法使用ADB,此时可按如下方式处理。

一、手动授权

自定义授权框(或服务),并将framework的处理流程重定向到自定义类。

  1. /// frameworks/base/core/res/res/values/config.xml
  2. <!-- Name of the activity or service that prompts the user to reject, accept, or whitelist
  3. an adb host's public key, when an unwhitelisted host connects to the local adbd.
  4. Can be customized for other product types -->
  5. <string name="config_customAdbPublicKeyConfirmationComponent"
  6. >com.android.systemui/com.android.systemui.usb.UsbDebuggingActivity</string>
  7. <!-- Name of the activity that prompts the secondary user to acknowledge she/he needs to
  8. switch to the primary user to enable USB debugging.
  9. Can be customized for other product types -->
  10. <string name="config_customAdbPublicKeyConfirmationSecondaryUserComponent"
  11. >com.android.systemui/com.android.systemui.usb.UsbDebuggingSecondaryUserActivity</string>

当framework检测到ADB时,就会将授权请求转发给两个值指定的类。

  1. /// Q0: \frameworks\base\services\core\java\com\android\server\adb\AdbDebuggingManager.java
  2. /// P0: com.android.server.usb.UsbDebuggingManager
  3. /// 版本不同,代码有一下差异,下面是Q0的代码
  4. case MESSAGE_ADB_CONFIRM: {
  5. String key = (String) msg.obj;
  6. if ("trigger_restart_min_framework".equals(
  7. SystemProperties.get("vold.decrypt"))) {
  8. Slog.d(TAG, "Deferring adb confirmation until after vold decrypt");
  9. if (mThread != null) {
  10. mThread.sendResponse("NO");
  11. logAdbConnectionChanged(key, AdbProtoEnums.DENIED_VOLD_DECRYPT, false);
  12. }
  13. break;
  14. }
  15. String fingerprints = getFingerprints(key);
  16. if ("".equals(fingerprints)) {
  17. if (mThread != null) {
  18. mThread.sendResponse("NO");
  19. logAdbConnectionChanged(key, AdbProtoEnums.DENIED_INVALID_KEY, false);
  20. }
  21. break;
  22. }
  23. logAdbConnectionChanged(key, AdbProtoEnums.AWAITING_USER_APPROVAL, false);
  24. mFingerprints = fingerprints;
  25. // 开始确认授权
  26. startConfirmation(key, mFingerprints);
  27. break;
  28. }
  29. private void startConfirmation(String key, String fingerprints) {
  30. int currentUserId = ActivityManager.getCurrentUser();
  31. UserInfo userInfo = UserManager.get(mContext).getUserInfo(currentUserId);
  32. String componentString;
  33. // 当前用户是管理员,则使用第一个xml的值;如果是其它用户,则用第二个xml的值
  34. if (userInfo.isAdmin()) {
  35. componentString = mConfirmComponent != null
  36. ? mConfirmComponent : Resources.getSystem().getString(
  37. com.android.internal.R.string.config_customAdbPublicKeyConfirmationComponent);
  38. } else {
  39. // If the current foreground user is not the admin user we send a different
  40. // notification specific to secondary users.
  41. componentString = Resources.getSystem().getString(
  42. R.string.config_customAdbPublicKeyConfirmationSecondaryUserComponent);
  43. }
  44. ComponentName componentName = ComponentName.unflattenFromString(componentString);
  45. if (startConfirmationActivity(componentName, userInfo.getUserHandle(), key, fingerprints)
  46. || startConfirmationService(componentName, userInfo.getUserHandle(),
  47. key, fingerprints)) {
  48. return;
  49. }
  50. Slog.e(TAG, "unable to start customAdbPublicKeyConfirmation[SecondaryUser]Component "
  51. + componentString + " as an Activity or a Service");
  52. }

手动授权代码可参考:

  1. SystemUI\src\com\android\systemui\usb\UsbDebuggingActivity.java

二、自动授权

自动授权的处理方式是在framework调用startConfirmation时,直接根据预先设置的规则自动授予ADB权限,不在拉起用户授权界面(或服务)。