55import android .media .AudioManager ;
66import android .os .Bundle ;
77import android .support .annotation .NonNull ;
8- import android .support .annotation .Nullable ;
9- import android .support .constraint .ConstraintLayout ;
108import android .support .v4 .app .ActivityCompat ;
119import android .util .Log ;
1210import android .util .Pair ;
2826import com .chattylabs .sdk .android .common .PermissionsHelper ;
2927import com .chattylabs .sdk .android .common .Tag ;
3028import com .chattylabs .sdk .android .common .ThreadUtils ;
29+ import com .chattylabs .sdk .android .voice .AndroidSpeechRecognizer ;
3130import com .chattylabs .sdk .android .voice .AndroidSpeechSynthesizer ;
32- import com .chattylabs .sdk .android .voice .ComponentConfig ;
3331import com .chattylabs .sdk .android .voice .ConversationalFlowComponent ;
32+ import com .chattylabs .sdk .android .voice .GoogleSpeechRecognizer ;
3433import com .chattylabs .sdk .android .voice .GoogleSpeechSynthesizer ;
3534import com .chattylabs .sdk .android .voice .Peripheral ;
3635import com .chattylabs .sdk .android .voice .TextFilterForUrl ;
5857public class MainActivity extends DaggerAppCompatActivity
5958 implements ActivityCompat .OnRequestPermissionsResultCallback {
6059
61- public static final String TAG = Tag .make (MainActivity .class );
60+ private static final String TAG = Tag .make (MainActivity .class );
6261
6362 // Constants
64- public static final int CHECK = 3 ;
65- public static final int LISTEN = 2 ;
66- public static final int READ = 1 ;
63+ private static final int CHECK = 3 ;
64+ private static final int LISTEN = 2 ;
65+ private static final int READ = 1 ;
66+
67+ private static final String ANDROID = "Android" ;
68+ private static final String GOOGLE = "Google" ;
69+ // ...
6770
6871 private static LinkedHashMap <Integer , String > addonMap = new LinkedHashMap <>();
6972 static {
70- addonMap .put (0 , "Android" );
71- addonMap .put (1 , "Google" );
73+ addonMap .put (0 , ANDROID );
74+ addonMap .put (1 , GOOGLE );
7275 }
7376
74- private static String ADDON_TYPE = "Android" ;
77+ private static String ADDON_TYPE = addonMap . get ( 0 ) ;
7578
7679 // Resources
77- private ConstraintLayout root ;
7880 private TextView execution ;
7981 private Spinner actionSpinner ;
8082 private Spinner addonSpinner ;
@@ -86,7 +88,6 @@ public class MainActivity extends DaggerAppCompatActivity
8688 private ArrayAdapter <CharSequence > actionAdapter ;
8789 private ArrayAdapter <String > addonAdapter ;
8890 private CheckBox scoCheck ;
89- private Menu menu ;
9091 private ThreadUtils .SerialThread serialThread ;
9192
9293 // Components
@@ -136,23 +137,22 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
136137 if (PermissionsHelper .isPermissionRequest (requestCode )) {
137138 if (PermissionsHelper .isPermissionGranted (grantResults )) {
138139 serialThread .addTask (() -> {
139- //component = ConversationalFlowModule.provideComponent();
140140 component .updateConfiguration (builder ->
141141 builder .setGoogleCredentialsResourceFile (() -> R .raw .credential )
142142 .setRecognizerServiceType (() -> {
143143 switch (ADDON_TYPE ) {
144- case "Google" :
145- return ComponentConfig . RECOGNIZER_SERVICE_GOOGLE ;
144+ case GOOGLE :
145+ return GoogleSpeechRecognizer . class ;
146146 default :
147- return ComponentConfig . RECOGNIZER_SERVICE_ANDROID ;
147+ return AndroidSpeechRecognizer . class ;
148148 }
149149 })
150150 .setSynthesizerServiceType (() -> {
151151 switch (ADDON_TYPE ) {
152- case "Google" :
153- return ComponentConfig . SYNTHESIZER_SERVICE_GOOGLE ;
152+ case GOOGLE :
153+ return GoogleSpeechSynthesizer . class ;
154154 default :
155- return ComponentConfig . SYNTHESIZER_SERVICE_ANDROID ;
155+ return AndroidSpeechSynthesizer . class ;
156156 }
157157 })
158158 .build ());
@@ -191,7 +191,6 @@ private void initActions() {
191191 });
192192 }
193193
194- @ Nullable
195194 private void representQueue (int index ) {
196195 StringBuilder tx = null ;
197196 boolean isChecking = false ;
@@ -293,7 +292,7 @@ private void listen(int index) {
293292 @ NonNull
294293 private String getErrorString (int i1 ) {
295294 switch (ADDON_TYPE ) {
296- case "Google" :
295+ case GOOGLE :
297296 return GoogleSpeechSynthesizer .getErrorType (i1 );
298297 default :
299298 return AndroidSpeechSynthesizer .getErrorType (i1 );
@@ -316,8 +315,8 @@ private void readAll() {
316315 Pair <Integer , String > item = queue .get (i );
317316 if (item .first == READ ) {
318317 play (item .second , i );
319- // TODO: Check! How it continues speaking after listening?
320318 } else if (i == 0 && item .first == LISTEN ) {
319+ // FIXME: Check why it continues speaking sometimes after listening
321320 listen (-1 );
322321 }
323322 }
@@ -339,7 +338,6 @@ public void onResume() {
339338 }
340339
341340 private void initViews () {
342- root = findViewById (R .id .root );
343341 execution = findViewById (R .id .execution );
344342 actionSpinner = findViewById (R .id .spinner );
345343 addonSpinner = findViewById (R .id .addon );
@@ -349,7 +347,7 @@ private void initViews() {
349347 proceed = findViewById (R .id .proceed );
350348 scoCheck = findViewById (R .id .bluetooth_sco );
351349
352- //
350+ // Check if there is a Bluetooth device connected and setup the config for a Sco connection
353351 scoCheck .setOnCheckedChangeListener ((buttonView , isChecked ) -> {
354352 if (isChecked && !peripheral .get (Peripheral .Type .BLUETOOTH ).isConnected ()) {
355353 buttonView .setChecked (false );
@@ -364,6 +362,7 @@ private void initViews() {
364362 });
365363 });
366364 proceed .setEnabled (false );
365+
367366 // Create an ArrayAdapter of the actions
368367 actionAdapter = ArrayAdapter .createFromResource (this , R .array .actions , android .R .layout .simple_spinner_item );
369368 actionAdapter .setDropDownViewResource (android .R .layout .simple_spinner_dropdown_item );
@@ -383,6 +382,7 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
383382 public void onNothingSelected (AdapterView <?> parent ) {
384383 }
385384 });
385+
386386 // Create an ArrayAdapter of the addons
387387 List <String > addonList = Arrays .asList (addonMap .values ().toArray (new String [addonMap .size ()]));
388388 addonAdapter = new ArrayAdapter <>(this , android .R .layout .simple_spinner_item ,
0 commit comments