blob: 059a7eeeb87dba77b4ffe668d283a97af68bc8e4 [file] [log] [blame]
The Android Open Source Project37a16ac2009-03-18 17:39:48 -07001/*
2 * Copyright (C) 2009 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
Jeff Sharkey3f177592009-05-18 15:23:12 -070019import com.android.contacts.NotifyingAsyncQueryHandler.QueryCompleteListener;
Jeff Sharkey3f177592009-05-18 15:23:12 -070020import com.android.providers.contacts2.ContactsContract;
21import com.android.providers.contacts2.ContactsContract.Aggregates;
22
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070023import android.app.Activity;
24import android.app.AlertDialog;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070025import android.content.ComponentName;
26import android.content.ContentUris;
Jeff Sharkey3f177592009-05-18 15:23:12 -070027import android.content.Context;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070028import android.content.DialogInterface;
29import android.content.Intent;
30import android.database.Cursor;
31import android.net.Uri;
32import android.os.Bundle;
Jeff Sharkey3f177592009-05-18 15:23:12 -070033import android.os.IBinder;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070034import android.provider.Contacts;
35import android.provider.Contacts.ContactMethods;
36import android.provider.Contacts.ContactMethodsColumns;
37import android.provider.Contacts.Intents;
38import android.provider.Contacts.People;
39import android.provider.Contacts.Phones;
Jeff Sharkey3f177592009-05-18 15:23:12 -070040import android.view.View;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070041
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070042/**
43 * Handle several edge cases around showing or possibly creating contacts in
44 * connected with a specific E-mail address or phone number. Will search based
45 * on incoming {@link Intent#getData()} as described by
46 * {@link Intents#SHOW_OR_CREATE_CONTACT}.
Jeff Sharkey26c7e732009-04-01 17:30:46 -070047 *
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070048 * <ul>
49 * <li>If no matching contacts found, will prompt user with dialog to add to a
50 * contact, then will use {@link Intent#ACTION_INSERT_OR_EDIT} to let create new
51 * contact or edit new data into an existing one.
52 * <li>If one matching contact found, directly show {@link Intent#ACTION_VIEW}
53 * that specific contact.
54 * <li>If more than one matching found, show list of matching contacts using
55 * {@link Intent#ACTION_SEARCH}.
56 * </ul>
57 */
Jeff Sharkey549aa162009-05-21 01:33:30 -070058public final class ShowOrCreateActivity extends Activity implements QueryCompleteListener, FastTrackWindow.OnDismissListener {
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070059 static final String TAG = "ShowOrCreateActivity";
60 static final boolean LOGD = false;
61
62 static final String[] PHONES_PROJECTION = new String[] {
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070063 Phones.PERSON_ID,
64 };
65
Jeff Sharkey3f177592009-05-18 15:23:12 -070066 static final String[] CONTACTS_PROJECTION = new String[] {
67 ContactsContract.Contacts.AGGREGATE_ID,
68// People._ID,
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070069 };
Jeff Sharkey549aa162009-05-21 01:33:30 -070070
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070071 static final String SCHEME_MAILTO = "mailto";
72 static final String SCHEME_TEL = "tel";
Jeff Sharkey549aa162009-05-21 01:33:30 -070073
Jeff Sharkey3f177592009-05-18 15:23:12 -070074 static final int AGGREGATE_ID_INDEX = 0;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070075
76 /**
77 * Query clause to filter {@link ContactMethods#CONTENT_URI} to only search
78 * {@link Contacts#KIND_EMAIL} or {@link Contacts#KIND_IM}.
79 */
80 static final String QUERY_KIND_EMAIL_OR_IM = ContactMethodsColumns.KIND +
81 " IN (" + Contacts.KIND_EMAIL + "," + Contacts.KIND_IM + ")";
Jeff Sharkey549aa162009-05-21 01:33:30 -070082
Jeff Sharkey3f177592009-05-18 15:23:12 -070083 /**
84 * Extra used to request a specific {@link FastTrackWindow} position.
85 */
Jeff Sharkey80a193a2009-05-21 14:18:18 -070086 private static final String EXTRA_X = "pixel_x";
Jeff Sharkey3f177592009-05-18 15:23:12 -070087 private static final String EXTRA_Y = "pixel_y";
Jeff Sharkey80a193a2009-05-21 14:18:18 -070088 private static final String EXTRA_HEIGHT = "pixel_height";
89 private static final int DEFAULT_X = 0;
Jeff Sharkey3f177592009-05-18 15:23:12 -070090 private static final int DEFAULT_Y = 90;
Jeff Sharkey80a193a2009-05-21 14:18:18 -070091 private static final int DEFAULT_HEIGHT = 30;
Jeff Sharkey3f177592009-05-18 15:23:12 -070092
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070093 static final int QUERY_TOKEN = 42;
Jeff Sharkey549aa162009-05-21 01:33:30 -070094
Jeff Sharkey3f177592009-05-18 15:23:12 -070095 private NotifyingAsyncQueryHandler mQueryHandler;
96
97 private Bundle mCreateExtras;
98 private String mCreateDescrip;
99 private boolean mCreateForce;
100
101 private FastTrackWindow mFastTrack;
Jeff Sharkey549aa162009-05-21 01:33:30 -0700102
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700103 @Override
104 protected void onCreate(Bundle icicle) {
105 super.onCreate(icicle);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700106
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700107 // Create handler if doesn't exist, otherwise cancel any running
108 if (mQueryHandler == null) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700109 mQueryHandler = new NotifyingAsyncQueryHandler(this, this);
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700110 } else {
111 mQueryHandler.cancelOperation(QUERY_TOKEN);
112 }
113
114 final Intent intent = getIntent();
115 final Uri data = intent.getData();
Jeff Sharkey3f177592009-05-18 15:23:12 -0700116
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700117 // Unpack scheme and target data from intent
118 String scheme = null;
119 String ssp = null;
120 if (data != null) {
121 scheme = data.getScheme();
122 ssp = data.getSchemeSpecificPart();
123 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700124
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700125 // Build set of extras for possible use when creating contact
Jeff Sharkey3f177592009-05-18 15:23:12 -0700126 mCreateExtras = new Bundle();
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700127 Bundle originalExtras = intent.getExtras();
128 if (originalExtras != null) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700129 mCreateExtras.putAll(originalExtras);
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700130 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700131
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700132 // Read possible extra with specific title
Jeff Sharkey549aa162009-05-21 01:33:30 -0700133 mCreateDescrip = intent.getStringExtra(Intents.EXTRA_CREATE_DESCRIPTION);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700134 if (mCreateDescrip == null) {
135 mCreateDescrip = ssp;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700136 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700137
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700138 // Allow caller to bypass dialog prompt
Jeff Sharkey3f177592009-05-18 15:23:12 -0700139 mCreateForce = intent.getBooleanExtra(Intents.EXTRA_FORCE_CREATE, false);
140
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700141 // Handle specific query request
142 if (SCHEME_MAILTO.equals(scheme)) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700143 mCreateExtras.putString(Intents.Insert.EMAIL, ssp);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700144
Jeff Sharkey549aa162009-05-21 01:33:30 -0700145 Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_EMAIL_URI,
146 Uri.encode(ssp));
Jeffrey Sharkeyc2158b72009-03-27 17:57:15 -0700147 mQueryHandler.startQuery(QUERY_TOKEN, null, uri,
Jeff Sharkey3f177592009-05-18 15:23:12 -0700148 CONTACTS_PROJECTION, null, null, null);
149
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700150 } else if (SCHEME_TEL.equals(scheme)) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700151 mCreateExtras.putString(Intents.Insert.PHONE, ssp);
152// mQueryHandler.startQuery(QUERY_TOKEN, null,
153// Uri.withAppendedPath(Phones.CONTENT_FILTER_URL, ssp),
154// PHONES_PROJECTION, null, null, null);
155
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700156 } else {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700157 // Otherwise assume incoming aggregate Uri
Jeff Sharkey80a193a2009-05-21 14:18:18 -0700158 showFastTrack(data);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700159
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700160 }
161 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700162
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700163 @Override
164 protected void onStop() {
165 super.onStop();
166 if (mQueryHandler != null) {
167 mQueryHandler.cancelOperation(QUERY_TOKEN);
168 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700169 if (mFastTrack != null) {
170 mFastTrack.dismiss();
171 }
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700172 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700173
174 /**
175 * Show a {@link FastTrackWindow} for the given aggregate at the requested
176 * screen location.
177 */
Jeff Sharkey80a193a2009-05-21 14:18:18 -0700178 private void showFastTrack(Uri aggUri) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700179 // Use our local window token for now
Jeff Sharkey80a193a2009-05-21 14:18:18 -0700180 Bundle extras = getIntent().getExtras();
181 final int x = extras.getInt(EXTRA_X, DEFAULT_X);
182 final int y = extras.getInt(EXTRA_Y, DEFAULT_Y);
183 final int height = extras.getInt(EXTRA_HEIGHT, DEFAULT_HEIGHT);
Jeff Sharkey549aa162009-05-21 01:33:30 -0700184 mFastTrack = new FastTrackWindow(this, this);
Jeff Sharkey80a193a2009-05-21 14:18:18 -0700185 mFastTrack.show(aggUri, x, y, height);
Jeff Sharkey549aa162009-05-21 01:33:30 -0700186 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700187
Jeff Sharkey549aa162009-05-21 01:33:30 -0700188 /** {@inheritDoc} */
189 public void onDismiss(FastTrackWindow dialog) {
190 // When dismissed, finish this activity
191 finish();
Jeff Sharkey3f177592009-05-18 15:23:12 -0700192 }
193
194 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
195 if (cursor == null) {
196 return;
197 }
198
199 // Count contacts found by query
200 int count = 0;
201 long aggId = -1;
202 try {
203 count = cursor.getCount();
204 if (count == 1 && cursor.moveToFirst()) {
205 // Try reading ID if only one contact returned
206 aggId = cursor.getLong(AGGREGATE_ID_INDEX);
207 }
208 } finally {
209 cursor.close();
210 }
211
212 if (count == 1 && aggId != -1) {
213 // If we only found one item, jump right to viewing it
214 final Uri aggUri = ContentUris.withAppendedId(Aggregates.CONTENT_URI, aggId);
Jeff Sharkey80a193a2009-05-21 14:18:18 -0700215 showFastTrack(aggUri);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700216
217// Intent viewIntent = new Intent(Intent.ACTION_VIEW,
218// ContentUris.withAppendedId(People.CONTENT_URI, personId));
219// activity.startActivity(viewIntent);
220// activity.finish();
221
222 } else if (count > 1) {
223 // If more than one, show pick list
224 Intent listIntent = new Intent(Intent.ACTION_SEARCH);
225 listIntent.setComponent(new ComponentName(this, ContactsListActivity.class));
226 listIntent.putExtras(mCreateExtras);
227 startActivity(listIntent);
228 finish();
229
230 } else {
231 // No matching contacts found
232 if (mCreateForce) {
233 // Forced to create new contact
234 Intent createIntent = new Intent(Intent.ACTION_INSERT, People.CONTENT_URI);
235 createIntent.putExtras(mCreateExtras);
236 createIntent.setType(People.CONTENT_TYPE);
237
238 startActivity(createIntent);
239 finish();
240
241 } else {
242 // Prompt user to insert or edit contact
243 Intent createIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
244 createIntent.putExtras(mCreateExtras);
245 createIntent.setType(People.CONTENT_ITEM_TYPE);
246
247 CharSequence message = getResources().getString(
248 R.string.add_contact_dlg_message_fmt, mCreateDescrip);
249
250 new AlertDialog.Builder(this)
251 .setTitle(R.string.add_contact_dlg_title)
252 .setMessage(message)
253 .setPositiveButton(android.R.string.ok,
254 new IntentClickListener(this, createIntent))
255 .setNegativeButton(android.R.string.cancel,
256 new IntentClickListener(this, null))
257 .show();
258 }
259 }
260 }
261
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700262 /**
263 * Listener for {@link DialogInterface} that launches a given {@link Intent}
264 * when clicked. When clicked, this also closes the parent using
265 * {@link Activity#finish()}.
266 */
267 private static class IntentClickListener implements DialogInterface.OnClickListener {
268 private Activity mParent;
269 private Intent mIntent;
270
271 /**
272 * @param parent {@link Activity} to use for launching target.
273 * @param intent Target {@link Intent} to launch when clicked.
274 */
275 public IntentClickListener(Activity parent, Intent intent) {
276 mParent = parent;
277 mIntent = intent;
278 }
279
280 public void onClick(DialogInterface dialog, int which) {
281 if (mIntent != null) {
282 mParent.startActivity(mIntent);
283 }
284 mParent.finish();
285 }
286 }
287
288 /**
Jeff Sharkey3f177592009-05-18 15:23:12 -0700289 * Fake view that simply exists to pass through a specific {@link IBinder}
290 * window token.
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700291 */
Jeff Sharkey3f177592009-05-18 15:23:12 -0700292 private static class FakeView extends View {
293 private IBinder mWindowToken;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700294
Jeff Sharkey3f177592009-05-18 15:23:12 -0700295 public FakeView(Context context, IBinder windowToken) {
296 super(context);
297 mWindowToken = windowToken;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700298 }
299
300 @Override
Jeff Sharkey3f177592009-05-18 15:23:12 -0700301 public IBinder getWindowToken() {
302 return mWindowToken;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700303 }
304 }
305}