getPhysicalDevices method

  1. @override
Future<List<Device>> getPhysicalDevices()
override

Implementation

@override
Future<List<Device>> getPhysicalDevices() async {
  try {
    final result = await _exec.run(
      adbPath,
      arguments: ['devices', '-l'],
      timeout: _deviceListTimeout,
    );
    if (!result.success) return [];

    final stdout = result.stdout;

    final rawDevices = stdout
        .split('\n')
        .map((l) => l.trim())
        .where((l) => l.isNotEmpty)
        .where((l) => l != 'List of devices attached')
        .where((l) => !l.startsWith('*'))
        .where((l) => !l.startsWith('daemon'))
        .where((l) => !l.startsWith('adb-'))
        .where((l) => !l.startsWith('emulator-'))
        .where((l) {
          final parts = l.split(RegExp(r'\s+'));
          return parts.length >= 2 &&
              !(parts.length >= 3 &&
                  parts[1] == 'no' &&
                  parts[2].startsWith('permissions')) &&
              _physicalDeviceIdPattern.hasMatch(parts.first);
        });

    return rawDevices
        .map((line) {
          final parts = line.split(RegExp(r'\s+'));
          final id = parts.isNotEmpty ? parts.first : '';
          final adbState = parts.length > 1 ? parts[1] : '';

          var name = id;
          for (final part in parts) {
            if (part.startsWith('model:')) {
              name = part.substring(6).replaceAll('_', ' ');
              break;
            }
          }

          return Device.android(
            id: id,
            name: name,
            type: DeviceType.physical,
            state: switch (adbState) {
              'device' => DeviceState.booted,
              'unauthorized' ||
              'offline' ||
              'recovery' ||
              'sideload' => DeviceState.booting,
              _ => DeviceState.shutdown,
            },
          );
        })
        .where((d) => d.id.isNotEmpty)
        .toList();
  } catch (e) {
    return [];
  }
}