blob: fb6f60b5381a6e1cbefe8708da0ad05cdbbe5523 [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 Sharkey3f177592009-05-18 15:23:12 -070019import com.android.contacts.NotifyingAsyncQueryHandler.QueryCompleteListener;
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;
28import 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;
Jeff Sharkey3f177592009-05-18 15:23:12 -070033import android.os.IBinder;
Jeff Sharkey802b2052009-08-04 14:21:06 -070034import android.provider.ContactsContract.Intents;
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -070035import android.provider.ContactsContract.Contacts;
Dmitri Plotnikov39466592009-07-27 11:23:51 -070036import android.provider.ContactsContract.RawContacts;
Jeff Sharkey39261272009-06-03 19:15:09 -070037import android.provider.ContactsContract.PhoneLookup;
Jeff Sharkey3f177592009-05-18 15:23:12 -070038import android.view.View;
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 Sharkey39261272009-06-03 19:15:09 -070044 * {@link android.provider.Contacts.Intents#SHOW_OR_CREATE_CONTACT}.
Jeff Sharkey26c7e732009-04-01 17:30:46 -070045 *
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.
50 * <li>If one matching contact found, directly show {@link Intent#ACTION_VIEW}
51 * that specific contact.
52 * <li>If more than one matching found, show list of matching contacts using
53 * {@link Intent#ACTION_SEARCH}.
54 * </ul>
55 */
Jeff Sharkey39261272009-06-03 19:15:09 -070056public final class ShowOrCreateActivity extends Activity implements QueryCompleteListener,
57 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
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070069 static final String SCHEME_MAILTO = "mailto";
70 static final String SCHEME_TEL = "tel";
Jeff Sharkey549aa162009-05-21 01:33:30 -070071
Jeff Sharkey3f177592009-05-18 15:23:12 -070072 static final int AGGREGATE_ID_INDEX = 0;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070073
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070074 static final int QUERY_TOKEN = 42;
Jeff Sharkey549aa162009-05-21 01:33:30 -070075
Jeff Sharkey3f177592009-05-18 15:23:12 -070076 private NotifyingAsyncQueryHandler mQueryHandler;
77
78 private Bundle mCreateExtras;
79 private String mCreateDescrip;
80 private boolean mCreateForce;
81
82 private FastTrackWindow mFastTrack;
Jeff Sharkey549aa162009-05-21 01:33:30 -070083
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070084 @Override
85 protected void onCreate(Bundle icicle) {
86 super.onCreate(icicle);
Jeff Sharkey3f177592009-05-18 15:23:12 -070087
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070088 // Create handler if doesn't exist, otherwise cancel any running
89 if (mQueryHandler == null) {
Jeff Sharkey3f177592009-05-18 15:23:12 -070090 mQueryHandler = new NotifyingAsyncQueryHandler(this, this);
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070091 } else {
92 mQueryHandler.cancelOperation(QUERY_TOKEN);
93 }
94
95 final Intent intent = getIntent();
96 final Uri data = intent.getData();
Jeff Sharkey3f177592009-05-18 15:23:12 -070097
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070098 // Unpack scheme and target data from intent
99 String scheme = null;
100 String ssp = null;
101 if (data != null) {
102 scheme = data.getScheme();
103 ssp = data.getSchemeSpecificPart();
104 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700105
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700106 // Build set of extras for possible use when creating contact
Jeff Sharkey3f177592009-05-18 15:23:12 -0700107 mCreateExtras = new Bundle();
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700108 Bundle originalExtras = intent.getExtras();
109 if (originalExtras != null) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700110 mCreateExtras.putAll(originalExtras);
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700111 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700112
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700113 // Read possible extra with specific title
Jeff Sharkey549aa162009-05-21 01:33:30 -0700114 mCreateDescrip = intent.getStringExtra(Intents.EXTRA_CREATE_DESCRIPTION);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700115 if (mCreateDescrip == null) {
116 mCreateDescrip = ssp;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700117 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700118
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700119 // Allow caller to bypass dialog prompt
Jeff Sharkey3f177592009-05-18 15:23:12 -0700120 mCreateForce = intent.getBooleanExtra(Intents.EXTRA_FORCE_CREATE, false);
121
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700122 // Handle specific query request
123 if (SCHEME_MAILTO.equals(scheme)) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700124 mCreateExtras.putString(Intents.Insert.EMAIL, ssp);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700125
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700126 Uri uri = Uri.withAppendedPath(RawContacts.CONTENT_FILTER_EMAIL_URI, Uri.encode(ssp));
Jeff Sharkey39261272009-06-03 19:15:09 -0700127 mQueryHandler.startQuery(QUERY_TOKEN, null, uri, CONTACTS_PROJECTION, null, null, null);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700128
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700129 } else if (SCHEME_TEL.equals(scheme)) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700130 mCreateExtras.putString(Intents.Insert.PHONE, ssp);
Jeff Sharkey39261272009-06-03 19:15:09 -0700131
132 Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, ssp);
133 mQueryHandler.startQuery(QUERY_TOKEN, null, uri, PHONES_PROJECTION, null, null, null);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700134
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700135 } else {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700136 // Otherwise assume incoming aggregate Uri
Jeff Sharkey80a193a2009-05-21 14:18:18 -0700137 showFastTrack(data);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700138
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700139 }
140 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700141
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700142 @Override
143 protected void onStop() {
144 super.onStop();
145 if (mQueryHandler != null) {
146 mQueryHandler.cancelOperation(QUERY_TOKEN);
147 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700148 if (mFastTrack != null) {
149 mFastTrack.dismiss();
150 }
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700151 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700152
153 /**
154 * Show a {@link FastTrackWindow} for the given aggregate at the requested
155 * screen location.
156 */
Jeff Sharkey80a193a2009-05-21 14:18:18 -0700157 private void showFastTrack(Uri aggUri) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700158 // Use our local window token for now
Jeff Sharkey802b2052009-08-04 14:21:06 -0700159 final Bundle extras = getIntent().getExtras();
Jeff Sharkey39261272009-06-03 19:15:09 -0700160
161 Rect targetRect;
Jeff Sharkey802b2052009-08-04 14:21:06 -0700162 if (extras.containsKey(Intents.EXTRA_TARGET_RECT)) {
163 targetRect = (Rect)extras.getParcelable(Intents.EXTRA_TARGET_RECT);
Jeff Sharkey39261272009-06-03 19:15:09 -0700164 } else {
165 // TODO: this default rect matches gmail messages, and should move over there
166 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);
171
Jeff Sharkey549aa162009-05-21 01:33:30 -0700172 mFastTrack = new FastTrackWindow(this, this);
Jeff Sharkey802b2052009-08-04 14:21:06 -0700173 mFastTrack.show(aggUri, targetRect, mode);
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
182 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
183 if (cursor == null) {
184 return;
185 }
186
187 // Count contacts found by query
188 int count = 0;
189 long aggId = -1;
190 try {
191 count = cursor.getCount();
192 if (count == 1 && cursor.moveToFirst()) {
193 // Try reading ID if only one contact returned
194 aggId = cursor.getLong(AGGREGATE_ID_INDEX);
195 }
196 } finally {
197 cursor.close();
198 }
199
200 if (count == 1 && aggId != -1) {
Jeff Sharkey39261272009-06-03 19:15:09 -0700201 // If we only found one item, show fast-track
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700202 final Uri aggUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, aggId);
Jeff Sharkey80a193a2009-05-21 14:18:18 -0700203 showFastTrack(aggUri);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700204
Jeff Sharkey3f177592009-05-18 15:23:12 -0700205 } else if (count > 1) {
206 // If more than one, show pick list
207 Intent listIntent = new Intent(Intent.ACTION_SEARCH);
208 listIntent.setComponent(new ComponentName(this, ContactsListActivity.class));
209 listIntent.putExtras(mCreateExtras);
210 startActivity(listIntent);
211 finish();
212
213 } else {
214 // No matching contacts found
215 if (mCreateForce) {
216 // Forced to create new contact
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700217 Intent createIntent = new Intent(Intent.ACTION_INSERT, RawContacts.CONTENT_URI);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700218 createIntent.putExtras(mCreateExtras);
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700219 createIntent.setType(RawContacts.CONTENT_TYPE);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700220
221 startActivity(createIntent);
222 finish();
223
224 } else {
225 // Prompt user to insert or edit contact
226 Intent createIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
227 createIntent.putExtras(mCreateExtras);
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700228 createIntent.setType(RawContacts.CONTENT_ITEM_TYPE);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700229
230 CharSequence message = getResources().getString(
231 R.string.add_contact_dlg_message_fmt, mCreateDescrip);
232
233 new AlertDialog.Builder(this)
234 .setTitle(R.string.add_contact_dlg_title)
235 .setMessage(message)
236 .setPositiveButton(android.R.string.ok,
237 new IntentClickListener(this, createIntent))
238 .setNegativeButton(android.R.string.cancel,
239 new IntentClickListener(this, null))
240 .show();
241 }
242 }
243 }
244
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700245 /**
246 * Listener for {@link DialogInterface} that launches a given {@link Intent}
247 * when clicked. When clicked, this also closes the parent using
248 * {@link Activity#finish()}.
249 */
250 private static class IntentClickListener implements DialogInterface.OnClickListener {
251 private Activity mParent;
252 private Intent mIntent;
253
254 /**
255 * @param parent {@link Activity} to use for launching target.
256 * @param intent Target {@link Intent} to launch when clicked.
257 */
258 public IntentClickListener(Activity parent, Intent intent) {
259 mParent = parent;
260 mIntent = intent;
261 }
262
263 public void onClick(DialogInterface dialog, int which) {
264 if (mIntent != null) {
265 mParent.startActivity(mIntent);
266 }
267 mParent.finish();
268 }
269 }
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700270}