Setting 打开adb
从上层现象看,打开adb 后显示的对话框对应系统的UI UsbDebuggingActivity.java
/**
* 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);
}
}
通过AIDL 调用AdbService.java 中的方法
关键代码如下
/**
允许设备连接
*/
@Override
public void allowDebugging(boolean alwaysAllow, @NonNull String publicKey) {
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_DEBUGGING, null);
Preconditions.checkStringNotEmpty(publicKey);
if (mDebuggingManager != null) {
mDebuggingManager.allowDebugging(alwaysAllow, publicKey);
}
}
/**
取消授权
*/
@Override
public void denyDebugging() {
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_DEBUG GING, null);
if (mDebuggingManager != null) {
mDebuggingManager.denyDebugging();
}
}
两者都是通过handler 方式 在AdbDebuggingManager.java
case MESSAGE_ADB_ALLOW: {
.....
if (mThread != null) {
mThread.sendResponse("OK");//确认连接
if (alwaysAllow) {
if (!mConnectedKeys.containsKey(key)) {
mConnectedKeys.put(key, 1);
}
mAdbKeyStore.setLastConnectionTime(key, System.currentTimeMillis());
sendPersistKeyStoreMessage();//存储key
scheduleJobToUpdateAdbKeyStore();
}
logAdbConnectionChanged(key, AdbProtoEnums.USER_ALLOWED, alwaysAllow);
}
break;
}
case MESSAGE_ADB_DENY:
.....
mThread.sendResponse("NO");
logAdbConnectionChanged(null, AdbProtoEnums.USER_DENIED, false);//记录连接状态变化
......
}
break;
这里可以看下sendResponse(),为什么设置OK 和NO 系统就能确认连接状态呢,继续往下看
class AdbDebuggingThread extends Thread {
@Override
public void run() {
while (true) {
....................
openSocketLocked();
listenToSocket();//监听底层送上来的adb 连接状态
....................
}
}
原来系统会启动一个死循环一直读取这个response 状态,UI可以及时响应是否连接