ActionBarAdapter.java revision c5792ae6760291d435e91e0fdd28ece6b3311439
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.OnQueryTextListener;
36import android.widget.TextView;
37
38/**
39 * Adapter for the action bar at the top of the Contacts activity.
40 */
41public class ActionBarAdapter implements OnQueryTextListener, 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.setOnQueryTextListener(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 && hasFocus) {
123            setSearchMode(true);
124        }
125    }
126
127    public boolean isSearchMode() {
128        return mSearchMode;
129    }
130
131    public void setSearchMode(boolean flag) {
132        if (mSearchMode != flag) {
133            mSearchMode = flag;
134            update();
135            if (mSearchMode) {
136                mSearchView.requestFocus();
137            } else {
138                mSearchView.setQuery(null, false);
139            }
140            if (mListener != null) {
141                mListener.onAction();
142            }
143        }
144    }
145
146    public String getQueryString() {
147        return mQueryString;
148    }
149
150    public void setQueryString(String query) {
151        mQueryString = query;
152        mSearchView.setQuery(query, false);
153    }
154
155    public void update() {
156        if (!mEnabled) {
157            mNavigationBar.setVisibility(View.GONE);
158        } else if (mSearchMode) {
159            mNavigationBar.setVisibility(View.VISIBLE);
160            mSearchLabel.setVisibility(View.VISIBLE);
161            mFilterView.setVisibility(View.GONE);
162            if (mFilterController != null) {
163                mFilterController.setEnabled(false);
164            }
165        } else {
166            mNavigationBar.setVisibility(View.VISIBLE);
167            mSearchLabel.setVisibility(View.GONE);
168            mFilterView.setVisibility(View.VISIBLE);
169            if (mFilterController != null){
170                mFilterController.setEnabled(true);
171                if (mFilterController.isLoaded()) {
172                    mFilterView.setContactListFilter(mFilterController.getFilter());
173                    mFilterView.setSingleAccount(mFilterController.getAccountCount() == 1);
174                    mFilterView.bindView(false);
175                }
176            }
177        }
178    }
179
180    @Override
181    public boolean onQueryTextChange(String queryString) {
182        mQueryString = queryString;
183        if (!mSearchMode) {
184            if (!TextUtils.isEmpty(queryString)) {
185                setSearchMode(true);
186            }
187        } else if (mListener != null) {
188            mListener.onAction();
189        }
190
191        return true;
192    }
193
194    @Override
195    public boolean onQueryTextSubmit(String query) {
196        return true;
197    }
198
199    @Override
200    public boolean onClose() {
201        setSearchMode(false);
202        return false;
203    }
204
205    public void onSaveInstanceState(Bundle outState) {
206        outState.putBoolean(EXTRA_KEY_SEARCH_MODE, mSearchMode);
207        outState.putString(EXTRA_KEY_QUERY, mQueryString);
208    }
209
210    public void onRestoreInstanceState(Bundle savedState) {
211        mSearchMode = savedState.getBoolean(EXTRA_KEY_SEARCH_MODE);
212        mQueryString = savedState.getString(EXTRA_KEY_QUERY);
213    }
214
215    @Override
216    public void onContactListFiltersLoaded() {
217        update();
218    }
219
220    @Override
221    public void onContactListFilterChanged() {
222        update();
223    }
224
225    @Override
226    public void onContactListFilterCustomizationRequest() {
227    }
228}
229