blob: 645032bba4079b4fbd62acf3d91dd8cb05ddaebe [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 -070020import com.android.providers.contacts2.ContactsContract;
21import com.android.providers.contacts2.ContactsContract.Aggregates;
22
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070023import android.app.Activity;
24import android.app.AlertDialog;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070025import android.content.ComponentName;
26import android.content.ContentUris;
Jeff Sharkey3f177592009-05-18 15:23:12 -070027import android.content.Context;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070028import android.content.DialogInterface;
29import android.content.Intent;
30import android.database.Cursor;
31import android.net.Uri;
32import android.os.Bundle;
Jeff Sharkey3f177592009-05-18 15:23:12 -070033import android.os.IBinder;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070034import android.provider.Contacts;
35import android.provider.Contacts.ContactMethods;
36import android.provider.Contacts.ContactMethodsColumns;
37import android.provider.Contacts.Intents;
38import android.provider.Contacts.People;
39import android.provider.Contacts.Phones;
Jeff Sharkey3f177592009-05-18 15:23:12 -070040import android.view.View;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070041
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070042/**
43 * Handle several edge cases around showing or possibly creating contacts in
44 * connected with a specific E-mail address or phone number. Will search based
45 * on incoming {@link Intent#getData()} as described by
46 * {@link Intents#SHOW_OR_CREATE_CONTACT}.
Jeff Sharkey26c7e732009-04-01 17:30:46 -070047 *
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070048 * <ul>
49 * <li>If no matching contacts found, will prompt user with dialog to add to a
50 * contact, then will use {@link Intent#ACTION_INSERT_OR_EDIT} to let create new
51 * contact or edit new data into an existing one.
52 * <li>If one matching contact found, directly show {@link Intent#ACTION_VIEW}
53 * that specific contact.
54 * <li>If more than one matching found, show list of matching contacts using
55 * {@link Intent#ACTION_SEARCH}.
56 * </ul>
57 */
Jeff Sharkey549aa162009-05-21 01:33:30 -070058public final class ShowOrCreateActivity extends Activity implements QueryCompleteListener, 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[] {
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070063 Phones.PERSON_ID,
64 };
65
Jeff Sharkey3f177592009-05-18 15:23:12 -070066 static final String[] CONTACTS_PROJECTION = new String[] {
67 ContactsContract.Contacts.AGGREGATE_ID,
68// People._ID,
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070069 };
Jeff Sharkey549aa162009-05-21 01:33:30 -070070
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070071 static final String SCHEME_MAILTO = "mailto";
72 static final String SCHEME_TEL = "tel";
Jeff Sharkey549aa162009-05-21 01:33:30 -070073
Jeff Sharkey3f177592009-05-18 15:23:12 -070074 static final int AGGREGATE_ID_INDEX = 0;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070075
76 /**
77 * Query clause to filter {@link ContactMethods#CONTENT_URI} to only search
78 * {@link Contacts#KIND_EMAIL} or {@link Contacts#KIND_IM}.
79 */
80 static final String QUERY_KIND_EMAIL_OR_IM = ContactMethodsColumns.KIND +
81 " IN (" + Contacts.KIND_EMAIL + "," + Contacts.KIND_IM + ")";
Jeff Sharkey549aa162009-05-21 01:33:30 -070082
Jeff Sharkey3f177592009-05-18 15:23:12 -070083 /**
84 * Extra used to request a specific {@link FastTrackWindow} position.
85 */
86 private static final String EXTRA_Y = "pixel_y";
87 private static final int DEFAULT_Y = 90;
88
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070089 static final int QUERY_TOKEN = 42;
Jeff Sharkey549aa162009-05-21 01:33:30 -070090
Jeff Sharkey3f177592009-05-18 15:23:12 -070091 private NotifyingAsyncQueryHandler mQueryHandler;
92
93 private Bundle mCreateExtras;
94 private String mCreateDescrip;
95 private boolean mCreateForce;
96
97 private FastTrackWindow mFastTrack;
Jeff Sharkey549aa162009-05-21 01:33:30 -070098
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070099 @Override
100 protected void onCreate(Bundle icicle) {
101 super.onCreate(icicle);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700102
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700103 // Create handler if doesn't exist, otherwise cancel any running
104 if (mQueryHandler == null) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700105 mQueryHandler = new NotifyingAsyncQueryHandler(this, this);
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700106 } else {
107 mQueryHandler.cancelOperation(QUERY_TOKEN);
108 }
109
110 final Intent intent = getIntent();
111 final Uri data = intent.getData();
Jeff Sharkey3f177592009-05-18 15:23:12 -0700112
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700113 // Unpack scheme and target data from intent
114 String scheme = null;
115 String ssp = null;
116 if (data != null) {
117 scheme = data.getScheme();
118 ssp = data.getSchemeSpecificPart();
119 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700120
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700121 // Build set of extras for possible use when creating contact
Jeff Sharkey3f177592009-05-18 15:23:12 -0700122 mCreateExtras = new Bundle();
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700123 Bundle originalExtras = intent.getExtras();
124 if (originalExtras != null) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700125 mCreateExtras.putAll(originalExtras);
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700126 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700127
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700128 // Read possible extra with specific title
Jeff Sharkey549aa162009-05-21 01:33:30 -0700129 mCreateDescrip = intent.getStringExtra(Intents.EXTRA_CREATE_DESCRIPTION);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700130 if (mCreateDescrip == null) {
131 mCreateDescrip = ssp;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700132 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700133
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700134 // Allow caller to bypass dialog prompt
Jeff Sharkey3f177592009-05-18 15:23:12 -0700135 mCreateForce = intent.getBooleanExtra(Intents.EXTRA_FORCE_CREATE, false);
136
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700137 // Handle specific query request
138 if (SCHEME_MAILTO.equals(scheme)) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700139 mCreateExtras.putString(Intents.Insert.EMAIL, ssp);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700140
Jeff Sharkey549aa162009-05-21 01:33:30 -0700141 Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_EMAIL_URI,
142 Uri.encode(ssp));
Jeffrey Sharkeyc2158b72009-03-27 17:57:15 -0700143 mQueryHandler.startQuery(QUERY_TOKEN, null, uri,
Jeff Sharkey3f177592009-05-18 15:23:12 -0700144 CONTACTS_PROJECTION, null, null, null);
145
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700146 } else if (SCHEME_TEL.equals(scheme)) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700147 mCreateExtras.putString(Intents.Insert.PHONE, ssp);
148// mQueryHandler.startQuery(QUERY_TOKEN, null,
149// Uri.withAppendedPath(Phones.CONTENT_FILTER_URL, ssp),
150// PHONES_PROJECTION, null, null, null);
151
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700152 } else {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700153 // Otherwise assume incoming aggregate Uri
154 final int y = getIntent().getExtras().getInt(EXTRA_Y, DEFAULT_Y);
155 showFastTrack(data, y);
156
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700157 }
158 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700159
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700160 @Override
161 protected void onStop() {
162 super.onStop();
163 if (mQueryHandler != null) {
164 mQueryHandler.cancelOperation(QUERY_TOKEN);
165 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700166 if (mFastTrack != null) {
167 mFastTrack.dismiss();
168 }
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700169 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700170
171 /**
172 * Show a {@link FastTrackWindow} for the given aggregate at the requested
173 * screen location.
174 */
175 private void showFastTrack(Uri aggUri, int y) {
176 // Use our local window token for now
Jeff Sharkey549aa162009-05-21 01:33:30 -0700177 mFastTrack = new FastTrackWindow(this, this);
178 mFastTrack.show(aggUri, y);
179 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700180
Jeff Sharkey549aa162009-05-21 01:33:30 -0700181 /** {@inheritDoc} */
182 public void onDismiss(FastTrackWindow dialog) {
183 // When dismissed, finish this activity
184 finish();
Jeff Sharkey3f177592009-05-18 15:23:12 -0700185 }
186
187 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
188 if (cursor == null) {
189 return;
190 }
191
192 // Count contacts found by query
193 int count = 0;
194 long aggId = -1;
195 try {
196 count = cursor.getCount();
197 if (count == 1 && cursor.moveToFirst()) {
198 // Try reading ID if only one contact returned
199 aggId = cursor.getLong(AGGREGATE_ID_INDEX);
200 }
201 } finally {
202 cursor.close();
203 }
204
205 if (count == 1 && aggId != -1) {
206 // If we only found one item, jump right to viewing it
207 final Uri aggUri = ContentUris.withAppendedId(Aggregates.CONTENT_URI, aggId);
208 final int y = getIntent().getExtras().getInt(EXTRA_Y, DEFAULT_Y);
209 showFastTrack(aggUri, y);
210
211// Intent viewIntent = new Intent(Intent.ACTION_VIEW,
212// ContentUris.withAppendedId(People.CONTENT_URI, personId));
213// activity.startActivity(viewIntent);
214// activity.finish();
215
216 } else if (count > 1) {
217 // If more than one, show pick list
218 Intent listIntent = new Intent(Intent.ACTION_SEARCH);
219 listIntent.setComponent(new ComponentName(this, ContactsListActivity.class));
220 listIntent.putExtras(mCreateExtras);
221 startActivity(listIntent);
222 finish();
223
224 } else {
225 // No matching contacts found
226 if (mCreateForce) {
227 // Forced to create new contact
228 Intent createIntent = new Intent(Intent.ACTION_INSERT, People.CONTENT_URI);
229 createIntent.putExtras(mCreateExtras);
230 createIntent.setType(People.CONTENT_TYPE);
231
232 startActivity(createIntent);
233 finish();
234
235 } else {
236 // Prompt user to insert or edit contact
237 Intent createIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
238 createIntent.putExtras(mCreateExtras);
239 createIntent.setType(People.CONTENT_ITEM_TYPE);
240
241 CharSequence message = getResources().getString(
242 R.string.add_contact_dlg_message_fmt, mCreateDescrip);
243
244 new AlertDialog.Builder(this)
245 .setTitle(R.string.add_contact_dlg_title)
246 .setMessage(message)
247 .setPositiveButton(android.R.string.ok,
248 new IntentClickListener(this, createIntent))
249 .setNegativeButton(android.R.string.cancel,
250 new IntentClickListener(this, null))
251 .show();
252 }
253 }
254 }
255
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700256 /**
257 * Listener for {@link DialogInterface} that launches a given {@link Intent}
258 * when clicked. When clicked, this also closes the parent using
259 * {@link Activity#finish()}.
260 */
261 private static class IntentClickListener implements DialogInterface.OnClickListener {
262 private Activity mParent;
263 private Intent mIntent;
264
265 /**
266 * @param parent {@link Activity} to use for launching target.
267 * @param intent Target {@link Intent} to launch when clicked.
268 */
269 public IntentClickListener(Activity parent, Intent intent) {
270 mParent = parent;
271 mIntent = intent;
272 }
273
274 public void onClick(DialogInterface dialog, int which) {
275 if (mIntent != null) {
276 mParent.startActivity(mIntent);
277 }
278 mParent.finish();
279 }
280 }
281
282 /**
Jeff Sharkey3f177592009-05-18 15:23:12 -0700283 * Fake view that simply exists to pass through a specific {@link IBinder}
284 * window token.
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700285 */
Jeff Sharkey3f177592009-05-18 15:23:12 -0700286 private static class FakeView extends View {
287 private IBinder mWindowToken;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700288
Jeff Sharkey3f177592009-05-18 15:23:12 -0700289 public FakeView(Context context, IBinder windowToken) {
290 super(context);
291 mWindowToken = windowToken;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700292 }
293
294 @Override
Jeff Sharkey3f177592009-05-18 15:23:12 -0700295 public IBinder getWindowToken() {
296 return mWindowToken;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700297 }
298 }
299}