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 notifications or to clear notifications.
30 /// <since_tizen> 4 </since_tizen>
31 public partial class NotificationListenerManager
33 private const string LogTag = "Tizen.Applications.NotificationEventListener";
35 private static event EventHandler<NotificationEventArgs> AddEventHandler;
37 private static event EventHandler<NotificationEventArgs> UpdateEventHandler;
39 private static event EventHandler<NotificationDeleteEventArgs> DeleteEventHandler;
41 private static Interop.NotificationEventListener.ChangedCallback callback;
43 [StructLayout(LayoutKind.Sequential)]
44 private struct NotificationOperation
46 NotificationOperationType type;
48 int extraInformation1;
49 int extraInformation2;
53 private static int GetEventHandleLength()
57 length += (DeleteEventHandler == null) ? 0 : DeleteEventHandler.GetInvocationList().Length;
58 length += (UpdateEventHandler == null) ? 0 : UpdateEventHandler.GetInvocationList().Length;
59 length += (AddEventHandler == null) ? 0 : AddEventHandler.GetInvocationList().Length;
65 /// Event handler for notification insert event.
67 /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
68 /// <exception cref="UnauthorizedAccessException"> Thrown in case of a permission is denied.</exception>
69 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
70 /// <privilege>http://tizen.org/privilege/notification</privilege>
71 /// <since_tizen> 4 </since_tizen>
72 public static event EventHandler<NotificationEventArgs> Added
78 callback = new Interop.NotificationEventListener.ChangedCallback(ChangedEvent);
81 if (GetEventHandleLength() == 0)
83 Interop.NotificationEventListener.ErrorCode err = Interop.NotificationEventListener.SetChangedCallback(callback, IntPtr.Zero);
84 if (err != (int)Interop.NotificationEventListener.ErrorCode.None)
86 throw NotificationEventListenerErrorFactory.GetException(err, "unable to set changed callback");
90 AddEventHandler += value;
95 if (AddEventHandler != null && AddEventHandler.GetInvocationList().Length > 0)
97 AddEventHandler -= value;
99 if (GetEventHandleLength() == 0)
101 Interop.NotificationEventListener.ErrorCode err = Interop.NotificationEventListener.UnsetChangedCallback(callback);
102 if (err != (int)Interop.NotificationEventListener.ErrorCode.None)
104 throw NotificationEventListenerErrorFactory.GetException(err, "unable to unset changed callback");
112 /// Event handler for notification update event.
114 /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
115 /// <exception cref="UnauthorizedAccessException"> Thrown in case of a permission is denied.</exception>
116 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
117 /// <privilege>http://tizen.org/privilege/notification</privilege>
118 /// <since_tizen> 4 </since_tizen>
119 public static event EventHandler<NotificationEventArgs> Updated
123 if (callback == null)
125 callback = new Interop.NotificationEventListener.ChangedCallback(ChangedEvent);
128 if (GetEventHandleLength() == 0)
130 Interop.NotificationEventListener.ErrorCode err = Interop.NotificationEventListener.SetChangedCallback(callback, IntPtr.Zero);
131 if (err != Interop.NotificationEventListener.ErrorCode.None)
133 throw NotificationEventListenerErrorFactory.GetException(err, "unable to set changed callback");
137 UpdateEventHandler += value;
142 if (UpdateEventHandler != null && UpdateEventHandler.GetInvocationList().Length > 0)
144 UpdateEventHandler -= value;
146 if (GetEventHandleLength() == 0)
148 Interop.NotificationEventListener.ErrorCode err = Interop.NotificationEventListener.UnsetChangedCallback(callback);
149 if (err != Interop.NotificationEventListener.ErrorCode.None)
151 throw NotificationEventListenerErrorFactory.GetException(err, "unable to unset changed callback");
159 /// Event handler for notification delete event.
161 /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
162 /// <exception cref="UnauthorizedAccessException"> Thrown in case of a permission is denied.</exception>
163 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
164 /// <privilege>http://tizen.org/privilege/notification</privilege>
165 /// <since_tizen> 4 </since_tizen>
166 public static event EventHandler<NotificationDeleteEventArgs> Deleted
170 if (callback == null)
172 callback = new Interop.NotificationEventListener.ChangedCallback(ChangedEvent);
175 if (GetEventHandleLength() == 0)
177 Interop.NotificationEventListener.ErrorCode err = Interop.NotificationEventListener.SetChangedCallback(callback, IntPtr.Zero);
178 if (err != Interop.NotificationEventListener.ErrorCode.None)
180 throw NotificationEventListenerErrorFactory.GetException(err, "unable to set changed callback");
184 DeleteEventHandler += value;
189 if (DeleteEventHandler != null && DeleteEventHandler.GetInvocationList().Length > 0)
191 DeleteEventHandler -= value;
193 if (GetEventHandleLength() == 0)
195 Interop.NotificationEventListener.ErrorCode err = Interop.NotificationEventListener.UnsetChangedCallback(callback);
196 if (err != Interop.NotificationEventListener.ErrorCode.None)
198 throw NotificationEventListenerErrorFactory.GetException(err, "unable to unset changed callback");
205 private static void ChangedEvent(IntPtr userData, NotificationType type, IntPtr operationList, int num)
207 IntPtr operationType;
211 NotificationEventArgs eventargs;
212 NotificationDeleteEventArgs deleteargs;
214 for (int i = 0; i < num; i++)
216 uniqueNumber = IntPtr.Zero;
217 operationType = IntPtr.Zero;
218 notification = IntPtr.Zero;
220 Interop.NotificationEventListener.GetOperationData(operationList + (i * Marshal.SizeOf<NotificationOperation>()), NotificationOperationDataType.Type, out operationType);
221 Interop.NotificationEventListener.GetOperationData(operationList + (i * Marshal.SizeOf<NotificationOperation>()), NotificationOperationDataType.UniqueNumber, out uniqueNumber);
222 Interop.NotificationEventListener.GetOperationData(operationList + (i * Marshal.SizeOf<NotificationOperation>()), NotificationOperationDataType.Notification, out notification);
224 if (operationType == IntPtr.Zero)
226 Log.Error(LogTag, "unable to get operationType");
230 Log.Info(LogTag, "type : " + ((int)operationType).ToString());
231 Log.Info(LogTag, "Add : " + (AddEventHandler == null ? "0" : AddEventHandler.GetInvocationList().Length.ToString()));
232 Log.Info(LogTag, "update: " + (UpdateEventHandler == null ? "0" : UpdateEventHandler.GetInvocationList().Length.ToString()));
233 Log.Info(LogTag, "delete : " + (DeleteEventHandler == null ? "0" : DeleteEventHandler.GetInvocationList().Length.ToString()));
235 switch ((int)operationType)
237 case (int)NotificationOperationType.Insert:
238 if (notification != IntPtr.Zero)
242 eventargs = NotificationEventArgsBinder.BindObject(notification, false);
243 AddEventHandler?.Invoke(null, eventargs);
247 Log.Error(LogTag, e.Message);
253 case (int)NotificationOperationType.Update:
254 if (notification != IntPtr.Zero)
258 eventargs = NotificationEventArgsBinder.BindObject(notification, false);
259 UpdateEventHandler?.Invoke(null, eventargs);
263 Log.Error(LogTag, e.Message);
269 case (int)NotificationOperationType.Delete:
270 if (uniqueNumber != IntPtr.Zero)
274 deleteargs = NotificationDeleteEventArgsBinder.BindObject((int)uniqueNumber);
275 DeleteEventHandler?.Invoke(null, deleteargs);
279 Log.Error(LogTag, e.Message);
286 Log.Info(LogTag, "Event : " + (int)operationType);
293 /// Deletes a notification with appId and uniqueNumber.
295 /// <param name="appId">The name of the application you want to delete.</param>
296 /// <param name="uniqueNumber">The unique number of the notification.</param>
297 /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
298 /// <exception cref="UnauthorizedAccessException"> Thrown in case of a permission is denied.</exception>
299 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
300 /// <privilege>http://tizen.org/privilege/notification</privilege>
301 /// <since_tizen> 4 </since_tizen>
302 public static void Delete(string appId, int uniqueNumber)
304 Interop.NotificationEventListener.ErrorCode err;
306 if (string.IsNullOrEmpty(appId) || uniqueNumber < 0)
308 throw NotificationEventListenerErrorFactory.GetException(Interop.NotificationEventListener.ErrorCode.InvalidParameter, "invalid parameter");
311 err = Interop.NotificationEventListener.Delete(appId, 0, uniqueNumber);
312 if (err != Interop.NotificationEventListener.ErrorCode.None)
314 throw NotificationEventListenerErrorFactory.GetException(err, "unable to delete");
319 /// Deletes all notifications.
321 /// <exception cref="UnauthorizedAccessException"> Thrown in case of a permission is denied.</exception>
322 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
323 /// <privilege>http://tizen.org/privilege/notification</privilege>
324 /// <since_tizen> 4 </since_tizen>
325 public static void DeleteAll()
327 Interop.NotificationEventListener.ErrorCode err;
329 err = Interop.NotificationEventListener.DeleteAll((int)NotificationType.Notification);
330 if (err != Interop.NotificationEventListener.ErrorCode.None)
332 throw NotificationEventListenerErrorFactory.GetException(err, "delete all notifications failed of Noti type");
335 err = Interop.NotificationEventListener.DeleteAll((int)NotificationType.Ongoing);
336 if (err != Interop.NotificationEventListener.ErrorCode.None)
338 throw NotificationEventListenerErrorFactory.GetException(err, "delete all notifications failed of Ongoing type");
343 /// Returns the notification list.
345 /// <exception cref="UnauthorizedAccessException"> Thrown in case of a permission is denied.</exception>
346 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
347 /// <privilege>http://tizen.org/privilege/notification</privilege>
348 /// <since_tizen> 4 </since_tizen>
349 public static IList<NotificationEventArgs> GetList()
351 Interop.NotificationEventListener.ErrorCode err;
352 IntPtr notificationList = IntPtr.Zero;
353 IntPtr currentList = IntPtr.Zero;
354 IList<NotificationEventArgs> list = new List<NotificationEventArgs>();
356 err = Interop.NotificationEventListener.GetList(NotificationType.None, -1, out notificationList);
357 if (err != Interop.NotificationEventListener.ErrorCode.None)
359 throw NotificationEventListenerErrorFactory.GetException(err, "unable to get notification list");
362 if (notificationList != IntPtr.Zero)
364 currentList = notificationList;
365 while (currentList != IntPtr.Zero)
368 NotificationEventArgs eventargs = new NotificationEventArgs();
370 notification = Interop.NotificationEventListener.GetData(currentList);
372 eventargs = NotificationEventArgsBinder.BindObject(notification, false);
376 currentList = Interop.NotificationEventListener.GetNext(currentList);
379 Interop.NotificationEventListener.NotificationListFree(notificationList);
386 /// Sends occured event from viewer application to the notification owner.
388 /// <param name="uniqueNumber">The unique number of the notification.</param>
389 /// <param name="type">Event type on notification.</param>
390 /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
391 /// <exception cref="UnauthorizedAccessException"> Thrown in case of a permission is denied.</exception>
392 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
393 /// <privilege>http://tizen.org/privilege/notification</privilege>
394 /// <since_tizen> 4 </since_tizen>
395 [EditorBrowsable(EditorBrowsableState.Never)]
396 public static void SendEvent(int uniqueNumber, UserEventType type)
398 Interop.NotificationEventListener.ErrorCode err;
400 err = Interop.NotificationEventListener.SendEvent(uniqueNumber, (int)type);
401 if (err != Interop.NotificationEventListener.ErrorCode.None)
403 throw NotificationEventListenerErrorFactory.GetException(err, "failed to send event");
408 /// Returns NotificationEventArgs by UniqueNumber.
410 /// <param name="uniqueNumber">The unique number of the Notification.</param>
411 /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
412 /// <exception cref="UnauthorizedAccessException"> Thrown in case of a permission is denied.</exception>
413 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
414 /// <privilege>http://tizen.org/privilege/notification</privilege>
415 /// <since_tizen> 4 </since_tizen>
416 [EditorBrowsable(EditorBrowsableState.Never)]
417 public static NotificationEventArgs GetNotificationEventArgs(int uniqueNumber)
419 if (uniqueNumber <= 0)
421 throw NotificationEventListenerErrorFactory.GetException(Interop.NotificationEventListener.ErrorCode.InvalidParameter, "Invalid parameter");
424 IntPtr notificationPtr = Interop.NotificationEventListener.LoadNotification(null, uniqueNumber);
425 if (notificationPtr == IntPtr.Zero)
427 int err = Tizen.Internals.Errors.ErrorFacts.GetLastResult();
428 if (err.Equals((int)Interop.NotificationEventListener.ErrorCode.DbError))
430 throw NotificationEventListenerErrorFactory.GetException(Interop.NotificationEventListener.ErrorCode.InvalidParameter, "Not exist");
434 throw NotificationEventListenerErrorFactory.GetException((Interop.NotificationEventListener.ErrorCode)err, "failed to get NotificationEventArgs");
438 NotificationEventArgs eventArgs = new NotificationEventArgs();
439 eventArgs = NotificationEventArgsBinder.BindObject(notificationPtr, false);