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