blob: c865f148c2767da4ffe9034a168f57bedb12fc33 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -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.phone;
18
19import android.app.AlertDialog;
20import android.app.Dialog;
21import android.app.ProgressDialog;
Sailesh Nepalbfb68322013-11-07 14:07:41 -080022import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import android.content.Context;
24import android.content.DialogInterface;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070025import android.net.Uri;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070026import android.os.Handler;
27import android.os.Message;
SongFerngWang5ae4f6d2018-10-31 22:11:32 +080028import android.os.PersistableBundle;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070029import android.telecom.PhoneAccount;
Andrew Leed5165b02014-12-05 15:53:58 -080030import android.telecom.PhoneAccountHandle;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070031import android.telecom.VideoProfile;
SongFerngWang5ae4f6d2018-10-31 22:11:32 +080032import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070033import android.telephony.PhoneNumberUtils;
Tyler Gunn77ee9382019-10-31 13:08:23 -070034import android.telephony.Rlog;
Santos Cordonf69df962015-02-03 11:15:19 -080035import android.telephony.SubscriptionManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070036import android.text.TextUtils;
37import android.util.Log;
Andrew Leeb3ee4dc2015-01-23 17:35:00 -080038import android.view.ContextThemeWrapper;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070039import android.view.KeyEvent;
40import android.view.LayoutInflater;
41import android.view.View;
42import android.view.WindowManager;
43import android.widget.EditText;
44import android.widget.Toast;
45
46import com.android.internal.telephony.Call;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import com.android.internal.telephony.CallStateException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import com.android.internal.telephony.Connection;
Stuart Scottdcf40a92014-12-09 10:45:01 -080049import com.android.internal.telephony.IccCard;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070050import com.android.internal.telephony.MmiCode;
51import com.android.internal.telephony.Phone;
52import com.android.internal.telephony.PhoneConstants;
Stuart Scottdcf40a92014-12-09 10:45:01 -080053import com.android.internal.telephony.PhoneFactory;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import com.android.internal.telephony.TelephonyCapabilities;
SongFerngWang5ae4f6d2018-10-31 22:11:32 +080055import com.android.phone.settings.SuppServicesUiUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057import java.util.List;
58
59/**
60 * Misc utilities for the Phone app.
61 */
62public class PhoneUtils {
Brad Ebingerd3edc222016-03-23 19:44:38 -070063 public static final String EMERGENCY_ACCOUNT_HANDLE_ID = "E";
Santos Cordon7d4ddf62013-07-10 11:58:08 -070064 private static final String LOG_TAG = "PhoneUtils";
65 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
66
67 // Do not check in with VDBG = true, since that may write PII to the system log.
68 private static final boolean VDBG = false;
69
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070 // Return codes from placeCall()
fionaxuf7885142016-11-27 16:08:39 -080071 public static final int CALL_STATUS_DIALED = 0; // The number was successfully dialed
72 public static final int CALL_STATUS_DIALED_MMI = 1; // The specified number was an MMI code
73 public static final int CALL_STATUS_FAILED = 2; // The call failed
Santos Cordon7d4ddf62013-07-10 11:58:08 -070074
Santos Cordon7d4ddf62013-07-10 11:58:08 -070075 // USSD string length for MMI operations
76 static final int MIN_USSD_LEN = 1;
77 static final int MAX_USSD_LEN = 160;
78
Santos Cordon7d4ddf62013-07-10 11:58:08 -070079 /** Define for not a special CNAP string */
80 private static final int CNAP_SPECIAL_CASE_NO = -1;
81
Yorke Leee0f2b792014-10-15 12:30:44 -070082 /**
83 * Theme to use for dialogs displayed by utility methods in this class. This is needed
84 * because these dialogs are displayed using the application context, which does not resolve
85 * the dialog theme correctly.
86 */
Zheng Zhangd889c772017-11-02 15:49:37 -070087 private static final int THEME = com.android.internal.R.style.Theme_DeviceDefault_Dialog_Alert;
Yorke Leee0f2b792014-10-15 12:30:44 -070088
Etan Cohen37abbab2014-03-07 16:57:03 -080089 /** USSD information used to aggregate all USSD messages */
90 private static AlertDialog sUssdDialog = null;
91 private static StringBuilder sUssdMsg = new StringBuilder();
92
Tony Mak4c8f3c62015-12-07 02:30:37 +000093 private static final ComponentName PSTN_CONNECTION_SERVICE_COMPONENT =
94 new ComponentName("com.android.phone",
95 "com.android.services.telephony.TelephonyConnectionService");
96
Santos Cordon7d4ddf62013-07-10 11:58:08 -070097 /** This class is never instantiated. */
98 private PhoneUtils() {
99 }
100
101 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700102 * For a CDMA phone, advance the call state upon making a new
103 * outgoing call.
104 *
105 * <pre>
106 * IDLE -> SINGLE_ACTIVE
107 * or
108 * SINGLE_ACTIVE -> THRWAY_ACTIVE
109 * </pre>
110 * @param app The phone instance.
111 */
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700112 private static void updateCdmaCallStateOnNewOutgoingCall(PhoneGlobals app,
113 Connection connection) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700114 if (app.cdmaPhoneCallState.getCurrentCallState() ==
115 CdmaPhoneCallState.PhoneCallState.IDLE) {
116 // This is the first outgoing call. Set the Phone Call State to ACTIVE
117 app.cdmaPhoneCallState.setCurrentCallState(
118 CdmaPhoneCallState.PhoneCallState.SINGLE_ACTIVE);
119 } else {
120 // This is the second outgoing call. Set the Phone Call State to 3WAY
121 app.cdmaPhoneCallState.setCurrentCallState(
122 CdmaPhoneCallState.PhoneCallState.THRWAY_ACTIVE);
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700123
Santos Cordonda120f42014-08-06 04:44:34 -0700124 // TODO: Remove this code.
Sailesh Nepal23d9ed72014-07-03 09:40:26 -0700125 //app.getCallModeler().setCdmaOutgoing3WayCall(connection);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700126 }
127 }
128
129 /**
130 * Dial the number using the phone passed in.
131 *
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700132 * @param context To perform the CallerInfo query.
133 * @param phone the Phone object.
134 * @param number to be dialed as requested by the user. This is
135 * NOT the phone number to connect to. It is used only to build the
136 * call card and to update the call log. See above for restrictions.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700137 *
138 * @return either CALL_STATUS_DIALED or CALL_STATUS_FAILED
139 */
Hall Liu0c2ef0b2019-10-31 15:16:38 -0700140 public static int placeOtaspCall(Context context, Phone phone, String number) {
141 final Uri gatewayUri = null;
Santos Cordon69a69192013-08-22 14:25:42 -0700142
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700143 if (VDBG) {
144 log("placeCall()... number: '" + number + "'"
Hall Liu0c2ef0b2019-10-31 15:16:38 -0700145 + ", GW:'" + gatewayUri + "'");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700146 } else {
147 log("placeCall()... number: " + toLogSafePhoneNumber(number)
Hall Liu0c2ef0b2019-10-31 15:16:38 -0700148 + ", GW: " + (gatewayUri != null ? "non-null" : "null"));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700149 }
150 final PhoneGlobals app = PhoneGlobals.getInstance();
151
152 boolean useGateway = false;
Hall Liu0c2ef0b2019-10-31 15:16:38 -0700153 Uri contactRef = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700154
155 int status = CALL_STATUS_DIALED;
156 Connection connection;
157 String numberToDial;
Hall Liu0c2ef0b2019-10-31 15:16:38 -0700158 numberToDial = number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700159
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700160 try {
Tyler Gunnc6514352015-06-02 21:46:34 -0700161 connection = app.mCM.dial(phone, numberToDial, VideoProfile.STATE_AUDIO_ONLY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700162 } catch (CallStateException ex) {
163 // CallStateException means a new outgoing call is not currently
164 // possible: either no more call slots exist, or there's another
165 // call already in the process of dialing or ringing.
166 Log.w(LOG_TAG, "Exception from app.mCM.dial()", ex);
167 return CALL_STATUS_FAILED;
168
169 // Note that it's possible for CallManager.dial() to return
170 // null *without* throwing an exception; that indicates that
171 // we dialed an MMI (see below).
172 }
173
174 int phoneType = phone.getPhoneType();
175
176 // On GSM phones, null is returned for MMI codes
177 if (null == connection) {
Santos Cordonf77a3392014-07-21 19:48:10 -0700178 status = CALL_STATUS_FAILED;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700179 } else {
180 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700181 updateCdmaCallStateOnNewOutgoingCall(app, connection);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700182 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700183 }
184
185 return status;
186 }
187
188 /* package */ static String toLogSafePhoneNumber(String number) {
189 // For unknown number, log empty string.
190 if (number == null) {
191 return "";
192 }
193
194 if (VDBG) {
195 // When VDBG is true we emit PII.
196 return number;
197 }
198
199 // Do exactly same thing as Uri#toSafeString() does, which will enable us to compare
200 // sanitized phone numbers.
201 StringBuilder builder = new StringBuilder();
202 for (int i = 0; i < number.length(); i++) {
203 char c = number.charAt(i);
204 if (c == '-' || c == '@' || c == '.') {
205 builder.append(c);
206 } else {
207 builder.append('x');
208 }
209 }
210 return builder.toString();
211 }
212
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700213 /**
214 * Handle the MMIInitiate message and put up an alert that lets
215 * the user cancel the operation, if applicable.
216 *
217 * @param context context to get strings.
218 * @param mmiCode the MmiCode object being started.
219 * @param buttonCallbackMessage message to post when button is clicked.
220 * @param previousAlert a previous alert used in this activity.
221 * @return the dialog handle
222 */
223 static Dialog displayMMIInitiate(Context context,
224 MmiCode mmiCode,
225 Message buttonCallbackMessage,
226 Dialog previousAlert) {
Tyler Gunn77ee9382019-10-31 13:08:23 -0700227 log("displayMMIInitiate: " + Rlog.pii(LOG_TAG, mmiCode.toString()));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700228 if (previousAlert != null) {
229 previousAlert.dismiss();
230 }
231
232 // The UI paradigm we are using now requests that all dialogs have
233 // user interaction, and that any other messages to the user should
234 // be by way of Toasts.
235 //
236 // In adhering to this request, all MMI initiating "OK" dialogs
237 // (non-cancelable MMIs) that end up being closed when the MMI
238 // completes (thereby showing a completion dialog) are being
239 // replaced with Toasts.
240 //
241 // As a side effect, moving to Toasts for the non-cancelable MMIs
242 // also means that buttonCallbackMessage (which was tied into "OK")
243 // is no longer invokable for these dialogs. This is not a problem
244 // since the only callback messages we supported were for cancelable
245 // MMIs anyway.
246 //
247 // A cancelable MMI is really just a USSD request. The term
248 // "cancelable" here means that we can cancel the request when the
249 // system prompts us for a response, NOT while the network is
250 // processing the MMI request. Any request to cancel a USSD while
251 // the network is NOT ready for a response may be ignored.
252 //
253 // With this in mind, we replace the cancelable alert dialog with
254 // a progress dialog, displayed until we receive a request from
255 // the the network. For more information, please see the comments
256 // in the displayMMIComplete() method below.
257 //
258 // Anything that is NOT a USSD request is a normal MMI request,
259 // which will bring up a toast (desribed above).
260
261 boolean isCancelable = (mmiCode != null) && mmiCode.isCancelable();
262
263 if (!isCancelable) {
Tyler Gunn13fe2492017-05-22 14:14:20 -0700264 log("displayMMIInitiate: not a USSD code, displaying status toast.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700265 CharSequence text = context.getText(R.string.mmiStarted);
266 Toast.makeText(context, text, Toast.LENGTH_SHORT)
267 .show();
268 return null;
269 } else {
Tyler Gunn13fe2492017-05-22 14:14:20 -0700270 log("displayMMIInitiate: running USSD code, displaying intermediate progress.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700271
272 // create the indeterminate progress dialog and display it.
Alex Hills779841a2015-10-22 13:23:28 -0400273 ProgressDialog pd = new ProgressDialog(context, THEME);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700274 pd.setMessage(context.getText(R.string.ussdRunning));
275 pd.setCancelable(false);
276 pd.setIndeterminate(true);
277 pd.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
278
279 pd.show();
280
281 return pd;
282 }
283
284 }
285
286 /**
287 * Handle the MMIComplete message and fire off an intent to display
288 * the message.
289 *
290 * @param context context to get strings.
291 * @param mmiCode MMI result.
292 * @param previousAlert a previous alert used in this activity.
293 */
294 static void displayMMIComplete(final Phone phone, Context context, final MmiCode mmiCode,
295 Message dismissCallbackMessage,
296 AlertDialog previousAlert) {
297 final PhoneGlobals app = PhoneGlobals.getInstance();
298 CharSequence text;
299 int title = 0; // title for the progress dialog, if needed.
300 MmiCode.State state = mmiCode.getState();
301
Tyler Gunn13fe2492017-05-22 14:14:20 -0700302 log("displayMMIComplete: state=" + state);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700303
304 switch (state) {
305 case PENDING:
306 // USSD code asking for feedback from user.
307 text = mmiCode.getMessage();
Tyler Gunn13fe2492017-05-22 14:14:20 -0700308 log("displayMMIComplete: using text from PENDING MMI message: '" + text + "'");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700309 break;
310 case CANCELLED:
311 text = null;
312 break;
313 case COMPLETE:
SongFerngWang5ae4f6d2018-10-31 22:11:32 +0800314 PersistableBundle b = null;
315 if (SubscriptionManager.isValidSubscriptionId(phone.getSubId())) {
316 b = app.getCarrierConfigForSubId(
317 phone.getSubId());
318 } else {
319 b = app.getCarrierConfig();
320 }
321
322 if (b.getBoolean(CarrierConfigManager.KEY_USE_CALLER_ID_USSD_BOOL)) {
323 text = SuppServicesUiUtil.handleCallerIdUssdResponse(app, context, phone,
324 mmiCode);
325 if (mmiCode.getMessage() != null && !text.equals(mmiCode.getMessage())) {
326 break;
327 }
328 }
329
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700330 if (app.getPUKEntryActivity() != null) {
331 // if an attempt to unPUK the device was made, we specify
332 // the title and the message here.
333 title = com.android.internal.R.string.PinMmi;
334 text = context.getText(R.string.puk_unlocked);
335 break;
336 }
337 // All other conditions for the COMPLETE mmi state will cause
338 // the case to fall through to message logic in common with
339 // the FAILED case.
340
341 case FAILED:
342 text = mmiCode.getMessage();
Tyler Gunn13fe2492017-05-22 14:14:20 -0700343 log("displayMMIComplete (failed): using text from MMI message: '" + text + "'");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700344 break;
345 default:
346 throw new IllegalStateException("Unexpected MmiCode state: " + state);
347 }
348
349 if (previousAlert != null) {
350 previousAlert.dismiss();
351 }
352
353 // Check to see if a UI exists for the PUK activation. If it does
354 // exist, then it indicates that we're trying to unblock the PUK.
355 if ((app.getPUKEntryActivity() != null) && (state == MmiCode.State.COMPLETE)) {
356 if (DBG) log("displaying PUK unblocking progress dialog.");
357
358 // create the progress dialog, make sure the flags and type are
359 // set correctly.
Alex Hills779841a2015-10-22 13:23:28 -0400360 ProgressDialog pd = new ProgressDialog(app, THEME);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700361 pd.setTitle(title);
362 pd.setMessage(text);
363 pd.setCancelable(false);
364 pd.setIndeterminate(true);
365 pd.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
366 pd.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
367
368 // display the dialog
369 pd.show();
370
371 // indicate to the Phone app that the progress dialog has
372 // been assigned for the PUK unlock / SIM READY process.
373 app.setPukEntryProgressDialog(pd);
374
SongFerngWangdfa107a2018-10-29 20:31:58 +0800375 } else if ((app.getPUKEntryActivity() != null) && (state == MmiCode.State.FAILED)) {
Youming Yeef84f3e2019-04-15 14:05:02 -0700376 createUssdDialog(app, context, text, phone,
SongFerngWangdfa107a2018-10-29 20:31:58 +0800377 WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
378 // In case of failure to unlock, we'll need to reset the
379 // PUK unlock activity, so that the user may try again.
380 app.setPukEntryActivity(null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700381 } else {
382 // In case of failure to unlock, we'll need to reset the
383 // PUK unlock activity, so that the user may try again.
384 if (app.getPUKEntryActivity() != null) {
385 app.setPukEntryActivity(null);
386 }
387
388 // A USSD in a pending state means that it is still
389 // interacting with the user.
390 if (state != MmiCode.State.PENDING) {
Youming Yeef84f3e2019-04-15 14:05:02 -0700391 createUssdDialog(app, context, text, phone,
SongFerngWangdfa107a2018-10-29 20:31:58 +0800392 WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700393 } else {
Tyler Gunn13fe2492017-05-22 14:14:20 -0700394 log("displayMMIComplete: USSD code has requested user input. Constructing input "
395 + "dialog.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700396
397 // USSD MMI code that is interacting with the user. The
398 // basic set of steps is this:
399 // 1. User enters a USSD request
400 // 2. We recognize the request and displayMMIInitiate
401 // (above) creates a progress dialog.
402 // 3. Request returns and we get a PENDING or COMPLETE
403 // message.
404 // 4. These MMI messages are caught in the PhoneApp
405 // (onMMIComplete) and the InCallScreen
406 // (mHandler.handleMessage) which bring up this dialog
407 // and closes the original progress dialog,
408 // respectively.
409 // 5. If the message is anything other than PENDING,
410 // we are done, and the alert dialog (directly above)
411 // displays the outcome.
412 // 6. If the network is requesting more information from
413 // the user, the MMI will be in a PENDING state, and
414 // we display this dialog with the message.
415 // 7. User input, or cancel requests result in a return
416 // to step 1. Keep in mind that this is the only
417 // time that a USSD should be canceled.
418
419 // inflate the layout with the scrolling text area for the dialog.
Andrew Leeb3ee4dc2015-01-23 17:35:00 -0800420 ContextThemeWrapper contextThemeWrapper =
421 new ContextThemeWrapper(context, R.style.DialerAlertDialogTheme);
422 LayoutInflater inflater = (LayoutInflater) contextThemeWrapper.getSystemService(
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700423 Context.LAYOUT_INFLATER_SERVICE);
424 View dialogView = inflater.inflate(R.layout.dialog_ussd_response, null);
425
426 // get the input field.
427 final EditText inputText = (EditText) dialogView.findViewById(R.id.input_field);
428
429 // specify the dialog's click listener, with SEND and CANCEL logic.
430 final DialogInterface.OnClickListener mUSSDDialogListener =
431 new DialogInterface.OnClickListener() {
432 public void onClick(DialogInterface dialog, int whichButton) {
433 switch (whichButton) {
434 case DialogInterface.BUTTON_POSITIVE:
435 // As per spec 24.080, valid length of ussd string
436 // is 1 - 160. If length is out of the range then
437 // display toast message & Cancel MMI operation.
438 if (inputText.length() < MIN_USSD_LEN
439 || inputText.length() > MAX_USSD_LEN) {
440 Toast.makeText(app,
441 app.getResources().getString(R.string.enter_input,
442 MIN_USSD_LEN, MAX_USSD_LEN),
443 Toast.LENGTH_LONG).show();
444 if (mmiCode.isCancelable()) {
445 mmiCode.cancel();
446 }
447 } else {
448 phone.sendUssdResponse(inputText.getText().toString());
449 }
450 break;
451 case DialogInterface.BUTTON_NEGATIVE:
452 if (mmiCode.isCancelable()) {
453 mmiCode.cancel();
454 }
455 break;
456 }
457 }
458 };
459
460 // build the dialog
Andrew Leeb3ee4dc2015-01-23 17:35:00 -0800461 final AlertDialog newDialog = new AlertDialog.Builder(contextThemeWrapper)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700462 .setMessage(text)
463 .setView(dialogView)
464 .setPositiveButton(R.string.send_button, mUSSDDialogListener)
465 .setNegativeButton(R.string.cancel, mUSSDDialogListener)
466 .setCancelable(false)
467 .create();
468
469 // attach the key listener to the dialog's input field and make
470 // sure focus is set.
471 final View.OnKeyListener mUSSDDialogInputListener =
472 new View.OnKeyListener() {
473 public boolean onKey(View v, int keyCode, KeyEvent event) {
474 switch (keyCode) {
475 case KeyEvent.KEYCODE_CALL:
476 case KeyEvent.KEYCODE_ENTER:
477 if(event.getAction() == KeyEvent.ACTION_DOWN) {
478 phone.sendUssdResponse(inputText.getText().toString());
479 newDialog.dismiss();
480 }
481 return true;
482 }
483 return false;
484 }
485 };
486 inputText.setOnKeyListener(mUSSDDialogInputListener);
487 inputText.requestFocus();
488
489 // set the window properties of the dialog
490 newDialog.getWindow().setType(
491 WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
492 newDialog.getWindow().addFlags(
493 WindowManager.LayoutParams.FLAG_DIM_BEHIND);
494
495 // now show the dialog!
496 newDialog.show();
Andrew Leeb3ee4dc2015-01-23 17:35:00 -0800497
498 newDialog.getButton(DialogInterface.BUTTON_POSITIVE)
499 .setTextColor(context.getResources().getColor(R.color.dialer_theme_color));
500 newDialog.getButton(DialogInterface.BUTTON_NEGATIVE)
501 .setTextColor(context.getResources().getColor(R.color.dialer_theme_color));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700502 }
503 }
504 }
505
SongFerngWang5ae4f6d2018-10-31 22:11:32 +0800506 /**
507 * It displays the message dialog for user about the mmi code result message.
508 *
509 * @param app This is {@link PhoneGlobals}
510 * @param context Context to get strings.
511 * @param text This is message's result.
Youming Yeef84f3e2019-04-15 14:05:02 -0700512 * @param phone This is phone to create sssd dialog.
SongFerngWang5ae4f6d2018-10-31 22:11:32 +0800513 * @param windowType The new window type. {@link WindowManager.LayoutParams}.
514 */
515 public static void createUssdDialog(PhoneGlobals app, Context context, CharSequence text,
Youming Yeef84f3e2019-04-15 14:05:02 -0700516 Phone phone, int windowType) {
SongFerngWangdfa107a2018-10-29 20:31:58 +0800517 log("displayMMIComplete: MMI code has finished running.");
518
519 log("displayMMIComplete: Extended NW displayMMIInitiate (" + text + ")");
520 if (text == null || text.length() == 0) {
521 return;
522 }
523
524 // displaying system alert dialog on the screen instead of
525 // using another activity to display the message. This
526 // places the message at the forefront of the UI.
527
528 if (sUssdDialog == null) {
529 sUssdDialog = new AlertDialog.Builder(context, THEME)
530 .setPositiveButton(R.string.ok, null)
531 .setCancelable(true)
532 .setOnDismissListener(new DialogInterface.OnDismissListener() {
533 @Override
534 public void onDismiss(DialogInterface dialog) {
535 sUssdMsg.setLength(0);
536 }
537 })
538 .create();
539
540 sUssdDialog.getWindow().setType(windowType);
541 sUssdDialog.getWindow().addFlags(
542 WindowManager.LayoutParams.FLAG_DIM_BEHIND);
543 }
544 if (sUssdMsg.length() != 0) {
545 sUssdMsg
546 .insert(0, "\n")
547 .insert(0, app.getResources().getString(R.string.ussd_dialog_sep))
548 .insert(0, "\n");
549 }
Youming Yeef84f3e2019-04-15 14:05:02 -0700550 if (phone != null && phone.getCarrierName() != null) {
551 sUssdDialog.setTitle(app.getResources().getString(R.string.carrier_mmi_msg_title,
552 phone.getCarrierName()));
553 } else {
554 sUssdDialog
555 .setTitle(app.getResources().getString(R.string.default_carrier_mmi_msg_title));
556 }
SongFerngWangdfa107a2018-10-29 20:31:58 +0800557 sUssdMsg.insert(0, text);
558 sUssdDialog.setMessage(sUssdMsg.toString());
559 sUssdDialog.show();
560 }
561
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700562 /**
563 * Cancels the current pending MMI operation, if applicable.
564 * @return true if we canceled an MMI operation, or false
565 * if the current pending MMI wasn't cancelable
566 * or if there was no current pending MMI at all.
567 *
568 * @see displayMMIInitiate
569 */
570 static boolean cancelMmiCode(Phone phone) {
571 List<? extends MmiCode> pendingMmis = phone.getPendingMmiCodes();
572 int count = pendingMmis.size();
573 if (DBG) log("cancelMmiCode: num pending MMIs = " + count);
574
575 boolean canceled = false;
576 if (count > 0) {
577 // assume that we only have one pending MMI operation active at a time.
578 // I don't think it's possible to enter multiple MMI codes concurrently
579 // in the phone UI, because during the MMI operation, an Alert panel
580 // is displayed, which prevents more MMI code from being entered.
581 MmiCode mmiCode = pendingMmis.get(0);
582 if (mmiCode.isCancelable()) {
583 mmiCode.cancel();
584 canceled = true;
585 }
586 }
587 return canceled;
588 }
589
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700590
591 //
592 // Misc UI policy helper functions
593 //
594
595 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700596 * Check if a phone number can be route through a 3rd party
597 * gateway. The number must be a global phone number in numerical
598 * form (1-800-666-SEXY won't work).
599 *
600 * MMI codes and the like cannot be used as a dial number for the
601 * gateway either.
602 *
603 * @param number To be dialed via a 3rd party gateway.
604 * @return true If the number can be routed through the 3rd party network.
605 */
Santos Cordon69a69192013-08-22 14:25:42 -0700606 private static boolean isRoutableViaGateway(String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700607 if (TextUtils.isEmpty(number)) {
608 return false;
609 }
610 number = PhoneNumberUtils.stripSeparators(number);
611 if (!number.equals(PhoneNumberUtils.convertKeypadLettersToDigits(number))) {
612 return false;
613 }
614 number = PhoneNumberUtils.extractNetworkPortion(number);
615 return PhoneNumberUtils.isGlobalPhoneNumber(number);
616 }
617
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700618 /**
619 * Returns whether the phone is in ECM ("Emergency Callback Mode") or not.
620 */
621 /* package */ static boolean isPhoneInEcm(Phone phone) {
622 if ((phone != null) && TelephonyCapabilities.supportsEcm(phone)) {
Shuo Qiandd210312017-04-12 22:11:33 +0000623 return phone.isInEcm();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700624 }
625 return false;
626 }
627
628 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700629 * Returns true when the given call is in INCOMING state and there's no foreground phone call,
630 * meaning the call is the first real incoming call the phone is having.
631 */
632 public static boolean isRealIncomingCall(Call.State state) {
633 return (state == Call.State.INCOMING && !PhoneGlobals.getInstance().mCM.hasActiveFgCall());
634 }
635
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700636 //
637 // General phone and call state debugging/testing code
638 //
639
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700640 private static void log(String msg) {
641 Log.d(LOG_TAG, msg);
642 }
643
Nancy Chene53a0642015-03-12 11:20:12 -0700644 public static PhoneAccountHandle makePstnPhoneAccountHandle(String id) {
645 return makePstnPhoneAccountHandleWithPrefix(id, "", false);
646 }
647
Nancy Chen32031f42015-03-04 21:00:07 -0800648 public static PhoneAccountHandle makePstnPhoneAccountHandle(int phoneId) {
649 return makePstnPhoneAccountHandle(PhoneFactory.getPhone(phoneId));
650 }
651
Andrew Leed5165b02014-12-05 15:53:58 -0800652 public static PhoneAccountHandle makePstnPhoneAccountHandle(Phone phone) {
653 return makePstnPhoneAccountHandleWithPrefix(phone, "", false);
654 }
655
656 public static PhoneAccountHandle makePstnPhoneAccountHandleWithPrefix(
657 Phone phone, String prefix, boolean isEmergency) {
Andrew Leed5165b02014-12-05 15:53:58 -0800658 // TODO: Should use some sort of special hidden flag to decorate this account as
659 // an emergency-only account
Brad Ebingerd3edc222016-03-23 19:44:38 -0700660 String id = isEmergency ? EMERGENCY_ACCOUNT_HANDLE_ID : prefix +
Brad Ebinger4fe48112016-04-13 11:21:53 -0700661 String.valueOf(phone.getFullIccSerialNumber());
Nancy Chene53a0642015-03-12 11:20:12 -0700662 return makePstnPhoneAccountHandleWithPrefix(id, prefix, isEmergency);
663 }
664
665 public static PhoneAccountHandle makePstnPhoneAccountHandleWithPrefix(
666 String id, String prefix, boolean isEmergency) {
667 ComponentName pstnConnectionServiceName = getPstnConnectionServiceName();
Andrew Leed5165b02014-12-05 15:53:58 -0800668 return new PhoneAccountHandle(pstnConnectionServiceName, id);
669 }
Stuart Scottdcf40a92014-12-09 10:45:01 -0800670
Santos Cordonf69df962015-02-03 11:15:19 -0800671 public static int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
Santos Cordon21e78ac2015-02-06 05:19:29 -0800672 if (phoneAccount != null
673 && phoneAccount.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
674 return getSubIdForPhoneAccountHandle(phoneAccount.getAccountHandle());
675 }
676 return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
677 }
678
679 public static int getSubIdForPhoneAccountHandle(PhoneAccountHandle handle) {
Nancy Chen31f9ba12016-01-06 11:42:12 -0800680 Phone phone = getPhoneForPhoneAccountHandle(handle);
681 if (phone != null) {
682 return phone.getSubId();
Santos Cordonf69df962015-02-03 11:15:19 -0800683 }
684 return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
685 }
686
Ta-wei Yen845f6992016-06-16 12:53:46 -0700687 public static Phone getPhoneForPhoneAccountHandle(PhoneAccountHandle handle) {
Nancy Chen31f9ba12016-01-06 11:42:12 -0800688 if (handle != null && handle.getComponentName().equals(getPstnConnectionServiceName())) {
689 return getPhoneFromIccId(handle.getId());
690 }
691 return null;
692 }
693
Nancy Chene53a0642015-03-12 11:20:12 -0700694 /**
695 * Determine if a given phone account corresponds to an active SIM
696 *
697 * @param sm An instance of the subscription manager so it is not recreated for each calling of
698 * this method.
699 * @param handle The handle for the phone account to check
700 * @return {@code true} If there is an active SIM for this phone account,
701 * {@code false} otherwise.
702 */
703 public static boolean isPhoneAccountActive(SubscriptionManager sm, PhoneAccountHandle handle) {
Shuo Qianb87f0dc2020-01-06 15:59:12 -0800704 return sm.getActiveSubscriptionInfoForIcc(handle.getId()) != null;
Nancy Chene53a0642015-03-12 11:20:12 -0700705 }
706
Santos Cordonf69df962015-02-03 11:15:19 -0800707 private static ComponentName getPstnConnectionServiceName() {
Tony Mak4c8f3c62015-12-07 02:30:37 +0000708 return PSTN_CONNECTION_SERVICE_COMPONENT;
Santos Cordonf69df962015-02-03 11:15:19 -0800709 }
710
Santos Cordon80855d52015-02-10 04:33:36 -0800711 private static Phone getPhoneFromIccId(String iccId) {
712 if (!TextUtils.isEmpty(iccId)) {
713 for (Phone phone : PhoneFactory.getPhones()) {
Brad Ebinger4fe48112016-04-13 11:21:53 -0700714 String phoneIccId = phone.getFullIccSerialNumber();
Santos Cordon80855d52015-02-10 04:33:36 -0800715 if (iccId.equals(phoneIccId)) {
716 return phone;
717 }
718 }
719 }
Santos Cordon80855d52015-02-10 04:33:36 -0800720 return null;
721 }
722
Stuart Scottdcf40a92014-12-09 10:45:01 -0800723 /**
724 * Register ICC status for all phones.
725 */
726 static final void registerIccStatus(Handler handler, int event) {
727 for (Phone phone : PhoneFactory.getPhones()) {
728 IccCard sim = phone.getIccCard();
729 if (sim != null) {
730 if (VDBG) Log.v(LOG_TAG, "register for ICC status, phone " + phone.getPhoneId());
731 sim.registerForNetworkLocked(handler, event, phone);
732 }
733 }
734 }
735
736 /**
Amit Mahajanf5d92c82018-11-02 17:44:40 -0700737 * Register ICC status for all phones.
738 */
739 static final void registerIccStatus(Handler handler, int event, int phoneId) {
740 Phone[] phones = PhoneFactory.getPhones();
741 IccCard sim = phones[phoneId].getIccCard();
742 if (sim != null) {
743 if (VDBG) {
744 Log.v(LOG_TAG, "register for ICC status, phone " + phones[phoneId].getPhoneId());
745 }
746 sim.registerForNetworkLocked(handler, event, phones[phoneId]);
747 }
748 }
749
750 /**
751 * Unregister ICC status for a specific phone.
752 */
753 static final void unregisterIccStatus(Handler handler, int phoneId) {
754 Phone[] phones = PhoneFactory.getPhones();
755 IccCard sim = phones[phoneId].getIccCard();
756 if (sim != null) {
757 if (VDBG) {
758 Log.v(LOG_TAG, "unregister for ICC status, phone " + phones[phoneId].getPhoneId());
759 }
760 sim.unregisterForNetworkLocked(handler);
761 }
762 }
763
764 /**
Stuart Scottdcf40a92014-12-09 10:45:01 -0800765 * Set the radio power on/off state for all phones.
766 *
767 * @param enabled true means on, false means off.
768 */
769 static final void setRadioPower(boolean enabled) {
770 for (Phone phone : PhoneFactory.getPhones()) {
771 phone.setRadioPower(enabled);
772 }
773 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700774}