]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/DictionaryActivity.java
Make more robust by catching some exceptions.
[Dictionary.git] / src / com / hughes / android / dictionary / DictionaryActivity.java
index ef5a9ccc035104d40bb065a21a42fb9d7c45f548..840f2d89ba085b0169bac448323a4fe79a3f93e8 100644 (file)
@@ -28,7 +28,7 @@ import android.graphics.Typeface;
 import android.net.Uri;
 import android.os.Bundle;
 import android.os.Handler;
-import android.preference.PreferenceManager;
+import android.support.v7.preference.PreferenceManager;
 import android.speech.tts.TextToSpeech;
 import android.speech.tts.TextToSpeech.OnInitListener;
 import android.support.annotation.NonNull;
@@ -109,7 +109,6 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Date;
-import java.util.HashMap;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Locale;
@@ -732,7 +731,7 @@ public class DictionaryActivity extends AppCompatActivity {
         };
         searchView.setOnQueryTextListener(onQueryTextListener);
         searchView.setFocusable(true);
-        searchTextView = (AutoCompleteTextView)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
+        searchTextView = searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
         LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0,
                 FrameLayout.LayoutParams.WRAP_CONTENT, 1);
         customSearchView.addView(searchView, lp);
@@ -854,7 +853,10 @@ public class DictionaryActivity extends AppCompatActivity {
             @Override
             public void run() {
                 searchTextView.setThreshold(0);
-                searchTextView.showDropDown();
+                try {
+                    searchTextView.showDropDown();
+                // ignore any errors, in particular BadTokenException happens a lot
+                } catch (Exception e) {}
             }
         });
     }
@@ -880,6 +882,11 @@ public class DictionaryActivity extends AppCompatActivity {
         updateTTSLanguage(indexIndex);
     }
 
+    @SuppressWarnings("deprecation")
+    private void speak(String text) {
+        textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
+    }
+
     private void updateTTSLanguage(int i) {
         if (!ttsReady || index == null || textToSpeech == null) {
             Log.d(LOG, "Can't updateTTSLanguage.");
@@ -1236,8 +1243,7 @@ public class DictionaryActivity extends AppCompatActivity {
             speak.setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
                 @Override
                 public boolean onMenuItemClick(android.view.MenuItem item) {
-                    textToSpeech.speak(textToSpeak, TextToSpeech.QUEUE_FLUSH,
-                                       new HashMap<String, String>());
+                    speak(textToSpeak);
                     return false;
                 }
             });
@@ -1253,8 +1259,7 @@ public class DictionaryActivity extends AppCompatActivity {
                     String text = "";
                     for (Pair p : pairs) text += p.get(idx);
                     text = text.replaceAll("\\{[^{}]*\\}", "").replace("{", "").replace("}", "");
-                    textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH,
-                                       new HashMap<String, String>());
+                    speak(text);
                     return false;
                 }
             });
@@ -1267,8 +1272,7 @@ public class DictionaryActivity extends AppCompatActivity {
                     String text = "";
                     for (Pair p : pairs) text += p.get(idx);
                     text = text.replaceAll("\\{[^{}]*\\}", "").replace("{", "").replace("}", "");
-                    textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH,
-                                       new HashMap<String, String>());
+                    speak(text);
                     return false;
                 }
             });