1 // Copyright 2016 by Samsung Electronics, Inc.,
3 // This software is the confidential and proprietary information
4 // of Samsung Electronics, Inc. ("Confidential Information"). You
5 // shall not disclose such Confidential Information and shall use
6 // it only in accordance with the terms of the license agreement
7 // you entered into with Samsung.
10 using System.Runtime.InteropServices;
12 namespace Tizen.Applications.Notifications
15 /// Class for creating a simple event notification
19 /// public class EventNotificationExample
22 /// EventNotification noti = new EventNotification();
23 /// noti.EventCount = 2;
24 /// noti.Title = "Unread Messages";
25 /// NotificationManager.Post(noti);
29 public class EventNotification : Notification
32 /// Intializes instance of EventNotification class
34 public EventNotification()
38 _handle = Interop.Notification.Create(NotificationType.Event);
39 if (_handle.IsInvalid)
41 ret = Tizen.Internals.Errors.ErrorFacts.GetLastResult();
42 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to create Event Notification");
45 ret = Interop.Notification.SetLayout(_handle, NotiLayout.SingleEvent);
46 if(ret != (int)NotificationError.None)
48 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set event layout");
51 NotificationType = NotificationType.Event;
54 internal EventNotification(Interop.Notification.SafeNotificationHandle handle)
58 NotificationType = NotificationType.Event;
62 /// Sets and gets the event count to be displayed on a Notification.
65 /// EventCount is a numeric value which is displayed on a notification.
66 /// For example, to show unread message count on a messenger notification event count can be set.
70 /// EventNotification noti = new EventNotification();
71 /// noti.EventCount = 2;
72 /// Log.Debug(LogTag, "Event Count: " + noti.EventCount);
82 int ret = Interop.Notification.GetText(_handle, NotiText.EventCount, out countPtr);
83 if(ret != (int)NotificationError.None)
85 Log.Warn(_logTag, "unable to get event count");
89 if(countPtr == IntPtr.Zero)
92 count = Marshal.PtrToStringAnsi(countPtr);
93 if(Int32.TryParse(count, out cnt))
99 Log.Warn(_logTag, "unable to parse string");
108 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid value for event count");
111 ret = Interop.Notification.SetText(_handle, NotiText.EventCount, value.ToString(), null, -1);
112 if(ret != (int)NotificationError.None)
114 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set event count");
120 /// Gets and sets TimeStamp. Defaults to 0 ms UTC i.e 1 Jan 1970.
123 /// Timestamp can be used to display a timestamp on a notification.
124 /// For example, in a message received notification, Timestamp can be used to display the time of message reception.
128 /// EventNotification noti = new EventNotification();
129 /// noti.Timestamp = DateTime.Now;
130 /// Log.Debug(LogTag, "Timestamp: " + noti.Timestamp);
133 public DateTime Timestamp
138 int ret = Interop.Notification.GetTime(_handle, out time);
139 if(ret != (int)NotificationError.None)
141 Log.Warn(_logTag, "unable to get timestamp");
142 return (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddSeconds(0).ToLocalTime();
145 return (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddSeconds(time).ToLocalTime();
149 int time = (int)value.ToUniversalTime().Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
150 int ret = Interop.Notification.SetTime(_handle, time);
151 if(ret != (int)NotificationError.None)
153 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set timestamp");
159 /// Method to add a button to an event notification
161 /// <param name="index">Index of the Button to be added to notification</param>
162 /// <param name="imagePath">Path of Button image</param>
163 /// <param name="text">Text of button</param>
164 /// <param name="appControl">App control to be invoke on button click</param>
166 /// AddButton method can be used to add 2 different types of buttons, one for positive response and one for negative.
167 /// A button comprises of Image path for the button, Text for the button and an App control which is to be invoked on button click.
171 /// Application app = Application.Current;
172 /// DirectoryInfo dir = app.DirectoryInfo;
173 /// string imagePath = dir.Resource+"notification.png";
174 /// string text = "Click";
175 /// AppControl clickApp = new AppControl();
176 /// clickApp.ApplicationId = "org.tizen.setting"; /// EventNotification noti = new EventNotification();
177 /// noti.AddButton(ButtonIndex.Positive, imagePath, text, clickApp);
180 public void AddButton(ButtonIndex index, string imagePath = "", string text = "", AppControl appControl = null)
182 int ret = Interop.Notification.SetImage(_handle, (NotiImage)((int)index + (int)NotiImage.PositiveButton), imagePath);
183 if (ret != (int)NotificationError.None)
185 throw NotificationErrorFactory.GetException((NotificationError)ret, "set button image path failed");
188 ret = Interop.Notification.SetText(_handle, (NotiText)((int)NotiText.PositiveButton + (int)index), text, null, -1);
189 if (ret != (int)NotificationError.None)
191 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set button text");
194 if(appControl != null)
196 ret = Interop.Notification.SetEventHandler(_handle, (int)index, appControl.SafeAppControlHandle);
197 if (ret != (int)NotificationError.None)
199 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set button event handler");
203 ret = Interop.Notification.AddButton(_handle, (int)index + 1);
204 if(ret != (int)NotificationError.None)
206 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to add button");
211 /// Method to remove a button from an event notification
213 /// <param name="index">Index of button to be removed</param>
215 /// RemoveButton method can be used to remove a button which has been added to the notification.
216 /// RemoveButton should only be called after a button has been added.
220 /// EventNotification noti = new EventNotification();
221 /// noti.AddButton(ButtonIndex.Positive);
223 /// noti.RemoveButton(ButtonIndex.Negative);
226 public void RemoveButton(ButtonIndex index)
228 AppControl app = new AppControl();
230 int ret = Interop.Notification.RemoveButton(_handle, (int)index + 1);
231 if(ret != (int)NotificationError.None)
233 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to remove button");
236 ret = Interop.Notification.SetImage(_handle, (NotiImage)((int)index + (int)NotiImage.PositiveButton), string.Empty);
237 if (ret != (int)NotificationError.None)
239 throw NotificationErrorFactory.GetException((NotificationError)ret, "set button image path failed");
242 ret = Interop.Notification.SetText(_handle, (NotiText)((int)NotiText.PositiveButton + (int)index), string.Empty, null, -1);
243 if (ret != (int)NotificationError.None)
245 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set button text");
248 ret = Interop.Notification.SetEventHandler(_handle, (int)index, app.SafeAppControlHandle);
249 if (ret != (int)NotificationError.None)
251 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set button event handler");