Skip to content

Commit

Permalink
Implement search on type
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ispedals committed Sep 18, 2013
1 parent b5d61e4 commit 770898e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 770898e

Please sign in to comment.