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 "provider_buffer.h"
28 #include <FBaseSysLog.h>
29 #include <FBase_StringConverter.h>
31 #include <FApp_AppControlManager.h>
33 #include "FShell_AppWidgetManagerService.h"
34 #include "FShell_AppWidgetPopupContext.h"
35 #include "FShell_AppWidgetContextBase.h"
36 #include "FShell_AppWidgetContext.h"
39 namespace Tizen { namespace Shell { namespace App
42 using namespace Tizen::App;
43 using namespace Tizen::Base;
44 using namespace Tizen::Base::Collection;
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";
53 const int DEFAULT_LIFE_DURATION_MSEC = 30000;//30sec
54 const int UPDATE_PERIOD_MSEC_MIN = 1800000;//30min
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)
62 // __lifeDurationTimer.Construct(*this);//, true);
63 // __lifeDurationTimer.Start(DEFAULT_LIFE_DURATION_MSEC );
65 __updateMillis = (__updateMillis > UPDATE_PERIOD_MSEC_MIN) ? __updateMillis : UPDATE_PERIOD_MSEC_MIN;
66 SysLog(NID_SHELL, "period(%d)", __updateMillis);
67 if( __updateMillis > 0)
69 __updateTimer.Construct(*this);//, false);
70 __updateTimer.StartAsRepeatable(__updateMillis);
73 __pPendingTouchEventList = new ArrayListT<PendingTouchEvent*>();
74 __pPendingTouchEventList->Construct();
77 _AppWidgetContext::~_AppWidgetContext()
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();
83 if (__pAppWidgetPopup)
85 SysLog(NID_SHELL, "Destroying dangling AppWidgetPopup instance..");
86 __pAppWidgetPopup->OnPopupDestoyed();
87 delete __pAppWidgetPopup;
91 if( __pPendingTouchEventList )
93 for( int i = 0; i < __pPendingTouchEventList->GetCount(); i++)
95 PendingTouchEvent* pTouchEvent = null;
96 __pPendingTouchEventList->GetAt(i, pTouchEvent);
99 __pPendingTouchEventList->RemoveAll();
100 delete __pPendingTouchEventList;
104 _AppWidgetPopupContext*
105 _AppWidgetContext::GetAppWidgetPopup() const
107 return __pAppWidgetPopup;
111 _AppWidgetContext::OnAdded(void)
113 SendAddRequest(__width, __height);
117 _AppWidgetContext::OnRemoved()
123 _AppWidgetContext::OnUpdate(const String& argument)
125 SysLog(NID_SHELL, "appId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority);
127 SendUpdateRequest(__width, __height, argument);
131 _AppWidgetContext::OnResize(int width, int height)
133 SysLog(NID_SHELL, "appId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority);
137 SendResizeRequest(__width, __height);
141 _AppWidgetContext::OnForeground()
143 SysLog(NID_SHELL, "");
144 __isForeground = true;
146 __updateTimer.Cancel();
147 __updateTimer.StartAsRepeatable(__updateMillis);
151 _AppWidgetContext::OnBackground()
153 SysLog(NID_SHELL, "");
154 __isForeground = false;
155 __updateTimer.Cancel();
159 _AppWidgetContext::OnPopupCreated(double x, double y, int width, int height)
161 __pAppWidgetPopup = new (std::nothrow) _AppWidgetPopupContext(__userInfo, __providerId, __instanceId, width, height, __priority, this);
162 __pAppWidgetPopup->SetIpcClientId(__ipcClientId);
163 __pAppWidgetPopup->OnPopupCreated(x, y, width, height);
165 // __lifeDurationTimer.Cancel();
169 _AppWidgetContext::OnPopupDestoyed(void)
171 SysLog(NID_SHELL, "");
172 if (__pAppWidgetPopup)
174 __pAppWidgetPopup->OnPopupDestoyed();
175 delete __pAppWidgetPopup;
176 __pAppWidgetPopup = null;
178 // RestartLifeDurationTimer();
182 _AppWidgetContext::SendAddRequest(int width, int height)
184 std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
186 return SendRequestToApp( __appId, APPWIDGET_ON_ADD, pArgs.get());
190 _AppWidgetContext::SendUpdateRequest(int width, int height, const String& argument)
192 std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
194 pArgs->Add(new String(ARG_KEY_ARGUMENT), new String(argument));
196 return SendRequestToApp( __appId, APPWIDGET_ON_UPDATE, pArgs.get());
200 _AppWidgetContext::SendResizeRequest(int width, int height)
202 std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
204 return SendRequestToApp( __appId, APPWIDGET_ON_RESIZE, pArgs.get());
208 _AppWidgetContext::SendRemoveRequest()
210 std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
212 return SendRequestToApp( __appId, APPWIDGET_ON_REMOVE, pArgs.get());
216 _AppWidgetContext::SendPendingTouchEvent(void)
218 for(int i =0; i< __pPendingTouchEventList->GetCount(); i++)
220 PendingTouchEvent* pTouchEvent = null;
221 __pPendingTouchEventList->GetAt(i, pTouchEvent);
222 AppWidgetManagerService::GetInstance()->SendTouchEvent(__ipcClientId, __instanceId, pTouchEvent->eventType, pTouchEvent->timeStamp, pTouchEvent->x, pTouchEvent->y);
225 __pPendingTouchEventList->RemoveAll();
229 _AppWidgetContext::SendTouchEvent(buffer_event eventType, double timeStamp, double x, double y)
231 if( HasValidClientId() && IsRunning() )
233 SysAssert(IsSharedMemCreated() == true);
234 SysLog(NID_SHELL, "send IPC message");
235 AppWidgetManagerService::GetInstance()->SendTouchEvent(__ipcClientId, __instanceId, eventType, timeStamp, x, y);
239 SysLog(NID_SHELL, "request to start AppControl");
240 __pPendingTouchEventList->Add(new PendingTouchEvent(eventType, timeStamp, x, y));
243 std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
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)));
250 return SendRequestToApp( __appId, APPWIDGET_ON_TOUCH, pArgs.get());
256 _AppWidgetContext::SendRequestToApp(const AppId& appId, const String& operation, HashMap* pArgs)
258 result r = _AppWidgetContextBase::SendRequestToApp(appId, operation, pArgs);
259 // RestartLifeDurationTimer();
265 _AppWidgetContext::RequestUpdateRemote(int width, int height)
267 /*if( GetAppWidgetPopup() != null)
269 SysLog(NID_SHELL, "AppWidgetPopup is appeared, so appWidget doesn't need to update");
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));
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");
279 SendPendingTouchEvent();
280 SysLog(NID_SHELL, "Done");
284 Tizen::Base::Collection::HashMap*
285 _AppWidgetContext::CreateRequestArgs(void)
287 HashMap* pArgs = new (std::nothrow) HashMap(SingleObjectDeleter);
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)));
300 //_AppWidgetContext::RestartLifeDurationTimer()
302 // __lifeDurationTimer.Cancel();
303 // __lifeDurationTimer.Start(DEFAULT_LIFE_DURATION_MSEC);
304 // SysLog(NID_SHELL, "lifeDuration timer restarted (%d)msec", DEFAULT_LIFE_DURATION_MSEC);
308 _AppWidgetContext::OnTimerExpired(Tizen::Base::Runtime::Timer& timer)
310 // if( &timer == &__lifeDurationTimer)
312 // SysLog(NID_SHELL, "lifeDuration timer is expired, so terminating appWidget app(%ls)..", __providerId.GetPointer() );
314 //// ReleaseSharedMem();
315 // AppManager::GetInstance()->TerminateApplication(__appId);
319 if( &timer == &__updateTimer)
321 SysLog(NID_SHELL, "update timer is expired for appWidget app(%ls)..", __providerId.GetPointer() );
327 } } } // Tizen::Shell::App {