Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 1 | /* |
Dmitri Plotnikov | fcfdffb | 2010-07-15 18:00:55 -0700 | [diff] [blame] | 2 | * Copyright (C) 2010 The Android Open Source Project |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 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.contacts.activities; |
| 18 | |
Wenyi Wang | 11ff39a | 2016-08-07 15:46:03 -0700 | [diff] [blame] | 19 | import android.animation.ArgbEvaluator; |
Brian Attwell | 3c763b3 | 2014-07-21 14:25:28 -0700 | [diff] [blame] | 20 | import android.animation.ValueAnimator; |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 21 | import android.app.Activity; |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 22 | import android.content.Context; |
Wenyi Wang | db66269 | 2016-07-14 16:07:44 -0700 | [diff] [blame] | 23 | import android.content.res.ColorStateList; |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 24 | import android.os.Bundle; |
Wenyi Wang | 23d9b6e | 2015-11-19 15:09:06 -0800 | [diff] [blame] | 25 | import android.support.v4.content.ContextCompat; |
Wenyi Wang | 3c3b6f1 | 2016-04-02 13:58:43 -0700 | [diff] [blame] | 26 | import android.support.v7.app.ActionBar; |
| 27 | import android.support.v7.widget.Toolbar; |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 28 | import android.text.Editable; |
Dmitri Plotnikov | 15ccbb4 | 2010-10-05 17:29:07 -0700 | [diff] [blame] | 29 | import android.text.TextUtils; |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 30 | import android.text.TextWatcher; |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 31 | import android.view.Gravity; |
Brian Attwell | 3c763b3 | 2014-07-21 14:25:28 -0700 | [diff] [blame] | 32 | import android.view.LayoutInflater; |
Katherine Kuan | 1ee9df6 | 2011-06-11 16:54:01 -0700 | [diff] [blame] | 33 | import android.view.View; |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 34 | import android.view.View.OnClickListener; |
Brian Attwell | 3c763b3 | 2014-07-21 14:25:28 -0700 | [diff] [blame] | 35 | import android.view.ViewGroup; |
Wenyi Wang | db66269 | 2016-07-14 16:07:44 -0700 | [diff] [blame] | 36 | import android.view.ViewTreeObserver; |
Wenyi Wang | 11ff39a | 2016-08-07 15:46:03 -0700 | [diff] [blame] | 37 | import android.view.Window; |
Katherine Kuan | d8a61b7 | 2011-07-27 13:57:13 -0700 | [diff] [blame] | 38 | import android.view.inputmethod.InputMethodManager; |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 39 | import android.widget.EditText; |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 40 | import android.widget.FrameLayout; |
John Shao | bd9ef3c | 2016-12-15 12:42:03 -0800 | [diff] [blame] | 41 | import android.widget.ImageButton; |
Wenyi Wang | db66269 | 2016-07-14 16:07:44 -0700 | [diff] [blame] | 42 | import android.widget.ImageView; |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 43 | import android.widget.LinearLayout.LayoutParams; |
Dmitri Plotnikov | 1a59cff | 2010-09-18 12:50:45 -0700 | [diff] [blame] | 44 | import android.widget.SearchView.OnCloseListener; |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 45 | import android.widget.TextView; |
Dmitri Plotnikov | 92a6fdf | 2010-07-09 13:19:33 -0700 | [diff] [blame] | 46 | |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 47 | import com.android.contacts.R; |
| 48 | import com.android.contacts.activities.ActionBarAdapter.Listener.Action; |
Wenyi Wang | 917cb4a | 2016-12-13 17:42:18 -0800 | [diff] [blame] | 49 | import com.android.contacts.activities.PeopleActivity; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 50 | import com.android.contacts.compat.CompatUtils; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 51 | import com.android.contacts.list.ContactsRequest; |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 52 | import com.android.contacts.util.MaterialColorMapUtils; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 53 | |
Wenyi Wang | db66269 | 2016-07-14 16:07:44 -0700 | [diff] [blame] | 54 | import java.util.ArrayList; |
| 55 | |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 56 | /** |
Dmitri Plotnikov | fcfdffb | 2010-07-15 18:00:55 -0700 | [diff] [blame] | 57 | * Adapter for the action bar at the top of the Contacts activity. |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 58 | */ |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 59 | public class ActionBarAdapter implements OnCloseListener { |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 60 | |
| 61 | public interface Listener { |
Daniel Lehmann | 311882a | 2012-04-27 22:31:38 -0700 | [diff] [blame] | 62 | public abstract class Action { |
| 63 | public static final int CHANGE_SEARCH_QUERY = 0; |
| 64 | public static final int START_SEARCH_MODE = 1; |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 65 | public static final int START_SELECTION_MODE = 2; |
| 66 | public static final int STOP_SEARCH_AND_SELECTION_MODE = 3; |
Brian Attwell | 865847b | 2015-04-20 14:40:17 -0700 | [diff] [blame] | 67 | public static final int BEGIN_STOPPING_SEARCH_AND_SELECTION_MODE = 4; |
Katherine Kuan | b5760b9 | 2011-06-01 16:19:40 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Daniel Lehmann | 311882a | 2012-04-27 22:31:38 -0700 | [diff] [blame] | 70 | void onAction(int action); |
Makoto Onuki | 4d788fc | 2011-07-12 10:00:10 -0700 | [diff] [blame] | 71 | |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 72 | void onUpButtonPressed(); |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Dmitri Plotnikov | 15ccbb4 | 2010-10-05 17:29:07 -0700 | [diff] [blame] | 75 | private static final String EXTRA_KEY_SEARCH_MODE = "navBar.searchMode"; |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 76 | private static final String EXTRA_KEY_QUERY = "navBar.query"; |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 77 | private static final String EXTRA_KEY_SELECTED_MODE = "navBar.selectionMode"; |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 78 | |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 79 | private boolean mSelectionMode; |
Dmitri Plotnikov | 15ccbb4 | 2010-10-05 17:29:07 -0700 | [diff] [blame] | 80 | private boolean mSearchMode; |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 81 | private String mQueryString; |
Dmitri Plotnikov | 15ccbb4 | 2010-10-05 17:29:07 -0700 | [diff] [blame] | 82 | |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 83 | private EditText mSearchView; |
Andrew Lee | ad6e0ee | 2015-05-05 17:47:42 -0700 | [diff] [blame] | 84 | private View mClearSearchView; |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 85 | private View mSearchContainer; |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 86 | private View mSelectionContainer; |
Dmitri Plotnikov | 92a6fdf | 2010-07-09 13:19:33 -0700 | [diff] [blame] | 87 | |
Brian Attwell | 3c763b3 | 2014-07-21 14:25:28 -0700 | [diff] [blame] | 88 | private int mMaxToolbarContentInsetStart; |
Wenyi Wang | bd11497 | 2016-07-22 10:42:40 -0700 | [diff] [blame] | 89 | private int mActionBarAnimationDuration; |
Brian Attwell | 3c763b3 | 2014-07-21 14:25:28 -0700 | [diff] [blame] | 90 | |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 91 | private final Activity mActivity; |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 92 | |
| 93 | private Listener mListener; |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 94 | |
Makoto Onuki | 4d788fc | 2011-07-12 10:00:10 -0700 | [diff] [blame] | 95 | private final ActionBar mActionBar; |
Brian Attwell | 3c763b3 | 2014-07-21 14:25:28 -0700 | [diff] [blame] | 96 | private final Toolbar mToolbar; |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 97 | /** |
| 98 | * Frame that contains the toolbar and draws the toolbar's background color. This is useful |
| 99 | * for placing things behind the toolbar. |
| 100 | */ |
| 101 | private final FrameLayout mToolBarFrame; |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 102 | |
Makoto Onuki | e0c66af | 2011-07-13 14:24:03 -0700 | [diff] [blame] | 103 | private boolean mShowHomeIcon; |
Walter Jang | 59087d3 | 2016-05-17 13:52:15 -0700 | [diff] [blame] | 104 | private boolean mShowHomeAsUp; |
Makoto Onuki | e0c66af | 2011-07-13 14:24:03 -0700 | [diff] [blame] | 105 | |
Walter Jang | 68a0be7 | 2016-05-20 10:36:28 -0700 | [diff] [blame] | 106 | private int mSearchHintResId; |
| 107 | |
Wenyi Wang | 11ff39a | 2016-08-07 15:46:03 -0700 | [diff] [blame] | 108 | private ValueAnimator mStatusBarAnimator; |
| 109 | |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 110 | public ActionBarAdapter(Activity activity, Listener listener, ActionBar actionBar, |
Wenyi Wang | ca6018d | 2016-08-18 17:23:13 -0700 | [diff] [blame] | 111 | Toolbar toolbar) { |
| 112 | this(activity, listener, actionBar, toolbar, R.string.hint_findContacts); |
Walter Jang | 68a0be7 | 2016-05-20 10:36:28 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | public ActionBarAdapter(Activity activity, Listener listener, ActionBar actionBar, |
Wenyi Wang | ca6018d | 2016-08-18 17:23:13 -0700 | [diff] [blame] | 116 | Toolbar toolbar, int searchHintResId) { |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 117 | mActivity = activity; |
Makoto Onuki | 4d788fc | 2011-07-12 10:00:10 -0700 | [diff] [blame] | 118 | mListener = listener; |
| 119 | mActionBar = actionBar; |
Brian Attwell | 3c763b3 | 2014-07-21 14:25:28 -0700 | [diff] [blame] | 120 | mToolbar = toolbar; |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 121 | mToolBarFrame = (FrameLayout) mToolbar.getParent(); |
Brian Attwell | 3c763b3 | 2014-07-21 14:25:28 -0700 | [diff] [blame] | 122 | mMaxToolbarContentInsetStart = mToolbar.getContentInsetStart(); |
Walter Jang | 68a0be7 | 2016-05-20 10:36:28 -0700 | [diff] [blame] | 123 | mSearchHintResId = searchHintResId; |
Wenyi Wang | bd11497 | 2016-07-22 10:42:40 -0700 | [diff] [blame] | 124 | mActionBarAnimationDuration = |
| 125 | mActivity.getResources().getInteger(R.integer.action_bar_animation_duration); |
Makoto Onuki | e0c66af | 2011-07-13 14:24:03 -0700 | [diff] [blame] | 126 | |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 127 | setupSearchAndSelectionViews(); |
Maurice Chu | 7f5220e | 2012-05-14 10:00:28 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Walter Jang | 59087d3 | 2016-05-17 13:52:15 -0700 | [diff] [blame] | 130 | public void setShowHomeIcon(boolean showHomeIcon) { |
| 131 | mShowHomeIcon = showHomeIcon; |
| 132 | } |
| 133 | |
| 134 | public void setShowHomeAsUp(boolean showHomeAsUp) { |
| 135 | mShowHomeAsUp = showHomeAsUp; |
| 136 | } |
| 137 | |
Wenyi Wang | f5493a6 | 2016-07-11 14:28:52 -0700 | [diff] [blame] | 138 | public View getSelectionContainer() { |
| 139 | return mSelectionContainer; |
| 140 | } |
| 141 | |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 142 | private void setupSearchAndSelectionViews() { |
Brian Attwell | 3c763b3 | 2014-07-21 14:25:28 -0700 | [diff] [blame] | 143 | final LayoutInflater inflater = (LayoutInflater) mToolbar.getContext().getSystemService( |
| 144 | Context.LAYOUT_INFLATER_SERVICE); |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 145 | |
| 146 | // Setup search bar |
Brian Attwell | 3c763b3 | 2014-07-21 14:25:28 -0700 | [diff] [blame] | 147 | mSearchContainer = inflater.inflate(R.layout.search_bar_expanded, mToolbar, |
| 148 | /* attachToRoot = */ false); |
| 149 | mSearchContainer.setVisibility(View.VISIBLE); |
| 150 | mToolbar.addView(mSearchContainer); |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 151 | mSearchContainer.setBackgroundColor(mActivity.getResources().getColor( |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 152 | R.color.searchbox_background_color)); |
| 153 | mSearchView = (EditText) mSearchContainer.findViewById(R.id.search_view); |
Walter Jang | 68a0be7 | 2016-05-20 10:36:28 -0700 | [diff] [blame] | 154 | mSearchView.setHint(mActivity.getString(mSearchHintResId)); |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 155 | mSearchView.addTextChangedListener(new SearchTextWatcher()); |
John Shao | bd9ef3c | 2016-12-15 12:42:03 -0800 | [diff] [blame] | 156 | final ImageButton searchBackButton = (ImageButton) mSearchContainer |
| 157 | .findViewById(R.id.search_back_button); |
| 158 | searchBackButton.setOnClickListener( |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 159 | new OnClickListener() { |
| 160 | @Override |
| 161 | public void onClick(View v) { |
| 162 | if (mListener != null) { |
| 163 | mListener.onUpButtonPressed(); |
| 164 | } |
| 165 | } |
| 166 | }); |
John Shao | bd9ef3c | 2016-12-15 12:42:03 -0800 | [diff] [blame] | 167 | searchBackButton.getDrawable().setAutoMirrored(true); |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 168 | |
Andrew Lee | ad6e0ee | 2015-05-05 17:47:42 -0700 | [diff] [blame] | 169 | mClearSearchView = mSearchContainer.findViewById(R.id.search_close_button); |
| 170 | mClearSearchView.setOnClickListener( |
| 171 | new OnClickListener() { |
| 172 | @Override |
| 173 | public void onClick(View v) { |
| 174 | setQueryString(null); |
| 175 | } |
| 176 | }); |
| 177 | |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 178 | // Setup selection bar |
| 179 | mSelectionContainer = inflater.inflate(R.layout.selection_bar, mToolbar, |
| 180 | /* attachToRoot = */ false); |
| 181 | // Insert the selection container into mToolBarFrame behind the Toolbar, so that |
| 182 | // the Toolbar's MenuItems can appear on top of the selection container. |
| 183 | mToolBarFrame.addView(mSelectionContainer, 0); |
| 184 | mSelectionContainer.findViewById(R.id.selection_close).setOnClickListener( |
| 185 | new OnClickListener() { |
| 186 | @Override |
| 187 | public void onClick(View v) { |
| 188 | if (mListener != null) { |
| 189 | mListener.onUpButtonPressed(); |
| 190 | } |
| 191 | } |
| 192 | }); |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Makoto Onuki | 4d788fc | 2011-07-12 10:00:10 -0700 | [diff] [blame] | 195 | public void initialize(Bundle savedState, ContactsRequest request) { |
| 196 | if (savedState == null) { |
| 197 | mSearchMode = request.isSearchMode(); |
| 198 | mQueryString = request.getQueryString(); |
Brian Attwell | d2962a3 | 2015-03-02 14:48:50 -0800 | [diff] [blame] | 199 | mSelectionMode = false; |
Makoto Onuki | 4d788fc | 2011-07-12 10:00:10 -0700 | [diff] [blame] | 200 | } else { |
| 201 | mSearchMode = savedState.getBoolean(EXTRA_KEY_SEARCH_MODE); |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 202 | mSelectionMode = savedState.getBoolean(EXTRA_KEY_SELECTED_MODE); |
Makoto Onuki | 4d788fc | 2011-07-12 10:00:10 -0700 | [diff] [blame] | 203 | mQueryString = savedState.getString(EXTRA_KEY_QUERY); |
Brian Attwell | 26dc559 | 2014-02-07 17:10:03 -0800 | [diff] [blame] | 204 | } |
Katherine Kuan | 3c877e3 | 2012-01-03 17:27:36 -0800 | [diff] [blame] | 205 | // Show tabs or the expanded {@link SearchView}, depending on whether or not we are in |
| 206 | // search mode. |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 207 | update(true /* skipAnimation */); |
Katherine Kuan | 3c877e3 | 2012-01-03 17:27:36 -0800 | [diff] [blame] | 208 | // Expanding the {@link SearchView} clears the query, so set the query from the |
| 209 | // {@link ContactsRequest} after it has been expanded, if applicable. |
| 210 | if (mSearchMode && !TextUtils.isEmpty(mQueryString)) { |
| 211 | setQueryString(mQueryString); |
| 212 | } |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 215 | public void setListener(Listener listener) { |
| 216 | mListener = listener; |
| 217 | } |
| 218 | |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 219 | private class SearchTextWatcher implements TextWatcher { |
| 220 | |
| 221 | @Override |
| 222 | public void onTextChanged(CharSequence queryString, int start, int before, int count) { |
| 223 | if (queryString.equals(mQueryString)) { |
| 224 | return; |
| 225 | } |
| 226 | mQueryString = queryString.toString(); |
| 227 | if (!mSearchMode) { |
| 228 | if (!TextUtils.isEmpty(queryString)) { |
| 229 | setSearchMode(true); |
| 230 | } |
| 231 | } else if (mListener != null) { |
| 232 | mListener.onAction(Action.CHANGE_SEARCH_QUERY); |
| 233 | } |
Andrew Lee | ad6e0ee | 2015-05-05 17:47:42 -0700 | [diff] [blame] | 234 | mClearSearchView.setVisibility( |
| 235 | TextUtils.isEmpty(queryString) ? View.GONE : View.VISIBLE); |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public void afterTextChanged(Editable s) {} |
| 240 | |
| 241 | @Override |
| 242 | public void beforeTextChanged(CharSequence s, int start, int count, int after) {} |
| 243 | } |
| 244 | |
Makoto Onuki | 4d788fc | 2011-07-12 10:00:10 -0700 | [diff] [blame] | 245 | /** |
Makoto Onuki | 1db00f6 | 2011-10-19 12:40:12 -0700 | [diff] [blame] | 246 | * @return Whether in search mode, i.e. if the search view is visible/expanded. |
| 247 | * |
| 248 | * Note even if the action bar is in search mode, if the query is empty, the search fragment |
| 249 | * will not be in search mode. |
| 250 | */ |
Dmitri Plotnikov | 15ccbb4 | 2010-10-05 17:29:07 -0700 | [diff] [blame] | 251 | public boolean isSearchMode() { |
| 252 | return mSearchMode; |
| 253 | } |
| 254 | |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 255 | /** |
| 256 | * @return Whether in selection mode, i.e. if the selection view is visible/expanded. |
| 257 | */ |
| 258 | public boolean isSelectionMode() { |
| 259 | return mSelectionMode; |
| 260 | } |
| 261 | |
Dmitri Plotnikov | 15ccbb4 | 2010-10-05 17:29:07 -0700 | [diff] [blame] | 262 | public void setSearchMode(boolean flag) { |
| 263 | if (mSearchMode != flag) { |
| 264 | mSearchMode = flag; |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 265 | update(false /* skipAnimation */); |
Katherine Kuan | f3f933a | 2011-06-10 11:34:54 -0700 | [diff] [blame] | 266 | if (mSearchView == null) { |
| 267 | return; |
| 268 | } |
Dmitri Plotnikov | 0b51a8d | 2010-10-21 21:22:52 -0700 | [diff] [blame] | 269 | if (mSearchMode) { |
Brian Attwell | 87901e0 | 2014-12-01 19:11:31 -0800 | [diff] [blame] | 270 | mSearchView.setEnabled(true); |
Makoto Onuki | 4ba903c | 2011-07-07 16:55:17 -0700 | [diff] [blame] | 271 | setFocusOnSearchView(); |
Brian Attwell | 87901e0 | 2014-12-01 19:11:31 -0800 | [diff] [blame] | 272 | } else { |
| 273 | // Disable search view, so that it doesn't keep the IME visible. |
| 274 | mSearchView.setEnabled(false); |
Dmitri Plotnikov | 0b51a8d | 2010-10-21 21:22:52 -0700 | [diff] [blame] | 275 | } |
Brian Attwell | b3944dc | 2014-11-18 20:53:59 -0800 | [diff] [blame] | 276 | setQueryString(null); |
Daniel Lehmann | 311882a | 2012-04-27 22:31:38 -0700 | [diff] [blame] | 277 | } else if (flag) { |
| 278 | // Everything is already set up. Still make sure the keyboard is up |
| 279 | if (mSearchView != null) setFocusOnSearchView(); |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 283 | public void setSelectionMode(boolean flag) { |
| 284 | if (mSelectionMode != flag) { |
| 285 | mSelectionMode = flag; |
| 286 | update(false /* skipAnimation */); |
| 287 | } |
| 288 | } |
| 289 | |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 290 | public String getQueryString() { |
Makoto Onuki | 1db00f6 | 2011-10-19 12:40:12 -0700 | [diff] [blame] | 291 | return mSearchMode ? mQueryString : null; |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | public void setQueryString(String query) { |
| 295 | mQueryString = query; |
Katherine Kuan | f3f933a | 2011-06-10 11:34:54 -0700 | [diff] [blame] | 296 | if (mSearchView != null) { |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 297 | mSearchView.setText(query); |
Brian Attwell | b3944dc | 2014-11-18 20:53:59 -0800 | [diff] [blame] | 298 | // When programmatically entering text into the search view, the most reasonable |
| 299 | // place for the cursor is after all the text. |
| 300 | mSearchView.setSelection(mSearchView.getText() == null ? |
| 301 | 0 : mSearchView.getText().length()); |
Katherine Kuan | f3f933a | 2011-06-10 11:34:54 -0700 | [diff] [blame] | 302 | } |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Makoto Onuki | e0c66af | 2011-07-13 14:24:03 -0700 | [diff] [blame] | 305 | /** @return true if the "UP" icon is showing. */ |
| 306 | public boolean isUpShowing() { |
| 307 | return mSearchMode; // Only shown on the search mode. |
| 308 | } |
| 309 | |
Brian Attwell | 3c763b3 | 2014-07-21 14:25:28 -0700 | [diff] [blame] | 310 | private void updateDisplayOptionsInner() { |
Makoto Onuki | e0c66af | 2011-07-13 14:24:03 -0700 | [diff] [blame] | 311 | // All the flags we may change in this method. |
| 312 | final int MASK = ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 313 | | ActionBar.DISPLAY_HOME_AS_UP; |
Makoto Onuki | e0c66af | 2011-07-13 14:24:03 -0700 | [diff] [blame] | 314 | |
| 315 | // The current flags set to the action bar. (only the ones that we may change here) |
| 316 | final int current = mActionBar.getDisplayOptions() & MASK; |
| 317 | |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 318 | final boolean isSearchOrSelectionMode = mSearchMode || mSelectionMode; |
| 319 | |
Makoto Onuki | e0c66af | 2011-07-13 14:24:03 -0700 | [diff] [blame] | 320 | // Build the new flags... |
| 321 | int newFlags = 0; |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 322 | if (mShowHomeIcon && !isSearchOrSelectionMode) { |
Makoto Onuki | e0c66af | 2011-07-13 14:24:03 -0700 | [diff] [blame] | 323 | newFlags |= ActionBar.DISPLAY_SHOW_HOME; |
Walter Jang | 59087d3 | 2016-05-17 13:52:15 -0700 | [diff] [blame] | 324 | if (mShowHomeAsUp) { |
| 325 | newFlags |= ActionBar.DISPLAY_HOME_AS_UP; |
| 326 | } |
Makoto Onuki | e0c66af | 2011-07-13 14:24:03 -0700 | [diff] [blame] | 327 | } |
Brian Attwell | 865847b | 2015-04-20 14:40:17 -0700 | [diff] [blame] | 328 | if (mSearchMode && !mSelectionMode) { |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 329 | // The search container is placed inside the toolbar. So we need to disable the |
| 330 | // Toolbar's content inset in order to allow the search container to be the width of |
| 331 | // the window. |
Brian Attwell | 3c763b3 | 2014-07-21 14:25:28 -0700 | [diff] [blame] | 332 | mToolbar.setContentInsetsRelative(0, mToolbar.getContentInsetEnd()); |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 333 | } |
| 334 | if (!isSearchOrSelectionMode) { |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 335 | newFlags |= ActionBar.DISPLAY_SHOW_TITLE; |
Brian Attwell | 3c763b3 | 2014-07-21 14:25:28 -0700 | [diff] [blame] | 336 | mToolbar.setContentInsetsRelative(mMaxToolbarContentInsetStart, |
| 337 | mToolbar.getContentInsetEnd()); |
John Shao | bd9ef3c | 2016-12-15 12:42:03 -0800 | [diff] [blame] | 338 | mToolbar.setNavigationIcon(R.drawable.quantum_ic_menu_vd_theme_24); |
Wenyi Wang | f568b30 | 2016-04-02 13:58:43 -0700 | [diff] [blame] | 339 | } else { |
| 340 | mToolbar.setNavigationIcon(null); |
Makoto Onuki | e0c66af | 2011-07-13 14:24:03 -0700 | [diff] [blame] | 341 | } |
Brian Attwell | b0d3c76 | 2014-05-22 03:44:22 +0000 | [diff] [blame] | 342 | |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 343 | if (mSelectionMode) { |
| 344 | // Minimize the horizontal width of the Toolbar since the selection container is placed |
| 345 | // behind the toolbar and its left hand side needs to be clickable. |
| 346 | FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mToolbar.getLayoutParams(); |
| 347 | params.width = LayoutParams.WRAP_CONTENT; |
| 348 | params.gravity = Gravity.END; |
| 349 | mToolbar.setLayoutParams(params); |
| 350 | } else { |
| 351 | FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mToolbar.getLayoutParams(); |
| 352 | params.width = LayoutParams.MATCH_PARENT; |
| 353 | params.gravity = Gravity.END; |
| 354 | mToolbar.setLayoutParams(params); |
| 355 | } |
Brian Attwell | 3c763b3 | 2014-07-21 14:25:28 -0700 | [diff] [blame] | 356 | |
Makoto Onuki | e0c66af | 2011-07-13 14:24:03 -0700 | [diff] [blame] | 357 | if (current != newFlags) { |
| 358 | // Pass the mask here to preserve other flags that we're not interested here. |
| 359 | mActionBar.setDisplayOptions(newFlags, MASK); |
| 360 | } |
| 361 | } |
| 362 | |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 363 | private void update(boolean skipAnimation) { |
Wenyi Wang | db66269 | 2016-07-14 16:07:44 -0700 | [diff] [blame] | 364 | updateOverflowButtonColor(); |
| 365 | |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 366 | final boolean isSelectionModeChanging |
| 367 | = (mSelectionContainer.getParent() == null) == mSelectionMode; |
Brian Attwell | 865847b | 2015-04-20 14:40:17 -0700 | [diff] [blame] | 368 | final boolean isSwitchingFromSearchToSelection = |
| 369 | mSearchMode && isSelectionModeChanging || mSearchMode && mSelectionMode; |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 370 | final boolean isSearchModeChanging |
Brian Attwell | 3c763b3 | 2014-07-21 14:25:28 -0700 | [diff] [blame] | 371 | = (mSearchContainer.getParent() == null) == mSearchMode; |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 372 | final boolean isTabHeightChanging = isSearchModeChanging || isSelectionModeChanging; |
| 373 | |
Wenyi Wang | 92d7e51 | 2016-10-03 14:57:19 -0700 | [diff] [blame] | 374 | // Update toolbar and status bar color. |
| 375 | mToolBarFrame.setBackgroundColor(MaterialColorMapUtils.getToolBarColor(mActivity)); |
Wenyi Wang | 11ff39a | 2016-08-07 15:46:03 -0700 | [diff] [blame] | 376 | updateStatusBarColor(isSelectionModeChanging && !isSearchModeChanging); |
| 377 | |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 378 | // When skipAnimation=true, it is possible that we will switch from search mode |
| 379 | // to selection mode directly. So we need to remove the undesired container in addition |
| 380 | // to adding the desired container. |
Brian Attwell | 865847b | 2015-04-20 14:40:17 -0700 | [diff] [blame] | 381 | if (skipAnimation || isSwitchingFromSearchToSelection) { |
| 382 | if (isTabHeightChanging || isSwitchingFromSearchToSelection) { |
Brian Attwell | 865847b | 2015-04-20 14:40:17 -0700 | [diff] [blame] | 383 | mToolbar.removeView(mSearchContainer); |
| 384 | mToolBarFrame.removeView(mSelectionContainer); |
| 385 | if (mSelectionMode) { |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 386 | addSelectionContainer(); |
Brian Attwell | 865847b | 2015-04-20 14:40:17 -0700 | [diff] [blame] | 387 | } else if (mSearchMode) { |
Brian Attwell | 865847b | 2015-04-20 14:40:17 -0700 | [diff] [blame] | 388 | addSearchContainer(); |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 389 | } |
| 390 | updateDisplayOptions(isSearchModeChanging); |
| 391 | } |
| 392 | return; |
| 393 | } |
| 394 | |
| 395 | // Handle a switch to/from selection mode, due to UI interaction. |
| 396 | if (isSelectionModeChanging) { |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 397 | if (mSelectionMode) { |
| 398 | addSelectionContainer(); |
| 399 | mSelectionContainer.setAlpha(0); |
Wenyi Wang | bd11497 | 2016-07-22 10:42:40 -0700 | [diff] [blame] | 400 | mSelectionContainer.animate().alpha(1).setDuration(mActionBarAnimationDuration); |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 401 | updateDisplayOptions(isSearchModeChanging); |
| 402 | } else { |
Brian Attwell | 865847b | 2015-04-20 14:40:17 -0700 | [diff] [blame] | 403 | if (mListener != null) { |
| 404 | mListener.onAction(Action.BEGIN_STOPPING_SEARCH_AND_SELECTION_MODE); |
| 405 | } |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 406 | mSelectionContainer.setAlpha(1); |
Wenyi Wang | bd11497 | 2016-07-22 10:42:40 -0700 | [diff] [blame] | 407 | mSelectionContainer.animate().alpha(0).setDuration(mActionBarAnimationDuration) |
| 408 | .withEndAction(new Runnable() { |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 409 | @Override |
| 410 | public void run() { |
| 411 | updateDisplayOptions(isSearchModeChanging); |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 412 | mToolBarFrame.removeView(mSelectionContainer); |
| 413 | } |
| 414 | }); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | // Handle a switch to/from search mode, due to UI interaction. |
| 419 | if (isSearchModeChanging) { |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 420 | if (mSearchMode) { |
Brian Attwell | 3c763b3 | 2014-07-21 14:25:28 -0700 | [diff] [blame] | 421 | addSearchContainer(); |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 422 | mSearchContainer.setAlpha(0); |
Wenyi Wang | bd11497 | 2016-07-22 10:42:40 -0700 | [diff] [blame] | 423 | mSearchContainer.animate().alpha(1).setDuration(mActionBarAnimationDuration); |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 424 | updateDisplayOptions(isSearchModeChanging); |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 425 | } else { |
| 426 | mSearchContainer.setAlpha(1); |
Wenyi Wang | bd11497 | 2016-07-22 10:42:40 -0700 | [diff] [blame] | 427 | mSearchContainer.animate().alpha(0).setDuration(mActionBarAnimationDuration) |
| 428 | .withEndAction(new Runnable() { |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 429 | @Override |
| 430 | public void run() { |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 431 | updateDisplayOptions(isSearchModeChanging); |
Brian Attwell | 3c763b3 | 2014-07-21 14:25:28 -0700 | [diff] [blame] | 432 | mToolbar.removeView(mSearchContainer); |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 433 | } |
| 434 | }); |
| 435 | } |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 436 | } |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 437 | } |
| 438 | |
Wenyi Wang | db66269 | 2016-07-14 16:07:44 -0700 | [diff] [blame] | 439 | /** |
| 440 | * Find overflow menu ImageView by its content description and update its color. |
| 441 | */ |
Wenyi Wang | 0012afb | 2016-12-22 12:16:22 -0800 | [diff] [blame] | 442 | public void updateOverflowButtonColor() { |
Wenyi Wang | db66269 | 2016-07-14 16:07:44 -0700 | [diff] [blame] | 443 | final String overflowDescription = mActivity.getResources().getString( |
| 444 | R.string.abc_action_menu_overflow_description); |
| 445 | final ViewGroup decorView = (ViewGroup) mActivity.getWindow().getDecorView(); |
| 446 | final ViewTreeObserver viewTreeObserver = decorView.getViewTreeObserver(); |
| 447 | viewTreeObserver.addOnGlobalLayoutListener( |
| 448 | new ViewTreeObserver.OnGlobalLayoutListener() { |
| 449 | @Override |
| 450 | public void onGlobalLayout() { |
| 451 | // Find the overflow ImageView. |
| 452 | final ArrayList<View> outViews = new ArrayList<>(); |
| 453 | decorView.findViewsWithText(outViews, overflowDescription, |
| 454 | View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION); |
Wenyi Wang | db66269 | 2016-07-14 16:07:44 -0700 | [diff] [blame] | 455 | |
Wenyi Wang | 44702ac | 2016-12-01 13:31:22 -0800 | [diff] [blame] | 456 | for (View view : outViews) { |
| 457 | if (!(view instanceof ImageView)) { |
| 458 | continue; |
| 459 | } |
| 460 | final ImageView overflow = (ImageView) view; |
| 461 | |
| 462 | // Update the overflow image color. |
| 463 | final int iconColor; |
| 464 | if (mSelectionMode) { |
| 465 | iconColor = mActivity.getResources().getColor( |
| 466 | R.color.actionbar_color_grey_solid); |
| 467 | } else { |
| 468 | iconColor = mActivity.getResources().getColor( |
| 469 | R.color.actionbar_text_color); |
| 470 | } |
| 471 | overflow.setImageTintList(ColorStateList.valueOf(iconColor)); |
Wenyi Wang | db66269 | 2016-07-14 16:07:44 -0700 | [diff] [blame] | 472 | } |
Wenyi Wang | db66269 | 2016-07-14 16:07:44 -0700 | [diff] [blame] | 473 | |
| 474 | // We're done, remove the listener. |
| 475 | decorView.getViewTreeObserver().removeOnGlobalLayoutListener(this); |
| 476 | } |
| 477 | }); |
| 478 | } |
| 479 | |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 480 | public void setSelectionCount(int selectionCount) { |
| 481 | TextView textView = (TextView) mSelectionContainer.findViewById(R.id.selection_count_text); |
| 482 | if (selectionCount == 0) { |
| 483 | textView.setVisibility(View.GONE); |
| 484 | } else { |
| 485 | textView.setVisibility(View.VISIBLE); |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 486 | } |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 487 | textView.setText(String.valueOf(selectionCount)); |
| 488 | } |
| 489 | |
Wenyi Wang | 64a6bc4 | 2016-07-07 18:34:48 -0700 | [diff] [blame] | 490 | public void setActionBarTitle(String title) { |
| 491 | final TextView textView = |
| 492 | (TextView) mSelectionContainer.findViewById(R.id.selection_count_text); |
| 493 | textView.setVisibility(View.VISIBLE); |
| 494 | textView.setText(title); |
| 495 | } |
| 496 | |
Wenyi Wang | 11ff39a | 2016-08-07 15:46:03 -0700 | [diff] [blame] | 497 | private void updateStatusBarColor(boolean shouldAnimate) { |
Wenyi Wang | 4c3d3e2 | 2015-12-17 14:30:02 -0800 | [diff] [blame] | 498 | if (!CompatUtils.isLollipopCompatible()) { |
| 499 | return; // we can't change the status bar color prior to Lollipop |
| 500 | } |
Wenyi Wang | 11ff39a | 2016-08-07 15:46:03 -0700 | [diff] [blame] | 501 | |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 502 | if (mSelectionMode) { |
Wenyi Wang | d348a90 | 2016-08-14 12:56:06 -0700 | [diff] [blame] | 503 | final int cabStatusBarColor = ContextCompat.getColor( |
Wenyi Wang | 11ff39a | 2016-08-07 15:46:03 -0700 | [diff] [blame] | 504 | mActivity, R.color.contextual_selection_bar_status_bar_color); |
| 505 | runStatusBarAnimation(/* colorTo */ cabStatusBarColor); |
Wenyi Wang | 8e73c36 | 2016-06-23 18:03:53 -0700 | [diff] [blame] | 506 | } else { |
Wenyi Wang | 11ff39a | 2016-08-07 15:46:03 -0700 | [diff] [blame] | 507 | if (shouldAnimate) { |
Wenyi Wang | 92d7e51 | 2016-10-03 14:57:19 -0700 | [diff] [blame] | 508 | runStatusBarAnimation(/* colorTo */ |
| 509 | MaterialColorMapUtils.getStatusBarColor(mActivity)); |
Wenyi Wang | 917cb4a | 2016-12-13 17:42:18 -0800 | [diff] [blame] | 510 | } else if (mActivity instanceof PeopleActivity) { |
| 511 | ((PeopleActivity) mActivity).updateStatusBarBackground(); |
Wenyi Wang | 11ff39a | 2016-08-07 15:46:03 -0700 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | private void runStatusBarAnimation(int colorTo) { |
| 517 | final Window window = mActivity.getWindow(); |
| 518 | if (window.getStatusBarColor() != colorTo) { |
| 519 | // Cancel running animation. |
| 520 | if (mStatusBarAnimator != null && mStatusBarAnimator.isRunning()) { |
| 521 | mStatusBarAnimator.cancel(); |
| 522 | } |
| 523 | final int from = window.getStatusBarColor(); |
| 524 | // Set up mStatusBarAnimator and run animation. |
| 525 | mStatusBarAnimator = ValueAnimator.ofObject(new ArgbEvaluator(), from, colorTo); |
| 526 | mStatusBarAnimator.addUpdateListener( |
| 527 | new ValueAnimator.AnimatorUpdateListener() { |
| 528 | @Override |
| 529 | public void onAnimationUpdate(ValueAnimator animator) { |
| 530 | window.setStatusBarColor((Integer) animator.getAnimatedValue()); |
| 531 | } |
| 532 | }); |
| 533 | mStatusBarAnimator.setDuration(mActionBarAnimationDuration); |
| 534 | mStatusBarAnimator.setStartDelay(0); |
| 535 | mStatusBarAnimator.start(); |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 536 | } |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Brian Attwell | 3c763b3 | 2014-07-21 14:25:28 -0700 | [diff] [blame] | 539 | private void addSearchContainer() { |
| 540 | mToolbar.removeView(mSearchContainer); |
| 541 | mToolbar.addView(mSearchContainer); |
Brian Attwell | 865847b | 2015-04-20 14:40:17 -0700 | [diff] [blame] | 542 | mSearchContainer.setAlpha(1); |
Brian Attwell | 3c763b3 | 2014-07-21 14:25:28 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 545 | private void addSelectionContainer() { |
| 546 | mToolBarFrame.removeView(mSelectionContainer); |
| 547 | mToolBarFrame.addView(mSelectionContainer, 0); |
Brian Attwell | 865847b | 2015-04-20 14:40:17 -0700 | [diff] [blame] | 548 | mSelectionContainer.setAlpha(1); |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | private void updateDisplayOptions(boolean isSearchModeChanging) { |
Brian Attwell | 865847b | 2015-04-20 14:40:17 -0700 | [diff] [blame] | 552 | if (mSearchMode && !mSelectionMode) { |
Makoto Onuki | 5267367 | 2011-07-25 12:39:59 -0700 | [diff] [blame] | 553 | setFocusOnSearchView(); |
Katherine Kuan | d8a61b7 | 2011-07-27 13:57:13 -0700 | [diff] [blame] | 554 | // Since we have the {@link SearchView} in a custom action bar, we must manually handle |
Katherine Kuan | 3c877e3 | 2012-01-03 17:27:36 -0800 | [diff] [blame] | 555 | // expanding the {@link SearchView} when a search is initiated. Note that a side effect |
| 556 | // of this method is that the {@link SearchView} query text is set to empty string. |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 557 | if (isSearchModeChanging) { |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 558 | final CharSequence queryText = mSearchView.getText(); |
Yorke Lee | 8d5eec3 | 2013-01-18 11:07:48 -0800 | [diff] [blame] | 559 | if (!TextUtils.isEmpty(queryText)) { |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 560 | mSearchView.setText(queryText); |
Yorke Lee | 8d5eec3 | 2013-01-18 11:07:48 -0800 | [diff] [blame] | 561 | } |
Daniel Lehmann | b0d82d2 | 2012-05-07 19:00:06 -0700 | [diff] [blame] | 562 | } |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 563 | } |
| 564 | if (mListener != null) { |
| 565 | if (mSearchMode) { |
Katherine Kuan | b5760b9 | 2011-06-01 16:19:40 -0700 | [diff] [blame] | 566 | mListener.onAction(Action.START_SEARCH_MODE); |
Dmitri Plotnikov | c9916d3 | 2010-10-13 11:42:09 -0700 | [diff] [blame] | 567 | } |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 568 | if (mSelectionMode) { |
| 569 | mListener.onAction(Action.START_SELECTION_MODE); |
| 570 | } |
| 571 | if (!mSearchMode && !mSelectionMode) { |
| 572 | mListener.onAction(Action.STOP_SEARCH_AND_SELECTION_MODE); |
Dmitri Plotnikov | bee8d3b | 2010-10-12 20:57:46 -0700 | [diff] [blame] | 573 | } |
Dmitri Plotnikov | 88a405f | 2010-09-27 18:50:57 -0700 | [diff] [blame] | 574 | } |
Brian Attwell | 3c763b3 | 2014-07-21 14:25:28 -0700 | [diff] [blame] | 575 | updateDisplayOptionsInner(); |
Dmitri Plotnikov | 1a59cff | 2010-09-18 12:50:45 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | @Override |
Dmitri Plotnikov | 1a59cff | 2010-09-18 12:50:45 -0700 | [diff] [blame] | 579 | public boolean onClose() { |
Dmitri Plotnikov | 15ccbb4 | 2010-10-05 17:29:07 -0700 | [diff] [blame] | 580 | setSearchMode(false); |
| 581 | return false; |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | public void onSaveInstanceState(Bundle outState) { |
Dmitri Plotnikov | 15ccbb4 | 2010-10-05 17:29:07 -0700 | [diff] [blame] | 585 | outState.putBoolean(EXTRA_KEY_SEARCH_MODE, mSearchMode); |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 586 | outState.putBoolean(EXTRA_KEY_SELECTED_MODE, mSelectionMode); |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 587 | outState.putString(EXTRA_KEY_QUERY, mQueryString); |
Dmitri Plotnikov | fa49a66 | 2011-01-13 11:04:56 -0800 | [diff] [blame] | 588 | } |
| 589 | |
Daniel Lehmann | 09d66a3 | 2012-04-13 19:14:39 -0700 | [diff] [blame] | 590 | public void setFocusOnSearchView() { |
Makoto Onuki | 4ba903c | 2011-07-07 16:55:17 -0700 | [diff] [blame] | 591 | mSearchView.requestFocus(); |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 592 | showInputMethod(mSearchView); // Workaround for the "IME not popping up" issue. |
| 593 | } |
| 594 | |
| 595 | private void showInputMethod(View view) { |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 596 | final InputMethodManager imm = (InputMethodManager) mActivity.getSystemService( |
Brian Attwell | 6adbf1e | 2014-05-30 16:44:51 -0700 | [diff] [blame] | 597 | Context.INPUT_METHOD_SERVICE); |
| 598 | if (imm != null) { |
| 599 | imm.showSoftInput(view, 0); |
| 600 | } |
Makoto Onuki | 4ba903c | 2011-07-07 16:55:17 -0700 | [diff] [blame] | 601 | } |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 602 | } |