blob: c11240ee7e7f980e3a51f0e59aa39172a9bc2586 [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 Sharkey3f0b7b82009-08-12 11:28:53 -070020import com.android.contacts.util.NotifyingAsyncQueryHandler;
Jeff Sharkey3f177592009-05-18 15:23:12 -070021
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070022import android.app.Activity;
23import android.app.AlertDialog;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070024import android.content.ComponentName;
25import android.content.ContentUris;
26import android.content.DialogInterface;
Evan Millar5f4af702009-08-11 11:12:00 -070027import android.content.EntityIterator;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070028import android.content.Intent;
29import android.database.Cursor;
Jeff Sharkey39261272009-06-03 19:15:09 -070030import android.graphics.Rect;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070031import android.net.Uri;
32import android.os.Bundle;
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -070033import android.provider.ContactsContract.Contacts;
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -070034import android.provider.ContactsContract.Intents;
Jeff Sharkey39261272009-06-03 19:15:09 -070035import android.provider.ContactsContract.PhoneLookup;
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -070036import android.provider.ContactsContract.RawContacts;
Jeff Sharkey24097052009-08-24 15:27:31 -070037import android.provider.ContactsContract.CommonDataKinds.Email;
Jeff Sharkey73714ff2009-08-23 22:13:04 -070038import android.util.Log;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070039
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070040/**
41 * Handle several edge cases around showing or possibly creating contacts in
42 * connected with a specific E-mail address or phone number. Will search based
43 * on incoming {@link Intent#getData()} as described by
Jeff Sharkey73714ff2009-08-23 22:13:04 -070044 * {@link Intents#SHOW_OR_CREATE_CONTACT}.
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070045 * <ul>
46 * <li>If no matching contacts found, will prompt user with dialog to add to a
47 * contact, then will use {@link Intent#ACTION_INSERT_OR_EDIT} to let create new
48 * contact or edit new data into an existing one.
Jeff Sharkey73714ff2009-08-23 22:13:04 -070049 * <li>If one matching contact found, show the {@link FastTrackWindow}
50 * associated with the found contact. Will show translucent over the caller.
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070051 * <li>If more than one matching found, show list of matching contacts using
52 * {@link Intent#ACTION_SEARCH}.
53 * </ul>
54 */
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -070055public final class ShowOrCreateActivity extends Activity implements
56 NotifyingAsyncQueryHandler.AsyncQueryListener, FastTrackWindow.OnDismissListener {
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070057 static final String TAG = "ShowOrCreateActivity";
58 static final boolean LOGD = false;
59
60 static final String[] PHONES_PROJECTION = new String[] {
Jeff Sharkey39261272009-06-03 19:15:09 -070061 PhoneLookup._ID,
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070062 };
63
Jeff Sharkey3f177592009-05-18 15:23:12 -070064 static final String[] CONTACTS_PROJECTION = new String[] {
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -070065 RawContacts.CONTACT_ID,
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070066 };
Jeff Sharkey549aa162009-05-21 01:33:30 -070067
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070068 static final String SCHEME_MAILTO = "mailto";
69 static final String SCHEME_TEL = "tel";
Jeff Sharkey549aa162009-05-21 01:33:30 -070070
Jeff Sharkey3f177592009-05-18 15:23:12 -070071 static final int AGGREGATE_ID_INDEX = 0;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070072
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070073 static final int QUERY_TOKEN = 42;
Jeff Sharkey549aa162009-05-21 01:33:30 -070074
Jeff Sharkey3f177592009-05-18 15:23:12 -070075 private NotifyingAsyncQueryHandler mQueryHandler;
76
77 private Bundle mCreateExtras;
78 private String mCreateDescrip;
79 private boolean mCreateForce;
80
81 private FastTrackWindow mFastTrack;
Jeff Sharkey549aa162009-05-21 01:33:30 -070082
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070083 @Override
84 protected void onCreate(Bundle icicle) {
85 super.onCreate(icicle);
Jeff Sharkey3f177592009-05-18 15:23:12 -070086
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070087 // Create handler if doesn't exist, otherwise cancel any running
88 if (mQueryHandler == null) {
Jeff Sharkey3f177592009-05-18 15:23:12 -070089 mQueryHandler = new NotifyingAsyncQueryHandler(this, this);
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070090 } else {
91 mQueryHandler.cancelOperation(QUERY_TOKEN);
92 }
93
94 final Intent intent = getIntent();
95 final Uri data = intent.getData();
Jeff Sharkey3f177592009-05-18 15:23:12 -070096
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070097 // Unpack scheme and target data from intent
98 String scheme = null;
99 String ssp = null;
100 if (data != null) {
101 scheme = data.getScheme();
102 ssp = data.getSchemeSpecificPart();
103 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700104
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700105 // Build set of extras for possible use when creating contact
Jeff Sharkey3f177592009-05-18 15:23:12 -0700106 mCreateExtras = new Bundle();
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700107 Bundle originalExtras = intent.getExtras();
108 if (originalExtras != null) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700109 mCreateExtras.putAll(originalExtras);
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700110 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700111
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700112 // Read possible extra with specific title
Jeff Sharkey549aa162009-05-21 01:33:30 -0700113 mCreateDescrip = intent.getStringExtra(Intents.EXTRA_CREATE_DESCRIPTION);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700114 if (mCreateDescrip == null) {
115 mCreateDescrip = ssp;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700116 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700117
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700118 // Allow caller to bypass dialog prompt
Jeff Sharkey3f177592009-05-18 15:23:12 -0700119 mCreateForce = intent.getBooleanExtra(Intents.EXTRA_FORCE_CREATE, false);
120
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700121 // Handle specific query request
122 if (SCHEME_MAILTO.equals(scheme)) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700123 mCreateExtras.putString(Intents.Insert.EMAIL, ssp);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700124
Jeff Sharkey24097052009-08-24 15:27:31 -0700125 Uri uri = Uri.withAppendedPath(Email.CONTENT_FILTER_EMAIL_URI, Uri.encode(ssp));
Jeff Sharkey39261272009-06-03 19:15:09 -0700126 mQueryHandler.startQuery(QUERY_TOKEN, null, uri, CONTACTS_PROJECTION, null, null, null);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700127
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700128 } else if (SCHEME_TEL.equals(scheme)) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700129 mCreateExtras.putString(Intents.Insert.PHONE, ssp);
Jeff Sharkey39261272009-06-03 19:15:09 -0700130
131 Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, ssp);
132 mQueryHandler.startQuery(QUERY_TOKEN, null, uri, PHONES_PROJECTION, null, null, null);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700133
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700134 } else {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700135 // Otherwise assume incoming aggregate Uri
Jeff Sharkey80a193a2009-05-21 14:18:18 -0700136 showFastTrack(data);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700137
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700138 }
139 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700140
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700141 @Override
142 protected void onStop() {
143 super.onStop();
144 if (mQueryHandler != null) {
145 mQueryHandler.cancelOperation(QUERY_TOKEN);
146 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700147 if (mFastTrack != null) {
148 mFastTrack.dismiss();
149 }
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700150 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700151
152 /**
153 * Show a {@link FastTrackWindow} for the given aggregate at the requested
154 * screen location.
155 */
Jeff Sharkey80a193a2009-05-21 14:18:18 -0700156 private void showFastTrack(Uri aggUri) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700157 // Use our local window token for now
Jeff Sharkey802b2052009-08-04 14:21:06 -0700158 final Bundle extras = getIntent().getExtras();
Jeff Sharkey39261272009-06-03 19:15:09 -0700159
160 Rect targetRect;
Jeff Sharkey802b2052009-08-04 14:21:06 -0700161 if (extras.containsKey(Intents.EXTRA_TARGET_RECT)) {
162 targetRect = (Rect)extras.getParcelable(Intents.EXTRA_TARGET_RECT);
Jeff Sharkey39261272009-06-03 19:15:09 -0700163 } else {
164 // TODO: this default rect matches gmail messages, and should move over there
Jeff Sharkey73714ff2009-08-23 22:13:04 -0700165 Log.w(TAG, "Using default TARGET_RECT");
Jeff Sharkey39261272009-06-03 19:15:09 -0700166 targetRect = new Rect(15, 110, 15+18, 110+18);
167 }
168
Jeff Sharkey802b2052009-08-04 14:21:06 -0700169 // Use requested display mode, defaulting to medium
170 final int mode = extras.getInt(Intents.EXTRA_MODE, Intents.MODE_MEDIUM);
Jeff Sharkey0b4ad002009-08-23 14:16:27 -0700171 final String[] excludeMimes = extras.getStringArray(Intents.EXTRA_EXCLUDE_MIMES);
Jeff Sharkey802b2052009-08-04 14:21:06 -0700172
Jeff Sharkey549aa162009-05-21 01:33:30 -0700173 mFastTrack = new FastTrackWindow(this, this);
Jeff Sharkey0b4ad002009-08-23 14:16:27 -0700174 mFastTrack.show(aggUri, targetRect, mode, excludeMimes);
Jeff Sharkey549aa162009-05-21 01:33:30 -0700175 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700176
Jeff Sharkey549aa162009-05-21 01:33:30 -0700177 /** {@inheritDoc} */
178 public void onDismiss(FastTrackWindow dialog) {
179 // When dismissed, finish this activity
180 finish();
Jeff Sharkey3f177592009-05-18 15:23:12 -0700181 }
182
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -0700183 /** {@inheritDoc} */
Jeff Sharkey3f177592009-05-18 15:23:12 -0700184 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
185 if (cursor == null) {
Jeff Sharkey24097052009-08-24 15:27:31 -0700186 // Bail when problem running query in background
187 finish();
Jeff Sharkey3f177592009-05-18 15:23:12 -0700188 return;
189 }
190
191 // Count contacts found by query
192 int count = 0;
193 long aggId = -1;
194 try {
195 count = cursor.getCount();
196 if (count == 1 && cursor.moveToFirst()) {
197 // Try reading ID if only one contact returned
198 aggId = cursor.getLong(AGGREGATE_ID_INDEX);
199 }
200 } finally {
201 cursor.close();
202 }
203
204 if (count == 1 && aggId != -1) {
Jeff Sharkey39261272009-06-03 19:15:09 -0700205 // If we only found one item, show fast-track
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700206 final Uri aggUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, aggId);
Jeff Sharkey80a193a2009-05-21 14:18:18 -0700207 showFastTrack(aggUri);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700208
Jeff Sharkey3f177592009-05-18 15:23:12 -0700209 } else if (count > 1) {
210 // If more than one, show pick list
211 Intent listIntent = new Intent(Intent.ACTION_SEARCH);
212 listIntent.setComponent(new ComponentName(this, ContactsListActivity.class));
213 listIntent.putExtras(mCreateExtras);
214 startActivity(listIntent);
215 finish();
216
217 } else {
218 // No matching contacts found
219 if (mCreateForce) {
220 // Forced to create new contact
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700221 Intent createIntent = new Intent(Intent.ACTION_INSERT, RawContacts.CONTENT_URI);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700222 createIntent.putExtras(mCreateExtras);
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700223 createIntent.setType(RawContacts.CONTENT_TYPE);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700224
225 startActivity(createIntent);
226 finish();
227
228 } else {
229 // Prompt user to insert or edit contact
230 Intent createIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
231 createIntent.putExtras(mCreateExtras);
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700232 createIntent.setType(RawContacts.CONTENT_ITEM_TYPE);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700233
234 CharSequence message = getResources().getString(
235 R.string.add_contact_dlg_message_fmt, mCreateDescrip);
236
237 new AlertDialog.Builder(this)
238 .setTitle(R.string.add_contact_dlg_title)
239 .setMessage(message)
240 .setPositiveButton(android.R.string.ok,
241 new IntentClickListener(this, createIntent))
242 .setNegativeButton(android.R.string.cancel,
243 new IntentClickListener(this, null))
244 .show();
245 }
246 }
247 }
248
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -0700249 /** {@inheritDoc} */
250 public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) {
251 // No actions
252 }
253
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700254 /**
255 * Listener for {@link DialogInterface} that launches a given {@link Intent}
256 * when clicked. When clicked, this also closes the parent using
257 * {@link Activity#finish()}.
258 */
259 private static class IntentClickListener implements DialogInterface.OnClickListener {
260 private Activity mParent;
261 private Intent mIntent;
262
263 /**
264 * @param parent {@link Activity} to use for launching target.
265 * @param intent Target {@link Intent} to launch when clicked.
266 */
267 public IntentClickListener(Activity parent, Intent intent) {
268 mParent = parent;
269 mIntent = intent;
270 }
271
272 public void onClick(DialogInterface dialog, int which) {
273 if (mIntent != null) {
274 mParent.startActivity(mIntent);
275 }
276 mParent.finish();
277 }
278 }
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700279}