blob: 6ba2315d7c3fc7d4900024fe1eed910b31290a0e [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
Evan Millar5f4af702009-08-11 11:12:00 -070019import com.android.contacts.NotifyingAsyncQueryHandler.AsyncQueryListener;
Jeff Sharkey802b2052009-08-04 14:21:06 -070020import com.android.contacts.ui.FastTrackWindow;
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;
Jeff Sharkey3f177592009-05-18 15:23:12 -070026import android.content.Context;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070027import 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;
Jeff Sharkey3f177592009-05-18 15:23:12 -070034import android.os.IBinder;
Jeff Sharkey802b2052009-08-04 14:21:06 -070035import android.provider.ContactsContract.Intents;
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -070036import android.provider.ContactsContract.Contacts;
Dmitri Plotnikov39466592009-07-27 11:23:51 -070037import android.provider.ContactsContract.RawContacts;
Jeff Sharkey39261272009-06-03 19:15:09 -070038import android.provider.ContactsContract.PhoneLookup;
Jeff Sharkey3f177592009-05-18 15:23:12 -070039import android.view.View;
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 Sharkey39261272009-06-03 19:15:09 -070045 * {@link android.provider.Contacts.Intents#SHOW_OR_CREATE_CONTACT}.
Jeff Sharkey26c7e732009-04-01 17:30:46 -070046 *
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070047 * <ul>
48 * <li>If no matching contacts found, will prompt user with dialog to add to a
49 * contact, then will use {@link Intent#ACTION_INSERT_OR_EDIT} to let create new
50 * contact or edit new data into an existing one.
51 * <li>If one matching contact found, directly show {@link Intent#ACTION_VIEW}
52 * that specific contact.
53 * <li>If more than one matching found, show list of matching contacts using
54 * {@link Intent#ACTION_SEARCH}.
55 * </ul>
56 */
Evan Millar5f4af702009-08-11 11:12:00 -070057public final class ShowOrCreateActivity extends Activity implements AsyncQueryListener,
Jeff Sharkey39261272009-06-03 19:15:09 -070058 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[] {
Jeff Sharkey39261272009-06-03 19:15:09 -070063 PhoneLookup._ID,
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070064 };
65
Jeff Sharkey3f177592009-05-18 15:23:12 -070066 static final String[] CONTACTS_PROJECTION = new String[] {
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -070067 RawContacts.CONTACT_ID,
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070068 };
Jeff Sharkey549aa162009-05-21 01:33:30 -070069
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070070 static final String SCHEME_MAILTO = "mailto";
71 static final String SCHEME_TEL = "tel";
Jeff Sharkey549aa162009-05-21 01:33:30 -070072
Jeff Sharkey3f177592009-05-18 15:23:12 -070073 static final int AGGREGATE_ID_INDEX = 0;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070074
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070075 static final int QUERY_TOKEN = 42;
Jeff Sharkey549aa162009-05-21 01:33:30 -070076
Jeff Sharkey3f177592009-05-18 15:23:12 -070077 private NotifyingAsyncQueryHandler mQueryHandler;
78
79 private Bundle mCreateExtras;
80 private String mCreateDescrip;
81 private boolean mCreateForce;
82
83 private FastTrackWindow mFastTrack;
Jeff Sharkey549aa162009-05-21 01:33:30 -070084
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070085 @Override
86 protected void onCreate(Bundle icicle) {
87 super.onCreate(icicle);
Jeff Sharkey3f177592009-05-18 15:23:12 -070088
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070089 // Create handler if doesn't exist, otherwise cancel any running
90 if (mQueryHandler == null) {
Jeff Sharkey3f177592009-05-18 15:23:12 -070091 mQueryHandler = new NotifyingAsyncQueryHandler(this, this);
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070092 } else {
93 mQueryHandler.cancelOperation(QUERY_TOKEN);
94 }
95
96 final Intent intent = getIntent();
97 final Uri data = intent.getData();
Jeff Sharkey3f177592009-05-18 15:23:12 -070098
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070099 // Unpack scheme and target data from intent
100 String scheme = null;
101 String ssp = null;
102 if (data != null) {
103 scheme = data.getScheme();
104 ssp = data.getSchemeSpecificPart();
105 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700106
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700107 // Build set of extras for possible use when creating contact
Jeff Sharkey3f177592009-05-18 15:23:12 -0700108 mCreateExtras = new Bundle();
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700109 Bundle originalExtras = intent.getExtras();
110 if (originalExtras != null) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700111 mCreateExtras.putAll(originalExtras);
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700112 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700113
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700114 // Read possible extra with specific title
Jeff Sharkey549aa162009-05-21 01:33:30 -0700115 mCreateDescrip = intent.getStringExtra(Intents.EXTRA_CREATE_DESCRIPTION);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700116 if (mCreateDescrip == null) {
117 mCreateDescrip = ssp;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700118 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700119
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700120 // Allow caller to bypass dialog prompt
Jeff Sharkey3f177592009-05-18 15:23:12 -0700121 mCreateForce = intent.getBooleanExtra(Intents.EXTRA_FORCE_CREATE, false);
122
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700123 // Handle specific query request
124 if (SCHEME_MAILTO.equals(scheme)) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700125 mCreateExtras.putString(Intents.Insert.EMAIL, ssp);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700126
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700127 Uri uri = Uri.withAppendedPath(RawContacts.CONTENT_FILTER_EMAIL_URI, Uri.encode(ssp));
Jeff Sharkey39261272009-06-03 19:15:09 -0700128 mQueryHandler.startQuery(QUERY_TOKEN, null, uri, CONTACTS_PROJECTION, null, null, null);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700129
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700130 } else if (SCHEME_TEL.equals(scheme)) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700131 mCreateExtras.putString(Intents.Insert.PHONE, ssp);
Jeff Sharkey39261272009-06-03 19:15:09 -0700132
133 Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, ssp);
134 mQueryHandler.startQuery(QUERY_TOKEN, null, uri, PHONES_PROJECTION, null, null, null);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700135
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700136 } else {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700137 // Otherwise assume incoming aggregate Uri
Jeff Sharkey80a193a2009-05-21 14:18:18 -0700138 showFastTrack(data);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700139
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700140 }
141 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700142
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700143 @Override
144 protected void onStop() {
145 super.onStop();
146 if (mQueryHandler != null) {
147 mQueryHandler.cancelOperation(QUERY_TOKEN);
148 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700149 if (mFastTrack != null) {
150 mFastTrack.dismiss();
151 }
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700152 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700153
154 /**
155 * Show a {@link FastTrackWindow} for the given aggregate at the requested
156 * screen location.
157 */
Jeff Sharkey80a193a2009-05-21 14:18:18 -0700158 private void showFastTrack(Uri aggUri) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700159 // Use our local window token for now
Jeff Sharkey802b2052009-08-04 14:21:06 -0700160 final Bundle extras = getIntent().getExtras();
Jeff Sharkey39261272009-06-03 19:15:09 -0700161
162 Rect targetRect;
Jeff Sharkey802b2052009-08-04 14:21:06 -0700163 if (extras.containsKey(Intents.EXTRA_TARGET_RECT)) {
164 targetRect = (Rect)extras.getParcelable(Intents.EXTRA_TARGET_RECT);
Jeff Sharkey39261272009-06-03 19:15:09 -0700165 } else {
166 // TODO: this default rect matches gmail messages, and should move over there
167 targetRect = new Rect(15, 110, 15+18, 110+18);
168 }
169
Jeff Sharkey802b2052009-08-04 14:21:06 -0700170 // Use requested display mode, defaulting to medium
171 final int mode = extras.getInt(Intents.EXTRA_MODE, Intents.MODE_MEDIUM);
172
Jeff Sharkey549aa162009-05-21 01:33:30 -0700173 mFastTrack = new FastTrackWindow(this, this);
Jeff Sharkey802b2052009-08-04 14:21:06 -0700174 mFastTrack.show(aggUri, targetRect, mode);
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
183 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
184 if (cursor == null) {
185 return;
186 }
187
188 // Count contacts found by query
189 int count = 0;
190 long aggId = -1;
191 try {
192 count = cursor.getCount();
193 if (count == 1 && cursor.moveToFirst()) {
194 // Try reading ID if only one contact returned
195 aggId = cursor.getLong(AGGREGATE_ID_INDEX);
196 }
197 } finally {
198 cursor.close();
199 }
200
201 if (count == 1 && aggId != -1) {
Jeff Sharkey39261272009-06-03 19:15:09 -0700202 // If we only found one item, show fast-track
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700203 final Uri aggUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, aggId);
Jeff Sharkey80a193a2009-05-21 14:18:18 -0700204 showFastTrack(aggUri);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700205
Jeff Sharkey3f177592009-05-18 15:23:12 -0700206 } else if (count > 1) {
207 // If more than one, show pick list
208 Intent listIntent = new Intent(Intent.ACTION_SEARCH);
209 listIntent.setComponent(new ComponentName(this, ContactsListActivity.class));
210 listIntent.putExtras(mCreateExtras);
211 startActivity(listIntent);
212 finish();
213
214 } else {
215 // No matching contacts found
216 if (mCreateForce) {
217 // Forced to create new contact
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700218 Intent createIntent = new Intent(Intent.ACTION_INSERT, RawContacts.CONTENT_URI);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700219 createIntent.putExtras(mCreateExtras);
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700220 createIntent.setType(RawContacts.CONTENT_TYPE);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700221
222 startActivity(createIntent);
223 finish();
224
225 } else {
226 // Prompt user to insert or edit contact
227 Intent createIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
228 createIntent.putExtras(mCreateExtras);
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700229 createIntent.setType(RawContacts.CONTENT_ITEM_TYPE);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700230
231 CharSequence message = getResources().getString(
232 R.string.add_contact_dlg_message_fmt, mCreateDescrip);
233
234 new AlertDialog.Builder(this)
235 .setTitle(R.string.add_contact_dlg_title)
236 .setMessage(message)
237 .setPositiveButton(android.R.string.ok,
238 new IntentClickListener(this, createIntent))
239 .setNegativeButton(android.R.string.cancel,
240 new IntentClickListener(this, null))
241 .show();
242 }
243 }
244 }
245
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700246 /**
247 * Listener for {@link DialogInterface} that launches a given {@link Intent}
248 * when clicked. When clicked, this also closes the parent using
249 * {@link Activity#finish()}.
250 */
251 private static class IntentClickListener implements DialogInterface.OnClickListener {
252 private Activity mParent;
253 private Intent mIntent;
254
255 /**
256 * @param parent {@link Activity} to use for launching target.
257 * @param intent Target {@link Intent} to launch when clicked.
258 */
259 public IntentClickListener(Activity parent, Intent intent) {
260 mParent = parent;
261 mIntent = intent;
262 }
263
264 public void onClick(DialogInterface dialog, int which) {
265 if (mIntent != null) {
266 mParent.startActivity(mIntent);
267 }
268 mParent.finish();
269 }
270 }
Evan Millar5f4af702009-08-11 11:12:00 -0700271
272 public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) {
273 // Empty
274 }
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700275}