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