blob: bacd0afe826708cd014e8347cdfe5a0d2a3ff929 [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
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -070020import com.android.contacts.list.JoinContactListAdapter;
Dmitri Plotnikov59fb48e2010-04-26 17:09:19 -070021import com.android.contacts.list.JoinContactListFragment;
Dmitri Plotnikov807a0fe2010-04-20 12:03:10 -070022
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -070023import android.content.ContentUris;
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -070024import android.content.Intent;
25import android.database.Cursor;
26import android.database.MatrixCursor;
27import android.net.Uri;
28import android.net.Uri.Builder;
29import android.provider.ContactsContract;
30import android.provider.ContactsContract.Contacts;
31import android.provider.ContactsContract.Contacts.AggregationSuggestions;
32import android.text.TextUtils;
33import android.util.Log;
Dmitri Plotnikovcaf498b2010-04-23 14:58:08 -070034import android.widget.ListView;
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 Plotnikov59fb48e2010-04-26 17:09:19 -070074 protected boolean 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 Plotnikov59fb48e2010-04-26 17:09:19 -070082 return false;
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -070083 }
Dmitri Plotnikov59fb48e2010-04-26 17:09:19 -070084
85 mListFragment = new JoinContactListFragment();
86
87 return true;
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -070088 }
89
90 @Override
Dmitri Plotnikov59fb48e2010-04-26 17:09:19 -070091 protected void onResume() {
92 super.onResume();
Dmitri Plotnikovcaf498b2010-04-23 14:58:08 -070093
Dmitri Plotnikov59fb48e2010-04-26 17:09:19 -070094 // TODO move this to onAttach of the corresponding fragment
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -070095 TextView blurbView = (TextView)findViewById(R.id.join_contact_blurb);
96
97 String blurb = getString(R.string.blurbJoinContactDataWith,
98 getContactDisplayName(mTargetContactId));
99 blurbView.setText(blurb);
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700100
Dmitri Plotnikovcaf498b2010-04-23 14:58:08 -0700101 ListView listView = (ListView)findViewById(android.R.id.list);
102 mAdapter = (JoinContactListAdapter)listView.getAdapter();
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700103 mAdapter.setJoinModeShowAllContacts(true);
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -0700104 }
105
106 @Override
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700107 public void onListItemClick(int position, long id) {
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -0700108 if (id == JOIN_MODE_SHOW_ALL_CONTACTS_ID) {
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700109 mAdapter.setJoinModeShowAllContacts(false);
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -0700110 startQuery();
111 } else {
112 final Uri uri = getSelectedUri(position);
113 returnPickerResult(null, null, uri);
114 }
115 }
116
117 @Override
118 protected Uri getUriToQuery() {
119 return getJoinSuggestionsUri(null);
120 }
121
122 /*
123 * TODO: move to a background thread.
124 */
125 private String getContactDisplayName(long contactId) {
126 String contactName = null;
127 Cursor c = getContentResolver().query(
128 ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId),
129 new String[] {Contacts.DISPLAY_NAME}, null, null, null);
130 try {
131 if (c != null && c.moveToFirst()) {
132 contactName = c.getString(0);
133 }
134 } finally {
135 if (c != null) {
136 c.close();
137 }
138 }
139
140 if (contactName == null) {
141 contactName = "";
142 }
143
144 return contactName;
145 }
146
147 private Uri getJoinSuggestionsUri(String filter) {
148 Builder builder = Contacts.CONTENT_URI.buildUpon();
149 builder.appendEncodedPath(String.valueOf(mTargetContactId));
150 builder.appendEncodedPath(AggregationSuggestions.CONTENT_DIRECTORY);
151 if (!TextUtils.isEmpty(filter)) {
152 builder.appendEncodedPath(Uri.encode(filter));
153 }
154 builder.appendQueryParameter("limit", String.valueOf(MAX_SUGGESTIONS));
155 return builder.build();
156 }
157
158 @Override
Dmitri Plotnikov807a0fe2010-04-20 12:03:10 -0700159 public
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -0700160 Cursor doFilter(String filter) {
161 throw new UnsupportedOperationException();
162 }
163
164 private Cursor getShowAllContactsLabelCursor(String[] projection) {
165 MatrixCursor matrixCursor = new MatrixCursor(projection);
166 Object[] row = new Object[projection.length];
167 // The only columns we care about is the id
168 row[SUMMARY_ID_COLUMN_INDEX] = JOIN_MODE_SHOW_ALL_CONTACTS_ID;
169 matrixCursor.addRow(row);
170 return matrixCursor;
171 }
172
173 @Override
174 protected void startQuery(Uri uri, String[] projection) {
175 mLoadingJoinSuggestions = true;
176 startQuery(uri, projection, null, null, null);
177 }
178
179 @Override
180 protected void onQueryComplete(Cursor cursor) {
181 // Whenever we get a suggestions cursor, we need to immediately kick off
182 // another query for the complete list of contacts
183 if (cursor != null && mLoadingJoinSuggestions) {
184 mLoadingJoinSuggestions = false;
185 if (cursor.getCount() > 0) {
186 mAdapter.setSuggestionsCursor(cursor);
187 } else {
188 cursor.close();
189 mAdapter.setSuggestionsCursor(null);
190 }
191
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700192 if (mAdapter.getSuggestionsCursorCount() == 0
193 || !mAdapter.isJoinModeShowAllContacts()) {
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -0700194 startQuery(getContactFilterUri(getTextFilter()),
195 CONTACTS_SUMMARY_PROJECTION,
196 Contacts._ID + " != " + mTargetContactId
197 + " AND " + ContactsContract.Contacts.IN_VISIBLE_GROUP + "=1", null,
198 getSortOrder(CONTACTS_SUMMARY_PROJECTION));
199 return;
200 }
201
202 cursor = getShowAllContactsLabelCursor(CONTACTS_SUMMARY_PROJECTION);
203 }
204
205 super.onQueryComplete(cursor);
206 }
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -0700207}