blob: 9024e0b3fbd306c9a88ebf4bcd71902335b6a937 [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 Plotnikov02935722010-05-06 11:56:12 -070022import com.android.internal.telephony.gsm.stk.ResultCode;
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 Plotnikov59fb48e2010-04-26 17:09:19 -070075 protected boolean 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 Plotnikov59fb48e2010-04-26 17:09:19 -070083 return false;
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -070084 }
Dmitri Plotnikov59fb48e2010-04-26 17:09:19 -070085
86 mListFragment = new JoinContactListFragment();
87
88 return true;
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -070089 }
90
91 @Override
Dmitri Plotnikov59fb48e2010-04-26 17:09:19 -070092 protected void onResume() {
93 super.onResume();
Dmitri Plotnikovcaf498b2010-04-23 14:58:08 -070094
Dmitri Plotnikov59fb48e2010-04-26 17:09:19 -070095 // TODO move this to onAttach of the corresponding fragment
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -070096 TextView blurbView = (TextView)findViewById(R.id.join_contact_blurb);
97
98 String blurb = getString(R.string.blurbJoinContactDataWith,
99 getContactDisplayName(mTargetContactId));
100 blurbView.setText(blurb);
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700101
Dmitri Plotnikovcaf498b2010-04-23 14:58:08 -0700102 ListView listView = (ListView)findViewById(android.R.id.list);
103 mAdapter = (JoinContactListAdapter)listView.getAdapter();
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700104 mAdapter.setJoinModeShowAllContacts(true);
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -0700105 }
106
107 @Override
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700108 public void onListItemClick(int position, long id) {
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -0700109 if (id == JOIN_MODE_SHOW_ALL_CONTACTS_ID) {
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700110 mAdapter.setJoinModeShowAllContacts(false);
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -0700111 startQuery();
112 } else {
113 final Uri uri = getSelectedUri(position);
Dmitri Plotnikov02935722010-05-06 11:56:12 -0700114 setResult(RESULT_OK, new Intent(null, uri));
115 finish();
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -0700116 }
117 }
118
119 @Override
120 protected Uri getUriToQuery() {
121 return getJoinSuggestionsUri(null);
122 }
123
124 /*
125 * TODO: move to a background thread.
126 */
127 private String getContactDisplayName(long contactId) {
128 String contactName = null;
129 Cursor c = getContentResolver().query(
130 ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId),
131 new String[] {Contacts.DISPLAY_NAME}, null, null, null);
132 try {
133 if (c != null && c.moveToFirst()) {
134 contactName = c.getString(0);
135 }
136 } finally {
137 if (c != null) {
138 c.close();
139 }
140 }
141
142 if (contactName == null) {
143 contactName = "";
144 }
145
146 return contactName;
147 }
148
149 private Uri getJoinSuggestionsUri(String filter) {
150 Builder builder = Contacts.CONTENT_URI.buildUpon();
151 builder.appendEncodedPath(String.valueOf(mTargetContactId));
152 builder.appendEncodedPath(AggregationSuggestions.CONTENT_DIRECTORY);
153 if (!TextUtils.isEmpty(filter)) {
154 builder.appendEncodedPath(Uri.encode(filter));
155 }
156 builder.appendQueryParameter("limit", String.valueOf(MAX_SUGGESTIONS));
157 return builder.build();
158 }
159
160 @Override
Dmitri Plotnikov807a0fe2010-04-20 12:03:10 -0700161 public
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -0700162 Cursor doFilter(String filter) {
163 throw new UnsupportedOperationException();
164 }
165
166 private Cursor getShowAllContactsLabelCursor(String[] projection) {
167 MatrixCursor matrixCursor = new MatrixCursor(projection);
168 Object[] row = new Object[projection.length];
169 // The only columns we care about is the id
170 row[SUMMARY_ID_COLUMN_INDEX] = JOIN_MODE_SHOW_ALL_CONTACTS_ID;
171 matrixCursor.addRow(row);
172 return matrixCursor;
173 }
174
175 @Override
176 protected void startQuery(Uri uri, String[] projection) {
177 mLoadingJoinSuggestions = true;
178 startQuery(uri, projection, null, null, null);
179 }
180
181 @Override
182 protected void onQueryComplete(Cursor cursor) {
183 // Whenever we get a suggestions cursor, we need to immediately kick off
184 // another query for the complete list of contacts
185 if (cursor != null && mLoadingJoinSuggestions) {
186 mLoadingJoinSuggestions = false;
187 if (cursor.getCount() > 0) {
188 mAdapter.setSuggestionsCursor(cursor);
189 } else {
190 cursor.close();
191 mAdapter.setSuggestionsCursor(null);
192 }
193
Dmitri Plotnikov6e2009d2010-04-22 16:03:53 -0700194 if (mAdapter.getSuggestionsCursorCount() == 0
195 || !mAdapter.isJoinModeShowAllContacts()) {
Dmitri Plotnikove3fbfd92010-04-28 11:20:52 -0700196 startQuery(getContactFilterUri(mListFragment.getQueryString()),
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -0700197 CONTACTS_SUMMARY_PROJECTION,
198 Contacts._ID + " != " + mTargetContactId
199 + " AND " + ContactsContract.Contacts.IN_VISIBLE_GROUP + "=1", null,
200 getSortOrder(CONTACTS_SUMMARY_PROJECTION));
201 return;
202 }
203
204 cursor = getShowAllContactsLabelCursor(CONTACTS_SUMMARY_PROJECTION);
205 }
206
207 super.onQueryComplete(cursor);
208 }
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -0700209}