2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 namespace Tizen.Applications
20 using System.Collections.Generic;
22 /// Class for badge operation.
24 public static class BadgeControl
26 private static event EventHandler<BadgeEventArgs> s_changed;
27 private static bool s_registered = false;
28 private static Interop.Badge.ChangedCallback s_callback;
31 /// Event handler for receiving badge events.
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
40 if (s_changed == null && !s_registered)
42 if (s_callback == null)
44 s_callback = new Interop.Badge.ChangedCallback(OnChangedEvent);
47 Interop.Badge.ErrorCode err = Interop.Badge.SetChangedCallback(s_callback, IntPtr.Zero);
51 case Interop.Badge.ErrorCode.InvalidParameter:
52 throw new InvalidOperationException("Invalid parameter at unmanaged code");
54 case Interop.Badge.ErrorCode.PermissionDenied:
55 throw new UnauthorizedAccessException();
57 case Interop.Badge.ErrorCode.OutOfMemory:
58 throw new InvalidOperationException("Out-of-memory at unmanaged code");
68 if (s_changed == null && s_registered)
70 Interop.Badge.ErrorCode err = Interop.Badge.UnsetChangedCallback(s_callback);
74 case Interop.Badge.ErrorCode.InvalidParameter:
75 throw new InvalidOperationException("Invalid parameter at unmanaged code");
77 case Interop.Badge.ErrorCode.PermissionDenied:
78 throw new UnauthorizedAccessException();
80 case Interop.Badge.ErrorCode.NotExist:
81 throw new InvalidOperationException("Not exist");
91 /// Gets the badge information from application ID.
93 /// <param name="appId">Application ID</param>
94 /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
95 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
96 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
97 /// <privilege>http://tizen.org/privilege/notification</privilege>
98 public static Badge Find(string appId)
103 Interop.Badge.ErrorCode err = Interop.Badge.GetCount(appId, out count);
107 case Interop.Badge.ErrorCode.InvalidParameter:
108 throw new ArgumentException("Invalid parameter");
110 case Interop.Badge.ErrorCode.PermissionDenied:
111 throw new UnauthorizedAccessException();
113 case Interop.Badge.ErrorCode.FromDb:
114 throw new InvalidOperationException("Error from DB");
116 case Interop.Badge.ErrorCode.OutOfMemory:
117 throw new InvalidOperationException("Out-of-memory at unmanaged code");
120 err = Interop.Badge.GetDisplay(appId, out display);
124 case Interop.Badge.ErrorCode.InvalidParameter:
125 throw new ArgumentException("Invalid parameter");
127 case Interop.Badge.ErrorCode.PermissionDenied:
128 throw new UnauthorizedAccessException();
130 case Interop.Badge.ErrorCode.FromDb:
131 throw new InvalidOperationException("Error from DB");
133 case Interop.Badge.ErrorCode.OutOfMemory:
134 throw new InvalidOperationException("Out-of-memory at unmanaged code");
136 case Interop.Badge.ErrorCode.NotExist:
137 throw new InvalidOperationException("Not exist");
140 return new Badge(appId, (int)count, display == 0 ? false : true);
144 /// Removes the badge information.
146 /// <param name="appId">Application ID</param>
147 /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
148 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
149 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
150 /// <privilege>http://tizen.org/privilege/notification</privilege>
151 public static void Remove(string appId)
153 Interop.Badge.ErrorCode err = Interop.Badge.Remove(appId);
157 case Interop.Badge.ErrorCode.InvalidParameter:
158 throw new ArgumentException("Invalid parameter");
160 case Interop.Badge.ErrorCode.PermissionDenied:
161 throw new UnauthorizedAccessException();
163 case Interop.Badge.ErrorCode.IoError:
164 throw new InvalidOperationException("Error from I/O");
166 case Interop.Badge.ErrorCode.ServiceNotReady:
167 throw new InvalidOperationException("Service is not ready");
169 case Interop.Badge.ErrorCode.NotExist:
170 throw new InvalidOperationException("Not exist");
175 /// Adds the badge information.
177 /// <param name="appId">Application ID</param>
178 /// <param name="count">Count value</param>
179 /// <param name="isDisplay">True if it should be displayed</param>
180 /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
181 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
182 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
183 /// <privilege>http://tizen.org/privilege/notification</privilege>
184 public static void Add(string appId, int count = 1, bool isDisplay = true)
186 Interop.Badge.ErrorCode err = Interop.Badge.Add(appId);
190 case Interop.Badge.ErrorCode.InvalidParameter:
191 throw new ArgumentException("Invalid parameter");
193 case Interop.Badge.ErrorCode.PermissionDenied:
194 throw new UnauthorizedAccessException();
196 case Interop.Badge.ErrorCode.IoError:
197 throw new InvalidOperationException("Error from I/O");
199 case Interop.Badge.ErrorCode.ServiceNotReady:
200 throw new InvalidOperationException("Service is not ready");
202 case Interop.Badge.ErrorCode.InvalidPackage:
203 throw new InvalidOperationException("The caller application is not signed with the certificate of badge application ID");
208 Update(appId, count, isDisplay);
218 /// Updates the badge information.
220 /// <param name="appId">Application ID</param>
221 /// <param name="count">Count value</param>
222 /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
223 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
224 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
225 /// <privilege>http://tizen.org/privilege/notification</privilege>
226 public static void Update(string appId, int count)
228 Interop.Badge.ErrorCode err = Interop.Badge.SetCount(appId, (uint)count);
232 case Interop.Badge.ErrorCode.InvalidParameter:
233 throw new ArgumentException("Invalid parameter");
235 case Interop.Badge.ErrorCode.PermissionDenied:
236 throw new UnauthorizedAccessException();
238 case Interop.Badge.ErrorCode.IoError:
239 throw new InvalidOperationException("Error from I/O");
241 case Interop.Badge.ErrorCode.ServiceNotReady:
242 throw new InvalidOperationException("Service is not ready");
247 /// Updates the badge information.
249 /// <param name="appId">Application ID</param>
250 /// <param name="isDisplay">True if it should be displayed</param>
251 /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
252 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
253 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
254 /// <privilege>http://tizen.org/privilege/notification</privilege>
255 public static void Update(string appId, bool isDisplay)
257 Interop.Badge.ErrorCode err = Interop.Badge.SetDisplay(appId, isDisplay ? 1U : 0U);
261 case Interop.Badge.ErrorCode.InvalidParameter:
262 throw new ArgumentException("Invalid parameter");
264 case Interop.Badge.ErrorCode.PermissionDenied:
265 throw new UnauthorizedAccessException();
267 case Interop.Badge.ErrorCode.IoError:
268 throw new InvalidOperationException("Error from I/O");
270 case Interop.Badge.ErrorCode.ServiceNotReady:
271 throw new InvalidOperationException("Service is not ready");
276 /// Updates the badge information.
278 /// <param name="appId">Application ID</param>
279 /// <param name="count">Count value</param>
280 /// <param name="isDisplay">True if it should be displayed</param>
281 /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
282 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
283 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
284 /// <privilege>http://tizen.org/privilege/notification</privilege>
285 public static void Update(string appId, int count, bool isDisplay)
287 Update(appId, count);
288 Update(appId, isDisplay);
292 /// Gets all badge information.
294 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
295 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
296 /// <privilege>http://tizen.org/privilege/notification</privilege>
297 public static IEnumerable<Badge> GetBadges()
299 IList<Badge> list = new List<Badge>();
301 Interop.Badge.ErrorCode err = Interop.Badge.Foreach((appId, count, userData) =>
304 Interop.Badge.ErrorCode e = Interop.Badge.GetDisplay(appId, out display);
307 case Interop.Badge.ErrorCode.InvalidParameter:
308 throw new InvalidOperationException("Invalid parameter at unmanaged code");
310 case Interop.Badge.ErrorCode.PermissionDenied:
311 throw new UnauthorizedAccessException();
313 case Interop.Badge.ErrorCode.FromDb:
314 throw new InvalidOperationException("Error from DB");
316 case Interop.Badge.ErrorCode.OutOfMemory:
317 throw new InvalidOperationException("Out-of-memory at unmanaged code");
319 case Interop.Badge.ErrorCode.NotExist:
320 throw new InvalidOperationException("Not exist");
322 case Interop.Badge.ErrorCode.ServiceNotReady:
323 throw new InvalidOperationException("Service is not ready");
326 list.Add(new Badge(appId, (int)count, display == 0 ? false : true));
331 case Interop.Badge.ErrorCode.InvalidParameter:
332 throw new InvalidOperationException("Invalid parameter at unmanaged code");
334 case Interop.Badge.ErrorCode.PermissionDenied:
335 throw new UnauthorizedAccessException();
337 case Interop.Badge.ErrorCode.FromDb:
338 throw new InvalidOperationException("Error from DB");
340 case Interop.Badge.ErrorCode.OutOfMemory:
341 throw new InvalidOperationException("Out-of-memory at unmanaged code");
343 case Interop.Badge.ErrorCode.NotExist:
344 throw new InvalidOperationException("Not exist");
350 private static void OnChangedEvent(Interop.Badge.Action action, string appId, uint count, IntPtr userData)
357 case Interop.Badge.Action.Create:
358 s_changed?.Invoke(null, new BadgeEventArgs()
360 Reason = BadgeEventArgs.Action.Add,
361 Badge = new Badge(appId, 0, false)
365 case Interop.Badge.Action.Remove:
366 s_changed?.Invoke(null, new BadgeEventArgs()
368 Reason = BadgeEventArgs.Action.Remove,
369 Badge = new Badge(appId, 0, false)
373 case Interop.Badge.Action.Update:
374 Interop.Badge.GetDisplay(appId, out display);
375 s_changed?.Invoke(null, new BadgeEventArgs()
377 Reason = BadgeEventArgs.Action.Update,
378 Badge = new Badge(appId, (int)count, display == 0 ? false : true)
382 case Interop.Badge.Action.ChangedDisplay:
383 Interop.Badge.GetCount(appId, out countLocal);
384 s_changed?.Invoke(null, new BadgeEventArgs()
386 Reason = BadgeEventArgs.Action.Update,
387 Badge = new Badge(appId, (int)countLocal, count == 0 ? false : true)
391 case Interop.Badge.Action.ServiceReady: