Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.System / Device / Battery.cs
1 /*
2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 using System;
18
19 namespace Tizen.System
20 {
21     /// <summary>
22     /// Enumeration for the battery levels.
23     /// </summary>
24     /// <since_tizen> 3 </since_tizen>
25     public enum BatteryLevelStatus
26     {
27         /// <summary>
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.
32         /// </summary>
33         /// <since_tizen> 3 </since_tizen>
34         Empty = 0,
35         /// <summary>
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.
40         /// </summary>
41         /// <since_tizen> 3 </since_tizen>
42         Critical,
43         /// <summary>
44         /// The battery has little charge left.
45         /// </summary>
46         /// <since_tizen> 3 </since_tizen>
47         Low,
48         /// <summary>
49         /// The battery status is not to be careful.
50         /// </summary>
51         /// <since_tizen> 3 </since_tizen>
52         High,
53         /// <summary>
54         /// The battery status is full.
55         /// </summary>
56         /// <since_tizen> 3 </since_tizen>
57         Full
58     }
59
60     /// <summary>
61     /// The Battery class provides the properties and events for the device battery.
62     /// </summary>
63     /// <remarks>
64     /// The Battery API provides the way to get the current battery capacity value (Percent),
65     /// the battery state, and the charging state. It also provides the events for an application
66     /// to receive the battery status change events from the device.
67     /// To receive the battery event, the application should register with the respective EventHandler.
68     /// </remarks>
69     /// <code>
70     ///     Console.WriteLine("battery Charging state is: {0}", Tizen.System.Battery.IsCharging);
71     ///     Console.WriteLine("battery Percent is: {0}", Tizen.System.Battery.Percent);
72     /// </code>
73     public static class Battery
74     {
75         private static readonly object s_lock = new object();
76         /// <summary>
77         /// Gets the battery charge percentage.
78         /// </summary>
79         /// <since_tizen> 3 </since_tizen>
80         /// <value>It returns an integer value from 0 to 100 that indicates the remaining
81         /// battery charge as a percentage of the maximum level.</value>
82         public static int Percent
83         {
84             get
85             {
86                 int percent = 0;
87                 DeviceError res = (DeviceError)Interop.Device.DeviceBatteryGetPercent(out percent);
88                 if (res != DeviceError.None)
89                 {
90                     Log.Warn(DeviceExceptionFactory.LogTag, "unable to get battery percentage.");
91                 }
92                 return percent;
93             }
94         }
95         /// <summary>
96         /// Gets the current battery level.
97         /// </summary>
98         /// <since_tizen> 3 </since_tizen>
99         public static BatteryLevelStatus Level
100         {
101             get
102             {
103                 int level = 0;
104                 DeviceError res = (DeviceError)Interop.Device.DeviceBatteryGetLevelStatus(out level);
105                 if (res != DeviceError.None)
106                 {
107                     Log.Warn(DeviceExceptionFactory.LogTag, "unable to get battery status.");
108                 }
109                 return (BatteryLevelStatus)level;
110             }
111         }
112         /// <summary>
113         /// Gets the current charging state.
114         /// </summary>
115         /// <since_tizen> 3 </since_tizen>
116         public static bool IsCharging
117         {
118             get
119             {
120                 bool charging;
121                 DeviceError res = (DeviceError)Interop.Device.DeviceBatteryIsCharging(out charging);
122                 if (res != DeviceError.None)
123                 {
124                     Log.Warn(DeviceExceptionFactory.LogTag, "unable to get battery charging state.");
125                 }
126                 return charging;
127             }
128         }
129
130         private static EventHandler<BatteryPercentChangedEventArgs> s_capacityChanged;
131         /// <summary>
132         /// CapacityChanged is triggered when the battery charge percentage is changed.
133         /// </summary>
134         /// <since_tizen> 3 </since_tizen>
135         /// <param name="sender"></param>
136         /// <param name="e">BatteryCapacityChangedEventArgs is an object that contains the changed battery capacity (Percent).</param>
137         /// <code>
138         /// public static async Task BatteryEventHandler()
139         /// {
140         ///     EventHandler<BatteryPercentChangedEventArgs> handler = null;
141         ///     handler = (object sender, BatteryChargingStateChangedEventArgs args) =>
142         ///     {
143         ///          Console.WriteLine("battery Percent is: {0}", args.Percent);
144         ///     }
145         ///     Battery.PercentChanged += handler;
146         ///     await Task.Delay(20000);
147         ///  }
148         /// </code>
149         public static event EventHandler<BatteryPercentChangedEventArgs> PercentChanged
150         {
151             add
152             {
153                 lock (s_lock)
154                 {
155                     if (s_capacityChanged == null)
156                     {
157                         EventListenerStart(EventType.BatteryPercent);
158                     }
159                     s_capacityChanged += value;
160                 }
161             }
162             remove
163             {
164                 lock (s_lock)
165                 {
166                     s_capacityChanged -= value;
167                     if (s_capacityChanged == null)
168                     {
169                         EventListenerStop(EventType.BatteryPercent);
170                     }
171                 }
172             }
173         }
174
175         private static event EventHandler<BatteryLevelChangedEventArgs> s_levelChanged;
176
177         /// <summary>
178         /// LevelChanged is triggered when the battery level is changed.
179         /// </summary>
180         /// <since_tizen> 3 </since_tizen>
181         /// <param name="sender"></param>
182         /// <param name="e">BatteryLevelChangedEventArgs is an object that contains the changed battery level.</param>
183         /// <code>
184         /// public static async Task BatteryEventHandler()
185         /// {
186         ///     EventHandler<BatteryLevelChangedEventArgs> handler = null;
187         ///     handler = (object sender, BatteryLevelChangedEventArgs args) =>
188         ///     {
189         ///          Console.WriteLine("battery Level is: {0}", args.Level);
190         ///     }
191         ///     Battery.LevelChanged += handler;
192         ///     await Task.Delay(20000);
193         ///  }
194         /// </code>
195         public static event EventHandler<BatteryLevelChangedEventArgs> LevelChanged
196         {
197             add
198             {
199                 lock (s_lock)
200                 {
201                     if (s_levelChanged == null)
202                     {
203                         EventListenerStart(EventType.BatteryLevel);
204                     }
205                     s_levelChanged += value;
206                 }
207             }
208             remove
209             {
210                 lock (s_lock)
211                 {
212                     s_levelChanged -= value;
213                     if (s_levelChanged == null)
214                     {
215                         EventListenerStop(EventType.BatteryLevel);
216                     }
217                 }
218             }
219         }
220
221         private static event EventHandler<BatteryChargingStateChangedEventArgs> s_chargingStateChanged;
222         /// <summary>
223         /// ChargingStatusChanged is triggered when the battery charging status is changed.
224         /// This event is triggered when the charger is connected/disconnected.
225         /// </summary>
226         /// <since_tizen> 3 </since_tizen>
227         /// <param name="sender"></param>
228         /// <param name="e">BatteryChargingStateChangedEventArgs is an object that contains the changed battery charging state.</param>
229         /// <code>
230         /// public static async Task BatteryEventHandler()
231         /// {
232         ///     EventHandler<BatteryChargingStateChangedEventArgs> handler = null;
233         ///     handler = (object sender, BatteryChargingStateChangedEventArgs args) =>
234         ///     {
235         ///          Console.WriteLine("battery Level is: {0}", args.IsCharging);
236         ///     }
237         ///     Battery.ChargingStateChanged += handler;
238         ///     await Task.Delay(20000);
239         ///  }
240         /// </code>
241         public static event EventHandler<BatteryChargingStateChangedEventArgs> ChargingStateChanged
242         {
243             add
244             {
245                 lock (s_lock)
246                 {
247                     if (s_chargingStateChanged == null)
248                     {
249                         EventListenerStart(EventType.BatteryCharging);
250                     }
251                     s_chargingStateChanged += value;
252                 }
253             }
254             remove
255             {
256                 lock (s_lock)
257                 {
258                     s_chargingStateChanged -= value;
259                     if (s_chargingStateChanged == null)
260                     {
261                         EventListenerStop(EventType.BatteryCharging);
262                     }
263                 }
264             }
265         }
266
267         private static Interop.Device.deviceCallback s_cpacityHandler;
268         private static Interop.Device.deviceCallback s_levelHandler;
269         private static Interop.Device.deviceCallback s_chargingHandler;
270
271         private static void EventListenerStart(EventType eventType)
272         {
273             switch (eventType)
274             {
275                 case EventType.BatteryPercent:
276                     s_cpacityHandler = (int type, IntPtr value, IntPtr data) =>
277                     {
278                         int val = value.ToInt32();
279                         BatteryPercentChangedEventArgs e = new BatteryPercentChangedEventArgs()
280                         {
281                             Percent = val
282                         };
283                         s_capacityChanged?.Invoke(null, e);
284                         return true;
285                     };
286
287                     Interop.Device.DeviceAddCallback(eventType, s_cpacityHandler, IntPtr.Zero);
288                     break;
289
290                 case EventType.BatteryLevel:
291                     s_levelHandler = (int type, IntPtr value, IntPtr data) =>
292                     {
293                         int val = value.ToInt32();
294                         BatteryLevelChangedEventArgs e = new BatteryLevelChangedEventArgs()
295                         {
296                             Level = (BatteryLevelStatus)val
297                         };
298                         s_levelChanged?.Invoke(null, e);
299                         return true;
300                     };
301
302                     Interop.Device.DeviceAddCallback(eventType, s_levelHandler, IntPtr.Zero);
303                     break;
304
305                 case EventType.BatteryCharging:
306                     s_chargingHandler = (int type, IntPtr value, IntPtr data) =>
307                     {
308                         bool val = (value.ToInt32() == 1);
309                         BatteryChargingStateChangedEventArgs e = new BatteryChargingStateChangedEventArgs()
310                         {
311                             IsCharging = val
312                         };
313                         s_chargingStateChanged?.Invoke(null, e);
314                         return true;
315                     };
316                     Interop.Device.DeviceAddCallback(eventType, s_chargingHandler, IntPtr.Zero);
317                     break;
318             }
319         }
320
321         private static void EventListenerStop(EventType eventType)
322         {
323             switch (eventType)
324             {
325                 case EventType.BatteryPercent:
326                     Interop.Device.DeviceRemoveCallback(eventType, s_cpacityHandler);
327                     break;
328
329                 case EventType.BatteryLevel:
330                     Interop.Device.DeviceRemoveCallback(eventType, s_levelHandler);
331                     break;
332
333                 case EventType.BatteryCharging:
334                     Interop.Device.DeviceRemoveCallback(eventType, s_chargingHandler);
335                     break;
336             }
337         }
338     }
339 }