ActionBarAdapter.java revision f3f933a798c9cfffb1c1c0cc770187b5b0b763eb
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.content.Context;
27import android.os.Bundle;
28import android.text.TextUtils;
29import android.widget.SearchView;
30import android.widget.SearchView.OnCloseListener;
31import android.widget.SearchView.OnQueryTextListener;
32
33/**
34 * Adapter for the action bar at the top of the Contacts activity.
35 */
36public class ActionBarAdapter
37        implements OnQueryTextListener, OnCloseListener, ContactListFilterListener {
38
39    public interface Listener {
40        public enum Action {
41            CHANGE_SEARCH_QUERY, START_SEARCH_MODE, STOP_SEARCH_MODE
42        }
43
44        void onAction(Action action);
45    }
46
47    private static final String EXTRA_KEY_SEARCH_MODE = "navBar.searchMode";
48    private static final String EXTRA_KEY_QUERY = "navBar.query";
49
50    private boolean mSearchMode;
51    private String mQueryString;
52
53    private String mSearchLabelText;
54    private SearchView mSearchView;
55
56    private final Context mContext;
57
58    private Listener mListener;
59    private ContactListFilterController mFilterController;
60
61    private ActionBar mActionBar;
62
63    public ActionBarAdapter(Context context) {
64        mContext = context;
65        mSearchLabelText = mContext.getString(R.string.search_label);
66    }
67
68    public void onCreate(Bundle savedState, ContactsRequest request, ActionBar actionBar) {
69        mActionBar = actionBar;
70        mQueryString = null;
71
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 (mSearchView != null) {
81            mSearchView.setQuery(mQueryString, false);
82        }
83
84        update();
85    }
86
87    public void setSearchView(SearchView searchView) {
88        mSearchView = searchView;
89        mSearchView.setOnQueryTextListener(this);
90        mSearchView.setOnCloseListener(this);
91        mSearchView.setQuery(mQueryString, false);
92    }
93
94    public void setListener(Listener listener) {
95        mListener = listener;
96    }
97
98    public void setContactListFilterController(ContactListFilterController controller) {
99        mFilterController = controller;
100        mFilterController.addListener(this);
101    }
102
103    public boolean isSearchMode() {
104        return mSearchMode;
105    }
106
107    public void setSearchMode(boolean flag) {
108        if (mSearchMode != flag) {
109            mSearchMode = flag;
110            update();
111            if (mSearchView == null) {
112                return;
113            }
114            if (mSearchMode) {
115                mSearchView.requestFocus();
116            } else {
117                mSearchView.setQuery(null, false);
118            }
119        }
120    }
121
122    public String getQueryString() {
123        return mQueryString;
124    }
125
126    public void setQueryString(String query) {
127        mQueryString = query;
128        if (mSearchView != null) {
129            mSearchView.setQuery(query, false);
130        }
131    }
132
133    public void update() {
134        if (mSearchMode) {
135            mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
136            mActionBar.setTitle(mSearchLabelText);
137            if (mListener != null) {
138                mListener.onAction(Action.START_SEARCH_MODE);
139            }
140        } else {
141            mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
142            mActionBar.setTitle(null);
143            if (mListener != null) {
144                mListener.onAction(Action.STOP_SEARCH_MODE);
145            }
146        }
147    }
148
149    @Override
150    public boolean onQueryTextChange(String queryString) {
151        // TODO: Clean up SearchView code because it keeps setting the SearchView query,
152        // invoking onQueryChanged, setting up the fragment again, invalidating the options menu,
153        // storing the SearchView again, and etc... unless we add in the early return statements.
154        if (queryString.equals(mQueryString)) {
155            return false;
156        }
157        mQueryString = queryString;
158        if (!mSearchMode) {
159            if (!TextUtils.isEmpty(queryString)) {
160                setSearchMode(true);
161            }
162        } else if (mListener != null) {
163            mListener.onAction(Action.CHANGE_SEARCH_QUERY);
164        }
165
166        return true;
167    }
168
169    @Override
170    public boolean onQueryTextSubmit(String query) {
171        return true;
172    }
173
174    @Override
175    public boolean onClose() {
176        setSearchMode(false);
177        return false;
178    }
179
180    public void onSaveInstanceState(Bundle outState) {
181        outState.putBoolean(EXTRA_KEY_SEARCH_MODE, mSearchMode);
182        outState.putString(EXTRA_KEY_QUERY, mQueryString);
183    }
184
185    public void onRestoreInstanceState(Bundle savedState) {
186        mSearchMode = savedState.getBoolean(EXTRA_KEY_SEARCH_MODE);
187        mQueryString = savedState.getString(EXTRA_KEY_QUERY);
188    }
189
190    @Override
191    public void onContactListFiltersLoaded() {
192        update();
193    }
194
195    @Override
196    public void onContactListFilterChanged() {
197        update();
198    }
199
200    @Override
201    public void onContactListFilterCustomizationRequest() {
202    }
203}
204