remove the legacy code for accessibility
[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                                         default:
77                                                 PRINTFUNC(DLOG_INFO, "");
78                                                 break;
79                                 }
80                                 return;
81                         }
82                         PRINTFUNC(DLOG_INFO, "Empty result");
83                 }
84
85                 PRINTFUNC(DLOG_INFO, "Need to specify this case");
86
87                 /**
88                  * Note.
89                  *
90                  * if recognized result doesn't have any data,
91                  * it will process as recognition fail.
92                  *
93                  */
94                 //vd.state = STT_STATE_VAL_NOT_RECOGNISED;
95                 vd.state = STT_STATE_VAL_INIT;
96                 set_animation_state(&vd);
97         }
98 }
99
100 void SttFeedback::AutoStart(void) {
101         PRINTFUNC(DLOG_DEBUG, "start");
102         start_by_press((VoiceData *) owner);
103 }
104
105 void SttFeedback::SttIdle(void)
106 {
107         if (!owner) {
108                 PRINTFUNC(DLOG_WARN, "no owner");
109                 return;
110         }
111
112         VoiceData& vd = *((VoiceData *) owner);
113
114         /**
115          * Note.
116          *
117          * When recognition is failed, do nothing.
118          * Because after result, it's called continuous. So it looks not natural.
119          *
120          * So in this case, we will not change as INIT state.
121          * using 2 sec timer, it will change as idle state.
122          *
123          */
124         if (vd.state == STT_STATE_VAL_NOT_RECOGNISED) {
125                 PRINTFUNC(DLOG_INFO, "Ignore when state was STT_STATE_VAL_NOT_RECOGNISED");
126                 return;
127         }
128
129         PRINTFUNC(DLOG_DEBUG, "UI will go to idle state");
130
131         vd.state = STT_STATE_VAL_INIT;
132         set_animation_state(&vd);
133 }
134
135 void SttFeedback::SttRecording(void)
136 {
137         if(!owner) {
138                 PRINTFUNC(DLOG_WARN, "no owner");
139                 return;
140         }
141
142         VoiceData& vd = *((VoiceData *) owner);
143
144         if(vd.partial_result){
145                 free(vd.partial_result);
146                 vd.partial_result = NULL;
147         }
148
149         PRINTFUNC(DLOG_DEBUG, "UI will go to listening state");
150
151         vd.state = STT_STATE_VAL_LISTENING;
152         set_animation_state(&vd);
153 }
154
155 void SttFeedback::SttProcessing(void)
156 {
157         if(!owner) {
158                 PRINTFUNC(DLOG_WARN, "no owner");
159                 return;
160         }
161
162         VoiceData& vd = *((VoiceData *) owner);
163
164         PRINTFUNC(DLOG_DEBUG, "UI will go to processing state");
165
166         vd.state = STT_STATE_VAL_PROCESSING;
167         set_animation_state(&vd);
168 }
169
170
171
172 void SttFeedback::OnError(stt_error_e reason)
173 {
174         if(!owner) {
175                 PRINTFUNC(DLOG_WARN, "no owner");
176                 return;
177         }
178
179         VoiceData& vd = *((VoiceData *) owner);
180
181         PRINTFUNC(DLOG_ERROR, "error = %d\n", reason);
182         vd.state = STT_STATE_VAL_INIT;
183         set_animation_state(&vd);
184         show_error_message(&vd, reason);
185 }
186
187
188 void SttFeedback::SetVoiceData(void *data) {
189         if(!data) {
190                 PRINTFUNC(DLOG_WARN, "no data");
191                 return;
192         }
193         owner = data;
194 }