• 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. }
  • 相关阅读:
    javascript 中 document.getElementsByClassName 和 document.querySelector区别
    vite+vue 项目使用 electron
    关于架构的认知
    DL在材料化学中(基于图像模型)的应用
    windows安装MongoDB后进入命令交互界面失败解决方案
    非零基础自学Java (老师:韩顺平) 第4章 运算符 4.21 二进制转八进制等 && 4.27 原码、反码、补码 && 4.28 位运算符
    【关联分析实战篇】为什么 BI 软件都搞不定关联分析
    K8S深度解析:从入门到精通的全方位指南
    Vue3快速上手指南
    ESP32智能小车+PS2无线遥控器+麦克纳姆轮+microPython
  • 原文地址:https://blog.csdn.net/vviccc/article/details/133091448