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