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