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