blob: 46c6de6c9c1d8ba09fc8bafe59f51d4239b95a38 [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;
Nancy Chen0f4ec2a2015-03-23 15:03:03 -070036import android.text.TextUtils;
Chiao Cheng91197042012-08-24 14:19:37 -070037import android.util.Log;
38import android.view.WindowManager;
39import android.widget.EditText;
40import android.widget.Toast;
41
Jay Shraunerede67ec2014-09-11 15:03:36 -070042import com.android.common.io.MoreCloseables;
Chiao Cheng07af7642012-09-14 12:05:14 -070043import com.android.contacts.common.database.NoNullCursorAsyncQueryHandler;
Nancy Chen675af1f2014-10-16 18:33:51 -070044import com.android.contacts.common.widget.SelectPhoneAccountDialogFragment;
45import com.android.contacts.common.widget.SelectPhoneAccountDialogFragment.SelectPhoneAccountListener;
46import com.android.dialer.calllog.PhoneAccountUtils;
Chiao Cheng91197042012-08-24 14:19:37 -070047
Nancy Chen675af1f2014-10-16 18:33:51 -070048import java.util.Arrays;
Nancy Chen8c258ac2014-10-20 19:33:55 -070049import java.util.ArrayList;
50import java.util.List;
51
Chiao Cheng91197042012-08-24 14:19:37 -070052/**
53 * Helper class to listen for some magic character sequences
54 * that are handled specially by the dialer.
55 *
56 * Note the Phone app also handles these sequences too (in a couple of
Jake Hamby1d6fb572013-04-09 15:49:56 -070057 * relatively obscure places in the UI), so there's a separate version of
Chiao Cheng91197042012-08-24 14:19:37 -070058 * this class under apps/Phone.
59 *
60 * TODO: there's lots of duplicated code between this class and the
61 * corresponding class under apps/Phone. Let's figure out a way to
62 * unify these two classes (in the framework? in a common shared library?)
63 */
64public class SpecialCharSequenceMgr {
65 private static final String TAG = "SpecialCharSequenceMgr";
Jake Hamby1d6fb572013-04-09 15:49:56 -070066
Yorke Leef90dada2013-12-09 11:50:28 -080067 private static final String SECRET_CODE_ACTION = "android.provider.Telephony.SECRET_CODE";
Chiao Cheng91197042012-08-24 14:19:37 -070068 private static final String MMI_IMEI_DISPLAY = "*#06#";
Jake Hamby1d6fb572013-04-09 15:49:56 -070069 private static final String MMI_REGULATORY_INFO_DISPLAY = "*#07#";
Chiao Cheng91197042012-08-24 14:19:37 -070070
71 /**
72 * Remembers the previous {@link QueryHandler} and cancel the operation when needed, to
73 * prevent possible crash.
74 *
75 * QueryHandler may call {@link ProgressDialog#dismiss()} when the screen is already gone,
76 * which will cause the app crash. This variable enables the class to prevent the crash
77 * on {@link #cleanup()}.
78 *
79 * TODO: Remove this and replace it (and {@link #cleanup()}) with better implementation.
Jake Hamby1d6fb572013-04-09 15:49:56 -070080 * One complication is that we have SpecialCharSequenceMgr in Phone package too, which has
Chiao Cheng91197042012-08-24 14:19:37 -070081 * *slightly* different implementation. Note that Phone package doesn't have this problem,
82 * so the class on Phone side doesn't have this functionality.
83 * Fundamental fix would be to have one shared implementation and resolve this corner case more
84 * gracefully.
85 */
86 private static QueryHandler sPreviousAdnQueryHandler;
87
88 /** This class is never instantiated. */
89 private SpecialCharSequenceMgr() {
90 }
91
92 public static boolean handleChars(Context context, String input, EditText textField) {
Chiao Cheng91197042012-08-24 14:19:37 -070093 //get rid of the separators so that the string gets parsed correctly
94 String dialString = PhoneNumberUtils.stripSeparators(input);
95
Nancy Chen8c258ac2014-10-20 19:33:55 -070096 if (handleDeviceIdDisplay(context, dialString)
Jake Hamby1d6fb572013-04-09 15:49:56 -070097 || handleRegulatoryInfoDisplay(context, dialString)
Chiao Cheng91197042012-08-24 14:19:37 -070098 || handlePinEntry(context, dialString)
99 || handleAdnEntry(context, dialString, textField)
100 || handleSecretCode(context, dialString)) {
101 return true;
102 }
103
104 return false;
105 }
106
107 /**
108 * Cleanup everything around this class. Must be run inside the main thread.
109 *
110 * This should be called when the screen becomes background.
111 */
112 public static void cleanup() {
113 if (Looper.myLooper() != Looper.getMainLooper()) {
114 Log.wtf(TAG, "cleanup() is called outside the main thread");
115 return;
116 }
117
118 if (sPreviousAdnQueryHandler != null) {
119 sPreviousAdnQueryHandler.cancel();
120 sPreviousAdnQueryHandler = null;
121 }
122 }
123
124 /**
125 * Handles secret codes to launch arbitrary activities in the form of *#*#<code>#*#*.
126 * If a secret code is encountered an Intent is started with the android_secret_code://<code>
127 * URI.
128 *
129 * @param context the context to use
130 * @param input the text to check for a secret code in
131 * @return true if a secret code was encountered
132 */
133 static boolean handleSecretCode(Context context, String input) {
134 // Secret codes are in the form *#*#<code>#*#*
135 int len = input.length();
136 if (len > 8 && input.startsWith("*#*#") && input.endsWith("#*#*")) {
Yorke Leef90dada2013-12-09 11:50:28 -0800137 final Intent intent = new Intent(SECRET_CODE_ACTION,
Chiao Cheng91197042012-08-24 14:19:37 -0700138 Uri.parse("android_secret_code://" + input.substring(4, len - 4)));
139 context.sendBroadcast(intent);
140 return true;
141 }
142
143 return false;
144 }
145
146 /**
147 * Handle ADN requests by filling in the SIM contact number into the requested
148 * EditText.
149 *
150 * This code works alongside the Asynchronous query handler {@link QueryHandler}
151 * and query cancel handler implemented in {@link SimContactQueryCookie}.
152 */
Nancy Chen18c52ff2014-10-30 10:25:00 -0700153 static boolean handleAdnEntry(final Context context, String input, EditText textField) {
Chiao Cheng91197042012-08-24 14:19:37 -0700154 /* ADN entries are of the form "N(N)(N)#" */
155
156 TelephonyManager telephonyManager =
157 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
158 if (telephonyManager == null
Yorke Lee62280c72013-11-22 18:24:59 -0800159 || telephonyManager.getPhoneType() != TelephonyManager.PHONE_TYPE_GSM) {
Chiao Cheng91197042012-08-24 14:19:37 -0700160 return false;
161 }
162
163 // if the phone is keyguard-restricted, then just ignore this
164 // input. We want to make sure that sim card contacts are NOT
165 // exposed unless the phone is unlocked, and this code can be
166 // accessed from the emergency dialer.
167 KeyguardManager keyguardManager =
168 (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
169 if (keyguardManager.inKeyguardRestrictedInputMode()) {
170 return false;
171 }
172
173 int len = input.length();
174 if ((len > 1) && (len < 5) && (input.endsWith("#"))) {
175 try {
176 // get the ordinal number of the sim contact
Nancy Chen18c52ff2014-10-30 10:25:00 -0700177 final int index = Integer.parseInt(input.substring(0, len-1));
Chiao Cheng91197042012-08-24 14:19:37 -0700178
179 // The original code that navigated to a SIM Contacts list view did not
180 // highlight the requested contact correctly, a requirement for PTCRB
181 // certification. This behaviour is consistent with the UI paradigm
182 // for touch-enabled lists, so it does not make sense to try to work
183 // around it. Instead we fill in the the requested phone number into
184 // the dialer text field.
185
186 // create the async query handler
Nancy Chen18c52ff2014-10-30 10:25:00 -0700187 final QueryHandler handler = new QueryHandler (context.getContentResolver());
Chiao Cheng91197042012-08-24 14:19:37 -0700188
189 // create the cookie object
Nancy Chen18c52ff2014-10-30 10:25:00 -0700190 final SimContactQueryCookie sc = new SimContactQueryCookie(index - 1, handler,
Chiao Cheng91197042012-08-24 14:19:37 -0700191 ADN_QUERY_TOKEN);
192
193 // setup the cookie fields
194 sc.contactNum = index - 1;
195 sc.setTextField(textField);
196
197 // create the progress dialog
198 sc.progressDialog = new ProgressDialog(context);
199 sc.progressDialog.setTitle(R.string.simContacts_title);
200 sc.progressDialog.setMessage(context.getText(R.string.simContacts_emptyLoading));
201 sc.progressDialog.setIndeterminate(true);
202 sc.progressDialog.setCancelable(true);
203 sc.progressDialog.setOnCancelListener(sc);
204 sc.progressDialog.getWindow().addFlags(
205 WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
206
Nancy Chen18c52ff2014-10-30 10:25:00 -0700207 final TelecomManager telecomManager =
208 (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
Nancy Chen39c18f22014-12-03 16:37:45 -0800209 List<PhoneAccountHandle> subscriptionAccountHandles =
Nancy Chen18c52ff2014-10-30 10:25:00 -0700210 PhoneAccountUtils.getSubscriptionPhoneAccounts(context);
Chiao Cheng91197042012-08-24 14:19:37 -0700211
Nancy Chen39c18f22014-12-03 16:37:45 -0800212 boolean hasUserSelectedDefault = subscriptionAccountHandles.contains(
213 telecomManager.getUserSelectedOutgoingPhoneAccount());
Chiao Cheng91197042012-08-24 14:19:37 -0700214
Nancy Chen39c18f22014-12-03 16:37:45 -0800215 if (subscriptionAccountHandles.size() == 1 || hasUserSelectedDefault) {
Nancy Chen18c52ff2014-10-30 10:25:00 -0700216 Uri uri = telecomManager.getAdnUriForPhoneAccount(null);
217 handleAdnQuery(handler, sc, uri);
Nancy Chen39c18f22014-12-03 16:37:45 -0800218 } else if (subscriptionAccountHandles.size() > 1){
Nancy Chen18c52ff2014-10-30 10:25:00 -0700219 SelectPhoneAccountListener listener = new SelectPhoneAccountListener() {
220 @Override
221 public void onPhoneAccountSelected(PhoneAccountHandle selectedAccountHandle,
222 boolean setDefault) {
223 Uri uri =
224 telecomManager.getAdnUriForPhoneAccount(selectedAccountHandle);
225 handleAdnQuery(handler, sc, uri);
226 //TODO: show error dialog if result isn't valid
227 }
228 @Override
229 public void onDialogDismissed() {}
230 };
231
232 SelectPhoneAccountDialogFragment.showAccountDialog(
Nancy Chen39c18f22014-12-03 16:37:45 -0800233 ((Activity) context).getFragmentManager(), subscriptionAccountHandles,
Nancy Chen18c52ff2014-10-30 10:25:00 -0700234 listener);
235 } else {
236 return false;
Chiao Cheng91197042012-08-24 14:19:37 -0700237 }
Nancy Chen18c52ff2014-10-30 10:25:00 -0700238
Chiao Cheng91197042012-08-24 14:19:37 -0700239 return true;
240 } catch (NumberFormatException ex) {
241 // Ignore
242 }
243 }
244 return false;
245 }
246
Nancy Chen18c52ff2014-10-30 10:25:00 -0700247 private static void handleAdnQuery(QueryHandler handler, SimContactQueryCookie cookie,
248 Uri uri) {
249 if (handler == null || cookie == null || uri == null) {
250 Log.w(TAG, "queryAdn parameters incorrect");
251 return;
252 }
253
254 // display the progress dialog
255 cookie.progressDialog.show();
256
257 // run the query.
258 handler.startQuery(ADN_QUERY_TOKEN, cookie, uri, new String[]{ADN_PHONE_NUMBER_COLUMN_NAME},
259 null, null, null);
260
261 if (sPreviousAdnQueryHandler != null) {
262 // It is harmless to call cancel() even after the handler's gone.
263 sPreviousAdnQueryHandler.cancel();
264 }
265 sPreviousAdnQueryHandler = handler;
266 }
267
Nancy Chen675af1f2014-10-16 18:33:51 -0700268 static boolean handlePinEntry(Context context, final String input) {
Chiao Cheng91197042012-08-24 14:19:37 -0700269 if ((input.startsWith("**04") || input.startsWith("**05")) && input.endsWith("#")) {
Nancy Chen675af1f2014-10-16 18:33:51 -0700270 final TelecomManager telecomManager =
Tyler Gunn9dc924c2014-09-12 09:33:50 -0700271 (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
Nancy Chen39c18f22014-12-03 16:37:45 -0800272 List<PhoneAccountHandle> subscriptionAccountHandles =
Nancy Chen675af1f2014-10-16 18:33:51 -0700273 PhoneAccountUtils.getSubscriptionPhoneAccounts(context);
Nancy Chen39c18f22014-12-03 16:37:45 -0800274 boolean hasUserSelectedDefault = subscriptionAccountHandles.contains(
275 telecomManager.getUserSelectedOutgoingPhoneAccount());
Nancy Chen675af1f2014-10-16 18:33:51 -0700276
Nancy Chen39c18f22014-12-03 16:37:45 -0800277 if (subscriptionAccountHandles.size() == 1 || hasUserSelectedDefault) {
Nancy Chen675af1f2014-10-16 18:33:51 -0700278 // Don't bring up the dialog for single-SIM or if the default outgoing account is
279 // a subscription account.
280 return telecomManager.handleMmi(input);
Nancy Chen39c18f22014-12-03 16:37:45 -0800281 } else if (subscriptionAccountHandles.size() > 1){
Nancy Chen675af1f2014-10-16 18:33:51 -0700282 SelectPhoneAccountListener listener = new SelectPhoneAccountListener() {
283 @Override
284 public void onPhoneAccountSelected(PhoneAccountHandle selectedAccountHandle,
285 boolean setDefault) {
286 telecomManager.handleMmi(selectedAccountHandle, input);
287 //TODO: show error dialog if result isn't valid
288 }
289 @Override
290 public void onDialogDismissed() {}
291 };
292
293 SelectPhoneAccountDialogFragment.showAccountDialog(
Nancy Chen39c18f22014-12-03 16:37:45 -0800294 ((Activity) context).getFragmentManager(), subscriptionAccountHandles,
Nancy Chen675af1f2014-10-16 18:33:51 -0700295 listener);
296 }
297 return true;
298 }
299 return false;
300 }
301
Nancy Chen8c258ac2014-10-20 19:33:55 -0700302 // TODO: Use TelephonyCapabilities.getDeviceIdLabel() to get the device id label instead of a
303 // hard-coded string.
304 static boolean handleDeviceIdDisplay(Context context, String input) {
Chiao Cheng91197042012-08-24 14:19:37 -0700305 TelephonyManager telephonyManager =
306 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Chiao Cheng91197042012-08-24 14:19:37 -0700307
Nancy Chen8c258ac2014-10-20 19:33:55 -0700308 if (telephonyManager != null && input.equals(MMI_IMEI_DISPLAY)) {
309 int labelResId = (telephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) ?
310 R.string.imei : R.string.meid;
311
312 List<String> deviceIds = new ArrayList<String>();
313 for (int slot = 0; slot < telephonyManager.getPhoneCount(); slot++) {
Nancy Chen0f4ec2a2015-03-23 15:03:03 -0700314 String deviceId = telephonyManager.getDeviceId(slot);
315 if (!TextUtils.isEmpty(deviceId)) {
316 deviceIds.add(deviceId);
317 }
Nancy Chen8c258ac2014-10-20 19:33:55 -0700318 }
319
320 AlertDialog alert = new AlertDialog.Builder(context)
321 .setTitle(labelResId)
322 .setItems(deviceIds.toArray(new String[deviceIds.size()]), null)
Nancy Chen0f4ec2a2015-03-23 15:03:03 -0700323 .setPositiveButton(android.R.string.ok, null)
Nancy Chen8c258ac2014-10-20 19:33:55 -0700324 .setCancelable(false)
325 .show();
326 return true;
327 }
Chiao Cheng91197042012-08-24 14:19:37 -0700328 return false;
329 }
330
Jake Hamby1d6fb572013-04-09 15:49:56 -0700331 private static boolean handleRegulatoryInfoDisplay(Context context, String input) {
332 if (input.equals(MMI_REGULATORY_INFO_DISPLAY)) {
333 Log.d(TAG, "handleRegulatoryInfoDisplay() sending intent to settings app");
Jeff Sharkeyf4f47662014-04-16 17:21:12 -0700334 Intent showRegInfoIntent = new Intent(Settings.ACTION_SHOW_REGULATORY_INFO);
Jake Hamby1d6fb572013-04-09 15:49:56 -0700335 try {
336 context.startActivity(showRegInfoIntent);
337 } catch (ActivityNotFoundException e) {
338 Log.e(TAG, "startActivity() failed: " + e);
339 }
340 return true;
341 }
342 return false;
343 }
344
Chiao Cheng91197042012-08-24 14:19:37 -0700345 /*******
346 * This code is used to handle SIM Contact queries
347 *******/
348 private static final String ADN_PHONE_NUMBER_COLUMN_NAME = "number";
349 private static final String ADN_NAME_COLUMN_NAME = "name";
350 private static final int ADN_QUERY_TOKEN = -1;
351
352 /**
353 * Cookie object that contains everything we need to communicate to the
354 * handler's onQuery Complete, as well as what we need in order to cancel
355 * the query (if requested).
356 *
357 * Note, access to the textField field is going to be synchronized, because
358 * the user can request a cancel at any time through the UI.
359 */
360 private static class SimContactQueryCookie implements DialogInterface.OnCancelListener{
361 public ProgressDialog progressDialog;
362 public int contactNum;
363
364 // Used to identify the query request.
365 private int mToken;
366 private QueryHandler mHandler;
367
368 // The text field we're going to update
369 private EditText textField;
370
371 public SimContactQueryCookie(int number, QueryHandler handler, int token) {
372 contactNum = number;
373 mHandler = handler;
374 mToken = token;
375 }
376
377 /**
378 * Synchronized getter for the EditText.
379 */
380 public synchronized EditText getTextField() {
381 return textField;
382 }
383
384 /**
385 * Synchronized setter for the EditText.
386 */
387 public synchronized void setTextField(EditText text) {
388 textField = text;
389 }
390
391 /**
392 * Cancel the ADN query by stopping the operation and signaling
393 * the cookie that a cancel request is made.
394 */
395 public synchronized void onCancel(DialogInterface dialog) {
396 // close the progress dialog
397 if (progressDialog != null) {
398 progressDialog.dismiss();
399 }
400
401 // setting the textfield to null ensures that the UI does NOT get
402 // updated.
403 textField = null;
404
405 // Cancel the operation if possible.
406 mHandler.cancelOperation(mToken);
407 }
408 }
409
410 /**
411 * Asynchronous query handler that services requests to look up ADNs
412 *
Jake Hamby1d6fb572013-04-09 15:49:56 -0700413 * Queries originate from {@link #handleAdnEntry}.
Chiao Cheng91197042012-08-24 14:19:37 -0700414 */
Chiao Cheng07af7642012-09-14 12:05:14 -0700415 private static class QueryHandler extends NoNullCursorAsyncQueryHandler {
Chiao Cheng91197042012-08-24 14:19:37 -0700416
417 private boolean mCanceled;
418
419 public QueryHandler(ContentResolver cr) {
420 super(cr);
421 }
422
423 /**
424 * Override basic onQueryComplete to fill in the textfield when
425 * we're handed the ADN cursor.
426 */
427 @Override
Chiao Cheng07af7642012-09-14 12:05:14 -0700428 protected void onNotNullableQueryComplete(int token, Object cookie, Cursor c) {
Jay Shraunerede67ec2014-09-11 15:03:36 -0700429 try {
430 sPreviousAdnQueryHandler = null;
431 if (mCanceled) {
432 return;
433 }
Chiao Cheng91197042012-08-24 14:19:37 -0700434
Jay Shraunerede67ec2014-09-11 15:03:36 -0700435 SimContactQueryCookie sc = (SimContactQueryCookie) cookie;
Chiao Cheng91197042012-08-24 14:19:37 -0700436
Jay Shraunerede67ec2014-09-11 15:03:36 -0700437 // close the progress dialog.
438 sc.progressDialog.dismiss();
Chiao Cheng91197042012-08-24 14:19:37 -0700439
Jay Shraunerede67ec2014-09-11 15:03:36 -0700440 // get the EditText to update or see if the request was cancelled.
441 EditText text = sc.getTextField();
Chiao Cheng91197042012-08-24 14:19:37 -0700442
Nancy Chen18c52ff2014-10-30 10:25:00 -0700443 // if the TextView is valid, and the cursor is valid and positionable on the
444 // Nth number, then we update the text field and display a toast indicating the
445 // caller name.
Jay Shraunerede67ec2014-09-11 15:03:36 -0700446 if ((c != null) && (text != null) && (c.moveToPosition(sc.contactNum))) {
447 String name = c.getString(c.getColumnIndexOrThrow(ADN_NAME_COLUMN_NAME));
Nancy Chen18c52ff2014-10-30 10:25:00 -0700448 String number =
449 c.getString(c.getColumnIndexOrThrow(ADN_PHONE_NUMBER_COLUMN_NAME));
Chiao Cheng91197042012-08-24 14:19:37 -0700450
Jay Shraunerede67ec2014-09-11 15:03:36 -0700451 // fill the text in.
452 text.getText().replace(0, 0, number);
Chiao Cheng91197042012-08-24 14:19:37 -0700453
Jay Shraunerede67ec2014-09-11 15:03:36 -0700454 // display the name as a toast
455 Context context = sc.progressDialog.getContext();
456 name = context.getString(R.string.menu_callNumber, name);
457 Toast.makeText(context, name, Toast.LENGTH_SHORT)
458 .show();
459 }
460 } finally {
461 MoreCloseables.closeQuietly(c);
Chiao Cheng91197042012-08-24 14:19:37 -0700462 }
463 }
464
465 public void cancel() {
466 mCanceled = true;
Nancy Chen18c52ff2014-10-30 10:25:00 -0700467 // Ask AsyncQueryHandler to cancel the whole request. This will fail when the query is
468 // already started.
Chiao Cheng91197042012-08-24 14:19:37 -0700469 cancelOperation(ADN_QUERY_TOKEN);
470 }
471 }
472}