public ProcessRunner(FileInfo executable, ProcessArgumentBuilder builder) : this(executable, builder, System.Threading.CancellationToken.None) { }
internal ProcessResult RunAdb(DirectoryInfo androidSdkHome, ProcessArgumentBuilder builder) => RunAdb(androidSdkHome, builder, System.Threading.CancellationToken.None);
public bool StartActivity(string adbIntentArguments, ActivityManagerStartOptions options = null) { if (options == null) { options = new ActivityManagerStartOptions(); } // start [options] intent var builder = new ProcessArgumentBuilder(); runner.AddSerial(AdbSerial, builder); builder.Append("shell"); builder.Append("am"); builder.Append("start"); if (options.EnableDebugging) { builder.Append("-D"); } if (options.WaitForLaunch) { builder.Append("-W"); } if (options.ProfileToFile != null) { if (options.ProfileUntilIdle) { builder.Append("-P"); } else { builder.Append("--start"); } builder.AppendQuoted(options.ProfileToFile.FullName); } if (options.RepeatLaunch.HasValue && options.RepeatLaunch.Value > 0) { builder.Append("-R"); builder.Append(options.RepeatLaunch.Value.ToString()); } if (options.ForceStopTarget) { builder.Append("-S"); } if (options.EnableOpenGLTrace) { builder.Append("--opengl-trace"); } if (!string.IsNullOrEmpty(options.RunAsUserId)) { builder.Append("--user"); builder.Append(options.RunAsUserId); } builder.Append(adbIntentArguments); var r = runner.RunAdb(AndroidSdkHome, builder); return(r.StandardOutput.Any(l => l.StartsWith("Starting:", StringComparison.OrdinalIgnoreCase))); }
public List <string> Logcat(AdbLogcatOptions options = null, string adbSerial = null) { // logcat[option][filter - specs] if (options == null) { options = new AdbLogcatOptions(); } // adb uninstall -k <package> // -k keeps data & cache dir var builder = new ProcessArgumentBuilder(); runner.AddSerial(adbSerial, builder); builder.Append("logcat"); if (options.BufferType != AdbLogcatBufferType.Main) { builder.Append("-b"); builder.Append(options.BufferType.ToString().ToLowerInvariant()); } if (options.Clear || options.PrintSize) { if (options.Clear) { builder.Append("-c"); } else if (options.PrintSize) { builder.Append("-g"); } } else { // Always dump, since we want to return and not listen to logcat forever // in the future might be nice to add an alias that takes a cancellation token // and can pipe output until that token is cancelled. //if (options.Dump) builder.Append("-d"); if (options.OutputFile != null) { builder.Append("-f"); builder.AppendQuoted(options.OutputFile.FullName); if (options.NumRotatedLogs.HasValue) { builder.Append("-n"); builder.Append(options.NumRotatedLogs.Value.ToString()); } var kb = options.LogRotationKb ?? 16; builder.Append("-r"); builder.Append(kb.ToString()); } if (options.SilentFilter) { builder.Append("-s"); } if (options.Verbosity != AdbLogcatOutputVerbosity.Brief) { builder.Append("-v"); builder.Append(options.Verbosity.ToString().ToLowerInvariant()); } } var r = runner.RunAdb(AndroidSdkHome, builder); return(r.StandardOutput); }
public AndroidEmulatorProcess Start(string avdName, EmulatorStartOptions options = null) { if (options == null) { options = new EmulatorStartOptions(); } var builder = new ProcessArgumentBuilder(); builder.Append($"-avd {avdName}"); if (options.NoSnapshotLoad) { builder.Append("-no-snapshot-load"); } if (options.NoSnapshotSave) { builder.Append("-no-snapshot-save"); } if (options.NoSnapshot) { builder.Append("-no-snapshot"); } if (!string.IsNullOrEmpty(options.CameraBack)) { builder.Append($"-camera-back {options.CameraBack}"); } if (!string.IsNullOrEmpty(options.CameraFront)) { builder.Append($"-camera-front {options.CameraFront}"); } if (options.MemoryMegabytes.HasValue) { builder.Append($"-memory {options.MemoryMegabytes}"); } if (options.SdCard != null) { builder.Append("-sdcard"); builder.AppendQuoted(options.SdCard.FullName); } if (options.WipeData) { builder.Append("-wipe-data"); } if (options.Debug != null && options.Debug.Length > 0) { builder.Append("-debug " + string.Join(",", options.Debug)); } if (options.Logcat != null && options.Logcat.Length > 0) { builder.Append("-logcat " + string.Join(",", options.Logcat)); } if (options.ShowKernel) { builder.Append("-show-kernel"); } if (options.Verbose) { builder.Append("-verbose"); } if (options.DnsServers != null && options.DnsServers.Length > 0) { builder.Append("-dns-server " + string.Join(",", options.DnsServers)); } if (!string.IsNullOrEmpty(options.HttpProxy)) { builder.Append($"-http-proxy {options.HttpProxy}"); } if (!string.IsNullOrEmpty(options.NetDelay)) { builder.Append($"-netdelay {options.NetDelay}"); } if (options.NetFast) { builder.Append("-netfast"); } if (!string.IsNullOrEmpty(options.NetSpeed)) { builder.Append($"-netspeed {options.NetSpeed}"); } if (options.Ports.HasValue) { builder.Append($"-ports {options.Ports.Value.console},{options.Ports.Value.adb}"); } else if (options.Port.HasValue) { builder.Append($"-port {options.Port.Value}"); } if (options.TcpDump != null) { builder.Append("-tcpdump"); builder.AppendQuoted(options.TcpDump.FullName); } if (options.Acceleration.HasValue) { builder.Append($"-accel {options.Acceleration.Value.ToString().ToLowerInvariant()}"); } if (options.NoAccel) { builder.Append("-no-accel"); } if (options.Engine.HasValue) { builder.Append($"-engine {options.Engine.Value.ToString().ToLowerInvariant()}"); } if (options.NoJni) { builder.Append("-no-jni"); } if (options.SeLinux.HasValue) { builder.Append($"-selinux {options.SeLinux.Value.ToString().ToLowerInvariant()}"); } if (!string.IsNullOrEmpty(options.Timezone)) { builder.Append($"-timezone {options.Timezone}"); } if (options.NoBootAnim) { builder.Append("-no-boot-anim"); } if (options.Screen.HasValue) { builder.Append($"-screen {options.Screen.Value.ToString().ToLowerInvariant()}"); } //var uuid = Guid.NewGuid().ToString("D"); //builder.Append($"-prop emu.uuid={uuid}"); if (options.ExtraArgs != null && options.ExtraArgs.Length > 0) { foreach (var arg in options.ExtraArgs) { builder.Append(arg); } } return(new AndroidEmulatorProcess(Start(builder), avdName, AndroidSdkHome)); }
public List <AdbDevice> GetDevices() { var devices = new List <AdbDevice>(); //adb devices -l var builder = new ProcessArgumentBuilder(); builder.Append("devices"); builder.Append("-l"); var r = runner.RunAdb(AndroidSdkHome, builder); if (r.StandardOutput.Count > 1) { foreach (var line in r.StandardOutput?.Skip(1)) { var parts = Regex.Split(line, "\\s+"); var d = new AdbDevice { Serial = parts[0].Trim() }; if (parts.Length > 1 && (parts[1]?.ToLowerInvariant() ?? "offline") == "offline") { continue; } if (parts.Length > 2) { foreach (var part in parts.Skip(2)) { var bits = part.Split(new[] { ':' }, 2); if (bits == null || bits.Length != 2) { continue; } switch (bits[0].ToLower()) { case "usb": d.Usb = bits[1]; break; case "product": d.Product = bits[1]; break; case "model": d.Model = bits[1]; break; case "device": d.Device = bits[1]; break; } } } if (!string.IsNullOrEmpty(d?.Serial)) { devices.Add(d); } } } return(devices); }
List <string> RunWithAccept(ProcessArgumentBuilder builder, bool moveToolsToTemp = false) => RunWithAccept(builder, TimeSpan.Zero, moveToolsToTemp);
public SdkManagerList List() { var result = new SdkManagerList(); CheckSdkManagerVersion(); //adb devices -l var builder = new ProcessArgumentBuilder(); builder.Append("--list --verbose"); BuildStandardOptions(builder); var p = Run(builder); int section = 0; var path = string.Empty; var description = string.Empty; var version = string.Empty; var location = string.Empty; foreach (var line in p) { if (line.StartsWith("------")) { continue; } if (line.ToLowerInvariant().Contains("installed packages:")) { section = 1; continue; } else if (line.ToLowerInvariant().Contains("available packages:")) { section = 2; continue; } else if (line.ToLowerInvariant().Contains("available updates:")) { section = 3; continue; } if (section >= 1 && section <= 2) { if (string.IsNullOrEmpty(path)) { // If we have spaces preceding the line, it's not a new item yet if (line.StartsWith(" ")) { continue; } path = line.Trim(); continue; } if (rxListDesc.IsMatch(line)) { description = rxListDesc.Match(line)?.Groups?["desc"]?.Value; continue; } if (rxListVers.IsMatch(line)) { version = rxListVers.Match(line)?.Groups?["ver"]?.Value; continue; } if (rxListLoc.IsMatch(line)) { location = rxListLoc.Match(line)?.Groups?["loc"]?.Value; continue; } // If we got here, we should have a good line of data if (section == 1) { result.InstalledPackages.Add(new InstalledSdkPackage { Path = path, Version = version, Description = description, Location = location }); } else if (section == 2) { result.AvailablePackages.Add(new SdkPackage { Path = path, Version = version, Description = description }); } path = null; description = null; version = null; location = null; } } return(result); }
public List <PackageListInfo> ListPackages(bool includeUninstalled = false, PackageListState showState = PackageListState.All, PackageSourceType showSource = PackageSourceType.All) { // list packages [options] filter // start [options] intent var builder = new ProcessArgumentBuilder(); runner.AddSerial(AdbSerial, builder); builder.Append("shell"); builder.Append("pm"); builder.Append("list"); builder.Append("packages"); builder.Append("-f"); builder.Append("-i"); if (showState == PackageListState.OnlyDisabled) { builder.Append("-d"); } else if (showState == PackageListState.OnlyEnabled) { builder.Append("-e"); } if (showSource == PackageSourceType.OnlySystem) { builder.Append("-s"); } else if (showSource == PackageSourceType.OnlyThirdParty) { builder.Append("-3"); } if (includeUninstalled) { builder.Append("-u"); } var r = runner.RunAdb(AndroidSdkHome, builder); var results = new List <PackageListInfo>(); const string rxPackageListInfo = "^package:(?<path>.*?)=(?<package>.*?)\\s+installer=(?<installer>.*?)$"; foreach (var line in r.StandardOutput) { var m = Regex.Match(line, rxPackageListInfo, RegexOptions.Singleline); var installPath = m?.Groups?["path"]?.Value; var packageName = m?.Groups?["package"]?.Value; var installer = m?.Groups?["installer"]?.Value; if (!string.IsNullOrEmpty(installPath) && !string.IsNullOrEmpty(packageName)) { results.Add(new PackageListInfo { InstallPath = new FileInfo(installPath), PackageName = packageName, Installer = installer, }); } } return(results); }
public List <PermissionGroupInfo> ListPermissions(bool onlyDangerous = false, bool onlyUserVisible = false) { // list packages [options] filter // start [options] intent var builder = new ProcessArgumentBuilder(); runner.AddSerial(AdbSerial, builder); builder.Append("shell"); builder.Append("pm"); builder.Append("list"); builder.Append("permissions"); builder.Append("-g"); builder.Append("-f"); if (onlyDangerous) { builder.Append("-d"); } if (onlyUserVisible) { builder.Append("-u"); } var r = runner.RunAdb(AndroidSdkHome, builder); var results = new List <PermissionGroupInfo>(); PermissionGroupInfo currentGroup = null; PermissionInfo currentPerm = null; foreach (var line in r.StandardOutput) { if (string.IsNullOrWhiteSpace(line) || line.StartsWith("All Permissions:", StringComparison.OrdinalIgnoreCase)) { continue; } if (line.StartsWith("+ group:")) { if (currentPerm != null) { currentGroup.Permissions.Add(currentPerm); currentPerm = null; } if (currentGroup != null) { results.Add(currentGroup); } currentGroup = new PermissionGroupInfo(); currentGroup.Group = line.Substring(8); } else if (line.StartsWith(" package:")) { currentGroup.PackageName = line.Substring(10); } else if (line.StartsWith(" label:")) { currentGroup.Label = line.Substring(8); } else if (line.StartsWith(" description:")) { currentGroup.Label = line.Substring(14); } else if (line.StartsWith(" + permission:")) { if (currentPerm != null && currentGroup != null) { currentGroup.Permissions.Add(currentPerm); } currentPerm = new PermissionInfo(); currentPerm.Permission = line.Substring(15); } else if (line.StartsWith(" package:")) { currentPerm.PackageName = line.Substring(12); } else if (line.StartsWith(" label:")) { currentPerm.Label = line.Substring(10); } else if (line.StartsWith(" description:")) { currentPerm.Description = line.Substring(16); } else if (line.StartsWith(" protectionLevel:")) { var plraw = line.Substring(20); currentPerm.ProtectionLevels.AddRange(plraw.Split('|')); } } if (currentPerm != null && currentGroup != null) { currentGroup.Permissions.Add(currentPerm); } if (currentGroup != null) { results.Add(currentGroup); } return(results); }