2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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 FApp_UiAppImpl.cpp
20 * @brief This is the implementation for the _UiAppImpl class.
24 #include <appsvc/appsvc.h>
26 #include <FBaseSysLog.h>
27 #include <FBaseColArrayList.h>
28 #include <FAppAppRegistry.h>
29 #include <FUiCtrlFrame.h>
31 #include <FBaseRt_Process.h>
32 #include <FUi_ControlImplManager.h>
33 #include <FUi_KeyEventManager.h>
34 #include <FUi_WindowImpl.h>
35 #include <FUi_EcoreEvasMgr.h>
36 #include <FUi_EcoreEvas.h>
37 #include <FUiCtrl_FrameImpl.h>
38 #include <FSys_PowerManagerImpl.h>
40 #include "FApp_AppFrame.h"
41 #include "FApp_AppInfo.h"
42 #include "FApp_AppImpl.h"
43 #include "FApp_UiAppImpl.h"
44 #include "FApp_AppArg.h"
46 using namespace Tizen::App;
47 using namespace Tizen::Base;
48 using namespace Tizen::Base::Collection;
49 using namespace Tizen::Base::Runtime;
50 using namespace Tizen::Ui;
51 using namespace Tizen::Ui::Controls;
52 using namespace Tizen::System;
53 using namespace Tizen::Graphics;
56 extern "C" int appsvc_request_transient_app(bundle*, Ecore_X_Window, appsvc_host_res_fn, void*);
58 namespace Tizen { namespace App
61 _UiAppImpl* _UiAppImpl::__pUiAppImpl = null;
64 _UiAppImpl::_UiAppImpl(UiApp* pUiApp)
66 , __pAppImpl(_AppImpl::GetInstance())
67 , __appUiState(APP_UI_STATE_BACKGROUND)
72 __pFrameList = new (std::nothrow) ArrayList();
73 SysTryReturnVoidResult(NID_APP, __pFrameList != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
74 __pFrameList->Construct();
75 SysTryReturnVoidResult(NID_APP, __pAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
79 _UiAppImpl::~_UiAppImpl(void)
89 _UiAppImpl::OnCreate(void)
91 SysLog(NID_APP, "Platform creation event.");
93 _AppInfo::SetAppState(INITIALIZING);
100 TransientResponseCb(void* pData)
102 SysLog(NID_APP, "Handling cleanup for submode app.");
104 // platform invokes ecore_main_loop_quit() after returning this callback
109 _UiAppImpl::OnService(service_s* service, bool initial)
111 Frame* pFrame = dynamic_cast<Frame*>(__pFrameList->GetAt(0));
112 _EcoreEvas* pEvas = GetEcoreEvasMgr()->GetEcoreEvas();
114 // make OnForeground event
118 if (pEvas && _AppInfo::IsSubMode())
120 _FrameImpl* pFrameImpl = _FrameImpl::GetInstance(*pFrame);
121 if (pFrameImpl != null)
123 unsigned int curHandle = pFrameImpl->GetNativeHandle();
125 bundle* pBundle = _AppArg::GetBundleFromSvc(service);
126 int ret = appsvc_request_transient_app(pBundle, curHandle, TransientResponseCb, NULL);
128 SysLog(NID_APP, "Transient sets for (0x%x) with result (%d).", curHandle, ret);
134 const int type = _AppInfo::GetAppType();
135 if (type & _APP_TYPE_IME_APP)
137 SysLog(NID_APP, "Skipping 1st resume for IME app.");
146 // [INFO] to confirm that the window is not foreground
149 int pid = pEvas->GetProcessId(pEvas->GetActiveWindow());
150 SysLog(NID_APP, "%d -> %d", pid, _AppInfo::GetProcessId());
151 if (pid != _AppInfo::GetProcessId())
157 _FrameImpl* pFrameImpl = _FrameImpl::GetInstance(*pFrame);
159 // [FIXME] Multi window handling
160 if (pFrameImpl != null)
162 // N_SE-24616, N_SE-24383 for OnForground() and visibility issue
164 pEvas->ActivateWindow(pFrameImpl->GetCore());
174 _UiAppImpl::OnTerminate(void)
176 SysLog(NID_APP, "Termination event 0x%x state", _AppInfo::GetAppState());
178 if (__pUiApp->GetAppUiState() == APP_UI_STATE_FOREGROUND)
183 if (OnUiAppImplTerminating() != true)
185 SysLog(NID_APP, "[E_SYSTEM] The Termination of application failed.");
191 _UiAppImpl::OnResume(void)
193 SysLog(NID_APP, "System resume event on 0x%x state", _AppInfo::GetAppState());
195 if (_AppInfo::GetAppState() == RUNNING)
203 _UiAppImpl::OnPause(void)
205 SysLog(NID_APP, "System pause event on 0x%x state", _AppInfo::GetAppState());
207 if (_AppInfo::GetAppState() == RUNNING)
215 _UiAppImpl::OnDeviceOrientationChanged(app_device_orientation_e orientation)
217 SysLog(NID_APP, "System device orientation event.");
219 _ControlImplManager* pControlImplManager = _ControlImplManager::GetInstance();
220 if (pControlImplManager == null)
222 SysLog(NID_APP, "Device orientation event arrived too early.");
226 pControlImplManager->OnScreenRotated(orientation);
231 _UiAppImpl::OnWindowHandleRequest(void)
233 const _EcoreEvas* const pEvas = GetEcoreEvasMgr()->GetEcoreEvas();
234 return (pEvas) ? static_cast<long>(pEvas->GetXWindow()) : -1;
239 _UiAppImpl::AddFrame(const Frame& frame)
241 result r = E_SUCCESS;
242 Frame& tmpFrame = const_cast <Frame&>(frame);
244 SysTryReturnResult(NID_APP, __pFrameList != null, E_INVALID_STATE, "Getting FrameList failed.");
245 SysTryReturnResult(NID_APP, !__pFrameList->Contains(tmpFrame), E_OBJ_ALREADY_EXIST, "The frame is already registered.");
247 _WindowImpl* pFrameImpl = _WindowImpl::GetInstance(tmpFrame);
248 SysTryReturnResult(NID_APP, pFrameImpl != null, E_INVALID_ARG, "The frame is not constructed yet.");
250 __pFrameList->Add(tmpFrame);
251 r = pFrameImpl->Open(false); // Attach to the main tree without 'draw & show'.
254 SysLog(NID_UI, "Failed to attach frame.");
255 __pFrameList->Remove(tmpFrame);
263 _UiAppImpl::GetAppFrame(void)
265 SysTryReturn(NID_APP, __pFrameList != null, null, E_INVALID_STATE, "[E_INVALID_STATE] Getting FrameList failed.");
266 int frameCount = __pFrameList->GetCount();
270 SysLogException(NID_APP, E_OBJ_NOT_FOUND, "There is no frame !!! use AddFrame() before call GetAppFrame()");
271 SysAssertf(false, "There is no frame !!! use AddFrame() before call GetAppFrame()");
274 Frame* pFrame = dynamic_cast <Frame*>(__pFrameList->GetAt(frameCount - 1));
278 SysLogException(NID_APP, E_OBJ_NOT_FOUND, "There is no frame !!! use AddFrame() before call GetAppFrame()");
279 SysAssertf(false, "There is no frame !!! use AddFrame() before call GetAppFrame()");
282 if (__pAppFrame == null)
284 __pAppFrame = new (std::nothrow) _AppFrame(*pFrame);
285 SysTryReturn(NID_APP, __pAppFrame != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
286 __pAppFrame->Construct();
288 else if (__pAppFrame->GetFrame() != pFrame)
290 __pAppFrame->SetFrame(pFrame);
298 _UiAppImpl::RemoveFrame(const Frame& frame)
300 result r = E_SUCCESS;
301 SysTryReturnResult(NID_APP, __pFrameList != null, E_INVALID_STATE, "Getting FrameList failed.");
303 Frame& tmpFrame = const_cast <Frame&>(frame);
304 _WindowImpl* pFrameImpl = _WindowImpl::GetInstance(tmpFrame);
306 r = __pFrameList->Remove(frame, false);
310 pFrameImpl->Destroy();
318 _UiAppImpl::RemoveAllFrames(void)
320 result r = E_SUCCESS;
321 SysTryReturnResult(NID_APP, __pFrameList != null, E_INVALID_STATE, "Getting FrameList failed.");
323 int frameCount = __pFrameList->GetCount();
324 for (int i = 0; i < frameCount; i++)
326 Frame* pFrame = dynamic_cast <Frame*>(__pFrameList->GetAt(i));
329 _WindowImpl* pFrameImpl = _WindowImpl::GetInstance(*pFrame);
332 pFrameImpl->Destroy();
337 if (__pFrameList->GetCount() > 0)
339 __pFrameList->RemoveAll(false);
350 _UiAppImpl::GetFrameList(void)
357 _UiAppImpl::GetFrame(const String& name)
359 Frame* pFrame = null;
360 SysTryReturn(NID_APP, __pFrameList != null, null, E_INVALID_STATE, "[E_INVALID_STATE] Getting FrameList failed.");
361 int frameCount = __pFrameList->GetCount();
363 for (int i = 0; i < frameCount; i++)
365 pFrame = dynamic_cast <Frame*>(__pFrameList->GetAt(i));
367 if (pFrame != null && pFrame->GetName() == name)
378 _UiAppImpl::GetFrameAt(int index)
380 Frame* pFrame = null;
381 SysTryReturn(NID_APP, __pFrameList != null, null, E_INVALID_STATE, "[E_INVALID_STATE] Getting FrameList failed.");
382 SysTryReturn(NID_APP, index >= 0, null, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The index is less than 0.");
384 int frameCount = __pFrameList->GetCount();
385 SysTryReturn(NID_APP, index < frameCount, null, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The index is greater than the number of frames.");
387 pFrame = dynamic_cast <Frame*>(__pFrameList->GetAt(index));
393 _UiAppImpl::OnAppInitializing(void)
395 // Do Ui related initializing for UiApp
396 result r = InitializeUiFramework();
399 SysLogException(NID_APP, E_SYSTEM, "Getting resolution information failure. Application may not be installed correctly.");
403 _KeyEventManager* pKeyManager = _KeyEventManager::GetInstance();
406 pKeyManager->AddKeyEventListener(*this);
409 // API versioning for initial frame creation
410 if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
411 { // if API version is less than 3.0, create initial frame
412 Frame* pDefaultFrame = new (std::nothrow) Frame();
413 SysTryReturn(NID_APP, pDefaultFrame != null, false, r = E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Frame creation failed.");
414 pDefaultFrame->Construct();
415 AddFrame(*pDefaultFrame);
416 SysLog(NID_APP, "Default frame is added for API version %d Compatibility.", _AppInfo::GetApiVersion());
419 SysTryReturn(NID_APP, __pUiApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting UiApp instance failed.");
421 SysLog(NID_APP, "Entering user OnAppInitializing().");
422 const bool bReturn = __pUiApp->OnAppInitializing(*(AppRegistry::GetInstance()));
424 SysLog(NID_APP, "Back to the platform initializing routine.");
430 _UiAppImpl::OnAppInitialized(void)
432 SysTryReturn(NID_APP, __pUiApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting UiApp instance failed.");
434 const bool b = __pUiApp->OnAppInitialized();
436 Frame* pFrame = dynamic_cast<Frame*>(__pFrameList->GetAt(0));
439 int type = _AppInfo::GetAppType();
440 if (type & _APP_TYPE_IME_APP)
442 SysLog(NID_APP, "Defering frame update for IME app.");
443 pFrame->SetShowState(false);
449 if (__pFrameList->GetCount() > 0)
455 SysLogException(NID_APP, E_OBJ_NOT_FOUND, "There is no frame !!! use AddFrame() before returning OnAppInitialized()");
456 SysAssertf(false, "There is no frame !!! use AddFrame() before returning OnAppInitialized()");
464 _UiAppImpl::OnUiAppImplTerminating(void)
470 if (_AppInfo::GetAppState() != TERMINATED)
472 result = __pUiApp->OnAppTerminating(*(AppRegistry::GetInstance()), __pAppImpl->IsForcedTermination());
473 _AppInfo::SetAppState(TERMINATED);
476 // Do Ui related finalizing for UiApp
477 FinalizeUiFramework();
484 _UiAppImpl::OnKeyPressed(const _Control& source, const _KeyInfo& keyInfo)
491 _UiAppImpl::OnKeyReleased(const _Control& source, const _KeyInfo& keyInfo)
493 if (__appUiState == APP_UI_STATE_FOREGROUND)
495 if (keyInfo.GetKeyCode() == _KEY_END)
497 SysTryReturn(NID_APP, __pUiApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting UI App instance failed.");
499 if (__pUiApp->OnAppWillTerminate())
501 __pUiApp->Terminate();
511 _UiAppImpl::OnForeground(void)
513 result r = E_SUCCESS;
515 SysLog(NID_APP, "Invoking application callback.");
517 __appUiState = APP_UI_STATE_FOREGROUND;
518 __pUiApp->OnForeground();
520 SysLog(NID_APP, "Returned from application callback.");
522 r = _PowerManagerImpl::OnForeground();
524 SysTryLog(NID_APP, !IsFailed(r), "Failed to send foreground event to powermanager");
529 _UiAppImpl::OnBackground(void)
531 result r = E_SUCCESS;
533 SysLog(NID_APP, "Invoking application callback.");
535 __appUiState = APP_UI_STATE_BACKGROUND;
536 __pUiApp->OnBackground();
538 SysLog(NID_APP, "Returned from application callback.");
540 r = _PowerManagerImpl::OnBackground();
542 SysTryLog(NID_APP, !IsFailed(r), "Failed to send background event to powermanager");
547 _UiAppImpl::GetAppUiState(void) const
554 _UiAppImpl::GetInstance(void)
561 _UiAppImpl::GetUiAppInstance(void)