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.
18 using System.Collections.Generic;
20 namespace Tizen.Applications
23 /// Class for badge operation.
25 public static class BadgeControl
27 private static event EventHandler<BadgeEventArgs> s_changed;
28 private static bool s_registered = false;
29 private static Interop.Badge.ChangedCallback s_callback;
32 /// Event handler for receiving badge events.
34 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
35 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
36 /// <privilege>http://tizen.org/privilege/notification</privilege>
37 public static event EventHandler<BadgeEventArgs> Changed
41 if (s_changed == null && !s_registered)
43 if (s_callback == null)
45 s_callback = new Interop.Badge.ChangedCallback(OnChangedEvent);
48 Interop.Badge.ErrorCode err = Interop.Badge.SetChangedCallback(s_callback, IntPtr.Zero);
52 case Interop.Badge.ErrorCode.InvalidParameter:
53 throw new InvalidOperationException("Invalid parameter at unmanaged code");
55 case Interop.Badge.ErrorCode.PermissionDenied:
56 throw new UnauthorizedAccessException();
58 case Interop.Badge.ErrorCode.OutOfMemory:
59 throw new InvalidOperationException("Out-of-memory at unmanaged code");
69 if (s_changed == null && s_registered)
71 Interop.Badge.ErrorCode err = Interop.Badge.UnsetChangedCallback(s_callback);
75 case Interop.Badge.ErrorCode.InvalidParameter:
76 throw new InvalidOperationException("Invalid parameter at unmanaged code");
78 case Interop.Badge.ErrorCode.PermissionDenied:
79 throw new UnauthorizedAccessException();
81 case Interop.Badge.ErrorCode.NotExist:
82 throw new InvalidOperationException("Not exist");
92 /// Gets the badge information from application ID.
94 /// <param name="appId">Application ID</param>
95 /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
96 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
97 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
98 /// <privilege>http://tizen.org/privilege/notification</privilege>
99 public static Badge Find(string appId)
104 Interop.Badge.ErrorCode err = Interop.Badge.GetCount(appId, out count);
108 case Interop.Badge.ErrorCode.InvalidParameter:
109 throw new ArgumentException("Invalid parameter");
111 case Interop.Badge.ErrorCode.PermissionDenied:
112 throw new UnauthorizedAccessException();
114 case Interop.Badge.ErrorCode.FromDb:
115 throw new InvalidOperationException("Error from DB");
117 case Interop.Badge.ErrorCode.OutOfMemory:
118 throw new InvalidOperationException("Out-of-memory at unmanaged code");
121 err = Interop.Badge.GetDisplay(appId, out display);
125 case Interop.Badge.ErrorCode.InvalidParameter:
126 throw new ArgumentException("Invalid parameter");
128 case Interop.Badge.ErrorCode.PermissionDenied:
129 throw new UnauthorizedAccessException();
131 case Interop.Badge.ErrorCode.FromDb:
132 throw new InvalidOperationException("Error from DB");
134 case Interop.Badge.ErrorCode.OutOfMemory:
135 throw new InvalidOperationException("Out-of-memory at unmanaged code");
137 case Interop.Badge.ErrorCode.NotExist:
138 throw new InvalidOperationException("Not exist");
141 return new Badge(appId, (int)count, display == 0 ? false : true);
145 /// Removes the badge information.
147 /// <param name="appId">Application ID</param>
148 /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
149 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
150 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
151 /// <privilege>http://tizen.org/privilege/notification</privilege>
152 public static void Remove(string appId)
154 Interop.Badge.ErrorCode err = Interop.Badge.Remove(appId);
158 case Interop.Badge.ErrorCode.InvalidParameter:
159 throw new ArgumentException("Invalid parameter");
161 case Interop.Badge.ErrorCode.PermissionDenied:
162 throw new UnauthorizedAccessException();
164 case Interop.Badge.ErrorCode.IoError:
165 throw new InvalidOperationException("Error from I/O");
167 case Interop.Badge.ErrorCode.ServiceNotReady:
168 throw new InvalidOperationException("Service is not ready");
170 case Interop.Badge.ErrorCode.NotExist:
171 throw new InvalidOperationException("Not exist");
176 /// Adds the badge information.
178 /// <param name="appId">Application ID</param>
179 /// <param name="count">Count value</param>
180 /// <param name="isDisplay">True if it should be displayed</param>
181 /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
182 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
183 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
184 /// <privilege>http://tizen.org/privilege/notification</privilege>
185 public static void Add(string appId, int count = 1, bool isDisplay = true)
187 Interop.Badge.ErrorCode err = Interop.Badge.Add(appId);
191 case Interop.Badge.ErrorCode.InvalidParameter:
192 throw new ArgumentException("Invalid parameter");
194 case Interop.Badge.ErrorCode.PermissionDenied:
195 throw new UnauthorizedAccessException();
197 case Interop.Badge.ErrorCode.IoError:
198 throw new InvalidOperationException("Error from I/O");
200 case Interop.Badge.ErrorCode.ServiceNotReady:
201 throw new InvalidOperationException("Service is not ready");
203 case Interop.Badge.ErrorCode.InvalidPackage:
204 throw new InvalidOperationException("The caller application is not signed with the certificate of badge application ID");
209 Update(appId, count, isDisplay);
219 /// Updates the badge information.
221 /// <param name="appId">Application ID</param>
222 /// <param name="count">Count value</param>
223 /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
224 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
225 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
226 /// <privilege>http://tizen.org/privilege/notification</privilege>
227 public static void Update(string appId, int count)
229 Interop.Badge.ErrorCode err = Interop.Badge.SetCount(appId, (uint)count);
233 case Interop.Badge.ErrorCode.InvalidParameter:
234 throw new ArgumentException("Invalid parameter");
236 case Interop.Badge.ErrorCode.PermissionDenied:
237 throw new UnauthorizedAccessException();
239 case Interop.Badge.ErrorCode.IoError:
240 throw new InvalidOperationException("Error from I/O");
242 case Interop.Badge.ErrorCode.ServiceNotReady:
243 throw new InvalidOperationException("Service is not ready");
248 /// Updates the badge information.
250 /// <param name="appId">Application ID</param>
251 /// <param name="isDisplay">True if it should be displayed</param>
252 /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
253 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
254 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
255 /// <privilege>http://tizen.org/privilege/notification</privilege>
256 public static void Update(string appId, bool isDisplay)
258 Interop.Badge.ErrorCode err = Interop.Badge.SetDisplay(appId, isDisplay ? 1U : 0U);
262 case Interop.Badge.ErrorCode.InvalidParameter:
263 throw new ArgumentException("Invalid parameter");
265 case Interop.Badge.ErrorCode.PermissionDenied:
266 throw new UnauthorizedAccessException();
268 case Interop.Badge.ErrorCode.IoError:
269 throw new InvalidOperationException("Error from I/O");
271 case Interop.Badge.ErrorCode.ServiceNotReady:
272 throw new InvalidOperationException("Service is not ready");
277 /// Updates the badge information.
279 /// <param name="appId">Application ID</param>
280 /// <param name="count">Count value</param>
281 /// <param name="isDisplay">True if it should be displayed</param>
282 /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
283 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
284 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
285 /// <privilege>http://tizen.org/privilege/notification</privilege>
286 public static void Update(string appId, int count, bool isDisplay)
288 Update(appId, count);
289 Update(appId, isDisplay);
293 /// Gets all badge information.
295 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
296 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
297 /// <privilege>http://tizen.org/privilege/notification</privilege>
298 public static IEnumerable<Badge> GetBadges()
300 IList<Badge> list = new List<Badge>();
302 Interop.Badge.ErrorCode err = Interop.Badge.Foreach((appId, count, userData) =>
305 Interop.Badge.ErrorCode e = Interop.Badge.GetDisplay(appId, out display);
308 case Interop.Badge.ErrorCode.InvalidParameter:
309 throw new InvalidOperationException("Invalid parameter at unmanaged code");
311 case Interop.Badge.ErrorCode.PermissionDenied:
312 throw new UnauthorizedAccessException();
314 case Interop.Badge.ErrorCode.FromDb:
315 throw new InvalidOperationException("Error from DB");
317 case Interop.Badge.ErrorCode.OutOfMemory:
318 throw new InvalidOperationException("Out-of-memory at unmanaged code");
320 case Interop.Badge.ErrorCode.NotExist:
321 throw new InvalidOperationException("Not exist");
323 case Interop.Badge.ErrorCode.ServiceNotReady:
324 throw new InvalidOperationException("Service is not ready");
327 list.Add(new Badge(appId, (int)count, display == 0 ? false : true));
332 case Interop.Badge.ErrorCode.InvalidParameter:
333 throw new InvalidOperationException("Invalid parameter at unmanaged code");
335 case Interop.Badge.ErrorCode.PermissionDenied:
336 throw new UnauthorizedAccessException();
338 case Interop.Badge.ErrorCode.FromDb:
339 throw new InvalidOperationException("Error from DB");
341 case Interop.Badge.ErrorCode.OutOfMemory:
342 throw new InvalidOperationException("Out-of-memory at unmanaged code");
344 case Interop.Badge.ErrorCode.NotExist:
345 throw new InvalidOperationException("Not exist");
351 private static void OnChangedEvent(Interop.Badge.Action action, string appId, uint count, IntPtr userData)
358 case Interop.Badge.Action.Create:
359 s_changed?.Invoke(null, new BadgeEventArgs()
361 Reason = BadgeEventArgs.Action.Add,
362 Badge = new Badge(appId, 0, false)
366 case Interop.Badge.Action.Remove:
367 s_changed?.Invoke(null, new BadgeEventArgs()
369 Reason = BadgeEventArgs.Action.Remove,
370 Badge = new Badge(appId, 0, false)
374 case Interop.Badge.Action.Update:
375 Interop.Badge.GetDisplay(appId, out display);
376 s_changed?.Invoke(null, new BadgeEventArgs()
378 Reason = BadgeEventArgs.Action.Update,
379 Badge = new Badge(appId, (int)count, display == 0 ? false : true)
383 case Interop.Badge.Action.ChangedDisplay:
384 Interop.Badge.GetCount(appId, out countLocal);
385 s_changed?.Invoke(null, new BadgeEventArgs()
387 Reason = BadgeEventArgs.Action.Update,
388 Badge = new Badge(appId, (int)countLocal, count == 0 ? false : true)
392 case Interop.Badge.Action.ServiceReady: