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 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 /// <feature>http://tizen.org/feature/badge</feature>
35 /// <privilege>http://tizen.org/privilege/notification</privilege>
36 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
37 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access.</exception>
38 /// <exception cref="NotSupportedException">Thrown when Badge is not supported.</exception>
39 public static event EventHandler<BadgeEventArgs> Changed
43 if (s_changed == null && !s_registered)
45 if (s_callback == null)
47 s_callback = new Interop.Badge.ChangedCallback(OnChangedEvent);
50 BadgeError err = Interop.Badge.SetChangedCallback(s_callback, IntPtr.Zero);
51 if (err != BadgeError.None)
53 throw BadgeErrorFactory.GetException(err, "Failed to add event handler");
64 if (s_changed == null && s_registered)
66 BadgeError err = Interop.Badge.UnsetChangedCallback(s_callback);
67 if (err != BadgeError.None)
69 throw BadgeErrorFactory.GetException(err, "Failed to remove event handler");
79 /// Gets the badge information from the application ID.
81 /// <since_tizen> 3 </since_tizen>
82 /// <param name="appId">Application ID.</param>
83 /// <returns>The Badge object with inputted application ID</returns>
84 /// <feature>http://tizen.org/feature/badge</feature>
85 /// <privilege>http://tizen.org/privilege/notification</privilege>
86 /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
87 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access.</exception>
88 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
89 /// <exception cref="NotSupportedException">Thrown when Badge is not supported.</exception>
90 public static Badge Find(string appId)
95 BadgeError err = Interop.Badge.GetCount(appId, out count);
96 if (err != BadgeError.None)
98 throw BadgeErrorFactory.GetException(err, "Failed to find badge count of " + appId);
101 err = Interop.Badge.GetDisplay(appId, out display);
102 if (err != BadgeError.None)
104 throw BadgeErrorFactory.GetException(err, "Failed to find badge display of " + appId);
107 return new Badge(appId, (int)count, display == 0 ? false : true);
111 /// Removes the badge information.
113 /// <since_tizen> 3 </since_tizen>
114 /// <param name="appId">Application ID.</param>
115 /// <feature>http://tizen.org/feature/badge</feature>
116 /// <privilege>http://tizen.org/privilege/notification</privilege>
117 /// <exception cref="ArgumentException">Thrown when failed because of a an invalid argument.</exception>
118 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access.</exception>
119 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
120 /// <exception cref="NotSupportedException">Thrown when Badge is not supported.</exception>
121 public static void Remove(string appId)
123 BadgeError err = Interop.Badge.Remove(appId);
124 if (err != BadgeError.None)
126 throw BadgeErrorFactory.GetException(err, "Failed to Remove badge of " + appId);
131 /// Removes the badge information.
133 /// <since_tizen> 3 </since_tizen>
134 /// <param name="badge">The Badge object.</param>
135 /// <feature>http://tizen.org/feature/badge</feature>
136 /// <privilege>http://tizen.org/privilege/notification</privilege>
137 /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
138 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access.</exception>
139 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
140 /// <exception cref="NotSupportedException">Thrown when Badge is not supported.</exception>
141 public static void Remove(Badge badge)
145 throw BadgeErrorFactory.GetException(BadgeError.InvalidParameter, "Invalid Badge object");
152 /// Adds the badge information.
154 /// <since_tizen> 3 </since_tizen>
155 /// <param name="badge">The Badge object.</param>
156 /// <feature>http://tizen.org/feature/badge</feature>
157 /// <privilege>http://tizen.org/privilege/notification</privilege>
158 /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
159 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access.</exception>
160 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
161 /// <exception cref="NotSupportedException">Thrown when Badge is not supported.</exception>
162 public static void Add(Badge badge)
166 throw BadgeErrorFactory.GetException(BadgeError.InvalidParameter, "Invalid Badge object");
169 BadgeError err = Interop.Badge.Add(badge.AppId);
170 if (err != BadgeError.None)
172 throw BadgeErrorFactory.GetException(err, "Failed to add badge of " + badge.AppId);
187 /// Updates the badge information.
189 /// <since_tizen> 3 </since_tizen>
190 /// <param name="badge">The Badge object.</param>
191 /// <feature>http://tizen.org/feature/badge</feature>
192 /// <privilege>http://tizen.org/privilege/notification</privilege>
193 /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
194 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access.</exception>
195 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
196 /// <exception cref="NotSupportedException">Thrown when Badge is not supported.</exception>
197 public static void Update(Badge badge)
201 throw BadgeErrorFactory.GetException(BadgeError.InvalidParameter, "Invalid Badge object");
204 BadgeError err = Interop.Badge.SetCount(badge.AppId, (uint)badge.Count);
205 if (err != BadgeError.None)
207 throw BadgeErrorFactory.GetException(err, "Failed to update badge of " + badge.AppId);
210 err = Interop.Badge.SetDisplay(badge.AppId, badge.Visible ? 1U : 0U);
211 if (err != BadgeError.None)
213 throw BadgeErrorFactory.GetException(err, "Failed to update badge of " + badge.AppId);
218 /// Gets all the badge information.
220 /// <since_tizen> 3 </since_tizen>
221 /// <returns>List of all Badge instances.</returns>
222 /// <feature>http://tizen.org/feature/badge</feature>
223 /// <privilege>http://tizen.org/privilege/notification</privilege>
224 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access.</exception>
225 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
226 /// <exception cref="NotSupportedException">Thrown when Badge is not supported.</exception>
227 public static IEnumerable<Badge> GetBadges()
229 IList<Badge> list = new List<Badge>();
231 BadgeError err = Interop.Badge.Foreach((appId, count, userData) =>
234 BadgeError errGetDisplay = Interop.Badge.GetDisplay(appId, out display);
235 if (errGetDisplay != BadgeError.None)
237 throw BadgeErrorFactory.GetException(errGetDisplay, "Failed to get badges ");
240 list.Add(new Badge(appId, (int)count, display == 0 ? false : true));
243 if (err != BadgeError.None)
245 throw BadgeErrorFactory.GetException(err, "Failed to get badges");
251 private static void OnChangedEvent(Interop.Badge.Action action, string appId, uint count, IntPtr userData)
258 case Interop.Badge.Action.Create:
259 s_changed?.Invoke(null, new BadgeEventArgs()
261 Reason = BadgeEventArgs.Action.Add,
262 Badge = new Badge(appId, 0, false)
266 case Interop.Badge.Action.Remove:
267 s_changed?.Invoke(null, new BadgeEventArgs()
269 Reason = BadgeEventArgs.Action.Remove,
270 Badge = new Badge(appId, 0, false)
274 case Interop.Badge.Action.Update:
275 Interop.Badge.GetDisplay(appId, out display);
276 s_changed?.Invoke(null, new BadgeEventArgs()
278 Reason = BadgeEventArgs.Action.Update,
279 Badge = new Badge(appId, (int)count, display == 0 ? false : true)
283 case Interop.Badge.Action.ChangedDisplay:
284 Interop.Badge.GetCount(appId, out countLocal);
285 s_changed?.Invoke(null, new BadgeEventArgs()
287 Reason = BadgeEventArgs.Action.Update,
288 Badge = new Badge(appId, (int)countLocal, count == 0 ? false : true)
292 case Interop.Badge.Action.ServiceReady: