implement LifeDuration feature(remove from osp-appwidget-service)
[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.0 (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 String ARG_KEY_ARGUMENT = L"_Argument";
55 const String ARG_KEY_EVENT_TYPE = L"_EventType";
56 const String ARG_KEY_TIME_STAMP = L"_TimeStamp";
57 const String ARG_KEY_X = L"_X";
58 const String ARG_KEY_Y = L"_Y";
59
60 const int DEFAULT_LIFE_DURATION_MSEC = 30000;//30sec
61 const int UPDATE_PERIOD_MSEC_MIN = 1800000;//30min
62
63 _AppWidgetContext::_AppWidgetContext(const String& info, const String& providerId, const String& instanceId, int width, int height, int period, int priority)
64  :_AppWidgetContextBase(TYPE_LB, info, providerId, instanceId, width, height, priority)
65  ,__pAppWidgetPopup(null)
66  ,__updateMillis(period)
67  ,__pPendingTouchEventList(null)
68 {
69 //      __lifeDurationTimer.Construct(*this);//, true);
70 //      __lifeDurationTimer.Start(DEFAULT_LIFE_DURATION_MSEC );
71
72         __updateMillis = (__updateMillis > UPDATE_PERIOD_MSEC_MIN) ? __updateMillis : UPDATE_PERIOD_MSEC_MIN;
73         SysLog(NID_APP, "period(%d)", __updateMillis);
74         if( __updateMillis > 0)
75         {
76                 __UpdateTimer.Construct(*this);//, false);
77                 __UpdateTimer.StartAsRepeatable(__updateMillis);
78         }
79
80         __pPendingTouchEventList = new ArrayListT<PendingTouchEvent*>();
81         __pPendingTouchEventList->Construct();
82 }
83
84 _AppWidgetContext::~_AppWidgetContext()
85 {
86         SysLog(NID_APP, "appId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority);
87 //      __lifeDurationTimer.Cancel();
88         __UpdateTimer.Cancel();
89
90         if (__pAppWidgetPopup)
91         {
92                 SysLog(NID_APP, "Destroying dangling AppWidgetPopup instance..");
93                 __pAppWidgetPopup->OnPopupDestoyed();
94                 delete __pAppWidgetPopup;
95         }
96
97
98         if( __pPendingTouchEventList )
99         {
100                 for( int i = 0; i < __pPendingTouchEventList->GetCount(); i++)
101                 {
102                         PendingTouchEvent* pTouchEvent = null;
103                         __pPendingTouchEventList->GetAt(i, pTouchEvent);
104                         delete pTouchEvent;
105                 }
106                 __pPendingTouchEventList->RemoveAll();
107                 delete __pPendingTouchEventList;
108         }
109 }
110
111 _AppWidgetPopupContext*
112 _AppWidgetContext::GetAppWidgetPopup() const
113 {
114         return __pAppWidgetPopup;
115 }
116
117 void
118 _AppWidgetContext::OnAdded(void)
119 {
120         SendAddRequest(__width, __height);
121 }
122
123 void
124 _AppWidgetContext::OnRemoved()
125 {
126         SendRemoveRequest();
127 }
128
129 void
130 _AppWidgetContext::OnUpdate(const String& argument)
131 {
132         SysLog(NID_APP, "appId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority);
133
134         SendUpdateRequest(__width, __height, argument);
135 }
136
137 void
138 _AppWidgetContext::OnResize(int width, int height)
139 {
140         SysLog(NID_APP, "appId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority);
141
142         __width = width;
143         __height = height;
144         SendResizeRequest(__width, __height);
145 }
146
147 void
148 _AppWidgetContext::OnForeground()
149 {
150         SysLog(NID_APP, "");
151         __isForeground = true;
152
153         __UpdateTimer.Cancel();
154         __UpdateTimer.StartAsRepeatable(__updateMillis);
155 }
156
157 void
158 _AppWidgetContext::OnBackground()
159 {
160         SysLog(NID_APP, "");
161         __isForeground = false;
162         __UpdateTimer.Cancel();
163 }
164
165 void
166 _AppWidgetContext::OnPopupCreated(double x, double y, int width, int height)
167 {
168         __pAppWidgetPopup = new (std::nothrow) _AppWidgetPopupContext(__userInfo, __providerId, __instanceId, width, height, __priority);
169         __pAppWidgetPopup->SetIpcClientId(__ipcClientId);
170         __pAppWidgetPopup->OnPopupCreated(x, y, width, height);
171
172 //      __lifeDurationTimer.Cancel();
173 }
174
175 void
176 _AppWidgetContext::OnPopupDestoyed(void)
177 {
178         SysLog(NID_APP, "");
179         if (__pAppWidgetPopup)
180         {
181                 __pAppWidgetPopup->OnPopupDestoyed();
182                 delete __pAppWidgetPopup;
183                 __pAppWidgetPopup = null;
184         }
185 //      RestartLifeDurationTimer();
186 }
187
188 result
189 _AppWidgetContext::SendAddRequest(int width, int height)
190 {
191         std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
192
193         return SendRequestToApp( __appId, APPWIDGET_ON_ADD, pArgs.get());
194 }
195
196 result
197 _AppWidgetContext::SendUpdateRequest(int width, int height, const String& argument)
198 {
199         std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
200
201         pArgs->Add(new String(ARG_KEY_ARGUMENT), new String(argument));
202
203         return SendRequestToApp( __appId, APPWIDGET_ON_UPDATE, pArgs.get());
204 }
205
206 result
207 _AppWidgetContext::SendResizeRequest(int width, int height)
208 {
209         std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
210
211         return SendRequestToApp( __appId, APPWIDGET_ON_RESIZE, pArgs.get());
212 }
213
214 result
215 _AppWidgetContext::SendRemoveRequest()
216 {
217         std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
218
219         return SendRequestToApp( __appId, APPWIDGET_ON_REMOVE, pArgs.get());
220 }
221
222 void
223 _AppWidgetContext::SendPendingTouchEvent(void)
224 {
225         for(int i =0; i< __pPendingTouchEventList->GetCount(); i++)
226         {
227                 PendingTouchEvent* pTouchEvent = null;
228                 __pPendingTouchEventList->GetAt(i, pTouchEvent);
229                 AppWidgetManagerService::GetInstance()->SendTouchEvent(__ipcClientId, __instanceId, pTouchEvent->eventType, pTouchEvent->timeStamp, pTouchEvent->x, pTouchEvent->y);
230                 delete pTouchEvent;
231         }
232         __pPendingTouchEventList->RemoveAll();
233 }
234
235 result
236 _AppWidgetContext::SendTouchEvent(buffer_event eventType, double timeStamp, double x, double y)
237 {
238         if( HasValidClientId() && IsRunning() )
239         {
240                 SysAssert(IsSharedMemCreated() == true);
241                 SysLog(NID_APP, "send IPC message");
242                 AppWidgetManagerService::GetInstance()->SendTouchEvent(__ipcClientId, __instanceId, eventType, timeStamp, x, y);
243         }
244         else
245         {
246                 SysLog(NID_APP, "request to start AppControl");
247                 __pPendingTouchEventList->Add(new PendingTouchEvent(eventType, timeStamp, x, y));
248
249                 //
250                 std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
251
252                 pArgs->Add(new String(ARG_KEY_EVENT_TYPE), new String(Integer::ToString(eventType)));
253                 pArgs->Add(new String(ARG_KEY_TIME_STAMP), new String(Double::ToString(timeStamp)));
254                 pArgs->Add(new String(ARG_KEY_X), new String(Double::ToString(x)));
255                 pArgs->Add(new String(ARG_KEY_Y), new String(Double::ToString(y)));
256
257                 return SendRequestToApp( __appId, APPWIDGET_ON_TOUCH, pArgs.get());
258         }
259         return E_SUCCESS;
260 }
261
262 result
263 _AppWidgetContext::SendRequestToApp(const AppId& appId, const String& operation, HashMap* pArgs)
264 {
265         result r = _AppWidgetContextBase::SendRequestToApp(appId, operation, pArgs);
266         RestartLifeDurationTimer();
267
268         return r;
269 }
270
271 result
272 _AppWidgetContext::RequestUpdateRemote(int width, int height)
273 {
274         /*if( GetAppWidgetPopup() != null)
275         {
276                 SysLog(NID_APP, "AppWidgetPopup is appeared, so appWidget doesn't need to update");
277                 return E_SUCCESS;
278         }*/
279         std::unique_ptr<char[]> packageName(_StringConverter::CopyToCharArrayN(__providerId));
280         std::unique_ptr<char[]> id(_StringConverter::CopyToCharArrayN(__instanceId));
281         std::unique_ptr<char[]> content_info(_StringConverter::CopyToCharArrayN(__userInfo));
282
283         int ret = provider_send_updated(packageName.get(), id.get(), width, height, __priority, content_info.get(), null);
284         SysTryReturnResult(NID_APP, ret >= 0 , E_SYSTEM, "[E_SYSTEM] failed to provider_send_updated");
285
286         SendPendingTouchEvent();
287         SysLog(NID_APP, "Done");
288         return E_SUCCESS;
289 }
290
291 //void
292 //_AppWidgetContext::RestartLifeDurationTimer()
293 //{
294 //      __lifeDurationTimer.Cancel();
295 //      __lifeDurationTimer.Start(DEFAULT_LIFE_DURATION_MSEC);
296 //      SysLog(NID_APP, "lifeDuration timer restarted (%d)msec", DEFAULT_LIFE_DURATION_MSEC);
297 //}
298
299 void
300 _AppWidgetContext::OnTimerExpired(Tizen::Base::Runtime::Timer& timer)
301 {
302 //      if( &timer == &__lifeDurationTimer)
303 //      {
304 //              SysLog(NID_APP, "lifeDuration timer is expired, so terminating appWidget app(%ls)..", __providerId.GetPointer() );
305 //
306 ////            ReleaseSharedMem();
307 //              AppManager::GetInstance()->TerminateApplication(__appId);
308 //
309 //      }
310 //      else
311         if( &timer == &__UpdateTimer)
312         {
313                 SysLog(NID_APP, "update timer is expired for appWidget app(%ls)..", __providerId.GetPointer() );
314                 OnUpdate(L"");
315         }
316 }
317
318
319 } } } // Tizen::Shell::App {
320