blob: e0d95ff0cfee5c4bdf431ed3d2d0193d0ca38c52 [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;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070038
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070039/**
40 * Handle several edge cases around showing or possibly creating contacts in
41 * connected with a specific E-mail address or phone number. Will search based
42 * on incoming {@link Intent#getData()} as described by
Jeff Sharkey39261272009-06-03 19:15:09 -070043 * {@link android.provider.Contacts.Intents#SHOW_OR_CREATE_CONTACT}.
Jeff Sharkey26c7e732009-04-01 17:30:46 -070044 *
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.
49 * <li>If one matching contact found, directly show {@link Intent#ACTION_VIEW}
50 * that specific contact.
51 * <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
165 targetRect = new Rect(15, 110, 15+18, 110+18);
166 }
167
Jeff Sharkey802b2052009-08-04 14:21:06 -0700168 // Use requested display mode, defaulting to medium
169 final int mode = extras.getInt(Intents.EXTRA_MODE, Intents.MODE_MEDIUM);
Jeff Sharkey0b4ad002009-08-23 14:16:27 -0700170 final String[] excludeMimes = extras.getStringArray(Intents.EXTRA_EXCLUDE_MIMES);
Jeff Sharkey802b2052009-08-04 14:21:06 -0700171
Jeff Sharkey549aa162009-05-21 01:33:30 -0700172 mFastTrack = new FastTrackWindow(this, this);
Jeff Sharkey0b4ad002009-08-23 14:16:27 -0700173 mFastTrack.show(aggUri, targetRect, mode, excludeMimes);
Jeff Sharkey549aa162009-05-21 01:33:30 -0700174 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700175
Jeff Sharkey549aa162009-05-21 01:33:30 -0700176 /** {@inheritDoc} */
177 public void onDismiss(FastTrackWindow dialog) {
178 // When dismissed, finish this activity
179 finish();
Jeff Sharkey3f177592009-05-18 15:23:12 -0700180 }
181
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -0700182 /** {@inheritDoc} */
Jeff Sharkey3f177592009-05-18 15:23:12 -0700183 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
184 if (cursor == null) {
Jeff Sharkey24097052009-08-24 15:27:31 -0700185 // Bail when problem running query in background
186 finish();
Jeff Sharkey3f177592009-05-18 15:23:12 -0700187 return;
188 }
189
190 // Count contacts found by query
191 int count = 0;
192 long aggId = -1;
193 try {
194 count = cursor.getCount();
195 if (count == 1 && cursor.moveToFirst()) {
196 // Try reading ID if only one contact returned
197 aggId = cursor.getLong(AGGREGATE_ID_INDEX);
198 }
199 } finally {
200 cursor.close();
201 }
202
203 if (count == 1 && aggId != -1) {
Jeff Sharkey39261272009-06-03 19:15:09 -0700204 // If we only found one item, show fast-track
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700205 final Uri aggUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, aggId);
Jeff Sharkey80a193a2009-05-21 14:18:18 -0700206 showFastTrack(aggUri);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700207
Jeff Sharkey3f177592009-05-18 15:23:12 -0700208 } else if (count > 1) {
209 // If more than one, show pick list
210 Intent listIntent = new Intent(Intent.ACTION_SEARCH);
211 listIntent.setComponent(new ComponentName(this, ContactsListActivity.class));
212 listIntent.putExtras(mCreateExtras);
213 startActivity(listIntent);
214 finish();
215
216 } else {
217 // No matching contacts found
218 if (mCreateForce) {
219 // Forced to create new contact
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700220 Intent createIntent = new Intent(Intent.ACTION_INSERT, RawContacts.CONTENT_URI);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700221 createIntent.putExtras(mCreateExtras);
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700222 createIntent.setType(RawContacts.CONTENT_TYPE);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700223
224 startActivity(createIntent);
225 finish();
226
227 } else {
228 // Prompt user to insert or edit contact
229 Intent createIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
230 createIntent.putExtras(mCreateExtras);
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700231 createIntent.setType(RawContacts.CONTENT_ITEM_TYPE);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700232
233 CharSequence message = getResources().getString(
234 R.string.add_contact_dlg_message_fmt, mCreateDescrip);
235
236 new AlertDialog.Builder(this)
237 .setTitle(R.string.add_contact_dlg_title)
238 .setMessage(message)
239 .setPositiveButton(android.R.string.ok,
240 new IntentClickListener(this, createIntent))
241 .setNegativeButton(android.R.string.cancel,
242 new IntentClickListener(this, null))
243 .show();
244 }
245 }
246 }
247
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -0700248 /** {@inheritDoc} */
249 public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) {
250 // No actions
251 }
252
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700253 /**
254 * Listener for {@link DialogInterface} that launches a given {@link Intent}
255 * when clicked. When clicked, this also closes the parent using
256 * {@link Activity#finish()}.
257 */
258 private static class IntentClickListener implements DialogInterface.OnClickListener {
259 private Activity mParent;
260 private Intent mIntent;
261
262 /**
263 * @param parent {@link Activity} to use for launching target.
264 * @param intent Target {@link Intent} to launch when clicked.
265 */
266 public IntentClickListener(Activity parent, Intent intent) {
267 mParent = parent;
268 mIntent = intent;
269 }
270
271 public void onClick(DialogInterface dialog, int which) {
272 if (mIntent != null) {
273 mParent.startActivity(mIntent);
274 }
275 mParent.finish();
276 }
277 }
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700278}