Skip to content

Commit ac10d67

Browse files
committedMar 3, 2024
Do not defensively check if the recognizer component is present,
and fall back to the default recognizer if it is not. Instead, always just call SpeechRecognizer.createSpeechRecognizer and let it handle the error.
1 parent 6b7bb00 commit ac10d67

File tree

6 files changed

+30
-12
lines changed

6 files changed

+30
-12
lines changed
 

‎app/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ dependencies {
66
// TODO: used only in ChunkedWebRecSessionResult, use Android's JSON library instead
77
implementation 'com.googlecode.json-simple:json-simple:1.1'
88
implementation 'com.koushikdutta.async:androidasync:3.1.0'
9-
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.9.0'
9+
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.9.20'
1010
implementation 'androidx.appcompat:appcompat:1.6.1'
1111
implementation 'androidx.preference:preference-ktx:1.2.1'
1212
implementation 'androidx.recyclerview:recyclerview:1.3.2'

‎app/src/main/java/ee/ioc/phon/android/speak/ServiceLanguageChooser.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import ee.ioc.phon.android.speak.utils.Utils;
2121
import ee.ioc.phon.android.speechutils.Extras;
2222
import ee.ioc.phon.android.speechutils.RecognitionServiceManager;
23-
import ee.ioc.phon.android.speechutils.utils.IntentUtils;
2423
import ee.ioc.phon.android.speechutils.utils.PreferenceUtils;
2524

2625

@@ -87,14 +86,11 @@ public ServiceLanguageChooser(Context context, SharedPreferences prefs, int keys
8786
}
8887

8988
/**
90-
* If the stored recognizer component name does not refer to an existing service on the device then we use
91-
* the default service. This can happen if services get removed or renamed.
92-
* TODO: improve
89+
* Note that if the stored recognizer component name does not refer to an existing service on the device,
90+
* because it has been removed in the mean time, then SpeechRecognizer returns error 10
91+
* ("Bind to system recognition service failed with error 10").
9392
*/
9493
public SpeechRecognizer getSpeechRecognizer() {
95-
if (mRecognizerComponentName == null || !IntentUtils.isRecognitionAvailable(mContext, mRecognizerComponentName)) {
96-
return SpeechRecognizer.createSpeechRecognizer(mContext);
97-
}
9894
return SpeechRecognizer.createSpeechRecognizer(mContext, mRecognizerComponentName);
9995
}
10096

‎app/src/main/java/ee/ioc/phon/android/speak/view/SpeechInputView.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,23 @@ public void onEndOfSpeech() {
908908
* are generated by our implementation, others can be generated by the
909909
* framework, e.g. ERROR_CLIENT results from
910910
* "stopListening called with no preceding startListening".
911+
* <p>
912+
* Note: we currently cover error codes up to API level 8 + code 10 (from API level 31).
913+
* Code 10 ("too many requests"?) results from trying to use a non-existing recognizer
914+
* component.
915+
*
916+
* <ul>
917+
* <li>1 NETWORK_TIMEOUT
918+
* <li>2 NETWORK
919+
* <li>3 AUDIO
920+
* <li>4 SERVER
921+
* <li>5 CLIENT
922+
* <li>6 SPEECH TIMEOUT
923+
* <li>7 NO MATCH
924+
* <li>8 RECOGNIZER BUSY
925+
* <li>9 INSUFFICIENT PERMISSIONS
926+
* <li>10 TOO MANY REQUESTS
927+
* </ul>
911928
*
912929
* @param errorCode SpeechRecognizer error code
913930
*/
@@ -944,8 +961,11 @@ public void onError(final int errorCode) {
944961
case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
945962
setGuiInitState(R.string.errorImeResultSpeechTimeout);
946963
break;
964+
case SpeechRecognizer.ERROR_TOO_MANY_REQUESTS:
965+
setGuiInitState(R.string.errorImeResultTooManyRequests);
966+
break;
947967
default:
948-
Log.e("This might happen in future Android versions: code " + errorCode);
968+
Log.e("Error code > 10 shown as code 5: code " + errorCode);
949969
setGuiInitState(R.string.errorImeResultClientError);
950970
break;
951971
}

‎app/src/main/res/values-et/strings.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@
214214
<string name="errorFailedGetGrammarUrl">VIGA: grammatika aadressi valimine luhtus</string>
215215
<string name="errorFailedGetServerUrl">VIGA: serveri aadressi valimine luhtus</string>
216216

217-
// SpeechRecognizer error messages (9) shown in the IME
217+
// SpeechRecognizer error messages shown in the IME
218218
<string name="errorImeResultAudioError">lindistamine luhtus</string>
219219
<string name="errorImeResultNetworkError">serveriga puudub ühendus</string>
220220
<string name="errorImeResultNetworkTimeoutError">server ei tööta</string>
@@ -224,6 +224,7 @@
224224
<string name="errorImeResultSpeechTimeout">kõnet polnud kuulda</string>
225225
<string name="errorImeResultNoMatch">transkribeerimine luhtus</string>
226226
<string name="errorImeResultInsufficientPermissions">vajalikud õigused puuduvad</string>
227+
<string name="errorImeResultTooManyRequests">liiga palju päringuid</string>
227228

228229
<string name="errorResultAudioError">Lindistamine luhtus.
229230
Võibolla mõni teine rakendus parasjagu lindistab.</string>

‎app/src/main/res/values/strings.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@
218218
<string name="errorFailedLaunchApp">ERROR: App cannot be launched or is not installed (anymore): %1$s</string>
219219
<string name="errorFailedPlayAudio">ERROR: Failed to play the recorded audio: %1$s, %2$s</string>
220220

221-
// SpeechRecognizer error messages (9) shown in the IME
221+
// SpeechRecognizer error messages shown in the IME
222222
<string name="errorImeResultAudioError">audio recording failed</string>
223223
<string name="errorImeResultNetworkError">server not reachable</string>
224224
<string name="errorImeResultNetworkTimeoutError">network connection timeout</string>
@@ -228,6 +228,7 @@
228228
<string name="errorImeResultInsufficientPermissions">insufficient permissions</string>
229229
<string name="errorImeResultClientError">general app error</string>
230230
<string name="errorImeResultServerError">recognizer server error</string>
231+
<string name="errorImeResultTooManyRequests">too many requests</string>
231232

232233
<string name="errorResultAudioError">Audio recording failed. Maybe another app is currently recording.</string>
233234
<string name="errorResultNetworkError">Server not reachable. The internet connection is broken or the server is down.</string>

‎build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
}
66
dependencies {
77
classpath 'com.android.tools.build:gradle:8.3.0'
8-
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.0'
8+
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.20'
99
}
1010
}
1111

0 commit comments

Comments
 (0)
Failed to load comments.