Merge remote-tracking branch 'nui/merge_work'
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.Notification / Tizen.Applications.Notifications / NotificationBinder.cs
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 namespace Tizen.Applications.Notifications
18 {
19     using System;
20
21     internal static class NotificationBinder
22     {
23         private static readonly int DoNotShowTimeStamp = -1;
24
25         internal static void BindObject(Notification notification)
26         {
27             BindNotificationSafeHandle(notification);
28             BindNotificationText(notification);
29             BindNotificationTime(notification);
30
31             Interop.Notification.SetID(notification.Handle, notification.PrivID);
32
33             if (notification.IsVisible)
34             {
35                 Interop.Notification.SetApplist(notification.Handle, (int)NotificationDisplayApplist.Tray);
36             }
37             else
38             {
39                 Interop.Notification.SetApplist(notification.Handle, 0);
40             }
41
42             if (notification.IsOngoing == true)
43             {
44                 Log.Info(Notification.LogTag, "Start to set IsOngoing to SafeHandle");
45                 Interop.Notification.SetLayout(notification.Handle, NotificationLayout.Ongoing);
46                 Interop.Notification.SetOngoingFlag(notification.Handle, true);
47             }
48
49             if (string.IsNullOrEmpty(notification.Tag) != true)
50             {
51                 Interop.Notification.SetTag(notification.Handle, notification.Tag);
52             }
53
54             if (notification.Action != null && notification.Action.SafeAppControlHandle.IsInvalid == false)
55             {
56                 Interop.Notification.SetAppControl(notification.Handle, LaunchOption.AppControl, notification.Action.SafeAppControlHandle);
57             }
58
59             Interop.Notification.SetProperties(notification.Handle, (int)notification.Property);
60         }
61
62         internal static void BindSafeHandle(Notification notification)
63         {
64             int privID, groupID;
65             Interop.Notification.GetID(notification.Handle, out groupID, out privID);
66             notification.PrivID = privID;
67
68             NotificationLayout layout;
69             Interop.Notification.GetLayout(notification.Handle, out layout);
70             NotificationType type;
71             Interop.Notification.GetType(notification.Handle, out type);
72             if (layout == NotificationLayout.Ongoing && type == NotificationType.Ongoing)
73             {
74                 notification.IsOngoing = true;
75             }
76
77             int appList;
78             Interop.Notification.GetApplist(notification.Handle, out appList);
79             if ((appList & (int)NotificationDisplayApplist.Tray) == 0)
80             {
81                 notification.IsVisible = false;
82             }
83
84             BindSafeHandleText(notification);
85             BindSafeHandleTime(notification);
86             BindSafeHandleTag(notification);
87             BindSafeHandleAction(notification);
88         }
89
90         private static void BindNotificationSafeHandle(Notification notification)
91         {
92             IntPtr ptr;
93             NotificationError ret;
94
95             if (notification.Handle != null && notification.Handle.IsInvalid == false)
96             {
97                 notification.Handle.Dispose();
98             }
99
100             if (notification.IsOngoing == true || notification.Progress != null)
101             {
102                 ptr = Interop.Notification.Create(NotificationType.Ongoing);
103             }
104             else
105             {
106                 ptr = Interop.Notification.Create(NotificationType.Basic);
107             }
108
109             if (ptr == IntPtr.Zero)
110             {
111                 ret = (NotificationError)Tizen.Internals.Errors.ErrorFacts.GetLastResult();
112                 throw NotificationErrorFactory.GetException(ret, "Unable to create IntPtr Notification");
113             }
114
115             notification.Handle = new NotificationSafeHandle(ptr, true);
116         }
117
118         private static void BindNotificationText(Notification notification)
119         {
120             Interop.Notification.SetText(notification.Handle, NotificationText.Title, notification.Title, null, -1);
121             Interop.Notification.SetText(notification.Handle, NotificationText.Content, notification.Content, null, -1);
122             Interop.Notification.SetImage(notification.Handle, NotificationImage.Icon, notification.Icon);
123             Interop.Notification.SetImage(notification.Handle, NotificationImage.SubIcon, notification.SubIcon);
124             Interop.Notification.SetText(notification.Handle, NotificationText.EventCount, notification.Count.ToString(), null, -1);
125         }
126
127         private static void BindNotificationTime(Notification notification)
128         {
129             if (notification.IsTimeStampVisible == true)
130             {
131                 if (notification.TimeStamp != DateTime.MinValue)
132                 {
133                     TimeSpan datatime = notification.TimeStamp.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
134                     Interop.Notification.SetTime(notification.Handle, (int)datatime.TotalSeconds);
135                 }
136             }
137             else
138             {
139                 Interop.Notification.SetTime(notification.Handle, DoNotShowTimeStamp);
140             }
141         }
142
143         private static void BindSafeHandleText(Notification notification)
144         {
145             string text;
146             Interop.Notification.GetText(notification.Handle, NotificationText.Title, out text);
147             if (string.IsNullOrEmpty(text) == false)
148             {
149                 notification.Title = text;
150             }
151
152             Interop.Notification.GetText(notification.Handle, NotificationText.Content, out text);
153             if (string.IsNullOrEmpty(text) == false)
154             {
155                 notification.Content = text;
156             }
157
158             string path;
159             Interop.Notification.GetImage(notification.Handle, NotificationImage.Icon, out path);
160             if (string.IsNullOrEmpty(path) == false)
161             {
162                 notification.Icon = path;
163             }
164
165             Interop.Notification.GetImage(notification.Handle, NotificationImage.SubIcon, out path);
166             if (string.IsNullOrEmpty(path) == false)
167             {
168                 notification.SubIcon = path;
169             }
170
171             Interop.Notification.GetText(notification.Handle, NotificationText.EventCount, out text);
172             if (string.IsNullOrEmpty(text) == false)
173             {
174                 try
175                 {
176                     notification.Count = int.Parse(text);
177                 }
178                 catch (Exception ex)
179                 {
180                     Log.Error(Notification.LogTag, ex.ToString());
181                 }
182             }
183         }
184
185         private static void BindSafeHandleTime(Notification notification)
186         {
187             int time;
188
189             Interop.Notification.GetTime(notification.Handle, out time);
190
191             if (time == DoNotShowTimeStamp)
192             {
193                 notification.IsTimeStampVisible = false;
194             }
195             else
196             {
197                 notification.IsTimeStampVisible = true;
198
199                 if (time != 0)
200                 {
201                     notification.TimeStamp = (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Local)).AddSeconds(time).ToLocalTime();
202                 }
203             }
204         }
205
206         private static void BindSafeHandleTag(Notification notification)
207         {
208             string text;
209             Interop.Notification.GetTag(notification.Handle, out text);
210             if (string.IsNullOrEmpty(text) != true)
211             {
212                 notification.Tag = text;
213             }
214         }
215
216         private static void BindSafeHandleProperty(Notification notification)
217         {
218             int property;
219             Interop.Notification.GetProperties(notification.Handle, out property);
220             notification.Property = (NotificationProperty)property;
221         }
222
223         private static void BindSafeHandleAction(Notification notification)
224         {
225             SafeAppControlHandle appcontrol = null;
226             Interop.Notification.GetAppControl(notification.Handle, LaunchOption.AppControl, out appcontrol);
227             if (appcontrol != null && appcontrol.IsInvalid == false)
228             {
229                 notification.Action = new AppControl(appcontrol);
230             }
231         }
232     }
233 }