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

5
import 'package:process/process.dart';
6 7

import '../base/common.dart';
8
import '../base/file_system.dart';
9 10
import '../base/io.dart';
import '../base/logger.dart';
11
import '../base/platform.dart';
12
import '../base/process.dart';
13
import '../base/user_messages.dart';
14 15 16 17
import '../device.dart';
import 'adb.dart';
import 'android_device.dart';
import 'android_sdk.dart';
18
import 'android_workflow.dart';
19

20
/// Device discovery for Android physical devices and emulators.
21 22 23 24 25 26
///
/// This class primarily delegates to the `adb` command line tool provided by
/// the Android SDK to discover instances of connected android devices.
///
/// See also:
///   * [AndroidDevice], the type of discovered device.
27 28
class AndroidDevices extends PollingDeviceDiscovery {
  AndroidDevices({
29 30 31 32 33 34 35
    required AndroidWorkflow androidWorkflow,
    required ProcessManager processManager,
    required Logger logger,
    AndroidSdk? androidSdk,
    required FileSystem fileSystem,
    required Platform platform,
    required UserMessages userMessages,
36 37
  }) : _androidWorkflow = androidWorkflow,
       _androidSdk = androidSdk,
38
       _processUtils = ProcessUtils(
39 40
         logger: logger,
         processManager: processManager,
41
        ),
42 43
        _processManager = processManager,
        _logger = logger,
44 45 46 47
        _fileSystem = fileSystem,
        _platform = platform,
        _userMessages = userMessages,
        super('Android devices');
48 49 50

  final AndroidWorkflow _androidWorkflow;
  final ProcessUtils _processUtils;
51
  final AndroidSdk? _androidSdk;
52 53 54 55
  final ProcessManager _processManager;
  final Logger _logger;
  final FileSystem _fileSystem;
  final Platform _platform;
56
  final UserMessages _userMessages;
57 58

  @override
59
  bool get supportsPlatform => _androidWorkflow.appliesToHostPlatform;
60 61 62 63 64

  @override
  bool get canListAnything => _androidWorkflow.canListDevices;

  @override
65
  Future<List<Device>> pollingGetDevices({ Duration? timeout }) async {
66
    if (_doesNotHaveAdb()) {
67 68 69 70
      return <AndroidDevice>[];
    }
    String text;
    try {
71
      text = (await _processUtils.run(<String>[_androidSdk!.adbPath!, 'devices', '-l'],
72 73 74
        throwOnError: true,
      )).stdout.trim();
    } on ProcessException catch (exception) {
75 76 77 78
      throwToolExit(
        'Unable to run "adb", check your Android SDK installation and '
        '$kAndroidSdkRoot environment variable: ${exception.executable}',
      );
79 80
    }
    final List<AndroidDevice> devices = <AndroidDevice>[];
81
    _parseADBDeviceOutput(
82 83 84
      text,
      devices: devices,
    );
85 86 87 88 89
    return devices;
  }

  @override
  Future<List<String>> getDiagnostics() async {
90
    if (_doesNotHaveAdb()) {
91 92 93
      return <String>[];
    }

94
    final RunResult result = await _processUtils.run(<String>[_androidSdk!.adbPath!, 'devices', '-l']);
95 96 97
    if (result.exitCode != 0) {
      return <String>[];
    }
98 99 100 101 102 103
    final List<String> diagnostics = <String>[];
    _parseADBDeviceOutput(
      result.stdout,
      diagnostics: diagnostics,
    );
    return diagnostics;
104 105
  }

106 107
  bool _doesNotHaveAdb() {
    return _androidSdk == null ||
108 109
      _androidSdk?.adbPath == null ||
      !_processManager.canRun(_androidSdk!.adbPath);
110 111
  }

112 113 114 115 116 117
  // 015d172c98400a03       device usb:340787200X product:nakasi model:Nexus_7 device:grouper
  static final RegExp _kDeviceRegex = RegExp(r'^(\S+)\s+(\S+)(.*)');

  /// 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.
118
  void _parseADBDeviceOutput(
119
    String text, {
120 121
    List<AndroidDevice>? devices,
    List<String>? diagnostics,
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  }) {
    // Check for error messages from adb
    if (!text.contains('List of devices')) {
      diagnostics?.add(text);
      return;
    }

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

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

      if (line.startsWith('List of devices')) {
        continue;
      }

      if (_kDeviceRegex.hasMatch(line)) {
146
        final Match match = _kDeviceRegex.firstMatch(line)!;
147

148 149 150
        final String deviceID = match[1]!;
        final String deviceState = match[2]!;
        String rest = match[3]!;
151 152 153 154 155 156 157 158 159 160 161 162

        final Map<String, String> info = <String, String>{};
        if (rest != null && rest.isNotEmpty) {
          rest = rest.trim();
          for (final String data in rest.split(' ')) {
            if (data.contains(':')) {
              final List<String> fields = data.split(':');
              info[fields[0]] = fields[1];
            }
          }
        }

163 164 165
        final String? model = info['model'];
        if (model != null) {
          info['model'] = cleanAdbDeviceName(model);
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
        }

        if (deviceState == 'unauthorized') {
          diagnostics?.add(
            'Device $deviceID is not authorized.\n'
            'You might need to check your device for an authorization dialog.'
          );
        } else if (deviceState == 'offline') {
          diagnostics?.add('Device $deviceID is offline.');
        } else {
          devices?.add(AndroidDevice(
            deviceID,
            productID: info['product'],
            modelID: info['model'] ?? deviceID,
            deviceCodeName: info['device'],
181
            androidSdk: _androidSdk!,
182 183 184 185
            fileSystem: _fileSystem,
            logger: _logger,
            platform: _platform,
            processManager: _processManager,
186 187 188 189 190 191
          ));
        }
      } else {
        diagnostics?.add(
          'Unexpected failure parsing device information from adb output:\n'
          '$line\n'
192
          '${_userMessages.flutterToolBugInstructions}');
193 194 195
      }
    }
  }
196 197 198

  @override
  List<String> get wellKnownIds => const <String>[];
199
}