blob: 5e737a7ae19dc6360f1578ceac5388bd563e4ba4 [file] [log] [blame]
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -07001/*
2 * Copyright (C) 2007 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
17package com.android.contacts;
18
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -070019
20import com.android.contacts.list.ContactEntryListConfiguration;
21import com.android.contacts.list.JoinContactListAdapter;
22import com.android.contacts.list.JoinContactListConfiguration;
Dmitri Plotnikov807a0fe2010-04-20 12:03:10 -070023
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -070024import android.content.ContentUris;
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -070025import android.content.Intent;
26import android.database.Cursor;
27import android.database.MatrixCursor;
28import android.net.Uri;
29import android.net.Uri.Builder;
30import android.provider.ContactsContract;
31import android.provider.ContactsContract.Contacts;
32import android.provider.ContactsContract.Contacts.AggregationSuggestions;
33import android.text.TextUtils;
34import android.util.Log;
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -070035import android.widget.TextView;
36
37/**
38 * An activity that shows a list of contacts that can be joined with the target contact.
39 */
40public class JoinContactActivity extends ContactsListActivity {
41
42 private static final String TAG = "JoinContactActivity";
43
44 /**
45 * The action for the join contact activity.
46 * <p>
47 * Input: extra field {@link #EXTRA_TARGET_CONTACT_ID} is the aggregate ID.
48 * TODO: move to {@link ContactsContract}.
49 */
50 public static final String JOIN_CONTACT = "com.android.contacts.action.JOIN_CONTACT";
51
52 /**
53 * Used with {@link #JOIN_CONTACT} to give it the target for aggregation.
54 * <p>
55 * Type: LONG
56 */
57 public static final String EXTRA_TARGET_CONTACT_ID = "com.android.contacts.action.CONTACT_ID";
58
59 /** Maximum number of suggestions shown for joining aggregates */
60 private static final int MAX_SUGGESTIONS = 4;
61
62 private long mTargetContactId;
63
64 /**
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -070065 * The ID of the special item described above.
66 */
67 private static final long JOIN_MODE_SHOW_ALL_CONTACTS_ID = -2;
68
69 private boolean mLoadingJoinSuggestions;
70
71 private JoinContactListAdapter mAdapter;
72
73 @Override
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -070074 protected ContactEntryListConfiguration resolveIntent(Intent intent) {
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -070075 mMode = MODE_PICK_CONTACT;
76 mTargetContactId = intent.getLongExtra(EXTRA_TARGET_CONTACT_ID, -1);
77 if (mTargetContactId == -1) {
78 Log.e(TAG, "Intent " + intent.getAction() + " is missing required extra: "
79 + EXTRA_TARGET_CONTACT_ID);
80 setResult(RESULT_CANCELED);
81 finish();
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -070082 return null;
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -070083 }
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -070084 return new JoinContactListConfiguration(this, this);
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -070085 }
86
87 @Override
88 public void initContentView() {
89 setContentView(R.layout.contacts_list_content_join);
90 TextView blurbView = (TextView)findViewById(R.id.join_contact_blurb);
91
92 String blurb = getString(R.string.blurbJoinContactDataWith,
93 getContactDisplayName(mTargetContactId));
94 blurbView.setText(blurb);
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -070095 mConfig.configureListView(getListView());
96
97 mAdapter = (JoinContactListAdapter)getListView().getAdapter();
98 mAdapter.setJoinModeShowAllContacts(true);
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -070099 }
100
101 @Override
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700102 public void onListItemClick(int position, long id) {
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -0700103 if (id == JOIN_MODE_SHOW_ALL_CONTACTS_ID) {
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700104 mAdapter.setJoinModeShowAllContacts(false);
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -0700105 startQuery();
106 } else {
107 final Uri uri = getSelectedUri(position);
108 returnPickerResult(null, null, uri);
109 }
110 }
111
112 @Override
113 protected Uri getUriToQuery() {
114 return getJoinSuggestionsUri(null);
115 }
116
117 /*
118 * TODO: move to a background thread.
119 */
120 private String getContactDisplayName(long contactId) {
121 String contactName = null;
122 Cursor c = getContentResolver().query(
123 ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId),
124 new String[] {Contacts.DISPLAY_NAME}, null, null, null);
125 try {
126 if (c != null && c.moveToFirst()) {
127 contactName = c.getString(0);
128 }
129 } finally {
130 if (c != null) {
131 c.close();
132 }
133 }
134
135 if (contactName == null) {
136 contactName = "";
137 }
138
139 return contactName;
140 }
141
142 private Uri getJoinSuggestionsUri(String filter) {
143 Builder builder = Contacts.CONTENT_URI.buildUpon();
144 builder.appendEncodedPath(String.valueOf(mTargetContactId));
145 builder.appendEncodedPath(AggregationSuggestions.CONTENT_DIRECTORY);
146 if (!TextUtils.isEmpty(filter)) {
147 builder.appendEncodedPath(Uri.encode(filter));
148 }
149 builder.appendQueryParameter("limit", String.valueOf(MAX_SUGGESTIONS));
150 return builder.build();
151 }
152
153 @Override
Dmitri Plotnikov807a0fe2010-04-20 12:03:10 -0700154 public
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -0700155 Cursor doFilter(String filter) {
156 throw new UnsupportedOperationException();
157 }
158
159 private Cursor getShowAllContactsLabelCursor(String[] projection) {
160 MatrixCursor matrixCursor = new MatrixCursor(projection);
161 Object[] row = new Object[projection.length];
162 // The only columns we care about is the id
163 row[SUMMARY_ID_COLUMN_INDEX] = JOIN_MODE_SHOW_ALL_CONTACTS_ID;
164 matrixCursor.addRow(row);
165 return matrixCursor;
166 }
167
168 @Override
169 protected void startQuery(Uri uri, String[] projection) {
170 mLoadingJoinSuggestions = true;
171 startQuery(uri, projection, null, null, null);
172 }
173
174 @Override
175 protected void onQueryComplete(Cursor cursor) {
176 // Whenever we get a suggestions cursor, we need to immediately kick off
177 // another query for the complete list of contacts
178 if (cursor != null && mLoadingJoinSuggestions) {
179 mLoadingJoinSuggestions = false;
180 if (cursor.getCount() > 0) {
181 mAdapter.setSuggestionsCursor(cursor);
182 } else {
183 cursor.close();
184 mAdapter.setSuggestionsCursor(null);
185 }
186
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700187 if (mAdapter.getSuggestionsCursorCount() == 0
188 || !mAdapter.isJoinModeShowAllContacts()) {
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -0700189 startQuery(getContactFilterUri(getTextFilter()),
190 CONTACTS_SUMMARY_PROJECTION,
191 Contacts._ID + " != " + mTargetContactId
192 + " AND " + ContactsContract.Contacts.IN_VISIBLE_GROUP + "=1", null,
193 getSortOrder(CONTACTS_SUMMARY_PROJECTION));
194 return;
195 }
196
197 cursor = getShowAllContactsLabelCursor(CONTACTS_SUMMARY_PROJECTION);
198 }
199
200 super.onQueryComplete(cursor);
201 }
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -0700202}