NABI issue 43925
[apps/osp/Camera.git] / src / CmCameraApp.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://floralicense.org/license/
9 //
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.
15 //
16
17 /**
18  * @file                CmCameraApp.cpp
19  * @brief               This is the implementation file for CameraApp class.
20  */
21
22 #include "CmCameraApp.h"
23 #include "CmCameraPresentationModel.h"
24 #include "CmCameraSettingsPresentationModel.h"
25 #include "CmCameraStarterThread.h"
26 #include "CmMainFrame.h"
27
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;
35
36 static const wchar_t* STRING_FRAME_NAME = L"CameraApp";
37
38 CameraApp::CameraApp(void)
39         : __requestId(null)
40         , __operationId(L"")
41         , __mimeType(L"")
42         , __uriData(L"")
43         , __pArguments(null)
44         , __appLaunchType(APP_LAUNCH_TYPE_UNKNOWN)
45         , __isUsedRunStartPreviewThread(false)
46 {
47         AppLogDebug("ENTER");
48         AppLogDebug("EXIT");
49 }
50
51 CameraApp::~CameraApp(void)
52 {
53         AppLogDebug("ENTER");
54         AppLogDebug("EXIT");
55 }
56
57 UiApp*
58 CameraApp::CreateInstance(void)
59 {
60         AppLogDebug("ENTER");
61         AppLogDebug("EXIT");
62         return new (std::nothrow) CameraApp();
63 }
64
65 String
66 CameraApp::GetAppControlOperationId(void) const
67 {
68         AppLogDebug("ENTER");
69         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
70         return __operationId;
71 }
72
73 String
74 CameraApp::GetUriData(void) const
75 {
76         AppLogDebug("ENTER");
77         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
78         return __uriData;
79 }
80
81 const String&
82 CameraApp::GetMimeType(void) const
83 {
84         AppLogDebug("ENTER");
85         AppLogDebug("EXIT");
86         return __mimeType;
87 }
88
89 const IMap*
90 CameraApp::GetAppControlArguments(void) const
91 {
92         AppLogDebug("ENTER");
93         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
94         return __pArguments;
95 }
96
97 AppLaunchType
98 CameraApp::GetAppControlType(void) const
99 {
100         AppLogDebug("ENTER");
101         AppLogDebug("EXIT");
102         return __appLaunchType;
103 }
104
105 RequestId
106 CameraApp::GetRequestId(void) const
107 {
108         AppLogDebug("ENTER");
109         AppLogDebug("EXIT");
110         return __requestId;
111 }
112
113 bool
114 CameraApp::GetUsedRunStartPreviewThread(void)
115 {
116         AppLogDebug("ENTER");
117         AppLogDebug("EXIT");
118         return __isUsedRunStartPreviewThread;
119 }
120
121 void
122 CameraApp::SetAppControlType(AppLaunchType type)
123 {
124         AppLogDebug("ENTER");
125         __appLaunchType = type;
126         AppLogDebug("EXIT");
127 }
128
129 void
130 CameraApp::SetUsedRunStartPreviewThread(bool isUsed)
131 {
132         AppLogDebug("ENTER");
133         __isUsedRunStartPreviewThread = isUsed;
134         AppLogDebug("EXIT");
135 }
136
137 bool
138 CameraApp::OnAppInitializing(AppRegistry& appRegistry)
139 {
140         AppLogDebug("ENTER");
141         result r = E_SUCCESS;
142         NotificationManager notiMgr;
143         MessageBox errorMsgBox;
144         int badgeNumber = 0, modResult=0;
145
146         CameraPresentationModel* pCameraPresentationModel = CameraPresentationModel::GetInstance();
147         TryCatch(pCameraPresentationModel != null, r = GetLastResult(), "pCameraPresentationModel is null");
148
149         AppControlProviderManager::GetInstance()->SetAppControlProviderEventListener(this);
150
151         SettingInfo::AddSettingEventListener(*this);
152
153         PowerManager::AddChargingEventListener(*this);
154
155         SetUsedRunStartPreviewThread(true);
156
157         AppLogDebug("EXIT");
158         return true;
159
160 CATCH:
161         notiMgr.Construct();
162         badgeNumber = notiMgr.GetBadgeNumber();
163         badgeNumber++;
164
165         notiMgr.Notify(L"Camera could not be launched due to unknown error.", 1);
166
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);
169
170         if ( GetAppControlType() == APP_CONTROL_TYPE_REQUESET)
171                 AppControlProviderManager::GetInstance()->SendAppControlResult(__requestId, APP_CTRL_RESULT_FAILED, null);
172
173         AppLogDebug("Exit with Error : %s", GetErrorMessage(GetLastResult()));
174         return false;
175 }
176
177 bool
178 CameraApp::OnAppInitialized(void)
179 {
180         AppLogDebug("ENTER");
181         result r = E_SUCCESS;
182         int mode = 0;
183         NotificationManager notiMgr;
184         String errorMessage;
185         CameraPresentationModel* pCameraPresentationModel = null;
186
187         MainFrame* pCameraAppFrame = new (std::nothrow) MainFrame();
188         r = pCameraAppFrame->Construct();
189
190         pCameraAppFrame->SetName(STRING_FRAME_NAME);
191         r = AddFrame(*pCameraAppFrame);
192         TryCatch(r == E_SUCCESS, , "UiApp::AddFrame() fail[%s]", GetErrorMessage(r));
193
194         pCameraPresentationModel = CameraPresentationModel::GetInstance();
195         TryCatch(pCameraPresentationModel != null, r = GetLastResult(), "pCameraPresentationModel is null");
196
197         r = pCameraPresentationModel->GetValue(CURRENT_MODE, mode);
198         AppLogDebug("Registry :GetvalueResult : %s", GetErrorMessage(r));
199
200         if ( r == E_STORAGE_FULL )
201         {
202                 errorMessage =L"Camera can't launch due to low memory, free some memory on default storage and try again!!!";
203         }
204         else if ( r== E_SECTION_NOT_FOUND || r == E_DATA_NOT_FOUND || r == E_PARSING_FAILED || r==E_KEY_NOT_FOUND )
205         {
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!!!";
209                  else
210                          errorMessage =L"Camera can't launch due to unknown exception, try again!!!";
211         }
212
213         r = pCameraAppFrame->Initialize(mode);
214         TryCatch(r == E_SUCCESS, , "MainFrame::Initialize() fail[%s]", GetErrorMessage(r));
215
216         AppLogDebug("EXIT");
217         return true;
218
219 CATCH:
220
221         if ( GetAppControlType() == APP_CONTROL_TYPE_REQUESET)
222                 AppControlProviderManager::GetInstance()->SendAppControlResult(__requestId, APP_CTRL_RESULT_FAILED, null);
223
224         AppLogDebug("EXIT - with error : %s", GetErrorMessage(r));
225         return false;
226 }
227
228 bool
229 CameraApp::OnAppTerminating(AppRegistry& appRegistry, bool forcedTermination)
230 {
231         AppLogDebug("ENTER");
232         result r = E_SUCCESS;
233
234         __appLaunchType = APP_LAUNCH_TYPE_UNKNOWN;
235
236         r = CameraPresentationModel::GetInstance()->ResetCameraSettings();
237         AppLogDebug("OnAppTerminating->ResetCameraSettings() Fail[%s]", GetErrorMessage(r));
238
239         CameraPresentationModel::DestroyPresentationModelInstance();
240         CameraSettingsPresentationModel::DestroyCameraSettingInstance();
241         CameraStarterThread::DestroyCameraStarterThreadInstance();
242
243         AppControlProviderManager::GetInstance()->SetAppControlProviderEventListener(null);
244         PowerManager::RemoveChargingEventListener(*this);
245
246         SetUsedRunStartPreviewThread(false);
247
248         if (__pArguments)
249         {
250                 delete __pArguments;
251                 __pArguments = null;
252         }
253
254         AppLogDebug("EXIT");
255         return true;
256 }
257
258 void
259 CameraApp::OnBatteryLevelChanged(BatteryLevel batteryLevel)
260 {
261         AppLogDebug("ENTER");
262         CameraPresentationModel* pCameraPresentationModel = CameraPresentationModel::GetInstance();
263         TryReturnVoid(pCameraPresentationModel != null, "pCameraPresentationModel is null");
264
265         if (pCameraPresentationModel->IsCameraInstance() == false)
266         {
267                 AppLogDebug("IsCameraInstance is null");
268         }
269         else
270         {
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");
274
275                 Form* pForm = pFrame->GetCurrentForm();
276                 TryReturnVoid(pForm != null, "pForm is null");
277
278                 if (batteryLevel == BATTERY_CRITICAL || batteryLevel == BATTERY_EMPTY || batteryLevel == BATTERY_LOW)
279                 {
280                         pForm->SendUserEvent(CAMERA_ACTION_EVENT_BATTERY_LEVEL_CHANGED, null);
281                 }
282                 else
283                 {
284                         if (pCameraPresentationModel->GetIntervalTimerRun() == false)
285                         {
286                                 pForm->SendUserEvent(CAMERA_ACTION_EVENT_BATTERY_LEVEL_CHANGED, null);
287                         }
288                 }
289         }
290         AppLogDebug("EXIT");
291 }
292
293 void
294 CameraApp::OnChargingStateChanged(bool charging)
295 {
296         AppLogDebug("ENTER");
297         BatteryLevel batteryLevel;
298
299         CameraPresentationModel* pCameraPresentationModel = CameraPresentationModel::GetInstance();
300         TryReturnVoid(pCameraPresentationModel != null, "pCameraPresentationModel is null");
301
302         batteryLevel = pCameraPresentationModel->GetBatteryLevel();
303         AppLogDebug("batteryLevel = %d", batteryLevel);
304
305         MainFrame* pFrame = static_cast<MainFrame*>(UiApp::GetInstance()->GetFrame(STRING_FRAME_NAME));
306         TryReturnVoid(pFrame != null, "pFrame is null");
307
308         Form* pForm = pFrame->GetCurrentForm();
309         TryReturnVoid(pForm != null, "pForm is null");
310
311         if (charging == false)
312         {
313                 AppLogDebug("charging..disconnect");
314                 if (batteryLevel == BATTERY_CRITICAL || batteryLevel == BATTERY_EMPTY || batteryLevel == BATTERY_LOW)
315                 {
316                         pForm->SendUserEvent(CAMERA_ACTION_EVENT_BATTERY_LEVEL_CHANGED, null);
317                 }
318                 else
319                 {
320                         if (pCameraPresentationModel->GetIntervalTimerRun() == false)
321                         {
322                                 pForm->SendUserEvent(CAMERA_ACTION_EVENT_BATTERY_LEVEL_CHANGED, null);
323                         }
324                 }
325         }
326         else
327         {
328                 AppLogDebug("charging..connect");
329                 if (batteryLevel == BATTERY_CRITICAL || batteryLevel == BATTERY_EMPTY || batteryLevel == BATTERY_LOW)
330                 {
331                         pForm->SendUserEvent(CAMERA_ACTION_EVENT_BATTERY_LEVEL_CHANGED, null);
332                 }
333                 else
334                 {
335                         if (pCameraPresentationModel->GetIntervalTimerRun() == false)
336                         {
337                                 pForm->SendUserEvent(CAMERA_ACTION_EVENT_BATTERY_LEVEL_CHARGING, null);
338                         }
339                 }
340         }
341         AppLogDebug("EXIT");
342 }
343
344 void 
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)
347 {
348         AppLogDebug("ENTER");
349         result r = E_SUCCESS;
350         __operationId = operationId;
351         AppLogDebug("operationId : %ls", operationId.GetPointer());
352
353         if (pMimeType != null)
354         {
355                 __mimeType = *pMimeType;
356                 AppLogDebug("pMimeType : %ls", __mimeType.GetPointer());
357         }
358
359         if (pUriData != null)
360         {
361                 __uriData = *pUriData;
362                 AppLogDebug("pUriData : %ls", __uriData.GetPointer());
363         }
364
365         if (pExtraData != null)
366         {
367                 HashMap* pArguments = new (std::nothrow) HashMap(SingleObjectDeleter);
368
369                 r = pArguments->Construct();
370                 AppLogDebug("HashMap::Construct() fail[%s]", GetErrorMessage(r));
371
372                 const String* pKey = null;
373                 const String* pValue = null;
374                 IList* pKeyList = pExtraData->GetKeysN();
375                 int loopCount = pKeyList->GetCount();
376
377                 AppLogDebug("Loopcount is %d",loopCount);
378
379                 for (int i = 0; i < loopCount; ++i)
380                 {
381                         pKey = static_cast<const String*>(pKeyList->GetAt(i));
382                         pValue = static_cast<const String*>(pExtraData->GetValue(*pKey));
383
384                         AppLogDebug("Key is %ls",pKey->GetPointer());
385                         AppLogDebug("pValue is %ls",pValue->GetPointer());
386
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));
389                 }
390                 __pArguments = pArguments;
391                 delete pKeyList;
392
393                 r = OnRequestAppControl();
394                 if (IsFailed(r))
395                 {
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));
399
400                         Terminate();
401                 }
402
403                 __requestId = reqId;
404         }
405         else 
406         {
407                 AppLogDebug("No switch parameter specified");
408                 if (operationId.CompareTo(APPCONTROL_OPERATION_ID_CREATECONTENT) == 0)
409                 {
410                         HashMap* pArguments = new (std::nothrow) HashMap(SingleObjectDeleter);
411
412                         pArguments->Construct();
413                         pArguments->Add(new (std::nothrow) String(ALLOW_SWITCH), new (std::nothrow) String(ALLOW_SWITCH_TRUE));
414                         __pArguments = pArguments;
415
416                         AppLogDebug("HashMap::Construct() result[%s]", GetErrorMessage(r));
417
418                         OnRequestAppControlTest();
419                 }
420                 else
421                 {
422                         __appLaunchType = APP_LAUNCH_TYPE_MAIN;
423                 }
424         }
425         AppLogDebug("EXIT");
426 }
427
428 void
429 CameraApp::OnSettingChanged(Tizen::Base::String& key)
430 {
431         AppLogDebug("ENTER");
432         if (key.CompareTo(PARAM_LANGUAGE) == 0 || key.CompareTo(PARAM_COUNTRY) == 0)
433         {
434                 result r = AppControlProviderManager::GetInstance()->SendAppControlResult(__requestId, APP_CTRL_RESULT_TERMINATED, null);
435                 TryReturnVoid(r == E_SUCCESS, "[%s] Unable to return result.", GetErrorMessage(r));
436
437                 Terminate();
438         }
439         AppLogDebug("EXIT");
440 }
441
442 void
443 CameraApp::SetFrameEnabled(bool enabled)
444 {
445         AppLogDebug("ENTER");
446         MainFrame* pMainFrame = dynamic_cast<MainFrame*>(GetFrameAt(0));
447         TryReturnVoid(pMainFrame != null, "pMainFrame is null");
448
449         pMainFrame->SetEnabled(enabled);
450         AppLogDebug("EXIT");
451 }
452
453 bool
454 CameraApp::IsFrameEnabled(void)
455 {
456         AppLogDebug("ENTER");
457         bool isEnabled = false;
458
459         MainFrame* pMainFrame = dynamic_cast<MainFrame*>(GetFrameAt(0));
460         TryCatch(pMainFrame != null, , "pMainFrame is null");
461
462         isEnabled = pMainFrame->IsEnabled();
463
464         AppLogDebug("EXIT");
465         return isEnabled;
466
467 CATCH:
468         AppLogDebug("EXIT - CATCH");
469         return false;
470 }
471
472 void
473 CameraApp::OnRequestAppControlTest(void)
474 {
475         AppLogDebug("ENTER");
476         result r = E_SUCCESS;
477
478         if (GetMimeType() == MIME_TYPE_IMAGE_JPG || GetMimeType() == MIME_TYPE_IMAGE_3GP || GetMimeType() == MIME_TYPE_IMAGE_JPEG)
479         {
480                 __appLaunchType = APP_LAUNCH_TYPE_REQUESET;
481
482                 r = CameraPresentationModel::GetInstance()->SetValue(CURRENT_MODE, CameraPresentationModel::CAMERA_MODE_SNAPSHOT);
483                 AppLogDebug("SetValue Reg Fail[%s]", GetErrorMessage(r));
484         }
485         else if (GetMimeType() == MIME_TYPE_VIDEO_JPG || GetMimeType() == MIME_TYPE_VIDEO_3GP || GetMimeType() == MIME_TYPE_VIDEO_3GPP
486                         || GetMimeType() == MIME_TYPE_VIDEO_MP4)
487         {
488                 __appLaunchType = APP_LAUNCH_TYPE_REQUESET;
489
490                 r = CameraPresentationModel::GetInstance()->SetValue(CURRENT_MODE, CameraPresentationModel::CAMERA_MODE_RECORD);
491                 AppLogDebug("SetValue Reg Fail[%s]", GetErrorMessage(r));
492         }
493         else
494         {
495                 __appLaunchType = APP_LAUNCH_TYPE_REQUESET;
496
497                 r = CameraPresentationModel::GetInstance()->SetValue(CURRENT_MODE, CameraPresentationModel::CAMERA_MODE_SNAPSHOT);
498                 AppLogDebug("SetValue Reg Fail[%s]", GetErrorMessage(r));
499         }
500         AppLogDebug("EXIT");
501 }
502
503 result
504 CameraApp::OnRequestAppControl(void)
505 {
506         AppLogDebug("ENTER");
507         result r = E_SUCCESS;
508
509         if (GetMimeType() == MIME_TYPE_IMAGE_JPG || GetMimeType() == MIME_TYPE_IMAGE_3GP || GetMimeType() == MIME_TYPE_IMAGE_JPEG)
510         {
511                 __appLaunchType = APP_LAUNCH_TYPE_REQUESET;
512
513                 r = CameraPresentationModel::GetInstance()->SetValue(CURRENT_MODE, CameraPresentationModel::CAMERA_MODE_SNAPSHOT);
514                 AppLogDebug("SetValue Reg Fail[%s]", GetErrorMessage(r));
515
516                 AppLogDebug("EXIT");
517                 return E_SUCCESS;
518         }
519         else if (GetMimeType() == MIME_TYPE_VIDEO_JPG || GetMimeType() == MIME_TYPE_VIDEO_3GP || GetMimeType() == MIME_TYPE_VIDEO_3GPP
520                         || GetMimeType() == MIME_TYPE_VIDEO_MP4)
521         {
522                 __appLaunchType = APP_LAUNCH_TYPE_REQUESET;
523
524                 r = CameraPresentationModel::GetInstance()->SetValue(CURRENT_MODE, CameraPresentationModel::CAMERA_MODE_RECORD);
525                 AppLogDebug("SetValue Reg Fail[%s]", GetErrorMessage(r));
526
527                 AppLogDebug("EXIT");
528                 return E_SUCCESS;
529         }
530         else
531         {
532                 __appLaunchType = APP_LAUNCH_TYPE_REQUESET;
533
534                 r = CameraPresentationModel::GetInstance()->SetValue(CURRENT_MODE, CameraPresentationModel::CAMERA_MODE_SNAPSHOT);
535                 AppLogDebug("SetValue Reg Fail[%s]", GetErrorMessage(r));
536
537                 AppLogDebug("EXIT");
538                 return E_SUCCESS;
539         }
540 }
541
542 void
543 CameraApp::OnLowMemory (void)
544 {
545         AppLogDebug("Enter");
546         AppLogDebug("Exit");
547 }
548
549 bool
550 CameraApp::GetAppControlSwitch(void)
551 {
552         AppLogDebug("Enter");
553         const String* pKeyValue;
554         bool switchEnabled = false;
555
556         pKeyValue = static_cast<const String*>(GetAppControlArguments()->GetValue(String(ALLOW_SWITCH)));
557
558         if (pKeyValue != null)
559         {
560                 if (pKeyValue->CompareTo(ALLOW_SWITCH_TRUE) == 0)
561                 {
562                         switchEnabled = true;
563                 }
564         }
565
566         return switchEnabled;
567         AppLogDebug("Exit");
568 }