Lao Li Recommendation: 8th Chapter 4, "Monkeyrunner Source Analysis" Monkeyrunner start running Process-start Androiddebugbridge 1

Source: Internet
Author: User

In the previous section we saw that the Start method was called in the process of starting Androiddebugbridge, and the method would do 2 main things:

    1. 715 row startadb: Open Androiddebugbridge
    2. 722-723 Line: Initialize the Android device monitor and start the Devicemonitor device monitoring thread.

1th of which we have done a detailed analysis of the previous section, then we go down to analyze the 2nd.

The main function of devicemonitor is to monitor whether or not an Android device is removed or connected, and then monitor all the adjustable processes of each connected device. This function more is to DDMS this debugging tool use, everyone from Androiddebugbridge in the jar package Ddmlib.jar should be able to guess actually DDMS used is this jar package.

Every Android application runs in a separate process, and every process runs in its own virtual machine (VM), which is basically the sandbox concept of the Android operating system (sandbox). The goal is to prevent your process from rushing around and ruining it. VMS for each process will provide a unique port for the debugger to connect to. DDMS is connected to Androiddebugbridge (that is, the ADB server) when it starts. When a device is connected, a virtual machine notifies DDMS when it is started or stopped on the device, creating a monitoring service for the virtual machine between DDMS and ADB. Once the virtual machine is running, DDMS retrieves the virtual machine's process ID (PID) through the Androiddebugbridge devicemonitor thread and opens a debugger connected to the virtual machine through the ADB daemon on the device. So DDMS can talk to the virtual machine.

Figure 8-4-1 Ddms Interface

The most important feature of this class for the Monkeyrunner framework is that it holds a list of devices that represent all the connected Devicemonitor, a device class that is very important, Basically it can be said that the communication between Monkeyrunner and ADB server is ultimately done through it, so it is equivalent to an Android device on the PC side of the agent, we will do a detailed analysis of this class in the next chapter. Let's look at the Devicemonitor class first:

Figure 8-4-2 Devicemonitor Class

This class is a class of Ddmlib library, the above class diagram lists the key member variables and methods of Devicemonitor, we first introduce them, so that we can better follow me in the next face of this class of source code for analysis:

  • Mserver:debugbridgeserver object, because it is necessary to send the appropriate device monitoring command to the ADB server, so when the devicemonitor is initialized, a Debugbridgeserver instance is passed in and saved to Mserver
  • Mmainadbconnection: A Socketchannel instance, devicemonitor need to establish a link to the ADB server before monitoring each device
  • Mdevices: A list of device instances that represent an Android device. Note that this device is very important throughout the Monkeyrunner framework, as described above, it can basically be seen as a real Android device on the PC side of the agent, many of the commands sent to the ADB server are sent out through it eventually. Devicemonitor Monitoring thread Once a new device is found, it will be set up with a device instance and placed in the Mdevices list, conversely, if one of the devices is removed, it will be removed from the mdevices list .
  • Start: A default permission method, so it can be called by the Androiddebugbridge in the same ddmlib package. Its role is to start the device list monitoring thread, the thread loop body in the Devicemonitorloop method described below
  • GetDevices: Return to the mdevices device above. The Waitforconnection method of the Monkeyrunner class, when establishing a connection to the device, will eventually call this method to get all the devices and locate the target device according to the device serial number to connect
  • Devicemonitorloop: Sends the "host:track-devices" Device monitoring command to the ADB server to loop through the list of devices of the latest string type, similar to the one that sends "ADB devices" on the command line to get the list of devices currently connected, But the difference is that when the monitoring command is sent to the ADB server, the ADB server periodically updates the list of devices and then sends it back on its own. After each loop is completed, if you find that the ADB server has data, call the following Processincomingdevicedata method to update the Mdevices device list, and so on.
  • Senddevicelistmonitoringrequest: The above Devicemonitorloop method calls a method to send a "host:track-devices" Device List Monitor command to and from the ADB server
  • Processincomingdevicedata: The above Devicemonitorloop method calls this method when it finds that the ADB server has data coming up to read the latest string type device list from the mmainadbconnection connected to the ADB server Socketchannel Then, for each device, instantiate one device instance and place the instances in a list, and pass them to the following updatedevices method to handle
  • Updatedevices: After receiving the list of devices of the latest device type that Processincomingdevicedata sent over, Updatedevices will check the current list of mdevices devices to find out which devices are newly added, which are removed, and which are state-changed to handle accordingly:
      • New device: Add the device to the list of mdevices devices maintained by Devicemonitor and call the following startmonitoringdevice to establish a monitoring thread for each of the device's debug application processes to monitor the VM process
      • The device becomes online: set the corresponding device in the Mdevices device list to online status, and call the following startmonitoringdevice to establish a monitoring thread for each of the device's debug application processes to monitor the VM process
      • Device changed from online to other state: set the corresponding device to the corresponding state in the Mdevices device list
      • Remove Device: Remove the device from the current device list mdevices
  • Startmonitoringdevice: Obtain a PID list of all the debug VM processes in the corresponding device (the debug process refers to each process in DALVIKVM that initiates a debug tool connection such as JDWP line Chengyang DDMS), Connect the debug ports on each VM and establish corresponding client monitoring threads to monitor the process. Because the client machine below the JDWP related code is mainly for DDMS and other monitoring debugging tools to use, so this book will not be analyzed in depth
  • The Senddevicemonitoringrequest:startmonitoringdevice method calls this method to get the PID of all the processes that the specified device can debug, actually sending the "track-jdwp" command

With the above basic understanding, we continue to analyze its code implementation. When the Androiddebugbridge is up, the next step is to upload the ADB instance to Devicemonitor to monitor the status of all Android devices connected to the ADB server, which is the PC host side:

Devicemonitor (Androiddebugbridge server)

71 {

This.mserver = server;

73

This.mDebuggerPorts.add (Integer.valueof (Ddmpreferences.getdebugportbase ()));

75}

Code 8-4-1 devicemonitor Constructors

After saving the Androiddebugbridge instance, the next step is to continue Androiddebugbridge start function start () to launch the Devicemonitor device monitoring thread:

+ void Start ()

80 {

Bayi New Thread ("Device List Monitor")

82 {

The public void run () {

DeviceMonitor.this.deviceMonitorLoop ();

85}

}.start ();

87}

Code 8-4-2 Devicemonitor-start

Lao Li Recommendation: 8th Chapter 4, "Monkeyrunner Source Analysis" Monkeyrunner start running Process-start Androiddebugbridge 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.