Skip to content

Commit

Permalink
Fix Voice Search crash on a fresh install (#1185)
Browse files Browse the repository at this point in the history
  • Loading branch information
MortimerGoro authored and Randall E. Barker committed May 9, 2019
1 parent e4acc4a commit d5a55ec
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,6 @@ private void initialize(Context aContext) {

mBackHandler = () -> onDismiss();

mVoiceSearchWidget = createChild(VoiceSearchWidget.class, false);
mVoiceSearchWidget.setPlacementForKeyboard(this.getHandle());
mVoiceSearchWidget.setDelegate(this); // VoiceSearchDelegate
mVoiceSearchWidget.setDelegate(() -> exitVoiceInputMode()); // DismissDelegate

mAutoCompletionView = findViewById(R.id.autoCompletionView);
mAutoCompletionView.setExtendedHeight((int)(mWidgetPlacement.height * mWidgetPlacement.density));
mAutoCompletionView.setDelegate(this);
Expand Down Expand Up @@ -664,6 +659,12 @@ private void handleVoiceInput() {
if (mIsInVoiceInput) {
return;
}
if (mVoiceSearchWidget == null) {
mVoiceSearchWidget = createChild(VoiceSearchWidget.class, false);
mVoiceSearchWidget.setPlacementForKeyboard(this.getHandle());
mVoiceSearchWidget.setDelegate(this); // VoiceSearchDelegate
mVoiceSearchWidget.setDelegate(() -> exitVoiceInputMode()); // DismissDelegate
}
mIsInVoiceInput = true;
TelemetryWrapper.voiceInputEvent();
mVoiceSearchWidget.show(false);
Expand Down Expand Up @@ -839,7 +840,7 @@ public void OnVoiceSearchError() {
}

private void exitVoiceInputMode() {
if (mIsInVoiceInput) {
if (mIsInVoiceInput && mVoiceSearchWidget != null) {
mVoiceSearchWidget.hide(REMOVE_WIDGET);
mWidgetPlacement.visible = true;
mWidgetManager.updateWidget(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ private void initialize(Context aContext) {
onDismiss();
});

mMozillaSpeechService.addListener(mVoiceSearchListener);
((Application)aContext.getApplicationContext()).registerActivityLifecycleCallbacks(this);
}

Expand Down Expand Up @@ -164,60 +165,60 @@ public void setPlacementForKeyboard(int aHandle) {
private ISpeechRecognitionListener mVoiceSearchListener = new ISpeechRecognitionListener() {

public void onSpeechStatusChanged(final MozillaSpeechService.SpeechState aState, final Object aPayload){
((Activity)getContext()).runOnUiThread(new Runnable() {
@Override
public void run() {
switch (aState) {
case DECODING:
// Handle when the speech object changes to decoding state
Log.d(LOGTAG, "===> DECODING");
setDecodingState();
break;
case MIC_ACTIVITY:
// Captures the activity from the microphone
Log.d(LOGTAG, "===> MIC_ACTIVITY");
double db = (double)aPayload * -1; // the higher the value, quieter the user/environment is
db = db == Double.POSITIVE_INFINITY ? MAX_DB : db;
int level = (int)(MAX_CLIPPING - (((db - MIN_DB) / (MAX_DB - MIN_DB)) * MAX_CLIPPING));
Log.d(LOGTAG, "===> db: " + db);
Log.d(LOGTAG, "===> level " + level);
mVoiceInputClipDrawable.setLevel(level);
break;
case STT_RESULT:
// When the api finished processing and returned a hypothesis
Log.d(LOGTAG, "===> STT_RESULT");
String transcription = ((STTResult)aPayload).mTranscription;
float confidence = ((STTResult)aPayload).mConfidence;
if (mDelegate != null)
mDelegate.OnVoiceSearchResult(transcription, confidence);
hide(REMOVE_WIDGET);
break;
case START_LISTEN:
// Handle when the api successfully opened the microphone and started listening
Log.d(LOGTAG, "===> START_LISTEN");
break;
case NO_VOICE:
// Handle when the api didn't detect any voice
Log.d(LOGTAG, "===> NO_VOICE");
setResultState();
break;
case CANCELED:
// Handle when a cancelation was fully executed
Log.d(LOGTAG, "===> CANCELED");
setResultState();
if (mDelegate != null)
mDelegate.OnVoiceSearchCanceled();
break;
case ERROR:
Log.d(LOGTAG, "===> ERROR: " + aPayload.toString());
setResultState();
// Handle when any error occurred
if (mDelegate != null)
mDelegate.OnVoiceSearchError();
break;
default:
break;
}
if (!mIsSpeechRecognitionRunning) {
return;
}
((Activity)getContext()).runOnUiThread(() -> {
switch (aState) {
case DECODING:
// Handle when the speech object changes to decoding state
Log.d(LOGTAG, "===> DECODING");
setDecodingState();
break;
case MIC_ACTIVITY:
// Captures the activity from the microphone
Log.d(LOGTAG, "===> MIC_ACTIVITY");
double db = (double)aPayload * -1; // the higher the value, quieter the user/environment is
db = db == Double.POSITIVE_INFINITY ? MAX_DB : db;
int level = (int)(MAX_CLIPPING - (((db - MIN_DB) / (MAX_DB - MIN_DB)) * MAX_CLIPPING));
Log.d(LOGTAG, "===> db: " + db);
Log.d(LOGTAG, "===> level " + level);
mVoiceInputClipDrawable.setLevel(level);
break;
case STT_RESULT:
// When the api finished processing and returned a hypothesis
Log.d(LOGTAG, "===> STT_RESULT");
String transcription = ((STTResult)aPayload).mTranscription;
float confidence = ((STTResult)aPayload).mConfidence;
if (mDelegate != null)
mDelegate.OnVoiceSearchResult(transcription, confidence);
hide(REMOVE_WIDGET);
break;
case START_LISTEN:
// Handle when the api successfully opened the microphone and started listening
Log.d(LOGTAG, "===> START_LISTEN");
break;
case NO_VOICE:
// Handle when the api didn't detect any voice
Log.d(LOGTAG, "===> NO_VOICE");
setResultState();
break;
case CANCELED:
// Handle when a cancelation was fully executed
Log.d(LOGTAG, "===> CANCELED");
setResultState();
if (mDelegate != null)
mDelegate.OnVoiceSearchCanceled();
break;
case ERROR:
Log.d(LOGTAG, "===> ERROR: " + aPayload.toString());
setResultState();
// Handle when any error occurred
if (mDelegate != null)
mDelegate.OnVoiceSearchError();
break;
default:
break;
}
});
}
Expand All @@ -231,15 +232,13 @@ public void startVoiceSearch() {
} else {
String language = SettingsStore.getInstance(getContext()).getVoiceSearchLanguage();
mMozillaSpeechService.setLanguage(language);
mMozillaSpeechService.addListener(mVoiceSearchListener);
mMozillaSpeechService.start(getContext().getApplicationContext());
mIsSpeechRecognitionRunning = true;
}
}

public void stopVoiceSearch() {
try {
mMozillaSpeechService.removeListener(mVoiceSearchListener);
mMozillaSpeechService.cancel();
mIsSpeechRecognitionRunning = false;

Expand Down Expand Up @@ -345,7 +344,6 @@ public void onActivityStarted(Activity activity) {
@Override
public void onActivityResumed(Activity activity) {
if (mWasSpeechRecognitionRunning) {
mMozillaSpeechService.addListener(mVoiceSearchListener);
startVoiceSearch();
}
}
Expand All @@ -354,7 +352,6 @@ public void onActivityResumed(Activity activity) {
public void onActivityPaused(Activity activity) {
mWasSpeechRecognitionRunning = mIsSpeechRecognitionRunning;
if (mIsSpeechRecognitionRunning) {
mMozillaSpeechService.removeListener(mVoiceSearchListener);
stopVoiceSearch();
}
}
Expand Down

0 comments on commit d5a55ec

Please sign in to comment.