]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/DictionaryActivity.java
Switch to app compat preferences.
[Dictionary.git] / src / com / hughes / android / dictionary / DictionaryActivity.java
index 4e1bf6572d453b3d028c62778b10db5c33ab1474..e2558a0773605615ff629cb7aab682a9605a2de3 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;
@@ -222,6 +222,7 @@ public class DictionaryActivity extends AppCompatActivity {
         Log.d(LOG, "onSaveInstanceState: " + searchView.getQuery().toString());
         outState.putString(C.INDEX_SHORT_NAME, index.shortName);
         outState.putString(C.SEARCH_TOKEN, searchView.getQuery().toString());
+        outState.putStringArrayList(C.SEARCH_HISTORY, searchHistory);
     }
 
     private int getMatchLen(String search, Index.IndexEntry e) {
@@ -254,6 +255,20 @@ public class DictionaryActivity extends AppCompatActivity {
         finish();
     }
 
+    private void saveSearchHistory() {
+        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
+        final SharedPreferences.Editor ed = prefs.edit();
+        for (int i = 0; i < searchHistory.size(); i++) {
+            ed.putString("history" + i, searchHistory.get(i));
+        }
+        ed.remove("history" + searchHistory.size());
+        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);
@@ -606,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;
             }
         });
@@ -625,7 +642,22 @@ public class DictionaryActivity extends AppCompatActivity {
                 t.setText(c.getString(1));
             }
         });
+
         // Set up search history
+        ArrayList<String> savedHistory = null;
+        if (savedInstanceState != null) savedHistory = savedInstanceState.getStringArrayList(C.SEARCH_HISTORY);
+        if (savedHistory != null && !savedHistory.isEmpty()) {
+        } else {
+            savedHistory = new ArrayList<>();
+            for (int i = 0; i < MAX_SEARCH_HISTORY; i++) {
+                String h = prefs.getString("history" + i, null);
+                if (h == null) break;
+                savedHistory.add(h);
+            }
+        }
+        for (int i = savedHistory.size() - 1; i >= 0; i--) {
+            addToSearchHistory(savedHistory.get(i));
+        }
         addToSearchHistory(text);
 
         setSearchText(text, true);
@@ -686,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;
             }
@@ -754,6 +786,13 @@ public class DictionaryActivity extends AppCompatActivity {
         prefs.commit();
     }
 
+    @Override
+    protected void onPause() {
+        super.onPause();
+        addToSearchHistory();
+        saveSearchHistory();
+    }
+
     @Override
     protected void onDestroy() {
         super.onDestroy();
@@ -865,6 +904,7 @@ public class DictionaryActivity extends AppCompatActivity {
             searchView.requestFocus();
         }
         if (searchView.getQuery().toString().length() > 0) {
+            addToSearchHistory();
             searchView.setQuery("", false);
         }
         showKeyboard();
@@ -1266,6 +1306,7 @@ public class DictionaryActivity extends AppCompatActivity {
             @Override
             public void run() {
                 setIndexAndSearchText(actualIndexToUse, selectedText, true);
+                addToSearchHistory(selectedText);
             }
         }, 100);
     }