/// <summary> /// Gets the battery level. /// </summary> /// <param name="freshness">The freshness.</param> /// <returns></returns> public BatteryInfo GetBatteryInfo(long freshness) { if (this.lastBatteryInfo != null && this.lastBatteryCheckTime > DateTime.Now.AddMilliseconds(-freshness)) { return(this.lastBatteryInfo); } var receiver = new BatteryReceiver(); this.ExecuteShellCommand("dumpsys battery", receiver, BATTERY_TIMEOUT); this.lastBatteryInfo = receiver.BatteryInfo; this.lastBatteryCheckTime = DateTime.Now; return(this.lastBatteryInfo); }
/// <summary> /// Gets the battery level. /// </summary> /// <param name="freshness">The freshness.</param> /// <returns></returns> public BatteryInfo GetBatteryInfo(long freshness) { if (this.lastBatteryInfo != null && this.lastBatteryCheckTime > DateTime.Now.AddMilliseconds(-freshness)) { return this.lastBatteryInfo; } var receiver = new BatteryReceiver(); this.ExecuteShellCommand("dumpsys battery", receiver, BATTERY_TIMEOUT); this.lastBatteryInfo = receiver.BatteryInfo; this.lastBatteryCheckTime = DateTime.Now; return this.lastBatteryInfo; }
/// <summary> /// Processes the new lines. /// </summary> /// <param name="lines">The lines.</param> protected override void ProcessNewLines(IEnumerable<string> lines) { this.BatteryInfo = new BatteryInfo(); foreach (var line in lines) { var match = line.Match(BATTERY_LEVEL, REOPTIONS); if (match.Success) { try { this.BatteryInfo.Level = int.Parse(match.Groups[1].Value); } catch (FormatException) { Log.w(TAG, string.Format("Failed to parse {0} as an integer", match.Groups[1].Value)); } } match = line.Match(SCALE, REOPTIONS); if (match.Success) { try { this.BatteryInfo.Scale = int.Parse(match.Groups[1].Value); } catch (FormatException) { Log.w(TAG, string.Format("Failed to parse {0} as an integer", match.Groups[1].Value)); } } match = line.Match(AC_POWER, REOPTIONS); if (match.Success) { this.BatteryInfo.ACPowered = string.Compare(match.Groups[1].Value, bool.TrueString, true) == 0; } match = line.Match(USB_POWER, REOPTIONS); if (match.Success) { this.BatteryInfo.UsbPowered = string.Compare(match.Groups[1].Value, bool.TrueString, true) == 0; } match = line.Match(PRESENT, REOPTIONS); if (match.Success) { this.BatteryInfo.Present = string.Compare(match.Groups[1].Value, bool.TrueString, true) == 0; } match = line.Match(STATUS, REOPTIONS); if (match.Success) { try { var i = int.Parse(match.Groups[1].Value); this.BatteryInfo.Status = (BatteryInfo.StatusTypes)i; } catch (FormatException) { Log.w(TAG, string.Format("Failed to parse {0} as an integer", match.Groups[1].Value)); } } match = line.Match(HEALTH, REOPTIONS); if (match.Success) { try { var i = int.Parse(match.Groups[1].Value); this.BatteryInfo.Health = (BatteryInfo.HealthTypes)i; } catch (FormatException) { Log.w(TAG, string.Format("Failed to parse {0} as an integer", match.Groups[1].Value)); } } match = line.Match(VOLTAGE, REOPTIONS); if (match.Success) { try { this.BatteryInfo.Voltage = int.Parse(match.Groups[1].Value); } catch (FormatException) { Log.w(TAG, string.Format("Failed to parse {0} as an integer", match.Groups[1].Value)); } } match = line.Match(TYPE, REOPTIONS); if (match.Success) { this.BatteryInfo.Type = match.Groups[1].Value; } match = line.Match(TEMP, REOPTIONS); if (match.Success) { try { this.BatteryInfo.Temperature = int.Parse(match.Groups[1].Value); } catch (FormatException) { Log.w(TAG, string.Format("Failed to parse {0} as an integer", match.Groups[1].Value)); } } } }
/// <summary> /// Processes the new lines. /// </summary> /// <param name="lines">The lines.</param> protected override void ProcessNewLines(IEnumerable <string> lines) { this.BatteryInfo = new BatteryInfo(); foreach (var line in lines) { var match = line.Match(BATTERY_LEVEL, REOPTIONS); if (match.Success) { try { this.BatteryInfo.Level = int.Parse(match.Groups[1].Value); } catch (FormatException) { Log.w(TAG, string.Format("Failed to parse {0} as an integer", match.Groups[1].Value)); } } match = line.Match(SCALE, REOPTIONS); if (match.Success) { try { this.BatteryInfo.Scale = int.Parse(match.Groups[1].Value); } catch (FormatException) { Log.w(TAG, string.Format("Failed to parse {0} as an integer", match.Groups[1].Value)); } } match = line.Match(AC_POWER, REOPTIONS); if (match.Success) { this.BatteryInfo.ACPowered = string.Compare(match.Groups[1].Value, bool.TrueString, true) == 0; } match = line.Match(USB_POWER, REOPTIONS); if (match.Success) { this.BatteryInfo.UsbPowered = string.Compare(match.Groups[1].Value, bool.TrueString, true) == 0; } match = line.Match(PRESENT, REOPTIONS); if (match.Success) { this.BatteryInfo.Present = string.Compare(match.Groups[1].Value, bool.TrueString, true) == 0; } match = line.Match(STATUS, REOPTIONS); if (match.Success) { try { var i = int.Parse(match.Groups[1].Value); this.BatteryInfo.Status = (BatteryInfo.StatusTypes)i; } catch (FormatException) { Log.w(TAG, string.Format("Failed to parse {0} as an integer", match.Groups[1].Value)); } } match = line.Match(HEALTH, REOPTIONS); if (match.Success) { try { var i = int.Parse(match.Groups[1].Value); this.BatteryInfo.Health = (BatteryInfo.HealthTypes)i; } catch (FormatException) { Log.w(TAG, string.Format("Failed to parse {0} as an integer", match.Groups[1].Value)); } } match = line.Match(VOLTAGE, REOPTIONS); if (match.Success) { try { this.BatteryInfo.Voltage = int.Parse(match.Groups[1].Value); } catch (FormatException) { Log.w(TAG, string.Format("Failed to parse {0} as an integer", match.Groups[1].Value)); } } match = line.Match(TYPE, REOPTIONS); if (match.Success) { this.BatteryInfo.Type = match.Groups[1].Value; } match = line.Match(TEMP, REOPTIONS); if (match.Success) { try { this.BatteryInfo.Temperature = int.Parse(match.Groups[1].Value); } catch (FormatException) { Log.w(TAG, string.Format("Failed to parse {0} as an integer", match.Groups[1].Value)); } } } }