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