Modify the screen UI for stt mode and refactoring code
[platform/core/uifw/inputdelegator.git] / src / SttFeedback.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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 #include "Debug.h"
18 #include "SttFeedback.h"
19
20 #include "w-input-stt-engine.h"
21 #include "w-input-stt-voice.h"
22
23 using namespace is::stt;
24
25 SttFeedback::SttFeedback()
26 {
27         owner = NULL;
28 }
29
30 SttFeedback::~SttFeedback()
31 {
32 }
33
34 void SttFeedback::OnResult(
35         std::string asrtype,
36         stt_result_event_e event,
37         std::vector<std::string> results,
38         std::string msg) {
39         if (!owner) return;
40
41         VoiceData& vd = *((VoiceData *) owner);
42
43         if (vd.state == STT_STATE_VAL_TERMINATING) {
44                 PRINTFUNC(DLOG_WARN, "STT_STATE_VAL_TERMINATING");
45                 return;
46         }
47
48         // asrtype
49         // Partial result case : STT_RECOGNITION_TYPE_FREE_PARTIAL
50         // Normal result case : STT_RECOGNITION_TYPE_FREE
51
52         if(results.size() < 1) {
53                 PRINTFUNC(DLOG_ERROR, "Result size : %d", results.size());
54                 vd.state = STT_STATE_VAL_NOT_RECOGNISED;
55                 set_animation_state(&vd);
56         } else {
57                 PRINTFUNC(DLOG_INFO, "Meaningful result : size (%d)", results.size());
58
59                 for(auto result : results) {
60                         PRINTFUNC(DLOG_INFO, "Results");
61                         if(!result.empty() && result.length() > 0) {
62                                 PRINTFUNC(DLOG_INFO, "%s\n", result.c_str());
63                                 switch(event){
64                                         case STT_RESULT_EVENT_FINAL_RESULT:
65                                                 PRINTFUNC(DLOG_DEBUG, "STT_RESULT_EVENT_FINAL_RESULT");
66                                                 vd.state = STT_STATE_VAL_INIT;
67                                                 set_animation_state(&vd);
68                                                 vd.result_type = STT_RESULT_EVENT_FINAL_RESULT;
69                                                 voice_get_string(result.c_str(), &vd);
70                                                 break;
71                                         case STT_RESULT_EVENT_PARTIAL_RESULT:
72                                                 PRINTFUNC(DLOG_DEBUG, "STT_RESULT_EVENT_PARTIAL_RESULT");
73                                                 vd.result_type = STT_RESULT_EVENT_PARTIAL_RESULT;
74                                                 voice_get_string(result.c_str(), &vd);
75                                                 break;
76                                         case STT_RESULT_EVENT_ERROR:
77                                                 PRINTFUNC(DLOG_DEBUG, "STT_RESULT_EVENT_ERROR");
78                                                 vd.state = STT_STATE_VAL_NOT_RECOGNISED;
79                                                 set_animation_state(&vd);
80                                                 break;
81                                         default:
82                                                 PRINTFUNC(DLOG_INFO, "");
83                                                 break;
84                                 }
85                                 return;
86                         }
87                         PRINTFUNC(DLOG_INFO, "Empty result");
88                 }
89
90                 PRINTFUNC(DLOG_INFO, "Need to specify this case");
91
92                 /**
93                  * Note.
94                  *
95                  * if recognized result doesn't have any data,
96                  * it will process as recognition fail.
97                  *
98                  */
99                 //vd.state = STT_STATE_VAL_NOT_RECOGNISED;
100                 vd.state = STT_STATE_VAL_INIT;
101                 set_animation_state(&vd);
102         }
103 }
104
105 void SttFeedback::AutoStart(void) {
106         PRINTFUNC(DLOG_DEBUG, "start");
107         start_by_press((VoiceData *) owner);
108 }
109
110 void SttFeedback::SttIdle(void)
111 {
112         if (!owner) {
113                 PRINTFUNC(DLOG_WARN, "no owner");
114                 return;
115         }
116
117         VoiceData& vd = *((VoiceData *) owner);
118
119         /**
120          * Note.
121          *
122          * When recognition is failed, do nothing.
123          * Because after result, it's called continuous. So it looks not natural.
124          *
125          * So in this case, we will not change as INIT state.
126          * using 2 sec timer, it will change as idle state.
127          *
128          */
129         if (vd.state == STT_STATE_VAL_NOT_RECOGNISED) {
130                 PRINTFUNC(DLOG_INFO, "Ignore when state was STT_STATE_VAL_NOT_RECOGNISED");
131                 return;
132         }
133
134         PRINTFUNC(DLOG_DEBUG, "UI will go to idle state");
135
136         vd.state = STT_STATE_VAL_INIT;
137         set_animation_state(&vd);
138 }
139
140 void SttFeedback::SttRecording(void)
141 {
142         if(!owner) {
143                 PRINTFUNC(DLOG_WARN, "no owner");
144                 return;
145         }
146
147         VoiceData& vd = *((VoiceData *) owner);
148
149         if(vd.partial_result){
150                 free(vd.partial_result);
151                 vd.partial_result = NULL;
152         }
153
154         PRINTFUNC(DLOG_DEBUG, "UI will go to listening state");
155
156         vd.state = STT_STATE_VAL_LISTENING;
157         set_animation_state(&vd);
158 }
159
160 void SttFeedback::SttProcessing(void)
161 {
162         if(!owner) {
163                 PRINTFUNC(DLOG_WARN, "no owner");
164                 return;
165         }
166
167         VoiceData& vd = *((VoiceData *) owner);
168
169         PRINTFUNC(DLOG_DEBUG, "UI will go to processing state");
170
171         vd.state = STT_STATE_VAL_PROCESSING;
172         set_animation_state(&vd);
173 }
174
175
176
177 void SttFeedback::OnError(stt_error_e reason)
178 {
179         if(!owner) {
180                 PRINTFUNC(DLOG_WARN, "no owner");
181                 return;
182         }
183
184         VoiceData& vd = *((VoiceData *) owner);
185
186         PRINTFUNC(DLOG_ERROR, "error = %d\n", reason);
187         vd.state = STT_STATE_VAL_INIT;
188         set_animation_state(&vd);
189         show_error_message(&vd, reason);
190 }
191
192
193 void SttFeedback::SetVoiceData(void *data) {
194         if(!data) {
195                 PRINTFUNC(DLOG_WARN, "no data");
196                 return;
197         }
198         owner = data;
199 }