From 00c7734c8ba983564be97b5ad8fc2abcc8de65f5 Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Thu, 2 Jun 2016 14:00:27 -0700 Subject: [PATCH] Tools daemon handlers for discovering apps and forwarding device ports (#4324) --- .../lib/src/android/android_device.dart | 22 ++++++ .../lib/src/commands/daemon.dart | 73 +++++++++++++++---- packages/flutter_tools/lib/src/device.dart | 11 +++ 3 files changed, 92 insertions(+), 14 deletions(-) diff --git a/packages/flutter_tools/lib/src/android/android_device.dart b/packages/flutter_tools/lib/src/android/android_device.dart index b7051e3bf4..f131774813 100644 --- a/packages/flutter_tools/lib/src/android/android_device.dart +++ b/packages/flutter_tools/lib/src/android/android_device.dart @@ -513,6 +513,28 @@ class AndroidDevice extends Device { return new Future.value(true); } + + @override + Future> discoverApps() { + RegExp discoverExp = new RegExp(r'DISCOVER: (.*)'); + List result = []; + StreamSubscription logs = logReader.logLines.listen((String line) { + Match match = discoverExp.firstMatch(line); + if (match != null) { + Map app = JSON.decode(match.group(1)); + result.add(new DiscoveredApp(app['id'], app['observatoryPort'])); + } + }); + + runCheckedSync(adbCommandForDevice([ + 'shell', 'am', 'broadcast', '-a', 'io.flutter.view.DISCOVER' + ])); + + return new Future>.delayed(new Duration(seconds: 1), () { + logs.cancel(); + return result; + }); + } } // 015d172c98400a03 device usb:340787200X product:nakasi model:Nexus_7 device:grouper diff --git a/packages/flutter_tools/lib/src/commands/daemon.dart b/packages/flutter_tools/lib/src/commands/daemon.dart index 069ff8fc0c..247a4c3ac9 100644 --- a/packages/flutter_tools/lib/src/commands/daemon.dart +++ b/packages/flutter_tools/lib/src/commands/daemon.dart @@ -237,6 +237,23 @@ class DaemonDomain extends Domain { } } +/// Return the device matching the deviceId field in the args. +Future _getDevice(Daemon daemon, Map args) async { + if (args == null || args['deviceId'] is! String) + throw 'deviceId is required'; + + List devices = await daemon.deviceDomain.getDevices(); + Device device = devices.firstWhere( + (Device device) => device.id == args['deviceId'], + orElse: () => null + ); + + if (device == null) + throw "device '${args['deviceId']}' not found"; + + return device; +} + /// This domain responds to methods like [start] and [stop]. /// /// It'll be extended to fire events for when applications start, stop, and @@ -245,14 +262,11 @@ class AppDomain extends Domain { AppDomain(Daemon daemon) : super(daemon, 'app') { registerHandler('start', start); registerHandler('stop', stop); + registerHandler('discover', discover); } Future start(Map args) async { - if (args == null || args['deviceId'] is! String) - throw "deviceId is required"; - Device device = await _getDevice(args['deviceId']); - if (device == null) - throw "device '${args['deviceId']}' not found"; + Device device = await _getDevice(daemon, args); if (args['projectDirectory'] is! String) throw "projectDirectory is required"; @@ -282,12 +296,8 @@ class AppDomain extends Domain { return null; } - Future stop(dynamic args) async { - if (args == null || args['deviceId'] is! String) - throw "deviceId is required"; - Device device = await _getDevice(args['deviceId']); - if (device == null) - throw "device '${args['deviceId']}' not found"; + Future stop(Map args) async { + Device device = await _getDevice(daemon, args); if (args['projectDirectory'] is! String) throw "projectDirectory is required"; @@ -306,9 +316,12 @@ class AppDomain extends Domain { } } - Future _getDevice(String deviceId) async { - List devices = await daemon.deviceDomain.getDevices(); - return devices.firstWhere((Device device) => device.id == deviceId, orElse: () => null); + Future>> discover(Map args) async { + Device device = await _getDevice(daemon, args); + List apps = await device.discoverApps(); + return apps.map((DiscoveredApp app) => + {'id': app.id, 'observatoryDevicePort': app.observatoryPort} + ).toList(); } } @@ -321,6 +334,8 @@ class DeviceDomain extends Domain { registerHandler('getDevices', getDevices); registerHandler('enable', enable); registerHandler('disable', disable); + registerHandler('forward', forward); + registerHandler('unforward', unforward); PollingDeviceDiscovery deviceDiscovery = new AndroidDevices(); if (deviceDiscovery.supportsPlatform) @@ -369,6 +384,36 @@ class DeviceDomain extends Domain { return new Future.value(); } + /// Forward a host port to a device port. + Future> forward(Map args) async { + Device device = await _getDevice(daemon, args); + + if (args['devicePort'] is! int) + throw 'devicePort is required'; + int devicePort = args['devicePort']; + + int hostPort = args['hostPort']; + + hostPort = await device.portForwarder.forward(devicePort, hostPort: hostPort); + + return {'hostPort': hostPort}; + } + + /// Removes a forwarded port. + Future unforward(Map args) async { + Device device = await _getDevice(daemon, args); + + if (args['devicePort'] is! int) + throw 'devicePort is required'; + int devicePort = args['devicePort']; + + if (args['hostPort'] is! int) + throw 'hostPort is required'; + int hostPort = args['hostPort']; + + device.portForwarder.unforward(new ForwardedPort(hostPort, devicePort)); + } + @override void dispose() { for (PollingDeviceDiscovery discoverer in _discoverers) { diff --git a/packages/flutter_tools/lib/src/device.dart b/packages/flutter_tools/lib/src/device.dart index d0451f05c1..fc96d64ae3 100644 --- a/packages/flutter_tools/lib/src/device.dart +++ b/packages/flutter_tools/lib/src/device.dart @@ -205,6 +205,10 @@ abstract class Device { Future takeScreenshot(File outputFile) => new Future.error('unimplemented'); + /// Find the apps that are currently running on this device. + Future> discoverApps() => + new Future>.value([]); + @override int get hashCode => id.hashCode; @@ -333,3 +337,10 @@ abstract class DeviceLogReader { @override String toString() => name; } + +/// Describes an app running on the device. +class DiscoveredApp { + DiscoveredApp(this.id, this.observatoryPort); + final String id; + final int observatoryPort; +}