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