blob: 91c93b99e00d479fa4c0e460a9a7ff9c069b62ee [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
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070037import com.android.contacts.ContactsActivity;
Gary Mai0a49afa2016-12-05 15:53:58 -080038import com.android.contacts.ContactsUtils;
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070039import com.android.contacts.R;
Gary Mai69c182a2016-12-05 13:07:03 -080040import com.android.contacts.util.ImplicitIntentsUtil;
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070041import com.android.contacts.util.NotifyingAsyncQueryHandler;
42
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070043/**
44 * Handle several edge cases around showing or possibly creating contacts in
45 * connected with a specific E-mail address or phone number. Will search based
46 * on incoming {@link Intent#getData()} as described by
Jeff Sharkey73714ff2009-08-23 22:13:04 -070047 * {@link Intents#SHOW_OR_CREATE_CONTACT}.
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.
Jeff Sharkeye8971622009-09-17 15:42:36 -070052 * <li>If one matching contact found, directly show {@link Intent#ACTION_VIEW}
53 * that specific contact.
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070054 * <li>If more than one matching found, show list of matching contacts using
55 * {@link Intent#ACTION_SEARCH}.
56 * </ul>
57 */
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080058public final class ShowOrCreateActivity extends ContactsActivity
59 implements NotifyingAsyncQueryHandler.AsyncQueryListener {
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070060 static final String TAG = "ShowOrCreateActivity";
61 static final boolean LOGD = false;
62
63 static final String[] PHONES_PROJECTION = new String[] {
Jeff Sharkey39261272009-06-03 19:15:09 -070064 PhoneLookup._ID,
Dmitri Plotnikov7d402242011-01-09 16:52:08 -080065 PhoneLookup.LOOKUP_KEY,
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070066 };
67
Jeff Sharkey3f177592009-05-18 15:23:12 -070068 static final String[] CONTACTS_PROJECTION = new String[] {
Dmitri Plotnikov7d402242011-01-09 16:52:08 -080069 Email.CONTACT_ID,
70 Email.LOOKUP_KEY,
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070071 };
Jeff Sharkey549aa162009-05-21 01:33:30 -070072
Jeff Sharkeye8971622009-09-17 15:42:36 -070073 static final int CONTACT_ID_INDEX = 0;
Dmitri Plotnikov7d402242011-01-09 16:52:08 -080074 static final int LOOKUP_KEY_INDEX = 1;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070075
Megha Joshifdaff642009-09-23 03:21:07 -070076 static final int CREATE_CONTACT_DIALOG = 1;
77
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070078 static final int QUERY_TOKEN = 42;
Jeff Sharkey549aa162009-05-21 01:33:30 -070079
Jeff Sharkey3f177592009-05-18 15:23:12 -070080 private NotifyingAsyncQueryHandler mQueryHandler;
81
82 private Bundle mCreateExtras;
83 private String mCreateDescrip;
84 private boolean mCreateForce;
85
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070086 @Override
87 protected void onCreate(Bundle icicle) {
88 super.onCreate(icicle);
Jeff Sharkey3f177592009-05-18 15:23:12 -070089
Brian Attwellbdd32642015-05-08 17:03:15 -070090 if (RequestPermissionsActivity.startPermissionActivity(this)) {
91 return;
92 }
93
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070094 // Create handler if doesn't exist, otherwise cancel any running
95 if (mQueryHandler == null) {
Jeff Sharkey3f177592009-05-18 15:23:12 -070096 mQueryHandler = new NotifyingAsyncQueryHandler(this, this);
The Android Open Source Project37a16ac2009-03-18 17:39:48 -070097 } else {
98 mQueryHandler.cancelOperation(QUERY_TOKEN);
99 }
100
101 final Intent intent = getIntent();
102 final Uri data = intent.getData();
Jeff Sharkey3f177592009-05-18 15:23:12 -0700103
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700104 // Unpack scheme and target data from intent
105 String scheme = null;
106 String ssp = null;
107 if (data != null) {
108 scheme = data.getScheme();
109 ssp = data.getSchemeSpecificPart();
110 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700111
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700112 // Build set of extras for possible use when creating contact
Jeff Sharkey3f177592009-05-18 15:23:12 -0700113 mCreateExtras = new Bundle();
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700114 Bundle originalExtras = intent.getExtras();
115 if (originalExtras != null) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700116 mCreateExtras.putAll(originalExtras);
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 // Read possible extra with specific title
Jeff Sharkey549aa162009-05-21 01:33:30 -0700120 mCreateDescrip = intent.getStringExtra(Intents.EXTRA_CREATE_DESCRIPTION);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700121 if (mCreateDescrip == null) {
122 mCreateDescrip = ssp;
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700123 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700124
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700125 // Allow caller to bypass dialog prompt
Jeff Sharkey3f177592009-05-18 15:23:12 -0700126 mCreateForce = intent.getBooleanExtra(Intents.EXTRA_FORCE_CREATE, false);
127
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700128 // Handle specific query request
Jay Shrauner1cd88e32014-09-05 15:37:55 -0700129 if (ContactsUtils.SCHEME_MAILTO.equals(scheme)) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700130 mCreateExtras.putString(Intents.Insert.EMAIL, ssp);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700131
Jeff Sharkeye8971622009-09-17 15:42:36 -0700132 Uri uri = Uri.withAppendedPath(Email.CONTENT_FILTER_URI, Uri.encode(ssp));
Jeff Sharkey39261272009-06-03 19:15:09 -0700133 mQueryHandler.startQuery(QUERY_TOKEN, null, uri, CONTACTS_PROJECTION, null, null, null);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700134
Jay Shrauner1cd88e32014-09-05 15:37:55 -0700135 } else if (PhoneAccount.SCHEME_TEL.equals(scheme)) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700136 mCreateExtras.putString(Intents.Insert.PHONE, ssp);
Jeff Sharkey39261272009-06-03 19:15:09 -0700137
138 Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, ssp);
139 mQueryHandler.startQuery(QUERY_TOKEN, null, uri, PHONES_PROJECTION, null, null, null);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700140
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700141 } else {
Jeff Sharkeye8971622009-09-17 15:42:36 -0700142 Log.w(TAG, "Invalid intent:" + getIntent());
143 finish();
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700144 }
145 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700146
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700147 @Override
148 protected void onStop() {
149 super.onStop();
150 if (mQueryHandler != null) {
151 mQueryHandler.cancelOperation(QUERY_TOKEN);
152 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700153 }
154
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -0700155 /** {@inheritDoc} */
Jeff Sharkey3f177592009-05-18 15:23:12 -0700156 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
157 if (cursor == null) {
Jeff Sharkey24097052009-08-24 15:27:31 -0700158 // Bail when problem running query in background
159 finish();
Jeff Sharkey3f177592009-05-18 15:23:12 -0700160 return;
161 }
162
163 // Count contacts found by query
164 int count = 0;
Jeff Sharkeye8971622009-09-17 15:42:36 -0700165 long contactId = -1;
Dmitri Plotnikov7d402242011-01-09 16:52:08 -0800166 String lookupKey = null;
Jeff Sharkey3f177592009-05-18 15:23:12 -0700167 try {
168 count = cursor.getCount();
169 if (count == 1 && cursor.moveToFirst()) {
170 // Try reading ID if only one contact returned
Jeff Sharkeye8971622009-09-17 15:42:36 -0700171 contactId = cursor.getLong(CONTACT_ID_INDEX);
Dmitri Plotnikov7d402242011-01-09 16:52:08 -0800172 lookupKey = cursor.getString(LOOKUP_KEY_INDEX);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700173 }
174 } finally {
175 cursor.close();
176 }
177
Jay Shraunere320c0b2015-03-05 12:45:18 -0800178 if (count == 1 && contactId != -1 && !TextUtils.isEmpty(lookupKey)) {
Jeff Sharkeye8971622009-09-17 15:42:36 -0700179 // If we only found one item, jump right to viewing it
Dmitri Plotnikov7d402242011-01-09 16:52:08 -0800180 final Uri contactUri = Contacts.getLookupUri(contactId, lookupKey);
Jeff Sharkeye8971622009-09-17 15:42:36 -0700181 final Intent viewIntent = new Intent(Intent.ACTION_VIEW, contactUri);
Brian Attwellc6100ff2015-02-19 21:35:36 -0800182 ImplicitIntentsUtil.startActivityInApp(this, viewIntent);
Jeff Sharkeye8971622009-09-17 15:42:36 -0700183 finish();
Jeff Sharkey3f177592009-05-18 15:23:12 -0700184
Jeff Sharkey3f177592009-05-18 15:23:12 -0700185 } else if (count > 1) {
186 // If more than one, show pick list
187 Intent listIntent = new Intent(Intent.ACTION_SEARCH);
Katherine Kuan9856fce2011-06-01 10:24:09 -0700188 listIntent.setComponent(new ComponentName(this, PeopleActivity.class));
Jeff Sharkey3f177592009-05-18 15:23:12 -0700189 listIntent.putExtras(mCreateExtras);
190 startActivity(listIntent);
191 finish();
192
193 } else {
194 // No matching contacts found
195 if (mCreateForce) {
196 // Forced to create new contact
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700197 Intent createIntent = new Intent(Intent.ACTION_INSERT, RawContacts.CONTENT_URI);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700198 createIntent.putExtras(mCreateExtras);
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700199 createIntent.setType(RawContacts.CONTENT_TYPE);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700200
Brian Attwellc6100ff2015-02-19 21:35:36 -0800201 ImplicitIntentsUtil.startActivityInApp(this, createIntent);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700202 finish();
203
204 } else {
Dmitri Plotnikov7d402242011-01-09 16:52:08 -0800205 showDialog(CREATE_CONTACT_DIALOG);
206 }
Megha Joshifdaff642009-09-23 03:21:07 -0700207 }
208 }
209
210 @Override
211 protected Dialog onCreateDialog(int id) {
212 switch(id) {
213 case CREATE_CONTACT_DIALOG:
Jeff Sharkey3f177592009-05-18 15:23:12 -0700214 // Prompt user to insert or edit contact
Megha Joshifdaff642009-09-23 03:21:07 -0700215 final Intent createIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700216 createIntent.putExtras(mCreateExtras);
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700217 createIntent.setType(RawContacts.CONTENT_ITEM_TYPE);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700218
Megha Joshifdaff642009-09-23 03:21:07 -0700219 final CharSequence message = getResources().getString(
Jeff Sharkey3f177592009-05-18 15:23:12 -0700220 R.string.add_contact_dlg_message_fmt, mCreateDescrip);
221
Brian Attwell53e42012014-10-22 11:15:14 -0700222 return new AlertDialog.Builder(this)
Jeff Sharkey3f177592009-05-18 15:23:12 -0700223 .setMessage(message)
224 .setPositiveButton(android.R.string.ok,
225 new IntentClickListener(this, createIntent))
226 .setNegativeButton(android.R.string.cancel,
227 new IntentClickListener(this, null))
Makoto Onuki3b10fd02010-11-15 16:12:12 -0800228 .setOnCancelListener(new DialogInterface.OnCancelListener() {
229 @Override
230 public void onCancel(DialogInterface dialog) {
231 finish(); // Close the activity.
232 }})
Megha Joshifdaff642009-09-23 03:21:07 -0700233 .create();
Jeff Sharkey3f177592009-05-18 15:23:12 -0700234 }
Megha Joshifdaff642009-09-23 03:21:07 -0700235 return super.onCreateDialog(id);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700236 }
237
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700238 /**
239 * Listener for {@link DialogInterface} that launches a given {@link Intent}
240 * when clicked. When clicked, this also closes the parent using
241 * {@link Activity#finish()}.
242 */
243 private static class IntentClickListener implements DialogInterface.OnClickListener {
244 private Activity mParent;
245 private Intent mIntent;
246
247 /**
248 * @param parent {@link Activity} to use for launching target.
249 * @param intent Target {@link Intent} to launch when clicked.
250 */
251 public IntentClickListener(Activity parent, Intent intent) {
252 mParent = parent;
253 mIntent = intent;
254 }
255
256 public void onClick(DialogInterface dialog, int which) {
257 if (mIntent != null) {
Brian Attwellc6100ff2015-02-19 21:35:36 -0800258 ImplicitIntentsUtil.startActivityInApp(mParent, mIntent);
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700259 }
260 mParent.finish();
261 }
262 }
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700263}