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