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 | |
| 19 | import com.android.contacts.R; |
Katherine Kuan | b5760b9 | 2011-06-01 16:19:40 -0700 | [diff] [blame^] | 20 | import com.android.contacts.activities.ActionBarAdapter.Listener.Action; |
Dmitri Plotnikov | 15ccbb4 | 2010-10-05 17:29:07 -0700 | [diff] [blame] | 21 | import com.android.contacts.list.ContactListFilterController; |
Dmitri Plotnikov | bee8d3b | 2010-10-12 20:57:46 -0700 | [diff] [blame] | 22 | import com.android.contacts.list.ContactListFilterController.ContactListFilterListener; |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 23 | import com.android.contacts.list.ContactsRequest; |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 24 | |
Dmitri Plotnikov | 1a59cff | 2010-09-18 12:50:45 -0700 | [diff] [blame] | 25 | import android.app.ActionBar; |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 26 | import android.content.Context; |
| 27 | import android.os.Bundle; |
Dmitri Plotnikov | 15ccbb4 | 2010-10-05 17:29:07 -0700 | [diff] [blame] | 28 | import android.text.TextUtils; |
Dmitri Plotnikov | 1a59cff | 2010-09-18 12:50:45 -0700 | [diff] [blame] | 29 | import android.widget.SearchView; |
| 30 | import android.widget.SearchView.OnCloseListener; |
Adam Powell | c5792ae | 2011-01-20 18:35:48 -0800 | [diff] [blame] | 31 | import android.widget.SearchView.OnQueryTextListener; |
Dmitri Plotnikov | 92a6fdf | 2010-07-09 13:19:33 -0700 | [diff] [blame] | 32 | |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 33 | /** |
Dmitri Plotnikov | fcfdffb | 2010-07-15 18:00:55 -0700 | [diff] [blame] | 34 | * Adapter for the action bar at the top of the Contacts activity. |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 35 | */ |
Dmitri Plotnikov | eb5c322 | 2011-02-22 18:27:13 -0800 | [diff] [blame] | 36 | public class ActionBarAdapter |
| 37 | implements OnQueryTextListener, OnCloseListener, ContactListFilterListener { |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 38 | |
| 39 | public interface Listener { |
Katherine Kuan | b5760b9 | 2011-06-01 16:19:40 -0700 | [diff] [blame^] | 40 | public enum Action { |
| 41 | CHANGE_SEARCH_QUERY, START_SEARCH_MODE, STOP_SEARCH_MODE |
| 42 | } |
| 43 | |
| 44 | void onAction(Action action); |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Dmitri Plotnikov | 15ccbb4 | 2010-10-05 17:29:07 -0700 | [diff] [blame] | 47 | private static final String EXTRA_KEY_SEARCH_MODE = "navBar.searchMode"; |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 48 | private static final String EXTRA_KEY_QUERY = "navBar.query"; |
| 49 | |
Dmitri Plotnikov | 15ccbb4 | 2010-10-05 17:29:07 -0700 | [diff] [blame] | 50 | private boolean mSearchMode; |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 51 | private String mQueryString; |
Dmitri Plotnikov | 15ccbb4 | 2010-10-05 17:29:07 -0700 | [diff] [blame] | 52 | |
Katherine Kuan | b5760b9 | 2011-06-01 16:19:40 -0700 | [diff] [blame^] | 53 | private String mSearchLabelText; |
Dmitri Plotnikov | 15ccbb4 | 2010-10-05 17:29:07 -0700 | [diff] [blame] | 54 | private SearchView mSearchView; |
Dmitri Plotnikov | 92a6fdf | 2010-07-09 13:19:33 -0700 | [diff] [blame] | 55 | |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 56 | private final Context mContext; |
| 57 | |
| 58 | private Listener mListener; |
Dmitri Plotnikov | bee8d3b | 2010-10-12 20:57:46 -0700 | [diff] [blame] | 59 | private ContactListFilterController mFilterController; |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 60 | |
Katherine Kuan | b5760b9 | 2011-06-01 16:19:40 -0700 | [diff] [blame^] | 61 | private ActionBar mActionBar; |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 62 | |
Dmitri Plotnikov | fcfdffb | 2010-07-15 18:00:55 -0700 | [diff] [blame] | 63 | public ActionBarAdapter(Context context) { |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 64 | mContext = context; |
Katherine Kuan | b5760b9 | 2011-06-01 16:19:40 -0700 | [diff] [blame^] | 65 | mSearchLabelText = mContext.getString(R.string.search_label); |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Dmitri Plotnikov | 1a59cff | 2010-09-18 12:50:45 -0700 | [diff] [blame] | 68 | public void onCreate(Bundle savedState, ContactsRequest request, ActionBar actionBar) { |
Katherine Kuan | b5760b9 | 2011-06-01 16:19:40 -0700 | [diff] [blame^] | 69 | mActionBar = actionBar; |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 70 | mQueryString = null; |
Katherine Kuan | b5760b9 | 2011-06-01 16:19:40 -0700 | [diff] [blame^] | 71 | |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 72 | if (savedState != null) { |
Dmitri Plotnikov | 15ccbb4 | 2010-10-05 17:29:07 -0700 | [diff] [blame] | 73 | mSearchMode = savedState.getBoolean(EXTRA_KEY_SEARCH_MODE); |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 74 | mQueryString = savedState.getString(EXTRA_KEY_QUERY); |
Dmitri Plotnikov | 15ccbb4 | 2010-10-05 17:29:07 -0700 | [diff] [blame] | 75 | } else { |
| 76 | mSearchMode = request.isSearchMode(); |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 77 | mQueryString = request.getQueryString(); |
| 78 | } |
Dmitri Plotnikov | 1a59cff | 2010-09-18 12:50:45 -0700 | [diff] [blame] | 79 | |
Katherine Kuan | b5760b9 | 2011-06-01 16:19:40 -0700 | [diff] [blame^] | 80 | if (mSearchView != null) { |
| 81 | mSearchView.setQuery(mQueryString, false); |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 82 | } |
Dmitri Plotnikov | 3bfccdd | 2010-11-04 13:29:11 -0700 | [diff] [blame] | 83 | |
Dmitri Plotnikov | bee8d3b | 2010-10-12 20:57:46 -0700 | [diff] [blame] | 84 | update(); |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Katherine Kuan | b5760b9 | 2011-06-01 16:19:40 -0700 | [diff] [blame^] | 87 | public void setSearchView(SearchView searchView) { |
| 88 | mSearchView = searchView; |
| 89 | mSearchView.setOnQueryTextListener(this); |
| 90 | mSearchView.setOnCloseListener(this); |
| 91 | mSearchView.setQuery(mQueryString, false); |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 94 | public void setListener(Listener listener) { |
| 95 | mListener = listener; |
| 96 | } |
| 97 | |
Dmitri Plotnikov | 15ccbb4 | 2010-10-05 17:29:07 -0700 | [diff] [blame] | 98 | public void setContactListFilterController(ContactListFilterController controller) { |
Dmitri Plotnikov | bee8d3b | 2010-10-12 20:57:46 -0700 | [diff] [blame] | 99 | mFilterController = controller; |
Dmitri Plotnikov | bee8d3b | 2010-10-12 20:57:46 -0700 | [diff] [blame] | 100 | mFilterController.addListener(this); |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Dmitri Plotnikov | 15ccbb4 | 2010-10-05 17:29:07 -0700 | [diff] [blame] | 103 | public boolean isSearchMode() { |
| 104 | return mSearchMode; |
| 105 | } |
| 106 | |
| 107 | public void setSearchMode(boolean flag) { |
| 108 | if (mSearchMode != flag) { |
| 109 | mSearchMode = flag; |
Dmitri Plotnikov | bee8d3b | 2010-10-12 20:57:46 -0700 | [diff] [blame] | 110 | update(); |
Dmitri Plotnikov | 0b51a8d | 2010-10-21 21:22:52 -0700 | [diff] [blame] | 111 | if (mSearchMode) { |
| 112 | mSearchView.requestFocus(); |
Dmitri Plotnikov | ab4d29f | 2010-10-28 12:43:54 -0700 | [diff] [blame] | 113 | } else { |
| 114 | mSearchView.setQuery(null, false); |
Dmitri Plotnikov | 0b51a8d | 2010-10-21 21:22:52 -0700 | [diff] [blame] | 115 | } |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 119 | public String getQueryString() { |
| 120 | return mQueryString; |
| 121 | } |
| 122 | |
| 123 | public void setQueryString(String query) { |
| 124 | mQueryString = query; |
Dmitri Plotnikov | 1a59cff | 2010-09-18 12:50:45 -0700 | [diff] [blame] | 125 | mSearchView.setQuery(query, false); |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Dmitri Plotnikov | bee8d3b | 2010-10-12 20:57:46 -0700 | [diff] [blame] | 128 | public void update() { |
Katherine Kuan | b5760b9 | 2011-06-01 16:19:40 -0700 | [diff] [blame^] | 129 | if (mSearchMode) { |
| 130 | mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); |
| 131 | mActionBar.setTitle(mSearchLabelText); |
| 132 | if (mListener != null) { |
| 133 | mListener.onAction(Action.START_SEARCH_MODE); |
Dmitri Plotnikov | c9916d3 | 2010-10-13 11:42:09 -0700 | [diff] [blame] | 134 | } |
Dmitri Plotnikov | 15ccbb4 | 2010-10-05 17:29:07 -0700 | [diff] [blame] | 135 | } else { |
Katherine Kuan | b5760b9 | 2011-06-01 16:19:40 -0700 | [diff] [blame^] | 136 | mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); |
| 137 | mActionBar.setTitle(null); |
| 138 | if (mListener != null) { |
| 139 | mListener.onAction(Action.STOP_SEARCH_MODE); |
Dmitri Plotnikov | bee8d3b | 2010-10-12 20:57:46 -0700 | [diff] [blame] | 140 | } |
Dmitri Plotnikov | 88a405f | 2010-09-27 18:50:57 -0700 | [diff] [blame] | 141 | } |
Dmitri Plotnikov | 1a59cff | 2010-09-18 12:50:45 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Override |
Adam Powell | c5792ae | 2011-01-20 18:35:48 -0800 | [diff] [blame] | 145 | public boolean onQueryTextChange(String queryString) { |
Katherine Kuan | b5760b9 | 2011-06-01 16:19:40 -0700 | [diff] [blame^] | 146 | // TODO: Clean up SearchView code because it keeps setting the SearchView query, |
| 147 | // invoking onQueryChanged, setting up the fragment again, invalidating the options menu, |
| 148 | // storing the SearchView again, and etc... unless we add in the early return statements. |
| 149 | if (queryString.equals(mQueryString)) { |
| 150 | return false; |
| 151 | } |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 152 | mQueryString = queryString; |
Dmitri Plotnikov | ab4d29f | 2010-10-28 12:43:54 -0700 | [diff] [blame] | 153 | if (!mSearchMode) { |
| 154 | if (!TextUtils.isEmpty(queryString)) { |
| 155 | setSearchMode(true); |
Dmitri Plotnikov | 9806ed0 | 2010-10-25 16:20:53 -0700 | [diff] [blame] | 156 | } |
Dmitri Plotnikov | ab4d29f | 2010-10-28 12:43:54 -0700 | [diff] [blame] | 157 | } else if (mListener != null) { |
Katherine Kuan | b5760b9 | 2011-06-01 16:19:40 -0700 | [diff] [blame^] | 158 | mListener.onAction(Action.CHANGE_SEARCH_QUERY); |
Dmitri Plotnikov | 9806ed0 | 2010-10-25 16:20:53 -0700 | [diff] [blame] | 159 | } |
Dmitri Plotnikov | ab4d29f | 2010-10-28 12:43:54 -0700 | [diff] [blame] | 160 | |
Dmitri Plotnikov | 1a59cff | 2010-09-18 12:50:45 -0700 | [diff] [blame] | 161 | return true; |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | @Override |
Adam Powell | c5792ae | 2011-01-20 18:35:48 -0800 | [diff] [blame] | 165 | public boolean onQueryTextSubmit(String query) { |
Dmitri Plotnikov | 1a59cff | 2010-09-18 12:50:45 -0700 | [diff] [blame] | 166 | return true; |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public boolean onClose() { |
Dmitri Plotnikov | 15ccbb4 | 2010-10-05 17:29:07 -0700 | [diff] [blame] | 171 | setSearchMode(false); |
| 172 | return false; |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | public void onSaveInstanceState(Bundle outState) { |
Dmitri Plotnikov | 15ccbb4 | 2010-10-05 17:29:07 -0700 | [diff] [blame] | 176 | outState.putBoolean(EXTRA_KEY_SEARCH_MODE, mSearchMode); |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 177 | outState.putString(EXTRA_KEY_QUERY, mQueryString); |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 178 | } |
Dmitri Plotnikov | bee8d3b | 2010-10-12 20:57:46 -0700 | [diff] [blame] | 179 | |
Dmitri Plotnikov | fa49a66 | 2011-01-13 11:04:56 -0800 | [diff] [blame] | 180 | public void onRestoreInstanceState(Bundle savedState) { |
| 181 | mSearchMode = savedState.getBoolean(EXTRA_KEY_SEARCH_MODE); |
| 182 | mQueryString = savedState.getString(EXTRA_KEY_QUERY); |
| 183 | } |
| 184 | |
Dmitri Plotnikov | bee8d3b | 2010-10-12 20:57:46 -0700 | [diff] [blame] | 185 | @Override |
| 186 | public void onContactListFiltersLoaded() { |
| 187 | update(); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public void onContactListFilterChanged() { |
| 192 | update(); |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public void onContactListFilterCustomizationRequest() { |
| 197 | } |
Dmitri Plotnikov | 08e1c4e | 2010-07-02 17:57:51 -0700 | [diff] [blame] | 198 | } |