blob: f466c417c4c778580960352d4c7c434725382042 [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;
21import android.app.KeyguardManager;
22import android.app.ProgressDialog;
Jake Hamby1d6fb572013-04-09 15:49:56 -070023import android.content.ActivityNotFoundException;
Chiao Cheng91197042012-08-24 14:19:37 -070024import android.content.ContentResolver;
25import android.content.Context;
26import android.content.DialogInterface;
27import android.content.Intent;
28import android.database.Cursor;
29import android.net.Uri;
30import android.os.Looper;
Jeff Sharkeyf4f47662014-04-16 17:21:12 -070031import android.provider.Settings;
Nancy Chen675af1f2014-10-16 18:33:51 -070032import android.telecom.PhoneAccountHandle;
Tyler Gunn9dc924c2014-09-12 09:33:50 -070033import android.telecom.TelecomManager;
Chiao Cheng91197042012-08-24 14:19:37 -070034import android.telephony.PhoneNumberUtils;
35import android.telephony.TelephonyManager;
36import android.util.Log;
37import android.view.WindowManager;
38import android.widget.EditText;
39import android.widget.Toast;
40
Jay Shraunerede67ec2014-09-11 15:03:36 -070041import com.android.common.io.MoreCloseables;
Chiao Cheng07af7642012-09-14 12:05:14 -070042import com.android.contacts.common.database.NoNullCursorAsyncQueryHandler;
Nancy Chen675af1f2014-10-16 18:33:51 -070043import com.android.contacts.common.widget.SelectPhoneAccountDialogFragment;
44import com.android.contacts.common.widget.SelectPhoneAccountDialogFragment.SelectPhoneAccountListener;
45import com.android.dialer.calllog.PhoneAccountUtils;
Chiao Cheng91197042012-08-24 14:19:37 -070046
Nancy Chen675af1f2014-10-16 18:33:51 -070047import java.util.Arrays;
Nancy Chen8c258ac2014-10-20 19:33:55 -070048import java.util.ArrayList;
49import java.util.List;
50
Chiao Cheng91197042012-08-24 14:19:37 -070051/**
52 * Helper class to listen for some magic character sequences
53 * that are handled specially by the dialer.
54 *
55 * Note the Phone app also handles these sequences too (in a couple of
Jake Hamby1d6fb572013-04-09 15:49:56 -070056 * relatively obscure places in the UI), so there's a separate version of
Chiao Cheng91197042012-08-24 14:19:37 -070057 * this class under apps/Phone.
58 *
59 * TODO: there's lots of duplicated code between this class and the
60 * corresponding class under apps/Phone. Let's figure out a way to
61 * unify these two classes (in the framework? in a common shared library?)
62 */
63public class SpecialCharSequenceMgr {
64 private static final String TAG = "SpecialCharSequenceMgr";
Jake Hamby1d6fb572013-04-09 15:49:56 -070065
Yorke Leef90dada2013-12-09 11:50:28 -080066 private static final String SECRET_CODE_ACTION = "android.provider.Telephony.SECRET_CODE";
Chiao Cheng91197042012-08-24 14:19:37 -070067 private static final String MMI_IMEI_DISPLAY = "*#06#";
Jake Hamby1d6fb572013-04-09 15:49:56 -070068 private static final String MMI_REGULATORY_INFO_DISPLAY = "*#07#";
Chiao Cheng91197042012-08-24 14:19:37 -070069
70 /**
71 * Remembers the previous {@link QueryHandler} and cancel the operation when needed, to
72 * prevent possible crash.
73 *
74 * QueryHandler may call {@link ProgressDialog#dismiss()} when the screen is already gone,
75 * which will cause the app crash. This variable enables the class to prevent the crash
76 * on {@link #cleanup()}.
77 *
78 * TODO: Remove this and replace it (and {@link #cleanup()}) with better implementation.
Jake Hamby1d6fb572013-04-09 15:49:56 -070079 * One complication is that we have SpecialCharSequenceMgr in Phone package too, which has
Chiao Cheng91197042012-08-24 14:19:37 -070080 * *slightly* different implementation. Note that Phone package doesn't have this problem,
81 * so the class on Phone side doesn't have this functionality.
82 * Fundamental fix would be to have one shared implementation and resolve this corner case more
83 * gracefully.
84 */
85 private static QueryHandler sPreviousAdnQueryHandler;
86
87 /** This class is never instantiated. */
88 private SpecialCharSequenceMgr() {
89 }
90
91 public static boolean handleChars(Context context, String input, EditText textField) {
Chiao Cheng91197042012-08-24 14:19:37 -070092 //get rid of the separators so that the string gets parsed correctly
93 String dialString = PhoneNumberUtils.stripSeparators(input);
94
Nancy Chen8c258ac2014-10-20 19:33:55 -070095 if (handleDeviceIdDisplay(context, dialString)
Jake Hamby1d6fb572013-04-09 15:49:56 -070096 || handleRegulatoryInfoDisplay(context, dialString)
Chiao Cheng91197042012-08-24 14:19:37 -070097 || handlePinEntry(context, dialString)
98 || handleAdnEntry(context, dialString, textField)
99 || handleSecretCode(context, dialString)) {
100 return true;
101 }
102
103 return false;
104 }
105
106 /**
107 * Cleanup everything around this class. Must be run inside the main thread.
108 *
109 * This should be called when the screen becomes background.
110 */
111 public static void cleanup() {
112 if (Looper.myLooper() != Looper.getMainLooper()) {
113 Log.wtf(TAG, "cleanup() is called outside the main thread");
114 return;
115 }
116
117 if (sPreviousAdnQueryHandler != null) {
118 sPreviousAdnQueryHandler.cancel();
119 sPreviousAdnQueryHandler = null;
120 }
121 }
122
123 /**
124 * Handles secret codes to launch arbitrary activities in the form of *#*#<code>#*#*.
125 * If a secret code is encountered an Intent is started with the android_secret_code://<code>
126 * URI.
127 *
128 * @param context the context to use
129 * @param input the text to check for a secret code in
130 * @return true if a secret code was encountered
131 */
132 static boolean handleSecretCode(Context context, String input) {
133 // Secret codes are in the form *#*#<code>#*#*
134 int len = input.length();
135 if (len > 8 && input.startsWith("*#*#") && input.endsWith("#*#*")) {
Yorke Leef90dada2013-12-09 11:50:28 -0800136 final Intent intent = new Intent(SECRET_CODE_ACTION,
Chiao Cheng91197042012-08-24 14:19:37 -0700137 Uri.parse("android_secret_code://" + input.substring(4, len - 4)));
138 context.sendBroadcast(intent);
139 return true;
140 }
141
142 return false;
143 }
144
145 /**
146 * Handle ADN requests by filling in the SIM contact number into the requested
147 * EditText.
148 *
149 * This code works alongside the Asynchronous query handler {@link QueryHandler}
150 * and query cancel handler implemented in {@link SimContactQueryCookie}.
151 */
Nancy Chen18c52ff2014-10-30 10:25:00 -0700152 static boolean handleAdnEntry(final Context context, String input, EditText textField) {
Chiao Cheng91197042012-08-24 14:19:37 -0700153 /* ADN entries are of the form "N(N)(N)#" */
154
155 TelephonyManager telephonyManager =
156 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
157 if (telephonyManager == null
Yorke Lee62280c72013-11-22 18:24:59 -0800158 || telephonyManager.getPhoneType() != TelephonyManager.PHONE_TYPE_GSM) {
Chiao Cheng91197042012-08-24 14:19:37 -0700159 return false;
160 }
161
162 // if the phone is keyguard-restricted, then just ignore this
163 // input. We want to make sure that sim card contacts are NOT
164 // exposed unless the phone is unlocked, and this code can be
165 // accessed from the emergency dialer.
166 KeyguardManager keyguardManager =
167 (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
168 if (keyguardManager.inKeyguardRestrictedInputMode()) {
169 return false;
170 }
171
172 int len = input.length();
173 if ((len > 1) && (len < 5) && (input.endsWith("#"))) {
174 try {
175 // get the ordinal number of the sim contact
Nancy Chen18c52ff2014-10-30 10:25:00 -0700176 final int index = Integer.parseInt(input.substring(0, len-1));
Chiao Cheng91197042012-08-24 14:19:37 -0700177
178 // The original code that navigated to a SIM Contacts list view did not
179 // highlight the requested contact correctly, a requirement for PTCRB
180 // certification. This behaviour is consistent with the UI paradigm
181 // for touch-enabled lists, so it does not make sense to try to work
182 // around it. Instead we fill in the the requested phone number into
183 // the dialer text field.
184
185 // create the async query handler
Nancy Chen18c52ff2014-10-30 10:25:00 -0700186 final QueryHandler handler = new QueryHandler (context.getContentResolver());
Chiao Cheng91197042012-08-24 14:19:37 -0700187
188 // create the cookie object
Nancy Chen18c52ff2014-10-30 10:25:00 -0700189 final SimContactQueryCookie sc = new SimContactQueryCookie(index - 1, handler,
Chiao Cheng91197042012-08-24 14:19:37 -0700190 ADN_QUERY_TOKEN);
191
192 // setup the cookie fields
193 sc.contactNum = index - 1;
194 sc.setTextField(textField);
195
196 // create the progress dialog
197 sc.progressDialog = new ProgressDialog(context);
198 sc.progressDialog.setTitle(R.string.simContacts_title);
199 sc.progressDialog.setMessage(context.getText(R.string.simContacts_emptyLoading));
200 sc.progressDialog.setIndeterminate(true);
201 sc.progressDialog.setCancelable(true);
202 sc.progressDialog.setOnCancelListener(sc);
203 sc.progressDialog.getWindow().addFlags(
204 WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
205
Nancy Chen18c52ff2014-10-30 10:25:00 -0700206 final TelecomManager telecomManager =
207 (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
Nancy Chen39c18f22014-12-03 16:37:45 -0800208 List<PhoneAccountHandle> subscriptionAccountHandles =
Nancy Chen18c52ff2014-10-30 10:25:00 -0700209 PhoneAccountUtils.getSubscriptionPhoneAccounts(context);
Chiao Cheng91197042012-08-24 14:19:37 -0700210
Nancy Chen39c18f22014-12-03 16:37:45 -0800211 boolean hasUserSelectedDefault = subscriptionAccountHandles.contains(
212 telecomManager.getUserSelectedOutgoingPhoneAccount());
Chiao Cheng91197042012-08-24 14:19:37 -0700213
Nancy Chen39c18f22014-12-03 16:37:45 -0800214 if (subscriptionAccountHandles.size() == 1 || hasUserSelectedDefault) {
Nancy Chen18c52ff2014-10-30 10:25:00 -0700215 Uri uri = telecomManager.getAdnUriForPhoneAccount(null);
216 handleAdnQuery(handler, sc, uri);
Nancy Chen39c18f22014-12-03 16:37:45 -0800217 } else if (subscriptionAccountHandles.size() > 1){
Nancy Chen18c52ff2014-10-30 10:25:00 -0700218 SelectPhoneAccountListener listener = new SelectPhoneAccountListener() {
219 @Override
220 public void onPhoneAccountSelected(PhoneAccountHandle selectedAccountHandle,
221 boolean setDefault) {
222 Uri uri =
223 telecomManager.getAdnUriForPhoneAccount(selectedAccountHandle);
224 handleAdnQuery(handler, sc, uri);
225 //TODO: show error dialog if result isn't valid
226 }
227 @Override
228 public void onDialogDismissed() {}
229 };
230
231 SelectPhoneAccountDialogFragment.showAccountDialog(
Nancy Chen39c18f22014-12-03 16:37:45 -0800232 ((Activity) context).getFragmentManager(), subscriptionAccountHandles,
Nancy Chen18c52ff2014-10-30 10:25:00 -0700233 listener);
234 } else {
235 return false;
Chiao Cheng91197042012-08-24 14:19:37 -0700236 }
Nancy Chen18c52ff2014-10-30 10:25:00 -0700237
Chiao Cheng91197042012-08-24 14:19:37 -0700238 return true;
239 } catch (NumberFormatException ex) {
240 // Ignore
241 }
242 }
243 return false;
244 }
245
Nancy Chen18c52ff2014-10-30 10:25:00 -0700246 private static void handleAdnQuery(QueryHandler handler, SimContactQueryCookie cookie,
247 Uri uri) {
248 if (handler == null || cookie == null || uri == null) {
249 Log.w(TAG, "queryAdn parameters incorrect");
250 return;
251 }
252
253 // display the progress dialog
254 cookie.progressDialog.show();
255
256 // run the query.
257 handler.startQuery(ADN_QUERY_TOKEN, cookie, uri, new String[]{ADN_PHONE_NUMBER_COLUMN_NAME},
258 null, null, null);
259
260 if (sPreviousAdnQueryHandler != null) {
261 // It is harmless to call cancel() even after the handler's gone.
262 sPreviousAdnQueryHandler.cancel();
263 }
264 sPreviousAdnQueryHandler = handler;
265 }
266
Nancy Chen675af1f2014-10-16 18:33:51 -0700267 static boolean handlePinEntry(Context context, final String input) {
Chiao Cheng91197042012-08-24 14:19:37 -0700268 if ((input.startsWith("**04") || input.startsWith("**05")) && input.endsWith("#")) {
Nancy Chen675af1f2014-10-16 18:33:51 -0700269 final TelecomManager telecomManager =
Tyler Gunn9dc924c2014-09-12 09:33:50 -0700270 (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
Nancy Chen39c18f22014-12-03 16:37:45 -0800271 List<PhoneAccountHandle> subscriptionAccountHandles =
Nancy Chen675af1f2014-10-16 18:33:51 -0700272 PhoneAccountUtils.getSubscriptionPhoneAccounts(context);
Nancy Chen39c18f22014-12-03 16:37:45 -0800273 boolean hasUserSelectedDefault = subscriptionAccountHandles.contains(
274 telecomManager.getUserSelectedOutgoingPhoneAccount());
Nancy Chen675af1f2014-10-16 18:33:51 -0700275
Nancy Chen39c18f22014-12-03 16:37:45 -0800276 if (subscriptionAccountHandles.size() == 1 || hasUserSelectedDefault) {
Nancy Chen675af1f2014-10-16 18:33:51 -0700277 // Don't bring up the dialog for single-SIM or if the default outgoing account is
278 // a subscription account.
279 return telecomManager.handleMmi(input);
Nancy Chen39c18f22014-12-03 16:37:45 -0800280 } else if (subscriptionAccountHandles.size() > 1){
Nancy Chen675af1f2014-10-16 18:33:51 -0700281 SelectPhoneAccountListener listener = new SelectPhoneAccountListener() {
282 @Override
283 public void onPhoneAccountSelected(PhoneAccountHandle selectedAccountHandle,
284 boolean setDefault) {
285 telecomManager.handleMmi(selectedAccountHandle, input);
286 //TODO: show error dialog if result isn't valid
287 }
288 @Override
289 public void onDialogDismissed() {}
290 };
291
292 SelectPhoneAccountDialogFragment.showAccountDialog(
Nancy Chen39c18f22014-12-03 16:37:45 -0800293 ((Activity) context).getFragmentManager(), subscriptionAccountHandles,
Nancy Chen675af1f2014-10-16 18:33:51 -0700294 listener);
295 }
296 return true;
297 }
298 return false;
299 }
300
Nancy Chen8c258ac2014-10-20 19:33:55 -0700301 // TODO: Use TelephonyCapabilities.getDeviceIdLabel() to get the device id label instead of a
302 // hard-coded string.
303 static boolean handleDeviceIdDisplay(Context context, String input) {
Chiao Cheng91197042012-08-24 14:19:37 -0700304 TelephonyManager telephonyManager =
305 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Chiao Cheng91197042012-08-24 14:19:37 -0700306
Nancy Chen8c258ac2014-10-20 19:33:55 -0700307 if (telephonyManager != null && input.equals(MMI_IMEI_DISPLAY)) {
308 int labelResId = (telephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) ?
309 R.string.imei : R.string.meid;
310
311 List<String> deviceIds = new ArrayList<String>();
312 for (int slot = 0; slot < telephonyManager.getPhoneCount(); slot++) {
313 deviceIds.add(telephonyManager.getDeviceId(slot));
314 }
315
316 AlertDialog alert = new AlertDialog.Builder(context)
317 .setTitle(labelResId)
318 .setItems(deviceIds.toArray(new String[deviceIds.size()]), null)
319 .setPositiveButton(R.string.ok, null)
320 .setCancelable(false)
321 .show();
322 return true;
323 }
Chiao Cheng91197042012-08-24 14:19:37 -0700324 return false;
325 }
326
Jake Hamby1d6fb572013-04-09 15:49:56 -0700327 private static boolean handleRegulatoryInfoDisplay(Context context, String input) {
328 if (input.equals(MMI_REGULATORY_INFO_DISPLAY)) {
329 Log.d(TAG, "handleRegulatoryInfoDisplay() sending intent to settings app");
Jeff Sharkeyf4f47662014-04-16 17:21:12 -0700330 Intent showRegInfoIntent = new Intent(Settings.ACTION_SHOW_REGULATORY_INFO);
Jake Hamby1d6fb572013-04-09 15:49:56 -0700331 try {
332 context.startActivity(showRegInfoIntent);
333 } catch (ActivityNotFoundException e) {
334 Log.e(TAG, "startActivity() failed: " + e);
335 }
336 return true;
337 }
338 return false;
339 }
340
Chiao Cheng91197042012-08-24 14:19:37 -0700341 /*******
342 * This code is used to handle SIM Contact queries
343 *******/
344 private static final String ADN_PHONE_NUMBER_COLUMN_NAME = "number";
345 private static final String ADN_NAME_COLUMN_NAME = "name";
346 private static final int ADN_QUERY_TOKEN = -1;
347
348 /**
349 * Cookie object that contains everything we need to communicate to the
350 * handler's onQuery Complete, as well as what we need in order to cancel
351 * the query (if requested).
352 *
353 * Note, access to the textField field is going to be synchronized, because
354 * the user can request a cancel at any time through the UI.
355 */
356 private static class SimContactQueryCookie implements DialogInterface.OnCancelListener{
357 public ProgressDialog progressDialog;
358 public int contactNum;
359
360 // Used to identify the query request.
361 private int mToken;
362 private QueryHandler mHandler;
363
364 // The text field we're going to update
365 private EditText textField;
366
367 public SimContactQueryCookie(int number, QueryHandler handler, int token) {
368 contactNum = number;
369 mHandler = handler;
370 mToken = token;
371 }
372
373 /**
374 * Synchronized getter for the EditText.
375 */
376 public synchronized EditText getTextField() {
377 return textField;
378 }
379
380 /**
381 * Synchronized setter for the EditText.
382 */
383 public synchronized void setTextField(EditText text) {
384 textField = text;
385 }
386
387 /**
388 * Cancel the ADN query by stopping the operation and signaling
389 * the cookie that a cancel request is made.
390 */
391 public synchronized void onCancel(DialogInterface dialog) {
392 // close the progress dialog
393 if (progressDialog != null) {
394 progressDialog.dismiss();
395 }
396
397 // setting the textfield to null ensures that the UI does NOT get
398 // updated.
399 textField = null;
400
401 // Cancel the operation if possible.
402 mHandler.cancelOperation(mToken);
403 }
404 }
405
406 /**
407 * Asynchronous query handler that services requests to look up ADNs
408 *
Jake Hamby1d6fb572013-04-09 15:49:56 -0700409 * Queries originate from {@link #handleAdnEntry}.
Chiao Cheng91197042012-08-24 14:19:37 -0700410 */
Chiao Cheng07af7642012-09-14 12:05:14 -0700411 private static class QueryHandler extends NoNullCursorAsyncQueryHandler {
Chiao Cheng91197042012-08-24 14:19:37 -0700412
413 private boolean mCanceled;
414
415 public QueryHandler(ContentResolver cr) {
416 super(cr);
417 }
418
419 /**
420 * Override basic onQueryComplete to fill in the textfield when
421 * we're handed the ADN cursor.
422 */
423 @Override
Chiao Cheng07af7642012-09-14 12:05:14 -0700424 protected void onNotNullableQueryComplete(int token, Object cookie, Cursor c) {
Jay Shraunerede67ec2014-09-11 15:03:36 -0700425 try {
426 sPreviousAdnQueryHandler = null;
427 if (mCanceled) {
428 return;
429 }
Chiao Cheng91197042012-08-24 14:19:37 -0700430
Jay Shraunerede67ec2014-09-11 15:03:36 -0700431 SimContactQueryCookie sc = (SimContactQueryCookie) cookie;
Chiao Cheng91197042012-08-24 14:19:37 -0700432
Jay Shraunerede67ec2014-09-11 15:03:36 -0700433 // close the progress dialog.
434 sc.progressDialog.dismiss();
Chiao Cheng91197042012-08-24 14:19:37 -0700435
Jay Shraunerede67ec2014-09-11 15:03:36 -0700436 // get the EditText to update or see if the request was cancelled.
437 EditText text = sc.getTextField();
Chiao Cheng91197042012-08-24 14:19:37 -0700438
Nancy Chen18c52ff2014-10-30 10:25:00 -0700439 // if the TextView is valid, and the cursor is valid and positionable on the
440 // Nth number, then we update the text field and display a toast indicating the
441 // caller name.
Jay Shraunerede67ec2014-09-11 15:03:36 -0700442 if ((c != null) && (text != null) && (c.moveToPosition(sc.contactNum))) {
443 String name = c.getString(c.getColumnIndexOrThrow(ADN_NAME_COLUMN_NAME));
Nancy Chen18c52ff2014-10-30 10:25:00 -0700444 String number =
445 c.getString(c.getColumnIndexOrThrow(ADN_PHONE_NUMBER_COLUMN_NAME));
Chiao Cheng91197042012-08-24 14:19:37 -0700446
Jay Shraunerede67ec2014-09-11 15:03:36 -0700447 // fill the text in.
448 text.getText().replace(0, 0, number);
Chiao Cheng91197042012-08-24 14:19:37 -0700449
Jay Shraunerede67ec2014-09-11 15:03:36 -0700450 // display the name as a toast
451 Context context = sc.progressDialog.getContext();
452 name = context.getString(R.string.menu_callNumber, name);
453 Toast.makeText(context, name, Toast.LENGTH_SHORT)
454 .show();
455 }
456 } finally {
457 MoreCloseables.closeQuietly(c);
Chiao Cheng91197042012-08-24 14:19:37 -0700458 }
459 }
460
461 public void cancel() {
462 mCanceled = true;
Nancy Chen18c52ff2014-10-30 10:25:00 -0700463 // Ask AsyncQueryHandler to cancel the whole request. This will fail when the query is
464 // already started.
Chiao Cheng91197042012-08-24 14:19:37 -0700465 cancelOperation(ADN_QUERY_TOKEN);
466 }
467 }
468}