blob: e5555231002dfc8f7ac83e7a27941e80c25c97ec [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 Plotnikovcaf498b2010-04-23 14:58:08 -070035import android.widget.ListView;
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -070036import android.widget.TextView;
37
38/**
39 * An activity that shows a list of contacts that can be joined with the target contact.
40 */
41public class JoinContactActivity extends ContactsListActivity {
42
43 private static final String TAG = "JoinContactActivity";
44
45 /**
46 * The action for the join contact activity.
47 * <p>
48 * Input: extra field {@link #EXTRA_TARGET_CONTACT_ID} is the aggregate ID.
49 * TODO: move to {@link ContactsContract}.
50 */
51 public static final String JOIN_CONTACT = "com.android.contacts.action.JOIN_CONTACT";
52
53 /**
54 * Used with {@link #JOIN_CONTACT} to give it the target for aggregation.
55 * <p>
56 * Type: LONG
57 */
58 public static final String EXTRA_TARGET_CONTACT_ID = "com.android.contacts.action.CONTACT_ID";
59
60 /** Maximum number of suggestions shown for joining aggregates */
61 private static final int MAX_SUGGESTIONS = 4;
62
63 private long mTargetContactId;
64
65 /**
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -070066 * The ID of the special item described above.
67 */
68 private static final long JOIN_MODE_SHOW_ALL_CONTACTS_ID = -2;
69
70 private boolean mLoadingJoinSuggestions;
71
72 private JoinContactListAdapter mAdapter;
73
74 @Override
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -070075 protected ContactEntryListConfiguration resolveIntent(Intent intent) {
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -070076 mMode = MODE_PICK_CONTACT;
77 mTargetContactId = intent.getLongExtra(EXTRA_TARGET_CONTACT_ID, -1);
78 if (mTargetContactId == -1) {
79 Log.e(TAG, "Intent " + intent.getAction() + " is missing required extra: "
80 + EXTRA_TARGET_CONTACT_ID);
81 setResult(RESULT_CANCELED);
82 finish();
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -070083 return null;
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -070084 }
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -070085 return new JoinContactListConfiguration(this, this);
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -070086 }
87
88 @Override
89 public void initContentView() {
Dmitri Plotnikovcaf498b2010-04-23 14:58:08 -070090 setContentView(mConfig.createView());
91
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -070092 TextView blurbView = (TextView)findViewById(R.id.join_contact_blurb);
93
94 String blurb = getString(R.string.blurbJoinContactDataWith,
95 getContactDisplayName(mTargetContactId));
96 blurbView.setText(blurb);
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -070097
Dmitri Plotnikovcaf498b2010-04-23 14:58:08 -070098 ListView listView = (ListView)findViewById(android.R.id.list);
99 mAdapter = (JoinContactListAdapter)listView.getAdapter();
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700100 mAdapter.setJoinModeShowAllContacts(true);
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -0700101 }
102
103 @Override
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700104 public void onListItemClick(int position, long id) {
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -0700105 if (id == JOIN_MODE_SHOW_ALL_CONTACTS_ID) {
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700106 mAdapter.setJoinModeShowAllContacts(false);
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -0700107 startQuery();
108 } else {
109 final Uri uri = getSelectedUri(position);
110 returnPickerResult(null, null, uri);
111 }
112 }
113
114 @Override
115 protected Uri getUriToQuery() {
116 return getJoinSuggestionsUri(null);
117 }
118
119 /*
120 * TODO: move to a background thread.
121 */
122 private String getContactDisplayName(long contactId) {
123 String contactName = null;
124 Cursor c = getContentResolver().query(
125 ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId),
126 new String[] {Contacts.DISPLAY_NAME}, null, null, null);
127 try {
128 if (c != null && c.moveToFirst()) {
129 contactName = c.getString(0);
130 }
131 } finally {
132 if (c != null) {
133 c.close();
134 }
135 }
136
137 if (contactName == null) {
138 contactName = "";
139 }
140
141 return contactName;
142 }
143
144 private Uri getJoinSuggestionsUri(String filter) {
145 Builder builder = Contacts.CONTENT_URI.buildUpon();
146 builder.appendEncodedPath(String.valueOf(mTargetContactId));
147 builder.appendEncodedPath(AggregationSuggestions.CONTENT_DIRECTORY);
148 if (!TextUtils.isEmpty(filter)) {
149 builder.appendEncodedPath(Uri.encode(filter));
150 }
151 builder.appendQueryParameter("limit", String.valueOf(MAX_SUGGESTIONS));
152 return builder.build();
153 }
154
155 @Override
Dmitri Plotnikov807a0fe2010-04-20 12:03:10 -0700156 public
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -0700157 Cursor doFilter(String filter) {
158 throw new UnsupportedOperationException();
159 }
160
161 private Cursor getShowAllContactsLabelCursor(String[] projection) {
162 MatrixCursor matrixCursor = new MatrixCursor(projection);
163 Object[] row = new Object[projection.length];
164 // The only columns we care about is the id
165 row[SUMMARY_ID_COLUMN_INDEX] = JOIN_MODE_SHOW_ALL_CONTACTS_ID;
166 matrixCursor.addRow(row);
167 return matrixCursor;
168 }
169
170 @Override
171 protected void startQuery(Uri uri, String[] projection) {
172 mLoadingJoinSuggestions = true;
173 startQuery(uri, projection, null, null, null);
174 }
175
176 @Override
177 protected void onQueryComplete(Cursor cursor) {
178 // Whenever we get a suggestions cursor, we need to immediately kick off
179 // another query for the complete list of contacts
180 if (cursor != null && mLoadingJoinSuggestions) {
181 mLoadingJoinSuggestions = false;
182 if (cursor.getCount() > 0) {
183 mAdapter.setSuggestionsCursor(cursor);
184 } else {
185 cursor.close();
186 mAdapter.setSuggestionsCursor(null);
187 }
188
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700189 if (mAdapter.getSuggestionsCursorCount() == 0
190 || !mAdapter.isJoinModeShowAllContacts()) {
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -0700191 startQuery(getContactFilterUri(getTextFilter()),
192 CONTACTS_SUMMARY_PROJECTION,
193 Contacts._ID + " != " + mTargetContactId
194 + " AND " + ContactsContract.Contacts.IN_VISIBLE_GROUP + "=1", null,
195 getSortOrder(CONTACTS_SUMMARY_PROJECTION));
196 return;
197 }
198
199 cursor = getShowAllContactsLabelCursor(CONTACTS_SUMMARY_PROJECTION);
200 }
201
202 super.onQueryComplete(cursor);
203 }
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -0700204}