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