blob: 6a516ad1b41b100812eca6273e188abe68de02d2 [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
Daniel Lehmann72af89f2010-10-18 15:15:59 -070017package com.android.contacts.activities;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070018
19import android.app.Activity;
20import android.app.AlertDialog;
Megha Joshifdaff642009-09-23 03:21:07 -070021import android.app.Dialog;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070022import android.content.ComponentName;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070023import android.content.DialogInterface;
24import android.content.Intent;
25import android.database.Cursor;
26import android.net.Uri;
27import android.os.Bundle;
Daniel Lehmannafeae642010-10-18 14:40:53 -070028import android.provider.ContactsContract.CommonDataKinds.Email;
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -070029import android.provider.ContactsContract.Contacts;
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -070030import android.provider.ContactsContract.Intents;
Jeff Sharkey39261272009-06-03 19:15:09 -070031import android.provider.ContactsContract.PhoneLookup;
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -070032import android.provider.ContactsContract.RawContacts;
Tyler Gunn03192222014-09-10 15:20:09 -070033import android.telecom.PhoneAccount;
Jay Shraunere320c0b2015-03-05 12:45:18 -080034import android.text.TextUtils;
Jeff Sharkey73714ff2009-08-23 22:13:04 -070035import android.util.Log;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070036
Jay Shrauner1cd88e32014-09-05 15:37:55 -070037import com.android.contacts.common.ContactsUtils;
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070038import com.android.contacts.ContactsActivity;
39import com.android.contacts.R;
Brian Attwellbdd32642015-05-08 17:03:15 -070040import com.android.contacts.common.activity.RequestPermissionsActivity;
Brian Attwellc6100ff2015-02-19 21:35:36 -080041import com.android.contacts.common.util.ImplicitIntentsUtil;
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070042import com.android.contacts.util.NotifyingAsyncQueryHandler;
43
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070044/**
45 * Handle several edge cases around showing or possibly creating contacts in
46 * connected with a specific E-mail address or phone number. Will search based
47 * on incoming {@link Intent#getData()} as described by
Jeff Sharkey73714ff2009-08-23 22:13:04 -070048 * {@link Intents#SHOW_OR_CREATE_CONTACT}.
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070049 * <ul>
50 * <li>If no matching contacts found, will prompt user with dialog to add to a
51 * contact, then will use {@link Intent#ACTION_INSERT_OR_EDIT} to let create new
52 * contact or edit new data into an existing one.
Jeff Sharkeye8971622009-09-17 15:42:36 -070053 * <li>If one matching contact found, directly show {@link Intent#ACTION_VIEW}
54 * that specific contact.
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070055 * <li>If more than one matching found, show list of matching contacts using
56 * {@link Intent#ACTION_SEARCH}.
57 * </ul>
58 */
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080059public final class ShowOrCreateActivity extends ContactsActivity
60 implements NotifyingAsyncQueryHandler.AsyncQueryListener {
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070061 static final String TAG = "ShowOrCreateActivity";
62 static final boolean LOGD = false;
63
64 static final String[] PHONES_PROJECTION = new String[] {
Jeff Sharkey39261272009-06-03 19:15:09 -070065 PhoneLookup._ID,
Dmitri Plotnikov7d402242011-01-09 16:52:08 -080066 PhoneLookup.LOOKUP_KEY,
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070067 };
68
Jeff Sharkey3f177592009-05-18 15:23:12 -070069 static final String[] CONTACTS_PROJECTION = new String[] {
Dmitri Plotnikov7d402242011-01-09 16:52:08 -080070 Email.CONTACT_ID,
71 Email.LOOKUP_KEY,
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070072 };
Jeff Sharkey549aa162009-05-21 01:33:30 -070073
Jeff Sharkeye8971622009-09-17 15:42:36 -070074 static final int CONTACT_ID_INDEX = 0;
Dmitri Plotnikov7d402242011-01-09 16:52:08 -080075 static final int LOOKUP_KEY_INDEX = 1;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070076
Megha Joshifdaff642009-09-23 03:21:07 -070077 static final int CREATE_CONTACT_DIALOG = 1;
78
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070079 static final int QUERY_TOKEN = 42;
Jeff Sharkey549aa162009-05-21 01:33:30 -070080
Jeff Sharkey3f177592009-05-18 15:23:12 -070081 private NotifyingAsyncQueryHandler mQueryHandler;
82
83 private Bundle mCreateExtras;
84 private String mCreateDescrip;
85 private boolean mCreateForce;
86
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070087 @Override
88 protected void onCreate(Bundle icicle) {
89 super.onCreate(icicle);
Jeff Sharkey3f177592009-05-18 15:23:12 -070090
Brian Attwellbdd32642015-05-08 17:03:15 -070091 if (RequestPermissionsActivity.startPermissionActivity(this)) {
92 return;
93 }
94
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
Jay Shrauner1cd88e32014-09-05 15:37:55 -0700130 if (ContactsUtils.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
Jeff Sharkeye8971622009-09-17 15:42:36 -0700133 Uri uri = Uri.withAppendedPath(Email.CONTENT_FILTER_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
Jay Shrauner1cd88e32014-09-05 15:37:55 -0700136 } else if (PhoneAccount.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 Sharkeye8971622009-09-17 15:42:36 -0700143 Log.w(TAG, "Invalid intent:" + getIntent());
144 finish();
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700145 }
146 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700147
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700148 @Override
149 protected void onStop() {
150 super.onStop();
151 if (mQueryHandler != null) {
152 mQueryHandler.cancelOperation(QUERY_TOKEN);
153 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700154 }
155
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -0700156 /** {@inheritDoc} */
Jeff Sharkey3f177592009-05-18 15:23:12 -0700157 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
158 if (cursor == null) {
Jeff Sharkey24097052009-08-24 15:27:31 -0700159 // Bail when problem running query in background
160 finish();
Jeff Sharkey3f177592009-05-18 15:23:12 -0700161 return;
162 }
163
164 // Count contacts found by query
165 int count = 0;
Jeff Sharkeye8971622009-09-17 15:42:36 -0700166 long contactId = -1;
Dmitri Plotnikov7d402242011-01-09 16:52:08 -0800167 String lookupKey = null;
Jeff Sharkey3f177592009-05-18 15:23:12 -0700168 try {
169 count = cursor.getCount();
170 if (count == 1 && cursor.moveToFirst()) {
171 // Try reading ID if only one contact returned
Jeff Sharkeye8971622009-09-17 15:42:36 -0700172 contactId = cursor.getLong(CONTACT_ID_INDEX);
Dmitri Plotnikov7d402242011-01-09 16:52:08 -0800173 lookupKey = cursor.getString(LOOKUP_KEY_INDEX);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700174 }
175 } finally {
176 cursor.close();
177 }
178
Jay Shraunere320c0b2015-03-05 12:45:18 -0800179 if (count == 1 && contactId != -1 && !TextUtils.isEmpty(lookupKey)) {
Jeff Sharkeye8971622009-09-17 15:42:36 -0700180 // If we only found one item, jump right to viewing it
Dmitri Plotnikov7d402242011-01-09 16:52:08 -0800181 final Uri contactUri = Contacts.getLookupUri(contactId, lookupKey);
Jeff Sharkeye8971622009-09-17 15:42:36 -0700182 final Intent viewIntent = new Intent(Intent.ACTION_VIEW, contactUri);
Brian Attwellc6100ff2015-02-19 21:35:36 -0800183 ImplicitIntentsUtil.startActivityInApp(this, viewIntent);
Jeff Sharkeye8971622009-09-17 15:42:36 -0700184 finish();
Jeff Sharkey3f177592009-05-18 15:23:12 -0700185
Jeff Sharkey3f177592009-05-18 15:23:12 -0700186 } else if (count > 1) {
187 // If more than one, show pick list
188 Intent listIntent = new Intent(Intent.ACTION_SEARCH);
Katherine Kuan9856fce2011-06-01 10:24:09 -0700189 listIntent.setComponent(new ComponentName(this, PeopleActivity.class));
Jeff Sharkey3f177592009-05-18 15:23:12 -0700190 listIntent.putExtras(mCreateExtras);
191 startActivity(listIntent);
192 finish();
193
194 } else {
195 // No matching contacts found
196 if (mCreateForce) {
197 // Forced to create new contact
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700198 Intent createIntent = new Intent(Intent.ACTION_INSERT, RawContacts.CONTENT_URI);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700199 createIntent.putExtras(mCreateExtras);
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700200 createIntent.setType(RawContacts.CONTENT_TYPE);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700201
Brian Attwellc6100ff2015-02-19 21:35:36 -0800202 ImplicitIntentsUtil.startActivityInApp(this, createIntent);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700203 finish();
204
205 } else {
Dmitri Plotnikov7d402242011-01-09 16:52:08 -0800206 showDialog(CREATE_CONTACT_DIALOG);
207 }
Megha Joshifdaff642009-09-23 03:21:07 -0700208 }
209 }
210
211 @Override
212 protected Dialog onCreateDialog(int id) {
213 switch(id) {
214 case CREATE_CONTACT_DIALOG:
Jeff Sharkey3f177592009-05-18 15:23:12 -0700215 // Prompt user to insert or edit contact
Megha Joshifdaff642009-09-23 03:21:07 -0700216 final Intent createIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700217 createIntent.putExtras(mCreateExtras);
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700218 createIntent.setType(RawContacts.CONTENT_ITEM_TYPE);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700219
Megha Joshifdaff642009-09-23 03:21:07 -0700220 final CharSequence message = getResources().getString(
Jeff Sharkey3f177592009-05-18 15:23:12 -0700221 R.string.add_contact_dlg_message_fmt, mCreateDescrip);
222
Brian Attwell53e42012014-10-22 11:15:14 -0700223 return new AlertDialog.Builder(this)
Jeff Sharkey3f177592009-05-18 15:23:12 -0700224 .setMessage(message)
225 .setPositiveButton(android.R.string.ok,
226 new IntentClickListener(this, createIntent))
227 .setNegativeButton(android.R.string.cancel,
228 new IntentClickListener(this, null))
Makoto Onuki3b10fd02010-11-15 16:12:12 -0800229 .setOnCancelListener(new DialogInterface.OnCancelListener() {
230 @Override
231 public void onCancel(DialogInterface dialog) {
232 finish(); // Close the activity.
233 }})
Megha Joshifdaff642009-09-23 03:21:07 -0700234 .create();
Jeff Sharkey3f177592009-05-18 15:23:12 -0700235 }
Megha Joshifdaff642009-09-23 03:21:07 -0700236 return super.onCreateDialog(id);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700237 }
238
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700239 /**
240 * Listener for {@link DialogInterface} that launches a given {@link Intent}
241 * when clicked. When clicked, this also closes the parent using
242 * {@link Activity#finish()}.
243 */
244 private static class IntentClickListener implements DialogInterface.OnClickListener {
245 private Activity mParent;
246 private Intent mIntent;
247
248 /**
249 * @param parent {@link Activity} to use for launching target.
250 * @param intent Target {@link Intent} to launch when clicked.
251 */
252 public IntentClickListener(Activity parent, Intent intent) {
253 mParent = parent;
254 mIntent = intent;
255 }
256
257 public void onClick(DialogInterface dialog, int which) {
258 if (mIntent != null) {
Brian Attwellc6100ff2015-02-19 21:35:36 -0800259 ImplicitIntentsUtil.startActivityInApp(mParent, mIntent);
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700260 }
261 mParent.finish();
262 }
263 }
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700264}