summaryrefslogtreecommitdiff
path: root/src/Tizen.System/Device/Battery.cs
blob: e622284ab88d82bff61f8970f649d7bff888e35d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;

namespace Tizen.System
{
    /// <summary>
    /// Enumeration for the Battery level.
    /// </summary>
    /// <since_tizen> 3 </since_tizen>
    public enum BatteryLevelStatus
    {
        /// <summary>
        /// The battery goes empty.
        /// Prepare for the safe termination of the application,
        /// because the device starts a shutdown process soon
        /// after entering this level.
        /// </summary>
        /// <since_tizen> 3 </since_tizen>
        Empty = 0,
        /// <summary>
        /// The battery charge is at a critical state.
        /// You may have to stop using multimedia features,
        /// because they are not guaranteed to work correctly
        /// at this battery status.
        /// </summary>
        /// <since_tizen> 3 </since_tizen>
        Critical,
        /// <summary>
        /// The battery has little charge left.
        /// </summary>
        /// <since_tizen> 3 </since_tizen>
        Low,
        /// <summary>
        /// The battery status is not to be careful.
        /// </summary>
        /// <since_tizen> 3 </since_tizen>
        High,
        /// <summary>
        /// The battery status is full.
        /// </summary>
        /// <since_tizen> 3 </since_tizen>
        Full
    }

    /// <summary>
    /// The Battery class provides the Properties and Events for device battery.
    /// </summary>
    /// <remarks>
    /// The Battery API provides the way to get the current battery capacity value (Percent),
    /// battery state and charging state. It also provides Events for an application
    /// to receive the battery status change events from the device.
    /// To receive the battery event, application should register with respective EventHandler.
    /// </remarks>
    /// <code>
    ///     Console.WriteLine("battery Charging state is: {0}", Tizen.System.Battery.IsCharging);
    ///     Console.WriteLine("battery Percent is: {0}", Tizen.System.Battery.Percent);
    /// </code>
    public static class Battery
    {
        private static readonly object s_lock = new object();
        /// <summary>
        /// Gets the battery charge percentage.
        /// </summary>
        /// <since_tizen> 3 </since_tizen>
        /// <value>It returns an integer value from 0 to 100 that indicates remaining
        /// battery charge as a percentage of the maximum level.</value>
        public static int Percent
        {
            get
            {
                int percent = 0;
                DeviceError res = (DeviceError)Interop.Device.DeviceBatteryGetPercent(out percent);
                if (res != DeviceError.None)
                {
                    Log.Warn(DeviceExceptionFactory.LogTag, "unable to get battery percentage.");
                }
                return percent;
            }
        }
        /// <summary>
        /// Gets the current Battery level.
        /// </summary>
        /// <since_tizen> 3 </since_tizen>
        public static BatteryLevelStatus Level
        {
            get
            {
                int level = 0;
                DeviceError res = (DeviceError)Interop.Device.DeviceBatteryGetLevelStatus(out level);
                if (res != DeviceError.None)
                {
                    Log.Warn(DeviceExceptionFactory.LogTag, "unable to get battery status.");
                }
                return (BatteryLevelStatus)level;
            }
        }
        /// <summary>
        /// Gets the current charging state.
        /// </summary>
        /// <since_tizen> 3 </since_tizen>
        public static bool IsCharging
        {
            get
            {
                bool charging;
                DeviceError res = (DeviceError)Interop.Device.DeviceBatteryIsCharging(out charging);
                if (res != DeviceError.None)
                {
                    Log.Warn(DeviceExceptionFactory.LogTag, "unable to get battery charging state.");
                }
                return charging;
            }
        }

        private static EventHandler<BatteryPercentChangedEventArgs> s_capacityChanged;
        /// <summary>
        /// CapacityChanged is triggered when the battery charge percentage is changed
        /// </summary>
        /// <since_tizen> 3 </since_tizen>
        /// <param name="sender"></param>
        /// <param name="e">A BatteryCapacityChangedEventArgs object that contains changed battery capacity (Percent)</param>
        /// <code>
        /// public static async Task BatteryEventHandler()
        /// {
        ///     EventHandler<BatteryPercentChangedEventArgs> handler = null;
        ///     handler = (object sender, BatteryChargingStateChangedEventArgs args) =>
        ///     {
        ///          Console.WriteLine("battery Percent is: {0}", args.Percent);
        ///     }
        ///     Battery.PercentChanged += handler;
        ///     await Task.Delay(20000);
        ///  }
        /// </code>
        public static event EventHandler<BatteryPercentChangedEventArgs> PercentChanged
        {
            add
            {
                lock (s_lock)
                {
                    if (s_capacityChanged == null)
                    {
                        EventListenerStart(EventType.BatteryPercent);
                    }
                    s_capacityChanged += value;
                }
            }
            remove
            {
                lock (s_lock)
                {
                    s_capacityChanged -= value;
                    if (s_capacityChanged == null)
                    {
                        EventListenerStop(EventType.BatteryPercent);
                    }
                }
            }
        }

        private static event EventHandler<BatteryLevelChangedEventArgs> s_levelChanged;

        /// <summary>
        /// LevelChanged is triggered when the battery level is changed
        /// </summary>
        /// <since_tizen> 3 </since_tizen>
        /// <param name="sender"></param>
        /// <param name="e">A BatteryLevelChangedEventArgs object that contains changed battery level </param>
        /// <code>
        /// public static async Task BatteryEventHandler()
        /// {
        ///     EventHandler<BatteryLevelChangedEventArgs> handler = null;
        ///     handler = (object sender, BatteryLevelChangedEventArgs args) =>
        ///     {
        ///          Console.WriteLine("battery Level is: {0}", args.Level);
        ///     }
        ///     Battery.LevelChanged += handler;
        ///     await Task.Delay(20000);
        ///  }
        /// </code>
        public static event EventHandler<BatteryLevelChangedEventArgs> LevelChanged
        {
            add
            {
                lock (s_lock)
                {
                    if (s_levelChanged == null)
                    {
                        EventListenerStart(EventType.BatteryLevel);
                    }
                    s_levelChanged += value;
                }
            }
            remove
            {
                lock (s_lock)
                {
                    s_levelChanged -= value;
                    if (s_levelChanged == null)
                    {
                        EventListenerStop(EventType.BatteryLevel);
                    }
                }
            }
        }

        private static event EventHandler<BatteryChargingStateChangedEventArgs> s_chargingStateChanged;
        /// <summary>
        /// ChargingStatusChanged is triggered when the Battery charging status is changed.
        /// This event is triggered when Charger is connected/Disconnected.
        /// </summary>
        /// <since_tizen> 3 </since_tizen>
        /// <param name="sender"></param>
        /// <param name="e">A BatteryChargingStateChangedEventArgs object that contains changed battery charging state</param>
        /// <code>
        /// public static async Task BatteryEventHandler()
        /// {
        ///     EventHandler<BatteryChargingStateChangedEventArgs> handler = null;
        ///     handler = (object sender, BatteryChargingStateChangedEventArgs args) =>
        ///     {
        ///          Console.WriteLine("battery Level is: {0}", args.IsCharging);
        ///     }
        ///     Battery.ChargingStateChanged += handler;
        ///     await Task.Delay(20000);
        ///  }
        /// </code>
        public static event EventHandler<BatteryChargingStateChangedEventArgs> ChargingStateChanged
        {
            add
            {
                lock (s_lock)
                {
                    if (s_chargingStateChanged == null)
                    {
                        EventListenerStart(EventType.BatteryCharging);
                    }
                    s_chargingStateChanged += value;
                }
            }
            remove
            {
                lock (s_lock)
                {
                    s_chargingStateChanged -= value;
                    if (s_chargingStateChanged == null)
                    {
                        EventListenerStop(EventType.BatteryCharging);
                    }
                }
            }
        }

        private static Interop.Device.deviceCallback s_cpacityHandler;
        private static Interop.Device.deviceCallback s_levelHandler;
        private static Interop.Device.deviceCallback s_chargingHandler;

        private static void EventListenerStart(EventType eventType)
        {
            switch (eventType)
            {
                case EventType.BatteryPercent:
                    s_cpacityHandler = (int type, IntPtr value, IntPtr data) =>
                    {
                        int val = value.ToInt32();
                        BatteryPercentChangedEventArgs e = new BatteryPercentChangedEventArgs()
                        {
                            Percent = val
                        };
                        s_capacityChanged?.Invoke(null, e);
                        return true;
                    };

                    Interop.Device.DeviceAddCallback(eventType, s_cpacityHandler, IntPtr.Zero);
                    break;

                case EventType.BatteryLevel:
                    s_levelHandler = (int type, IntPtr value, IntPtr data) =>
                    {
                        int val = value.ToInt32();
                        BatteryLevelChangedEventArgs e = new BatteryLevelChangedEventArgs()
                        {
                            Level = (BatteryLevelStatus)val
                        };
                        s_levelChanged?.Invoke(null, e);
                        return true;
                    };

                    Interop.Device.DeviceAddCallback(eventType, s_levelHandler, IntPtr.Zero);
                    break;

                case EventType.BatteryCharging:
                    s_chargingHandler = (int type, IntPtr value, IntPtr data) =>
                    {
                        bool val = (value.ToInt32() == 1);
                        BatteryChargingStateChangedEventArgs e = new BatteryChargingStateChangedEventArgs()
                        {
                            IsCharging = val
                        };
                        s_chargingStateChanged?.Invoke(null, e);
                        return true;
                    };
                    Interop.Device.DeviceAddCallback(eventType, s_chargingHandler, IntPtr.Zero);
                    break;
            }
        }

        private static void EventListenerStop(EventType eventType)
        {
            switch (eventType)
            {
                case EventType.BatteryPercent:
                    Interop.Device.DeviceRemoveCallback(eventType, s_cpacityHandler);
                    break;

                case EventType.BatteryLevel:
                    Interop.Device.DeviceRemoveCallback(eventType, s_levelHandler);
                    break;

                case EventType.BatteryCharging:
                    Interop.Device.DeviceRemoveCallback(eventType, s_chargingHandler);
                    break;
            }
        }
    }
}