• Android Framework 常见解决方案(21)默认开启adb


    1 原理解读

    本质上要解决两个问题:

    • 设置默认ADB enable为真,android的user版本中也默认为真,即使之有效。
    • 一般android设备连接电脑PC后会出现USB连接的弹框,去掉弹框直接生效。

    修改方案(Android Q R S)

    设置默认ADB Enable为真,android的user版本中也默认为真
    文件 AOSP/frameworks/base/services/core/java/com/android/server/adb/AdbService.java
    在该文件的systemReady中修改

    1. /**
    2. * Called in response to {@code SystemService.PHASE_ACTIVITY_MANAGER_READY} from {@code
    3. * SystemServer}.
    4. */
    5. public void systemReady() {
    6. if (DEBUG) Slog.d(TAG, "systemReady");
    7. // make sure the ADB_ENABLED setting value matches the current state
    8. try {
    9. //这里不管mIsAdbUsbEnabled是否为真,强制设定属性Settings.Global.ADB_ENABLED为1
    10. Settings.Global.putInt(mContentResolver,
    11. - Settings.Global.ADB_ENABLED, mIsAdbUsbEnabled ? 1 : 0);
    12. + Settings.Global.ADB_ENABLED, 1);
    13. Settings.Global.putInt(mContentResolver,
    14. Settings.Global.ADB_WIFI_ENABLED, mIsAdbWifiEnabled ? 1 : 0);
    15. } catch (SecurityException e) {
    16. // If UserManager.DISALLOW_DEBUGGING_FEATURES is on, that this setting can't be changed.
    17. Slog.d(TAG, "ADB_ENABLED is restricted.");
    18. }
    19. }

    修改文件AOSP/frameworks/base/packages/SystemUI/src/com/android/systemui/usb/UsbDebuggingActivity.java,一般android设备连接电脑PC后会出现USB连接的弹框,这里的修改主要是不弹框并直接连接成功,代码修改如下:
     

    1. public class UsbDebuggingActivity extends AlertActivity
    2. implements DialogInterface.OnClickListener {
    3. //...
    4. @Override
    5. public void onCreate(Bundle icicle) {
    6. Window window = getWindow();
    7. window.addSystemFlags(
    8. WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
    9. window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
    10. super.onCreate(icicle);
    11. // Emulator does not support reseating the usb cable to reshow the dialog.
    12. boolean isEmulator = SystemProperties.get("ro.boot.qemu").equals("1");
    13. if (SystemProperties.getInt("service.adb.tcp.port", 0) == 0 && !isEmulator) {
    14. mDisconnectedReceiver = new UsbDisconnectedReceiver(this);
    15. IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_STATE);
    16. mBroadcastDispatcher.registerReceiver(mDisconnectedReceiver, filter);
    17. }
    18. Intent intent = getIntent();
    19. String fingerprints = intent.getStringExtra("fingerprints");
    20. mKey = intent.getStringExtra("key");
    21. if (fingerprints == null || mKey == null) {
    22. finish();
    23. return;
    24. }
    25. + try {
    26. + IBinder b = ServiceManager.getService(ADB_SERVICE);
    27. + IAdbManager service = IAdbManager.Stub.asInterface(b);
    28. + service.allowDebugging(true, mKey);
    29. + finish();
    30. + } catch (Exception e) {
    31. + Log.e(TAG, "Unable to notify Usb service", e);
    32. + }
    33. final AlertController.AlertParams ap = mAlertParams;
    34. ap.mTitle = getString(R.string.usb_debugging_title);
    35. ap.mMessage = getString(R.string.usb_debugging_message, fingerprints);
    36. ap.mPositiveButtonText = getString(R.string.usb_debugging_allow);
    37. ap.mNegativeButtonText = getString(android.R.string.cancel);
    38. ap.mPositiveButtonListener = this;
    39. ap.mNegativeButtonListener = this;
    40. // add "always allow" checkbox
    41. LayoutInflater inflater = LayoutInflater.from(ap.mContext);
    42. View checkbox = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null);
    43. mAlwaysAllow = (CheckBox)checkbox.findViewById(com.android.internal.R.id.alwaysUse);
    44. mAlwaysAllow.setText(getString(R.string.usb_debugging_always));
    45. ap.mView = checkbox;
    46. window.setCloseOnTouchOutside(false);
    47. setupAlert();
    48. }
    49. //...
    50. }
  • 相关阅读:
    容器云平台初始化(harbor的安装部署)
    【uni-app从入门到实战】打包
    Tomcat的安装、在idea中的使用以及创建Web项目
    专精特新新企业技术创新发展趋势研究分析讲座详情
    PyTorch笔记 - Convolution卷积运算的原理 (3)
    xv6源码解析(四)——进程管理
    用 Sentence Transformers v3 训练和微调嵌入模型
    C和指针 第10章 结构和联合 10.10 问题
    数仓开发之DWS层(二)
    Java学习笔记5.2.3 List接口 - 遍历集合
  • 原文地址:https://blog.csdn.net/vviccc/article/details/133091448