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.Collections;
11 using System.Collections.Generic;
12 using System.Runtime.InteropServices;
15 namespace Tizen.Applications.Notifications
18 /// Structure containing led on and off periods
20 public struct LedBlinkPeriod
25 /// <param name="on">On time of led in ms</param>
26 /// <param name="off">Off time of led in ms</param>
27 public LedBlinkPeriod(int on, int off)
34 /// On time of Led in milliseconds
43 /// Off time of Led in milliseconds
53 /// Class containing common properties and methods of Notifications
55 public abstract class Notification
57 internal const string _logTag = "Tizen.Applications.Notification";
59 internal Interop.Notification.SafeNotificationHandle _handle;
61 private const int DisableAppLaunchMask = 1 << 1;
63 private const int DisableAutoDeleteMask = 1 << 2;
65 private const int VolatileDisplayMask = 1 << 8;
67 private string _soundPath = string.Empty;
69 private string _vibrationPath = null;
71 private Color _ledColor = new Color(0, 0, 0, 0);
74 /// Insert time of Notification. Default is 1 Jan 1970 9:00:00 until notification is posted.
76 public DateTime InsertTime
81 int ret = Interop.Notification.GetInsertTime(_handle, out time);
82 if (ret != (int)NotificationError.None)
84 Log.Warn(_logTag, "unable to get insert time");
85 return (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddSeconds(0).ToLocalTime();
88 return (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddSeconds(time).ToLocalTime();
93 /// Title of Notification. Defaults to empty string.
100 int ret = Interop.Notification.GetText(_handle, NotiText.Title, out titlePtr);
101 if(ret != (int)NotificationError.None)
103 Log.Warn(_logTag, "unable to get title");
107 if (titlePtr == IntPtr.Zero)
110 return Marshal.PtrToStringAuto(titlePtr);
114 int ret = Interop.Notification.SetText(_handle, NotiText.Title, value, null, -1);
115 if(ret != (int)NotificationError.None)
117 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set title");
123 /// Content of Notification. Defaults to empty string.
125 public string Content
130 int ret = Interop.Notification.GetText(_handle, NotiText.Content, out contentPtr);
131 if(ret != (int)NotificationError.None)
133 Log.Warn(_logTag, "unable to get content");
137 if (contentPtr == IntPtr.Zero)
140 return Marshal.PtrToStringAuto(contentPtr);
144 int ret = Interop.Notification.SetText(_handle, NotiText.Content, value, null, -1);
145 if(ret != (int)NotificationError.None)
147 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set content");
153 /// Gets and sets Icon image Path. Defaults to empty string.
155 public string IconPath
160 int ret = Interop.Notification.GetImage(_handle, NotiImage.Icon, out pathPtr);
161 if(ret != (int)NotificationError.None)
163 Log.Warn(_logTag, "unable to get icon image path");
167 if (pathPtr == IntPtr.Zero)
170 return Marshal.PtrToStringAuto(pathPtr);
174 int ret = Interop.Notification.SetImage(_handle, NotiImage.Icon, value);
175 if(ret != (int)NotificationError.None)
177 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set icon image path");
183 /// Gets and sets Indicator icon image Path. Defaults to empty string.
185 public string IndicatorIconPath
190 int ret = Interop.Notification.GetImage(_handle, NotiImage.Indicator, out pathPtr);
191 if(ret != (int)NotificationError.None)
193 Log.Warn(_logTag, "unable to get indicator icon image path");
197 if (pathPtr == IntPtr.Zero)
200 return Marshal.PtrToStringAuto(pathPtr);
204 int ret = Interop.Notification.SetImage(_handle, NotiImage.Indicator, value);
205 if(ret != (int)NotificationError.None)
207 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set indicator icon image path");
213 /// Gets and sets Background image Path. Defaults to string.Empty.
215 public string BackgroundImagePath
220 int ret = Interop.Notification.GetImage(_handle, NotiImage.Background, out pathPtr);
221 if(ret != (int)NotificationError.None)
223 Log.Warn(_logTag, "unable to get background path");
227 if (pathPtr == IntPtr.Zero)
230 return Marshal.PtrToStringAuto(pathPtr);
234 int ret = Interop.Notification.SetImage(_handle, NotiImage.Background, value);
235 if(ret != (int)NotificationError.None)
237 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set background path");
243 /// Gets and sets SubIcon image Path. Defaults to empty string.
245 public string SubIconPath
250 int ret = Interop.Notification.GetImage(_handle, NotiImage.SubIcon, out pathPtr);
251 if(ret != (int)NotificationError.None)
253 Log.Warn(_logTag, "unable to get sub icon path");
257 if (pathPtr == IntPtr.Zero)
260 return Marshal.PtrToStringAuto(pathPtr);
264 int ret = Interop.Notification.SetImage(_handle, NotiImage.SubIcon, value);
265 if(ret != (int)NotificationError.None)
267 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set sub icon path");
273 /// Gets and sets Tag. Defaults to empty string.
280 int ret = Interop.Notification.GetTag(_handle, out tagPtr);
281 if(ret != (int)NotificationError.None)
283 Log.Warn(_logTag, "unable to get tag");
287 if (tagPtr == IntPtr.Zero)
290 return Marshal.PtrToStringAuto(tagPtr);
294 int ret = Interop.Notification.SetTag(_handle, value);
295 if(ret != (int)NotificationError.None)
297 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set tag");
303 /// Gets and sets Areas to display the notification. Defaults to NotificaitonTray | Ticker | Indicator.
305 public NotificationArea NotificationArea
310 int ret = Interop.Notification.GetApplist(_handle, out appList);
311 if(ret != (int)NotificationError.None)
313 Log.Warn(_logTag, "unable get applist");
314 return NotificationArea.None;
317 return (NotificationArea)appList;
321 int ret = Interop.Notification.SetApplist(_handle, (int)value);
322 if(ret != (int)NotificationError.None)
324 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set applist");
330 /// Gets the Notification Type.
332 public NotificationType NotificationType
339 /// Gets and sets the Led on and off time. Defaults to 0 for both.
341 public LedBlinkPeriod LedBlinkPeriod
347 int ret = Interop.Notification.GetLedTimePeriod(_handle, out on, out off);
348 if(ret != (int)NotificationError.None)
350 Log.Warn(_logTag, "unable to get led time period");
351 return new LedBlinkPeriod(0,0);
354 return new LedBlinkPeriod(on, off);
358 int ret = Interop.Notification.SetLedTimePeriod(_handle, value.OnTime, value.OffTime);
359 if(ret != (int)NotificationError.None)
361 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set led time period");
367 /// Gets and sets value to enable/disable sound for a notification. False by default.
369 public bool SoundEnabled
376 int ret = Interop.Notification.GetSound(_handle, out enabled, out pathPtr);
377 if(ret != (int)NotificationError.None)
379 Log.Warn(_logTag, "unable to get sound enabled");
383 if(enabled >= SoundOption.Default)
397 ret = Interop.Notification.SetSound(_handle, _soundPath == string.Empty || _soundPath == null ? SoundOption.Default : SoundOption.Custom, _soundPath);
398 if(ret != (int)NotificationError.None)
400 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set sound enabled ");
405 ret = Interop.Notification.SetSound(_handle, SoundOption.Off, null);
406 if(ret != (int)NotificationError.None)
408 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable disable sound");
415 /// Get and set path of custom sound file to be played for notification.Defaults to string.Empty.
417 public string SoundPath
423 int ret = Interop.Notification.GetSound(_handle, out enabled, out pathPtr);
424 if(ret != (int)NotificationError.None)
426 Log.Warn(_logTag, "unable to get sound path");
430 if(pathPtr != IntPtr.Zero)
431 _soundPath = Marshal.PtrToStringAuto(pathPtr);
441 ret = Interop.Notification.GetSound(_handle, out enabled, out pathPtr);
442 if(ret != (int)NotificationError.None)
444 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to get sound enabled");
447 if(enabled >= SoundOption.Default)
449 ret = Interop.Notification.SetSound(_handle, value == string.Empty || _soundPath == null ? SoundOption.Default : SoundOption.Custom, value);
450 if(ret != (int)NotificationError.None)
452 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set sound path");
461 /// Gets and sets value to enable/disable vibration for a notification. Defaults to false;
463 public bool VibrationEnabled
467 VibrationOption enabled;
469 int ret = Interop.Notification.GetVibration(_handle, out enabled, out pathPtr);
470 if(ret != (int)NotificationError.None)
472 Log.Warn(_logTag, "unable to get vibration enabled");
476 if(enabled >= VibrationOption.Default)
490 ret = Interop.Notification.SetVibration(_handle, _vibrationPath == null ? VibrationOption.Default : VibrationOption.Custom, _vibrationPath);
491 if(ret != (int)NotificationError.None)
493 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set vibration enabled ");
498 ret = Interop.Notification.SetVibration(_handle, VibrationOption.Off, null);
499 if(ret != (int)NotificationError.None)
501 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable disable vibration");
508 /// Get and sets value to enable/disable led. Defaults to false.
510 public bool LedEnabled
516 int ret = Interop.Notification.GetLed(_handle, out enabled, out c);
517 if(ret != (int)NotificationError.None)
519 Log.Warn(_logTag, "unable to get led enabled");
523 if(enabled >= LedOption.On)
537 ret = Interop.Notification.SetLed(_handle, LedOption.On, _ledColor.Argb);
538 if(ret != (int)NotificationError.None)
540 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set led enabled");
545 ret = Interop.Notification.SetLed(_handle, LedOption.Off, 0);
546 if(ret != (int)NotificationError.None)
548 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set led enabled");
555 /// Gets and sets custom led color for led. Default to Color(0, 0, 0, 0).
557 public Color LedColor
563 int ret = Interop.Notification.GetLed(_handle, out enabled, out c);
564 if(ret != (int)NotificationError.None)
566 Log.Warn(_logTag, "unable to get led color");
571 _ledColor = new Color(c >> 16 & 255, c >> 8 & 255, c >> 0 & 255, c >> 24 & 255);
579 int ret = Interop.Notification.GetLed(_handle, out enabled, out c);
580 if(ret != (int)NotificationError.None)
582 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to get led enabled");
585 if(enabled >= LedOption.On)
587 ret = Interop.Notification.SetLed(_handle, LedOption.On, value.Argb);
588 if(ret != (int)NotificationError.None)
590 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set led color");
599 /// Gets and sets value to enable/disable Volatile Display property. Defaults to false.
601 public bool VolatileDisplayEnabled
606 int ret = Interop.Notification.GetProperties(_handle, out result);
607 if(ret != (int)NotificationError.None)
609 Log.Warn(_logTag, "unable to get disable app launch");
613 if((result & VolatileDisplayMask) != 0)
622 int ret = Interop.Notification.GetProperties(_handle, out result);
623 if(ret != (int)NotificationError.None)
625 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to get property values");
629 result = result | VolatileDisplayMask;
631 result = result & ~VolatileDisplayMask;
633 ret = Interop.Notification.SetProperties(_handle, result);
634 if(ret != (int)NotificationError.None)
636 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set properties");
642 /// Gets and sets Thumbnail path for notification. Defaults to string.Empty.
644 public string ThumbnailPath
650 int ret = Interop.Notification.GetImage(_handle, NotiImage.Thumbnail, out pathPtr);
651 if(ret != (int)NotificationError.None)
653 Log.Warn(_logTag, "unable to get thumbnail Path");
657 if(pathPtr == IntPtr.Zero)
660 path = Marshal.PtrToStringAuto(pathPtr);
665 int ret = Interop.Notification.SetImage(_handle, NotiImage.Thumbnail, value);
666 if(ret != (int)NotificationError.None)
668 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set thumbnail path");
674 /// Method for setting Notification Appcontrol which is invoked when notification is clicked
676 /// <param name="app">AppControl object to set</param>
677 public void SetLaunchAppControl(AppControl app)
681 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
684 int ret = Interop.Notification.SetAppControl(_handle, LaunchOption.AppControl, app.SafeAppControlHandle);
685 if(ret != (int)NotificationError.None)
687 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set app control");
692 /// Method for getting Notification AppControl which will be invoked when notification is clicked
694 /// <returns>AppControl object which was set</returns>
695 public AppControl GetLaunchAppControl()
697 SafeAppControlHandle app;
698 int ret = Interop.Notification.GetAppControl(_handle, LaunchOption.AppControl, out app);
699 if(ret != (int)NotificationError.None)
701 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to get app control");
707 return new AppControl(app);