X-Git-Url: http://gitweb.fperrin.net/?a=blobdiff_plain;f=src%2Fcom%2Fhughes%2Fandroid%2Fdictionary%2FDictionaryActivity.java;h=15efdbe06c6f65f8d6308979546bfd02bd23dba1;hb=75a1b9d9ca54bcb1dadf4f164adeea7894d1ee4c;hp=4e1bf6572d453b3d028c62778b10db5c33ab1474;hpb=81e7773623371b4fc2de7bee9a2ad8112dddb30a;p=Dictionary.git diff --git a/src/com/hughes/android/dictionary/DictionaryActivity.java b/src/com/hughes/android/dictionary/DictionaryActivity.java index 4e1bf65..15efdbe 100644 --- a/src/com/hughes/android/dictionary/DictionaryActivity.java +++ b/src/com/hughes/android/dictionary/DictionaryActivity.java @@ -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; @@ -152,8 +151,10 @@ public class DictionaryActivity extends AppCompatActivity { }); private SearchOperation currentSearchOperation = null; - private final int MAX_SEARCH_HISTORY = 10; - private final ArrayList searchHistory = new ArrayList<>(MAX_SEARCH_HISTORY); + private final int MAX_SEARCH_HISTORY = 100; + private final int DEFAULT_SEARCH_HISTORY = 10; + private int searchHistoryLimit; + private final ArrayList searchHistory = new ArrayList<>(DEFAULT_SEARCH_HISTORY); private MatrixCursor searchHistoryCursor = new MatrixCursor(new String[] {"_id", "search"}); private TextToSpeech textToSpeech; @@ -222,6 +223,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,11 +256,27 @@ 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)); + } + for (int i = searchHistory.size(); i <= MAX_SEARCH_HISTORY; i++) { + ed.remove("history" + i); + } + ed.apply(); + } + + private void addToSearchHistory() { + addToSearchHistory(searchView.getQuery().toString()); + } + private void addToSearchHistory(String text) { - if (text == null || text.isEmpty()) return; + if (text == null || text.isEmpty() || searchHistoryLimit == 0) return; int exists = searchHistory.indexOf(text); if (exists >= 0) searchHistory.remove(exists); - else if (searchHistory.size() >= MAX_SEARCH_HISTORY) searchHistory.remove(searchHistory.size() - 1); + else if (searchHistory.size() >= searchHistoryLimit) searchHistory.remove(searchHistory.size() - 1); searchHistory.add(0, text); searchHistoryCursor = new MatrixCursor(new String[] {"_id", "search"}); for (int i = 0; i < searchHistory.size(); i++) { @@ -567,6 +585,13 @@ public class DictionaryActivity extends AppCompatActivity { fontSizeSp = 14; } + final String searchHistoryLimitStr = prefs.getString(getString(R.string.historySizeKey), "" + DEFAULT_SEARCH_HISTORY); + try { + searchHistoryLimit = Math.min(Integer.parseInt(searchHistoryLimitStr.trim()), MAX_SEARCH_HISTORY); + } catch (NumberFormatException e) { + searchHistoryLimit = DEFAULT_SEARCH_HISTORY; + } + // ContextMenu. registerForContextMenu(getListView()); @@ -606,7 +631,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 +652,22 @@ public class DictionaryActivity extends AppCompatActivity { t.setText(c.getString(1)); } }); + // Set up search history + ArrayList 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 < searchHistoryLimit; 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 +728,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; } @@ -700,7 +742,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); @@ -754,6 +796,13 @@ public class DictionaryActivity extends AppCompatActivity { prefs.commit(); } + @Override + protected void onPause() { + super.onPause(); + addToSearchHistory(); + saveSearchHistory(); + } + @Override protected void onDestroy() { super.onDestroy(); @@ -815,7 +864,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) {} } }); } @@ -841,6 +893,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."); @@ -865,6 +922,7 @@ public class DictionaryActivity extends AppCompatActivity { searchView.requestFocus(); } if (searchView.getQuery().toString().length() > 0) { + addToSearchHistory(); searchView.setQuery("", false); } showKeyboard(); @@ -1196,8 +1254,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()); + speak(textToSpeak); return false; } }); @@ -1213,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()); + speak(text); return false; } }); @@ -1227,8 +1283,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()); + speak(text); return false; } }); @@ -1266,6 +1321,7 @@ public class DictionaryActivity extends AppCompatActivity { @Override public void run() { setIndexAndSearchText(actualIndexToUse, selectedText, true); + addToSearchHistory(selectedText); } }, 100); }