From 4ff43bf4187ebfbba0614047681eec0a3e4ee2c2 Mon Sep 17 00:00:00 2001 From: Devon Carew Date: Thu, 4 Feb 2016 13:28:14 -0800 Subject: [PATCH] get the adb device name when using the adb server --- .../flutter_tools/lib/src/android/adb.dart | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/packages/flutter_tools/lib/src/android/adb.dart b/packages/flutter_tools/lib/src/android/adb.dart index 207f2d19f91..3b42fe2d91d 100644 --- a/packages/flutter_tools/lib/src/android/adb.dart +++ b/packages/flutter_tools/lib/src/android/adb.dart @@ -19,6 +19,8 @@ class Adb { final String adbPath; + Map _idToNameCache = {}; + bool exists() { try { runCheckedSync([adbPath, 'version']); @@ -85,7 +87,7 @@ class Adb { socket = await Socket.connect(InternetAddress.LOOPBACK_IP_V4, adbServerPort); printTrace('--> host:track-devices'); socket.add(_createAdbRequest('host:track-devices')); - socket.listen((List data) { + socket.listen((List data) async { String stringResult = new String.fromCharCodes(data); printTrace('<-- ${stringResult.trim()}'); _AdbServerResponse response = new _AdbServerResponse( @@ -99,9 +101,13 @@ class Adb { if (devicesText.isEmpty) { controller.add([]); } else { - controller.add(devicesText.split('\n').map((String deviceInfo) { + List devices = devicesText.split('\n').map((String deviceInfo) { return new AdbDevice(deviceInfo); - }).toList()); + }).toList(); + + await _populateDeviceNames(devices); + + controller.add(devices); } }); socket.done.then((_) => controller.close()); @@ -112,6 +118,26 @@ class Adb { return controller.stream; } + Future _populateDeviceNames(List devices) async { + for (AdbDevice device in devices) { + if (device.modelID == null) { + // If we don't have a name of a device in our cache, call `device -l` to populate it. + if (_idToNameCache[device.id] == null) + await _populateDeviceCache(); + + // Set the device name from the cached name. Adb device notifications only + // have IDs, not names. We get the name by calling `listDevices()`. + device.modelID = _idToNameCache[device.id]; + } + } + } + + Future _populateDeviceCache() async { + List devices = await listDevices(); + for (AdbDevice device in devices) + _idToNameCache[device.id] = device.modelID; + } + Future _sendAdbServerCommand(String command) async { Socket socket = await Socket.connect(InternetAddress.LOOPBACK_IP_V4, adbServerPort); @@ -151,6 +177,10 @@ class AdbDevice { } } } + + // Convert `Nexus_7` / `Nexus_5X` style names to `Nexus 7` ones. + if (modelID != null) + modelID = modelID.replaceAll('_', ' '); } static final RegExp deviceRegex = new RegExp(r'^(\S+)\s+(\S+)(.*)'); @@ -168,6 +198,10 @@ class AdbDevice { /// Device model; can be null. `XT1045`, `Nexus_7` String get modelID => _info['model']; + set modelID(String value) { + _info['model'] = value; + } + /// Device code name; can be null. `peregrine`, `grouper` String get deviceCodeName => _info['device'];