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 | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 20 | import com.android.providers.contacts2.ContactsContract; |
| 21 | import com.android.providers.contacts2.ContactsContract.Aggregates; |
| 22 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 23 | import android.app.Activity; |
| 24 | import android.app.AlertDialog; |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 25 | import android.content.ComponentName; |
| 26 | import android.content.ContentUris; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 27 | import android.content.Context; |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 28 | import android.content.DialogInterface; |
| 29 | import android.content.Intent; |
| 30 | import android.database.Cursor; |
| 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; |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 34 | import android.provider.Contacts; |
| 35 | import android.provider.Contacts.ContactMethods; |
| 36 | import android.provider.Contacts.ContactMethodsColumns; |
| 37 | import android.provider.Contacts.Intents; |
| 38 | import android.provider.Contacts.People; |
| 39 | import android.provider.Contacts.Phones; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 40 | import android.view.View; |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 41 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 42 | /** |
| 43 | * Handle several edge cases around showing or possibly creating contacts in |
| 44 | * connected with a specific E-mail address or phone number. Will search based |
| 45 | * on incoming {@link Intent#getData()} as described by |
| 46 | * {@link Intents#SHOW_OR_CREATE_CONTACT}. |
Jeff Sharkey | 26c7e73 | 2009-04-01 17:30:46 -0700 | [diff] [blame] | 47 | * |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 48 | * <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. |
| 52 | * <li>If one matching contact found, directly show {@link Intent#ACTION_VIEW} |
| 53 | * that specific contact. |
| 54 | * <li>If more than one matching found, show list of matching contacts using |
| 55 | * {@link Intent#ACTION_SEARCH}. |
| 56 | * </ul> |
| 57 | */ |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 58 | public final class ShowOrCreateActivity extends Activity implements QueryCompleteListener, 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[] { |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 63 | Phones.PERSON_ID, |
| 64 | }; |
| 65 | |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 66 | static final String[] CONTACTS_PROJECTION = new String[] { |
| 67 | ContactsContract.Contacts.AGGREGATE_ID, |
| 68 | // People._ID, |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 69 | }; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 70 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 71 | static final String SCHEME_MAILTO = "mailto"; |
| 72 | static final String SCHEME_TEL = "tel"; |
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 | static final int AGGREGATE_ID_INDEX = 0; |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Query clause to filter {@link ContactMethods#CONTENT_URI} to only search |
| 78 | * {@link Contacts#KIND_EMAIL} or {@link Contacts#KIND_IM}. |
| 79 | */ |
| 80 | static final String QUERY_KIND_EMAIL_OR_IM = ContactMethodsColumns.KIND + |
| 81 | " IN (" + Contacts.KIND_EMAIL + "," + Contacts.KIND_IM + ")"; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 82 | |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 83 | /** |
| 84 | * Extra used to request a specific {@link FastTrackWindow} position. |
| 85 | */ |
Jeff Sharkey | 80a193a | 2009-05-21 14:18:18 -0700 | [diff] [blame^] | 86 | private static final String EXTRA_X = "pixel_x"; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 87 | private static final String EXTRA_Y = "pixel_y"; |
Jeff Sharkey | 80a193a | 2009-05-21 14:18:18 -0700 | [diff] [blame^] | 88 | private static final String EXTRA_HEIGHT = "pixel_height"; |
| 89 | private static final int DEFAULT_X = 0; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 90 | private static final int DEFAULT_Y = 90; |
Jeff Sharkey | 80a193a | 2009-05-21 14:18:18 -0700 | [diff] [blame^] | 91 | private static final int DEFAULT_HEIGHT = 30; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 92 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 93 | static final int QUERY_TOKEN = 42; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 94 | |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 95 | private NotifyingAsyncQueryHandler mQueryHandler; |
| 96 | |
| 97 | private Bundle mCreateExtras; |
| 98 | private String mCreateDescrip; |
| 99 | private boolean mCreateForce; |
| 100 | |
| 101 | private FastTrackWindow mFastTrack; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 102 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 103 | @Override |
| 104 | protected void onCreate(Bundle icicle) { |
| 105 | super.onCreate(icicle); |
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 | // Create handler if doesn't exist, otherwise cancel any running |
| 108 | if (mQueryHandler == null) { |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 109 | mQueryHandler = new NotifyingAsyncQueryHandler(this, this); |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 110 | } else { |
| 111 | mQueryHandler.cancelOperation(QUERY_TOKEN); |
| 112 | } |
| 113 | |
| 114 | final Intent intent = getIntent(); |
| 115 | final Uri data = intent.getData(); |
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 | // Unpack scheme and target data from intent |
| 118 | String scheme = null; |
| 119 | String ssp = null; |
| 120 | if (data != null) { |
| 121 | scheme = data.getScheme(); |
| 122 | ssp = data.getSchemeSpecificPart(); |
| 123 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 124 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 125 | // Build set of extras for possible use when creating contact |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 126 | mCreateExtras = new Bundle(); |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 127 | Bundle originalExtras = intent.getExtras(); |
| 128 | if (originalExtras != null) { |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 129 | mCreateExtras.putAll(originalExtras); |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 130 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 131 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 132 | // Read possible extra with specific title |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 133 | mCreateDescrip = intent.getStringExtra(Intents.EXTRA_CREATE_DESCRIPTION); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 134 | if (mCreateDescrip == null) { |
| 135 | mCreateDescrip = ssp; |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 136 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 137 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 138 | // Allow caller to bypass dialog prompt |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 139 | mCreateForce = intent.getBooleanExtra(Intents.EXTRA_FORCE_CREATE, false); |
| 140 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 141 | // Handle specific query request |
| 142 | if (SCHEME_MAILTO.equals(scheme)) { |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 143 | mCreateExtras.putString(Intents.Insert.EMAIL, ssp); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 144 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 145 | Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_EMAIL_URI, |
| 146 | Uri.encode(ssp)); |
Jeffrey Sharkey | c2158b7 | 2009-03-27 17:57:15 -0700 | [diff] [blame] | 147 | mQueryHandler.startQuery(QUERY_TOKEN, null, uri, |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 148 | CONTACTS_PROJECTION, null, null, null); |
| 149 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 150 | } else if (SCHEME_TEL.equals(scheme)) { |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 151 | mCreateExtras.putString(Intents.Insert.PHONE, ssp); |
| 152 | // mQueryHandler.startQuery(QUERY_TOKEN, null, |
| 153 | // Uri.withAppendedPath(Phones.CONTENT_FILTER_URL, ssp), |
| 154 | // PHONES_PROJECTION, null, null, null); |
| 155 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 156 | } else { |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 157 | // Otherwise assume incoming aggregate Uri |
Jeff Sharkey | 80a193a | 2009-05-21 14:18:18 -0700 | [diff] [blame^] | 158 | showFastTrack(data); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 159 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 162 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 163 | @Override |
| 164 | protected void onStop() { |
| 165 | super.onStop(); |
| 166 | if (mQueryHandler != null) { |
| 167 | mQueryHandler.cancelOperation(QUERY_TOKEN); |
| 168 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 169 | if (mFastTrack != null) { |
| 170 | mFastTrack.dismiss(); |
| 171 | } |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 172 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 173 | |
| 174 | /** |
| 175 | * Show a {@link FastTrackWindow} for the given aggregate at the requested |
| 176 | * screen location. |
| 177 | */ |
Jeff Sharkey | 80a193a | 2009-05-21 14:18:18 -0700 | [diff] [blame^] | 178 | private void showFastTrack(Uri aggUri) { |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 179 | // Use our local window token for now |
Jeff Sharkey | 80a193a | 2009-05-21 14:18:18 -0700 | [diff] [blame^] | 180 | Bundle extras = getIntent().getExtras(); |
| 181 | final int x = extras.getInt(EXTRA_X, DEFAULT_X); |
| 182 | final int y = extras.getInt(EXTRA_Y, DEFAULT_Y); |
| 183 | final int height = extras.getInt(EXTRA_HEIGHT, DEFAULT_HEIGHT); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 184 | mFastTrack = new FastTrackWindow(this, this); |
Jeff Sharkey | 80a193a | 2009-05-21 14:18:18 -0700 | [diff] [blame^] | 185 | mFastTrack.show(aggUri, x, y, height); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 186 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 187 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 188 | /** {@inheritDoc} */ |
| 189 | public void onDismiss(FastTrackWindow dialog) { |
| 190 | // When dismissed, finish this activity |
| 191 | finish(); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | public void onQueryComplete(int token, Object cookie, Cursor cursor) { |
| 195 | if (cursor == null) { |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | // Count contacts found by query |
| 200 | int count = 0; |
| 201 | long aggId = -1; |
| 202 | try { |
| 203 | count = cursor.getCount(); |
| 204 | if (count == 1 && cursor.moveToFirst()) { |
| 205 | // Try reading ID if only one contact returned |
| 206 | aggId = cursor.getLong(AGGREGATE_ID_INDEX); |
| 207 | } |
| 208 | } finally { |
| 209 | cursor.close(); |
| 210 | } |
| 211 | |
| 212 | if (count == 1 && aggId != -1) { |
| 213 | // If we only found one item, jump right to viewing it |
| 214 | final Uri aggUri = ContentUris.withAppendedId(Aggregates.CONTENT_URI, aggId); |
Jeff Sharkey | 80a193a | 2009-05-21 14:18:18 -0700 | [diff] [blame^] | 215 | showFastTrack(aggUri); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 216 | |
| 217 | // Intent viewIntent = new Intent(Intent.ACTION_VIEW, |
| 218 | // ContentUris.withAppendedId(People.CONTENT_URI, personId)); |
| 219 | // activity.startActivity(viewIntent); |
| 220 | // activity.finish(); |
| 221 | |
| 222 | } else if (count > 1) { |
| 223 | // If more than one, show pick list |
| 224 | Intent listIntent = new Intent(Intent.ACTION_SEARCH); |
| 225 | listIntent.setComponent(new ComponentName(this, ContactsListActivity.class)); |
| 226 | listIntent.putExtras(mCreateExtras); |
| 227 | startActivity(listIntent); |
| 228 | finish(); |
| 229 | |
| 230 | } else { |
| 231 | // No matching contacts found |
| 232 | if (mCreateForce) { |
| 233 | // Forced to create new contact |
| 234 | Intent createIntent = new Intent(Intent.ACTION_INSERT, People.CONTENT_URI); |
| 235 | createIntent.putExtras(mCreateExtras); |
| 236 | createIntent.setType(People.CONTENT_TYPE); |
| 237 | |
| 238 | startActivity(createIntent); |
| 239 | finish(); |
| 240 | |
| 241 | } else { |
| 242 | // Prompt user to insert or edit contact |
| 243 | Intent createIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT); |
| 244 | createIntent.putExtras(mCreateExtras); |
| 245 | createIntent.setType(People.CONTENT_ITEM_TYPE); |
| 246 | |
| 247 | CharSequence message = getResources().getString( |
| 248 | R.string.add_contact_dlg_message_fmt, mCreateDescrip); |
| 249 | |
| 250 | new AlertDialog.Builder(this) |
| 251 | .setTitle(R.string.add_contact_dlg_title) |
| 252 | .setMessage(message) |
| 253 | .setPositiveButton(android.R.string.ok, |
| 254 | new IntentClickListener(this, createIntent)) |
| 255 | .setNegativeButton(android.R.string.cancel, |
| 256 | new IntentClickListener(this, null)) |
| 257 | .show(); |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 262 | /** |
| 263 | * Listener for {@link DialogInterface} that launches a given {@link Intent} |
| 264 | * when clicked. When clicked, this also closes the parent using |
| 265 | * {@link Activity#finish()}. |
| 266 | */ |
| 267 | private static class IntentClickListener implements DialogInterface.OnClickListener { |
| 268 | private Activity mParent; |
| 269 | private Intent mIntent; |
| 270 | |
| 271 | /** |
| 272 | * @param parent {@link Activity} to use for launching target. |
| 273 | * @param intent Target {@link Intent} to launch when clicked. |
| 274 | */ |
| 275 | public IntentClickListener(Activity parent, Intent intent) { |
| 276 | mParent = parent; |
| 277 | mIntent = intent; |
| 278 | } |
| 279 | |
| 280 | public void onClick(DialogInterface dialog, int which) { |
| 281 | if (mIntent != null) { |
| 282 | mParent.startActivity(mIntent); |
| 283 | } |
| 284 | mParent.finish(); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /** |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 289 | * Fake view that simply exists to pass through a specific {@link IBinder} |
| 290 | * window token. |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 291 | */ |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 292 | private static class FakeView extends View { |
| 293 | private IBinder mWindowToken; |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 294 | |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 295 | public FakeView(Context context, IBinder windowToken) { |
| 296 | super(context); |
| 297 | mWindowToken = windowToken; |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | @Override |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 301 | public IBinder getWindowToken() { |
| 302 | return mWindowToken; |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | } |