blob: 56aef2648beb807c91dd48719a9991240deef3dc [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;
20import com.android.contacts.SocialStreamActivity.MappingCache;
21import com.android.providers.contacts2.ContactsContract;
22import com.android.providers.contacts2.ContactsContract.Aggregates;
23
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070024import android.app.Activity;
25import android.app.AlertDialog;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070026import android.content.ComponentName;
27import android.content.ContentUris;
Jeff Sharkey3f177592009-05-18 15:23:12 -070028import android.content.Context;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070029import android.content.DialogInterface;
30import android.content.Intent;
31import android.database.Cursor;
32import android.net.Uri;
33import android.os.Bundle;
Jeff Sharkey3f177592009-05-18 15:23:12 -070034import android.os.IBinder;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070035import android.provider.Contacts;
36import android.provider.Contacts.ContactMethods;
37import android.provider.Contacts.ContactMethodsColumns;
38import android.provider.Contacts.Intents;
39import android.provider.Contacts.People;
40import android.provider.Contacts.Phones;
41import android.util.Log;
Jeff Sharkey3f177592009-05-18 15:23:12 -070042import android.view.View;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070043
44import java.lang.ref.WeakReference;
45
46/**
47 * Handle several edge cases around showing or possibly creating contacts in
48 * connected with a specific E-mail address or phone number. Will search based
49 * on incoming {@link Intent#getData()} as described by
50 * {@link Intents#SHOW_OR_CREATE_CONTACT}.
Jeff Sharkey26c7e732009-04-01 17:30:46 -070051 *
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070052 * <ul>
53 * <li>If no matching contacts found, will prompt user with dialog to add to a
54 * contact, then will use {@link Intent#ACTION_INSERT_OR_EDIT} to let create new
55 * contact or edit new data into an existing one.
56 * <li>If one matching contact found, directly show {@link Intent#ACTION_VIEW}
57 * that specific contact.
58 * <li>If more than one matching found, show list of matching contacts using
59 * {@link Intent#ACTION_SEARCH}.
60 * </ul>
61 */
Jeff Sharkey3f177592009-05-18 15:23:12 -070062public final class ShowOrCreateActivity extends Activity implements QueryCompleteListener {
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070063 static final String TAG = "ShowOrCreateActivity";
64 static final boolean LOGD = false;
65
66 static final String[] PHONES_PROJECTION = new String[] {
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070067 Phones.PERSON_ID,
68 };
69
Jeff Sharkey3f177592009-05-18 15:23:12 -070070 static final String[] CONTACTS_PROJECTION = new String[] {
71 ContactsContract.Contacts.AGGREGATE_ID,
72// People._ID,
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070073 };
74
75 static final String SCHEME_MAILTO = "mailto";
76 static final String SCHEME_TEL = "tel";
77
Jeff Sharkey3f177592009-05-18 15:23:12 -070078 static final int AGGREGATE_ID_INDEX = 0;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070079
80 /**
81 * Query clause to filter {@link ContactMethods#CONTENT_URI} to only search
82 * {@link Contacts#KIND_EMAIL} or {@link Contacts#KIND_IM}.
83 */
84 static final String QUERY_KIND_EMAIL_OR_IM = ContactMethodsColumns.KIND +
85 " IN (" + Contacts.KIND_EMAIL + "," + Contacts.KIND_IM + ")";
86
Jeff Sharkey3f177592009-05-18 15:23:12 -070087 /**
88 * Extra used to request a specific {@link FastTrackWindow} position.
89 */
90 private static final String EXTRA_Y = "pixel_y";
91 private static final int DEFAULT_Y = 90;
92
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070093 static final int QUERY_TOKEN = 42;
94
Jeff Sharkey3f177592009-05-18 15:23:12 -070095 private NotifyingAsyncQueryHandler mQueryHandler;
96
97 private Bundle mCreateExtras;
98 private String mCreateDescrip;
99 private boolean mCreateForce;
100
101 private FastTrackWindow mFastTrack;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700102
103 @Override
104 protected void onCreate(Bundle icicle) {
105 super.onCreate(icicle);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700106
107 // Throw an empty layout up so we have a window token later
108 setContentView(R.layout.empty);
109
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700110 // Create handler if doesn't exist, otherwise cancel any running
111 if (mQueryHandler == null) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700112 mQueryHandler = new NotifyingAsyncQueryHandler(this, this);
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700113 } else {
114 mQueryHandler.cancelOperation(QUERY_TOKEN);
115 }
116
117 final Intent intent = getIntent();
118 final Uri data = intent.getData();
Jeff Sharkey3f177592009-05-18 15:23:12 -0700119
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700120 // Unpack scheme and target data from intent
121 String scheme = null;
122 String ssp = null;
123 if (data != null) {
124 scheme = data.getScheme();
125 ssp = data.getSchemeSpecificPart();
126 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700127
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700128 // Build set of extras for possible use when creating contact
Jeff Sharkey3f177592009-05-18 15:23:12 -0700129 mCreateExtras = new Bundle();
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700130 Bundle originalExtras = intent.getExtras();
131 if (originalExtras != null) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700132 mCreateExtras.putAll(originalExtras);
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700133 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700134
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700135 // Read possible extra with specific title
Jeff Sharkey3f177592009-05-18 15:23:12 -0700136 String mCreateDescrip = intent.getStringExtra(Intents.EXTRA_CREATE_DESCRIPTION);
137 if (mCreateDescrip == null) {
138 mCreateDescrip = ssp;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700139 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700140
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700141 // Allow caller to bypass dialog prompt
Jeff Sharkey3f177592009-05-18 15:23:12 -0700142 mCreateForce = intent.getBooleanExtra(Intents.EXTRA_FORCE_CREATE, false);
143
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700144 // Handle specific query request
145 if (SCHEME_MAILTO.equals(scheme)) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700146 mCreateExtras.putString(Intents.Insert.EMAIL, ssp);
147// Uri uri = Uri.withAppendedPath(People.WITH_EMAIL_OR_IM_FILTER_URI, Uri.encode(ssp));
148// mQueryHandler.startQuery(QUERY_TOKEN, null, uri,
149// PEOPLE_PROJECTION, null, null, null);
150
151 Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_EMAIL_FILTER_URI, Uri.encode(ssp));
Jeffrey Sharkeyc2158b72009-03-27 17:57:15 -0700152 mQueryHandler.startQuery(QUERY_TOKEN, null, uri,
Jeff Sharkey3f177592009-05-18 15:23:12 -0700153 CONTACTS_PROJECTION, null, null, null);
154
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700155 } else if (SCHEME_TEL.equals(scheme)) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700156 mCreateExtras.putString(Intents.Insert.PHONE, ssp);
157// mQueryHandler.startQuery(QUERY_TOKEN, null,
158// Uri.withAppendedPath(Phones.CONTENT_FILTER_URL, ssp),
159// PHONES_PROJECTION, null, null, null);
160
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700161 } else {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700162 // Otherwise assume incoming aggregate Uri
163 final int y = getIntent().getExtras().getInt(EXTRA_Y, DEFAULT_Y);
164 showFastTrack(data, y);
165
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700166 }
167 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700168
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700169 @Override
170 protected void onStop() {
171 super.onStop();
172 if (mQueryHandler != null) {
173 mQueryHandler.cancelOperation(QUERY_TOKEN);
174 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700175 if (mFastTrack != null) {
176 mFastTrack.dismiss();
177 }
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700178 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700179
180 /**
181 * Show a {@link FastTrackWindow} for the given aggregate at the requested
182 * screen location.
183 */
184 private void showFastTrack(Uri aggUri, int y) {
185 // Use our local window token for now
186 final IBinder windowToken = findViewById(android.R.id.empty).getWindowToken();
187 FakeView fakeView = new FakeView(this, windowToken);
188
189 final MappingCache mappingCache = MappingCache.createAndFill(this);
190
191 mFastTrack = new FastTrackWindow(this, fakeView, aggUri, mappingCache);
192 mFastTrack.showAt(0, y);
193 }
194
195 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
196 if (cursor == null) {
197 return;
198 }
199
200 // Count contacts found by query
201 int count = 0;
202 long aggId = -1;
203 try {
204 count = cursor.getCount();
205 if (count == 1 && cursor.moveToFirst()) {
206 // Try reading ID if only one contact returned
207 aggId = cursor.getLong(AGGREGATE_ID_INDEX);
208 }
209 } finally {
210 cursor.close();
211 }
212
213 if (count == 1 && aggId != -1) {
214 // If we only found one item, jump right to viewing it
215 final Uri aggUri = ContentUris.withAppendedId(Aggregates.CONTENT_URI, aggId);
216 final int y = getIntent().getExtras().getInt(EXTRA_Y, DEFAULT_Y);
217 showFastTrack(aggUri, y);
218
219// Intent viewIntent = new Intent(Intent.ACTION_VIEW,
220// ContentUris.withAppendedId(People.CONTENT_URI, personId));
221// activity.startActivity(viewIntent);
222// activity.finish();
223
224 } else if (count > 1) {
225 // If more than one, show pick list
226 Intent listIntent = new Intent(Intent.ACTION_SEARCH);
227 listIntent.setComponent(new ComponentName(this, ContactsListActivity.class));
228 listIntent.putExtras(mCreateExtras);
229 startActivity(listIntent);
230 finish();
231
232 } else {
233 // No matching contacts found
234 if (mCreateForce) {
235 // Forced to create new contact
236 Intent createIntent = new Intent(Intent.ACTION_INSERT, People.CONTENT_URI);
237 createIntent.putExtras(mCreateExtras);
238 createIntent.setType(People.CONTENT_TYPE);
239
240 startActivity(createIntent);
241 finish();
242
243 } else {
244 // Prompt user to insert or edit contact
245 Intent createIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
246 createIntent.putExtras(mCreateExtras);
247 createIntent.setType(People.CONTENT_ITEM_TYPE);
248
249 CharSequence message = getResources().getString(
250 R.string.add_contact_dlg_message_fmt, mCreateDescrip);
251
252 new AlertDialog.Builder(this)
253 .setTitle(R.string.add_contact_dlg_title)
254 .setMessage(message)
255 .setPositiveButton(android.R.string.ok,
256 new IntentClickListener(this, createIntent))
257 .setNegativeButton(android.R.string.cancel,
258 new IntentClickListener(this, null))
259 .show();
260 }
261 }
262 }
263
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700264 /**
265 * Listener for {@link DialogInterface} that launches a given {@link Intent}
266 * when clicked. When clicked, this also closes the parent using
267 * {@link Activity#finish()}.
268 */
269 private static class IntentClickListener implements DialogInterface.OnClickListener {
270 private Activity mParent;
271 private Intent mIntent;
272
273 /**
274 * @param parent {@link Activity} to use for launching target.
275 * @param intent Target {@link Intent} to launch when clicked.
276 */
277 public IntentClickListener(Activity parent, Intent intent) {
278 mParent = parent;
279 mIntent = intent;
280 }
281
282 public void onClick(DialogInterface dialog, int which) {
283 if (mIntent != null) {
284 mParent.startActivity(mIntent);
285 }
286 mParent.finish();
287 }
288 }
289
290 /**
Jeff Sharkey3f177592009-05-18 15:23:12 -0700291 * Fake view that simply exists to pass through a specific {@link IBinder}
292 * window token.
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700293 */
Jeff Sharkey3f177592009-05-18 15:23:12 -0700294 private static class FakeView extends View {
295 private IBinder mWindowToken;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700296
Jeff Sharkey3f177592009-05-18 15:23:12 -0700297 public FakeView(Context context, IBinder windowToken) {
298 super(context);
299 mWindowToken = windowToken;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700300 }
301
302 @Override
Jeff Sharkey3f177592009-05-18 15:23:12 -0700303 public IBinder getWindowToken() {
304 return mWindowToken;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700305 }
306 }
307}