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 /// The 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 /// <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
41 if (s_changed == null && !s_registered)
43 if (s_callback == null)
45 s_callback = new Interop.Badge.ChangedCallback(OnChangedEvent);
48 BadgeError err = Interop.Badge.SetChangedCallback(s_callback, IntPtr.Zero);
49 if (err != BadgeError.None)
51 throw BadgeErrorFactory.GetException(err, "Failed to add event handler");
62 if (s_changed == null && s_registered)
64 BadgeError err = Interop.Badge.UnsetChangedCallback(s_callback);
65 if (err != BadgeError.None)
67 throw BadgeErrorFactory.GetException(err, "Failed to remove event handler");
77 /// Gets the badge information from the application ID.
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)
90 BadgeError err = Interop.Badge.GetCount(appId, out count);
91 if (err != BadgeError.None)
93 throw BadgeErrorFactory.GetException(err, "Failed to find badge count of " + appId);
96 err = Interop.Badge.GetDisplay(appId, out display);
97 if (err != BadgeError.None)
99 throw BadgeErrorFactory.GetException(err, "Failed to find badge display of " + appId);
102 return new Badge(appId, (int)count, display == 0 ? false : true);
106 /// Removes the badge information.
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)
116 BadgeError err = Interop.Badge.Remove(appId);
117 if (err != BadgeError.None)
119 throw BadgeErrorFactory.GetException(err, "Failed to Remove badge of " + appId);
124 /// Adds the badge information.
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)
136 BadgeError err = Interop.Badge.Add(appId);
137 if (err != BadgeError.None)
139 throw BadgeErrorFactory.GetException(err, "Failed to add badge of " + appId);
144 Update(appId, count, isDisplay);
154 /// Updates the badge information.
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)
165 BadgeError err = Interop.Badge.SetCount(appId, (uint)count);
166 if (err != BadgeError.None)
168 throw BadgeErrorFactory.GetException(err, "Failed to update badge of " + appId);
173 /// Updates the badge information.
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)
184 BadgeError err = Interop.Badge.SetDisplay(appId, isDisplay ? 1U : 0U);
185 if (err != BadgeError.None)
187 throw BadgeErrorFactory.GetException(err, "Failed to update badge of " + appId);
192 /// Updates the badge information.
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)
204 Update(appId, count);
205 Update(appId, isDisplay);
209 /// Gets all the badge information.
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()
217 IList<Badge> list = new List<Badge>();
219 BadgeError err = Interop.Badge.Foreach((appId, count, userData) =>
222 BadgeError errGetDisplay = Interop.Badge.GetDisplay(appId, out display);
223 if (errGetDisplay != BadgeError.None)
225 throw BadgeErrorFactory.GetException(errGetDisplay, "Failed to get badges ");
228 list.Add(new Badge(appId, (int)count, display == 0 ? false : true));
231 if (err != BadgeError.None)
233 throw BadgeErrorFactory.GetException(err, "Failed to get badges");
239 private static void OnChangedEvent(Interop.Badge.Action action, string appId, uint count, IntPtr userData)
246 case Interop.Badge.Action.Create:
247 s_changed?.Invoke(null, new BadgeEventArgs()
249 Reason = BadgeEventArgs.Action.Add,
250 Badge = new Badge(appId, 0, false)
254 case Interop.Badge.Action.Remove:
255 s_changed?.Invoke(null, new BadgeEventArgs()
257 Reason = BadgeEventArgs.Action.Remove,
258 Badge = new Badge(appId, 0, false)
262 case Interop.Badge.Action.Update:
263 Interop.Badge.GetDisplay(appId, out display);
264 s_changed?.Invoke(null, new BadgeEventArgs()
266 Reason = BadgeEventArgs.Action.Update,
267 Badge = new Badge(appId, (int)count, display == 0 ? false : true)
271 case Interop.Badge.Action.ChangedDisplay:
272 Interop.Badge.GetCount(appId, out countLocal);
273 s_changed?.Invoke(null, new BadgeEventArgs()
275 Reason = BadgeEventArgs.Action.Update,
276 Badge = new Badge(appId, (int)countLocal, count == 0 ? false : true)
280 case Interop.Badge.Action.ServiceReady: