ActionBarAdapter.java revision 4ba903cdf132a6be2e5efa33187b18ca8446e503
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    private final boolean mAlwaysShowSearchView;
62
63    private Listener mListener;
64    private ContactListFilterController mFilterController;
65
66    private ActionBar mActionBar;
67
68
69    public ActionBarAdapter(Context context, Listener listener) {
70        mContext = context;
71        mListener = listener;
72        mSearchLabelText = mContext.getString(R.string.search_label);
73        mAlwaysShowSearchView = mContext.getResources().getBoolean(R.bool.always_show_search_view);
74    }
75
76    public void onCreate(Bundle savedState, ContactsRequest request, ActionBar actionBar) {
77        mActionBar = actionBar;
78        mQueryString = null;
79
80        if (savedState != null) {
81            mSearchMode = savedState.getBoolean(EXTRA_KEY_SEARCH_MODE);
82            mQueryString = savedState.getString(EXTRA_KEY_QUERY);
83        } else {
84            mSearchMode = request.isSearchMode();
85            mQueryString = request.getQueryString();
86        }
87
88        // Set up search view.
89        View customSearchView = LayoutInflater.from(mContext).inflate(R.layout.custom_action_bar,
90                null);
91        int searchViewWidth = mContext.getResources().getDimensionPixelSize(
92                R.dimen.search_view_width);
93        if (searchViewWidth == 0) {
94            searchViewWidth = LayoutParams.MATCH_PARENT;
95        }
96        LayoutParams layoutParams = new LayoutParams(searchViewWidth, LayoutParams.WRAP_CONTENT);
97        mSearchView = (SearchView) customSearchView.findViewById(R.id.search_view);
98        mSearchView.setQueryHint(mContext.getString(R.string.hint_findContacts));
99        mSearchView.setOnQueryTextListener(this);
100        mSearchView.setOnCloseListener(this);
101        mSearchView.setQuery(mQueryString, false);
102        mActionBar.setCustomView(customSearchView, layoutParams);
103
104        update();
105    }
106
107    public void setListener(Listener listener) {
108        mListener = listener;
109    }
110
111    public void setContactListFilterController(ContactListFilterController controller) {
112        mFilterController = controller;
113        mFilterController.addListener(this);
114    }
115
116    public boolean isSearchMode() {
117        return mSearchMode;
118    }
119
120    public void setSearchMode(boolean flag) {
121        if (mSearchMode != flag) {
122            mSearchMode = flag;
123            update();
124            if (mSearchView == null) {
125                return;
126            }
127            if (mSearchMode) {
128                setFocusOnSearchView();
129            } else {
130                mSearchView.setQuery(null, false);
131            }
132        }
133    }
134
135    public String getQueryString() {
136        return mQueryString;
137    }
138
139    public void setQueryString(String query) {
140        mQueryString = query;
141        if (mSearchView != null) {
142            mSearchView.setQuery(query, false);
143        }
144    }
145
146    public void update() {
147        if (mSearchMode) {
148            mActionBar.setDisplayShowCustomEnabled(true);
149            if (mAlwaysShowSearchView) {
150                // Tablet -- change the app title for the search mode
151                mActionBar.setTitle(mSearchLabelText);
152            } else {
153                // Phone -- search view gets focus
154                setFocusOnSearchView();
155            }
156            mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
157            if (mListener != null) {
158                mListener.onAction(Action.START_SEARCH_MODE);
159            }
160        } else {
161            mActionBar.setDisplayShowCustomEnabled(mAlwaysShowSearchView);
162            mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
163            mActionBar.setTitle(null);
164            if (mListener != null) {
165                mListener.onAction(Action.STOP_SEARCH_MODE);
166            }
167        }
168    }
169
170    @Override
171    public boolean onQueryTextChange(String queryString) {
172        // TODO: Clean up SearchView code because it keeps setting the SearchView query,
173        // invoking onQueryChanged, setting up the fragment again, invalidating the options menu,
174        // storing the SearchView again, and etc... unless we add in the early return statements.
175        if (queryString.equals(mQueryString)) {
176            return false;
177        }
178        mQueryString = queryString;
179        if (!mSearchMode) {
180            if (!TextUtils.isEmpty(queryString)) {
181                setSearchMode(true);
182            }
183        } else if (mListener != null) {
184            mListener.onAction(Action.CHANGE_SEARCH_QUERY);
185        }
186
187        return true;
188    }
189
190    @Override
191    public boolean onQueryTextSubmit(String query) {
192        return true;
193    }
194
195    @Override
196    public boolean onClose() {
197        setSearchMode(false);
198        return false;
199    }
200
201    public void onSaveInstanceState(Bundle outState) {
202        outState.putBoolean(EXTRA_KEY_SEARCH_MODE, mSearchMode);
203        outState.putString(EXTRA_KEY_QUERY, mQueryString);
204        outState.putInt(EXTRA_KEY_SELECTED_TAB, mActionBar.getSelectedNavigationIndex());
205    }
206
207    public void onRestoreInstanceState(Bundle savedState) {
208        mSearchMode = savedState.getBoolean(EXTRA_KEY_SEARCH_MODE);
209        mQueryString = savedState.getString(EXTRA_KEY_QUERY);
210        mActionBar.setSelectedNavigationItem(savedState.getInt(EXTRA_KEY_SELECTED_TAB));
211    }
212
213    @Override
214    public void onContactListFiltersLoaded() {
215        update();
216    }
217
218    @Override
219    public void onContactListFilterChanged() {
220        update();
221    }
222
223    @Override
224    public void onContactListFilterCustomizationRequest() {
225    }
226
227    private void setFocusOnSearchView() {
228        mSearchView.requestFocus();
229        mSearchView.setIconified(false); // Workaround for the "IME not popping up" issue.
230    }
231}
232