Move using statement to namespace scope
[platform/core/csapi/badge.git] / Tizen.Applications.Badge / Tizen.Applications / BadgeControl.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 namespace Tizen.Applications
18 {
19     using System;
20     using System.Collections.Generic;
21     /// <summary>
22     /// Class for badge operation.
23     /// </summary>
24     public static class BadgeControl
25     {
26         private static event EventHandler<BadgeEventArgs> s_changed;
27         private static bool s_registered = false;
28         private static Interop.Badge.ChangedCallback s_callback;
29
30         /// <summary>
31         /// Event handler for receiving badge events.
32         /// </summary>
33         /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
34         /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
35         /// <privilege>http://tizen.org/privilege/notification</privilege>
36         public static event EventHandler<BadgeEventArgs> Changed
37         {
38             add
39             {
40                 if (s_changed == null && !s_registered)
41                 {
42                     if (s_callback == null)
43                     {
44                         s_callback = new Interop.Badge.ChangedCallback(OnChangedEvent);
45                     }
46
47                     Interop.Badge.ErrorCode err = Interop.Badge.SetChangedCallback(s_callback, IntPtr.Zero);
48
49                     switch (err)
50                     {
51                         case Interop.Badge.ErrorCode.InvalidParameter:
52                             throw new InvalidOperationException("Invalid parameter at unmanaged code");
53
54                         case Interop.Badge.ErrorCode.PermissionDenied:
55                             throw new UnauthorizedAccessException();
56
57                         case Interop.Badge.ErrorCode.OutOfMemory:
58                             throw new InvalidOperationException("Out-of-memory at unmanaged code");
59                     }
60                     s_registered = true;
61                 }
62
63                 s_changed += value;
64             }
65             remove
66             {
67                 s_changed -= value;
68                 if (s_changed == null && s_registered)
69                 {
70                     Interop.Badge.ErrorCode err = Interop.Badge.UnsetChangedCallback(s_callback);
71
72                     switch (err)
73                     {
74                         case Interop.Badge.ErrorCode.InvalidParameter:
75                             throw new InvalidOperationException("Invalid parameter at unmanaged code");
76
77                         case Interop.Badge.ErrorCode.PermissionDenied:
78                             throw new UnauthorizedAccessException();
79
80                         case Interop.Badge.ErrorCode.NotExist:
81                             throw new InvalidOperationException("Not exist");
82                     }
83
84                     s_callback = null;
85                     s_registered = false;
86                 }
87             }
88         }
89
90         /// <summary>
91         /// Gets the badge information from application ID.
92         /// </summary>
93         /// <param name="appId">Application ID</param>
94         /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
95         /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
96         /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
97         /// <privilege>http://tizen.org/privilege/notification</privilege>
98         public static Badge Find(string appId)
99         {
100             uint count;
101             uint display;
102
103             Interop.Badge.ErrorCode err = Interop.Badge.GetCount(appId, out count);
104
105             switch (err)
106             {
107                 case Interop.Badge.ErrorCode.InvalidParameter:
108                     throw new ArgumentException("Invalid parameter");
109
110                 case Interop.Badge.ErrorCode.PermissionDenied:
111                     throw new UnauthorizedAccessException();
112
113                 case Interop.Badge.ErrorCode.FromDb:
114                     throw new InvalidOperationException("Error from DB");
115
116                 case Interop.Badge.ErrorCode.OutOfMemory:
117                     throw new InvalidOperationException("Out-of-memory at unmanaged code");
118             }
119
120             err = Interop.Badge.GetDisplay(appId, out display);
121
122             switch (err)
123             {
124                 case Interop.Badge.ErrorCode.InvalidParameter:
125                     throw new ArgumentException("Invalid parameter");
126
127                 case Interop.Badge.ErrorCode.PermissionDenied:
128                     throw new UnauthorizedAccessException();
129
130                 case Interop.Badge.ErrorCode.FromDb:
131                     throw new InvalidOperationException("Error from DB");
132
133                 case Interop.Badge.ErrorCode.OutOfMemory:
134                     throw new InvalidOperationException("Out-of-memory at unmanaged code");
135
136                 case Interop.Badge.ErrorCode.NotExist:
137                     throw new InvalidOperationException("Not exist");
138             }
139
140             return new Badge(appId, (int)count, display == 0 ? false : true);
141         }
142
143         /// <summary>
144         /// Removes the badge information.
145         /// </summary>
146         /// <param name="appId">Application ID</param>
147         /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
148         /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
149         /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
150         /// <privilege>http://tizen.org/privilege/notification</privilege>
151         public static void Remove(string appId)
152         {
153             Interop.Badge.ErrorCode err = Interop.Badge.Remove(appId);
154
155             switch (err)
156             {
157                 case Interop.Badge.ErrorCode.InvalidParameter:
158                     throw new ArgumentException("Invalid parameter");
159
160                 case Interop.Badge.ErrorCode.PermissionDenied:
161                     throw new UnauthorizedAccessException();
162
163                 case Interop.Badge.ErrorCode.IoError:
164                     throw new InvalidOperationException("Error from I/O");
165
166                 case Interop.Badge.ErrorCode.ServiceNotReady:
167                     throw new InvalidOperationException("Service is not ready");
168
169                 case Interop.Badge.ErrorCode.NotExist:
170                     throw new InvalidOperationException("Not exist");
171             }
172         }
173
174         /// <summary>
175         /// Adds the badge information.
176         /// </summary>
177         /// <param name="appId">Application ID</param>
178         /// <param name="count">Count value</param>
179         /// <param name="isDisplay">True if it should be displayed</param>
180         /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
181         /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
182         /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
183         /// <privilege>http://tizen.org/privilege/notification</privilege>
184         public static void Add(string appId, int count = 1, bool isDisplay = true)
185         {
186             Interop.Badge.ErrorCode err = Interop.Badge.Add(appId);
187
188             switch (err)
189             {
190                 case Interop.Badge.ErrorCode.InvalidParameter:
191                     throw new ArgumentException("Invalid parameter");
192
193                 case Interop.Badge.ErrorCode.PermissionDenied:
194                     throw new UnauthorizedAccessException();
195
196                 case Interop.Badge.ErrorCode.IoError:
197                     throw new InvalidOperationException("Error from I/O");
198
199                 case Interop.Badge.ErrorCode.ServiceNotReady:
200                     throw new InvalidOperationException("Service is not ready");
201
202                 case Interop.Badge.ErrorCode.InvalidPackage:
203                     throw new InvalidOperationException("The caller application is not signed with the certificate of badge application ID");
204             }
205
206             try
207             {
208                 Update(appId, count, isDisplay);
209             }
210             catch (Exception e)
211             {
212                 Remove(appId);
213                 throw e;
214             }
215         }
216
217         /// <summary>
218         /// Updates the badge information.
219         /// </summary>
220         /// <param name="appId">Application ID</param>
221         /// <param name="count">Count value</param>
222         /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
223         /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
224         /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
225         /// <privilege>http://tizen.org/privilege/notification</privilege>
226         public static void Update(string appId, int count)
227         {
228             Interop.Badge.ErrorCode err = Interop.Badge.SetCount(appId, (uint)count);
229
230             switch (err)
231             {
232                 case Interop.Badge.ErrorCode.InvalidParameter:
233                     throw new ArgumentException("Invalid parameter");
234
235                 case Interop.Badge.ErrorCode.PermissionDenied:
236                     throw new UnauthorizedAccessException();
237
238                 case Interop.Badge.ErrorCode.IoError:
239                     throw new InvalidOperationException("Error from I/O");
240
241                 case Interop.Badge.ErrorCode.ServiceNotReady:
242                     throw new InvalidOperationException("Service is not ready");
243             }
244         }
245
246         /// <summary>
247         /// Updates the badge information.
248         /// </summary>
249         /// <param name="appId">Application ID</param>
250         /// <param name="isDisplay">True if it should be displayed</param>
251         /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
252         /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
253         /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
254         /// <privilege>http://tizen.org/privilege/notification</privilege>
255         public static void Update(string appId, bool isDisplay)
256         {
257             Interop.Badge.ErrorCode err = Interop.Badge.SetDisplay(appId, isDisplay ? 1U : 0U);
258
259             switch (err)
260             {
261                 case Interop.Badge.ErrorCode.InvalidParameter:
262                     throw new ArgumentException("Invalid parameter");
263
264                 case Interop.Badge.ErrorCode.PermissionDenied:
265                     throw new UnauthorizedAccessException();
266
267                 case Interop.Badge.ErrorCode.IoError:
268                     throw new InvalidOperationException("Error from I/O");
269
270                 case Interop.Badge.ErrorCode.ServiceNotReady:
271                     throw new InvalidOperationException("Service is not ready");
272             }
273         }
274
275         /// <summary>
276         /// Updates the badge information.
277         /// </summary>
278         /// <param name="appId">Application ID</param>
279         /// <param name="count">Count value</param>
280         /// <param name="isDisplay">True if it should be displayed</param>
281         /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
282         /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
283         /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
284         /// <privilege>http://tizen.org/privilege/notification</privilege>
285         public static void Update(string appId, int count, bool isDisplay)
286         {
287             Update(appId, count);
288             Update(appId, isDisplay);
289         }
290
291         /// <summary>
292         /// Gets all badge information.
293         /// </summary>
294         /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
295         /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
296         /// <privilege>http://tizen.org/privilege/notification</privilege>
297         public static IEnumerable<Badge> GetBadges()
298         {
299             IList<Badge> list = new List<Badge>();
300
301             Interop.Badge.ErrorCode err = Interop.Badge.Foreach((appId, count, userData) =>
302             {
303                 uint display = 0;
304                 Interop.Badge.ErrorCode e = Interop.Badge.GetDisplay(appId, out display);
305                 switch (e)
306                 {
307                     case Interop.Badge.ErrorCode.InvalidParameter:
308                         throw new InvalidOperationException("Invalid parameter at unmanaged code");
309
310                     case Interop.Badge.ErrorCode.PermissionDenied:
311                         throw new UnauthorizedAccessException();
312
313                     case Interop.Badge.ErrorCode.FromDb:
314                         throw new InvalidOperationException("Error from DB");
315
316                     case Interop.Badge.ErrorCode.OutOfMemory:
317                         throw new InvalidOperationException("Out-of-memory at unmanaged code");
318
319                     case Interop.Badge.ErrorCode.NotExist:
320                         throw new InvalidOperationException("Not exist");
321
322                     case Interop.Badge.ErrorCode.ServiceNotReady:
323                         throw new InvalidOperationException("Service is not ready");
324                 }
325
326                 list.Add(new Badge(appId, (int)count, display == 0 ? false : true));
327             }, IntPtr.Zero);
328
329             switch (err)
330             {
331                 case Interop.Badge.ErrorCode.InvalidParameter:
332                     throw new InvalidOperationException("Invalid parameter at unmanaged code");
333
334                 case Interop.Badge.ErrorCode.PermissionDenied:
335                     throw new UnauthorizedAccessException();
336
337                 case Interop.Badge.ErrorCode.FromDb:
338                     throw new InvalidOperationException("Error from DB");
339
340                 case Interop.Badge.ErrorCode.OutOfMemory:
341                     throw new InvalidOperationException("Out-of-memory at unmanaged code");
342
343                 case Interop.Badge.ErrorCode.NotExist:
344                     throw new InvalidOperationException("Not exist");
345             }
346
347             return list;
348         }
349
350         private static void OnChangedEvent(Interop.Badge.Action action, string appId, uint count, IntPtr userData)
351         {
352             uint display = 0;
353             uint countLocal = 0;
354
355             switch (action)
356             {
357                 case Interop.Badge.Action.Create:
358                     s_changed?.Invoke(null, new BadgeEventArgs()
359                     {
360                         Reason = BadgeEventArgs.Action.Add,
361                         Badge = new Badge(appId, 0, false)
362                     });
363                     break;
364
365                 case Interop.Badge.Action.Remove:
366                     s_changed?.Invoke(null, new BadgeEventArgs()
367                     {
368                         Reason = BadgeEventArgs.Action.Remove,
369                         Badge = new Badge(appId, 0, false)
370                     });
371                     break;
372
373                 case Interop.Badge.Action.Update:
374                     Interop.Badge.GetDisplay(appId, out display);
375                     s_changed?.Invoke(null, new BadgeEventArgs()
376                     {
377                         Reason = BadgeEventArgs.Action.Update,
378                         Badge = new Badge(appId, (int)count, display == 0 ? false : true)
379                     });
380                     break;
381
382                 case Interop.Badge.Action.ChangedDisplay:
383                     Interop.Badge.GetCount(appId, out countLocal);
384                     s_changed?.Invoke(null, new BadgeEventArgs()
385                     {
386                         Reason = BadgeEventArgs.Action.Update,
387                         Badge = new Badge(appId, (int)countLocal, count == 0 ? false : true)
388                     });
389                     break;
390
391                 case Interop.Badge.Action.ServiceReady:
392                     // Ignore
393                     break;
394             }
395         }
396     }
397 }