Release 4.0.0-preview1-00194
[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 fully charged.
55         /// It means no more charge.
56         /// </summary>
57         /// <since_tizen> 3 </since_tizen>
58         Full
59     }
60
61     /// <summary>
62     /// The Battery class provides the properties and events for the device battery.
63     /// </summary>
64     /// <remarks>
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.
69     /// </remarks>
70     /// <code>
71     ///     Console.WriteLine("battery Charging state is: {0}", Tizen.System.Battery.IsCharging);
72     ///     Console.WriteLine("battery Percent is: {0}", Tizen.System.Battery.Percent);
73     /// </code>
74     public static class Battery
75     {
76         private static readonly object s_lock = new object();
77         /// <summary>
78         /// Gets the battery charge percentage.
79         /// </summary>
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
84         {
85             get
86             {
87                 int percent = 0;
88                 DeviceError res = (DeviceError)Interop.Device.DeviceBatteryGetPercent(out percent);
89                 if (res != DeviceError.None)
90                 {
91                     Log.Warn(DeviceExceptionFactory.LogTag, "unable to get battery percentage.");
92                 }
93                 return percent;
94             }
95         }
96         /// <summary>
97         /// Gets the current battery level.
98         /// </summary>
99         /// <since_tizen> 3 </since_tizen>
100         public static BatteryLevelStatus Level
101         {
102             get
103             {
104                 int level = 0;
105                 DeviceError res = (DeviceError)Interop.Device.DeviceBatteryGetLevelStatus(out level);
106                 if (res != DeviceError.None)
107                 {
108                     Log.Warn(DeviceExceptionFactory.LogTag, "unable to get battery status.");
109                 }
110                 return (BatteryLevelStatus)level;
111             }
112         }
113         /// <summary>
114         /// Gets the current charging state.
115         /// </summary>
116         /// <since_tizen> 3 </since_tizen>
117         public static bool IsCharging
118         {
119             get
120             {
121                 bool charging;
122                 DeviceError res = (DeviceError)Interop.Device.DeviceBatteryIsCharging(out charging);
123                 if (res != DeviceError.None)
124                 {
125                     Log.Warn(DeviceExceptionFactory.LogTag, "unable to get battery charging state.");
126                 }
127                 return charging;
128             }
129         }
130
131         private static EventHandler<BatteryPercentChangedEventArgs> s_capacityChanged;
132         /// <summary>
133         /// CapacityChanged is triggered when the battery charge percentage is changed.
134         /// </summary>
135         /// <since_tizen> 3 </since_tizen>
136         /// <code>
137         /// public static async Task BatteryEventHandler()
138         /// {
139         ///     EventHandler<BatteryPercentChangedEventArgs> handler = null;
140         ///     handler = (object sender, BatteryChargingStateChangedEventArgs args) =>
141         ///     {
142         ///          Console.WriteLine("battery Percent is: {0}", args.Percent);
143         ///     }
144         ///     Battery.PercentChanged += handler;
145         ///     await Task.Delay(20000);
146         ///  }
147         /// </code>
148         public static event EventHandler<BatteryPercentChangedEventArgs> PercentChanged
149         {
150             add
151             {
152                 lock (s_lock)
153                 {
154                     if (s_capacityChanged == null)
155                     {
156                         EventListenerStart(EventType.BatteryPercent);
157                     }
158                     s_capacityChanged += value;
159                 }
160             }
161             remove
162             {
163                 lock (s_lock)
164                 {
165                     s_capacityChanged -= value;
166                     if (s_capacityChanged == null)
167                     {
168                         EventListenerStop(EventType.BatteryPercent);
169                     }
170                 }
171             }
172         }
173
174         private static event EventHandler<BatteryLevelChangedEventArgs> s_levelChanged;
175
176         /// <summary>
177         /// LevelChanged is triggered when the battery level is changed.
178         /// </summary>
179         /// <since_tizen> 3 </since_tizen>
180         /// <code>
181         /// public static async Task BatteryEventHandler()
182         /// {
183         ///     EventHandler<BatteryLevelChangedEventArgs> handler = null;
184         ///     handler = (object sender, BatteryLevelChangedEventArgs args) =>
185         ///     {
186         ///          Console.WriteLine("battery Level is: {0}", args.Level);
187         ///     }
188         ///     Battery.LevelChanged += handler;
189         ///     await Task.Delay(20000);
190         ///  }
191         /// </code>
192         public static event EventHandler<BatteryLevelChangedEventArgs> LevelChanged
193         {
194             add
195             {
196                 lock (s_lock)
197                 {
198                     if (s_levelChanged == null)
199                     {
200                         EventListenerStart(EventType.BatteryLevel);
201                     }
202                     s_levelChanged += value;
203                 }
204             }
205             remove
206             {
207                 lock (s_lock)
208                 {
209                     s_levelChanged -= value;
210                     if (s_levelChanged == null)
211                     {
212                         EventListenerStop(EventType.BatteryLevel);
213                     }
214                 }
215             }
216         }
217
218         private static event EventHandler<BatteryChargingStateChangedEventArgs> s_chargingStateChanged;
219         /// <summary>
220         /// ChargingStatusChanged is triggered when the battery charging status is changed.
221         /// This event is triggered when the charger is connected/disconnected.
222         /// </summary>
223         /// <since_tizen> 3 </since_tizen>
224         /// <code>
225         /// public static async Task BatteryEventHandler()
226         /// {
227         ///     EventHandler<BatteryChargingStateChangedEventArgs> handler = null;
228         ///     handler = (object sender, BatteryChargingStateChangedEventArgs args) =>
229         ///     {
230         ///          Console.WriteLine("battery Level is: {0}", args.IsCharging);
231         ///     }
232         ///     Battery.ChargingStateChanged += handler;
233         ///     await Task.Delay(20000);
234         ///  }
235         /// </code>
236         public static event EventHandler<BatteryChargingStateChangedEventArgs> ChargingStateChanged
237         {
238             add
239             {
240                 lock (s_lock)
241                 {
242                     if (s_chargingStateChanged == null)
243                     {
244                         EventListenerStart(EventType.BatteryCharging);
245                     }
246                     s_chargingStateChanged += value;
247                 }
248             }
249             remove
250             {
251                 lock (s_lock)
252                 {
253                     s_chargingStateChanged -= value;
254                     if (s_chargingStateChanged == null)
255                     {
256                         EventListenerStop(EventType.BatteryCharging);
257                     }
258                 }
259             }
260         }
261
262         private static Interop.Device.deviceCallback s_cpacityHandler;
263         private static Interop.Device.deviceCallback s_levelHandler;
264         private static Interop.Device.deviceCallback s_chargingHandler;
265
266         private static void EventListenerStart(EventType eventType)
267         {
268             switch (eventType)
269             {
270                 case EventType.BatteryPercent:
271                     s_cpacityHandler = (int type, IntPtr value, IntPtr data) =>
272                     {
273                         int val = value.ToInt32();
274                         BatteryPercentChangedEventArgs e = new BatteryPercentChangedEventArgs()
275                         {
276                             Percent = val
277                         };
278                         s_capacityChanged?.Invoke(null, e);
279                         return true;
280                     };
281
282                     Interop.Device.DeviceAddCallback(eventType, s_cpacityHandler, IntPtr.Zero);
283                     break;
284
285                 case EventType.BatteryLevel:
286                     s_levelHandler = (int type, IntPtr value, IntPtr data) =>
287                     {
288                         int val = value.ToInt32();
289                         BatteryLevelChangedEventArgs e = new BatteryLevelChangedEventArgs()
290                         {
291                             Level = (BatteryLevelStatus)val
292                         };
293                         s_levelChanged?.Invoke(null, e);
294                         return true;
295                     };
296
297                     Interop.Device.DeviceAddCallback(eventType, s_levelHandler, IntPtr.Zero);
298                     break;
299
300                 case EventType.BatteryCharging:
301                     s_chargingHandler = (int type, IntPtr value, IntPtr data) =>
302                     {
303                         bool val = (value.ToInt32() == 1);
304                         BatteryChargingStateChangedEventArgs e = new BatteryChargingStateChangedEventArgs()
305                         {
306                             IsCharging = val
307                         };
308                         s_chargingStateChanged?.Invoke(null, e);
309                         return true;
310                     };
311                     Interop.Device.DeviceAddCallback(eventType, s_chargingHandler, IntPtr.Zero);
312                     break;
313             }
314         }
315
316         private static void EventListenerStop(EventType eventType)
317         {
318             switch (eventType)
319             {
320                 case EventType.BatteryPercent:
321                     Interop.Device.DeviceRemoveCallback(eventType, s_cpacityHandler);
322                     break;
323
324                 case EventType.BatteryLevel:
325                     Interop.Device.DeviceRemoveCallback(eventType, s_levelHandler);
326                     break;
327
328                 case EventType.BatteryCharging:
329                     Interop.Device.DeviceRemoveCallback(eventType, s_chargingHandler);
330                     break;
331             }
332         }
333     }
334 }