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