The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.contacts; |
| 18 | |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame^] | 19 | import com.android.contacts.NotifyingAsyncQueryHandler.AsyncQueryListener; |
Jeff Sharkey | 802b205 | 2009-08-04 14:21:06 -0700 | [diff] [blame] | 20 | import com.android.contacts.ui.FastTrackWindow; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 21 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 22 | import android.app.Activity; |
| 23 | import android.app.AlertDialog; |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 24 | import android.content.ComponentName; |
| 25 | import android.content.ContentUris; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 26 | import android.content.Context; |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 27 | import android.content.DialogInterface; |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame^] | 28 | import android.content.EntityIterator; |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 29 | import android.content.Intent; |
| 30 | import android.database.Cursor; |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 31 | import android.graphics.Rect; |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 32 | import android.net.Uri; |
| 33 | import android.os.Bundle; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 34 | import android.os.IBinder; |
Jeff Sharkey | 802b205 | 2009-08-04 14:21:06 -0700 | [diff] [blame] | 35 | import android.provider.ContactsContract.Intents; |
Dmitri Plotnikov | e1cd679 | 2009-07-27 20:28:17 -0700 | [diff] [blame] | 36 | import android.provider.ContactsContract.Contacts; |
Dmitri Plotnikov | 3946659 | 2009-07-27 11:23:51 -0700 | [diff] [blame] | 37 | import android.provider.ContactsContract.RawContacts; |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 38 | import android.provider.ContactsContract.PhoneLookup; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 39 | import android.view.View; |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 40 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 41 | /** |
| 42 | * Handle several edge cases around showing or possibly creating contacts in |
| 43 | * connected with a specific E-mail address or phone number. Will search based |
| 44 | * on incoming {@link Intent#getData()} as described by |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 45 | * {@link android.provider.Contacts.Intents#SHOW_OR_CREATE_CONTACT}. |
Jeff Sharkey | 26c7e73 | 2009-04-01 17:30:46 -0700 | [diff] [blame] | 46 | * |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 47 | * <ul> |
| 48 | * <li>If no matching contacts found, will prompt user with dialog to add to a |
| 49 | * contact, then will use {@link Intent#ACTION_INSERT_OR_EDIT} to let create new |
| 50 | * contact or edit new data into an existing one. |
| 51 | * <li>If one matching contact found, directly show {@link Intent#ACTION_VIEW} |
| 52 | * that specific contact. |
| 53 | * <li>If more than one matching found, show list of matching contacts using |
| 54 | * {@link Intent#ACTION_SEARCH}. |
| 55 | * </ul> |
| 56 | */ |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame^] | 57 | public final class ShowOrCreateActivity extends Activity implements AsyncQueryListener, |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 58 | FastTrackWindow.OnDismissListener { |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 59 | static final String TAG = "ShowOrCreateActivity"; |
| 60 | static final boolean LOGD = false; |
| 61 | |
| 62 | static final String[] PHONES_PROJECTION = new String[] { |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 63 | PhoneLookup._ID, |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 66 | static final String[] CONTACTS_PROJECTION = new String[] { |
Dmitri Plotnikov | e1cd679 | 2009-07-27 20:28:17 -0700 | [diff] [blame] | 67 | RawContacts.CONTACT_ID, |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 68 | }; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 69 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 70 | static final String SCHEME_MAILTO = "mailto"; |
| 71 | static final String SCHEME_TEL = "tel"; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 72 | |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 73 | static final int AGGREGATE_ID_INDEX = 0; |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 74 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 75 | static final int QUERY_TOKEN = 42; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 76 | |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 77 | private NotifyingAsyncQueryHandler mQueryHandler; |
| 78 | |
| 79 | private Bundle mCreateExtras; |
| 80 | private String mCreateDescrip; |
| 81 | private boolean mCreateForce; |
| 82 | |
| 83 | private FastTrackWindow mFastTrack; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 84 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 85 | @Override |
| 86 | protected void onCreate(Bundle icicle) { |
| 87 | super.onCreate(icicle); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 88 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 89 | // Create handler if doesn't exist, otherwise cancel any running |
| 90 | if (mQueryHandler == null) { |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 91 | mQueryHandler = new NotifyingAsyncQueryHandler(this, this); |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 92 | } else { |
| 93 | mQueryHandler.cancelOperation(QUERY_TOKEN); |
| 94 | } |
| 95 | |
| 96 | final Intent intent = getIntent(); |
| 97 | final Uri data = intent.getData(); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 98 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 99 | // Unpack scheme and target data from intent |
| 100 | String scheme = null; |
| 101 | String ssp = null; |
| 102 | if (data != null) { |
| 103 | scheme = data.getScheme(); |
| 104 | ssp = data.getSchemeSpecificPart(); |
| 105 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 106 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 107 | // Build set of extras for possible use when creating contact |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 108 | mCreateExtras = new Bundle(); |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 109 | Bundle originalExtras = intent.getExtras(); |
| 110 | if (originalExtras != null) { |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 111 | mCreateExtras.putAll(originalExtras); |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 112 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 113 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 114 | // Read possible extra with specific title |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 115 | mCreateDescrip = intent.getStringExtra(Intents.EXTRA_CREATE_DESCRIPTION); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 116 | if (mCreateDescrip == null) { |
| 117 | mCreateDescrip = ssp; |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 118 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 119 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 120 | // Allow caller to bypass dialog prompt |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 121 | mCreateForce = intent.getBooleanExtra(Intents.EXTRA_FORCE_CREATE, false); |
| 122 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 123 | // Handle specific query request |
| 124 | if (SCHEME_MAILTO.equals(scheme)) { |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 125 | mCreateExtras.putString(Intents.Insert.EMAIL, ssp); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 126 | |
Dmitri Plotnikov | 3946659 | 2009-07-27 11:23:51 -0700 | [diff] [blame] | 127 | Uri uri = Uri.withAppendedPath(RawContacts.CONTENT_FILTER_EMAIL_URI, Uri.encode(ssp)); |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 128 | mQueryHandler.startQuery(QUERY_TOKEN, null, uri, CONTACTS_PROJECTION, null, null, null); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 129 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 130 | } else if (SCHEME_TEL.equals(scheme)) { |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 131 | mCreateExtras.putString(Intents.Insert.PHONE, ssp); |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 132 | |
| 133 | Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, ssp); |
| 134 | mQueryHandler.startQuery(QUERY_TOKEN, null, uri, PHONES_PROJECTION, null, null, null); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 135 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 136 | } else { |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 137 | // Otherwise assume incoming aggregate Uri |
Jeff Sharkey | 80a193a | 2009-05-21 14:18:18 -0700 | [diff] [blame] | 138 | showFastTrack(data); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 139 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 142 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 143 | @Override |
| 144 | protected void onStop() { |
| 145 | super.onStop(); |
| 146 | if (mQueryHandler != null) { |
| 147 | mQueryHandler.cancelOperation(QUERY_TOKEN); |
| 148 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 149 | if (mFastTrack != null) { |
| 150 | mFastTrack.dismiss(); |
| 151 | } |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 152 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 153 | |
| 154 | /** |
| 155 | * Show a {@link FastTrackWindow} for the given aggregate at the requested |
| 156 | * screen location. |
| 157 | */ |
Jeff Sharkey | 80a193a | 2009-05-21 14:18:18 -0700 | [diff] [blame] | 158 | private void showFastTrack(Uri aggUri) { |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 159 | // Use our local window token for now |
Jeff Sharkey | 802b205 | 2009-08-04 14:21:06 -0700 | [diff] [blame] | 160 | final Bundle extras = getIntent().getExtras(); |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 161 | |
| 162 | Rect targetRect; |
Jeff Sharkey | 802b205 | 2009-08-04 14:21:06 -0700 | [diff] [blame] | 163 | if (extras.containsKey(Intents.EXTRA_TARGET_RECT)) { |
| 164 | targetRect = (Rect)extras.getParcelable(Intents.EXTRA_TARGET_RECT); |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 165 | } else { |
| 166 | // TODO: this default rect matches gmail messages, and should move over there |
| 167 | targetRect = new Rect(15, 110, 15+18, 110+18); |
| 168 | } |
| 169 | |
Jeff Sharkey | 802b205 | 2009-08-04 14:21:06 -0700 | [diff] [blame] | 170 | // Use requested display mode, defaulting to medium |
| 171 | final int mode = extras.getInt(Intents.EXTRA_MODE, Intents.MODE_MEDIUM); |
| 172 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 173 | mFastTrack = new FastTrackWindow(this, this); |
Jeff Sharkey | 802b205 | 2009-08-04 14:21:06 -0700 | [diff] [blame] | 174 | mFastTrack.show(aggUri, targetRect, mode); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 175 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 176 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 177 | /** {@inheritDoc} */ |
| 178 | public void onDismiss(FastTrackWindow dialog) { |
| 179 | // When dismissed, finish this activity |
| 180 | finish(); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | public void onQueryComplete(int token, Object cookie, Cursor cursor) { |
| 184 | if (cursor == null) { |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | // Count contacts found by query |
| 189 | int count = 0; |
| 190 | long aggId = -1; |
| 191 | try { |
| 192 | count = cursor.getCount(); |
| 193 | if (count == 1 && cursor.moveToFirst()) { |
| 194 | // Try reading ID if only one contact returned |
| 195 | aggId = cursor.getLong(AGGREGATE_ID_INDEX); |
| 196 | } |
| 197 | } finally { |
| 198 | cursor.close(); |
| 199 | } |
| 200 | |
| 201 | if (count == 1 && aggId != -1) { |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 202 | // If we only found one item, show fast-track |
Dmitri Plotnikov | e1cd679 | 2009-07-27 20:28:17 -0700 | [diff] [blame] | 203 | final Uri aggUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, aggId); |
Jeff Sharkey | 80a193a | 2009-05-21 14:18:18 -0700 | [diff] [blame] | 204 | showFastTrack(aggUri); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 205 | |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 206 | } else if (count > 1) { |
| 207 | // If more than one, show pick list |
| 208 | Intent listIntent = new Intent(Intent.ACTION_SEARCH); |
| 209 | listIntent.setComponent(new ComponentName(this, ContactsListActivity.class)); |
| 210 | listIntent.putExtras(mCreateExtras); |
| 211 | startActivity(listIntent); |
| 212 | finish(); |
| 213 | |
| 214 | } else { |
| 215 | // No matching contacts found |
| 216 | if (mCreateForce) { |
| 217 | // Forced to create new contact |
Dmitri Plotnikov | 3946659 | 2009-07-27 11:23:51 -0700 | [diff] [blame] | 218 | Intent createIntent = new Intent(Intent.ACTION_INSERT, RawContacts.CONTENT_URI); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 219 | createIntent.putExtras(mCreateExtras); |
Dmitri Plotnikov | 3946659 | 2009-07-27 11:23:51 -0700 | [diff] [blame] | 220 | createIntent.setType(RawContacts.CONTENT_TYPE); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 221 | |
| 222 | startActivity(createIntent); |
| 223 | finish(); |
| 224 | |
| 225 | } else { |
| 226 | // Prompt user to insert or edit contact |
| 227 | Intent createIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT); |
| 228 | createIntent.putExtras(mCreateExtras); |
Dmitri Plotnikov | 3946659 | 2009-07-27 11:23:51 -0700 | [diff] [blame] | 229 | createIntent.setType(RawContacts.CONTENT_ITEM_TYPE); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 230 | |
| 231 | CharSequence message = getResources().getString( |
| 232 | R.string.add_contact_dlg_message_fmt, mCreateDescrip); |
| 233 | |
| 234 | new AlertDialog.Builder(this) |
| 235 | .setTitle(R.string.add_contact_dlg_title) |
| 236 | .setMessage(message) |
| 237 | .setPositiveButton(android.R.string.ok, |
| 238 | new IntentClickListener(this, createIntent)) |
| 239 | .setNegativeButton(android.R.string.cancel, |
| 240 | new IntentClickListener(this, null)) |
| 241 | .show(); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 246 | /** |
| 247 | * Listener for {@link DialogInterface} that launches a given {@link Intent} |
| 248 | * when clicked. When clicked, this also closes the parent using |
| 249 | * {@link Activity#finish()}. |
| 250 | */ |
| 251 | private static class IntentClickListener implements DialogInterface.OnClickListener { |
| 252 | private Activity mParent; |
| 253 | private Intent mIntent; |
| 254 | |
| 255 | /** |
| 256 | * @param parent {@link Activity} to use for launching target. |
| 257 | * @param intent Target {@link Intent} to launch when clicked. |
| 258 | */ |
| 259 | public IntentClickListener(Activity parent, Intent intent) { |
| 260 | mParent = parent; |
| 261 | mIntent = intent; |
| 262 | } |
| 263 | |
| 264 | public void onClick(DialogInterface dialog, int which) { |
| 265 | if (mIntent != null) { |
| 266 | mParent.startActivity(mIntent); |
| 267 | } |
| 268 | mParent.finish(); |
| 269 | } |
| 270 | } |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame^] | 271 | |
| 272 | public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) { |
| 273 | // Empty |
| 274 | } |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 275 | } |