ContactPickerFragment.java revision 8a7831944232fd01740492b39bc67d746213b82b
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<ContactListAdapter>
33        implements OnShortcutIntentCreatedListener {
34
35    private OnContactPickerActionListener mListener;
36    private boolean mCreateContactEnabled;
37    private boolean mShortcutRequested;
38
39    public void setOnContactPickerActionListener(OnContactPickerActionListener listener) {
40        mListener = listener;
41    }
42
43    public boolean isCreateContactEnabled() {
44        return mCreateContactEnabled;
45    }
46
47    public void setCreateContactEnabled(boolean flag) {
48        this.mCreateContactEnabled = flag;
49    }
50
51    public boolean isShortcutRequested() {
52        return mShortcutRequested;
53    }
54
55    public void setShortcutRequested(boolean flag) {
56        mShortcutRequested = flag;
57    }
58
59    @Override
60    protected View createView(LayoutInflater inflater, ViewGroup container) {
61        View view = super.createView(inflater, container);
62        if (mCreateContactEnabled) {
63            getListView().addHeaderView(inflater.inflate(R.layout.create_new_contact, null, false));
64        }
65        return view;
66    }
67
68    public boolean isSearchAllContactsItemPosition(int position) {
69        return isSearchMode() && position == getAdapter().getCount() - 1;
70    }
71
72    @Override
73    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
74        if (position == 0 && !isSearchMode() && mCreateContactEnabled) {
75            mListener.onCreateNewContactAction();
76        } else {
77            super.onItemClick(parent, view, position, id);
78        }
79    }
80
81    @Override
82    protected void onItemClick(int position, long id) {
83        if (isSearchAllContactsItemPosition(position)) {
84            mListener.onSearchAllContactsAction((String)null);
85        } else {
86            ContactListAdapter adapter = getAdapter();
87            adapter.moveToPosition(position);
88            if (mShortcutRequested) {
89                ShortcutIntentBuilder builder = new ShortcutIntentBuilder(getActivity(), this);
90                builder.createContactShortcutIntent(adapter.getContactUri());
91            } else {
92                mListener.onPickContactAction(adapter.getContactUri());
93            }
94        }
95    }
96
97    @Override
98    protected ContactListAdapter createListAdapter() {
99        ContactListAdapter adapter = new DefaultContactListAdapter(getActivity());
100        adapter.setSectionHeaderDisplayEnabled(true);
101
102        adapter.setDisplayPhotos(true);
103        adapter.setQuickContactEnabled(false);
104
105        adapter.setSearchMode(isSearchMode());
106        adapter.setSearchResultsMode(isSearchResultsMode());
107        adapter.setQueryString(getQueryString());
108
109        adapter.setContactNameDisplayOrder(getContactNameDisplayOrder());
110        adapter.setSortOrder(getSortOrder());
111
112        return adapter;
113    }
114
115    @Override
116    protected View inflateView(LayoutInflater inflater, ViewGroup container) {
117        if (isSearchMode()) {
118            return inflater.inflate(R.layout.contacts_search_content, null);
119        } else if (isSearchResultsMode()) {
120            return inflater.inflate(R.layout.contacts_list_search_results, null);
121        } else {
122            return inflater.inflate(R.layout.contacts_list_content, null);
123        }
124    }
125
126    public void onShortcutIntentCreated(Uri uri, Intent shortcutIntent) {
127        mListener.onShortcutIntentCreated(shortcutIntent);
128    }
129}
130