ContactPickerFragment.java revision e8a9517483cfa0c4d521b834d872a8cb05482bad
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 */
16package com.android.contacts.list;
17
18import com.android.contacts.ContactsSearchManager;
19import com.android.contacts.R;
20import com.android.contacts.list.ShortcutIntentBuilder.OnShortcutIntentCreatedListener;
21
22import android.content.Intent;
23import android.net.Uri;
24import android.view.LayoutInflater;
25import android.view.View;
26import android.view.ViewGroup;
27import android.widget.AdapterView;
28
29/**
30 * Fragment for the contact list used for browsing contacts (as compared to
31 * picking a contact with one of the PICK or SHORTCUT intents).
32 */
33public class ContactPickerFragment extends ContactEntryListFragment<ContactEntryListAdapter>
34        implements OnShortcutIntentCreatedListener {
35
36    private OnContactPickerActionListener mListener;
37    private boolean mCreateContactEnabled;
38    private boolean mShortcutRequested;
39
40    public ContactPickerFragment() {
41        setPhotoLoaderEnabled(true);
42        setSectionHeaderDisplayEnabled(true);
43        setAizyEnabled(true);
44    }
45
46    public void setOnContactPickerActionListener(OnContactPickerActionListener listener) {
47        mListener = listener;
48    }
49
50    public boolean isCreateContactEnabled() {
51        return mCreateContactEnabled;
52    }
53
54    public void setCreateContactEnabled(boolean flag) {
55        this.mCreateContactEnabled = flag;
56    }
57
58    public boolean isShortcutRequested() {
59        return mShortcutRequested;
60    }
61
62    public void setShortcutRequested(boolean flag) {
63        mShortcutRequested = flag;
64    }
65
66    @Override
67    protected void onCreateView(LayoutInflater inflater, ViewGroup container) {
68        super.onCreateView(inflater, container);
69        if (mCreateContactEnabled) {
70            getListView().addHeaderView(inflater.inflate(R.layout.create_new_contact, null, false));
71        }
72    }
73
74    @Override
75    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
76        if (position == 0 && !isSearchMode() && mCreateContactEnabled) {
77            mListener.onCreateNewContactAction();
78        } else {
79            super.onItemClick(parent, view, position, id);
80        }
81    }
82
83    @Override
84    protected void onItemClick(int position, long id) {
85        Uri uri;
86        if (isLegacyCompatibilityMode()) {
87            uri = ((LegacyContactListAdapter)getAdapter()).getPersonUri(position);
88        } else {
89            uri = ((ContactListAdapter)getAdapter()).getContactUri(position);
90        }
91        if (mShortcutRequested) {
92            ShortcutIntentBuilder builder = new ShortcutIntentBuilder(getActivity(), this);
93            builder.createContactShortcutIntent(uri);
94        } else {
95            mListener.onPickContactAction(uri);
96        }
97    }
98
99    @Override
100    protected ContactEntryListAdapter createListAdapter() {
101        if (!isLegacyCompatibilityMode()) {
102            ContactListAdapter adapter = new DefaultContactListAdapter(getActivity());
103            adapter.setSectionHeaderDisplayEnabled(true);
104            adapter.setDisplayPhotos(true);
105            adapter.setQuickContactEnabled(false);
106            return adapter;
107        } else {
108            LegacyContactListAdapter adapter = new LegacyContactListAdapter(getActivity());
109            adapter.setSectionHeaderDisplayEnabled(false);
110            adapter.setDisplayPhotos(false);
111            return adapter;
112        }
113    }
114
115    @Override
116    protected void configureAdapter() {
117        super.configureAdapter();
118
119        ContactEntryListAdapter adapter = getAdapter();
120        if (adapter instanceof DefaultContactListAdapter) {
121            ((DefaultContactListAdapter)adapter).setVisibleContactsOnly(true);
122        }
123
124        // If "Create new contact" is shown, don't display the empty list UI
125        adapter.setEmptyListEnabled(!isCreateContactEnabled());
126    }
127
128    @Override
129    protected View inflateView(LayoutInflater inflater, ViewGroup container) {
130        return inflater.inflate(R.layout.contacts_list_content, null);
131    }
132
133    @Override
134    protected void prepareEmptyView() {
135        if (isSearchMode()) {
136            return;
137        } else if (isSearchResultsMode()) {
138            setEmptyText(R.string.noMatchingContacts);
139        } else if (isSyncActive()) {
140            if (mShortcutRequested) {
141                // Help text is the same no matter whether there is SIM or not.
142                setEmptyText(R.string.noContactsHelpTextWithSyncForCreateShortcut);
143            } else if (hasIccCard()) {
144                setEmptyText(R.string.noContactsHelpTextWithSync);
145            } else {
146                setEmptyText(R.string.noContactsNoSimHelpTextWithSync);
147            }
148        } else {
149            if (mShortcutRequested) {
150                // Help text is the same no matter whether there is SIM or not.
151                setEmptyText(R.string.noContactsHelpTextWithSyncForCreateShortcut);
152            } else if (hasIccCard()) {
153                setEmptyText(R.string.noContactsHelpText);
154            } else {
155                setEmptyText(R.string.noContactsNoSimHelpText);
156            }
157        }
158    }
159
160    public void onShortcutIntentCreated(Uri uri, Intent shortcutIntent) {
161        mListener.onShortcutIntentCreated(shortcutIntent);
162    }
163
164    @Override
165    public void startSearch(String initialQuery) {
166        ContactsSearchManager.startSearchForResult(getActivity(), initialQuery,
167                ACTIVITY_REQUEST_CODE_PICKER, getContactsRequest());
168    }
169
170    @Override
171    public void onPickerResult(Intent data) {
172        mListener.onPickContactAction(data.getData());
173    }
174}
175