2 * Copyright (c) 2016 - 2017 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 /// <since_tizen> 3 </since_tizen>
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 /// <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
44 if (s_changed == null && !s_registered)
46 if (s_callback == null)
48 s_callback = new Interop.Badge.ChangedCallback(OnChangedEvent);
51 BadgeError err = Interop.Badge.SetChangedCallback(s_callback, IntPtr.Zero);
52 if (err != BadgeError.None)
54 throw BadgeErrorFactory.GetException(err, "Failed to add event handler");
65 if (s_changed == null && s_registered)
67 BadgeError err = Interop.Badge.UnsetChangedCallback(s_callback);
68 if (err != BadgeError.None)
70 throw BadgeErrorFactory.GetException(err, "Failed to remove event handler");
80 /// Gets the badge information from the application ID.
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)
96 BadgeError err = Interop.Badge.GetCount(appId, out count);
97 if (err != BadgeError.None)
99 throw BadgeErrorFactory.GetException(err, "Failed to find badge count of " + appId);
102 err = Interop.Badge.GetDisplay(appId, out display);
103 if (err != BadgeError.None)
105 throw BadgeErrorFactory.GetException(err, "Failed to find badge display of " + appId);
108 return new Badge(appId, (int)count, display == 0 ? false : true);
112 /// Removes the badge information.
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)
124 BadgeError err = Interop.Badge.Remove(appId);
125 if (err != BadgeError.None)
127 throw BadgeErrorFactory.GetException(err, "Failed to Remove badge of " + appId);
132 /// Removes the badge information.
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)
146 throw BadgeErrorFactory.GetException(BadgeError.InvalidParameter, "Invalid Badge object");
153 /// Adds the badge information.
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)
167 throw BadgeErrorFactory.GetException(BadgeError.InvalidParameter, "Invalid Badge object");
170 BadgeError err = Interop.Badge.Add(badge.AppId);
171 if (err != BadgeError.None)
173 throw BadgeErrorFactory.GetException(err, "Failed to add badge of " + badge.AppId);
188 /// Updates the badge information.
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)
202 throw BadgeErrorFactory.GetException(BadgeError.InvalidParameter, "Invalid Badge object");
205 BadgeError err = Interop.Badge.SetCount(badge.AppId, (uint)badge.Count);
206 if (err != BadgeError.None)
208 throw BadgeErrorFactory.GetException(err, "Failed to update badge of " + badge.AppId);
211 err = Interop.Badge.SetDisplay(badge.AppId, badge.Visible ? 1U : 0U);
212 if (err != BadgeError.None)
214 throw BadgeErrorFactory.GetException(err, "Failed to update badge of " + badge.AppId);
219 /// Gets all the badge information.
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()
230 IList<Badge> list = new List<Badge>();
232 BadgeError err = Interop.Badge.Foreach((appId, count, userData) =>
235 BadgeError errGetDisplay = Interop.Badge.GetDisplay(appId, out display);
236 if (errGetDisplay != BadgeError.None)
238 throw BadgeErrorFactory.GetException(errGetDisplay, "Failed to get badges ");
241 list.Add(new Badge(appId, (int)count, display == 0 ? false : true));
246 if (err != BadgeError.None)
248 throw BadgeErrorFactory.GetException(err, "Failed to get badges");
254 private static void OnChangedEvent(Interop.Badge.Action action, string appId, uint count, IntPtr userData)
261 case Interop.Badge.Action.Create:
262 s_changed?.Invoke(null, new BadgeEventArgs()
264 Reason = BadgeEventArgs.Action.Add,
265 Badge = new Badge(appId, 0, false)
269 case Interop.Badge.Action.Remove:
270 s_changed?.Invoke(null, new BadgeEventArgs()
272 Reason = BadgeEventArgs.Action.Remove,
273 Badge = new Badge(appId, 0, false)
277 case Interop.Badge.Action.Update:
278 Interop.Badge.GetDisplay(appId, out display);
279 s_changed?.Invoke(null, new BadgeEventArgs()
281 Reason = BadgeEventArgs.Action.Update,
282 Badge = new Badge(appId, (int)count, display == 0 ? false : true)
286 case Interop.Badge.Action.ChangedDisplay:
287 Interop.Badge.GetCount(appId, out countLocal);
288 s_changed?.Invoke(null, new BadgeEventArgs()
290 Reason = BadgeEventArgs.Action.Update,
291 Badge = new Badge(appId, (int)countLocal, count == 0 ? false : true)
295 case Interop.Badge.Action.ServiceReady: