X-Git-Url: http://gitweb.fperrin.net/?a=blobdiff_plain;f=src%2Fcom%2Fhughes%2Fandroid%2Fdictionary%2FDictionaryActivity.java;h=4665192120772d7ccb4851c54e0fc30aa4c0ab8d;hb=c804b0668d65dc901a248d358672826fd4da5fac;hp=8590d90fbf7f13103895f305d61ba2f67d3a6e35;hpb=6075de7ee940e23133cf187f44a525e19cccf038;p=Dictionary.git diff --git a/src/com/hughes/android/dictionary/DictionaryActivity.java b/src/com/hughes/android/dictionary/DictionaryActivity.java index 8590d90..4665192 100644 --- a/src/com/hughes/android/dictionary/DictionaryActivity.java +++ b/src/com/hughes/android/dictionary/DictionaryActivity.java @@ -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; @@ -261,7 +262,9 @@ public class DictionaryActivity extends AppCompatActivity { for (int i = 0; i < searchHistory.size(); i++) { ed.putString("history" + i, searchHistory.get(i)); } - ed.remove("history" + searchHistory.size()); + for (int i = searchHistory.size(); i <= MAX_SEARCH_HISTORY; i++) { + ed.remove("history" + i); + } ed.apply(); } @@ -270,10 +273,10 @@ public class DictionaryActivity extends AppCompatActivity { } 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++) { @@ -582,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()); @@ -649,7 +659,7 @@ public class DictionaryActivity extends AppCompatActivity { if (savedHistory != null && !savedHistory.isEmpty()) { } else { savedHistory = new ArrayList<>(); - for (int i = 0; i < MAX_SEARCH_HISTORY; i++) { + for (int i = 0; i < searchHistoryLimit; i++) { String h = prefs.getString("history" + i, null); if (h == null) break; savedHistory.add(h); @@ -732,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); @@ -854,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) {} } }); } @@ -1570,11 +1583,11 @@ public class DictionaryActivity extends AppCompatActivity { else if (typeface == Typeface.SANS_SERIF) { style = "font-family: sans-serif;"; } else if (typeface == Typeface.MONOSPACE) { style = "font-family: monospace;"; } if (application.getSelectedTheme() == DictionaryApplication.Theme.DEFAULT) - style += "background-color: black; color: white;"; + style += "body { background-color: black; color: white; } a { color: #00aaff; }"; // Log.d(LOG, "html=" + html); startActivityForResult( HtmlDisplayActivity.getHtmlIntent(getApplicationContext(), String.format( - "%s", style, html), + "%s", style, html), htmlTextToHighlight, false), 0); }