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