2 * Copyright (c) 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.NotificationEventListener
20 using System.Collections.Generic;
21 using System.ComponentModel;
22 using System.Runtime.InteropServices;
25 /// This class provides a way to register callback function for some notification events.
28 /// The event listener can use this class to get a list of notification or to clear notifications.
30 public partial class NotificationListenerManager
32 private const string LogTag = "Tizen.Applications.NotificationEventListener";
34 private static event EventHandler<NotificationEventArgs> AddEventHandler;
36 private static event EventHandler<NotificationEventArgs> UpdateEventHandler;
38 private static event EventHandler<NotificationDeleteEventArgs> DeleteEventHandler;
40 private static Interop.NotificationEventListener.ChangedCallback callback;
42 [StructLayout(LayoutKind.Sequential)]
43 private struct NotificationOperation
45 NotificationOperationType type;
47 int extraInformation1;
48 int extraInformation2;
52 private static int GetEventHandleLength()
56 length += (DeleteEventHandler == null) ? 0 : DeleteEventHandler.GetInvocationList().Length;
57 length += (UpdateEventHandler == null) ? 0 : UpdateEventHandler.GetInvocationList().Length;
58 length += (AddEventHandler == null) ? 0 : AddEventHandler.GetInvocationList().Length;
64 /// Registers a callback for notification insert event.
66 /// <exception cref="ArgumentException">Thrown in case of Invalid parameter.</exception>
67 /// <exception cref="UnauthorizedAccessException"> Thrown in case of Permission deny.</exception>
68 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
69 /// <privilege>http://tizen.org/privilege/notification</privilege>
70 public static event EventHandler<NotificationEventArgs> NotificationAddEventHandler
76 callback = new Interop.NotificationEventListener.ChangedCallback(ChangedEvent);
79 if (GetEventHandleLength() == 0)
81 Interop.NotificationEventListener.ErrorCode err = Interop.NotificationEventListener.SetChangedCallback(callback, IntPtr.Zero);
82 if (err != (int)Interop.NotificationEventListener.ErrorCode.None)
84 throw NotificationEventListenerErrorFactory.GetException(err, "unable to set changed callback");
88 AddEventHandler += value;
93 if (AddEventHandler != null && AddEventHandler.GetInvocationList().Length > 0)
95 AddEventHandler -= value;
97 if (GetEventHandleLength() == 0)
99 Interop.NotificationEventListener.ErrorCode err = Interop.NotificationEventListener.UnsetChangedCallback(callback);
100 if (err != (int)Interop.NotificationEventListener.ErrorCode.None)
102 throw NotificationEventListenerErrorFactory.GetException(err, "unable to unset changed callback");
110 /// Registers a callback for notification update event.
112 /// <exception cref="ArgumentException">Thrown in case of Invalid parameter.</exception>
113 /// <exception cref="UnauthorizedAccessException"> Thrown in case of Permission deny.</exception>
114 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
115 /// <privilege>http://tizen.org/privilege/notification</privilege>
116 public static event EventHandler<NotificationEventArgs> NotificationUpdateEventHandler
120 if (callback == null)
122 callback = new Interop.NotificationEventListener.ChangedCallback(ChangedEvent);
125 if (GetEventHandleLength() == 0)
127 Interop.NotificationEventListener.ErrorCode err = Interop.NotificationEventListener.SetChangedCallback(callback, IntPtr.Zero);
128 if (err != Interop.NotificationEventListener.ErrorCode.None)
130 throw NotificationEventListenerErrorFactory.GetException(err, "unable to set changed callback");
134 UpdateEventHandler += value;
139 if (UpdateEventHandler != null && UpdateEventHandler.GetInvocationList().Length > 0)
141 UpdateEventHandler -= value;
143 if (GetEventHandleLength() == 0)
145 Interop.NotificationEventListener.ErrorCode err = Interop.NotificationEventListener.UnsetChangedCallback(callback);
146 if (err != Interop.NotificationEventListener.ErrorCode.None)
148 throw NotificationEventListenerErrorFactory.GetException(err, "unable to unset changed callback");
156 /// Registers a callback for notification delete event.
158 /// <exception cref="ArgumentException">Thrown in case of Invalid parameter.</exception>
159 /// <exception cref="UnauthorizedAccessException"> Thrown in case of Permission deny.</exception>
160 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
161 /// <privilege>http://tizen.org/privilege/notification</privilege>
162 public static event EventHandler<NotificationDeleteEventArgs> NotificationDeleteEventHandler
166 if (callback == null)
168 callback = new Interop.NotificationEventListener.ChangedCallback(ChangedEvent);
171 if (GetEventHandleLength() == 0)
173 Interop.NotificationEventListener.ErrorCode err = Interop.NotificationEventListener.SetChangedCallback(callback, IntPtr.Zero);
174 if (err != Interop.NotificationEventListener.ErrorCode.None)
176 throw NotificationEventListenerErrorFactory.GetException(err, "unable to set changed callback");
180 DeleteEventHandler += value;
185 if (DeleteEventHandler != null && DeleteEventHandler.GetInvocationList().Length > 0)
187 DeleteEventHandler -= value;
189 if (GetEventHandleLength() == 0)
191 Interop.NotificationEventListener.ErrorCode err = Interop.NotificationEventListener.UnsetChangedCallback(callback);
192 if (err != Interop.NotificationEventListener.ErrorCode.None)
194 throw NotificationEventListenerErrorFactory.GetException(err, "unable to unset changed callback");
201 private static void ChangedEvent(IntPtr userData, NotificationType type, IntPtr operationList, int num)
203 IntPtr operationType;
207 NotificationEventArgs eventargs;
208 NotificationDeleteEventArgs deleteargs;
210 for (int i = 0; i < num; i++)
212 uniqueNumber = IntPtr.Zero;
213 operationType = IntPtr.Zero;
214 notification = IntPtr.Zero;
216 Interop.NotificationEventListener.GetOperationData(operationList + (i * Marshal.SizeOf<NotificationOperation>()), NotificationOperationDataType.Type, out operationType);
217 Interop.NotificationEventListener.GetOperationData(operationList + (i * Marshal.SizeOf<NotificationOperation>()), NotificationOperationDataType.UniqueNumber, out uniqueNumber);
218 Interop.NotificationEventListener.GetOperationData(operationList + (i * Marshal.SizeOf<NotificationOperation>()), NotificationOperationDataType.Notification, out notification);
220 if (operationType == IntPtr.Zero)
222 Log.Error(LogTag, "unable to get operationType");
226 Log.Info(LogTag, "type : " + ((int)operationType).ToString());
227 Log.Info(LogTag, "Add : " + (AddEventHandler == null ? "0" : AddEventHandler.GetInvocationList().Length.ToString()));
228 Log.Info(LogTag, "update: " + (UpdateEventHandler == null ? "0" : UpdateEventHandler.GetInvocationList().Length.ToString()));
229 Log.Info(LogTag, "delete : " + (DeleteEventHandler == null ? "0" : DeleteEventHandler.GetInvocationList().Length.ToString()));
231 switch ((int)operationType)
233 case (int)NotificationOperationType.Insert:
234 if (notification != IntPtr.Zero)
238 eventargs = NotificationEventArgsBinder.BindObject(notification, false);
239 AddEventHandler?.Invoke(null, eventargs);
243 Log.Error(LogTag, e.Message);
249 case (int)NotificationOperationType.Update:
250 if (notification != IntPtr.Zero)
254 eventargs = NotificationEventArgsBinder.BindObject(notification, false);
255 UpdateEventHandler?.Invoke(null, eventargs);
259 Log.Error(LogTag, e.Message);
265 case (int)NotificationOperationType.Delete:
266 if (uniqueNumber != IntPtr.Zero)
270 deleteargs = NotificationDeleteEventArgsBinder.BindObject((int)uniqueNumber);
271 DeleteEventHandler?.Invoke(null, deleteargs);
275 Log.Error(LogTag, e.Message);
282 Log.Info(LogTag, "Event : " + (int)operationType);
289 /// Deletes a Notification with appId and uniqueNumber.
291 /// <param name="appId">The name of the application you want to delete.</param>
292 /// <param name="uniqueNumber">The unique number of the notification.</param>
293 /// <exception cref="ArgumentException">Thrown in case of Invalid parameter.</exception>
294 /// <exception cref="UnauthorizedAccessException"> Thrown in case of Permission deny.</exception>
295 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
296 /// <privilege>http://tizen.org/privilege/notification</privilege>
297 public static void Delete(string appId, int uniqueNumber)
299 Interop.NotificationEventListener.ErrorCode err;
301 if (string.IsNullOrEmpty(appId) || uniqueNumber < 0)
303 throw NotificationEventListenerErrorFactory.GetException(Interop.NotificationEventListener.ErrorCode.InvalidParameter, "invalid parameter");
306 err = Interop.NotificationEventListener.Delete(appId, 0, uniqueNumber);
307 if (err != Interop.NotificationEventListener.ErrorCode.None)
309 throw NotificationEventListenerErrorFactory.GetException(err, "unable to delete");
314 /// Deletes all Notification.
316 /// <exception cref="UnauthorizedAccessException"> Thrown in case of Permission deny.</exception>
317 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
318 /// <privilege>http://tizen.org/privilege/notification</privilege>
319 public static void DeleteAll()
321 Interop.NotificationEventListener.ErrorCode err;
323 err = Interop.NotificationEventListener.DeleteAll((int)NotificationType.Notification);
324 if (err != Interop.NotificationEventListener.ErrorCode.None)
326 throw NotificationEventListenerErrorFactory.GetException(err, "delete all notifications failed of Noti type");
329 err = Interop.NotificationEventListener.DeleteAll((int)NotificationType.Ongoing);
330 if (err != Interop.NotificationEventListener.ErrorCode.None)
332 throw NotificationEventListenerErrorFactory.GetException(err, "delete all notifications failed of Ongoing type");
337 /// Returns the notification list handle.
339 /// <exception cref="UnauthorizedAccessException"> Thrown in case of Permission deny.</exception>
340 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
341 /// <privilege>http://tizen.org/privilege/notification</privilege>
342 public static IList<NotificationEventArgs> GetList()
344 Interop.NotificationEventListener.ErrorCode err;
345 IntPtr notificationList = IntPtr.Zero;
346 IntPtr currentList = IntPtr.Zero;
347 IList<NotificationEventArgs> list = new List<NotificationEventArgs>();
349 err = Interop.NotificationEventListener.GetList(NotificationType.Notification, -1, out notificationList);
350 if (err != Interop.NotificationEventListener.ErrorCode.None)
352 throw NotificationEventListenerErrorFactory.GetException(err, "unable to get notification list");
355 if (notificationList != IntPtr.Zero)
357 currentList = notificationList;
358 while (currentList != IntPtr.Zero)
361 NotificationEventArgs eventargs = new NotificationEventArgs();
363 notification = Interop.NotificationEventListener.GetData(currentList);
365 eventargs = NotificationEventArgsBinder.BindObject(notification, false);
369 currentList = Interop.NotificationEventListener.GetNext(currentList);
372 Interop.NotificationEventListener.NotificationListFree(notificationList);
373 notificationList = IntPtr.Zero;
376 err = Interop.NotificationEventListener.GetList(NotificationType.Ongoing, -1, out notificationList);
377 if (err != Interop.NotificationEventListener.ErrorCode.None)
379 throw NotificationEventListenerErrorFactory.GetException(err, "unable to get notification list");
382 if (notificationList != IntPtr.Zero)
384 currentList = notificationList;
385 while (currentList != IntPtr.Zero)
388 NotificationEventArgs eventargs = new NotificationEventArgs();
390 notification = Interop.NotificationEventListener.GetData(currentList);
392 eventargs = NotificationEventArgsBinder.BindObject(notification, false);
396 currentList = Interop.NotificationEventListener.GetNext(currentList);
399 Interop.NotificationEventListener.NotificationListFree(notificationList);
405 [EditorBrowsable(EditorBrowsableState.Never)]
406 public static void SendEvent(int uniqueNumber, UserEventType type)
408 Interop.NotificationEventListener.ErrorCode err;
410 err = Interop.NotificationEventListener.SendEvent(uniqueNumber, (int)type);
411 if (err != Interop.NotificationEventListener.ErrorCode.None)
413 throw NotificationEventListenerErrorFactory.GetException(err, "failed to send event");