SearchView keyboard opens and closes appropriately

- Keyboard opens when Search View opens
- Keyboard closes on query text submit
- Keyboard closes when user scrolls through results

Change-Id: I7cef383a2799eeb456fbaa07b8577c88cd35a997
Fixes: 34723251, 35164545
Test: SearchFragmentEspressoTest, manual test requested from QA
This commit is contained in:
Matthew Fritze
2017-01-31 17:33:25 -08:00
parent 007d120c47
commit 592e4d739b
3 changed files with 87 additions and 13 deletions

View File

@@ -30,6 +30,7 @@ import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout.LayoutParams;
import android.widget.SearchView;
@@ -48,6 +49,9 @@ public class SearchFragment extends InstrumentedFragment implements
{
private static final String TAG = "SearchFragment";
@VisibleForTesting
static final int SEARCH_TAG = "SearchViewTag".hashCode();
// State values
private static final String STATE_QUERY = "state_query";
private static final String STATE_NEVER_ENTERED_QUERY = "state_never_entered_query";
@@ -80,12 +84,23 @@ public class SearchFragment extends InstrumentedFragment implements
@VisibleForTesting (otherwise = VisibleForTesting.PRIVATE)
SearchFeatureProvider mSearchFeatureProvider;
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
SearchResultsAdapter mSearchAdapter;
private SearchResultsAdapter mSearchAdapter;
private RecyclerView mResultsRecyclerView;
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
RecyclerView mResultsRecyclerView;
private SearchView mSearchView;
@VisibleForTesting
final RecyclerView.OnScrollListener mScrollListener =
new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (dy != 0) {
hideKeyboard();
}
}
};
@Override
public int getMetricsCategory() {
return MetricsProto.MetricsEvent.DASHBOARD_SEARCH_RESULTS;
@@ -122,6 +137,7 @@ public class SearchFragment extends InstrumentedFragment implements
actionBar.setCustomView(mSearchView);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
mSearchView.requestFocus();
// Run the Index update only if we have some space
if (!Utils.isLowStorage(activity)) {
@@ -138,6 +154,7 @@ public class SearchFragment extends InstrumentedFragment implements
mResultsRecyclerView = (RecyclerView) view.findViewById(R.id.list_results);
mResultsRecyclerView.setAdapter(mSearchAdapter);
mResultsRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mResultsRecyclerView.addOnScrollListener(mScrollListener);
return view;
}
@@ -189,7 +206,7 @@ public class SearchFragment extends InstrumentedFragment implements
// Save submitted query.
getLoaderManager().restartLoader(SaveQueryRecorderCallback.LOADER_ID_SAVE_QUERY_TASK, null,
mSaveQueryRecorderCallback);
hideKeyboard();
return true;
}
@@ -240,17 +257,33 @@ public class SearchFragment extends InstrumentedFragment implements
loaderManager.restartLoader(LOADER_ID_INSTALLED_APPS, null /* args */, this /* callback */);
}
private SearchView makeSearchView(ActionBar actionBar, String query) {
@VisibleForTesting (otherwise = VisibleForTesting.PRIVATE)
SearchView makeSearchView(ActionBar actionBar, String query) {
final SearchView searchView = new SearchView(actionBar.getThemedContext());
searchView.setIconifiedByDefault(false);
searchView.setQuery(query, false /* submitQuery */);
searchView.setOnQueryTextListener(this);
searchView.setTag(SEARCH_TAG, searchView);
final LayoutParams lp =
new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
searchView.setLayoutParams(lp);
return searchView;
}
private void hideKeyboard() {
final Activity activity = getActivity();
if (activity != null) {
View view = activity.getCurrentFocus();
InputMethodManager imm = (InputMethodManager)
activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
if (mResultsRecyclerView != null) {
mResultsRecyclerView.requestFocus();
}
}
private class SaveQueryRecorderCallback implements LoaderManager.LoaderCallbacks<Void> {
// TODO: make a generic background task manager to handle one-off tasks like this one.