]> 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 9a4e913b69927d986b1bfd2543d8a637d49de10e..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;
@@ -265,6 +264,10 @@ public class DictionaryActivity extends AppCompatActivity {
         ed.apply();
     }
 
+    private void addToSearchHistory() {
+        addToSearchHistory(searchView.getQuery().toString());
+    }
+
     private void addToSearchHistory(String text) {
         if (text == null || text.isEmpty()) return;
         int exists = searchHistory.indexOf(text);
@@ -617,7 +620,9 @@ public class DictionaryActivity extends AppCompatActivity {
 
             @Override
             public boolean onSuggestionClick(int position) {
-                setSearchText(searchHistory.get(position), true);
+                String h = searchHistory.get(position);
+                addToSearchHistory(h);
+                setSearchText(h, true);
                 return true;
             }
         });
@@ -652,6 +657,7 @@ public class DictionaryActivity extends AppCompatActivity {
         for (int i = savedHistory.size() - 1; i >= 0; i--) {
             addToSearchHistory(savedHistory.get(i));
         }
+        addToSearchHistory(text);
 
         setSearchText(text, true);
         Log.d(LOG, "Trying to restore searchText=" + text);
@@ -711,7 +717,7 @@ public class DictionaryActivity extends AppCompatActivity {
             @Override
             public boolean onQueryTextSubmit(String query) {
                 Log.d(LOG, "OnQueryTextListener: onQueryTextSubmit: " + searchView.getQuery());
-                addToSearchHistory(searchView.getQuery().toString());
+                addToSearchHistory();
                 hideKeyboard();
                 return true;
             }
@@ -725,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);
@@ -782,6 +788,7 @@ public class DictionaryActivity extends AppCompatActivity {
     @Override
     protected void onPause() {
         super.onPause();
+        addToSearchHistory();
         saveSearchHistory();
     }
 
@@ -846,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) {}
             }
         });
     }
@@ -872,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.");
@@ -896,6 +911,7 @@ public class DictionaryActivity extends AppCompatActivity {
             searchView.requestFocus();
         }
         if (searchView.getQuery().toString().length() > 0) {
+            addToSearchHistory();
             searchView.setQuery("", false);
         }
         showKeyboard();
@@ -1227,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;
                 }
             });
@@ -1244,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;
                 }
             });
@@ -1258,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;
                 }
             });
@@ -1297,6 +1310,7 @@ public class DictionaryActivity extends AppCompatActivity {
             @Override
             public void run() {
                 setIndexAndSearchText(actualIndexToUse, selectedText, true);
+                addToSearchHistory(selectedText);
             }
         }, 100);
     }
@@ -1426,7 +1440,6 @@ public class DictionaryActivity extends AppCompatActivity {
         if (hideKeyboard) {
             hideKeyboard();
         }
-        addToSearchHistory(text);
     }
 
     private void setSearchText(final String text, final boolean triggerSearch) {