6 /// Enumeration for the battery level.
8 public enum BatteryLevelStatus
11 /// The battery goes empty.
12 /// Prepare for the safe termination of the application,
13 /// because the device starts a shutdown process soon
14 /// after entering this level.
18 /// The battery charge is at a critical state.
19 /// You may have to stop using multimedia features,
20 /// because they are not guaranteed to work correctly
21 /// at this battery status.
25 /// The battery has little charge left.
29 /// The battery status is not to be careful.
33 /// The battery status is full.
39 /// The Battery API provides functions to get information about the battery.
42 /// The Battery API provides the way to get the current battery capacity value,
43 /// battery state and charging state. It also supports the API for an application
44 /// to receive the battery events from the system. To receive the battery event
45 /// it should be described by the callback function.
47 public static class Battery
49 private static readonly object s_lock = new object();
51 /// Gets the battery charge percentage.
53 /// <value>It returns an integer value from 0 to 100 that indicates remaining
54 /// battery charge as a percentage of the maximum level.</value>
55 public static int Capacity
60 DeviceError res = (DeviceError) Interop.Device.DeviceBatteryGetPercent(out percent);
61 if (res != DeviceError.None)
63 Log.Warn(DeviceExceptionFactory.LogTag, "unable to get battery percentage.");
69 /// Gets the battery level.
71 public static BatteryLevelStatus Level
76 DeviceError res = (DeviceError) Interop.Device.DeviceBatteryGetLevelStatus(out level);
77 if (res != DeviceError.None)
79 Log.Warn(DeviceExceptionFactory.LogTag, "unable to get battery status.");
81 return (BatteryLevelStatus)level;
85 /// Gets the charging state.
87 public static bool IsCharging
92 DeviceError res = (DeviceError) Interop.Device.DeviceBatteryIsCharging(out charging);
93 if (res != DeviceError.None)
95 Log.Warn(DeviceExceptionFactory.LogTag, "unable to get battery charging state.");
101 private static EventHandler<BatteryCapacityChangedEventArgs> s_capacityChanged;
103 /// CapacityChanged is triggered when the battery charge percentage is changed
105 /// <param name="sender"></param>
106 /// <param name="e">A BatteryCapacityChangedEventArgs object that contains changed battery capacity</param>
108 public static event EventHandler<BatteryCapacityChangedEventArgs> CapacityChanged
114 if (s_capacityChanged == null)
116 EventListenerStart(EventType.BatteryCapacity);
118 s_capacityChanged += value;
125 s_capacityChanged -= value;
126 if (s_capacityChanged == null)
128 EventListenerStop(EventType.BatteryCapacity);
134 private static event EventHandler<BatteryLevelChangedEventArgs> s_levelChanged;
137 /// LevelChanged is triggered when the battery level is changed
139 /// <param name="sender"></param>
140 /// <param name="e">A BatteryLevelChangedEventArgs object that contains changed battery level </param>
141 public static event EventHandler<BatteryLevelChangedEventArgs> LevelChanged
147 if (s_levelChanged == null)
149 EventListenerStart(EventType.BatteryLevel);
151 s_levelChanged += value;
158 s_levelChanged -= value;
159 if (s_levelChanged == null)
161 EventListenerStop(EventType.BatteryLevel);
167 private static event EventHandler<BatteryChargingStateChangedEventArgs> s_chargingStateChanged;
169 /// ChargingStatusChanged is triggered when the battery charging status is changed
171 /// <param name="sender"></param>
172 /// <param name="e">A BatteryChargingStateChangedEventArgs object that contains changed battery charging state</param>
174 public static event EventHandler<BatteryChargingStateChangedEventArgs> ChargingStatusChanged
180 if (s_chargingStateChanged == null)
182 EventListenerStart(EventType.BatteryCharging);
184 s_chargingStateChanged += value;
191 s_chargingStateChanged -= value;
192 if (s_chargingStateChanged == null)
194 EventListenerStop(EventType.BatteryCharging);
200 private static Interop.Device.deviceCallback s_cpacityHandler;
201 private static Interop.Device.deviceCallback s_levelHandler;
202 private static Interop.Device.deviceCallback s_chargingHandler;
204 private static void EventListenerStart(EventType eventType)
208 case EventType.BatteryCapacity:
209 s_cpacityHandler = (int type, IntPtr value, IntPtr data) =>
211 int val = value.ToInt32();
212 BatteryCapacityChangedEventArgs e = new BatteryCapacityChangedEventArgs()
216 s_capacityChanged?.Invoke(null, e);
220 Interop.Device.DeviceAddCallback(eventType, s_cpacityHandler, IntPtr.Zero);
223 case EventType.BatteryLevel:
224 s_levelHandler = (int type, IntPtr value, IntPtr data) =>
226 int val = value.ToInt32();
227 BatteryLevelChangedEventArgs e = new BatteryLevelChangedEventArgs()
229 Level = (BatteryLevelStatus)val
231 s_levelChanged?.Invoke(null, e);
235 Interop.Device.DeviceAddCallback(eventType, s_levelHandler, IntPtr.Zero);
238 case EventType.BatteryCharging:
239 s_chargingHandler = (int type, IntPtr value, IntPtr data) =>
241 bool val = (value.ToInt32() == 1);
242 BatteryChargingStateChangedEventArgs e = new BatteryChargingStateChangedEventArgs()
246 s_chargingStateChanged?.Invoke(null, e);
249 Interop.Device.DeviceAddCallback(eventType, s_chargingHandler, IntPtr.Zero);
254 private static void EventListenerStop(EventType eventType)
258 case EventType.BatteryCapacity:
259 Interop.Device.DeviceRemoveCallback(eventType, s_cpacityHandler);
262 case EventType.BatteryLevel:
263 Interop.Device.DeviceRemoveCallback(eventType, s_levelHandler);
266 case EventType.BatteryCharging:
267 Interop.Device.DeviceRemoveCallback(eventType, s_chargingHandler);