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";
38 CameraApp::CameraApp(void)
44 , __appLaunchType(APP_LAUNCH_TYPE_UNKNOWN)
45 , __isUsedRunStartPreviewThread(false)
51 CameraApp::~CameraApp(void)
58 CameraApp::CreateInstance(void)
62 return new (std::nothrow) CameraApp();
66 CameraApp::GetAppControlOperationId(void) const
69 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
74 CameraApp::GetUriData(void) const
77 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
82 CameraApp::GetMimeType(void) const
90 CameraApp::GetAppControlArguments(void) const
93 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
98 CameraApp::GetAppControlType(void) const
100 AppLogDebug("ENTER");
102 return __appLaunchType;
106 CameraApp::GetRequestId(void) const
108 AppLogDebug("ENTER");
114 CameraApp::GetUsedRunStartPreviewThread(void)
116 AppLogDebug("ENTER");
118 return __isUsedRunStartPreviewThread;
122 CameraApp::SetAppControlType(AppLaunchType type)
124 AppLogDebug("ENTER");
125 __appLaunchType = type;
130 CameraApp::SetUsedRunStartPreviewThread(bool isUsed)
132 AppLogDebug("ENTER");
133 __isUsedRunStartPreviewThread = isUsed;
138 CameraApp::OnAppInitializing(AppRegistry& appRegistry)
140 AppLogDebug("ENTER");
141 result r = E_SUCCESS;
142 NotificationManager notiMgr;
143 MessageBox errorMsgBox;
144 int badgeNumber = 0, modResult=0;
146 CameraPresentationModel* pCameraPresentationModel = CameraPresentationModel::GetInstance();
147 TryCatch(pCameraPresentationModel != null, r = GetLastResult(), "pCameraPresentationModel is null");
149 AppControlProviderManager::GetInstance()->SetAppControlProviderEventListener(this);
151 SettingInfo::AddSettingEventListener(*this);
153 PowerManager::AddChargingEventListener(*this);
155 SetUsedRunStartPreviewThread(true);
162 badgeNumber = notiMgr.GetBadgeNumber();
165 notiMgr.Notify(L"Camera could not be launched due to unknown error.", 1);
167 errorMsgBox.Construct(L"Camera launch failed", L"Camera could not be launched due to unknown error.", MSGBOX_STYLE_NONE, 3000);
168 errorMsgBox.ShowAndWait(modResult);
170 if ( GetAppControlType() == APP_CONTROL_TYPE_REQUESET)
171 AppControlProviderManager::GetInstance()->SendAppControlResult(__requestId, APP_CTRL_RESULT_FAILED, null);
173 AppLogDebug("Exit with Error : %s", GetErrorMessage(GetLastResult()));
178 CameraApp::OnAppInitialized(void)
180 AppLogDebug("ENTER");
181 result r = E_SUCCESS;
183 NotificationManager notiMgr;
185 CameraPresentationModel* pCameraPresentationModel = null;
187 MainFrame* pCameraAppFrame = new (std::nothrow) MainFrame();
188 r = pCameraAppFrame->Construct();
190 pCameraAppFrame->SetName(STRING_FRAME_NAME);
191 r = AddFrame(*pCameraAppFrame);
192 TryCatch(r == E_SUCCESS, , "UiApp::AddFrame() fail[%s]", GetErrorMessage(r));
194 pCameraPresentationModel = CameraPresentationModel::GetInstance();
195 TryCatch(pCameraPresentationModel != null, r = GetLastResult(), "pCameraPresentationModel is null");
197 r = pCameraPresentationModel->GetValue(CURRENT_MODE, mode);
198 AppLogDebug("Registry :GetvalueResult : %s", GetErrorMessage(r));
200 if ( r == E_STORAGE_FULL )
202 errorMessage =L"Camera can't launch due to low memory, free some memory on default storage and try again!!!";
204 else if ( r== E_SECTION_NOT_FOUND || r == E_DATA_NOT_FOUND || r == E_PARSING_FAILED || r==E_KEY_NOT_FOUND )
206 r = pCameraPresentationModel->ResetCameraSettingsRegistry();
207 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 errorMessage =L"Camera can't launch due to unknown exception, try again!!!";
213 r = pCameraAppFrame->Initialize(mode);
214 TryCatch(r == E_SUCCESS, , "MainFrame::Initialize() fail[%s]", GetErrorMessage(r));
221 if ( GetAppControlType() == APP_CONTROL_TYPE_REQUESET)
222 AppControlProviderManager::GetInstance()->SendAppControlResult(__requestId, APP_CTRL_RESULT_FAILED, null);
224 AppLogDebug("EXIT - with error : %s", GetErrorMessage(r));
229 CameraApp::OnAppTerminating(AppRegistry& appRegistry, bool forcedTermination)
231 AppLogDebug("ENTER");
232 result r = E_SUCCESS;
234 __appLaunchType = APP_LAUNCH_TYPE_UNKNOWN;
236 r = CameraPresentationModel::GetInstance()->ResetCameraSettings();
237 AppLogDebug("OnAppTerminating->ResetCameraSettings() Fail[%s]", GetErrorMessage(r));
239 CameraPresentationModel::DestroyPresentationModelInstance();
240 CameraSettingsPresentationModel::DestroyCameraSettingInstance();
241 CameraStarterThread::DestroyCameraStarterThreadInstance();
243 AppControlProviderManager::GetInstance()->SetAppControlProviderEventListener(null);
244 PowerManager::RemoveChargingEventListener(*this);
246 SetUsedRunStartPreviewThread(false);
259 CameraApp::OnBatteryLevelChanged(BatteryLevel batteryLevel)
261 AppLogDebug("ENTER");
262 CameraPresentationModel* pCameraPresentationModel = CameraPresentationModel::GetInstance();
263 TryReturnVoid(pCameraPresentationModel != null, "pCameraPresentationModel is null");
265 if (pCameraPresentationModel->IsCameraInstance() == false)
267 AppLogDebug("IsCameraInstance is null");
271 AppLogDebug("IsCameraInstance is not null");
272 MainFrame* pFrame = static_cast<MainFrame*>(UiApp::GetInstance()->GetFrame(STRING_FRAME_NAME));
273 TryReturnVoid(pFrame != null, "pFrame is null");
275 Form* pForm = pFrame->GetCurrentForm();
276 TryReturnVoid(pForm != null, "pForm is null");
278 if (batteryLevel == BATTERY_CRITICAL || batteryLevel == BATTERY_EMPTY || batteryLevel == BATTERY_LOW)
280 pForm->SendUserEvent(CAMERA_ACTION_EVENT_BATTERY_LEVEL_CHANGED, null);
284 if (pCameraPresentationModel->GetIntervalTimerRun() == false)
286 pForm->SendUserEvent(CAMERA_ACTION_EVENT_BATTERY_LEVEL_CHANGED, null);
294 CameraApp::OnChargingStateChanged(bool charging)
296 AppLogDebug("ENTER");
297 BatteryLevel batteryLevel;
299 CameraPresentationModel* pCameraPresentationModel = CameraPresentationModel::GetInstance();
300 TryReturnVoid(pCameraPresentationModel != null, "pCameraPresentationModel is null");
302 batteryLevel = pCameraPresentationModel->GetBatteryLevel();
303 AppLogDebug("batteryLevel = %d", batteryLevel);
305 MainFrame* pFrame = static_cast<MainFrame*>(UiApp::GetInstance()->GetFrame(STRING_FRAME_NAME));
306 TryReturnVoid(pFrame != null, "pFrame is null");
308 Form* pForm = pFrame->GetCurrentForm();
309 TryReturnVoid(pForm != null, "pForm is null");
311 if (charging == false)
313 AppLogDebug("charging..disconnect");
314 if (batteryLevel == BATTERY_CRITICAL || batteryLevel == BATTERY_EMPTY || batteryLevel == BATTERY_LOW)
316 pForm->SendUserEvent(CAMERA_ACTION_EVENT_BATTERY_LEVEL_CHANGED, null);
320 if (pCameraPresentationModel->GetIntervalTimerRun() == false)
322 pForm->SendUserEvent(CAMERA_ACTION_EVENT_BATTERY_LEVEL_CHANGED, null);
328 AppLogDebug("charging..connect");
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_CHARGING, null);
345 CameraApp::OnAppControlRequestReceived(RequestId reqId, const Tizen::Base::String& operationId, const Tizen::Base::String* pUriData,
346 const Tizen::Base::String* pMimeType, const Tizen::Base::Collection::IMap* pExtraData)
348 AppLogDebug("ENTER");
349 result r = E_SUCCESS;
350 __operationId = operationId;
351 AppLogDebug("operationId : %ls", operationId.GetPointer());
353 if (pMimeType != null)
355 __mimeType = *pMimeType;
356 AppLogDebug("pMimeType : %ls", __mimeType.GetPointer());
359 if (pUriData != null)
361 __uriData = *pUriData;
362 AppLogDebug("pUriData : %ls", __uriData.GetPointer());
365 if (pExtraData != null)
367 HashMap* pArguments = new (std::nothrow) HashMap(SingleObjectDeleter);
369 r = pArguments->Construct();
370 AppLogDebug("HashMap::Construct() fail[%s]", GetErrorMessage(r));
372 const String* pKey = null;
373 const String* pValue = null;
374 IList* pKeyList = pExtraData->GetKeysN();
375 int loopCount = pKeyList->GetCount();
377 AppLogDebug("Loopcount is %d",loopCount);
379 for (int i = 0; i < loopCount; ++i)
381 pKey = static_cast<const String*>(pKeyList->GetAt(i));
382 pValue = static_cast<const String*>(pExtraData->GetValue(*pKey));
384 AppLogDebug("Key is %ls",pKey->GetPointer());
385 AppLogDebug("pValue is %ls",pValue->GetPointer());
387 AppLogDebug("(%ls:%ls)", pKey == null ? L"(null)" : pKey->GetPointer(), pValue == null ? L"(null)" : pValue->GetPointer());
388 pArguments->Add(new (std::nothrow) String(*pKey), new (std::nothrow) String(*pValue));
390 __pArguments = pArguments;
393 r = OnRequestAppControl();
396 AppLogDebug("[%s] initializing app control failed", GetErrorMessage(r));
397 result r = AppControlProviderManager::GetInstance()->SendAppControlResult(reqId, APP_CTRL_RESULT_FAILED, null);
398 AppLogDebugIf(r != E_SUCCESS, "[%s] Unable to return result.", GetErrorMessage(r));
407 AppLogDebug("No switch parameter specified");
408 if (operationId.CompareTo(APPCONTROL_OPERATION_ID_CREATECONTENT) == 0)
410 HashMap* pArguments = new (std::nothrow) HashMap(SingleObjectDeleter);
412 pArguments->Construct();
413 pArguments->Add(new (std::nothrow) String(ALLOW_SWITCH), new (std::nothrow) String(ALLOW_SWITCH_TRUE));
414 __pArguments = pArguments;
416 AppLogDebug("HashMap::Construct() result[%s]", GetErrorMessage(r));
418 OnRequestAppControlTest();
422 __appLaunchType = APP_LAUNCH_TYPE_MAIN;
429 CameraApp::OnSettingChanged(Tizen::Base::String& key)
431 AppLogDebug("ENTER");
432 if (key.CompareTo(PARAM_LANGUAGE) == 0 || key.CompareTo(PARAM_COUNTRY) == 0)
434 result r = AppControlProviderManager::GetInstance()->SendAppControlResult(__requestId, APP_CTRL_RESULT_TERMINATED, null);
435 TryReturnVoid(r == E_SUCCESS, "[%s] Unable to return result.", GetErrorMessage(r));
443 CameraApp::SetFrameEnabled(bool enabled)
445 AppLogDebug("ENTER");
446 MainFrame* pMainFrame = dynamic_cast<MainFrame*>(GetFrameAt(0));
447 TryReturnVoid(pMainFrame != null, "pMainFrame is null");
449 pMainFrame->SetEnabled(enabled);
454 CameraApp::IsFrameEnabled(void)
456 AppLogDebug("ENTER");
457 bool isEnabled = false;
459 MainFrame* pMainFrame = dynamic_cast<MainFrame*>(GetFrameAt(0));
460 TryCatch(pMainFrame != null, , "pMainFrame is null");
462 isEnabled = pMainFrame->IsEnabled();
468 AppLogDebug("EXIT - CATCH");
473 CameraApp::OnRequestAppControlTest(void)
475 AppLogDebug("ENTER");
476 result r = E_SUCCESS;
478 if (GetMimeType() == MIME_TYPE_IMAGE_JPG || GetMimeType() == MIME_TYPE_IMAGE_3GP || GetMimeType() == MIME_TYPE_IMAGE_JPEG)
480 __appLaunchType = APP_LAUNCH_TYPE_REQUESET;
482 r = CameraPresentationModel::GetInstance()->SetValue(CURRENT_MODE, CameraPresentationModel::CAMERA_MODE_SNAPSHOT);
483 AppLogDebug("SetValue Reg Fail[%s]", GetErrorMessage(r));
485 else if (GetMimeType() == MIME_TYPE_VIDEO_JPG || GetMimeType() == MIME_TYPE_VIDEO_3GP || GetMimeType() == MIME_TYPE_VIDEO_3GPP
486 || GetMimeType() == MIME_TYPE_VIDEO_MP4)
488 __appLaunchType = APP_LAUNCH_TYPE_REQUESET;
490 r = CameraPresentationModel::GetInstance()->SetValue(CURRENT_MODE, CameraPresentationModel::CAMERA_MODE_RECORD);
491 AppLogDebug("SetValue Reg Fail[%s]", GetErrorMessage(r));
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));
504 CameraApp::OnRequestAppControl(void)
506 AppLogDebug("ENTER");
507 result r = E_SUCCESS;
509 if (GetMimeType() == MIME_TYPE_IMAGE_JPG || GetMimeType() == MIME_TYPE_IMAGE_3GP || GetMimeType() == MIME_TYPE_IMAGE_JPEG)
511 __appLaunchType = APP_LAUNCH_TYPE_REQUESET;
513 r = CameraPresentationModel::GetInstance()->SetValue(CURRENT_MODE, CameraPresentationModel::CAMERA_MODE_SNAPSHOT);
514 AppLogDebug("SetValue Reg Fail[%s]", GetErrorMessage(r));
519 else if (GetMimeType() == MIME_TYPE_VIDEO_JPG || GetMimeType() == MIME_TYPE_VIDEO_3GP || GetMimeType() == MIME_TYPE_VIDEO_3GPP
520 || GetMimeType() == MIME_TYPE_VIDEO_MP4)
522 __appLaunchType = APP_LAUNCH_TYPE_REQUESET;
524 r = CameraPresentationModel::GetInstance()->SetValue(CURRENT_MODE, CameraPresentationModel::CAMERA_MODE_RECORD);
525 AppLogDebug("SetValue Reg Fail[%s]", GetErrorMessage(r));
532 __appLaunchType = APP_LAUNCH_TYPE_REQUESET;
534 r = CameraPresentationModel::GetInstance()->SetValue(CURRENT_MODE, CameraPresentationModel::CAMERA_MODE_SNAPSHOT);
535 AppLogDebug("SetValue Reg Fail[%s]", GetErrorMessage(r));
543 CameraApp::OnLowMemory (void)
545 AppLogDebug("Enter");
550 CameraApp::GetAppControlSwitch(void)
552 AppLogDebug("Enter");
553 const String* pKeyValue;
554 bool switchEnabled = false;
556 pKeyValue = static_cast<const String*>(GetAppControlArguments()->GetValue(String(ALLOW_SWITCH)));
558 if (pKeyValue != null)
560 if (pKeyValue->CompareTo(ALLOW_SWITCH_TRUE) == 0)
562 switchEnabled = true;
566 return switchEnabled;