blob: 365353ecabcef157e3c1ccc851cca93259d1a832 [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 Sharkey3f177592009-05-18 15:23:12 -070020
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070021import android.app.Activity;
22import android.app.AlertDialog;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070023import android.content.ComponentName;
24import android.content.ContentUris;
Jeff Sharkey3f177592009-05-18 15:23:12 -070025import android.content.Context;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070026import android.content.DialogInterface;
27import android.content.Intent;
28import android.database.Cursor;
Jeff Sharkey39261272009-06-03 19:15:09 -070029import android.graphics.Rect;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070030import android.net.Uri;
31import android.os.Bundle;
Jeff Sharkey3f177592009-05-18 15:23:12 -070032import android.os.IBinder;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070033import android.provider.Contacts.Intents;
Evan Millar66388be2009-05-28 15:41:07 -070034import android.provider.ContactsContract.Aggregates;
Dmitri Plotnikov39466592009-07-27 11:23:51 -070035import android.provider.ContactsContract.RawContacts;
Jeff Sharkey39261272009-06-03 19:15:09 -070036import android.provider.ContactsContract.PhoneLookup;
Jeff Sharkey3f177592009-05-18 15:23:12 -070037import android.view.View;
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 Sharkey39261272009-06-03 19:15:09 -070055public final class ShowOrCreateActivity extends Activity implements QueryCompleteListener,
56 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 Plotnikov39466592009-07-27 11:23:51 -070065 RawContacts.AGGREGATE_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
73 /**
Jeff Sharkey3f177592009-05-18 15:23:12 -070074 * Extra used to request a specific {@link FastTrackWindow} position.
Jeff Sharkey39261272009-06-03 19:15:09 -070075 * Normally the fast-track window will try pointing an arrow towards this
76 * location, but if the left and right edges are crossed, the arrow may be
77 * hidden.
Jeff Sharkey3f177592009-05-18 15:23:12 -070078 */
Jeff Sharkey39261272009-06-03 19:15:09 -070079 private static final String EXTRA_RECT = "target_rect";
Jeff Sharkey3f177592009-05-18 15:23:12 -070080
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070081 static final int QUERY_TOKEN = 42;
Jeff Sharkey549aa162009-05-21 01:33:30 -070082
Jeff Sharkey3f177592009-05-18 15:23:12 -070083 private NotifyingAsyncQueryHandler mQueryHandler;
84
85 private Bundle mCreateExtras;
86 private String mCreateDescrip;
87 private boolean mCreateForce;
88
89 private FastTrackWindow mFastTrack;
Jeff Sharkey549aa162009-05-21 01:33:30 -070090
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070091 @Override
92 protected void onCreate(Bundle icicle) {
93 super.onCreate(icicle);
Jeff Sharkey3f177592009-05-18 15:23:12 -070094
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070095 // Create handler if doesn't exist, otherwise cancel any running
96 if (mQueryHandler == null) {
Jeff Sharkey3f177592009-05-18 15:23:12 -070097 mQueryHandler = new NotifyingAsyncQueryHandler(this, this);
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070098 } else {
99 mQueryHandler.cancelOperation(QUERY_TOKEN);
100 }
101
102 final Intent intent = getIntent();
103 final Uri data = intent.getData();
Jeff Sharkey3f177592009-05-18 15:23:12 -0700104
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700105 // Unpack scheme and target data from intent
106 String scheme = null;
107 String ssp = null;
108 if (data != null) {
109 scheme = data.getScheme();
110 ssp = data.getSchemeSpecificPart();
111 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700112
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700113 // Build set of extras for possible use when creating contact
Jeff Sharkey3f177592009-05-18 15:23:12 -0700114 mCreateExtras = new Bundle();
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700115 Bundle originalExtras = intent.getExtras();
116 if (originalExtras != null) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700117 mCreateExtras.putAll(originalExtras);
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 // Read possible extra with specific title
Jeff Sharkey549aa162009-05-21 01:33:30 -0700121 mCreateDescrip = intent.getStringExtra(Intents.EXTRA_CREATE_DESCRIPTION);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700122 if (mCreateDescrip == null) {
123 mCreateDescrip = ssp;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700124 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700125
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700126 // Allow caller to bypass dialog prompt
Jeff Sharkey3f177592009-05-18 15:23:12 -0700127 mCreateForce = intent.getBooleanExtra(Intents.EXTRA_FORCE_CREATE, false);
128
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700129 // Handle specific query request
130 if (SCHEME_MAILTO.equals(scheme)) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700131 mCreateExtras.putString(Intents.Insert.EMAIL, ssp);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700132
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700133 Uri uri = Uri.withAppendedPath(RawContacts.CONTENT_FILTER_EMAIL_URI, Uri.encode(ssp));
Jeff Sharkey39261272009-06-03 19:15:09 -0700134 mQueryHandler.startQuery(QUERY_TOKEN, null, uri, CONTACTS_PROJECTION, null, null, null);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700135
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700136 } else if (SCHEME_TEL.equals(scheme)) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700137 mCreateExtras.putString(Intents.Insert.PHONE, ssp);
Jeff Sharkey39261272009-06-03 19:15:09 -0700138
139 Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, ssp);
140 mQueryHandler.startQuery(QUERY_TOKEN, null, uri, PHONES_PROJECTION, null, null, null);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700141
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700142 } else {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700143 // Otherwise assume incoming aggregate Uri
Jeff Sharkey80a193a2009-05-21 14:18:18 -0700144 showFastTrack(data);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700145
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700146 }
147 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700148
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700149 @Override
150 protected void onStop() {
151 super.onStop();
152 if (mQueryHandler != null) {
153 mQueryHandler.cancelOperation(QUERY_TOKEN);
154 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700155 if (mFastTrack != null) {
156 mFastTrack.dismiss();
157 }
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700158 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700159
160 /**
161 * Show a {@link FastTrackWindow} for the given aggregate at the requested
162 * screen location.
163 */
Jeff Sharkey80a193a2009-05-21 14:18:18 -0700164 private void showFastTrack(Uri aggUri) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700165 // Use our local window token for now
Jeff Sharkey80a193a2009-05-21 14:18:18 -0700166 Bundle extras = getIntent().getExtras();
Jeff Sharkey39261272009-06-03 19:15:09 -0700167
168 Rect targetRect;
169 if (extras.containsKey(EXTRA_RECT)) {
170 targetRect = (Rect)extras.getParcelable(EXTRA_RECT);
171 } else {
172 // TODO: this default rect matches gmail messages, and should move over there
173 targetRect = new Rect(15, 110, 15+18, 110+18);
174 }
175
Jeff Sharkey549aa162009-05-21 01:33:30 -0700176 mFastTrack = new FastTrackWindow(this, this);
Jeff Sharkey39261272009-06-03 19:15:09 -0700177 mFastTrack.show(aggUri, targetRect);
Jeff Sharkey549aa162009-05-21 01:33:30 -0700178 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700179
Jeff Sharkey549aa162009-05-21 01:33:30 -0700180 /** {@inheritDoc} */
181 public void onDismiss(FastTrackWindow dialog) {
182 // When dismissed, finish this activity
183 finish();
Jeff Sharkey3f177592009-05-18 15:23:12 -0700184 }
185
186 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
187 if (cursor == null) {
188 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
Jeff Sharkey3f177592009-05-18 15:23:12 -0700206 final Uri aggUri = ContentUris.withAppendedId(Aggregates.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
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}