2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 namespace Tizen.System
22 /// Enumeration for the battery levels.
24 /// <since_tizen> 3 </since_tizen>
25 public enum BatteryLevelStatus
28 /// The battery goes empty.
29 /// Prepare for the safe termination of the application,
30 /// because the device starts a shutdown process soon
31 /// after entering this level.
33 /// <since_tizen> 3 </since_tizen>
36 /// The battery charge is at a critical state.
37 /// You may have to stop using the multimedia features,
38 /// because they are not guaranteed to work correctly
39 /// with this battery status.
41 /// <since_tizen> 3 </since_tizen>
44 /// The battery has little charge left.
46 /// <since_tizen> 3 </since_tizen>
49 /// The battery status is not to be careful.
51 /// <since_tizen> 3 </since_tizen>
54 /// The battery status is fully charged.
55 /// It means no more charge.
57 /// <since_tizen> 3 </since_tizen>
62 /// The Battery class provides the properties and events for the device battery.
65 /// The Battery API provides the way to get the current battery capacity value (Percent),
66 /// the battery state, and the charging state. It also provides the events for an application
67 /// to receive the battery status change events from the device.
68 /// To receive the battery event, the application should register with the respective EventHandler.
71 /// Console.WriteLine("battery Charging state is: {0}", Tizen.System.Battery.IsCharging);
72 /// Console.WriteLine("battery Percent is: {0}", Tizen.System.Battery.Percent);
74 public static class Battery
76 private static readonly object s_lock = new object();
78 /// Gets the battery charge percentage.
80 /// <since_tizen> 3 </since_tizen>
81 /// <value>It returns an integer value from 0 to 100 that indicates the remaining
82 /// battery charge as a percentage of the maximum level.</value>
83 public static int Percent
88 DeviceError res = (DeviceError)Interop.Device.DeviceBatteryGetPercent(out percent);
89 if (res != DeviceError.None)
91 Log.Warn(DeviceExceptionFactory.LogTag, "unable to get battery percentage.");
97 /// Gets the current battery level.
99 /// <since_tizen> 3 </since_tizen>
100 public static BatteryLevelStatus Level
105 DeviceError res = (DeviceError)Interop.Device.DeviceBatteryGetLevelStatus(out level);
106 if (res != DeviceError.None)
108 Log.Warn(DeviceExceptionFactory.LogTag, "unable to get battery status.");
110 return (BatteryLevelStatus)level;
114 /// Gets the current charging state.
116 /// <since_tizen> 3 </since_tizen>
117 public static bool IsCharging
122 DeviceError res = (DeviceError)Interop.Device.DeviceBatteryIsCharging(out charging);
123 if (res != DeviceError.None)
125 Log.Warn(DeviceExceptionFactory.LogTag, "unable to get battery charging state.");
131 private static EventHandler<BatteryPercentChangedEventArgs> s_capacityChanged;
133 /// CapacityChanged is triggered when the battery charge percentage is changed.
135 /// <since_tizen> 3 </since_tizen>
136 /// <param name="sender"></param>
137 /// <param name="e">BatteryCapacityChangedEventArgs is an object that contains the changed battery capacity (Percent).</param>
139 /// public static async Task BatteryEventHandler()
141 /// EventHandler<BatteryPercentChangedEventArgs> handler = null;
142 /// handler = (object sender, BatteryChargingStateChangedEventArgs args) =>
144 /// Console.WriteLine("battery Percent is: {0}", args.Percent);
146 /// Battery.PercentChanged += handler;
147 /// await Task.Delay(20000);
150 public static event EventHandler<BatteryPercentChangedEventArgs> PercentChanged
156 if (s_capacityChanged == null)
158 EventListenerStart(EventType.BatteryPercent);
160 s_capacityChanged += value;
167 s_capacityChanged -= value;
168 if (s_capacityChanged == null)
170 EventListenerStop(EventType.BatteryPercent);
176 private static event EventHandler<BatteryLevelChangedEventArgs> s_levelChanged;
179 /// LevelChanged is triggered when the battery level is changed.
181 /// <since_tizen> 3 </since_tizen>
182 /// <param name="sender"></param>
183 /// <param name="e">BatteryLevelChangedEventArgs is an object that contains the changed battery level.</param>
185 /// public static async Task BatteryEventHandler()
187 /// EventHandler<BatteryLevelChangedEventArgs> handler = null;
188 /// handler = (object sender, BatteryLevelChangedEventArgs args) =>
190 /// Console.WriteLine("battery Level is: {0}", args.Level);
192 /// Battery.LevelChanged += handler;
193 /// await Task.Delay(20000);
196 public static event EventHandler<BatteryLevelChangedEventArgs> LevelChanged
202 if (s_levelChanged == null)
204 EventListenerStart(EventType.BatteryLevel);
206 s_levelChanged += value;
213 s_levelChanged -= value;
214 if (s_levelChanged == null)
216 EventListenerStop(EventType.BatteryLevel);
222 private static event EventHandler<BatteryChargingStateChangedEventArgs> s_chargingStateChanged;
224 /// ChargingStatusChanged is triggered when the battery charging status is changed.
225 /// This event is triggered when the charger is connected/disconnected.
227 /// <since_tizen> 3 </since_tizen>
228 /// <param name="sender"></param>
229 /// <param name="e">BatteryChargingStateChangedEventArgs is an object that contains the changed battery charging state.</param>
231 /// public static async Task BatteryEventHandler()
233 /// EventHandler<BatteryChargingStateChangedEventArgs> handler = null;
234 /// handler = (object sender, BatteryChargingStateChangedEventArgs args) =>
236 /// Console.WriteLine("battery Level is: {0}", args.IsCharging);
238 /// Battery.ChargingStateChanged += handler;
239 /// await Task.Delay(20000);
242 public static event EventHandler<BatteryChargingStateChangedEventArgs> ChargingStateChanged
248 if (s_chargingStateChanged == null)
250 EventListenerStart(EventType.BatteryCharging);
252 s_chargingStateChanged += value;
259 s_chargingStateChanged -= value;
260 if (s_chargingStateChanged == null)
262 EventListenerStop(EventType.BatteryCharging);
268 private static Interop.Device.deviceCallback s_cpacityHandler;
269 private static Interop.Device.deviceCallback s_levelHandler;
270 private static Interop.Device.deviceCallback s_chargingHandler;
272 private static void EventListenerStart(EventType eventType)
276 case EventType.BatteryPercent:
277 s_cpacityHandler = (int type, IntPtr value, IntPtr data) =>
279 int val = value.ToInt32();
280 BatteryPercentChangedEventArgs e = new BatteryPercentChangedEventArgs()
284 s_capacityChanged?.Invoke(null, e);
288 Interop.Device.DeviceAddCallback(eventType, s_cpacityHandler, IntPtr.Zero);
291 case EventType.BatteryLevel:
292 s_levelHandler = (int type, IntPtr value, IntPtr data) =>
294 int val = value.ToInt32();
295 BatteryLevelChangedEventArgs e = new BatteryLevelChangedEventArgs()
297 Level = (BatteryLevelStatus)val
299 s_levelChanged?.Invoke(null, e);
303 Interop.Device.DeviceAddCallback(eventType, s_levelHandler, IntPtr.Zero);
306 case EventType.BatteryCharging:
307 s_chargingHandler = (int type, IntPtr value, IntPtr data) =>
309 bool val = (value.ToInt32() == 1);
310 BatteryChargingStateChangedEventArgs e = new BatteryChargingStateChangedEventArgs()
314 s_chargingStateChanged?.Invoke(null, e);
317 Interop.Device.DeviceAddCallback(eventType, s_chargingHandler, IntPtr.Zero);
322 private static void EventListenerStop(EventType eventType)
326 case EventType.BatteryPercent:
327 Interop.Device.DeviceRemoveCallback(eventType, s_cpacityHandler);
330 case EventType.BatteryLevel:
331 Interop.Device.DeviceRemoveCallback(eventType, s_levelHandler);
334 case EventType.BatteryCharging:
335 Interop.Device.DeviceRemoveCallback(eventType, s_chargingHandler);