ActionBarAdapter.java revision 1ee9df6facd2340416d6a33aeb739707643d2fc3
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
53    private boolean mSearchMode;
54    private String mQueryString;
55
56    private String mSearchLabelText;
57    private SearchView mSearchView;
58
59    private final Context mContext;
60
61    private Listener mListener;
62    private ContactListFilterController mFilterController;
63
64    private ActionBar mActionBar;
65
66    private View mCustomSearchView;
67    private LayoutParams mLayoutParams;
68    private boolean mIsSearchInOverflowMenu;
69
70    public ActionBarAdapter(Context context, Listener listener) {
71        mContext = context;
72        mListener = listener;
73        mSearchLabelText = mContext.getString(R.string.search_label);
74    }
75
76    public void onCreate(Bundle savedState, ContactsRequest request, ActionBar actionBar,
77            boolean searchInOverflowMenu) {
78        mActionBar = actionBar;
79        mQueryString = null;
80        mIsSearchInOverflowMenu = searchInOverflowMenu;
81
82        if (savedState != null) {
83            mSearchMode = savedState.getBoolean(EXTRA_KEY_SEARCH_MODE);
84            mQueryString = savedState.getString(EXTRA_KEY_QUERY);
85        } else {
86            mSearchMode = request.isSearchMode();
87            mQueryString = request.getQueryString();
88        }
89
90        if (mSearchView != null) {
91            mSearchView.setQuery(mQueryString, false);
92        }
93
94        update();
95    }
96
97    public void setSearchView(SearchView searchView) {
98        mSearchView = searchView;
99        mSearchView.setOnQueryTextListener(this);
100        mSearchView.setOnCloseListener(this);
101        mSearchView.setQuery(mQueryString, false);
102    }
103
104    public void setListener(Listener listener) {
105        mListener = listener;
106    }
107
108    public void setContactListFilterController(ContactListFilterController controller) {
109        mFilterController = controller;
110        mFilterController.addListener(this);
111    }
112
113    public boolean isSearchInOverflowMenu() {
114        return mIsSearchInOverflowMenu;
115    }
116
117    public boolean isSearchMode() {
118        return mSearchMode;
119    }
120
121    public void setSearchMode(boolean flag) {
122        if (mSearchMode != flag) {
123            mSearchMode = flag;
124            update();
125            if (mSearchView == null) {
126                return;
127            }
128            if (mSearchMode) {
129                mSearchView.requestFocus();
130            } else {
131                mSearchView.setQuery(null, false);
132            }
133        }
134    }
135
136    public String getQueryString() {
137        return mQueryString;
138    }
139
140    public void setQueryString(String query) {
141        mQueryString = query;
142        if (mSearchView != null) {
143            mSearchView.setQuery(query, false);
144        }
145    }
146
147    public void update() {
148        if (mSearchMode) {
149            // If the search icon was in the overflow menu, then inflate a custom view containing
150            // a search view for the action bar (and hide the tabs).
151            if (mIsSearchInOverflowMenu) {
152                if (mCustomSearchView == null) {
153                    mCustomSearchView = LayoutInflater.from(mContext).inflate(
154                            R.layout.custom_action_bar, null);
155                    mLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
156                            LayoutParams.WRAP_CONTENT);
157                    SearchView searchView = (SearchView) mCustomSearchView.
158                            findViewById(R.id.search_view);
159                    searchView.setQueryHint(mContext.getString(R.string.hint_findContacts));
160                    setSearchView(searchView);
161                }
162                mActionBar.setDisplayShowCustomEnabled(true);
163                mActionBar.setCustomView(mCustomSearchView, mLayoutParams);
164                mSearchView.requestFocus();
165            } else {
166                mActionBar.setTitle(mSearchLabelText);
167            }
168            mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
169            if (mListener != null) {
170                mListener.onAction(Action.START_SEARCH_MODE);
171            }
172        } else {
173            mActionBar.setDisplayShowCustomEnabled(false);
174            mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
175            mActionBar.setTitle(null);
176            if (mListener != null) {
177                mListener.onAction(Action.STOP_SEARCH_MODE);
178            }
179        }
180    }
181
182    @Override
183    public boolean onQueryTextChange(String queryString) {
184        // TODO: Clean up SearchView code because it keeps setting the SearchView query,
185        // invoking onQueryChanged, setting up the fragment again, invalidating the options menu,
186        // storing the SearchView again, and etc... unless we add in the early return statements.
187        if (queryString.equals(mQueryString)) {
188            return false;
189        }
190        mQueryString = queryString;
191        if (!mSearchMode) {
192            if (!TextUtils.isEmpty(queryString)) {
193                setSearchMode(true);
194            }
195        } else if (mListener != null) {
196            mListener.onAction(Action.CHANGE_SEARCH_QUERY);
197        }
198
199        return true;
200    }
201
202    @Override
203    public boolean onQueryTextSubmit(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    public void onRestoreInstanceState(Bundle savedState) {
219        mSearchMode = savedState.getBoolean(EXTRA_KEY_SEARCH_MODE);
220        mQueryString = savedState.getString(EXTRA_KEY_QUERY);
221    }
222
223    @Override
224    public void onContactListFiltersLoaded() {
225        update();
226    }
227
228    @Override
229    public void onContactListFilterChanged() {
230        update();
231    }
232
233    @Override
234    public void onContactListFilterCustomizationRequest() {
235    }
236}
237