Commit 972be9c8 authored by Chris Bracken's avatar Chris Bracken Committed by GitHub

Make device discovery asynchronous (#10803)

Migrates DeviceDiscovery.devices and all device-specific lookup to be
asynchronous.
parent 41e1eb5c
......@@ -48,7 +48,7 @@ class AndroidDevices extends PollingDeviceDiscovery {
bool get canListAnything => androidWorkflow.canListDevices;
@override
List<Device> pollingGetDevices() => getAdbDevices();
Future<List<Device>> pollingGetDevices() async => getAdbDevices();
}
class AndroidDevice extends Device {
......
......@@ -305,7 +305,7 @@ class AppDomain extends Domain {
final String target = _getStringArg(args, 'target');
final bool enableHotReload = _getBoolArg(args, 'hot') ?? kHotReloadDefault;
final Device device = daemon.deviceDomain._getOrLocateDevice(deviceId);
final Device device = await daemon.deviceDomain._getOrLocateDevice(deviceId);
if (device == null)
throw "device '$deviceId' not found";
......@@ -493,7 +493,7 @@ class AppDomain extends Domain {
Future<List<Map<String, dynamic>>> discover(Map<String, dynamic> args) async {
final String deviceId = _getStringArg(args, 'deviceId', required: true);
final Device device = daemon.deviceDomain._getDevice(deviceId);
final Device device = await daemon.deviceDomain._getDevice(deviceId);
if (device == null)
throw "device '$deviceId' not found";
......@@ -575,11 +575,12 @@ class DeviceDomain extends Domain {
final List<PollingDeviceDiscovery> _discoverers = <PollingDeviceDiscovery>[];
Future<List<Device>> getDevices([Map<String, dynamic> args]) {
final List<Device> devices = _discoverers.expand((PollingDeviceDiscovery discoverer) {
return discoverer.devices;
}).toList();
return new Future<List<Device>>.value(devices);
Future<List<Device>> getDevices([Map<String, dynamic> args]) async {
final List<Device> devices = <Device>[];
for (PollingDeviceDiscovery discoverer in _discoverers) {
devices.addAll(await discoverer.devices);
}
return devices;
}
/// Enable device events.
......@@ -602,7 +603,7 @@ class DeviceDomain extends Domain {
final int devicePort = _getIntArg(args, 'devicePort', required: true);
int hostPort = _getIntArg(args, 'hostPort');
final Device device = daemon.deviceDomain._getDevice(deviceId);
final Device device = await daemon.deviceDomain._getDevice(deviceId);
if (device == null)
throw "device '$deviceId' not found";
......@@ -617,7 +618,7 @@ class DeviceDomain extends Domain {
final int devicePort = _getIntArg(args, 'devicePort', required: true);
final int hostPort = _getIntArg(args, 'hostPort', required: true);
final Device device = daemon.deviceDomain._getDevice(deviceId);
final Device device = await daemon.deviceDomain._getDevice(deviceId);
if (device == null)
throw "device '$deviceId' not found";
......@@ -631,23 +632,25 @@ class DeviceDomain extends Domain {
}
/// Return the device matching the deviceId field in the args.
Device _getDevice(String deviceId) {
final List<Device> devices = _discoverers.expand((PollingDeviceDiscovery discoverer) {
return discoverer.devices;
}).toList();
return devices.firstWhere((Device device) => device.id == deviceId, orElse: () => null);
Future<Device> _getDevice(String deviceId) async {
for (PollingDeviceDiscovery discoverer in _discoverers) {
final Device device = (await discoverer.devices).firstWhere((Device device) => device.id == deviceId, orElse: () => null);
if (device != null)
return device;
}
return null;
}
/// Return a known matching device, or scan for devices if no known match is found.
Device _getOrLocateDevice(String deviceId) {
Future<Device> _getOrLocateDevice(String deviceId) async {
// Look for an already known device.
final Device device = _getDevice(deviceId);
final Device device = await _getDevice(deviceId);
if (device != null)
return device;
// Scan the different device providers for a match.
for (PollingDeviceDiscovery discoverer in _discoverers) {
final List<Device> devices = discoverer.pollingGetDevices();
final List<Device> devices = await discoverer.pollingGetDevices();
for (Device device in devices)
if (device.id == deviceId)
return device;
......
......@@ -81,11 +81,17 @@ class DeviceManager {
: getAllConnectedDevices();
}
Iterable<DeviceDiscovery> get _platformDiscoverers {
return _deviceDiscoverers.where((DeviceDiscovery discoverer) => discoverer.supportsPlatform);
}
/// Return the list of all connected devices.
Stream<Device> getAllConnectedDevices() {
return new Stream<Device>.fromIterable(_deviceDiscoverers
.where((DeviceDiscovery discoverer) => discoverer.supportsPlatform)
.expand((DeviceDiscovery discoverer) => discoverer.devices));
Stream<Device> getAllConnectedDevices() async* {
for (DeviceDiscovery discoverer in _platformDiscoverers) {
for (Device device in await discoverer.devices) {
yield device;
}
}
}
}
......@@ -97,7 +103,7 @@ abstract class DeviceDiscovery {
/// current environment configuration.
bool get canListAnything;
List<Device> get devices;
Future<List<Device>> get devices;
}
/// A [DeviceDiscovery] implementation that uses polling to discover device adds
......@@ -111,13 +117,13 @@ abstract class PollingDeviceDiscovery extends DeviceDiscovery {
ItemListNotifier<Device> _items;
Timer _timer;
List<Device> pollingGetDevices();
Future<List<Device>> pollingGetDevices();
void startPolling() {
if (_timer == null) {
_items ??= new ItemListNotifier<Device>();
_timer = new Timer.periodic(_pollingDuration, (Timer timer) {
_items.updateWithNewList(pollingGetDevices());
_timer = new Timer.periodic(_pollingDuration, (Timer timer) async {
_items.updateWithNewList(await pollingGetDevices());
});
}
}
......@@ -128,8 +134,8 @@ abstract class PollingDeviceDiscovery extends DeviceDiscovery {
}
@override
List<Device> get devices {
_items ??= new ItemListNotifier<Device>.from(pollingGetDevices());
Future<List<Device>> get devices async {
_items ??= new ItemListNotifier<Device>.from(await pollingGetDevices());
return _items.items;
}
......
......@@ -36,7 +36,7 @@ class IOSDevices extends PollingDeviceDiscovery {
bool get canListAnything => iosWorkflow.canListDevices;
@override
List<Device> pollingGetDevices() => IOSDevice.getAttachedDevices();
Future<List<Device>> pollingGetDevices() => IOSDevice.getAttachedDevices();
}
class IOSDevice extends Device {
......@@ -81,12 +81,12 @@ class IOSDevice extends Device {
// iPhone 6s (9.3) [F6CEE7CF-81EB-4448-81B4-1755288C7C11] (Simulator)
static final RegExp _deviceRegex = new RegExp(r'^(.*) +\((.*)\) +\[(.*)\]$');
static List<IOSDevice> getAttachedDevices() {
static Future<List<IOSDevice>> getAttachedDevices() async {
if (!xcode.isInstalled)
return <IOSDevice>[];
final List<IOSDevice> devices = <IOSDevice>[];
final Iterable<String> deviceLines = xcode.getAvailableDevices()
final Iterable<String> deviceLines = (await xcode.getAvailableDevices())
.split('\n')
.map((String line) => line.trim());
for (String line in deviceLines) {
......
......@@ -158,7 +158,12 @@ class Xcode {
return _xcodeVersionCheckValid(_xcodeMajorVersion, _xcodeMinorVersion);
}
String getAvailableDevices() => runSync(<String>['/usr/bin/instruments', '-s', 'devices']);
Future<String> getAvailableDevices() async {
final RunResult result = await runAsync(<String>['/usr/bin/instruments', '-s', 'devices']);
if (result.exitCode != 0)
throw new ToolExit('Failed to invoke /usr/bin/instruments. Is Xcode installed?');
return result.stdout;
}
}
bool _xcodeVersionCheckValid(int major, int minor) {
......
......@@ -37,7 +37,7 @@ class IOSSimulators extends PollingDeviceDiscovery {
bool get canListAnything => iosWorkflow.canListDevices;
@override
List<Device> pollingGetDevices() => IOSSimulatorUtils.instance.getAttachedDevices();
Future<List<Device>> pollingGetDevices() async => IOSSimulatorUtils.instance.getAttachedDevices();
}
class IOSSimulatorUtils {
......
......@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:file/file.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/ios/devices.dart';
......@@ -30,15 +32,15 @@ void main() {
testUsingContext('return no devices if Xcode is not installed', () async {
when(mockXcode.isInstalled).thenReturn(false);
expect(IOSDevice.getAttachedDevices(), isEmpty);
expect(await IOSDevice.getAttachedDevices(), isEmpty);
}, overrides: <Type, Generator>{
Xcode: () => mockXcode,
});
testUsingContext('returns no devices if none are attached', () async {
when(mockXcode.isInstalled).thenReturn(true);
when(mockXcode.getAvailableDevices()).thenReturn('');
final List<IOSDevice> devices = IOSDevice.getAttachedDevices();
when(mockXcode.getAvailableDevices()).thenReturn(new Future<String>.value(''));
final List<IOSDevice> devices = await IOSDevice.getAttachedDevices();
expect(devices, isEmpty);
}, overrides: <Type, Generator>{
Xcode: () => mockXcode,
......@@ -46,7 +48,7 @@ void main() {
testUsingContext('returns attached devices', () async {
when(mockXcode.isInstalled).thenReturn(true);
when(mockXcode.getAvailableDevices()).thenReturn('''
when(mockXcode.getAvailableDevices()).thenReturn(new Future<String>.value('''
Known Devices:
je-mappelle-horse [ED6552C4-B774-5A4E-8B5A-606710C87C77]
La tele me regarde (10.3.2) [98206e7a4afd4aedaff06e687594e089dede3c44]
......@@ -55,8 +57,8 @@ iPhone 6 Plus (9.3) [FBA880E6-4020-49A5-8083-DCD50CA5FA09] (Simulator)
iPhone 6s (11.0) [E805F496-FC6A-4EA4-92FF-B7901FF4E7CC] (Simulator)
iPhone 7 (11.0) + Apple Watch Series 2 - 38mm (4.0) [60027FDD-4A7A-42BF-978F-C2209D27AD61] (Simulator)
iPhone SE (11.0) [667E8DCD-5DCD-4C80-93A9-60D1D995206F] (Simulator)
''');
final List<IOSDevice> devices = IOSDevice.getAttachedDevices();
'''));
final List<IOSDevice> devices = await IOSDevice.getAttachedDevices();
expect(devices, hasLength(2));
expect(devices[0].id, '98206e7a4afd4aedaff06e687594e089dede3c44');
expect(devices[0].name, 'La tele me regarde');
......
......@@ -37,7 +37,7 @@ class MockPollingDeviceDiscovery extends PollingDeviceDiscovery {
MockPollingDeviceDiscovery() : super('mock');
@override
List<Device> pollingGetDevices() => _devices;
Future<List<Device>> pollingGetDevices() async => _devices;
@override
bool get supportsPlatform => true;
......@@ -52,7 +52,7 @@ class MockPollingDeviceDiscovery extends PollingDeviceDiscovery {
}
@override
List<Device> get devices => _devices;
Future<List<Device>> get devices async => _devices;
@override
Stream<Device> get onAdded => _onAddedController.stream;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment