blob: 2bf1afcf95104b60060ecc09760140b99918a371 [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;
46import com.android.incallui.InCallPresenter;
Chiao Cheng91197042012-08-24 14:19:37 -070047
Nancy Chen675af1f2014-10-16 18:33:51 -070048import java.util.Arrays;
49import java.util.List;
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
Yorke Leef90dada2013-12-09 11:50:28 -080068 private static final String SECRET_CODE_ACTION = "android.provider.Telephony.SECRET_CODE";
Chiao Cheng91197042012-08-24 14:19:37 -070069 private static final String MMI_IMEI_DISPLAY = "*#06#";
Jake Hamby1d6fb572013-04-09 15:49:56 -070070 private static final String MMI_REGULATORY_INFO_DISPLAY = "*#07#";
Chiao Cheng91197042012-08-24 14:19:37 -070071
72 /**
73 * Remembers the previous {@link QueryHandler} and cancel the operation when needed, to
74 * prevent possible crash.
75 *
76 * QueryHandler may call {@link ProgressDialog#dismiss()} when the screen is already gone,
77 * which will cause the app crash. This variable enables the class to prevent the crash
78 * on {@link #cleanup()}.
79 *
80 * TODO: Remove this and replace it (and {@link #cleanup()}) with better implementation.
Jake Hamby1d6fb572013-04-09 15:49:56 -070081 * One complication is that we have SpecialCharSequenceMgr in Phone package too, which has
Chiao Cheng91197042012-08-24 14:19:37 -070082 * *slightly* different implementation. Note that Phone package doesn't have this problem,
83 * so the class on Phone side doesn't have this functionality.
84 * Fundamental fix would be to have one shared implementation and resolve this corner case more
85 * gracefully.
86 */
87 private static QueryHandler sPreviousAdnQueryHandler;
88
89 /** This class is never instantiated. */
90 private SpecialCharSequenceMgr() {
91 }
92
93 public static boolean handleChars(Context context, String input, EditText textField) {
Chiao Cheng91197042012-08-24 14:19:37 -070094 //get rid of the separators so that the string gets parsed correctly
95 String dialString = PhoneNumberUtils.stripSeparators(input);
96
Nancy Chen8c258ac2014-10-20 19:33:55 -070097 if (handleDeviceIdDisplay(context, dialString)
Jake Hamby1d6fb572013-04-09 15:49:56 -070098 || handleRegulatoryInfoDisplay(context, dialString)
Chiao Cheng91197042012-08-24 14:19:37 -070099 || handlePinEntry(context, dialString)
100 || handleAdnEntry(context, dialString, textField)
101 || handleSecretCode(context, dialString)) {
102 return true;
103 }
104
105 return false;
106 }
107
108 /**
109 * Cleanup everything around this class. Must be run inside the main thread.
110 *
111 * This should be called when the screen becomes background.
112 */
113 public static void cleanup() {
114 if (Looper.myLooper() != Looper.getMainLooper()) {
115 Log.wtf(TAG, "cleanup() is called outside the main thread");
116 return;
117 }
118
119 if (sPreviousAdnQueryHandler != null) {
120 sPreviousAdnQueryHandler.cancel();
121 sPreviousAdnQueryHandler = null;
122 }
123 }
124
125 /**
126 * Handles secret codes to launch arbitrary activities in the form of *#*#<code>#*#*.
127 * If a secret code is encountered an Intent is started with the android_secret_code://<code>
128 * URI.
129 *
130 * @param context the context to use
131 * @param input the text to check for a secret code in
132 * @return true if a secret code was encountered
133 */
134 static boolean handleSecretCode(Context context, String input) {
135 // Secret codes are in the form *#*#<code>#*#*
136 int len = input.length();
137 if (len > 8 && input.startsWith("*#*#") && input.endsWith("#*#*")) {
Yorke Leef90dada2013-12-09 11:50:28 -0800138 final Intent intent = new Intent(SECRET_CODE_ACTION,
Chiao Cheng91197042012-08-24 14:19:37 -0700139 Uri.parse("android_secret_code://" + input.substring(4, len - 4)));
140 context.sendBroadcast(intent);
141 return true;
142 }
143
144 return false;
145 }
146
147 /**
148 * Handle ADN requests by filling in the SIM contact number into the requested
149 * EditText.
150 *
151 * This code works alongside the Asynchronous query handler {@link QueryHandler}
152 * and query cancel handler implemented in {@link SimContactQueryCookie}.
153 */
154 static boolean handleAdnEntry(Context context, String input, EditText textField) {
155 /* ADN entries are of the form "N(N)(N)#" */
156
157 TelephonyManager telephonyManager =
158 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
159 if (telephonyManager == null
Yorke Lee62280c72013-11-22 18:24:59 -0800160 || telephonyManager.getPhoneType() != TelephonyManager.PHONE_TYPE_GSM) {
Chiao Cheng91197042012-08-24 14:19:37 -0700161 return false;
162 }
163
164 // if the phone is keyguard-restricted, then just ignore this
165 // input. We want to make sure that sim card contacts are NOT
166 // exposed unless the phone is unlocked, and this code can be
167 // accessed from the emergency dialer.
168 KeyguardManager keyguardManager =
169 (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
170 if (keyguardManager.inKeyguardRestrictedInputMode()) {
171 return false;
172 }
173
174 int len = input.length();
175 if ((len > 1) && (len < 5) && (input.endsWith("#"))) {
176 try {
177 // get the ordinal number of the sim contact
178 int index = Integer.parseInt(input.substring(0, len-1));
179
180 // The original code that navigated to a SIM Contacts list view did not
181 // highlight the requested contact correctly, a requirement for PTCRB
182 // certification. This behaviour is consistent with the UI paradigm
183 // for touch-enabled lists, so it does not make sense to try to work
184 // around it. Instead we fill in the the requested phone number into
185 // the dialer text field.
186
187 // create the async query handler
188 QueryHandler handler = new QueryHandler (context.getContentResolver());
189
190 // create the cookie object
191 SimContactQueryCookie sc = new SimContactQueryCookie(index - 1, handler,
192 ADN_QUERY_TOKEN);
193
194 // setup the cookie fields
195 sc.contactNum = index - 1;
196 sc.setTextField(textField);
197
198 // create the progress dialog
199 sc.progressDialog = new ProgressDialog(context);
200 sc.progressDialog.setTitle(R.string.simContacts_title);
201 sc.progressDialog.setMessage(context.getText(R.string.simContacts_emptyLoading));
202 sc.progressDialog.setIndeterminate(true);
203 sc.progressDialog.setCancelable(true);
204 sc.progressDialog.setOnCancelListener(sc);
205 sc.progressDialog.getWindow().addFlags(
206 WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
207
208 // display the progress dialog
209 sc.progressDialog.show();
210
211 // run the query.
212 handler.startQuery(ADN_QUERY_TOKEN, sc, Uri.parse("content://icc/adn"),
213 new String[]{ADN_PHONE_NUMBER_COLUMN_NAME}, null, null, null);
214
215 if (sPreviousAdnQueryHandler != null) {
216 // It is harmless to call cancel() even after the handler's gone.
217 sPreviousAdnQueryHandler.cancel();
218 }
219 sPreviousAdnQueryHandler = handler;
220 return true;
221 } catch (NumberFormatException ex) {
222 // Ignore
223 }
224 }
225 return false;
226 }
227
Nancy Chen675af1f2014-10-16 18:33:51 -0700228 static boolean handlePinEntry(Context context, final String input) {
Chiao Cheng91197042012-08-24 14:19:37 -0700229 if ((input.startsWith("**04") || input.startsWith("**05")) && input.endsWith("#")) {
Nancy Chen675af1f2014-10-16 18:33:51 -0700230 final TelecomManager telecomManager =
Tyler Gunn9dc924c2014-09-12 09:33:50 -0700231 (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
Nancy Chen675af1f2014-10-16 18:33:51 -0700232 List<PhoneAccountHandle> phoneAccountHandles =
233 PhoneAccountUtils.getSubscriptionPhoneAccounts(context);
234 boolean hasUserSelectedDefault = hasDefaultSubscriptionAccount(
235 telecomManager.getUserSelectedOutgoingPhoneAccount(), phoneAccountHandles);
236
237 if (phoneAccountHandles.size() == 1 || hasUserSelectedDefault) {
238 // Don't bring up the dialog for single-SIM or if the default outgoing account is
239 // a subscription account.
240 return telecomManager.handleMmi(input);
241 } else if (phoneAccountHandles.size() > 1){
242 SelectPhoneAccountListener listener = new SelectPhoneAccountListener() {
243 @Override
244 public void onPhoneAccountSelected(PhoneAccountHandle selectedAccountHandle,
245 boolean setDefault) {
246 telecomManager.handleMmi(selectedAccountHandle, input);
247 //TODO: show error dialog if result isn't valid
248 }
249 @Override
250 public void onDialogDismissed() {}
251 };
252
253 SelectPhoneAccountDialogFragment.showAccountDialog(
254 ((Activity) context).getFragmentManager(), false, phoneAccountHandles,
255 listener);
256 }
257 return true;
258 }
259 return false;
260 }
261
262 /**
263 * Check if the default outgoing phone account set is a subscription phone account.
264 */
265 static private boolean hasDefaultSubscriptionAccount(PhoneAccountHandle defaultOutgoingAccount,
266 List<PhoneAccountHandle> phoneAccountHandles) {
267 for (PhoneAccountHandle accountHandle : phoneAccountHandles) {
268 if (accountHandle.equals(defaultOutgoingAccount)) {
269 return true;
270 }
Chiao Cheng91197042012-08-24 14:19:37 -0700271 }
272 return false;
273 }
274
Nancy Chen8c258ac2014-10-20 19:33:55 -0700275
276 // TODO: Use TelephonyCapabilities.getDeviceIdLabel() to get the device id label instead of a
277 // hard-coded string.
278 static boolean handleDeviceIdDisplay(Context context, String input) {
Chiao Cheng91197042012-08-24 14:19:37 -0700279 TelephonyManager telephonyManager =
280 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Chiao Cheng91197042012-08-24 14:19:37 -0700281
Nancy Chen8c258ac2014-10-20 19:33:55 -0700282 if (telephonyManager != null && input.equals(MMI_IMEI_DISPLAY)) {
283 int labelResId = (telephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) ?
284 R.string.imei : R.string.meid;
285
286 List<String> deviceIds = new ArrayList<String>();
287 for (int slot = 0; slot < telephonyManager.getPhoneCount(); slot++) {
288 deviceIds.add(telephonyManager.getDeviceId(slot));
289 }
290
291 AlertDialog alert = new AlertDialog.Builder(context)
292 .setTitle(labelResId)
293 .setItems(deviceIds.toArray(new String[deviceIds.size()]), null)
294 .setPositiveButton(R.string.ok, null)
295 .setCancelable(false)
296 .show();
297 return true;
298 }
Chiao Cheng91197042012-08-24 14:19:37 -0700299 return false;
300 }
301
Jake Hamby1d6fb572013-04-09 15:49:56 -0700302 private static boolean handleRegulatoryInfoDisplay(Context context, String input) {
303 if (input.equals(MMI_REGULATORY_INFO_DISPLAY)) {
304 Log.d(TAG, "handleRegulatoryInfoDisplay() sending intent to settings app");
Jeff Sharkeyf4f47662014-04-16 17:21:12 -0700305 Intent showRegInfoIntent = new Intent(Settings.ACTION_SHOW_REGULATORY_INFO);
Jake Hamby1d6fb572013-04-09 15:49:56 -0700306 try {
307 context.startActivity(showRegInfoIntent);
308 } catch (ActivityNotFoundException e) {
309 Log.e(TAG, "startActivity() failed: " + e);
310 }
311 return true;
312 }
313 return false;
314 }
315
Chiao Cheng91197042012-08-24 14:19:37 -0700316 /*******
317 * This code is used to handle SIM Contact queries
318 *******/
319 private static final String ADN_PHONE_NUMBER_COLUMN_NAME = "number";
320 private static final String ADN_NAME_COLUMN_NAME = "name";
321 private static final int ADN_QUERY_TOKEN = -1;
322
323 /**
324 * Cookie object that contains everything we need to communicate to the
325 * handler's onQuery Complete, as well as what we need in order to cancel
326 * the query (if requested).
327 *
328 * Note, access to the textField field is going to be synchronized, because
329 * the user can request a cancel at any time through the UI.
330 */
331 private static class SimContactQueryCookie implements DialogInterface.OnCancelListener{
332 public ProgressDialog progressDialog;
333 public int contactNum;
334
335 // Used to identify the query request.
336 private int mToken;
337 private QueryHandler mHandler;
338
339 // The text field we're going to update
340 private EditText textField;
341
342 public SimContactQueryCookie(int number, QueryHandler handler, int token) {
343 contactNum = number;
344 mHandler = handler;
345 mToken = token;
346 }
347
348 /**
349 * Synchronized getter for the EditText.
350 */
351 public synchronized EditText getTextField() {
352 return textField;
353 }
354
355 /**
356 * Synchronized setter for the EditText.
357 */
358 public synchronized void setTextField(EditText text) {
359 textField = text;
360 }
361
362 /**
363 * Cancel the ADN query by stopping the operation and signaling
364 * the cookie that a cancel request is made.
365 */
366 public synchronized void onCancel(DialogInterface dialog) {
367 // close the progress dialog
368 if (progressDialog != null) {
369 progressDialog.dismiss();
370 }
371
372 // setting the textfield to null ensures that the UI does NOT get
373 // updated.
374 textField = null;
375
376 // Cancel the operation if possible.
377 mHandler.cancelOperation(mToken);
378 }
379 }
380
381 /**
382 * Asynchronous query handler that services requests to look up ADNs
383 *
Jake Hamby1d6fb572013-04-09 15:49:56 -0700384 * Queries originate from {@link #handleAdnEntry}.
Chiao Cheng91197042012-08-24 14:19:37 -0700385 */
Chiao Cheng07af7642012-09-14 12:05:14 -0700386 private static class QueryHandler extends NoNullCursorAsyncQueryHandler {
Chiao Cheng91197042012-08-24 14:19:37 -0700387
388 private boolean mCanceled;
389
390 public QueryHandler(ContentResolver cr) {
391 super(cr);
392 }
393
394 /**
395 * Override basic onQueryComplete to fill in the textfield when
396 * we're handed the ADN cursor.
397 */
398 @Override
Chiao Cheng07af7642012-09-14 12:05:14 -0700399 protected void onNotNullableQueryComplete(int token, Object cookie, Cursor c) {
Jay Shraunerede67ec2014-09-11 15:03:36 -0700400 try {
401 sPreviousAdnQueryHandler = null;
402 if (mCanceled) {
403 return;
404 }
Chiao Cheng91197042012-08-24 14:19:37 -0700405
Jay Shraunerede67ec2014-09-11 15:03:36 -0700406 SimContactQueryCookie sc = (SimContactQueryCookie) cookie;
Chiao Cheng91197042012-08-24 14:19:37 -0700407
Jay Shraunerede67ec2014-09-11 15:03:36 -0700408 // close the progress dialog.
409 sc.progressDialog.dismiss();
Chiao Cheng91197042012-08-24 14:19:37 -0700410
Jay Shraunerede67ec2014-09-11 15:03:36 -0700411 // get the EditText to update or see if the request was cancelled.
412 EditText text = sc.getTextField();
Chiao Cheng91197042012-08-24 14:19:37 -0700413
Jay Shraunerede67ec2014-09-11 15:03:36 -0700414 // if the textview is valid, and the cursor is valid and postionable
415 // on the Nth number, then we update the text field and display a
416 // toast indicating the caller name.
417 if ((c != null) && (text != null) && (c.moveToPosition(sc.contactNum))) {
418 String name = c.getString(c.getColumnIndexOrThrow(ADN_NAME_COLUMN_NAME));
419 String number = c.getString(c.getColumnIndexOrThrow(ADN_PHONE_NUMBER_COLUMN_NAME));
Chiao Cheng91197042012-08-24 14:19:37 -0700420
Jay Shraunerede67ec2014-09-11 15:03:36 -0700421 // fill the text in.
422 text.getText().replace(0, 0, number);
Chiao Cheng91197042012-08-24 14:19:37 -0700423
Jay Shraunerede67ec2014-09-11 15:03:36 -0700424 // display the name as a toast
425 Context context = sc.progressDialog.getContext();
426 name = context.getString(R.string.menu_callNumber, name);
427 Toast.makeText(context, name, Toast.LENGTH_SHORT)
428 .show();
429 }
430 } finally {
431 MoreCloseables.closeQuietly(c);
Chiao Cheng91197042012-08-24 14:19:37 -0700432 }
433 }
434
435 public void cancel() {
436 mCanceled = true;
437 // Ask AsyncQueryHandler to cancel the whole request. This will fails when the
438 // query already started.
439 cancelOperation(ADN_QUERY_TOKEN);
440 }
441 }
442}