blob: ef49ca36d909ee2c65b6f185eeb0aa53b918a1a8 [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;
Santos Cordonf69df962015-02-03 11:15:19 -080034import android.telephony.SubscriptionManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070035import android.text.TextUtils;
36import android.util.Log;
Andrew Leeb3ee4dc2015-01-23 17:35:00 -080037import android.view.ContextThemeWrapper;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.view.KeyEvent;
39import android.view.LayoutInflater;
40import android.view.View;
41import android.view.WindowManager;
42import android.widget.EditText;
43import android.widget.Toast;
44
45import com.android.internal.telephony.Call;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070046import com.android.internal.telephony.CallStateException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import com.android.internal.telephony.Connection;
Stuart Scottdcf40a92014-12-09 10:45:01 -080048import com.android.internal.telephony.IccCard;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070049import com.android.internal.telephony.MmiCode;
50import com.android.internal.telephony.Phone;
51import com.android.internal.telephony.PhoneConstants;
Stuart Scottdcf40a92014-12-09 10:45:01 -080052import com.android.internal.telephony.PhoneFactory;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053import com.android.internal.telephony.TelephonyCapabilities;
Daniel Bright9a8791d2020-02-11 18:19:36 -080054import com.android.internal.telephony.util.TelephonyResourceUtils;
SongFerngWang5ae4f6d2018-10-31 22:11:32 +080055import com.android.phone.settings.SuppServicesUiUtil;
Daniel Bright9a8791d2020-02-11 18:19:36 -080056import com.android.telephony.Rlog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057
Santos Cordon7d4ddf62013-07-10 11:58:08 -070058import java.util.List;
59
60/**
61 * Misc utilities for the Phone app.
62 */
63public class PhoneUtils {
Brad Ebingerd3edc222016-03-23 19:44:38 -070064 public static final String EMERGENCY_ACCOUNT_HANDLE_ID = "E";
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065 private static final String LOG_TAG = "PhoneUtils";
66 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
67
68 // Do not check in with VDBG = true, since that may write PII to the system log.
69 private static final boolean VDBG = false;
70
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071 // Return codes from placeCall()
fionaxuf7885142016-11-27 16:08:39 -080072 public static final int CALL_STATUS_DIALED = 0; // The number was successfully dialed
73 public static final int CALL_STATUS_DIALED_MMI = 1; // The specified number was an MMI code
74 public static final int CALL_STATUS_FAILED = 2; // The call failed
Santos Cordon7d4ddf62013-07-10 11:58:08 -070075
Santos Cordon7d4ddf62013-07-10 11:58:08 -070076 // USSD string length for MMI operations
77 static final int MIN_USSD_LEN = 1;
78 static final int MAX_USSD_LEN = 160;
79
Santos Cordon7d4ddf62013-07-10 11:58:08 -070080 /** Define for not a special CNAP string */
81 private static final int CNAP_SPECIAL_CASE_NO = -1;
82
Yorke Leee0f2b792014-10-15 12:30:44 -070083 /**
84 * Theme to use for dialogs displayed by utility methods in this class. This is needed
85 * because these dialogs are displayed using the application context, which does not resolve
86 * the dialog theme correctly.
87 */
Zheng Zhangd889c772017-11-02 15:49:37 -070088 private static final int THEME = com.android.internal.R.style.Theme_DeviceDefault_Dialog_Alert;
Yorke Leee0f2b792014-10-15 12:30:44 -070089
Etan Cohen37abbab2014-03-07 16:57:03 -080090 /** USSD information used to aggregate all USSD messages */
91 private static AlertDialog sUssdDialog = null;
92 private static StringBuilder sUssdMsg = new StringBuilder();
93
Tony Mak4c8f3c62015-12-07 02:30:37 +000094 private static final ComponentName PSTN_CONNECTION_SERVICE_COMPONENT =
95 new ComponentName("com.android.phone",
96 "com.android.services.telephony.TelephonyConnectionService");
97
Santos Cordon7d4ddf62013-07-10 11:58:08 -070098 /** This class is never instantiated. */
99 private PhoneUtils() {
100 }
101
102 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700103 * For a CDMA phone, advance the call state upon making a new
104 * outgoing call.
105 *
106 * <pre>
107 * IDLE -> SINGLE_ACTIVE
108 * or
109 * SINGLE_ACTIVE -> THRWAY_ACTIVE
110 * </pre>
111 * @param app The phone instance.
112 */
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700113 private static void updateCdmaCallStateOnNewOutgoingCall(PhoneGlobals app,
114 Connection connection) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700115 if (app.cdmaPhoneCallState.getCurrentCallState() ==
116 CdmaPhoneCallState.PhoneCallState.IDLE) {
117 // This is the first outgoing call. Set the Phone Call State to ACTIVE
118 app.cdmaPhoneCallState.setCurrentCallState(
119 CdmaPhoneCallState.PhoneCallState.SINGLE_ACTIVE);
120 } else {
121 // This is the second outgoing call. Set the Phone Call State to 3WAY
122 app.cdmaPhoneCallState.setCurrentCallState(
123 CdmaPhoneCallState.PhoneCallState.THRWAY_ACTIVE);
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700124
Santos Cordonda120f42014-08-06 04:44:34 -0700125 // TODO: Remove this code.
Sailesh Nepal23d9ed72014-07-03 09:40:26 -0700126 //app.getCallModeler().setCdmaOutgoing3WayCall(connection);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700127 }
128 }
129
130 /**
131 * Dial the number using the phone passed in.
132 *
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700133 * @param context To perform the CallerInfo query.
134 * @param phone the Phone object.
135 * @param number to be dialed as requested by the user. This is
136 * NOT the phone number to connect to. It is used only to build the
137 * call card and to update the call log. See above for restrictions.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700138 *
139 * @return either CALL_STATUS_DIALED or CALL_STATUS_FAILED
140 */
Hall Liu0587e3f2019-10-31 15:16:38 -0700141 public static int placeOtaspCall(Context context, Phone phone, String number) {
142 final Uri gatewayUri = null;
Santos Cordon69a69192013-08-22 14:25:42 -0700143
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700144 if (VDBG) {
145 log("placeCall()... number: '" + number + "'"
Hall Liu0587e3f2019-10-31 15:16:38 -0700146 + ", GW:'" + gatewayUri + "'");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700147 } else {
148 log("placeCall()... number: " + toLogSafePhoneNumber(number)
Hall Liu0587e3f2019-10-31 15:16:38 -0700149 + ", GW: " + (gatewayUri != null ? "non-null" : "null"));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700150 }
151 final PhoneGlobals app = PhoneGlobals.getInstance();
152
153 boolean useGateway = false;
Hall Liu0587e3f2019-10-31 15:16:38 -0700154 Uri contactRef = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700155
156 int status = CALL_STATUS_DIALED;
157 Connection connection;
158 String numberToDial;
Hall Liu0587e3f2019-10-31 15:16:38 -0700159 numberToDial = number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700160
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700161 try {
Tyler Gunnc6514352015-06-02 21:46:34 -0700162 connection = app.mCM.dial(phone, numberToDial, VideoProfile.STATE_AUDIO_ONLY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700163 } catch (CallStateException ex) {
164 // CallStateException means a new outgoing call is not currently
165 // possible: either no more call slots exist, or there's another
166 // call already in the process of dialing or ringing.
167 Log.w(LOG_TAG, "Exception from app.mCM.dial()", ex);
168 return CALL_STATUS_FAILED;
169
170 // Note that it's possible for CallManager.dial() to return
171 // null *without* throwing an exception; that indicates that
172 // we dialed an MMI (see below).
173 }
174
175 int phoneType = phone.getPhoneType();
176
177 // On GSM phones, null is returned for MMI codes
178 if (null == connection) {
Santos Cordonf77a3392014-07-21 19:48:10 -0700179 status = CALL_STATUS_FAILED;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700180 } else {
181 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700182 updateCdmaCallStateOnNewOutgoingCall(app, connection);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700183 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700184 }
185
186 return status;
187 }
188
189 /* package */ static String toLogSafePhoneNumber(String number) {
190 // For unknown number, log empty string.
191 if (number == null) {
192 return "";
193 }
194
195 if (VDBG) {
196 // When VDBG is true we emit PII.
197 return number;
198 }
199
200 // Do exactly same thing as Uri#toSafeString() does, which will enable us to compare
201 // sanitized phone numbers.
202 StringBuilder builder = new StringBuilder();
203 for (int i = 0; i < number.length(); i++) {
204 char c = number.charAt(i);
205 if (c == '-' || c == '@' || c == '.') {
206 builder.append(c);
207 } else {
208 builder.append('x');
209 }
210 }
211 return builder.toString();
212 }
213
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700214 /**
215 * Handle the MMIInitiate message and put up an alert that lets
216 * the user cancel the operation, if applicable.
217 *
218 * @param context context to get strings.
219 * @param mmiCode the MmiCode object being started.
220 * @param buttonCallbackMessage message to post when button is clicked.
221 * @param previousAlert a previous alert used in this activity.
222 * @return the dialog handle
223 */
224 static Dialog displayMMIInitiate(Context context,
225 MmiCode mmiCode,
226 Message buttonCallbackMessage,
227 Dialog previousAlert) {
Tyler Gunn5ddfdc92019-10-31 13:08:23 -0700228 log("displayMMIInitiate: " + Rlog.pii(LOG_TAG, mmiCode.toString()));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700229 if (previousAlert != null) {
230 previousAlert.dismiss();
231 }
232
233 // The UI paradigm we are using now requests that all dialogs have
234 // user interaction, and that any other messages to the user should
235 // be by way of Toasts.
236 //
237 // In adhering to this request, all MMI initiating "OK" dialogs
238 // (non-cancelable MMIs) that end up being closed when the MMI
239 // completes (thereby showing a completion dialog) are being
240 // replaced with Toasts.
241 //
242 // As a side effect, moving to Toasts for the non-cancelable MMIs
243 // also means that buttonCallbackMessage (which was tied into "OK")
244 // is no longer invokable for these dialogs. This is not a problem
245 // since the only callback messages we supported were for cancelable
246 // MMIs anyway.
247 //
248 // A cancelable MMI is really just a USSD request. The term
249 // "cancelable" here means that we can cancel the request when the
250 // system prompts us for a response, NOT while the network is
251 // processing the MMI request. Any request to cancel a USSD while
252 // the network is NOT ready for a response may be ignored.
253 //
254 // With this in mind, we replace the cancelable alert dialog with
255 // a progress dialog, displayed until we receive a request from
256 // the the network. For more information, please see the comments
257 // in the displayMMIComplete() method below.
258 //
259 // Anything that is NOT a USSD request is a normal MMI request,
260 // which will bring up a toast (desribed above).
261
262 boolean isCancelable = (mmiCode != null) && mmiCode.isCancelable();
263
264 if (!isCancelable) {
Tyler Gunn13fe2492017-05-22 14:14:20 -0700265 log("displayMMIInitiate: not a USSD code, displaying status toast.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700266 CharSequence text = context.getText(R.string.mmiStarted);
267 Toast.makeText(context, text, Toast.LENGTH_SHORT)
268 .show();
269 return null;
270 } else {
Tyler Gunn13fe2492017-05-22 14:14:20 -0700271 log("displayMMIInitiate: running USSD code, displaying intermediate progress.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700272
273 // create the indeterminate progress dialog and display it.
Alex Hills779841a2015-10-22 13:23:28 -0400274 ProgressDialog pd = new ProgressDialog(context, THEME);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700275 pd.setMessage(context.getText(R.string.ussdRunning));
276 pd.setCancelable(false);
277 pd.setIndeterminate(true);
278 pd.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
279
280 pd.show();
281
282 return pd;
283 }
284
285 }
286
287 /**
288 * Handle the MMIComplete message and fire off an intent to display
289 * the message.
290 *
291 * @param context context to get strings.
292 * @param mmiCode MMI result.
293 * @param previousAlert a previous alert used in this activity.
294 */
295 static void displayMMIComplete(final Phone phone, Context context, final MmiCode mmiCode,
296 Message dismissCallbackMessage,
297 AlertDialog previousAlert) {
298 final PhoneGlobals app = PhoneGlobals.getInstance();
299 CharSequence text;
Daniel Bright9a8791d2020-02-11 18:19:36 -0800300 CharSequence title = ""; // title for the progress dialog, if needed.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700301 MmiCode.State state = mmiCode.getState();
302
Tyler Gunn13fe2492017-05-22 14:14:20 -0700303 log("displayMMIComplete: state=" + state);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700304
305 switch (state) {
306 case PENDING:
307 // USSD code asking for feedback from user.
308 text = mmiCode.getMessage();
Tyler Gunn13fe2492017-05-22 14:14:20 -0700309 log("displayMMIComplete: using text from PENDING MMI message: '" + text + "'");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700310 break;
311 case CANCELLED:
312 text = null;
313 break;
314 case COMPLETE:
SongFerngWang5ae4f6d2018-10-31 22:11:32 +0800315 PersistableBundle b = null;
316 if (SubscriptionManager.isValidSubscriptionId(phone.getSubId())) {
317 b = app.getCarrierConfigForSubId(
318 phone.getSubId());
319 } else {
320 b = app.getCarrierConfig();
321 }
322
323 if (b.getBoolean(CarrierConfigManager.KEY_USE_CALLER_ID_USSD_BOOL)) {
324 text = SuppServicesUiUtil.handleCallerIdUssdResponse(app, context, phone,
325 mmiCode);
326 if (mmiCode.getMessage() != null && !text.equals(mmiCode.getMessage())) {
327 break;
328 }
329 }
330
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700331 if (app.getPUKEntryActivity() != null) {
332 // if an attempt to unPUK the device was made, we specify
333 // the title and the message here.
Daniel Bright9a8791d2020-02-11 18:19:36 -0800334 title = TelephonyResourceUtils.getTelephonyResources(context)
335 .getText(com.android.telephony.resources.R.string.PinMmi);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700336 text = context.getText(R.string.puk_unlocked);
337 break;
338 }
339 // All other conditions for the COMPLETE mmi state will cause
340 // the case to fall through to message logic in common with
341 // the FAILED case.
342
343 case FAILED:
344 text = mmiCode.getMessage();
Tyler Gunn13fe2492017-05-22 14:14:20 -0700345 log("displayMMIComplete (failed): using text from MMI message: '" + text + "'");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700346 break;
347 default:
348 throw new IllegalStateException("Unexpected MmiCode state: " + state);
349 }
350
351 if (previousAlert != null) {
352 previousAlert.dismiss();
353 }
354
355 // Check to see if a UI exists for the PUK activation. If it does
356 // exist, then it indicates that we're trying to unblock the PUK.
357 if ((app.getPUKEntryActivity() != null) && (state == MmiCode.State.COMPLETE)) {
358 if (DBG) log("displaying PUK unblocking progress dialog.");
359
360 // create the progress dialog, make sure the flags and type are
361 // set correctly.
Alex Hills779841a2015-10-22 13:23:28 -0400362 ProgressDialog pd = new ProgressDialog(app, THEME);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700363 pd.setTitle(title);
364 pd.setMessage(text);
365 pd.setCancelable(false);
366 pd.setIndeterminate(true);
367 pd.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
368 pd.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
369
370 // display the dialog
371 pd.show();
372
373 // indicate to the Phone app that the progress dialog has
374 // been assigned for the PUK unlock / SIM READY process.
375 app.setPukEntryProgressDialog(pd);
376
SongFerngWangdfa107a2018-10-29 20:31:58 +0800377 } else if ((app.getPUKEntryActivity() != null) && (state == MmiCode.State.FAILED)) {
Youming Yeeb117bd2019-04-15 14:05:02 -0700378 createUssdDialog(app, context, text, phone,
SongFerngWangdfa107a2018-10-29 20:31:58 +0800379 WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
380 // In case of failure to unlock, we'll need to reset the
381 // PUK unlock activity, so that the user may try again.
382 app.setPukEntryActivity(null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700383 } else {
384 // In case of failure to unlock, we'll need to reset the
385 // PUK unlock activity, so that the user may try again.
386 if (app.getPUKEntryActivity() != null) {
387 app.setPukEntryActivity(null);
388 }
389
390 // A USSD in a pending state means that it is still
391 // interacting with the user.
392 if (state != MmiCode.State.PENDING) {
Youming Yeeb117bd2019-04-15 14:05:02 -0700393 createUssdDialog(app, context, text, phone,
SongFerngWangdfa107a2018-10-29 20:31:58 +0800394 WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700395 } else {
Tyler Gunn13fe2492017-05-22 14:14:20 -0700396 log("displayMMIComplete: USSD code has requested user input. Constructing input "
397 + "dialog.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700398
399 // USSD MMI code that is interacting with the user. The
400 // basic set of steps is this:
401 // 1. User enters a USSD request
402 // 2. We recognize the request and displayMMIInitiate
403 // (above) creates a progress dialog.
404 // 3. Request returns and we get a PENDING or COMPLETE
405 // message.
406 // 4. These MMI messages are caught in the PhoneApp
407 // (onMMIComplete) and the InCallScreen
408 // (mHandler.handleMessage) which bring up this dialog
409 // and closes the original progress dialog,
410 // respectively.
411 // 5. If the message is anything other than PENDING,
412 // we are done, and the alert dialog (directly above)
413 // displays the outcome.
414 // 6. If the network is requesting more information from
415 // the user, the MMI will be in a PENDING state, and
416 // we display this dialog with the message.
417 // 7. User input, or cancel requests result in a return
418 // to step 1. Keep in mind that this is the only
419 // time that a USSD should be canceled.
420
421 // inflate the layout with the scrolling text area for the dialog.
Andrew Leeb3ee4dc2015-01-23 17:35:00 -0800422 ContextThemeWrapper contextThemeWrapper =
423 new ContextThemeWrapper(context, R.style.DialerAlertDialogTheme);
424 LayoutInflater inflater = (LayoutInflater) contextThemeWrapper.getSystemService(
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700425 Context.LAYOUT_INFLATER_SERVICE);
426 View dialogView = inflater.inflate(R.layout.dialog_ussd_response, null);
427
428 // get the input field.
429 final EditText inputText = (EditText) dialogView.findViewById(R.id.input_field);
430
431 // specify the dialog's click listener, with SEND and CANCEL logic.
432 final DialogInterface.OnClickListener mUSSDDialogListener =
433 new DialogInterface.OnClickListener() {
434 public void onClick(DialogInterface dialog, int whichButton) {
435 switch (whichButton) {
436 case DialogInterface.BUTTON_POSITIVE:
437 // As per spec 24.080, valid length of ussd string
438 // is 1 - 160. If length is out of the range then
439 // display toast message & Cancel MMI operation.
440 if (inputText.length() < MIN_USSD_LEN
441 || inputText.length() > MAX_USSD_LEN) {
442 Toast.makeText(app,
443 app.getResources().getString(R.string.enter_input,
444 MIN_USSD_LEN, MAX_USSD_LEN),
445 Toast.LENGTH_LONG).show();
446 if (mmiCode.isCancelable()) {
447 mmiCode.cancel();
448 }
449 } else {
450 phone.sendUssdResponse(inputText.getText().toString());
451 }
452 break;
453 case DialogInterface.BUTTON_NEGATIVE:
454 if (mmiCode.isCancelable()) {
455 mmiCode.cancel();
456 }
457 break;
458 }
459 }
460 };
461
462 // build the dialog
Andrew Leeb3ee4dc2015-01-23 17:35:00 -0800463 final AlertDialog newDialog = new AlertDialog.Builder(contextThemeWrapper)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700464 .setMessage(text)
465 .setView(dialogView)
466 .setPositiveButton(R.string.send_button, mUSSDDialogListener)
467 .setNegativeButton(R.string.cancel, mUSSDDialogListener)
468 .setCancelable(false)
469 .create();
470
471 // attach the key listener to the dialog's input field and make
472 // sure focus is set.
473 final View.OnKeyListener mUSSDDialogInputListener =
474 new View.OnKeyListener() {
475 public boolean onKey(View v, int keyCode, KeyEvent event) {
476 switch (keyCode) {
477 case KeyEvent.KEYCODE_CALL:
478 case KeyEvent.KEYCODE_ENTER:
479 if(event.getAction() == KeyEvent.ACTION_DOWN) {
480 phone.sendUssdResponse(inputText.getText().toString());
481 newDialog.dismiss();
482 }
483 return true;
484 }
485 return false;
486 }
487 };
488 inputText.setOnKeyListener(mUSSDDialogInputListener);
489 inputText.requestFocus();
490
491 // set the window properties of the dialog
492 newDialog.getWindow().setType(
493 WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
494 newDialog.getWindow().addFlags(
495 WindowManager.LayoutParams.FLAG_DIM_BEHIND);
496
497 // now show the dialog!
498 newDialog.show();
Andrew Leeb3ee4dc2015-01-23 17:35:00 -0800499
500 newDialog.getButton(DialogInterface.BUTTON_POSITIVE)
501 .setTextColor(context.getResources().getColor(R.color.dialer_theme_color));
502 newDialog.getButton(DialogInterface.BUTTON_NEGATIVE)
503 .setTextColor(context.getResources().getColor(R.color.dialer_theme_color));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700504 }
505 }
506 }
507
SongFerngWang5ae4f6d2018-10-31 22:11:32 +0800508 /**
509 * It displays the message dialog for user about the mmi code result message.
510 *
511 * @param app This is {@link PhoneGlobals}
512 * @param context Context to get strings.
513 * @param text This is message's result.
Youming Yeeb117bd2019-04-15 14:05:02 -0700514 * @param phone This is phone to create sssd dialog.
SongFerngWang5ae4f6d2018-10-31 22:11:32 +0800515 * @param windowType The new window type. {@link WindowManager.LayoutParams}.
516 */
517 public static void createUssdDialog(PhoneGlobals app, Context context, CharSequence text,
Youming Yeeb117bd2019-04-15 14:05:02 -0700518 Phone phone, int windowType) {
SongFerngWangdfa107a2018-10-29 20:31:58 +0800519 log("displayMMIComplete: MMI code has finished running.");
520
521 log("displayMMIComplete: Extended NW displayMMIInitiate (" + text + ")");
522 if (text == null || text.length() == 0) {
523 return;
524 }
525
526 // displaying system alert dialog on the screen instead of
527 // using another activity to display the message. This
528 // places the message at the forefront of the UI.
529
530 if (sUssdDialog == null) {
531 sUssdDialog = new AlertDialog.Builder(context, THEME)
532 .setPositiveButton(R.string.ok, null)
533 .setCancelable(true)
534 .setOnDismissListener(new DialogInterface.OnDismissListener() {
535 @Override
536 public void onDismiss(DialogInterface dialog) {
537 sUssdMsg.setLength(0);
538 }
539 })
540 .create();
541
542 sUssdDialog.getWindow().setType(windowType);
543 sUssdDialog.getWindow().addFlags(
544 WindowManager.LayoutParams.FLAG_DIM_BEHIND);
545 }
546 if (sUssdMsg.length() != 0) {
547 sUssdMsg
548 .insert(0, "\n")
549 .insert(0, app.getResources().getString(R.string.ussd_dialog_sep))
550 .insert(0, "\n");
551 }
Youming Yeeb117bd2019-04-15 14:05:02 -0700552 if (phone != null && phone.getCarrierName() != null) {
553 sUssdDialog.setTitle(app.getResources().getString(R.string.carrier_mmi_msg_title,
554 phone.getCarrierName()));
555 } else {
556 sUssdDialog
557 .setTitle(app.getResources().getString(R.string.default_carrier_mmi_msg_title));
558 }
SongFerngWangdfa107a2018-10-29 20:31:58 +0800559 sUssdMsg.insert(0, text);
560 sUssdDialog.setMessage(sUssdMsg.toString());
561 sUssdDialog.show();
562 }
563
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700564 /**
565 * Cancels the current pending MMI operation, if applicable.
566 * @return true if we canceled an MMI operation, or false
567 * if the current pending MMI wasn't cancelable
568 * or if there was no current pending MMI at all.
569 *
570 * @see displayMMIInitiate
571 */
572 static boolean cancelMmiCode(Phone phone) {
573 List<? extends MmiCode> pendingMmis = phone.getPendingMmiCodes();
574 int count = pendingMmis.size();
575 if (DBG) log("cancelMmiCode: num pending MMIs = " + count);
576
577 boolean canceled = false;
578 if (count > 0) {
579 // assume that we only have one pending MMI operation active at a time.
580 // I don't think it's possible to enter multiple MMI codes concurrently
581 // in the phone UI, because during the MMI operation, an Alert panel
582 // is displayed, which prevents more MMI code from being entered.
583 MmiCode mmiCode = pendingMmis.get(0);
584 if (mmiCode.isCancelable()) {
585 mmiCode.cancel();
586 canceled = true;
587 }
588 }
589 return canceled;
590 }
591
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700592
593 //
594 // Misc UI policy helper functions
595 //
596
597 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700598 * Check if a phone number can be route through a 3rd party
599 * gateway. The number must be a global phone number in numerical
600 * form (1-800-666-SEXY won't work).
601 *
602 * MMI codes and the like cannot be used as a dial number for the
603 * gateway either.
604 *
605 * @param number To be dialed via a 3rd party gateway.
606 * @return true If the number can be routed through the 3rd party network.
607 */
Santos Cordon69a69192013-08-22 14:25:42 -0700608 private static boolean isRoutableViaGateway(String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700609 if (TextUtils.isEmpty(number)) {
610 return false;
611 }
612 number = PhoneNumberUtils.stripSeparators(number);
613 if (!number.equals(PhoneNumberUtils.convertKeypadLettersToDigits(number))) {
614 return false;
615 }
616 number = PhoneNumberUtils.extractNetworkPortion(number);
617 return PhoneNumberUtils.isGlobalPhoneNumber(number);
618 }
619
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700620 /**
621 * Returns whether the phone is in ECM ("Emergency Callback Mode") or not.
622 */
623 /* package */ static boolean isPhoneInEcm(Phone phone) {
624 if ((phone != null) && TelephonyCapabilities.supportsEcm(phone)) {
Shuo Qiandd210312017-04-12 22:11:33 +0000625 return phone.isInEcm();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700626 }
627 return false;
628 }
629
630 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700631 * Returns true when the given call is in INCOMING state and there's no foreground phone call,
632 * meaning the call is the first real incoming call the phone is having.
633 */
634 public static boolean isRealIncomingCall(Call.State state) {
635 return (state == Call.State.INCOMING && !PhoneGlobals.getInstance().mCM.hasActiveFgCall());
636 }
637
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700638 //
639 // General phone and call state debugging/testing code
640 //
641
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700642 private static void log(String msg) {
643 Log.d(LOG_TAG, msg);
644 }
645
Nancy Chene53a0642015-03-12 11:20:12 -0700646 public static PhoneAccountHandle makePstnPhoneAccountHandle(String id) {
647 return makePstnPhoneAccountHandleWithPrefix(id, "", false);
648 }
649
Nancy Chen32031f42015-03-04 21:00:07 -0800650 public static PhoneAccountHandle makePstnPhoneAccountHandle(int phoneId) {
651 return makePstnPhoneAccountHandle(PhoneFactory.getPhone(phoneId));
652 }
653
Andrew Leed5165b02014-12-05 15:53:58 -0800654 public static PhoneAccountHandle makePstnPhoneAccountHandle(Phone phone) {
655 return makePstnPhoneAccountHandleWithPrefix(phone, "", false);
656 }
657
658 public static PhoneAccountHandle makePstnPhoneAccountHandleWithPrefix(
659 Phone phone, String prefix, boolean isEmergency) {
Andrew Leed5165b02014-12-05 15:53:58 -0800660 // TODO: Should use some sort of special hidden flag to decorate this account as
661 // an emergency-only account
Brad Ebingerd3edc222016-03-23 19:44:38 -0700662 String id = isEmergency ? EMERGENCY_ACCOUNT_HANDLE_ID : prefix +
Brad Ebinger4fe48112016-04-13 11:21:53 -0700663 String.valueOf(phone.getFullIccSerialNumber());
Nancy Chene53a0642015-03-12 11:20:12 -0700664 return makePstnPhoneAccountHandleWithPrefix(id, prefix, isEmergency);
665 }
666
667 public static PhoneAccountHandle makePstnPhoneAccountHandleWithPrefix(
668 String id, String prefix, boolean isEmergency) {
669 ComponentName pstnConnectionServiceName = getPstnConnectionServiceName();
Andrew Leed5165b02014-12-05 15:53:58 -0800670 return new PhoneAccountHandle(pstnConnectionServiceName, id);
671 }
Stuart Scottdcf40a92014-12-09 10:45:01 -0800672
Santos Cordonf69df962015-02-03 11:15:19 -0800673 public static int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
Santos Cordon21e78ac2015-02-06 05:19:29 -0800674 if (phoneAccount != null
675 && phoneAccount.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
676 return getSubIdForPhoneAccountHandle(phoneAccount.getAccountHandle());
677 }
678 return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
679 }
680
681 public static int getSubIdForPhoneAccountHandle(PhoneAccountHandle handle) {
Nancy Chen31f9ba12016-01-06 11:42:12 -0800682 Phone phone = getPhoneForPhoneAccountHandle(handle);
683 if (phone != null) {
684 return phone.getSubId();
Santos Cordonf69df962015-02-03 11:15:19 -0800685 }
686 return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
687 }
688
Ta-wei Yen845f6992016-06-16 12:53:46 -0700689 public static Phone getPhoneForPhoneAccountHandle(PhoneAccountHandle handle) {
Nancy Chen31f9ba12016-01-06 11:42:12 -0800690 if (handle != null && handle.getComponentName().equals(getPstnConnectionServiceName())) {
691 return getPhoneFromIccId(handle.getId());
692 }
693 return null;
694 }
695
Nancy Chene53a0642015-03-12 11:20:12 -0700696 /**
697 * Determine if a given phone account corresponds to an active SIM
698 *
699 * @param sm An instance of the subscription manager so it is not recreated for each calling of
700 * this method.
701 * @param handle The handle for the phone account to check
702 * @return {@code true} If there is an active SIM for this phone account,
703 * {@code false} otherwise.
704 */
705 public static boolean isPhoneAccountActive(SubscriptionManager sm, PhoneAccountHandle handle) {
Shuo Qiandfc85882020-01-06 15:59:12 -0800706 return sm.getActiveSubscriptionInfoForIcc(handle.getId()) != null;
Nancy Chene53a0642015-03-12 11:20:12 -0700707 }
708
Santos Cordonf69df962015-02-03 11:15:19 -0800709 private static ComponentName getPstnConnectionServiceName() {
Tony Mak4c8f3c62015-12-07 02:30:37 +0000710 return PSTN_CONNECTION_SERVICE_COMPONENT;
Santos Cordonf69df962015-02-03 11:15:19 -0800711 }
712
Santos Cordon80855d52015-02-10 04:33:36 -0800713 private static Phone getPhoneFromIccId(String iccId) {
714 if (!TextUtils.isEmpty(iccId)) {
715 for (Phone phone : PhoneFactory.getPhones()) {
Brad Ebinger4fe48112016-04-13 11:21:53 -0700716 String phoneIccId = phone.getFullIccSerialNumber();
Santos Cordon80855d52015-02-10 04:33:36 -0800717 if (iccId.equals(phoneIccId)) {
718 return phone;
719 }
720 }
721 }
Santos Cordon80855d52015-02-10 04:33:36 -0800722 return null;
723 }
724
Stuart Scottdcf40a92014-12-09 10:45:01 -0800725 /**
726 * Register ICC status for all phones.
727 */
728 static final void registerIccStatus(Handler handler, int event) {
729 for (Phone phone : PhoneFactory.getPhones()) {
730 IccCard sim = phone.getIccCard();
731 if (sim != null) {
732 if (VDBG) Log.v(LOG_TAG, "register for ICC status, phone " + phone.getPhoneId());
733 sim.registerForNetworkLocked(handler, event, phone);
734 }
735 }
736 }
737
738 /**
Amit Mahajanf5d92c82018-11-02 17:44:40 -0700739 * Register ICC status for all phones.
740 */
741 static final void registerIccStatus(Handler handler, int event, int phoneId) {
742 Phone[] phones = PhoneFactory.getPhones();
743 IccCard sim = phones[phoneId].getIccCard();
744 if (sim != null) {
745 if (VDBG) {
746 Log.v(LOG_TAG, "register for ICC status, phone " + phones[phoneId].getPhoneId());
747 }
748 sim.registerForNetworkLocked(handler, event, phones[phoneId]);
749 }
750 }
751
752 /**
753 * Unregister ICC status for a specific phone.
754 */
755 static final void unregisterIccStatus(Handler handler, int phoneId) {
756 Phone[] phones = PhoneFactory.getPhones();
757 IccCard sim = phones[phoneId].getIccCard();
758 if (sim != null) {
759 if (VDBG) {
760 Log.v(LOG_TAG, "unregister for ICC status, phone " + phones[phoneId].getPhoneId());
761 }
762 sim.unregisterForNetworkLocked(handler);
763 }
764 }
765
766 /**
Stuart Scottdcf40a92014-12-09 10:45:01 -0800767 * Set the radio power on/off state for all phones.
768 *
769 * @param enabled true means on, false means off.
770 */
771 static final void setRadioPower(boolean enabled) {
772 for (Phone phone : PhoneFactory.getPhones()) {
773 phone.setRadioPower(enabled);
774 }
775 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700776}