c94f2437bdf6bf0cd02db42f25bc143a822b4338
[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                     BadgeError err = Interop.Badge.SetChangedCallback(s_callback, IntPtr.Zero);
48                     if (err != BadgeError.None)
49                     {
50                         throw BadgeErrorFactory.GetException(err, "Failed to add event handler");
51                     }
52
53                     s_registered = true;
54                 }
55
56                 s_changed += value;
57             }
58             remove
59             {
60                 s_changed -= value;
61                 if (s_changed == null && s_registered)
62                 {
63                     BadgeError err = Interop.Badge.UnsetChangedCallback(s_callback);
64                     if (err != BadgeError.None)
65                     {
66                         throw BadgeErrorFactory.GetException(err, "Failed to remove event handler");
67                     }
68
69                     s_callback = null;
70                     s_registered = false;
71                 }
72             }
73         }
74
75         /// <summary>
76         /// Gets the badge information from application ID.
77         /// </summary>
78         /// <param name="appId">Application ID</param>
79         /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
80         /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
81         /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
82         /// <privilege>http://tizen.org/privilege/notification</privilege>
83         public static Badge Find(string appId)
84         {
85             uint count;
86             uint display;
87
88             BadgeError err = Interop.Badge.GetCount(appId, out count);
89             if (err != BadgeError.None)
90             {
91                 throw BadgeErrorFactory.GetException(err, "Failed to find badge count of " + appId);
92             }
93
94             err = Interop.Badge.GetDisplay(appId, out display);
95             if (err != BadgeError.None)
96             {
97                 throw BadgeErrorFactory.GetException(err, "Failed to find badge display of " + appId);
98             }
99
100             return new Badge(appId, (int)count, display == 0 ? false : true);
101         }
102
103         /// <summary>
104         /// Removes the badge information.
105         /// </summary>
106         /// <param name="appId">Application ID</param>
107         /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
108         /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
109         /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
110         /// <privilege>http://tizen.org/privilege/notification</privilege>
111         public static void Remove(string appId)
112         {
113             BadgeError err = Interop.Badge.Remove(appId);
114             if (err != BadgeError.None)
115             {
116                 throw BadgeErrorFactory.GetException(err, "Failed to Remove badge of " + appId);
117             }
118         }
119
120         /// <summary>
121         /// Adds the badge information.
122         /// </summary>
123         /// <param name="appId">Application ID</param>
124         /// <param name="count">Count value</param>
125         /// <param name="isDisplay">True if it should be displayed</param>
126         /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
127         /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
128         /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
129         /// <privilege>http://tizen.org/privilege/notification</privilege>
130         public static void Add(string appId, int count = 1, bool isDisplay = true)
131         {
132             BadgeError err = Interop.Badge.Add(appId);
133             if (err != BadgeError.None)
134             {
135                 throw BadgeErrorFactory.GetException(err, "Failed to add badge of " + appId);
136             }
137
138             try
139             {
140                 Update(appId, count, isDisplay);
141             }
142             catch (Exception e)
143             {
144                 Remove(appId);
145                 throw e;
146             }
147         }
148
149         /// <summary>
150         /// Updates the badge information.
151         /// </summary>
152         /// <param name="appId">Application ID</param>
153         /// <param name="count">Count value</param>
154         /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
155         /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
156         /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
157         /// <privilege>http://tizen.org/privilege/notification</privilege>
158         public static void Update(string appId, int count)
159         {
160             BadgeError err = Interop.Badge.SetCount(appId, (uint)count);
161             if (err != BadgeError.None)
162             {
163                 throw BadgeErrorFactory.GetException(err, "Failed to update badge of " + appId);
164             }
165         }
166
167         /// <summary>
168         /// Updates the badge information.
169         /// </summary>
170         /// <param name="appId">Application ID</param>
171         /// <param name="isDisplay">True if it should be displayed</param>
172         /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
173         /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
174         /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
175         /// <privilege>http://tizen.org/privilege/notification</privilege>
176         public static void Update(string appId, bool isDisplay)
177         {
178             BadgeError err = Interop.Badge.SetDisplay(appId, isDisplay ? 1U : 0U);
179             if (err != BadgeError.None)
180             {
181                 throw BadgeErrorFactory.GetException(err, "Failed to update badge of " + appId);
182             }
183         }
184
185         /// <summary>
186         /// Updates the badge information.
187         /// </summary>
188         /// <param name="appId">Application ID</param>
189         /// <param name="count">Count value</param>
190         /// <param name="isDisplay">True if it should be displayed</param>
191         /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
192         /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
193         /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
194         /// <privilege>http://tizen.org/privilege/notification</privilege>
195         public static void Update(string appId, int count, bool isDisplay)
196         {
197             Update(appId, count);
198             Update(appId, isDisplay);
199         }
200
201         /// <summary>
202         /// Gets all badge information.
203         /// </summary>
204         /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
205         /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
206         /// <privilege>http://tizen.org/privilege/notification</privilege>
207         public static IEnumerable<Badge> GetBadges()
208         {
209             IList<Badge> list = new List<Badge>();
210
211             BadgeError err = Interop.Badge.Foreach((appId, count, userData) =>
212             {
213                 uint display = 0;
214                 BadgeError errGetDisplay = Interop.Badge.GetDisplay(appId, out display);
215                 if (errGetDisplay != BadgeError.None)
216                 {
217                     throw BadgeErrorFactory.GetException(errGetDisplay, "Failed to get badges ");
218                 }
219
220                 list.Add(new Badge(appId, (int)count, display == 0 ? false : true));
221             }, IntPtr.Zero);
222
223             if (err != BadgeError.None)
224             {
225                 throw BadgeErrorFactory.GetException(err, "Failed to get badges");
226             }
227
228             return list;
229         }
230
231         private static void OnChangedEvent(Interop.Badge.Action action, string appId, uint count, IntPtr userData)
232         {
233             uint display = 0;
234             uint countLocal = 0;
235
236             switch (action)
237             {
238                 case Interop.Badge.Action.Create:
239                     s_changed?.Invoke(null, new BadgeEventArgs()
240                     {
241                         Reason = BadgeEventArgs.Action.Add,
242                         Badge = new Badge(appId, 0, false)
243                     });
244                     break;
245
246                 case Interop.Badge.Action.Remove:
247                     s_changed?.Invoke(null, new BadgeEventArgs()
248                     {
249                         Reason = BadgeEventArgs.Action.Remove,
250                         Badge = new Badge(appId, 0, false)
251                     });
252                     break;
253
254                 case Interop.Badge.Action.Update:
255                     Interop.Badge.GetDisplay(appId, out display);
256                     s_changed?.Invoke(null, new BadgeEventArgs()
257                     {
258                         Reason = BadgeEventArgs.Action.Update,
259                         Badge = new Badge(appId, (int)count, display == 0 ? false : true)
260                     });
261                     break;
262
263                 case Interop.Badge.Action.ChangedDisplay:
264                     Interop.Badge.GetCount(appId, out countLocal);
265                     s_changed?.Invoke(null, new BadgeEventArgs()
266                     {
267                         Reason = BadgeEventArgs.Action.Update,
268                         Badge = new Badge(appId, (int)countLocal, count == 0 ? false : true)
269                     });
270                     break;
271
272                 case Interop.Badge.Action.ServiceReady:
273                     // Ignore
274                     break;
275             }
276         }
277     }
278 }