protected override Task <ExitCode> InvokeInternal(ILogger logger) { logger.LogInformation("Getting state of ADB and attached Android device(s)"); try { var runner = new AdbRunner(logger); string state = runner.GetAdbState(); if (string.IsNullOrEmpty(state)) { state = "No device attached"; } logger.LogInformation($"ADB Version info:{Environment.NewLine}{runner.GetAdbVersion()}"); logger.LogInformation($"ADB State ('device' if physically attached):{Environment.NewLine}{state}"); logger.LogInformation($"List of devices:"); var deviceAndArchList = runner.GetAttachedDevicesAndArchitectures(); foreach (string device in deviceAndArchList.Keys) { logger.LogInformation($"Device: '{device}' - Architecture: {deviceAndArchList[device]}"); } return(Task.FromResult(ExitCode.SUCCESS)); } catch (Exception toLog) { logger.LogCritical(toLog, $"Error: {toLog.Message}"); return(Task.FromResult(ExitCode.GENERAL_FAILURE)); } }
private string?GetDeviceToUse(ILogger logger, AdbRunner runner, string apkRequiredArchitecture) { var allDevicesAndTheirArchitectures = runner.GetAttachedDevicesAndArchitectures(); if (allDevicesAndTheirArchitectures.Count == 1) { // There's only one device, so -s argument isn't needed but still check if (!allDevicesAndTheirArchitectures.Values.Contains(apkRequiredArchitecture, StringComparer.InvariantCultureIgnoreCase)) { logger.LogWarning($"Single device available has architecture '{allDevicesAndTheirArchitectures.Values.First()}', != '{apkRequiredArchitecture}'. Package installation will likely fail, but we'll try anyways."); } return(null); // null = Use default device } else if (allDevicesAndTheirArchitectures.Count > 1) { if (allDevicesAndTheirArchitectures.Any(kvp => kvp.Value != null && kvp.Value.Equals(apkRequiredArchitecture, StringComparison.OrdinalIgnoreCase))) { var firstAvailableCompatible = allDevicesAndTheirArchitectures.Where(kvp => kvp.Value != null && kvp.Value.Equals(apkRequiredArchitecture, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); logger.LogInformation($"Using first-found compatible device of {allDevicesAndTheirArchitectures.Count} total- serial: '{firstAvailableCompatible.Key}' - Arch: {firstAvailableCompatible.Value}"); return(firstAvailableCompatible.Key); } else { logger.LogWarning($"No devices found with architecture '{apkRequiredArchitecture}'. Just returning first available device; installation will likely fail, but we'll try anyways."); return(allDevicesAndTheirArchitectures.Keys.First()); } } logger.LogError("No attached device detected"); return(null); }
public void ListDevicesAndArchitectures() { var runner = new AdbRunner(_mainLog.Object, _processManager.Object, s_adbPath); var result = runner.GetAttachedDevicesAndArchitectures(); _processManager.Verify(pm => pm.Run(s_adbPath, "devices -l", TimeSpan.FromSeconds(30)), Times.Once); // Ensure it called, parsed the three random device names and found all three architectures foreach (var fakeDeviceInfo in _fakeDeviceList.Keys) { _processManager.Verify(pm => pm.Run(s_adbPath, $"-s {fakeDeviceInfo.Item1} shell getprop ro.product.cpu.abi", TimeSpan.FromMinutes(5)), Times.Once); Assert.Equal(fakeDeviceInfo.Item2, result[fakeDeviceInfo.Item1]); } Assert.Equal(3, result.Count); }