From 97831485e7bbd71052d39bcd372d47d48b6ea480 Mon Sep 17 00:00:00 2001 From: Aga Wronska Date: Fri, 8 Jan 2016 17:29:24 -0800 Subject: Keep Search View visible after search completed Improve Search View behavior Add Search View background color Bug:26143355 Change-Id: Ifdc4bfff56fc6244c571dfbeff4179e1515d53b7 --- packages/DocumentsUI/res/menu/activity.xml | 10 ++- packages/DocumentsUI/res/values/colors.xml | 2 + .../src/com/android/documentsui/BaseActivity.java | 87 ++++++++++++++-------- .../documentsui/dirlist/DirectoryFragment.java | 1 - 4 files changed, 65 insertions(+), 35 deletions(-) diff --git a/packages/DocumentsUI/res/menu/activity.xml b/packages/DocumentsUI/res/menu/activity.xml index 7e0649be722d..a3cfde825f47 100644 --- a/packages/DocumentsUI/res/menu/activity.xml +++ b/packages/DocumentsUI/res/menu/activity.xml @@ -15,11 +15,19 @@ --> + #fffafafa #ffe0f2f1 + #ff676f74 + diff --git a/packages/DocumentsUI/src/com/android/documentsui/BaseActivity.java b/packages/DocumentsUI/src/com/android/documentsui/BaseActivity.java index 9c0a04ca751c..fe3430b23603 100644 --- a/packages/DocumentsUI/src/com/android/documentsui/BaseActivity.java +++ b/packages/DocumentsUI/src/com/android/documentsui/BaseActivity.java @@ -38,13 +38,15 @@ import android.provider.DocumentsContract; import android.provider.DocumentsContract.Root; import android.support.annotation.LayoutRes; import android.support.annotation.Nullable; +import android.text.TextUtils; import android.util.Log; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; -import android.view.MenuItem.OnActionExpandListener; import android.view.View; +import android.view.View.OnClickListener; +import android.view.View.OnFocusChangeListener; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; @@ -214,6 +216,7 @@ public abstract class BaseActivity extends Activity { case R.id.menu_advanced: case R.id.menu_file_size: case R.id.menu_new_window: + case R.id.menu_search: break; default: item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); @@ -315,6 +318,8 @@ public abstract class BaseActivity extends Activity { * the (abstract) directoryChanged method will be called. * @param anim */ + // TODO: Refactor the usage of the method - now it is called not only when the directory + // changed, but also to refresh the content of the directory while searching final void onCurrentDirectoryChanged(int anim) { mDirectoryContainer.setDrawDisappearingFirst(anim == ANIM_DOWN); onDirectoryChanged(anim); @@ -325,7 +330,11 @@ public abstract class BaseActivity extends Activity { } updateActionBar(); - invalidateOptionsMenu(); + + // Prevents searchView from being recreated while searching + if (!mSearchManager.isSearching()) { + invalidateOptionsMenu(); + } } final List getExcludedAuthorities() { @@ -682,7 +691,7 @@ public abstract class BaseActivity extends Activity { * Facade over the various search parts in the menu. */ final class SearchManager implements - SearchView.OnCloseListener, OnActionExpandListener, OnQueryTextListener, + SearchView.OnCloseListener, OnQueryTextListener, OnClickListener, OnFocusChangeListener, DocumentsToolBar.OnActionViewCollapsedListener { private boolean mSearchExpanded; @@ -700,9 +709,10 @@ public abstract class BaseActivity extends Activity { mView = (SearchView) mMenu.getActionView(); mActionBar.setOnActionViewCollapsedListener(this); - mMenu.setOnActionExpandListener(this); mView.setOnQueryTextListener(this); mView.setOnCloseListener(this); + mView.setOnSearchClickListener(this); + mView.setOnQueryTextFocusChangeListener(this); } /** @@ -755,19 +765,13 @@ public abstract class BaseActivity extends Activity { * search currently. */ boolean cancelSearch() { - boolean collapsed = false; - boolean closed = false; - - if (mActionBar.hasExpandedActionView()) { - mActionBar.collapseActionView(); - collapsed = true; - } - if (isExpanded() || isSearching()) { - onClose(); - closed = true; + // If the query string is not empty search view won't get iconified + mView.setQuery("", false); + mView.setIconified(true); + return true; } - return collapsed || closed; + return false; } boolean isSearching() { @@ -778,6 +782,11 @@ public abstract class BaseActivity extends Activity { return mSearchExpanded; } + /** + * Clears the search. + * @return True if the default behavior of clearing/dismissing SearchView should be + * overridden. False otherwise. + */ @Override public boolean onClose() { mSearchExpanded = false; @@ -786,33 +795,33 @@ public abstract class BaseActivity extends Activity { return false; } - mState.currentSearch = null; - onCurrentDirectoryChanged(ANIM_NONE); + mView.setBackgroundColor( + getResources().getColor(android.R.color.transparent, null)); + + // Refresh the directory if a search was done + if(mState.currentSearch != null) { + mState.currentSearch = null; + onCurrentDirectoryChanged(ANIM_NONE); + } + return false; } + /** + * Sets mSearchExpanded. + * Called when search icon is clicked to start search. + * Used to detect when the view expanded instead of onMenuItemActionExpand, because + * SearchView has showAsAction set to always and onMenuItemAction* methods are not called. + */ @Override - public boolean onMenuItemActionExpand(MenuItem item) { + public void onClick (View v) { mSearchExpanded = true; - updateActionBar(); - return true; - } - - @Override - public boolean onMenuItemActionCollapse(MenuItem item) { - mSearchExpanded = false; - if (mIgnoreNextCollapse) { - mIgnoreNextCollapse = false; - return true; - } - mState.currentSearch = null; - onCurrentDirectoryChanged(ANIM_NONE); - return true; + mView.setBackgroundColor( + getResources().getColor(R.color.menu_search_background, null)); } @Override public boolean onQueryTextSubmit(String query) { - mSearchExpanded = true; mState.currentSearch = query; mView.clearFocus(); onCurrentDirectoryChanged(ANIM_NONE); @@ -824,6 +833,18 @@ public abstract class BaseActivity extends Activity { return false; } + @Override + public void onFocusChange(View v, boolean hasFocus) { + if(!hasFocus) { + if(mState.currentSearch == null) { + mView.setIconified(true); + } + else if(TextUtils.isEmpty(mView.getQuery())) { + cancelSearch(); + } + } + } + @Override public void onActionViewCollapsed() { updateActionBar(); diff --git a/packages/DocumentsUI/src/com/android/documentsui/dirlist/DirectoryFragment.java b/packages/DocumentsUI/src/com/android/documentsui/dirlist/DirectoryFragment.java index 0d5e34e87ebb..c7716660c209 100644 --- a/packages/DocumentsUI/src/com/android/documentsui/dirlist/DirectoryFragment.java +++ b/packages/DocumentsUI/src/com/android/documentsui/dirlist/DirectoryFragment.java @@ -403,7 +403,6 @@ public class DirectoryFragment extends Fragment { state.derivedMode = result.mode; } state.derivedSortOrder = result.sortOrder; - ((BaseActivity) context).onStateChanged(); updateDisplayState(); -- cgit v1.2.3-59-g8ed1b