Fix issue that Black screen displayed when exiting Voice input screen
[platform/core/uifw/inputdelegator.git] / src / MoreOption.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
19 #include <string>
20 #include <assert.h>
21 #include <iostream>
22 #include <stdexcept>
23
24 #include <app.h>
25 #include <app_common.h>
26
27 #include "MoreOption.h"
28 #include "w-input-selector.h"
29 #include "w-input-stt-ise.h"
30 #include "w-input-stt-voice.h"
31
32 using namespace std;
33
34 extern Evas_Object *g_setting_window;
35
36 MoreOption::MoreOption(Evas_Object *naviframe, void* voicedata)
37         : nf(naviframe)
38         , more_option_layout(NULL)
39         , item(NULL)
40         , option_opened(EINA_FALSE)
41         , voicedata(voicedata) {
42         /** todo. implement constructor */
43 }
44
45 MoreOption::~MoreOption() {
46         /** todo. implement destructor */
47
48 //      if(more_option_layout)
49 //              evas_object_del(more_option_layout);
50 }
51
52 void MoreOption::Create() {
53         try {
54                 AddLayout();
55                 AddMorePage();
56         }
57         catch(std::exception &e) {
58                 PRINTFUNC(DLOG_ERROR, "%s", e.what());
59                 assert(0);
60         }
61 }
62
63 void MoreOption::AddLayout() {
64         /** validation */
65         if(!nf)
66                 PRINTFUNC(DLOG_ERROR, "Invalid naviframe.");
67
68         /** add layout */
69         more_option_layout = eext_more_option_add(nf);
70
71         if(!more_option_layout)
72                 PRINTFUNC(DLOG_ERROR, "It's failed to create layout");
73
74         evas_object_smart_callback_add(more_option_layout, "more,option,opened",
75                 [](void *data, Evas_Object *obj, void *event_info){
76                         PRINTFUNC(DLOG_DEBUG, "more option is opened!!! \n");
77
78                         if(!data) return;
79
80                         /**
81                         * if more option is completed, stt have to be stopped.
82                         *
83                         */
84                         MoreOption *opt = (MoreOption *)data;
85                         VoiceData *vd = (VoiceData *) opt->voicedata;
86                         try {
87                                    if (vd->sttmanager->GetCurrent() == STT_STATE_RECORDING ||
88                                                    vd->sttmanager->GetCurrent() == STT_STATE_PROCESSING) {
89                                                    vd->sttmanager->Cancel();
90                                   }
91                         }
92                         catch(is::stt::SttException &e) {
93                                 PRINTFUNC(DLOG_ERROR, "%s", e.what());
94                         }
95
96                         opt->option_opened = EINA_TRUE;
97         }, this);
98
99         evas_object_smart_callback_add(more_option_layout, "more,option,closed",
100                 [](void *data, Evas_Object *obj, void *event_info){
101                 PRINTFUNC(DLOG_DEBUG, "more option is closed!!! \n");
102
103                 MoreOption *opt = (MoreOption *)data;
104                 VoiceData *vd = (VoiceData *) opt->voicedata;
105
106                 activate_circle_scroller_for_stt_textbox(vd, EINA_TRUE);
107
108                 opt->option_opened = EINA_FALSE;
109         }, this);
110
111         evas_object_show(more_option_layout);
112 }
113
114 void MoreOption::SetContentLayout(Evas_Object *content) {
115         /**
116          * Set content layout
117          *
118          */
119         elm_object_part_content_set(more_option_layout, "elm.swallow.content", content);
120
121         Elm_Object_Item *nit = NULL;
122         const char *item_style = NULL;
123         if (_WEARABLE)
124                 item_style = "empty";
125         nit = elm_naviframe_item_push(nf, NULL, NULL, NULL, more_option_layout, item_style);
126         elm_naviframe_item_title_enabled_set(nit, EINA_FALSE, EINA_FALSE);
127
128         elm_naviframe_item_pop_cb_set(nit,
129                 [](void *data, Elm_Object_Item *it)->Eina_Bool
130                 {
131                         MoreOption *opt = (MoreOption *)data;
132                         VoiceData *vd = (VoiceData *) opt->voicedata;
133
134                         if(vd->disclaimer == 1){
135                                 PRINTFUNC(DLOG_ERROR, "pop to top"); //inb case of (selector view -> disclaimer view-> stt view)
136
137                                 if(opt->option_opened == EINA_FALSE){
138                                         PRINTFUNC(DLOG_DEBUG, "pop_cb called in STT view\n");
139
140                                         if(vd->sttmanager){
141                                                 if (vd->sttmanager->GetCurrent() == STT_STATE_CREATED ||
142                                                                    vd->sttmanager->GetCurrent() == STT_STATE_READY) {
143                                                         PRINTFUNC(DLOG_DEBUG, "STT_STATE_CREATED || STT_STATE_READY\n");
144
145                                                 } else if (vd->sttmanager->GetCurrent() == STT_STATE_RECORDING) {
146                                                         PRINTFUNC(DLOG_DEBUG, "STT_STATE_RECORDING\n");
147                                                         vd->effector->Stop();
148                                                         try{
149                                                                 vd->sttmanager->Stop();
150                                                         }catch(is::stt::SttException &e){
151                                                                 PRINTFUNC(DLOG_ERROR, "reason : %s", e.what());
152                                                         }
153
154                                                 } else if (vd->sttmanager->GetCurrent() == STT_STATE_PROCESSING) {
155                                                         PRINTFUNC(DLOG_DEBUG, "STT_STATE_RECORDING\n");
156                                                         try{
157                                                                 vd->sttmanager->Cancel();
158                                                         }catch(is::stt::SttException &e){
159                                                                 PRINTFUNC(DLOG_ERROR, "reason : %s", e.what());
160                                                         }
161                                                 }
162                                         }
163                                 }
164
165
166                                 elm_naviframe_item_pop_to(elm_naviframe_bottom_item_get(vd->naviframe));
167                                 vd->disclaimer  = 0;
168
169                                 powerUnlock();
170                                 _back_to_genlist_for_selector();
171
172                                 return EINA_TRUE;
173                         }
174
175                         if(opt->option_opened == EINA_FALSE){
176                                 PRINTFUNC(DLOG_DEBUG, "pop_cb called in STT view\n");
177
178                                 if(vd->sttmanager){
179                                         if (vd->sttmanager->GetCurrent() == STT_STATE_CREATED ||
180                                                            vd->sttmanager->GetCurrent() == STT_STATE_READY) {
181                                                 PRINTFUNC(DLOG_DEBUG, "STT_STATE_CREATED || STT_STATE_READY\n");
182
183                                         } else if (vd->sttmanager->GetCurrent() == STT_STATE_RECORDING) {
184                                                 PRINTFUNC(DLOG_DEBUG, "STT_STATE_RECORDING\n");
185                                                 vd->effector->Stop();
186                                                 try{
187                                                         vd->sttmanager->Stop();
188                                                 }catch(is::stt::SttException &e){
189                                                         PRINTFUNC(DLOG_ERROR, "reason : %s", e.what());
190                                                 }
191
192                                         } else if (vd->sttmanager->GetCurrent() == STT_STATE_PROCESSING) {
193                                                 PRINTFUNC(DLOG_DEBUG, "STT_STATE_RECORDING\n");
194                                                 try{
195                                                         vd->sttmanager->Cancel();
196                                                 }catch(is::stt::SttException &e){
197                                                         PRINTFUNC(DLOG_ERROR, "reason : %s", e.what());
198                                                 }
199                                         }
200
201                                         _back_to_genlist_for_selector();
202
203                                         destroy_voice();
204
205                                         return EINA_TRUE;
206                                 } else {
207                                         PRINTFUNC(DLOG_DEBUG, "naviframe transition, not finished\n");
208                                         return EINA_FALSE;
209                                 }
210                         }
211                         return EINA_FALSE;
212                 }, this);
213 }
214
215 void MoreOption::Update()
216 {
217         char lang[6] = {0, };
218         strncpy(lang, ((VoiceData *)voicedata)->kbd_lang , 5);
219
220         const char* display_lang = get_lang_label(lang);
221         eext_more_option_item_part_text_set(item, "selector,sub_text", display_lang);
222 }
223
224 Evas_Object* MoreOption::AddLanguageIcon(Evas_Object *parent) {
225         Evas_Object *icon = elm_image_add(parent);
226         if (!icon) {
227                 PRINTFUNC(DLOG_ERROR, "It's failed to add image.");
228         }
229
230         string res_path = get_resource_path();
231         if (_WEARABLE)
232                 res_path = res_path + "wearable/";
233         else if (_TV)
234                 res_path = res_path + "tv/";
235         else
236                 res_path = res_path + "mobile/";
237
238         std::string image_path = res_path + "images/prompt_ic_languages.png";
239
240         elm_image_file_set(icon, image_path.c_str(), NULL);
241         evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
242         evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
243         evas_object_show(icon);
244
245         return icon;
246 }
247
248 void MoreOption::AddMorePage() {
249         Evas_Object *img;
250
251         item  = eext_more_option_item_append(more_option_layout);
252
253         /* Slider content */
254         eext_more_option_item_part_text_set(item, "selector,main_text", _("WDS_IME_HEADER_INPUT_LANGUAGES_ABB"));
255
256         img = AddLanguageIcon(more_option_layout);
257
258         eext_more_option_item_part_content_set(item, "item,icon", img);
259
260         evas_object_smart_callback_add(more_option_layout, "item,clicked",
261         [](void *data, Evas_Object *obj, void *event_info)
262         {
263                 PRINTFUNC(DLOG_DEBUG, "item,clicked");
264                 if(g_setting_window == NULL)
265                         create_setting_window(obj);
266         }, NULL);
267
268         evas_object_smart_callback_add(more_option_layout, "item,selected",
269         [](void *data, Evas_Object *obj, void *event_info)
270         {
271                 PRINTFUNC(DLOG_DEBUG, "item,selected");
272         }, NULL);
273 }
274