2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
9 // http://floralicense.org/license/
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.
19 * @file FShell_AppWidgetContext.cpp
20 * @brief This is the implementation for the _AppWidgetContext class.
24 #include <unique_ptr.h>
26 #include "provider_buffer.h"
29 #include <FBaseSysLog.h>
30 #include <FBase_StringConverter.h>
32 #include <FApp_AppControlManager.h>
34 #include "FShell_AppWidgetManagerService.h"
35 #include "FShell_AppWidgetPopupContext.h"
36 #include "FShell_AppWidgetContextBase.h"
37 #include "FShell_AppWidgetContext.h"
40 namespace Tizen { namespace Shell { namespace App
43 using namespace Tizen::App;
44 using namespace Tizen::Base;
45 using namespace Tizen::Base::Collection;
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";
54 const int DEFAULT_LIFE_DURATION_MSEC = 30000;//30sec
55 const int UPDATE_PERIOD_MSEC_MIN = 1800000;//30min
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)
63 // __lifeDurationTimer.Construct(*this);//, true);
64 // __lifeDurationTimer.Start(DEFAULT_LIFE_DURATION_MSEC );
66 __updateMillis = (__updateMillis > UPDATE_PERIOD_MSEC_MIN) ? __updateMillis : UPDATE_PERIOD_MSEC_MIN;
67 SysLog(NID_SHELL, "period(%d)", __updateMillis);
68 if( __updateMillis > 0)
70 __updateTimer.Construct(*this);//, false);
71 __updateTimer.StartAsRepeatable(__updateMillis);
74 __pPendingTouchEventList = new ArrayListT<PendingTouchEvent*>();
75 __pPendingTouchEventList->Construct();
78 _AppWidgetContext::~_AppWidgetContext()
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();
84 if (__pAppWidgetPopup)
86 SysLog(NID_SHELL, "Destroying dangling AppWidgetPopup instance..");
87 __pAppWidgetPopup->OnPopupDestoyed();
88 delete __pAppWidgetPopup;
92 if( __pPendingTouchEventList )
94 for( int i = 0; i < __pPendingTouchEventList->GetCount(); i++)
96 PendingTouchEvent* pTouchEvent = null;
97 __pPendingTouchEventList->GetAt(i, pTouchEvent);
100 __pPendingTouchEventList->RemoveAll();
101 delete __pPendingTouchEventList;
105 _AppWidgetPopupContext*
106 _AppWidgetContext::GetAppWidgetPopup() const
108 return __pAppWidgetPopup;
112 _AppWidgetContext::OnAdded(void)
114 SendAddRequest(__width, __height);
118 _AppWidgetContext::OnRemoved()
124 _AppWidgetContext::OnUpdate(const String& argument)
126 SysLog(NID_SHELL, "appId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority);
128 SendUpdateRequest(__width, __height, argument);
132 _AppWidgetContext::OnResize(int width, int height)
134 SysLog(NID_SHELL, "appId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority);
138 SendResizeRequest(__width, __height);
142 _AppWidgetContext::OnForeground()
144 SysLog(NID_SHELL, "");
145 __isForeground = true;
147 __updateTimer.Cancel();
148 __updateTimer.StartAsRepeatable(__updateMillis);
152 _AppWidgetContext::OnBackground()
154 SysLog(NID_SHELL, "");
155 __isForeground = false;
156 __updateTimer.Cancel();
160 _AppWidgetContext::OnPopupCreated(double x, double y, int width, int height)
162 __pAppWidgetPopup = new (std::nothrow) _AppWidgetPopupContext(__userInfo, __providerId, __instanceId, width, height, __priority, this);
163 __pAppWidgetPopup->SetIpcClientId(__ipcClientId);
164 __pAppWidgetPopup->OnPopupCreated(x, y, width, height);
166 // __lifeDurationTimer.Cancel();
170 _AppWidgetContext::OnPopupDestoyed(void)
172 SysLog(NID_SHELL, "");
173 if (__pAppWidgetPopup)
175 __pAppWidgetPopup->OnPopupDestoyed();
176 delete __pAppWidgetPopup;
177 __pAppWidgetPopup = null;
179 // RestartLifeDurationTimer();
183 _AppWidgetContext::SendAddRequest(int width, int height)
185 std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
187 return SendRequestToApp( __appId, APPWIDGET_ON_ADD, pArgs.get());
191 _AppWidgetContext::SendUpdateRequest(int width, int height, const String& argument)
193 std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
195 pArgs->Add(new String(ARG_KEY_ARGUMENT), new String(argument));
197 return SendRequestToApp( __appId, APPWIDGET_ON_UPDATE, pArgs.get());
201 _AppWidgetContext::SendResizeRequest(int width, int height)
203 std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
205 return SendRequestToApp( __appId, APPWIDGET_ON_RESIZE, pArgs.get());
209 _AppWidgetContext::SendRemoveRequest()
211 std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
213 return SendRequestToApp( __appId, APPWIDGET_ON_REMOVE, pArgs.get());
217 _AppWidgetContext::SendPendingTouchEvent(void)
219 for(int i =0; i< __pPendingTouchEventList->GetCount(); i++)
221 PendingTouchEvent* pTouchEvent = null;
222 __pPendingTouchEventList->GetAt(i, pTouchEvent);
223 AppWidgetManagerService::GetInstance()->SendTouchEvent(__ipcClientId, __instanceId, pTouchEvent->eventType, pTouchEvent->timeStamp, pTouchEvent->x, pTouchEvent->y);
226 __pPendingTouchEventList->RemoveAll();
230 _AppWidgetContext::SendTouchEvent(buffer_event eventType, double timeStamp, double x, double y)
232 if( HasValidClientId() && IsRunning() )
234 SysAssert(IsSharedMemCreated() == true);
235 SysLog(NID_SHELL, "send IPC message");
236 AppWidgetManagerService::GetInstance()->SendTouchEvent(__ipcClientId, __instanceId, eventType, timeStamp, x, y);
240 SysLog(NID_SHELL, "request to start AppControl");
241 __pPendingTouchEventList->Add(new PendingTouchEvent(eventType, timeStamp, x, y));
244 std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
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)));
251 return SendRequestToApp( __appId, APPWIDGET_ON_TOUCH, pArgs.get());
257 _AppWidgetContext::SendRequestToApp(const AppId& appId, const String& operation, HashMap* pArgs)
259 result r = _AppWidgetContextBase::SendRequestToApp(appId, operation, pArgs);
260 // RestartLifeDurationTimer();
266 _AppWidgetContext::RequestUpdateRemote(int width, int height)
268 /*if( GetAppWidgetPopup() != null)
270 SysLog(NID_SHELL, "AppWidgetPopup is appeared, so appWidget doesn't need to update");
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));
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");
280 SendPendingTouchEvent();
281 SysLog(NID_SHELL, "Done");
285 Tizen::Base::Collection::HashMap*
286 _AppWidgetContext::CreateRequestArgs(void)
288 HashMap* pArgs = new (std::nothrow) HashMap(SingleObjectDeleter);
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)));
301 //_AppWidgetContext::RestartLifeDurationTimer()
303 // __lifeDurationTimer.Cancel();
304 // __lifeDurationTimer.Start(DEFAULT_LIFE_DURATION_MSEC);
305 // SysLog(NID_SHELL, "lifeDuration timer restarted (%d)msec", DEFAULT_LIFE_DURATION_MSEC);
309 _AppWidgetContext::OnTimerExpired(Tizen::Base::Runtime::Timer& timer)
311 // if( &timer == &__lifeDurationTimer)
313 // SysLog(NID_SHELL, "lifeDuration timer is expired, so terminating appWidget app(%ls)..", __providerId.GetPointer() );
315 //// ReleaseSharedMem();
316 // AppManager::GetInstance()->TerminateApplication(__appId);
320 if( &timer == &__updateTimer)
322 SysLog(NID_SHELL, "update timer is expired for appWidget app(%ls)..", __providerId.GetPointer() );
328 } } } // Tizen::Shell::App {