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