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 | |
| 19 | import android.app.Activity; |
| 20 | import android.app.AlertDialog; |
| 21 | import android.content.AsyncQueryHandler; |
| 22 | import android.content.ComponentName; |
| 23 | import android.content.ContentUris; |
| 24 | import android.content.DialogInterface; |
| 25 | import android.content.Intent; |
| 26 | import android.database.Cursor; |
| 27 | import android.net.Uri; |
| 28 | import android.os.Bundle; |
| 29 | import android.provider.Contacts; |
| 30 | import android.provider.Contacts.ContactMethods; |
| 31 | import android.provider.Contacts.ContactMethodsColumns; |
| 32 | import android.provider.Contacts.Intents; |
| 33 | import android.provider.Contacts.People; |
| 34 | import android.provider.Contacts.Phones; |
| 35 | import android.util.Log; |
| 36 | |
| 37 | import java.lang.ref.WeakReference; |
| 38 | |
| 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 |
| 43 | * {@link 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 | */ |
| 55 | public final class ShowOrCreateActivity extends Activity { |
| 56 | static final String TAG = "ShowOrCreateActivity"; |
| 57 | static final boolean LOGD = false; |
| 58 | |
| 59 | static final String[] PHONES_PROJECTION = new String[] { |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 60 | Phones.PERSON_ID, |
| 61 | }; |
| 62 | |
Jeffrey Sharkey | c2158b7 | 2009-03-27 17:57:15 -0700 | [diff] [blame] | 63 | static final String[] PEOPLE_PROJECTION = new String[] { |
| 64 | People._ID, |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | static final String SCHEME_MAILTO = "mailto"; |
| 68 | static final String SCHEME_TEL = "tel"; |
| 69 | |
Jeffrey Sharkey | c2158b7 | 2009-03-27 17:57:15 -0700 | [diff] [blame] | 70 | static final int PERSON_ID_INDEX = 0; |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * Query clause to filter {@link ContactMethods#CONTENT_URI} to only search |
| 74 | * {@link Contacts#KIND_EMAIL} or {@link Contacts#KIND_IM}. |
| 75 | */ |
| 76 | static final String QUERY_KIND_EMAIL_OR_IM = ContactMethodsColumns.KIND + |
| 77 | " IN (" + Contacts.KIND_EMAIL + "," + Contacts.KIND_IM + ")"; |
| 78 | |
| 79 | static final int QUERY_TOKEN = 42; |
| 80 | |
| 81 | private QueryHandler mQueryHandler; |
| 82 | |
| 83 | @Override |
| 84 | protected void onCreate(Bundle icicle) { |
| 85 | super.onCreate(icicle); |
| 86 | |
| 87 | // Create handler if doesn't exist, otherwise cancel any running |
| 88 | if (mQueryHandler == null) { |
| 89 | mQueryHandler = new QueryHandler(this); |
| 90 | } else { |
| 91 | mQueryHandler.cancelOperation(QUERY_TOKEN); |
| 92 | } |
| 93 | |
| 94 | final Intent intent = getIntent(); |
| 95 | final Uri data = intent.getData(); |
| 96 | |
| 97 | // Unpack scheme and target data from intent |
| 98 | String scheme = null; |
| 99 | String ssp = null; |
| 100 | if (data != null) { |
| 101 | scheme = data.getScheme(); |
| 102 | ssp = data.getSchemeSpecificPart(); |
| 103 | } |
| 104 | |
| 105 | // Build set of extras for possible use when creating contact |
| 106 | Bundle createExtras = new Bundle(); |
| 107 | Bundle originalExtras = intent.getExtras(); |
| 108 | if (originalExtras != null) { |
| 109 | createExtras.putAll(originalExtras); |
| 110 | } |
| 111 | mQueryHandler.setCreateExtras(createExtras); |
| 112 | |
| 113 | // Read possible extra with specific title |
| 114 | String createDescrip = intent.getStringExtra(Intents.EXTRA_CREATE_DESCRIPTION); |
| 115 | if (createDescrip == null) { |
| 116 | createDescrip = ssp; |
| 117 | } |
| 118 | mQueryHandler.setCreateDescription(createDescrip); |
| 119 | |
| 120 | // Allow caller to bypass dialog prompt |
| 121 | boolean createForce = intent.getBooleanExtra(Intents.EXTRA_FORCE_CREATE, false); |
| 122 | mQueryHandler.setCreateForce(createForce); |
| 123 | |
| 124 | // Handle specific query request |
| 125 | if (SCHEME_MAILTO.equals(scheme)) { |
| 126 | createExtras.putString(Intents.Insert.EMAIL, ssp); |
Jeffrey Sharkey | c2158b7 | 2009-03-27 17:57:15 -0700 | [diff] [blame] | 127 | Uri uri = Uri.withAppendedPath(People.WITH_EMAIL_OR_IM_FILTER_URI, Uri.encode(ssp)); |
| 128 | mQueryHandler.startQuery(QUERY_TOKEN, null, uri, |
| 129 | PEOPLE_PROJECTION, null, null, null); |
The Android Open Source Project | 37a16ac | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 130 | } else if (SCHEME_TEL.equals(scheme)) { |
| 131 | createExtras.putString(Intents.Insert.PHONE, ssp); |
| 132 | mQueryHandler.startQuery(QUERY_TOKEN, null, |
| 133 | Uri.withAppendedPath(Phones.CONTENT_FILTER_URL, ssp), |
| 134 | PHONES_PROJECTION, null, null, null); |
| 135 | |
| 136 | } else { |
| 137 | Log.w(TAG, "Invalid intent:" + getIntent()); |
| 138 | finish(); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | protected void onStop() { |
| 144 | super.onStop(); |
| 145 | if (mQueryHandler != null) { |
| 146 | mQueryHandler.cancelOperation(QUERY_TOKEN); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Listener for {@link DialogInterface} that launches a given {@link Intent} |
| 152 | * when clicked. When clicked, this also closes the parent using |
| 153 | * {@link Activity#finish()}. |
| 154 | */ |
| 155 | private static class IntentClickListener implements DialogInterface.OnClickListener { |
| 156 | private Activity mParent; |
| 157 | private Intent mIntent; |
| 158 | |
| 159 | /** |
| 160 | * @param parent {@link Activity} to use for launching target. |
| 161 | * @param intent Target {@link Intent} to launch when clicked. |
| 162 | */ |
| 163 | public IntentClickListener(Activity parent, Intent intent) { |
| 164 | mParent = parent; |
| 165 | mIntent = intent; |
| 166 | } |
| 167 | |
| 168 | public void onClick(DialogInterface dialog, int which) { |
| 169 | if (mIntent != null) { |
| 170 | mParent.startActivity(mIntent); |
| 171 | } |
| 172 | mParent.finish(); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Handle asynchronous query to find matching contacts. When query finishes, |
| 178 | * will handle based on number of matching contacts found. |
| 179 | */ |
| 180 | private static final class QueryHandler extends AsyncQueryHandler { |
| 181 | private final WeakReference<Activity> mActivity; |
| 182 | private Bundle mCreateExtras; |
| 183 | private String mCreateDescrip; |
| 184 | private boolean mCreateForce; |
| 185 | |
| 186 | public QueryHandler(Activity activity) { |
| 187 | super(activity.getContentResolver()); |
| 188 | mActivity = new WeakReference<Activity>(activity); |
| 189 | } |
| 190 | |
| 191 | public void setCreateExtras(Bundle createExtras) { |
| 192 | mCreateExtras = createExtras; |
| 193 | } |
| 194 | |
| 195 | public void setCreateDescription(String createDescrip) { |
| 196 | mCreateDescrip = createDescrip; |
| 197 | } |
| 198 | |
| 199 | public void setCreateForce(boolean createForce) { |
| 200 | mCreateForce = createForce; |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | protected void onQueryComplete(int token, Object cookie, Cursor cursor) { |
| 205 | Activity activity = mActivity.get(); |
| 206 | if (activity == null) { |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | // Count contacts found by query |
| 211 | int count = 0; |
| 212 | long personId = -1; |
| 213 | if (cursor != null) { |
| 214 | try { |
| 215 | count = cursor.getCount(); |
| 216 | if (count == 1 && cursor.moveToFirst()) { |
| 217 | // Try reading ID if only one contact returned |
| 218 | personId = cursor.getLong(PERSON_ID_INDEX); |
| 219 | } |
| 220 | } finally { |
| 221 | cursor.close(); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | if (LOGD) Log.d(TAG, "onQueryComplete count=" + count); |
| 226 | |
| 227 | if (count == 1) { |
| 228 | // If we only found one item, jump right to viewing it |
| 229 | Intent viewIntent = new Intent(Intent.ACTION_VIEW, |
| 230 | ContentUris.withAppendedId(People.CONTENT_URI, personId)); |
| 231 | activity.startActivity(viewIntent); |
| 232 | activity.finish(); |
| 233 | |
| 234 | } else if (count > 1) { |
| 235 | // If more than one, show pick list |
| 236 | Intent listIntent = new Intent(Intent.ACTION_SEARCH); |
| 237 | listIntent.setComponent(new ComponentName(activity, ContactsListActivity.class)); |
| 238 | listIntent.putExtras(mCreateExtras); |
| 239 | activity.startActivity(listIntent); |
| 240 | activity.finish(); |
| 241 | |
| 242 | } else { |
| 243 | // No matching contacts found |
| 244 | if (mCreateForce) { |
| 245 | // Forced to create new contact |
| 246 | Intent createIntent = new Intent(Intent.ACTION_INSERT, People.CONTENT_URI); |
| 247 | createIntent.putExtras(mCreateExtras); |
| 248 | createIntent.setType(People.CONTENT_TYPE); |
| 249 | |
| 250 | activity.startActivity(createIntent); |
| 251 | activity.finish(); |
| 252 | |
| 253 | } else { |
| 254 | // Prompt user to insert or edit contact |
| 255 | Intent createIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT); |
| 256 | createIntent.putExtras(mCreateExtras); |
| 257 | createIntent.setType(People.CONTENT_ITEM_TYPE); |
| 258 | |
| 259 | CharSequence message = activity.getResources().getString( |
| 260 | R.string.add_contact_dlg_message_fmt, mCreateDescrip); |
| 261 | |
| 262 | new AlertDialog.Builder(activity) |
| 263 | .setTitle(R.string.add_contact_dlg_title) |
| 264 | .setMessage(message) |
| 265 | .setPositiveButton(android.R.string.ok, |
| 266 | new IntentClickListener(activity, createIntent)) |
| 267 | .setNegativeButton(android.R.string.cancel, |
| 268 | new IntentClickListener(activity, null)) |
| 269 | .show(); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | } |