blob: fe2163f1748b51d739f9ee7e1ffcdc068b01f25e [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;
Nancy Chenee5b3dc2015-12-08 16:52:51 -080044import com.android.contacts.common.compat.CompatUtils;
Nancy Chenfdb93b72015-12-22 11:41:36 -080045import com.android.contacts.common.compat.TelephonyManagerCompat;
Chiao Cheng07af7642012-09-14 12:05:14 -070046import com.android.contacts.common.database.NoNullCursorAsyncQueryHandler;
Ta-wei Yen6d98cf62015-11-05 17:33:58 -080047import com.android.contacts.common.util.ContactDisplayUtils;
Nancy Chen675af1f2014-10-16 18:33:51 -070048import com.android.contacts.common.widget.SelectPhoneAccountDialogFragment;
49import com.android.contacts.common.widget.SelectPhoneAccountDialogFragment.SelectPhoneAccountListener;
50import com.android.dialer.calllog.PhoneAccountUtils;
Yorke Lee4aece952015-05-02 22:22:54 -070051import com.android.dialer.util.TelecomUtil;
Chiao Cheng91197042012-08-24 14:19:37 -070052
Nancy Chen8c258ac2014-10-20 19:33:55 -070053import java.util.ArrayList;
54import java.util.List;
55
Chiao Cheng91197042012-08-24 14:19:37 -070056/**
57 * Helper class to listen for some magic character sequences
58 * that are handled specially by the dialer.
59 *
60 * Note the Phone app also handles these sequences too (in a couple of
Jake Hamby1d6fb572013-04-09 15:49:56 -070061 * relatively obscure places in the UI), so there's a separate version of
Chiao Cheng91197042012-08-24 14:19:37 -070062 * this class under apps/Phone.
63 *
64 * TODO: there's lots of duplicated code between this class and the
65 * corresponding class under apps/Phone. Let's figure out a way to
66 * unify these two classes (in the framework? in a common shared library?)
67 */
68public class SpecialCharSequenceMgr {
69 private static final String TAG = "SpecialCharSequenceMgr";
Jake Hamby1d6fb572013-04-09 15:49:56 -070070
Jay Shrauner384eaad2015-04-03 16:03:41 -070071 private static final String TAG_SELECT_ACCT_FRAGMENT = "tag_select_acct_fragment";
72
Yorke Leef90dada2013-12-09 11:50:28 -080073 private static final String SECRET_CODE_ACTION = "android.provider.Telephony.SECRET_CODE";
Chiao Cheng91197042012-08-24 14:19:37 -070074 private static final String MMI_IMEI_DISPLAY = "*#06#";
Jake Hamby1d6fb572013-04-09 15:49:56 -070075 private static final String MMI_REGULATORY_INFO_DISPLAY = "*#07#";
Chiao Cheng91197042012-08-24 14:19:37 -070076
77 /**
78 * Remembers the previous {@link QueryHandler} and cancel the operation when needed, to
79 * prevent possible crash.
80 *
81 * QueryHandler may call {@link ProgressDialog#dismiss()} when the screen is already gone,
82 * which will cause the app crash. This variable enables the class to prevent the crash
83 * on {@link #cleanup()}.
84 *
85 * TODO: Remove this and replace it (and {@link #cleanup()}) with better implementation.
Jake Hamby1d6fb572013-04-09 15:49:56 -070086 * One complication is that we have SpecialCharSequenceMgr in Phone package too, which has
Chiao Cheng91197042012-08-24 14:19:37 -070087 * *slightly* different implementation. Note that Phone package doesn't have this problem,
88 * so the class on Phone side doesn't have this functionality.
89 * Fundamental fix would be to have one shared implementation and resolve this corner case more
90 * gracefully.
91 */
92 private static QueryHandler sPreviousAdnQueryHandler;
93
Yorke Lee89e784b2015-06-10 13:36:15 -070094 public static class HandleAdnEntryAccountSelectedCallback extends SelectPhoneAccountListener{
Yorke Lee762b3572015-09-18 12:54:59 -070095 final private Context mContext;
Yorke Lee89e784b2015-06-10 13:36:15 -070096 final private QueryHandler mQueryHandler;
97 final private SimContactQueryCookie mCookie;
98
Yorke Lee762b3572015-09-18 12:54:59 -070099 public HandleAdnEntryAccountSelectedCallback(Context context,
Yorke Lee89e784b2015-06-10 13:36:15 -0700100 QueryHandler queryHandler, SimContactQueryCookie cookie) {
Yorke Lee762b3572015-09-18 12:54:59 -0700101 mContext = context;
Yorke Lee89e784b2015-06-10 13:36:15 -0700102 mQueryHandler = queryHandler;
103 mCookie = cookie;
104 }
105
106 @Override
107 public void onPhoneAccountSelected(PhoneAccountHandle selectedAccountHandle,
108 boolean setDefault) {
Yorke Lee762b3572015-09-18 12:54:59 -0700109 Uri uri = TelecomUtil.getAdnUriForPhoneAccount(mContext, selectedAccountHandle);
Yorke Lee89e784b2015-06-10 13:36:15 -0700110 handleAdnQuery(mQueryHandler, mCookie, uri);
111 // TODO: Show error dialog if result isn't valid.
112 }
113
114 }
115
116 public static class HandleMmiAccountSelectedCallback extends SelectPhoneAccountListener{
117 final private Context mContext;
118 final private String mInput;
119 public HandleMmiAccountSelectedCallback(Context context, String input) {
120 mContext = context.getApplicationContext();
121 mInput = input;
122 }
123
124 @Override
125 public void onPhoneAccountSelected(PhoneAccountHandle selectedAccountHandle,
126 boolean setDefault) {
127 TelecomUtil.handleMmi(mContext, mInput, selectedAccountHandle);
128 }
129 }
130
Chiao Cheng91197042012-08-24 14:19:37 -0700131 /** This class is never instantiated. */
132 private SpecialCharSequenceMgr() {
133 }
134
135 public static boolean handleChars(Context context, String input, EditText textField) {
Chiao Cheng91197042012-08-24 14:19:37 -0700136 //get rid of the separators so that the string gets parsed correctly
137 String dialString = PhoneNumberUtils.stripSeparators(input);
138
Nancy Chen8c258ac2014-10-20 19:33:55 -0700139 if (handleDeviceIdDisplay(context, dialString)
Jake Hamby1d6fb572013-04-09 15:49:56 -0700140 || handleRegulatoryInfoDisplay(context, dialString)
Chiao Cheng91197042012-08-24 14:19:37 -0700141 || handlePinEntry(context, dialString)
142 || handleAdnEntry(context, dialString, textField)
143 || handleSecretCode(context, dialString)) {
144 return true;
145 }
146
147 return false;
148 }
149
150 /**
151 * Cleanup everything around this class. Must be run inside the main thread.
152 *
153 * This should be called when the screen becomes background.
154 */
155 public static void cleanup() {
156 if (Looper.myLooper() != Looper.getMainLooper()) {
157 Log.wtf(TAG, "cleanup() is called outside the main thread");
158 return;
159 }
160
161 if (sPreviousAdnQueryHandler != null) {
162 sPreviousAdnQueryHandler.cancel();
163 sPreviousAdnQueryHandler = null;
164 }
165 }
166
167 /**
168 * Handles secret codes to launch arbitrary activities in the form of *#*#<code>#*#*.
fionaxufa589da2017-01-24 22:22:17 -0800169 * If a secret code is encountered, an Intent is started with the android_secret_code://<code>
Chiao Cheng91197042012-08-24 14:19:37 -0700170 * URI.
171 *
172 * @param context the context to use
173 * @param input the text to check for a secret code in
fionaxufa589da2017-01-24 22:22:17 -0800174 * @return true if a secret code was encountered and intent is sent out
Chiao Cheng91197042012-08-24 14:19:37 -0700175 */
176 static boolean handleSecretCode(Context context, String input) {
fionaxufa589da2017-01-24 22:22:17 -0800177 final TelephonyManager telephonyManager =
178 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
179 if (telephonyManager != null) {
180 return telephonyManager.sendDialerCode(input);
Chiao Cheng91197042012-08-24 14:19:37 -0700181 }
Chiao Cheng91197042012-08-24 14:19:37 -0700182 return false;
183 }
184
185 /**
186 * Handle ADN requests by filling in the SIM contact number into the requested
187 * EditText.
188 *
189 * This code works alongside the Asynchronous query handler {@link QueryHandler}
190 * and query cancel handler implemented in {@link SimContactQueryCookie}.
191 */
Yorke Lee89e784b2015-06-10 13:36:15 -0700192 static boolean handleAdnEntry(Context context, String input, EditText textField) {
Chiao Cheng91197042012-08-24 14:19:37 -0700193 /* ADN entries are of the form "N(N)(N)#" */
Chiao Cheng91197042012-08-24 14:19:37 -0700194 TelephonyManager telephonyManager =
195 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
196 if (telephonyManager == null
Yorke Lee62280c72013-11-22 18:24:59 -0800197 || telephonyManager.getPhoneType() != TelephonyManager.PHONE_TYPE_GSM) {
Chiao Cheng91197042012-08-24 14:19:37 -0700198 return false;
199 }
200
201 // if the phone is keyguard-restricted, then just ignore this
202 // input. We want to make sure that sim card contacts are NOT
203 // exposed unless the phone is unlocked, and this code can be
204 // accessed from the emergency dialer.
205 KeyguardManager keyguardManager =
206 (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
207 if (keyguardManager.inKeyguardRestrictedInputMode()) {
208 return false;
209 }
210
211 int len = input.length();
212 if ((len > 1) && (len < 5) && (input.endsWith("#"))) {
213 try {
214 // get the ordinal number of the sim contact
Nancy Chen18c52ff2014-10-30 10:25:00 -0700215 final int index = Integer.parseInt(input.substring(0, len-1));
Chiao Cheng91197042012-08-24 14:19:37 -0700216
217 // The original code that navigated to a SIM Contacts list view did not
218 // highlight the requested contact correctly, a requirement for PTCRB
219 // certification. This behaviour is consistent with the UI paradigm
220 // for touch-enabled lists, so it does not make sense to try to work
221 // around it. Instead we fill in the the requested phone number into
222 // the dialer text field.
223
224 // create the async query handler
Brandon Maxwellf6bf9e42015-12-02 12:55:57 -0800225 final QueryHandler handler = new QueryHandler(context.getContentResolver());
Chiao Cheng91197042012-08-24 14:19:37 -0700226
227 // create the cookie object
Nancy Chen18c52ff2014-10-30 10:25:00 -0700228 final SimContactQueryCookie sc = new SimContactQueryCookie(index - 1, handler,
Chiao Cheng91197042012-08-24 14:19:37 -0700229 ADN_QUERY_TOKEN);
230
231 // setup the cookie fields
232 sc.contactNum = index - 1;
233 sc.setTextField(textField);
234
235 // create the progress dialog
236 sc.progressDialog = new ProgressDialog(context);
237 sc.progressDialog.setTitle(R.string.simContacts_title);
238 sc.progressDialog.setMessage(context.getText(R.string.simContacts_emptyLoading));
239 sc.progressDialog.setIndeterminate(true);
240 sc.progressDialog.setCancelable(true);
241 sc.progressDialog.setOnCancelListener(sc);
242 sc.progressDialog.getWindow().addFlags(
243 WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
244
Nancy Chen39c18f22014-12-03 16:37:45 -0800245 List<PhoneAccountHandle> subscriptionAccountHandles =
Nancy Chen18c52ff2014-10-30 10:25:00 -0700246 PhoneAccountUtils.getSubscriptionPhoneAccounts(context);
Brandon Maxwellf6bf9e42015-12-02 12:55:57 -0800247 Context applicationContext = context.getApplicationContext();
Nancy Chen39c18f22014-12-03 16:37:45 -0800248 boolean hasUserSelectedDefault = subscriptionAccountHandles.contains(
Brandon Maxwellf6bf9e42015-12-02 12:55:57 -0800249 TelecomUtil.getDefaultOutgoingPhoneAccount(applicationContext,
Yorke Lee762b3572015-09-18 12:54:59 -0700250 PhoneAccount.SCHEME_TEL));
Chiao Cheng91197042012-08-24 14:19:37 -0700251
Nancy Chenfdb93b72015-12-22 11:41:36 -0800252 if (subscriptionAccountHandles.size() <= 1 || hasUserSelectedDefault) {
Brandon Maxwellf6bf9e42015-12-02 12:55:57 -0800253 Uri uri = TelecomUtil.getAdnUriForPhoneAccount(applicationContext, null);
Nancy Chen18c52ff2014-10-30 10:25:00 -0700254 handleAdnQuery(handler, sc, uri);
Nancy Chenfdb93b72015-12-22 11:41:36 -0800255 } else {
Brandon Maxwellf6bf9e42015-12-02 12:55:57 -0800256 SelectPhoneAccountListener callback = new HandleAdnEntryAccountSelectedCallback(
257 applicationContext, handler, sc);
Nancy Chen18c52ff2014-10-30 10:25:00 -0700258
Jay Shrauner384eaad2015-04-03 16:03:41 -0700259 DialogFragment dialogFragment = SelectPhoneAccountDialogFragment.newInstance(
Yorke Lee89e784b2015-06-10 13:36:15 -0700260 subscriptionAccountHandles, callback);
Jay Shrauner384eaad2015-04-03 16:03:41 -0700261 dialogFragment.show(((Activity) context).getFragmentManager(),
262 TAG_SELECT_ACCT_FRAGMENT);
Chiao Cheng91197042012-08-24 14:19:37 -0700263 }
Nancy Chen18c52ff2014-10-30 10:25:00 -0700264
Chiao Cheng91197042012-08-24 14:19:37 -0700265 return true;
266 } catch (NumberFormatException ex) {
267 // Ignore
268 }
269 }
270 return false;
271 }
272
Nancy Chen18c52ff2014-10-30 10:25:00 -0700273 private static void handleAdnQuery(QueryHandler handler, SimContactQueryCookie cookie,
274 Uri uri) {
275 if (handler == null || cookie == null || uri == null) {
276 Log.w(TAG, "queryAdn parameters incorrect");
277 return;
278 }
279
280 // display the progress dialog
281 cookie.progressDialog.show();
282
283 // run the query.
284 handler.startQuery(ADN_QUERY_TOKEN, cookie, uri, new String[]{ADN_PHONE_NUMBER_COLUMN_NAME},
285 null, null, null);
286
287 if (sPreviousAdnQueryHandler != null) {
288 // It is harmless to call cancel() even after the handler's gone.
289 sPreviousAdnQueryHandler.cancel();
290 }
291 sPreviousAdnQueryHandler = handler;
292 }
293
Yorke Lee4aece952015-05-02 22:22:54 -0700294 static boolean handlePinEntry(final Context context, final String input) {
Chiao Cheng91197042012-08-24 14:19:37 -0700295 if ((input.startsWith("**04") || input.startsWith("**05")) && input.endsWith("#")) {
Nancy Chen39c18f22014-12-03 16:37:45 -0800296 List<PhoneAccountHandle> subscriptionAccountHandles =
Nancy Chen675af1f2014-10-16 18:33:51 -0700297 PhoneAccountUtils.getSubscriptionPhoneAccounts(context);
Nancy Chen39c18f22014-12-03 16:37:45 -0800298 boolean hasUserSelectedDefault = subscriptionAccountHandles.contains(
Yorke Lee762b3572015-09-18 12:54:59 -0700299 TelecomUtil.getDefaultOutgoingPhoneAccount(context, PhoneAccount.SCHEME_TEL));
Nancy Chen675af1f2014-10-16 18:33:51 -0700300
Nancy Chenfdb93b72015-12-22 11:41:36 -0800301 if (subscriptionAccountHandles.size() <= 1 || hasUserSelectedDefault) {
Nancy Chen675af1f2014-10-16 18:33:51 -0700302 // Don't bring up the dialog for single-SIM or if the default outgoing account is
303 // a subscription account.
Yorke Lee4aece952015-05-02 22:22:54 -0700304 return TelecomUtil.handleMmi(context, input, null);
Nancy Chenfdb93b72015-12-22 11:41:36 -0800305 } else {
Yorke Lee89e784b2015-06-10 13:36:15 -0700306 SelectPhoneAccountListener listener =
307 new HandleMmiAccountSelectedCallback(context, input);
Nancy Chen675af1f2014-10-16 18:33:51 -0700308
Jay Shrauner384eaad2015-04-03 16:03:41 -0700309 DialogFragment dialogFragment = SelectPhoneAccountDialogFragment.newInstance(
310 subscriptionAccountHandles, listener);
311 dialogFragment.show(((Activity) context).getFragmentManager(),
312 TAG_SELECT_ACCT_FRAGMENT);
Nancy Chen675af1f2014-10-16 18:33:51 -0700313 }
314 return true;
315 }
316 return false;
317 }
318
Nancy Chen8c258ac2014-10-20 19:33:55 -0700319 // TODO: Use TelephonyCapabilities.getDeviceIdLabel() to get the device id label instead of a
320 // hard-coded string.
321 static boolean handleDeviceIdDisplay(Context context, String input) {
Chiao Cheng91197042012-08-24 14:19:37 -0700322 TelephonyManager telephonyManager =
323 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Chiao Cheng91197042012-08-24 14:19:37 -0700324
Nancy Chen8c258ac2014-10-20 19:33:55 -0700325 if (telephonyManager != null && input.equals(MMI_IMEI_DISPLAY)) {
326 int labelResId = (telephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) ?
327 R.string.imei : R.string.meid;
328
329 List<String> deviceIds = new ArrayList<String>();
Nancy Chenfdb93b72015-12-22 11:41:36 -0800330 if (TelephonyManagerCompat.getPhoneCount(telephonyManager) > 1 &&
331 CompatUtils.isMethodAvailable(TelephonyManagerCompat.TELEPHONY_MANAGER_CLASS,
332 "getDeviceId", Integer.TYPE)) {
Nancy Chenee5b3dc2015-12-08 16:52:51 -0800333 for (int slot = 0; slot < telephonyManager.getPhoneCount(); slot++) {
334 String deviceId = telephonyManager.getDeviceId(slot);
335 if (!TextUtils.isEmpty(deviceId)) {
336 deviceIds.add(deviceId);
337 }
Nancy Chen0f4ec2a2015-03-23 15:03:03 -0700338 }
Nancy Chenfdb93b72015-12-22 11:41:36 -0800339 } else {
340 deviceIds.add(telephonyManager.getDeviceId());
Nancy Chen8c258ac2014-10-20 19:33:55 -0700341 }
342
343 AlertDialog alert = new AlertDialog.Builder(context)
344 .setTitle(labelResId)
345 .setItems(deviceIds.toArray(new String[deviceIds.size()]), null)
Nancy Chen0f4ec2a2015-03-23 15:03:03 -0700346 .setPositiveButton(android.R.string.ok, null)
Nancy Chen8c258ac2014-10-20 19:33:55 -0700347 .setCancelable(false)
348 .show();
349 return true;
350 }
Chiao Cheng91197042012-08-24 14:19:37 -0700351 return false;
352 }
353
Jake Hamby1d6fb572013-04-09 15:49:56 -0700354 private static boolean handleRegulatoryInfoDisplay(Context context, String input) {
355 if (input.equals(MMI_REGULATORY_INFO_DISPLAY)) {
356 Log.d(TAG, "handleRegulatoryInfoDisplay() sending intent to settings app");
Jeff Sharkeyf4f47662014-04-16 17:21:12 -0700357 Intent showRegInfoIntent = new Intent(Settings.ACTION_SHOW_REGULATORY_INFO);
Jake Hamby1d6fb572013-04-09 15:49:56 -0700358 try {
359 context.startActivity(showRegInfoIntent);
360 } catch (ActivityNotFoundException e) {
361 Log.e(TAG, "startActivity() failed: " + e);
362 }
363 return true;
364 }
365 return false;
366 }
367
Chiao Cheng91197042012-08-24 14:19:37 -0700368 /*******
369 * This code is used to handle SIM Contact queries
370 *******/
371 private static final String ADN_PHONE_NUMBER_COLUMN_NAME = "number";
372 private static final String ADN_NAME_COLUMN_NAME = "name";
373 private static final int ADN_QUERY_TOKEN = -1;
374
375 /**
376 * Cookie object that contains everything we need to communicate to the
377 * handler's onQuery Complete, as well as what we need in order to cancel
378 * the query (if requested).
379 *
380 * Note, access to the textField field is going to be synchronized, because
381 * the user can request a cancel at any time through the UI.
382 */
383 private static class SimContactQueryCookie implements DialogInterface.OnCancelListener{
384 public ProgressDialog progressDialog;
385 public int contactNum;
386
387 // Used to identify the query request.
388 private int mToken;
389 private QueryHandler mHandler;
390
391 // The text field we're going to update
392 private EditText textField;
393
394 public SimContactQueryCookie(int number, QueryHandler handler, int token) {
395 contactNum = number;
396 mHandler = handler;
397 mToken = token;
398 }
399
400 /**
401 * Synchronized getter for the EditText.
402 */
403 public synchronized EditText getTextField() {
404 return textField;
405 }
406
407 /**
408 * Synchronized setter for the EditText.
409 */
410 public synchronized void setTextField(EditText text) {
411 textField = text;
412 }
413
414 /**
415 * Cancel the ADN query by stopping the operation and signaling
416 * the cookie that a cancel request is made.
417 */
418 public synchronized void onCancel(DialogInterface dialog) {
419 // close the progress dialog
420 if (progressDialog != null) {
421 progressDialog.dismiss();
422 }
423
424 // setting the textfield to null ensures that the UI does NOT get
425 // updated.
426 textField = null;
427
428 // Cancel the operation if possible.
429 mHandler.cancelOperation(mToken);
430 }
431 }
432
433 /**
434 * Asynchronous query handler that services requests to look up ADNs
435 *
Jake Hamby1d6fb572013-04-09 15:49:56 -0700436 * Queries originate from {@link #handleAdnEntry}.
Chiao Cheng91197042012-08-24 14:19:37 -0700437 */
Chiao Cheng07af7642012-09-14 12:05:14 -0700438 private static class QueryHandler extends NoNullCursorAsyncQueryHandler {
Chiao Cheng91197042012-08-24 14:19:37 -0700439
440 private boolean mCanceled;
441
442 public QueryHandler(ContentResolver cr) {
443 super(cr);
444 }
445
446 /**
447 * Override basic onQueryComplete to fill in the textfield when
448 * we're handed the ADN cursor.
449 */
450 @Override
Chiao Cheng07af7642012-09-14 12:05:14 -0700451 protected void onNotNullableQueryComplete(int token, Object cookie, Cursor c) {
Jay Shraunerede67ec2014-09-11 15:03:36 -0700452 try {
453 sPreviousAdnQueryHandler = null;
454 if (mCanceled) {
455 return;
456 }
Chiao Cheng91197042012-08-24 14:19:37 -0700457
Jay Shraunerede67ec2014-09-11 15:03:36 -0700458 SimContactQueryCookie sc = (SimContactQueryCookie) cookie;
Chiao Cheng91197042012-08-24 14:19:37 -0700459
Jay Shraunerede67ec2014-09-11 15:03:36 -0700460 // close the progress dialog.
461 sc.progressDialog.dismiss();
Chiao Cheng91197042012-08-24 14:19:37 -0700462
Jay Shraunerede67ec2014-09-11 15:03:36 -0700463 // get the EditText to update or see if the request was cancelled.
464 EditText text = sc.getTextField();
Chiao Cheng91197042012-08-24 14:19:37 -0700465
Nancy Chen18c52ff2014-10-30 10:25:00 -0700466 // if the TextView is valid, and the cursor is valid and positionable on the
467 // Nth number, then we update the text field and display a toast indicating the
468 // caller name.
Jay Shraunerede67ec2014-09-11 15:03:36 -0700469 if ((c != null) && (text != null) && (c.moveToPosition(sc.contactNum))) {
470 String name = c.getString(c.getColumnIndexOrThrow(ADN_NAME_COLUMN_NAME));
Nancy Chen18c52ff2014-10-30 10:25:00 -0700471 String number =
472 c.getString(c.getColumnIndexOrThrow(ADN_PHONE_NUMBER_COLUMN_NAME));
Chiao Cheng91197042012-08-24 14:19:37 -0700473
Jay Shraunerede67ec2014-09-11 15:03:36 -0700474 // fill the text in.
475 text.getText().replace(0, 0, number);
Chiao Cheng91197042012-08-24 14:19:37 -0700476
Jay Shraunerede67ec2014-09-11 15:03:36 -0700477 // display the name as a toast
478 Context context = sc.progressDialog.getContext();
Ta-wei Yenf0017b72015-11-06 16:03:20 -0800479 CharSequence msg = ContactDisplayUtils.getTtsSpannedPhoneNumber(
Ta-wei Yen6d98cf62015-11-05 17:33:58 -0800480 context.getResources(), R.string.menu_callNumber, name);
481 Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
Jay Shraunerede67ec2014-09-11 15:03:36 -0700482 }
483 } finally {
484 MoreCloseables.closeQuietly(c);
Chiao Cheng91197042012-08-24 14:19:37 -0700485 }
486 }
487
488 public void cancel() {
489 mCanceled = true;
Nancy Chen18c52ff2014-10-30 10:25:00 -0700490 // Ask AsyncQueryHandler to cancel the whole request. This will fail when the query is
491 // already started.
Chiao Cheng91197042012-08-24 14:19:37 -0700492 cancelOperation(ADN_QUERY_TOKEN);
493 }
494 }
495}