第14章5节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-装备ViewServer-查

上一小节我们描述了HierarchyViewer是如何组建ADB协议命令来实现ViewServer的端口转发的,在端口转发设置好后,下一个要做的事情就是去检测目标设备端ViewServer线程是否已经启动起来了。我们进入setupViewServer调用的DeviceBridge的isViewServerRunning方法:

165public static boolean isViewServerRunning(IDevice device) {166final boolean[] result = new boolean[1];167try {168if (device.isOnline()) {169device.executeShellCommand(buildIsServerRunningShellCommand(),170new BooleanResultReader(result));171if (!result[0]) {172ViewServerInfo serverInfo = loadViewServerInfo(device);173if (serverInfo != null && serverInfo.protocolVersion > 2) {174result[0] = true;175}176}177}178} catch (TimeoutException e) {…187}188return result[0];189}代码14-5-1 DeviceBridge – isViewServerRunning

关键代码是上面的169行,通过Device类的实例来往ADB服务器发送相应的命令来检测ViewServer是否已经在运行。

device.executeShellCommand在前面章节已经分析过了,就是用来发送”adb shell”命令的。

我们看下buildIsServerRunningShellCommand方法,看这个命令是如何组织起来的:

235private static String buildIsServerRunningShellCommand() {236return String.format("service call window %d", SERVICE_CODE_IS_SERVER_RUNNING); 237}代码14-5-2 DeviceBridge – buildIsServerRunningShellCommand

而全局变量 SERVICE_CODE_IS_SERVER_RUNNING 的定义是:

48private static final int DEFAULT_SERVER_PORT = 4939; 49// These codes must match the auto-generated codes in IWindowManager.java 50// See IWindowManager.aidl as well 51private static final int SERVICE_CODE_START_SERVER = 1; 52private static final int SERVICE_CODE_STOP_SERVER = 2; 53private static final int SERVICE_CODE_IS_SERVER_RUNNING = 3;代码14-5-3 DeviceBridge – 全局变量示例

236行整出来的这一串不就是”service call window 3”嘛。所以结合device.sendShellCommand,事实上就是往设备发送了命令”adb shell service call window 3”,上一章我们才用它来查询ViewServer的运行状态了!

你说,你可以把它取下来吗?当我要取的时候,你淘气的躲开了,

第14章5节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-装备ViewServer-查

相关文章:

你感兴趣的文章:

标签云: