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