new GUI
[apps/native/sample/QrCodeRecognizer.git] / project / src / QrCodeRecognizerSample.cpp
1 //
2 // Tizen Native SDK
3 // Open Service Platform
4 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
5 // All rights reserved.
6 //
7 // This software contains confidential and proprietary information
8 // of Samsung Electronics Co., Ltd.
9 // The user of this software agrees not to disclose, disseminate or copy such
10 // Confidential Information and shall use the software only in accordance with
11 // the terms of the license agreement the user entered into with Samsung.
12 //
13
14 #include "QrCodeRecognizerSample.h"
15 #include "CameraTools.h"
16 #include "GLtools.h"
17
18 using namespace Tizen::App;
19 using namespace Tizen::Ui::Controls;
20 using namespace Tizen::System;
21
22 QrCodeRecognizer::QrCodeRecognizer()
23     : __pTracker(0)
24     , __pForm(0)
25     , __pControlManager(0)
26     , __pPopup(0)
27 {
28 }
29
30 QrCodeRecognizer::~QrCodeRecognizer()
31 {
32 }
33
34
35 UiApp*
36 QrCodeRecognizer::CreateInstance(void)
37 {
38     // Create the instance through the constructor.
39     return new QrCodeRecognizer();
40 }
41
42
43 bool
44 QrCodeRecognizer::OnAppInitializing(AppRegistry& appRegistry)
45 {
46     PowerManager::SetScreenEventListener(*this);
47     PowerManager::KeepScreenOnState(true, false);
48     return true;
49 }
50
51 bool
52 QrCodeRecognizer::OnAppInitialized(void)
53 {
54     Frame* pAppFrame = new Frame();
55     pAppFrame->Construct();
56     AddFrame(*pAppFrame);
57
58     //pAppFrame->SetOrientation(Tizen::Ui::ORIENTATION_PORTRAIT);
59
60     // Creates an instance of Popup
61     __pPopup = new Tizen::Ui::Controls::Popup();
62     __pPopup->Construct(true, Tizen::Graphics::Dimension(570, 250));
63
64     __pForm = new TouchForm();
65     __pForm->Initialize(this, this);
66     pAppFrame->AddControl(*__pForm);
67     GLtools::initGL(__pForm);
68     __pForm->SendUserEvent(TouchForm::REQUEST_ACTIVATE, null);
69
70     __pTracker = new Tracker();
71
72     if(CameraTools::StartCamera(*__pTracker, Tracker::DESIRED_CAMERA_WIDTH, Tracker::DESIRED_CAMERA_HEIGHT))
73     {
74         __pTracker->Init(__pForm);
75     }
76
77         __pPopup->SetTitleText(L"Exit application?");
78
79         // Creates an instance of Button to close the popup.
80         Button* pYesButton = new Button();
81         pYesButton->Construct(Tizen::Graphics::Rectangle(10, 50, 250, 80), L"Yes");
82         pYesButton->SetActionId(ID_BUTTON_YES);
83         pYesButton->AddActionEventListener(*this);
84
85         Button* pNoButton = new Button();
86         pNoButton->Construct(Tizen::Graphics::Rectangle(270, 50, 250, 80), L"No");
87         pNoButton->SetActionId(ID_BUTTON_NO);
88         pNoButton->AddActionEventListener(*this);
89
90         // Adds the button to the popup
91         __pPopup->AddControl(*pYesButton);
92         __pPopup->AddControl(*pNoButton);
93
94     return true;
95 }
96
97 bool
98 QrCodeRecognizer::OnAppWillTerminate(void)
99 {
100     return true;
101 }
102
103 bool
104 QrCodeRecognizer::OnAppTerminating(AppRegistry& appRegistry, bool forcedTermination)
105 {
106     CameraTools::StopCamera();
107     GLtools::destroyGL();
108     if(__pTracker)
109     {
110         __pTracker->DoRendering(false);
111         delete __pTracker;
112     }
113
114     return true;
115 }
116
117 void
118 QrCodeRecognizer::OnForeground(void)
119 {
120     PowerManager::KeepScreenOnState(true, false);
121
122     if(CameraTools::Restart() == false)
123     {
124         if(__pTracker != null)
125         {
126                 delete __pTracker;
127                 __pTracker = null;
128         }
129         MessageBox msgBox;
130         int result = 0;
131
132         msgBox.Construct("Warning","No Camera. Terminating App.", MSGBOX_STYLE_NONE, 3000);
133         msgBox.ShowAndWait(result);
134
135         switch (result)
136         {
137                 case MSGBOX_RESULT_CLOSE:
138                         Tizen::App::Application::GetInstance()->Terminate();
139                         break;
140
141                 default:
142                         break;
143         }
144     }
145 }
146
147 // Called when application is moved to background
148 void
149 QrCodeRecognizer::OnBackground(void)
150 {
151     CameraTools::Suspend();
152 }
153
154 void
155 QrCodeRecognizer::OnLowMemory(void)
156 {
157 }
158
159 void
160 QrCodeRecognizer::OnBatteryLevelChanged(BatteryLevel batteryLevel)
161 {
162 }
163
164 void
165 QrCodeRecognizer::OnScreenOn (void)
166 {
167     PowerManager::KeepScreenOnState(true, false);
168     CameraTools::Restart();
169 }
170
171 void
172 QrCodeRecognizer::OnScreenOff (void)
173 {
174     CameraTools::Suspend();
175 }
176
177 void
178 QrCodeRecognizer::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
179 {
180     switch(actionId)
181     {
182     case TouchForm::ID_BUTTON_EXECUTE:
183     {
184         if(__pTracker)
185         {
186             ExecuteQr(__pTracker->GetTextMessage());
187         }
188     }
189         break;
190         
191     case ID_BUTTON_YES:
192     {
193         //Crushing application without this
194         if(__pPopup)
195         {
196             delete __pPopup;
197         }
198         if(__pTracker)
199         {
200             __pTracker->Terminate();
201         }
202         else
203         {
204             Tizen::App::Application::GetInstance()->Terminate();
205         }
206     }
207         break;
208     case ID_BUTTON_NO:
209         __pPopup->SetShowState(false);
210         break;
211     default:
212         break;
213     }
214 }
215
216 void
217 QrCodeRecognizer::ExecuteQr(Tizen::Base::String text)
218 {
219     if (!text.IsEmpty())
220     {
221        ControlManager* cMan = ControlManager::GetInstance();
222
223        if (text.StartsWith(L"http://", 0) ||
224                text.StartsWith("ftp://", 0) ||
225                text.StartsWith("ftps://", 0) ||
226                text.StartsWith("sftp://", 0) ||
227                text.StartsWith("https://", 0)
228                )
229        {
230            cMan->ShowUrl(text);
231        }
232        else if (text.StartsWith(L"sms:", 0))
233        {
234            Tizen::Base::String _number;
235            Tizen::Base::String _body;
236
237            int _numberStartIndex = 4; // message starting with "sms:" substring with length = 4
238            int _numberFinishIndex;
239            int _messageStartIndex;
240            text.IndexOf(L"?", _numberStartIndex, _numberFinishIndex);
241            text.SubString(_numberStartIndex, _numberFinishIndex - _numberStartIndex, _number);
242            text.IndexOf(L"body=", _numberStartIndex, _messageStartIndex);
243            text.SubString(_messageStartIndex + 5, _body);
244
245            cMan->ShowSms(_number, _body);
246        }
247        else if (text.StartsWith(L"BEGIN:VCARD", 0))
248        {
249            cMan->ShowContact(text);
250        }
251        else
252        {
253            cMan->ShowText(text);
254        }
255     }
256 }
257
258 void
259 QrCodeRecognizer::OnFormBackRequested(Tizen::Ui::Controls::Form &source)
260 {
261     __pPopup->SetShowState(true);
262     __pPopup->Show();
263 }