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

@Override

public 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" checkbox

LayoutInflater 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

/**

* Notifie