11package com .chattylabs .demo .voice ;
22
33import android .annotation .SuppressLint ;
4- import android .app .AlertDialog ;
54import android .content .pm .PackageManager ;
65import android .media .AudioManager ;
76import android .os .Bundle ;
3029import com .chattylabs .sdk .android .common .Tag ;
3130import com .chattylabs .sdk .android .common .ThreadUtils ;
3231import com .chattylabs .sdk .android .voice .AndroidSpeechSynthesizer ;
32+ import com .chattylabs .sdk .android .voice .ComponentConfig ;
3333import com .chattylabs .sdk .android .voice .ConversationalFlowComponent ;
3434import com .chattylabs .sdk .android .voice .GoogleSpeechSynthesizer ;
3535import com .chattylabs .sdk .android .voice .Peripheral ;
3636import com .chattylabs .sdk .android .voice .TextFilterForUrl ;
37- import com .chattylabs .sdk .android .voice .ComponentConfig ;
37+
38+ import java .util .Arrays ;
39+ import java .util .LinkedHashMap ;
40+ import java .util .List ;
3841
3942import javax .inject .Inject ;
4043
4144import dagger .android .support .DaggerAppCompatActivity ;
4245
43- import static com .chattylabs .sdk .android .voice .ConversationalFlowComponent .*;
46+ import static com .chattylabs .sdk .android .voice .ConversationalFlowComponent .OnRecognizerError ;
47+ import static com .chattylabs .sdk .android .voice .ConversationalFlowComponent .OnRecognizerMostConfidentResult ;
48+ import static com .chattylabs .sdk .android .voice .ConversationalFlowComponent .OnRecognizerReady ;
49+ import static com .chattylabs .sdk .android .voice .ConversationalFlowComponent .OnSynthesizerDone ;
50+ import static com .chattylabs .sdk .android .voice .ConversationalFlowComponent .OnSynthesizerError ;
51+ import static com .chattylabs .sdk .android .voice .ConversationalFlowComponent .OnSynthesizerStart ;
4452import static com .chattylabs .sdk .android .voice .ConversationalFlowComponent .SpeechRecognizer ;
4553import static com .chattylabs .sdk .android .voice .ConversationalFlowComponent .SpeechSynthesizer ;
54+ import static com .chattylabs .sdk .android .voice .ConversationalFlowComponent .SynthesizerListener ;
55+ import static com .chattylabs .sdk .android .voice .ConversationalFlowComponent .matches ;
4656
4757
4858public class MainActivity extends DaggerAppCompatActivity
@@ -55,18 +65,26 @@ public class MainActivity extends DaggerAppCompatActivity
5565 public static final int LISTEN = 2 ;
5666 public static final int READ = 1 ;
5767
58- private static int ADDON_TYPE = R .id .addon_android ;
68+ private static LinkedHashMap <Integer , String > addonMap = new LinkedHashMap <>();
69+ static {
70+ addonMap .put (0 , "Android" );
71+ addonMap .put (1 , "Google" );
72+ }
73+
74+ private static String ADDON_TYPE = "Android" ;
5975
6076 // Resources
6177 private ConstraintLayout root ;
6278 private TextView execution ;
63- private Spinner spinner ;
79+ private Spinner actionSpinner ;
80+ private Spinner addonSpinner ;
6481 private EditText text ;
6582 private Button add ;
6683 private Button clear ;
6784 private Button proceed ;
6885 private SparseArray <Pair <Integer , String >> queue = new SparseArray <>();
69- private ArrayAdapter <CharSequence > adapter ;
86+ private ArrayAdapter <CharSequence > actionAdapter ;
87+ private ArrayAdapter <String > addonAdapter ;
7088 private CheckBox scoCheck ;
7189 private Menu menu ;
7290 private ThreadUtils .SerialThread serialThread ;
@@ -80,23 +98,12 @@ public class MainActivity extends DaggerAppCompatActivity
8098 @ Override
8199 public boolean onCreateOptionsMenu (Menu menu ) {
82100 MenuInflater inflater = getMenuInflater ();
83- inflater .inflate (R .menu .addons , menu );
84- this .menu = menu ;
85- updateMenu (menu );
101+ inflater .inflate (R .menu .demo , menu );
86102 return true ;
87103 }
88104
89- private void updateMenu (Menu menu ) {
90- for (int a = 0 ; a < menu .size (); a ++)
91- if (menu .getItem (a ).getItemId () == ADDON_TYPE ) menu .getItem (a ).setChecked (true );
92- else menu .getItem (a ).setChecked (false );
93- }
94-
95105 @ Override
96106 public boolean onOptionsItemSelected (MenuItem item ) {
97- ADDON_TYPE = item .getItemId ();
98- updateMenu (menu );
99- setup ();
100107 return super .onOptionsItemSelected (item );
101108 }
102109
@@ -132,15 +139,22 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
132139 //component = ConversationalFlowModule.provideComponent();
133140 component .updateConfiguration (builder ->
134141 builder .setGoogleCredentialsResourceFile (() -> R .raw .credential )
135- .setRecognizerServiceType (() ->
136- ADDON_TYPE == R .id .addon_android ?
137- ComponentConfig .RECOGNIZER_SERVICE_ANDROID_BUILTIN :
138- ComponentConfig .RECOGNIZER_SERVICE_ANDROID_BUILTIN
139- )
140- .setSynthesizerServiceType (() ->
141- ADDON_TYPE == R .id .addon_android ?
142- ComponentConfig .SYNTHESIZER_SERVICE_ANDROID_BUILTIN :
143- ComponentConfig .SYNTHESIZER_SERVICE_GOOGLE_BUILTIN )
142+ .setRecognizerServiceType (() -> {
143+ switch (ADDON_TYPE ) {
144+ case "Google" :
145+ return ComponentConfig .RECOGNIZER_SERVICE_GOOGLE ;
146+ default :
147+ return ComponentConfig .RECOGNIZER_SERVICE_ANDROID ;
148+ }
149+ })
150+ .setSynthesizerServiceType (() -> {
151+ switch (ADDON_TYPE ) {
152+ case "Google" :
153+ return ComponentConfig .SYNTHESIZER_SERVICE_GOOGLE ;
154+ default :
155+ return ComponentConfig .SYNTHESIZER_SERVICE_ANDROID ;
156+ }
157+ })
144158 .build ());
145159 component .setup (this , status -> {
146160 if (status .isAvailable ()) {
@@ -157,7 +171,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
157171 private void initActions () {
158172 add .setOnClickListener (v -> {
159173 String msg = text .getText ().toString ().trim ();
160- int itemPosition = spinner .getSelectedItemPosition ();
174+ int itemPosition = actionSpinner .getSelectedItemPosition ();
161175 if (msg .length () > 0 || itemPosition == LISTEN ) {
162176 queue .put (queue .size (), Pair .create (itemPosition , itemPosition == LISTEN ? null : msg ));
163177 representQueue (-1 );
@@ -191,7 +205,7 @@ private void representQueue(int index) {
191205 tx .append ("or \" <i>" ).append (text ).append ("</i>\" " );
192206 } else {
193207 String text = item .second ;
194- String label = (String ) adapter .getItem (item .first );
208+ String label = (String ) actionAdapter .getItem (item .first );
195209 if (i == index ) {
196210 if (text != null ) {
197211 text = "<font color=\" #FFFFFF\" >" + text + "</font>" ;
@@ -263,18 +277,27 @@ private void listen(int index) {
263277 }, (OnRecognizerError ) (i , i1 ) -> {
264278 Log .e (TAG , "Error " + i );
265279 Log .e (TAG , "Original Error " + getErrorString (i1 ));
266- runOnUiThread (() -> new AlertDialog .Builder (this )
267- .setTitle ("Error" )
268- .setMessage (getErrorString (i1 ))
269- .create ().show ());
280+
281+ synthesizer .releaseCurrentQueue ();
282+ if (synthesizer .isEmpty ()) {
283+ component .shutdown ();
284+ } else synthesizer .resume ();
285+
286+ // runOnUiThread(() -> new AlertDialog.Builder(this)
287+ // .setTitle("Error")
288+ // .setMessage(getErrorString(i1))
289+ // .create().show());
270290 });
271291 }
272292
273293 @ NonNull
274294 private String getErrorString (int i1 ) {
275- return ADDON_TYPE == R .id .addon_android ?
276- AndroidSpeechSynthesizer .getErrorType (i1 ) :
277- GoogleSpeechSynthesizer .getErrorType (i1 );
295+ switch (ADDON_TYPE ) {
296+ case "Google" :
297+ return GoogleSpeechSynthesizer .getErrorType (i1 );
298+ default :
299+ return AndroidSpeechSynthesizer .getErrorType (i1 );
300+ }
278301 }
279302
280303 private SparseArray <String > getChecks (SparseArray <String > news , int index ) {
@@ -295,7 +318,7 @@ private void readAll() {
295318 play (item .second , i );
296319 // TODO: Check! How it continues speaking after listening?
297320 } else if (i == 0 && item .first == LISTEN ) {
298- listen (i );
321+ listen (- 1 );
299322 }
300323 }
301324 });
@@ -305,6 +328,7 @@ private void readAll() {
305328 public void onDestroy () {
306329 super .onDestroy ();
307330 //UpdateManager.unregister();
331+ serialThread .shutdownNow ();
308332 component .shutdown ();
309333 }
310334
@@ -317,7 +341,8 @@ public void onResume() {
317341 private void initViews () {
318342 root = findViewById (R .id .root );
319343 execution = findViewById (R .id .execution );
320- spinner = findViewById (R .id .spinner );
344+ actionSpinner = findViewById (R .id .spinner );
345+ addonSpinner = findViewById (R .id .addon );
321346 text = findViewById (R .id .text );
322347 add = findViewById (R .id .add );
323348 clear = findViewById (R .id .clear );
@@ -339,14 +364,12 @@ private void initViews() {
339364 });
340365 });
341366 proceed .setEnabled (false );
342- // Create an ArrayAdapter using the string array and a default spinner layout
343- adapter = ArrayAdapter .createFromResource (this , R .array .actions , android .R .layout .simple_spinner_item );
344- // Specify the layout to use when the list of choices appears
345- adapter .setDropDownViewResource (android .R .layout .simple_spinner_dropdown_item );
346- // Apply the adapter to the spinner
347- spinner .setAdapter (adapter );
348- spinner .setSelection (0 );
349- spinner .setOnItemSelectedListener (new AdapterView .OnItemSelectedListener () {
367+ // Create an ArrayAdapter of the actions
368+ actionAdapter = ArrayAdapter .createFromResource (this , R .array .actions , android .R .layout .simple_spinner_item );
369+ actionAdapter .setDropDownViewResource (android .R .layout .simple_spinner_dropdown_item );
370+ actionSpinner .setAdapter (actionAdapter );
371+ actionSpinner .setSelection (0 );
372+ actionSpinner .setOnItemSelectedListener (new AdapterView .OnItemSelectedListener () {
350373 @ Override
351374 public void onItemSelected (AdapterView <?> parent , View view , int position , long id ) {
352375 boolean isDefault = position > 0 ;
@@ -356,6 +379,24 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
356379 if (position == LISTEN ) text .setVisibility (View .GONE );
357380 }
358381
382+ @ Override
383+ public void onNothingSelected (AdapterView <?> parent ) {
384+ }
385+ });
386+ // Create an ArrayAdapter of the addons
387+ List <String > addonList = Arrays .asList (addonMap .values ().toArray (new String [addonMap .size ()]));
388+ addonAdapter = new ArrayAdapter <>(this , android .R .layout .simple_spinner_item ,
389+ addonList );
390+ addonAdapter .setDropDownViewResource (android .R .layout .simple_spinner_dropdown_item );
391+ addonSpinner .setAdapter (addonAdapter );
392+ addonSpinner .setSelection (0 );
393+ addonSpinner .setOnItemSelectedListener (new AdapterView .OnItemSelectedListener () {
394+ @ Override
395+ public void onItemSelected (AdapterView <?> parent , View view , int position , long id ) {
396+ ADDON_TYPE = addonMap .get (position );
397+ setup ();
398+ }
399+
359400 @ Override
360401 public void onNothingSelected (AdapterView <?> parent ) {
361402 }
0 commit comments