blob: ed8568a9e84a21d2b580c768029c2bbe69ef222c [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 Sharkey802b2052009-08-04 14:21:06 -070019import com.android.contacts.ui.FastTrackWindow;
Jeff Sharkey49d17b32009-09-07 02:14:21 -070020import com.android.contacts.util.Constants;
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -070021import com.android.contacts.util.NotifyingAsyncQueryHandler;
Jeff Sharkey3f177592009-05-18 15:23:12 -070022
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;
27import android.content.DialogInterface;
Evan Millar5f4af702009-08-11 11:12:00 -070028import android.content.EntityIterator;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070029import android.content.Intent;
30import android.database.Cursor;
Jeff Sharkey39261272009-06-03 19:15:09 -070031import android.graphics.Rect;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070032import android.net.Uri;
33import android.os.Bundle;
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -070034import android.provider.ContactsContract.Contacts;
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -070035import android.provider.ContactsContract.Intents;
Jeff Sharkey39261272009-06-03 19:15:09 -070036import android.provider.ContactsContract.PhoneLookup;
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -070037import android.provider.ContactsContract.RawContacts;
Jeff Sharkey24097052009-08-24 15:27:31 -070038import android.provider.ContactsContract.CommonDataKinds.Email;
Jeff Sharkey73714ff2009-08-23 22:13:04 -070039import android.util.Log;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070040
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070041/**
42 * Handle several edge cases around showing or possibly creating contacts in
43 * connected with a specific E-mail address or phone number. Will search based
44 * on incoming {@link Intent#getData()} as described by
Jeff Sharkey73714ff2009-08-23 22:13:04 -070045 * {@link Intents#SHOW_OR_CREATE_CONTACT}.
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070046 * <ul>
47 * <li>If no matching contacts found, will prompt user with dialog to add to a
48 * contact, then will use {@link Intent#ACTION_INSERT_OR_EDIT} to let create new
49 * contact or edit new data into an existing one.
Jeff Sharkey73714ff2009-08-23 22:13:04 -070050 * <li>If one matching contact found, show the {@link FastTrackWindow}
51 * associated with the found contact. Will show translucent over the caller.
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070052 * <li>If more than one matching found, show list of matching contacts using
53 * {@link Intent#ACTION_SEARCH}.
54 * </ul>
55 */
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -070056public final class ShowOrCreateActivity extends Activity implements
57 NotifyingAsyncQueryHandler.AsyncQueryListener, FastTrackWindow.OnDismissListener {
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070058 static final String TAG = "ShowOrCreateActivity";
59 static final boolean LOGD = false;
60
61 static final String[] PHONES_PROJECTION = new String[] {
Jeff Sharkey39261272009-06-03 19:15:09 -070062 PhoneLookup._ID,
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070063 };
64
Jeff Sharkey3f177592009-05-18 15:23:12 -070065 static final String[] CONTACTS_PROJECTION = new String[] {
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -070066 RawContacts.CONTACT_ID,
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070067 };
Jeff Sharkey549aa162009-05-21 01:33:30 -070068
Jeff Sharkey3f177592009-05-18 15:23:12 -070069 static final int AGGREGATE_ID_INDEX = 0;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070070
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070071 static final int QUERY_TOKEN = 42;
Jeff Sharkey549aa162009-05-21 01:33:30 -070072
Jeff Sharkey3f177592009-05-18 15:23:12 -070073 private NotifyingAsyncQueryHandler mQueryHandler;
74
75 private Bundle mCreateExtras;
76 private String mCreateDescrip;
77 private boolean mCreateForce;
78
79 private FastTrackWindow mFastTrack;
Jeff Sharkey549aa162009-05-21 01:33:30 -070080
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070081 @Override
82 protected void onCreate(Bundle icicle) {
83 super.onCreate(icicle);
Jeff Sharkey3f177592009-05-18 15:23:12 -070084
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070085 // Create handler if doesn't exist, otherwise cancel any running
86 if (mQueryHandler == null) {
Jeff Sharkey3f177592009-05-18 15:23:12 -070087 mQueryHandler = new NotifyingAsyncQueryHandler(this, this);
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070088 } else {
89 mQueryHandler.cancelOperation(QUERY_TOKEN);
90 }
91
92 final Intent intent = getIntent();
93 final Uri data = intent.getData();
Jeff Sharkey3f177592009-05-18 15:23:12 -070094
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070095 // Unpack scheme and target data from intent
96 String scheme = null;
97 String ssp = null;
98 if (data != null) {
99 scheme = data.getScheme();
100 ssp = data.getSchemeSpecificPart();
101 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700102
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700103 // Build set of extras for possible use when creating contact
Jeff Sharkey3f177592009-05-18 15:23:12 -0700104 mCreateExtras = new Bundle();
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700105 Bundle originalExtras = intent.getExtras();
106 if (originalExtras != null) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700107 mCreateExtras.putAll(originalExtras);
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700108 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700109
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700110 // Read possible extra with specific title
Jeff Sharkey549aa162009-05-21 01:33:30 -0700111 mCreateDescrip = intent.getStringExtra(Intents.EXTRA_CREATE_DESCRIPTION);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700112 if (mCreateDescrip == null) {
113 mCreateDescrip = ssp;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700114 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700115
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700116 // Allow caller to bypass dialog prompt
Jeff Sharkey3f177592009-05-18 15:23:12 -0700117 mCreateForce = intent.getBooleanExtra(Intents.EXTRA_FORCE_CREATE, false);
118
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700119 // Handle specific query request
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700120 if (Constants.SCHEME_MAILTO.equals(scheme)) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700121 mCreateExtras.putString(Intents.Insert.EMAIL, ssp);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700122
Jeff Sharkey24097052009-08-24 15:27:31 -0700123 Uri uri = Uri.withAppendedPath(Email.CONTENT_FILTER_EMAIL_URI, Uri.encode(ssp));
Jeff Sharkey39261272009-06-03 19:15:09 -0700124 mQueryHandler.startQuery(QUERY_TOKEN, null, uri, CONTACTS_PROJECTION, null, null, null);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700125
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700126 } else if (Constants.SCHEME_TEL.equals(scheme)) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700127 mCreateExtras.putString(Intents.Insert.PHONE, ssp);
Jeff Sharkey39261272009-06-03 19:15:09 -0700128
129 Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, ssp);
130 mQueryHandler.startQuery(QUERY_TOKEN, null, uri, PHONES_PROJECTION, null, null, null);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700131
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700132 } else {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700133 // Otherwise assume incoming aggregate Uri
Jeff Sharkey80a193a2009-05-21 14:18:18 -0700134 showFastTrack(data);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700135
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700136 }
137 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700138
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700139 @Override
140 protected void onStop() {
141 super.onStop();
142 if (mQueryHandler != null) {
143 mQueryHandler.cancelOperation(QUERY_TOKEN);
144 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700145 if (mFastTrack != null) {
146 mFastTrack.dismiss();
147 }
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700148 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700149
150 /**
151 * Show a {@link FastTrackWindow} for the given aggregate at the requested
152 * screen location.
153 */
Jeff Sharkey80a193a2009-05-21 14:18:18 -0700154 private void showFastTrack(Uri aggUri) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700155 // Use our local window token for now
Jeff Sharkey802b2052009-08-04 14:21:06 -0700156 final Bundle extras = getIntent().getExtras();
Jeff Sharkey39261272009-06-03 19:15:09 -0700157
158 Rect targetRect;
Jeff Sharkey802b2052009-08-04 14:21:06 -0700159 if (extras.containsKey(Intents.EXTRA_TARGET_RECT)) {
160 targetRect = (Rect)extras.getParcelable(Intents.EXTRA_TARGET_RECT);
Jeff Sharkey39261272009-06-03 19:15:09 -0700161 } else {
162 // TODO: this default rect matches gmail messages, and should move over there
Jeff Sharkey73714ff2009-08-23 22:13:04 -0700163 Log.w(TAG, "Using default TARGET_RECT");
Jeff Sharkey39261272009-06-03 19:15:09 -0700164 targetRect = new Rect(15, 110, 15+18, 110+18);
165 }
166
Jeff Sharkey802b2052009-08-04 14:21:06 -0700167 // Use requested display mode, defaulting to medium
168 final int mode = extras.getInt(Intents.EXTRA_MODE, Intents.MODE_MEDIUM);
Jeff Sharkey0b4ad002009-08-23 14:16:27 -0700169 final String[] excludeMimes = extras.getStringArray(Intents.EXTRA_EXCLUDE_MIMES);
Jeff Sharkey802b2052009-08-04 14:21:06 -0700170
Jeff Sharkey549aa162009-05-21 01:33:30 -0700171 mFastTrack = new FastTrackWindow(this, this);
Jeff Sharkey0b4ad002009-08-23 14:16:27 -0700172 mFastTrack.show(aggUri, targetRect, mode, excludeMimes);
Jeff Sharkey549aa162009-05-21 01:33:30 -0700173 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700174
Jeff Sharkey549aa162009-05-21 01:33:30 -0700175 /** {@inheritDoc} */
176 public void onDismiss(FastTrackWindow dialog) {
177 // When dismissed, finish this activity
178 finish();
Jeff Sharkey3f177592009-05-18 15:23:12 -0700179 }
180
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -0700181 /** {@inheritDoc} */
Jeff Sharkey3f177592009-05-18 15:23:12 -0700182 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
183 if (cursor == null) {
Jeff Sharkey24097052009-08-24 15:27:31 -0700184 // Bail when problem running query in background
185 finish();
Jeff Sharkey3f177592009-05-18 15:23:12 -0700186 return;
187 }
188
189 // Count contacts found by query
190 int count = 0;
191 long aggId = -1;
192 try {
193 count = cursor.getCount();
194 if (count == 1 && cursor.moveToFirst()) {
195 // Try reading ID if only one contact returned
196 aggId = cursor.getLong(AGGREGATE_ID_INDEX);
197 }
198 } finally {
199 cursor.close();
200 }
201
202 if (count == 1 && aggId != -1) {
Jeff Sharkey39261272009-06-03 19:15:09 -0700203 // If we only found one item, show fast-track
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700204 final Uri aggUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, aggId);
Jeff Sharkey80a193a2009-05-21 14:18:18 -0700205 showFastTrack(aggUri);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700206
Jeff Sharkey3f177592009-05-18 15:23:12 -0700207 } else if (count > 1) {
208 // If more than one, show pick list
209 Intent listIntent = new Intent(Intent.ACTION_SEARCH);
210 listIntent.setComponent(new ComponentName(this, ContactsListActivity.class));
211 listIntent.putExtras(mCreateExtras);
212 startActivity(listIntent);
213 finish();
214
215 } else {
216 // No matching contacts found
217 if (mCreateForce) {
218 // Forced to create new contact
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700219 Intent createIntent = new Intent(Intent.ACTION_INSERT, RawContacts.CONTENT_URI);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700220 createIntent.putExtras(mCreateExtras);
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700221 createIntent.setType(RawContacts.CONTENT_TYPE);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700222
223 startActivity(createIntent);
224 finish();
225
226 } else {
227 // Prompt user to insert or edit contact
228 Intent createIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
229 createIntent.putExtras(mCreateExtras);
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700230 createIntent.setType(RawContacts.CONTENT_ITEM_TYPE);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700231
232 CharSequence message = getResources().getString(
233 R.string.add_contact_dlg_message_fmt, mCreateDescrip);
234
235 new AlertDialog.Builder(this)
236 .setTitle(R.string.add_contact_dlg_title)
237 .setMessage(message)
238 .setPositiveButton(android.R.string.ok,
239 new IntentClickListener(this, createIntent))
240 .setNegativeButton(android.R.string.cancel,
241 new IntentClickListener(this, null))
242 .show();
243 }
244 }
245 }
246
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -0700247 /** {@inheritDoc} */
248 public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) {
249 // No actions
250 }
251
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700252 /**
253 * Listener for {@link DialogInterface} that launches a given {@link Intent}
254 * when clicked. When clicked, this also closes the parent using
255 * {@link Activity#finish()}.
256 */
257 private static class IntentClickListener implements DialogInterface.OnClickListener {
258 private Activity mParent;
259 private Intent mIntent;
260
261 /**
262 * @param parent {@link Activity} to use for launching target.
263 * @param intent Target {@link Intent} to launch when clicked.
264 */
265 public IntentClickListener(Activity parent, Intent intent) {
266 mParent = parent;
267 mIntent = intent;
268 }
269
270 public void onClick(DialogInterface dialog, int which) {
271 if (mIntent != null) {
272 mParent.startActivity(mIntent);
273 }
274 mParent.finish();
275 }
276 }
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700277}