2 // Copyright (c) 2012 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 CmCameraApp.cpp
19 * @brief This is the implementation file for CameraApp class.
22 #include "CmCameraApp.h"
23 #include "CmCameraPresentationModel.h"
24 #include "CmCameraSettingsPresentationModel.h"
25 #include "CmCameraStarterThread.h"
26 #include "CmMainFrame.h"
28 using namespace Tizen::App;
29 using namespace Tizen::Base;
30 using namespace Tizen::Base::Collection;
31 using namespace Tizen::Io;
32 using namespace Tizen::System;
33 using namespace Tizen::Ui;
34 using namespace Tizen::Ui::Controls;
36 static const wchar_t* STRING_FRAME_NAME = L"CameraApp";
37 static const wchar_t* STRING_HARD_KEY_BACK = L"http://tizen.org/feature/input.keys.back";
39 CameraApp::CameraApp(void)
45 , __appLaunchType(APP_LAUNCH_TYPE_UNKNOWN)
46 , __isUsedRunStartPreviewThread(false)
47 ,__isHwKeySupported(false)
53 CameraApp::~CameraApp(void)
60 CameraApp::CreateInstance(void)
64 return new (std::nothrow) CameraApp();
68 CameraApp::GetAppControlOperationId(void) const
71 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
76 CameraApp::GetUriData(void) const
79 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
84 CameraApp::GetMimeType(void) const
92 CameraApp::GetAppControlArguments(void) const
95 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
100 CameraApp::GetAppControlType(void) const
102 AppLogDebug("ENTER");
104 return __appLaunchType;
108 CameraApp::GetRequestId(void) const
110 AppLogDebug("ENTER");
116 CameraApp::GetUsedRunStartPreviewThread(void)
118 AppLogDebug("ENTER");
120 return __isUsedRunStartPreviewThread;
124 CameraApp::SetAppControlType(AppLaunchType type)
126 AppLogDebug("ENTER");
127 __appLaunchType = type;
132 CameraApp::SetUsedRunStartPreviewThread(bool isUsed)
134 AppLogDebug("ENTER");
135 __isUsedRunStartPreviewThread = isUsed;
140 CameraApp::OnAppInitializing(AppRegistry& appRegistry)
142 AppLogDebug("ENTER");
143 result r = E_SUCCESS;
144 NotificationManager notiMgr;
145 MessageBox errorMsgBox;
146 int badgeNumber = 0, modResult=0;
148 CameraPresentationModel* pCameraPresentationModel = CameraPresentationModel::GetInstance();
149 TryCatch(pCameraPresentationModel != null, r = GetLastResult(), "pCameraPresentationModel is null");
151 AppControlProviderManager::GetInstance()->SetAppControlProviderEventListener(this);
153 SettingInfo::AddSettingEventListener(*this);
155 PowerManager::AddChargingEventListener(*this);
157 SetUsedRunStartPreviewThread(true);
159 Tizen::System::SystemInfo::GetValue(STRING_HARD_KEY_BACK,__isHwKeySupported);
161 AppLogDebug("Hardware supported key value is %d",__isHwKeySupported);
167 badgeNumber = notiMgr.GetBadgeNumber();
170 notiMgr.Notify(L"Camera could not be launched due to unknown error.", 1);
172 errorMsgBox.Construct(L"Camera launch failed", L"Camera could not be launched due to unknown error.", MSGBOX_STYLE_NONE, 3000);
173 errorMsgBox.ShowAndWait(modResult);
175 if ( GetAppControlType() == APP_CONTROL_TYPE_REQUESET)
176 AppControlProviderManager::GetInstance()->SendAppControlResult(__requestId, APP_CTRL_RESULT_FAILED, null);
178 AppLogDebug("Exit with Error : %s", GetErrorMessage(GetLastResult()));
183 CameraApp::OnAppInitialized(void)
185 AppLogDebug("ENTER");
186 result r = E_SUCCESS;
188 NotificationManager notiMgr;
190 MessageBox errorMsgBox;
191 int badgeNumber = 0, modResult=0;
193 MainFrame* pCameraAppFrame = new (std::nothrow) MainFrame();
194 r = pCameraAppFrame->Construct();
196 CameraPresentationModel* pCameraPresentationModel = CameraPresentationModel::GetInstance();
197 TryCatch(pCameraPresentationModel != null, r = GetLastResult(), "pCameraPresentationModel is null");
199 pCameraAppFrame->SetName(STRING_FRAME_NAME);
200 r = AddFrame(*pCameraAppFrame);
201 TryCatch(r == E_SUCCESS, , "UiApp::AddFrame() fail[%s]", GetErrorMessage(r));
203 r = pCameraPresentationModel->GetValue(CURRENT_MODE, mode);
204 AppLogDebug("Registry :GetvalueResult : %s", GetErrorMessage(r));
206 if ( r == E_STORAGE_FULL )
208 errorMessage =L"Camera can't launch due to low memory, free some memory on default storage and try again!!!";
210 else if ( r== E_SECTION_NOT_FOUND || r == E_DATA_NOT_FOUND || r == E_PARSING_FAILED || r==E_KEY_NOT_FOUND )
212 r = pCameraPresentationModel->ResetCameraSettingsRegistry();
213 if( r == E_STORAGE_FULL )
214 errorMessage =L"Camera can't launch due to low memory, free some memory on default storage and try again!!!";
216 errorMessage =L"Camera can't launch due to unknown exception, try again!!!";
219 TryCatch(r == E_SUCCESS, , "Read register GetValue() fail[%s]", GetErrorMessage(r));
220 r = pCameraAppFrame->Initialize(mode);
221 TryCatch(r == E_SUCCESS, , "MainFrame::Initialize() fail[%s]", GetErrorMessage(r));
227 r = notiMgr.Construct();
228 badgeNumber = notiMgr.GetBadgeNumber();
231 notiMgr.Notify(errorMessage, 1);
233 errorMsgBox.Construct(L"Camera launch failed", errorMessage, MSGBOX_STYLE_NONE, 3000);
234 errorMsgBox.ShowAndWait(modResult);
236 if ( GetAppControlType() == APP_CONTROL_TYPE_REQUESET)
237 AppControlProviderManager::GetInstance()->SendAppControlResult(__requestId, APP_CTRL_RESULT_FAILED, null);
239 AppLogDebug("EXIT - with error : %s", GetErrorMessage(r));
244 CameraApp::OnAppTerminating(AppRegistry& appRegistry, bool forcedTermination)
246 AppLogDebug("ENTER");
247 result r = E_SUCCESS;
249 __appLaunchType = APP_LAUNCH_TYPE_UNKNOWN;
251 r = CameraPresentationModel::GetInstance()->ResetCameraSettings();
252 AppLogDebug("OnAppTerminating->ResetCameraSettings() Fail[%s]", GetErrorMessage(r));
254 CameraPresentationModel::DestroyPresentationModelInstance();
255 CameraSettingsPresentationModel::DestroyCameraSettingInstance();
256 CameraStarterThread::DestroyCameraStarterThreadInstance();
258 AppControlProviderManager::GetInstance()->SetAppControlProviderEventListener(null);
259 PowerManager::RemoveChargingEventListener(*this);
261 SetUsedRunStartPreviewThread(false);
274 CameraApp::OnBatteryLevelChanged(BatteryLevel batteryLevel)
276 AppLogDebug("ENTER");
277 CameraPresentationModel* pCameraPresentationModel = CameraPresentationModel::GetInstance();
278 TryReturnVoid(pCameraPresentationModel != null, "pCameraPresentationModel is null");
280 if (pCameraPresentationModel->IsCameraInstance() == false)
282 AppLogDebug("IsCameraInstance is null");
286 AppLogDebug("IsCameraInstance is not null");
287 MainFrame* pFrame = static_cast<MainFrame*>(UiApp::GetInstance()->GetFrame(STRING_FRAME_NAME));
288 TryReturnVoid(pFrame != null, "pFrame is null");
290 Form* pForm = pFrame->GetCurrentForm();
291 TryReturnVoid(pForm != null, "pForm is null");
293 if (batteryLevel == BATTERY_CRITICAL || batteryLevel == BATTERY_EMPTY || batteryLevel == BATTERY_LOW)
295 pForm->SendUserEvent(CAMERA_ACTION_EVENT_BATTERY_LEVEL_CHANGED, null);
299 if (pCameraPresentationModel->GetIntervalTimerRun() == false)
301 pForm->SendUserEvent(CAMERA_ACTION_EVENT_BATTERY_LEVEL_CHANGED, null);
309 CameraApp::OnChargingStateChanged(bool charging)
311 AppLogDebug("ENTER");
312 BatteryLevel batteryLevel;
314 CameraPresentationModel* pCameraPresentationModel = CameraPresentationModel::GetInstance();
315 TryReturnVoid(pCameraPresentationModel != null, "pCameraPresentationModel is null");
317 batteryLevel = pCameraPresentationModel->GetBatteryLevel();
318 AppLogDebug("batteryLevel = %d", batteryLevel);
320 MainFrame* pFrame = static_cast<MainFrame*>(UiApp::GetInstance()->GetFrame(STRING_FRAME_NAME));
321 TryReturnVoid(pFrame != null, "pFrame is null");
323 Form* pForm = pFrame->GetCurrentForm();
324 TryReturnVoid(pForm != null, "pForm is null");
326 if (charging == false)
328 AppLogDebug("charging..disconnect");
329 if (batteryLevel == BATTERY_CRITICAL || batteryLevel == BATTERY_EMPTY || batteryLevel == BATTERY_LOW)
331 pForm->SendUserEvent(CAMERA_ACTION_EVENT_BATTERY_LEVEL_CHANGED, null);
335 if (pCameraPresentationModel->GetIntervalTimerRun() == false)
337 pForm->SendUserEvent(CAMERA_ACTION_EVENT_BATTERY_LEVEL_CHANGED, null);
343 AppLogDebug("charging..connect");
344 if (batteryLevel == BATTERY_CRITICAL || batteryLevel == BATTERY_EMPTY || batteryLevel == BATTERY_LOW)
346 pForm->SendUserEvent(CAMERA_ACTION_EVENT_BATTERY_LEVEL_CHANGED, null);
350 if (pCameraPresentationModel->GetIntervalTimerRun() == false)
352 pForm->SendUserEvent(CAMERA_ACTION_EVENT_BATTERY_LEVEL_CHARGING, null);
360 CameraApp::OnAppControlRequestReceived(RequestId reqId, const Tizen::Base::String& operationId, const Tizen::Base::String* pUriData,
361 const Tizen::Base::String* pMimeType, const Tizen::Base::Collection::IMap* pExtraData)
363 AppLogDebug("ENTER");
364 result r = E_SUCCESS;
365 __operationId = operationId;
366 AppLogDebug("operationId : %ls", operationId.GetPointer());
368 if (pMimeType != null)
370 __mimeType = *pMimeType;
371 AppLogDebug("pMimeType : %ls", __mimeType.GetPointer());
374 if (pUriData != null)
376 __uriData = *pUriData;
377 AppLogDebug("pUriData : %ls", __uriData.GetPointer());
380 if (pExtraData != null)
382 HashMap* pArguments = new (std::nothrow) HashMap(SingleObjectDeleter);
384 r = pArguments->Construct();
385 AppLogDebug("HashMap::Construct() fail[%s]", GetErrorMessage(r));
387 const String* pKey = null;
388 const String* pValue = null;
389 IList* pKeyList = pExtraData->GetKeysN();
390 int loopCount = pKeyList->GetCount();
392 AppLogDebug("Loopcount is %d",loopCount);
394 for (int i = 0; i < loopCount; ++i)
396 pKey = static_cast<const String*>(pKeyList->GetAt(i));
397 pValue = static_cast<const String*>(pExtraData->GetValue(*pKey));
399 AppLogDebug("Key is %ls",pKey->GetPointer());
400 AppLogDebug("pValue is %ls",pValue->GetPointer());
402 AppLogDebug("(%ls:%ls)", pKey == null ? L"(null)" : pKey->GetPointer(), pValue == null ? L"(null)" : pValue->GetPointer());
403 pArguments->Add(new (std::nothrow) String(*pKey), new (std::nothrow) String(*pValue));
405 __pArguments = pArguments;
408 r = OnRequestAppControl();
411 AppLogDebug("[%s] initializing app control failed", GetErrorMessage(r));
412 result r = AppControlProviderManager::GetInstance()->SendAppControlResult(reqId, APP_CTRL_RESULT_FAILED, null);
413 AppLogDebugIf(r != E_SUCCESS, "[%s] Unable to return result.", GetErrorMessage(r));
422 AppLogDebug("No switch parameter specified");
423 if (operationId.CompareTo(APPCONTROL_OPERATION_ID_CREATECONTENT) == 0)
425 HashMap* pArguments = new (std::nothrow) HashMap(SingleObjectDeleter);
427 pArguments->Construct();
428 pArguments->Add(new (std::nothrow) String(ALLOW_SWITCH), new (std::nothrow) String(ALLOW_SWITCH_TRUE));
429 __pArguments = pArguments;
431 AppLogDebug("HashMap::Construct() result[%s]", GetErrorMessage(r));
433 OnRequestAppControlTest();
437 __appLaunchType = APP_LAUNCH_TYPE_MAIN;
444 CameraApp::OnSettingChanged(Tizen::Base::String& key)
446 AppLogDebug("ENTER");
447 if (key.CompareTo(PARAM_LANGUAGE) == 0 || key.CompareTo(PARAM_COUNTRY) == 0)
449 result r = AppControlProviderManager::GetInstance()->SendAppControlResult(__requestId, APP_CTRL_RESULT_TERMINATED, null);
450 TryReturnVoid(r == E_SUCCESS, "[%s] Unable to return result.", GetErrorMessage(r));
458 CameraApp::SetFrameEnabled(bool enabled)
460 AppLogDebug("ENTER");
461 MainFrame* pMainFrame = dynamic_cast<MainFrame*>(GetFrameAt(0));
462 TryReturnVoid(pMainFrame != null, "pMainFrame is null");
464 pMainFrame->SetEnabled(enabled);
469 CameraApp::IsFrameEnabled(void)
471 AppLogDebug("ENTER");
472 bool isEnabled = false;
474 MainFrame* pMainFrame = dynamic_cast<MainFrame*>(GetFrameAt(0));
475 TryCatch(pMainFrame != null, , "pMainFrame is null");
477 isEnabled = pMainFrame->IsEnabled();
483 AppLogDebug("EXIT - CATCH");
488 CameraApp::OnRequestAppControlTest(void)
490 AppLogDebug("ENTER");
491 result r = E_SUCCESS;
493 if (GetMimeType() == MIME_TYPE_IMAGE_JPG || GetMimeType() == MIME_TYPE_IMAGE_3GP)
495 __appLaunchType = APP_LAUNCH_TYPE_REQUESET;
497 r = CameraPresentationModel::GetInstance()->SetValue(CURRENT_MODE, CameraPresentationModel::CAMERA_MODE_SNAPSHOT);
498 AppLogDebug("SetValue Reg Fail[%s]", GetErrorMessage(r));
500 else if (GetMimeType() == MIME_TYPE_VIDEO_JPG || GetMimeType() == MIME_TYPE_VIDEO_3GP || GetMimeType() == MIME_TYPE_VIDEO_3GPP)
502 __appLaunchType = APP_LAUNCH_TYPE_REQUESET;
504 r = CameraPresentationModel::GetInstance()->SetValue(CURRENT_MODE, CameraPresentationModel::CAMERA_MODE_RECORD);
505 AppLogDebug("SetValue Reg Fail[%s]", GetErrorMessage(r));
509 __appLaunchType = APP_LAUNCH_TYPE_REQUESET;
511 r = CameraPresentationModel::GetInstance()->SetValue(CURRENT_MODE, CameraPresentationModel::CAMERA_MODE_SNAPSHOT);
512 AppLogDebug("SetValue Reg Fail[%s]", GetErrorMessage(r));
518 CameraApp::OnRequestAppControl(void)
520 AppLogDebug("ENTER");
521 result r = E_SUCCESS;
523 if (GetMimeType() == MIME_TYPE_IMAGE_JPG || GetMimeType() == MIME_TYPE_IMAGE_3GP)
525 __appLaunchType = APP_LAUNCH_TYPE_REQUESET;
527 r = CameraPresentationModel::GetInstance()->SetValue(CURRENT_MODE, CameraPresentationModel::CAMERA_MODE_SNAPSHOT);
528 AppLogDebug("SetValue Reg Fail[%s]", GetErrorMessage(r));
533 else if (GetMimeType() == MIME_TYPE_VIDEO_JPG || GetMimeType() == MIME_TYPE_VIDEO_3GP || GetMimeType() == MIME_TYPE_VIDEO_3GPP)
535 __appLaunchType = APP_LAUNCH_TYPE_REQUESET;
537 r = CameraPresentationModel::GetInstance()->SetValue(CURRENT_MODE, CameraPresentationModel::CAMERA_MODE_RECORD);
538 AppLogDebug("SetValue Reg Fail[%s]", GetErrorMessage(r));
545 __appLaunchType = APP_LAUNCH_TYPE_REQUESET;
547 r = CameraPresentationModel::GetInstance()->SetValue(CURRENT_MODE, CameraPresentationModel::CAMERA_MODE_SNAPSHOT);
548 AppLogDebug("SetValue Reg Fail[%s]", GetErrorMessage(r));
556 CameraApp::OnLowMemory (void)
558 AppLogDebug("Enter");
563 CameraApp::GetAppControlSwitch(void)
565 AppLogDebug("Enter");
566 const String* pKeyValue;
567 bool switchEnabled = false;
569 pKeyValue = static_cast<const String*>(GetAppControlArguments()->GetValue(String(ALLOW_SWITCH)));
571 if (pKeyValue != null)
573 if (pKeyValue->CompareTo(ALLOW_SWITCH_TRUE) == 0)
575 switchEnabled = true;
579 return switchEnabled;
584 CameraApp::CheckHwKeySupport(void)
586 return __isHwKeySupported;