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