[Device] Implementation of system.device module including error handling.
[platform/core/csapi/system.git] / Tizen.System / Device / Battery.cs
1 using System;
2
3 namespace Tizen.System
4 {
5     /// <summary>
6     /// Enumeration for the battery level.
7     /// </summary>
8     public enum BatteryLevelStatus
9     {
10         /// <summary>
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.
15         /// </summary>
16         Empty = 0,
17         /// <summary>
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.
22         /// </summary>
23         Critical,
24         /// <summary>
25         /// The battery has little charge left.
26         /// </summary>
27         Low,
28         /// <summary>
29         /// The battery status is not to be careful.
30         /// </summary>
31         High,
32         /// <summary>
33         /// The battery status is full.
34         /// </summary>
35         Full
36     }
37
38     /// <summary>
39     /// The Battery API provides functions to get information about the battery.
40     /// </summary>
41     /// <remarks>
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.
46     /// </remarks>
47     public static class Battery
48     {
49         private static readonly object s_lock = new object();
50         /// <summary>
51         /// Gets the battery charge percentage.
52         /// </summary>
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
56         {
57             get
58             {
59                 int percent = 0;
60                 DeviceError res = (DeviceError) Interop.Device.DeviceBatteryGetPercent(out percent);
61                 if (res != DeviceError.None)
62                 {
63                     Log.Warn(DeviceExceptionFactory.LogTag, "unable to get battery percentage.");
64                 }
65                 return percent;
66             }
67         }
68         /// <summary>
69         /// Gets the battery level.
70         /// </summary>
71         public static BatteryLevelStatus Level
72         {
73             get
74             {
75                 int level = 0;
76                 DeviceError res = (DeviceError) Interop.Device.DeviceBatteryGetLevelStatus(out level);
77                 if (res != DeviceError.None)
78                 {
79                     Log.Warn(DeviceExceptionFactory.LogTag, "unable to get battery status.");
80                 }
81                 return (BatteryLevelStatus)level;
82             }
83         }
84         /// <summary>
85         /// Gets the charging state.
86         /// </summary>
87         public static bool IsCharging
88         {
89             get
90             {
91                 bool charging;
92                 DeviceError res = (DeviceError) Interop.Device.DeviceBatteryIsCharging(out charging);
93                 if (res != DeviceError.None)
94                 {
95                     Log.Warn(DeviceExceptionFactory.LogTag, "unable to get battery charging state.");
96                 }
97                 return charging;
98             }
99         }
100
101         private static EventHandler<BatteryCapacityChangedEventArgs> s_capacityChanged;
102         /// <summary>
103         /// CapacityChanged is triggered when the battery charge percentage is changed
104         /// </summary>
105         /// <param name="sender"></param>
106         /// <param name="e">A BatteryCapacityChangedEventArgs object that contains changed battery capacity</param>
107
108         public static event EventHandler<BatteryCapacityChangedEventArgs> CapacityChanged
109         {
110             add
111             {
112                 lock (s_lock)
113                 {
114                     if (s_capacityChanged == null)
115                     {
116                         EventListenerStart(EventType.BatteryCapacity);
117                     }
118                     s_capacityChanged += value;
119                 }
120             }
121             remove
122             {
123                 lock (s_lock)
124                 {
125                     s_capacityChanged -= value;
126                     if (s_capacityChanged == null)
127                     {
128                         EventListenerStop(EventType.BatteryCapacity);
129                     }
130                 }
131             }
132         }
133
134         private static event EventHandler<BatteryLevelChangedEventArgs> s_levelChanged;
135
136         /// <summary>
137         /// LevelChanged is triggered when the battery level is changed
138         /// </summary>
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
142         {
143             add
144             {
145                 lock (s_lock)
146                 {
147                     if (s_levelChanged == null)
148                     {
149                         EventListenerStart(EventType.BatteryLevel);
150                     }
151                     s_levelChanged += value;
152                 }
153             }
154             remove
155             {
156                 lock (s_lock)
157                 {
158                     s_levelChanged -= value;
159                     if (s_levelChanged == null)
160                     {
161                         EventListenerStop(EventType.BatteryLevel);
162                     }
163                 }
164             }
165         }
166
167         private static event EventHandler<BatteryChargingStateChangedEventArgs> s_chargingStateChanged;
168         /// <summary>
169         /// ChargingStatusChanged is triggered when the battery charging status is changed
170         /// </summary>
171         /// <param name="sender"></param>
172         /// <param name="e">A BatteryChargingStateChangedEventArgs object that contains changed battery charging state</param>
173
174         public static event EventHandler<BatteryChargingStateChangedEventArgs> ChargingStatusChanged
175         {
176             add
177             {
178                 lock (s_lock)
179                 {
180                     if (s_chargingStateChanged == null)
181                     {
182                         EventListenerStart(EventType.BatteryCharging);
183                     }
184                     s_chargingStateChanged += value;
185                 }
186             }
187             remove
188             {
189                 lock (s_lock)
190                 {
191                     s_chargingStateChanged -= value;
192                     if (s_chargingStateChanged == null)
193                     {
194                         EventListenerStop(EventType.BatteryCharging);
195                     }
196                 }
197             }
198         }
199
200         private static Interop.Device.deviceCallback s_cpacityHandler;
201         private static Interop.Device.deviceCallback s_levelHandler;
202         private static Interop.Device.deviceCallback s_chargingHandler;
203
204         private static void EventListenerStart(EventType eventType)
205         {
206             switch (eventType)
207             {
208                 case EventType.BatteryCapacity:
209                     s_cpacityHandler = (int type, IntPtr value, IntPtr data) =>
210                     {
211                         int val = value.ToInt32();
212                         BatteryCapacityChangedEventArgs e = new BatteryCapacityChangedEventArgs()
213                         {
214                             Capacity = val
215                         };
216                         s_capacityChanged?.Invoke(null, e);
217                         return true;
218                     };
219
220                     Interop.Device.DeviceAddCallback(eventType, s_cpacityHandler, IntPtr.Zero);
221                     break;
222
223                 case EventType.BatteryLevel:
224                     s_levelHandler = (int type, IntPtr value, IntPtr data) =>
225                     {
226                         int val = value.ToInt32();
227                         BatteryLevelChangedEventArgs e = new BatteryLevelChangedEventArgs()
228                         {
229                             Level = (BatteryLevelStatus)val
230                         };
231                         s_levelChanged?.Invoke(null, e);
232                         return true;
233                     };
234
235                     Interop.Device.DeviceAddCallback(eventType, s_levelHandler, IntPtr.Zero);
236                     break;
237
238                 case EventType.BatteryCharging:
239                     s_chargingHandler = (int type, IntPtr value, IntPtr data) =>
240                     {
241                         bool val = (value.ToInt32() == 1);
242                         BatteryChargingStateChangedEventArgs e = new BatteryChargingStateChangedEventArgs()
243                         {
244                             Charging = val
245                         };
246                         s_chargingStateChanged?.Invoke(null, e);
247                         return true;
248                     };
249                     Interop.Device.DeviceAddCallback(eventType, s_chargingHandler, IntPtr.Zero);
250                     break;
251             }
252         }
253
254         private static void EventListenerStop(EventType eventType)
255         {
256             switch (eventType)
257             {
258                 case EventType.BatteryCapacity:
259                     Interop.Device.DeviceRemoveCallback(eventType, s_cpacityHandler);
260                     break;
261
262                 case EventType.BatteryLevel:
263                     Interop.Device.DeviceRemoveCallback(eventType, s_levelHandler);
264                     break;
265
266                 case EventType.BatteryCharging:
267                     Interop.Device.DeviceRemoveCallback(eventType, s_chargingHandler);
268                     break;
269             }
270         }
271     }
272 }