ContactPickerFragment.java revision 84b3cc7fd565ff911ac46a763fdc2f5740a18001
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        setQuickContactEnabled(false);
45    }
46
47    public void setOnContactPickerActionListener(OnContactPickerActionListener listener) {
48        mListener = listener;
49    }
50
51    public boolean isCreateContactEnabled() {
52        return mCreateContactEnabled;
53    }
54
55    public void setCreateContactEnabled(boolean flag) {
56        this.mCreateContactEnabled = flag;
57    }
58
59    public boolean isShortcutRequested() {
60        return mShortcutRequested;
61    }
62
63    public void setShortcutRequested(boolean flag) {
64        mShortcutRequested = flag;
65    }
66
67    @Override
68    protected void onCreateView(LayoutInflater inflater, ViewGroup container) {
69        super.onCreateView(inflater, container);
70        if (mCreateContactEnabled) {
71            getListView().addHeaderView(inflater.inflate(R.layout.create_new_contact, null, false));
72        }
73    }
74
75    @Override
76    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
77        if (position == 0 && !isSearchMode() && mCreateContactEnabled) {
78            mListener.onCreateNewContactAction();
79        } else {
80            super.onItemClick(parent, view, position, id);
81        }
82    }
83
84    @Override
85    protected void onItemClick(int position, long id) {
86        Uri uri;
87        if (isLegacyCompatibilityMode()) {
88            uri = ((LegacyContactListAdapter)getAdapter()).getPersonUri(position);
89        } else {
90            uri = ((ContactListAdapter)getAdapter()).getContactUri(position);
91        }
92        if (mShortcutRequested) {
93            ShortcutIntentBuilder builder = new ShortcutIntentBuilder(getActivity(), this);
94            builder.createContactShortcutIntent(uri);
95        } else {
96            mListener.onPickContactAction(uri);
97        }
98    }
99
100    @Override
101    protected ContactEntryListAdapter createListAdapter() {
102        if (!isLegacyCompatibilityMode()) {
103            ContactListAdapter adapter = new DefaultContactListAdapter(getActivity());
104            adapter.setSectionHeaderDisplayEnabled(true);
105            adapter.setDisplayPhotos(true);
106            adapter.setQuickContactEnabled(false);
107            return adapter;
108        } else {
109            LegacyContactListAdapter adapter = new LegacyContactListAdapter(getActivity());
110            adapter.setSectionHeaderDisplayEnabled(false);
111            adapter.setDisplayPhotos(false);
112            return adapter;
113        }
114    }
115
116    @Override
117    protected void configureAdapter() {
118        super.configureAdapter();
119
120        ContactEntryListAdapter adapter = getAdapter();
121
122        // If "Create new contact" is shown, don't display the empty list UI
123        adapter.setEmptyListEnabled(!isCreateContactEnabled());
124    }
125
126    @Override
127    protected View inflateView(LayoutInflater inflater, ViewGroup container) {
128        return inflater.inflate(R.layout.contact_picker_content, null);
129    }
130
131    @Override
132    protected void prepareEmptyView() {
133        if (isSearchMode()) {
134            return;
135        } else if (isSyncActive()) {
136            if (mShortcutRequested) {
137                // Help text is the same no matter whether there is SIM or not.
138                setEmptyText(R.string.noContactsHelpTextWithSyncForCreateShortcut);
139            } else if (hasIccCard()) {
140                setEmptyText(R.string.noContactsHelpTextWithSync);
141            } else {
142                setEmptyText(R.string.noContactsNoSimHelpTextWithSync);
143            }
144        } else {
145            if (mShortcutRequested) {
146                // Help text is the same no matter whether there is SIM or not.
147                setEmptyText(R.string.noContactsHelpTextWithSyncForCreateShortcut);
148            } else if (hasIccCard()) {
149                setEmptyText(R.string.noContactsHelpText);
150            } else {
151                setEmptyText(R.string.noContactsNoSimHelpText);
152            }
153        }
154    }
155
156    public void onShortcutIntentCreated(Uri uri, Intent shortcutIntent) {
157        mListener.onShortcutIntentCreated(shortcutIntent);
158    }
159
160    @Override
161    public void startSearch(String initialQuery) {
162        ContactsSearchManager.startSearchForResult(getActivity(), initialQuery,
163                ACTIVITY_REQUEST_CODE_PICKER, getContactsRequest());
164    }
165
166    @Override
167    public void onPickerResult(Intent data) {
168        mListener.onPickContactAction(data.getData());
169    }
170}
171