blob: 994829a50f6f1b5c6657b191d6526f389657938c [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;
Nancy Chen675af1f2014-10-16 18:33:51 -070045import com.android.contacts.common.widget.SelectPhoneAccountDialogFragment;
46import com.android.contacts.common.widget.SelectPhoneAccountDialogFragment.SelectPhoneAccountListener;
47import com.android.dialer.calllog.PhoneAccountUtils;
Yorke Lee4aece952015-05-02 22:22:54 -070048import com.android.dialer.util.TelecomUtil;
Chiao Cheng91197042012-08-24 14:19:37 -070049
Nancy Chen8c258ac2014-10-20 19:33:55 -070050import java.util.ArrayList;
51import java.util.List;
52
Chiao Cheng91197042012-08-24 14:19:37 -070053/**
54 * Helper class to listen for some magic character sequences
55 * that are handled specially by the dialer.
56 *
57 * Note the Phone app also handles these sequences too (in a couple of
Jake Hamby1d6fb572013-04-09 15:49:56 -070058 * relatively obscure places in the UI), so there's a separate version of
Chiao Cheng91197042012-08-24 14:19:37 -070059 * this class under apps/Phone.
60 *
61 * TODO: there's lots of duplicated code between this class and the
62 * corresponding class under apps/Phone. Let's figure out a way to
63 * unify these two classes (in the framework? in a common shared library?)
64 */
65public class SpecialCharSequenceMgr {
66 private static final String TAG = "SpecialCharSequenceMgr";
Jake Hamby1d6fb572013-04-09 15:49:56 -070067
Jay Shrauner384eaad2015-04-03 16:03:41 -070068 private static final String TAG_SELECT_ACCT_FRAGMENT = "tag_select_acct_fragment";
69
Yorke Leef90dada2013-12-09 11:50:28 -080070 private static final String SECRET_CODE_ACTION = "android.provider.Telephony.SECRET_CODE";
Chiao Cheng91197042012-08-24 14:19:37 -070071 private static final String MMI_IMEI_DISPLAY = "*#06#";
Jake Hamby1d6fb572013-04-09 15:49:56 -070072 private static final String MMI_REGULATORY_INFO_DISPLAY = "*#07#";
Chiao Cheng91197042012-08-24 14:19:37 -070073
74 /**
75 * Remembers the previous {@link QueryHandler} and cancel the operation when needed, to
76 * prevent possible crash.
77 *
78 * QueryHandler may call {@link ProgressDialog#dismiss()} when the screen is already gone,
79 * which will cause the app crash. This variable enables the class to prevent the crash
80 * on {@link #cleanup()}.
81 *
82 * TODO: Remove this and replace it (and {@link #cleanup()}) with better implementation.
Jake Hamby1d6fb572013-04-09 15:49:56 -070083 * One complication is that we have SpecialCharSequenceMgr in Phone package too, which has
Chiao Cheng91197042012-08-24 14:19:37 -070084 * *slightly* different implementation. Note that Phone package doesn't have this problem,
85 * so the class on Phone side doesn't have this functionality.
86 * Fundamental fix would be to have one shared implementation and resolve this corner case more
87 * gracefully.
88 */
89 private static QueryHandler sPreviousAdnQueryHandler;
90
Yorke Lee89e784b2015-06-10 13:36:15 -070091 public static class HandleAdnEntryAccountSelectedCallback extends SelectPhoneAccountListener{
Yorke Lee762b3572015-09-18 12:54:59 -070092 final private Context mContext;
Yorke Lee89e784b2015-06-10 13:36:15 -070093 final private QueryHandler mQueryHandler;
94 final private SimContactQueryCookie mCookie;
95
Yorke Lee762b3572015-09-18 12:54:59 -070096 public HandleAdnEntryAccountSelectedCallback(Context context,
Yorke Lee89e784b2015-06-10 13:36:15 -070097 QueryHandler queryHandler, SimContactQueryCookie cookie) {
Yorke Lee762b3572015-09-18 12:54:59 -070098 mContext = context;
Yorke Lee89e784b2015-06-10 13:36:15 -070099 mQueryHandler = queryHandler;
100 mCookie = cookie;
101 }
102
103 @Override
104 public void onPhoneAccountSelected(PhoneAccountHandle selectedAccountHandle,
105 boolean setDefault) {
Yorke Lee762b3572015-09-18 12:54:59 -0700106 Uri uri = TelecomUtil.getAdnUriForPhoneAccount(mContext, selectedAccountHandle);
Yorke Lee89e784b2015-06-10 13:36:15 -0700107 handleAdnQuery(mQueryHandler, mCookie, uri);
108 // TODO: Show error dialog if result isn't valid.
109 }
110
111 }
112
113 public static class HandleMmiAccountSelectedCallback extends SelectPhoneAccountListener{
114 final private Context mContext;
115 final private String mInput;
116 public HandleMmiAccountSelectedCallback(Context context, String input) {
117 mContext = context.getApplicationContext();
118 mInput = input;
119 }
120
121 @Override
122 public void onPhoneAccountSelected(PhoneAccountHandle selectedAccountHandle,
123 boolean setDefault) {
124 TelecomUtil.handleMmi(mContext, mInput, selectedAccountHandle);
125 }
126 }
127
Chiao Cheng91197042012-08-24 14:19:37 -0700128 /** This class is never instantiated. */
129 private SpecialCharSequenceMgr() {
130 }
131
132 public static boolean handleChars(Context context, String input, EditText textField) {
Chiao Cheng91197042012-08-24 14:19:37 -0700133 //get rid of the separators so that the string gets parsed correctly
134 String dialString = PhoneNumberUtils.stripSeparators(input);
135
Nancy Chen8c258ac2014-10-20 19:33:55 -0700136 if (handleDeviceIdDisplay(context, dialString)
Jake Hamby1d6fb572013-04-09 15:49:56 -0700137 || handleRegulatoryInfoDisplay(context, dialString)
Chiao Cheng91197042012-08-24 14:19:37 -0700138 || handlePinEntry(context, dialString)
139 || handleAdnEntry(context, dialString, textField)
140 || handleSecretCode(context, dialString)) {
141 return true;
142 }
143
144 return false;
145 }
146
147 /**
148 * Cleanup everything around this class. Must be run inside the main thread.
149 *
150 * This should be called when the screen becomes background.
151 */
152 public static void cleanup() {
153 if (Looper.myLooper() != Looper.getMainLooper()) {
154 Log.wtf(TAG, "cleanup() is called outside the main thread");
155 return;
156 }
157
158 if (sPreviousAdnQueryHandler != null) {
159 sPreviousAdnQueryHandler.cancel();
160 sPreviousAdnQueryHandler = null;
161 }
162 }
163
164 /**
165 * Handles secret codes to launch arbitrary activities in the form of *#*#<code>#*#*.
166 * If a secret code is encountered an Intent is started with the android_secret_code://<code>
167 * URI.
168 *
169 * @param context the context to use
170 * @param input the text to check for a secret code in
171 * @return true if a secret code was encountered
172 */
173 static boolean handleSecretCode(Context context, String input) {
174 // Secret codes are in the form *#*#<code>#*#*
175 int len = input.length();
176 if (len > 8 && input.startsWith("*#*#") && input.endsWith("#*#*")) {
Yorke Leef90dada2013-12-09 11:50:28 -0800177 final Intent intent = new Intent(SECRET_CODE_ACTION,
Chiao Cheng91197042012-08-24 14:19:37 -0700178 Uri.parse("android_secret_code://" + input.substring(4, len - 4)));
179 context.sendBroadcast(intent);
180 return true;
181 }
182
183 return false;
184 }
185
186 /**
187 * Handle ADN requests by filling in the SIM contact number into the requested
188 * EditText.
189 *
190 * This code works alongside the Asynchronous query handler {@link QueryHandler}
191 * and query cancel handler implemented in {@link SimContactQueryCookie}.
192 */
Yorke Lee89e784b2015-06-10 13:36:15 -0700193 static boolean handleAdnEntry(Context context, String input, EditText textField) {
Yorke Lee762b3572015-09-18 12:54:59 -0700194 context = context.getApplicationContext();
Chiao Cheng91197042012-08-24 14:19:37 -0700195 /* ADN entries are of the form "N(N)(N)#" */
Chiao Cheng91197042012-08-24 14:19:37 -0700196 TelephonyManager telephonyManager =
197 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
198 if (telephonyManager == null
Yorke Lee62280c72013-11-22 18:24:59 -0800199 || telephonyManager.getPhoneType() != TelephonyManager.PHONE_TYPE_GSM) {
Chiao Cheng91197042012-08-24 14:19:37 -0700200 return false;
201 }
202
203 // if the phone is keyguard-restricted, then just ignore this
204 // input. We want to make sure that sim card contacts are NOT
205 // exposed unless the phone is unlocked, and this code can be
206 // accessed from the emergency dialer.
207 KeyguardManager keyguardManager =
208 (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
209 if (keyguardManager.inKeyguardRestrictedInputMode()) {
210 return false;
211 }
212
213 int len = input.length();
214 if ((len > 1) && (len < 5) && (input.endsWith("#"))) {
215 try {
216 // get the ordinal number of the sim contact
Nancy Chen18c52ff2014-10-30 10:25:00 -0700217 final int index = Integer.parseInt(input.substring(0, len-1));
Chiao Cheng91197042012-08-24 14:19:37 -0700218
219 // The original code that navigated to a SIM Contacts list view did not
220 // highlight the requested contact correctly, a requirement for PTCRB
221 // certification. This behaviour is consistent with the UI paradigm
222 // for touch-enabled lists, so it does not make sense to try to work
223 // around it. Instead we fill in the the requested phone number into
224 // the dialer text field.
225
226 // create the async query handler
Nancy Chen18c52ff2014-10-30 10:25:00 -0700227 final QueryHandler handler = new QueryHandler (context.getContentResolver());
Chiao Cheng91197042012-08-24 14:19:37 -0700228
229 // create the cookie object
Nancy Chen18c52ff2014-10-30 10:25:00 -0700230 final SimContactQueryCookie sc = new SimContactQueryCookie(index - 1, handler,
Chiao Cheng91197042012-08-24 14:19:37 -0700231 ADN_QUERY_TOKEN);
232
233 // setup the cookie fields
234 sc.contactNum = index - 1;
235 sc.setTextField(textField);
236
237 // create the progress dialog
238 sc.progressDialog = new ProgressDialog(context);
239 sc.progressDialog.setTitle(R.string.simContacts_title);
240 sc.progressDialog.setMessage(context.getText(R.string.simContacts_emptyLoading));
241 sc.progressDialog.setIndeterminate(true);
242 sc.progressDialog.setCancelable(true);
243 sc.progressDialog.setOnCancelListener(sc);
244 sc.progressDialog.getWindow().addFlags(
245 WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
246
Nancy Chen39c18f22014-12-03 16:37:45 -0800247 List<PhoneAccountHandle> subscriptionAccountHandles =
Nancy Chen18c52ff2014-10-30 10:25:00 -0700248 PhoneAccountUtils.getSubscriptionPhoneAccounts(context);
Chiao Cheng91197042012-08-24 14:19:37 -0700249
Nancy Chen39c18f22014-12-03 16:37:45 -0800250 boolean hasUserSelectedDefault = subscriptionAccountHandles.contains(
Yorke Lee762b3572015-09-18 12:54:59 -0700251 TelecomUtil.getDefaultOutgoingPhoneAccount(context,
252 PhoneAccount.SCHEME_TEL));
Chiao Cheng91197042012-08-24 14:19:37 -0700253
Nancy Chen39c18f22014-12-03 16:37:45 -0800254 if (subscriptionAccountHandles.size() == 1 || hasUserSelectedDefault) {
Yorke Lee762b3572015-09-18 12:54:59 -0700255 Uri uri = TelecomUtil.getAdnUriForPhoneAccount(context, null);
Nancy Chen18c52ff2014-10-30 10:25:00 -0700256 handleAdnQuery(handler, sc, uri);
Nancy Chen39c18f22014-12-03 16:37:45 -0800257 } else if (subscriptionAccountHandles.size() > 1){
Yorke Lee89e784b2015-06-10 13:36:15 -0700258 SelectPhoneAccountListener callback =
Yorke Lee762b3572015-09-18 12:54:59 -0700259 new HandleAdnEntryAccountSelectedCallback(context, handler, sc);
Nancy Chen18c52ff2014-10-30 10:25:00 -0700260
Jay Shrauner384eaad2015-04-03 16:03:41 -0700261 DialogFragment dialogFragment = SelectPhoneAccountDialogFragment.newInstance(
Yorke Lee89e784b2015-06-10 13:36:15 -0700262 subscriptionAccountHandles, callback);
Jay Shrauner384eaad2015-04-03 16:03:41 -0700263 dialogFragment.show(((Activity) context).getFragmentManager(),
264 TAG_SELECT_ACCT_FRAGMENT);
Nancy Chen18c52ff2014-10-30 10:25:00 -0700265 } else {
266 return false;
Chiao Cheng91197042012-08-24 14:19:37 -0700267 }
Nancy Chen18c52ff2014-10-30 10:25:00 -0700268
Chiao Cheng91197042012-08-24 14:19:37 -0700269 return true;
270 } catch (NumberFormatException ex) {
271 // Ignore
272 }
273 }
274 return false;
275 }
276
Nancy Chen18c52ff2014-10-30 10:25:00 -0700277 private static void handleAdnQuery(QueryHandler handler, SimContactQueryCookie cookie,
278 Uri uri) {
279 if (handler == null || cookie == null || uri == null) {
280 Log.w(TAG, "queryAdn parameters incorrect");
281 return;
282 }
283
284 // display the progress dialog
285 cookie.progressDialog.show();
286
287 // run the query.
288 handler.startQuery(ADN_QUERY_TOKEN, cookie, uri, new String[]{ADN_PHONE_NUMBER_COLUMN_NAME},
289 null, null, null);
290
291 if (sPreviousAdnQueryHandler != null) {
292 // It is harmless to call cancel() even after the handler's gone.
293 sPreviousAdnQueryHandler.cancel();
294 }
295 sPreviousAdnQueryHandler = handler;
296 }
297
Yorke Lee4aece952015-05-02 22:22:54 -0700298 static boolean handlePinEntry(final Context context, final String input) {
Chiao Cheng91197042012-08-24 14:19:37 -0700299 if ((input.startsWith("**04") || input.startsWith("**05")) && input.endsWith("#")) {
Nancy Chen39c18f22014-12-03 16:37:45 -0800300 List<PhoneAccountHandle> subscriptionAccountHandles =
Nancy Chen675af1f2014-10-16 18:33:51 -0700301 PhoneAccountUtils.getSubscriptionPhoneAccounts(context);
Nancy Chen39c18f22014-12-03 16:37:45 -0800302 boolean hasUserSelectedDefault = subscriptionAccountHandles.contains(
Yorke Lee762b3572015-09-18 12:54:59 -0700303 TelecomUtil.getDefaultOutgoingPhoneAccount(context, PhoneAccount.SCHEME_TEL));
Nancy Chen675af1f2014-10-16 18:33:51 -0700304
Nancy Chen39c18f22014-12-03 16:37:45 -0800305 if (subscriptionAccountHandles.size() == 1 || hasUserSelectedDefault) {
Nancy Chen675af1f2014-10-16 18:33:51 -0700306 // Don't bring up the dialog for single-SIM or if the default outgoing account is
307 // a subscription account.
Yorke Lee4aece952015-05-02 22:22:54 -0700308 return TelecomUtil.handleMmi(context, input, null);
Nancy Chen39c18f22014-12-03 16:37:45 -0800309 } else if (subscriptionAccountHandles.size() > 1){
Yorke Lee89e784b2015-06-10 13:36:15 -0700310 SelectPhoneAccountListener listener =
311 new HandleMmiAccountSelectedCallback(context, input);
Nancy Chen675af1f2014-10-16 18:33:51 -0700312
Jay Shrauner384eaad2015-04-03 16:03:41 -0700313 DialogFragment dialogFragment = SelectPhoneAccountDialogFragment.newInstance(
314 subscriptionAccountHandles, listener);
315 dialogFragment.show(((Activity) context).getFragmentManager(),
316 TAG_SELECT_ACCT_FRAGMENT);
Nancy Chen675af1f2014-10-16 18:33:51 -0700317 }
318 return true;
319 }
320 return false;
321 }
322
Nancy Chen8c258ac2014-10-20 19:33:55 -0700323 // TODO: Use TelephonyCapabilities.getDeviceIdLabel() to get the device id label instead of a
324 // hard-coded string.
325 static boolean handleDeviceIdDisplay(Context context, String input) {
Chiao Cheng91197042012-08-24 14:19:37 -0700326 TelephonyManager telephonyManager =
327 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Chiao Cheng91197042012-08-24 14:19:37 -0700328
Nancy Chen8c258ac2014-10-20 19:33:55 -0700329 if (telephonyManager != null && input.equals(MMI_IMEI_DISPLAY)) {
330 int labelResId = (telephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) ?
331 R.string.imei : R.string.meid;
332
333 List<String> deviceIds = new ArrayList<String>();
334 for (int slot = 0; slot < telephonyManager.getPhoneCount(); slot++) {
Nancy Chen0f4ec2a2015-03-23 15:03:03 -0700335 String deviceId = telephonyManager.getDeviceId(slot);
336 if (!TextUtils.isEmpty(deviceId)) {
337 deviceIds.add(deviceId);
338 }
Nancy Chen8c258ac2014-10-20 19:33:55 -0700339 }
340
341 AlertDialog alert = new AlertDialog.Builder(context)
342 .setTitle(labelResId)
343 .setItems(deviceIds.toArray(new String[deviceIds.size()]), null)
Nancy Chen0f4ec2a2015-03-23 15:03:03 -0700344 .setPositiveButton(android.R.string.ok, null)
Nancy Chen8c258ac2014-10-20 19:33:55 -0700345 .setCancelable(false)
346 .show();
347 return true;
348 }
Chiao Cheng91197042012-08-24 14:19:37 -0700349 return false;
350 }
351
Jake Hamby1d6fb572013-04-09 15:49:56 -0700352 private static boolean handleRegulatoryInfoDisplay(Context context, String input) {
353 if (input.equals(MMI_REGULATORY_INFO_DISPLAY)) {
354 Log.d(TAG, "handleRegulatoryInfoDisplay() sending intent to settings app");
Jeff Sharkeyf4f47662014-04-16 17:21:12 -0700355 Intent showRegInfoIntent = new Intent(Settings.ACTION_SHOW_REGULATORY_INFO);
Jake Hamby1d6fb572013-04-09 15:49:56 -0700356 try {
357 context.startActivity(showRegInfoIntent);
358 } catch (ActivityNotFoundException e) {
359 Log.e(TAG, "startActivity() failed: " + e);
360 }
361 return true;
362 }
363 return false;
364 }
365
Chiao Cheng91197042012-08-24 14:19:37 -0700366 /*******
367 * This code is used to handle SIM Contact queries
368 *******/
369 private static final String ADN_PHONE_NUMBER_COLUMN_NAME = "number";
370 private static final String ADN_NAME_COLUMN_NAME = "name";
371 private static final int ADN_QUERY_TOKEN = -1;
372
373 /**
374 * Cookie object that contains everything we need to communicate to the
375 * handler's onQuery Complete, as well as what we need in order to cancel
376 * the query (if requested).
377 *
378 * Note, access to the textField field is going to be synchronized, because
379 * the user can request a cancel at any time through the UI.
380 */
381 private static class SimContactQueryCookie implements DialogInterface.OnCancelListener{
382 public ProgressDialog progressDialog;
383 public int contactNum;
384
385 // Used to identify the query request.
386 private int mToken;
387 private QueryHandler mHandler;
388
389 // The text field we're going to update
390 private EditText textField;
391
392 public SimContactQueryCookie(int number, QueryHandler handler, int token) {
393 contactNum = number;
394 mHandler = handler;
395 mToken = token;
396 }
397
398 /**
399 * Synchronized getter for the EditText.
400 */
401 public synchronized EditText getTextField() {
402 return textField;
403 }
404
405 /**
406 * Synchronized setter for the EditText.
407 */
408 public synchronized void setTextField(EditText text) {
409 textField = text;
410 }
411
412 /**
413 * Cancel the ADN query by stopping the operation and signaling
414 * the cookie that a cancel request is made.
415 */
416 public synchronized void onCancel(DialogInterface dialog) {
417 // close the progress dialog
418 if (progressDialog != null) {
419 progressDialog.dismiss();
420 }
421
422 // setting the textfield to null ensures that the UI does NOT get
423 // updated.
424 textField = null;
425
426 // Cancel the operation if possible.
427 mHandler.cancelOperation(mToken);
428 }
429 }
430
431 /**
432 * Asynchronous query handler that services requests to look up ADNs
433 *
Jake Hamby1d6fb572013-04-09 15:49:56 -0700434 * Queries originate from {@link #handleAdnEntry}.
Chiao Cheng91197042012-08-24 14:19:37 -0700435 */
Chiao Cheng07af7642012-09-14 12:05:14 -0700436 private static class QueryHandler extends NoNullCursorAsyncQueryHandler {
Chiao Cheng91197042012-08-24 14:19:37 -0700437
438 private boolean mCanceled;
439
440 public QueryHandler(ContentResolver cr) {
441 super(cr);
442 }
443
444 /**
445 * Override basic onQueryComplete to fill in the textfield when
446 * we're handed the ADN cursor.
447 */
448 @Override
Chiao Cheng07af7642012-09-14 12:05:14 -0700449 protected void onNotNullableQueryComplete(int token, Object cookie, Cursor c) {
Jay Shraunerede67ec2014-09-11 15:03:36 -0700450 try {
451 sPreviousAdnQueryHandler = null;
452 if (mCanceled) {
453 return;
454 }
Chiao Cheng91197042012-08-24 14:19:37 -0700455
Jay Shraunerede67ec2014-09-11 15:03:36 -0700456 SimContactQueryCookie sc = (SimContactQueryCookie) cookie;
Chiao Cheng91197042012-08-24 14:19:37 -0700457
Jay Shraunerede67ec2014-09-11 15:03:36 -0700458 // close the progress dialog.
459 sc.progressDialog.dismiss();
Chiao Cheng91197042012-08-24 14:19:37 -0700460
Jay Shraunerede67ec2014-09-11 15:03:36 -0700461 // get the EditText to update or see if the request was cancelled.
462 EditText text = sc.getTextField();
Chiao Cheng91197042012-08-24 14:19:37 -0700463
Nancy Chen18c52ff2014-10-30 10:25:00 -0700464 // if the TextView is valid, and the cursor is valid and positionable on the
465 // Nth number, then we update the text field and display a toast indicating the
466 // caller name.
Jay Shraunerede67ec2014-09-11 15:03:36 -0700467 if ((c != null) && (text != null) && (c.moveToPosition(sc.contactNum))) {
468 String name = c.getString(c.getColumnIndexOrThrow(ADN_NAME_COLUMN_NAME));
Nancy Chen18c52ff2014-10-30 10:25:00 -0700469 String number =
470 c.getString(c.getColumnIndexOrThrow(ADN_PHONE_NUMBER_COLUMN_NAME));
Chiao Cheng91197042012-08-24 14:19:37 -0700471
Jay Shraunerede67ec2014-09-11 15:03:36 -0700472 // fill the text in.
473 text.getText().replace(0, 0, number);
Chiao Cheng91197042012-08-24 14:19:37 -0700474
Jay Shraunerede67ec2014-09-11 15:03:36 -0700475 // display the name as a toast
476 Context context = sc.progressDialog.getContext();
477 name = context.getString(R.string.menu_callNumber, name);
478 Toast.makeText(context, name, Toast.LENGTH_SHORT)
479 .show();
480 }
481 } finally {
482 MoreCloseables.closeQuietly(c);
Chiao Cheng91197042012-08-24 14:19:37 -0700483 }
484 }
485
486 public void cancel() {
487 mCanceled = true;
Nancy Chen18c52ff2014-10-30 10:25:00 -0700488 // Ask AsyncQueryHandler to cancel the whole request. This will fail when the query is
489 // already started.
Chiao Cheng91197042012-08-24 14:19:37 -0700490 cancelOperation(ADN_QUERY_TOKEN);
491 }
492 }
493}