Add Internal Notification Response Event handler (#3178)
authorHyunho Kang <hhstark.kang@samsung.com>
Thu, 10 Jun 2021 05:53:23 +0000 (14:53 +0900)
committerGitHub <noreply@github.com>
Thu, 10 Jun 2021 05:53:23 +0000 (14:53 +0900)
* Add ViewerEventReceived event handler

Signed-off-by: Hyunho <hhstark.kang@samsung.com>
* Fix wrongly handled unmanaged codes

Signed-off-by: Hyunho <hhstark.kang@samsung.com>
* Fix api name and wrongly managed handle

Signed-off-by: Hyunho <hhstark.kang@samsung.com>
* Add missing files

Signed-off-by: Hyunho <hhstark.kang@samsung.com>
* Add newline for the end of file

Signed-off-by: Hyunho <hhstark.kang@samsung.com>
* Change throw -> Log for unmanaged callback

Signed-off-by: Hyunho <hhstark.kang@samsung.com>
* Update since tag 9 to 8

Signed-off-by: Hyunho <hhstark.kang@samsung.com>
src/Tizen.Applications.Notification/Interop/Interop.Notification.cs [changed mode: 0755->0644]
src/Tizen.Applications.Notification/Tizen.Applications.Notifications/NotificationManager.cs [changed mode: 0755->0644]
src/Tizen.Applications.Notification/Tizen.Applications.Notifications/NotificationResponseEventArgs.cs [new file with mode: 0644]
src/Tizen.Applications.Notification/Tizen.Applications.Notifications/NotificationResponseEventType.cs [new file with mode: 0644]

old mode 100755 (executable)
new mode 100644 (file)
index 1d5651b..aa900fd
@@ -23,6 +23,8 @@ internal static partial class Interop
 {
     internal static class Notification
     {
+        internal delegate void ResponseEventCallback(IntPtr ptr, int type, IntPtr userData);
+
         [DllImport(Libraries.Notification, EntryPoint = "notification_create")]
         internal static extern IntPtr Create(NotificationType type);
 
@@ -131,6 +133,9 @@ internal static partial class Interop
         [DllImport(Libraries.Notification, EntryPoint = "notification_post")]
         internal static extern NotificationError Post(NotificationSafeHandle handle);
 
+        [DllImport(Libraries.Notification, EntryPoint = "notification_post_with_event_cb")]
+        internal static extern NotificationError PostWithEventCallback(NotificationSafeHandle handle, ResponseEventCallback cb, IntPtr userdata);
+
         [DllImport(Libraries.Notification, EntryPoint = "notification_get_pkgname")]
         internal static extern NotificationError GetPackageName(NotificationSafeHandle handle, out IntPtr name);
 
@@ -224,6 +229,9 @@ internal static partial class Interop
         [DllImport(Libraries.Notification, EntryPoint = "notification_get_extension_event_handler")]
         internal static extern NotificationError GetExtensionAction(NotificationSafeHandle handle, NotificationEventType type, out SafeAppControlHandle appcontrol);
 
+        [DllImport(Libraries.Notification, EntryPoint = "notification_clone")]
+        internal static extern NotificationError Clone(IntPtr handle, out IntPtr cloned);
+
         internal static NotificationError GetText(NotificationSafeHandle handle, NotificationText type, out string text)
         {
             NotificationError ret;
old mode 100755 (executable)
new mode 100644 (file)
index 5ef8dd1..aadca4e
@@ -25,6 +25,55 @@ namespace Tizen.Applications.Notifications
     /// <since_tizen> 3 </since_tizen>
     public static class NotificationManager
     {
+        private static event EventHandler<NotificationResponseEventArgs> ResponseEventHandler;
+
+        private static Interop.Notification.ResponseEventCallback responseEventCallback;
+
+        private static void ResponseEventCallback(IntPtr ptr, int type, IntPtr userData)
+        {
+            IntPtr cloned;
+            NotificationError ret = Interop.Notification.Clone(ptr, out cloned);
+            if (ret != NotificationError.None)
+            {
+                Log.Error(Notification.LogTag, "Fail to clone notification : " + ret.ToString());
+                return;
+            }
+
+            NotificationResponseEventArgs eventArgs = new NotificationResponseEventArgs();
+            eventArgs.EventType = (NotificationResponseEventType)type;
+            eventArgs.Notification = new Notification
+            {
+                Handle = new NotificationSafeHandle(cloned, true)
+            }.Build();
+            ResponseEventHandler?.Invoke(null, eventArgs);
+        }
+
+        /// <summary> 
+        /// The event handler for receiving a response event from the notification viewers
+        /// </summary>
+        /// <since_tizen> 8 </since_tizen>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public static event EventHandler<NotificationResponseEventArgs> ResponseReceived
+        {
+            add
+            {
+                if (responseEventCallback == null)
+                {
+                    responseEventCallback = new Interop.Notification.ResponseEventCallback(ResponseEventCallback);
+                }
+                
+                ResponseEventHandler += value;
+            }
+
+            remove
+            {
+                if (ResponseEventHandler != null && ResponseEventHandler.GetInvocationList().Length > 0)
+                {
+                    ResponseEventHandler -= value;
+                }
+            }
+        }
+
         /// <summary>
         /// Posts a new notification.
         /// </summary>
@@ -65,11 +114,22 @@ namespace Tizen.Applications.Notifications
 
             notification.Make();
 
-            NotificationError ret = Interop.Notification.Post(notification.Handle);
-            if (ret != NotificationError.None)
+            if (ResponseEventHandler != null && ResponseEventHandler.GetInvocationList().Length > 0)
             {
-                throw NotificationErrorFactory.GetException(ret, "post notification failed");
+                NotificationError ret = Interop.Notification.PostWithEventCallback(notification.Handle, responseEventCallback, IntPtr.Zero);
+                if (ret != NotificationError.None)
+                {
+                    throw NotificationErrorFactory.GetException(ret, "post notification with event callback failed");
+                }
             }
+            else
+            {
+                NotificationError ret = Interop.Notification.Post(notification.Handle);
+                if (ret != NotificationError.None)
+                {
+                    throw NotificationErrorFactory.GetException(ret, "post notification failed");
+                }
+            }   
 
             int priv_id, group_id;
             Interop.Notification.GetID(notification.Handle, out group_id, out priv_id);
diff --git a/src/Tizen.Applications.Notification/Tizen.Applications.Notifications/NotificationResponseEventArgs.cs b/src/Tizen.Applications.Notification/Tizen.Applications.Notifications/NotificationResponseEventArgs.cs
new file mode 100644 (file)
index 0000000..ccc632c
--- /dev/null
@@ -0,0 +1,25 @@
+using System;
+using System.ComponentModel;
+
+namespace Tizen.Applications.Notifications
+{
+    /// <summary>
+    /// The EventArgs for the notification response
+    /// </summary>
+    /// <since_tizen> 8 </since_tizen>
+    [EditorBrowsable(EditorBrowsableState.Never)]
+    public class NotificationResponseEventArgs : EventArgs
+    {
+        /// <summary>
+        /// The type of response
+        /// </summary>
+        /// <since_tizen> 8 </since_tizen>
+        public NotificationResponseEventType EventType { get; internal set; }
+
+        /// <summary>
+        /// The response's target notification
+        /// </summary>
+        /// <since_tizen> 8 </since_tizen>
+        public Notification Notification { get; internal set; }
+    }
+}
diff --git a/src/Tizen.Applications.Notification/Tizen.Applications.Notifications/NotificationResponseEventType.cs b/src/Tizen.Applications.Notification/Tizen.Applications.Notifications/NotificationResponseEventType.cs
new file mode 100644 (file)
index 0000000..b03f10e
--- /dev/null
@@ -0,0 +1,58 @@
+
+using System.ComponentModel;
+
+namespace Tizen.Applications.Notifications
+{
+    /// <summary>
+    /// Enumeration for event type on notification.
+    /// </summary>
+    /// <since_tizen> 8 </since_tizen>
+    [EditorBrowsable(EditorBrowsableState.Never)]
+    public enum NotificationResponseEventType
+    {
+        /// <summary>
+        /// Event type : Click on button 1.
+        /// </summary>
+        ClickOnButton1 = 0,
+
+        /// <summary>
+        /// Event type : Click on button 2.
+        /// </summary>
+        ClickOnButton2,
+
+        /// <summary>
+        /// Event type : Click on button 3.
+        /// </summary>
+        ClickOnButton3,
+
+        /// <summary>
+        /// Event type : Click on text_input button.
+        /// </summary>
+        ClickOnReplyButton = 8,
+
+        /// <summary>
+        /// Event type : Hidden by user.
+        /// </summary>
+        HiddenByUser = 100,
+
+        /// <summary>
+        /// Event type : Deleted by timer.
+        /// </summary>
+        HiddenByTimeout = 101,
+
+        /// <summary>
+        /// Event type : Deleted by timer.
+        /// </summary>
+        HiddenByExternal = 102,
+
+        /// <summary>
+        /// Event type : Clicked by user.
+        /// </summary>
+        ClickOnNotification = 200,
+
+        /// <summary>
+        /// Event type : Deleted by user.
+        /// </summary>
+        DeleteNotification = 201,
+    }
+}