Chiao Cheng | 9119704 | 2012-08-24 14:19:37 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.dialer; |
| 18 | |
| 19 | import android.app.AlertDialog; |
| 20 | import android.app.KeyguardManager; |
| 21 | import android.app.ProgressDialog; |
Jake Hamby | 1d6fb57 | 2013-04-09 15:49:56 -0700 | [diff] [blame] | 22 | import android.content.ActivityNotFoundException; |
| 23 | import android.content.ComponentName; |
Chiao Cheng | 9119704 | 2012-08-24 14:19:37 -0700 | [diff] [blame] | 24 | import android.content.ContentResolver; |
| 25 | import android.content.Context; |
| 26 | import android.content.DialogInterface; |
| 27 | import android.content.Intent; |
| 28 | import android.database.Cursor; |
| 29 | import android.net.Uri; |
| 30 | import android.os.Looper; |
| 31 | import android.os.RemoteException; |
| 32 | import android.os.ServiceManager; |
Jeff Sharkey | f4f4766 | 2014-04-16 17:21:12 -0700 | [diff] [blame^] | 33 | import android.provider.Settings; |
Chiao Cheng | 9119704 | 2012-08-24 14:19:37 -0700 | [diff] [blame] | 34 | import android.telephony.PhoneNumberUtils; |
| 35 | import android.telephony.TelephonyManager; |
| 36 | import android.util.Log; |
| 37 | import android.view.WindowManager; |
| 38 | import android.widget.EditText; |
| 39 | import android.widget.Toast; |
| 40 | |
Chiao Cheng | 07af764 | 2012-09-14 12:05:14 -0700 | [diff] [blame] | 41 | import com.android.contacts.common.database.NoNullCursorAsyncQueryHandler; |
Chiao Cheng | 9119704 | 2012-08-24 14:19:37 -0700 | [diff] [blame] | 42 | import com.android.internal.telephony.ITelephony; |
Chiao Cheng | 9119704 | 2012-08-24 14:19:37 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Helper class to listen for some magic character sequences |
| 46 | * that are handled specially by the dialer. |
| 47 | * |
| 48 | * Note the Phone app also handles these sequences too (in a couple of |
Jake Hamby | 1d6fb57 | 2013-04-09 15:49:56 -0700 | [diff] [blame] | 49 | * relatively obscure places in the UI), so there's a separate version of |
Chiao Cheng | 9119704 | 2012-08-24 14:19:37 -0700 | [diff] [blame] | 50 | * this class under apps/Phone. |
| 51 | * |
| 52 | * TODO: there's lots of duplicated code between this class and the |
| 53 | * corresponding class under apps/Phone. Let's figure out a way to |
| 54 | * unify these two classes (in the framework? in a common shared library?) |
| 55 | */ |
| 56 | public class SpecialCharSequenceMgr { |
| 57 | private static final String TAG = "SpecialCharSequenceMgr"; |
Jake Hamby | 1d6fb57 | 2013-04-09 15:49:56 -0700 | [diff] [blame] | 58 | |
Yorke Lee | f90dada | 2013-12-09 11:50:28 -0800 | [diff] [blame] | 59 | private static final String SECRET_CODE_ACTION = "android.provider.Telephony.SECRET_CODE"; |
Chiao Cheng | 9119704 | 2012-08-24 14:19:37 -0700 | [diff] [blame] | 60 | private static final String MMI_IMEI_DISPLAY = "*#06#"; |
Jake Hamby | 1d6fb57 | 2013-04-09 15:49:56 -0700 | [diff] [blame] | 61 | private static final String MMI_REGULATORY_INFO_DISPLAY = "*#07#"; |
Chiao Cheng | 9119704 | 2012-08-24 14:19:37 -0700 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * Remembers the previous {@link QueryHandler} and cancel the operation when needed, to |
| 65 | * prevent possible crash. |
| 66 | * |
| 67 | * QueryHandler may call {@link ProgressDialog#dismiss()} when the screen is already gone, |
| 68 | * which will cause the app crash. This variable enables the class to prevent the crash |
| 69 | * on {@link #cleanup()}. |
| 70 | * |
| 71 | * TODO: Remove this and replace it (and {@link #cleanup()}) with better implementation. |
Jake Hamby | 1d6fb57 | 2013-04-09 15:49:56 -0700 | [diff] [blame] | 72 | * One complication is that we have SpecialCharSequenceMgr in Phone package too, which has |
Chiao Cheng | 9119704 | 2012-08-24 14:19:37 -0700 | [diff] [blame] | 73 | * *slightly* different implementation. Note that Phone package doesn't have this problem, |
| 74 | * so the class on Phone side doesn't have this functionality. |
| 75 | * Fundamental fix would be to have one shared implementation and resolve this corner case more |
| 76 | * gracefully. |
| 77 | */ |
| 78 | private static QueryHandler sPreviousAdnQueryHandler; |
| 79 | |
| 80 | /** This class is never instantiated. */ |
| 81 | private SpecialCharSequenceMgr() { |
| 82 | } |
| 83 | |
| 84 | public static boolean handleChars(Context context, String input, EditText textField) { |
| 85 | return handleChars(context, input, false, textField); |
| 86 | } |
| 87 | |
| 88 | static boolean handleChars(Context context, String input) { |
| 89 | return handleChars(context, input, false, null); |
| 90 | } |
| 91 | |
| 92 | static boolean handleChars(Context context, String input, boolean useSystemWindow, |
| 93 | EditText textField) { |
| 94 | |
| 95 | //get rid of the separators so that the string gets parsed correctly |
| 96 | String dialString = PhoneNumberUtils.stripSeparators(input); |
| 97 | |
| 98 | if (handleIMEIDisplay(context, dialString, useSystemWindow) |
Jake Hamby | 1d6fb57 | 2013-04-09 15:49:56 -0700 | [diff] [blame] | 99 | || handleRegulatoryInfoDisplay(context, dialString) |
Chiao Cheng | 9119704 | 2012-08-24 14:19:37 -0700 | [diff] [blame] | 100 | || handlePinEntry(context, dialString) |
| 101 | || handleAdnEntry(context, dialString, textField) |
| 102 | || handleSecretCode(context, dialString)) { |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Cleanup everything around this class. Must be run inside the main thread. |
| 111 | * |
| 112 | * This should be called when the screen becomes background. |
| 113 | */ |
| 114 | public static void cleanup() { |
| 115 | if (Looper.myLooper() != Looper.getMainLooper()) { |
| 116 | Log.wtf(TAG, "cleanup() is called outside the main thread"); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | if (sPreviousAdnQueryHandler != null) { |
| 121 | sPreviousAdnQueryHandler.cancel(); |
| 122 | sPreviousAdnQueryHandler = null; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Handles secret codes to launch arbitrary activities in the form of *#*#<code>#*#*. |
| 128 | * If a secret code is encountered an Intent is started with the android_secret_code://<code> |
| 129 | * URI. |
| 130 | * |
| 131 | * @param context the context to use |
| 132 | * @param input the text to check for a secret code in |
| 133 | * @return true if a secret code was encountered |
| 134 | */ |
| 135 | static boolean handleSecretCode(Context context, String input) { |
| 136 | // Secret codes are in the form *#*#<code>#*#* |
| 137 | int len = input.length(); |
| 138 | if (len > 8 && input.startsWith("*#*#") && input.endsWith("#*#*")) { |
Yorke Lee | f90dada | 2013-12-09 11:50:28 -0800 | [diff] [blame] | 139 | final Intent intent = new Intent(SECRET_CODE_ACTION, |
Chiao Cheng | 9119704 | 2012-08-24 14:19:37 -0700 | [diff] [blame] | 140 | Uri.parse("android_secret_code://" + input.substring(4, len - 4))); |
| 141 | context.sendBroadcast(intent); |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Handle ADN requests by filling in the SIM contact number into the requested |
| 150 | * EditText. |
| 151 | * |
| 152 | * This code works alongside the Asynchronous query handler {@link QueryHandler} |
| 153 | * and query cancel handler implemented in {@link SimContactQueryCookie}. |
| 154 | */ |
| 155 | static boolean handleAdnEntry(Context context, String input, EditText textField) { |
| 156 | /* ADN entries are of the form "N(N)(N)#" */ |
| 157 | |
| 158 | TelephonyManager telephonyManager = |
| 159 | (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| 160 | if (telephonyManager == null |
Yorke Lee | 62280c7 | 2013-11-22 18:24:59 -0800 | [diff] [blame] | 161 | || telephonyManager.getPhoneType() != TelephonyManager.PHONE_TYPE_GSM) { |
Chiao Cheng | 9119704 | 2012-08-24 14:19:37 -0700 | [diff] [blame] | 162 | return false; |
| 163 | } |
| 164 | |
| 165 | // if the phone is keyguard-restricted, then just ignore this |
| 166 | // input. We want to make sure that sim card contacts are NOT |
| 167 | // exposed unless the phone is unlocked, and this code can be |
| 168 | // accessed from the emergency dialer. |
| 169 | KeyguardManager keyguardManager = |
| 170 | (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); |
| 171 | if (keyguardManager.inKeyguardRestrictedInputMode()) { |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | int len = input.length(); |
| 176 | if ((len > 1) && (len < 5) && (input.endsWith("#"))) { |
| 177 | try { |
| 178 | // get the ordinal number of the sim contact |
| 179 | int index = Integer.parseInt(input.substring(0, len-1)); |
| 180 | |
| 181 | // The original code that navigated to a SIM Contacts list view did not |
| 182 | // highlight the requested contact correctly, a requirement for PTCRB |
| 183 | // certification. This behaviour is consistent with the UI paradigm |
| 184 | // for touch-enabled lists, so it does not make sense to try to work |
| 185 | // around it. Instead we fill in the the requested phone number into |
| 186 | // the dialer text field. |
| 187 | |
| 188 | // create the async query handler |
| 189 | QueryHandler handler = new QueryHandler (context.getContentResolver()); |
| 190 | |
| 191 | // create the cookie object |
| 192 | SimContactQueryCookie sc = new SimContactQueryCookie(index - 1, handler, |
| 193 | ADN_QUERY_TOKEN); |
| 194 | |
| 195 | // setup the cookie fields |
| 196 | sc.contactNum = index - 1; |
| 197 | sc.setTextField(textField); |
| 198 | |
| 199 | // create the progress dialog |
| 200 | sc.progressDialog = new ProgressDialog(context); |
| 201 | sc.progressDialog.setTitle(R.string.simContacts_title); |
| 202 | sc.progressDialog.setMessage(context.getText(R.string.simContacts_emptyLoading)); |
| 203 | sc.progressDialog.setIndeterminate(true); |
| 204 | sc.progressDialog.setCancelable(true); |
| 205 | sc.progressDialog.setOnCancelListener(sc); |
| 206 | sc.progressDialog.getWindow().addFlags( |
| 207 | WindowManager.LayoutParams.FLAG_BLUR_BEHIND); |
| 208 | |
| 209 | // display the progress dialog |
| 210 | sc.progressDialog.show(); |
| 211 | |
| 212 | // run the query. |
| 213 | handler.startQuery(ADN_QUERY_TOKEN, sc, Uri.parse("content://icc/adn"), |
| 214 | new String[]{ADN_PHONE_NUMBER_COLUMN_NAME}, null, null, null); |
| 215 | |
| 216 | if (sPreviousAdnQueryHandler != null) { |
| 217 | // It is harmless to call cancel() even after the handler's gone. |
| 218 | sPreviousAdnQueryHandler.cancel(); |
| 219 | } |
| 220 | sPreviousAdnQueryHandler = handler; |
| 221 | return true; |
| 222 | } catch (NumberFormatException ex) { |
| 223 | // Ignore |
| 224 | } |
| 225 | } |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | static boolean handlePinEntry(Context context, String input) { |
| 230 | if ((input.startsWith("**04") || input.startsWith("**05")) && input.endsWith("#")) { |
| 231 | try { |
| 232 | return ITelephony.Stub.asInterface(ServiceManager.getService("phone")) |
| 233 | .handlePinMmi(input); |
| 234 | } catch (RemoteException e) { |
| 235 | Log.e(TAG, "Failed to handlePinMmi due to remote exception"); |
| 236 | return false; |
| 237 | } |
| 238 | } |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | static boolean handleIMEIDisplay(Context context, String input, boolean useSystemWindow) { |
| 243 | TelephonyManager telephonyManager = |
| 244 | (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| 245 | if (telephonyManager != null && input.equals(MMI_IMEI_DISPLAY)) { |
Yorke Lee | 62280c7 | 2013-11-22 18:24:59 -0800 | [diff] [blame] | 246 | int phoneType = telephonyManager.getPhoneType(); |
Chiao Cheng | 9119704 | 2012-08-24 14:19:37 -0700 | [diff] [blame] | 247 | if (phoneType == TelephonyManager.PHONE_TYPE_GSM) { |
| 248 | showIMEIPanel(context, useSystemWindow, telephonyManager); |
| 249 | return true; |
| 250 | } else if (phoneType == TelephonyManager.PHONE_TYPE_CDMA) { |
| 251 | showMEIDPanel(context, useSystemWindow, telephonyManager); |
| 252 | return true; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | return false; |
| 257 | } |
| 258 | |
Jake Hamby | 1d6fb57 | 2013-04-09 15:49:56 -0700 | [diff] [blame] | 259 | private static boolean handleRegulatoryInfoDisplay(Context context, String input) { |
| 260 | if (input.equals(MMI_REGULATORY_INFO_DISPLAY)) { |
| 261 | Log.d(TAG, "handleRegulatoryInfoDisplay() sending intent to settings app"); |
Jeff Sharkey | f4f4766 | 2014-04-16 17:21:12 -0700 | [diff] [blame^] | 262 | Intent showRegInfoIntent = new Intent(Settings.ACTION_SHOW_REGULATORY_INFO); |
Jake Hamby | 1d6fb57 | 2013-04-09 15:49:56 -0700 | [diff] [blame] | 263 | try { |
| 264 | context.startActivity(showRegInfoIntent); |
| 265 | } catch (ActivityNotFoundException e) { |
| 266 | Log.e(TAG, "startActivity() failed: " + e); |
| 267 | } |
| 268 | return true; |
| 269 | } |
| 270 | return false; |
| 271 | } |
| 272 | |
Chiao Cheng | 9119704 | 2012-08-24 14:19:37 -0700 | [diff] [blame] | 273 | // TODO: Combine showIMEIPanel() and showMEIDPanel() into a single |
| 274 | // generic "showDeviceIdPanel()" method, like in the apps/Phone |
| 275 | // version of SpecialCharSequenceMgr.java. (This will require moving |
| 276 | // the phone app's TelephonyCapabilities.getDeviceIdLabel() method |
| 277 | // into the telephony framework, though.) |
| 278 | |
| 279 | private static void showIMEIPanel(Context context, boolean useSystemWindow, |
| 280 | TelephonyManager telephonyManager) { |
| 281 | String imeiStr = telephonyManager.getDeviceId(); |
| 282 | |
| 283 | AlertDialog alert = new AlertDialog.Builder(context) |
| 284 | .setTitle(R.string.imei) |
| 285 | .setMessage(imeiStr) |
| 286 | .setPositiveButton(android.R.string.ok, null) |
| 287 | .setCancelable(false) |
| 288 | .show(); |
| 289 | } |
| 290 | |
| 291 | private static void showMEIDPanel(Context context, boolean useSystemWindow, |
| 292 | TelephonyManager telephonyManager) { |
| 293 | String meidStr = telephonyManager.getDeviceId(); |
| 294 | |
| 295 | AlertDialog alert = new AlertDialog.Builder(context) |
| 296 | .setTitle(R.string.meid) |
| 297 | .setMessage(meidStr) |
| 298 | .setPositiveButton(android.R.string.ok, null) |
| 299 | .setCancelable(false) |
| 300 | .show(); |
| 301 | } |
| 302 | |
| 303 | /******* |
| 304 | * This code is used to handle SIM Contact queries |
| 305 | *******/ |
| 306 | private static final String ADN_PHONE_NUMBER_COLUMN_NAME = "number"; |
| 307 | private static final String ADN_NAME_COLUMN_NAME = "name"; |
| 308 | private static final int ADN_QUERY_TOKEN = -1; |
| 309 | |
| 310 | /** |
| 311 | * Cookie object that contains everything we need to communicate to the |
| 312 | * handler's onQuery Complete, as well as what we need in order to cancel |
| 313 | * the query (if requested). |
| 314 | * |
| 315 | * Note, access to the textField field is going to be synchronized, because |
| 316 | * the user can request a cancel at any time through the UI. |
| 317 | */ |
| 318 | private static class SimContactQueryCookie implements DialogInterface.OnCancelListener{ |
| 319 | public ProgressDialog progressDialog; |
| 320 | public int contactNum; |
| 321 | |
| 322 | // Used to identify the query request. |
| 323 | private int mToken; |
| 324 | private QueryHandler mHandler; |
| 325 | |
| 326 | // The text field we're going to update |
| 327 | private EditText textField; |
| 328 | |
| 329 | public SimContactQueryCookie(int number, QueryHandler handler, int token) { |
| 330 | contactNum = number; |
| 331 | mHandler = handler; |
| 332 | mToken = token; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Synchronized getter for the EditText. |
| 337 | */ |
| 338 | public synchronized EditText getTextField() { |
| 339 | return textField; |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Synchronized setter for the EditText. |
| 344 | */ |
| 345 | public synchronized void setTextField(EditText text) { |
| 346 | textField = text; |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Cancel the ADN query by stopping the operation and signaling |
| 351 | * the cookie that a cancel request is made. |
| 352 | */ |
| 353 | public synchronized void onCancel(DialogInterface dialog) { |
| 354 | // close the progress dialog |
| 355 | if (progressDialog != null) { |
| 356 | progressDialog.dismiss(); |
| 357 | } |
| 358 | |
| 359 | // setting the textfield to null ensures that the UI does NOT get |
| 360 | // updated. |
| 361 | textField = null; |
| 362 | |
| 363 | // Cancel the operation if possible. |
| 364 | mHandler.cancelOperation(mToken); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Asynchronous query handler that services requests to look up ADNs |
| 370 | * |
Jake Hamby | 1d6fb57 | 2013-04-09 15:49:56 -0700 | [diff] [blame] | 371 | * Queries originate from {@link #handleAdnEntry}. |
Chiao Cheng | 9119704 | 2012-08-24 14:19:37 -0700 | [diff] [blame] | 372 | */ |
Chiao Cheng | 07af764 | 2012-09-14 12:05:14 -0700 | [diff] [blame] | 373 | private static class QueryHandler extends NoNullCursorAsyncQueryHandler { |
Chiao Cheng | 9119704 | 2012-08-24 14:19:37 -0700 | [diff] [blame] | 374 | |
| 375 | private boolean mCanceled; |
| 376 | |
| 377 | public QueryHandler(ContentResolver cr) { |
| 378 | super(cr); |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Override basic onQueryComplete to fill in the textfield when |
| 383 | * we're handed the ADN cursor. |
| 384 | */ |
| 385 | @Override |
Chiao Cheng | 07af764 | 2012-09-14 12:05:14 -0700 | [diff] [blame] | 386 | protected void onNotNullableQueryComplete(int token, Object cookie, Cursor c) { |
Chiao Cheng | 9119704 | 2012-08-24 14:19:37 -0700 | [diff] [blame] | 387 | sPreviousAdnQueryHandler = null; |
| 388 | if (mCanceled) { |
| 389 | return; |
| 390 | } |
| 391 | |
| 392 | SimContactQueryCookie sc = (SimContactQueryCookie) cookie; |
| 393 | |
| 394 | // close the progress dialog. |
| 395 | sc.progressDialog.dismiss(); |
| 396 | |
| 397 | // get the EditText to update or see if the request was cancelled. |
| 398 | EditText text = sc.getTextField(); |
| 399 | |
| 400 | // if the textview is valid, and the cursor is valid and postionable |
| 401 | // on the Nth number, then we update the text field and display a |
| 402 | // toast indicating the caller name. |
| 403 | if ((c != null) && (text != null) && (c.moveToPosition(sc.contactNum))) { |
| 404 | String name = c.getString(c.getColumnIndexOrThrow(ADN_NAME_COLUMN_NAME)); |
| 405 | String number = c.getString(c.getColumnIndexOrThrow(ADN_PHONE_NUMBER_COLUMN_NAME)); |
| 406 | |
| 407 | // fill the text in. |
| 408 | text.getText().replace(0, 0, number); |
| 409 | |
| 410 | // display the name as a toast |
| 411 | Context context = sc.progressDialog.getContext(); |
| 412 | name = context.getString(R.string.menu_callNumber, name); |
| 413 | Toast.makeText(context, name, Toast.LENGTH_SHORT) |
| 414 | .show(); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | public void cancel() { |
| 419 | mCanceled = true; |
| 420 | // Ask AsyncQueryHandler to cancel the whole request. This will fails when the |
| 421 | // query already started. |
| 422 | cancelOperation(ADN_QUERY_TOKEN); |
| 423 | } |
| 424 | } |
| 425 | } |