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));
113 // [INFO] to confirm that the window is not foreground
114 _EcoreEvas* pEvas = GetEcoreEvasMgr()->GetEcoreEvas();
117 int pid = pEvas->GetProcessId(pEvas->GetActiveWindow());
118 SysLog(NID_APP, "%d -> %d", pid, _AppInfo::GetProcessId());
119 if (pid != _AppInfo::GetProcessId())
125 _FrameImpl* pFrameImpl = _FrameImpl::GetInstance(*pFrame);
127 // [FIXME] Multi window handling
128 if (pFrameImpl != null)
130 pEvas->ActivateWindow(pFrameImpl->GetCore());
137 // make OnForeground event
141 if (pEvas && _AppInfo::IsSubMode())
143 _FrameImpl* pFrameImpl = _FrameImpl::GetInstance(*pFrame);
144 if (pFrameImpl != null)
146 unsigned int curHandle = pFrameImpl->GetNativeHandle();
148 bundle* pBundle = _AppArg::GetBundleFromSvc(service);
149 int ret = appsvc_request_transient_app(pBundle, curHandle, TransientResponseCb, NULL);
151 SysLog(NID_APP, "Transient sets for (0x%x) with result (%d).", curHandle, ret);
162 _UiAppImpl::OnTerminate(void)
164 SysLog(NID_APP, "Termination event 0x%x state", _AppInfo::GetAppState());
166 if (OnUiAppImplTerminating() != true)
168 SysLog(NID_APP, "[E_SYSTEM] The Termination of application failed.");
174 _UiAppImpl::OnResume(void)
176 SysLog(NID_APP, "System resume event on 0x%x state", _AppInfo::GetAppState());
178 if (_AppInfo::GetAppState() == RUNNING)
186 _UiAppImpl::OnPause(void)
188 SysLog(NID_APP, "System pause event on 0x%x state", _AppInfo::GetAppState());
190 if (_AppInfo::GetAppState() == RUNNING)
198 _UiAppImpl::OnDeviceOrientationChanged(app_device_orientation_e orientation)
200 SysLog(NID_APP, "System device orientation event.");
202 _ControlImplManager* pControlImplManager = _ControlImplManager::GetInstance();
203 if (pControlImplManager == null)
205 SysLog(NID_APP, "Device orientation event arrived too early.");
209 pControlImplManager->OnScreenRotated(orientation);
214 _UiAppImpl::OnWindowHandleRequest(void)
216 const _EcoreEvas* const pEvas = GetEcoreEvasMgr()->GetEcoreEvas();
217 return (pEvas) ? static_cast<long>(pEvas->GetXWindow()) : -1;
222 _UiAppImpl::AddFrame(const Frame& frame)
224 result r = E_SUCCESS;
225 Frame& tmpFrame = const_cast <Frame&>(frame);
227 SysTryReturnResult(NID_APP, __pFrameList != null, E_INVALID_STATE, "Getting FrameList failed.");
228 SysTryReturnResult(NID_APP, !__pFrameList->Contains(tmpFrame), E_OBJ_ALREADY_EXIST, "The frame is already registered.");
230 _WindowImpl* pFrameImpl = _WindowImpl::GetInstance(tmpFrame);
231 SysTryReturnResult(NID_APP, pFrameImpl != null, E_INVALID_ARG, "The frame is not constructed yet.");
233 __pFrameList->Add(tmpFrame);
234 r = pFrameImpl->Open(false); // Attach to the main tree without 'draw & show'.
237 SysLog(NID_UI, "Failed to attach frame.");
238 __pFrameList->Remove(tmpFrame);
246 _UiAppImpl::GetAppFrame(void)
248 SysTryReturn(NID_APP, __pFrameList != null, null, E_INVALID_STATE, "[E_INVALID_STATE] Getting FrameList failed.");
249 int frameCount = __pFrameList->GetCount();
253 SysLogException(NID_APP, E_OBJ_NOT_FOUND, "There is no frame !!! use AddFrame() before call GetAppFrame()");
254 SysAssertf(false, "There is no frame !!! use AddFrame() before call GetAppFrame()");
257 Frame* pFrame = dynamic_cast <Frame*>(__pFrameList->GetAt(frameCount - 1));
261 SysLogException(NID_APP, E_OBJ_NOT_FOUND, "There is no frame !!! use AddFrame() before call GetAppFrame()");
262 SysAssertf(false, "There is no frame !!! use AddFrame() before call GetAppFrame()");
265 if (__pAppFrame == null)
267 __pAppFrame = new (std::nothrow) _AppFrame(*pFrame);
268 SysTryReturn(NID_APP, __pAppFrame != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
269 __pAppFrame->Construct();
271 else if (__pAppFrame->GetFrame() != pFrame)
273 __pAppFrame->SetFrame(pFrame);
281 _UiAppImpl::RemoveFrame(const Frame& frame)
283 result r = E_SUCCESS;
284 SysTryReturnResult(NID_APP, __pFrameList != null, E_INVALID_STATE, "Getting FrameList failed.");
286 r = __pFrameList->Remove(frame);
297 _UiAppImpl::RemoveAllFrames(void)
299 result r = E_SUCCESS;
300 SysTryReturnResult(NID_APP, __pFrameList != null, E_INVALID_STATE, "Getting FrameList failed.");
302 if (__pFrameList->GetCount() > 0)
304 __pFrameList->RemoveAll(true);
315 _UiAppImpl::GetFrameList(void)
322 _UiAppImpl::GetFrame(const String& name)
324 Frame* pFrame = null;
325 SysTryReturn(NID_APP, __pFrameList != null, null, E_INVALID_STATE, "[E_INVALID_STATE] Getting FrameList failed.");
326 int frameCount = __pFrameList->GetCount();
328 for (int i = 0; i < frameCount; i++)
330 pFrame = dynamic_cast <Frame*>(__pFrameList->GetAt(i));
332 if (pFrame != null && pFrame->GetName() == name)
343 _UiAppImpl::GetFrameAt(int index)
345 Frame* pFrame = null;
346 SysTryReturn(NID_APP, __pFrameList != null, null, E_INVALID_STATE, "[E_INVALID_STATE] Getting FrameList failed.");
347 SysTryReturn(NID_APP, index >= 0, null, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The index is less than 0.");
349 int frameCount = __pFrameList->GetCount();
350 SysTryReturn(NID_APP, index < frameCount, null, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The index is greater than the number of frames.");
352 pFrame = dynamic_cast <Frame*>(__pFrameList->GetAt(index));
357 // For header inclusion dependency, elementary-1/elm_config.h is not included
358 extern "C" const char* elm_config_preferred_engine_set(const char*);
361 _UiAppImpl::OnAppInitializing(void)
363 // Do Ui related initializing for UiApp
364 result r = InitializeUiFramework();
367 SysLogException(NID_APP, E_SYSTEM, "Getting resolution information failure. Application may not be installed correctly.");
371 _KeyEventManager* pKeyManager = _KeyEventManager::GetInstance();
374 pKeyManager->AddKeyEventListener(*this);
377 // API versioning for initial frame creation
378 if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
379 { // if API version is less than 3.0, create initial frame
380 Frame* pDefaultFrame = new (std::nothrow) Frame();
381 SysTryReturn(NID_APP, pDefaultFrame != null, false, r = E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Frame creation failed.");
382 pDefaultFrame->Construct();
383 AddFrame(*pDefaultFrame);
384 SysLog(NID_APP, "Default frame is added for API version %d.", _AppInfo::GetApiVersion());
387 SysTryReturn(NID_APP, __pUiApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting UiApp instance failed.");
389 SysLog(NID_APP, "Entering user OnAppInitializing().");
390 const bool bReturn = __pUiApp->OnAppInitializing(*(AppRegistry::GetInstance()));
392 SysLog(NID_APP, "Back to the platform initializing routine.");
398 _UiAppImpl::OnAppInitialized(void)
400 SysTryReturn(NID_APP, __pUiApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting UiApp instance failed.");
402 if (__pUiApp->OnAppInitialized())
404 if (__pFrameList->GetCount() > 0)
410 SysLogException(NID_APP, E_OBJ_NOT_FOUND, "There is no frame !!! use AddFrame() before returning OnAppInitialized()");
411 SysAssertf(false, "There is no frame !!! use AddFrame() before returning OnAppInitialized()");
419 _UiAppImpl::OnUiAppImplTerminating(void)
425 if (_AppInfo::GetAppState() != TERMINATED)
427 result = __pUiApp->OnAppTerminating(*(AppRegistry::GetInstance()), false);
428 _AppInfo::SetAppState(TERMINATED);
431 // Do Ui related finalizing for UiApp
432 FinalizeUiFramework();
439 _UiAppImpl::OnKeyPressed(const _Control& source, const _KeyInfo& keyInfo)
446 _UiAppImpl::OnKeyReleased(const _Control& source, const _KeyInfo& keyInfo)
448 if (__appUiState == APP_UI_STATE_FOREGROUND)
450 if (keyInfo.GetKeyCode() == _KEY_END)
452 SysTryReturn(NID_APP, __pUiApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting UI App instance failed.");
454 if (__pUiApp->OnAppWillTerminate())
456 __pUiApp->Terminate();
466 _UiAppImpl::OnForeground(void)
468 result r = E_SUCCESS;
470 SysLog(NID_APP, "Invoking application callback.");
472 __appUiState = APP_UI_STATE_FOREGROUND;
473 __pUiApp->OnForeground();
475 SysLog(NID_APP, "Returned from application callback.");
477 r = _PowerManagerImpl::OnForeground();
479 SysTryLog(NID_APP, !IsFailed(r), "Failed to send foreground event to powermanager");
484 _UiAppImpl::OnBackground(void)
486 result r = E_SUCCESS;
488 SysLog(NID_APP, "Invoking application callback.");
490 __appUiState = APP_UI_STATE_BACKGROUND;
491 __pUiApp->OnBackground();
493 SysLog(NID_APP, "Returned from application callback.");
495 r = _PowerManagerImpl::OnBackground();
497 SysTryLog(NID_APP, !IsFailed(r), "Failed to send background event to powermanager");
502 _UiAppImpl::GetAppUiState(void) const
509 _UiAppImpl::GetInstance(void)
516 _UiAppImpl::GetUiAppInstance(void)