7a358e3fd7d87f89a8026c00c3b194847807ea3d
[platform/core/csapi/tizenfx.git] / Tizen.Applications / Tizen.Applications / Notification.cs
1 // Copyright 2016 by Samsung Electronics, Inc.,
2 //
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.
8
9 using System;
10 using System.Collections;
11 using System.Collections.Generic;
12 using System.Runtime.InteropServices;
13 using Tizen.UI;
14
15 namespace Tizen.Applications.Notifications
16 {
17     /// <summary>
18     /// Structure containing led on and off periods
19     /// </summary>
20     public struct LedBlinkPeriod
21     {
22         /// <summary>
23         /// Constructor
24         /// </summary>
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)
28         {
29             OnTime = on;
30             OffTime = off;
31         }
32
33         /// <summary>
34         /// On time of Led in milliseconds
35         /// </summary>
36         public int OnTime
37         {
38             get;
39             set;
40         }
41
42         /// <summary>
43         /// Off time of Led in milliseconds
44         /// </summary>
45         public int OffTime
46         {
47             get;
48             set;
49         }
50     }
51
52     /// <summary>
53     /// Class containing common properties and methods of Notifications
54     /// </summary>
55     public abstract class Notification
56     {
57         internal const string _logTag = "Tizen.Applications.Notification";
58
59         internal Interop.Notification.SafeNotificationHandle _handle;
60
61         private const int DisableAppLaunchMask = 1 << 1;
62
63         private const int DisableAutoDeleteMask = 1 << 2;
64
65         private const int VolatileDisplayMask = 1 << 8;
66
67         private string _soundPath = string.Empty;
68
69         private string _vibrationPath = null;
70
71         private Color _ledColor = new Color(0, 0, 0, 0);
72
73         /// <summary>
74         /// Insert time of Notification. Default is 1 Jan 1970 9:00:00 until notification is posted.
75         /// </summary>
76         public DateTime InsertTime
77         {
78             get
79             {
80                 long time;
81                 int ret = Interop.Notification.GetInsertTime(_handle, out time);
82                 if (ret != (int)NotificationError.None)
83                 {
84                     Log.Warn(_logTag, "unable to get insert time");
85                     return (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddSeconds(0).ToLocalTime();
86                 }
87
88                 return (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddSeconds(time).ToLocalTime();
89             }
90         }
91
92         /// <summary>
93         /// Title of Notification. Defaults to empty string.
94         /// </summary>
95         public string Title
96         {
97             get
98             {
99                 IntPtr titlePtr;
100                 int ret = Interop.Notification.GetText(_handle, NotiText.Title, out titlePtr);
101                 if(ret != (int)NotificationError.None)
102                 {
103                     Log.Warn(_logTag, "unable to get title");
104                     return string.Empty;
105                 }
106
107                 if (titlePtr == IntPtr.Zero)
108                     return string.Empty;
109
110                 return Marshal.PtrToStringAuto(titlePtr);
111             }
112             set
113             {
114                 int ret = Interop.Notification.SetText(_handle, NotiText.Title, value, null, -1);
115                 if(ret != (int)NotificationError.None)
116                 {
117                     throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set title");
118                 }
119             }
120         }
121
122         /// <summary>
123         /// Content of Notification. Defaults to empty string.
124         /// </summary>
125         public string Content
126         {
127             get
128             {
129                 IntPtr contentPtr;
130                 int ret = Interop.Notification.GetText(_handle, NotiText.Content, out contentPtr);
131                 if(ret != (int)NotificationError.None)
132                 {
133                     Log.Warn(_logTag, "unable to get content");
134                     return string.Empty;
135                 }
136
137                 if (contentPtr == IntPtr.Zero)
138                     return string.Empty;
139
140                 return Marshal.PtrToStringAuto(contentPtr);
141             }
142             set
143             {
144                 int ret = Interop.Notification.SetText(_handle, NotiText.Content, value, null, -1);
145                 if(ret != (int)NotificationError.None)
146                 {
147                     throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set content");
148                 }
149             }
150         }
151
152         /// <summary>
153         /// Gets and sets Icon image Path. Defaults to empty string.
154         /// </summary>
155         public string IconPath
156         {
157             get
158             {
159                 IntPtr pathPtr;
160                 int ret = Interop.Notification.GetImage(_handle, NotiImage.Icon, out pathPtr);
161                 if(ret != (int)NotificationError.None)
162                 {
163                     Log.Warn(_logTag, "unable to get icon image path");
164                     return string.Empty;
165                 }
166
167                 if (pathPtr == IntPtr.Zero)
168                     return string.Empty;
169
170                 return Marshal.PtrToStringAuto(pathPtr);
171             }
172             set
173             {
174                 int ret = Interop.Notification.SetImage(_handle, NotiImage.Icon, value);
175                 if(ret != (int)NotificationError.None)
176                 {
177                     throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set icon image path");
178                 }
179             }
180         }
181
182         /// <summary>
183         /// Gets and sets Indicator icon image Path. Defaults to empty string.
184         /// </summary>
185         public string IndicatorIconPath
186         {
187             get
188             {
189                 IntPtr pathPtr;
190                 int ret = Interop.Notification.GetImage(_handle, NotiImage.Indicator, out pathPtr);
191                 if(ret != (int)NotificationError.None)
192                 {
193                     Log.Warn(_logTag, "unable to get indicator icon image path");
194                     return string.Empty;
195                 }
196
197                 if (pathPtr == IntPtr.Zero)
198                     return string.Empty;
199
200                 return Marshal.PtrToStringAuto(pathPtr);
201             }
202             set
203             {
204                 int ret = Interop.Notification.SetImage(_handle, NotiImage.Indicator, value);
205                 if(ret != (int)NotificationError.None)
206                 {
207                     throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set indicator icon image path");
208                 }
209             }
210         }
211
212         /// <summary>
213         /// Gets and sets Background image Path. Defaults to string.Empty.
214         /// </summary>
215         public string BackgroundImagePath
216         {
217             get
218             {
219                 IntPtr pathPtr;
220                 int ret = Interop.Notification.GetImage(_handle, NotiImage.Background, out pathPtr);
221                 if(ret != (int)NotificationError.None)
222                 {
223                     Log.Warn(_logTag, "unable to get background path");
224                     return string.Empty;
225                 }
226
227                 if (pathPtr == IntPtr.Zero)
228                     return string.Empty;
229
230                 return Marshal.PtrToStringAuto(pathPtr);
231             }
232             set
233             {
234                 int ret = Interop.Notification.SetImage(_handle, NotiImage.Background, value);
235                 if(ret != (int)NotificationError.None)
236                 {
237                     throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set background path");
238                 }
239             }
240         }
241
242         /// <summary>
243         /// Gets and sets SubIcon image Path. Defaults to empty string.
244         /// </summary>
245         public string SubIconPath
246         {
247             get
248             {
249                 IntPtr pathPtr;
250                 int ret = Interop.Notification.GetImage(_handle, NotiImage.SubIcon, out pathPtr);
251                 if(ret != (int)NotificationError.None)
252                 {
253                     Log.Warn(_logTag, "unable to get sub icon path");
254                     return string.Empty;
255                 }
256
257                 if (pathPtr == IntPtr.Zero)
258                     return string.Empty;
259
260                 return Marshal.PtrToStringAuto(pathPtr);
261             }
262             set
263             {
264                 int ret = Interop.Notification.SetImage(_handle, NotiImage.SubIcon, value);
265                 if(ret != (int)NotificationError.None)
266                 {
267                     throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set sub icon path");
268                 }
269             }
270         }
271
272         /// <summary>
273         /// Gets and sets Tag. Defaults to empty string.
274         /// </summary>
275         public string Tag
276         {
277             get
278             {
279                 IntPtr tagPtr;
280                 int ret = Interop.Notification.GetTag(_handle, out tagPtr);
281                 if(ret != (int)NotificationError.None)
282                 {
283                     Log.Warn(_logTag, "unable to get tag");
284                     return string.Empty;
285                 }
286
287                 if (tagPtr == IntPtr.Zero)
288                     return string.Empty;
289
290                 return Marshal.PtrToStringAuto(tagPtr);
291             }
292             set
293             {
294                 int ret = Interop.Notification.SetTag(_handle, value);
295                 if(ret != (int)NotificationError.None)
296                 {
297                     throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set tag");
298                 }
299             }
300         }
301
302         /// <summary>
303         /// Gets and sets Areas to display the notification. Defaults to NotificaitonTray | Ticker | Indicator.
304         /// </summary>
305         public NotificationArea NotificationArea
306         {
307             get
308             {
309                 int appList;
310                 int ret = Interop.Notification.GetApplist(_handle, out appList);
311                 if(ret != (int)NotificationError.None)
312                 {
313                     Log.Warn(_logTag, "unable get applist");
314                     return NotificationArea.None;
315                 }
316
317                 return (NotificationArea)appList;
318             }
319             set
320             {
321                 int ret = Interop.Notification.SetApplist(_handle, (int)value);
322                 if(ret != (int)NotificationError.None)
323                 {
324                     throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set applist");
325                 }
326             }
327         }
328
329         /// <summary>
330         /// Gets the Notification Type.
331         /// </summary>
332         public NotificationType NotificationType
333         {
334             get;
335             protected set;
336         }
337
338         /// <summary>
339         /// Gets and sets the Led on and off time. Defaults to 0 for both.
340         /// </summary>
341         public LedBlinkPeriod LedBlinkPeriod
342         {
343             get
344             {
345                 int on;
346                 int off;
347                 int ret = Interop.Notification.GetLedTimePeriod(_handle, out on, out off);
348                 if(ret != (int)NotificationError.None)
349                 {
350                     Log.Warn(_logTag, "unable to get led time period");
351                     return new LedBlinkPeriod(0,0);
352                 }
353
354                 return new LedBlinkPeriod(on, off);
355             }
356             set
357             {
358                 int ret = Interop.Notification.SetLedTimePeriod(_handle, value.OnTime, value.OffTime);
359                 if(ret != (int)NotificationError.None)
360                 {
361                     throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set led time period");
362                 }
363             }
364         }
365
366         /// <summary>
367         /// Gets and sets value to enable/disable sound for a notification. False by default.
368         /// </summary>
369         public bool SoundEnabled
370         {
371             get
372             {
373                 SoundOption enabled;
374                 IntPtr pathPtr;
375
376                 int ret = Interop.Notification.GetSound(_handle, out enabled, out pathPtr);
377                 if(ret != (int)NotificationError.None)
378                 {
379                     Log.Warn(_logTag, "unable to get sound enabled");
380                     return false;
381                 }
382
383                 if(enabled >= SoundOption.Default)
384                 {
385                     return true;
386                 }
387                 else
388                 {
389                     return false;
390                 }
391             }
392             set
393             {
394                 int ret;
395                 if(value)
396                 {
397                     ret = Interop.Notification.SetSound(_handle, _soundPath == string.Empty || _soundPath == null ? SoundOption.Default : SoundOption.Custom, _soundPath);
398                     if(ret != (int)NotificationError.None)
399                     {
400                         throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set sound enabled ");
401                     }
402                 }
403                 else
404                 {
405                     ret = Interop.Notification.SetSound(_handle, SoundOption.Off, null);
406                     if(ret != (int)NotificationError.None)
407                     {
408                         throw NotificationErrorFactory.GetException((NotificationError)ret, "unable disable sound");
409                     }
410                 }
411             }
412         }
413
414         /// <summary>
415         /// Get and set path of custom sound file to be played for notification.Defaults to string.Empty.
416         /// </summary>
417         public string SoundPath
418         {
419             get
420             {
421                 SoundOption enabled;
422                 IntPtr pathPtr;
423                 int ret = Interop.Notification.GetSound(_handle, out enabled, out pathPtr);
424                 if(ret != (int)NotificationError.None)
425                 {
426                     Log.Warn(_logTag, "unable to get sound path");
427                     return string.Empty;
428                 }
429
430                 if(pathPtr != IntPtr.Zero)
431                     _soundPath = Marshal.PtrToStringAuto(pathPtr);
432
433                 return _soundPath;
434             }
435             set
436             {
437                 int ret;
438                 IntPtr pathPtr;
439                 SoundOption enabled;
440
441                 ret = Interop.Notification.GetSound(_handle, out enabled, out pathPtr);
442                 if(ret != (int)NotificationError.None)
443                 {
444                     throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to get sound enabled");
445                 }
446
447                 if(enabled >= SoundOption.Default)
448                 {
449                     ret = Interop.Notification.SetSound(_handle, value == string.Empty || _soundPath == null ? SoundOption.Default : SoundOption.Custom, value);
450                     if(ret != (int)NotificationError.None)
451                     {
452                         throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set sound path");
453                     }
454                 }
455
456                 _soundPath = value;
457             }
458         }
459
460         /// <summary>
461         /// Gets and sets value to enable/disable vibration for a notification. Defaults to false;
462         /// </summary>
463         public bool VibrationEnabled
464         {
465             get
466             {
467                 VibrationOption enabled;
468                 IntPtr pathPtr;
469                 int ret = Interop.Notification.GetVibration(_handle, out enabled, out pathPtr);
470                 if(ret != (int)NotificationError.None)
471                 {
472                     Log.Warn(_logTag, "unable to get vibration enabled");
473                     return false;
474                 }
475
476                 if(enabled >= VibrationOption.Default)
477                 {
478                     return true;
479                 }
480                 else
481                 {
482                     return false;
483                 }
484             }
485             set
486             {
487                 int ret;
488                 if(value)
489                 {
490                     ret = Interop.Notification.SetVibration(_handle, _vibrationPath == null ? VibrationOption.Default : VibrationOption.Custom, _vibrationPath);
491                     if(ret != (int)NotificationError.None)
492                     {
493                         throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set vibration enabled ");
494                     }
495                 }
496                 else
497                 {
498                     ret = Interop.Notification.SetVibration(_handle, VibrationOption.Off, null);
499                     if(ret != (int)NotificationError.None)
500                     {
501                         throw NotificationErrorFactory.GetException((NotificationError)ret, "unable disable vibration");
502                     }
503                 }
504             }
505         }
506
507         /// <summary>
508         /// Get and sets value to enable/disable led. Defaults to false.
509         /// </summary>
510         public bool LedEnabled
511         {
512             get
513             {
514                 LedOption enabled;
515                 int c;
516                 int ret = Interop.Notification.GetLed(_handle, out enabled, out c);
517                 if(ret != (int)NotificationError.None)
518                 {
519                     Log.Warn(_logTag, "unable to get led enabled");
520                     return false;
521                 }
522
523                 if(enabled >= LedOption.On)
524                 {
525                     return true;
526                 }
527                 else
528                 {
529                     return false;
530                 }
531             }
532             set
533             {
534                 int ret;
535                 if(value)
536                 {
537                     ret = Interop.Notification.SetLed(_handle, LedOption.On, _ledColor.Argb);
538                     if(ret != (int)NotificationError.None)
539                     {
540                         throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set led enabled");
541                     }
542                 }
543                 else
544                 {
545                     ret = Interop.Notification.SetLed(_handle, LedOption.Off, 0);
546                     if(ret != (int)NotificationError.None)
547                     {
548                         throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set led enabled");
549                     }
550                 }
551             }
552         }
553
554         /// <summary>
555         /// Gets and sets custom led color for led. Default to Color(0, 0, 0, 0).
556         /// </summary>
557         public Color LedColor
558         {
559             get
560             {
561                 LedOption enabled;
562                 int c;
563                 int ret = Interop.Notification.GetLed(_handle, out enabled, out c);
564                 if(ret != (int)NotificationError.None)
565                 {
566                     Log.Warn(_logTag, "unable to get led color");
567                     return _ledColor;
568                 }
569
570                 if(c != 0)
571                     _ledColor = new Color(c >> 16 & 255, c >> 8 & 255, c >> 0 & 255, c >> 24 & 255);
572
573                 return _ledColor;
574             }
575             set
576             {
577                 LedOption enabled;
578                 int c;
579                 int ret = Interop.Notification.GetLed(_handle, out enabled, out c);
580                 if(ret != (int)NotificationError.None)
581                 {
582                     throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to get led enabled");
583                 }
584
585                 if(enabled >= LedOption.On)
586                 {
587                     ret = Interop.Notification.SetLed(_handle, LedOption.On, value.Argb);
588                     if(ret != (int)NotificationError.None)
589                     {
590                         throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set led color");
591                     }
592                 }
593
594                 _ledColor = value;
595             }
596         }
597
598         /// <summary>
599         /// Gets and sets value to enable/disable Volatile Display property. Defaults to false.
600         /// </summary>
601         public bool VolatileDisplayEnabled
602         {
603             get
604             {
605                 int result;
606                 int ret = Interop.Notification.GetProperties(_handle, out result);
607                 if(ret != (int)NotificationError.None)
608                 {
609                     Log.Warn(_logTag, "unable to get disable app launch");
610                     return false;
611                 }
612
613                 if((result & VolatileDisplayMask) != 0)
614                     return true;
615                 else
616                     return false;
617             }
618             set
619             {
620                 int result = 0;
621
622                 int ret = Interop.Notification.GetProperties(_handle, out result);
623                 if(ret != (int)NotificationError.None)
624                 {
625                     throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to get property values");
626                 }
627
628                 if(value)
629                     result = result | VolatileDisplayMask;
630                 else
631                     result = result & ~VolatileDisplayMask;
632
633                 ret = Interop.Notification.SetProperties(_handle, result);
634                 if(ret != (int)NotificationError.None)
635                 {
636                     throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set properties");
637                 }
638             }
639         }
640
641         /// <summary>
642         /// Gets and sets Thumbnail path for notification. Defaults to string.Empty.
643         /// </summary>
644         public string ThumbnailPath
645         {
646             get
647             {
648                 IntPtr pathPtr;
649                 string path;
650                 int ret = Interop.Notification.GetImage(_handle, NotiImage.Thumbnail, out pathPtr);
651                 if(ret != (int)NotificationError.None)
652                 {
653                     Log.Warn(_logTag, "unable to get thumbnail Path");
654                     return string.Empty;
655                 }
656
657                 if(pathPtr == IntPtr.Zero)
658                     return string.Empty;
659
660                 path = Marshal.PtrToStringAuto(pathPtr);
661                 return path;
662             }
663             set
664             {
665                 int ret = Interop.Notification.SetImage(_handle, NotiImage.Thumbnail, value);
666                 if(ret != (int)NotificationError.None)
667                 {
668                     throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set thumbnail path");
669                 }
670             }
671         }
672
673         /// <summary>
674         /// Method for setting Notification Appcontrol which is invoked when notification is clicked
675         /// </summary>
676         /// <param name="app">AppControl object to set</param>
677         public void SetLaunchAppControl(AppControl app)
678         {
679             if(app == null)
680             {
681                 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
682             }
683
684             int ret = Interop.Notification.SetAppControl(_handle, LaunchOption.AppControl, app.SafeAppControlHandle);
685             if(ret != (int)NotificationError.None)
686             {
687                 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set app control");
688             }
689         }
690
691         /// <summary>
692         /// Method for getting Notification AppControl which will be invoked when notification is clicked
693         /// </summary>
694         /// <returns>AppControl object which was set</returns>
695         public AppControl GetLaunchAppControl()
696         {
697             SafeAppControlHandle app;
698             int ret = Interop.Notification.GetAppControl(_handle, LaunchOption.AppControl, out app);
699             if(ret != (int)NotificationError.None)
700             {
701                 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to get app control");
702             }
703
704             if(app.IsInvalid)
705                 return null;
706
707             return new AppControl(app);
708         }
709     }
710 }