android_device.dart 27.8 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 381 382
    final bool traceStartup = platformArgs['trace-startup'] ?? false;
    final AndroidApk apk = package;
    printTrace('$this startApp');
383

384
    ProtocolDiscovery observatoryDiscovery;
385

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

397 398
    List<String> cmd;

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

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

431
    if (!debuggingOptions.debuggingEnabled)
Devon Carew's avatar
Devon Carew committed
432
      return new LaunchResult.succeeded();
Devon Carew's avatar
Devon Carew committed
433

434 435 436
    // 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...');
437

438
    // TODO(danrubel) Waiting for observatory services can be made common across all devices.
439
    try {
440 441 442
      Uri observatoryUri;

      if (debuggingOptions.buildInfo.isDebug || debuggingOptions.buildInfo.isProfile) {
443
        observatoryUri = await observatoryDiscovery.uri;
Devon Carew's avatar
Devon Carew committed
444
      }
445

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

455 456 457
  @override
  bool get supportsHotMode => true;

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

464
  @override
465
  void clearLogs() {
466
    runSync(adbCommandForDevice(<String>['logcat', '-c']));
467 468
  }

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

476
  @override
477
  DevicePortForwarder get portForwarder => _portForwarder ??= new _AndroidDevicePortForwarder(this);
478

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

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

488
    final Match timeMatch = _timeRegExp.firstMatch(output);
489
    return timeMatch?.group(0);
490 491
  }

492
  @override
493 494
  bool isSupported() => true;

Devon Carew's avatar
Devon Carew committed
495 496 497 498
  @override
  bool get supportsScreenshot => true;

  @override
499
  Future<Null> takeScreenshot(File outputFile) async {
Devon Carew's avatar
Devon Carew committed
500
    const String remotePath = '/data/local/tmp/flutter_screenshot.png';
501 502 503
    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
504
  }
505 506

  @override
507
  Future<List<DiscoveredApp>> discoverApps() async {
508 509 510 511
    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);
512
      if (match != null) {
513
        final Map<String, dynamic> app = JSON.decode(match.group(1));
514
        result.add(new DiscoveredApp(app['id'], app['observatoryPort']));
515 516 517
      }
    });

518
    await runCheckedAsync(adbCommandForDevice(<String>[
519 520 521
      'shell', 'am', 'broadcast', '-a', 'io.flutter.view.DISCOVER'
    ]));

522 523 524 525
    await waitGroup<Null>(<Future<Null>>[
      new Future<Null>.delayed(const Duration(seconds: 1)),
      logs.cancel(),
    ]);
526
    return result;
527
  }
528
}
529

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

538
/// Return the list of connected ADB devices.
539 540 541 542 543
List<AndroidDevice> getAdbDevices() {
  final String adbPath = getAdbPath(androidSdk);
  if (adbPath == null)
    return <AndroidDevice>[];
  final String text = runSync(<String>[adbPath, 'devices', '-l']);
544
  final List<AndroidDevice> devices = <AndroidDevice>[];
545 546 547 548 549 550 551 552 553 554 555 556 557
  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>[];
558
  } else {
559 560 561 562
    final String text = result.stdout;
    final List<String> diagnostics = <String>[];
    parseADBDeviceOutput(text, diagnostics: diagnostics);
    return diagnostics;
563
  }
564 565 566 567
}

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

569 570 571 572 573 574 575 576
/// 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
}) {
577 578
  // Check for error messages from adb
  if (!text.contains('List of devices')) {
579 580
    diagnostics?.add(text);
    return;
581 582 583
  }

  for (String line in text.trim().split('\n')) {
584
    // Skip lines like: * daemon started successfully *
585 586 587
    if (line.startsWith('* daemon '))
      continue;

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

594 595 596
    if (line.startsWith('List of devices'))
      continue;

597
    if (_kDeviceRegex.hasMatch(line)) {
598
      final Match match = _kDeviceRegex.firstMatch(line);
599

600 601
      final String deviceID = match[1];
      final String deviceState = match[2];
602 603
      String rest = match[3];

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

      if (info['model'] != null)
        info['model'] = cleanAdbDeviceName(info['model']);
617 618

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

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

  final AndroidDevice device;

Devon Carew's avatar
Devon Carew committed
653
  StreamController<String> _linesController;
654
  Process _process;
655

656
  @override
Devon Carew's avatar
Devon Carew committed
657
  Stream<String> get logLines => _linesController.stream;
658

659
  @override
660
  String get name => device.name;
Devon Carew's avatar
Devon Carew committed
661

662 663 664 665 666 667 668 669 670 671
  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
672
  void _start() {
673
    // Start the adb logcat process.
674 675
    final List<String> args = <String>['logcat', '-v', 'time'];
    final String lastTimestamp = device.lastLogcatTimestamp;
676 677 678 679
    if (lastTimestamp != null)
        _timeOrigin = _adbTimestampToDateTime(lastTimestamp);
    else
        _timeOrigin = null;
680
    runCommand(device.adbCommandForDevice(args)).then<Null>((Process process) {
Devon Carew's avatar
Devon Carew committed
681
      _process = process;
682
      final Utf8Decoder decoder = const Utf8Decoder(allowMalformed: true);
683 684
      _process.stdout.transform(decoder).transform(const LineSplitter()).listen(_onLine);
      _process.stderr.transform(decoder).transform(const LineSplitter()).listen(_onLine);
685
      _process.exitCode.whenComplete(() {
Devon Carew's avatar
Devon Carew committed
686 687 688 689
        if (_linesController.hasListener)
          _linesController.close();
      });
    });
690 691
  }

692 693
  // 'W/ActivityManager(pid): '
  static final RegExp _logFormat = new RegExp(r'^[VDIWEF]\/.*?\(\s*(\d+)\):\s');
694 695 696

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

704 705 706 707 708 709 710 711 712
  // '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');

713 714 715
  // we default to true in case none of the log lines match
  bool _acceptedLastLine = true;

716 717 718 719 720
  // 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;

721 722 723
  // 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): ....
724
  void _onLine(String line) {
725 726 727 728 729 730
    final Match timeMatch = AndroidDevice._timeRegExp.firstMatch(line);
    if (timeMatch == null) {
      return;
    }
    if (_timeOrigin != null) {
      final String timestamp = timeMatch.group(0);
731
      final DateTime time = _adbTimestampToDateTime(timestamp);
732
      if (!time.isAfter(_timeOrigin)) {
733 734 735 736 737 738 739 740 741
        // Ignore log messages before the origin.
        return;
      }
    }
    if (line.length == timeMatch.end) {
      return;
    }
    // Chop off the time.
    line = line.substring(timeMatch.end + 1);
742 743 744
    final Match logMatch = _logFormat.firstMatch(line);
    if (logMatch != null) {
      bool acceptLine = false;
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762

      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) {
763
        acceptLine = true;
764 765 766 767 768

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

774 775 776 777
      if (acceptLine) {
        _acceptedLastLine = true;
        _linesController.add(line);
        return;
778
      }
779 780 781 782 783
      _acceptedLastLine = false;
    } else if (line == '--------- beginning of system' ||
               line == '--------- beginning of main' ) {
      // hide the ugly adb logcat log boundaries at the start
      _acceptedLastLine = false;
784
    } else {
785 786 787
      // 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) {
788
        _linesController.add(line);
789 790
        return;
      }
791
    }
Devon Carew's avatar
Devon Carew committed
792 793
  }

Devon Carew's avatar
Devon Carew committed
794 795
  void _stop() {
    // TODO(devoncarew): We should remove adb port forwarding here.
Devon Carew's avatar
Devon Carew committed
796

Devon Carew's avatar
Devon Carew committed
797
    _process?.kill();
Devon Carew's avatar
Devon Carew committed
798 799
  }
}
800 801 802 803 804 805 806 807 808 809

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

  final AndroidDevice device;

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

810
  @override
811 812 813
  List<ForwardedPort> get forwardedPorts {
    final List<ForwardedPort> ports = <ForwardedPort>[];

814
    final String stdout = runCheckedSync(device.adbCommandForDevice(
815 816
      <String>['forward', '--list']
    ));
817

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

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

        // Attempt to extract ports.
828 829
        final int hostPort = _extractPort(splitLine[1]);
        final int devicePort = _extractPort(splitLine[2]);
830 831 832 833 834 835 836 837 838 839 840 841

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

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

    return ports;
  }

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

849
    await runCheckedAsync(device.adbCommandForDevice(
850 851
      <String>['forward', 'tcp:$hostPort', 'tcp:$devicePort']
    ));
852 853 854 855

    return hostPort;
  }

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