private void UpdateDevices(List <Device> list) { // because we are going to call mServer.deviceDisconnected which will acquire this lock // we lock it first, so that the AndroidDebugBridge lock is always locked first. lock (AndroidDebugBridge.GetLock()) { lock (Devices) { // For each device in the current list, we look for a matching the new list. // * if we find it, we update the current object with whatever new information // there is // (mostly state change, if the device becomes ready, we query for build info). // We also remove the device from the new list to mark it as "processed" // * if we do not find it, we remove it from the current list. // Once this is done, the new list contains device we aren't monitoring yet, so we // add them to the list, and start monitoring them. for (int d = 0; d < Devices.Count;) { Device device = Devices[d]; // look for a similar device in the new list. int count = list.Count; bool foundMatch = false; for (int dd = 0; dd < count; dd++) { Device newDevice = list[dd]; // see if it matches in id and serial number. if (string.Compare(newDevice.SerialNumber, device.SerialNumber, true) == 0) { foundMatch = true; // update the state if needed. if (device.State != newDevice.State) { device.State = newDevice.State; // if the device just got ready/online, we need to start // monitoring it. if (device.IsOnline) { if (AndroidDebugBridge.ClientSupport) { if (StartMonitoringDevice(device) == false) { Log.E(TAG, "Failed to start monitoring {0}", device.SerialNumber); } } if (device.Properties.Count == 0) { device.RetrieveDeviceInfo(); } } device.OnStateChanged(EventArgs.Empty); } // remove the new device from the list since it's been used list.RemoveAt(dd); break; } } if (foundMatch == false) { // the device is gone, we need to remove it, and keep current index // to process the next one. RemoveDevice(device); device.State = DeviceState.Offline; device.OnStateChanged(EventArgs.Empty); Server.OnDeviceDisconnected(new DeviceEventArgs(device)); } else { // process the next one d++; } } // at this point we should still have some new devices in newList, so we // process them. foreach (Device newDevice in list) { // add them to the list Devices.Add(newDevice); if (Server != null) { newDevice.State = DeviceState.Online; newDevice.RetrieveDeviceInfo(false); newDevice.OnStateChanged(EventArgs.Empty); Server.OnDeviceConnected(new DeviceEventArgs(newDevice)); } // start monitoring them. if (AndroidDebugBridge.ClientSupport) { if (newDevice.IsOnline) { StartMonitoringDevice(newDevice); } } } } } list.Clear(); }