Skip to content

Commit

Permalink
Fix voice service toast generation. (#872)
Browse files Browse the repository at this point in the history
This service runs in a background thread now, so UI updates need to be
explicitly scheduled in the main thread.

Closes #866

Signed-off-by: Danny Baumann <[email protected]>
  • Loading branch information
maniac103 authored and mueller-ma committed May 19, 2018
1 parent e919f31 commit fb543c2
Showing 1 changed file with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import android.app.IntentService;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.speech.RecognizerIntent;
import android.support.annotation.Nullable;
import android.util.Log;
Expand All @@ -32,6 +34,7 @@
*/
public class OpenHABVoiceService extends IntentService {
private static final String TAG = OpenHABVoiceService.class.getSimpleName();
private Handler mHandler = new Handler(Looper.getMainLooper());

public OpenHABVoiceService() {
super("OpenHABVoiceService");
Expand All @@ -56,9 +59,7 @@ protected void onHandleIntent(@Nullable Intent intent) {
if (connection != null) {
sendVoiceCommand(connection.getSyncHttpClient(), voiceCommand);
} else {
Toast.makeText(this,
R.string.error_couldnt_determine_openhab_url, Toast.LENGTH_SHORT)
.show();
showToast(getString(R.string.error_couldnt_determine_openhab_url));
}
}

Expand All @@ -69,8 +70,7 @@ private String extractVoiceCommand(Intent data) {
voiceCommand = textMatchList.get(0);
}
Log.i(TAG, "Recognized text: " + voiceCommand);
final String message = getString(R.string.info_voice_recognized_text, voiceCommand);
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
showToast(getString(R.string.info_voice_recognized_text, voiceCommand));
return voiceCommand;
}

Expand All @@ -90,4 +90,8 @@ private void sendVoiceCommand(final SyncHttpClient client, final String command)
Log.e(TAG, "Sending voice command failed", result.error);
}
}

private void showToast(CharSequence text) {
mHandler.post(() -> Toast.makeText(this, text, Toast.LENGTH_SHORT).show());
}
}

0 comments on commit fb543c2

Please sign in to comment.