Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.NotificationEventListener / Tizen.Applications.NotificationEventListener / NotificationEventArgsBinder.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.NotificationEventListener
18 {
19     using System;
20
21     internal static class NotificationEventArgsBinder
22     {
23         private const string LogTag = "Tizen.Applications.NotificationEventListener";
24
25         internal static NotificationEventArgs BindObject(IntPtr notification, bool data)
26         {
27             Interop.NotificationEventListener.ErrorCode err;
28             int time;
29             int uniqueNumber = -1;
30             int groupNumber = -1;
31             int property;
32             int doNotShowTimeStamp = -1;
33             int displayList;
34             bool eventFlag = false;
35             NotificationLayout layout;
36             NotificationType type;
37             string text;
38             IntPtr extention = IntPtr.Zero;
39             IntPtr dummy = IntPtr.Zero;
40             SafeAppControlHandle appcontrol = null;
41
42             NotificationEventArgs eventargs = new NotificationEventArgs();
43
44             eventargs.Handle = new Interop.NotificationEventListener.NotificationSafeHandle(notification, data);
45
46             err = Interop.NotificationEventListener.GetID(eventargs.Handle, out groupNumber, out uniqueNumber);
47             if (err != Interop.NotificationEventListener.ErrorCode.None)
48             {
49                 Log.Error(LogTag, "unable to get UniqueNumber");
50             }
51
52             eventargs.UniqueNumber = uniqueNumber;
53
54             Interop.NotificationEventListener.GetAppId(eventargs.Handle, out text);
55             if (string.IsNullOrEmpty(text) == false)
56             {
57                 eventargs.AppID = text;
58             }
59
60             Interop.NotificationEventListener.GetText(eventargs.Handle, NotificationText.Title, out text);
61             if (string.IsNullOrEmpty(text) == false)
62             {
63                 eventargs.Title = text;
64             }
65
66             Interop.NotificationEventListener.GetText(eventargs.Handle, NotificationText.Content, out text);
67             if (string.IsNullOrEmpty(text) == false)
68             {
69                 eventargs.Content = text;
70             }
71
72             Interop.NotificationEventListener.GetImage(eventargs.Handle, NotificationImage.Icon, out text);
73             if (string.IsNullOrEmpty(text) == false)
74             {
75                 eventargs.Icon = text;
76             }
77
78             Interop.NotificationEventListener.GetImage(eventargs.Handle, NotificationImage.SubIcon, out text);
79             if (string.IsNullOrEmpty(text) == false)
80             {
81                 eventargs.SubIcon = text;
82             }
83
84             err = Interop.NotificationEventListener.GetTime(eventargs.Handle, out time);
85             if (err != Interop.NotificationEventListener.ErrorCode.None)
86             {
87                 Log.Info(LogTag, "unable to get TimeStamp");
88             }
89
90             if (time == doNotShowTimeStamp)
91             {
92                 eventargs.IsTimeStampVisible = false;
93             }
94             else
95             {
96                 eventargs.IsTimeStampVisible = true;
97
98                 if (time == 0)
99                 {
100                     err = Interop.NotificationEventListener.GetInsertTime(eventargs.Handle, out time);
101                     if (err != Interop.NotificationEventListener.ErrorCode.None)
102                     {
103                         Log.Info(LogTag, "unable to get InsertTime");
104                     }
105                 }
106
107                 eventargs.TimeStamp = (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddSeconds(time).ToLocalTime();
108             }
109
110             err = Interop.NotificationEventListener.GetText(eventargs.Handle, NotificationText.EventCount, out text);
111             if (err != Interop.NotificationEventListener.ErrorCode.None)
112             {
113                 Log.Info(LogTag, "unable to get Event Count");
114             }
115
116             if (string.IsNullOrEmpty(text) == false)
117             {
118                 try
119                 {
120                     eventargs.Count = int.Parse(text);
121                 }
122                 catch (Exception ex)
123                 {
124                     Log.Error(LogTag, ex.ToString());
125                 }
126             }
127
128             err = Interop.NotificationEventListener.GetAppControl(eventargs.Handle, LaunchOption.AppControl, out appcontrol);
129             if (err != Interop.NotificationEventListener.ErrorCode.None)
130             {
131                 Log.Info(LogTag, "unable to get Action");
132             }
133
134             if (appcontrol != null && appcontrol.IsInvalid == false)
135             {
136                 eventargs.Action = new AppControl(appcontrol);
137             }
138
139             Interop.NotificationEventListener.GetTag(eventargs.Handle, out text);
140             if (string.IsNullOrEmpty(text) == false)
141             {
142                 eventargs.Tag = text;
143             }
144
145             err = Interop.NotificationEventListener.GetProperties(eventargs.Handle, out property);
146             if (err != Interop.NotificationEventListener.ErrorCode.None)
147             {
148                 Log.Info(LogTag, "unable to get Property");
149             }
150             else
151             {
152                 eventargs.Property = (NotificationProperty)property;
153             }
154
155             Interop.NotificationEventListener.GetStyleList(eventargs.Handle, out displayList);
156             if ((displayList & (int)NotificationDisplayApplist.Tray) == 0)
157             {
158                 eventargs.IsDisplay = false;
159             }
160
161             err = Interop.NotificationEventListener.GetExtentionBundle(eventargs.Handle, out extention, out dummy);
162             if (err != Interop.NotificationEventListener.ErrorCode.None)
163             {
164                 Log.Info(LogTag, "unable to get Extender");
165             }
166
167             if (extention != IntPtr.Zero)
168             {
169                 Bundle bundle = new Bundle(new SafeBundleHandle(extention, false));
170                 foreach (string key in bundle.Keys)
171                 {
172                     SafeBundleHandle sbh;
173                     Interop.NotificationEventListener.GetExtender(eventargs.Handle, key, out sbh);
174                     eventargs.Extender.Add(key, new Bundle(sbh));
175                 }
176             }
177
178             err = Interop.NotificationEventListener.GetLayout(eventargs.Handle, out layout);
179             if (err != Interop.NotificationEventListener.ErrorCode.None)
180             {
181                 Log.Info(LogTag, "unable to get layout");
182             }
183
184             err = Interop.NotificationEventListener.GetType(eventargs.Handle, out type);
185             if (err != Interop.NotificationEventListener.ErrorCode.None)
186             {
187                 Log.Info(LogTag, "unable to get type");
188             }
189
190             if (layout == NotificationLayout.OngoingEvent && type == NotificationType.Ongoing)
191             {
192                 eventargs.IsOngoing = true;
193             }
194
195             err = Interop.NotificationEventListener.GetEventFlag(eventargs.Handle, out eventFlag);
196             if (err != Interop.NotificationEventListener.ErrorCode.None)
197             {
198                 Log.Info(LogTag, "unable to get event flag");
199             }
200
201             eventargs.HasEventFlag = eventFlag;
202
203             NotificationAccessoryAgsBinder.BindObject(eventargs);
204             NotificationStyleArgBinder.BindObject(eventargs);
205             NotificationProgressArgBinder.BindObject(eventargs);
206
207             return eventargs;
208         }
209     }
210 }