ActionBarAdapter.java revision 15ccbb4d22fd65165cacb7970cbe61de1aa9aac4
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.ContactsRequest;
22import com.android.contacts.widget.NotifyingSpinner;
23
24import android.app.ActionBar;
25import android.content.Context;
26import android.os.Bundle;
27import android.text.TextUtils;
28import android.view.LayoutInflater;
29import android.view.View;
30import android.widget.SearchView;
31import android.widget.SearchView.OnCloseListener;
32import android.widget.SearchView.OnQueryChangeListener;
33import android.widget.TextView;
34
35/**
36 * Adapter for the action bar at the top of the Contacts activity.
37 */
38public class ActionBarAdapter implements OnQueryChangeListener, OnCloseListener {
39
40    public interface Listener {
41        void onAction();
42    }
43
44    private static final String EXTRA_KEY_SEARCH_MODE = "navBar.searchMode";
45    private static final String EXTRA_KEY_QUERY = "navBar.query";
46
47    private static final String KEY_MODE_DEFAULT = "mode_default";
48    private static final String KEY_MODE_SEARCH = "mode_search";
49
50    private boolean mSearchMode;
51    private String mQueryString;
52    private Bundle mSavedStateForSearchMode;
53    private Bundle mSavedStateForDefaultMode;
54
55    private View mNavigationBar;
56    private TextView mSearchLabel;
57    private SearchView mSearchView;
58
59    private final Context mContext;
60
61    private Listener mListener;
62    private NotifyingSpinner mFilterSpinner;
63
64    public ActionBarAdapter(Context context) {
65        mContext = context;
66    }
67
68    public void onCreate(Bundle savedState, ContactsRequest request, ActionBar actionBar) {
69        mQueryString = null;
70        if (savedState != null) {
71            mSearchMode = savedState.getBoolean(EXTRA_KEY_SEARCH_MODE);
72            mQueryString = savedState.getString(EXTRA_KEY_QUERY);
73            mSavedStateForDefaultMode = savedState.getParcelable(KEY_MODE_DEFAULT);
74            mSavedStateForSearchMode = savedState.getParcelable(KEY_MODE_SEARCH);
75        } else {
76            mSearchMode = request.isSearchMode();
77            mQueryString = request.getQueryString();
78        }
79
80        mNavigationBar = LayoutInflater.from(mContext).inflate(R.layout.navigation_bar, null);
81        actionBar.setCustomNavigationMode(mNavigationBar);
82
83        mFilterSpinner = (NotifyingSpinner) mNavigationBar.findViewById(R.id.filter_spinner);
84        mSearchLabel = (TextView) mNavigationBar.findViewById(R.id.search_label);
85        mSearchView = (SearchView) mNavigationBar.findViewById(R.id.search_view);
86        mSearchView.setIconifiedByDefault(false);
87        mSearchView.setEnabled(false);
88        mSearchView.setOnQueryChangeListener(this);
89        mSearchView.setOnCloseListener(this);
90        mSearchView.setQuery(mQueryString, false);
91
92        updateVisibility();
93    }
94
95    public void setListener(Listener listener) {
96        mListener = listener;
97    }
98
99    public void setContactListFilterController(ContactListFilterController controller) {
100        controller.setFilterSpinner(mFilterSpinner);
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            updateVisibility();
111            if (mListener != null) {
112                mListener.onAction();
113            }
114        }
115    }
116
117    public String getQueryString() {
118        return mQueryString;
119    }
120
121    public void setQueryString(String query) {
122        mQueryString = query;
123        mSearchView.setQuery(query, false);
124    }
125
126    public void updateVisibility() {
127        if (mSearchMode) {
128            mSearchLabel.setVisibility(View.VISIBLE);
129            mFilterSpinner.setVisibility(View.GONE);
130        } else {
131            mSearchLabel.setVisibility(View.GONE);
132            mFilterSpinner.setVisibility(View.VISIBLE);
133        }
134    }
135
136    @Override
137    public boolean onQueryTextChanged(String queryString) {
138        mQueryString = queryString;
139        mSearchMode = !TextUtils.isEmpty(queryString);
140        updateVisibility();
141        if (mListener != null) {
142            mListener.onAction();
143        }
144        return true;
145    }
146
147    @Override
148    public boolean onSubmitQuery(String query) {
149        return true;
150    }
151
152    @Override
153    public boolean onClose() {
154        setSearchMode(false);
155        return false;
156    }
157
158    public Bundle getSavedStateForSearchMode() {
159        return mSavedStateForSearchMode;
160    }
161
162    public void setSavedStateForSearchMode(Bundle state) {
163        mSavedStateForSearchMode = state;
164    }
165
166    public Bundle getSavedStateForDefaultMode() {
167        return mSavedStateForDefaultMode;
168    }
169
170    public void setSavedStateForDefaultMode(Bundle state) {
171        mSavedStateForDefaultMode = state;
172    }
173
174    public void onSaveInstanceState(Bundle outState) {
175        outState.putBoolean(EXTRA_KEY_SEARCH_MODE, mSearchMode);
176        outState.putString(EXTRA_KEY_QUERY, mQueryString);
177        if (mSavedStateForDefaultMode != null) {
178            outState.putParcelable(KEY_MODE_DEFAULT, mSavedStateForDefaultMode);
179        }
180        if (mSavedStateForSearchMode != null) {
181            outState.putParcelable(KEY_MODE_SEARCH, mSavedStateForSearchMode);
182        }
183    }
184}
185