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