d11af9290fc9e9cfafc0ae6a865d75a59de8dbee
[apps/native/sample/QrCodeRecognizer.git] / project / src / QrCodeRecognizerSample.cpp
1 //
2 // Tizen C++ SDK
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.1 (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
8 //
9 //     http://floralicense.org/license
10 //
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.
16 //
17
18 #include "QrCodeRecognizerSample.h"
19 #include "CameraTools.h"
20 #include "GLtools.h"
21
22 using namespace Tizen::App;
23 using namespace Tizen::Base;
24 using namespace Tizen::System;
25 using namespace Tizen::Ui;
26 using namespace Tizen::Ui::Controls;
27
28 const char* pTagApp = "QrCodeRecognizer";
29
30 QrCodeRecognizer::QrCodeRecognizer()
31     : __pTracker(0)
32     , __pForm(0)
33 {
34 }
35
36 QrCodeRecognizer::~QrCodeRecognizer()
37 {
38         DeviceManager::RemoveDeviceEventListener(Charger,*this);
39 }
40
41
42 UiApp*
43 QrCodeRecognizer::CreateInstance(void)
44 {
45     // Create the instance through the constructor.
46     return new QrCodeRecognizer();
47 }
48
49 bool
50 QrCodeRecognizer::OnAppInitializing(AppRegistry& appRegistry)
51 {
52     PowerManager::KeepScreenOnState(true, false);
53     return true;
54 }
55
56 bool
57 QrCodeRecognizer::OnAppInitialized(void)
58 {
59     Frame* pAppFrame = new Frame();
60     pAppFrame->Construct();
61     AddFrame(*pAppFrame);
62
63     __pForm = new TouchForm();
64     __pForm->Initialize(this, this);
65     pAppFrame->AddControl(__pForm);
66
67     // dealing with low battery
68     DeviceManager::AddDeviceEventListener(Charger,*this);
69     bool isCharging = true;
70         Tizen::System::RuntimeInfo::GetValue(String(L"IsCharging"),isCharging);
71         if (isCharging == false)
72         {
73                 BatteryLevel level = BATTERY_FULL;
74                 Tizen::System::Battery::GetCurrentLevel(level);
75                 if ((level == BATTERY_EMPTY) || (level == BATTERY_CRITICAL) || (level == BATTERY_LOW))
76                 {
77                         MessageBox msgBox;
78                         int result = 0;
79
80                         msgBox.Construct("Warning", "Low battery. Terminating App.", MSGBOX_STYLE_NONE, 3000);
81                         msgBox.ShowAndWait(result);
82                         Tizen::App::Application::GetInstance()->Terminate();
83                         return false;
84                 }
85         }
86
87     if (!GLtools::initGL(__pForm))
88     {
89         MessageBox msgBox;
90                 int result = 0;
91
92                 msgBox.Construct("Warning", "Cannot init OpenGL. Terminating App.", MSGBOX_STYLE_NONE, 3000);
93                 msgBox.ShowAndWait(result);
94                 Tizen::App::Application::GetInstance()->Terminate();
95                 return false;
96     }
97
98     __pTracker = new Tracker();
99     __pForm->SendUserEvent(TouchForm::REQUEST_ACTIVATE, null);
100
101     if (!CameraTools::InitCamera(*__pTracker, Tracker::DESIRED_CAMERA_WIDTH, Tracker::DESIRED_CAMERA_HEIGHT))
102     {
103         MessageBox msgBox;
104                 int result = 0;
105
106                 msgBox.Construct("Warning","Cannot init camera. Terminating App.", MSGBOX_STYLE_NONE, 3000);
107                 msgBox.ShowAndWait(result);
108                 Tizen::App::Application::GetInstance()->Terminate();
109                 return false;
110     }
111
112         __pTracker->Init(__pForm);
113
114     return true;
115 }
116
117 bool
118 QrCodeRecognizer::OnAppWillTerminate(void)
119 {
120     return true;
121 }
122
123 bool
124 QrCodeRecognizer::OnAppTerminating(AppRegistry& appRegistry, bool forcedTermination)
125 {
126     if (__pTracker)
127     {
128         __pTracker->DoRendering(false);
129         delete __pTracker;
130         __pTracker = null;
131     }
132
133     GLtools::destroyGL();
134     CameraTools::StopCamera();
135
136     return true;
137 }
138
139 void
140 QrCodeRecognizer::OnForeground(void)
141 {
142         AppSecureLogTag(pTagApp, "OnForeground");
143     PowerManager::KeepScreenOnState(true, false);
144     if (!CameraTools::StartCamera())
145     {
146         MessageBox msgBox;
147                 int result = 0;
148
149                 msgBox.Construct("Warning", "Cannot restart camera. Terminating App.", MSGBOX_STYLE_NONE, 3000);
150                 msgBox.ShowAndWait(result);
151                 Tizen::App::Application::GetInstance()->Terminate();
152     }
153 }
154
155 // Called when application is moved to background
156 void
157 QrCodeRecognizer::OnBackground(void)
158 {
159     CameraTools::StopCamera();
160 }
161
162 void
163 QrCodeRecognizer::OnLowMemory(void)
164 {
165 }
166
167 void
168 QrCodeRecognizer::OnBatteryLevelChanged(BatteryLevel batteryLevel)
169 {
170         bool isCharging = true;
171         Tizen::System::RuntimeInfo::GetValue(String(L"IsCharging"),isCharging);
172         if (isCharging == false)
173         {
174                 if ((batteryLevel == BATTERY_EMPTY) || (batteryLevel == BATTERY_CRITICAL) || (batteryLevel == BATTERY_LOW))
175                 {
176                         MessageBox msgBox;
177                         int result = 0;
178
179                         msgBox.Construct("Warning","Low Battery.\nTerminating App.",MSGBOX_STYLE_NONE,8000);
180                         msgBox.ShowAndWait(result);
181                         Application::GetInstance()->Terminate();
182                 }
183         }
184 }
185
186 bool
187 QrCodeRecognizer::OnKeyPressed (Control &source, const KeyEventInfo &keyEventInfo)
188 {
189         return false;
190 }
191
192 bool
193 QrCodeRecognizer::OnKeyReleased (Control &source, const KeyEventInfo &keyEventInfo)
194 {
195         return false;
196 }
197
198 bool
199 QrCodeRecognizer::OnPreviewKeyPressed (Control &source, const KeyEventInfo &keyEventInfo)
200 {
201         return false;
202 }
203
204 bool
205 QrCodeRecognizer::OnPreviewKeyReleased (Control &source, const KeyEventInfo &keyEventInfo)
206 {
207         return false;
208 }
209
210 bool
211 QrCodeRecognizer::TranslateKeyEventInfo (Control &source, KeyEventInfo &keyEventInfo)
212 {
213         return false;
214 }
215
216 void
217 QrCodeRecognizer::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
218 {
219
220 }
221
222 void
223 QrCodeRecognizer::OnFormBackRequested(Tizen::Ui::Controls::Form &source)
224 {
225         if (__pTracker && (CameraTools::GetCamera()->GetState() == Tizen::Media::CAMERA_STATE_PREVIEW || CameraTools::GetCamera()->GetState() == Tizen::Media::CAMERA_STATE_AUTO_FOCUSING))
226                 {
227                     __pTracker->Terminate();
228                 }
229                 else
230                 {
231                     Tizen::App::Application::GetInstance()->Terminate();
232                 }
233 }
234
235 void
236 QrCodeRecognizer::OnDeviceStateChanged(Tizen::System::DeviceType deviceType, const Tizen::Base::String &state)
237 {
238     if (deviceType == Charger)
239     {
240         BatteryLevel level = BATTERY_FULL;
241         Tizen::System::Battery::GetCurrentLevel(level);
242         OnBatteryLevelChanged(level);
243     }
244 }