From 770898e6e68dfb78f3313fe76a82e48aafdc8d6b Mon Sep 17 00:00:00 2001 From: ispedals Date: Sun, 15 Sep 2013 09:31:16 -0400 Subject: [PATCH] Implement search on type This adds a onQueryTextChangeListener to the searchview and manually calls DictionarySearcherActivity. This adds a new intent flag, SUBMITTED, which allows DictionarySearcherActivity to differentiate between calls that were submitted by search on type, and those through the normal submitting process. This differentiation is necessary because we change the query text if it is submitted so that we can record what the voice query returned. If we did not, we would change the query text during search on type, which breaks IME composition. --- .../FloatingJapaneseDictionaryService.java | 35 +++++++++++++++++++ .../DictionarySearcherActivity.java | 12 ++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/pedals/is/floatingjapanesedictionary/FloatingJapaneseDictionaryService.java b/src/pedals/is/floatingjapanesedictionary/FloatingJapaneseDictionaryService.java index c3e4a79..0ac858a 100644 --- a/src/pedals/is/floatingjapanesedictionary/FloatingJapaneseDictionaryService.java +++ b/src/pedals/is/floatingjapanesedictionary/FloatingJapaneseDictionaryService.java @@ -25,6 +25,7 @@ import pedals.is.floatingjapanesedictionary.dictionarysearcher.DictionaryEntries; import pedals.is.floatingjapanesedictionary.dictionarysearcher.DictionaryEntry; +import pedals.is.floatingjapanesedictionary.dictionarysearcher.DictionarySearcherActivity; import pedals.is.floatingjapanesedictionary.downloader.DictionaryManagerService; import android.annotation.TargetApi; import android.app.SearchManager; @@ -51,6 +52,7 @@ public class FloatingJapaneseDictionaryService extends StandOutWindow { public static final int DISPLAY_TEXT = 0, DISPLAY_DEFINITION = 1, DISPLAY_SEARCH = 2; + public static final String SUBMITTED = "SUBMITTED"; private static final String APP_NAME = "Floating Japanese Dictionary"; private static final int APP_ICON = android.R.drawable.ic_menu_add; @@ -123,6 +125,39 @@ public boolean onClose() { } }); + searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { + + @Override + public boolean onQueryTextSubmit(String query) { + + return false; + } + + @Override + public boolean onQueryTextChange(String query) { + + Intent intent = new Intent( + FloatingJapaneseDictionaryService.this, + DictionarySearcherActivity.class); + intent.setAction(Intent.ACTION_SEARCH); + intent.putExtra(SearchManager.QUERY, query); + intent.putExtra(SUBMITTED, false); + /* + * SearchView itself launches the activity with + * FLAG_ACTIVITY_NEW_TASK. + * + * Because multiple instances of the activity will be fired + * rapidly, FLAG_ACTIVITY_CLEAR_TOP will either start the + * activity, or kill the activity if it is running and start it + * again. + */ + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP + | Intent.FLAG_ACTIVITY_NEW_TASK); + startActivity(intent); + return true; + } + }); + searchView.setOnSearchClickListener(new View.OnClickListener() { @Override diff --git a/src/pedals/is/floatingjapanesedictionary/dictionarysearcher/DictionarySearcherActivity.java b/src/pedals/is/floatingjapanesedictionary/dictionarysearcher/DictionarySearcherActivity.java index 2be4f97..b3cca1b 100644 --- a/src/pedals/is/floatingjapanesedictionary/dictionarysearcher/DictionarySearcherActivity.java +++ b/src/pedals/is/floatingjapanesedictionary/dictionarysearcher/DictionarySearcherActivity.java @@ -35,7 +35,17 @@ public void onCreate(Bundle savedInstanceState) { Intent intent = getIntent(); if (Intent.ACTION_SEARCH.equals(intent.getAction())) { String query = intent.getStringExtra(SearchManager.QUERY); - displaySearch(query); + + /* + * If the flag isn't set, it means that we can freely update the + * textfield without worrying about disturbing search on type ime + * composition. We need to update the textfield when a voice search + * occurs so that the user knows what the query was recognized as. + */ + if (intent.getBooleanExtra( + FloatingJapaneseDictionaryService.SUBMITTED, true)) { + displaySearch(query); + } DictionaryEntries result = doQuery(query); if (result.isEmpty()) { displayText("No results");