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