Skip to content

Commit

Permalink
Recreate SpeechRecognizer every time the mic button is pressed (fixes #…
Browse files Browse the repository at this point in the history
…115)

+ Update dependencies
  • Loading branch information
Kaljurand committed Mar 2, 2024
1 parent 4b294cd commit 6b7bb00
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public class ServiceLanguageChooser {
private final String mAppId;
private final int mKeyCurrentCombo;
private int mIndex;
private SpeechRecognizer mSpeechRecognizer;
private Intent mIntent;
private String mLanguage = null;
private ComponentName mRecognizerComponentName = null;
Expand Down Expand Up @@ -87,9 +86,16 @@ public ServiceLanguageChooser(Context context, SharedPreferences prefs, int keys
update();
}


/**
* If the stored recognizer component name does not refer to an existing service on the device then we use
* the default service. This can happen if services get removed or renamed.
* TODO: improve
*/
public SpeechRecognizer getSpeechRecognizer() {
return mSpeechRecognizer;
if (mRecognizerComponentName == null || !IntentUtils.isRecognitionAvailable(mContext, mRecognizerComponentName)) {
return SpeechRecognizer.createSpeechRecognizer(mContext);
}
return SpeechRecognizer.createSpeechRecognizer(mContext, mRecognizerComponentName);
}

public int size() {
Expand Down Expand Up @@ -139,10 +145,12 @@ public String getCombo() {
return mCombosAsList.get(mIndex);
}

// TODO: can return null, but some callers expect non-null
public String getLanguage() {
return mLanguage;
}

// TODO: can return null, but some callers expect non-null
public ComponentName getService() {
return mRecognizerComponentName;
}
Expand All @@ -156,14 +164,6 @@ private void update() {
language = splits[1];
}

// If the stored combo name does not refer to an existing service on the device then we use
// the default service. This can happen if services get removed or renamed.
if (mRecognizerComponentName == null || !IntentUtils.isRecognitionAvailable(mContext, mRecognizerComponentName)) {
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(mContext);
} else {
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(mContext, mRecognizerComponentName);
}

// TODO: support other actions
mIntent = Utils.getRecognizerIntent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH, mCallerInfo, language);
mLanguage = language;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ private void showMessageArrow(int numOfChars, String dash) {
}

private static String selectFirstResult(List<String> results) {
if (results == null || results.size() < 1) {
if (results == null || results.isEmpty()) {
return null;
}
return results.get(0);
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:8.2.2'
classpath 'com.android.tools.build:gradle:8.3.0'
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.0'
}
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip

0 comments on commit 6b7bb00

Please sign in to comment.