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 BadgeError err = Interop.Badge.SetChangedCallback(s_callback, IntPtr.Zero);
48 if (err != BadgeError.None)
50 throw BadgeErrorFactory.GetException(err, "Failed to add event handler");
61 if (s_changed == null && s_registered)
63 BadgeError err = Interop.Badge.UnsetChangedCallback(s_callback);
64 if (err != BadgeError.None)
66 throw BadgeErrorFactory.GetException(err, "Failed to remove event handler");
76 /// Gets the badge information from application ID.
78 /// <param name="appId">Application ID</param>
79 /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
80 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
81 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
82 /// <privilege>http://tizen.org/privilege/notification</privilege>
83 public static Badge Find(string appId)
88 BadgeError err = Interop.Badge.GetCount(appId, out count);
89 if (err != BadgeError.None)
91 throw BadgeErrorFactory.GetException(err, "Failed to find badge count of " + appId);
94 err = Interop.Badge.GetDisplay(appId, out display);
95 if (err != BadgeError.None)
97 throw BadgeErrorFactory.GetException(err, "Failed to find badge display of " + appId);
100 return new Badge(appId, (int)count, display == 0 ? false : true);
104 /// Removes the badge information.
106 /// <param name="appId">Application ID</param>
107 /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
108 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
109 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
110 /// <privilege>http://tizen.org/privilege/notification</privilege>
111 public static void Remove(string appId)
113 BadgeError err = Interop.Badge.Remove(appId);
114 if (err != BadgeError.None)
116 throw BadgeErrorFactory.GetException(err, "Failed to Remove badge of " + appId);
121 /// Adds the badge information.
123 /// <param name="appId">Application ID</param>
124 /// <param name="count">Count value</param>
125 /// <param name="isDisplay">True if it should be displayed</param>
126 /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
127 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
128 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
129 /// <privilege>http://tizen.org/privilege/notification</privilege>
130 public static void Add(string appId, int count = 1, bool isDisplay = true)
132 BadgeError err = Interop.Badge.Add(appId);
133 if (err != BadgeError.None)
135 throw BadgeErrorFactory.GetException(err, "Failed to add badge of " + appId);
140 Update(appId, count, isDisplay);
150 /// Updates the badge information.
152 /// <param name="appId">Application ID</param>
153 /// <param name="count">Count value</param>
154 /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
155 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
156 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
157 /// <privilege>http://tizen.org/privilege/notification</privilege>
158 public static void Update(string appId, int count)
160 BadgeError err = Interop.Badge.SetCount(appId, (uint)count);
161 if (err != BadgeError.None)
163 throw BadgeErrorFactory.GetException(err, "Failed to update badge of " + appId);
168 /// Updates the badge information.
170 /// <param name="appId">Application ID</param>
171 /// <param name="isDisplay">True if it should be displayed</param>
172 /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
173 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
174 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
175 /// <privilege>http://tizen.org/privilege/notification</privilege>
176 public static void Update(string appId, bool isDisplay)
178 BadgeError err = Interop.Badge.SetDisplay(appId, isDisplay ? 1U : 0U);
179 if (err != BadgeError.None)
181 throw BadgeErrorFactory.GetException(err, "Failed to update badge of " + appId);
186 /// Updates the badge information.
188 /// <param name="appId">Application ID</param>
189 /// <param name="count">Count value</param>
190 /// <param name="isDisplay">True if it should be displayed</param>
191 /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
192 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
193 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
194 /// <privilege>http://tizen.org/privilege/notification</privilege>
195 public static void Update(string appId, int count, bool isDisplay)
197 Update(appId, count);
198 Update(appId, isDisplay);
202 /// Gets all badge information.
204 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
205 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
206 /// <privilege>http://tizen.org/privilege/notification</privilege>
207 public static IEnumerable<Badge> GetBadges()
209 IList<Badge> list = new List<Badge>();
211 BadgeError err = Interop.Badge.Foreach((appId, count, userData) =>
214 BadgeError errGetDisplay = Interop.Badge.GetDisplay(appId, out display);
215 if (errGetDisplay != BadgeError.None)
217 throw BadgeErrorFactory.GetException(errGetDisplay, "Failed to get badges ");
220 list.Add(new Badge(appId, (int)count, display == 0 ? false : true));
223 if (err != BadgeError.None)
225 throw BadgeErrorFactory.GetException(err, "Failed to get badges");
231 private static void OnChangedEvent(Interop.Badge.Action action, string appId, uint count, IntPtr userData)
238 case Interop.Badge.Action.Create:
239 s_changed?.Invoke(null, new BadgeEventArgs()
241 Reason = BadgeEventArgs.Action.Add,
242 Badge = new Badge(appId, 0, false)
246 case Interop.Badge.Action.Remove:
247 s_changed?.Invoke(null, new BadgeEventArgs()
249 Reason = BadgeEventArgs.Action.Remove,
250 Badge = new Badge(appId, 0, false)
254 case Interop.Badge.Action.Update:
255 Interop.Badge.GetDisplay(appId, out display);
256 s_changed?.Invoke(null, new BadgeEventArgs()
258 Reason = BadgeEventArgs.Action.Update,
259 Badge = new Badge(appId, (int)count, display == 0 ? false : true)
263 case Interop.Badge.Action.ChangedDisplay:
264 Interop.Badge.GetCount(appId, out countLocal);
265 s_changed?.Invoke(null, new BadgeEventArgs()
267 Reason = BadgeEventArgs.Action.Update,
268 Badge = new Badge(appId, (int)countLocal, count == 0 ? false : true)
272 case Interop.Badge.Action.ServiceReady: