• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package com.android.dialer.dialpadview;
18 
19 import android.Manifest;
20 import android.annotation.SuppressLint;
21 import android.app.Activity;
22 import android.app.AlertDialog;
23 import android.app.DialogFragment;
24 import android.app.KeyguardManager;
25 import android.app.ProgressDialog;
26 import android.content.ActivityNotFoundException;
27 import android.content.ContentResolver;
28 import android.content.Context;
29 import android.content.DialogInterface;
30 import android.content.Intent;
31 import android.database.Cursor;
32 import android.graphics.Bitmap;
33 import android.graphics.Bitmap.Config;
34 import android.graphics.Color;
35 import android.net.Uri;
36 import android.provider.Settings;
37 import android.support.annotation.Nullable;
38 import android.support.annotation.VisibleForTesting;
39 import android.telecom.PhoneAccount;
40 import android.telecom.PhoneAccountHandle;
41 import android.telephony.PhoneNumberUtils;
42 import android.telephony.TelephonyManager;
43 import android.text.TextUtils;
44 import android.view.LayoutInflater;
45 import android.view.View;
46 import android.view.ViewGroup;
47 import android.view.ViewGroup.LayoutParams;
48 import android.view.ViewTreeObserver.OnGlobalLayoutListener;
49 import android.view.WindowManager;
50 import android.widget.EditText;
51 import android.widget.ImageView;
52 import android.widget.TextView;
53 import android.widget.Toast;
54 import com.android.common.io.MoreCloseables;
55 import com.android.contacts.common.database.NoNullCursorAsyncQueryHandler;
56 import com.android.contacts.common.util.ContactDisplayUtils;
57 import com.android.contacts.common.widget.SelectPhoneAccountDialogFragment;
58 import com.android.contacts.common.widget.SelectPhoneAccountDialogFragment.SelectPhoneAccountListener;
59 import com.android.dialer.common.Assert;
60 import com.android.dialer.common.LogUtil;
61 import com.android.dialer.compat.telephony.TelephonyManagerCompat;
62 import com.android.dialer.oem.MotorolaUtils;
63 import com.android.dialer.telecom.TelecomUtil;
64 import com.android.dialer.util.PermissionsUtil;
65 import com.google.zxing.BarcodeFormat;
66 import com.google.zxing.MultiFormatWriter;
67 import com.google.zxing.WriterException;
68 import com.google.zxing.common.BitMatrix;
69 import java.util.ArrayList;
70 import java.util.Arrays;
71 import java.util.List;
72 import java.util.Locale;
73 
74 /**
75  * Helper class to listen for some magic character sequences that are handled specially by the
76  * dialer.
77  *
78  * <p>Note the Phone app also handles these sequences too (in a couple of relatively obscure places
79  * in the UI), so there's a separate version of this class under apps/Phone.
80  *
81  * <p>TODO: there's lots of duplicated code between this class and the corresponding class under
82  * apps/Phone. Let's figure out a way to unify these two classes (in the framework? in a common
83  * shared library?)
84  */
85 public class SpecialCharSequenceMgr {
86   private static final String TAG_SELECT_ACCT_FRAGMENT = "tag_select_acct_fragment";
87 
88   @VisibleForTesting static final String MMI_IMEI_DISPLAY = "*#06#";
89   private static final String MMI_REGULATORY_INFO_DISPLAY = "*#07#";
90   /** ***** This code is used to handle SIM Contact queries ***** */
91   private static final String ADN_PHONE_NUMBER_COLUMN_NAME = "number";
92 
93   private static final String ADN_NAME_COLUMN_NAME = "name";
94   private static final int ADN_QUERY_TOKEN = -1;
95 
96   @VisibleForTesting
97   static final List<String> TRANSSION_CODES =
98       new ArrayList<String>() {
99         {
100           add("*#07#");
101           add("*#87#");
102           add("*#43#");
103           add("*#2727#");
104           add("*#88#");
105         }
106       };
107 
108   /**
109    * Remembers the previous {@link QueryHandler} and cancel the operation when needed, to prevent
110    * possible crash.
111    *
112    * <p>QueryHandler may call {@link ProgressDialog#dismiss()} when the screen is already gone,
113    * which will cause the app crash. This variable enables the class to prevent the crash on {@link
114    * #cleanup()}.
115    *
116    * <p>TODO: Remove this and replace it (and {@link #cleanup()}) with better implementation. One
117    * complication is that we have SpecialCharSequenceMgr in Phone package too, which has *slightly*
118    * different implementation. Note that Phone package doesn't have this problem, so the class on
119    * Phone side doesn't have this functionality. Fundamental fix would be to have one shared
120    * implementation and resolve this corner case more gracefully.
121    */
122   private static QueryHandler previousAdnQueryHandler;
123 
124   /** This class is never instantiated. */
SpecialCharSequenceMgr()125   private SpecialCharSequenceMgr() {}
126 
handleChars(Context context, String input, EditText textField)127   public static boolean handleChars(Context context, String input, EditText textField) {
128     // get rid of the separators so that the string gets parsed correctly
129     String dialString = PhoneNumberUtils.stripSeparators(input);
130 
131     if (handleDeviceIdDisplay(context, dialString)
132         || handleRegulatoryInfoDisplay(context, dialString)
133         || handlePinEntry(context, dialString)
134         || handleAdnEntry(context, dialString, textField)
135         || handleSecretCode(context, dialString)) {
136       return true;
137     }
138 
139     if (MotorolaUtils.handleSpecialCharSequence(context, input)) {
140       return true;
141     }
142 
143     return false;
144   }
145 
146   /**
147    * Cleanup everything around this class. Must be run inside the main thread.
148    *
149    * <p>This should be called when the screen becomes background.
150    */
cleanup()151   public static void cleanup() {
152     Assert.isMainThread();
153 
154     if (previousAdnQueryHandler != null) {
155       previousAdnQueryHandler.cancel();
156       previousAdnQueryHandler = null;
157     }
158   }
159 
160   /**
161    * Handles secret codes to launch arbitrary activities in the form of
162    * *#*#<code>#*#* or *#<code_starting_with_number>#.
163    *
164    * @param context the context to use
165    * @param input the text to check for a secret code in
166    * @return true if a secret code was encountered and handled
167    */
handleSecretCode(Context context, String input)168   static boolean handleSecretCode(Context context, String input) {
169     // Secret codes are accessed by dialing *#*#<code>#*#* or "*#<code_starting_with_number>#"
170     if (input.length() > 8 && input.startsWith("*#*#") && input.endsWith("#*#*")) {
171       String secretCode = input.substring(4, input.length() - 4);
172       TelephonyManagerCompat.handleSecretCode(context, secretCode);
173       return true;
174     }
175     if (TRANSSION_CODES.contains(input)) {
176       String secretCode = input.substring(2, input.length() - 1);
177       TelephonyManagerCompat.handleSecretCode(context, secretCode);
178       return true;
179     }
180     return false;
181   }
182 
183   /**
184    * Handle ADN requests by filling in the SIM contact number into the requested EditText.
185    *
186    * <p>This code works alongside the Asynchronous query handler {@link QueryHandler} and query
187    * cancel handler implemented in {@link SimContactQueryCookie}.
188    */
handleAdnEntry(Context context, String input, EditText textField)189   static boolean handleAdnEntry(Context context, String input, EditText textField) {
190     /* ADN entries are of the form "N(N)(N)#" */
191     TelephonyManager telephonyManager =
192         (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
193     if (telephonyManager == null
194         || telephonyManager.getPhoneType() != TelephonyManager.PHONE_TYPE_GSM) {
195       return false;
196     }
197 
198     // if the phone is keyguard-restricted, then just ignore this
199     // input.  We want to make sure that sim card contacts are NOT
200     // exposed unless the phone is unlocked, and this code can be
201     // accessed from the emergency dialer.
202     KeyguardManager keyguardManager =
203         (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
204     if (keyguardManager.inKeyguardRestrictedInputMode()) {
205       return false;
206     }
207 
208     int len = input.length();
209     if ((len > 1) && (len < 5) && (input.endsWith("#"))) {
210       try {
211         // get the ordinal number of the sim contact
212         final int index = Integer.parseInt(input.substring(0, len - 1));
213 
214         // The original code that navigated to a SIM Contacts list view did not
215         // highlight the requested contact correctly, a requirement for PTCRB
216         // certification.  This behaviour is consistent with the UI paradigm
217         // for touch-enabled lists, so it does not make sense to try to work
218         // around it.  Instead we fill in the the requested phone number into
219         // the dialer text field.
220 
221         // create the async query handler
222         final QueryHandler handler = new QueryHandler(context.getContentResolver());
223 
224         // create the cookie object
225         final SimContactQueryCookie sc =
226             new SimContactQueryCookie(index - 1, handler, ADN_QUERY_TOKEN);
227 
228         // setup the cookie fields
229         sc.contactNum = index - 1;
230         sc.setTextField(textField);
231 
232         // create the progress dialog
233         sc.progressDialog = new ProgressDialog(context);
234         sc.progressDialog.setTitle(R.string.simContacts_title);
235         sc.progressDialog.setMessage(context.getText(R.string.simContacts_emptyLoading));
236         sc.progressDialog.setIndeterminate(true);
237         sc.progressDialog.setCancelable(true);
238         sc.progressDialog.setOnCancelListener(sc);
239         sc.progressDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
240 
241         List<PhoneAccountHandle> subscriptionAccountHandles =
242             TelecomUtil.getSubscriptionPhoneAccounts(context);
243         Context applicationContext = context.getApplicationContext();
244         boolean hasUserSelectedDefault =
245             subscriptionAccountHandles.contains(
246                 TelecomUtil.getDefaultOutgoingPhoneAccount(
247                     applicationContext, PhoneAccount.SCHEME_TEL));
248 
249         if (subscriptionAccountHandles.size() <= 1 || hasUserSelectedDefault) {
250           Uri uri = TelecomUtil.getAdnUriForPhoneAccount(applicationContext, null);
251           handleAdnQuery(handler, sc, uri);
252         } else {
253           SelectPhoneAccountListener callback =
254               new HandleAdnEntryAccountSelectedCallback(applicationContext, handler, sc);
255 
256           DialogFragment dialogFragment =
257               SelectPhoneAccountDialogFragment.newInstance(
258                   subscriptionAccountHandles, callback, null);
259           dialogFragment.show(((Activity) context).getFragmentManager(), TAG_SELECT_ACCT_FRAGMENT);
260         }
261 
262         return true;
263       } catch (NumberFormatException ex) {
264         // Ignore
265       }
266     }
267     return false;
268   }
269 
handleAdnQuery(QueryHandler handler, SimContactQueryCookie cookie, Uri uri)270   private static void handleAdnQuery(QueryHandler handler, SimContactQueryCookie cookie, Uri uri) {
271     if (handler == null || cookie == null || uri == null) {
272       LogUtil.w("SpecialCharSequenceMgr.handleAdnQuery", "queryAdn parameters incorrect");
273       return;
274     }
275 
276     // display the progress dialog
277     cookie.progressDialog.show();
278 
279     // run the query.
280     handler.startQuery(
281         ADN_QUERY_TOKEN,
282         cookie,
283         uri,
284         new String[] {ADN_PHONE_NUMBER_COLUMN_NAME},
285         null,
286         null,
287         null);
288 
289     if (previousAdnQueryHandler != null) {
290       // It is harmless to call cancel() even after the handler's gone.
291       previousAdnQueryHandler.cancel();
292     }
293     previousAdnQueryHandler = handler;
294   }
295 
handlePinEntry(final Context context, final String input)296   static boolean handlePinEntry(final Context context, final String input) {
297     if ((input.startsWith("**04") || input.startsWith("**05")) && input.endsWith("#")) {
298       List<PhoneAccountHandle> subscriptionAccountHandles =
299           TelecomUtil.getSubscriptionPhoneAccounts(context);
300       boolean hasUserSelectedDefault =
301           subscriptionAccountHandles.contains(
302               TelecomUtil.getDefaultOutgoingPhoneAccount(context, PhoneAccount.SCHEME_TEL));
303 
304       if (subscriptionAccountHandles.size() <= 1 || hasUserSelectedDefault) {
305         // Don't bring up the dialog for single-SIM or if the default outgoing account is
306         // a subscription account.
307         return TelecomUtil.handleMmi(context, input, null);
308       } else {
309         SelectPhoneAccountListener listener = new HandleMmiAccountSelectedCallback(context, input);
310 
311         DialogFragment dialogFragment =
312             SelectPhoneAccountDialogFragment.newInstance(
313                 subscriptionAccountHandles, listener, null);
314         dialogFragment.show(((Activity) context).getFragmentManager(), TAG_SELECT_ACCT_FRAGMENT);
315       }
316       return true;
317     }
318     return false;
319   }
320 
321   // TODO: Use TelephonyCapabilities.getDeviceIdLabel() to get the device id label instead of a
322   // hard-coded string.
323   @SuppressLint("HardwareIds")
handleDeviceIdDisplay(Context context, String input)324   static boolean handleDeviceIdDisplay(Context context, String input) {
325     if (!PermissionsUtil.hasPermission(context, Manifest.permission.READ_PHONE_STATE)) {
326       return false;
327     }
328     TelephonyManager telephonyManager =
329         (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
330 
331     if (telephonyManager != null && input.equals(MMI_IMEI_DISPLAY)) {
332       int labelResId =
333           (telephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM)
334               ? R.string.imei
335               : R.string.meid;
336 
337       View customView = LayoutInflater.from(context).inflate(R.layout.dialog_deviceids, null);
338       ViewGroup holder = customView.findViewById(R.id.deviceids_holder);
339 
340       if (TelephonyManagerCompat.getPhoneCount(telephonyManager) > 1) {
341         for (int slot = 0; slot < telephonyManager.getPhoneCount(); slot++) {
342           String deviceId = telephonyManager.getDeviceId(slot);
343           if (!TextUtils.isEmpty(deviceId)) {
344             addDeviceIdRow(
345                 holder,
346                 deviceId,
347                 /* showDecimal */
348                 context.getResources().getBoolean(R.bool.show_device_id_in_hex_and_decimal),
349                 /* showBarcode */ false);
350           }
351         }
352       } else {
353         addDeviceIdRow(
354             holder,
355             telephonyManager.getDeviceId(),
356             /* showDecimal */
357             context.getResources().getBoolean(R.bool.show_device_id_in_hex_and_decimal),
358             /* showBarcode */
359             context.getResources().getBoolean(R.bool.show_device_id_as_barcode));
360       }
361 
362       new AlertDialog.Builder(context)
363           .setTitle(labelResId)
364           .setView(customView)
365           .setPositiveButton(android.R.string.ok, null)
366           .setCancelable(false)
367           .show()
368           .getWindow()
369           .setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
370       return true;
371     }
372     return false;
373   }
374 
addDeviceIdRow( ViewGroup holder, String deviceId, boolean showDecimal, boolean showBarcode)375   private static void addDeviceIdRow(
376       ViewGroup holder, String deviceId, boolean showDecimal, boolean showBarcode) {
377     if (TextUtils.isEmpty(deviceId)) {
378       return;
379     }
380 
381     ViewGroup row =
382         (ViewGroup)
383             LayoutInflater.from(holder.getContext()).inflate(R.layout.row_deviceid, holder, false);
384     holder.addView(row);
385 
386     // Remove the check digit, if exists. This digit is a checksum of the ID.
387     // See https://en.wikipedia.org/wiki/International_Mobile_Equipment_Identity
388     // and https://en.wikipedia.org/wiki/Mobile_equipment_identifier
389     String hex = deviceId.length() == 15 ? deviceId.substring(0, 14) : deviceId;
390 
391     // If this is the valid length IMEI or MEID (14 digits), show it in all formats, otherwise fall
392     // back to just showing the raw hex
393     if (hex.length() == 14 && showDecimal) {
394       ((TextView) row.findViewById(R.id.deviceid_hex)).setText(hex);
395       ((TextView) row.findViewById(R.id.deviceid_dec)).setText(getDecimalFromHex(hex));
396       row.findViewById(R.id.deviceid_dec_label).setVisibility(View.VISIBLE);
397     } else {
398       row.findViewById(R.id.deviceid_hex_label).setVisibility(View.GONE);
399       ((TextView) row.findViewById(R.id.deviceid_hex)).setText(deviceId);
400     }
401 
402     final ImageView barcode = row.findViewById(R.id.deviceid_barcode);
403     if (showBarcode) {
404       // Wait until the layout pass has completed so we the barcode is measured before drawing. We
405       // do this by adding a layout listener and setting the bitmap after getting the callback.
406       barcode
407           .getViewTreeObserver()
408           .addOnGlobalLayoutListener(
409               new OnGlobalLayoutListener() {
410                 @Override
411                 public void onGlobalLayout() {
412                   barcode.getViewTreeObserver().removeOnGlobalLayoutListener(this);
413                   Bitmap barcodeBitmap =
414                       generateBarcode(hex, barcode.getWidth(), barcode.getHeight());
415                   if (barcodeBitmap != null) {
416                     barcode.setImageBitmap(barcodeBitmap);
417                   }
418                 }
419               });
420     } else {
421       barcode.setVisibility(View.GONE);
422     }
423   }
424 
getDecimalFromHex(String hex)425   private static String getDecimalFromHex(String hex) {
426     final String part1 = hex.substring(0, 8);
427     final String part2 = hex.substring(8);
428 
429     long dec1;
430     try {
431       dec1 = Long.parseLong(part1, 16);
432     } catch (NumberFormatException e) {
433       LogUtil.e("SpecialCharSequenceMgr.getDecimalFromHex", "unable to parse hex", e);
434       return "";
435     }
436 
437     final String manufacturerCode = String.format(Locale.US, "%010d", dec1);
438 
439     long dec2;
440     try {
441       dec2 = Long.parseLong(part2, 16);
442     } catch (NumberFormatException e) {
443       LogUtil.e("SpecialCharSequenceMgr.getDecimalFromHex", "unable to parse hex", e);
444       return "";
445     }
446 
447     final String serialNum = String.format(Locale.US, "%08d", dec2);
448 
449     StringBuilder builder = new StringBuilder(22);
450     builder
451         .append(manufacturerCode, 0, 5)
452         .append(' ')
453         .append(manufacturerCode, 5, manufacturerCode.length())
454         .append(' ')
455         .append(serialNum, 0, 4)
456         .append(' ')
457         .append(serialNum, 4, serialNum.length());
458     return builder.toString();
459   }
460 
461   /**
462    * This method generates a 2d barcode using the zxing library. Each pixel of the bitmap is either
463    * black or white painted vertically. We determine which color using the BitMatrix.get(x, y)
464    * method.
465    */
generateBarcode(String hex, int width, int height)466   private static Bitmap generateBarcode(String hex, int width, int height) {
467     MultiFormatWriter writer = new MultiFormatWriter();
468     String data = Uri.encode(hex);
469 
470     try {
471       BitMatrix bitMatrix = writer.encode(data, BarcodeFormat.CODE_128, width, 1);
472       Bitmap bitmap = Bitmap.createBitmap(bitMatrix.getWidth(), height, Config.RGB_565);
473 
474       for (int i = 0; i < bitMatrix.getWidth(); i++) {
475         // Paint columns of width 1
476         int[] column = new int[height];
477         Arrays.fill(column, bitMatrix.get(i, 0) ? Color.BLACK : Color.WHITE);
478         bitmap.setPixels(column, 0, 1, i, 0, 1, height);
479       }
480       return bitmap;
481     } catch (WriterException e) {
482       LogUtil.e("SpecialCharSequenceMgr.generateBarcode", "error generating barcode", e);
483     }
484     return null;
485   }
486 
handleRegulatoryInfoDisplay(Context context, String input)487   private static boolean handleRegulatoryInfoDisplay(Context context, String input) {
488     if (input.equals(MMI_REGULATORY_INFO_DISPLAY)) {
489       LogUtil.i(
490           "SpecialCharSequenceMgr.handleRegulatoryInfoDisplay", "sending intent to settings app");
491       Intent showRegInfoIntent = new Intent(Settings.ACTION_SHOW_REGULATORY_INFO);
492       try {
493         context.startActivity(showRegInfoIntent);
494       } catch (ActivityNotFoundException e) {
495         LogUtil.e(
496             "SpecialCharSequenceMgr.handleRegulatoryInfoDisplay", "startActivity() failed: ", e);
497       }
498       return true;
499     }
500     return false;
501   }
502 
503   public static class HandleAdnEntryAccountSelectedCallback extends SelectPhoneAccountListener {
504 
505     private final Context context;
506     private final QueryHandler queryHandler;
507     private final SimContactQueryCookie cookie;
508 
HandleAdnEntryAccountSelectedCallback( Context context, QueryHandler queryHandler, SimContactQueryCookie cookie)509     public HandleAdnEntryAccountSelectedCallback(
510         Context context, QueryHandler queryHandler, SimContactQueryCookie cookie) {
511       this.context = context;
512       this.queryHandler = queryHandler;
513       this.cookie = cookie;
514     }
515 
516     @Override
onPhoneAccountSelected( PhoneAccountHandle selectedAccountHandle, boolean setDefault, @Nullable String callId)517     public void onPhoneAccountSelected(
518         PhoneAccountHandle selectedAccountHandle, boolean setDefault, @Nullable String callId) {
519       Uri uri = TelecomUtil.getAdnUriForPhoneAccount(context, selectedAccountHandle);
520       handleAdnQuery(queryHandler, cookie, uri);
521       // TODO: Show error dialog if result isn't valid.
522     }
523   }
524 
525   public static class HandleMmiAccountSelectedCallback extends SelectPhoneAccountListener {
526 
527     private final Context context;
528     private final String input;
529 
HandleMmiAccountSelectedCallback(Context context, String input)530     public HandleMmiAccountSelectedCallback(Context context, String input) {
531       this.context = context.getApplicationContext();
532       this.input = input;
533     }
534 
535     @Override
onPhoneAccountSelected( PhoneAccountHandle selectedAccountHandle, boolean setDefault, @Nullable String callId)536     public void onPhoneAccountSelected(
537         PhoneAccountHandle selectedAccountHandle, boolean setDefault, @Nullable String callId) {
538       TelecomUtil.handleMmi(context, input, selectedAccountHandle);
539     }
540   }
541 
542   /**
543    * Cookie object that contains everything we need to communicate to the handler's onQuery
544    * Complete, as well as what we need in order to cancel the query (if requested).
545    *
546    * <p>Note, access to the textField field is going to be synchronized, because the user can
547    * request a cancel at any time through the UI.
548    */
549   private static class SimContactQueryCookie implements DialogInterface.OnCancelListener {
550 
551     public ProgressDialog progressDialog;
552     public int contactNum;
553 
554     // Used to identify the query request.
555     private int token;
556     private QueryHandler handler;
557 
558     // The text field we're going to update
559     private EditText textField;
560 
SimContactQueryCookie(int number, QueryHandler handler, int token)561     public SimContactQueryCookie(int number, QueryHandler handler, int token) {
562       contactNum = number;
563       this.handler = handler;
564       this.token = token;
565     }
566 
567     /** Synchronized getter for the EditText. */
getTextField()568     public synchronized EditText getTextField() {
569       return textField;
570     }
571 
572     /** Synchronized setter for the EditText. */
setTextField(EditText text)573     public synchronized void setTextField(EditText text) {
574       textField = text;
575     }
576 
577     /**
578      * Cancel the ADN query by stopping the operation and signaling the cookie that a cancel request
579      * is made.
580      */
581     @Override
onCancel(DialogInterface dialog)582     public synchronized void onCancel(DialogInterface dialog) {
583       // close the progress dialog
584       if (progressDialog != null) {
585         progressDialog.dismiss();
586       }
587 
588       // setting the textfield to null ensures that the UI does NOT get
589       // updated.
590       textField = null;
591 
592       // Cancel the operation if possible.
593       handler.cancelOperation(token);
594     }
595   }
596 
597   /**
598    * Asynchronous query handler that services requests to look up ADNs
599    *
600    * <p>Queries originate from {@link #handleAdnEntry}.
601    */
602   private static class QueryHandler extends NoNullCursorAsyncQueryHandler {
603 
604     private boolean canceled;
605 
QueryHandler(ContentResolver cr)606     public QueryHandler(ContentResolver cr) {
607       super(cr);
608     }
609 
610     /** Override basic onQueryComplete to fill in the textfield when we're handed the ADN cursor. */
611     @Override
onNotNullableQueryComplete(int token, Object cookie, Cursor c)612     protected void onNotNullableQueryComplete(int token, Object cookie, Cursor c) {
613       try {
614         previousAdnQueryHandler = null;
615         if (canceled) {
616           return;
617         }
618 
619         SimContactQueryCookie sc = (SimContactQueryCookie) cookie;
620 
621         // close the progress dialog.
622         sc.progressDialog.dismiss();
623 
624         // get the EditText to update or see if the request was cancelled.
625         EditText text = sc.getTextField();
626 
627         // if the TextView is valid, and the cursor is valid and positionable on the
628         // Nth number, then we update the text field and display a toast indicating the
629         // caller name.
630         if ((c != null) && (text != null) && (c.moveToPosition(sc.contactNum))) {
631           String name = c.getString(c.getColumnIndexOrThrow(ADN_NAME_COLUMN_NAME));
632           String number = c.getString(c.getColumnIndexOrThrow(ADN_PHONE_NUMBER_COLUMN_NAME));
633 
634           // fill the text in.
635           text.getText().replace(0, 0, number);
636 
637           // display the name as a toast
638           Context context = sc.progressDialog.getContext();
639           CharSequence msg =
640               ContactDisplayUtils.getTtsSpannedPhoneNumber(
641                   context.getResources(), R.string.menu_callNumber, name);
642           Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
643         }
644       } finally {
645         MoreCloseables.closeQuietly(c);
646       }
647     }
648 
cancel()649     public void cancel() {
650       canceled = true;
651       // Ask AsyncQueryHandler to cancel the whole request. This will fail when the query is
652       // already started.
653       cancelOperation(ADN_QUERY_TOKEN);
654     }
655   }
656 }
657