第8章4节《MonkeyRunner源码剖析》MonkeyRunner启动运行过程-启动 5

所以最终重点就是调用processIncomingDeviceData来处理更新后的设备列表。

296   private void processIncomingDeviceData(int length) throws IOException

297   {

298     ArrayList<Device> list = new ArrayList();

299   

300     if (length > 0) {

301       byte[] buffer = new byte[length];

302       String result = read(this.mMainAdbConnection, buffer);

303     

304       String[] devices = result.split("\n");

305     

306       for (String d : devices) {

307         String[] param = d.split("\t");

308         if (param.length == 2)

309         {

310           Device device = new Device(this, param[0],

IDevice.DeviceState.getState(param[1]));

311

312

313

314           list.add(device);

315         }

316       }

317     }

318   

319

320     updateDevices(list);

321   }

 代码8-4-4 DeviceMonitor - processIncomingDeviceData

方法体首先在302行获得ADB服务器发送过来的”设备序列号 状态”的设备列表,然后在后续几行循环将每个设备的序列号的和状态解析出来,然后就是根据序列号和状态创建一个代表该设备的Device对象并把其存储到一个列表list里面,最后就是调用updateDevices方法进行下一步动作了。这了我们先简单看下Device的构造函数,至于它的详细分析将会留到下一章。

677 Device(DeviceMonitor monitor, String serialNumber, IDevice.DeviceState deviceState)
678 {
679 this.mMonitor = monitor;
680 this.mSerialNumber = serialNumber;
681 this.mState = deviceState;
682 }

代码8-4-5 Device构造函数

 

Device的构造函数非常简单,就是把上面传进来的DeviceMonitor实例,ADB服务器主动发送过来的设备的序列号字串,以及设备当前的状态给保存起来到对应的成员变量中而已。这里要注意的的士mSerialNumber和mState,在往下分析“启动Monkey“的时候我们需要用到。

我们往下继续分析updateDevices这个方法。这个方法的代码稍微长那么一点点,我们把它分开来分析:

323     /**

324      *  Updates the device list with the new items received from the monitoring service.

325      */

326     private void updateDevices(ArrayList<Device> newList) {

327         // because we are going to call mServer.deviceDisconnected which will acquire this lock

328         // we lock it first, so that the AndroidDebugBridge lock is always locked first.

329         synchronized (AndroidDebugBridge.getLock()) {

330             // array to store the devices that must be queried for information.

331             // it's important to not do it inside the synchronized loop as this could block

332             // the whole workspace (this lock is acquired during build too).

333             ArrayList<Device> devicesToQuery = new ArrayList<Device>();

334             synchronized (mDevices) {

335                 // For each device in the current list, we look for a matching the new list.

336                 // * if we find it, we update the current object with whatever new information

337                 //   there is

338                 //   (mostly state change, if the device becomes ready, we query for build info).

339                 //   We also remove the device from the new list to mark it as "processed"

340                 // * if we do not find it, we remove it from the current list.

341                 // Once this is done, the new list contains device we aren't monitoring yet, so we

342                 // add them to the list, and start monitoring them.

343

344                 for (int d = 0 ; d < mDevices.size() ;) {

345                     Device device = mDevices.get(d);

346

347                     // look for a similar device in the new list.

348                     int count = newList.size();

349                     boolean foundMatch = false;

350                     for (int dd = 0 ; dd < count ; dd++) {

351                         Device newDevice = newList.get(dd);

352                         // see if it matches in id and serial number.

353                         if (newDevice.getSerialNumber().equals(device.getSerialNumber())) {

354                             foundMatch = true;

355

356                             // update the state if needed.

357                             if (device.getState() != newDevice.getState()) {

358                                 device.setState(newDevice.getState());

359                                 device.update(Device.CHANGE_STATE);

360

361                                 // if the device just got ready/online, we need to start

362                                 // monitoring it.

363                                 if (device.isOnline()) {

364                                     if (AndroidDebugBridge.getClientSupport()) {

365                                         if (!startMonitoringDevice(device)) {

366                                             Log.e("DeviceMonitor",

367                                                     "Failed to start monitoring "

368                                                     + device.getSerialNumber());

369                                         }

370                                     }

371

372                                     if (device.getPropertyCount() == 0) {

373                                         devicesToQuery.add(device);

374                                     }

375                                 }

376                             }

377

378                             // remove the new device from the list since it's been used

379                             newList.remove(dd);

380                             break;

381                         }

382                     }

383

384                     if (!foundMatch) {

385                         // the device is gone, we need to remove it, and keep current index

386                         // to process the next one.

387                         removeDevice(device);

388                         mServer.deviceDisconnected(device);

389                     } else {

390                         // process the next one

391                         d++;

392                     }

393                 }

...

}

代码8-4-5 DeviceMonitor - updateDevices处理移除和状态改变设备


你可能感兴趣的:(软件测试开发)