From fb543c2437170b12a6419989e4f4d10720d5f168 Mon Sep 17 00:00:00 2001 From: maniac103 Date: Sat, 19 May 2018 10:09:22 +0200 Subject: [PATCH] Fix voice service toast generation. (#872) 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 --- .../openhab/habdroid/core/OpenHABVoiceService.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/mobile/src/main/java/org/openhab/habdroid/core/OpenHABVoiceService.java b/mobile/src/main/java/org/openhab/habdroid/core/OpenHABVoiceService.java index 73f15595aa..afcd2f021b 100644 --- a/mobile/src/main/java/org/openhab/habdroid/core/OpenHABVoiceService.java +++ b/mobile/src/main/java/org/openhab/habdroid/core/OpenHABVoiceService.java @@ -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; @@ -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"); @@ -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)); } } @@ -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; } @@ -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()); + } }