Switch many Device methods to be async (#9587)

`adb` can sometimes hang, which will in turn hang the Dart isolate if
we're using `Process.runSync()`. This changes many of the `Device` methods
to return `Future<T>` in order to allow them to use the async process
methods. A future change will add timeouts to the associated calls so
that we can properly alert the user to the hung `adb` process.

This is work towards #7102, #9567
This commit is contained in:
Todd Volkert
2017-04-25 17:23:00 -07:00
committed by GitHub
parent 596eb033c7
commit 60c5ffc1a9
29 changed files with 249 additions and 241 deletions

View File

@@ -51,7 +51,7 @@ class AndroidDevice extends Device {
bool _isLocalEmulator;
TargetPlatform _platform;
String _getProperty(String name) {
Future<String> _getProperty(String name) async {
if (_properties == null) {
_properties = <String, String>{};
@@ -79,19 +79,19 @@ class AndroidDevice extends Device {
}
@override
bool get isLocalEmulator {
Future<bool> get isLocalEmulator async {
if (_isLocalEmulator == null) {
final String characteristics = _getProperty('ro.build.characteristics');
final String characteristics = await _getProperty('ro.build.characteristics');
_isLocalEmulator = characteristics != null && characteristics.contains('emulator');
}
return _isLocalEmulator;
}
@override
TargetPlatform get targetPlatform {
Future<TargetPlatform> get targetPlatform async {
if (_platform == null) {
// http://developer.android.com/ndk/guides/abis.html (x86, armeabi-v7a, ...)
switch (_getProperty('ro.product.cpu.abi')) {
switch (await _getProperty('ro.product.cpu.abi')) {
case 'x86_64':
_platform = TargetPlatform.android_x64;
break;
@@ -108,11 +108,12 @@ class AndroidDevice extends Device {
}
@override
String get sdkNameAndVersion => 'Android $_sdkVersion (API $_apiVersion)';
Future<String> get sdkNameAndVersion async =>
'Android ${await _sdkVersion} (API ${await _apiVersion})';
String get _sdkVersion => _getProperty('ro.build.version.release');
Future<String> get _sdkVersion => _getProperty('ro.build.version.release');
String get _apiVersion => _getProperty('ro.build.version.sdk');
Future<String> get _apiVersion => _getProperty('ro.build.version.sdk');
_AdbLogReader _logReader;
_AndroidDevicePortForwarder _portForwarder;
@@ -160,16 +161,16 @@ class AndroidDevice extends Device {
return false;
}
bool _checkForSupportedAndroidVersion() {
Future<bool> _checkForSupportedAndroidVersion() async {
try {
// If the server is automatically restarted, then we get irrelevant
// output lines like this, which we want to ignore:
// adb server is out of date. killing..
// * daemon started successfully *
runCheckedSync(<String>[getAdbPath(androidSdk), 'start-server']);
await runCheckedAsync(<String>[getAdbPath(androidSdk), 'start-server']);
// Sample output: '22'
final String sdkVersion = _getProperty('ro.build.version.sdk');
final String sdkVersion = await _getProperty('ro.build.version.sdk');
final int sdkVersionParsed = int.parse(sdkVersion, onError: (String source) => null);
if (sdkVersionParsed == null) {
@@ -195,8 +196,9 @@ class AndroidDevice extends Device {
return '/data/local/tmp/sky.${app.id}.sha1';
}
String _getDeviceApkSha1(ApplicationPackage app) {
return runSync(adbCommandForDevice(<String>['shell', 'cat', _getDeviceSha1Path(app)]));
Future<String> _getDeviceApkSha1(ApplicationPackage app) async {
final RunResult result = await runAsync(adbCommandForDevice(<String>['shell', 'cat', _getDeviceSha1Path(app)]));
return result.stdout;
}
String _getSourceSha1(ApplicationPackage app) {
@@ -209,15 +211,15 @@ class AndroidDevice extends Device {
String get name => modelID;
@override
bool isAppInstalled(ApplicationPackage app) {
Future<bool> isAppInstalled(ApplicationPackage app) async {
// This call takes 400ms - 600ms.
final String listOut = runCheckedSync(adbCommandForDevice(<String>['shell', 'pm', 'list', 'packages', app.id]));
return LineSplitter.split(listOut).contains("package:${app.id}");
final RunResult listOut = await runCheckedAsync(adbCommandForDevice(<String>['shell', 'pm', 'list', 'packages', app.id]));
return LineSplitter.split(listOut.stdout).contains("package:${app.id}");
}
@override
bool isLatestBuildInstalled(ApplicationPackage app) {
final String installedSha1 = _getDeviceApkSha1(app);
Future<bool> isLatestBuildInstalled(ApplicationPackage app) async {
final String installedSha1 = await _getDeviceApkSha1(app);
return installedSha1.isNotEmpty && installedSha1 == _getSourceSha1(app);
}
@@ -229,7 +231,7 @@ class AndroidDevice extends Device {
return false;
}
if (!_checkForSupportedAdbVersion() || !_checkForSupportedAndroidVersion())
if (!_checkForSupportedAdbVersion() || !await _checkForSupportedAndroidVersion())
return false;
final Status status = logger.startProgress('Installing ${apk.apkPath}...', expectSlowOperation: true);
@@ -249,11 +251,11 @@ class AndroidDevice extends Device {
}
@override
bool uninstallApp(ApplicationPackage app) {
if (!_checkForSupportedAdbVersion() || !_checkForSupportedAndroidVersion())
Future<bool> uninstallApp(ApplicationPackage app) async {
if (!_checkForSupportedAdbVersion() || !await _checkForSupportedAndroidVersion())
return false;
final String uninstallOut = runCheckedSync(adbCommandForDevice(<String>['uninstall', app.id]));
final String uninstallOut = (await runCheckedAsync(adbCommandForDevice(<String>['uninstall', app.id]))).stdout;
final RegExp failureExp = new RegExp(r'^Failure.*$', multiLine: true);
final String failure = failureExp.stringMatch(uninstallOut);
if (failure != null) {
@@ -265,9 +267,9 @@ class AndroidDevice extends Device {
}
Future<bool> _installLatestApp(ApplicationPackage package) async {
final bool wasInstalled = isAppInstalled(package);
final bool wasInstalled = await isAppInstalled(package);
if (wasInstalled) {
if (isLatestBuildInstalled(package)) {
if (await isLatestBuildInstalled(package)) {
printTrace('Latest build already installed.');
return true;
}
@@ -277,7 +279,7 @@ class AndroidDevice extends Device {
printTrace('Warning: Failed to install APK.');
if (wasInstalled) {
printStatus('Uninstalling old version...');
if (!uninstallApp(package)) {
if (!await uninstallApp(package)) {
printError('Error: Uninstalling old version failed.');
return false;
}
@@ -304,10 +306,10 @@ class AndroidDevice extends Device {
bool prebuiltApplication: false,
bool applicationNeedsRebuild: false,
}) async {
if (!_checkForSupportedAdbVersion() || !_checkForSupportedAndroidVersion())
if (!_checkForSupportedAdbVersion() || !await _checkForSupportedAndroidVersion())
return new LaunchResult.failed();
if (targetPlatform != TargetPlatform.android_arm && mode != BuildMode.debug) {
if (await targetPlatform != TargetPlatform.android_arm && mode != BuildMode.debug) {
printError('Profile and release builds are only supported on ARM targets.');
return new LaunchResult.failed();
}
@@ -369,7 +371,7 @@ class AndroidDevice extends Device {
cmd.addAll(<String>['--ez', 'use-test-fonts', 'true']);
}
cmd.add(apk.launchActivity);
final String result = runCheckedSync(cmd);
final String result = (await runCheckedAsync(cmd)).stdout;
// This invocation returns 0 even when it fails.
if (result.contains('Error: ')) {
printError(result.trim());
@@ -455,16 +457,15 @@ class AndroidDevice extends Device {
bool get supportsScreenshot => true;
@override
Future<Null> takeScreenshot(File outputFile) {
Future<Null> takeScreenshot(File outputFile) async {
const String remotePath = '/data/local/tmp/flutter_screenshot.png';
runCheckedSync(adbCommandForDevice(<String>['shell', 'screencap', '-p', remotePath]));
runCheckedSync(adbCommandForDevice(<String>['pull', remotePath, outputFile.path]));
runCheckedSync(adbCommandForDevice(<String>['shell', 'rm', remotePath]));
return new Future<Null>.value();
await runCheckedAsync(adbCommandForDevice(<String>['shell', 'screencap', '-p', remotePath]));
await runCheckedAsync(adbCommandForDevice(<String>['pull', remotePath, outputFile.path]));
await runCheckedAsync(adbCommandForDevice(<String>['shell', 'rm', remotePath]));
}
@override
Future<List<DiscoveredApp>> discoverApps() {
Future<List<DiscoveredApp>> discoverApps() async {
final RegExp discoverExp = new RegExp(r'DISCOVER: (.*)');
final List<DiscoveredApp> result = <DiscoveredApp>[];
final StreamSubscription<String> logs = getLogReader().logLines.listen((String line) {
@@ -475,14 +476,13 @@ class AndroidDevice extends Device {
}
});
runCheckedSync(adbCommandForDevice(<String>[
await runCheckedAsync(adbCommandForDevice(<String>[
'shell', 'am', 'broadcast', '-a', 'io.flutter.view.DISCOVER'
]));
return new Future<List<DiscoveredApp>>.delayed(const Duration(seconds: 1), () {
logs.cancel();
return result;
});
await new Future<Null>.delayed(const Duration(seconds: 1));
logs.cancel();
return result;
}
}
@@ -744,7 +744,7 @@ class _AndroidDevicePortForwarder extends DevicePortForwarder {
hostPort = await portScanner.findAvailablePort();
}
runCheckedSync(device.adbCommandForDevice(
await runCheckedAsync(device.adbCommandForDevice(
<String>['forward', 'tcp:$hostPort', 'tcp:$devicePort']
));
@@ -753,7 +753,7 @@ class _AndroidDevicePortForwarder extends DevicePortForwarder {
@override
Future<Null> unforward(ForwardedPort forwardedPort) async {
runCheckedSync(device.adbCommandForDevice(
await runCheckedAsync(device.adbCommandForDevice(
<String>['forward', '--remove', 'tcp:${forwardedPort.hostPort}']
));
}