8f26a0ae05bf38553b3296c58e261e13e2c339d8
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.Notification / Tizen.Applications.Notifications / NotificationStyleBinder.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 IndicatorBinder
22     {
23         internal static void BindObject(Notification notification)
24         {
25             int flag;
26             NotificationError ret = NotificationError.None;
27             Notification.IndicatorStyle style = (Notification.IndicatorStyle)notification.GetStyle("Indicator");
28             Interop.Notification.GetApplist(notification.Handle, out flag);
29
30             if (string.IsNullOrEmpty(style.SubText) == false)
31             {
32                 ret = Interop.Notification.SetText(notification.Handle, NotificationText.FirstMainText, style.SubText, null, -1);
33                 if (ret != NotificationError.None)
34                 {
35                     throw NotificationErrorFactory.GetException(ret, "unable to set indicator text");
36                 }
37                 flag |= (int)NotificationDisplayApplist.Ticker;
38             }
39
40             if (string.IsNullOrEmpty(style.IconPath) == false)
41             {
42                 ret = Interop.Notification.SetImage(notification.Handle, NotificationImage.IconForIndicator, style.IconPath);
43                 if (ret != NotificationError.None)
44                 {
45                     throw NotificationErrorFactory.GetException(ret, "unable to set indicator image");
46                 }
47                 flag |= (int)NotificationDisplayApplist.Indicator;
48             }
49             Interop.Notification.SetApplist(notification.Handle, flag);
50         }
51
52         internal static void BindSafeHandle(Notification notification)
53         {
54             int appList;
55             Interop.Notification.GetApplist(notification.Handle, out appList);
56             if ((appList & (int)NotificationDisplayApplist.Ticker) != 0 || (appList & (int)NotificationDisplayApplist.Indicator) != 0)
57             {
58                 string path, text;
59                 Notification.IndicatorStyle indicator = new Notification.IndicatorStyle();
60                 Interop.Notification.GetImage(notification.Handle, NotificationImage.IconForIndicator, out path);
61                 indicator.IconPath = path;
62                 Interop.Notification.GetText(notification.Handle, NotificationText.FirstMainText, out text);
63                 indicator.SubText = text;
64
65                 notification.AddStyle(indicator);
66             }
67         }
68     }
69
70     internal static class ActiveBinder
71     {
72         internal static void BindObject(Notification notification)
73         {
74             int flag;
75             NotificationError ret = NotificationError.None;
76             Notification.ActiveStyle style = (Notification.ActiveStyle)notification.GetStyle("Active");
77
78             Interop.Notification.SetAutoRemove(notification.Handle, style.IsAutoRemove);
79             if (style.IsAutoRemove == true)
80             {
81                 int hidetime;
82                 int deletetime;
83                 style.GetRemoveTime(out hidetime, out deletetime);
84
85                 Interop.Notification.SetHideTime(notification.Handle, hidetime);
86                 try
87                 {
88                     Interop.Notification.SetDeleteTime(notification.Handle, deletetime);
89                 }
90                 catch (TypeLoadException)
91                 {
92                     // To support in API version 3.0
93                     style.SetRemoveTime(hidetime, 60);
94                 }
95             }
96
97             ret = Interop.Notification.SetImage(notification.Handle, NotificationImage.Background, style?.BackgroundImage);
98             if (ret != NotificationError.None)
99             {
100                 throw NotificationErrorFactory.GetException(ret, "unable to set background Image");
101             }
102
103             if (style.DefaultButton != ButtonIndex.None)
104             {
105                 Interop.Notification.SetDefaultButton(notification.Handle, (int)style.DefaultButton + 1);
106             }
107
108             Interop.Notification.GetApplist(notification.Handle, out flag);
109             Interop.Notification.SetApplist(notification.Handle, flag | (int)NotificationDisplayApplist.Active);
110
111             foreach (Notification.ButtonAction button in style.GetButtonAction())
112             {
113                 button.Make(notification);
114             }
115
116             if (style.ReplyAction != null)
117             {
118                 style.ReplyAction.Make(notification);
119             }
120
121             if (style.HiddenByUserAction != null)
122             {
123                 Interop.Notification.SetExtensionAction(notification.Handle, NotificationEventType.HiddenByUser, style.HiddenByUserAction.SafeAppControlHandle);
124             }
125
126             if (style.HiddenByTimeoutAction != null)
127             {
128                 Interop.Notification.SetExtensionAction(notification.Handle, NotificationEventType.HiddenByTimeout, style.HiddenByTimeoutAction.SafeAppControlHandle);
129             }
130
131             if (style.HiddenByExternalAction != null)
132             {
133                 Interop.Notification.SetExtensionAction(notification.Handle, NotificationEventType.HiddenByExternal, style.HiddenByExternalAction.SafeAppControlHandle);
134             }
135         }
136
137         internal static void BindSafeHandle(Notification notification)
138         {
139             int appList;
140             Interop.Notification.GetApplist(notification.Handle, out appList);
141
142             if ((appList & (int)NotificationDisplayApplist.Active) != 0)
143             {
144                 Notification.ActiveStyle active = new Notification.ActiveStyle();
145                 bool autoRemove;
146                 string path, text;
147                 SafeAppControlHandle appcontrol = null;
148                 string replyKey = "__PARENT_INDEX__";
149
150                 for (int i = (int)ButtonIndex.First; i <= (int)ButtonIndex.Third; i++)
151                 {
152                     appcontrol = null;
153
154                     Interop.Notification.GetImage(notification.Handle, NotificationImage.FirstButton + i, out path);
155                     Interop.Notification.GetText(notification.Handle, NotificationText.FirstButton + i, out text);
156                     Interop.Notification.GetEventHandler(notification.Handle, i, out appcontrol);
157
158                     if (string.IsNullOrEmpty(path) == false || string.IsNullOrEmpty(text) == false
159                         || (appcontrol != null && appcontrol.IsInvalid == false))
160                     {
161                         Notification.ButtonAction button = new Notification.ButtonAction();
162                         if (appcontrol != null && appcontrol.IsInvalid == false)
163                         {
164                             button.Action = new AppControl(appcontrol);
165                         }
166
167                         button.ImagePath = path;
168                         button.Text = text;
169                         button.Index = (ButtonIndex)i;
170                         active.AddButtonAction(button);
171                     }
172                 }
173
174                 appcontrol = null;
175                 Interop.Notification.GetExtensionAction(notification.Handle, NotificationEventType.HiddenByUser, out appcontrol);
176                 if (appcontrol != null && appcontrol.IsInvalid == false)
177                 {
178                     active.HiddenByUserAction = new AppControl(appcontrol);
179                 }
180
181                 appcontrol = null;
182                 Interop.Notification.GetExtensionAction(notification.Handle, NotificationEventType.HiddenByTimeout, out appcontrol);
183                 if (appcontrol != null && appcontrol.IsInvalid == false)
184                 {
185                     active.HiddenByTimeoutAction = new AppControl(appcontrol);
186                 }
187
188                 appcontrol = null;
189                 Interop.Notification.GetExtensionAction(notification.Handle, NotificationEventType.HiddenByExternal, out appcontrol);
190                 if (appcontrol != null && appcontrol.IsInvalid == false)
191                 {
192                     active.HiddenByExternalAction = new AppControl(appcontrol);
193                 }
194
195                 Interop.Notification.GetAutoRemove(notification.Handle, out autoRemove);
196                 active.IsAutoRemove = autoRemove;
197                 if (autoRemove)
198                 {
199                     int hidetime, deletetime;
200                     Interop.Notification.GetHideTime(notification.Handle, out hidetime);
201                     try
202                     {
203                         Interop.Notification.GetDeleteTime(notification.Handle, out deletetime);
204                     }
205                     catch (TypeLoadException)
206                     {
207                         // To support in API version 3.0
208                         deletetime = 60;
209                     }
210
211                     active.SetRemoveTime(hidetime, deletetime);
212                 }
213
214                 Interop.Notification.GetImage(notification.Handle, NotificationImage.Background, out path);
215                 if (string.IsNullOrEmpty(path) == false)
216                 {
217                     active.BackgroundImage = path;
218                 }
219
220                 int defaultIndex;
221                 Interop.Notification.GetDefaultButton(notification.Handle, out defaultIndex);
222                 active.DefaultButton = (ButtonIndex)(defaultIndex - 1);
223
224                 appcontrol = null;
225                 Interop.Notification.GetImage(notification.Handle, NotificationImage.TextInputButton, out path);
226                 Interop.Notification.GetText(notification.Handle, NotificationText.InputButton, out text);
227                 Interop.Notification.GetEventHandler(notification.Handle, (int)NotificationEventType.ClickOnTextInputButton, out appcontrol);
228
229                 if (string.IsNullOrEmpty(path) == false || string.IsNullOrEmpty(text) == false
230                     || (appcontrol != null && appcontrol.IsInvalid == false))
231                 {
232                     Notification.ReplyAction reply = new Notification.ReplyAction();
233                     Notification.ButtonAction button = new Notification.ButtonAction();
234                     if (appcontrol != null && appcontrol.IsInvalid == false)
235                     {
236                         button.Action = new AppControl(appcontrol);
237                     }
238
239                     button.ImagePath = path;
240                     button.Text = text;
241                     reply.Button = button;
242
243                     Interop.Notification.GetText(notification.Handle, NotificationText.PlaceHolder, out text);
244                     reply.PlaceHolderText = text;
245
246                     int holderLength;
247                     Interop.Notification.GetPlaceHolderLength(notification.Handle, out holderLength);
248                     reply.ReplyMax = holderLength;
249
250                     try
251                     {
252                         SafeBundleHandle bundleHandle;
253                         Interop.Notification.GetExtensionData(notification.Handle, replyKey, out bundleHandle);
254                         Bundle bundle = new Bundle(bundleHandle);
255                         reply.ParentIndex = (ButtonIndex)int.Parse(bundle.GetItem(replyKey).ToString());
256                     }
257                     catch (Exception ex)
258                     {
259                         Log.Error(Notification.LogTag, ex.ToString());
260                     }
261
262                     active.ReplyAction = reply;
263                 }
264
265                 notification.AddStyle(active);
266             }
267         }
268     }
269
270     internal static class LockBinder
271     {
272         internal static void BindObject(Notification notification)
273         {
274             int flag;
275             NotificationError ret = NotificationError.None;
276             Notification.LockStyle style = (Notification.LockStyle)notification.GetStyle("Lock");
277
278             ret = Interop.Notification.SetImage(notification.Handle, NotificationImage.IconForLock, style.IconPath);
279             if (ret != NotificationError.None)
280             {
281                 throw NotificationErrorFactory.GetException(ret, "unable to set lock icon");
282             }
283
284             ret = Interop.Notification.SetImage(notification.Handle, NotificationImage.ThumbnailForLock, style.ThumbnailPath);
285             if (ret != NotificationError.None)
286             {
287                 throw NotificationErrorFactory.GetException(ret, "unable to set lock thumbnail");
288             }
289
290             Interop.Notification.GetApplist(notification.Handle, out flag);
291             Interop.Notification.SetApplist(notification.Handle, flag | (int)NotificationDisplayApplist.Lock);
292         }
293
294         internal static void BindSafehandle(Notification notification)
295         {
296             int applist;
297             Interop.Notification.GetApplist(notification.Handle, out applist);
298
299             if ((applist & (int)NotificationDisplayApplist.Lock) != 0)
300             {
301                 string path;
302                 Notification.LockStyle lockStyle = new Notification.LockStyle();
303
304                 Interop.Notification.GetImage(notification.Handle, NotificationImage.IconForLock, out path);
305                 lockStyle.IconPath = path;
306
307                 Interop.Notification.GetImage(notification.Handle, NotificationImage.ThumbnailForLock, out path);
308                 lockStyle.ThumbnailPath = path;
309
310                 notification.AddStyle(lockStyle);
311             }
312         }
313     }
314 }