Release 4.0.0-preview1-00083
[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.HiddenByUserAction.SafeAppControlHandle);
129             }
130
131             if (style.HiddenByExternalAction != null)
132             {
133                 Interop.Notification.SetExtensionAction(notification.Handle, NotificationEventType.HiddenByExternal, style.HiddenByUserAction.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 isExisted = false;
146                 bool autoRemove;
147                 string path, text;
148                 SafeAppControlHandle appcontrol = null;
149                 string replyKey = "__PARENT_INDEX__";
150
151                 for (int i = (int)ButtonIndex.First; i <= (int)ButtonIndex.Third; i++)
152                 {
153                     appcontrol = null;
154
155                     Interop.Notification.GetImage(notification.Handle, NotificationImage.FirstButton + i, out path);
156                     Interop.Notification.GetText(notification.Handle, NotificationText.FirstButton + i, out text);
157                     Interop.Notification.GetEventHandler(notification.Handle, i, out appcontrol);
158
159                     if (string.IsNullOrEmpty(path) == false || string.IsNullOrEmpty(text) == false
160                         || (appcontrol != null && appcontrol.IsInvalid == false))
161                     {
162                         Notification.ButtonAction button = new Notification.ButtonAction();
163                         if (appcontrol != null && appcontrol.IsInvalid == false)
164                         {
165                             button.Action = new AppControl(appcontrol);
166                         }
167
168                         button.ImagePath = path;
169                         button.Text = text;
170                         button.Index = (ButtonIndex)i;
171                         active.AddButtonAction(button);
172                         isExisted = true;
173                     }
174                 }
175
176                 appcontrol = null;
177                 Interop.Notification.GetExtensionAction(notification.Handle, NotificationEventType.HiddenByUser, out appcontrol);
178                 if (appcontrol != null && appcontrol.IsInvalid == false)
179                 {
180                     active.HiddenByUserAction = new AppControl(appcontrol);
181                 }
182
183                 appcontrol = null;
184                 Interop.Notification.GetExtensionAction(notification.Handle, NotificationEventType.HiddenByTimeout, out appcontrol);
185                 if (appcontrol != null && appcontrol.IsInvalid == false)
186                 {
187                     active.HiddenByTimeoutAction = new AppControl(appcontrol);
188                 }
189
190                 appcontrol = null;
191                 Interop.Notification.GetExtensionAction(notification.Handle, NotificationEventType.HiddenByExternal, out appcontrol);
192                 if (appcontrol != null && appcontrol.IsInvalid == false)
193                 {
194                     active.HiddenByExternalAction = new AppControl(appcontrol);
195                 }
196
197                 Interop.Notification.GetAutoRemove(notification.Handle, out autoRemove);
198                 active.IsAutoRemove = autoRemove;
199                 if (autoRemove)
200                 {
201                     int hidetime, deletetime;
202                     Interop.Notification.GetHideTime(notification.Handle, out hidetime);
203                     try
204                     {
205                         Interop.Notification.GetDeleteTime(notification.Handle, out deletetime);
206                     }
207                     catch (TypeLoadException)
208                     {
209                         // To support in API version 3.0
210                         deletetime = 60;
211                     }
212
213                     active.SetRemoveTime(hidetime, deletetime);
214                 }
215
216                 Interop.Notification.GetImage(notification.Handle, NotificationImage.Background, out path);
217                 if (string.IsNullOrEmpty(path) == false)
218                 {
219                     isExisted = true;
220                     active.BackgroundImage = path;
221                 }
222
223                 int defaultIndex;
224                 Interop.Notification.GetDefaultButton(notification.Handle, out defaultIndex);
225                 active.DefaultButton = (ButtonIndex)(defaultIndex - 1);
226
227                 appcontrol = null;
228                 Interop.Notification.GetImage(notification.Handle, NotificationImage.TextInputButton, out path);
229                 Interop.Notification.GetText(notification.Handle, NotificationText.InputButton, out text);
230                 Interop.Notification.GetEventHandler(notification.Handle, (int)NotificationEventType.ClickOnTextInputButton, out appcontrol);
231
232                 if (string.IsNullOrEmpty(path) == false || string.IsNullOrEmpty(text) == false
233                     || (appcontrol != null && appcontrol.IsInvalid == false))
234                 {
235                     Notification.ReplyAction reply = new Notification.ReplyAction();
236                     Notification.ButtonAction button = new Notification.ButtonAction();
237                     if (appcontrol != null && appcontrol.IsInvalid == false)
238                     {
239                         button.Action = new AppControl(appcontrol);
240                     }
241
242                     button.ImagePath = path;
243                     button.Text = text;
244                     reply.Button = button;
245
246                     Interop.Notification.GetText(notification.Handle, NotificationText.PlaceHolder, out text);
247                     reply.PlaceHolderText = text;
248
249                     int holderLength;
250                     Interop.Notification.GetPlaceHolderLength(notification.Handle, out holderLength);
251                     reply.ReplyMax = holderLength;
252
253                     isExisted = true;
254
255                     try
256                     {
257                         SafeBundleHandle bundleHandle;
258                         Interop.Notification.GetExtentionData(notification.Handle, replyKey, out bundleHandle);
259                         Bundle bundle = new Bundle(bundleHandle);
260                         reply.ParentIndex = (ButtonIndex)int.Parse(bundle.GetItem(replyKey).ToString());
261                     }
262                     catch (Exception ex)
263                     {
264                         Log.Error(Notification.LogTag, ex.ToString());
265                     }
266
267                     active.ReplyAction = reply;
268                 }
269
270                 if (isExisted)
271                 {
272                     notification.AddStyle(active);
273                 }
274             }
275         }
276     }
277
278     internal static class LockBinder
279     {
280         internal static void BindObject(Notification notification)
281         {
282             int flag;
283             NotificationError ret = NotificationError.None;
284             Notification.LockStyle style = (Notification.LockStyle)notification.GetStyle("Lock");
285
286             ret = Interop.Notification.SetImage(notification.Handle, NotificationImage.IconForLock, style.IconPath);
287             if (ret != NotificationError.None)
288             {
289                 throw NotificationErrorFactory.GetException(ret, "unable to set lock icon");
290             }
291
292             ret = Interop.Notification.SetImage(notification.Handle, NotificationImage.ThumbnailForLock, style.ThumbnailPath);
293             if (ret != NotificationError.None)
294             {
295                 throw NotificationErrorFactory.GetException(ret, "unable to set lock thumbnail");
296             }
297
298             Interop.Notification.GetApplist(notification.Handle, out flag);
299             Interop.Notification.SetApplist(notification.Handle, flag | (int)NotificationDisplayApplist.Lock);
300         }
301
302         internal static void BindSafehandle(Notification notification)
303         {
304             int applist;
305             Interop.Notification.GetApplist(notification.Handle, out applist);
306
307             if ((applist & (int)NotificationDisplayApplist.Lock) != 0)
308             {
309                 string path;
310                 Notification.LockStyle lockStyle = new Notification.LockStyle();
311
312                 Interop.Notification.GetImage(notification.Handle, NotificationImage.IconForLock, out path);
313                 lockStyle.IconPath = path;
314
315                 Interop.Notification.GetImage(notification.Handle, NotificationImage.ThumbnailForLock, out path);
316                 lockStyle.ThumbnailPath = path;
317
318                 notification.AddStyle(lockStyle);
319             }
320         }
321     }
322 }