Dmitri Plotnikov | 6e2009d | 2010-04-22 16:03:53 -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 | e4d32d9 | 2010-05-10 19:06:22 -0700 | [diff] [blame] | 18 | import android.content.Context; |
Jeff Hamilton | 3c46291 | 2010-05-15 02:20:01 -0500 | [diff] [blame] | 19 | import android.content.CursorLoader; |
Dmitri Plotnikov | 6e2009d | 2010-04-22 16:03:53 -0700 | [diff] [blame] | 20 | import android.database.Cursor; |
Dmitri Plotnikov | e4d32d9 | 2010-05-10 19:06:22 -0700 | [diff] [blame] | 21 | import android.net.Uri; |
| 22 | import android.net.Uri.Builder; |
| 23 | import android.provider.ContactsContract; |
| 24 | import android.provider.ContactsContract.Contacts; |
| 25 | import android.provider.ContactsContract.Contacts.AggregationSuggestions; |
Dmitri Plotnikov | 23411b2 | 2010-12-21 17:53:10 -0800 | [diff] [blame] | 26 | import android.provider.ContactsContract.Directory; |
Dmitri Plotnikov | e4d32d9 | 2010-05-10 19:06:22 -0700 | [diff] [blame] | 27 | import android.text.TextUtils; |
Dmitri Plotnikov | 6e2009d | 2010-04-22 16:03:53 -0700 | [diff] [blame] | 28 | import android.view.LayoutInflater; |
| 29 | import android.view.View; |
| 30 | import android.view.ViewGroup; |
| 31 | import android.widget.TextView; |
| 32 | |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 33 | import com.android.contacts.R; |
Chiao Cheng | a0233a0 | 2012-11-01 16:41:08 -0700 | [diff] [blame] | 34 | import com.android.contacts.common.list.ContactListAdapter; |
| 35 | import com.android.contacts.common.list.ContactListItemView; |
| 36 | import com.android.contacts.common.list.DirectoryListLoader; |
Yorke Lee | c9bb217 | 2014-07-10 11:38:34 -0700 | [diff] [blame] | 37 | import com.android.contacts.common.preference.ContactsPreferences; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 38 | |
Dmitri Plotnikov | e4d32d9 | 2010-05-10 19:06:22 -0700 | [diff] [blame] | 39 | public class JoinContactListAdapter extends ContactListAdapter { |
| 40 | |
| 41 | /** Maximum number of suggestions shown for joining aggregates */ |
| 42 | private static final int MAX_SUGGESTIONS = 4; |
| 43 | |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 44 | public static final int PARTITION_SUGGESTIONS = 0; |
Daisuke Miyakawa | a8729cd | 2011-10-12 12:27:16 -0700 | [diff] [blame] | 45 | public static final int PARTITION_ALL_CONTACTS = 1; |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 46 | |
Dmitri Plotnikov | e4d32d9 | 2010-05-10 19:06:22 -0700 | [diff] [blame] | 47 | private long mTargetContactId; |
Dmitri Plotnikov | 6e2009d | 2010-04-22 16:03:53 -0700 | [diff] [blame] | 48 | |
Dmitri Plotnikov | e4d32d9 | 2010-05-10 19:06:22 -0700 | [diff] [blame] | 49 | public JoinContactListAdapter(Context context) { |
Dmitri Plotnikov | 6e2009d | 2010-04-22 16:03:53 -0700 | [diff] [blame] | 50 | super(context); |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 51 | setPinnedPartitionHeadersEnabled(true); |
Dmitri Plotnikov | e4d32d9 | 2010-05-10 19:06:22 -0700 | [diff] [blame] | 52 | setSectionHeaderDisplayEnabled(true); |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 53 | setIndexedPartition(PARTITION_ALL_CONTACTS); |
Dmitri Plotnikov | 4d174aa | 2010-10-25 15:38:54 -0700 | [diff] [blame] | 54 | setDirectorySearchMode(DirectoryListLoader.SEARCH_MODE_NONE); |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | @Override |
| 58 | protected void addPartitions() { |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 59 | // Partition 0: suggestions |
| 60 | addPartition(false, true); |
| 61 | |
Daisuke Miyakawa | a8729cd | 2011-10-12 12:27:16 -0700 | [diff] [blame] | 62 | // Partition 1: All contacts |
Dmitri Plotnikov | e8a9517 | 2010-06-23 16:18:10 -0700 | [diff] [blame] | 63 | addPartition(createDefaultDirectoryPartition()); |
Dmitri Plotnikov | 6e2009d | 2010-04-22 16:03:53 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Dmitri Plotnikov | e4d32d9 | 2010-05-10 19:06:22 -0700 | [diff] [blame] | 66 | public void setTargetContactId(long targetContactId) { |
| 67 | this.mTargetContactId = targetContactId; |
Dmitri Plotnikov | 6e2009d | 2010-04-22 16:03:53 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Dmitri Plotnikov | e4d32d9 | 2010-05-10 19:06:22 -0700 | [diff] [blame] | 70 | @Override |
Dmitri Plotnikov | d5061fe | 2010-06-07 15:06:58 -0700 | [diff] [blame] | 71 | public void configureLoader(CursorLoader cursorLoader, long directoryId) { |
Daisuke Miyakawa | 6da2a2d | 2011-11-09 10:33:11 -0800 | [diff] [blame] | 72 | JoinContactLoader loader = (JoinContactLoader) cursorLoader; |
Dmitri Plotnikov | e4d32d9 | 2010-05-10 19:06:22 -0700 | [diff] [blame] | 73 | |
Daisuke Miyakawa | ecd392b | 2011-10-12 12:27:16 -0700 | [diff] [blame] | 74 | final Builder builder = Contacts.CONTENT_URI.buildUpon(); |
Dmitri Plotnikov | e4d32d9 | 2010-05-10 19:06:22 -0700 | [diff] [blame] | 75 | builder.appendEncodedPath(String.valueOf(mTargetContactId)); |
| 76 | builder.appendEncodedPath(AggregationSuggestions.CONTENT_DIRECTORY); |
| 77 | |
Daisuke Miyakawa | ecd392b | 2011-10-12 12:27:16 -0700 | [diff] [blame] | 78 | final String filter = getQueryString(); |
Dmitri Plotnikov | e4d32d9 | 2010-05-10 19:06:22 -0700 | [diff] [blame] | 79 | if (!TextUtils.isEmpty(filter)) { |
| 80 | builder.appendEncodedPath(Uri.encode(filter)); |
| 81 | } |
| 82 | |
| 83 | builder.appendQueryParameter("limit", String.valueOf(MAX_SUGGESTIONS)); |
| 84 | |
| 85 | loader.setSuggestionUri(builder.build()); |
| 86 | |
| 87 | // TODO simplify projection |
Daisuke Miyakawa | 6da2a2d | 2011-11-09 10:33:11 -0800 | [diff] [blame] | 88 | loader.setProjection(getProjection(false)); |
Daisuke Miyakawa | ecd392b | 2011-10-12 12:27:16 -0700 | [diff] [blame] | 89 | final Uri allContactsUri; |
| 90 | if (!TextUtils.isEmpty(filter)) { |
| 91 | allContactsUri = buildSectionIndexerUri(Contacts.CONTENT_FILTER_URI).buildUpon() |
| 92 | .appendEncodedPath(Uri.encode(filter)) |
Dmitri Plotnikov | 23411b2 | 2010-12-21 17:53:10 -0800 | [diff] [blame] | 93 | .appendQueryParameter( |
| 94 | ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(Directory.DEFAULT)) |
| 95 | .build(); |
Daisuke Miyakawa | ecd392b | 2011-10-12 12:27:16 -0700 | [diff] [blame] | 96 | } else { |
| 97 | allContactsUri = buildSectionIndexerUri(Contacts.CONTENT_URI).buildUpon() |
| 98 | .appendQueryParameter( |
| 99 | ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(Directory.DEFAULT)) |
| 100 | .build(); |
| 101 | } |
Dmitri Plotnikov | 23411b2 | 2010-12-21 17:53:10 -0800 | [diff] [blame] | 102 | loader.setUri(allContactsUri); |
| 103 | loader.setSelection(Contacts._ID + "!=?"); |
Daisuke Miyakawa | ecd392b | 2011-10-12 12:27:16 -0700 | [diff] [blame] | 104 | loader.setSelectionArgs(new String[]{ String.valueOf(mTargetContactId) }); |
Yorke Lee | c9bb217 | 2014-07-10 11:38:34 -0700 | [diff] [blame] | 105 | if (getSortOrder() == ContactsPreferences.SORT_ORDER_PRIMARY) { |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 106 | loader.setSortOrder(Contacts.SORT_KEY_PRIMARY); |
| 107 | } else { |
| 108 | loader.setSortOrder(Contacts.SORT_KEY_ALTERNATIVE); |
Dmitri Plotnikov | e4d32d9 | 2010-05-10 19:06:22 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public boolean isEmpty() { |
| 114 | return false; |
| 115 | } |
| 116 | |
Dmitri Plotnikov | 6e2009d | 2010-04-22 16:03:53 -0700 | [diff] [blame] | 117 | public void setSuggestionsCursor(Cursor cursor) { |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 118 | changeCursor(PARTITION_SUGGESTIONS, cursor); |
Dmitri Plotnikov | 6e2009d | 2010-04-22 16:03:53 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @Override |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 122 | public void changeCursor(Cursor cursor) { |
| 123 | changeCursor(PARTITION_ALL_CONTACTS, cursor); |
| 124 | } |
Dmitri Plotnikov | 6e2009d | 2010-04-22 16:03:53 -0700 | [diff] [blame] | 125 | |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 126 | @Override |
Dmitri Plotnikov | e8a9517 | 2010-06-23 16:18:10 -0700 | [diff] [blame] | 127 | public void configureDefaultPartition(boolean showIfEmpty, boolean hasHeader) { |
| 128 | // Don't change default partition parameters from these defaults |
| 129 | super.configureDefaultPartition(false, true); |
| 130 | } |
| 131 | |
| 132 | @Override |
Dmitri Plotnikov | 2475ac8 | 2010-07-14 15:11:25 -0700 | [diff] [blame] | 133 | public int getViewTypeCount() { |
Daisuke Miyakawa | ecd392b | 2011-10-12 12:27:16 -0700 | [diff] [blame] | 134 | return super.getViewTypeCount(); |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
Dmitri Plotnikov | 2475ac8 | 2010-07-14 15:11:25 -0700 | [diff] [blame] | 138 | public int getItemViewType(int partition, int position) { |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 139 | return super.getItemViewType(partition, position); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | protected View newHeaderView(Context context, int partition, Cursor cursor, |
| 144 | ViewGroup parent) { |
| 145 | switch (partition) { |
| 146 | case PARTITION_SUGGESTIONS: { |
Daisuke Miyakawa | a8729cd | 2011-10-12 12:27:16 -0700 | [diff] [blame] | 147 | View view = inflate(R.layout.join_contact_picker_section_header, parent); |
Dmitri Plotnikov | d8f84e0 | 2011-01-23 18:03:14 -0800 | [diff] [blame] | 148 | ((TextView) view.findViewById(R.id.text)).setText( |
| 149 | R.string.separatorJoinAggregateSuggestions); |
| 150 | return view; |
Dmitri Plotnikov | e4d32d9 | 2010-05-10 19:06:22 -0700 | [diff] [blame] | 151 | } |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 152 | case PARTITION_ALL_CONTACTS: { |
Daisuke Miyakawa | a8729cd | 2011-10-12 12:27:16 -0700 | [diff] [blame] | 153 | View view = inflate(R.layout.join_contact_picker_section_header, parent); |
Dmitri Plotnikov | d8f84e0 | 2011-01-23 18:03:14 -0800 | [diff] [blame] | 154 | ((TextView) view.findViewById(R.id.text)).setText( |
| 155 | R.string.separatorJoinAggregateAll); |
| 156 | return view; |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 157 | } |
Dmitri Plotnikov | 6e2009d | 2010-04-22 16:03:53 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 160 | return null; |
| 161 | } |
| 162 | |
| 163 | @Override |
Dmitri Plotnikov | e8a9517 | 2010-06-23 16:18:10 -0700 | [diff] [blame] | 164 | protected void bindHeaderView(View view, int partitionIndex, Cursor cursor) { |
| 165 | // Header views are static - nothing needs to be bound |
| 166 | } |
| 167 | |
| 168 | @Override |
Andrew Lee | 551da17 | 2014-04-28 11:43:47 -0700 | [diff] [blame] | 169 | protected ContactListItemView newView( |
| 170 | Context context, int partition, Cursor cursor, int position, ViewGroup parent) { |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 171 | switch (partition) { |
| 172 | case PARTITION_SUGGESTIONS: |
| 173 | case PARTITION_ALL_CONTACTS: |
| 174 | return super.newView(context, partition, cursor, position, parent); |
Dmitri Plotnikov | 6e2009d | 2010-04-22 16:03:53 -0700 | [diff] [blame] | 175 | } |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 176 | return null; |
Dmitri Plotnikov | 6e2009d | 2010-04-22 16:03:53 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Dmitri Plotnikov | e4d32d9 | 2010-05-10 19:06:22 -0700 | [diff] [blame] | 179 | private View inflate(int layoutId, ViewGroup parent) { |
| 180 | return LayoutInflater.from(getContext()).inflate(layoutId, parent, false); |
| 181 | } |
| 182 | |
| 183 | @Override |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 184 | protected void bindView(View itemView, int partition, Cursor cursor, int position) { |
Brian Attwell | c9e8107 | 2014-09-15 11:08:19 -0700 | [diff] [blame] | 185 | super.bindView(itemView, partition, cursor, position); |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 186 | switch (partition) { |
| 187 | case PARTITION_SUGGESTIONS: { |
Daisuke Miyakawa | ecd392b | 2011-10-12 12:27:16 -0700 | [diff] [blame] | 188 | final ContactListItemView view = (ContactListItemView) itemView; |
Dmitri Plotnikov | d8f84e0 | 2011-01-23 18:03:14 -0800 | [diff] [blame] | 189 | view.setSectionHeader(null); |
Dmitri Plotnikov | 1228c81 | 2010-09-23 13:11:56 -0700 | [diff] [blame] | 190 | bindPhoto(view, partition, cursor); |
Brian Attwell | 56151b8 | 2014-09-03 20:14:47 -0700 | [diff] [blame] | 191 | bindNameAndViewId(view, cursor); |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 192 | break; |
| 193 | } |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 194 | case PARTITION_ALL_CONTACTS: { |
Daisuke Miyakawa | ecd392b | 2011-10-12 12:27:16 -0700 | [diff] [blame] | 195 | final ContactListItemView view = (ContactListItemView) itemView; |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 196 | bindSectionHeaderAndDivider(view, position, cursor); |
Dmitri Plotnikov | 1228c81 | 2010-09-23 13:11:56 -0700 | [diff] [blame] | 197 | bindPhoto(view, partition, cursor); |
Brian Attwell | 56151b8 | 2014-09-03 20:14:47 -0700 | [diff] [blame] | 198 | bindNameAndViewId(view, cursor); |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 199 | break; |
| 200 | } |
Dmitri Plotnikov | e4d32d9 | 2010-05-10 19:06:22 -0700 | [diff] [blame] | 201 | } |
Dmitri Plotnikov | e4d32d9 | 2010-05-10 19:06:22 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Dmitri Plotnikov | 6e2009d | 2010-04-22 16:03:53 -0700 | [diff] [blame] | 204 | @Override |
Dmitri Plotnikov | 8773bcb | 2010-07-15 18:55:46 -0700 | [diff] [blame] | 205 | public Uri getContactUri(int partitionIndex, Cursor cursor) { |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 206 | long contactId = cursor.getLong(ContactQuery.CONTACT_ID); |
| 207 | String lookupKey = cursor.getString(ContactQuery.CONTACT_LOOKUP_KEY); |
Dmitri Plotnikov | e4d32d9 | 2010-05-10 19:06:22 -0700 | [diff] [blame] | 208 | return Contacts.getLookupUri(contactId, lookupKey); |
Dmitri Plotnikov | 6e2009d | 2010-04-22 16:03:53 -0700 | [diff] [blame] | 209 | } |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 210 | } |