blob: 82da6f7a569b5bacb0df342362b5860817af65c3 [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;
Gary Mai69c182a2016-12-05 13:07:03 -080034import com.android.contacts.preference.ContactsPreferences;
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070035
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -070036public class JoinContactListAdapter extends ContactListAdapter {
37
38 /** Maximum number of suggestions shown for joining aggregates */
39 private static final int MAX_SUGGESTIONS = 4;
40
Dmitri Plotnikove1247222010-06-02 18:14:21 -070041 public static final int PARTITION_SUGGESTIONS = 0;
Daisuke Miyakawaa8729cd2011-10-12 12:27:16 -070042 public static final int PARTITION_ALL_CONTACTS = 1;
Dmitri Plotnikove1247222010-06-02 18:14:21 -070043
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -070044 private long mTargetContactId;
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -070045
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -070046 public JoinContactListAdapter(Context context) {
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -070047 super(context);
Dmitri Plotnikove1247222010-06-02 18:14:21 -070048 setPinnedPartitionHeadersEnabled(true);
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -070049 setSectionHeaderDisplayEnabled(true);
Dmitri Plotnikove1247222010-06-02 18:14:21 -070050 setIndexedPartition(PARTITION_ALL_CONTACTS);
Dmitri Plotnikov4d174aa2010-10-25 15:38:54 -070051 setDirectorySearchMode(DirectoryListLoader.SEARCH_MODE_NONE);
Dmitri Plotnikove1247222010-06-02 18:14:21 -070052 }
53
54 @Override
55 protected void addPartitions() {
Dmitri Plotnikove1247222010-06-02 18:14:21 -070056 // Partition 0: suggestions
57 addPartition(false, true);
58
Daisuke Miyakawaa8729cd2011-10-12 12:27:16 -070059 // Partition 1: All contacts
Dmitri Plotnikove8a95172010-06-23 16:18:10 -070060 addPartition(createDefaultDirectoryPartition());
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -070061 }
62
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -070063 public void setTargetContactId(long targetContactId) {
64 this.mTargetContactId = targetContactId;
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -070065 }
66
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -070067 @Override
Dmitri Plotnikovd5061fe2010-06-07 15:06:58 -070068 public void configureLoader(CursorLoader cursorLoader, long directoryId) {
Daisuke Miyakawa6da2a2d2011-11-09 10:33:11 -080069 JoinContactLoader loader = (JoinContactLoader) cursorLoader;
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -070070
Daisuke Miyakawaecd392b2011-10-12 12:27:16 -070071 final Builder builder = Contacts.CONTENT_URI.buildUpon();
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -070072 builder.appendEncodedPath(String.valueOf(mTargetContactId));
73 builder.appendEncodedPath(AggregationSuggestions.CONTENT_DIRECTORY);
74
Daisuke Miyakawaecd392b2011-10-12 12:27:16 -070075 final String filter = getQueryString();
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -070076 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 Miyakawa6da2a2d2011-11-09 10:33:11 -080085 loader.setProjection(getProjection(false));
Daisuke Miyakawaecd392b2011-10-12 12:27:16 -070086 final Uri allContactsUri;
87 if (!TextUtils.isEmpty(filter)) {
88 allContactsUri = buildSectionIndexerUri(Contacts.CONTENT_FILTER_URI).buildUpon()
89 .appendEncodedPath(Uri.encode(filter))
Dmitri Plotnikov23411b22010-12-21 17:53:10 -080090 .appendQueryParameter(
91 ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(Directory.DEFAULT))
92 .build();
Daisuke Miyakawaecd392b2011-10-12 12:27:16 -070093 } else {
94 allContactsUri = buildSectionIndexerUri(Contacts.CONTENT_URI).buildUpon()
95 .appendQueryParameter(
96 ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(Directory.DEFAULT))
97 .build();
98 }
Dmitri Plotnikov23411b22010-12-21 17:53:10 -080099 loader.setUri(allContactsUri);
100 loader.setSelection(Contacts._ID + "!=?");
Daisuke Miyakawaecd392b2011-10-12 12:27:16 -0700101 loader.setSelectionArgs(new String[]{ String.valueOf(mTargetContactId) });
Yorke Leec9bb2172014-07-10 11:38:34 -0700102 if (getSortOrder() == ContactsPreferences.SORT_ORDER_PRIMARY) {
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700103 loader.setSortOrder(Contacts.SORT_KEY_PRIMARY);
104 } else {
105 loader.setSortOrder(Contacts.SORT_KEY_ALTERNATIVE);
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -0700106 }
107 }
108
109 @Override
110 public boolean isEmpty() {
111 return false;
112 }
113
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700114 public void setSuggestionsCursor(Cursor cursor) {
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700115 changeCursor(PARTITION_SUGGESTIONS, cursor);
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700116 }
117
118 @Override
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700119 public void changeCursor(Cursor cursor) {
120 changeCursor(PARTITION_ALL_CONTACTS, cursor);
121 }
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700122
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700123 @Override
Dmitri Plotnikove8a95172010-06-23 16:18:10 -0700124 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 Plotnikov2475ac82010-07-14 15:11:25 -0700130 public int getViewTypeCount() {
Daisuke Miyakawaecd392b2011-10-12 12:27:16 -0700131 return super.getViewTypeCount();
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700132 }
133
134 @Override
Dmitri Plotnikov2475ac82010-07-14 15:11:25 -0700135 public int getItemViewType(int partition, int position) {
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700136 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 Miyakawaa8729cd2011-10-12 12:27:16 -0700144 View view = inflate(R.layout.join_contact_picker_section_header, parent);
Dmitri Plotnikovd8f84e02011-01-23 18:03:14 -0800145 ((TextView) view.findViewById(R.id.text)).setText(
146 R.string.separatorJoinAggregateSuggestions);
147 return view;
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -0700148 }
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700149 case PARTITION_ALL_CONTACTS: {
Daisuke Miyakawaa8729cd2011-10-12 12:27:16 -0700150 View view = inflate(R.layout.join_contact_picker_section_header, parent);
Dmitri Plotnikovd8f84e02011-01-23 18:03:14 -0800151 ((TextView) view.findViewById(R.id.text)).setText(
152 R.string.separatorJoinAggregateAll);
153 return view;
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700154 }
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700155 }
156
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700157 return null;
158 }
159
160 @Override
Dmitri Plotnikove8a95172010-06-23 16:18:10 -0700161 protected void bindHeaderView(View view, int partitionIndex, Cursor cursor) {
162 // Header views are static - nothing needs to be bound
163 }
164
165 @Override
Andrew Lee551da172014-04-28 11:43:47 -0700166 protected ContactListItemView newView(
167 Context context, int partition, Cursor cursor, int position, ViewGroup parent) {
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700168 switch (partition) {
169 case PARTITION_SUGGESTIONS:
170 case PARTITION_ALL_CONTACTS:
171 return super.newView(context, partition, cursor, position, parent);
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700172 }
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700173 return null;
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700174 }
175
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -0700176 private View inflate(int layoutId, ViewGroup parent) {
177 return LayoutInflater.from(getContext()).inflate(layoutId, parent, false);
178 }
179
180 @Override
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700181 protected void bindView(View itemView, int partition, Cursor cursor, int position) {
Brian Attwellc9e81072014-09-15 11:08:19 -0700182 super.bindView(itemView, partition, cursor, position);
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700183 switch (partition) {
184 case PARTITION_SUGGESTIONS: {
Daisuke Miyakawaecd392b2011-10-12 12:27:16 -0700185 final ContactListItemView view = (ContactListItemView) itemView;
Dmitri Plotnikovd8f84e02011-01-23 18:03:14 -0800186 view.setSectionHeader(null);
Dmitri Plotnikov1228c812010-09-23 13:11:56 -0700187 bindPhoto(view, partition, cursor);
Brian Attwell56151b82014-09-03 20:14:47 -0700188 bindNameAndViewId(view, cursor);
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700189 break;
190 }
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700191 case PARTITION_ALL_CONTACTS: {
Daisuke Miyakawaecd392b2011-10-12 12:27:16 -0700192 final ContactListItemView view = (ContactListItemView) itemView;
Isaac Katzenelsonead19c52011-07-29 18:24:53 -0700193 bindSectionHeaderAndDivider(view, position, cursor);
Dmitri Plotnikov1228c812010-09-23 13:11:56 -0700194 bindPhoto(view, partition, cursor);
Brian Attwell56151b82014-09-03 20:14:47 -0700195 bindNameAndViewId(view, cursor);
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700196 break;
197 }
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -0700198 }
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -0700199 }
200
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700201 @Override
Dmitri Plotnikov8773bcb2010-07-15 18:55:46 -0700202 public Uri getContactUri(int partitionIndex, Cursor cursor) {
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -0700203 long contactId = cursor.getLong(ContactQuery.CONTACT_ID);
204 String lookupKey = cursor.getString(ContactQuery.CONTACT_LOOKUP_KEY);
Dmitri Plotnikove4d32d92010-05-10 19:06:22 -0700205 return Contacts.getLookupUri(contactId, lookupKey);
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700206 }
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700207}