blob: d2652ee669372c8162d4b7c312e59e4d78c812bf [file] [log] [blame]
Eric Erfanianccca3152017-02-22 16:32:36 -08001/*
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 Erfanianfc0eb8c2017-08-31 06:57:16 -070017package com.android.dialer.dialpadview;
Eric Erfanianccca3152017-02-22 16:32:36 -080018
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -070019import android.Manifest;
roldenburg9c9175f2018-02-15 15:22:24 -080020import android.annotation.SuppressLint;
Eric Erfanianccca3152017-02-22 16:32:36 -080021import android.app.Activity;
22import android.app.AlertDialog;
23import android.app.DialogFragment;
24import android.app.KeyguardManager;
25import android.app.ProgressDialog;
26import android.content.ActivityNotFoundException;
27import android.content.ContentResolver;
28import android.content.Context;
29import android.content.DialogInterface;
30import android.content.Intent;
31import android.database.Cursor;
roldenburg9c9175f2018-02-15 15:22:24 -080032import android.graphics.Bitmap;
33import android.graphics.Bitmap.Config;
34import android.graphics.Color;
Eric Erfanianccca3152017-02-22 16:32:36 -080035import android.net.Uri;
Eric Erfanianccca3152017-02-22 16:32:36 -080036import android.provider.Settings;
37import android.support.annotation.Nullable;
roldenburg12c4e712018-01-30 17:40:21 -080038import android.support.annotation.VisibleForTesting;
Eric Erfanianccca3152017-02-22 16:32:36 -080039import android.telecom.PhoneAccount;
40import android.telecom.PhoneAccountHandle;
41import android.telephony.PhoneNumberUtils;
42import android.telephony.TelephonyManager;
43import android.text.TextUtils;
roldenburg9c9175f2018-02-15 15:22:24 -080044import android.view.LayoutInflater;
45import android.view.View;
46import android.view.ViewGroup;
47import android.view.ViewGroup.LayoutParams;
48import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Eric Erfanianccca3152017-02-22 16:32:36 -080049import android.view.WindowManager;
50import android.widget.EditText;
roldenburg9c9175f2018-02-15 15:22:24 -080051import android.widget.ImageView;
52import android.widget.TextView;
Eric Erfanianccca3152017-02-22 16:32:36 -080053import android.widget.Toast;
54import com.android.common.io.MoreCloseables;
Eric Erfanianccca3152017-02-22 16:32:36 -080055import com.android.contacts.common.database.NoNullCursorAsyncQueryHandler;
56import com.android.contacts.common.util.ContactDisplayUtils;
57import com.android.contacts.common.widget.SelectPhoneAccountDialogFragment;
58import com.android.contacts.common.widget.SelectPhoneAccountDialogFragment.SelectPhoneAccountListener;
twyen66adad02018-04-24 13:51:08 -070059import com.android.contacts.common.widget.SelectPhoneAccountDialogOptionsUtil;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070060import com.android.dialer.common.Assert;
61import com.android.dialer.common.LogUtil;
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -070062import com.android.dialer.compat.telephony.TelephonyManagerCompat;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070063import com.android.dialer.oem.MotorolaUtils;
linyuh1d1111e2018-03-20 15:45:25 -070064import com.android.dialer.oem.TranssionUtils;
Eric Erfanianccca3152017-02-22 16:32:36 -080065import com.android.dialer.telecom.TelecomUtil;
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -070066import com.android.dialer.util.PermissionsUtil;
roldenburg9c9175f2018-02-15 15:22:24 -080067import com.google.zxing.BarcodeFormat;
68import com.google.zxing.MultiFormatWriter;
69import com.google.zxing.WriterException;
70import com.google.zxing.common.BitMatrix;
roldenburg9c9175f2018-02-15 15:22:24 -080071import java.util.Arrays;
Eric Erfanianccca3152017-02-22 16:32:36 -080072import java.util.List;
roldenburg9c9175f2018-02-15 15:22:24 -080073import java.util.Locale;
Eric Erfanianccca3152017-02-22 16:32:36 -080074
75/**
linyuh1d1111e2018-03-20 15:45:25 -070076 * Helper class to listen for some magic character sequences that are handled specially by Dialer.
Eric Erfanianccca3152017-02-22 16:32:36 -080077 */
78public class SpecialCharSequenceMgr {
Eric Erfanianccca3152017-02-22 16:32:36 -080079 private static final String TAG_SELECT_ACCT_FRAGMENT = "tag_select_acct_fragment";
80
roldenburg9c9175f2018-02-15 15:22:24 -080081 @VisibleForTesting static final String MMI_IMEI_DISPLAY = "*#06#";
Eric Erfanianccca3152017-02-22 16:32:36 -080082 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;
roldenburg12c4e712018-01-30 17:40:21 -080088
Eric Erfanianccca3152017-02-22 16:32:36 -080089 /**
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 */
linyuh183cb712017-12-27 17:02:37 -0800103 private static QueryHandler previousAdnQueryHandler;
Eric Erfanianccca3152017-02-22 16:32:36 -0800104
105 /** This class is never instantiated. */
106 private SpecialCharSequenceMgr() {}
107
108 public static boolean handleChars(Context context, String input, EditText textField) {
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -0700109 // get rid of the separators so that the string gets parsed correctly
Eric Erfanianccca3152017-02-22 16:32:36 -0800110 String dialString = PhoneNumberUtils.stripSeparators(input);
111
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700112 if (handleDeviceIdDisplay(context, dialString)
Eric Erfanianccca3152017-02-22 16:32:36 -0800113 || handleRegulatoryInfoDisplay(context, dialString)
114 || handlePinEntry(context, dialString)
115 || handleAdnEntry(context, dialString, textField)
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700116 || handleSecretCode(context, dialString)) {
117 return true;
118 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800119
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700120 if (MotorolaUtils.handleSpecialCharSequence(context, input)) {
121 return true;
122 }
123
124 return false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800125 }
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 Erfaniand5e47f62017-03-15 14:41:07 -0700133 Assert.isMainThread();
Eric Erfanianccca3152017-02-22 16:32:36 -0800134
linyuh183cb712017-12-27 17:02:37 -0800135 if (previousAdnQueryHandler != null) {
136 previousAdnQueryHandler.cancel();
137 previousAdnQueryHandler = null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800138 }
139 }
140
141 /**
roldenburg6b0a0182018-01-24 14:09:58 -0800142 * Handles secret codes to launch arbitrary activities in the form of
143 * *#*#<code>#*#* or *#<code_starting_with_number>#.
Eric Erfanianccca3152017-02-22 16:32:36 -0800144 *
145 * @param context the context to use
146 * @param input the text to check for a secret code in
Eric Erfaniand8046e52017-04-06 09:41:50 -0700147 * @return true if a secret code was encountered and handled
Eric Erfanianccca3152017-02-22 16:32:36 -0800148 */
149 static boolean handleSecretCode(Context context, String input) {
linyuh1d1111e2018-03-20 15:45:25 -0700150 // Secret code specific to OEMs should be handled first.
151 if (TranssionUtils.isTranssionSecretCode(input)) {
152 TranssionUtils.handleTranssionSecretCode(context, input);
153 return true;
154 }
155
roldenburg6b0a0182018-01-24 14:09:58 -0800156 // 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 Erfanianccca3152017-02-22 16:32:36 -0800161 }
twyen6b56ebf2018-03-12 15:14:30 -0700162
roldenburg6b0a0182018-01-24 14:09:58 -0800163 return false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800164 }
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 =
twyenc1623fe2018-01-25 15:54:00 -0800225 TelecomUtil.getSubscriptionPhoneAccounts(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800226 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 Erfanianccca3152017-02-22 16:32:36 -0800238 DialogFragment dialogFragment =
239 SelectPhoneAccountDialogFragment.newInstance(
twyen66adad02018-04-24 13:51:08 -0700240 SelectPhoneAccountDialogOptionsUtil.builderWithAccounts(
241 subscriptionAccountHandles)
242 .build(),
243 callback);
Eric Erfanianccca3152017-02-22 16:32:36 -0800244 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 Erfaniand5e47f62017-03-15 14:41:07 -0700257 LogUtil.w("SpecialCharSequenceMgr.handleAdnQuery", "queryAdn parameters incorrect");
Eric Erfanianccca3152017-02-22 16:32:36 -0800258 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
linyuh183cb712017-12-27 17:02:37 -0800274 if (previousAdnQueryHandler != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800275 // It is harmless to call cancel() even after the handler's gone.
linyuh183cb712017-12-27 17:02:37 -0800276 previousAdnQueryHandler.cancel();
Eric Erfanianccca3152017-02-22 16:32:36 -0800277 }
linyuh183cb712017-12-27 17:02:37 -0800278 previousAdnQueryHandler = handler;
Eric Erfanianccca3152017-02-22 16:32:36 -0800279 }
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 =
twyenc1623fe2018-01-25 15:54:00 -0800284 TelecomUtil.getSubscriptionPhoneAccounts(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800285 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(
twyen66adad02018-04-24 13:51:08 -0700298 SelectPhoneAccountDialogOptionsUtil.builderWithAccounts(subscriptionAccountHandles)
299 .build(),
300 listener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800301 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.
roldenburg9c9175f2018-02-15 15:22:24 -0800310 @SuppressLint("HardwareIds")
Eric Erfanianccca3152017-02-22 16:32:36 -0800311 static boolean handleDeviceIdDisplay(Context context, String input) {
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -0700312 if (!PermissionsUtil.hasPermission(context, Manifest.permission.READ_PHONE_STATE)) {
313 return false;
314 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800315 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
roldenburg9c9175f2018-02-15 15:22:24 -0800324 View customView = LayoutInflater.from(context).inflate(R.layout.dialog_deviceids, null);
325 ViewGroup holder = customView.findViewById(R.id.deviceids_holder);
326
Eric Erfanian91ce7d22017-06-05 13:35:02 -0700327 if (TelephonyManagerCompat.getPhoneCount(telephonyManager) > 1) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800328 for (int slot = 0; slot < telephonyManager.getPhoneCount(); slot++) {
329 String deviceId = telephonyManager.getDeviceId(slot);
330 if (!TextUtils.isEmpty(deviceId)) {
roldenburg3da989d2018-02-15 15:53:20 -0800331 addDeviceIdRow(
332 holder,
333 deviceId,
334 /* showDecimal */
335 context.getResources().getBoolean(R.bool.show_device_id_in_hex_and_decimal),
336 /* showBarcode */ false);
Eric Erfanianccca3152017-02-22 16:32:36 -0800337 }
338 }
339 } else {
roldenburg3da989d2018-02-15 15:53:20 -0800340 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 Erfanianccca3152017-02-22 16:32:36 -0800347 }
348
349 new AlertDialog.Builder(context)
350 .setTitle(labelResId)
roldenburg9c9175f2018-02-15 15:22:24 -0800351 .setView(customView)
Eric Erfanianccca3152017-02-22 16:32:36 -0800352 .setPositiveButton(android.R.string.ok, null)
353 .setCancelable(false)
roldenburg9c9175f2018-02-15 15:22:24 -0800354 .show()
355 .getWindow()
356 .setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
Eric Erfanianccca3152017-02-22 16:32:36 -0800357 return true;
358 }
359 return false;
360 }
361
roldenburg3da989d2018-02-15 15:53:20 -0800362 private static void addDeviceIdRow(
363 ViewGroup holder, String deviceId, boolean showDecimal, boolean showBarcode) {
roldenburg9c9175f2018-02-15 15:22:24 -0800364 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
roldenburg3da989d2018-02-15 15:53:20 -0800380 if (hex.length() == 14 && showDecimal) {
roldenburg9c9175f2018-02-15 15:22:24 -0800381 ((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 {
roldenburg3da989d2018-02-15 15:53:20 -0800385 row.findViewById(R.id.deviceid_hex_label).setVisibility(View.GONE);
roldenburg9c9175f2018-02-15 15:22:24 -0800386 ((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 Erfanianccca3152017-02-22 16:32:36 -0800474 private static boolean handleRegulatoryInfoDisplay(Context context, String input) {
475 if (input.equals(MMI_REGULATORY_INFO_DISPLAY)) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700476 LogUtil.i(
477 "SpecialCharSequenceMgr.handleRegulatoryInfoDisplay", "sending intent to settings app");
Eric Erfanianccca3152017-02-22 16:32:36 -0800478 Intent showRegInfoIntent = new Intent(Settings.ACTION_SHOW_REGULATORY_INFO);
479 try {
480 context.startActivity(showRegInfoIntent);
481 } catch (ActivityNotFoundException e) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700482 LogUtil.e(
483 "SpecialCharSequenceMgr.handleRegulatoryInfoDisplay", "startActivity() failed: ", e);
Eric Erfanianccca3152017-02-22 16:32:36 -0800484 }
485 return true;
486 }
487 return false;
488 }
489
490 public static class HandleAdnEntryAccountSelectedCallback extends SelectPhoneAccountListener {
491
linyuh183cb712017-12-27 17:02:37 -0800492 private final Context context;
493 private final QueryHandler queryHandler;
494 private final SimContactQueryCookie cookie;
Eric Erfanianccca3152017-02-22 16:32:36 -0800495
496 public HandleAdnEntryAccountSelectedCallback(
497 Context context, QueryHandler queryHandler, SimContactQueryCookie cookie) {
linyuh183cb712017-12-27 17:02:37 -0800498 this.context = context;
499 this.queryHandler = queryHandler;
500 this.cookie = cookie;
Eric Erfanianccca3152017-02-22 16:32:36 -0800501 }
502
503 @Override
504 public void onPhoneAccountSelected(
505 PhoneAccountHandle selectedAccountHandle, boolean setDefault, @Nullable String callId) {
linyuh183cb712017-12-27 17:02:37 -0800506 Uri uri = TelecomUtil.getAdnUriForPhoneAccount(context, selectedAccountHandle);
507 handleAdnQuery(queryHandler, cookie, uri);
Eric Erfanianccca3152017-02-22 16:32:36 -0800508 // TODO: Show error dialog if result isn't valid.
509 }
510 }
511
512 public static class HandleMmiAccountSelectedCallback extends SelectPhoneAccountListener {
513
linyuh183cb712017-12-27 17:02:37 -0800514 private final Context context;
515 private final String input;
Eric Erfanianccca3152017-02-22 16:32:36 -0800516
517 public HandleMmiAccountSelectedCallback(Context context, String input) {
linyuh183cb712017-12-27 17:02:37 -0800518 this.context = context.getApplicationContext();
519 this.input = input;
Eric Erfanianccca3152017-02-22 16:32:36 -0800520 }
521
522 @Override
523 public void onPhoneAccountSelected(
524 PhoneAccountHandle selectedAccountHandle, boolean setDefault, @Nullable String callId) {
linyuh183cb712017-12-27 17:02:37 -0800525 TelecomUtil.handleMmi(context, input, selectedAccountHandle);
Eric Erfanianccca3152017-02-22 16:32:36 -0800526 }
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.
linyuh183cb712017-12-27 17:02:37 -0800542 private int token;
543 private QueryHandler handler;
Eric Erfanianccca3152017-02-22 16:32:36 -0800544
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;
linyuh183cb712017-12-27 17:02:37 -0800550 this.handler = handler;
551 this.token = token;
Eric Erfanianccca3152017-02-22 16:32:36 -0800552 }
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.
linyuh183cb712017-12-27 17:02:37 -0800580 handler.cancelOperation(token);
Eric Erfanianccca3152017-02-22 16:32:36 -0800581 }
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
linyuh183cb712017-12-27 17:02:37 -0800591 private boolean canceled;
Eric Erfanianccca3152017-02-22 16:32:36 -0800592
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 {
linyuh183cb712017-12-27 17:02:37 -0800601 previousAdnQueryHandler = null;
602 if (canceled) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800603 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() {
linyuh183cb712017-12-27 17:02:37 -0800637 canceled = true;
Eric Erfanianccca3152017-02-22 16:32:36 -0800638 // 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}