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;
24 using Tizen.Internals;
27 /// This class provides a way to register callback function for some notification events.
30 /// The event listener can use this class to get a list of notifications or to clear notifications.
32 /// <since_tizen> 4 </since_tizen>
33 public partial class NotificationListenerManager
35 private const string LogTag = "Tizen.Applications.NotificationEventListener";
37 private static event EventHandler<NotificationEventArgs> AddEventHandler;
39 private static event EventHandler<NotificationEventArgs> UpdateEventHandler;
41 private static event EventHandler<NotificationDeleteEventArgs> DeleteEventHandler;
43 private static Interop.NotificationEventListener.ChangedCallback callback;
45 [NativeStruct("notification_op", Include="notification_type.h", PkgConfig="notification")]
46 [StructLayout(LayoutKind.Sequential)]
47 private struct NotificationOperation
49 NotificationOperationType type;
51 int extraInformation1;
52 int extraInformation2;
56 private static int GetEventHandleLength()
60 length += (DeleteEventHandler == null) ? 0 : DeleteEventHandler.GetInvocationList().Length;
61 length += (UpdateEventHandler == null) ? 0 : UpdateEventHandler.GetInvocationList().Length;
62 length += (AddEventHandler == null) ? 0 : AddEventHandler.GetInvocationList().Length;
68 /// Event handler for notification insert event.
70 /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
71 /// <exception cref="UnauthorizedAccessException"> Thrown in case of a permission is denied.</exception>
72 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
73 /// <privilege>http://tizen.org/privilege/notification</privilege>
74 /// <since_tizen> 4 </since_tizen>
75 public static event EventHandler<NotificationEventArgs> Added
81 callback = new Interop.NotificationEventListener.ChangedCallback(ChangedEvent);
84 if (GetEventHandleLength() == 0)
86 Interop.NotificationEventListener.ErrorCode err = Interop.NotificationEventListener.SetChangedCallback(callback, IntPtr.Zero);
87 if (err != (int)Interop.NotificationEventListener.ErrorCode.None)
89 throw NotificationEventListenerErrorFactory.GetException(err, "unable to set changed callback");
93 AddEventHandler += value;
98 if (AddEventHandler != null && AddEventHandler.GetInvocationList().Length > 0)
100 AddEventHandler -= value;
102 if (GetEventHandleLength() == 0)
104 Interop.NotificationEventListener.ErrorCode err = Interop.NotificationEventListener.UnsetChangedCallback(callback);
105 if (err != (int)Interop.NotificationEventListener.ErrorCode.None)
107 throw NotificationEventListenerErrorFactory.GetException(err, "unable to unset changed callback");
115 /// Event handler for notification update event.
117 /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
118 /// <exception cref="UnauthorizedAccessException"> Thrown in case of a permission is denied.</exception>
119 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
120 /// <privilege>http://tizen.org/privilege/notification</privilege>
121 /// <since_tizen> 4 </since_tizen>
122 public static event EventHandler<NotificationEventArgs> Updated
126 if (callback == null)
128 callback = new Interop.NotificationEventListener.ChangedCallback(ChangedEvent);
131 if (GetEventHandleLength() == 0)
133 Interop.NotificationEventListener.ErrorCode err = Interop.NotificationEventListener.SetChangedCallback(callback, IntPtr.Zero);
134 if (err != Interop.NotificationEventListener.ErrorCode.None)
136 throw NotificationEventListenerErrorFactory.GetException(err, "unable to set changed callback");
140 UpdateEventHandler += value;
145 if (UpdateEventHandler != null && UpdateEventHandler.GetInvocationList().Length > 0)
147 UpdateEventHandler -= value;
149 if (GetEventHandleLength() == 0)
151 Interop.NotificationEventListener.ErrorCode err = Interop.NotificationEventListener.UnsetChangedCallback(callback);
152 if (err != Interop.NotificationEventListener.ErrorCode.None)
154 throw NotificationEventListenerErrorFactory.GetException(err, "unable to unset changed callback");
162 /// Event handler for notification delete event.
164 /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
165 /// <exception cref="UnauthorizedAccessException"> Thrown in case of a permission is denied.</exception>
166 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
167 /// <privilege>http://tizen.org/privilege/notification</privilege>
168 /// <since_tizen> 4 </since_tizen>
169 public static event EventHandler<NotificationDeleteEventArgs> Deleted
173 if (callback == null)
175 callback = new Interop.NotificationEventListener.ChangedCallback(ChangedEvent);
178 if (GetEventHandleLength() == 0)
180 Interop.NotificationEventListener.ErrorCode err = Interop.NotificationEventListener.SetChangedCallback(callback, IntPtr.Zero);
181 if (err != Interop.NotificationEventListener.ErrorCode.None)
183 throw NotificationEventListenerErrorFactory.GetException(err, "unable to set changed callback");
187 DeleteEventHandler += value;
192 if (DeleteEventHandler != null && DeleteEventHandler.GetInvocationList().Length > 0)
194 DeleteEventHandler -= value;
196 if (GetEventHandleLength() == 0)
198 Interop.NotificationEventListener.ErrorCode err = Interop.NotificationEventListener.UnsetChangedCallback(callback);
199 if (err != Interop.NotificationEventListener.ErrorCode.None)
201 throw NotificationEventListenerErrorFactory.GetException(err, "unable to unset changed callback");
208 private static void ChangedEvent(IntPtr userData, NotificationType type, IntPtr operationList, int num)
210 IntPtr operationType;
214 NotificationEventArgs eventargs;
215 NotificationDeleteEventArgs deleteargs;
217 for (int i = 0; i < num; i++)
219 uniqueNumber = IntPtr.Zero;
220 operationType = IntPtr.Zero;
221 notification = IntPtr.Zero;
223 Interop.NotificationEventListener.GetOperationData(operationList + (i * Marshal.SizeOf<NotificationOperation>()), NotificationOperationDataType.Type, out operationType);
224 Interop.NotificationEventListener.GetOperationData(operationList + (i * Marshal.SizeOf<NotificationOperation>()), NotificationOperationDataType.UniqueNumber, out uniqueNumber);
225 Interop.NotificationEventListener.GetOperationData(operationList + (i * Marshal.SizeOf<NotificationOperation>()), NotificationOperationDataType.Notification, out notification);
227 if (operationType == IntPtr.Zero)
229 Log.Error(LogTag, "unable to get operationType");
233 Log.Info(LogTag, "type : " + ((int)operationType).ToString());
234 Log.Info(LogTag, "Add : " + (AddEventHandler == null ? "0" : AddEventHandler.GetInvocationList().Length.ToString()));
235 Log.Info(LogTag, "update: " + (UpdateEventHandler == null ? "0" : UpdateEventHandler.GetInvocationList().Length.ToString()));
236 Log.Info(LogTag, "delete : " + (DeleteEventHandler == null ? "0" : DeleteEventHandler.GetInvocationList().Length.ToString()));
238 switch ((int)operationType)
240 case (int)NotificationOperationType.Insert:
241 if (notification != IntPtr.Zero)
245 eventargs = NotificationEventArgsBinder.BindObject(notification, false);
246 AddEventHandler?.Invoke(null, eventargs);
250 Log.Error(LogTag, e.Message);
256 case (int)NotificationOperationType.Update:
257 if (notification != IntPtr.Zero)
261 eventargs = NotificationEventArgsBinder.BindObject(notification, false);
262 UpdateEventHandler?.Invoke(null, eventargs);
266 Log.Error(LogTag, e.Message);
272 case (int)NotificationOperationType.Delete:
273 if (uniqueNumber != IntPtr.Zero)
277 deleteargs = NotificationDeleteEventArgsBinder.BindObject((int)uniqueNumber);
278 DeleteEventHandler?.Invoke(null, deleteargs);
282 Log.Error(LogTag, e.Message);
289 Log.Info(LogTag, "Event : " + (int)operationType);
296 /// Deletes a notification with appId and uniqueNumber.
298 /// <param name="appId">The name of the application you want to delete.</param>
299 /// <param name="uniqueNumber">The unique number of the notification.</param>
300 /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
301 /// <exception cref="UnauthorizedAccessException"> Thrown in case of a permission is denied.</exception>
302 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
303 /// <privilege>http://tizen.org/privilege/notification</privilege>
304 /// <since_tizen> 4 </since_tizen>
305 public static void Delete(string appId, int uniqueNumber)
307 Interop.NotificationEventListener.ErrorCode err;
309 if (string.IsNullOrEmpty(appId) || uniqueNumber < 0)
311 throw NotificationEventListenerErrorFactory.GetException(Interop.NotificationEventListener.ErrorCode.InvalidParameter, "invalid parameter");
314 err = Interop.NotificationEventListener.Delete(appId, 0, uniqueNumber);
315 if (err != Interop.NotificationEventListener.ErrorCode.None)
317 throw NotificationEventListenerErrorFactory.GetException(err, "unable to delete");
322 /// Deletes all notifications.
324 /// <exception cref="UnauthorizedAccessException"> Thrown in case of a permission is denied.</exception>
325 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
326 /// <privilege>http://tizen.org/privilege/notification</privilege>
327 /// <since_tizen> 4 </since_tizen>
328 public static void DeleteAll()
330 Interop.NotificationEventListener.ErrorCode err;
332 err = Interop.NotificationEventListener.DeleteAll((int)NotificationType.Notification);
333 if (err != Interop.NotificationEventListener.ErrorCode.None)
335 throw NotificationEventListenerErrorFactory.GetException(err, "delete all notifications failed of Noti type");
338 err = Interop.NotificationEventListener.DeleteAll((int)NotificationType.Ongoing);
339 if (err != Interop.NotificationEventListener.ErrorCode.None)
341 throw NotificationEventListenerErrorFactory.GetException(err, "delete all notifications failed of Ongoing type");
346 /// Returns the notification list.
348 /// <exception cref="UnauthorizedAccessException"> Thrown in case of a permission is denied.</exception>
349 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
350 /// <privilege>http://tizen.org/privilege/notification</privilege>
351 /// <since_tizen> 4 </since_tizen>
352 public static IList<NotificationEventArgs> GetList()
354 Interop.NotificationEventListener.ErrorCode err;
355 IntPtr notificationList = IntPtr.Zero;
356 IntPtr currentList = IntPtr.Zero;
357 IList<NotificationEventArgs> list = new List<NotificationEventArgs>();
359 err = Interop.NotificationEventListener.GetList(NotificationType.None, -1, out notificationList);
360 if (err != Interop.NotificationEventListener.ErrorCode.None)
362 throw NotificationEventListenerErrorFactory.GetException(err, "unable to get notification list");
365 if (notificationList != IntPtr.Zero)
367 currentList = notificationList;
368 while (currentList != IntPtr.Zero)
371 NotificationEventArgs eventargs = new NotificationEventArgs();
373 notification = Interop.NotificationEventListener.GetData(currentList);
375 eventargs = NotificationEventArgsBinder.BindObject(notification, false);
379 currentList = Interop.NotificationEventListener.GetNext(currentList);
382 Interop.NotificationEventListener.NotificationListFree(notificationList);
389 /// Sends occured event from viewer application to the notification owner.
391 /// <param name="uniqueNumber">The unique number of the notification.</param>
392 /// <param name="type">Event type on notification.</param>
393 /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
394 /// <exception cref="UnauthorizedAccessException"> Thrown in case of a permission is denied.</exception>
395 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
396 /// <privilege>http://tizen.org/privilege/notification</privilege>
397 /// <since_tizen> 4 </since_tizen>
398 [EditorBrowsable(EditorBrowsableState.Never)]
399 public static void SendEvent(int uniqueNumber, UserEventType type)
401 Interop.NotificationEventListener.ErrorCode err;
403 err = Interop.NotificationEventListener.SendEvent(uniqueNumber, (int)type);
404 if (err != Interop.NotificationEventListener.ErrorCode.None)
406 throw NotificationEventListenerErrorFactory.GetException(err, "failed to send event");
410 [EditorBrowsable(EditorBrowsableState.Never)]
411 public static void SetChecked(NotificationEventArgs eventargs, bool checkedValue)
413 Interop.NotificationEventListener.ErrorCode err;
415 err = Interop.NotificationEventListener.SetCheckedValue(eventargs.Handle, checkedValue);
416 if (err != Interop.NotificationEventListener.ErrorCode.None)
418 throw NotificationEventListenerErrorFactory.GetException(err, "failed to set checked");
422 [EditorBrowsable(EditorBrowsableState.Never)]
423 public static void SendEventWithNotification(NotificationEventArgs eventargs, UserEventType type)
425 Interop.NotificationEventListener.ErrorCode err;
427 err = Interop.NotificationEventListener.SendEventWithNotification(eventargs.Handle, (int)type);
428 if (err != Interop.NotificationEventListener.ErrorCode.None)
430 throw NotificationEventListenerErrorFactory.GetException(err, "failed to send event");
435 /// Returns NotificationEventArgs by UniqueNumber.
437 /// <param name="uniqueNumber">The unique number of the Notification.</param>
438 /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
439 /// <exception cref="UnauthorizedAccessException"> Thrown in case of a permission is denied.</exception>
440 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
441 /// <privilege>http://tizen.org/privilege/notification</privilege>
442 /// <since_tizen> 4 </since_tizen>
443 [EditorBrowsable(EditorBrowsableState.Never)]
444 public static NotificationEventArgs GetNotificationEventArgs(int uniqueNumber)
446 if (uniqueNumber <= 0)
448 throw NotificationEventListenerErrorFactory.GetException(Interop.NotificationEventListener.ErrorCode.InvalidParameter, "Invalid parameter");
451 IntPtr notificationPtr = Interop.NotificationEventListener.LoadNotification(null, uniqueNumber);
452 if (notificationPtr == IntPtr.Zero)
454 int err = Tizen.Internals.Errors.ErrorFacts.GetLastResult();
455 if (err.Equals((int)Interop.NotificationEventListener.ErrorCode.DbError))
457 throw NotificationEventListenerErrorFactory.GetException(Interop.NotificationEventListener.ErrorCode.InvalidParameter, "Not exist");
461 throw NotificationEventListenerErrorFactory.GetException((Interop.NotificationEventListener.ErrorCode)err, "failed to get NotificationEventArgs");
465 NotificationEventArgs eventArgs = new NotificationEventArgs();
466 eventArgs = NotificationEventArgsBinder.BindObject(notificationPtr, false);
472 /// Gets the number of all notifications
474 /// <returns>The number of all notifications</returns>
475 /// <exception cref="UnauthorizedAccessException"> Thrown in case of a permission is denied.</exception>
476 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
477 /// <privilege>http://tizen.org/privilege/notification</privilege>
478 /// <since_tizen> 4 </since_tizen>
479 [EditorBrowsable(EditorBrowsableState.Never)]
480 public static int GetAllCount()
483 Interop.NotificationEventListener.ErrorCode error;
485 error = Interop.NotificationEventListener.GetAllCount(NotificationType.None, out count);
486 if (error != Interop.NotificationEventListener.ErrorCode.None)
488 if (error == Interop.NotificationEventListener.ErrorCode.PermissionDenied)
490 throw NotificationEventListenerErrorFactory.GetException(Interop.NotificationEventListener.ErrorCode.PermissionDenied, "failed to get all count");
494 throw NotificationEventListenerErrorFactory.GetException(Interop.NotificationEventListener.ErrorCode.InvalidOperation, "failed to get all count");