android adb打开授权调试

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可以及时响应是否连接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值