ActionBarAdapter.java revision 9d2a74ef7b1817ad0708e41151358f5899aeeb67
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.ContactsRequest; 22 23import android.app.ActionBar; 24import android.app.ActionBar.LayoutParams; 25import android.app.ActionBar.Tab; 26import android.app.FragmentTransaction; 27import android.content.Context; 28import android.content.SharedPreferences; 29import android.os.Bundle; 30import android.preference.PreferenceManager; 31import android.text.TextUtils; 32import android.view.LayoutInflater; 33import android.view.View; 34import android.view.inputmethod.InputMethodManager; 35import android.widget.SearchView; 36import android.widget.SearchView.OnCloseListener; 37import android.widget.SearchView.OnQueryTextListener; 38 39/** 40 * Adapter for the action bar at the top of the Contacts activity. 41 */ 42public class ActionBarAdapter implements OnQueryTextListener, OnCloseListener { 43 44 public interface Listener { 45 public enum Action { 46 CHANGE_SEARCH_QUERY, START_SEARCH_MODE, STOP_SEARCH_MODE 47 } 48 49 void onAction(Action action); 50 51 /** 52 * Called when the user selects a tab. The new tab can be obtained using 53 * {@link #getCurrentTab}. 54 */ 55 void onSelectedTabChanged(); 56 } 57 58 private static final String EXTRA_KEY_SEARCH_MODE = "navBar.searchMode"; 59 private static final String EXTRA_KEY_QUERY = "navBar.query"; 60 private static final String EXTRA_KEY_SELECTED_TAB = "navBar.selectedTab"; 61 62 private static final String PERSISTENT_LAST_TAB = "actionBarAdapter.lastTab"; 63 64 private boolean mSearchMode; 65 private String mQueryString; 66 67 private SearchView mSearchView; 68 69 private final Context mContext; 70 private final SharedPreferences mPrefs; 71 72 private Listener mListener; 73 74 private final ActionBar mActionBar; 75 private final MyTabListener mTabListener = new MyTabListener(); 76 77 private boolean mShowHomeIcon; 78 79 public enum TabState { 80 GROUPS, 81 ALL, 82 FAVORITES; 83 84 public static TabState fromInt(int value) { 85 if (GROUPS.ordinal() == value) { 86 return GROUPS; 87 } 88 if (ALL.ordinal() == value) { 89 return ALL; 90 } 91 if (FAVORITES.ordinal() == value) { 92 return FAVORITES; 93 } 94 throw new IllegalArgumentException("Invalid value: " + value); 95 } 96 } 97 98 private static final TabState DEFAULT_TAB = TabState.ALL; 99 private TabState mCurrentTab = DEFAULT_TAB; 100 101 public ActionBarAdapter(Context context, Listener listener, ActionBar actionBar) { 102 mContext = context; 103 mListener = listener; 104 mActionBar = actionBar; 105 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext); 106 107 mShowHomeIcon = mContext.getResources().getBoolean(R.bool.show_home_icon); 108 109 // Set up search view. 110 View customSearchView = LayoutInflater.from(mContext).inflate(R.layout.custom_action_bar, 111 null); 112 int searchViewWidth = mContext.getResources().getDimensionPixelSize( 113 R.dimen.search_view_width); 114 if (searchViewWidth == 0) { 115 searchViewWidth = LayoutParams.MATCH_PARENT; 116 } 117 LayoutParams layoutParams = new LayoutParams(searchViewWidth, LayoutParams.WRAP_CONTENT); 118 mSearchView = (SearchView) customSearchView.findViewById(R.id.search_view); 119 // Since the {@link SearchView} in this app is "click-to-expand", set the below mode on the 120 // {@link SearchView} so that the magnifying glass icon appears inside the editable text 121 // field. (In the "click-to-expand" search pattern, the user must explicitly expand the 122 // search field and already knows a search is being conducted, so the icon is redundant 123 // and can go away once the user starts typing.) 124 mSearchView.setIconifiedByDefault(true); 125 mSearchView.setQueryHint(mContext.getString(R.string.hint_findContacts)); 126 mSearchView.setOnQueryTextListener(this); 127 mSearchView.setOnCloseListener(this); 128 mSearchView.setQuery(mQueryString, false); 129 mActionBar.setCustomView(customSearchView, layoutParams); 130 131 // Set up tabs 132 addTab(TabState.GROUPS, mContext.getString(R.string.contactsGroupsLabel)); 133 addTab(TabState.ALL, mContext.getString(R.string.contactsAllLabel)); 134 addTab(TabState.FAVORITES, mContext.getString(R.string.contactsFavoritesLabel)); 135 } 136 137 public void initialize(Bundle savedState, ContactsRequest request) { 138 if (savedState == null) { 139 mSearchMode = request.isSearchMode(); 140 mQueryString = request.getQueryString(); 141 mCurrentTab = loadLastTabPreference(); 142 } else { 143 mSearchMode = savedState.getBoolean(EXTRA_KEY_SEARCH_MODE); 144 mQueryString = savedState.getString(EXTRA_KEY_QUERY); 145 146 // Just set to the field here. The listener will be notified by update(). 147 mCurrentTab = TabState.fromInt(savedState.getInt(EXTRA_KEY_SELECTED_TAB)); 148 } 149 update(); 150 } 151 152 public void setListener(Listener listener) { 153 mListener = listener; 154 } 155 156 private void addTab(TabState tabState, String text) { 157 final Tab tab = mActionBar.newTab(); 158 tab.setTag(tabState); 159 tab.setText(text); 160 tab.setTabListener(mTabListener); 161 mActionBar.addTab(tab); 162 } 163 164 private class MyTabListener implements ActionBar.TabListener { 165 /** 166 * If true, it won't call {@link #setCurrentTab} in {@link #onTabSelected}. 167 * This flag is used when we want to programmatically update the current tab without 168 * {@link #onTabSelected} getting called. 169 */ 170 public boolean mIgnoreTabSelected; 171 172 @Override public void onTabReselected(Tab tab, FragmentTransaction ft) { } 173 @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) { } 174 175 @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { 176 if (!mIgnoreTabSelected) { 177 setCurrentTab((TabState)tab.getTag()); 178 } 179 } 180 } 181 182 /** 183 * Change the current tab, and notify the listener. 184 */ 185 public void setCurrentTab(TabState tab) { 186 setCurrentTab(tab, true); 187 } 188 189 /** 190 * Change the current tab 191 */ 192 public void setCurrentTab(TabState tab, boolean notifyListener) { 193 if (tab == null) throw new NullPointerException(); 194 if (tab == mCurrentTab) { 195 return; 196 } 197 mCurrentTab = tab; 198 199 int index = mCurrentTab.ordinal(); 200 if ((mActionBar.getNavigationMode() == ActionBar.NAVIGATION_MODE_TABS) 201 && (index != mActionBar.getSelectedNavigationIndex())) { 202 mActionBar.setSelectedNavigationItem(index); 203 } 204 205 if (notifyListener && mListener != null) mListener.onSelectedTabChanged(); 206 saveLastTabPreference(mCurrentTab); 207 } 208 209 public TabState getCurrentTab() { 210 return mCurrentTab; 211 } 212 213 public boolean isSearchMode() { 214 return mSearchMode; 215 } 216 217 public boolean shouldShowSearchResult() { 218 return mSearchMode && !TextUtils.isEmpty(mQueryString); 219 } 220 221 public void setSearchMode(boolean flag) { 222 if (mSearchMode != flag) { 223 mSearchMode = flag; 224 update(); 225 if (mSearchView == null) { 226 return; 227 } 228 if (mSearchMode) { 229 setFocusOnSearchView(); 230 } else { 231 mSearchView.setQuery(null, false); 232 } 233 } 234 } 235 236 public String getQueryString() { 237 return mQueryString; 238 } 239 240 public void setQueryString(String query) { 241 mQueryString = query; 242 if (mSearchView != null) { 243 mSearchView.setQuery(query, false); 244 } 245 } 246 247 /** @return true if the "UP" icon is showing. */ 248 public boolean isUpShowing() { 249 return mSearchMode; // Only shown on the search mode. 250 } 251 252 private void updateDisplayOptions() { 253 // All the flags we may change in this method. 254 final int MASK = ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME 255 | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_CUSTOM; 256 257 // The current flags set to the action bar. (only the ones that we may change here) 258 final int current = mActionBar.getDisplayOptions() & MASK; 259 260 // Build the new flags... 261 int newFlags = 0; 262 newFlags |= ActionBar.DISPLAY_SHOW_TITLE; 263 if (mShowHomeIcon) { 264 newFlags |= ActionBar.DISPLAY_SHOW_HOME; 265 } 266 if (mSearchMode) { 267 newFlags |= ActionBar.DISPLAY_SHOW_HOME; 268 newFlags |= ActionBar.DISPLAY_HOME_AS_UP; 269 newFlags |= ActionBar.DISPLAY_SHOW_CUSTOM; 270 } 271 mActionBar.setHomeButtonEnabled(mSearchMode); 272 273 if (current != newFlags) { 274 // Pass the mask here to preserve other flags that we're not interested here. 275 mActionBar.setDisplayOptions(newFlags, MASK); 276 } 277 } 278 279 private void update() { 280 if (mSearchMode) { 281 setFocusOnSearchView(); 282 // Since we have the {@link SearchView} in a custom action bar, we must manually handle 283 // expanding the {@link SearchView} when a search is initiated. 284 mSearchView.onActionViewExpanded(); 285 if (mActionBar.getNavigationMode() != ActionBar.NAVIGATION_MODE_STANDARD) { 286 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 287 } 288 if (mListener != null) { 289 mListener.onAction(Action.START_SEARCH_MODE); 290 } 291 } else { 292 if (mActionBar.getNavigationMode() != ActionBar.NAVIGATION_MODE_TABS) { 293 // setNavigationMode will trigger onTabSelected() with the tab which was previously 294 // selected. 295 // The issue is that when we're first switching to the tab navigation mode after 296 // screen orientation changes, onTabSelected() will get called with the first tab 297 // (i.e. favorite), which would results in mCurrentTab getting set to FAVORITES and 298 // we'd lose restored tab. 299 // So let's just disable the callback here temporarily. We'll notify the listener 300 // after this anyway. 301 mTabListener.mIgnoreTabSelected = true; 302 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 303 mActionBar.setSelectedNavigationItem(mCurrentTab.ordinal()); 304 mTabListener.mIgnoreTabSelected = false; 305 } 306 mActionBar.setTitle(null); 307 // Since we have the {@link SearchView} in a custom action bar, we must manually handle 308 // collapsing the {@link SearchView} when search mode is exited. 309 mSearchView.onActionViewCollapsed(); 310 if (mListener != null) { 311 mListener.onAction(Action.STOP_SEARCH_MODE); 312 mListener.onSelectedTabChanged(); 313 } 314 } 315 updateDisplayOptions(); 316 } 317 318 @Override 319 public boolean onQueryTextChange(String queryString) { 320 // TODO: Clean up SearchView code because it keeps setting the SearchView query, 321 // invoking onQueryChanged, setting up the fragment again, invalidating the options menu, 322 // storing the SearchView again, and etc... unless we add in the early return statements. 323 if (queryString.equals(mQueryString)) { 324 return false; 325 } 326 mQueryString = queryString; 327 if (!mSearchMode) { 328 if (!TextUtils.isEmpty(queryString)) { 329 setSearchMode(true); 330 } 331 } else if (mListener != null) { 332 mListener.onAction(Action.CHANGE_SEARCH_QUERY); 333 } 334 335 return true; 336 } 337 338 @Override 339 public boolean onQueryTextSubmit(String query) { 340 // When the search is "committed" by the user, then hide the keyboard so the user can 341 // more easily browse the list of results. 342 if (mSearchView != null) { 343 InputMethodManager imm = (InputMethodManager) mContext.getSystemService( 344 Context.INPUT_METHOD_SERVICE); 345 if (imm != null) { 346 imm.hideSoftInputFromWindow(mSearchView.getWindowToken(), 0); 347 } 348 mSearchView.clearFocus(); 349 } 350 return true; 351 } 352 353 @Override 354 public boolean onClose() { 355 setSearchMode(false); 356 return false; 357 } 358 359 public void onSaveInstanceState(Bundle outState) { 360 outState.putBoolean(EXTRA_KEY_SEARCH_MODE, mSearchMode); 361 outState.putString(EXTRA_KEY_QUERY, mQueryString); 362 outState.putInt(EXTRA_KEY_SELECTED_TAB, mCurrentTab.ordinal()); 363 } 364 365 private void setFocusOnSearchView() { 366 mSearchView.requestFocus(); 367 mSearchView.setIconified(false); // Workaround for the "IME not popping up" issue. 368 } 369 370 private void saveLastTabPreference(TabState tab) { 371 mPrefs.edit().putInt(PERSISTENT_LAST_TAB, tab.ordinal()).apply(); 372 } 373 374 private TabState loadLastTabPreference() { 375 try { 376 return TabState.fromInt(mPrefs.getInt(PERSISTENT_LAST_TAB, DEFAULT_TAB.ordinal())); 377 } catch (IllegalArgumentException e) { 378 // Preference is corrupt? 379 return DEFAULT_TAB; 380 } 381 } 382} 383