/// <summary> /// </summary> public Dictionary <string, ListingType> GetFilesAndDirectories(string location) { if (location == null || string.IsNullOrEmpty(location) || Regex.IsMatch(location, @"\s")) { throw new ArgumentException("rootDir must not be null or empty!"); } Dictionary <string, ListingType> filesAndDirs = new Dictionary <string, ListingType>(); AdbCommand cmd = null; if (device.BusyBox.IsInstalled) { cmd = AdbCmd.FormAdbShellCommand(device, true, "busybox", "ls", "-a", "-p", "-l", location); } else { cmd = AdbCmd.FormAdbShellCommand(device, true, "ls", "-a", "-p", "-l", location); } using (StringReader reader = new StringReader(AdbCmd.ExecuteAdbCommand(cmd))) { string line = null; while (reader.Peek() != -1) { line = reader.ReadLine(); if (!string.IsNullOrEmpty(line) && !Regex.IsMatch(line, @"\s")) { filesAndDirs.Add(line, line.EndsWith("/") ? ListingType.DIRECTORY : ListingType.FILE); } } } return(filesAndDirs); }
/// <summary> /// Executes an <see cref="AdbCommand"/> on the running Adb Server /// </summary> /// <remarks>This should be used if you do not want the output of the command returned. Good for quick abd shell commands</remarks> /// <param name="command">Instance of <see cref="AdbCommand"/></param> /// <returns>Output of <paramref name="command"/> run on server</returns> public static void ExecuteAdbCommandNoReturn(AdbCommand command) { lock (_lock) { Command.RunProcessNoReturn(XiaomiController.Instance.ResourceDirectory + ADB_EXE, command.Command, command.Timeout); } }
/// <summary> /// Executes an <see cref="AdbCommand"/> on the running Adb Server /// </summary> /// <param name="command">Instance of <see cref="AdbCommand"/></param> /// <returns>Exit code of the process</returns> public static int ExecuteAdbCommandReturnExitCode(AdbCommand command) { int result = -1; lock (_lock) { result = Command.RunProcessReturnExitCode(XiaomiController.Instance.ResourceDirectory + ADB_EXE, command.Command, command.Timeout); } return(result); }
/// <summary> /// Executes an <see cref="AdbCommand"/> on the running Adb Server /// </summary> /// <remarks>This should be used if you want the output of the command returned</remarks> /// <param name="command">Instance of <see cref="AdbCommand"/></param> /// <param name="forceRegular">Forces Output of stdout, not stderror if any</param> /// <returns>Output of <paramref name="command"/> run on server</returns> public static string ExecuteAdbCommand(AdbCommand command, bool forceRegular = false) { string result = ""; lock (_lock) { result = Command.RunProcessReturnOutput(XiaomiController.Instance.ResourceDirectory + ADB_EXE, command.Command, forceRegular, command.Timeout); } return(result); }
private void UpdateMountPoints() { if (device.State != DeviceState.ONLINE) { systemMount = new MountInfo(null, null, MountType.NONE); return; } AdbCommand adbCmd = AdbCmd.FormAdbShellCommand(device, false, "mount"); using (StringReader r = new StringReader(AdbCmd.ExecuteAdbCommand(adbCmd))) { string line; string[] splitLine; string dir, mount; MountType type; while (r.Peek() != -1) { line = r.ReadLine(); splitLine = line.Split(' '); try { if (line.Contains(" on /system ")) { dir = splitLine[2]; mount = splitLine[0]; type = (MountType)Enum.Parse(typeof(MountType), splitLine[5].Substring(1, 2).ToUpper()); systemMount = new MountInfo(dir, mount, type); return; } if (line.Contains(" /system ")) { dir = splitLine[1]; mount = splitLine[0]; type = (MountType)Enum.Parse(typeof(MountType), splitLine[3].Substring(0, 2).ToUpper()); systemMount = new MountInfo(dir, mount, type); return; } } catch { dir = "/system"; mount = "ERROR"; type = MountType.NONE; systemMount = new MountInfo(dir, mount, type); } } } }
/// <summary> /// Updates the instance of busybox /// </summary> /// <remarks>Generally called only if busybox may have changed on the device</remarks> public void Update() { commands.Clear(); if (!device.HasRoot || device.State != DeviceState.ONLINE) { SetNoBusybox(); return; } AdbCommand adbCmd = AdbCmd.FormAdbShellCommand(device, false, EXECUTABLE); using (StringReader s = new StringReader(AdbCmd.ExecuteAdbCommand(adbCmd))) { string check = s.ReadLine(); if (check.Contains(string.Format("{0}: not found", EXECUTABLE))) { SetNoBusybox(); return; } isInstalled = true; version = check.Split(' ')[1].Substring(1); while (s.Peek() != -1 && s.ReadLine() != "Currently defined functions:") { } string[] cmds = s.ReadToEnd().Replace(" ", "").Replace("\r\r\n\t", "").Trim('\t', '\r', '\n').Split(','); if (cmds.Length.Equals(0)) { SetNoBusybox(); } else { foreach (string cmd in cmds) { commands.Add(cmd); } } } }
//void PushFile(); //void PullFile(); /// <summary> /// Mounts connected Android device's file system as specified /// </summary> /// <param name="type">The desired <see cref="MountType"/> (RW or RO)</param> /// <returns>True if remount is successful, False if remount is unsuccessful</returns> /// <example>The following example shows how you can mount the file system as Read-Writable or Read-Only /// <code> /// // This example demonstrates mounting the Android device's file system as Read-Writable /// using System; /// using Mrivai.Pelitabangsa; /// /// class Program /// { /// static void Main(string[] args) /// { /// AndroidController android = AndroidController.Instance; /// Device device; /// /// Console.WriteLine("Waiting For Device..."); /// android.WaitForDevice(); //This will wait until a device is connected to the computer /// device = android.ConnectedDevices[0]; //Sets device to the first Device in the collection /// /// Console.WriteLine("Connected Device - {0}", device.SerialNumber); /// /// Console.WriteLine("Mounting System as RW..."); /// Console.WriteLine("Mount success? - {0}", device.RemountSystem(MountType.RW)); /// } /// } /// /// // The example displays the following output (if mounting is successful): /// // Waiting For Device... /// // Connected Device - {serial # here} /// // Mounting System as RW... /// // Mount success? - true /// </code> /// </example> public bool RemountSystem(MountType type) { if (!device.HasRoot) { return(false); } AdbCommand adbCmd = AdbCmd.FormAdbShellCommand(device, true, "mount", string.Format("-o remount,{0} -t yaffs2 {1} /system", type.ToString().ToLower(), systemMount.Block)); AdbCmd.ExecuteAdbCommandNoReturn(adbCmd); UpdateMountPoints(); if (systemMount.MountType == type) { return(true); } return(false); }
/// <summary> /// Gets a <see cref="ListingType"/> indicating is the requested location is a File or Directory /// </summary> /// <param name="location">Path of requested location on device</param> /// <returns>See <see cref="ListingType"/></returns> /// <remarks><para>Requires a device containing BusyBox for now, returns ListingType.ERROR if not installed.</para> /// <para>Returns ListingType.NONE if file/Directory does not exist</para></remarks> public ListingType FileOrDirectory(string location) { if (!device.BusyBox.IsInstalled) { return(ListingType.ERROR); } AdbCommand isFile = AdbCmd.FormAdbShellCommand(device, false, string.Format(IS_FILE, location)); AdbCommand isDir = AdbCmd.FormAdbShellCommand(device, false, string.Format(IS_DIRECTORY, location)); if (AdbCmd.ExecuteAdbCommand(isFile).Contains("1")) { return(ListingType.FILE); } else if (AdbCmd.ExecuteAdbCommand(isDir).Contains("1")) { return(ListingType.DIRECTORY); } return(ListingType.NONE); }
/// <summary> /// Pulls a full directory recursively from the device /// </summary> /// <param name="location">Path to folder to pull from device</param> /// <param name="destination">Directory on local computer to pull file to</param> /// <param name="timeout">The timeout for this operation in milliseconds (Default = -1)</param> /// <returns>True if directory is pulled, false if pull failed</returns> public bool PullDirectory(string location, string destination, int timeout = Command.DEFAULT_TIMEOUT) { AdbCommand adbCmd = AdbCmd.FormAdbShellCommand(this, "pull", "\"" + (location.EndsWith("/") ? location : location + "/") + "\"", "\"" + destination + "\""); return(AdbCmd.ExecuteAdbCommandReturnExitCode(adbCmd.WithTimeout(timeout)) == 0); }
/// <summary> /// Pushes a file to the device /// </summary> /// <param name="filePath">The path to the file on the computer you want to push</param> /// <param name="destinationFilePath">The desired full path of the file after pushing to the device (including file name and extension)</param> /// <param name="timeout">The timeout for this operation in milliseconds (Default = -1)</param> /// <returns>If the push was successful</returns> public bool PushFile(string filePath, string destinationFilePath, int timeout = Command.DEFAULT_TIMEOUT) { AdbCommand adbCmd = AdbCmd.FormAdbShellCommand(this, "push", "\"" + filePath + "\"", "\"" + destinationFilePath + "\""); return(AdbCmd.ExecuteAdbCommandReturnExitCode(adbCmd.WithTimeout(timeout)) == 0); }
/// <summary> /// Pulls a file from the device /// </summary> /// <param name="fileOnDevice">Path to file to pull from device</param> /// <param name="destinationDirectory">Directory on local computer to pull file to</param> /// /// <param name="timeout">The timeout for this operation in milliseconds (Default = -1)</param> /// <returns>True if file is pulled, false if pull failed</returns> public bool PullFile(string fileOnDevice, string destinationDirectory, int timeout = Command.DEFAULT_TIMEOUT) { AdbCommand adbCmd = AdbCmd.FormAdbShellCommand(this, "pull", "\"" + fileOnDevice + "\"", "\"" + destinationDirectory + "\""); return(AdbCmd.ExecuteAdbCommandReturnExitCode(adbCmd.WithTimeout(timeout)) == 0); }
private void Update() { if (device.State != DeviceState.ONLINE) { acPower = false; dump = null; health = -1; level = -1; present = false; scale = -1; status = -1; technology = null; temperature = -1; usbPower = false; voltage = -1; wirelessPower = false; outString = "Device Not Online"; return; } AdbCommand adbCmd = AdbCmd.FormAdbShellCommand(device, false, "dumpsys", "battery"); dump = AdbCmd.ExecuteAdbCommand(adbCmd); using (StringReader r = new StringReader(dump)) { string line; while (true) { line = r.ReadLine(); if (!line.Contains("Current Battery Service state")) { continue; } else { dump = line + r.ReadToEnd(); break; } } } using (StringReader r = new StringReader(dump)) { string line = ""; while (r.Peek() != -1) { line = r.ReadLine(); if (line == "") { continue; } else if (line.Contains("AC ")) { bool.TryParse(line.Substring(14), out acPower); } else if (line.Contains("USB")) { bool.TryParse(line.Substring(15), out usbPower); } else if (line.Contains("Wireless")) { bool.TryParse(line.Substring(20), out wirelessPower); } else if (line.Contains("status")) { int.TryParse(line.Substring(10), out status); } else if (line.Contains("health")) { int.TryParse(line.Substring(10), out health); } else if (line.Contains("present")) { bool.TryParse(line.Substring(11), out present); } else if (line.Contains("level")) { int.TryParse(line.Substring(9), out level); } else if (line.Contains("scale")) { int.TryParse(line.Substring(9), out scale); } else if (line.Contains("voltage")) { int.TryParse(line.Substring(10), out voltage); } else if (line.Contains("temp")) { int.TryParse(line.Substring(15), out temperature); } else if (line.Contains("tech")) { this.technology = line.Substring(14); } } } outString = dump.Replace("Service state", "State For Device " + device.SerialNumber); }