blob: 27fce1851f86359911432b06b4d39a68f3536c62 [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;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070037
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070038/**
39 * Handle several edge cases around showing or possibly creating contacts in
40 * connected with a specific E-mail address or phone number. Will search based
41 * on incoming {@link Intent#getData()} as described by
Jeff Sharkey39261272009-06-03 19:15:09 -070042 * {@link android.provider.Contacts.Intents#SHOW_OR_CREATE_CONTACT}.
Jeff Sharkey26c7e732009-04-01 17:30:46 -070043 *
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070044 * <ul>
45 * <li>If no matching contacts found, will prompt user with dialog to add to a
46 * contact, then will use {@link Intent#ACTION_INSERT_OR_EDIT} to let create new
47 * contact or edit new data into an existing one.
48 * <li>If one matching contact found, directly show {@link Intent#ACTION_VIEW}
49 * that specific contact.
50 * <li>If more than one matching found, show list of matching contacts using
51 * {@link Intent#ACTION_SEARCH}.
52 * </ul>
53 */
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -070054public final class ShowOrCreateActivity extends Activity implements
55 NotifyingAsyncQueryHandler.AsyncQueryListener, FastTrackWindow.OnDismissListener {
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070056 static final String TAG = "ShowOrCreateActivity";
57 static final boolean LOGD = false;
58
59 static final String[] PHONES_PROJECTION = new String[] {
Jeff Sharkey39261272009-06-03 19:15:09 -070060 PhoneLookup._ID,
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070061 };
62
Jeff Sharkey3f177592009-05-18 15:23:12 -070063 static final String[] CONTACTS_PROJECTION = new String[] {
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -070064 RawContacts.CONTACT_ID,
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070065 };
Jeff Sharkey549aa162009-05-21 01:33:30 -070066
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070067 static final String SCHEME_MAILTO = "mailto";
68 static final String SCHEME_TEL = "tel";
Jeff Sharkey549aa162009-05-21 01:33:30 -070069
Jeff Sharkey3f177592009-05-18 15:23:12 -070070 static final int AGGREGATE_ID_INDEX = 0;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070071
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070072 static final int QUERY_TOKEN = 42;
Jeff Sharkey549aa162009-05-21 01:33:30 -070073
Jeff Sharkey3f177592009-05-18 15:23:12 -070074 private NotifyingAsyncQueryHandler mQueryHandler;
75
76 private Bundle mCreateExtras;
77 private String mCreateDescrip;
78 private boolean mCreateForce;
79
80 private FastTrackWindow mFastTrack;
Jeff Sharkey549aa162009-05-21 01:33:30 -070081
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070082 @Override
83 protected void onCreate(Bundle icicle) {
84 super.onCreate(icicle);
Jeff Sharkey3f177592009-05-18 15:23:12 -070085
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070086 // Create handler if doesn't exist, otherwise cancel any running
87 if (mQueryHandler == null) {
Jeff Sharkey3f177592009-05-18 15:23:12 -070088 mQueryHandler = new NotifyingAsyncQueryHandler(this, this);
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070089 } else {
90 mQueryHandler.cancelOperation(QUERY_TOKEN);
91 }
92
93 final Intent intent = getIntent();
94 final Uri data = intent.getData();
Jeff Sharkey3f177592009-05-18 15:23:12 -070095
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070096 // Unpack scheme and target data from intent
97 String scheme = null;
98 String ssp = null;
99 if (data != null) {
100 scheme = data.getScheme();
101 ssp = data.getSchemeSpecificPart();
102 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700103
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700104 // Build set of extras for possible use when creating contact
Jeff Sharkey3f177592009-05-18 15:23:12 -0700105 mCreateExtras = new Bundle();
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700106 Bundle originalExtras = intent.getExtras();
107 if (originalExtras != null) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700108 mCreateExtras.putAll(originalExtras);
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700109 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700110
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700111 // Read possible extra with specific title
Jeff Sharkey549aa162009-05-21 01:33:30 -0700112 mCreateDescrip = intent.getStringExtra(Intents.EXTRA_CREATE_DESCRIPTION);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700113 if (mCreateDescrip == null) {
114 mCreateDescrip = ssp;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700115 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700116
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700117 // Allow caller to bypass dialog prompt
Jeff Sharkey3f177592009-05-18 15:23:12 -0700118 mCreateForce = intent.getBooleanExtra(Intents.EXTRA_FORCE_CREATE, false);
119
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700120 // Handle specific query request
121 if (SCHEME_MAILTO.equals(scheme)) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700122 mCreateExtras.putString(Intents.Insert.EMAIL, ssp);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700123
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700124 Uri uri = Uri.withAppendedPath(RawContacts.CONTENT_FILTER_EMAIL_URI, Uri.encode(ssp));
Jeff Sharkey39261272009-06-03 19:15:09 -0700125 mQueryHandler.startQuery(QUERY_TOKEN, null, uri, CONTACTS_PROJECTION, null, null, null);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700126
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700127 } else if (SCHEME_TEL.equals(scheme)) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700128 mCreateExtras.putString(Intents.Insert.PHONE, ssp);
Jeff Sharkey39261272009-06-03 19:15:09 -0700129
130 Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, ssp);
131 mQueryHandler.startQuery(QUERY_TOKEN, null, uri, PHONES_PROJECTION, null, null, null);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700132
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700133 } else {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700134 // Otherwise assume incoming aggregate Uri
Jeff Sharkey80a193a2009-05-21 14:18:18 -0700135 showFastTrack(data);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700136
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700137 }
138 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700139
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700140 @Override
141 protected void onStop() {
142 super.onStop();
143 if (mQueryHandler != null) {
144 mQueryHandler.cancelOperation(QUERY_TOKEN);
145 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700146 if (mFastTrack != null) {
147 mFastTrack.dismiss();
148 }
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700149 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700150
151 /**
152 * Show a {@link FastTrackWindow} for the given aggregate at the requested
153 * screen location.
154 */
Jeff Sharkey80a193a2009-05-21 14:18:18 -0700155 private void showFastTrack(Uri aggUri) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700156 // Use our local window token for now
Jeff Sharkey802b2052009-08-04 14:21:06 -0700157 final Bundle extras = getIntent().getExtras();
Jeff Sharkey39261272009-06-03 19:15:09 -0700158
159 Rect targetRect;
Jeff Sharkey802b2052009-08-04 14:21:06 -0700160 if (extras.containsKey(Intents.EXTRA_TARGET_RECT)) {
161 targetRect = (Rect)extras.getParcelable(Intents.EXTRA_TARGET_RECT);
Jeff Sharkey39261272009-06-03 19:15:09 -0700162 } else {
163 // TODO: this default rect matches gmail messages, and should move over there
164 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);
169
Jeff Sharkey549aa162009-05-21 01:33:30 -0700170 mFastTrack = new FastTrackWindow(this, this);
Jeff Sharkey802b2052009-08-04 14:21:06 -0700171 mFastTrack.show(aggUri, targetRect, mode);
Jeff Sharkey549aa162009-05-21 01:33:30 -0700172 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700173
Jeff Sharkey549aa162009-05-21 01:33:30 -0700174 /** {@inheritDoc} */
175 public void onDismiss(FastTrackWindow dialog) {
176 // When dismissed, finish this activity
177 finish();
Jeff Sharkey3f177592009-05-18 15:23:12 -0700178 }
179
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -0700180 /** {@inheritDoc} */
Jeff Sharkey3f177592009-05-18 15:23:12 -0700181 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
182 if (cursor == null) {
183 return;
184 }
185
186 // Count contacts found by query
187 int count = 0;
188 long aggId = -1;
189 try {
190 count = cursor.getCount();
191 if (count == 1 && cursor.moveToFirst()) {
192 // Try reading ID if only one contact returned
193 aggId = cursor.getLong(AGGREGATE_ID_INDEX);
194 }
195 } finally {
196 cursor.close();
197 }
198
199 if (count == 1 && aggId != -1) {
Jeff Sharkey39261272009-06-03 19:15:09 -0700200 // If we only found one item, show fast-track
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700201 final Uri aggUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, aggId);
Jeff Sharkey80a193a2009-05-21 14:18:18 -0700202 showFastTrack(aggUri);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700203
Jeff Sharkey3f177592009-05-18 15:23:12 -0700204 } else if (count > 1) {
205 // If more than one, show pick list
206 Intent listIntent = new Intent(Intent.ACTION_SEARCH);
207 listIntent.setComponent(new ComponentName(this, ContactsListActivity.class));
208 listIntent.putExtras(mCreateExtras);
209 startActivity(listIntent);
210 finish();
211
212 } else {
213 // No matching contacts found
214 if (mCreateForce) {
215 // Forced to create new contact
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700216 Intent createIntent = new Intent(Intent.ACTION_INSERT, RawContacts.CONTENT_URI);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700217 createIntent.putExtras(mCreateExtras);
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700218 createIntent.setType(RawContacts.CONTENT_TYPE);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700219
220 startActivity(createIntent);
221 finish();
222
223 } else {
224 // Prompt user to insert or edit contact
225 Intent createIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
226 createIntent.putExtras(mCreateExtras);
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700227 createIntent.setType(RawContacts.CONTENT_ITEM_TYPE);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700228
229 CharSequence message = getResources().getString(
230 R.string.add_contact_dlg_message_fmt, mCreateDescrip);
231
232 new AlertDialog.Builder(this)
233 .setTitle(R.string.add_contact_dlg_title)
234 .setMessage(message)
235 .setPositiveButton(android.R.string.ok,
236 new IntentClickListener(this, createIntent))
237 .setNegativeButton(android.R.string.cancel,
238 new IntentClickListener(this, null))
239 .show();
240 }
241 }
242 }
243
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -0700244 /** {@inheritDoc} */
245 public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) {
246 // No actions
247 }
248
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700249 /**
250 * Listener for {@link DialogInterface} that launches a given {@link Intent}
251 * when clicked. When clicked, this also closes the parent using
252 * {@link Activity#finish()}.
253 */
254 private static class IntentClickListener implements DialogInterface.OnClickListener {
255 private Activity mParent;
256 private Intent mIntent;
257
258 /**
259 * @param parent {@link Activity} to use for launching target.
260 * @param intent Target {@link Intent} to launch when clicked.
261 */
262 public IntentClickListener(Activity parent, Intent intent) {
263 mParent = parent;
264 mIntent = intent;
265 }
266
267 public void onClick(DialogInterface dialog, int which) {
268 if (mIntent != null) {
269 mParent.startActivity(mIntent);
270 }
271 mParent.finish();
272 }
273 }
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700274}