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