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 package com.android.contacts.list;
17 
18 import android.content.Intent;
19 import android.net.Uri;
20 import android.os.Bundle;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.widget.AdapterView;
25 
26 import com.android.contacts.R;
27 import com.android.contacts.common.list.ContactEntryListAdapter;
28 import com.android.contacts.common.list.ContactEntryListFragment;
29 import com.android.contacts.common.list.ContactListAdapter;
30 import com.android.contacts.common.list.ContactListFilter;
31 import com.android.contacts.common.list.DirectoryListLoader;
32 import com.android.contacts.common.list.ShortcutIntentBuilder;
33 import com.android.contacts.common.list.ShortcutIntentBuilder.OnShortcutIntentCreatedListener;
34 
35 /**
36  * Fragment for the contact list used for browsing contacts (as compared to
37  * picking a contact with one of the PICK or SHORTCUT intents).
38  */
39 public class ContactPickerFragment extends ContactEntryListFragment<ContactEntryListAdapter>
40         implements OnShortcutIntentCreatedListener {
41 
42     private static final String KEY_EDIT_MODE = "editMode";
43     private static final String KEY_CREATE_CONTACT_ENABLED = "createContactEnabled";
44     private static final String KEY_SHORTCUT_REQUESTED = "shortcutRequested";
45 
46     private OnContactPickerActionListener mListener;
47     private boolean mCreateContactEnabled;
48     private boolean mEditMode;
49     private boolean mShortcutRequested;
50 
ContactPickerFragment()51     public ContactPickerFragment() {
52         setPhotoLoaderEnabled(true);
53         setSectionHeaderDisplayEnabled(true);
54         setVisibleScrollbarEnabled(true);
55         setQuickContactEnabled(false);
56         setDirectorySearchMode(DirectoryListLoader.SEARCH_MODE_CONTACT_SHORTCUT);
57     }
58 
setOnContactPickerActionListener(OnContactPickerActionListener listener)59     public void setOnContactPickerActionListener(OnContactPickerActionListener listener) {
60         mListener = listener;
61     }
62 
isCreateContactEnabled()63     public boolean isCreateContactEnabled() {
64         return mCreateContactEnabled;
65     }
66 
setCreateContactEnabled(boolean flag)67     public void setCreateContactEnabled(boolean flag) {
68         this.mCreateContactEnabled = flag;
69     }
70 
isEditMode()71     public boolean isEditMode() {
72         return mEditMode;
73     }
74 
setEditMode(boolean flag)75     public void setEditMode(boolean flag) {
76         mEditMode = flag;
77     }
78 
setShortcutRequested(boolean flag)79     public void setShortcutRequested(boolean flag) {
80         mShortcutRequested = flag;
81     }
82 
83     @Override
onSaveInstanceState(Bundle outState)84     public void onSaveInstanceState(Bundle outState) {
85         super.onSaveInstanceState(outState);
86         outState.putBoolean(KEY_EDIT_MODE, mEditMode);
87         outState.putBoolean(KEY_CREATE_CONTACT_ENABLED, mCreateContactEnabled);
88         outState.putBoolean(KEY_SHORTCUT_REQUESTED, mShortcutRequested);
89     }
90 
91     @Override
restoreSavedState(Bundle savedState)92     public void restoreSavedState(Bundle savedState) {
93         super.restoreSavedState(savedState);
94 
95         if (savedState == null) {
96             return;
97         }
98 
99         mEditMode = savedState.getBoolean(KEY_EDIT_MODE);
100         mCreateContactEnabled = savedState.getBoolean(KEY_CREATE_CONTACT_ENABLED);
101         mShortcutRequested = savedState.getBoolean(KEY_SHORTCUT_REQUESTED);
102     }
103 
104     @Override
onCreateView(LayoutInflater inflater, ViewGroup container)105     protected void onCreateView(LayoutInflater inflater, ViewGroup container) {
106         super.onCreateView(inflater, container);
107         if (mCreateContactEnabled && isLegacyCompatibilityMode()) {
108             // Since we are using the legacy adapter setShowCreateContact(true) isn't supported.
109             // So we need to add an ugly header above the list.
110             getListView().addHeaderView(inflater.inflate(R.layout.create_new_contact, null, false));
111         }
112     }
113 
114     @Override
onItemClick(AdapterView<?> parent, View view, int position, long id)115     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
116         if (position == 0 && mCreateContactEnabled && mListener != null) {
117             mListener.onCreateNewContactAction();
118         } else {
119             super.onItemClick(parent, view, position, id);
120         }
121     }
122 
123     @Override
onItemClick(int position, long id)124     protected void onItemClick(int position, long id) {
125         Uri uri;
126         if (isLegacyCompatibilityMode()) {
127             uri = ((LegacyContactListAdapter)getAdapter()).getPersonUri(position);
128         } else {
129             uri = ((ContactListAdapter)getAdapter()).getContactUri(position);
130         }
131         if (uri == null) {
132             return;
133         }
134         if (mEditMode) {
135             editContact(uri);
136         } else  if (mShortcutRequested) {
137             ShortcutIntentBuilder builder = new ShortcutIntentBuilder(getActivity(), this);
138             builder.createContactShortcutIntent(uri);
139         } else {
140             pickContact(uri);
141         }
142     }
143 
createNewContact()144     public void createNewContact() {
145         if (mListener != null) {
146             mListener.onCreateNewContactAction();
147         }
148     }
149 
editContact(Uri contactUri)150     public void editContact(Uri contactUri) {
151         if (mListener != null) {
152             mListener.onEditContactAction(contactUri);
153         }
154     }
155 
pickContact(Uri uri)156     public void pickContact(Uri uri) {
157         if (mListener != null) {
158             mListener.onPickContactAction(uri);
159         }
160     }
161 
162     @Override
createListAdapter()163     protected ContactEntryListAdapter createListAdapter() {
164         if (!isLegacyCompatibilityMode()) {
165             HeaderEntryContactListAdapter adapter
166                     = new HeaderEntryContactListAdapter(getActivity());
167             adapter.setFilter(ContactListFilter.createFilterWithType(
168                     ContactListFilter.FILTER_TYPE_ALL_ACCOUNTS));
169             adapter.setSectionHeaderDisplayEnabled(true);
170             adapter.setDisplayPhotos(true);
171             adapter.setQuickContactEnabled(false);
172             adapter.setShowCreateContact(mCreateContactEnabled);
173             return adapter;
174         } else {
175             LegacyContactListAdapter adapter = new LegacyContactListAdapter(getActivity());
176             adapter.setSectionHeaderDisplayEnabled(false);
177             adapter.setDisplayPhotos(false);
178             return adapter;
179         }
180     }
181 
182     @Override
configureAdapter()183     protected void configureAdapter() {
184         super.configureAdapter();
185 
186         ContactEntryListAdapter adapter = getAdapter();
187 
188         // If "Create new contact" is shown, don't display the empty list UI
189         adapter.setEmptyListEnabled(!isCreateContactEnabled());
190     }
191 
192     @Override
inflateView(LayoutInflater inflater, ViewGroup container)193     protected View inflateView(LayoutInflater inflater, ViewGroup container) {
194         return inflater.inflate(R.layout.contact_picker_content, null);
195     }
196 
197     @Override
onShortcutIntentCreated(Uri uri, Intent shortcutIntent)198     public void onShortcutIntentCreated(Uri uri, Intent shortcutIntent) {
199         if (mListener != null) {
200             mListener.onShortcutIntentCreated(shortcutIntent);
201         }
202     }
203 
204     @Override
onPickerResult(Intent data)205     public void onPickerResult(Intent data) {
206         if (mListener != null) {
207             mListener.onPickContactAction(data.getData());
208         }
209     }
210 }
211