]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/DictionaryActivity.java
Avoid deprecation warnings.
[Dictionary.git] / src / com / hughes / android / dictionary / DictionaryActivity.java
index 9a4e913b69927d986b1bfd2543d8a637d49de10e..8590d90fbf7f13103895f305d61ba2f67d3a6e35 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;
@@ -265,6 +265,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 +621,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 +658,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 +718,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;
             }
@@ -782,6 +789,7 @@ public class DictionaryActivity extends AppCompatActivity {
     @Override
     protected void onPause() {
         super.onPause();
+        addToSearchHistory();
         saveSearchHistory();
     }
 
@@ -872,6 +880,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 +909,7 @@ public class DictionaryActivity extends AppCompatActivity {
             searchView.requestFocus();
         }
         if (searchView.getQuery().toString().length() > 0) {
+            addToSearchHistory();
             searchView.setQuery("", false);
         }
         showKeyboard();
@@ -1227,8 +1241,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 +1257,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 +1270,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 +1308,7 @@ public class DictionaryActivity extends AppCompatActivity {
             @Override
             public void run() {
                 setIndexAndSearchText(actualIndexToUse, selectedText, true);
+                addToSearchHistory(selectedText);
             }
         }, 100);
     }
@@ -1426,7 +1438,6 @@ public class DictionaryActivity extends AppCompatActivity {
         if (hideKeyboard) {
             hideKeyboard();
         }
-        addToSearchHistory(text);
     }
 
     private void setSearchText(final String text, final boolean triggerSearch) {