blob: 362573365f3e3fb524816382a87a5134c3ead4cb [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;
SongFerngWang5ae4f6d2018-10-31 22:11:32 +080054import com.android.phone.settings.SuppServicesUiUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056import java.util.List;
57
58/**
59 * Misc utilities for the Phone app.
60 */
61public class PhoneUtils {
Brad Ebingerd3edc222016-03-23 19:44:38 -070062 public static final String EMERGENCY_ACCOUNT_HANDLE_ID = "E";
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063 private static final String LOG_TAG = "PhoneUtils";
64 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
65
66 // Do not check in with VDBG = true, since that may write PII to the system log.
67 private static final boolean VDBG = false;
68
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069 // Return codes from placeCall()
fionaxuf7885142016-11-27 16:08:39 -080070 public static final int CALL_STATUS_DIALED = 0; // The number was successfully dialed
71 public static final int CALL_STATUS_DIALED_MMI = 1; // The specified number was an MMI code
72 public static final int CALL_STATUS_FAILED = 2; // The call failed
Santos Cordon7d4ddf62013-07-10 11:58:08 -070073
Santos Cordon7d4ddf62013-07-10 11:58:08 -070074 // USSD string length for MMI operations
75 static final int MIN_USSD_LEN = 1;
76 static final int MAX_USSD_LEN = 160;
77
Santos Cordon7d4ddf62013-07-10 11:58:08 -070078 /** Define for not a special CNAP string */
79 private static final int CNAP_SPECIAL_CASE_NO = -1;
80
Yorke Leee0f2b792014-10-15 12:30:44 -070081 /**
82 * Theme to use for dialogs displayed by utility methods in this class. This is needed
83 * because these dialogs are displayed using the application context, which does not resolve
84 * the dialog theme correctly.
85 */
Zheng Zhangd889c772017-11-02 15:49:37 -070086 private static final int THEME = com.android.internal.R.style.Theme_DeviceDefault_Dialog_Alert;
Yorke Leee0f2b792014-10-15 12:30:44 -070087
Etan Cohen37abbab2014-03-07 16:57:03 -080088 /** USSD information used to aggregate all USSD messages */
89 private static AlertDialog sUssdDialog = null;
90 private static StringBuilder sUssdMsg = new StringBuilder();
91
Tony Mak4c8f3c62015-12-07 02:30:37 +000092 private static final ComponentName PSTN_CONNECTION_SERVICE_COMPONENT =
93 new ComponentName("com.android.phone",
94 "com.android.services.telephony.TelephonyConnectionService");
95
Santos Cordon7d4ddf62013-07-10 11:58:08 -070096 /** This class is never instantiated. */
97 private PhoneUtils() {
98 }
99
100 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700101 * For a CDMA phone, advance the call state upon making a new
102 * outgoing call.
103 *
104 * <pre>
105 * IDLE -> SINGLE_ACTIVE
106 * or
107 * SINGLE_ACTIVE -> THRWAY_ACTIVE
108 * </pre>
109 * @param app The phone instance.
110 */
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700111 private static void updateCdmaCallStateOnNewOutgoingCall(PhoneGlobals app,
112 Connection connection) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700113 if (app.cdmaPhoneCallState.getCurrentCallState() ==
114 CdmaPhoneCallState.PhoneCallState.IDLE) {
115 // This is the first outgoing call. Set the Phone Call State to ACTIVE
116 app.cdmaPhoneCallState.setCurrentCallState(
117 CdmaPhoneCallState.PhoneCallState.SINGLE_ACTIVE);
118 } else {
119 // This is the second outgoing call. Set the Phone Call State to 3WAY
120 app.cdmaPhoneCallState.setCurrentCallState(
121 CdmaPhoneCallState.PhoneCallState.THRWAY_ACTIVE);
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700122
Santos Cordonda120f42014-08-06 04:44:34 -0700123 // TODO: Remove this code.
Sailesh Nepal23d9ed72014-07-03 09:40:26 -0700124 //app.getCallModeler().setCdmaOutgoing3WayCall(connection);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700125 }
126 }
127
128 /**
129 * Dial the number using the phone passed in.
130 *
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700131 * @param context To perform the CallerInfo query.
132 * @param phone the Phone object.
133 * @param number to be dialed as requested by the user. This is
134 * NOT the phone number to connect to. It is used only to build the
135 * call card and to update the call log. See above for restrictions.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700136 *
137 * @return either CALL_STATUS_DIALED or CALL_STATUS_FAILED
138 */
Hall Liu0c2ef0b2019-10-31 15:16:38 -0700139 public static int placeOtaspCall(Context context, Phone phone, String number) {
140 final Uri gatewayUri = null;
Santos Cordon69a69192013-08-22 14:25:42 -0700141
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700142 if (VDBG) {
143 log("placeCall()... number: '" + number + "'"
Hall Liu0c2ef0b2019-10-31 15:16:38 -0700144 + ", GW:'" + gatewayUri + "'");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700145 } else {
146 log("placeCall()... number: " + toLogSafePhoneNumber(number)
Hall Liu0c2ef0b2019-10-31 15:16:38 -0700147 + ", GW: " + (gatewayUri != null ? "non-null" : "null"));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700148 }
149 final PhoneGlobals app = PhoneGlobals.getInstance();
150
151 boolean useGateway = false;
Hall Liu0c2ef0b2019-10-31 15:16:38 -0700152 Uri contactRef = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700153
154 int status = CALL_STATUS_DIALED;
155 Connection connection;
156 String numberToDial;
Hall Liu0c2ef0b2019-10-31 15:16:38 -0700157 numberToDial = number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700158
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700159 try {
Tyler Gunnc6514352015-06-02 21:46:34 -0700160 connection = app.mCM.dial(phone, numberToDial, VideoProfile.STATE_AUDIO_ONLY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700161 } catch (CallStateException ex) {
162 // CallStateException means a new outgoing call is not currently
163 // possible: either no more call slots exist, or there's another
164 // call already in the process of dialing or ringing.
165 Log.w(LOG_TAG, "Exception from app.mCM.dial()", ex);
166 return CALL_STATUS_FAILED;
167
168 // Note that it's possible for CallManager.dial() to return
169 // null *without* throwing an exception; that indicates that
170 // we dialed an MMI (see below).
171 }
172
173 int phoneType = phone.getPhoneType();
174
175 // On GSM phones, null is returned for MMI codes
176 if (null == connection) {
Santos Cordonf77a3392014-07-21 19:48:10 -0700177 status = CALL_STATUS_FAILED;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700178 } else {
179 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700180 updateCdmaCallStateOnNewOutgoingCall(app, connection);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700181 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700182 }
183
184 return status;
185 }
186
187 /* package */ static String toLogSafePhoneNumber(String number) {
188 // For unknown number, log empty string.
189 if (number == null) {
190 return "";
191 }
192
193 if (VDBG) {
194 // When VDBG is true we emit PII.
195 return number;
196 }
197
198 // Do exactly same thing as Uri#toSafeString() does, which will enable us to compare
199 // sanitized phone numbers.
200 StringBuilder builder = new StringBuilder();
201 for (int i = 0; i < number.length(); i++) {
202 char c = number.charAt(i);
203 if (c == '-' || c == '@' || c == '.') {
204 builder.append(c);
205 } else {
206 builder.append('x');
207 }
208 }
209 return builder.toString();
210 }
211
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700212 /**
213 * Handle the MMIInitiate message and put up an alert that lets
214 * the user cancel the operation, if applicable.
215 *
216 * @param context context to get strings.
217 * @param mmiCode the MmiCode object being started.
218 * @param buttonCallbackMessage message to post when button is clicked.
219 * @param previousAlert a previous alert used in this activity.
220 * @return the dialog handle
221 */
222 static Dialog displayMMIInitiate(Context context,
223 MmiCode mmiCode,
224 Message buttonCallbackMessage,
225 Dialog previousAlert) {
Tyler Gunn13fe2492017-05-22 14:14:20 -0700226 log("displayMMIInitiate: " + android.telecom.Log.pii(mmiCode.toString()));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700227 if (previousAlert != null) {
228 previousAlert.dismiss();
229 }
230
231 // The UI paradigm we are using now requests that all dialogs have
232 // user interaction, and that any other messages to the user should
233 // be by way of Toasts.
234 //
235 // In adhering to this request, all MMI initiating "OK" dialogs
236 // (non-cancelable MMIs) that end up being closed when the MMI
237 // completes (thereby showing a completion dialog) are being
238 // replaced with Toasts.
239 //
240 // As a side effect, moving to Toasts for the non-cancelable MMIs
241 // also means that buttonCallbackMessage (which was tied into "OK")
242 // is no longer invokable for these dialogs. This is not a problem
243 // since the only callback messages we supported were for cancelable
244 // MMIs anyway.
245 //
246 // A cancelable MMI is really just a USSD request. The term
247 // "cancelable" here means that we can cancel the request when the
248 // system prompts us for a response, NOT while the network is
249 // processing the MMI request. Any request to cancel a USSD while
250 // the network is NOT ready for a response may be ignored.
251 //
252 // With this in mind, we replace the cancelable alert dialog with
253 // a progress dialog, displayed until we receive a request from
254 // the the network. For more information, please see the comments
255 // in the displayMMIComplete() method below.
256 //
257 // Anything that is NOT a USSD request is a normal MMI request,
258 // which will bring up a toast (desribed above).
259
260 boolean isCancelable = (mmiCode != null) && mmiCode.isCancelable();
261
262 if (!isCancelable) {
Tyler Gunn13fe2492017-05-22 14:14:20 -0700263 log("displayMMIInitiate: not a USSD code, displaying status toast.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700264 CharSequence text = context.getText(R.string.mmiStarted);
265 Toast.makeText(context, text, Toast.LENGTH_SHORT)
266 .show();
267 return null;
268 } else {
Tyler Gunn13fe2492017-05-22 14:14:20 -0700269 log("displayMMIInitiate: running USSD code, displaying intermediate progress.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700270
271 // create the indeterminate progress dialog and display it.
Alex Hills779841a2015-10-22 13:23:28 -0400272 ProgressDialog pd = new ProgressDialog(context, THEME);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700273 pd.setMessage(context.getText(R.string.ussdRunning));
274 pd.setCancelable(false);
275 pd.setIndeterminate(true);
276 pd.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
277
278 pd.show();
279
280 return pd;
281 }
282
283 }
284
285 /**
286 * Handle the MMIComplete message and fire off an intent to display
287 * the message.
288 *
289 * @param context context to get strings.
290 * @param mmiCode MMI result.
291 * @param previousAlert a previous alert used in this activity.
292 */
293 static void displayMMIComplete(final Phone phone, Context context, final MmiCode mmiCode,
294 Message dismissCallbackMessage,
295 AlertDialog previousAlert) {
296 final PhoneGlobals app = PhoneGlobals.getInstance();
297 CharSequence text;
298 int title = 0; // title for the progress dialog, if needed.
299 MmiCode.State state = mmiCode.getState();
300
Tyler Gunn13fe2492017-05-22 14:14:20 -0700301 log("displayMMIComplete: state=" + state);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700302
303 switch (state) {
304 case PENDING:
305 // USSD code asking for feedback from user.
306 text = mmiCode.getMessage();
Tyler Gunn13fe2492017-05-22 14:14:20 -0700307 log("displayMMIComplete: using text from PENDING MMI message: '" + text + "'");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700308 break;
309 case CANCELLED:
310 text = null;
311 break;
312 case COMPLETE:
SongFerngWang5ae4f6d2018-10-31 22:11:32 +0800313 PersistableBundle b = null;
314 if (SubscriptionManager.isValidSubscriptionId(phone.getSubId())) {
315 b = app.getCarrierConfigForSubId(
316 phone.getSubId());
317 } else {
318 b = app.getCarrierConfig();
319 }
320
321 if (b.getBoolean(CarrierConfigManager.KEY_USE_CALLER_ID_USSD_BOOL)) {
322 text = SuppServicesUiUtil.handleCallerIdUssdResponse(app, context, phone,
323 mmiCode);
324 if (mmiCode.getMessage() != null && !text.equals(mmiCode.getMessage())) {
325 break;
326 }
327 }
328
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700329 if (app.getPUKEntryActivity() != null) {
330 // if an attempt to unPUK the device was made, we specify
331 // the title and the message here.
332 title = com.android.internal.R.string.PinMmi;
333 text = context.getText(R.string.puk_unlocked);
334 break;
335 }
336 // All other conditions for the COMPLETE mmi state will cause
337 // the case to fall through to message logic in common with
338 // the FAILED case.
339
340 case FAILED:
341 text = mmiCode.getMessage();
Tyler Gunn13fe2492017-05-22 14:14:20 -0700342 log("displayMMIComplete (failed): using text from MMI message: '" + text + "'");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700343 break;
344 default:
345 throw new IllegalStateException("Unexpected MmiCode state: " + state);
346 }
347
348 if (previousAlert != null) {
349 previousAlert.dismiss();
350 }
351
352 // Check to see if a UI exists for the PUK activation. If it does
353 // exist, then it indicates that we're trying to unblock the PUK.
354 if ((app.getPUKEntryActivity() != null) && (state == MmiCode.State.COMPLETE)) {
355 if (DBG) log("displaying PUK unblocking progress dialog.");
356
357 // create the progress dialog, make sure the flags and type are
358 // set correctly.
Alex Hills779841a2015-10-22 13:23:28 -0400359 ProgressDialog pd = new ProgressDialog(app, THEME);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700360 pd.setTitle(title);
361 pd.setMessage(text);
362 pd.setCancelable(false);
363 pd.setIndeterminate(true);
364 pd.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
365 pd.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
366
367 // display the dialog
368 pd.show();
369
370 // indicate to the Phone app that the progress dialog has
371 // been assigned for the PUK unlock / SIM READY process.
372 app.setPukEntryProgressDialog(pd);
373
SongFerngWangdfa107a2018-10-29 20:31:58 +0800374 } else if ((app.getPUKEntryActivity() != null) && (state == MmiCode.State.FAILED)) {
Youming Yeef84f3e2019-04-15 14:05:02 -0700375 createUssdDialog(app, context, text, phone,
SongFerngWangdfa107a2018-10-29 20:31:58 +0800376 WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
377 // In case of failure to unlock, we'll need to reset the
378 // PUK unlock activity, so that the user may try again.
379 app.setPukEntryActivity(null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700380 } else {
381 // In case of failure to unlock, we'll need to reset the
382 // PUK unlock activity, so that the user may try again.
383 if (app.getPUKEntryActivity() != null) {
384 app.setPukEntryActivity(null);
385 }
386
387 // A USSD in a pending state means that it is still
388 // interacting with the user.
389 if (state != MmiCode.State.PENDING) {
Youming Yeef84f3e2019-04-15 14:05:02 -0700390 createUssdDialog(app, context, text, phone,
SongFerngWangdfa107a2018-10-29 20:31:58 +0800391 WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700392 } else {
Tyler Gunn13fe2492017-05-22 14:14:20 -0700393 log("displayMMIComplete: USSD code has requested user input. Constructing input "
394 + "dialog.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700395
396 // USSD MMI code that is interacting with the user. The
397 // basic set of steps is this:
398 // 1. User enters a USSD request
399 // 2. We recognize the request and displayMMIInitiate
400 // (above) creates a progress dialog.
401 // 3. Request returns and we get a PENDING or COMPLETE
402 // message.
403 // 4. These MMI messages are caught in the PhoneApp
404 // (onMMIComplete) and the InCallScreen
405 // (mHandler.handleMessage) which bring up this dialog
406 // and closes the original progress dialog,
407 // respectively.
408 // 5. If the message is anything other than PENDING,
409 // we are done, and the alert dialog (directly above)
410 // displays the outcome.
411 // 6. If the network is requesting more information from
412 // the user, the MMI will be in a PENDING state, and
413 // we display this dialog with the message.
414 // 7. User input, or cancel requests result in a return
415 // to step 1. Keep in mind that this is the only
416 // time that a USSD should be canceled.
417
418 // inflate the layout with the scrolling text area for the dialog.
Andrew Leeb3ee4dc2015-01-23 17:35:00 -0800419 ContextThemeWrapper contextThemeWrapper =
420 new ContextThemeWrapper(context, R.style.DialerAlertDialogTheme);
421 LayoutInflater inflater = (LayoutInflater) contextThemeWrapper.getSystemService(
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700422 Context.LAYOUT_INFLATER_SERVICE);
423 View dialogView = inflater.inflate(R.layout.dialog_ussd_response, null);
424
425 // get the input field.
426 final EditText inputText = (EditText) dialogView.findViewById(R.id.input_field);
427
428 // specify the dialog's click listener, with SEND and CANCEL logic.
429 final DialogInterface.OnClickListener mUSSDDialogListener =
430 new DialogInterface.OnClickListener() {
431 public void onClick(DialogInterface dialog, int whichButton) {
432 switch (whichButton) {
433 case DialogInterface.BUTTON_POSITIVE:
434 // As per spec 24.080, valid length of ussd string
435 // is 1 - 160. If length is out of the range then
436 // display toast message & Cancel MMI operation.
437 if (inputText.length() < MIN_USSD_LEN
438 || inputText.length() > MAX_USSD_LEN) {
439 Toast.makeText(app,
440 app.getResources().getString(R.string.enter_input,
441 MIN_USSD_LEN, MAX_USSD_LEN),
442 Toast.LENGTH_LONG).show();
443 if (mmiCode.isCancelable()) {
444 mmiCode.cancel();
445 }
446 } else {
447 phone.sendUssdResponse(inputText.getText().toString());
448 }
449 break;
450 case DialogInterface.BUTTON_NEGATIVE:
451 if (mmiCode.isCancelable()) {
452 mmiCode.cancel();
453 }
454 break;
455 }
456 }
457 };
458
459 // build the dialog
Andrew Leeb3ee4dc2015-01-23 17:35:00 -0800460 final AlertDialog newDialog = new AlertDialog.Builder(contextThemeWrapper)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700461 .setMessage(text)
462 .setView(dialogView)
463 .setPositiveButton(R.string.send_button, mUSSDDialogListener)
464 .setNegativeButton(R.string.cancel, mUSSDDialogListener)
465 .setCancelable(false)
466 .create();
467
468 // attach the key listener to the dialog's input field and make
469 // sure focus is set.
470 final View.OnKeyListener mUSSDDialogInputListener =
471 new View.OnKeyListener() {
472 public boolean onKey(View v, int keyCode, KeyEvent event) {
473 switch (keyCode) {
474 case KeyEvent.KEYCODE_CALL:
475 case KeyEvent.KEYCODE_ENTER:
476 if(event.getAction() == KeyEvent.ACTION_DOWN) {
477 phone.sendUssdResponse(inputText.getText().toString());
478 newDialog.dismiss();
479 }
480 return true;
481 }
482 return false;
483 }
484 };
485 inputText.setOnKeyListener(mUSSDDialogInputListener);
486 inputText.requestFocus();
487
488 // set the window properties of the dialog
489 newDialog.getWindow().setType(
490 WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
491 newDialog.getWindow().addFlags(
492 WindowManager.LayoutParams.FLAG_DIM_BEHIND);
493
494 // now show the dialog!
495 newDialog.show();
Andrew Leeb3ee4dc2015-01-23 17:35:00 -0800496
497 newDialog.getButton(DialogInterface.BUTTON_POSITIVE)
498 .setTextColor(context.getResources().getColor(R.color.dialer_theme_color));
499 newDialog.getButton(DialogInterface.BUTTON_NEGATIVE)
500 .setTextColor(context.getResources().getColor(R.color.dialer_theme_color));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700501 }
502 }
503 }
504
SongFerngWang5ae4f6d2018-10-31 22:11:32 +0800505 /**
506 * It displays the message dialog for user about the mmi code result message.
507 *
508 * @param app This is {@link PhoneGlobals}
509 * @param context Context to get strings.
510 * @param text This is message's result.
Youming Yeef84f3e2019-04-15 14:05:02 -0700511 * @param phone This is phone to create sssd dialog.
SongFerngWang5ae4f6d2018-10-31 22:11:32 +0800512 * @param windowType The new window type. {@link WindowManager.LayoutParams}.
513 */
514 public static void createUssdDialog(PhoneGlobals app, Context context, CharSequence text,
Youming Yeef84f3e2019-04-15 14:05:02 -0700515 Phone phone, int windowType) {
SongFerngWangdfa107a2018-10-29 20:31:58 +0800516 log("displayMMIComplete: MMI code has finished running.");
517
518 log("displayMMIComplete: Extended NW displayMMIInitiate (" + text + ")");
519 if (text == null || text.length() == 0) {
520 return;
521 }
522
523 // displaying system alert dialog on the screen instead of
524 // using another activity to display the message. This
525 // places the message at the forefront of the UI.
526
527 if (sUssdDialog == null) {
528 sUssdDialog = new AlertDialog.Builder(context, THEME)
529 .setPositiveButton(R.string.ok, null)
530 .setCancelable(true)
531 .setOnDismissListener(new DialogInterface.OnDismissListener() {
532 @Override
533 public void onDismiss(DialogInterface dialog) {
534 sUssdMsg.setLength(0);
535 }
536 })
537 .create();
538
539 sUssdDialog.getWindow().setType(windowType);
540 sUssdDialog.getWindow().addFlags(
541 WindowManager.LayoutParams.FLAG_DIM_BEHIND);
542 }
543 if (sUssdMsg.length() != 0) {
544 sUssdMsg
545 .insert(0, "\n")
546 .insert(0, app.getResources().getString(R.string.ussd_dialog_sep))
547 .insert(0, "\n");
548 }
Youming Yeef84f3e2019-04-15 14:05:02 -0700549 if (phone != null && phone.getCarrierName() != null) {
550 sUssdDialog.setTitle(app.getResources().getString(R.string.carrier_mmi_msg_title,
551 phone.getCarrierName()));
552 } else {
553 sUssdDialog
554 .setTitle(app.getResources().getString(R.string.default_carrier_mmi_msg_title));
555 }
SongFerngWangdfa107a2018-10-29 20:31:58 +0800556 sUssdMsg.insert(0, text);
557 sUssdDialog.setMessage(sUssdMsg.toString());
558 sUssdDialog.show();
559 }
560
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700561 /**
562 * Cancels the current pending MMI operation, if applicable.
563 * @return true if we canceled an MMI operation, or false
564 * if the current pending MMI wasn't cancelable
565 * or if there was no current pending MMI at all.
566 *
567 * @see displayMMIInitiate
568 */
569 static boolean cancelMmiCode(Phone phone) {
570 List<? extends MmiCode> pendingMmis = phone.getPendingMmiCodes();
571 int count = pendingMmis.size();
572 if (DBG) log("cancelMmiCode: num pending MMIs = " + count);
573
574 boolean canceled = false;
575 if (count > 0) {
576 // assume that we only have one pending MMI operation active at a time.
577 // I don't think it's possible to enter multiple MMI codes concurrently
578 // in the phone UI, because during the MMI operation, an Alert panel
579 // is displayed, which prevents more MMI code from being entered.
580 MmiCode mmiCode = pendingMmis.get(0);
581 if (mmiCode.isCancelable()) {
582 mmiCode.cancel();
583 canceled = true;
584 }
585 }
586 return canceled;
587 }
588
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700589
590 //
591 // Misc UI policy helper functions
592 //
593
594 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700595 * Check if a phone number can be route through a 3rd party
596 * gateway. The number must be a global phone number in numerical
597 * form (1-800-666-SEXY won't work).
598 *
599 * MMI codes and the like cannot be used as a dial number for the
600 * gateway either.
601 *
602 * @param number To be dialed via a 3rd party gateway.
603 * @return true If the number can be routed through the 3rd party network.
604 */
Santos Cordon69a69192013-08-22 14:25:42 -0700605 private static boolean isRoutableViaGateway(String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700606 if (TextUtils.isEmpty(number)) {
607 return false;
608 }
609 number = PhoneNumberUtils.stripSeparators(number);
610 if (!number.equals(PhoneNumberUtils.convertKeypadLettersToDigits(number))) {
611 return false;
612 }
613 number = PhoneNumberUtils.extractNetworkPortion(number);
614 return PhoneNumberUtils.isGlobalPhoneNumber(number);
615 }
616
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700617 /**
618 * Returns whether the phone is in ECM ("Emergency Callback Mode") or not.
619 */
620 /* package */ static boolean isPhoneInEcm(Phone phone) {
621 if ((phone != null) && TelephonyCapabilities.supportsEcm(phone)) {
Shuo Qiandd210312017-04-12 22:11:33 +0000622 return phone.isInEcm();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700623 }
624 return false;
625 }
626
627 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700628 * Returns true when the given call is in INCOMING state and there's no foreground phone call,
629 * meaning the call is the first real incoming call the phone is having.
630 */
631 public static boolean isRealIncomingCall(Call.State state) {
632 return (state == Call.State.INCOMING && !PhoneGlobals.getInstance().mCM.hasActiveFgCall());
633 }
634
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700635 //
636 // General phone and call state debugging/testing code
637 //
638
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700639 private static void log(String msg) {
640 Log.d(LOG_TAG, msg);
641 }
642
Nancy Chene53a0642015-03-12 11:20:12 -0700643 public static PhoneAccountHandle makePstnPhoneAccountHandle(String id) {
644 return makePstnPhoneAccountHandleWithPrefix(id, "", false);
645 }
646
Nancy Chen32031f42015-03-04 21:00:07 -0800647 public static PhoneAccountHandle makePstnPhoneAccountHandle(int phoneId) {
648 return makePstnPhoneAccountHandle(PhoneFactory.getPhone(phoneId));
649 }
650
Andrew Leed5165b02014-12-05 15:53:58 -0800651 public static PhoneAccountHandle makePstnPhoneAccountHandle(Phone phone) {
652 return makePstnPhoneAccountHandleWithPrefix(phone, "", false);
653 }
654
655 public static PhoneAccountHandle makePstnPhoneAccountHandleWithPrefix(
656 Phone phone, String prefix, boolean isEmergency) {
Andrew Leed5165b02014-12-05 15:53:58 -0800657 // TODO: Should use some sort of special hidden flag to decorate this account as
658 // an emergency-only account
Brad Ebingerd3edc222016-03-23 19:44:38 -0700659 String id = isEmergency ? EMERGENCY_ACCOUNT_HANDLE_ID : prefix +
Brad Ebinger4fe48112016-04-13 11:21:53 -0700660 String.valueOf(phone.getFullIccSerialNumber());
Nancy Chene53a0642015-03-12 11:20:12 -0700661 return makePstnPhoneAccountHandleWithPrefix(id, prefix, isEmergency);
662 }
663
664 public static PhoneAccountHandle makePstnPhoneAccountHandleWithPrefix(
665 String id, String prefix, boolean isEmergency) {
666 ComponentName pstnConnectionServiceName = getPstnConnectionServiceName();
Andrew Leed5165b02014-12-05 15:53:58 -0800667 return new PhoneAccountHandle(pstnConnectionServiceName, id);
668 }
Stuart Scottdcf40a92014-12-09 10:45:01 -0800669
Santos Cordonf69df962015-02-03 11:15:19 -0800670 public static int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
Santos Cordon21e78ac2015-02-06 05:19:29 -0800671 if (phoneAccount != null
672 && phoneAccount.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
673 return getSubIdForPhoneAccountHandle(phoneAccount.getAccountHandle());
674 }
675 return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
676 }
677
678 public static int getSubIdForPhoneAccountHandle(PhoneAccountHandle handle) {
Nancy Chen31f9ba12016-01-06 11:42:12 -0800679 Phone phone = getPhoneForPhoneAccountHandle(handle);
680 if (phone != null) {
681 return phone.getSubId();
Santos Cordonf69df962015-02-03 11:15:19 -0800682 }
683 return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
684 }
685
Ta-wei Yen845f6992016-06-16 12:53:46 -0700686 public static Phone getPhoneForPhoneAccountHandle(PhoneAccountHandle handle) {
Nancy Chen31f9ba12016-01-06 11:42:12 -0800687 if (handle != null && handle.getComponentName().equals(getPstnConnectionServiceName())) {
688 return getPhoneFromIccId(handle.getId());
689 }
690 return null;
691 }
692
Nancy Chene53a0642015-03-12 11:20:12 -0700693 /**
694 * Determine if a given phone account corresponds to an active SIM
695 *
696 * @param sm An instance of the subscription manager so it is not recreated for each calling of
697 * this method.
698 * @param handle The handle for the phone account to check
699 * @return {@code true} If there is an active SIM for this phone account,
700 * {@code false} otherwise.
701 */
702 public static boolean isPhoneAccountActive(SubscriptionManager sm, PhoneAccountHandle handle) {
703 return sm.getActiveSubscriptionInfoForIccIndex(handle.getId()) != null;
704 }
705
Santos Cordonf69df962015-02-03 11:15:19 -0800706 private static ComponentName getPstnConnectionServiceName() {
Tony Mak4c8f3c62015-12-07 02:30:37 +0000707 return PSTN_CONNECTION_SERVICE_COMPONENT;
Santos Cordonf69df962015-02-03 11:15:19 -0800708 }
709
Santos Cordon80855d52015-02-10 04:33:36 -0800710 private static Phone getPhoneFromIccId(String iccId) {
711 if (!TextUtils.isEmpty(iccId)) {
712 for (Phone phone : PhoneFactory.getPhones()) {
Brad Ebinger4fe48112016-04-13 11:21:53 -0700713 String phoneIccId = phone.getFullIccSerialNumber();
Santos Cordon80855d52015-02-10 04:33:36 -0800714 if (iccId.equals(phoneIccId)) {
715 return phone;
716 }
717 }
718 }
Santos Cordon80855d52015-02-10 04:33:36 -0800719 return null;
720 }
721
Stuart Scottdcf40a92014-12-09 10:45:01 -0800722 /**
723 * Register ICC status for all phones.
724 */
725 static final void registerIccStatus(Handler handler, int event) {
726 for (Phone phone : PhoneFactory.getPhones()) {
727 IccCard sim = phone.getIccCard();
728 if (sim != null) {
729 if (VDBG) Log.v(LOG_TAG, "register for ICC status, phone " + phone.getPhoneId());
730 sim.registerForNetworkLocked(handler, event, phone);
731 }
732 }
733 }
734
735 /**
Amit Mahajanf5d92c82018-11-02 17:44:40 -0700736 * Register ICC status for all phones.
737 */
738 static final void registerIccStatus(Handler handler, int event, int phoneId) {
739 Phone[] phones = PhoneFactory.getPhones();
740 IccCard sim = phones[phoneId].getIccCard();
741 if (sim != null) {
742 if (VDBG) {
743 Log.v(LOG_TAG, "register for ICC status, phone " + phones[phoneId].getPhoneId());
744 }
745 sim.registerForNetworkLocked(handler, event, phones[phoneId]);
746 }
747 }
748
749 /**
750 * Unregister ICC status for a specific phone.
751 */
752 static final void unregisterIccStatus(Handler handler, int phoneId) {
753 Phone[] phones = PhoneFactory.getPhones();
754 IccCard sim = phones[phoneId].getIccCard();
755 if (sim != null) {
756 if (VDBG) {
757 Log.v(LOG_TAG, "unregister for ICC status, phone " + phones[phoneId].getPhoneId());
758 }
759 sim.unregisterForNetworkLocked(handler);
760 }
761 }
762
763 /**
Stuart Scottdcf40a92014-12-09 10:45:01 -0800764 * Set the radio power on/off state for all phones.
765 *
766 * @param enabled true means on, false means off.
767 */
768 static final void setRadioPower(boolean enabled) {
769 for (Phone phone : PhoneFactory.getPhones()) {
770 phone.setRadioPower(enabled);
771 }
772 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700773}