Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.phone; |
| 18 | |
| 19 | import android.app.AlertDialog; |
| 20 | import android.app.Dialog; |
| 21 | import android.app.ProgressDialog; |
Sailesh Nepal | bfb6832 | 2013-11-07 14:07:41 -0800 | [diff] [blame] | 22 | import android.content.ComponentName; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.content.DialogInterface; |
Emily Chai | e450cdc | 2014-03-10 15:10:43 +0800 | [diff] [blame] | 25 | import android.content.res.Resources; |
| 26 | import android.media.AudioAttributes; |
| 27 | import android.media.AudioManager; |
| 28 | import android.media.MediaPlayer; |
| 29 | import android.media.RingtoneManager; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 30 | import android.net.Uri; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 31 | import android.os.Handler; |
| 32 | import android.os.Message; |
SongFerngWang | 5ae4f6d | 2018-10-31 22:11:32 +0800 | [diff] [blame] | 33 | import android.os.PersistableBundle; |
Pranav Madapurmath | 78eb6cc | 2022-11-16 17:08:58 +0000 | [diff] [blame] | 34 | import android.os.UserHandle; |
Emily Chai | e450cdc | 2014-03-10 15:10:43 +0800 | [diff] [blame] | 35 | import android.os.VibrationEffect; |
| 36 | import android.os.Vibrator; |
Tyler Gunn | 4d45d1c | 2014-09-12 22:17:53 -0700 | [diff] [blame] | 37 | import android.telecom.PhoneAccount; |
Andrew Lee | d5165b0 | 2014-12-05 15:53:58 -0800 | [diff] [blame] | 38 | import android.telecom.PhoneAccountHandle; |
Tyler Gunn | 4d45d1c | 2014-09-12 22:17:53 -0700 | [diff] [blame] | 39 | import android.telecom.VideoProfile; |
SongFerngWang | 5ae4f6d | 2018-10-31 22:11:32 +0800 | [diff] [blame] | 40 | import android.telephony.CarrierConfigManager; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 41 | import android.telephony.PhoneNumberUtils; |
Santos Cordon | f69df96 | 2015-02-03 11:15:19 -0800 | [diff] [blame] | 42 | import android.telephony.SubscriptionManager; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 43 | import android.text.TextUtils; |
| 44 | import android.util.Log; |
Andrew Lee | b3ee4dc | 2015-01-23 17:35:00 -0800 | [diff] [blame] | 45 | import android.view.ContextThemeWrapper; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 46 | import android.view.KeyEvent; |
| 47 | import android.view.LayoutInflater; |
| 48 | import android.view.View; |
| 49 | import android.view.WindowManager; |
| 50 | import android.widget.EditText; |
| 51 | import android.widget.Toast; |
| 52 | |
| 53 | import com.android.internal.telephony.Call; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 54 | import com.android.internal.telephony.CallStateException; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 55 | import com.android.internal.telephony.Connection; |
Stuart Scott | dcf40a9 | 2014-12-09 10:45:01 -0800 | [diff] [blame] | 56 | import com.android.internal.telephony.IccCard; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 57 | import com.android.internal.telephony.MmiCode; |
| 58 | import com.android.internal.telephony.Phone; |
| 59 | import com.android.internal.telephony.PhoneConstants; |
Stuart Scott | dcf40a9 | 2014-12-09 10:45:01 -0800 | [diff] [blame] | 60 | import com.android.internal.telephony.PhoneFactory; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 61 | import com.android.internal.telephony.TelephonyCapabilities; |
SongFerngWang | 5ae4f6d | 2018-10-31 22:11:32 +0800 | [diff] [blame] | 62 | import com.android.phone.settings.SuppServicesUiUtil; |
Emily Chai | e450cdc | 2014-03-10 15:10:43 +0800 | [diff] [blame] | 63 | import com.android.telephony.Rlog; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 64 | |
Emily Chai | e450cdc | 2014-03-10 15:10:43 +0800 | [diff] [blame] | 65 | import java.io.IOException; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 66 | import java.util.List; |
| 67 | |
| 68 | /** |
| 69 | * Misc utilities for the Phone app. |
| 70 | */ |
| 71 | public class PhoneUtils { |
Brad Ebinger | d3edc22 | 2016-03-23 19:44:38 -0700 | [diff] [blame] | 72 | public static final String EMERGENCY_ACCOUNT_HANDLE_ID = "E"; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 73 | private static final String LOG_TAG = "PhoneUtils"; |
| 74 | private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2); |
| 75 | |
| 76 | // Do not check in with VDBG = true, since that may write PII to the system log. |
| 77 | private static final boolean VDBG = false; |
| 78 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 79 | // Return codes from placeCall() |
fionaxu | f788514 | 2016-11-27 16:08:39 -0800 | [diff] [blame] | 80 | public static final int CALL_STATUS_DIALED = 0; // The number was successfully dialed |
| 81 | public static final int CALL_STATUS_DIALED_MMI = 1; // The specified number was an MMI code |
| 82 | public static final int CALL_STATUS_FAILED = 2; // The call failed |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 83 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 84 | // USSD string length for MMI operations |
| 85 | static final int MIN_USSD_LEN = 1; |
| 86 | static final int MAX_USSD_LEN = 160; |
| 87 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 88 | /** Define for not a special CNAP string */ |
| 89 | private static final int CNAP_SPECIAL_CASE_NO = -1; |
| 90 | |
Emily Chai | e450cdc | 2014-03-10 15:10:43 +0800 | [diff] [blame] | 91 | /** Define for default vibrate pattern if res cannot be found */ |
| 92 | private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250}; |
| 93 | |
Yorke Lee | e0f2b79 | 2014-10-15 12:30:44 -0700 | [diff] [blame] | 94 | /** |
| 95 | * Theme to use for dialogs displayed by utility methods in this class. This is needed |
| 96 | * because these dialogs are displayed using the application context, which does not resolve |
| 97 | * the dialog theme correctly. |
| 98 | */ |
Zheng Zhang | d889c77 | 2017-11-02 15:49:37 -0700 | [diff] [blame] | 99 | private static final int THEME = com.android.internal.R.style.Theme_DeviceDefault_Dialog_Alert; |
Yorke Lee | e0f2b79 | 2014-10-15 12:30:44 -0700 | [diff] [blame] | 100 | |
Etan Cohen | 37abbab | 2014-03-07 16:57:03 -0800 | [diff] [blame] | 101 | /** USSD information used to aggregate all USSD messages */ |
Etan Cohen | 37abbab | 2014-03-07 16:57:03 -0800 | [diff] [blame] | 102 | private static StringBuilder sUssdMsg = new StringBuilder(); |
| 103 | |
Tony Mak | 4c8f3c6 | 2015-12-07 02:30:37 +0000 | [diff] [blame] | 104 | private static final ComponentName PSTN_CONNECTION_SERVICE_COMPONENT = |
| 105 | new ComponentName("com.android.phone", |
| 106 | "com.android.services.telephony.TelephonyConnectionService"); |
| 107 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 108 | /** This class is never instantiated. */ |
| 109 | private PhoneUtils() { |
| 110 | } |
| 111 | |
| 112 | /** |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 113 | * For a CDMA phone, advance the call state upon making a new |
| 114 | * outgoing call. |
| 115 | * |
| 116 | * <pre> |
| 117 | * IDLE -> SINGLE_ACTIVE |
| 118 | * or |
| 119 | * SINGLE_ACTIVE -> THRWAY_ACTIVE |
| 120 | * </pre> |
| 121 | * @param app The phone instance. |
| 122 | */ |
Santos Cordon | ad1ed6d | 2013-09-16 03:04:23 -0700 | [diff] [blame] | 123 | private static void updateCdmaCallStateOnNewOutgoingCall(PhoneGlobals app, |
| 124 | Connection connection) { |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 125 | if (app.cdmaPhoneCallState.getCurrentCallState() == |
| 126 | CdmaPhoneCallState.PhoneCallState.IDLE) { |
| 127 | // This is the first outgoing call. Set the Phone Call State to ACTIVE |
| 128 | app.cdmaPhoneCallState.setCurrentCallState( |
| 129 | CdmaPhoneCallState.PhoneCallState.SINGLE_ACTIVE); |
| 130 | } else { |
| 131 | // This is the second outgoing call. Set the Phone Call State to 3WAY |
| 132 | app.cdmaPhoneCallState.setCurrentCallState( |
| 133 | CdmaPhoneCallState.PhoneCallState.THRWAY_ACTIVE); |
Santos Cordon | ad1ed6d | 2013-09-16 03:04:23 -0700 | [diff] [blame] | 134 | |
Santos Cordon | da120f4 | 2014-08-06 04:44:34 -0700 | [diff] [blame] | 135 | // TODO: Remove this code. |
Sailesh Nepal | 23d9ed7 | 2014-07-03 09:40:26 -0700 | [diff] [blame] | 136 | //app.getCallModeler().setCdmaOutgoing3WayCall(connection); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Dial the number using the phone passed in. |
| 142 | * |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 143 | * @param context To perform the CallerInfo query. |
| 144 | * @param phone the Phone object. |
| 145 | * @param number to be dialed as requested by the user. This is |
| 146 | * NOT the phone number to connect to. It is used only to build the |
| 147 | * call card and to update the call log. See above for restrictions. |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 148 | * |
| 149 | * @return either CALL_STATUS_DIALED or CALL_STATUS_FAILED |
| 150 | */ |
Hall Liu | 0c2ef0b | 2019-10-31 15:16:38 -0700 | [diff] [blame] | 151 | public static int placeOtaspCall(Context context, Phone phone, String number) { |
| 152 | final Uri gatewayUri = null; |
Santos Cordon | 69a6919 | 2013-08-22 14:25:42 -0700 | [diff] [blame] | 153 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 154 | if (VDBG) { |
| 155 | log("placeCall()... number: '" + number + "'" |
Hall Liu | 0c2ef0b | 2019-10-31 15:16:38 -0700 | [diff] [blame] | 156 | + ", GW:'" + gatewayUri + "'"); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 157 | } else { |
| 158 | log("placeCall()... number: " + toLogSafePhoneNumber(number) |
Hall Liu | 0c2ef0b | 2019-10-31 15:16:38 -0700 | [diff] [blame] | 159 | + ", GW: " + (gatewayUri != null ? "non-null" : "null")); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 160 | } |
| 161 | final PhoneGlobals app = PhoneGlobals.getInstance(); |
| 162 | |
| 163 | boolean useGateway = false; |
Hall Liu | 0c2ef0b | 2019-10-31 15:16:38 -0700 | [diff] [blame] | 164 | Uri contactRef = null; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 165 | |
| 166 | int status = CALL_STATUS_DIALED; |
| 167 | Connection connection; |
| 168 | String numberToDial; |
Hall Liu | 0c2ef0b | 2019-10-31 15:16:38 -0700 | [diff] [blame] | 169 | numberToDial = number; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 170 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 171 | try { |
Tyler Gunn | c651435 | 2015-06-02 21:46:34 -0700 | [diff] [blame] | 172 | connection = app.mCM.dial(phone, numberToDial, VideoProfile.STATE_AUDIO_ONLY); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 173 | } catch (CallStateException ex) { |
| 174 | // CallStateException means a new outgoing call is not currently |
| 175 | // possible: either no more call slots exist, or there's another |
| 176 | // call already in the process of dialing or ringing. |
| 177 | Log.w(LOG_TAG, "Exception from app.mCM.dial()", ex); |
| 178 | return CALL_STATUS_FAILED; |
| 179 | |
| 180 | // Note that it's possible for CallManager.dial() to return |
| 181 | // null *without* throwing an exception; that indicates that |
| 182 | // we dialed an MMI (see below). |
| 183 | } |
| 184 | |
| 185 | int phoneType = phone.getPhoneType(); |
| 186 | |
| 187 | // On GSM phones, null is returned for MMI codes |
| 188 | if (null == connection) { |
Santos Cordon | f77a339 | 2014-07-21 19:48:10 -0700 | [diff] [blame] | 189 | status = CALL_STATUS_FAILED; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 190 | } else { |
| 191 | if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) { |
Santos Cordon | ad1ed6d | 2013-09-16 03:04:23 -0700 | [diff] [blame] | 192 | updateCdmaCallStateOnNewOutgoingCall(app, connection); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 193 | } |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | return status; |
| 197 | } |
| 198 | |
| 199 | /* package */ static String toLogSafePhoneNumber(String number) { |
| 200 | // For unknown number, log empty string. |
| 201 | if (number == null) { |
| 202 | return ""; |
| 203 | } |
| 204 | |
| 205 | if (VDBG) { |
| 206 | // When VDBG is true we emit PII. |
| 207 | return number; |
| 208 | } |
| 209 | |
| 210 | // Do exactly same thing as Uri#toSafeString() does, which will enable us to compare |
| 211 | // sanitized phone numbers. |
| 212 | StringBuilder builder = new StringBuilder(); |
| 213 | for (int i = 0; i < number.length(); i++) { |
| 214 | char c = number.charAt(i); |
| 215 | if (c == '-' || c == '@' || c == '.') { |
| 216 | builder.append(c); |
| 217 | } else { |
| 218 | builder.append('x'); |
| 219 | } |
| 220 | } |
| 221 | return builder.toString(); |
| 222 | } |
| 223 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 224 | /** |
| 225 | * Handle the MMIInitiate message and put up an alert that lets |
| 226 | * the user cancel the operation, if applicable. |
| 227 | * |
| 228 | * @param context context to get strings. |
| 229 | * @param mmiCode the MmiCode object being started. |
| 230 | * @param buttonCallbackMessage message to post when button is clicked. |
| 231 | * @param previousAlert a previous alert used in this activity. |
| 232 | * @return the dialog handle |
| 233 | */ |
| 234 | static Dialog displayMMIInitiate(Context context, |
| 235 | MmiCode mmiCode, |
| 236 | Message buttonCallbackMessage, |
| 237 | Dialog previousAlert) { |
Tyler Gunn | 77ee938 | 2019-10-31 13:08:23 -0700 | [diff] [blame] | 238 | log("displayMMIInitiate: " + Rlog.pii(LOG_TAG, mmiCode.toString())); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 239 | if (previousAlert != null) { |
| 240 | previousAlert.dismiss(); |
| 241 | } |
| 242 | |
| 243 | // The UI paradigm we are using now requests that all dialogs have |
| 244 | // user interaction, and that any other messages to the user should |
| 245 | // be by way of Toasts. |
| 246 | // |
| 247 | // In adhering to this request, all MMI initiating "OK" dialogs |
| 248 | // (non-cancelable MMIs) that end up being closed when the MMI |
| 249 | // completes (thereby showing a completion dialog) are being |
| 250 | // replaced with Toasts. |
| 251 | // |
| 252 | // As a side effect, moving to Toasts for the non-cancelable MMIs |
| 253 | // also means that buttonCallbackMessage (which was tied into "OK") |
| 254 | // is no longer invokable for these dialogs. This is not a problem |
| 255 | // since the only callback messages we supported were for cancelable |
| 256 | // MMIs anyway. |
| 257 | // |
| 258 | // A cancelable MMI is really just a USSD request. The term |
| 259 | // "cancelable" here means that we can cancel the request when the |
| 260 | // system prompts us for a response, NOT while the network is |
| 261 | // processing the MMI request. Any request to cancel a USSD while |
| 262 | // the network is NOT ready for a response may be ignored. |
| 263 | // |
| 264 | // With this in mind, we replace the cancelable alert dialog with |
| 265 | // a progress dialog, displayed until we receive a request from |
| 266 | // the the network. For more information, please see the comments |
| 267 | // in the displayMMIComplete() method below. |
| 268 | // |
| 269 | // Anything that is NOT a USSD request is a normal MMI request, |
| 270 | // which will bring up a toast (desribed above). |
| 271 | |
| 272 | boolean isCancelable = (mmiCode != null) && mmiCode.isCancelable(); |
| 273 | |
| 274 | if (!isCancelable) { |
Tyler Gunn | 13fe249 | 2017-05-22 14:14:20 -0700 | [diff] [blame] | 275 | log("displayMMIInitiate: not a USSD code, displaying status toast."); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 276 | CharSequence text = context.getText(R.string.mmiStarted); |
| 277 | Toast.makeText(context, text, Toast.LENGTH_SHORT) |
| 278 | .show(); |
| 279 | return null; |
| 280 | } else { |
Tyler Gunn | 13fe249 | 2017-05-22 14:14:20 -0700 | [diff] [blame] | 281 | log("displayMMIInitiate: running USSD code, displaying intermediate progress."); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 282 | |
| 283 | // create the indeterminate progress dialog and display it. |
Alex Hills | 779841a | 2015-10-22 13:23:28 -0400 | [diff] [blame] | 284 | ProgressDialog pd = new ProgressDialog(context, THEME); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 285 | pd.setMessage(context.getText(R.string.ussdRunning)); |
| 286 | pd.setCancelable(false); |
| 287 | pd.setIndeterminate(true); |
| 288 | pd.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); |
| 289 | |
| 290 | pd.show(); |
| 291 | |
| 292 | return pd; |
| 293 | } |
| 294 | |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Handle the MMIComplete message and fire off an intent to display |
| 299 | * the message. |
| 300 | * |
| 301 | * @param context context to get strings. |
| 302 | * @param mmiCode MMI result. |
| 303 | * @param previousAlert a previous alert used in this activity. |
| 304 | */ |
| 305 | static void displayMMIComplete(final Phone phone, Context context, final MmiCode mmiCode, |
| 306 | Message dismissCallbackMessage, |
| 307 | AlertDialog previousAlert) { |
| 308 | final PhoneGlobals app = PhoneGlobals.getInstance(); |
| 309 | CharSequence text; |
| 310 | int title = 0; // title for the progress dialog, if needed. |
| 311 | MmiCode.State state = mmiCode.getState(); |
| 312 | |
Tyler Gunn | 13fe249 | 2017-05-22 14:14:20 -0700 | [diff] [blame] | 313 | log("displayMMIComplete: state=" + state); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 314 | |
| 315 | switch (state) { |
| 316 | case PENDING: |
| 317 | // USSD code asking for feedback from user. |
| 318 | text = mmiCode.getMessage(); |
Tyler Gunn | 13fe249 | 2017-05-22 14:14:20 -0700 | [diff] [blame] | 319 | log("displayMMIComplete: using text from PENDING MMI message: '" + text + "'"); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 320 | break; |
| 321 | case CANCELLED: |
| 322 | text = null; |
| 323 | break; |
| 324 | case COMPLETE: |
SongFerngWang | 5ae4f6d | 2018-10-31 22:11:32 +0800 | [diff] [blame] | 325 | PersistableBundle b = null; |
| 326 | if (SubscriptionManager.isValidSubscriptionId(phone.getSubId())) { |
| 327 | b = app.getCarrierConfigForSubId( |
| 328 | phone.getSubId()); |
| 329 | } else { |
| 330 | b = app.getCarrierConfig(); |
| 331 | } |
| 332 | |
| 333 | if (b.getBoolean(CarrierConfigManager.KEY_USE_CALLER_ID_USSD_BOOL)) { |
| 334 | text = SuppServicesUiUtil.handleCallerIdUssdResponse(app, context, phone, |
| 335 | mmiCode); |
| 336 | if (mmiCode.getMessage() != null && !text.equals(mmiCode.getMessage())) { |
| 337 | break; |
| 338 | } |
| 339 | } |
| 340 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 341 | if (app.getPUKEntryActivity() != null) { |
| 342 | // if an attempt to unPUK the device was made, we specify |
| 343 | // the title and the message here. |
| 344 | title = com.android.internal.R.string.PinMmi; |
| 345 | text = context.getText(R.string.puk_unlocked); |
| 346 | break; |
| 347 | } |
| 348 | // All other conditions for the COMPLETE mmi state will cause |
| 349 | // the case to fall through to message logic in common with |
| 350 | // the FAILED case. |
| 351 | |
| 352 | case FAILED: |
| 353 | text = mmiCode.getMessage(); |
Tyler Gunn | 13fe249 | 2017-05-22 14:14:20 -0700 | [diff] [blame] | 354 | log("displayMMIComplete (failed): using text from MMI message: '" + text + "'"); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 355 | break; |
| 356 | default: |
| 357 | throw new IllegalStateException("Unexpected MmiCode state: " + state); |
| 358 | } |
| 359 | |
| 360 | if (previousAlert != null) { |
| 361 | previousAlert.dismiss(); |
| 362 | } |
| 363 | |
| 364 | // Check to see if a UI exists for the PUK activation. If it does |
| 365 | // exist, then it indicates that we're trying to unblock the PUK. |
| 366 | if ((app.getPUKEntryActivity() != null) && (state == MmiCode.State.COMPLETE)) { |
Tyler Gunn | 3fc09c0 | 2020-05-08 16:35:55 -0700 | [diff] [blame] | 367 | log("displaying PUK unblocking progress dialog."); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 368 | |
| 369 | // create the progress dialog, make sure the flags and type are |
| 370 | // set correctly. |
Alex Hills | 779841a | 2015-10-22 13:23:28 -0400 | [diff] [blame] | 371 | ProgressDialog pd = new ProgressDialog(app, THEME); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 372 | pd.setTitle(title); |
| 373 | pd.setMessage(text); |
| 374 | pd.setCancelable(false); |
| 375 | pd.setIndeterminate(true); |
| 376 | pd.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG); |
| 377 | pd.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); |
| 378 | |
| 379 | // display the dialog |
| 380 | pd.show(); |
| 381 | |
| 382 | // indicate to the Phone app that the progress dialog has |
| 383 | // been assigned for the PUK unlock / SIM READY process. |
| 384 | app.setPukEntryProgressDialog(pd); |
| 385 | |
SongFerngWang | dfa107a | 2018-10-29 20:31:58 +0800 | [diff] [blame] | 386 | } else if ((app.getPUKEntryActivity() != null) && (state == MmiCode.State.FAILED)) { |
Youming Ye | ef84f3e | 2019-04-15 14:05:02 -0700 | [diff] [blame] | 387 | createUssdDialog(app, context, text, phone, |
SongFerngWang | dfa107a | 2018-10-29 20:31:58 +0800 | [diff] [blame] | 388 | WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG); |
| 389 | // In case of failure to unlock, we'll need to reset the |
| 390 | // PUK unlock activity, so that the user may try again. |
| 391 | app.setPukEntryActivity(null); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 392 | } else { |
| 393 | // In case of failure to unlock, we'll need to reset the |
| 394 | // PUK unlock activity, so that the user may try again. |
| 395 | if (app.getPUKEntryActivity() != null) { |
| 396 | app.setPukEntryActivity(null); |
| 397 | } |
| 398 | |
| 399 | // A USSD in a pending state means that it is still |
| 400 | // interacting with the user. |
| 401 | if (state != MmiCode.State.PENDING) { |
Youming Ye | ef84f3e | 2019-04-15 14:05:02 -0700 | [diff] [blame] | 402 | createUssdDialog(app, context, text, phone, |
SongFerngWang | dfa107a | 2018-10-29 20:31:58 +0800 | [diff] [blame] | 403 | WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 404 | } else { |
Tyler Gunn | 13fe249 | 2017-05-22 14:14:20 -0700 | [diff] [blame] | 405 | log("displayMMIComplete: USSD code has requested user input. Constructing input " |
| 406 | + "dialog."); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 407 | |
| 408 | // USSD MMI code that is interacting with the user. The |
| 409 | // basic set of steps is this: |
| 410 | // 1. User enters a USSD request |
| 411 | // 2. We recognize the request and displayMMIInitiate |
| 412 | // (above) creates a progress dialog. |
| 413 | // 3. Request returns and we get a PENDING or COMPLETE |
| 414 | // message. |
| 415 | // 4. These MMI messages are caught in the PhoneApp |
| 416 | // (onMMIComplete) and the InCallScreen |
| 417 | // (mHandler.handleMessage) which bring up this dialog |
| 418 | // and closes the original progress dialog, |
| 419 | // respectively. |
| 420 | // 5. If the message is anything other than PENDING, |
| 421 | // we are done, and the alert dialog (directly above) |
| 422 | // displays the outcome. |
| 423 | // 6. If the network is requesting more information from |
| 424 | // the user, the MMI will be in a PENDING state, and |
| 425 | // we display this dialog with the message. |
| 426 | // 7. User input, or cancel requests result in a return |
| 427 | // to step 1. Keep in mind that this is the only |
| 428 | // time that a USSD should be canceled. |
| 429 | |
| 430 | // inflate the layout with the scrolling text area for the dialog. |
Andrew Lee | b3ee4dc | 2015-01-23 17:35:00 -0800 | [diff] [blame] | 431 | ContextThemeWrapper contextThemeWrapper = |
| 432 | new ContextThemeWrapper(context, R.style.DialerAlertDialogTheme); |
| 433 | LayoutInflater inflater = (LayoutInflater) contextThemeWrapper.getSystemService( |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 434 | Context.LAYOUT_INFLATER_SERVICE); |
| 435 | View dialogView = inflater.inflate(R.layout.dialog_ussd_response, null); |
| 436 | |
| 437 | // get the input field. |
| 438 | final EditText inputText = (EditText) dialogView.findViewById(R.id.input_field); |
| 439 | |
| 440 | // specify the dialog's click listener, with SEND and CANCEL logic. |
| 441 | final DialogInterface.OnClickListener mUSSDDialogListener = |
| 442 | new DialogInterface.OnClickListener() { |
| 443 | public void onClick(DialogInterface dialog, int whichButton) { |
| 444 | switch (whichButton) { |
| 445 | case DialogInterface.BUTTON_POSITIVE: |
| 446 | // As per spec 24.080, valid length of ussd string |
| 447 | // is 1 - 160. If length is out of the range then |
| 448 | // display toast message & Cancel MMI operation. |
| 449 | if (inputText.length() < MIN_USSD_LEN |
| 450 | || inputText.length() > MAX_USSD_LEN) { |
| 451 | Toast.makeText(app, |
| 452 | app.getResources().getString(R.string.enter_input, |
| 453 | MIN_USSD_LEN, MAX_USSD_LEN), |
| 454 | Toast.LENGTH_LONG).show(); |
| 455 | if (mmiCode.isCancelable()) { |
| 456 | mmiCode.cancel(); |
| 457 | } |
| 458 | } else { |
| 459 | phone.sendUssdResponse(inputText.getText().toString()); |
| 460 | } |
| 461 | break; |
| 462 | case DialogInterface.BUTTON_NEGATIVE: |
| 463 | if (mmiCode.isCancelable()) { |
| 464 | mmiCode.cancel(); |
| 465 | } |
| 466 | break; |
| 467 | } |
| 468 | } |
| 469 | }; |
| 470 | |
| 471 | // build the dialog |
Sooraj Sasindran | 9dbb288 | 2021-10-19 11:40:34 -0700 | [diff] [blame] | 472 | final AlertDialog newDialog = |
Chinmay Dhodapkar | e4a5a9c | 2022-07-06 21:10:37 +0000 | [diff] [blame] | 473 | new AlertDialog.Builder(contextThemeWrapper) |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 474 | .setMessage(text) |
| 475 | .setView(dialogView) |
| 476 | .setPositiveButton(R.string.send_button, mUSSDDialogListener) |
| 477 | .setNegativeButton(R.string.cancel, mUSSDDialogListener) |
| 478 | .setCancelable(false) |
| 479 | .create(); |
| 480 | |
| 481 | // attach the key listener to the dialog's input field and make |
| 482 | // sure focus is set. |
| 483 | final View.OnKeyListener mUSSDDialogInputListener = |
| 484 | new View.OnKeyListener() { |
| 485 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
| 486 | switch (keyCode) { |
| 487 | case KeyEvent.KEYCODE_CALL: |
| 488 | case KeyEvent.KEYCODE_ENTER: |
| 489 | if(event.getAction() == KeyEvent.ACTION_DOWN) { |
| 490 | phone.sendUssdResponse(inputText.getText().toString()); |
| 491 | newDialog.dismiss(); |
| 492 | } |
| 493 | return true; |
| 494 | } |
| 495 | return false; |
| 496 | } |
| 497 | }; |
| 498 | inputText.setOnKeyListener(mUSSDDialogInputListener); |
| 499 | inputText.requestFocus(); |
| 500 | |
| 501 | // set the window properties of the dialog |
| 502 | newDialog.getWindow().setType( |
| 503 | WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG); |
| 504 | newDialog.getWindow().addFlags( |
| 505 | WindowManager.LayoutParams.FLAG_DIM_BEHIND); |
| 506 | |
| 507 | // now show the dialog! |
| 508 | newDialog.show(); |
Andrew Lee | b3ee4dc | 2015-01-23 17:35:00 -0800 | [diff] [blame] | 509 | |
| 510 | newDialog.getButton(DialogInterface.BUTTON_POSITIVE) |
| 511 | .setTextColor(context.getResources().getColor(R.color.dialer_theme_color)); |
| 512 | newDialog.getButton(DialogInterface.BUTTON_NEGATIVE) |
| 513 | .setTextColor(context.getResources().getColor(R.color.dialer_theme_color)); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 514 | } |
Emily Chai | e450cdc | 2014-03-10 15:10:43 +0800 | [diff] [blame] | 515 | |
| 516 | if (mmiCode.isNetworkInitiatedUssd()) { |
| 517 | playSound(context); |
| 518 | } |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
Emily Chai | e450cdc | 2014-03-10 15:10:43 +0800 | [diff] [blame] | 522 | private static void playSound(Context context) { |
| 523 | AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); |
| 524 | int callsRingerMode = audioManager.getRingerMode(); |
| 525 | |
| 526 | if (callsRingerMode == AudioManager.RINGER_MODE_NORMAL) { |
| 527 | log("playSound : RINGER_MODE_NORMAL"); |
| 528 | try { |
| 529 | Uri notificationUri = RingtoneManager.getDefaultUri( |
| 530 | RingtoneManager.TYPE_NOTIFICATION); |
| 531 | MediaPlayer mediaPlayer = new MediaPlayer(); |
| 532 | mediaPlayer.setDataSource(context, notificationUri); |
| 533 | AudioAttributes aa = new AudioAttributes.Builder() |
| 534 | .setLegacyStreamType(AudioManager.STREAM_NOTIFICATION) |
| 535 | .setUsage(AudioAttributes.USAGE_NOTIFICATION) |
| 536 | .build(); |
| 537 | mediaPlayer.setAudioAttributes(aa); |
| 538 | mediaPlayer.setLooping(false); |
| 539 | mediaPlayer.prepare(); |
| 540 | mediaPlayer.start(); |
| 541 | } catch (IOException e) { |
| 542 | log("playSound exception : " + e); |
| 543 | } |
| 544 | } else if (callsRingerMode == AudioManager.RINGER_MODE_VIBRATE) { |
| 545 | log("playSound : RINGER_MODE_VIBRATE"); |
| 546 | Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); |
| 547 | // Use NotificationManagerService#DEFAULT_VIBRATE_PATTERN if |
| 548 | // R.array.config_defaultNotificationVibePattern is not defined. |
| 549 | long[] pattern = getLongArray(context.getResources(), |
| 550 | R.array.config_defaultNotificationVibePattern, DEFAULT_VIBRATE_PATTERN); |
Grace Jia | 82fcd87 | 2022-04-19 14:09:25 -0700 | [diff] [blame] | 551 | vibrator.vibrate(VibrationEffect.createWaveform(pattern, -1), |
| 552 | new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION) |
| 553 | .build()); |
Emily Chai | e450cdc | 2014-03-10 15:10:43 +0800 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | |
| 557 | private static long[] getLongArray(Resources r, int resid, long[] def) { |
| 558 | int[] ar = r.getIntArray(resid); |
| 559 | if (ar == null) { |
| 560 | return def; |
| 561 | } |
| 562 | final int len = ar.length; |
| 563 | long[] out = new long[len]; |
| 564 | for (int i = 0; i < len; i++) { |
| 565 | out[i] = ar[i]; |
| 566 | } |
| 567 | return out; |
| 568 | } |
| 569 | |
SongFerngWang | 5ae4f6d | 2018-10-31 22:11:32 +0800 | [diff] [blame] | 570 | /** |
| 571 | * It displays the message dialog for user about the mmi code result message. |
| 572 | * |
| 573 | * @param app This is {@link PhoneGlobals} |
| 574 | * @param context Context to get strings. |
| 575 | * @param text This is message's result. |
Youming Ye | ef84f3e | 2019-04-15 14:05:02 -0700 | [diff] [blame] | 576 | * @param phone This is phone to create sssd dialog. |
SongFerngWang | 5ae4f6d | 2018-10-31 22:11:32 +0800 | [diff] [blame] | 577 | * @param windowType The new window type. {@link WindowManager.LayoutParams}. |
| 578 | */ |
| 579 | public static void createUssdDialog(PhoneGlobals app, Context context, CharSequence text, |
Youming Ye | ef84f3e | 2019-04-15 14:05:02 -0700 | [diff] [blame] | 580 | Phone phone, int windowType) { |
SongFerngWang | dfa107a | 2018-10-29 20:31:58 +0800 | [diff] [blame] | 581 | log("displayMMIComplete: MMI code has finished running."); |
| 582 | |
| 583 | log("displayMMIComplete: Extended NW displayMMIInitiate (" + text + ")"); |
| 584 | if (text == null || text.length() == 0) { |
| 585 | return; |
| 586 | } |
| 587 | |
| 588 | // displaying system alert dialog on the screen instead of |
| 589 | // using another activity to display the message. This |
| 590 | // places the message at the forefront of the UI. |
Jack Yu | 52b754d | 2020-03-27 19:39:53 -0700 | [diff] [blame] | 591 | AlertDialog ussdDialog = new AlertDialog.Builder(context, THEME) |
| 592 | .setPositiveButton(R.string.ok, null) |
| 593 | .setCancelable(true) |
| 594 | .setOnDismissListener(new DialogInterface.OnDismissListener() { |
| 595 | @Override |
| 596 | public void onDismiss(DialogInterface dialog) { |
| 597 | sUssdMsg.setLength(0); |
| 598 | } |
| 599 | }) |
| 600 | .create(); |
SongFerngWang | dfa107a | 2018-10-29 20:31:58 +0800 | [diff] [blame] | 601 | |
Jack Yu | 52b754d | 2020-03-27 19:39:53 -0700 | [diff] [blame] | 602 | ussdDialog.getWindow().setType(windowType); |
| 603 | ussdDialog.getWindow().addFlags( |
| 604 | WindowManager.LayoutParams.FLAG_DIM_BEHIND); |
SongFerngWang | dfa107a | 2018-10-29 20:31:58 +0800 | [diff] [blame] | 605 | |
SongFerngWang | dfa107a | 2018-10-29 20:31:58 +0800 | [diff] [blame] | 606 | if (sUssdMsg.length() != 0) { |
Jack Yu | 52b754d | 2020-03-27 19:39:53 -0700 | [diff] [blame] | 607 | sUssdMsg.insert(0, "\n") |
SongFerngWang | dfa107a | 2018-10-29 20:31:58 +0800 | [diff] [blame] | 608 | .insert(0, app.getResources().getString(R.string.ussd_dialog_sep)) |
| 609 | .insert(0, "\n"); |
| 610 | } |
Youming Ye | ef84f3e | 2019-04-15 14:05:02 -0700 | [diff] [blame] | 611 | if (phone != null && phone.getCarrierName() != null) { |
Jack Yu | 52b754d | 2020-03-27 19:39:53 -0700 | [diff] [blame] | 612 | ussdDialog.setTitle(app.getResources().getString(R.string.carrier_mmi_msg_title, |
Youming Ye | ef84f3e | 2019-04-15 14:05:02 -0700 | [diff] [blame] | 613 | phone.getCarrierName())); |
| 614 | } else { |
Jack Yu | 52b754d | 2020-03-27 19:39:53 -0700 | [diff] [blame] | 615 | ussdDialog |
Youming Ye | ef84f3e | 2019-04-15 14:05:02 -0700 | [diff] [blame] | 616 | .setTitle(app.getResources().getString(R.string.default_carrier_mmi_msg_title)); |
| 617 | } |
SongFerngWang | dfa107a | 2018-10-29 20:31:58 +0800 | [diff] [blame] | 618 | sUssdMsg.insert(0, text); |
Jack Yu | 52b754d | 2020-03-27 19:39:53 -0700 | [diff] [blame] | 619 | ussdDialog.setMessage(sUssdMsg.toString()); |
| 620 | ussdDialog.show(); |
SongFerngWang | dfa107a | 2018-10-29 20:31:58 +0800 | [diff] [blame] | 621 | } |
| 622 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 623 | /** |
| 624 | * Cancels the current pending MMI operation, if applicable. |
| 625 | * @return true if we canceled an MMI operation, or false |
| 626 | * if the current pending MMI wasn't cancelable |
| 627 | * or if there was no current pending MMI at all. |
| 628 | * |
| 629 | * @see displayMMIInitiate |
| 630 | */ |
| 631 | static boolean cancelMmiCode(Phone phone) { |
| 632 | List<? extends MmiCode> pendingMmis = phone.getPendingMmiCodes(); |
| 633 | int count = pendingMmis.size(); |
| 634 | if (DBG) log("cancelMmiCode: num pending MMIs = " + count); |
| 635 | |
| 636 | boolean canceled = false; |
| 637 | if (count > 0) { |
| 638 | // assume that we only have one pending MMI operation active at a time. |
| 639 | // I don't think it's possible to enter multiple MMI codes concurrently |
| 640 | // in the phone UI, because during the MMI operation, an Alert panel |
| 641 | // is displayed, which prevents more MMI code from being entered. |
| 642 | MmiCode mmiCode = pendingMmis.get(0); |
| 643 | if (mmiCode.isCancelable()) { |
| 644 | mmiCode.cancel(); |
| 645 | canceled = true; |
| 646 | } |
| 647 | } |
| 648 | return canceled; |
| 649 | } |
| 650 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 651 | |
| 652 | // |
| 653 | // Misc UI policy helper functions |
| 654 | // |
| 655 | |
| 656 | /** |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 657 | * Check if a phone number can be route through a 3rd party |
| 658 | * gateway. The number must be a global phone number in numerical |
| 659 | * form (1-800-666-SEXY won't work). |
| 660 | * |
| 661 | * MMI codes and the like cannot be used as a dial number for the |
| 662 | * gateway either. |
| 663 | * |
| 664 | * @param number To be dialed via a 3rd party gateway. |
| 665 | * @return true If the number can be routed through the 3rd party network. |
| 666 | */ |
Santos Cordon | 69a6919 | 2013-08-22 14:25:42 -0700 | [diff] [blame] | 667 | private static boolean isRoutableViaGateway(String number) { |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 668 | if (TextUtils.isEmpty(number)) { |
| 669 | return false; |
| 670 | } |
| 671 | number = PhoneNumberUtils.stripSeparators(number); |
| 672 | if (!number.equals(PhoneNumberUtils.convertKeypadLettersToDigits(number))) { |
| 673 | return false; |
| 674 | } |
| 675 | number = PhoneNumberUtils.extractNetworkPortion(number); |
| 676 | return PhoneNumberUtils.isGlobalPhoneNumber(number); |
| 677 | } |
| 678 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 679 | /** |
| 680 | * Returns whether the phone is in ECM ("Emergency Callback Mode") or not. |
| 681 | */ |
| 682 | /* package */ static boolean isPhoneInEcm(Phone phone) { |
| 683 | if ((phone != null) && TelephonyCapabilities.supportsEcm(phone)) { |
Shuo Qian | dd21031 | 2017-04-12 22:11:33 +0000 | [diff] [blame] | 684 | return phone.isInEcm(); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 685 | } |
| 686 | return false; |
| 687 | } |
| 688 | |
| 689 | /** |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 690 | * Returns true when the given call is in INCOMING state and there's no foreground phone call, |
| 691 | * meaning the call is the first real incoming call the phone is having. |
| 692 | */ |
| 693 | public static boolean isRealIncomingCall(Call.State state) { |
| 694 | return (state == Call.State.INCOMING && !PhoneGlobals.getInstance().mCM.hasActiveFgCall()); |
| 695 | } |
| 696 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 697 | // |
| 698 | // General phone and call state debugging/testing code |
| 699 | // |
| 700 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 701 | private static void log(String msg) { |
| 702 | Log.d(LOG_TAG, msg); |
| 703 | } |
| 704 | |
Andrew Lee | d5165b0 | 2014-12-05 15:53:58 -0800 | [diff] [blame] | 705 | public static PhoneAccountHandle makePstnPhoneAccountHandle(Phone phone) { |
Grace Jia | ad56af5 | 2023-03-07 20:25:46 +0800 | [diff] [blame] | 706 | if (phone == null) { |
| 707 | return null; |
| 708 | } else { |
| 709 | return makePstnPhoneAccountHandleWithPrefix(phone, "", |
| 710 | false, phone.getUserHandle()); |
| 711 | } |
Andrew Lee | d5165b0 | 2014-12-05 15:53:58 -0800 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | public static PhoneAccountHandle makePstnPhoneAccountHandleWithPrefix( |
Pranav Madapurmath | 78eb6cc | 2022-11-16 17:08:58 +0000 | [diff] [blame] | 715 | Phone phone, String prefix, boolean isEmergency, UserHandle userHandle) { |
Andrew Lee | d5165b0 | 2014-12-05 15:53:58 -0800 | [diff] [blame] | 716 | // TODO: Should use some sort of special hidden flag to decorate this account as |
| 717 | // an emergency-only account |
Brad Ebinger | d3edc22 | 2016-03-23 19:44:38 -0700 | [diff] [blame] | 718 | String id = isEmergency ? EMERGENCY_ACCOUNT_HANDLE_ID : prefix + |
Shuo Qian | 075851e | 2021-10-11 12:59:06 -0700 | [diff] [blame] | 719 | String.valueOf(phone.getSubId()); |
Jack Yu | a29f39c | 2023-03-02 13:29:57 -0800 | [diff] [blame] | 720 | return makePstnPhoneAccountHandleWithId(id, userHandle); |
Nancy Chen | e53a064 | 2015-03-12 11:20:12 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Jack Yu | a29f39c | 2023-03-02 13:29:57 -0800 | [diff] [blame] | 723 | public static PhoneAccountHandle makePstnPhoneAccountHandleWithId( |
| 724 | String id, UserHandle userHandle) { |
Nancy Chen | e53a064 | 2015-03-12 11:20:12 -0700 | [diff] [blame] | 725 | ComponentName pstnConnectionServiceName = getPstnConnectionServiceName(); |
Pranav Madapurmath | 78eb6cc | 2022-11-16 17:08:58 +0000 | [diff] [blame] | 726 | // If user handle is null, resort to default constructor to use phone process's |
| 727 | // user handle |
| 728 | return userHandle == null |
| 729 | ? new PhoneAccountHandle(pstnConnectionServiceName, id) |
| 730 | : new PhoneAccountHandle(pstnConnectionServiceName, id, userHandle); |
Andrew Lee | d5165b0 | 2014-12-05 15:53:58 -0800 | [diff] [blame] | 731 | } |
Stuart Scott | dcf40a9 | 2014-12-09 10:45:01 -0800 | [diff] [blame] | 732 | |
Santos Cordon | f69df96 | 2015-02-03 11:15:19 -0800 | [diff] [blame] | 733 | public static int getSubIdForPhoneAccount(PhoneAccount phoneAccount) { |
Santos Cordon | 21e78ac | 2015-02-06 05:19:29 -0800 | [diff] [blame] | 734 | if (phoneAccount != null |
| 735 | && phoneAccount.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) { |
| 736 | return getSubIdForPhoneAccountHandle(phoneAccount.getAccountHandle()); |
| 737 | } |
| 738 | return SubscriptionManager.INVALID_SUBSCRIPTION_ID; |
| 739 | } |
| 740 | |
| 741 | public static int getSubIdForPhoneAccountHandle(PhoneAccountHandle handle) { |
Nancy Chen | 31f9ba1 | 2016-01-06 11:42:12 -0800 | [diff] [blame] | 742 | Phone phone = getPhoneForPhoneAccountHandle(handle); |
| 743 | if (phone != null) { |
| 744 | return phone.getSubId(); |
Santos Cordon | f69df96 | 2015-02-03 11:15:19 -0800 | [diff] [blame] | 745 | } |
| 746 | return SubscriptionManager.INVALID_SUBSCRIPTION_ID; |
| 747 | } |
| 748 | |
Ta-wei Yen | 845f699 | 2016-06-16 12:53:46 -0700 | [diff] [blame] | 749 | public static Phone getPhoneForPhoneAccountHandle(PhoneAccountHandle handle) { |
Nancy Chen | 31f9ba1 | 2016-01-06 11:42:12 -0800 | [diff] [blame] | 750 | if (handle != null && handle.getComponentName().equals(getPstnConnectionServiceName())) { |
Shuo Qian | 075851e | 2021-10-11 12:59:06 -0700 | [diff] [blame] | 751 | return getPhoneFromSubId(handle.getId()); |
Nancy Chen | 31f9ba1 | 2016-01-06 11:42:12 -0800 | [diff] [blame] | 752 | } |
| 753 | return null; |
| 754 | } |
| 755 | |
Nancy Chen | e53a064 | 2015-03-12 11:20:12 -0700 | [diff] [blame] | 756 | /** |
| 757 | * Determine if a given phone account corresponds to an active SIM |
| 758 | * |
| 759 | * @param sm An instance of the subscription manager so it is not recreated for each calling of |
| 760 | * this method. |
| 761 | * @param handle The handle for the phone account to check |
| 762 | * @return {@code true} If there is an active SIM for this phone account, |
| 763 | * {@code false} otherwise. |
| 764 | */ |
| 765 | public static boolean isPhoneAccountActive(SubscriptionManager sm, PhoneAccountHandle handle) { |
Shuo Qian | 075851e | 2021-10-11 12:59:06 -0700 | [diff] [blame] | 766 | return sm.getActiveSubscriptionInfo(Integer.parseInt(handle.getId())) != null; |
Nancy Chen | e53a064 | 2015-03-12 11:20:12 -0700 | [diff] [blame] | 767 | } |
| 768 | |
Santos Cordon | f69df96 | 2015-02-03 11:15:19 -0800 | [diff] [blame] | 769 | private static ComponentName getPstnConnectionServiceName() { |
Tony Mak | 4c8f3c6 | 2015-12-07 02:30:37 +0000 | [diff] [blame] | 770 | return PSTN_CONNECTION_SERVICE_COMPONENT; |
Santos Cordon | f69df96 | 2015-02-03 11:15:19 -0800 | [diff] [blame] | 771 | } |
| 772 | |
Shuo Qian | 075851e | 2021-10-11 12:59:06 -0700 | [diff] [blame] | 773 | private static Phone getPhoneFromSubId(String subId) { |
| 774 | if (!TextUtils.isEmpty(subId)) { |
Santos Cordon | 80855d5 | 2015-02-10 04:33:36 -0800 | [diff] [blame] | 775 | for (Phone phone : PhoneFactory.getPhones()) { |
Shuo Qian | 075851e | 2021-10-11 12:59:06 -0700 | [diff] [blame] | 776 | String phoneSubId = Integer.toString(phone.getSubId()); |
| 777 | if (subId.equals(phoneSubId)) { |
Santos Cordon | 80855d5 | 2015-02-10 04:33:36 -0800 | [diff] [blame] | 778 | return phone; |
| 779 | } |
| 780 | } |
| 781 | } |
Santos Cordon | 80855d5 | 2015-02-10 04:33:36 -0800 | [diff] [blame] | 782 | return null; |
| 783 | } |
| 784 | |
Stuart Scott | dcf40a9 | 2014-12-09 10:45:01 -0800 | [diff] [blame] | 785 | /** |
| 786 | * Register ICC status for all phones. |
| 787 | */ |
| 788 | static final void registerIccStatus(Handler handler, int event) { |
| 789 | for (Phone phone : PhoneFactory.getPhones()) { |
| 790 | IccCard sim = phone.getIccCard(); |
| 791 | if (sim != null) { |
| 792 | if (VDBG) Log.v(LOG_TAG, "register for ICC status, phone " + phone.getPhoneId()); |
| 793 | sim.registerForNetworkLocked(handler, event, phone); |
| 794 | } |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | /** |
Amit Mahajan | f5d92c8 | 2018-11-02 17:44:40 -0700 | [diff] [blame] | 799 | * Register ICC status for all phones. |
| 800 | */ |
| 801 | static final void registerIccStatus(Handler handler, int event, int phoneId) { |
| 802 | Phone[] phones = PhoneFactory.getPhones(); |
| 803 | IccCard sim = phones[phoneId].getIccCard(); |
| 804 | if (sim != null) { |
| 805 | if (VDBG) { |
| 806 | Log.v(LOG_TAG, "register for ICC status, phone " + phones[phoneId].getPhoneId()); |
| 807 | } |
| 808 | sim.registerForNetworkLocked(handler, event, phones[phoneId]); |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | /** |
| 813 | * Unregister ICC status for a specific phone. |
| 814 | */ |
| 815 | static final void unregisterIccStatus(Handler handler, int phoneId) { |
| 816 | Phone[] phones = PhoneFactory.getPhones(); |
| 817 | IccCard sim = phones[phoneId].getIccCard(); |
| 818 | if (sim != null) { |
| 819 | if (VDBG) { |
| 820 | Log.v(LOG_TAG, "unregister for ICC status, phone " + phones[phoneId].getPhoneId()); |
| 821 | } |
| 822 | sim.unregisterForNetworkLocked(handler); |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | /** |
Stuart Scott | dcf40a9 | 2014-12-09 10:45:01 -0800 | [diff] [blame] | 827 | * Set the radio power on/off state for all phones. |
| 828 | * |
| 829 | * @param enabled true means on, false means off. |
| 830 | */ |
| 831 | static final void setRadioPower(boolean enabled) { |
| 832 | for (Phone phone : PhoneFactory.getPhones()) { |
| 833 | phone.setRadioPower(enabled); |
| 834 | } |
| 835 | } |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 836 | } |