activeDevices = list();
for (final String deviceId : activeDevices) {
if (!devices.containsKey(deviceId)) {
addDevice(new Device(this, deviceId));
}
}
}
private void addDevice(final Device device) {
// System.err.println("AndroidEnvironment: adding " + device.getId());
try {
device.initialize();
if (devices.put(device.getId(), device) != null) {
throw new IllegalStateException("Adding " + device
+ ", which already exists!");
}
} catch (final Exception e) {
System.err.println("While initializing " + device.getId() + ": " + e);
}
}
void deviceRemoved(final Device device) {
// System.err.println("AndroidEnvironment: removing " + device.getId());
if (devices.remove(device.getId()) == null) {
throw new IllegalStateException("I didn't know about device "
+ device.getId() + "!");
}
}
/**
* First line starts "List of devices"
*
*
When an emulator is started with a debug port, then it shows up
* in the list of devices.
*
*
List of devices attached
*
HT91MLC00031 device
*
emulator-5554 offline
*
*
List of devices attached
*
HT91MLC00031 device
*
emulator-5554 device
*
* @return list of device identifiers
* @throws IOException
*/
public static List list() {
ProcessResult result;
try {
// System.out.println("listing devices 00");
result = AndroidSDK.runADB("devices");
// System.out.println("listing devices 05");
} catch (InterruptedException e) {
return Collections.emptyList();
} catch (IOException e) {
System.err.println("Problem inside Devices.list()");
e.printStackTrace();
// System.err.println(e);
// System.err.println("checking devices");
// e.printStackTrace(EditorConsole.systemErr);
return Collections.emptyList();
}
// System.out.println("listing devices 10");
if (!result.succeeded()) {
if (result.getStderr().contains("protocol fault (no status)")) {
System.err.println("bleh: " + result); // THIS IS WORKING
} else {
System.err.println("nope: " + result);
}
return Collections.emptyList();
}
// System.out.println("listing devices 20");
// might read "List of devices attached"
final String stdout = result.getStdout();
if (!(stdout.contains("List of devices") || stdout.trim().length() == 0)) {
System.err.println(ADB_DEVICES_ERROR);
System.err.println("Output was “" + stdout + "”");
return Collections.emptyList();
}
// System.out.println("listing devices 30");
final List devices = new ArrayList();
for (final String line : result) {
if (line.contains("\t")) {
final String[] fields = line.split("\t");
if (fields[1].equals("device")) {
devices.add(fields[0]);
}
}
}
return devices;
}
}