6f21e0cb7ccd5c6122ff5f91bebd050a181ae923
[apps/native/sample/SpeechApp.git] / project / src / SpeechAppSttForm.cpp
1 //
2 // Tizen C++ SDK
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (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://www.tizenopensource.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 <FUiScenes.h>
19 #include <FGraphics.h>
20 #include <FLocales.h>
21 #include "SpeechAppSttForm.h"
22
23 using namespace Tizen::Base;
24 using namespace Tizen::Base::Collection;
25 using namespace Tizen::Ui;
26 using namespace Tizen::Ui::Controls;
27 using namespace Tizen::Ui::Scenes;
28 using namespace Tizen::Uix::Speech;
29 using namespace Tizen::Graphics;
30 using namespace Tizen::Locales;
31
32
33 SpeechAppSttForm::SpeechAppSttForm(void)
34         : __pStt(null)
35         , __pLabel(null)
36         , __pButtonStart(null)
37         , __pButtonStop(null)
38         , __pButtonCancel(null)
39         , __isInited(false)
40 {
41 }
42
43 SpeechAppSttForm::~SpeechAppSttForm(void)
44 {
45 }
46
47 bool
48 SpeechAppSttForm::Initialize(void)
49 {
50         Construct(L"IDF_FORM_STT");
51
52         return true;
53 }
54
55 result
56 SpeechAppSttForm::OnInitializing(void)
57 {
58         result r = E_SUCCESS;
59
60         SetFormBackEventListener(this);
61
62         __pButtonStart = static_cast< Button* >(GetControl(L"IDC_BUTTON_START"));
63         if (__pButtonStart != null)
64         {
65                 __pButtonStart->SetActionId(ID_BUTTON_START);
66                 __pButtonStart->AddActionEventListener(*this);
67                 __pButtonStart->SetEnabled(true);
68         }
69
70         __pButtonStop = static_cast< Button* >(GetControl(L"IDC_BUTTON_STOP"));
71         if (__pButtonStop != null)
72         {
73                 __pButtonStop->SetActionId(ID_BUTTON_STOP);
74                 __pButtonStop->AddActionEventListener(*this);
75                 __pButtonStop->SetEnabled(false);
76         }
77
78         __pButtonCancel = static_cast< Button* >(GetControl(L"IDC_BUTTON_CANCEL"));
79         if (__pButtonCancel != null)
80         {
81                 __pButtonCancel->SetActionId(ID_BUTTON_CANCEL);
82                 __pButtonCancel->AddActionEventListener(*this);
83                 __pButtonCancel->SetEnabled(false);
84         }
85
86         __pLabel = static_cast< Label* >(GetControl(L"IDC_LABEL_INFO"));
87         if (__pLabel != null)
88         {
89                 __pLabel->SetText("Information box");
90         }
91
92         __pStt = new (std::nothrow) SpeechToText();
93         if (__pStt != null)
94         {
95                 r = __pStt->Construct(*this);
96             TryLog(r == E_SUCCESS, "[%s] Construct() error", GetErrorMessage(r));
97
98                 r = __pStt->Initialize();
99                 TryLog(r == E_SUCCESS, "[%s] Initialize() error", GetErrorMessage(r));
100         }
101         else
102         {
103                 SceneManager* pSceneManager = SceneManager::GetInstance();
104                 AppAssert(pSceneManager);
105                 pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT));
106         }
107
108         SetOrientation(ORIENTATION_PORTRAIT);
109
110         return r;
111 }
112
113 result
114 SpeechAppSttForm::OnTerminating(void)
115 {
116         result r = E_SUCCESS;
117
118         delete __pStt;
119
120         return r;
121 }
122
123 void
124 SpeechAppSttForm::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
125 {
126         result r = E_SUCCESS;
127
128         SceneManager* pSceneManager = SceneManager::GetInstance();
129         AppAssert(pSceneManager);
130
131         switch (actionId)
132         {
133         case ID_BUTTON_START:
134                 r = __pStt->Start();
135                 if (IsFailed(r))
136                 {
137                         AppLog("[%s] Start() error", GetErrorMessage(r));
138                 }
139                 else
140                 {
141                         __pButtonStart->SetEnabled(false);
142                         __pButtonStop->SetEnabled(true);
143                         __pButtonCancel->SetEnabled(true);
144
145                         __pLabel->SetText("Recording Speech....");
146
147                         Draw();
148                 }
149                 break;
150
151         case ID_BUTTON_STOP:
152                 r = __pStt->Stop();
153                 if (IsFailed(r))
154                 {
155                         AppLog("[%s] Stop() error", GetErrorMessage(r));
156                 }
157                 else
158                 {
159                         __pButtonStart->SetEnabled(false);
160                         __pButtonStop->SetEnabled(false);
161                         __pButtonCancel->SetEnabled(true);
162                         __pLabel->SetText("Converting Speech....");
163
164                         Draw();
165                 }
166                 break;
167
168         case ID_BUTTON_CANCEL:
169                 r = __pStt->Cancel();
170             TryLog(r == E_SUCCESS, "[%s] Cancel() error", GetErrorMessage(r));
171
172                 pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT));
173                 break;
174
175         default:
176                 break;
177         }
178 }
179
180 void
181 SpeechAppSttForm::OnUserEventReceivedN(RequestId requestId, Tizen::Base::Collection::IList* pArgs)
182 {
183         switch (requestId)
184         {
185         case REQUEST_ID_EXIT:
186                 ShowMsgBox();
187                 break;
188
189         default:
190                 break;
191         }
192 }
193
194 void
195 SpeechAppSttForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
196 {
197         SceneManager* pSceneManager = SceneManager::GetInstance();
198         AppAssert(pSceneManager);
199
200         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT));
201 }
202
203 void
204 SpeechAppSttForm::OnSpeechToTextInitialized(void)
205 {
206         Tizen::Ui::Controls::ListView* pList = static_cast< ListView* >(GetControl(L"IDC_LISTVIEW_LANGS"));
207         if (pList != null)
208         {
209                 pList->SetItemProvider(*this);
210                 pList->AddListViewItemEventListener(*this);
211         }
212         pList->UpdateList();
213
214         __pStt->SetLocale(Locale(LANGUAGE_ENG, COUNTRY_US));
215         __isInited = true;
216
217         __pLabel->SetText("Default Language: eng_US");
218         __pLabel->Draw();
219 }
220
221 void
222 SpeechAppSttForm::OnSpeechToTextStatusChanged(Tizen::Uix::Speech::SpeechToTextStatus status)
223 {
224 }
225
226 void
227 SpeechAppSttForm::OnSpeechToTextErrorOccurred(Tizen::Uix::Speech::SpeechToTextError error)
228 {
229         if (error == SPEECH_TO_TEXT_ERROR_UNSUPPORTED_SERVICE)
230         {
231                 this->SendUserEvent(REQUEST_ID_EXIT, null);
232         }
233 }
234
235 void
236 SpeechAppSttForm::OnSpeechToTextCompleted(const Tizen::Base::String& result)
237 {
238         SceneManager* pSceneManager = SceneManager::GetInstance();
239         AppAssert(pSceneManager);
240
241         IList* pList = new (std::nothrow) ArrayList();
242         pList->Add(*(new (std::nothrow) String(result)));
243         pList->Add(*(new (std::nothrow) Locale(__pStt->GetLocale())));
244         pSceneManager->GoForward(ForwardSceneTransition(L"TtsScene", SCENE_TRANSITION_ANIMATION_TYPE_LEFT, SCENE_HISTORY_OPTION_NO_HISTORY, SCENE_DESTROY_OPTION_DESTROY), pList);
245 }
246
247 void
248 SpeechAppSttForm::ShowMsgBox(void)
249 {
250         SceneManager* pSceneManager = SceneManager::GetInstance();
251         AppAssert(pSceneManager);
252
253         MessageBox messageBox;
254         messageBox.Construct(L"Error", L"Unsupported service.", MSGBOX_STYLE_OK);
255
256         int result = 0;
257         messageBox.ShowAndWait(result);
258
259         switch (result)
260         {
261         case MSGBOX_RESULT_OK:
262                 pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT));
263                 break;
264
265         default:
266                 break;
267         }
268 }
269
270 Tizen::Ui::Controls::ListItemBase*
271 SpeechAppSttForm::CreateItem(int index, int itemWidth)
272 {
273         ListAnnexStyle style = LIST_ANNEX_STYLE_NORMAL;
274         CustomItem* pItem = new (std::nothrow) CustomItem();
275         pItem->Construct(Tizen::Graphics::Dimension(itemWidth, 100), style);
276
277         const IList* pList = __pStt->GetSupportedLocales();
278         const Locale* pLocale = static_cast< const Locale* >(pList->GetAt(index));
279
280         pItem->AddElement(Rectangle(10, 25, itemWidth, 50), index, pLocale->GetLocaleCodeString(), true);
281
282         return pItem;
283 }
284
285 bool
286 SpeechAppSttForm::DeleteItem(int index, Tizen::Ui::Controls::ListItemBase* pItem, int itemWidth)
287 {
288         delete pItem;
289         pItem = null;
290
291         return true;
292 }
293
294 int
295 SpeechAppSttForm::GetItemCount(void)
296 {
297         return __pStt->GetSupportedLocales()->GetCount();
298 }
299
300 void
301 SpeechAppSttForm::OnListViewContextItemStateChanged(Tizen::Ui::Controls::ListView& listView, int index, int elementId, Tizen::Ui::Controls::ListContextItemStatus state)
302 {
303 }
304
305 void
306 SpeechAppSttForm::OnListViewItemStateChanged(Tizen::Ui::Controls::ListView& listView, int index, int elementId, Tizen::Ui::Controls::ListItemStatus status)
307 {
308         const IList* pList = __pStt->GetSupportedLocales();
309         const Locale* pLocale = static_cast< const Locale* >(pList->GetAt(index));
310
311         result r = __pStt->SetLocale(*pLocale);
312         TryLog(r == E_SUCCESS, "[%s] Error occurred", GetErrorMessage(r));
313
314         __pLabel->SetText(String("Selected Language: ") + pLocale->GetLocaleCodeString().GetPointer());
315         __pLabel->Draw();
316 }
317
318 void
319 SpeechAppSttForm::OnListViewItemSwept(Tizen::Ui::Controls::ListView& listView, int index, Tizen::Ui::Controls::SweepDirection direction)
320 {
321 }
322
323 void
324 SpeechAppSttForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId,
325                                                                         const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
326 {
327         AppLog("OnSceneActivatedN()");
328 }
329 void
330 SpeechAppSttForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId,
331                                                                          const Tizen::Ui::Scenes::SceneId& nextSceneId)
332 {
333         AppLog("OnSceneActivatedN()");
334 }