blob: f08fcbbe3780fb8e2eb197324ec7e1566d47d36d [file] [log] [blame]
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -07001/*
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 */
16package com.android.contacts.list;
17
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -070018import android.content.Context;
Jeff Hamilton3c462912010-05-15 02:20:01 -050019import android.content.CursorLoader;
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -070020import android.database.Cursor;
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -070021import android.net.Uri;
22import android.net.Uri.Builder;
23import android.provider.ContactsContract;
24import android.provider.ContactsContract.Contacts;
25import android.provider.ContactsContract.Contacts.AggregationSuggestions;
Dmitri Plotnikov23411b22010-12-21 17:53:10 -080026import android.provider.ContactsContract.Directory;
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -070027import android.text.TextUtils;
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -070028import android.view.LayoutInflater;
29import android.view.View;
30import android.view.ViewGroup;
31import android.widget.TextView;
32
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070033import com.android.contacts.R;
Chiao Chenga0233a02012-11-01 16:41:08 -070034import com.android.contacts.common.list.ContactListAdapter;
35import com.android.contacts.common.list.ContactListItemView;
36import com.android.contacts.common.list.DirectoryListLoader;
Yorke Leec9bb2172014-07-10 11:38:34 -070037import com.android.contacts.common.preference.ContactsPreferences;
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070038
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -070039public class JoinContactListAdapter extends ContactListAdapter {
40
41 /** Maximum number of suggestions shown for joining aggregates */
42 private static final int MAX_SUGGESTIONS = 4;
43
Dmitri Plotnikove1247222010-06-02 18:14:21 -070044 public static final int PARTITION_SUGGESTIONS = 0;
Daisuke Miyakawaa8729cd2011-10-12 12:27:16 -070045 public static final int PARTITION_ALL_CONTACTS = 1;
Dmitri Plotnikove1247222010-06-02 18:14:21 -070046
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -070047 private long mTargetContactId;
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -070048
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -070049 public JoinContactListAdapter(Context context) {
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -070050 super(context);
Dmitri Plotnikove1247222010-06-02 18:14:21 -070051 setPinnedPartitionHeadersEnabled(true);
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -070052 setSectionHeaderDisplayEnabled(true);
Dmitri Plotnikove1247222010-06-02 18:14:21 -070053 setIndexedPartition(PARTITION_ALL_CONTACTS);
Dmitri Plotnikov4d174aa2010-10-25 15:38:54 -070054 setDirectorySearchMode(DirectoryListLoader.SEARCH_MODE_NONE);
Dmitri Plotnikove1247222010-06-02 18:14:21 -070055 }
56
57 @Override
58 protected void addPartitions() {
Dmitri Plotnikove1247222010-06-02 18:14:21 -070059 // Partition 0: suggestions
60 addPartition(false, true);
61
Daisuke Miyakawaa8729cd2011-10-12 12:27:16 -070062 // Partition 1: All contacts
Dmitri Plotnikove8a95172010-06-23 16:18:10 -070063 addPartition(createDefaultDirectoryPartition());
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -070064 }
65
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -070066 public void setTargetContactId(long targetContactId) {
67 this.mTargetContactId = targetContactId;
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -070068 }
69
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -070070 @Override
Dmitri Plotnikovd5061fe2010-06-07 15:06:58 -070071 public void configureLoader(CursorLoader cursorLoader, long directoryId) {
Daisuke Miyakawa6da2a2d2011-11-09 10:33:11 -080072 JoinContactLoader loader = (JoinContactLoader) cursorLoader;
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -070073
Daisuke Miyakawaecd392b2011-10-12 12:27:16 -070074 final Builder builder = Contacts.CONTENT_URI.buildUpon();
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -070075 builder.appendEncodedPath(String.valueOf(mTargetContactId));
76 builder.appendEncodedPath(AggregationSuggestions.CONTENT_DIRECTORY);
77
Daisuke Miyakawaecd392b2011-10-12 12:27:16 -070078 final String filter = getQueryString();
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -070079 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 Miyakawa6da2a2d2011-11-09 10:33:11 -080088 loader.setProjection(getProjection(false));
Daisuke Miyakawaecd392b2011-10-12 12:27:16 -070089 final Uri allContactsUri;
90 if (!TextUtils.isEmpty(filter)) {
91 allContactsUri = buildSectionIndexerUri(Contacts.CONTENT_FILTER_URI).buildUpon()
92 .appendEncodedPath(Uri.encode(filter))
Dmitri Plotnikov23411b22010-12-21 17:53:10 -080093 .appendQueryParameter(
94 ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(Directory.DEFAULT))
95 .build();
Daisuke Miyakawaecd392b2011-10-12 12:27:16 -070096 } else {
97 allContactsUri = buildSectionIndexerUri(Contacts.CONTENT_URI).buildUpon()
98 .appendQueryParameter(
99 ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(Directory.DEFAULT))
100 .build();
101 }
Dmitri Plotnikov23411b22010-12-21 17:53:10 -0800102 loader.setUri(allContactsUri);
103 loader.setSelection(Contacts._ID + "!=?");
Daisuke Miyakawaecd392b2011-10-12 12:27:16 -0700104 loader.setSelectionArgs(new String[]{ String.valueOf(mTargetContactId) });
Yorke Leec9bb2172014-07-10 11:38:34 -0700105 if (getSortOrder() == ContactsPreferences.SORT_ORDER_PRIMARY) {
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700106 loader.setSortOrder(Contacts.SORT_KEY_PRIMARY);
107 } else {
108 loader.setSortOrder(Contacts.SORT_KEY_ALTERNATIVE);
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -0700109 }
110 }
111
112 @Override
113 public boolean isEmpty() {
114 return false;
115 }
116
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700117 public void setSuggestionsCursor(Cursor cursor) {
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700118 changeCursor(PARTITION_SUGGESTIONS, cursor);
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700119 }
120
121 @Override
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700122 public void changeCursor(Cursor cursor) {
123 changeCursor(PARTITION_ALL_CONTACTS, cursor);
124 }
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700125
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700126 @Override
Dmitri Plotnikove8a95172010-06-23 16:18:10 -0700127 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 Plotnikov2475ac82010-07-14 15:11:25 -0700133 public int getViewTypeCount() {
Daisuke Miyakawaecd392b2011-10-12 12:27:16 -0700134 return super.getViewTypeCount();
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700135 }
136
137 @Override
Dmitri Plotnikov2475ac82010-07-14 15:11:25 -0700138 public int getItemViewType(int partition, int position) {
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700139 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 Miyakawaa8729cd2011-10-12 12:27:16 -0700147 View view = inflate(R.layout.join_contact_picker_section_header, parent);
Dmitri Plotnikovd8f84e02011-01-23 18:03:14 -0800148 ((TextView) view.findViewById(R.id.text)).setText(
149 R.string.separatorJoinAggregateSuggestions);
150 return view;
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -0700151 }
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700152 case PARTITION_ALL_CONTACTS: {
Daisuke Miyakawaa8729cd2011-10-12 12:27:16 -0700153 View view = inflate(R.layout.join_contact_picker_section_header, parent);
Dmitri Plotnikovd8f84e02011-01-23 18:03:14 -0800154 ((TextView) view.findViewById(R.id.text)).setText(
155 R.string.separatorJoinAggregateAll);
156 return view;
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700157 }
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700158 }
159
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700160 return null;
161 }
162
163 @Override
Dmitri Plotnikove8a95172010-06-23 16:18:10 -0700164 protected void bindHeaderView(View view, int partitionIndex, Cursor cursor) {
165 // Header views are static - nothing needs to be bound
166 }
167
168 @Override
Andrew Lee551da172014-04-28 11:43:47 -0700169 protected ContactListItemView newView(
170 Context context, int partition, Cursor cursor, int position, ViewGroup parent) {
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700171 switch (partition) {
172 case PARTITION_SUGGESTIONS:
173 case PARTITION_ALL_CONTACTS:
174 return super.newView(context, partition, cursor, position, parent);
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700175 }
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700176 return null;
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700177 }
178
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -0700179 private View inflate(int layoutId, ViewGroup parent) {
180 return LayoutInflater.from(getContext()).inflate(layoutId, parent, false);
181 }
182
183 @Override
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700184 protected void bindView(View itemView, int partition, Cursor cursor, int position) {
Brian Attwellc9e81072014-09-15 11:08:19 -0700185 super.bindView(itemView, partition, cursor, position);
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700186 switch (partition) {
187 case PARTITION_SUGGESTIONS: {
Daisuke Miyakawaecd392b2011-10-12 12:27:16 -0700188 final ContactListItemView view = (ContactListItemView) itemView;
Dmitri Plotnikovd8f84e02011-01-23 18:03:14 -0800189 view.setSectionHeader(null);
Dmitri Plotnikov1228c812010-09-23 13:11:56 -0700190 bindPhoto(view, partition, cursor);
Brian Attwell56151b82014-09-03 20:14:47 -0700191 bindNameAndViewId(view, cursor);
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700192 break;
193 }
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700194 case PARTITION_ALL_CONTACTS: {
Daisuke Miyakawaecd392b2011-10-12 12:27:16 -0700195 final ContactListItemView view = (ContactListItemView) itemView;
Isaac Katzenelsonead19c52011-07-29 18:24:53 -0700196 bindSectionHeaderAndDivider(view, position, cursor);
Dmitri Plotnikov1228c812010-09-23 13:11:56 -0700197 bindPhoto(view, partition, cursor);
Brian Attwell56151b82014-09-03 20:14:47 -0700198 bindNameAndViewId(view, cursor);
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700199 break;
200 }
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -0700201 }
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -0700202 }
203
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700204 @Override
Dmitri Plotnikov8773bcb2010-07-15 18:55:46 -0700205 public Uri getContactUri(int partitionIndex, Cursor cursor) {
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -0700206 long contactId = cursor.getLong(ContactQuery.CONTACT_ID);
207 String lookupKey = cursor.getString(ContactQuery.CONTACT_LOOKUP_KEY);
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -0700208 return Contacts.getLookupUri(contactId, lookupKey);
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700209 }
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700210}