2 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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.
18 * @file FShell_AppWidgetContext.cpp
19 * @brief This is the implementation for the _AppWidgetContext class.
23 #include <unique_ptr.h>
25 #include <FBaseSysLog.h>
26 #include <FBaseInteger.h>
27 #include <FBaseDouble.h>
28 #include <FBaseColHashMap.h>
29 #include <FBaseColAllElementsDeleter.h>
30 #include <FSysSystemTime.h>
31 #include <FAppAppManager.h>
33 #include <FBase_StringConverter.h>
34 #include "FShell_AppWidgetManagerService.h"
35 #include "FShell_AppWidgetPopupContext.h"
36 #include "FShell_AppWidgetContext.h"
38 namespace Tizen { namespace Shell { namespace App
41 using namespace Tizen::App;
42 using namespace Tizen::Base;
43 using namespace Tizen::Base::Collection;
44 using namespace Tizen::System;
46 const String APPWIDGET_ON_ADD = L"http://tizen.org/appcontrol/appwidget/add";
47 const String APPWIDGET_ON_REMOVE = L"http://tizen.org/appcontrol/appwidget/remove";
48 const String APPWIDGET_ON_UPDATE = L"http://tizen.org/appcontrol/appwidget/update";
49 const String APPWIDGET_ON_RESIZE = L"http://tizen.org/appcontrol/appwidget/resize";
50 const String APPWIDGET_ON_TOUCH = L"http://tizen.org/appcontrol/appwidget/touch";
52 const int UPDATE_PERIOD_MSEC_MIN = 1800000; // 30min
54 _AppWidgetContext::_AppWidgetContext(const String& info, const String& providerId, const String& instanceId, int width, int height, int period, int priority)
55 :_AppWidgetContextBase(TYPE_LB, info, providerId, instanceId, width, height, priority)
56 ,__pAppWidgetPopup(null)
57 ,__updateMillis(period)
59 ,__pPendingTouchEventList(null)
60 ,__pPendingEventList(null)
61 , __hasPendingRequest(false)
63 if (__updateMillis > 0)
65 __updateMillis = (__updateMillis > UPDATE_PERIOD_MSEC_MIN) ? __updateMillis : UPDATE_PERIOD_MSEC_MIN;
66 SysLog(NID_SHELL, "period(%d)", __updateMillis);
68 SystemTime::GetTicks(this->__lastUpdatedTime);
70 __updateTimer.Construct(*this);
71 __updateTimer.StartAsRepeatable(__updateMillis);
74 __pPendingTouchEventList = new ArrayListT<PendingTouchEvent*>();
75 __pPendingTouchEventList->Construct();
77 __pPendingEventList = new ArrayListT<PendingEvent*>();
78 __pPendingEventList->Construct();
83 _AppWidgetContext::~_AppWidgetContext(void)
85 SysSecureLog(NID_SHELL, "appId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", GetProviderId().GetPointer(), GetInstanceId().GetPointer(), GetWidth(), GetHeight(), GetPriority());
87 __updateTimer.Cancel();
89 if (__pAppWidgetPopup)
91 SysLog(NID_SHELL, "Destroying dangling AppWidgetPopup instance..");
92 __pAppWidgetPopup->OnPopupDestoyed();
93 delete __pAppWidgetPopup;
96 if (__pPendingTouchEventList)
98 for (int i = 0; i < __pPendingTouchEventList->GetCount(); i++)
100 PendingTouchEvent* pTouchEvent = null;
101 __pPendingTouchEventList->GetAt(i, pTouchEvent);
104 __pPendingTouchEventList->RemoveAll();
105 delete __pPendingTouchEventList;
108 if (__pPendingEventList)
110 for (int i = 0; i < __pPendingEventList->GetCount(); i++)
112 PendingEvent* pEvent = null;
113 __pPendingEventList->GetAt(i, pEvent);
116 __pPendingEventList->RemoveAll();
117 delete __pPendingEventList;
121 _AppWidgetPopupContext*
122 _AppWidgetContext::GetAppWidgetPopup(void) const
124 return __pAppWidgetPopup;
128 _AppWidgetContext::OnAdded(void)
130 SendAddRequest(GetWidth(), GetHeight());
134 _AppWidgetContext::OnRemoved(void)
136 return SendRemoveRequest();
140 _AppWidgetContext::OnUpdate(const String& argument)
144 AppWidgetManagerService* pMgrService = AppWidgetManagerService::GetInstance();
145 SysTryReturnVoidResult(NID_SHELL, pMgrService, E_SYSTEM, "[E_SYSTEM] Failed to get an instance of AppWidgetManagerService.");
147 // Queueing the pointer of paused context.
148 if (this->IsPaused())
151 if (!argument.IsEmpty())
153 __pendingArgument = argument;
156 __hasPendingRequest = true;
157 SysLog(NID_SHELL, "Update is requested but the %ls is paused.", (this->GetAppId()).GetPointer());
161 result r = this->SendUpdateRequest(GetWidth(), GetHeight(), argument);
162 SysTryLog(NID_SHELL, !IsFailed(r), "Failed to execute SendUpdateRequest.");
164 SystemTime::GetTicks(this->__lastUpdatedTime);
165 SysLog(NID_SHELL, "The last updated time is %lld.", this->__lastUpdatedTime);
167 __hasPendingRequest = false;
168 __pendingArgument.Clear();
173 _AppWidgetContext::OnResize(int width, int height)
175 SysSecureLog(NID_SHELL, "appId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", GetProviderId().GetPointer(), GetInstanceId().GetPointer(), GetWidth(), GetHeight(), GetPriority());
180 SendResizeRequest(width, height);
184 _AppWidgetContext::RequestUpdate(const String& argument)
186 AppWidgetManagerService* pMgrService = AppWidgetManagerService::GetInstance();
187 SysTryReturnResult(NID_SHELL, pMgrService, E_SYSTEM, "[E_SYSTEM] Failed to get an instance of AppWidgetManagerService.");
189 result r = pMgrService->RequestUpdate(this, argument);
190 SysTryReturnResult(NID_SHELL, !IsFailed(r), r, "Failed to request update.");
196 _AppWidgetContext::OnForeground(void)
198 SysLog(NID_SHELL, "I");
202 SysLog(NID_SHELL, "%ls is already resumed.", (this->GetAppId()).GetPointer());
206 this->SetForeground(true);
208 if (__hasPendingRequest == true)
210 RequestUpdate(__pendingArgument);
214 if (this->GetPeriod() > 0)
216 long long currentTicks = 0;
217 SystemTime::GetTicks(currentTicks);
219 SysLog(NID_SHELL, "current[%lld] - updatedTime[%lld] = [%lld], period[%d]",
220 currentTicks, this->GetLastUpdatedTime(), currentTicks - this->GetLastUpdatedTime(), this->GetPeriod());
222 bool isPeriodExpired = (currentTicks - this->GetLastUpdatedTime()) >= (this->GetPeriod());
225 SysLog(NID_SHELL, "The period is expired.");
231 SysLog(NID_SHELL, "O");
235 _AppWidgetContext::OnBackground(void)
237 SysLog(NID_SHELL, "OnBackground");
241 SysLog(NID_SHELL, "%ls is already paused.", (this->GetAppId()).GetPointer());
244 SetForeground(false);
248 _AppWidgetContext::IsPaused(void) const
250 return IsForeground() ? false : true;
254 _AppWidgetContext::OnPopupCreated(double x, double y, int width, int height)
256 __pAppWidgetPopup = new (std::nothrow) _AppWidgetPopupContext(GetUserInfo(), GetProviderId(), GetInstanceId(), GetWidth(), GetHeight(), GetPriority(), this);
257 __pAppWidgetPopup->SetIpcClientId(GetClientId());
258 __pAppWidgetPopup->OnPopupCreated(x, y, width, height);
262 _AppWidgetContext::OnPopupDestoyed(void)
264 SysLog(NID_SHELL, "");
266 if (__pAppWidgetPopup)
268 __pAppWidgetPopup->OnPopupDestoyed();
269 delete __pAppWidgetPopup;
270 __pAppWidgetPopup = null;
275 _AppWidgetContext::SendAddRequest(int width, int height)
277 std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgsN());
279 return SendRequestToApp(GetAppId(), APPWIDGET_ON_ADD, pArgs.release());
283 _AppWidgetContext::SendUpdateRequest(int width, int height, const String& argument)
285 std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgsN());
287 pArgs->Add(new String(ARG_KEY_ARGUMENT), new String(argument));
289 return SendRequestToApp(GetAppId(), APPWIDGET_ON_UPDATE, pArgs.release());
293 _AppWidgetContext::SendResizeRequest(int width, int height)
295 std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgsN() );
297 return SendRequestToApp(GetAppId(), APPWIDGET_ON_RESIZE, pArgs.release());
301 _AppWidgetContext::SendRemoveRequest(void)
303 std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgsN());
305 return SendRequestToApp(GetAppId(), APPWIDGET_ON_REMOVE, pArgs.release());
309 _AppWidgetContext::SendPendingEvent(void)
311 SysTryReturnResult(NID_SHELL, __pPendingEventList->GetCount() > 0 , E_SUCCESS, "There is no pending event.");
313 PendingEvent* pEvent = null;
314 __pPendingEventList->GetAt(0, pEvent);
318 SysLog(NID_SHELL, "SendPendingEvent by IPC [%d existed.]", __pPendingEventList->GetCount());
319 _AppWidgetRequestHelper::SendRequestToApp(GetClientId(), pEvent->operation, pEvent->pArg);
320 __pPendingEventList->RemoveAt(0);
324 SysLog(NID_SHELL, "SendPendingEvent by IPC [0 existed.]");
331 _AppWidgetContext::SendPendingTouchEvent(void)
333 for (int i =0; i< __pPendingTouchEventList->GetCount(); i++)
335 PendingTouchEvent* pTouchEvent = null;
336 __pPendingTouchEventList->GetAt(i, pTouchEvent);
337 AppWidgetManagerService::GetInstance()->SendTouchEvent(GetClientId(), GetInstanceId(), pTouchEvent->eventType, pTouchEvent->timeStamp, pTouchEvent->x, pTouchEvent->y);
340 __pPendingTouchEventList->RemoveAll();
344 _AppWidgetContext::SendTouchEvent(buffer_event eventType, double timeStamp, double x, double y)
346 if (HasValidClientId() && IsRunning())
348 SysAssert(IsSharedMemCreated() == true);
349 SysLog(NID_SHELL, "%d, %f, %f", eventType, x, y);
350 AppWidgetManagerService::GetInstance()->SendTouchEvent(GetClientId(), GetInstanceId(), eventType, timeStamp, x, y);
354 __pPendingTouchEventList->Add(new PendingTouchEvent(eventType, timeStamp, x, y));
356 if( AppManager::GetInstance()->IsRunning(this->GetAppId() ) == false)
358 SysLog(NID_SHELL, "request to start AppControl");
359 std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgsN() );
361 // TODO: consider to remove these unused args.
362 pArgs->Add(new String(ARG_KEY_EVENT_TYPE), new String(Integer::ToString(eventType)));
363 pArgs->Add(new String(ARG_KEY_TIME_STAMP), new String(Double::ToString(timeStamp)));
364 pArgs->Add(new String(ARG_KEY_X), new String(Double::ToString(x)));
365 pArgs->Add(new String(ARG_KEY_Y), new String(Double::ToString(y)));
367 return SendRequestToApp(GetAppId(), APPWIDGET_ON_TOUCH, pArgs.get());
374 _AppWidgetContext::SendRequestToApp(const AppId& appId, const String& operation, HashMap* pArgs)
376 result r = E_SUCCESS;
378 if (AppManager::GetInstance()->IsRunning(appId) == false)
380 SysLog(NID_SHELL, "The application is not running.");
381 return _AppWidgetContextBase::SendRequestToApp(appId, operation, pArgs);
385 if(!HasValidClientId())
387 SysLog(NID_SHELL, "The application is running but IPC is not connected yet.");
388 __pPendingEventList->Add(new PendingEvent(*new String(GetInstanceId()), *new String(operation), pArgs));
392 SysLog(NID_SHELL, "The application is running and IPC is connected.");
393 r = _AppWidgetRequestHelper::SendRequestToApp(GetClientId(), operation, pArgs);
395 // pArgs->RemoveAll(true);
404 _AppWidgetContext::RequestUpdateRemote(int width, int height)
406 std::unique_ptr<char[]> packageName(_StringConverter::CopyToCharArrayN(GetProviderId()));
407 std::unique_ptr<char[]> id(_StringConverter::CopyToCharArrayN(GetInstanceId()));
408 std::unique_ptr<char[]> content_info(_StringConverter::CopyToCharArrayN(GetUserInfo()));
410 int ret = provider_send_updated(packageName.get(), id.get(), width, height, GetPriority(), content_info.get(), null);
411 SysTryReturnResult(NID_SHELL, ret >= 0 , E_SYSTEM, "[E_SYSTEM] failed to provider_send_updated");
413 SendPendingTouchEvent();
414 SysLog(NID_SHELL, "Done");
418 Tizen::Base::Collection::HashMap*
419 _AppWidgetContext::CreateRequestArgsN(void)
421 HashMap* pArgs = new (std::nothrow) HashMap(SingleObjectDeleter);
424 pArgs->Add(new String(ARG_KEY_INSTANCE_ID), new String(GetInstanceId()));
425 pArgs->Add(new String(ARG_KEY_PROVIDER_NAME), new String(GetProviderName()));
426 pArgs->Add(new String(ARG_KEY_USER_INFO), new String(GetUserInfo()));
427 pArgs->Add(new String(ARG_KEY_WIDTH), new String(Integer::ToString(GetWidth())));
428 pArgs->Add(new String(ARG_KEY_HEIGHT), new String(Integer::ToString(GetHeight())));
434 _AppWidgetContext::GetPeriod(void) const
436 SysLog(NID_SHELL, "GetPeriod[%d]", __updateMillis);
437 return __updateMillis;
441 _AppWidgetContext::GetLastUpdatedTime(void) const
443 return __lastUpdatedTime;
447 _AppWidgetContext::SetIpcClientId(int clientId)
449 _AppWidgetContextBase::SetIpcClientId(clientId);
453 _AppWidgetContext::OnTimerExpired(Tizen::Base::Runtime::Timer& timer)
455 if (&timer == &__updateTimer)
457 SysLog(NID_SHELL, "update timer is expired for appWidget app(%ls).", GetProviderId().GetPointer());
462 }}} // Tizen::Shell::App