fe00529aec3dddbae564f0e28ef7e46616d079c2
[framework/osp/appwidget-service.git] / src / FShell_AppWidgetContext.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.1 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        FShell_AppWidgetContext.cpp
20  * @brief       This is the implementation for the _AppWidgetContext class.
21  */
22
23 #include <stdlib.h>
24 #include <unique_ptr.h>
25
26 #include "provider_buffer.h"
27
28 #include <FBase.h>
29 #include <FBaseSysLog.h>
30 #include <FBase_StringConverter.h>
31
32 #include <FApp_AppControlManager.h>
33
34 #include "FShell_AppWidgetManagerService.h"
35 #include "FShell_AppWidgetPopupContext.h"
36 #include "FShell_AppWidgetContextBase.h"
37 #include "FShell_AppWidgetContext.h"
38
39
40 namespace Tizen { namespace Shell  { namespace App
41 {
42
43 using namespace Tizen::App;
44 using namespace Tizen::Base;
45 using namespace Tizen::Base::Collection;
46
47
48 const char APPWIDGET_ON_ADD[] = "http://tizen.org/appcontrol/appwidget/add";
49 const char APPWIDGET_ON_REMOVE[] = "http://tizen.org/appcontrol/appwidget/remove";
50 const char APPWIDGET_ON_UPDATE[] = "http://tizen.org/appcontrol/appwidget/update";
51 const char APPWIDGET_ON_RESIZE[] = "http://tizen.org/appcontrol/appwidget/resize";
52 const char APPWIDGET_ON_TOUCH[] = "http://tizen.org/appcontrol/appwidget/touch";
53
54 const int DEFAULT_LIFE_DURATION_MSEC = 30000;//30sec
55 const int UPDATE_PERIOD_MSEC_MIN = 1800000;//30min
56
57 _AppWidgetContext::_AppWidgetContext(const String& info, const String& providerId, const String& instanceId, int width, int height, int period, int priority)
58  :_AppWidgetContextBase(TYPE_LB, info, providerId, instanceId, width, height, priority)
59  ,__pAppWidgetPopup(null)
60  ,__updateMillis(period)
61  ,__pPendingTouchEventList(null)
62 {
63 //      __lifeDurationTimer.Construct(*this);//, true);
64 //      __lifeDurationTimer.Start(DEFAULT_LIFE_DURATION_MSEC );
65
66         __updateMillis = (__updateMillis > UPDATE_PERIOD_MSEC_MIN) ? __updateMillis : UPDATE_PERIOD_MSEC_MIN;
67         SysLog(NID_SHELL, "period(%d)", __updateMillis);
68         if( __updateMillis > 0)
69         {
70                 __updateTimer.Construct(*this);//, false);
71                 __updateTimer.StartAsRepeatable(__updateMillis);
72         }
73
74         __pPendingTouchEventList = new ArrayListT<PendingTouchEvent*>();
75         __pPendingTouchEventList->Construct();
76 }
77
78 _AppWidgetContext::~_AppWidgetContext()
79 {
80         SysLog(NID_SHELL, "appId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority);
81 //      __lifeDurationTimer.Cancel();
82         __updateTimer.Cancel();
83
84         if (__pAppWidgetPopup)
85         {
86                 SysLog(NID_SHELL, "Destroying dangling AppWidgetPopup instance..");
87                 __pAppWidgetPopup->OnPopupDestoyed();
88                 delete __pAppWidgetPopup;
89         }
90
91
92         if( __pPendingTouchEventList )
93         {
94                 for( int i = 0; i < __pPendingTouchEventList->GetCount(); i++)
95                 {
96                         PendingTouchEvent* pTouchEvent = null;
97                         __pPendingTouchEventList->GetAt(i, pTouchEvent);
98                         delete pTouchEvent;
99                 }
100                 __pPendingTouchEventList->RemoveAll();
101                 delete __pPendingTouchEventList;
102         }
103 }
104
105 _AppWidgetPopupContext*
106 _AppWidgetContext::GetAppWidgetPopup() const
107 {
108         return __pAppWidgetPopup;
109 }
110
111 void
112 _AppWidgetContext::OnAdded(void)
113 {
114         SendAddRequest(__width, __height);
115 }
116
117 void
118 _AppWidgetContext::OnRemoved()
119 {
120         SendRemoveRequest();
121 }
122
123 void
124 _AppWidgetContext::OnUpdate(const String& argument)
125 {
126         SysLog(NID_SHELL, "appId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority);
127
128         SendUpdateRequest(__width, __height, argument);
129 }
130
131 void
132 _AppWidgetContext::OnResize(int width, int height)
133 {
134         SysLog(NID_SHELL, "appId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority);
135
136         __width = width;
137         __height = height;
138         SendResizeRequest(__width, __height);
139 }
140
141 void
142 _AppWidgetContext::OnForeground()
143 {
144         SysLog(NID_SHELL, "");
145         __isForeground = true;
146
147         __updateTimer.Cancel();
148         __updateTimer.StartAsRepeatable(__updateMillis);
149 }
150
151 void
152 _AppWidgetContext::OnBackground()
153 {
154         SysLog(NID_SHELL, "");
155         __isForeground = false;
156         __updateTimer.Cancel();
157 }
158
159 void
160 _AppWidgetContext::OnPopupCreated(double x, double y, int width, int height)
161 {
162         __pAppWidgetPopup = new (std::nothrow) _AppWidgetPopupContext(__userInfo, __providerId, __instanceId, width, height, __priority, this);
163         __pAppWidgetPopup->SetIpcClientId(__ipcClientId);
164         __pAppWidgetPopup->OnPopupCreated(x, y, width, height);
165
166 //      __lifeDurationTimer.Cancel();
167 }
168
169 void
170 _AppWidgetContext::OnPopupDestoyed(void)
171 {
172         SysLog(NID_SHELL, "");
173         if (__pAppWidgetPopup)
174         {
175                 __pAppWidgetPopup->OnPopupDestoyed();
176                 delete __pAppWidgetPopup;
177                 __pAppWidgetPopup = null;
178         }
179 //      RestartLifeDurationTimer();
180 }
181
182 result
183 _AppWidgetContext::SendAddRequest(int width, int height)
184 {
185         std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
186
187         return SendRequestToApp( __appId, APPWIDGET_ON_ADD, pArgs.get());
188 }
189
190 result
191 _AppWidgetContext::SendUpdateRequest(int width, int height, const String& argument)
192 {
193         std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
194
195         pArgs->Add(new String(ARG_KEY_ARGUMENT), new String(argument));
196
197         return SendRequestToApp( __appId, APPWIDGET_ON_UPDATE, pArgs.get());
198 }
199
200 result
201 _AppWidgetContext::SendResizeRequest(int width, int height)
202 {
203         std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
204
205         return SendRequestToApp( __appId, APPWIDGET_ON_RESIZE, pArgs.get());
206 }
207
208 result
209 _AppWidgetContext::SendRemoveRequest()
210 {
211         std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
212
213         return SendRequestToApp( __appId, APPWIDGET_ON_REMOVE, pArgs.get());
214 }
215
216 void
217 _AppWidgetContext::SendPendingTouchEvent(void)
218 {
219         for(int i =0; i< __pPendingTouchEventList->GetCount(); i++)
220         {
221                 PendingTouchEvent* pTouchEvent = null;
222                 __pPendingTouchEventList->GetAt(i, pTouchEvent);
223                 AppWidgetManagerService::GetInstance()->SendTouchEvent(__ipcClientId, __instanceId, pTouchEvent->eventType, pTouchEvent->timeStamp, pTouchEvent->x, pTouchEvent->y);
224                 delete pTouchEvent;
225         }
226         __pPendingTouchEventList->RemoveAll();
227 }
228
229 result
230 _AppWidgetContext::SendTouchEvent(buffer_event eventType, double timeStamp, double x, double y)
231 {
232         if( HasValidClientId() && IsRunning() )
233         {
234                 SysAssert(IsSharedMemCreated() == true);
235                 SysLog(NID_SHELL, "send IPC message");
236                 AppWidgetManagerService::GetInstance()->SendTouchEvent(__ipcClientId, __instanceId, eventType, timeStamp, x, y);
237         }
238         else
239         {
240                 SysLog(NID_SHELL, "request to start AppControl");
241                 __pPendingTouchEventList->Add(new PendingTouchEvent(eventType, timeStamp, x, y));
242
243                 //
244                 std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
245
246                 pArgs->Add(new String(ARG_KEY_EVENT_TYPE), new String(Integer::ToString(eventType)));
247                 pArgs->Add(new String(ARG_KEY_TIME_STAMP), new String(Double::ToString(timeStamp)));
248                 pArgs->Add(new String(ARG_KEY_X), new String(Double::ToString(x)));
249                 pArgs->Add(new String(ARG_KEY_Y), new String(Double::ToString(y)));
250
251                 return SendRequestToApp( __appId, APPWIDGET_ON_TOUCH, pArgs.get());
252         }
253         return E_SUCCESS;
254 }
255
256 result
257 _AppWidgetContext::SendRequestToApp(const AppId& appId, const String& operation, HashMap* pArgs)
258 {
259         result r = _AppWidgetContextBase::SendRequestToApp(appId, operation, pArgs);
260 //      RestartLifeDurationTimer();
261
262         return r;
263 }
264
265 result
266 _AppWidgetContext::RequestUpdateRemote(int width, int height)
267 {
268         /*if( GetAppWidgetPopup() != null)
269         {
270                 SysLog(NID_SHELL, "AppWidgetPopup is appeared, so appWidget doesn't need to update");
271                 return E_SUCCESS;
272         }*/
273         std::unique_ptr<char[]> packageName(_StringConverter::CopyToCharArrayN(__providerId));
274         std::unique_ptr<char[]> id(_StringConverter::CopyToCharArrayN(__instanceId));
275         std::unique_ptr<char[]> content_info(_StringConverter::CopyToCharArrayN(__userInfo));
276
277         int ret = provider_send_updated(packageName.get(), id.get(), width, height, __priority, content_info.get(), null);
278         SysTryReturnResult(NID_SHELL, ret >= 0 , E_SYSTEM, "[E_SYSTEM] failed to provider_send_updated");
279
280         SendPendingTouchEvent();
281         SysLog(NID_SHELL, "Done");
282         return E_SUCCESS;
283 }
284
285 Tizen::Base::Collection::HashMap*
286 _AppWidgetContext::CreateRequestArgs(void)
287 {
288         HashMap* pArgs = new (std::nothrow) HashMap(SingleObjectDeleter);
289         pArgs->Construct();
290         pArgs->Add(new String(ARG_KEY_INSTANCE_ID), new String(__instanceId));
291         pArgs->Add(new String(ARG_KEY_PROVIDER_NAME), new String(__providerName));
292         pArgs->Add(new String(ARG_KEY_USER_INFO), new String(__userInfo));
293         pArgs->Add(new String(ARG_KEY_WIDTH), new String(Integer::ToString(__width)));
294         pArgs->Add(new String(ARG_KEY_HEIGHT), new String(Integer::ToString(__height)));
295
296         return pArgs;
297 }
298
299
300 //void
301 //_AppWidgetContext::RestartLifeDurationTimer()
302 //{
303 //      __lifeDurationTimer.Cancel();
304 //      __lifeDurationTimer.Start(DEFAULT_LIFE_DURATION_MSEC);
305 //      SysLog(NID_SHELL, "lifeDuration timer restarted (%d)msec", DEFAULT_LIFE_DURATION_MSEC);
306 //}
307
308 void
309 _AppWidgetContext::OnTimerExpired(Tizen::Base::Runtime::Timer& timer)
310 {
311 //      if( &timer == &__lifeDurationTimer)
312 //      {
313 //              SysLog(NID_SHELL, "lifeDuration timer is expired, so terminating appWidget app(%ls)..", __providerId.GetPointer() );
314 //
315 ////            ReleaseSharedMem();
316 //              AppManager::GetInstance()->TerminateApplication(__appId);
317 //
318 //      }
319 //      else
320         if( &timer == &__updateTimer)
321         {
322                 SysLog(NID_SHELL, "update timer is expired for appWidget app(%ls)..", __providerId.GetPointer() );
323                 OnUpdate(L"");
324         }
325 }
326
327
328 } } } // Tizen::Shell::App {
329