Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.documentsui; |
| 18 | |
Ben Kwa | 543a292 | 2016-03-22 11:11:46 -0700 | [diff] [blame^] | 19 | import static com.android.documentsui.Shared.DEBUG; |
| 20 | |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 21 | import android.annotation.Nullable; |
| 22 | import android.os.Bundle; |
Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 23 | import android.provider.DocumentsContract.Root; |
| 24 | import android.text.TextUtils; |
| 25 | import android.util.Log; |
| 26 | import android.view.MenuItem; |
| 27 | import android.view.View; |
| 28 | import android.view.View.OnClickListener; |
| 29 | import android.view.View.OnFocusChangeListener; |
| 30 | import android.widget.SearchView; |
| 31 | import android.widget.SearchView.OnQueryTextListener; |
| 32 | |
| 33 | import com.android.documentsui.model.RootInfo; |
| 34 | |
| 35 | /** |
| 36 | * Manages searching UI behavior. |
| 37 | */ |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 38 | final class SearchViewManager implements |
Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 39 | SearchView.OnCloseListener, OnQueryTextListener, OnClickListener, OnFocusChangeListener { |
| 40 | |
| 41 | public interface SearchManagerListener { |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 42 | void onSearchChanged(@Nullable String query); |
Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | public static final String TAG = "SearchManger"; |
| 46 | |
| 47 | private SearchManagerListener mListener; |
Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 48 | private boolean mSearchExpanded; |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 49 | private String mCurrentSearch; |
Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 50 | private boolean mIgnoreNextClose; |
| 51 | |
Steve McKay | 18d01e8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 52 | private DocumentsToolbar mActionBar; |
Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 53 | private MenuItem mMenu; |
| 54 | private SearchView mView; |
| 55 | |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 56 | public SearchViewManager(SearchManagerListener listener, @Nullable Bundle savedState) { |
Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 57 | mListener = listener; |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 58 | mCurrentSearch = savedState != null ? savedState.getString(Shared.EXTRA_QUERY) : null; |
Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | public void setSearchMangerListener(SearchManagerListener listener) { |
| 62 | mListener = listener; |
| 63 | } |
| 64 | |
Steve McKay | 18d01e8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 65 | public void install(DocumentsToolbar actionBar) { |
Steve McKay | 0af8afd | 2016-02-25 13:34:03 -0800 | [diff] [blame] | 66 | // assert(mActionBar == null); |
| 67 | |
Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 68 | mActionBar = actionBar; |
| 69 | mMenu = actionBar.getSearchMenu(); |
| 70 | mView = (SearchView) mMenu.getActionView(); |
| 71 | |
| 72 | mView.setOnQueryTextListener(this); |
| 73 | mView.setOnCloseListener(this); |
| 74 | mView.setOnSearchClickListener(this); |
| 75 | mView.setOnQueryTextFocusChangeListener(this); |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 76 | |
| 77 | restoreSearch(); |
Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @param root Info about the current directory. |
| 82 | */ |
| 83 | void update(RootInfo root) { |
| 84 | if (mMenu == null) { |
Ben Kwa | 543a292 | 2016-03-22 11:11:46 -0700 | [diff] [blame^] | 85 | if (DEBUG) Log.d(TAG, "update called before Search MenuItem installed."); |
Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 86 | return; |
| 87 | } |
| 88 | |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 89 | if (mCurrentSearch != null) { |
Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 90 | mMenu.expandActionView(); |
| 91 | |
| 92 | mView.setIconified(false); |
| 93 | mView.clearFocus(); |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 94 | mView.setQuery(mCurrentSearch, false); |
Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 95 | } else { |
| 96 | mView.clearFocus(); |
| 97 | if (!mView.isIconified()) { |
| 98 | mIgnoreNextClose = true; |
| 99 | mView.setIconified(true); |
| 100 | } |
| 101 | |
| 102 | if (mMenu.isActionViewExpanded()) { |
| 103 | mMenu.collapseActionView(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | showMenu(root != null |
| 108 | && ((root.flags & Root.FLAG_SUPPORTS_SEARCH) != 0)); |
| 109 | } |
| 110 | |
| 111 | void showMenu(boolean visible) { |
| 112 | if (mMenu == null) { |
Ben Kwa | 543a292 | 2016-03-22 11:11:46 -0700 | [diff] [blame^] | 113 | if (DEBUG) Log.d(TAG, "showMenu called before Search MenuItem installed."); |
Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 114 | return; |
| 115 | } |
| 116 | |
Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 117 | if (!visible) { |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 118 | mCurrentSearch = null; |
Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 119 | } |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 120 | |
| 121 | mMenu.setVisible(visible); |
Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Cancels current search operation. Triggers clearing and collapsing the SearchView. |
| 126 | * |
| 127 | * @return True if it cancels search. False if it does not operate search currently. |
| 128 | */ |
| 129 | boolean cancelSearch() { |
| 130 | if (isExpanded() || isSearching()) { |
| 131 | // If the query string is not empty search view won't get iconified |
| 132 | mView.setQuery("", false); |
| 133 | // Causes calling onClose(). onClose() is triggering directory content update. |
| 134 | mView.setIconified(true); |
| 135 | return true; |
| 136 | } |
| 137 | return false; |
| 138 | } |
| 139 | |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 140 | private void restoreSearch() { |
| 141 | if (isSearching()) { |
| 142 | onSearchExpanded(); |
| 143 | mView.setIconified(false); |
| 144 | mView.setQuery(mCurrentSearch, false); |
| 145 | mView.clearFocus(); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | private void onSearchExpanded() { |
| 150 | mSearchExpanded = true; |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 153 | boolean isSearching() { |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 154 | return mCurrentSearch != null; |
Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | boolean isExpanded() { |
| 158 | return mSearchExpanded; |
| 159 | } |
| 160 | |
| 161 | /** |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 162 | * Called when owning activity is saving state to be used to restore state during creation. |
| 163 | * @param state Bundle to save state too |
| 164 | */ |
| 165 | public void onSaveInstanceState(Bundle state) { |
| 166 | state.putString(Shared.EXTRA_QUERY, mCurrentSearch); |
| 167 | } |
| 168 | |
| 169 | /** |
Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 170 | * Clears the search. Clears the SearchView background color. Triggers refreshing of the |
| 171 | * directory content. |
| 172 | * @return True if the default behavior of clearing/dismissing SearchView should be overridden. |
| 173 | * False otherwise. |
| 174 | */ |
| 175 | @Override |
| 176 | public boolean onClose() { |
| 177 | mSearchExpanded = false; |
| 178 | if (mIgnoreNextClose) { |
| 179 | mIgnoreNextClose = false; |
| 180 | return false; |
| 181 | } |
| 182 | |
Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 183 | // Refresh the directory if a search was done |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 184 | if (mCurrentSearch != null) { |
| 185 | mCurrentSearch = null; |
Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 186 | if (mListener != null) { |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 187 | mListener.onSearchChanged(mCurrentSearch); |
Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Sets mSearchExpanded. Called when search icon is clicked to start search. Used to detect when |
| 195 | * the view expanded instead of onMenuItemActionExpand, because SearchView has showAsAction set |
| 196 | * to always and onMenuItemAction* methods are not called. |
| 197 | */ |
| 198 | @Override |
| 199 | public void onClick(View v) { |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 200 | onSearchExpanded(); |
Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public boolean onQueryTextSubmit(String query) { |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 205 | mCurrentSearch = query; |
Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 206 | mView.clearFocus(); |
| 207 | if (mListener != null) { |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 208 | mListener.onSearchChanged(mCurrentSearch); |
Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 209 | } |
| 210 | return true; |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public void onFocusChange(View v, boolean hasFocus) { |
| 215 | if (!hasFocus) { |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 216 | if (mCurrentSearch == null) { |
Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 217 | mView.setIconified(true); |
| 218 | } else if (TextUtils.isEmpty(mView.getQuery())) { |
| 219 | cancelSearch(); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public boolean onQueryTextChange(String newText) { |
| 226 | return false; |
| 227 | } |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 228 | |
| 229 | String getCurrentSearch() { |
| 230 | return mCurrentSearch; |
| 231 | } |
| 232 | |
Aga Wronska | 8788dad | 2016-01-15 17:30:15 -0800 | [diff] [blame] | 233 | } |