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