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