After the device serial number is obtained, the findattacheddevice is compared to the supplied serial number, and if the match is returned to the caller "code 8-6-3 adbbackend-waitforconnection". The Adbbackend Waitforconnection, after acquiring the device instance, will pass it to the Adbchimpdevice constructor to construct the Adbchimpdevice instance object. Let's see how its constructor is done:
adbchimpdevice Public (IDevice device)
69 {
This.device = device;
This.manager = Createmanager ("127.0.0.1", 12345);
72
Preconditions.checknotnull (This.manager);
74}
Code 8-6-8 adbchimpdevice Constructors
As has been emphasized previously, Adbchimpdevice is a very important class, a high-level abstract device object that combines devices that represent device control through Monkey Chimpmanager and device control via ADB. This combination is represented by the above-mentioned Adnbchimpdevice constructor function. The 70th line combines device devices, and the 71-row combination is the Chimpmanager instance. It is just that the device instance was instantiated in the boot-up monitoring thread devicemonitor, and Chimpmanager was created at this time. When created, a native loopback IP address "127.0.0.1" is specified, and the port designation is monkey local forwarding port 12345
The code to create the Chimpmanager call Createmanager is a bit long, and we'll analyze it in two parts, the first of which is start monkey, and the second part creates Chimpmanager. Let's look at the first part:
123 Private Chimpmanager Createmanager (String address, int port) {
124 try {
This.device.createForward (port, port);
126} catch (TimeoutException e) {
127 LOG.log (Level.severe, "Timeout creating adb port Forwarding", e);
return null;
129} catch (Adbcommandrejectedexception e) {
LOG.log (Level.severe, "ADB rejected adb port forwarding command:" + e.getmessage (), E);
131 return null;
IOException} catch (e) {
133 LOG.log (Level.severe, "Unable to create ADB port forwarding:" + e.getmessage (), E);
134 return null;
135}
136
137 String command = "Monkey--port" + port;
138 executeasynccommand (Command, new Loggingoutputreceiver (LOG, level.fine));
139
Try
141 {
142 Thread.Sleep (1000L);
143} catch (Interruptedexception e) {
144 LOG.log (Level.severe, "Unable to Sleep", e);
145}
146 InetAddress Addr;
147 Try
148 {
149 addr = inetaddress.getbyname (address);
(unknownhostexception e) {
151 LOG.log (Level.severe, "Unable to convert address into inetaddress:" + address, E);
return null;
153}
...
}
Code 8-6-9 Adbchimpdevice-createmanager Start Monkey
The first thing Createmanager do is to get the monkey service process on the target device to start up and receive the Monkeyrunner test script to send past requests. The code flow is as follows:
- 125 line: Set the local to target machine monkey process listening port forwarding, call the device's Createforward method, this method we describe the device class in the next chapter when the analysis. All it takes here is that it basically can be seen as sending "adb forward 12345 12345" on the command line to complete the forwarding of 12345 ports from the native 12345 port to the remote Monkey Monitor. After setting up port forwarding, the code can directly connect to the 12345 port of this machine, which is equivalent to the 12345 port of monkey listening in the remote target device.
- 139-138 Line: After setting the monkey port forwarding, the Createmanager method sends the shell command "monkey--port 12345" to the ADB server to start monkey to listen on port 12345. The method used to send the ADB shell command is the Createasynccommand method, in fact, there is no good analysis of the method, because it sends the command request directly to the device class Executeshellcommand just, And Executeshellcommand This method, we will analyze it in the next chapter.
- 149 Line: Convert the native listening address "127.0.0.1" to the InetAddress object format so that the socket connection can be created directly using
Createmanager start monkey here to complete, down we continue to see the second part of the creation of the Createmanager Chimpmanager:
123 Private Chimpmanager Createmanager (String address, int port) {
... //Start Monkey code slightly
159 Boolean success = false;
Chimpmanager mm = null;
161 Long start = System.currenttimemillis ();
162
163 while (!success) {
164 Long now = System.currenttimemillis ();
165 long diff = Now-start;
166 if (diff > 30000L) {
167 Log.severe ("Timeout while trying to create chimp Mananger");
168 return null;
169}
Try
171 {
172 Thread.Sleep (1000L);
173} catch (Interruptedexception e) {
174 LOG.log (Level.severe, "Unable to Sleep", e);
175}
176 Socket Monkeysocket;
177 Try
178 {
179 Monkeysocket = new Socket (addr, port);
IOException} catch (e) {
181 LOG.log (Level.fine, "Unable to connect socket", e);
182 success = false;}
183 continue;
184
185 Try
186 {
187 mm = new Chimpmanager (Monkeysocket);
188} catch (IOException e) {
189 LOG.log (Level.severe, "Unable to open writer and reader to Socket");}
continue;
191
192 Try
193 {
194 Mm.wake ();
195} catch (IOException e) {
196 LOG.log (Level.fine, "Unable to wake up device", e);
197 success = false;}
198 continue;
199
Success = true;
201}
202
203 return mm;
}
Code 8-6-10 Adbchimpdevice-createmanager Creation Chimpmanager
In fact, the above heap of code is nothing more than doing 3 things in a loop:
- 179 row: Create a Socket object that is connected to the native monkey to the originating port
- 187 Line: Based on the socket object construction Chimpmanager instance, Chimpmanager's detailed analysis will be placed in the next chapter of the description Chimpmanager class in detail when the analysis
- 194 Line: Send the command to monkey to wake the hibernation screen if the screen is in hibernation. The wake principle will be analyzed in the next chapter.
The objective of analyzing this section here has been achieved, and we have learned how the Monkey service process was started in the script by invoking the Waitforconnection method of Monkeyrunner. We also learned about the knowledge points created by the two key classes of Adbchimpdevice and Chimpmanager.
In the next section we will try to summarize what we have learned in this chapter.
Lao Li Recommendation: 8th Chapter 6, "Monkeyrunner Source Analysis" Monkeyrunner start running process-Start Monkey 4