blob: c212597f6fe3a9eb9e47daa624ce531dd881e0f4 [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;
Chiao Cheng07af7642012-09-14 12:05:14 -070045import com.android.contacts.common.database.NoNullCursorAsyncQueryHandler;
Ta-wei Yen6d98cf62015-11-05 17:33:58 -080046import com.android.contacts.common.util.ContactDisplayUtils;
Nancy Chen675af1f2014-10-16 18:33:51 -070047import com.android.contacts.common.widget.SelectPhoneAccountDialogFragment;
48import com.android.contacts.common.widget.SelectPhoneAccountDialogFragment.SelectPhoneAccountListener;
49import com.android.dialer.calllog.PhoneAccountUtils;
Yorke Lee4aece952015-05-02 22:22:54 -070050import com.android.dialer.util.TelecomUtil;
Chiao Cheng91197042012-08-24 14:19:37 -070051
Nancy Chen8c258ac2014-10-20 19:33:55 -070052import java.util.ArrayList;
53import java.util.List;
54
Chiao Cheng91197042012-08-24 14:19:37 -070055/**
56 * Helper class to listen for some magic character sequences
57 * that are handled specially by the dialer.
58 *
59 * Note the Phone app also handles these sequences too (in a couple of
Jake Hamby1d6fb572013-04-09 15:49:56 -070060 * relatively obscure places in the UI), so there's a separate version of
Chiao Cheng91197042012-08-24 14:19:37 -070061 * this class under apps/Phone.
62 *
63 * TODO: there's lots of duplicated code between this class and the
64 * corresponding class under apps/Phone. Let's figure out a way to
65 * unify these two classes (in the framework? in a common shared library?)
66 */
67public class SpecialCharSequenceMgr {
68 private static final String TAG = "SpecialCharSequenceMgr";
Jake Hamby1d6fb572013-04-09 15:49:56 -070069
Jay Shrauner384eaad2015-04-03 16:03:41 -070070 private static final String TAG_SELECT_ACCT_FRAGMENT = "tag_select_acct_fragment";
71
Yorke Leef90dada2013-12-09 11:50:28 -080072 private static final String SECRET_CODE_ACTION = "android.provider.Telephony.SECRET_CODE";
Chiao Cheng91197042012-08-24 14:19:37 -070073 private static final String MMI_IMEI_DISPLAY = "*#06#";
Jake Hamby1d6fb572013-04-09 15:49:56 -070074 private static final String MMI_REGULATORY_INFO_DISPLAY = "*#07#";
Chiao Cheng91197042012-08-24 14:19:37 -070075
76 /**
77 * Remembers the previous {@link QueryHandler} and cancel the operation when needed, to
78 * prevent possible crash.
79 *
80 * QueryHandler may call {@link ProgressDialog#dismiss()} when the screen is already gone,
81 * which will cause the app crash. This variable enables the class to prevent the crash
82 * on {@link #cleanup()}.
83 *
84 * TODO: Remove this and replace it (and {@link #cleanup()}) with better implementation.
Jake Hamby1d6fb572013-04-09 15:49:56 -070085 * One complication is that we have SpecialCharSequenceMgr in Phone package too, which has
Chiao Cheng91197042012-08-24 14:19:37 -070086 * *slightly* different implementation. Note that Phone package doesn't have this problem,
87 * so the class on Phone side doesn't have this functionality.
88 * Fundamental fix would be to have one shared implementation and resolve this corner case more
89 * gracefully.
90 */
91 private static QueryHandler sPreviousAdnQueryHandler;
92
Yorke Lee89e784b2015-06-10 13:36:15 -070093 public static class HandleAdnEntryAccountSelectedCallback extends SelectPhoneAccountListener{
Yorke Lee762b3572015-09-18 12:54:59 -070094 final private Context mContext;
Yorke Lee89e784b2015-06-10 13:36:15 -070095 final private QueryHandler mQueryHandler;
96 final private SimContactQueryCookie mCookie;
97
Yorke Lee762b3572015-09-18 12:54:59 -070098 public HandleAdnEntryAccountSelectedCallback(Context context,
Yorke Lee89e784b2015-06-10 13:36:15 -070099 QueryHandler queryHandler, SimContactQueryCookie cookie) {
Yorke Lee762b3572015-09-18 12:54:59 -0700100 mContext = context;
Yorke Lee89e784b2015-06-10 13:36:15 -0700101 mQueryHandler = queryHandler;
102 mCookie = cookie;
103 }
104
105 @Override
106 public void onPhoneAccountSelected(PhoneAccountHandle selectedAccountHandle,
107 boolean setDefault) {
Yorke Lee762b3572015-09-18 12:54:59 -0700108 Uri uri = TelecomUtil.getAdnUriForPhoneAccount(mContext, selectedAccountHandle);
Yorke Lee89e784b2015-06-10 13:36:15 -0700109 handleAdnQuery(mQueryHandler, mCookie, uri);
110 // TODO: Show error dialog if result isn't valid.
111 }
112
113 }
114
115 public static class HandleMmiAccountSelectedCallback extends SelectPhoneAccountListener{
116 final private Context mContext;
117 final private String mInput;
118 public HandleMmiAccountSelectedCallback(Context context, String input) {
119 mContext = context.getApplicationContext();
120 mInput = input;
121 }
122
123 @Override
124 public void onPhoneAccountSelected(PhoneAccountHandle selectedAccountHandle,
125 boolean setDefault) {
126 TelecomUtil.handleMmi(mContext, mInput, selectedAccountHandle);
127 }
128 }
129
Chiao Cheng91197042012-08-24 14:19:37 -0700130 /** This class is never instantiated. */
131 private SpecialCharSequenceMgr() {
132 }
133
134 public static boolean handleChars(Context context, String input, EditText textField) {
Chiao Cheng91197042012-08-24 14:19:37 -0700135 //get rid of the separators so that the string gets parsed correctly
136 String dialString = PhoneNumberUtils.stripSeparators(input);
137
Nancy Chen8c258ac2014-10-20 19:33:55 -0700138 if (handleDeviceIdDisplay(context, dialString)
Jake Hamby1d6fb572013-04-09 15:49:56 -0700139 || handleRegulatoryInfoDisplay(context, dialString)
Chiao Cheng91197042012-08-24 14:19:37 -0700140 || handlePinEntry(context, dialString)
141 || handleAdnEntry(context, dialString, textField)
142 || handleSecretCode(context, dialString)) {
143 return true;
144 }
145
146 return false;
147 }
148
149 /**
150 * Cleanup everything around this class. Must be run inside the main thread.
151 *
152 * This should be called when the screen becomes background.
153 */
154 public static void cleanup() {
155 if (Looper.myLooper() != Looper.getMainLooper()) {
156 Log.wtf(TAG, "cleanup() is called outside the main thread");
157 return;
158 }
159
160 if (sPreviousAdnQueryHandler != null) {
161 sPreviousAdnQueryHandler.cancel();
162 sPreviousAdnQueryHandler = null;
163 }
164 }
165
166 /**
167 * Handles secret codes to launch arbitrary activities in the form of *#*#<code>#*#*.
168 * If a secret code is encountered an Intent is started with the android_secret_code://<code>
169 * URI.
170 *
171 * @param context the context to use
172 * @param input the text to check for a secret code in
173 * @return true if a secret code was encountered
174 */
175 static boolean handleSecretCode(Context context, String input) {
176 // Secret codes are in the form *#*#<code>#*#*
177 int len = input.length();
178 if (len > 8 && input.startsWith("*#*#") && input.endsWith("#*#*")) {
Yorke Leef90dada2013-12-09 11:50:28 -0800179 final Intent intent = new Intent(SECRET_CODE_ACTION,
Chiao Cheng91197042012-08-24 14:19:37 -0700180 Uri.parse("android_secret_code://" + input.substring(4, len - 4)));
181 context.sendBroadcast(intent);
182 return true;
183 }
184
185 return false;
186 }
187
188 /**
189 * Handle ADN requests by filling in the SIM contact number into the requested
190 * EditText.
191 *
192 * This code works alongside the Asynchronous query handler {@link QueryHandler}
193 * and query cancel handler implemented in {@link SimContactQueryCookie}.
194 */
Yorke Lee89e784b2015-06-10 13:36:15 -0700195 static boolean handleAdnEntry(Context context, String input, EditText textField) {
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
Brandon Maxwellf6bf9e42015-12-02 12:55:57 -0800228 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);
Brandon Maxwellf6bf9e42015-12-02 12:55:57 -0800250 Context applicationContext = context.getApplicationContext();
Nancy Chen39c18f22014-12-03 16:37:45 -0800251 boolean hasUserSelectedDefault = subscriptionAccountHandles.contains(
Brandon Maxwellf6bf9e42015-12-02 12:55:57 -0800252 TelecomUtil.getDefaultOutgoingPhoneAccount(applicationContext,
Yorke Lee762b3572015-09-18 12:54:59 -0700253 PhoneAccount.SCHEME_TEL));
Chiao Cheng91197042012-08-24 14:19:37 -0700254
Nancy Chenee5b3dc2015-12-08 16:52:51 -0800255 if (!CompatUtils.isMSIMCompatible()) {
256 handleAdnQuery(handler, sc, Uri.parse("content://icc/adn"));
257 } else if (subscriptionAccountHandles.size() == 1 || hasUserSelectedDefault) {
Brandon Maxwellf6bf9e42015-12-02 12:55:57 -0800258 Uri uri = TelecomUtil.getAdnUriForPhoneAccount(applicationContext, null);
Nancy Chen18c52ff2014-10-30 10:25:00 -0700259 handleAdnQuery(handler, sc, uri);
Nancy Chen39c18f22014-12-03 16:37:45 -0800260 } else if (subscriptionAccountHandles.size() > 1){
Brandon Maxwellf6bf9e42015-12-02 12:55:57 -0800261 SelectPhoneAccountListener callback = new HandleAdnEntryAccountSelectedCallback(
262 applicationContext, handler, sc);
Nancy Chen18c52ff2014-10-30 10:25:00 -0700263
Jay Shrauner384eaad2015-04-03 16:03:41 -0700264 DialogFragment dialogFragment = SelectPhoneAccountDialogFragment.newInstance(
Yorke Lee89e784b2015-06-10 13:36:15 -0700265 subscriptionAccountHandles, callback);
Jay Shrauner384eaad2015-04-03 16:03:41 -0700266 dialogFragment.show(((Activity) context).getFragmentManager(),
267 TAG_SELECT_ACCT_FRAGMENT);
Nancy Chen18c52ff2014-10-30 10:25:00 -0700268 } else {
269 return false;
Chiao Cheng91197042012-08-24 14:19:37 -0700270 }
Nancy Chen18c52ff2014-10-30 10:25:00 -0700271
Chiao Cheng91197042012-08-24 14:19:37 -0700272 return true;
273 } catch (NumberFormatException ex) {
274 // Ignore
275 }
276 }
277 return false;
278 }
279
Nancy Chen18c52ff2014-10-30 10:25:00 -0700280 private static void handleAdnQuery(QueryHandler handler, SimContactQueryCookie cookie,
281 Uri uri) {
282 if (handler == null || cookie == null || uri == null) {
283 Log.w(TAG, "queryAdn parameters incorrect");
284 return;
285 }
286
287 // display the progress dialog
288 cookie.progressDialog.show();
289
290 // run the query.
291 handler.startQuery(ADN_QUERY_TOKEN, cookie, uri, new String[]{ADN_PHONE_NUMBER_COLUMN_NAME},
292 null, null, null);
293
294 if (sPreviousAdnQueryHandler != null) {
295 // It is harmless to call cancel() even after the handler's gone.
296 sPreviousAdnQueryHandler.cancel();
297 }
298 sPreviousAdnQueryHandler = handler;
299 }
300
Yorke Lee4aece952015-05-02 22:22:54 -0700301 static boolean handlePinEntry(final Context context, final String input) {
Chiao Cheng91197042012-08-24 14:19:37 -0700302 if ((input.startsWith("**04") || input.startsWith("**05")) && input.endsWith("#")) {
Nancy Chen39c18f22014-12-03 16:37:45 -0800303 List<PhoneAccountHandle> subscriptionAccountHandles =
Nancy Chen675af1f2014-10-16 18:33:51 -0700304 PhoneAccountUtils.getSubscriptionPhoneAccounts(context);
Nancy Chen39c18f22014-12-03 16:37:45 -0800305 boolean hasUserSelectedDefault = subscriptionAccountHandles.contains(
Yorke Lee762b3572015-09-18 12:54:59 -0700306 TelecomUtil.getDefaultOutgoingPhoneAccount(context, PhoneAccount.SCHEME_TEL));
Nancy Chen675af1f2014-10-16 18:33:51 -0700307
Nancy Chenee5b3dc2015-12-08 16:52:51 -0800308 if (!CompatUtils.isMSIMCompatible() || subscriptionAccountHandles.size() == 1
309 || hasUserSelectedDefault) {
Nancy Chen675af1f2014-10-16 18:33:51 -0700310 // Don't bring up the dialog for single-SIM or if the default outgoing account is
311 // a subscription account.
Yorke Lee4aece952015-05-02 22:22:54 -0700312 return TelecomUtil.handleMmi(context, input, null);
Nancy Chen39c18f22014-12-03 16:37:45 -0800313 } else if (subscriptionAccountHandles.size() > 1){
Yorke Lee89e784b2015-06-10 13:36:15 -0700314 SelectPhoneAccountListener listener =
315 new HandleMmiAccountSelectedCallback(context, input);
Nancy Chen675af1f2014-10-16 18:33:51 -0700316
Jay Shrauner384eaad2015-04-03 16:03:41 -0700317 DialogFragment dialogFragment = SelectPhoneAccountDialogFragment.newInstance(
318 subscriptionAccountHandles, listener);
319 dialogFragment.show(((Activity) context).getFragmentManager(),
320 TAG_SELECT_ACCT_FRAGMENT);
Nancy Chen675af1f2014-10-16 18:33:51 -0700321 }
322 return true;
323 }
324 return false;
325 }
326
Nancy Chen8c258ac2014-10-20 19:33:55 -0700327 // TODO: Use TelephonyCapabilities.getDeviceIdLabel() to get the device id label instead of a
328 // hard-coded string.
329 static boolean handleDeviceIdDisplay(Context context, String input) {
Chiao Cheng91197042012-08-24 14:19:37 -0700330 TelephonyManager telephonyManager =
331 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Chiao Cheng91197042012-08-24 14:19:37 -0700332
Nancy Chen8c258ac2014-10-20 19:33:55 -0700333 if (telephonyManager != null && input.equals(MMI_IMEI_DISPLAY)) {
334 int labelResId = (telephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) ?
335 R.string.imei : R.string.meid;
336
337 List<String> deviceIds = new ArrayList<String>();
Nancy Chenee5b3dc2015-12-08 16:52:51 -0800338 if (!CompatUtils.isMSIMCompatible()) {
339 deviceIds.add(telephonyManager.getDeviceId());
340 } else {
341 for (int slot = 0; slot < telephonyManager.getPhoneCount(); slot++) {
342 String deviceId = telephonyManager.getDeviceId(slot);
343 if (!TextUtils.isEmpty(deviceId)) {
344 deviceIds.add(deviceId);
345 }
Nancy Chen0f4ec2a2015-03-23 15:03:03 -0700346 }
Nancy Chen8c258ac2014-10-20 19:33:55 -0700347 }
348
349 AlertDialog alert = new AlertDialog.Builder(context)
350 .setTitle(labelResId)
351 .setItems(deviceIds.toArray(new String[deviceIds.size()]), null)
Nancy Chen0f4ec2a2015-03-23 15:03:03 -0700352 .setPositiveButton(android.R.string.ok, null)
Nancy Chen8c258ac2014-10-20 19:33:55 -0700353 .setCancelable(false)
354 .show();
355 return true;
356 }
Chiao Cheng91197042012-08-24 14:19:37 -0700357 return false;
358 }
359
Jake Hamby1d6fb572013-04-09 15:49:56 -0700360 private static boolean handleRegulatoryInfoDisplay(Context context, String input) {
361 if (input.equals(MMI_REGULATORY_INFO_DISPLAY)) {
362 Log.d(TAG, "handleRegulatoryInfoDisplay() sending intent to settings app");
Jeff Sharkeyf4f47662014-04-16 17:21:12 -0700363 Intent showRegInfoIntent = new Intent(Settings.ACTION_SHOW_REGULATORY_INFO);
Jake Hamby1d6fb572013-04-09 15:49:56 -0700364 try {
365 context.startActivity(showRegInfoIntent);
366 } catch (ActivityNotFoundException e) {
367 Log.e(TAG, "startActivity() failed: " + e);
368 }
369 return true;
370 }
371 return false;
372 }
373
Chiao Cheng91197042012-08-24 14:19:37 -0700374 /*******
375 * This code is used to handle SIM Contact queries
376 *******/
377 private static final String ADN_PHONE_NUMBER_COLUMN_NAME = "number";
378 private static final String ADN_NAME_COLUMN_NAME = "name";
379 private static final int ADN_QUERY_TOKEN = -1;
380
381 /**
382 * Cookie object that contains everything we need to communicate to the
383 * handler's onQuery Complete, as well as what we need in order to cancel
384 * the query (if requested).
385 *
386 * Note, access to the textField field is going to be synchronized, because
387 * the user can request a cancel at any time through the UI.
388 */
389 private static class SimContactQueryCookie implements DialogInterface.OnCancelListener{
390 public ProgressDialog progressDialog;
391 public int contactNum;
392
393 // Used to identify the query request.
394 private int mToken;
395 private QueryHandler mHandler;
396
397 // The text field we're going to update
398 private EditText textField;
399
400 public SimContactQueryCookie(int number, QueryHandler handler, int token) {
401 contactNum = number;
402 mHandler = handler;
403 mToken = token;
404 }
405
406 /**
407 * Synchronized getter for the EditText.
408 */
409 public synchronized EditText getTextField() {
410 return textField;
411 }
412
413 /**
414 * Synchronized setter for the EditText.
415 */
416 public synchronized void setTextField(EditText text) {
417 textField = text;
418 }
419
420 /**
421 * Cancel the ADN query by stopping the operation and signaling
422 * the cookie that a cancel request is made.
423 */
424 public synchronized void onCancel(DialogInterface dialog) {
425 // close the progress dialog
426 if (progressDialog != null) {
427 progressDialog.dismiss();
428 }
429
430 // setting the textfield to null ensures that the UI does NOT get
431 // updated.
432 textField = null;
433
434 // Cancel the operation if possible.
435 mHandler.cancelOperation(mToken);
436 }
437 }
438
439 /**
440 * Asynchronous query handler that services requests to look up ADNs
441 *
Jake Hamby1d6fb572013-04-09 15:49:56 -0700442 * Queries originate from {@link #handleAdnEntry}.
Chiao Cheng91197042012-08-24 14:19:37 -0700443 */
Chiao Cheng07af7642012-09-14 12:05:14 -0700444 private static class QueryHandler extends NoNullCursorAsyncQueryHandler {
Chiao Cheng91197042012-08-24 14:19:37 -0700445
446 private boolean mCanceled;
447
448 public QueryHandler(ContentResolver cr) {
449 super(cr);
450 }
451
452 /**
453 * Override basic onQueryComplete to fill in the textfield when
454 * we're handed the ADN cursor.
455 */
456 @Override
Chiao Cheng07af7642012-09-14 12:05:14 -0700457 protected void onNotNullableQueryComplete(int token, Object cookie, Cursor c) {
Jay Shraunerede67ec2014-09-11 15:03:36 -0700458 try {
459 sPreviousAdnQueryHandler = null;
460 if (mCanceled) {
461 return;
462 }
Chiao Cheng91197042012-08-24 14:19:37 -0700463
Jay Shraunerede67ec2014-09-11 15:03:36 -0700464 SimContactQueryCookie sc = (SimContactQueryCookie) cookie;
Chiao Cheng91197042012-08-24 14:19:37 -0700465
Jay Shraunerede67ec2014-09-11 15:03:36 -0700466 // close the progress dialog.
467 sc.progressDialog.dismiss();
Chiao Cheng91197042012-08-24 14:19:37 -0700468
Jay Shraunerede67ec2014-09-11 15:03:36 -0700469 // get the EditText to update or see if the request was cancelled.
470 EditText text = sc.getTextField();
Chiao Cheng91197042012-08-24 14:19:37 -0700471
Nancy Chen18c52ff2014-10-30 10:25:00 -0700472 // if the TextView is valid, and the cursor is valid and positionable on the
473 // Nth number, then we update the text field and display a toast indicating the
474 // caller name.
Jay Shraunerede67ec2014-09-11 15:03:36 -0700475 if ((c != null) && (text != null) && (c.moveToPosition(sc.contactNum))) {
476 String name = c.getString(c.getColumnIndexOrThrow(ADN_NAME_COLUMN_NAME));
Nancy Chen18c52ff2014-10-30 10:25:00 -0700477 String number =
478 c.getString(c.getColumnIndexOrThrow(ADN_PHONE_NUMBER_COLUMN_NAME));
Chiao Cheng91197042012-08-24 14:19:37 -0700479
Jay Shraunerede67ec2014-09-11 15:03:36 -0700480 // fill the text in.
481 text.getText().replace(0, 0, number);
Chiao Cheng91197042012-08-24 14:19:37 -0700482
Jay Shraunerede67ec2014-09-11 15:03:36 -0700483 // display the name as a toast
484 Context context = sc.progressDialog.getContext();
Ta-wei Yenf0017b72015-11-06 16:03:20 -0800485 CharSequence msg = ContactDisplayUtils.getTtsSpannedPhoneNumber(
Ta-wei Yen6d98cf62015-11-05 17:33:58 -0800486 context.getResources(), R.string.menu_callNumber, name);
487 Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
Jay Shraunerede67ec2014-09-11 15:03:36 -0700488 }
489 } finally {
490 MoreCloseables.closeQuietly(c);
Chiao Cheng91197042012-08-24 14:19:37 -0700491 }
492 }
493
494 public void cancel() {
495 mCanceled = true;
Nancy Chen18c52ff2014-10-30 10:25:00 -0700496 // Ask AsyncQueryHandler to cancel the whole request. This will fail when the query is
497 // already started.
Chiao Cheng91197042012-08-24 14:19:37 -0700498 cancelOperation(ADN_QUERY_TOKEN);
499 }
500 }
501}