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 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";
60 const int DEFAULT_LIFE_DURATION_MSEC = 30000;//30sec
61 const int UPDATE_PERIOD_MSEC_MIN = 1800000;//30min
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)
69 // __lifeDurationTimer.Construct(*this);//, true);
70 // __lifeDurationTimer.Start(DEFAULT_LIFE_DURATION_MSEC );
72 __updateMillis = (__updateMillis > UPDATE_PERIOD_MSEC_MIN) ? __updateMillis : UPDATE_PERIOD_MSEC_MIN;
73 SysLog(NID_APP, "period(%d)", __updateMillis);
74 if( __updateMillis > 0)
76 __UpdateTimer.Construct(*this);//, false);
77 __UpdateTimer.StartAsRepeatable(__updateMillis);
80 __pPendingTouchEventList = new ArrayListT<PendingTouchEvent*>();
81 __pPendingTouchEventList->Construct();
84 _AppWidgetContext::~_AppWidgetContext()
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();
90 if (__pAppWidgetPopup)
92 SysLog(NID_APP, "Destroying dangling AppWidgetPopup instance..");
93 __pAppWidgetPopup->OnPopupDestoyed();
94 delete __pAppWidgetPopup;
98 if( __pPendingTouchEventList )
100 for( int i = 0; i < __pPendingTouchEventList->GetCount(); i++)
102 PendingTouchEvent* pTouchEvent = null;
103 __pPendingTouchEventList->GetAt(i, pTouchEvent);
106 __pPendingTouchEventList->RemoveAll();
107 delete __pPendingTouchEventList;
111 _AppWidgetPopupContext*
112 _AppWidgetContext::GetAppWidgetPopup() const
114 return __pAppWidgetPopup;
118 _AppWidgetContext::OnAdded(void)
120 SendAddRequest(__width, __height);
124 _AppWidgetContext::OnRemoved()
130 _AppWidgetContext::OnUpdate(const String& argument)
132 SysLog(NID_APP, "appId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority);
134 SendUpdateRequest(__width, __height, argument);
138 _AppWidgetContext::OnResize(int width, int height)
140 SysLog(NID_APP, "appId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority);
144 SendResizeRequest(__width, __height);
148 _AppWidgetContext::OnForeground()
151 __isForeground = true;
153 __UpdateTimer.Cancel();
154 __UpdateTimer.StartAsRepeatable(__updateMillis);
158 _AppWidgetContext::OnBackground()
161 __isForeground = false;
162 __UpdateTimer.Cancel();
166 _AppWidgetContext::OnPopupCreated(double x, double y, int width, int height)
168 __pAppWidgetPopup = new (std::nothrow) _AppWidgetPopupContext(__userInfo, __providerId, __instanceId, width, height, __priority);
169 __pAppWidgetPopup->SetIpcClientId(__ipcClientId);
170 __pAppWidgetPopup->OnPopupCreated(x, y, width, height);
172 // __lifeDurationTimer.Cancel();
176 _AppWidgetContext::OnPopupDestoyed(void)
179 if (__pAppWidgetPopup)
181 __pAppWidgetPopup->OnPopupDestoyed();
182 delete __pAppWidgetPopup;
183 __pAppWidgetPopup = null;
185 // RestartLifeDurationTimer();
189 _AppWidgetContext::SendAddRequest(int width, int height)
191 std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
193 return SendRequestToApp( __appId, APPWIDGET_ON_ADD, pArgs.get());
197 _AppWidgetContext::SendUpdateRequest(int width, int height, const String& argument)
199 std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
201 pArgs->Add(new String(ARG_KEY_ARGUMENT), new String(argument));
203 return SendRequestToApp( __appId, APPWIDGET_ON_UPDATE, pArgs.get());
207 _AppWidgetContext::SendResizeRequest(int width, int height)
209 std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
211 return SendRequestToApp( __appId, APPWIDGET_ON_RESIZE, pArgs.get());
215 _AppWidgetContext::SendRemoveRequest()
217 std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
219 return SendRequestToApp( __appId, APPWIDGET_ON_REMOVE, pArgs.get());
223 _AppWidgetContext::SendPendingTouchEvent(void)
225 for(int i =0; i< __pPendingTouchEventList->GetCount(); i++)
227 PendingTouchEvent* pTouchEvent = null;
228 __pPendingTouchEventList->GetAt(i, pTouchEvent);
229 AppWidgetManagerService::GetInstance()->SendTouchEvent(__ipcClientId, __instanceId, pTouchEvent->eventType, pTouchEvent->timeStamp, pTouchEvent->x, pTouchEvent->y);
232 __pPendingTouchEventList->RemoveAll();
236 _AppWidgetContext::SendTouchEvent(buffer_event eventType, double timeStamp, double x, double y)
238 if( HasValidClientId() && IsRunning() )
240 SysAssert(IsSharedMemCreated() == true);
241 SysLog(NID_APP, "send IPC message");
242 AppWidgetManagerService::GetInstance()->SendTouchEvent(__ipcClientId, __instanceId, eventType, timeStamp, x, y);
246 SysLog(NID_APP, "request to start AppControl");
247 __pPendingTouchEventList->Add(new PendingTouchEvent(eventType, timeStamp, x, y));
250 std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
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)));
257 return SendRequestToApp( __appId, APPWIDGET_ON_TOUCH, pArgs.get());
263 _AppWidgetContext::SendRequestToApp(const AppId& appId, const String& operation, HashMap* pArgs)
265 result r = _AppWidgetContextBase::SendRequestToApp(appId, operation, pArgs);
266 RestartLifeDurationTimer();
272 _AppWidgetContext::RequestUpdateRemote(int width, int height)
274 /*if( GetAppWidgetPopup() != null)
276 SysLog(NID_APP, "AppWidgetPopup is appeared, so appWidget doesn't need to update");
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));
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");
286 SendPendingTouchEvent();
287 SysLog(NID_APP, "Done");
292 //_AppWidgetContext::RestartLifeDurationTimer()
294 // __lifeDurationTimer.Cancel();
295 // __lifeDurationTimer.Start(DEFAULT_LIFE_DURATION_MSEC);
296 // SysLog(NID_APP, "lifeDuration timer restarted (%d)msec", DEFAULT_LIFE_DURATION_MSEC);
300 _AppWidgetContext::OnTimerExpired(Tizen::Base::Runtime::Timer& timer)
302 // if( &timer == &__lifeDurationTimer)
304 // SysLog(NID_APP, "lifeDuration timer is expired, so terminating appWidget app(%ls)..", __providerId.GetPointer() );
306 //// ReleaseSharedMem();
307 // AppManager::GetInstance()->TerminateApplication(__appId);
311 if( &timer == &__UpdateTimer)
313 SysLog(NID_APP, "update timer is expired for appWidget app(%ls)..", __providerId.GetPointer() );
319 } } } // Tizen::Shell::App {