• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.phone;
18 
19 import android.app.Activity;
20 import android.app.AlertDialog;
21 import android.content.ActivityNotFoundException;
22 import android.content.ComponentName;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.net.Uri;
26 import android.provider.Settings;
27 
28 import com.android.internal.telephony.TelephonyIntents;
29 import com.android.internal.telephony.Phone;
30 
31 import android.telephony.PhoneNumberUtils;
32 import android.util.Log;
33 import android.view.WindowManager;
34 
35 import com.android.internal.telephony.TelephonyCapabilities;
36 
37 /**
38  * Helper class to listen for some magic dialpad character sequences
39  * that are handled specially by the Phone app.
40  *
41  * Note the Contacts app also handles these sequences too, so there's a
42  * separate version of this class under apps/Contacts.
43  *
44  * In fact, the most common use case for these special sequences is typing
45  * them from the regular "Dialer" used for outgoing calls, which is part
46  * of the contacts app; see DialtactsActivity and DialpadFragment.
47  * *This* version of SpecialCharSequenceMgr is used for only a few
48  * relatively obscure places in the UI:
49  * - The "SIM network unlock" PIN entry screen (see
50  *   IccNetworkDepersonalizationPanel.java)
51  * - The emergency dialer (see EmergencyDialer.java).
52  *
53  * TODO: there's lots of duplicated code between this class and the
54  * corresponding class under apps/Contacts.  Let's figure out a way to
55  * unify these two classes (in the framework? in a common shared library?)
56  */
57 public class SpecialCharSequenceMgr {
58     private static final String TAG = PhoneGlobals.LOG_TAG;
59     private static final boolean DBG = false;
60 
61     private static final String MMI_IMEI_DISPLAY = "*#06#";
62     private static final String MMI_REGULATORY_INFO_DISPLAY = "*#07#";
63 
64     /** This class is never instantiated. */
SpecialCharSequenceMgr()65     private SpecialCharSequenceMgr() {
66     }
67 
68     /**
69      * Check for special strings of digits from an input
70      * string.
71      * @param context input Context for the events we handle.
72      * @param input the dial string to be examined.
73      */
handleChars(Context context, String input)74     static boolean handleChars(Context context, String input) {
75         return handleChars(context, input, null);
76     }
77 
78     /**
79      * Generally used for the Personal Unblocking Key (PUK) unlocking
80      * case, where we want to be able to maintain a handle to the
81      * calling activity so that we can close it or otherwise display
82      * indication if the PUK code is recognized.
83      *
84      * NOTE: The counterpart to this file in Contacts does
85      * NOT contain the special PUK handling code, since it
86      * does NOT need it.  When the device gets into PUK-
87      * locked state, the keyguard comes up and the only way
88      * to unlock the device is through the Emergency dialer,
89      * which is still in the Phone App.
90      *
91      * @param context input Context for the events we handle.
92      * @param input the dial string to be examined.
93      * @param pukInputActivity activity that originated this
94      * PUK call, tracked so that we can close it or otherwise
95      * indicate that special character sequence is
96      * successfully processed. Can be null.
97      * @return true if the input was a special string which has been
98      * handled.
99      */
handleChars(Context context, String input, Activity pukInputActivity)100     static boolean handleChars(Context context,
101                                String input,
102                                Activity pukInputActivity) {
103 
104         //get rid of the separators so that the string gets parsed correctly
105         String dialString = PhoneNumberUtils.stripSeparators(input);
106 
107         if (handleIMEIDisplay(context, dialString)
108             || handleRegulatoryInfoDisplay(context, dialString)
109             || handlePinEntry(context, dialString, pukInputActivity)
110             || handleAdnEntry(context, dialString)
111             || handleSecretCode(dialString)) {
112             return true;
113         }
114 
115         return false;
116     }
117 
118     /**
119      * Variant of handleChars() that looks for the subset of "special
120      * sequences" that are available even if the device is locked.
121      *
122      * (Specifically, these are the sequences that you're allowed to type
123      * in the Emergency Dialer, which is accessible *without* unlocking
124      * the device.)
125      */
handleCharsForLockedDevice(Context context, String input, Activity pukInputActivity)126     static boolean handleCharsForLockedDevice(Context context,
127                                               String input,
128                                               Activity pukInputActivity) {
129         // Get rid of the separators so that the string gets parsed correctly
130         String dialString = PhoneNumberUtils.stripSeparators(input);
131 
132         // The only sequences available on a locked device are the "**04"
133         // or "**05" sequences that allow you to enter PIN or PUK-related
134         // codes.  (e.g. for the case where you're currently locked out of
135         // your phone, and need to change the PIN!  The only way to do
136         // that is via the Emergency Dialer.)
137 
138         if (handlePinEntry(context, dialString, pukInputActivity)) {
139             return true;
140         }
141 
142         return false;
143     }
144 
145     /**
146      * Handles secret codes to launch arbitrary receivers in the form of *#*#<code>#*#*.
147      * If a secret code is encountered, an broadcast intent is sent with the
148      * android_secret_code://<code> URI.
149      *
150      * @param input the text to check for a secret code in
151      * @return true if a secret code was encountered and intent is sent out
152      */
handleSecretCode(String input)153     static private boolean handleSecretCode(String input) {
154         // Secret codes are in the form *#*#<code>#*#*
155         int len = input.length();
156         if (len > 8 && input.startsWith("*#*#") && input.endsWith("#*#*")) {
157             final Phone phone = PhoneGlobals.getPhone();
158             phone.sendDialerSpecialCode(input.substring(4, len - 4));
159             return true;
160         }
161         return false;
162     }
163 
handleAdnEntry(Context context, String input)164     static private boolean handleAdnEntry(Context context, String input) {
165         /* ADN entries are of the form "N(N)(N)#" */
166 
167         // if the phone is keyguard-restricted, then just ignore this
168         // input.  We want to make sure that sim card contacts are NOT
169         // exposed unless the phone is unlocked, and this code can be
170         // accessed from the emergency dialer.
171         if (PhoneGlobals.getInstance().getKeyguardManager().inKeyguardRestrictedInputMode()) {
172             return false;
173         }
174 
175         int len = input.length();
176         if ((len > 1) && (len < 5) && (input.endsWith("#"))) {
177             try {
178                 int index = Integer.parseInt(input.substring(0, len-1));
179                 Intent intent = new Intent(Intent.ACTION_PICK);
180 
181                 intent.setClassName("com.android.phone",
182                                     "com.android.phone.SimContacts");
183                 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
184                 intent.putExtra("index", index);
185                 PhoneGlobals.getInstance().startActivity(intent);
186 
187                 return true;
188             } catch (NumberFormatException ex) {}
189         }
190         return false;
191     }
192 
handlePinEntry(Context context, String input, Activity pukInputActivity)193     static private boolean handlePinEntry(Context context, String input,
194                                           Activity pukInputActivity) {
195         // TODO: The string constants here should be removed in favor
196         // of some call to a static the MmiCode class that determines
197         // if a dialstring is an MMI code.
198         if ((input.startsWith("**04") || input.startsWith("**05"))
199                 && input.endsWith("#")) {
200             PhoneGlobals app = PhoneGlobals.getInstance();
201             Phone phone = PhoneGlobals.getPhone();
202             boolean isMMIHandled = phone.handlePinMmi(input);
203 
204             // if the PUK code is recognized then indicate to the
205             // phone app that an attempt to unPUK the device was
206             // made with this activity.  The PUK code may still
207             // fail though, but we won't know until the MMI code
208             // returns a result.
209             if (isMMIHandled && input.startsWith("**05")) {
210                 app.setPukEntryActivity(pukInputActivity);
211             }
212             return isMMIHandled;
213         }
214         return false;
215     }
216 
handleIMEIDisplay(Context context, String input)217     static private boolean handleIMEIDisplay(Context context,
218                                              String input) {
219         if (input.equals(MMI_IMEI_DISPLAY)) {
220             showDeviceIdPanel(context);
221             return true;
222         }
223 
224         return false;
225     }
226 
showDeviceIdPanel(Context context)227     static private void showDeviceIdPanel(Context context) {
228         if (DBG) log("showDeviceIdPanel()...");
229 
230         Phone phone = PhoneGlobals.getPhone();
231         int labelId = TelephonyCapabilities.getDeviceIdLabel(phone);
232         String deviceId = phone.getDeviceId();
233 
234         AlertDialog alert = new AlertDialog.Builder(context)
235                 .setTitle(labelId)
236                 .setMessage(deviceId)
237                 .setPositiveButton(R.string.ok, null)
238                 .setCancelable(false)
239                 .create();
240         alert.getWindow().setType(WindowManager.LayoutParams.TYPE_PRIORITY_PHONE);
241         alert.show();
242     }
243 
handleRegulatoryInfoDisplay(Context context, String input)244     private static boolean handleRegulatoryInfoDisplay(Context context, String input) {
245         if (input.equals(MMI_REGULATORY_INFO_DISPLAY)) {
246             log("handleRegulatoryInfoDisplay() sending intent to settings app");
247             Intent showRegInfoIntent = new Intent(Settings.ACTION_SHOW_REGULATORY_INFO);
248             try {
249                 context.startActivity(showRegInfoIntent);
250             } catch (ActivityNotFoundException e) {
251                 Log.e(TAG, "startActivity() failed: " + e);
252             }
253             return true;
254         }
255         return false;
256     }
257 
log(String msg)258     private static void log(String msg) {
259         Log.d(TAG, "[SpecialCharSequenceMgr] " + msg);
260     }
261 }
262