Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [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 | |
Eric Erfanian | fc0eb8c | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 17 | package com.android.dialer.dialpadview; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 18 | |
Eric Erfanian | fc0eb8c | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 19 | import android.Manifest; |
roldenburg | 9c9175f | 2018-02-15 15:22:24 -0800 | [diff] [blame] | 20 | import android.annotation.SuppressLint; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 21 | import android.app.Activity; |
| 22 | import android.app.AlertDialog; |
| 23 | import android.app.DialogFragment; |
| 24 | import android.app.KeyguardManager; |
| 25 | import android.app.ProgressDialog; |
| 26 | import android.content.ActivityNotFoundException; |
| 27 | import android.content.ContentResolver; |
| 28 | import android.content.Context; |
| 29 | import android.content.DialogInterface; |
| 30 | import android.content.Intent; |
| 31 | import android.database.Cursor; |
roldenburg | 9c9175f | 2018-02-15 15:22:24 -0800 | [diff] [blame] | 32 | import android.graphics.Bitmap; |
| 33 | import android.graphics.Bitmap.Config; |
| 34 | import android.graphics.Color; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 35 | import android.net.Uri; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 36 | import android.provider.Settings; |
| 37 | import android.support.annotation.Nullable; |
roldenburg | 12c4e71 | 2018-01-30 17:40:21 -0800 | [diff] [blame] | 38 | import android.support.annotation.VisibleForTesting; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 39 | import android.telecom.PhoneAccount; |
| 40 | import android.telecom.PhoneAccountHandle; |
| 41 | import android.telephony.PhoneNumberUtils; |
| 42 | import android.telephony.TelephonyManager; |
| 43 | import android.text.TextUtils; |
roldenburg | 9c9175f | 2018-02-15 15:22:24 -0800 | [diff] [blame] | 44 | import android.view.LayoutInflater; |
| 45 | import android.view.View; |
| 46 | import android.view.ViewGroup; |
| 47 | import android.view.ViewGroup.LayoutParams; |
| 48 | import android.view.ViewTreeObserver.OnGlobalLayoutListener; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 49 | import android.view.WindowManager; |
| 50 | import android.widget.EditText; |
roldenburg | 9c9175f | 2018-02-15 15:22:24 -0800 | [diff] [blame] | 51 | import android.widget.ImageView; |
| 52 | import android.widget.TextView; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 53 | import android.widget.Toast; |
| 54 | import com.android.common.io.MoreCloseables; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 55 | import com.android.contacts.common.database.NoNullCursorAsyncQueryHandler; |
| 56 | import com.android.contacts.common.util.ContactDisplayUtils; |
| 57 | import com.android.contacts.common.widget.SelectPhoneAccountDialogFragment; |
| 58 | import com.android.contacts.common.widget.SelectPhoneAccountDialogFragment.SelectPhoneAccountListener; |
twyen | 66adad0 | 2018-04-24 13:51:08 -0700 | [diff] [blame] | 59 | import com.android.contacts.common.widget.SelectPhoneAccountDialogOptionsUtil; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 60 | import com.android.dialer.common.Assert; |
| 61 | import com.android.dialer.common.LogUtil; |
Eric Erfanian | fc0eb8c | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 62 | import com.android.dialer.compat.telephony.TelephonyManagerCompat; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 63 | import com.android.dialer.oem.MotorolaUtils; |
linyuh | 1d1111e | 2018-03-20 15:45:25 -0700 | [diff] [blame] | 64 | import com.android.dialer.oem.TranssionUtils; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 65 | import com.android.dialer.telecom.TelecomUtil; |
Eric Erfanian | fc0eb8c | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 66 | import com.android.dialer.util.PermissionsUtil; |
roldenburg | 9c9175f | 2018-02-15 15:22:24 -0800 | [diff] [blame] | 67 | import com.google.zxing.BarcodeFormat; |
| 68 | import com.google.zxing.MultiFormatWriter; |
| 69 | import com.google.zxing.WriterException; |
| 70 | import com.google.zxing.common.BitMatrix; |
roldenburg | 9c9175f | 2018-02-15 15:22:24 -0800 | [diff] [blame] | 71 | import java.util.Arrays; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 72 | import java.util.List; |
roldenburg | 9c9175f | 2018-02-15 15:22:24 -0800 | [diff] [blame] | 73 | import java.util.Locale; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 74 | |
| 75 | /** |
linyuh | 1d1111e | 2018-03-20 15:45:25 -0700 | [diff] [blame] | 76 | * Helper class to listen for some magic character sequences that are handled specially by Dialer. |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 77 | */ |
| 78 | public class SpecialCharSequenceMgr { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 79 | private static final String TAG_SELECT_ACCT_FRAGMENT = "tag_select_acct_fragment"; |
| 80 | |
roldenburg | 9c9175f | 2018-02-15 15:22:24 -0800 | [diff] [blame] | 81 | @VisibleForTesting static final String MMI_IMEI_DISPLAY = "*#06#"; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 82 | private static final String MMI_REGULATORY_INFO_DISPLAY = "*#07#"; |
| 83 | /** ***** This code is used to handle SIM Contact queries ***** */ |
| 84 | private static final String ADN_PHONE_NUMBER_COLUMN_NAME = "number"; |
| 85 | |
| 86 | private static final String ADN_NAME_COLUMN_NAME = "name"; |
| 87 | private static final int ADN_QUERY_TOKEN = -1; |
roldenburg | 12c4e71 | 2018-01-30 17:40:21 -0800 | [diff] [blame] | 88 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 89 | /** |
| 90 | * Remembers the previous {@link QueryHandler} and cancel the operation when needed, to prevent |
| 91 | * possible crash. |
| 92 | * |
| 93 | * <p>QueryHandler may call {@link ProgressDialog#dismiss()} when the screen is already gone, |
| 94 | * which will cause the app crash. This variable enables the class to prevent the crash on {@link |
| 95 | * #cleanup()}. |
| 96 | * |
| 97 | * <p>TODO: Remove this and replace it (and {@link #cleanup()}) with better implementation. One |
| 98 | * complication is that we have SpecialCharSequenceMgr in Phone package too, which has *slightly* |
| 99 | * different implementation. Note that Phone package doesn't have this problem, so the class on |
| 100 | * Phone side doesn't have this functionality. Fundamental fix would be to have one shared |
| 101 | * implementation and resolve this corner case more gracefully. |
| 102 | */ |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 103 | private static QueryHandler previousAdnQueryHandler; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 104 | |
| 105 | /** This class is never instantiated. */ |
| 106 | private SpecialCharSequenceMgr() {} |
| 107 | |
| 108 | public static boolean handleChars(Context context, String input, EditText textField) { |
Eric Erfanian | fc0eb8c | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 109 | // get rid of the separators so that the string gets parsed correctly |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 110 | String dialString = PhoneNumberUtils.stripSeparators(input); |
| 111 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 112 | if (handleDeviceIdDisplay(context, dialString) |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 113 | || handleRegulatoryInfoDisplay(context, dialString) |
| 114 | || handlePinEntry(context, dialString) |
| 115 | || handleAdnEntry(context, dialString, textField) |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 116 | || handleSecretCode(context, dialString)) { |
| 117 | return true; |
| 118 | } |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 119 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 120 | if (MotorolaUtils.handleSpecialCharSequence(context, input)) { |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | return false; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Cleanup everything around this class. Must be run inside the main thread. |
| 129 | * |
| 130 | * <p>This should be called when the screen becomes background. |
| 131 | */ |
| 132 | public static void cleanup() { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 133 | Assert.isMainThread(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 134 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 135 | if (previousAdnQueryHandler != null) { |
| 136 | previousAdnQueryHandler.cancel(); |
| 137 | previousAdnQueryHandler = null; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
roldenburg | 6b0a018 | 2018-01-24 14:09:58 -0800 | [diff] [blame] | 142 | * Handles secret codes to launch arbitrary activities in the form of |
| 143 | * *#*#<code>#*#* or *#<code_starting_with_number>#. |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 144 | * |
| 145 | * @param context the context to use |
| 146 | * @param input the text to check for a secret code in |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 147 | * @return true if a secret code was encountered and handled |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 148 | */ |
| 149 | static boolean handleSecretCode(Context context, String input) { |
linyuh | 1d1111e | 2018-03-20 15:45:25 -0700 | [diff] [blame] | 150 | // Secret code specific to OEMs should be handled first. |
| 151 | if (TranssionUtils.isTranssionSecretCode(input)) { |
| 152 | TranssionUtils.handleTranssionSecretCode(context, input); |
| 153 | return true; |
| 154 | } |
| 155 | |
roldenburg | 6b0a018 | 2018-01-24 14:09:58 -0800 | [diff] [blame] | 156 | // Secret codes are accessed by dialing *#*#<code>#*#* or "*#<code_starting_with_number>#" |
| 157 | if (input.length() > 8 && input.startsWith("*#*#") && input.endsWith("#*#*")) { |
| 158 | String secretCode = input.substring(4, input.length() - 4); |
| 159 | TelephonyManagerCompat.handleSecretCode(context, secretCode); |
| 160 | return true; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 161 | } |
twyen | 6b56ebf | 2018-03-12 15:14:30 -0700 | [diff] [blame] | 162 | |
roldenburg | 6b0a018 | 2018-01-24 14:09:58 -0800 | [diff] [blame] | 163 | return false; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Handle ADN requests by filling in the SIM contact number into the requested EditText. |
| 168 | * |
| 169 | * <p>This code works alongside the Asynchronous query handler {@link QueryHandler} and query |
| 170 | * cancel handler implemented in {@link SimContactQueryCookie}. |
| 171 | */ |
| 172 | static boolean handleAdnEntry(Context context, String input, EditText textField) { |
| 173 | /* ADN entries are of the form "N(N)(N)#" */ |
| 174 | TelephonyManager telephonyManager = |
| 175 | (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| 176 | if (telephonyManager == null |
| 177 | || telephonyManager.getPhoneType() != TelephonyManager.PHONE_TYPE_GSM) { |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | // if the phone is keyguard-restricted, then just ignore this |
| 182 | // input. We want to make sure that sim card contacts are NOT |
| 183 | // exposed unless the phone is unlocked, and this code can be |
| 184 | // accessed from the emergency dialer. |
| 185 | KeyguardManager keyguardManager = |
| 186 | (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); |
| 187 | if (keyguardManager.inKeyguardRestrictedInputMode()) { |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | int len = input.length(); |
| 192 | if ((len > 1) && (len < 5) && (input.endsWith("#"))) { |
| 193 | try { |
| 194 | // get the ordinal number of the sim contact |
| 195 | final int index = Integer.parseInt(input.substring(0, len - 1)); |
| 196 | |
| 197 | // The original code that navigated to a SIM Contacts list view did not |
| 198 | // highlight the requested contact correctly, a requirement for PTCRB |
| 199 | // certification. This behaviour is consistent with the UI paradigm |
| 200 | // for touch-enabled lists, so it does not make sense to try to work |
| 201 | // around it. Instead we fill in the the requested phone number into |
| 202 | // the dialer text field. |
| 203 | |
| 204 | // create the async query handler |
| 205 | final QueryHandler handler = new QueryHandler(context.getContentResolver()); |
| 206 | |
| 207 | // create the cookie object |
| 208 | final SimContactQueryCookie sc = |
| 209 | new SimContactQueryCookie(index - 1, handler, ADN_QUERY_TOKEN); |
| 210 | |
| 211 | // setup the cookie fields |
| 212 | sc.contactNum = index - 1; |
| 213 | sc.setTextField(textField); |
| 214 | |
| 215 | // create the progress dialog |
| 216 | sc.progressDialog = new ProgressDialog(context); |
| 217 | sc.progressDialog.setTitle(R.string.simContacts_title); |
| 218 | sc.progressDialog.setMessage(context.getText(R.string.simContacts_emptyLoading)); |
| 219 | sc.progressDialog.setIndeterminate(true); |
| 220 | sc.progressDialog.setCancelable(true); |
| 221 | sc.progressDialog.setOnCancelListener(sc); |
| 222 | sc.progressDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); |
| 223 | |
| 224 | List<PhoneAccountHandle> subscriptionAccountHandles = |
twyen | c1623fe | 2018-01-25 15:54:00 -0800 | [diff] [blame] | 225 | TelecomUtil.getSubscriptionPhoneAccounts(context); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 226 | Context applicationContext = context.getApplicationContext(); |
| 227 | boolean hasUserSelectedDefault = |
| 228 | subscriptionAccountHandles.contains( |
| 229 | TelecomUtil.getDefaultOutgoingPhoneAccount( |
| 230 | applicationContext, PhoneAccount.SCHEME_TEL)); |
| 231 | |
| 232 | if (subscriptionAccountHandles.size() <= 1 || hasUserSelectedDefault) { |
| 233 | Uri uri = TelecomUtil.getAdnUriForPhoneAccount(applicationContext, null); |
| 234 | handleAdnQuery(handler, sc, uri); |
| 235 | } else { |
| 236 | SelectPhoneAccountListener callback = |
| 237 | new HandleAdnEntryAccountSelectedCallback(applicationContext, handler, sc); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 238 | DialogFragment dialogFragment = |
| 239 | SelectPhoneAccountDialogFragment.newInstance( |
twyen | 66adad0 | 2018-04-24 13:51:08 -0700 | [diff] [blame] | 240 | SelectPhoneAccountDialogOptionsUtil.builderWithAccounts( |
| 241 | subscriptionAccountHandles) |
| 242 | .build(), |
| 243 | callback); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 244 | dialogFragment.show(((Activity) context).getFragmentManager(), TAG_SELECT_ACCT_FRAGMENT); |
| 245 | } |
| 246 | |
| 247 | return true; |
| 248 | } catch (NumberFormatException ex) { |
| 249 | // Ignore |
| 250 | } |
| 251 | } |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | private static void handleAdnQuery(QueryHandler handler, SimContactQueryCookie cookie, Uri uri) { |
| 256 | if (handler == null || cookie == null || uri == null) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 257 | LogUtil.w("SpecialCharSequenceMgr.handleAdnQuery", "queryAdn parameters incorrect"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 258 | return; |
| 259 | } |
| 260 | |
| 261 | // display the progress dialog |
| 262 | cookie.progressDialog.show(); |
| 263 | |
| 264 | // run the query. |
| 265 | handler.startQuery( |
| 266 | ADN_QUERY_TOKEN, |
| 267 | cookie, |
| 268 | uri, |
| 269 | new String[] {ADN_PHONE_NUMBER_COLUMN_NAME}, |
| 270 | null, |
| 271 | null, |
| 272 | null); |
| 273 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 274 | if (previousAdnQueryHandler != null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 275 | // It is harmless to call cancel() even after the handler's gone. |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 276 | previousAdnQueryHandler.cancel(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 277 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 278 | previousAdnQueryHandler = handler; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static boolean handlePinEntry(final Context context, final String input) { |
| 282 | if ((input.startsWith("**04") || input.startsWith("**05")) && input.endsWith("#")) { |
| 283 | List<PhoneAccountHandle> subscriptionAccountHandles = |
twyen | c1623fe | 2018-01-25 15:54:00 -0800 | [diff] [blame] | 284 | TelecomUtil.getSubscriptionPhoneAccounts(context); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 285 | boolean hasUserSelectedDefault = |
| 286 | subscriptionAccountHandles.contains( |
| 287 | TelecomUtil.getDefaultOutgoingPhoneAccount(context, PhoneAccount.SCHEME_TEL)); |
| 288 | |
| 289 | if (subscriptionAccountHandles.size() <= 1 || hasUserSelectedDefault) { |
| 290 | // Don't bring up the dialog for single-SIM or if the default outgoing account is |
| 291 | // a subscription account. |
| 292 | return TelecomUtil.handleMmi(context, input, null); |
| 293 | } else { |
| 294 | SelectPhoneAccountListener listener = new HandleMmiAccountSelectedCallback(context, input); |
| 295 | |
| 296 | DialogFragment dialogFragment = |
| 297 | SelectPhoneAccountDialogFragment.newInstance( |
twyen | 66adad0 | 2018-04-24 13:51:08 -0700 | [diff] [blame] | 298 | SelectPhoneAccountDialogOptionsUtil.builderWithAccounts(subscriptionAccountHandles) |
| 299 | .build(), |
| 300 | listener); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 301 | dialogFragment.show(((Activity) context).getFragmentManager(), TAG_SELECT_ACCT_FRAGMENT); |
| 302 | } |
| 303 | return true; |
| 304 | } |
| 305 | return false; |
| 306 | } |
| 307 | |
| 308 | // TODO: Use TelephonyCapabilities.getDeviceIdLabel() to get the device id label instead of a |
| 309 | // hard-coded string. |
roldenburg | 9c9175f | 2018-02-15 15:22:24 -0800 | [diff] [blame] | 310 | @SuppressLint("HardwareIds") |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 311 | static boolean handleDeviceIdDisplay(Context context, String input) { |
Eric Erfanian | fc0eb8c | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 312 | if (!PermissionsUtil.hasPermission(context, Manifest.permission.READ_PHONE_STATE)) { |
| 313 | return false; |
| 314 | } |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 315 | TelephonyManager telephonyManager = |
| 316 | (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| 317 | |
| 318 | if (telephonyManager != null && input.equals(MMI_IMEI_DISPLAY)) { |
| 319 | int labelResId = |
| 320 | (telephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) |
| 321 | ? R.string.imei |
| 322 | : R.string.meid; |
| 323 | |
roldenburg | 9c9175f | 2018-02-15 15:22:24 -0800 | [diff] [blame] | 324 | View customView = LayoutInflater.from(context).inflate(R.layout.dialog_deviceids, null); |
| 325 | ViewGroup holder = customView.findViewById(R.id.deviceids_holder); |
| 326 | |
Eric Erfanian | 91ce7d2 | 2017-06-05 13:35:02 -0700 | [diff] [blame] | 327 | if (TelephonyManagerCompat.getPhoneCount(telephonyManager) > 1) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 328 | for (int slot = 0; slot < telephonyManager.getPhoneCount(); slot++) { |
| 329 | String deviceId = telephonyManager.getDeviceId(slot); |
| 330 | if (!TextUtils.isEmpty(deviceId)) { |
roldenburg | 3da989d | 2018-02-15 15:53:20 -0800 | [diff] [blame] | 331 | addDeviceIdRow( |
| 332 | holder, |
| 333 | deviceId, |
| 334 | /* showDecimal */ |
| 335 | context.getResources().getBoolean(R.bool.show_device_id_in_hex_and_decimal), |
| 336 | /* showBarcode */ false); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | } else { |
roldenburg | 3da989d | 2018-02-15 15:53:20 -0800 | [diff] [blame] | 340 | addDeviceIdRow( |
| 341 | holder, |
| 342 | telephonyManager.getDeviceId(), |
| 343 | /* showDecimal */ |
| 344 | context.getResources().getBoolean(R.bool.show_device_id_in_hex_and_decimal), |
| 345 | /* showBarcode */ |
| 346 | context.getResources().getBoolean(R.bool.show_device_id_as_barcode)); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | new AlertDialog.Builder(context) |
| 350 | .setTitle(labelResId) |
roldenburg | 9c9175f | 2018-02-15 15:22:24 -0800 | [diff] [blame] | 351 | .setView(customView) |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 352 | .setPositiveButton(android.R.string.ok, null) |
| 353 | .setCancelable(false) |
roldenburg | 9c9175f | 2018-02-15 15:22:24 -0800 | [diff] [blame] | 354 | .show() |
| 355 | .getWindow() |
| 356 | .setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 357 | return true; |
| 358 | } |
| 359 | return false; |
| 360 | } |
| 361 | |
roldenburg | 3da989d | 2018-02-15 15:53:20 -0800 | [diff] [blame] | 362 | private static void addDeviceIdRow( |
| 363 | ViewGroup holder, String deviceId, boolean showDecimal, boolean showBarcode) { |
roldenburg | 9c9175f | 2018-02-15 15:22:24 -0800 | [diff] [blame] | 364 | if (TextUtils.isEmpty(deviceId)) { |
| 365 | return; |
| 366 | } |
| 367 | |
| 368 | ViewGroup row = |
| 369 | (ViewGroup) |
| 370 | LayoutInflater.from(holder.getContext()).inflate(R.layout.row_deviceid, holder, false); |
| 371 | holder.addView(row); |
| 372 | |
| 373 | // Remove the check digit, if exists. This digit is a checksum of the ID. |
| 374 | // See https://en.wikipedia.org/wiki/International_Mobile_Equipment_Identity |
| 375 | // and https://en.wikipedia.org/wiki/Mobile_equipment_identifier |
| 376 | String hex = deviceId.length() == 15 ? deviceId.substring(0, 14) : deviceId; |
| 377 | |
| 378 | // If this is the valid length IMEI or MEID (14 digits), show it in all formats, otherwise fall |
| 379 | // back to just showing the raw hex |
roldenburg | 3da989d | 2018-02-15 15:53:20 -0800 | [diff] [blame] | 380 | if (hex.length() == 14 && showDecimal) { |
roldenburg | 9c9175f | 2018-02-15 15:22:24 -0800 | [diff] [blame] | 381 | ((TextView) row.findViewById(R.id.deviceid_hex)).setText(hex); |
| 382 | ((TextView) row.findViewById(R.id.deviceid_dec)).setText(getDecimalFromHex(hex)); |
| 383 | row.findViewById(R.id.deviceid_dec_label).setVisibility(View.VISIBLE); |
| 384 | } else { |
roldenburg | 3da989d | 2018-02-15 15:53:20 -0800 | [diff] [blame] | 385 | row.findViewById(R.id.deviceid_hex_label).setVisibility(View.GONE); |
roldenburg | 9c9175f | 2018-02-15 15:22:24 -0800 | [diff] [blame] | 386 | ((TextView) row.findViewById(R.id.deviceid_hex)).setText(deviceId); |
| 387 | } |
| 388 | |
| 389 | final ImageView barcode = row.findViewById(R.id.deviceid_barcode); |
| 390 | if (showBarcode) { |
| 391 | // Wait until the layout pass has completed so we the barcode is measured before drawing. We |
| 392 | // do this by adding a layout listener and setting the bitmap after getting the callback. |
| 393 | barcode |
| 394 | .getViewTreeObserver() |
| 395 | .addOnGlobalLayoutListener( |
| 396 | new OnGlobalLayoutListener() { |
| 397 | @Override |
| 398 | public void onGlobalLayout() { |
| 399 | barcode.getViewTreeObserver().removeOnGlobalLayoutListener(this); |
| 400 | Bitmap barcodeBitmap = |
| 401 | generateBarcode(hex, barcode.getWidth(), barcode.getHeight()); |
| 402 | if (barcodeBitmap != null) { |
| 403 | barcode.setImageBitmap(barcodeBitmap); |
| 404 | } |
| 405 | } |
| 406 | }); |
| 407 | } else { |
| 408 | barcode.setVisibility(View.GONE); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | private static String getDecimalFromHex(String hex) { |
| 413 | final String part1 = hex.substring(0, 8); |
| 414 | final String part2 = hex.substring(8); |
| 415 | |
| 416 | long dec1; |
| 417 | try { |
| 418 | dec1 = Long.parseLong(part1, 16); |
| 419 | } catch (NumberFormatException e) { |
| 420 | LogUtil.e("SpecialCharSequenceMgr.getDecimalFromHex", "unable to parse hex", e); |
| 421 | return ""; |
| 422 | } |
| 423 | |
| 424 | final String manufacturerCode = String.format(Locale.US, "%010d", dec1); |
| 425 | |
| 426 | long dec2; |
| 427 | try { |
| 428 | dec2 = Long.parseLong(part2, 16); |
| 429 | } catch (NumberFormatException e) { |
| 430 | LogUtil.e("SpecialCharSequenceMgr.getDecimalFromHex", "unable to parse hex", e); |
| 431 | return ""; |
| 432 | } |
| 433 | |
| 434 | final String serialNum = String.format(Locale.US, "%08d", dec2); |
| 435 | |
| 436 | StringBuilder builder = new StringBuilder(22); |
| 437 | builder |
| 438 | .append(manufacturerCode, 0, 5) |
| 439 | .append(' ') |
| 440 | .append(manufacturerCode, 5, manufacturerCode.length()) |
| 441 | .append(' ') |
| 442 | .append(serialNum, 0, 4) |
| 443 | .append(' ') |
| 444 | .append(serialNum, 4, serialNum.length()); |
| 445 | return builder.toString(); |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * This method generates a 2d barcode using the zxing library. Each pixel of the bitmap is either |
| 450 | * black or white painted vertically. We determine which color using the BitMatrix.get(x, y) |
| 451 | * method. |
| 452 | */ |
| 453 | private static Bitmap generateBarcode(String hex, int width, int height) { |
| 454 | MultiFormatWriter writer = new MultiFormatWriter(); |
| 455 | String data = Uri.encode(hex); |
| 456 | |
| 457 | try { |
| 458 | BitMatrix bitMatrix = writer.encode(data, BarcodeFormat.CODE_128, width, 1); |
| 459 | Bitmap bitmap = Bitmap.createBitmap(bitMatrix.getWidth(), height, Config.RGB_565); |
| 460 | |
| 461 | for (int i = 0; i < bitMatrix.getWidth(); i++) { |
| 462 | // Paint columns of width 1 |
| 463 | int[] column = new int[height]; |
| 464 | Arrays.fill(column, bitMatrix.get(i, 0) ? Color.BLACK : Color.WHITE); |
| 465 | bitmap.setPixels(column, 0, 1, i, 0, 1, height); |
| 466 | } |
| 467 | return bitmap; |
| 468 | } catch (WriterException e) { |
| 469 | LogUtil.e("SpecialCharSequenceMgr.generateBarcode", "error generating barcode", e); |
| 470 | } |
| 471 | return null; |
| 472 | } |
| 473 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 474 | private static boolean handleRegulatoryInfoDisplay(Context context, String input) { |
| 475 | if (input.equals(MMI_REGULATORY_INFO_DISPLAY)) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 476 | LogUtil.i( |
| 477 | "SpecialCharSequenceMgr.handleRegulatoryInfoDisplay", "sending intent to settings app"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 478 | Intent showRegInfoIntent = new Intent(Settings.ACTION_SHOW_REGULATORY_INFO); |
| 479 | try { |
| 480 | context.startActivity(showRegInfoIntent); |
| 481 | } catch (ActivityNotFoundException e) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 482 | LogUtil.e( |
| 483 | "SpecialCharSequenceMgr.handleRegulatoryInfoDisplay", "startActivity() failed: ", e); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 484 | } |
| 485 | return true; |
| 486 | } |
| 487 | return false; |
| 488 | } |
| 489 | |
| 490 | public static class HandleAdnEntryAccountSelectedCallback extends SelectPhoneAccountListener { |
| 491 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 492 | private final Context context; |
| 493 | private final QueryHandler queryHandler; |
| 494 | private final SimContactQueryCookie cookie; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 495 | |
| 496 | public HandleAdnEntryAccountSelectedCallback( |
| 497 | Context context, QueryHandler queryHandler, SimContactQueryCookie cookie) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 498 | this.context = context; |
| 499 | this.queryHandler = queryHandler; |
| 500 | this.cookie = cookie; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | @Override |
| 504 | public void onPhoneAccountSelected( |
| 505 | PhoneAccountHandle selectedAccountHandle, boolean setDefault, @Nullable String callId) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 506 | Uri uri = TelecomUtil.getAdnUriForPhoneAccount(context, selectedAccountHandle); |
| 507 | handleAdnQuery(queryHandler, cookie, uri); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 508 | // TODO: Show error dialog if result isn't valid. |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | public static class HandleMmiAccountSelectedCallback extends SelectPhoneAccountListener { |
| 513 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 514 | private final Context context; |
| 515 | private final String input; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 516 | |
| 517 | public HandleMmiAccountSelectedCallback(Context context, String input) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 518 | this.context = context.getApplicationContext(); |
| 519 | this.input = input; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | @Override |
| 523 | public void onPhoneAccountSelected( |
| 524 | PhoneAccountHandle selectedAccountHandle, boolean setDefault, @Nullable String callId) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 525 | TelecomUtil.handleMmi(context, input, selectedAccountHandle); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * Cookie object that contains everything we need to communicate to the handler's onQuery |
| 531 | * Complete, as well as what we need in order to cancel the query (if requested). |
| 532 | * |
| 533 | * <p>Note, access to the textField field is going to be synchronized, because the user can |
| 534 | * request a cancel at any time through the UI. |
| 535 | */ |
| 536 | private static class SimContactQueryCookie implements DialogInterface.OnCancelListener { |
| 537 | |
| 538 | public ProgressDialog progressDialog; |
| 539 | public int contactNum; |
| 540 | |
| 541 | // Used to identify the query request. |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 542 | private int token; |
| 543 | private QueryHandler handler; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 544 | |
| 545 | // The text field we're going to update |
| 546 | private EditText textField; |
| 547 | |
| 548 | public SimContactQueryCookie(int number, QueryHandler handler, int token) { |
| 549 | contactNum = number; |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 550 | this.handler = handler; |
| 551 | this.token = token; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | /** Synchronized getter for the EditText. */ |
| 555 | public synchronized EditText getTextField() { |
| 556 | return textField; |
| 557 | } |
| 558 | |
| 559 | /** Synchronized setter for the EditText. */ |
| 560 | public synchronized void setTextField(EditText text) { |
| 561 | textField = text; |
| 562 | } |
| 563 | |
| 564 | /** |
| 565 | * Cancel the ADN query by stopping the operation and signaling the cookie that a cancel request |
| 566 | * is made. |
| 567 | */ |
| 568 | @Override |
| 569 | public synchronized void onCancel(DialogInterface dialog) { |
| 570 | // close the progress dialog |
| 571 | if (progressDialog != null) { |
| 572 | progressDialog.dismiss(); |
| 573 | } |
| 574 | |
| 575 | // setting the textfield to null ensures that the UI does NOT get |
| 576 | // updated. |
| 577 | textField = null; |
| 578 | |
| 579 | // Cancel the operation if possible. |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 580 | handler.cancelOperation(token); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | |
| 584 | /** |
| 585 | * Asynchronous query handler that services requests to look up ADNs |
| 586 | * |
| 587 | * <p>Queries originate from {@link #handleAdnEntry}. |
| 588 | */ |
| 589 | private static class QueryHandler extends NoNullCursorAsyncQueryHandler { |
| 590 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 591 | private boolean canceled; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 592 | |
| 593 | public QueryHandler(ContentResolver cr) { |
| 594 | super(cr); |
| 595 | } |
| 596 | |
| 597 | /** Override basic onQueryComplete to fill in the textfield when we're handed the ADN cursor. */ |
| 598 | @Override |
| 599 | protected void onNotNullableQueryComplete(int token, Object cookie, Cursor c) { |
| 600 | try { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 601 | previousAdnQueryHandler = null; |
| 602 | if (canceled) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 603 | return; |
| 604 | } |
| 605 | |
| 606 | SimContactQueryCookie sc = (SimContactQueryCookie) cookie; |
| 607 | |
| 608 | // close the progress dialog. |
| 609 | sc.progressDialog.dismiss(); |
| 610 | |
| 611 | // get the EditText to update or see if the request was cancelled. |
| 612 | EditText text = sc.getTextField(); |
| 613 | |
| 614 | // if the TextView is valid, and the cursor is valid and positionable on the |
| 615 | // Nth number, then we update the text field and display a toast indicating the |
| 616 | // caller name. |
| 617 | if ((c != null) && (text != null) && (c.moveToPosition(sc.contactNum))) { |
| 618 | String name = c.getString(c.getColumnIndexOrThrow(ADN_NAME_COLUMN_NAME)); |
| 619 | String number = c.getString(c.getColumnIndexOrThrow(ADN_PHONE_NUMBER_COLUMN_NAME)); |
| 620 | |
| 621 | // fill the text in. |
| 622 | text.getText().replace(0, 0, number); |
| 623 | |
| 624 | // display the name as a toast |
| 625 | Context context = sc.progressDialog.getContext(); |
| 626 | CharSequence msg = |
| 627 | ContactDisplayUtils.getTtsSpannedPhoneNumber( |
| 628 | context.getResources(), R.string.menu_callNumber, name); |
| 629 | Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); |
| 630 | } |
| 631 | } finally { |
| 632 | MoreCloseables.closeQuietly(c); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | public void cancel() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 637 | canceled = true; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 638 | // Ask AsyncQueryHandler to cancel the whole request. This will fail when the query is |
| 639 | // already started. |
| 640 | cancelOperation(ADN_QUERY_TOKEN); |
| 641 | } |
| 642 | } |
| 643 | } |