From bef96cc9cd499ac7cb392842f8423f4b71c2e015 Mon Sep 17 00:00:00 2001
From: mueller-ma <mueller-ma@users.noreply.github.com>
Date: Mon, 10 Sep 2018 20:58:34 +0200
Subject: [PATCH] Revert "Remove unneeded try catch in voice command
 funtionality (#899)" (#984)

* Revert "Remove unneeded try catch in voice command funtionality (#899)"

In some case the try catch is needed. See
https://console.firebase.google.com/project/openhab-mobile/crashlytics/app/android:org.openhab.habdroid/issues/5b755c156007d59fcdf12bc8
(only visible to the openHAB staff).
I updated the code to open the Google app in any app store. Some users
might use YALP store or any other custom Play Store (or Amazon app
store). For users without any additional app store the Play Store is opened.

Signed-off-by: mueller-ma <mueller-ma@users.noreply.github.com>

* Show toast if no app store is installed

Signed-off-by: mueller-ma <mueller-ma@users.noreply.github.com>
---
 .../openhab/habdroid/ui/OpenHABMainActivity.java   | 14 +++++++++++++-
 mobile/src/main/res/values/strings.xml             |  1 +
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/mobile/src/main/java/org/openhab/habdroid/ui/OpenHABMainActivity.java b/mobile/src/main/java/org/openhab/habdroid/ui/OpenHABMainActivity.java
index 201de691..c4ba053a 100644
--- a/mobile/src/main/java/org/openhab/habdroid/ui/OpenHABMainActivity.java
+++ b/mobile/src/main/java/org/openhab/habdroid/ui/OpenHABMainActivity.java
@@ -12,6 +12,7 @@ package org.openhab.habdroid.ui;
 import android.app.AlertDialog;
 import android.app.Dialog;
 import android.app.PendingIntent;
+import android.content.ActivityNotFoundException;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.DialogInterface;
@@ -59,6 +60,7 @@ import android.view.View;
 import android.view.ViewStub;
 import android.view.WindowManager;
 import android.widget.ProgressBar;
+import android.widget.Toast;
 
 import org.json.JSONArray;
 import org.json.JSONException;
@@ -1004,7 +1006,17 @@ public class OpenHABMainActivity extends AppCompatActivity implements
         speechIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
         speechIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, openhabPendingIntent);
 
-        startActivity(speechIntent);
+        try {
+            startActivity(speechIntent);
+        } catch (ActivityNotFoundException speechRecognizerNotFoundException) {
+            try {
+                startActivity(new Intent(Intent.ACTION_VIEW,
+                        Uri.parse("market://details?id=com.google.android.googlequicksearchbox")));
+            } catch (ActivityNotFoundException appStoreNotFoundException) {
+                Toast.makeText(this, R.string.error_no_app_store_found,
+                        Toast.LENGTH_LONG).show();
+            }
+        }
     }
 
     public void showRefreshHintSnackbarIfNeeded() {
diff --git a/mobile/src/main/res/values/strings.xml b/mobile/src/main/res/values/strings.xml
index 45a4ff8e..da2c1f82 100644
--- a/mobile/src/main/res/values/strings.xml
+++ b/mobile/src/main/res/values/strings.xml
@@ -103,6 +103,7 @@
     <string name="error_http_code_511">Network authentication is required (HTTP response code 511)</string>
     <string name="error_about_no_conn">Error while fetching openHAB server information</string>
     <string name="error_openhab_offline">Your openHAB server is offline while the cloud instance is running</string>
+    <string name="error_no_app_store_found">No app store found to install voice recognizer app</string>
     <string name="title_activity_openhabwritetag">Write NFC tag</string>
     <string name="title_activity_libraries">Used libraries</string>
     <string name="info_write_tag">Touch the tag and keep it close until the confirmation message appear</string>
-- 
GitLab