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 17package com.android.contacts.activities; 18 19import com.android.contacts.ContactsActivity; 20import com.android.contacts.R; 21import com.android.contacts.util.Constants; 22import com.android.contacts.util.NotifyingAsyncQueryHandler; 23 24import android.app.Activity; 25import android.app.AlertDialog; 26import android.app.Dialog; 27import android.app.SearchManager; 28import android.content.ComponentName; 29import android.content.DialogInterface; 30import android.content.Intent; 31import android.database.Cursor; 32import android.net.Uri; 33import android.os.Bundle; 34import android.provider.ContactsContract.CommonDataKinds.Email; 35import android.provider.ContactsContract.Contacts; 36import android.provider.ContactsContract.Intents; 37import android.provider.ContactsContract.PhoneLookup; 38import android.provider.ContactsContract.RawContacts; 39import android.util.Log; 40 41/** 42 * Handle several edge cases around showing or possibly creating contacts in 43 * connected with a specific E-mail address or phone number. Will search based 44 * on incoming {@link Intent#getData()} as described by 45 * {@link Intents#SHOW_OR_CREATE_CONTACT}. 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 */ 56public final class ShowOrCreateActivity extends ContactsActivity 57 implements NotifyingAsyncQueryHandler.AsyncQueryListener { 58 static final String TAG = "ShowOrCreateActivity"; 59 static final boolean LOGD = false; 60 61 static final String[] PHONES_PROJECTION = new String[] { 62 PhoneLookup._ID, 63 PhoneLookup.LOOKUP_KEY, 64 }; 65 66 static final String[] CONTACTS_PROJECTION = new String[] { 67 Email.CONTACT_ID, 68 Email.LOOKUP_KEY, 69 }; 70 71 static final int CONTACT_ID_INDEX = 0; 72 static final int LOOKUP_KEY_INDEX = 1; 73 74 static final int CREATE_CONTACT_DIALOG = 1; 75 76 static final int QUERY_TOKEN = 42; 77 78 private NotifyingAsyncQueryHandler mQueryHandler; 79 80 private Bundle mCreateExtras; 81 private String mCreateDescrip; 82 private boolean mCreateForce; 83 84 @Override 85 protected void onCreate(Bundle icicle) { 86 super.onCreate(icicle); 87 88 // Create handler if doesn't exist, otherwise cancel any running 89 if (mQueryHandler == null) { 90 mQueryHandler = new NotifyingAsyncQueryHandler(this, this); 91 } else { 92 mQueryHandler.cancelOperation(QUERY_TOKEN); 93 } 94 95 final Intent intent = getIntent(); 96 final Uri data = intent.getData(); 97 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 } 105 106 // Build set of extras for possible use when creating contact 107 mCreateExtras = new Bundle(); 108 Bundle originalExtras = intent.getExtras(); 109 if (originalExtras != null) { 110 mCreateExtras.putAll(originalExtras); 111 } 112 113 // Read possible extra with specific title 114 mCreateDescrip = intent.getStringExtra(Intents.EXTRA_CREATE_DESCRIPTION); 115 if (mCreateDescrip == null) { 116 mCreateDescrip = ssp; 117 } 118 119 // Allow caller to bypass dialog prompt 120 mCreateForce = intent.getBooleanExtra(Intents.EXTRA_FORCE_CREATE, false); 121 122 // Handle specific query request 123 if (Constants.SCHEME_MAILTO.equals(scheme)) { 124 mCreateExtras.putString(Intents.Insert.EMAIL, ssp); 125 mCreateExtras.putString(SearchManager.QUERY, ssp); 126 127 Uri uri = Uri.withAppendedPath(Email.CONTENT_FILTER_URI, Uri.encode(ssp)); 128 mQueryHandler.startQuery(QUERY_TOKEN, null, uri, CONTACTS_PROJECTION, null, null, null); 129 130 } else if (Constants.SCHEME_TEL.equals(scheme)) { 131 mCreateExtras.putString(Intents.Insert.PHONE, ssp); 132 mCreateExtras.putString(SearchManager.QUERY, ssp); 133 134 Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, ssp); 135 mQueryHandler.startQuery(QUERY_TOKEN, null, uri, PHONES_PROJECTION, null, null, null); 136 137 } else { 138 Log.w(TAG, "Invalid intent:" + getIntent()); 139 finish(); 140 } 141 } 142 143 @Override 144 protected void onStop() { 145 super.onStop(); 146 if (mQueryHandler != null) { 147 mQueryHandler.cancelOperation(QUERY_TOKEN); 148 } 149 } 150 151 /** {@inheritDoc} */ 152 public void onQueryComplete(int token, Object cookie, Cursor cursor) { 153 if (cursor == null) { 154 // Bail when problem running query in background 155 finish(); 156 return; 157 } 158 159 // Count contacts found by query 160 int count = 0; 161 long contactId = -1; 162 String lookupKey = null; 163 try { 164 count = cursor.getCount(); 165 if (count == 1 && cursor.moveToFirst()) { 166 // Try reading ID if only one contact returned 167 contactId = cursor.getLong(CONTACT_ID_INDEX); 168 lookupKey = cursor.getString(LOOKUP_KEY_INDEX); 169 } 170 } finally { 171 cursor.close(); 172 } 173 174 if (count == 1 && contactId != -1) { 175 // If we only found one item, jump right to viewing it 176 final Uri contactUri = Contacts.getLookupUri(contactId, lookupKey); 177 final Intent viewIntent = new Intent(Intent.ACTION_VIEW, contactUri); 178 startActivity(viewIntent); 179 finish(); 180 181 } else if (count > 1) { 182 // If more than one, show pick list 183 Intent listIntent = new Intent(Intent.ACTION_SEARCH); 184 listIntent.setComponent(new ComponentName(this, PeopleActivity.class)); 185 listIntent.putExtras(mCreateExtras); 186 startActivity(listIntent); 187 finish(); 188 189 } else { 190 // No matching contacts found 191 if (mCreateForce) { 192 // Forced to create new contact 193 Intent createIntent = new Intent(Intent.ACTION_INSERT, RawContacts.CONTENT_URI); 194 createIntent.putExtras(mCreateExtras); 195 createIntent.setType(RawContacts.CONTENT_TYPE); 196 197 startActivity(createIntent); 198 finish(); 199 200 } else { 201 showDialog(CREATE_CONTACT_DIALOG); 202 } 203 } 204 } 205 206 @Override 207 protected Dialog onCreateDialog(int id) { 208 switch(id) { 209 case CREATE_CONTACT_DIALOG: 210 // Prompt user to insert or edit contact 211 final Intent createIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT); 212 createIntent.putExtras(mCreateExtras); 213 createIntent.setType(RawContacts.CONTENT_ITEM_TYPE); 214 215 final CharSequence message = getResources().getString( 216 R.string.add_contact_dlg_message_fmt, mCreateDescrip); 217 218 return new AlertDialog.Builder(this, AlertDialog.THEME_HOLO_LIGHT) 219 .setTitle(R.string.add_contact_dlg_title) 220 .setMessage(message) 221 .setPositiveButton(android.R.string.ok, 222 new IntentClickListener(this, createIntent)) 223 .setNegativeButton(android.R.string.cancel, 224 new IntentClickListener(this, null)) 225 .setOnCancelListener(new DialogInterface.OnCancelListener() { 226 @Override 227 public void onCancel(DialogInterface dialog) { 228 finish(); // Close the activity. 229 }}) 230 .create(); 231 } 232 return super.onCreateDialog(id); 233 } 234 235 /** 236 * Listener for {@link DialogInterface} that launches a given {@link Intent} 237 * when clicked. When clicked, this also closes the parent using 238 * {@link Activity#finish()}. 239 */ 240 private static class IntentClickListener implements DialogInterface.OnClickListener { 241 private Activity mParent; 242 private Intent mIntent; 243 244 /** 245 * @param parent {@link Activity} to use for launching target. 246 * @param intent Target {@link Intent} to launch when clicked. 247 */ 248 public IntentClickListener(Activity parent, Intent intent) { 249 mParent = parent; 250 mIntent = intent; 251 } 252 253 public void onClick(DialogInterface dialog, int which) { 254 if (mIntent != null) { 255 mParent.startActivity(mIntent); 256 } 257 mParent.finish(); 258 } 259 } 260} 261