android_emulator.dart 3.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2018 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';

import 'package:meta/meta.dart';

import '../android/android_sdk.dart';
import '../android/android_workflow.dart';
11
import '../base/file_system.dart';
12 13
import '../base/io.dart';
import '../base/process_manager.dart';
14 15 16 17 18 19 20 21
import '../emulator.dart';
import 'android_sdk.dart';

class AndroidEmulators extends EmulatorDiscovery {
  @override
  bool get supportsPlatform => true;

  @override
22
  bool get canListAnything => androidWorkflow.canListEmulators;
23 24 25 26 27 28

  @override
  Future<List<Emulator>> get emulators async => getEmulatorAvds();
}

class AndroidEmulator extends Emulator {
29
  AndroidEmulator(String id, [this._properties])
Danny Tuppeny's avatar
Danny Tuppeny committed
30
      : super(id, _properties != null && _properties.isNotEmpty);
31 32 33 34

  Map<String, String> _properties;

  @override
35
  String get name => _prop('hw.device.name');
36 37

  @override
38
  String get manufacturer => _prop('hw.device.manufacturer');
39 40

  @override
41
  String get label => _properties['avd.ini.displayname'];
42

43 44
  String _prop(String name) => _properties != null ? _properties[name] : null;

45
  @override
46 47
  Future<void> launch() async {
    final Future<void> launchResult =
48 49
        processManager.run(<String>[getEmulatorPath(), '-avd', id])
            .then((ProcessResult runResult) {
50
              if (runResult.exitCode != 0) {
51
                throw '${runResult.stdout}\n${runResult.stderr}'.trimRight();
52 53 54 55 56
              }
            });
    // emulator continues running on a successful launch so if we
    // haven't quit within 3 seconds we assume that's a success and just
    // return.
57
    return Future.any<void>(<Future<void>>[
58
      launchResult,
59
      new Future<void>.delayed(const Duration(seconds: 3))
60
    ]);
61
  }
62 63 64 65 66
}

/// Return the list of available emulator AVDs.
List<AndroidEmulator> getEmulatorAvds() {
  final String emulatorPath = getEmulatorPath(androidSdk);
67
  if (emulatorPath == null) {
68
    return <AndroidEmulator>[];
69
  }
Danny Tuppeny's avatar
Danny Tuppeny committed
70

71
  final String listAvdsOutput = processManager.runSync(<String>[emulatorPath, '-list-avds']).stdout;
72

73
  final List<AndroidEmulator> emulators = <AndroidEmulator>[];
74 75 76
  if (listAvdsOutput != null) {
    extractEmulatorAvdInfo(listAvdsOutput, emulators);
  }
77
  return emulators;
78 79 80
}

/// Parse the given `emulator -list-avds` output in [text], and fill out the given list
81 82
/// of emulators by reading information from the relevant ini files.
void extractEmulatorAvdInfo(String text, List<AndroidEmulator> emulators) {
83
  for (String id in text.trim().split('\n').where((String l) => l != '')) {
84
    emulators.add(_loadEmulatorInfo(id));
85 86 87
  }
}

88
AndroidEmulator _loadEmulatorInfo(String id) {
89
  id = id.trim();
90 91 92 93 94 95 96 97 98 99 100 101 102 103
  final String avdPath = getAvdPath();
  if (avdPath != null) {
    final File iniFile = fs.file(fs.path.join(avdPath, '$id.ini'));
    if (iniFile.existsSync()) {
      final Map<String, String> ini = parseIniLines(iniFile.readAsLinesSync());
      if (ini['path'] != null) {
        final File configFile =
            fs.file(fs.path.join(ini['path'], 'config.ini'));
        if (configFile.existsSync()) {
          final Map<String, String> properties =
              parseIniLines(configFile.readAsLinesSync());
          return new AndroidEmulator(id, properties);
        }
      }
104 105 106 107 108 109
    }
  }

  return new AndroidEmulator(id);
}

110
@visibleForTesting
111
Map<String, String> parseIniLines(List<String> contents) {
112 113 114 115
  final Map<String, String> results = <String, String>{};

  final Iterable<List<String>> properties = contents
      .map((String l) => l.trim())
Danny Tuppeny's avatar
Danny Tuppeny committed
116 117 118 119 120 121
      // Strip blank lines/comments
      .where((String l) => l != '' && !l.startsWith('#'))
      // Discard anything that isn't simple name=value
      .where((String l) => l.contains('='))
      // Split into name/value
      .map((String l) => l.split('='));
122 123 124

  for (List<String> property in properties) {
    results[property[0].trim()] = property[1].trim();
125
  }
126 127

  return results;
128
}