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