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) { 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 mListener.onCreateNewContactAction(); 146 } 147 editContact(Uri contactUri)148 public void editContact(Uri contactUri) { 149 mListener.onEditContactAction(contactUri); 150 } 151 pickContact(Uri uri)152 public void pickContact(Uri uri) { 153 mListener.onPickContactAction(uri); 154 } 155 156 @Override createListAdapter()157 protected ContactEntryListAdapter createListAdapter() { 158 if (!isLegacyCompatibilityMode()) { 159 HeaderEntryContactListAdapter adapter 160 = new HeaderEntryContactListAdapter(getActivity()); 161 adapter.setFilter(ContactListFilter.createFilterWithType( 162 ContactListFilter.FILTER_TYPE_ALL_ACCOUNTS)); 163 adapter.setSectionHeaderDisplayEnabled(true); 164 adapter.setDisplayPhotos(true); 165 adapter.setQuickContactEnabled(false); 166 adapter.setShowCreateContact(mCreateContactEnabled); 167 return adapter; 168 } else { 169 LegacyContactListAdapter adapter = new LegacyContactListAdapter(getActivity()); 170 adapter.setSectionHeaderDisplayEnabled(false); 171 adapter.setDisplayPhotos(false); 172 return adapter; 173 } 174 } 175 176 @Override configureAdapter()177 protected void configureAdapter() { 178 super.configureAdapter(); 179 180 ContactEntryListAdapter adapter = getAdapter(); 181 182 // If "Create new contact" is shown, don't display the empty list UI 183 adapter.setEmptyListEnabled(!isCreateContactEnabled()); 184 } 185 186 @Override inflateView(LayoutInflater inflater, ViewGroup container)187 protected View inflateView(LayoutInflater inflater, ViewGroup container) { 188 return inflater.inflate(R.layout.contact_picker_content, null); 189 } 190 191 @Override onShortcutIntentCreated(Uri uri, Intent shortcutIntent)192 public void onShortcutIntentCreated(Uri uri, Intent shortcutIntent) { 193 mListener.onShortcutIntentCreated(shortcutIntent); 194 } 195 196 @Override onPickerResult(Intent data)197 public void onPickerResult(Intent data) { 198 mListener.onPickContactAction(data.getData()); 199 } 200 } 201