/// <summary> /// Sets a build property value /// </summary> /// <remarks>If <paramref name="key"/> does not exist or device does not have root, returns false, and does not set any values</remarks> /// <param name="key">Build property key to set</param> /// <param name="newValue">Value you wish to set <paramref name="key"/> to</param> /// <returns>True if new value set, false if not</returns> public bool SetProp(string key, string newValue) { string before; if (!this._prop.TryGetValue(key, out before)) { return(false); } if (!this._device.HasRoot) { return(false); } var adbCmd = Adb.FormAdbShellCommand(this._device, true, "setprop", key, newValue); Adb.ExecuteAdbCommandNoReturn(adbCmd); Update(); string after; if (!this._prop.TryGetValue(key, out after)) { return(false); } return(newValue == after); }
private void GetSuData() { if (this._device.State != DeviceState.Online) { this._version = null; this._exists = false; return; } var adbCmd = Adb.FormAdbShellCommand(this._device, false, "su", "-v"); using (var r = new StringReader(Adb.ExecuteAdbCommand(adbCmd))) { var line = r.ReadLine(); if (line.Contains("not found") || line.Contains("permission denied")) { this._version = "-1"; this._exists = false; } else { this._version = line; this._exists = true; } } }
private void Update() { try { this._prop.Clear(); if (this._device.State != DeviceState.Online) { return; } var adbCmd = Adb.FormAdbShellCommand(this._device, false, "getprop"); var prop = Adb.ExecuteAdbCommand(adbCmd); var lines = prop.Split(new string[] { "\r\n\r\n" }, StringSplitOptions.RemoveEmptyEntries); for (var i = 0; i < lines.Length; i++) { var entry = lines[i].Split(new string[] { "[", "]: [", "]" }, StringSplitOptions.RemoveEmptyEntries); if (entry.Length == 2) { this._prop.Add(entry[0], entry[1]); } } } catch (Exception ex) { Logger.WriteLog(ex.Message, "Using: getprop in BuildProp.cs", ex.StackTrace); } }
/// <summary> /// Gets a Dictionary<c> string / ListingType></c> containing all the files and folders in the directory added as a parameter. /// </summary> /// The directory you'd like to list the files and folders from. /// E.G.: /system/bin/ /// <returns>See <see cref="Dictionary"/></returns> 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!"); } var filesAndDirs = new Dictionary <string, ListingType>(); AdbCommand cmd = null; if (_device.BusyBox.IsInstalled) { cmd = Adb.FormAdbShellCommand(_device, true, "busybox", "ls", "-a", "-p", "-l", location); } else { cmd = Adb.FormAdbShellCommand(_device, true, "ls", "-a", "-p", "-l", location); } using (var reader = new StringReader(Adb.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); }
private void UpdateMountPoints() { if (this._device.State != DeviceState.Online) { this._systemMount = new MountInfo(null, null, MountType.None); return; } var adbCmd = Adb.FormAdbShellCommand(this._device, false, "mount"); using (var r = new StringReader(Adb.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()); this._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()); this._systemMount = new MountInfo(dir, mount, type); return; } } catch { dir = "/system"; mount = "ERROR"; type = MountType.None; this._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() { this._commands.Clear(); if (!this._device.HasRoot || this._device.State != DeviceState.Online) { SetNoBusybox(); return; } var adbCmd = Adb.FormAdbShellCommand(this._device, false, Executable); using (var s = new StringReader(Adb.ExecuteAdbCommand(adbCmd))) { var check = s.ReadLine(); if (check.Contains(string.Format("{0}: not found", Executable))) { SetNoBusybox(); return; } this._isInstalled = true; this._version = check.Split(' ')[1].Substring(1); while (s.Peek() != -1 && s.ReadLine() != "Currently defined functions:") { } var cmds = s.ReadToEnd().Replace(" ", "").Replace("\r\r\n\t", "").Trim('\t', '\r', '\n').Split(','); if (cmds.Length.Equals(0)) { SetNoBusybox(); } else { foreach (var cmd in cmds) { this._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 RegawMOD.Android; /// /// 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 (!this._device.HasRoot) { return(false); } var adbCmd = Adb.FormAdbShellCommand(this._device, true, "mount", string.Format("-o remount,{0} -t yaffs2 {1} /system", type.ToString().ToLower(), this._systemMount.Block)); Adb.ExecuteAdbCommandNoReturn(adbCmd); UpdateMountPoints(); if (this._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 (!this._device.BusyBox.IsInstalled) { return(ListingType.Error); } var isFile = Adb.FormAdbShellCommand(this._device, false, string.Format(IsFile, location)); var isDir = Adb.FormAdbShellCommand(this._device, false, string.Format(IsDirectory, location)); if (Adb.ExecuteAdbCommand(isFile).Contains("1")) { return(ListingType.File); } else if (Adb.ExecuteAdbCommand(isDir).Contains("1")) { return(ListingType.Directory); } return(ListingType.None); }
private void Update() { if (this._device.State != DeviceState.Online) { this._acPower = false; this._dump = null; this._health = -1; this._level = -1; this._present = false; this._scale = -1; this._status = -1; this._technology = null; this._temperature = -1; this._usbPower = false; this._voltage = -1; this._wirelessPower = false; this._outString = "Device Not Online"; return; } else { var adbCmd = Adb.FormAdbShellCommand(this._device, false, "dumpsys", "battery"); this._dump = Adb.ExecuteAdbCommand(adbCmd); using (var r = new StringReader(this._dump)) { string line; while (true) { line = r.ReadLine(); if (line != null && !line.Contains("Current Battery Service state")) { continue; } this._dump = line + r.ReadToEnd(); break; } } using (var r = new StringReader(this._dump)) { var line = ""; while (r.Peek() != -1) { line = r.ReadLine(); if (line == "") { continue; } else if (line.Contains("AC ")) { bool.TryParse(line.Substring(14), out this._acPower); } else if (line.Contains("USB")) { bool.TryParse(line.Substring(15), out this._usbPower); } else if (line.Contains("Wireless")) { bool.TryParse(line.Substring(20), out this._wirelessPower); } else if (line.Contains("status")) { int.TryParse(line.Substring(10), out this._status); } else if (line.Contains("health")) { int.TryParse(line.Substring(10), out this._health); } else if (line.Contains("present")) { bool.TryParse(line.Substring(11), out this._present); } else if (line.Contains("level")) { int.TryParse(line.Substring(9), out this._level); } else if (line.Contains("scale")) { int.TryParse(line.Substring(9), out this._scale); } else if (line.Contains("voltage")) { int.TryParse(line.Substring(10), out this._voltage); } else if (line.Contains("temp")) { int.TryParse(line.Substring(15), out this._temperature); } else if (line.Contains("tech")) { this._technology = line.Substring(14); } } } this._outString = this._dump.Replace("Service state", "State For Device " + this._device.SerialNumber); } }