2 * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 namespace Tizen.Applications.Notifications
21 internal static class NotificationBinder
23 private static readonly int DoNotShowTimeStamp = -1;
25 internal static void BindObject(Notification notification)
27 BindNotificationSafeHandle(notification);
28 BindNotificationText(notification);
29 BindNotificationTime(notification);
31 Interop.Notification.SetID(notification.Handle, notification.PrivID);
33 if (notification.IsDisplay)
35 Interop.Notification.SetApplist(notification.Handle, (int)NotificationDisplayApplist.Tray);
39 Interop.Notification.SetApplist(notification.Handle, 0);
42 if (notification.IsOngoing == true)
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);
49 if (string.IsNullOrEmpty(notification.Tag) != true)
51 Interop.Notification.SetTag(notification.Handle, notification.Tag);
54 if (notification.Action != null && notification.Action.SafeAppControlHandle.IsInvalid == false)
56 Interop.Notification.SetAppControl(notification.Handle, LaunchOption.AppControl, notification.Action.SafeAppControlHandle);
59 Interop.Notification.SetProperties(notification.Handle, (int)notification.Property);
62 internal static void BindSafeHandle(Notification notification)
65 Interop.Notification.GetID(notification.Handle, out groupID, out privID);
66 notification.PrivID = privID;
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)
74 notification.IsOngoing = true;
78 Interop.Notification.GetApplist(notification.Handle, out appList);
79 if ((appList & (int)NotificationDisplayApplist.Tray) == 0)
81 notification.IsDisplay = false;
84 BindSafeHandleText(notification);
85 BindSafeHandleTime(notification);
86 BindSafeHandleTag(notification);
87 BindSafeHandleAction(notification);
90 private static void BindNotificationSafeHandle(Notification notification)
93 NotificationError ret;
95 if (notification.Handle != null && notification.Handle.IsInvalid == false)
97 notification.Handle.Dispose();
100 if (notification.IsOngoing == true || notification.Progress != null)
102 ptr = Interop.Notification.Create(NotificationType.Ongoing);
106 ptr = Interop.Notification.Create(NotificationType.Basic);
109 if (ptr == IntPtr.Zero)
111 ret = (NotificationError)Tizen.Internals.Errors.ErrorFacts.GetLastResult();
112 throw NotificationErrorFactory.GetException(ret, "Unable to create IntPtr Notification");
115 notification.Handle = new NotificationSafeHandle(ptr, true);
118 private static void BindNotificationText(Notification notification)
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);
127 private static void BindNotificationTime(Notification notification)
129 if (notification.IsTimeStampVisible == true)
131 if (notification.TimeStamp != DateTime.MinValue)
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);
139 Interop.Notification.SetTime(notification.Handle, DoNotShowTimeStamp);
143 private static void BindSafeHandleText(Notification notification)
146 Interop.Notification.GetText(notification.Handle, NotificationText.Title, out text);
147 if (string.IsNullOrEmpty(text) == false)
149 notification.Title = text;
152 Interop.Notification.GetText(notification.Handle, NotificationText.Content, out text);
153 if (string.IsNullOrEmpty(text) == false)
155 notification.Content = text;
159 Interop.Notification.GetImage(notification.Handle, NotificationImage.Icon, out path);
160 if (string.IsNullOrEmpty(path) == false)
162 notification.Icon = path;
165 Interop.Notification.GetImage(notification.Handle, NotificationImage.SubIcon, out path);
166 if (string.IsNullOrEmpty(path) == false)
168 notification.SubIcon = path;
171 Interop.Notification.GetText(notification.Handle, NotificationText.EventCount, out text);
172 if (string.IsNullOrEmpty(text) == false)
176 notification.Count = int.Parse(text);
180 Log.Error(Notification.LogTag, ex.ToString());
185 private static void BindSafeHandleTime(Notification notification)
189 Interop.Notification.GetTime(notification.Handle, out time);
191 if (time == DoNotShowTimeStamp)
193 notification.IsTimeStampVisible = false;
197 notification.IsTimeStampVisible = true;
201 notification.TimeStamp = (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Local)).AddSeconds(time).ToLocalTime();
206 private static void BindSafeHandleTag(Notification notification)
209 Interop.Notification.GetTag(notification.Handle, out text);
210 if (string.IsNullOrEmpty(text) != true)
212 notification.Tag = text;
216 private static void BindSafeHandleProperty(Notification notification)
219 Interop.Notification.GetProperties(notification.Handle, out property);
220 notification.Property = (NotificationProperty)property;
223 private static void BindSafeHandleAction(Notification notification)
225 SafeAppControlHandle appcontrol = null;
226 Interop.Notification.GetAppControl(notification.Handle, LaunchOption.AppControl, out appcontrol);
227 if (appcontrol != null && appcontrol.IsInvalid == false)
229 notification.Action = new AppControl(appcontrol);