ActionBarAdapter.java revision 59dc2751d64f60f743b8a9e78186aa5b79dcaf83
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.contacts.activities; 18 19import com.android.contacts.R; 20import com.android.contacts.activities.ActionBarAdapter.Listener.Action; 21import com.android.contacts.list.ContactListFilterController; 22import com.android.contacts.list.ContactListFilterController.ContactListFilterListener; 23import com.android.contacts.list.ContactsRequest; 24 25import android.app.ActionBar; 26import android.app.ActionBar.LayoutParams; 27import android.content.Context; 28import android.os.Bundle; 29import android.text.TextUtils; 30import android.view.LayoutInflater; 31import android.view.View; 32import android.widget.SearchView; 33import android.widget.SearchView.OnCloseListener; 34import android.widget.SearchView.OnQueryTextListener; 35 36/** 37 * Adapter for the action bar at the top of the Contacts activity. 38 */ 39public class ActionBarAdapter 40 implements OnQueryTextListener, OnCloseListener, ContactListFilterListener { 41 42 public interface Listener { 43 public enum Action { 44 CHANGE_SEARCH_QUERY, START_SEARCH_MODE, STOP_SEARCH_MODE 45 } 46 47 void onAction(Action action); 48 } 49 50 private static final String EXTRA_KEY_SEARCH_MODE = "navBar.searchMode"; 51 private static final String EXTRA_KEY_QUERY = "navBar.query"; 52 private static final String EXTRA_KEY_SELECTED_TAB = "navBar.selectedTab"; 53 54 private boolean mSearchMode; 55 private String mQueryString; 56 57 private String mSearchLabelText; 58 private SearchView mSearchView; 59 60 private final Context mContext; 61 62 private Listener mListener; 63 private ContactListFilterController mFilterController; 64 65 private ActionBar mActionBar; 66 67 private View mCustomSearchView; 68 private LayoutParams mLayoutParams; 69 private boolean mIsSearchInOverflowMenu; 70 71 public ActionBarAdapter(Context context, Listener listener) { 72 mContext = context; 73 mListener = listener; 74 mSearchLabelText = mContext.getString(R.string.search_label); 75 } 76 77 public void onCreate(Bundle savedState, ContactsRequest request, ActionBar actionBar, 78 boolean searchInOverflowMenu) { 79 mActionBar = actionBar; 80 mQueryString = null; 81 mIsSearchInOverflowMenu = searchInOverflowMenu; 82 83 if (savedState != null) { 84 mSearchMode = savedState.getBoolean(EXTRA_KEY_SEARCH_MODE); 85 mQueryString = savedState.getString(EXTRA_KEY_QUERY); 86 } else { 87 mSearchMode = request.isSearchMode(); 88 mQueryString = request.getQueryString(); 89 } 90 91 if (mSearchView != null) { 92 mSearchView.setQuery(mQueryString, false); 93 } 94 95 update(); 96 } 97 98 public void setSearchView(SearchView searchView) { 99 mSearchView = searchView; 100 mSearchView.setOnQueryTextListener(this); 101 mSearchView.setOnCloseListener(this); 102 mSearchView.setQuery(mQueryString, false); 103 } 104 105 public void setListener(Listener listener) { 106 mListener = listener; 107 } 108 109 public void setContactListFilterController(ContactListFilterController controller) { 110 mFilterController = controller; 111 mFilterController.addListener(this); 112 } 113 114 public boolean isSearchInOverflowMenu() { 115 return mIsSearchInOverflowMenu; 116 } 117 118 public boolean isSearchMode() { 119 return mSearchMode; 120 } 121 122 public void setSearchMode(boolean flag) { 123 if (mSearchMode != flag) { 124 mSearchMode = flag; 125 update(); 126 if (mSearchView == null) { 127 return; 128 } 129 if (mSearchMode) { 130 mSearchView.requestFocus(); 131 } else { 132 mSearchView.setQuery(null, false); 133 } 134 } 135 } 136 137 public String getQueryString() { 138 return mQueryString; 139 } 140 141 public void setQueryString(String query) { 142 mQueryString = query; 143 if (mSearchView != null) { 144 mSearchView.setQuery(query, false); 145 } 146 } 147 148 public void update() { 149 if (mSearchMode) { 150 // If the search icon was in the overflow menu, then inflate a custom view containing 151 // a search view for the action bar (and hide the tabs). 152 if (mIsSearchInOverflowMenu) { 153 if (mCustomSearchView == null) { 154 mCustomSearchView = LayoutInflater.from(mContext).inflate( 155 R.layout.custom_action_bar, null); 156 mLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, 157 LayoutParams.WRAP_CONTENT); 158 SearchView searchView = (SearchView) mCustomSearchView. 159 findViewById(R.id.search_view); 160 searchView.setQueryHint(mContext.getString(R.string.hint_findContacts)); 161 setSearchView(searchView); 162 } 163 mActionBar.setDisplayShowCustomEnabled(true); 164 mActionBar.setCustomView(mCustomSearchView, mLayoutParams); 165 mSearchView.requestFocus(); 166 } else { 167 mActionBar.setTitle(mSearchLabelText); 168 } 169 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 170 if (mListener != null) { 171 mListener.onAction(Action.START_SEARCH_MODE); 172 } 173 } else { 174 mActionBar.setDisplayShowCustomEnabled(false); 175 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 176 mActionBar.setTitle(null); 177 if (mListener != null) { 178 mListener.onAction(Action.STOP_SEARCH_MODE); 179 } 180 } 181 } 182 183 @Override 184 public boolean onQueryTextChange(String queryString) { 185 // TODO: Clean up SearchView code because it keeps setting the SearchView query, 186 // invoking onQueryChanged, setting up the fragment again, invalidating the options menu, 187 // storing the SearchView again, and etc... unless we add in the early return statements. 188 if (queryString.equals(mQueryString)) { 189 return false; 190 } 191 mQueryString = queryString; 192 if (!mSearchMode) { 193 if (!TextUtils.isEmpty(queryString)) { 194 setSearchMode(true); 195 } 196 } else if (mListener != null) { 197 mListener.onAction(Action.CHANGE_SEARCH_QUERY); 198 } 199 200 return true; 201 } 202 203 @Override 204 public boolean onQueryTextSubmit(String query) { 205 return true; 206 } 207 208 @Override 209 public boolean onClose() { 210 setSearchMode(false); 211 return false; 212 } 213 214 public void onSaveInstanceState(Bundle outState) { 215 outState.putBoolean(EXTRA_KEY_SEARCH_MODE, mSearchMode); 216 outState.putString(EXTRA_KEY_QUERY, mQueryString); 217 outState.putInt(EXTRA_KEY_SELECTED_TAB, mActionBar.getSelectedNavigationIndex()); 218 } 219 220 public void onRestoreInstanceState(Bundle savedState) { 221 mSearchMode = savedState.getBoolean(EXTRA_KEY_SEARCH_MODE); 222 mQueryString = savedState.getString(EXTRA_KEY_QUERY); 223 mActionBar.setSelectedNavigationItem(savedState.getInt(EXTRA_KEY_SELECTED_TAB)); 224 } 225 226 @Override 227 public void onContactListFiltersLoaded() { 228 update(); 229 } 230 231 @Override 232 public void onContactListFilterChanged() { 233 update(); 234 } 235 236 @Override 237 public void onContactListFilterCustomizationRequest() { 238 } 239} 240