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