• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 import android.content.Context;
19 import android.content.CursorLoader;
20 import android.database.Cursor;
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;
26 import android.provider.ContactsContract.Directory;
27 import android.text.TextUtils;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.widget.TextView;
32 
33 import com.android.contacts.R;
34 import com.android.contacts.preference.ContactsPreferences;
35 
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 
41     public static final int PARTITION_SUGGESTIONS = 0;
42     public static final int PARTITION_ALL_CONTACTS = 1;
43 
44     private long mTargetContactId;
45 
JoinContactListAdapter(Context context)46     public JoinContactListAdapter(Context context) {
47         super(context);
48         setPinnedPartitionHeadersEnabled(true);
49         setSectionHeaderDisplayEnabled(true);
50         setIndexedPartition(PARTITION_ALL_CONTACTS);
51         setDirectorySearchMode(DirectoryListLoader.SEARCH_MODE_NONE);
52     }
53 
54     @Override
addPartitions()55     protected void addPartitions() {
56         // Partition 0: suggestions
57         addPartition(false, true);
58 
59         // Partition 1: All contacts
60         addPartition(createDefaultDirectoryPartition());
61     }
62 
setTargetContactId(long targetContactId)63     public void setTargetContactId(long targetContactId) {
64         this.mTargetContactId = targetContactId;
65     }
66 
67     @Override
configureLoader(CursorLoader cursorLoader, long directoryId)68     public void configureLoader(CursorLoader cursorLoader, long directoryId) {
69         JoinContactLoader loader = (JoinContactLoader) cursorLoader;
70 
71         final Builder builder = Contacts.CONTENT_URI.buildUpon();
72         builder.appendEncodedPath(String.valueOf(mTargetContactId));
73         builder.appendEncodedPath(AggregationSuggestions.CONTENT_DIRECTORY);
74 
75         final String filter = getQueryString();
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
85         loader.setProjection(getProjection(false));
86         final Uri allContactsUri;
87         if (!TextUtils.isEmpty(filter)) {
88             allContactsUri = buildSectionIndexerUri(Contacts.CONTENT_FILTER_URI).buildUpon()
89                 .appendEncodedPath(Uri.encode(filter))
90                 .appendQueryParameter(
91                         ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(Directory.DEFAULT))
92                 .build();
93         } else {
94             allContactsUri = buildSectionIndexerUri(Contacts.CONTENT_URI).buildUpon()
95                 .appendQueryParameter(
96                         ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(Directory.DEFAULT))
97                 .build();
98         }
99         loader.setUri(allContactsUri);
100         loader.setSelection(Contacts._ID + "!=?");
101         loader.setSelectionArgs(new String[]{ String.valueOf(mTargetContactId) });
102         if (getSortOrder() == ContactsPreferences.SORT_ORDER_PRIMARY) {
103             loader.setSortOrder(Contacts.SORT_KEY_PRIMARY);
104         } else {
105             loader.setSortOrder(Contacts.SORT_KEY_ALTERNATIVE);
106         }
107     }
108 
109     @Override
isEmpty()110     public boolean isEmpty() {
111         return false;
112     }
113 
setSuggestionsCursor(Cursor cursor)114     public void setSuggestionsCursor(Cursor cursor) {
115         changeCursor(PARTITION_SUGGESTIONS, cursor);
116     }
117 
118     @Override
changeCursor(Cursor cursor)119     public void changeCursor(Cursor cursor) {
120         changeCursor(PARTITION_ALL_CONTACTS, cursor);
121     }
122 
123     @Override
configureDefaultPartition(boolean showIfEmpty, boolean hasHeader)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
getViewTypeCount()130     public int getViewTypeCount() {
131         return super.getViewTypeCount();
132     }
133 
134     @Override
getItemViewType(int partition, int position)135     public int getItemViewType(int partition, int position) {
136         return super.getItemViewType(partition, position);
137     }
138 
139     @Override
newHeaderView(Context context, int partition, Cursor cursor, ViewGroup parent)140     protected View newHeaderView(Context context, int partition, Cursor cursor,
141             ViewGroup parent) {
142         switch (partition) {
143             case PARTITION_SUGGESTIONS: {
144                 View view = inflate(R.layout.join_contact_picker_section_header, parent);
145                 ((TextView) view.findViewById(R.id.text)).setText(
146                         R.string.separatorJoinAggregateSuggestions);
147                 return view;
148             }
149             case PARTITION_ALL_CONTACTS: {
150                 View view = inflate(R.layout.join_contact_picker_section_header, parent);
151                 ((TextView) view.findViewById(R.id.text)).setText(
152                         R.string.separatorJoinAggregateAll);
153                 return view;
154             }
155         }
156 
157         return null;
158     }
159 
160     @Override
bindHeaderView(View view, int partitionIndex, Cursor cursor)161     protected void bindHeaderView(View view, int partitionIndex, Cursor cursor) {
162         // Header views are static - nothing needs to be bound
163     }
164 
165     @Override
newView( Context context, int partition, Cursor cursor, int position, ViewGroup parent)166     protected ContactListItemView newView(
167             Context context, int partition, Cursor cursor, int position, ViewGroup parent) {
168         switch (partition) {
169             case PARTITION_SUGGESTIONS:
170             case PARTITION_ALL_CONTACTS:
171                 return super.newView(context, partition, cursor, position, parent);
172         }
173         return null;
174     }
175 
inflate(int layoutId, ViewGroup parent)176     private View inflate(int layoutId, ViewGroup parent) {
177         return LayoutInflater.from(getContext()).inflate(layoutId, parent, false);
178     }
179 
180     @Override
bindView(View itemView, int partition, Cursor cursor, int position)181     protected void bindView(View itemView, int partition, Cursor cursor, int position) {
182         super.bindView(itemView, partition, cursor, position);
183         switch (partition) {
184             case PARTITION_SUGGESTIONS: {
185                 final ContactListItemView view = (ContactListItemView) itemView;
186                 view.setSectionHeader(null);
187                 bindPhoto(view, partition, cursor);
188                 bindNameAndViewId(view, cursor);
189                 break;
190             }
191             case PARTITION_ALL_CONTACTS: {
192                 final ContactListItemView view = (ContactListItemView) itemView;
193                 bindSectionHeaderAndDivider(view, position, cursor);
194                 bindPhoto(view, partition, cursor);
195                 bindNameAndViewId(view, cursor);
196                 break;
197             }
198         }
199     }
200 
201     @Override
getContactUri(int partitionIndex, Cursor cursor)202     public Uri getContactUri(int partitionIndex, Cursor cursor) {
203         long contactId = cursor.getLong(ContactQuery.CONTACT_ID);
204         String lookupKey = cursor.getString(ContactQuery.CONTACT_LOOKUP_KEY);
205         return Contacts.getLookupUri(contactId, lookupKey);
206     }
207 }
208