Dmitri Plotnikov | 732f652 | 2010-04-27 10:30:59 -0700 | [diff] [blame] | 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 | |
Dmitri Plotnikov | 8a78319 | 2010-05-05 18:59:36 -0700 | [diff] [blame] | 18 | import android.content.Intent; |
| 19 | import android.net.Uri; |
Dmitri Plotnikov | 8609252 | 2010-10-27 17:03:52 -0700 | [diff] [blame] | 20 | import android.os.Bundle; |
Dmitri Plotnikov | 732f652 | 2010-04-27 10:30:59 -0700 | [diff] [blame] | 21 | import android.view.LayoutInflater; |
| 22 | import android.view.View; |
| 23 | import android.view.ViewGroup; |
Dmitri Plotnikov | b6c7c6f | 2010-05-05 14:11:35 -0700 | [diff] [blame] | 24 | import android.widget.AdapterView; |
Dmitri Plotnikov | 732f652 | 2010-04-27 10:30:59 -0700 | [diff] [blame] | 25 | |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 26 | import com.android.contacts.R; |
Chiao Cheng | a0233a0 | 2012-11-01 16:41:08 -0700 | [diff] [blame] | 27 | import com.android.contacts.common.list.ContactEntryListAdapter; |
| 28 | import com.android.contacts.common.list.ContactListAdapter; |
| 29 | import com.android.contacts.common.list.ContactListFilter; |
| 30 | import com.android.contacts.common.list.DefaultContactListAdapter; |
| 31 | import com.android.contacts.common.list.DirectoryListLoader; |
Chiao Cheng | 30868d4 | 2012-11-28 17:45:57 -0800 | [diff] [blame^] | 32 | import com.android.contacts.common.list.ShortcutIntentBuilder; |
| 33 | import com.android.contacts.common.list.ShortcutIntentBuilder.OnShortcutIntentCreatedListener; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 34 | |
Dmitri Plotnikov | 732f652 | 2010-04-27 10:30:59 -0700 | [diff] [blame] | 35 | /** |
Dmitri Plotnikov | 416b569 | 2010-04-27 18:14:58 -0700 | [diff] [blame] | 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). |
Dmitri Plotnikov | 732f652 | 2010-04-27 10:30:59 -0700 | [diff] [blame] | 38 | */ |
Dmitri Plotnikov | 4137727 | 2010-05-07 10:28:47 -0700 | [diff] [blame] | 39 | public class ContactPickerFragment extends ContactEntryListFragment<ContactEntryListAdapter> |
Dmitri Plotnikov | 8a78319 | 2010-05-05 18:59:36 -0700 | [diff] [blame] | 40 | implements OnShortcutIntentCreatedListener { |
Dmitri Plotnikov | 732f652 | 2010-04-27 10:30:59 -0700 | [diff] [blame] | 41 | |
Dmitri Plotnikov | 8609252 | 2010-10-27 17:03:52 -0700 | [diff] [blame] | 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 | |
Dmitri Plotnikov | 416b569 | 2010-04-27 18:14:58 -0700 | [diff] [blame] | 46 | private OnContactPickerActionListener mListener; |
Dmitri Plotnikov | 732f652 | 2010-04-27 10:30:59 -0700 | [diff] [blame] | 47 | private boolean mCreateContactEnabled; |
Dmitri Plotnikov | 8609252 | 2010-10-27 17:03:52 -0700 | [diff] [blame] | 48 | private boolean mEditMode; |
Dmitri Plotnikov | 8a78319 | 2010-05-05 18:59:36 -0700 | [diff] [blame] | 49 | private boolean mShortcutRequested; |
Dmitri Plotnikov | 732f652 | 2010-04-27 10:30:59 -0700 | [diff] [blame] | 50 | |
Dmitri Plotnikov | d621f71 | 2010-06-16 17:28:07 -0700 | [diff] [blame] | 51 | public ContactPickerFragment() { |
| 52 | setPhotoLoaderEnabled(true); |
Dmitri Plotnikov | d3758b0 | 2010-06-17 11:34:59 -0700 | [diff] [blame] | 53 | setSectionHeaderDisplayEnabled(true); |
Dmitri Plotnikov | 5245ea6 | 2010-12-10 15:03:13 -0800 | [diff] [blame] | 54 | setVisibleScrollbarEnabled(true); |
Dmitri Plotnikov | 84b3cc7 | 2010-10-11 18:19:51 -0700 | [diff] [blame] | 55 | setQuickContactEnabled(false); |
Dmitri Plotnikov | 4d174aa | 2010-10-25 15:38:54 -0700 | [diff] [blame] | 56 | setDirectorySearchMode(DirectoryListLoader.SEARCH_MODE_CONTACT_SHORTCUT); |
Dmitri Plotnikov | d621f71 | 2010-06-16 17:28:07 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Dmitri Plotnikov | 416b569 | 2010-04-27 18:14:58 -0700 | [diff] [blame] | 59 | public void setOnContactPickerActionListener(OnContactPickerActionListener listener) { |
Dmitri Plotnikov | 732f652 | 2010-04-27 10:30:59 -0700 | [diff] [blame] | 60 | mListener = listener; |
| 61 | } |
| 62 | |
Dmitri Plotnikov | 8a78319 | 2010-05-05 18:59:36 -0700 | [diff] [blame] | 63 | public boolean isCreateContactEnabled() { |
| 64 | return mCreateContactEnabled; |
| 65 | } |
| 66 | |
Dmitri Plotnikov | b6c7c6f | 2010-05-05 14:11:35 -0700 | [diff] [blame] | 67 | public void setCreateContactEnabled(boolean flag) { |
| 68 | this.mCreateContactEnabled = flag; |
| 69 | } |
| 70 | |
Dmitri Plotnikov | 8609252 | 2010-10-27 17:03:52 -0700 | [diff] [blame] | 71 | public boolean isEditMode() { |
| 72 | return mEditMode; |
| 73 | } |
| 74 | |
| 75 | public void setEditMode(boolean flag) { |
| 76 | mEditMode = flag; |
| 77 | } |
| 78 | |
Dmitri Plotnikov | 8a78319 | 2010-05-05 18:59:36 -0700 | [diff] [blame] | 79 | public void setShortcutRequested(boolean flag) { |
| 80 | mShortcutRequested = flag; |
Dmitri Plotnikov | b6c7c6f | 2010-05-05 14:11:35 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Override |
Dmitri Plotnikov | 8609252 | 2010-10-27 17:03:52 -0700 | [diff] [blame] | 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 |
| 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 |
Dmitri Plotnikov | bba62ac | 2010-06-03 10:22:59 -0700 | [diff] [blame] | 105 | protected void onCreateView(LayoutInflater inflater, ViewGroup container) { |
| 106 | super.onCreateView(inflater, container); |
Dmitri Plotnikov | b6c7c6f | 2010-05-05 14:11:35 -0700 | [diff] [blame] | 107 | if (mCreateContactEnabled) { |
| 108 | getListView().addHeaderView(inflater.inflate(R.layout.create_new_contact, null, false)); |
| 109 | } |
Dmitri Plotnikov | b6c7c6f | 2010-05-05 14:11:35 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Dmitri Plotnikov | 732f652 | 2010-04-27 10:30:59 -0700 | [diff] [blame] | 112 | @Override |
Dmitri Plotnikov | b6c7c6f | 2010-05-05 14:11:35 -0700 | [diff] [blame] | 113 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
Dmitri Plotnikov | 8609252 | 2010-10-27 17:03:52 -0700 | [diff] [blame] | 114 | if (position == 0 && mCreateContactEnabled) { |
Dmitri Plotnikov | b6c7c6f | 2010-05-05 14:11:35 -0700 | [diff] [blame] | 115 | mListener.onCreateNewContactAction(); |
| 116 | } else { |
| 117 | super.onItemClick(parent, view, position, id); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | @Override |
Dmitri Plotnikov | 732f652 | 2010-04-27 10:30:59 -0700 | [diff] [blame] | 122 | protected void onItemClick(int position, long id) { |
Dmitri Plotnikov | d3758b0 | 2010-06-17 11:34:59 -0700 | [diff] [blame] | 123 | Uri uri; |
| 124 | if (isLegacyCompatibilityMode()) { |
| 125 | uri = ((LegacyContactListAdapter)getAdapter()).getPersonUri(position); |
Dmitri Plotnikov | 416b569 | 2010-04-27 18:14:58 -0700 | [diff] [blame] | 126 | } else { |
Dmitri Plotnikov | d3758b0 | 2010-06-17 11:34:59 -0700 | [diff] [blame] | 127 | uri = ((ContactListAdapter)getAdapter()).getContactUri(position); |
| 128 | } |
Dmitri Plotnikov | 8609252 | 2010-10-27 17:03:52 -0700 | [diff] [blame] | 129 | if (mEditMode) { |
| 130 | editContact(uri); |
| 131 | } else if (mShortcutRequested) { |
Dmitri Plotnikov | d3758b0 | 2010-06-17 11:34:59 -0700 | [diff] [blame] | 132 | ShortcutIntentBuilder builder = new ShortcutIntentBuilder(getActivity(), this); |
| 133 | builder.createContactShortcutIntent(uri); |
| 134 | } else { |
Dmitri Plotnikov | 8609252 | 2010-10-27 17:03:52 -0700 | [diff] [blame] | 135 | pickContact(uri); |
Dmitri Plotnikov | 732f652 | 2010-04-27 10:30:59 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Dmitri Plotnikov | 8609252 | 2010-10-27 17:03:52 -0700 | [diff] [blame] | 139 | public void createNewContact() { |
| 140 | mListener.onCreateNewContactAction(); |
| 141 | } |
| 142 | |
| 143 | public void editContact(Uri contactUri) { |
| 144 | mListener.onEditContactAction(contactUri); |
| 145 | } |
| 146 | |
| 147 | public void pickContact(Uri uri) { |
| 148 | mListener.onPickContactAction(uri); |
| 149 | } |
| 150 | |
Dmitri Plotnikov | 732f652 | 2010-04-27 10:30:59 -0700 | [diff] [blame] | 151 | @Override |
Dmitri Plotnikov | 4137727 | 2010-05-07 10:28:47 -0700 | [diff] [blame] | 152 | protected ContactEntryListAdapter createListAdapter() { |
Dmitri Plotnikov | 1ce1e7c | 2010-05-13 16:41:00 -0700 | [diff] [blame] | 153 | if (!isLegacyCompatibilityMode()) { |
Dmitri Plotnikov | 58be7ed | 2010-10-13 09:09:05 -0700 | [diff] [blame] | 154 | DefaultContactListAdapter adapter = new DefaultContactListAdapter(getActivity()); |
Katherine Kuan | be18de0 | 2011-06-07 12:38:29 -0700 | [diff] [blame] | 155 | adapter.setFilter(ContactListFilter.createFilterWithType( |
| 156 | ContactListFilter.FILTER_TYPE_ALL_ACCOUNTS)); |
Dmitri Plotnikov | 4137727 | 2010-05-07 10:28:47 -0700 | [diff] [blame] | 157 | adapter.setSectionHeaderDisplayEnabled(true); |
| 158 | adapter.setDisplayPhotos(true); |
| 159 | adapter.setQuickContactEnabled(false); |
| 160 | return adapter; |
| 161 | } else { |
| 162 | LegacyContactListAdapter adapter = new LegacyContactListAdapter(getActivity()); |
| 163 | adapter.setSectionHeaderDisplayEnabled(false); |
| 164 | adapter.setDisplayPhotos(false); |
| 165 | return adapter; |
| 166 | } |
| 167 | } |
Dmitri Plotnikov | b6c7c6f | 2010-05-05 14:11:35 -0700 | [diff] [blame] | 168 | |
Dmitri Plotnikov | 4137727 | 2010-05-07 10:28:47 -0700 | [diff] [blame] | 169 | @Override |
| 170 | protected void configureAdapter() { |
| 171 | super.configureAdapter(); |
Dmitri Plotnikov | d3758b0 | 2010-06-17 11:34:59 -0700 | [diff] [blame] | 172 | |
Dmitri Plotnikov | 4137727 | 2010-05-07 10:28:47 -0700 | [diff] [blame] | 173 | ContactEntryListAdapter adapter = getAdapter(); |
Dmitri Plotnikov | e44b043 | 2010-04-30 15:23:41 -0700 | [diff] [blame] | 174 | |
Dmitri Plotnikov | 68e32a3 | 2010-05-06 16:15:14 -0700 | [diff] [blame] | 175 | // If "Create new contact" is shown, don't display the empty list UI |
| 176 | adapter.setEmptyListEnabled(!isCreateContactEnabled()); |
Dmitri Plotnikov | 732f652 | 2010-04-27 10:30:59 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | @Override |
| 180 | protected View inflateView(LayoutInflater inflater, ViewGroup container) { |
Dmitri Plotnikov | 84b3cc7 | 2010-10-11 18:19:51 -0700 | [diff] [blame] | 181 | return inflater.inflate(R.layout.contact_picker_content, null); |
Dmitri Plotnikov | 732f652 | 2010-04-27 10:30:59 -0700 | [diff] [blame] | 182 | } |
Dmitri Plotnikov | 8a78319 | 2010-05-05 18:59:36 -0700 | [diff] [blame] | 183 | |
Dmitri Plotnikov | 68e32a3 | 2010-05-06 16:15:14 -0700 | [diff] [blame] | 184 | @Override |
Dmitri Plotnikov | 8a78319 | 2010-05-05 18:59:36 -0700 | [diff] [blame] | 185 | public void onShortcutIntentCreated(Uri uri, Intent shortcutIntent) { |
| 186 | mListener.onShortcutIntentCreated(shortcutIntent); |
| 187 | } |
Dmitri Plotnikov | d3758b0 | 2010-06-17 11:34:59 -0700 | [diff] [blame] | 188 | |
| 189 | @Override |
Dmitri Plotnikov | e8a9517 | 2010-06-23 16:18:10 -0700 | [diff] [blame] | 190 | public void onPickerResult(Intent data) { |
| 191 | mListener.onPickContactAction(data.getData()); |
Dmitri Plotnikov | d3758b0 | 2010-06-17 11:34:59 -0700 | [diff] [blame] | 192 | } |
Dmitri Plotnikov | 732f652 | 2010-04-27 10:30:59 -0700 | [diff] [blame] | 193 | } |