android_device.dart 28 KB
Newer Older
1 2 3 4 5
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
6
import 'dart:convert';
7

8 9
import 'package:meta/meta.dart';

10
import '../android/android_sdk.dart';
11
import '../android/android_workflow.dart';
12
import '../android/apk.dart';
13
import '../application_package.dart';
14
import '../base/common.dart' show throwToolExit;
15
import '../base/file_system.dart';
16
import '../base/io.dart';
17
import '../base/logger.dart';
18
import '../base/port_scanner.dart';
19
import '../base/process.dart';
20
import '../base/process_manager.dart';
21
import '../base/utils.dart';
22
import '../build_info.dart';
23
import '../device.dart';
24
import '../globals.dart';
25
import '../protocol_discovery.dart';
26

27
import 'adb.dart';
28
import 'android.dart';
29
import 'android_sdk.dart';
30

31 32 33 34 35 36 37 38
enum _HardwareType { emulator, physical }

/// Map to help our `isLocalEmulator` detection.
const Map<String, _HardwareType> _knownHardware = const <String, _HardwareType>{
  'goldfish': _HardwareType.emulator,
  'qcom': _HardwareType.physical,
  'ranchu': _HardwareType.emulator,
  'samsungexynos7420': _HardwareType.physical,
39
  'samsungexynos8890': _HardwareType.physical,
40 41 42
  'samsungexynos8895': _HardwareType.physical,
};

43
class AndroidDevices extends PollingDeviceDiscovery {
44
  AndroidDevices() : super('Android devices');
45

46
  @override
47
  bool get supportsPlatform => true;
48

49
  @override
50
  bool get canListAnything => androidWorkflow.canListDevices;
51

52
  @override
53
  Future<List<Device>> pollingGetDevices() async => getAdbDevices();
54 55 56

  @override
  Future<List<String>> getDiagnostics() async => getAdbDeviceDiagnostics();
57 58
}

59
class AndroidDevice extends Device {
60 61 62 63
  AndroidDevice(
    String id, {
    this.productID,
    this.modelID,
64 65
    this.deviceCodeName
  }) : super(id);
66

67 68 69 70
  final String productID;
  final String modelID;
  final String deviceCodeName;

71
  Map<String, String> _properties;
72
  bool _isLocalEmulator;
73 74
  TargetPlatform _platform;

75
  Future<String> _getProperty(String name) async {
76 77
    if (_properties == null) {
      _properties = <String, String>{};
78

79
      final List<String> propCommand = adbCommandForDevice(<String>['shell', 'getprop']);
80
      printTrace(propCommand.join(' '));
81 82 83 84

      try {
        // We pass an encoding of LATIN1 so that we don't try and interpret the
        // `adb shell getprop` result as UTF8.
85
        final ProcessResult result = await processManager.run(
86
          propCommand,
87 88
          stdoutEncoding: LATIN1,
          stderrEncoding: LATIN1,
89
        ).timeout(const Duration(seconds: 5));
90 91 92
        if (result.exitCode == 0) {
          _properties = parseAdbDeviceProperties(result.stdout);
        } else {
93 94
          printError('Error retrieving device properties for $name:');
          printError(result.stderr);
95
        }
96 97 98
      } on TimeoutException catch (_) {
        throwToolExit('adb not responding');
      } on ProcessException catch (error) {
99
        printError('Error retrieving device properties for $name: $error');
100 101
      }
    }
102

103 104
    return _properties[name];
  }
105

106
  @override
107
  Future<bool> get isLocalEmulator async {
108
    if (_isLocalEmulator == null) {
109 110 111 112 113 114 115 116 117 118 119
      final String hardware = await _getProperty('ro.hardware');
      printTrace('ro.hardware = $hardware');
      if (_knownHardware.containsKey(hardware)) {
        // Look for known hardware models.
        _isLocalEmulator = _knownHardware[hardware] == _HardwareType.emulator;
      } else {
        // Fall back to a best-effort heuristic-based approach.
        final String characteristics = await _getProperty('ro.build.characteristics');
        printTrace('ro.build.characteristics = $characteristics');
        _isLocalEmulator = characteristics != null && characteristics.contains('emulator');
      }
120 121 122 123 124
    }
    return _isLocalEmulator;
  }

  @override
125
  Future<TargetPlatform> get targetPlatform async {
126
    if (_platform == null) {
127
      // http://developer.android.com/ndk/guides/abis.html (x86, armeabi-v7a, ...)
128
      switch (await _getProperty('ro.product.cpu.abi')) {
129 130 131 132 133 134 135 136 137
        case 'x86_64':
          _platform = TargetPlatform.android_x64;
          break;
        case 'x86':
          _platform = TargetPlatform.android_x86;
          break;
        default:
          _platform = TargetPlatform.android_arm;
          break;
138
      }
139 140
    }

141
    return _platform;
142
  }
143

144
  @override
145 146
  Future<String> get sdkNameAndVersion async =>
      'Android ${await _sdkVersion} (API ${await _apiVersion})';
147

148
  Future<String> get _sdkVersion => _getProperty('ro.build.version.release');
149

150
  Future<String> get _apiVersion => _getProperty('ro.build.version.sdk');
151

152
  _AdbLogReader _logReader;
153
  _AndroidDevicePortForwarder _portForwarder;
154

155
  List<String> adbCommandForDevice(List<String> args) {
156
    return <String>[getAdbPath(androidSdk), '-s', id]..addAll(args);
157 158 159 160
  }

  bool _isValidAdbVersion(String adbVersion) {
    // Sample output: 'Android Debug Bridge version 1.0.31'
161
    final Match versionFields = new RegExp(r'(\d+)\.(\d+)\.(\d+)').firstMatch(adbVersion);
162
    if (versionFields != null) {
163 164 165
      final int majorVersion = int.parse(versionFields[1]);
      final int minorVersion = int.parse(versionFields[2]);
      final int patchVersion = int.parse(versionFields[3]);
166 167 168 169 170 171 172 173 174 175 176
      if (majorVersion > 1) {
        return true;
      }
      if (majorVersion == 1 && minorVersion > 0) {
        return true;
      }
      if (majorVersion == 1 && minorVersion == 0 && patchVersion >= 32) {
        return true;
      }
      return false;
    }
177
    printError(
178 179 180 181
        'Unrecognized adb version string $adbVersion. Skipping version check.');
    return true;
  }

182
  Future<bool> _checkForSupportedAdbVersion() async {
183 184 185
    if (androidSdk == null)
      return false;

186
    try {
187 188
      final RunResult adbVersion = await runCheckedAsync(<String>[getAdbPath(androidSdk), 'version']);
      if (_isValidAdbVersion(adbVersion.stdout))
189
        return true;
190
      printError('The ADB at "${getAdbPath(androidSdk)}" is too old; please install version 1.0.32 or later.');
191
    } catch (error, trace) {
192
      printError('Error running ADB: $error', stackTrace: trace);
193
    }
194

195 196 197
    return false;
  }

198
  Future<bool> _checkForSupportedAndroidVersion() async {
199 200 201 202 203
    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 *
204
      await runCheckedAsync(<String>[getAdbPath(androidSdk), 'start-server']);
205 206

      // Sample output: '22'
207
      final String sdkVersion = await _getProperty('ro.build.version.sdk');
208

209
      final int sdkVersionParsed = int.parse(sdkVersion, onError: (String source) => null);
210
      if (sdkVersionParsed == null) {
211
        printError('Unexpected response from getprop: "$sdkVersion"');
212 213
        return false;
      }
214

215
      if (sdkVersionParsed < minApiLevel) {
216
        printError(
217 218 219 220
          'The Android version ($sdkVersion) on the target device is too old. Please '
          'use a $minVersionName (version $minApiLevel / $minVersionText) device or later.');
        return false;
      }
221

222 223
      return true;
    } catch (e) {
224
      printError('Unexpected failure from adb: $e');
225
      return false;
226 227 228 229 230 231 232
    }
  }

  String _getDeviceSha1Path(ApplicationPackage app) {
    return '/data/local/tmp/sky.${app.id}.sha1';
  }

233 234 235
  Future<String> _getDeviceApkSha1(ApplicationPackage app) async {
    final RunResult result = await runAsync(adbCommandForDevice(<String>['shell', 'cat', _getDeviceSha1Path(app)]));
    return result.stdout;
236 237
  }

238
  String _getSourceSha1(ApplicationPackage app) {
239 240
    final AndroidApk apk = app;
    final File shaFile = fs.file('${apk.apkPath}.sha1');
Devon Carew's avatar
Devon Carew committed
241
    return shaFile.existsSync() ? shaFile.readAsStringSync() : '';
242 243
  }

244
  @override
245 246 247
  String get name => modelID;

  @override
248
  Future<bool> isAppInstalled(ApplicationPackage app) async {
249
    // This call takes 400ms - 600ms.
250 251 252 253 254 255 256
    try {
      final RunResult listOut = await runCheckedAsync(adbCommandForDevice(<String>['shell', 'pm', 'list', 'packages', app.id]));
      return LineSplitter.split(listOut.stdout).contains('package:${app.id}');
    } catch (error) {
      printTrace('$error');
      return false;
    }
257 258
  }

259
  @override
260 261
  Future<bool> isLatestBuildInstalled(ApplicationPackage app) async {
    final String installedSha1 = await _getDeviceApkSha1(app);
262 263 264
    return installedSha1.isNotEmpty && installedSha1 == _getSourceSha1(app);
  }

265
  @override
266
  Future<bool> installApp(ApplicationPackage app) async {
267
    final AndroidApk apk = app;
268
    if (!fs.isFileSync(apk.apkPath)) {
269
      printError('"${apk.apkPath}" does not exist.');
270 271 272
      return false;
    }

273
    if (!await _checkForSupportedAdbVersion() || !await _checkForSupportedAndroidVersion())
274 275
      return false;

276
    final Status status = logger.startProgress('Installing ${apk.apkPath}...', expectSlowOperation: true);
277
    final RunResult installResult = await runAsync(adbCommandForDevice(<String>['install', '-r', apk.apkPath]));
Devon Carew's avatar
Devon Carew committed
278
    status.stop();
279 280
    // Some versions of adb exit with exit code 0 even on failure :(
    // Parsing the output to check for failures.
281
    final RegExp failureExp = new RegExp(r'^Failure.*$', multiLine: true);
282
    final String failure = failureExp.stringMatch(installResult.stdout);
283 284 285 286
    if (failure != null) {
      printError('Package install error: $failure');
      return false;
    }
287 288
    if (installResult.exitCode != 0) {
      printError('Error: ADB exited with exit code ${installResult.exitCode}');
289
      printError('$installResult');
290 291
      return false;
    }
292

293 294 295
    await runCheckedAsync(adbCommandForDevice(<String>[
      'shell', 'echo', '-n', _getSourceSha1(app), '>', _getDeviceSha1Path(app)
    ]));
296 297 298
    return true;
  }

299
  @override
300
  Future<bool> uninstallApp(ApplicationPackage app) async {
301
    if (!await _checkForSupportedAdbVersion() || !await _checkForSupportedAndroidVersion())
302 303
      return false;

304
    final String uninstallOut = (await runCheckedAsync(adbCommandForDevice(<String>['uninstall', app.id]))).stdout;
305 306
    final RegExp failureExp = new RegExp(r'^Failure.*$', multiLine: true);
    final String failure = failureExp.stringMatch(uninstallOut);
307 308 309 310 311 312 313 314
    if (failure != null) {
      printError('Package uninstall error: $failure');
      return false;
    }

    return true;
  }

315
  Future<bool> _installLatestApp(ApplicationPackage package) async {
316
    final bool wasInstalled = await isAppInstalled(package);
317
    if (wasInstalled) {
318
      if (await isLatestBuildInstalled(package)) {
319
        printTrace('Latest build already installed.');
320 321 322 323
        return true;
      }
    }
    printTrace('Installing APK.');
324
    if (!await installApp(package)) {
325 326 327
      printTrace('Warning: Failed to install APK.');
      if (wasInstalled) {
        printStatus('Uninstalling old version...');
328
        if (!await uninstallApp(package)) {
329 330 331
          printError('Error: Uninstalling old version failed.');
          return false;
        }
332
        if (!await installApp(package)) {
333 334 335 336 337
          printError('Error: Failed to install APK again.');
          return false;
        }
        return true;
      }
338 339 340 341 342
      return false;
    }
    return true;
  }

343 344
  @override
  Future<LaunchResult> startApp(
345
    ApplicationPackage package, {
346
    String mainPath,
347
    String route,
348 349
    DebuggingOptions debuggingOptions,
    Map<String, dynamic> platformArgs,
350 351
    bool prebuiltApplication: false,
    bool applicationNeedsRebuild: false,
352
    bool usesTerminalUi: true,
353
    bool ipv6: false,
354
  }) async {
355
    if (!await _checkForSupportedAdbVersion() || !await _checkForSupportedAndroidVersion())
356
      return new LaunchResult.failed();
357

358
    if (await targetPlatform != TargetPlatform.android_arm && !debuggingOptions.buildInfo.isDebug) {
359 360 361 362
      printError('Profile and release builds are only supported on ARM targets.');
      return new LaunchResult.failed();
    }

363 364
    if (!prebuiltApplication) {
      printTrace('Building APK');
365
      await buildApk(
366
          target: mainPath,
367
          buildInfo: debuggingOptions.buildInfo,
368
      );
369 370
      // Package has been built, so we can get the updated application ID and
      // activity name from the .apk.
371
      package = await AndroidApk.fromCurrentDirectory();
372 373
    }

374 375 376
    printTrace("Stopping app '${package.name}' on $name.");
    await stopApp(package);

377
    if (!await _installLatestApp(package))
378
      return new LaunchResult.failed();
379

380
    final bool traceStartup = platformArgs['trace-startup'] ?? false;
381
    final bool strongMode = platformArgs['strong'] ?? false;
382 383
    final AndroidApk apk = package;
    printTrace('$this startApp');
384

385
    ProtocolDiscovery observatoryDiscovery;
386

387
    if (debuggingOptions.debuggingEnabled) {
388
      // TODO(devoncarew): Remember the forwarding information (so we can later remove the
389
      // port forwarding or set it up again when adb fails on us).
390
      observatoryDiscovery = new ProtocolDiscovery.observatory(
391 392 393 394 395
        getLogReader(),
        portForwarder: portForwarder,
        hostPort: debuggingOptions.observatoryPort,
        ipv6: ipv6,
      );
Devon Carew's avatar
Devon Carew committed
396
    }
397

398 399
    List<String> cmd;

400 401 402 403 404
    cmd = adbCommandForDevice(<String>[
      'shell', 'am', 'start',
      '-a', 'android.intent.action.RUN',
      '-f', '0x20000000',  // FLAG_ACTIVITY_SINGLE_TOP
      '--ez', 'enable-background-compilation', 'true',
405
      '--ez', 'enable-dart-profiling', 'true',
406
    ]);
407

408
    if (traceStartup)
409
      cmd.addAll(<String>['--ez', 'trace-startup', 'true']);
410 411 412
    if (strongMode) {
      cmd.addAll(<String>['--ez', 'strong', 'true']);
    }
413
    if (route != null)
414
      cmd.addAll(<String>['--es', 'route', route]);
415 416
    if (debuggingOptions.enableSoftwareRendering)
      cmd.addAll(<String>['--ez', 'enable-software-rendering', 'true']);
417 418
    if (debuggingOptions.traceSkia)
      cmd.addAll(<String>['--ez', 'trace-skia', 'true']);
419
    if (debuggingOptions.debuggingEnabled) {
420
      if (debuggingOptions.buildInfo.isDebug)
Devon Carew's avatar
Devon Carew committed
421
        cmd.addAll(<String>['--ez', 'enable-checked-mode', 'true']);
422
      if (debuggingOptions.startPaused)
Devon Carew's avatar
Devon Carew committed
423
        cmd.addAll(<String>['--ez', 'start-paused', 'true']);
424 425
      if (debuggingOptions.useTestFonts)
        cmd.addAll(<String>['--ez', 'use-test-fonts', 'true']);
Devon Carew's avatar
Devon Carew committed
426
    }
427
    cmd.add(apk.launchActivity);
428
    final String result = (await runCheckedAsync(cmd)).stdout;
429 430 431
    // This invocation returns 0 even when it fails.
    if (result.contains('Error: ')) {
      printError(result.trim());
Devon Carew's avatar
Devon Carew committed
432
      return new LaunchResult.failed();
433
    }
434

435
    if (!debuggingOptions.debuggingEnabled)
Devon Carew's avatar
Devon Carew committed
436
      return new LaunchResult.succeeded();
Devon Carew's avatar
Devon Carew committed
437

438 439 440
    // Wait for the service protocol port here. This will complete once the
    // device has printed "Observatory is listening on...".
    printTrace('Waiting for observatory port to be available...');
441

442
    // TODO(danrubel) Waiting for observatory services can be made common across all devices.
443
    try {
444 445 446
      Uri observatoryUri;

      if (debuggingOptions.buildInfo.isDebug || debuggingOptions.buildInfo.isProfile) {
447
        observatoryUri = await observatoryDiscovery.uri;
Devon Carew's avatar
Devon Carew committed
448
      }
449

450
      return new LaunchResult.succeeded(observatoryUri: observatoryUri);
451 452 453 454
    } catch (error) {
      printError('Error waiting for a debug connection: $error');
      return new LaunchResult.failed();
    } finally {
455
      await observatoryDiscovery.cancel();
Devon Carew's avatar
Devon Carew committed
456
    }
457 458
  }

459 460 461
  @override
  bool get supportsHotMode => true;

462
  @override
463
  Future<bool> stopApp(ApplicationPackage app) {
464
    final List<String> command = adbCommandForDevice(<String>['shell', 'am', 'force-stop', app.id]);
465
    return runCommandAndStreamOutput(command).then((int exitCode) => exitCode == 0);
466 467
  }

468
  @override
469
  void clearLogs() {
470
    runSync(adbCommandForDevice(<String>['logcat', '-c']));
471 472
  }

473
  @override
474 475 476
  DeviceLogReader getLogReader({ApplicationPackage app}) {
    // The Android log reader isn't app-specific.
    _logReader ??= new _AdbLogReader(this);
477 478
    return _logReader;
  }
479

480
  @override
481
  DevicePortForwarder get portForwarder => _portForwarder ??= new _AndroidDevicePortForwarder(this);
482

483
  static final RegExp _timeRegExp = new RegExp(r'^\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}', multiLine: true);
484

485
  /// Return the most recent timestamp in the Android log or null if there is
486
  /// no available timestamp. The format can be passed to logcat's -T option.
487
  String get lastLogcatTimestamp {
488
    final String output = runCheckedSync(adbCommandForDevice(<String>[
489
      'logcat', '-v', 'time', '-t', '1'
490
    ]));
491

492
    final Match timeMatch = _timeRegExp.firstMatch(output);
493
    return timeMatch?.group(0);
494 495
  }

496
  @override
497 498
  bool isSupported() => true;

Devon Carew's avatar
Devon Carew committed
499 500 501 502
  @override
  bool get supportsScreenshot => true;

  @override
503
  Future<Null> takeScreenshot(File outputFile) async {
Devon Carew's avatar
Devon Carew committed
504
    const String remotePath = '/data/local/tmp/flutter_screenshot.png';
505 506 507
    await runCheckedAsync(adbCommandForDevice(<String>['shell', 'screencap', '-p', remotePath]));
    await runCheckedAsync(adbCommandForDevice(<String>['pull', remotePath, outputFile.path]));
    await runCheckedAsync(adbCommandForDevice(<String>['shell', 'rm', remotePath]));
Devon Carew's avatar
Devon Carew committed
508
  }
509 510

  @override
511
  Future<List<DiscoveredApp>> discoverApps() async {
512 513 514 515
    final RegExp discoverExp = new RegExp(r'DISCOVER: (.*)');
    final List<DiscoveredApp> result = <DiscoveredApp>[];
    final StreamSubscription<String> logs = getLogReader().logLines.listen((String line) {
      final Match match = discoverExp.firstMatch(line);
516
      if (match != null) {
517
        final Map<String, dynamic> app = JSON.decode(match.group(1));
518
        result.add(new DiscoveredApp(app['id'], app['observatoryPort']));
519 520 521
      }
    });

522
    await runCheckedAsync(adbCommandForDevice(<String>[
523 524 525
      'shell', 'am', 'broadcast', '-a', 'io.flutter.view.DISCOVER'
    ]));

526 527 528 529
    await waitGroup<Null>(<Future<Null>>[
      new Future<Null>.delayed(const Duration(seconds: 1)),
      logs.cancel(),
    ]);
530
    return result;
531
  }
532
}
533

534
Map<String, String> parseAdbDeviceProperties(String str) {
535
  final Map<String, String> properties = <String, String>{};
536 537 538 539 540 541
  final RegExp propertyExp = new RegExp(r'\[(.*?)\]: \[(.*?)\]');
  for (Match match in propertyExp.allMatches(str))
    properties[match.group(1)] = match.group(2);
  return properties;
}

542
/// Return the list of connected ADB devices.
543 544 545 546 547
List<AndroidDevice> getAdbDevices() {
  final String adbPath = getAdbPath(androidSdk);
  if (adbPath == null)
    return <AndroidDevice>[];
  final String text = runSync(<String>[adbPath, 'devices', '-l']);
548
  final List<AndroidDevice> devices = <AndroidDevice>[];
549 550 551 552 553 554 555 556 557 558 559 560 561
  parseADBDeviceOutput(text, devices: devices);
  return devices;
}

/// Get diagnostics about issues with any connected devices.
Future<List<String>> getAdbDeviceDiagnostics() async {
  final String adbPath = getAdbPath(androidSdk);
  if (adbPath == null)
    return <String>[];

  final RunResult result = await runAsync(<String>[adbPath, 'devices', '-l']);
  if (result.exitCode != 0) {
    return <String>[];
562
  } else {
563 564 565 566
    final String text = result.stdout;
    final List<String> diagnostics = <String>[];
    parseADBDeviceOutput(text, diagnostics: diagnostics);
    return diagnostics;
567
  }
568 569 570 571
}

// 015d172c98400a03       device usb:340787200X product:nakasi model:Nexus_7 device:grouper
final RegExp _kDeviceRegex = new RegExp(r'^(\S+)\s+(\S+)(.*)');
572

573 574 575 576 577 578 579 580
/// Parse the given `adb devices` output in [text], and fill out the given list
/// of devices and possible device issue diagnostics. Either argument can be null,
/// in which case information for that parameter won't be populated.
@visibleForTesting
void parseADBDeviceOutput(String text, {
  List<AndroidDevice> devices,
  List<String> diagnostics
}) {
581 582
  // Check for error messages from adb
  if (!text.contains('List of devices')) {
583 584
    diagnostics?.add(text);
    return;
585 586 587
  }

  for (String line in text.trim().split('\n')) {
588
    // Skip lines like: * daemon started successfully *
589 590 591
    if (line.startsWith('* daemon '))
      continue;

592
    // Skip lines about adb server and client version not matching
593
    if (line.startsWith(new RegExp(r'adb server (version|is out of date)'))) {
594
      diagnostics?.add(line);
595 596 597
      continue;
    }

598 599 600
    if (line.startsWith('List of devices'))
      continue;

601
    if (_kDeviceRegex.hasMatch(line)) {
602
      final Match match = _kDeviceRegex.firstMatch(line);
603

604 605
      final String deviceID = match[1];
      final String deviceState = match[2];
606 607
      String rest = match[3];

608
      final Map<String, String> info = <String, String>{};
609 610 611 612
      if (rest != null && rest.isNotEmpty) {
        rest = rest.trim();
        for (String data in rest.split(' ')) {
          if (data.contains(':')) {
613
            final List<String> fields = data.split(':');
614 615 616 617 618 619 620
            info[fields[0]] = fields[1];
          }
        }
      }

      if (info['model'] != null)
        info['model'] = cleanAdbDeviceName(info['model']);
621 622

      if (deviceState == 'unauthorized') {
623
        diagnostics?.add(
624 625 626 627
          'Device $deviceID is not authorized.\n'
          'You might need to check your device for an authorization dialog.'
        );
      } else if (deviceState == 'offline') {
628
        diagnostics?.add('Device $deviceID is offline.');
629
      } else {
630
        devices?.add(new AndroidDevice(
631 632 633 634 635
          deviceID,
          productID: info['product'],
          modelID: info['model'] ?? deviceID,
          deviceCodeName: info['device']
        ));
636
      }
637
    } else {
638
      diagnostics?.add(
639 640 641 642 643 644 645
        'Unexpected failure parsing device information from adb output:\n'
        '$line\n'
        'Please report a bug at https://github.com/flutter/flutter/issues/new');
    }
  }
}

646
/// A log reader that logs from `adb logcat`.
Devon Carew's avatar
Devon Carew committed
647
class _AdbLogReader extends DeviceLogReader {
Devon Carew's avatar
Devon Carew committed
648 649 650 651 652 653
  _AdbLogReader(this.device) {
    _linesController = new StreamController<String>.broadcast(
      onListen: _start,
      onCancel: _stop
    );
  }
Devon Carew's avatar
Devon Carew committed
654 655 656

  final AndroidDevice device;

Devon Carew's avatar
Devon Carew committed
657
  StreamController<String> _linesController;
658
  Process _process;
659

660
  @override
Devon Carew's avatar
Devon Carew committed
661
  Stream<String> get logLines => _linesController.stream;
662

663
  @override
664
  String get name => device.name;
Devon Carew's avatar
Devon Carew committed
665

666 667 668 669 670 671 672 673 674 675
  DateTime _timeOrigin;

  DateTime _adbTimestampToDateTime(String adbTimestamp) {
    // The adb timestamp format is: mm-dd hours:minutes:seconds.milliseconds
    // Dart's DateTime parse function accepts this format so long as we provide
    // the year, resulting in:
    // yyyy-mm-dd hours:minutes:seconds.milliseconds.
    return DateTime.parse('${new DateTime.now().year}-$adbTimestamp');
  }

Devon Carew's avatar
Devon Carew committed
676
  void _start() {
677
    // Start the adb logcat process.
678 679
    final List<String> args = <String>['logcat', '-v', 'time'];
    final String lastTimestamp = device.lastLogcatTimestamp;
680 681 682 683
    if (lastTimestamp != null)
        _timeOrigin = _adbTimestampToDateTime(lastTimestamp);
    else
        _timeOrigin = null;
684
    runCommand(device.adbCommandForDevice(args)).then<Null>((Process process) {
Devon Carew's avatar
Devon Carew committed
685
      _process = process;
686
      final Utf8Decoder decoder = const Utf8Decoder(allowMalformed: true);
687 688
      _process.stdout.transform(decoder).transform(const LineSplitter()).listen(_onLine);
      _process.stderr.transform(decoder).transform(const LineSplitter()).listen(_onLine);
689
      _process.exitCode.whenComplete(() {
Devon Carew's avatar
Devon Carew committed
690 691 692 693
        if (_linesController.hasListener)
          _linesController.close();
      });
    });
694 695
  }

696 697
  // 'W/ActivityManager(pid): '
  static final RegExp _logFormat = new RegExp(r'^[VDIWEF]\/.*?\(\s*(\d+)\):\s');
698 699 700

  static final List<RegExp> _whitelistedTags = <RegExp>[
    new RegExp(r'^[VDIWEF]\/flutter[^:]*:\s+', caseSensitive: false),
701
    new RegExp(r'^[IE]\/DartVM[^:]*:\s+'),
702
    new RegExp(r'^[WEF]\/AndroidRuntime:\s+'),
703
    new RegExp(r'^[WEF]\/ActivityManager:\s+.*(\bflutter\b|\bdomokit\b|\bsky\b)'),
704 705 706 707
    new RegExp(r'^[WEF]\/System\.err:\s+'),
    new RegExp(r'^[F]\/[\S^:]+:\s+')
  ];

708 709 710 711 712 713 714 715 716
  // 'F/libc(pid): Fatal signal 11'
  static final RegExp _fatalLog = new RegExp(r'^F\/libc\s*\(\s*\d+\):\sFatal signal (\d+)');

  // 'I/DEBUG(pid): ...'
  static final RegExp _tombstoneLine = new RegExp(r'^[IF]\/DEBUG\s*\(\s*\d+\):\s(.+)$');

  // 'I/DEBUG(pid): Tombstone written to: '
  static final RegExp _tombstoneTerminator = new RegExp(r'^Tombstone written to:\s');

717 718 719
  // we default to true in case none of the log lines match
  bool _acceptedLastLine = true;

720 721 722 723 724
  // Whether a fatal crash is happening or not.
  // During a fatal crash only lines from the crash are accepted, the rest are
  // dropped.
  bool _fatalCrash = false;

725 726 727
  // The format of the line is controlled by the '-v' parameter passed to
  // adb logcat. We are currently passing 'time', which has the format:
  // mm-dd hh:mm:ss.milliseconds Priority/Tag( PID): ....
728
  void _onLine(String line) {
729 730 731 732 733 734
    final Match timeMatch = AndroidDevice._timeRegExp.firstMatch(line);
    if (timeMatch == null) {
      return;
    }
    if (_timeOrigin != null) {
      final String timestamp = timeMatch.group(0);
735
      final DateTime time = _adbTimestampToDateTime(timestamp);
736
      if (!time.isAfter(_timeOrigin)) {
737 738 739 740 741 742 743 744 745
        // Ignore log messages before the origin.
        return;
      }
    }
    if (line.length == timeMatch.end) {
      return;
    }
    // Chop off the time.
    line = line.substring(timeMatch.end + 1);
746 747 748
    final Match logMatch = _logFormat.firstMatch(line);
    if (logMatch != null) {
      bool acceptLine = false;
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766

      if (_fatalCrash) {
        // While a fatal crash is going on, only accept lines from the crash
        // Otherwise the crash log in the console may get interrupted

        final Match fatalMatch = _tombstoneLine.firstMatch(line);

        if (fatalMatch != null) {
          acceptLine = true;

          line = fatalMatch[1];

          if (_tombstoneTerminator.hasMatch(fatalMatch[1])) {
            // Hit crash terminator, stop logging the crash info
            _fatalCrash = false;
          }
        }
      } else if (appPid != null && int.parse(logMatch.group(1)) == appPid) {
767
        acceptLine = true;
768 769 770 771 772

        if (_fatalLog.hasMatch(line)) {
          // Hit fatal signal, app is now crashing
          _fatalCrash = true;
        }
773 774 775 776
      } else {
        // Filter on approved names and levels.
        acceptLine = _whitelistedTags.any((RegExp re) => re.hasMatch(line));
      }
777

778 779 780 781
      if (acceptLine) {
        _acceptedLastLine = true;
        _linesController.add(line);
        return;
782
      }
783 784 785 786 787
      _acceptedLastLine = false;
    } else if (line == '--------- beginning of system' ||
               line == '--------- beginning of main' ) {
      // hide the ugly adb logcat log boundaries at the start
      _acceptedLastLine = false;
788
    } else {
789 790 791
      // If it doesn't match the log pattern at all, then pass it through if we
      // passed the last matching line through. It might be a multiline message.
      if (_acceptedLastLine) {
792
        _linesController.add(line);
793 794
        return;
      }
795
    }
Devon Carew's avatar
Devon Carew committed
796 797
  }

Devon Carew's avatar
Devon Carew committed
798 799
  void _stop() {
    // TODO(devoncarew): We should remove adb port forwarding here.
Devon Carew's avatar
Devon Carew committed
800

Devon Carew's avatar
Devon Carew committed
801
    _process?.kill();
Devon Carew's avatar
Devon Carew committed
802 803
  }
}
804 805 806 807 808 809 810 811 812 813

class _AndroidDevicePortForwarder extends DevicePortForwarder {
  _AndroidDevicePortForwarder(this.device);

  final AndroidDevice device;

  static int _extractPort(String portString) {
    return int.parse(portString.trim(), onError: (_) => null);
  }

814
  @override
815 816 817
  List<ForwardedPort> get forwardedPorts {
    final List<ForwardedPort> ports = <ForwardedPort>[];

818
    final String stdout = runCheckedSync(device.adbCommandForDevice(
819 820
      <String>['forward', '--list']
    ));
821

822
    final List<String> lines = LineSplitter.split(stdout).toList();
823 824
    for (String line in lines) {
      if (line.startsWith(device.id)) {
825
        final List<String> splitLine = line.split('tcp:');
826 827 828 829 830 831

        // Sanity check splitLine.
        if (splitLine.length != 3)
          continue;

        // Attempt to extract ports.
832 833
        final int hostPort = _extractPort(splitLine[1]);
        final int devicePort = _extractPort(splitLine[2]);
834 835 836 837 838 839 840 841 842 843 844 845

        // Failed, skip.
        if ((hostPort == null) || (devicePort == null))
          continue;

        ports.add(new ForwardedPort(hostPort, devicePort));
      }
    }

    return ports;
  }

846
  @override
847
  Future<int> forward(int devicePort, { int hostPort }) async {
848 849
    if ((hostPort == null) || (hostPort == 0)) {
      // Auto select host port.
850
      hostPort = await portScanner.findAvailablePort();
851 852
    }

853
    await runCheckedAsync(device.adbCommandForDevice(
854 855
      <String>['forward', 'tcp:$hostPort', 'tcp:$devicePort']
    ));
856 857 858 859

    return hostPort;
  }

860
  @override
Ian Hickson's avatar
Ian Hickson committed
861
  Future<Null> unforward(ForwardedPort forwardedPort) async {
862
    await runCheckedAsync(device.adbCommandForDevice(
863 864
      <String>['forward', '--remove', 'tcp:${forwardedPort.hostPort}']
    ));
865 866
  }
}