Skip to content

Commit 3f73353

Browse files
committed
Fixing Google Speech recognizer
1 parent a37c8a9 commit 3f73353

File tree

22 files changed

+752
-422
lines changed

22 files changed

+752
-422
lines changed

app/src/main/java/com/chattylabs/demo/voice/MainActivity.java

Lines changed: 87 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.chattylabs.demo.voice;
22

33
import android.annotation.SuppressLint;
4-
import android.app.AlertDialog;
54
import android.content.pm.PackageManager;
65
import android.media.AudioManager;
76
import android.os.Bundle;
@@ -30,19 +29,30 @@
3029
import com.chattylabs.sdk.android.common.Tag;
3130
import com.chattylabs.sdk.android.common.ThreadUtils;
3231
import com.chattylabs.sdk.android.voice.AndroidSpeechSynthesizer;
32+
import com.chattylabs.sdk.android.voice.ComponentConfig;
3333
import com.chattylabs.sdk.android.voice.ConversationalFlowComponent;
3434
import com.chattylabs.sdk.android.voice.GoogleSpeechSynthesizer;
3535
import com.chattylabs.sdk.android.voice.Peripheral;
3636
import 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

3942
import javax.inject.Inject;
4043

4144
import 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;
4452
import static com.chattylabs.sdk.android.voice.ConversationalFlowComponent.SpeechRecognizer;
4553
import 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

4858
public 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
}

app/src/main/res/layout/activity_main.xml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
android:layout_width="wrap_content"
1414
android:layout_height="wrap_content"
1515
android:orientation="horizontal"
16-
app:layout_constraintGuide_percent="0.55" />
16+
app:layout_constraintGuide_percent="0.35" />
1717

1818
<TextView
1919
android:id="@+id/execution"
@@ -29,6 +29,18 @@
2929
app:layout_constraintStart_toStartOf="parent"
3030
app:layout_constraintTop_toTopOf="parent" />
3131

32+
<Spinner
33+
android:id="@+id/addon"
34+
android:layout_width="wrap_content"
35+
android:layout_height="wrap_content"
36+
android:layout_marginEnd="8dp"
37+
android:layout_marginStart="8dp"
38+
android:layout_marginTop="8dp"
39+
android:minWidth="100dp"
40+
app:layout_constraintEnd_toEndOf="@+id/barrier2"
41+
app:layout_constraintStart_toStartOf="parent"
42+
app:layout_constraintTop_toBottomOf="@+id/guideline" />
43+
3244
<CheckBox
3345
android:id="@+id/bluetooth_sco"
3446
android:layout_width="wrap_content"
@@ -39,7 +51,7 @@
3951
app:layout_constraintEnd_toEndOf="@+id/barrier2"
4052
app:layout_constraintHorizontal_bias="0.0"
4153
app:layout_constraintStart_toStartOf="parent"
42-
app:layout_constraintTop_toBottomOf="@+id/guideline" />
54+
app:layout_constraintTop_toBottomOf="@+id/addon" />
4355

4456
<EditText
4557
android:id="@+id/text"

app/src/main/res/menu/addons.xml

Lines changed: 0 additions & 9 deletions
This file was deleted.

app/src/main/res/menu/demo.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<menu xmlns:android="http://schemas.android.com/apk/res/android">
3+
<item android:id="@+id/demo_conversation"
4+
android:title="Conversation"/>
5+
<item android:id="@+id/demo_components"
6+
android:title="Components"/>
7+
</menu>

app/src/main/res/values/arrays.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66
<item>Listen to any message</item>
77
<item>Check for&#8230;</item>
88
</string-array>
9+
<string-array name="addons">
10+
<item>Android</item>
11+
<item>Google</item>
12+
</string-array>
913
</resources>

sdk-addon-android-speech/src/main/java/com/chattylabs/sdk/android/voice/AndroidSpeechRecognitionAdapter.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,22 +85,24 @@ public AndroidSpeechRecognitionAdapter _setOnError(OnRecognizerError onError) {
8585
return this;
8686
}
8787

88-
public AndroidSpeechRecognitionAdapter setNoSoundThreshold(float noSoundThreshold) {
89-
this.noSoundThreshold = noSoundThreshold;
90-
return this;
91-
}
92-
93-
public AndroidSpeechRecognitionAdapter setLowSoundThreshold(float lowSoundThreshold) {
94-
this.lowSoundThreshold = lowSoundThreshold;
88+
@Override
89+
public AndroidSpeechRecognitionAdapter setTryAgain(boolean tryAgain) {
90+
this.tryAgain = tryAgain;
9591
return this;
9692
}
9793

94+
@Override
9895
public boolean isTryAgain() {
9996
return tryAgain;
10097
}
10198

102-
public AndroidSpeechRecognitionAdapter setTryAgain(boolean tryAgain) {
103-
this.tryAgain = tryAgain;
99+
public AndroidSpeechRecognitionAdapter setNoSoundThreshold(float noSoundThreshold) {
100+
this.noSoundThreshold = noSoundThreshold;
101+
return this;
102+
}
103+
104+
public AndroidSpeechRecognitionAdapter setLowSoundThreshold(float lowSoundThreshold) {
105+
this.lowSoundThreshold = lowSoundThreshold;
104106
return this;
105107
}
106108

0 commit comments

Comments
 (0)