blob: 8e56e45e5c98d441fd85977bb7b9e7f70c8d10e1 [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
19import android.app.AlertDialog;
20import android.app.KeyguardManager;
21import android.app.ProgressDialog;
Jake Hamby1d6fb572013-04-09 15:49:56 -070022import android.content.ActivityNotFoundException;
Chiao Cheng91197042012-08-24 14:19:37 -070023import android.content.ContentResolver;
24import android.content.Context;
25import android.content.DialogInterface;
26import android.content.Intent;
27import android.database.Cursor;
28import android.net.Uri;
29import android.os.Looper;
Jeff Sharkeyf4f47662014-04-16 17:21:12 -070030import android.provider.Settings;
Tyler Gunn9dc924c2014-09-12 09:33:50 -070031import android.telecom.TelecomManager;
Chiao Cheng91197042012-08-24 14:19:37 -070032import android.telephony.PhoneNumberUtils;
33import android.telephony.TelephonyManager;
34import android.util.Log;
35import android.view.WindowManager;
36import android.widget.EditText;
37import android.widget.Toast;
38
Jay Shraunerede67ec2014-09-11 15:03:36 -070039import com.android.common.io.MoreCloseables;
Chiao Cheng07af7642012-09-14 12:05:14 -070040import com.android.contacts.common.database.NoNullCursorAsyncQueryHandler;
Chiao Cheng91197042012-08-24 14:19:37 -070041
Nancy Chen8c258ac2014-10-20 19:33:55 -070042import java.util.ArrayList;
43import java.util.List;
44
Chiao Cheng91197042012-08-24 14:19:37 -070045/**
46 * Helper class to listen for some magic character sequences
47 * that are handled specially by the dialer.
48 *
49 * Note the Phone app also handles these sequences too (in a couple of
Jake Hamby1d6fb572013-04-09 15:49:56 -070050 * relatively obscure places in the UI), so there's a separate version of
Chiao Cheng91197042012-08-24 14:19:37 -070051 * this class under apps/Phone.
52 *
53 * TODO: there's lots of duplicated code between this class and the
54 * corresponding class under apps/Phone. Let's figure out a way to
55 * unify these two classes (in the framework? in a common shared library?)
56 */
57public class SpecialCharSequenceMgr {
58 private static final String TAG = "SpecialCharSequenceMgr";
Jake Hamby1d6fb572013-04-09 15:49:56 -070059
Yorke Leef90dada2013-12-09 11:50:28 -080060 private static final String SECRET_CODE_ACTION = "android.provider.Telephony.SECRET_CODE";
Chiao Cheng91197042012-08-24 14:19:37 -070061 private static final String MMI_IMEI_DISPLAY = "*#06#";
Jake Hamby1d6fb572013-04-09 15:49:56 -070062 private static final String MMI_REGULATORY_INFO_DISPLAY = "*#07#";
Chiao Cheng91197042012-08-24 14:19:37 -070063
64 /**
65 * Remembers the previous {@link QueryHandler} and cancel the operation when needed, to
66 * prevent possible crash.
67 *
68 * QueryHandler may call {@link ProgressDialog#dismiss()} when the screen is already gone,
69 * which will cause the app crash. This variable enables the class to prevent the crash
70 * on {@link #cleanup()}.
71 *
72 * TODO: Remove this and replace it (and {@link #cleanup()}) with better implementation.
Jake Hamby1d6fb572013-04-09 15:49:56 -070073 * One complication is that we have SpecialCharSequenceMgr in Phone package too, which has
Chiao Cheng91197042012-08-24 14:19:37 -070074 * *slightly* different implementation. Note that Phone package doesn't have this problem,
75 * so the class on Phone side doesn't have this functionality.
76 * Fundamental fix would be to have one shared implementation and resolve this corner case more
77 * gracefully.
78 */
79 private static QueryHandler sPreviousAdnQueryHandler;
80
81 /** This class is never instantiated. */
82 private SpecialCharSequenceMgr() {
83 }
84
85 public static boolean handleChars(Context context, String input, EditText textField) {
Chiao Cheng91197042012-08-24 14:19:37 -070086 //get rid of the separators so that the string gets parsed correctly
87 String dialString = PhoneNumberUtils.stripSeparators(input);
88
Nancy Chen8c258ac2014-10-20 19:33:55 -070089 if (handleDeviceIdDisplay(context, dialString)
Jake Hamby1d6fb572013-04-09 15:49:56 -070090 || handleRegulatoryInfoDisplay(context, dialString)
Chiao Cheng91197042012-08-24 14:19:37 -070091 || handlePinEntry(context, dialString)
92 || handleAdnEntry(context, dialString, textField)
93 || handleSecretCode(context, dialString)) {
94 return true;
95 }
96
97 return false;
98 }
99
100 /**
101 * Cleanup everything around this class. Must be run inside the main thread.
102 *
103 * This should be called when the screen becomes background.
104 */
105 public static void cleanup() {
106 if (Looper.myLooper() != Looper.getMainLooper()) {
107 Log.wtf(TAG, "cleanup() is called outside the main thread");
108 return;
109 }
110
111 if (sPreviousAdnQueryHandler != null) {
112 sPreviousAdnQueryHandler.cancel();
113 sPreviousAdnQueryHandler = null;
114 }
115 }
116
117 /**
118 * Handles secret codes to launch arbitrary activities in the form of *#*#<code>#*#*.
119 * If a secret code is encountered an Intent is started with the android_secret_code://<code>
120 * URI.
121 *
122 * @param context the context to use
123 * @param input the text to check for a secret code in
124 * @return true if a secret code was encountered
125 */
126 static boolean handleSecretCode(Context context, String input) {
127 // Secret codes are in the form *#*#<code>#*#*
128 int len = input.length();
129 if (len > 8 && input.startsWith("*#*#") && input.endsWith("#*#*")) {
Yorke Leef90dada2013-12-09 11:50:28 -0800130 final Intent intent = new Intent(SECRET_CODE_ACTION,
Chiao Cheng91197042012-08-24 14:19:37 -0700131 Uri.parse("android_secret_code://" + input.substring(4, len - 4)));
132 context.sendBroadcast(intent);
133 return true;
134 }
135
136 return false;
137 }
138
139 /**
140 * Handle ADN requests by filling in the SIM contact number into the requested
141 * EditText.
142 *
143 * This code works alongside the Asynchronous query handler {@link QueryHandler}
144 * and query cancel handler implemented in {@link SimContactQueryCookie}.
145 */
146 static boolean handleAdnEntry(Context context, String input, EditText textField) {
147 /* ADN entries are of the form "N(N)(N)#" */
148
149 TelephonyManager telephonyManager =
150 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
151 if (telephonyManager == null
Yorke Lee62280c72013-11-22 18:24:59 -0800152 || telephonyManager.getPhoneType() != TelephonyManager.PHONE_TYPE_GSM) {
Chiao Cheng91197042012-08-24 14:19:37 -0700153 return false;
154 }
155
156 // if the phone is keyguard-restricted, then just ignore this
157 // input. We want to make sure that sim card contacts are NOT
158 // exposed unless the phone is unlocked, and this code can be
159 // accessed from the emergency dialer.
160 KeyguardManager keyguardManager =
161 (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
162 if (keyguardManager.inKeyguardRestrictedInputMode()) {
163 return false;
164 }
165
166 int len = input.length();
167 if ((len > 1) && (len < 5) && (input.endsWith("#"))) {
168 try {
169 // get the ordinal number of the sim contact
170 int index = Integer.parseInt(input.substring(0, len-1));
171
172 // The original code that navigated to a SIM Contacts list view did not
173 // highlight the requested contact correctly, a requirement for PTCRB
174 // certification. This behaviour is consistent with the UI paradigm
175 // for touch-enabled lists, so it does not make sense to try to work
176 // around it. Instead we fill in the the requested phone number into
177 // the dialer text field.
178
179 // create the async query handler
180 QueryHandler handler = new QueryHandler (context.getContentResolver());
181
182 // create the cookie object
183 SimContactQueryCookie sc = new SimContactQueryCookie(index - 1, handler,
184 ADN_QUERY_TOKEN);
185
186 // setup the cookie fields
187 sc.contactNum = index - 1;
188 sc.setTextField(textField);
189
190 // create the progress dialog
191 sc.progressDialog = new ProgressDialog(context);
192 sc.progressDialog.setTitle(R.string.simContacts_title);
193 sc.progressDialog.setMessage(context.getText(R.string.simContacts_emptyLoading));
194 sc.progressDialog.setIndeterminate(true);
195 sc.progressDialog.setCancelable(true);
196 sc.progressDialog.setOnCancelListener(sc);
197 sc.progressDialog.getWindow().addFlags(
198 WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
199
200 // display the progress dialog
201 sc.progressDialog.show();
202
203 // run the query.
204 handler.startQuery(ADN_QUERY_TOKEN, sc, Uri.parse("content://icc/adn"),
205 new String[]{ADN_PHONE_NUMBER_COLUMN_NAME}, null, null, null);
206
207 if (sPreviousAdnQueryHandler != null) {
208 // It is harmless to call cancel() even after the handler's gone.
209 sPreviousAdnQueryHandler.cancel();
210 }
211 sPreviousAdnQueryHandler = handler;
212 return true;
213 } catch (NumberFormatException ex) {
214 // Ignore
215 }
216 }
217 return false;
218 }
219
220 static boolean handlePinEntry(Context context, String input) {
221 if ((input.startsWith("**04") || input.startsWith("**05")) && input.endsWith("#")) {
Tyler Gunn9dc924c2014-09-12 09:33:50 -0700222 TelecomManager telecomManager =
223 (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
224 return telecomManager.handleMmi(input);
Chiao Cheng91197042012-08-24 14:19:37 -0700225 }
226 return false;
227 }
228
Nancy Chen8c258ac2014-10-20 19:33:55 -0700229
230 // TODO: Use TelephonyCapabilities.getDeviceIdLabel() to get the device id label instead of a
231 // hard-coded string.
232 static boolean handleDeviceIdDisplay(Context context, String input) {
Chiao Cheng91197042012-08-24 14:19:37 -0700233 TelephonyManager telephonyManager =
234 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Chiao Cheng91197042012-08-24 14:19:37 -0700235
Nancy Chen8c258ac2014-10-20 19:33:55 -0700236 if (telephonyManager != null && input.equals(MMI_IMEI_DISPLAY)) {
237 int labelResId = (telephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) ?
238 R.string.imei : R.string.meid;
239
240 List<String> deviceIds = new ArrayList<String>();
241 for (int slot = 0; slot < telephonyManager.getPhoneCount(); slot++) {
242 deviceIds.add(telephonyManager.getDeviceId(slot));
243 }
244
245 AlertDialog alert = new AlertDialog.Builder(context)
246 .setTitle(labelResId)
247 .setItems(deviceIds.toArray(new String[deviceIds.size()]), null)
248 .setPositiveButton(R.string.ok, null)
249 .setCancelable(false)
250 .show();
251 return true;
252 }
Chiao Cheng91197042012-08-24 14:19:37 -0700253 return false;
254 }
255
Jake Hamby1d6fb572013-04-09 15:49:56 -0700256 private static boolean handleRegulatoryInfoDisplay(Context context, String input) {
257 if (input.equals(MMI_REGULATORY_INFO_DISPLAY)) {
258 Log.d(TAG, "handleRegulatoryInfoDisplay() sending intent to settings app");
Jeff Sharkeyf4f47662014-04-16 17:21:12 -0700259 Intent showRegInfoIntent = new Intent(Settings.ACTION_SHOW_REGULATORY_INFO);
Jake Hamby1d6fb572013-04-09 15:49:56 -0700260 try {
261 context.startActivity(showRegInfoIntent);
262 } catch (ActivityNotFoundException e) {
263 Log.e(TAG, "startActivity() failed: " + e);
264 }
265 return true;
266 }
267 return false;
268 }
269
Chiao Cheng91197042012-08-24 14:19:37 -0700270 /*******
271 * This code is used to handle SIM Contact queries
272 *******/
273 private static final String ADN_PHONE_NUMBER_COLUMN_NAME = "number";
274 private static final String ADN_NAME_COLUMN_NAME = "name";
275 private static final int ADN_QUERY_TOKEN = -1;
276
277 /**
278 * Cookie object that contains everything we need to communicate to the
279 * handler's onQuery Complete, as well as what we need in order to cancel
280 * the query (if requested).
281 *
282 * Note, access to the textField field is going to be synchronized, because
283 * the user can request a cancel at any time through the UI.
284 */
285 private static class SimContactQueryCookie implements DialogInterface.OnCancelListener{
286 public ProgressDialog progressDialog;
287 public int contactNum;
288
289 // Used to identify the query request.
290 private int mToken;
291 private QueryHandler mHandler;
292
293 // The text field we're going to update
294 private EditText textField;
295
296 public SimContactQueryCookie(int number, QueryHandler handler, int token) {
297 contactNum = number;
298 mHandler = handler;
299 mToken = token;
300 }
301
302 /**
303 * Synchronized getter for the EditText.
304 */
305 public synchronized EditText getTextField() {
306 return textField;
307 }
308
309 /**
310 * Synchronized setter for the EditText.
311 */
312 public synchronized void setTextField(EditText text) {
313 textField = text;
314 }
315
316 /**
317 * Cancel the ADN query by stopping the operation and signaling
318 * the cookie that a cancel request is made.
319 */
320 public synchronized void onCancel(DialogInterface dialog) {
321 // close the progress dialog
322 if (progressDialog != null) {
323 progressDialog.dismiss();
324 }
325
326 // setting the textfield to null ensures that the UI does NOT get
327 // updated.
328 textField = null;
329
330 // Cancel the operation if possible.
331 mHandler.cancelOperation(mToken);
332 }
333 }
334
335 /**
336 * Asynchronous query handler that services requests to look up ADNs
337 *
Jake Hamby1d6fb572013-04-09 15:49:56 -0700338 * Queries originate from {@link #handleAdnEntry}.
Chiao Cheng91197042012-08-24 14:19:37 -0700339 */
Chiao Cheng07af7642012-09-14 12:05:14 -0700340 private static class QueryHandler extends NoNullCursorAsyncQueryHandler {
Chiao Cheng91197042012-08-24 14:19:37 -0700341
342 private boolean mCanceled;
343
344 public QueryHandler(ContentResolver cr) {
345 super(cr);
346 }
347
348 /**
349 * Override basic onQueryComplete to fill in the textfield when
350 * we're handed the ADN cursor.
351 */
352 @Override
Chiao Cheng07af7642012-09-14 12:05:14 -0700353 protected void onNotNullableQueryComplete(int token, Object cookie, Cursor c) {
Jay Shraunerede67ec2014-09-11 15:03:36 -0700354 try {
355 sPreviousAdnQueryHandler = null;
356 if (mCanceled) {
357 return;
358 }
Chiao Cheng91197042012-08-24 14:19:37 -0700359
Jay Shraunerede67ec2014-09-11 15:03:36 -0700360 SimContactQueryCookie sc = (SimContactQueryCookie) cookie;
Chiao Cheng91197042012-08-24 14:19:37 -0700361
Jay Shraunerede67ec2014-09-11 15:03:36 -0700362 // close the progress dialog.
363 sc.progressDialog.dismiss();
Chiao Cheng91197042012-08-24 14:19:37 -0700364
Jay Shraunerede67ec2014-09-11 15:03:36 -0700365 // get the EditText to update or see if the request was cancelled.
366 EditText text = sc.getTextField();
Chiao Cheng91197042012-08-24 14:19:37 -0700367
Jay Shraunerede67ec2014-09-11 15:03:36 -0700368 // if the textview is valid, and the cursor is valid and postionable
369 // on the Nth number, then we update the text field and display a
370 // toast indicating the caller name.
371 if ((c != null) && (text != null) && (c.moveToPosition(sc.contactNum))) {
372 String name = c.getString(c.getColumnIndexOrThrow(ADN_NAME_COLUMN_NAME));
373 String number = c.getString(c.getColumnIndexOrThrow(ADN_PHONE_NUMBER_COLUMN_NAME));
Chiao Cheng91197042012-08-24 14:19:37 -0700374
Jay Shraunerede67ec2014-09-11 15:03:36 -0700375 // fill the text in.
376 text.getText().replace(0, 0, number);
Chiao Cheng91197042012-08-24 14:19:37 -0700377
Jay Shraunerede67ec2014-09-11 15:03:36 -0700378 // display the name as a toast
379 Context context = sc.progressDialog.getContext();
380 name = context.getString(R.string.menu_callNumber, name);
381 Toast.makeText(context, name, Toast.LENGTH_SHORT)
382 .show();
383 }
384 } finally {
385 MoreCloseables.closeQuietly(c);
Chiao Cheng91197042012-08-24 14:19:37 -0700386 }
387 }
388
389 public void cancel() {
390 mCanceled = true;
391 // Ask AsyncQueryHandler to cancel the whole request. This will fails when the
392 // query already started.
393 cancelOperation(ADN_QUERY_TOKEN);
394 }
395 }
396}