ActionBarAdapter.java revision 3e076bb53d39eb3757b376a003c412e4f44b0e3d
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.list.ContactListFilterController; 21import com.android.contacts.list.ContactListFilterController.ContactListFilterListener; 22import com.android.contacts.list.ContactListFilterView; 23import com.android.contacts.list.ContactsRequest; 24 25import android.app.ActionBar; 26import android.app.ActionBar.LayoutParams; 27import android.app.ActionBar.OnNavigationListener; 28import android.content.Context; 29import android.os.Bundle; 30import android.text.TextUtils; 31import android.view.LayoutInflater; 32import android.view.View; 33import android.view.View.OnFocusChangeListener; 34import android.widget.SearchView; 35import android.widget.SearchView.OnCloseListener; 36import android.widget.SearchView.OnQueryChangeListener; 37import android.widget.TextView; 38 39/** 40 * Adapter for the action bar at the top of the Contacts activity. 41 */ 42public class ActionBarAdapter implements OnQueryChangeListener, OnCloseListener, 43 ContactListFilterListener, OnFocusChangeListener { 44 45 public interface Listener { 46 void onAction(); 47 } 48 49 private static final String EXTRA_KEY_SEARCH_MODE = "navBar.searchMode"; 50 private static final String EXTRA_KEY_QUERY = "navBar.query"; 51 52 private boolean mSearchMode; 53 private String mQueryString; 54 55 private View mNavigationBar; 56 private TextView mSearchLabel; 57 private SearchView mSearchView; 58 59 private final Context mContext; 60 61 private Listener mListener; 62 private ContactListFilterView mFilterView; 63 private ContactListFilterController mFilterController; 64 65 private boolean mEnabled; 66 67 public ActionBarAdapter(Context context) { 68 mContext = context; 69 } 70 71 public void onCreate(Bundle savedState, ContactsRequest request, ActionBar actionBar) { 72 mQueryString = null; 73 if (savedState != null) { 74 mSearchMode = savedState.getBoolean(EXTRA_KEY_SEARCH_MODE); 75 mQueryString = savedState.getString(EXTRA_KEY_QUERY); 76 } else { 77 mSearchMode = request.isSearchMode(); 78 mQueryString = request.getQueryString(); 79 } 80 81 actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM); 82 83 mNavigationBar = LayoutInflater.from(mContext).inflate(R.layout.navigation_bar, null); 84 LayoutParams layoutParams = new LayoutParams( 85 LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 86 actionBar.setCustomView(mNavigationBar, layoutParams); 87 88 mFilterView = (ContactListFilterView) mNavigationBar.findViewById(R.id.filter_view); 89 mSearchLabel = (TextView) mNavigationBar.findViewById(R.id.search_label); 90 mSearchView = (SearchView) mNavigationBar.findViewById(R.id.search_view); 91 92 mSearchView.setOnQueryChangeListener(this); 93 mSearchView.setOnCloseListener(this); 94 mSearchView.setOnQueryTextFocusChangeListener(this); 95 mSearchView.setQuery(mQueryString, false); 96 mSearchView.setQueryHint(mContext.getString(R.string.hint_findContacts)); 97 98 update(); 99 } 100 101 public void setEnabled(boolean enabled) { 102 mEnabled = enabled; 103 update(); 104 } 105 106 public void setListener(Listener listener) { 107 mListener = listener; 108 } 109 110 public void setContactListFilterController(ContactListFilterController controller) { 111 mFilterController = controller; 112 mFilterController.setAnchor(mFilterView); 113 mFilterController.addListener(this); 114 } 115 116 @Override 117 public void onFocusChange(View v, boolean hasFocus) { 118 if (v != mSearchView) { 119 return; 120 } 121 122 // When we switch search mode on/off, the activity may need to change 123 // fragments, which may lead to focus temporarily leaving the search 124 // view or coming back to it, which could lead to an infinite loop. 125 // Postponing the change breaks that loop. 126 mNavigationBar.post(new Runnable() { 127 128 @Override 129 public void run() { 130 setSearchMode(mSearchView.hasFocus()); 131 } 132 }); 133 } 134 135 public boolean isSearchMode() { 136 return mSearchMode; 137 } 138 139 public void setSearchMode(boolean flag) { 140 if (mSearchMode != flag) { 141 mSearchMode = flag; 142 update(); 143 if (mSearchMode) { 144 mSearchView.requestFocus(); 145 } else { 146 mSearchView.setQuery(null, false); 147 } 148 if (mListener != null) { 149 mListener.onAction(); 150 } 151 } 152 } 153 154 public String getQueryString() { 155 return mQueryString; 156 } 157 158 public void setQueryString(String query) { 159 mQueryString = query; 160 mSearchView.setQuery(query, false); 161 } 162 163 public void update() { 164 if (!mEnabled) { 165 mNavigationBar.setVisibility(View.GONE); 166 } else if (mSearchMode) { 167 mNavigationBar.setVisibility(View.VISIBLE); 168 mSearchLabel.setVisibility(View.VISIBLE); 169 mFilterView.setVisibility(View.GONE); 170 if (mFilterController != null) { 171 mFilterController.setEnabled(false); 172 } 173 } else { 174 mNavigationBar.setVisibility(View.VISIBLE); 175 mSearchLabel.setVisibility(View.GONE); 176 mFilterView.setVisibility(View.VISIBLE); 177 if (mFilterController != null){ 178 mFilterController.setEnabled(true); 179 if (mFilterController.isLoaded()) { 180 mFilterView.setContactListFilter(mFilterController.getFilter()); 181 mFilterView.setSingleAccount(mFilterController.getAccountCount() == 1); 182 mFilterView.bindView(false); 183 } 184 } 185 } 186 } 187 188 @Override 189 public boolean onQueryTextChanged(String queryString) { 190 mQueryString = queryString; 191 if (!mSearchMode) { 192 if (!TextUtils.isEmpty(queryString)) { 193 setSearchMode(true); 194 } 195 } else if (mListener != null) { 196 mListener.onAction(); 197 } 198 199 return true; 200 } 201 202 @Override 203 public boolean onSubmitQuery(String query) { 204 return true; 205 } 206 207 @Override 208 public boolean onClose() { 209 setSearchMode(false); 210 return false; 211 } 212 213 public void onSaveInstanceState(Bundle outState) { 214 outState.putBoolean(EXTRA_KEY_SEARCH_MODE, mSearchMode); 215 outState.putString(EXTRA_KEY_QUERY, mQueryString); 216 } 217 218 @Override 219 public void onContactListFiltersLoaded() { 220 update(); 221 } 222 223 @Override 224 public void onContactListFilterChanged() { 225 update(); 226 } 227 228 @Override 229 public void onContactListFilterCustomizationRequest() { 230 } 231} 232