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