Example #1
0
        public BatteryInfo GetBatteryInfo(long freshness)
        {
            if (this.lastBatteryInfo != null && this.lastBatteryCheckTime > (DateTime.Now.AddMilliseconds(-freshness)))
            {
                return(this.lastBatteryInfo);
            }
            BatteryReceiver receiver = new BatteryReceiver();

            ExecuteShellCommand("dumpsys battery", receiver, BATTERY_TIMEOUT);
            this.lastBatteryInfo      = receiver.BatteryInfo;
            this.lastBatteryCheckTime = DateTime.Now;
            return(this.lastBatteryInfo);
        }
Example #2
0
        /// <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 = Util.equalsIgnoreCase(match.Groups[1].Value, bool.TrueString);
                }

                match = line.Match(USB_POWER, REOPTIONS);
                if (match.Success)
                {
                    BatteryInfo.UsbPowered = Util.equalsIgnoreCase(match.Groups[1].Value, bool.TrueString);
                }

                match = line.Match(PRESENT, REOPTIONS);
                if (match.Success)
                {
                    BatteryInfo.Present = Util.equalsIgnoreCase(match.Groups[1].Value, bool.TrueString);
                }

                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));
                    }
                }
            }
        }
Example #3
0
 public BatteryInfo GetBatteryInfo(long freshness)
 {
     if (this.lastBatteryInfo != null && this.lastBatteryCheckTime > (DateTime.Now.AddMilliseconds(-freshness)))
         {
         return this.lastBatteryInfo;
         }
     BatteryReceiver receiver = new BatteryReceiver();
     ExecuteShellCommand("dumpsys battery", receiver, BATTERY_TIMEOUT);
     this.lastBatteryInfo = receiver.BatteryInfo;
     this.lastBatteryCheckTime = DateTime.Now;
     return this.lastBatteryInfo;
 }
Example #4
0
        /// <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 = Util.equalsIgnoreCase ( match.Groups[1].Value, bool.TrueString);
                }

                match = line.Match ( USB_POWER, REOPTIONS );
                if ( match.Success ) {
                    BatteryInfo.UsbPowered = Util.equalsIgnoreCase ( match.Groups[1].Value, bool.TrueString);
                }

                match = line.Match ( PRESENT, REOPTIONS );
                if ( match.Success ) {
                    BatteryInfo.Present = Util.equalsIgnoreCase ( match.Groups[1].Value, bool.TrueString);
                }

                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 ) );
                    }
                }
            }
        }