change type of x, y position of LiveboxPopup to float
[framework/osp/appwidget-service.git] / src / FShell_LiveboxContext.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file        FShell_LiveboxContext.cpp
20  * @brief       This is the implementation for the _LiveboxContext class.
21  */
22
23 #include <stdlib.h>
24 #include <unique_ptr.h>
25
26 #include "provider_buffer.h"
27
28 #include <FBase.h>
29 #include <FBaseSysLog.h>
30 #include <FBase_StringConverter.h>
31
32 #include <FApp_AppControlManager.h>
33
34 #include "FShell_LiveboxManagerService.h"
35 #include "FShell_LiveboxPopupContext.h"
36 #include "FShell_LiveboxContextBase.h"
37 #include "FShell_LiveboxContext.h"
38
39
40 namespace Tizen { namespace Shell  { namespace App
41 {
42
43 using namespace Tizen::App;
44 using namespace Tizen::Base;
45 using namespace Tizen::Base::Collection;
46
47
48
49 const String LIVEBOX_ON_ADD(L"Livebox='event=add'");
50 const String LIVEBOX_ON_REMOVE(L"Livebox='event=remove'");
51 const String LIVEBOX_ON_UPDATE(L"Livebox='event=update'");
52 const String LIVEBOX_ON_RESIZE(L"Livebox='event=resize'");
53 const String LIVEBOX_ON_TOUCH(L"Livebox='event=touch'");
54
55 const String ARG_KEY_WIDTH = L"_Width";
56 const String ARG_KEY_HEIGHT = L"_Height";
57 const String ARG_KEY_ARGUMENT = L"_Argument";
58 const String ARG_KEY_EVENT_TYPE = L"_EventType";
59 const String ARG_KEY_TIME_STAMP = L"_TimeStamp";
60 const String ARG_KEY_X = L"_X";
61 const String ARG_KEY_Y = L"_Y";
62
63 const int LIVE_DURATION_MSEC= 30000;//30sec
64
65 _LiveboxContext::_LiveboxContext(const String& info, const String& providerId, const String& instanceId, int width, int height, int period, int priority)
66 :_LiveboxContextBase(TYPE_LB, info, providerId, instanceId, width, height, priority)
67 ,__pLiveboxPopup(null), __UpdateMillis(period)
68 {
69         SysLog(NID_APP, "period(%d)", period);
70
71         __lifeDurationTimer.Construct(*this);//, true);
72         __lifeDurationTimer.Start(LIVE_DURATION_MSEC);
73
74         SysLog(NID_APP, "period(%d)", __UpdateMillis);
75         if( __UpdateMillis > 0)
76         {
77                 __UpdateTimer.Construct(*this);//, false);
78                 __UpdateTimer.StartAsRepeatable(__UpdateMillis);
79         }
80 }
81
82 _LiveboxContext::~_LiveboxContext()
83 {
84         SysLog(NID_APP, "appId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority);
85         __lifeDurationTimer.Cancel();
86         __UpdateTimer.Cancel();
87 }
88
89 _LiveboxPopupContext*
90 _LiveboxContext::GetLiveboxPopup() const
91 {
92         return __pLiveboxPopup;
93 }
94
95 void
96 _LiveboxContext::OnAdded(void)
97 {
98         SendAddRequest(__width, __height);
99 }
100
101 void
102 _LiveboxContext::OnRemoved()
103 {
104         SendRemoveRequest();
105 }
106
107 void
108 _LiveboxContext::OnUpdate(const String& argument)
109 {
110         SysLog(NID_APP, "appId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority);
111
112         SendUpdateRequest(__width, __height, argument);
113 }
114
115 void
116 _LiveboxContext::OnResize(int width, int height)
117 {
118         SysLog(NID_APP, "appId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority);
119
120         __width = width;
121         __height = height;
122         SendResizeRequest(__width, __height);
123 }
124
125 void
126 _LiveboxContext::OnForeground()
127 {
128         SysLog(NID_APP, "");
129         __isForeground = true;
130
131         __UpdateTimer.Cancel();
132         __UpdateTimer.StartAsRepeatable(__UpdateMillis);
133 }
134
135 void
136 _LiveboxContext::OnBackground()
137 {
138         SysLog(NID_APP, "");
139         __isForeground = false;
140         __UpdateTimer.Cancel();
141 }
142
143 void
144 _LiveboxContext::OnPopupCreated(double x, double y, int width, int height)
145 {
146         __pLiveboxPopup = new (std::nothrow) _LiveboxPopupContext(__userInfo, __providerId, __instanceId, width, height, __priority);
147         __pLiveboxPopup->SetClientId(__ipcClientId);
148         __pLiveboxPopup->OnPopupCreated(x, y, width, height);
149
150         __lifeDurationTimer.Cancel();
151 }
152
153 void
154 _LiveboxContext::OnPopupDestoyed(void)
155 {
156         if (__pLiveboxPopup)
157         {
158                 __pLiveboxPopup->OnPopupDestoyed();
159                 delete __pLiveboxPopup;
160                 __pLiveboxPopup = null;
161         }
162         RestartLifeDurationTimer();
163 }
164
165 result
166 _LiveboxContext::SendAddRequest(int width, int height)
167 {
168 /*      std::unique_ptr<ArrayList, AllElementsDeleter> pArgs (new (std::nothrow) ArrayList);
169         pArgs->Construct();
170         pArgs->Add(*new String(__instanceId));
171         pArgs->Add(*new String(__providerId));
172         pArgs->Add(*new String(__info));
173         pArgs->Add(*new String(Integer::ToString(width)));
174         pArgs->Add(*new String(Integer::ToString(height)));*/
175
176         std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
177
178         pArgs->Add(new String(ARG_KEY_WIDTH), new String(Integer::ToString(width)));
179         pArgs->Add(new String(ARG_KEY_HEIGHT), new String(Integer::ToString(height)));
180
181         return SendRequestToApp( __appId, LIVEBOX_ON_ADD, pArgs.get());
182 }
183
184 result
185 _LiveboxContext::SendUpdateRequest(int width, int height, const String& argument)
186 {
187         std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
188
189         pArgs->Add(new String(ARG_KEY_WIDTH), new String(Integer::ToString(width)));
190         pArgs->Add(new String(ARG_KEY_HEIGHT), new String(Integer::ToString(height)));
191         pArgs->Add(new String(ARG_KEY_ARGUMENT), new String(argument));
192
193         return SendRequestToApp( __appId, LIVEBOX_ON_UPDATE, pArgs.get());
194 }
195
196 result
197 _LiveboxContext::SendResizeRequest(int width, int height)
198 {
199         std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
200
201         pArgs->Add(new String(ARG_KEY_WIDTH), new String(Integer::ToString(width)));
202         pArgs->Add(new String(ARG_KEY_HEIGHT), new String(Integer::ToString(height)));
203
204         return SendRequestToApp( __appId, LIVEBOX_ON_RESIZE, pArgs.get());
205 }
206
207 result
208 _LiveboxContext::SendRemoveRequest()
209 {
210         std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
211
212         return SendRequestToApp( __appId, LIVEBOX_ON_REMOVE, pArgs.get());
213 }
214
215 result
216 _LiveboxContext::SendTouchEvent(buffer_event event, double timestamp, double x, double y)
217 {
218         SysLog(NID_APP, "");
219         if( HasValidClientId() == false )
220         {
221                 std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
222
223                 pArgs->Add(new String(ARG_KEY_EVENT_TYPE), new String(Integer::ToString(event)));
224                 pArgs->Add(new String(ARG_KEY_TIME_STAMP), new String(Double::ToString(timestamp)));
225                 pArgs->Add(new String(ARG_KEY_X), new String(Double::ToString(x)));
226                 pArgs->Add(new String(ARG_KEY_Y), new String(Double::ToString(y)));
227
228                 return SendRequestToApp( __appId, LIVEBOX_ON_TOUCH, pArgs.get());
229         }
230         else
231         {
232 //              SysAssertf( Tizen::App::AppManager::GetInstance()->IsRunning(__appId) == false, "application isn't running");
233                 LiveboxManagerService::GetInstance()->SendTouchEvent(__ipcClientId, __instanceId, event, timestamp, x, y);
234         }
235         return E_SUCCESS;
236 }
237
238 result
239 _LiveboxContext::SendRequestToApp(const AppId& appId, const String& operation, HashMap* pArgs)
240 {
241         result r = _LiveboxContextBase::SendRequestToApp(appId, operation, pArgs);
242         RestartLifeDurationTimer();
243
244         return r;
245 }
246
247 result
248 _LiveboxContext::RequestUpdateRemote(int width, int height)
249 {
250         /*if( GetLiveboxPopup() != null)
251         {
252                 SysLog(NID_APP, "LiveboxPopup is appeared, so livebox doesn't need to update");
253                 return E_SUCCESS;
254         }*/
255         std::unique_ptr<char[]> packageName(_StringConverter::CopyToCharArrayN(__providerId));
256         std::unique_ptr<char[]> id(_StringConverter::CopyToCharArrayN(__instanceId));
257         std::unique_ptr<char[]> content_info(_StringConverter::CopyToCharArrayN(__userInfo));
258
259         int ret = provider_send_updated(packageName.get(), id.get(), width, height, __priority, content_info.get(), null);
260         SysTryReturnResult(NID_APP, ret >= 0 , E_SYSTEM, "[E_SYSTEM] failed to provider_send_updated");
261
262         SysLog(NID_APP, "Done");
263         return E_SUCCESS;
264 }
265
266 void
267 _LiveboxContext::RestartLifeDurationTimer()
268 {
269         __lifeDurationTimer.Cancel();
270         __lifeDurationTimer.Start(LIVE_DURATION_MSEC);
271         SysLog(NID_APP, "lifeDuration timer restarted (%d)msec", LIVE_DURATION_MSEC);
272 }
273
274 void
275 _LiveboxContext::OnTimerExpired(Tizen::Base::Runtime::Timer& timer)
276 {
277         SysLog(NID_APP, "");
278
279         if( &timer == &__lifeDurationTimer)
280         {
281                 SysLog(NID_APP, "lifeDuration timer is expired, so terminating livebox app(%ls)..", __providerId.GetPointer() );
282
283                 ReleaseSharedMem();
284                 AppManager::GetInstance()->TerminateApplication(__appId);
285
286         }
287         else if( &timer == &__UpdateTimer)
288         {
289                 SysLog(NID_APP, "update timer is expired for livebox app(%ls)..", __providerId.GetPointer() );
290                 OnUpdate(L"");
291         }
292 }
293
294
295 } } } // Tizen::Shell::App {
296