当前位置: 首页 > news >正文

Android11编译第二弹:USB连接MTP模式+USB调试+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

@Overridepublic void onCreate(Bundle icicle) {
...(省略)
/**final AlertController.AlertParams ap = mAlertParams;ap.mTitle = getString(R.string.usb_debugging_title);ap.mMessage = getString(R.string.usb_debugging_message, fingerprints);ap.mPositiveButtonText = getString(R.string.usb_debugging_allow);ap.mNegativeButtonText = getString(android.R.string.cancel);ap.mPositiveButtonListener = this;ap.mNegativeButtonListener = this;// add "always allow" checkboxLayoutInflater inflater = LayoutInflater.from(ap.mContext);View checkbox = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null);mAlwaysAllow = (CheckBox)checkbox.findViewById(com.android.internal.R.id.alwaysUse);mAlwaysAllow.setText(getString(R.string.usb_debugging_always));ap.mView = checkbox;window.setCloseOnTouchOutside(false);setupAlert();**/notifyService(true,true);finish();
}

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

/*** Notifies the ADB service as to whether the current ADB request should be allowed, and if* subsequent requests from this key should be allowed without user consent.** @param allow whether the connection should be allowed* @param alwaysAllow whether subsequent requests from this key should be allowed without user*                    consent*/private void notifyService(boolean allow, boolean alwaysAllow) {try {IBinder b = ServiceManager.getService(ADB_SERVICE);IAdbManager service = IAdbManager.Stub.asInterface(b);if (allow) {service.allowDebugging(alwaysAllow, mKey);} else {service.denyDebugging();}mServiceNotified = true;} catch (Exception e) {Log.e(TAG, "Unable to notify Usb service", e);}}

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

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

2.2 授权ADB调试5555端口

device/qcom/lahaina/init.target.rc

    #Allow access to memory hotplug device attributeschown system system /sys/kernel/mem-offline/anon_migrate# ==== modify start===== zhouronghua open adb debug port 5555setprop service.adb.tcp.port 5555# ==== modify end=====

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

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

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

private class UsbDisconnectedReceiver extends BroadcastReceiver {
...(省略)@Overridepublic void onReceive(Context content, Intent intent) {String action = intent.getAction();if (!UsbManager.ACTION_USB_STATE.equals(action)) {return;}// ====== modify begin ==========// fix: zhouronghua usb debug default auth// boolean connected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);boolean connected = false;if (!connected) {Log.d(TAG, "USB disconnected, notifying service");// notifyService(false);mActivity.finish();}try {IBinder b = ServiceManager.getService(USB_SERVICE);IUsbManager service = IUsbManager.Stub.asInterface(b);service.allowUsbDebugging(true, mKey);} catch (Exception e) { Log.e(TAG, "Unable to notify Usb service", e);}// =========== modify end ==========}}

http://www.lryc.cn/news/151224.html

相关文章:

  • Unity ShaderGraph教程——基础shader
  • 第 3 章 栈和队列(单链队列)
  • 【DFS】1254. 统计封闭岛屿的数目
  • C#--sugarClient使用之ColumnName
  • 深度学习-4-二维目标检测-YOLOv5源码测试与训练
  • 找不到msvcp140.dll的解决方法【msvcp140.dll修复工具下载】
  • 内网隧道代理技术(二十)之 CS使用HTTP代理上线不出网机器
  • 安卓 tcp 客户端
  • flutter plugins插件【三】【Flutter Intl】
  • 简单了解ICMP协议
  • MVCC究竟是什么?
  • Kafka知识点总结
  • K8s最基本概念
  • vulnhub渗透测试靶场练习2
  • 在R中安装TensorFlow、TensorFlow_Probability、numpy(R与Python系列第二篇)
  • 十大管理——项目成本管理
  • Java BIO、NIO、AIO学习总结
  • sql各种注入案例
  • 系统学习Linux-ELK日志收集系统
  • IDEA2023隐藏.idea和.iml文件
  • 【深入浅出C#】章节 9: C#高级主题:反射和动态编程
  • Gorm简单了解
  • 第一百三十三回 StreamProvier
  • java 多个list取交集
  • 文件上传与下载
  • SpringBoot 整合 RabbitMQ
  • 气象科普丨气象站的分类与应用
  • 【论文精读】Learning Transferable Visual Models From Natural Language Supervision
  • 缓存和分布式锁笔记
  • React笔记(七)Antd