blob: 6ca5ef58ed4ddcc16ab904d8da05a2b175fd82c2 [file] [log] [blame]
Chiao Cheng91197042012-08-24 14:19:37 -07001/*
2 * Copyright (C) 2006 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.dialer;
18
Nancy Chen675af1f2014-10-16 18:33:51 -070019import android.app.Activity;
Chiao Cheng91197042012-08-24 14:19:37 -070020import android.app.AlertDialog;
Jay Shrauner384eaad2015-04-03 16:03:41 -070021import android.app.DialogFragment;
Chiao Cheng91197042012-08-24 14:19:37 -070022import android.app.KeyguardManager;
23import android.app.ProgressDialog;
Jake Hamby1d6fb572013-04-09 15:49:56 -070024import android.content.ActivityNotFoundException;
Chiao Cheng91197042012-08-24 14:19:37 -070025import android.content.ContentResolver;
26import android.content.Context;
27import android.content.DialogInterface;
28import android.content.Intent;
29import android.database.Cursor;
30import android.net.Uri;
31import android.os.Looper;
Jeff Sharkeyf4f47662014-04-16 17:21:12 -070032import android.provider.Settings;
Yorke Lee3af7e562015-04-15 11:05:54 -070033import android.telecom.PhoneAccount;
Nancy Chen675af1f2014-10-16 18:33:51 -070034import android.telecom.PhoneAccountHandle;
Chiao Cheng91197042012-08-24 14:19:37 -070035import android.telephony.PhoneNumberUtils;
36import android.telephony.TelephonyManager;
Nancy Chen0f4ec2a2015-03-23 15:03:03 -070037import android.text.TextUtils;
Chiao Cheng91197042012-08-24 14:19:37 -070038import android.util.Log;
39import android.view.WindowManager;
40import android.widget.EditText;
41import android.widget.Toast;
42
Jay Shraunerede67ec2014-09-11 15:03:36 -070043import com.android.common.io.MoreCloseables;
Chiao Cheng07af7642012-09-14 12:05:14 -070044import com.android.contacts.common.database.NoNullCursorAsyncQueryHandler;
Ta-wei Yen6d98cf62015-11-05 17:33:58 -080045import com.android.contacts.common.util.ContactDisplayUtils;
Nancy Chen675af1f2014-10-16 18:33:51 -070046import com.android.contacts.common.widget.SelectPhoneAccountDialogFragment;
47import com.android.contacts.common.widget.SelectPhoneAccountDialogFragment.SelectPhoneAccountListener;
48import com.android.dialer.calllog.PhoneAccountUtils;
Yorke Lee4aece952015-05-02 22:22:54 -070049import com.android.dialer.util.TelecomUtil;
Chiao Cheng91197042012-08-24 14:19:37 -070050
Nancy Chen8c258ac2014-10-20 19:33:55 -070051import java.util.ArrayList;
52import java.util.List;
53
Chiao Cheng91197042012-08-24 14:19:37 -070054/**
55 * Helper class to listen for some magic character sequences
56 * that are handled specially by the dialer.
57 *
58 * Note the Phone app also handles these sequences too (in a couple of
Jake Hamby1d6fb572013-04-09 15:49:56 -070059 * relatively obscure places in the UI), so there's a separate version of
Chiao Cheng91197042012-08-24 14:19:37 -070060 * this class under apps/Phone.
61 *
62 * TODO: there's lots of duplicated code between this class and the
63 * corresponding class under apps/Phone. Let's figure out a way to
64 * unify these two classes (in the framework? in a common shared library?)
65 */
66public class SpecialCharSequenceMgr {
67 private static final String TAG = "SpecialCharSequenceMgr";
Jake Hamby1d6fb572013-04-09 15:49:56 -070068
Jay Shrauner384eaad2015-04-03 16:03:41 -070069 private static final String TAG_SELECT_ACCT_FRAGMENT = "tag_select_acct_fragment";
70
Yorke Leef90dada2013-12-09 11:50:28 -080071 private static final String SECRET_CODE_ACTION = "android.provider.Telephony.SECRET_CODE";
Chiao Cheng91197042012-08-24 14:19:37 -070072 private static final String MMI_IMEI_DISPLAY = "*#06#";
Jake Hamby1d6fb572013-04-09 15:49:56 -070073 private static final String MMI_REGULATORY_INFO_DISPLAY = "*#07#";
Chiao Cheng91197042012-08-24 14:19:37 -070074
75 /**
76 * Remembers the previous {@link QueryHandler} and cancel the operation when needed, to
77 * prevent possible crash.
78 *
79 * QueryHandler may call {@link ProgressDialog#dismiss()} when the screen is already gone,
80 * which will cause the app crash. This variable enables the class to prevent the crash
81 * on {@link #cleanup()}.
82 *
83 * TODO: Remove this and replace it (and {@link #cleanup()}) with better implementation.
Jake Hamby1d6fb572013-04-09 15:49:56 -070084 * One complication is that we have SpecialCharSequenceMgr in Phone package too, which has
Chiao Cheng91197042012-08-24 14:19:37 -070085 * *slightly* different implementation. Note that Phone package doesn't have this problem,
86 * so the class on Phone side doesn't have this functionality.
87 * Fundamental fix would be to have one shared implementation and resolve this corner case more
88 * gracefully.
89 */
90 private static QueryHandler sPreviousAdnQueryHandler;
91
Yorke Lee89e784b2015-06-10 13:36:15 -070092 public static class HandleAdnEntryAccountSelectedCallback extends SelectPhoneAccountListener{
Yorke Lee762b3572015-09-18 12:54:59 -070093 final private Context mContext;
Yorke Lee89e784b2015-06-10 13:36:15 -070094 final private QueryHandler mQueryHandler;
95 final private SimContactQueryCookie mCookie;
96
Yorke Lee762b3572015-09-18 12:54:59 -070097 public HandleAdnEntryAccountSelectedCallback(Context context,
Yorke Lee89e784b2015-06-10 13:36:15 -070098 QueryHandler queryHandler, SimContactQueryCookie cookie) {
Yorke Lee762b3572015-09-18 12:54:59 -070099 mContext = context;
Yorke Lee89e784b2015-06-10 13:36:15 -0700100 mQueryHandler = queryHandler;
101 mCookie = cookie;
102 }
103
104 @Override
105 public void onPhoneAccountSelected(PhoneAccountHandle selectedAccountHandle,
106 boolean setDefault) {
Yorke Lee762b3572015-09-18 12:54:59 -0700107 Uri uri = TelecomUtil.getAdnUriForPhoneAccount(mContext, selectedAccountHandle);
Yorke Lee89e784b2015-06-10 13:36:15 -0700108 handleAdnQuery(mQueryHandler, mCookie, uri);
109 // TODO: Show error dialog if result isn't valid.
110 }
111
112 }
113
114 public static class HandleMmiAccountSelectedCallback extends SelectPhoneAccountListener{
115 final private Context mContext;
116 final private String mInput;
117 public HandleMmiAccountSelectedCallback(Context context, String input) {
118 mContext = context.getApplicationContext();
119 mInput = input;
120 }
121
122 @Override
123 public void onPhoneAccountSelected(PhoneAccountHandle selectedAccountHandle,
124 boolean setDefault) {
125 TelecomUtil.handleMmi(mContext, mInput, selectedAccountHandle);
126 }
127 }
128
Chiao Cheng91197042012-08-24 14:19:37 -0700129 /** This class is never instantiated. */
130 private SpecialCharSequenceMgr() {
131 }
132
133 public static boolean handleChars(Context context, String input, EditText textField) {
Chiao Cheng91197042012-08-24 14:19:37 -0700134 //get rid of the separators so that the string gets parsed correctly
135 String dialString = PhoneNumberUtils.stripSeparators(input);
136
Nancy Chen8c258ac2014-10-20 19:33:55 -0700137 if (handleDeviceIdDisplay(context, dialString)
Jake Hamby1d6fb572013-04-09 15:49:56 -0700138 || handleRegulatoryInfoDisplay(context, dialString)
Chiao Cheng91197042012-08-24 14:19:37 -0700139 || handlePinEntry(context, dialString)
140 || handleAdnEntry(context, dialString, textField)
141 || handleSecretCode(context, dialString)) {
142 return true;
143 }
144
145 return false;
146 }
147
148 /**
149 * Cleanup everything around this class. Must be run inside the main thread.
150 *
151 * This should be called when the screen becomes background.
152 */
153 public static void cleanup() {
154 if (Looper.myLooper() != Looper.getMainLooper()) {
155 Log.wtf(TAG, "cleanup() is called outside the main thread");
156 return;
157 }
158
159 if (sPreviousAdnQueryHandler != null) {
160 sPreviousAdnQueryHandler.cancel();
161 sPreviousAdnQueryHandler = null;
162 }
163 }
164
165 /**
166 * Handles secret codes to launch arbitrary activities in the form of *#*#<code>#*#*.
167 * If a secret code is encountered an Intent is started with the android_secret_code://<code>
168 * URI.
169 *
170 * @param context the context to use
171 * @param input the text to check for a secret code in
172 * @return true if a secret code was encountered
173 */
174 static boolean handleSecretCode(Context context, String input) {
175 // Secret codes are in the form *#*#<code>#*#*
176 int len = input.length();
177 if (len > 8 && input.startsWith("*#*#") && input.endsWith("#*#*")) {
Yorke Leef90dada2013-12-09 11:50:28 -0800178 final Intent intent = new Intent(SECRET_CODE_ACTION,
Chiao Cheng91197042012-08-24 14:19:37 -0700179 Uri.parse("android_secret_code://" + input.substring(4, len - 4)));
180 context.sendBroadcast(intent);
181 return true;
182 }
183
184 return false;
185 }
186
187 /**
188 * Handle ADN requests by filling in the SIM contact number into the requested
189 * EditText.
190 *
191 * This code works alongside the Asynchronous query handler {@link QueryHandler}
192 * and query cancel handler implemented in {@link SimContactQueryCookie}.
193 */
Yorke Lee89e784b2015-06-10 13:36:15 -0700194 static boolean handleAdnEntry(Context context, String input, EditText textField) {
Yorke Lee762b3572015-09-18 12:54:59 -0700195 context = context.getApplicationContext();
Chiao Cheng91197042012-08-24 14:19:37 -0700196 /* ADN entries are of the form "N(N)(N)#" */
Chiao Cheng91197042012-08-24 14:19:37 -0700197 TelephonyManager telephonyManager =
198 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
199 if (telephonyManager == null
Yorke Lee62280c72013-11-22 18:24:59 -0800200 || telephonyManager.getPhoneType() != TelephonyManager.PHONE_TYPE_GSM) {
Chiao Cheng91197042012-08-24 14:19:37 -0700201 return false;
202 }
203
204 // if the phone is keyguard-restricted, then just ignore this
205 // input. We want to make sure that sim card contacts are NOT
206 // exposed unless the phone is unlocked, and this code can be
207 // accessed from the emergency dialer.
208 KeyguardManager keyguardManager =
209 (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
210 if (keyguardManager.inKeyguardRestrictedInputMode()) {
211 return false;
212 }
213
214 int len = input.length();
215 if ((len > 1) && (len < 5) && (input.endsWith("#"))) {
216 try {
217 // get the ordinal number of the sim contact
Nancy Chen18c52ff2014-10-30 10:25:00 -0700218 final int index = Integer.parseInt(input.substring(0, len-1));
Chiao Cheng91197042012-08-24 14:19:37 -0700219
220 // The original code that navigated to a SIM Contacts list view did not
221 // highlight the requested contact correctly, a requirement for PTCRB
222 // certification. This behaviour is consistent with the UI paradigm
223 // for touch-enabled lists, so it does not make sense to try to work
224 // around it. Instead we fill in the the requested phone number into
225 // the dialer text field.
226
227 // create the async query handler
Nancy Chen18c52ff2014-10-30 10:25:00 -0700228 final QueryHandler handler = new QueryHandler (context.getContentResolver());
Chiao Cheng91197042012-08-24 14:19:37 -0700229
230 // create the cookie object
Nancy Chen18c52ff2014-10-30 10:25:00 -0700231 final SimContactQueryCookie sc = new SimContactQueryCookie(index - 1, handler,
Chiao Cheng91197042012-08-24 14:19:37 -0700232 ADN_QUERY_TOKEN);
233
234 // setup the cookie fields
235 sc.contactNum = index - 1;
236 sc.setTextField(textField);
237
238 // create the progress dialog
239 sc.progressDialog = new ProgressDialog(context);
240 sc.progressDialog.setTitle(R.string.simContacts_title);
241 sc.progressDialog.setMessage(context.getText(R.string.simContacts_emptyLoading));
242 sc.progressDialog.setIndeterminate(true);
243 sc.progressDialog.setCancelable(true);
244 sc.progressDialog.setOnCancelListener(sc);
245 sc.progressDialog.getWindow().addFlags(
246 WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
247
Nancy Chen39c18f22014-12-03 16:37:45 -0800248 List<PhoneAccountHandle> subscriptionAccountHandles =
Nancy Chen18c52ff2014-10-30 10:25:00 -0700249 PhoneAccountUtils.getSubscriptionPhoneAccounts(context);
Chiao Cheng91197042012-08-24 14:19:37 -0700250
Nancy Chen39c18f22014-12-03 16:37:45 -0800251 boolean hasUserSelectedDefault = subscriptionAccountHandles.contains(
Yorke Lee762b3572015-09-18 12:54:59 -0700252 TelecomUtil.getDefaultOutgoingPhoneAccount(context,
253 PhoneAccount.SCHEME_TEL));
Chiao Cheng91197042012-08-24 14:19:37 -0700254
Nancy Chen39c18f22014-12-03 16:37:45 -0800255 if (subscriptionAccountHandles.size() == 1 || hasUserSelectedDefault) {
Yorke Lee762b3572015-09-18 12:54:59 -0700256 Uri uri = TelecomUtil.getAdnUriForPhoneAccount(context, null);
Nancy Chen18c52ff2014-10-30 10:25:00 -0700257 handleAdnQuery(handler, sc, uri);
Nancy Chen39c18f22014-12-03 16:37:45 -0800258 } else if (subscriptionAccountHandles.size() > 1){
Yorke Lee89e784b2015-06-10 13:36:15 -0700259 SelectPhoneAccountListener callback =
Yorke Lee762b3572015-09-18 12:54:59 -0700260 new HandleAdnEntryAccountSelectedCallback(context, handler, sc);
Nancy Chen18c52ff2014-10-30 10:25:00 -0700261
Jay Shrauner384eaad2015-04-03 16:03:41 -0700262 DialogFragment dialogFragment = SelectPhoneAccountDialogFragment.newInstance(
Yorke Lee89e784b2015-06-10 13:36:15 -0700263 subscriptionAccountHandles, callback);
Jay Shrauner384eaad2015-04-03 16:03:41 -0700264 dialogFragment.show(((Activity) context).getFragmentManager(),
265 TAG_SELECT_ACCT_FRAGMENT);
Nancy Chen18c52ff2014-10-30 10:25:00 -0700266 } else {
267 return false;
Chiao Cheng91197042012-08-24 14:19:37 -0700268 }
Nancy Chen18c52ff2014-10-30 10:25:00 -0700269
Chiao Cheng91197042012-08-24 14:19:37 -0700270 return true;
271 } catch (NumberFormatException ex) {
272 // Ignore
273 }
274 }
275 return false;
276 }
277
Nancy Chen18c52ff2014-10-30 10:25:00 -0700278 private static void handleAdnQuery(QueryHandler handler, SimContactQueryCookie cookie,
279 Uri uri) {
280 if (handler == null || cookie == null || uri == null) {
281 Log.w(TAG, "queryAdn parameters incorrect");
282 return;
283 }
284
285 // display the progress dialog
286 cookie.progressDialog.show();
287
288 // run the query.
289 handler.startQuery(ADN_QUERY_TOKEN, cookie, uri, new String[]{ADN_PHONE_NUMBER_COLUMN_NAME},
290 null, null, null);
291
292 if (sPreviousAdnQueryHandler != null) {
293 // It is harmless to call cancel() even after the handler's gone.
294 sPreviousAdnQueryHandler.cancel();
295 }
296 sPreviousAdnQueryHandler = handler;
297 }
298
Yorke Lee4aece952015-05-02 22:22:54 -0700299 static boolean handlePinEntry(final Context context, final String input) {
Chiao Cheng91197042012-08-24 14:19:37 -0700300 if ((input.startsWith("**04") || input.startsWith("**05")) && input.endsWith("#")) {
Nancy Chen39c18f22014-12-03 16:37:45 -0800301 List<PhoneAccountHandle> subscriptionAccountHandles =
Nancy Chen675af1f2014-10-16 18:33:51 -0700302 PhoneAccountUtils.getSubscriptionPhoneAccounts(context);
Nancy Chen39c18f22014-12-03 16:37:45 -0800303 boolean hasUserSelectedDefault = subscriptionAccountHandles.contains(
Yorke Lee762b3572015-09-18 12:54:59 -0700304 TelecomUtil.getDefaultOutgoingPhoneAccount(context, PhoneAccount.SCHEME_TEL));
Nancy Chen675af1f2014-10-16 18:33:51 -0700305
Nancy Chen39c18f22014-12-03 16:37:45 -0800306 if (subscriptionAccountHandles.size() == 1 || hasUserSelectedDefault) {
Nancy Chen675af1f2014-10-16 18:33:51 -0700307 // Don't bring up the dialog for single-SIM or if the default outgoing account is
308 // a subscription account.
Yorke Lee4aece952015-05-02 22:22:54 -0700309 return TelecomUtil.handleMmi(context, input, null);
Nancy Chen39c18f22014-12-03 16:37:45 -0800310 } else if (subscriptionAccountHandles.size() > 1){
Yorke Lee89e784b2015-06-10 13:36:15 -0700311 SelectPhoneAccountListener listener =
312 new HandleMmiAccountSelectedCallback(context, input);
Nancy Chen675af1f2014-10-16 18:33:51 -0700313
Jay Shrauner384eaad2015-04-03 16:03:41 -0700314 DialogFragment dialogFragment = SelectPhoneAccountDialogFragment.newInstance(
315 subscriptionAccountHandles, listener);
316 dialogFragment.show(((Activity) context).getFragmentManager(),
317 TAG_SELECT_ACCT_FRAGMENT);
Nancy Chen675af1f2014-10-16 18:33:51 -0700318 }
319 return true;
320 }
321 return false;
322 }
323
Nancy Chen8c258ac2014-10-20 19:33:55 -0700324 // TODO: Use TelephonyCapabilities.getDeviceIdLabel() to get the device id label instead of a
325 // hard-coded string.
326 static boolean handleDeviceIdDisplay(Context context, String input) {
Chiao Cheng91197042012-08-24 14:19:37 -0700327 TelephonyManager telephonyManager =
328 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Chiao Cheng91197042012-08-24 14:19:37 -0700329
Nancy Chen8c258ac2014-10-20 19:33:55 -0700330 if (telephonyManager != null && input.equals(MMI_IMEI_DISPLAY)) {
331 int labelResId = (telephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) ?
332 R.string.imei : R.string.meid;
333
334 List<String> deviceIds = new ArrayList<String>();
335 for (int slot = 0; slot < telephonyManager.getPhoneCount(); slot++) {
Nancy Chen0f4ec2a2015-03-23 15:03:03 -0700336 String deviceId = telephonyManager.getDeviceId(slot);
337 if (!TextUtils.isEmpty(deviceId)) {
338 deviceIds.add(deviceId);
339 }
Nancy Chen8c258ac2014-10-20 19:33:55 -0700340 }
341
342 AlertDialog alert = new AlertDialog.Builder(context)
343 .setTitle(labelResId)
344 .setItems(deviceIds.toArray(new String[deviceIds.size()]), null)
Nancy Chen0f4ec2a2015-03-23 15:03:03 -0700345 .setPositiveButton(android.R.string.ok, null)
Nancy Chen8c258ac2014-10-20 19:33:55 -0700346 .setCancelable(false)
347 .show();
348 return true;
349 }
Chiao Cheng91197042012-08-24 14:19:37 -0700350 return false;
351 }
352
Jake Hamby1d6fb572013-04-09 15:49:56 -0700353 private static boolean handleRegulatoryInfoDisplay(Context context, String input) {
354 if (input.equals(MMI_REGULATORY_INFO_DISPLAY)) {
355 Log.d(TAG, "handleRegulatoryInfoDisplay() sending intent to settings app");
Jeff Sharkeyf4f47662014-04-16 17:21:12 -0700356 Intent showRegInfoIntent = new Intent(Settings.ACTION_SHOW_REGULATORY_INFO);
Jake Hamby1d6fb572013-04-09 15:49:56 -0700357 try {
358 context.startActivity(showRegInfoIntent);
359 } catch (ActivityNotFoundException e) {
360 Log.e(TAG, "startActivity() failed: " + e);
361 }
362 return true;
363 }
364 return false;
365 }
366
Chiao Cheng91197042012-08-24 14:19:37 -0700367 /*******
368 * This code is used to handle SIM Contact queries
369 *******/
370 private static final String ADN_PHONE_NUMBER_COLUMN_NAME = "number";
371 private static final String ADN_NAME_COLUMN_NAME = "name";
372 private static final int ADN_QUERY_TOKEN = -1;
373
374 /**
375 * Cookie object that contains everything we need to communicate to the
376 * handler's onQuery Complete, as well as what we need in order to cancel
377 * the query (if requested).
378 *
379 * Note, access to the textField field is going to be synchronized, because
380 * the user can request a cancel at any time through the UI.
381 */
382 private static class SimContactQueryCookie implements DialogInterface.OnCancelListener{
383 public ProgressDialog progressDialog;
384 public int contactNum;
385
386 // Used to identify the query request.
387 private int mToken;
388 private QueryHandler mHandler;
389
390 // The text field we're going to update
391 private EditText textField;
392
393 public SimContactQueryCookie(int number, QueryHandler handler, int token) {
394 contactNum = number;
395 mHandler = handler;
396 mToken = token;
397 }
398
399 /**
400 * Synchronized getter for the EditText.
401 */
402 public synchronized EditText getTextField() {
403 return textField;
404 }
405
406 /**
407 * Synchronized setter for the EditText.
408 */
409 public synchronized void setTextField(EditText text) {
410 textField = text;
411 }
412
413 /**
414 * Cancel the ADN query by stopping the operation and signaling
415 * the cookie that a cancel request is made.
416 */
417 public synchronized void onCancel(DialogInterface dialog) {
418 // close the progress dialog
419 if (progressDialog != null) {
420 progressDialog.dismiss();
421 }
422
423 // setting the textfield to null ensures that the UI does NOT get
424 // updated.
425 textField = null;
426
427 // Cancel the operation if possible.
428 mHandler.cancelOperation(mToken);
429 }
430 }
431
432 /**
433 * Asynchronous query handler that services requests to look up ADNs
434 *
Jake Hamby1d6fb572013-04-09 15:49:56 -0700435 * Queries originate from {@link #handleAdnEntry}.
Chiao Cheng91197042012-08-24 14:19:37 -0700436 */
Chiao Cheng07af7642012-09-14 12:05:14 -0700437 private static class QueryHandler extends NoNullCursorAsyncQueryHandler {
Chiao Cheng91197042012-08-24 14:19:37 -0700438
439 private boolean mCanceled;
440
441 public QueryHandler(ContentResolver cr) {
442 super(cr);
443 }
444
445 /**
446 * Override basic onQueryComplete to fill in the textfield when
447 * we're handed the ADN cursor.
448 */
449 @Override
Chiao Cheng07af7642012-09-14 12:05:14 -0700450 protected void onNotNullableQueryComplete(int token, Object cookie, Cursor c) {
Jay Shraunerede67ec2014-09-11 15:03:36 -0700451 try {
452 sPreviousAdnQueryHandler = null;
453 if (mCanceled) {
454 return;
455 }
Chiao Cheng91197042012-08-24 14:19:37 -0700456
Jay Shraunerede67ec2014-09-11 15:03:36 -0700457 SimContactQueryCookie sc = (SimContactQueryCookie) cookie;
Chiao Cheng91197042012-08-24 14:19:37 -0700458
Jay Shraunerede67ec2014-09-11 15:03:36 -0700459 // close the progress dialog.
460 sc.progressDialog.dismiss();
Chiao Cheng91197042012-08-24 14:19:37 -0700461
Jay Shraunerede67ec2014-09-11 15:03:36 -0700462 // get the EditText to update or see if the request was cancelled.
463 EditText text = sc.getTextField();
Chiao Cheng91197042012-08-24 14:19:37 -0700464
Nancy Chen18c52ff2014-10-30 10:25:00 -0700465 // if the TextView is valid, and the cursor is valid and positionable on the
466 // Nth number, then we update the text field and display a toast indicating the
467 // caller name.
Jay Shraunerede67ec2014-09-11 15:03:36 -0700468 if ((c != null) && (text != null) && (c.moveToPosition(sc.contactNum))) {
469 String name = c.getString(c.getColumnIndexOrThrow(ADN_NAME_COLUMN_NAME));
Nancy Chen18c52ff2014-10-30 10:25:00 -0700470 String number =
471 c.getString(c.getColumnIndexOrThrow(ADN_PHONE_NUMBER_COLUMN_NAME));
Chiao Cheng91197042012-08-24 14:19:37 -0700472
Jay Shraunerede67ec2014-09-11 15:03:36 -0700473 // fill the text in.
474 text.getText().replace(0, 0, number);
Chiao Cheng91197042012-08-24 14:19:37 -0700475
Jay Shraunerede67ec2014-09-11 15:03:36 -0700476 // display the name as a toast
477 Context context = sc.progressDialog.getContext();
Ta-wei Yenf0017b72015-11-06 16:03:20 -0800478 CharSequence msg = ContactDisplayUtils.getTtsSpannedPhoneNumber(
Ta-wei Yen6d98cf62015-11-05 17:33:58 -0800479 context.getResources(), R.string.menu_callNumber, name);
480 Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
Jay Shraunerede67ec2014-09-11 15:03:36 -0700481 }
482 } finally {
483 MoreCloseables.closeQuietly(c);
Chiao Cheng91197042012-08-24 14:19:37 -0700484 }
485 }
486
487 public void cancel() {
488 mCanceled = true;
Nancy Chen18c52ff2014-10-30 10:25:00 -0700489 // Ask AsyncQueryHandler to cancel the whole request. This will fail when the query is
490 // already started.
Chiao Cheng91197042012-08-24 14:19:37 -0700491 cancelOperation(ADN_QUERY_TOKEN);
492 }
493 }
494}