blob: bc6a7ae9b5d42961f06f9d3d3de5865770939f4a [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
Jordan Liuff2ccd72019-07-23 15:54:41 -070019import android.annotation.IntDef;
Ling Ma317906d2022-12-05 15:58:14 -080020import android.annotation.Nullable;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070021import android.app.Activity;
22import android.app.KeyguardManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import android.app.ProgressDialog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import android.content.BroadcastReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070025import android.content.ContentResolver;
26import android.content.Context;
27import android.content.ContextWrapper;
28import android.content.Intent;
29import android.content.IntentFilter;
Brad Ebinger05f52c22019-12-05 13:03:21 -080030import android.content.pm.PackageManager;
Youming Ye47a6adc2018-11-19 15:33:36 -080031import android.content.res.XmlResourceParser;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070032import android.media.AudioManager;
Santos Cordone18902f2016-03-22 17:16:04 -070033import android.net.ConnectivityManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070034import android.net.Uri;
35import android.os.AsyncResult;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070036import android.os.Handler;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070037import android.os.Message;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070038import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070039import android.os.PowerManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.os.SystemProperties;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.preference.PreferenceManager;
Sanket Padawe4c699232016-02-09 11:07:22 -080042import android.provider.Settings;
Meng Wang61307622019-10-23 14:08:43 -070043import android.sysprop.TelephonyProperties;
Tyler Gunn900d8fd2018-09-21 09:22:12 -070044import android.telecom.TelecomManager;
Nathan Haroldf9df6ea2019-03-08 11:54:22 -080045import android.telephony.AnomalyReporter;
Junda Liu12f7d802015-05-01 12:06:44 -070046import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.telephony.ServiceState;
chen xubaf9fe52019-07-02 17:28:24 -070048import android.telephony.SubscriptionInfo;
Andrew Lee385019f2014-11-24 14:19:50 -080049import android.telephony.SubscriptionManager;
Sarah Chin072ffb22022-05-03 17:58:59 -070050import android.telephony.TelephonyCallback;
Hall Liuaa4211e2021-01-20 15:43:39 -080051import android.telephony.TelephonyLocalConnection;
Jack Yu1a2fc352017-07-14 17:14:37 -070052import android.telephony.TelephonyManager;
Ling Ma317906d2022-12-05 15:58:14 -080053import android.util.ArraySet;
Jack Yu1a2fc352017-07-14 17:14:37 -070054import android.util.LocalLog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055import android.util.Log;
Santos Cordone18902f2016-03-22 17:16:04 -070056import android.widget.Toast;
Ta-wei Yenb29425b2016-09-21 17:28:14 -070057
Brad Ebingere3ae65a2020-09-11 12:45:11 -070058import com.android.ims.ImsFeatureBinderRepository;
Siim Sammul182d61b2021-05-04 16:01:42 +010059import com.android.internal.os.BinderCallsStats;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060import com.android.internal.telephony.CallManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import com.android.internal.telephony.IccCardConstants;
62import com.android.internal.telephony.MmiCode;
63import com.android.internal.telephony.Phone;
Sarah Chin072ffb22022-05-03 17:58:59 -070064import com.android.internal.telephony.PhoneConfigurationManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065import com.android.internal.telephony.PhoneConstants;
66import com.android.internal.telephony.PhoneFactory;
Jack Yu1a2fc352017-07-14 17:14:37 -070067import com.android.internal.telephony.SettingsObserver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068import com.android.internal.telephony.TelephonyCapabilities;
Youming Ye47a6adc2018-11-19 15:33:36 -080069import com.android.internal.telephony.TelephonyComponentFactory;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070import com.android.internal.telephony.TelephonyIntents;
Jack Yu3e2ac6e2022-03-26 13:59:45 -070071import com.android.internal.telephony.data.DataEvaluation.DataDisallowedReason;
Brad Ebinger05f52c22019-12-05 13:03:21 -080072import com.android.internal.telephony.ims.ImsResolver;
Tyler Gunn92479152021-01-20 16:30:10 -080073import com.android.internal.telephony.imsphone.ImsPhone;
74import com.android.internal.telephony.imsphone.ImsPhoneCallTracker;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +000075import com.android.internal.telephony.uicc.UiccPort;
Jordan Liu06bdef12021-03-24 17:25:46 -070076import com.android.internal.telephony.uicc.UiccProfile;
Jack Yu1a2fc352017-07-14 17:14:37 -070077import com.android.internal.util.IndentingPrintWriter;
Andrew Leefb7f92e2015-02-26 16:23:32 -080078import com.android.phone.settings.SettingsConstants;
Ta-wei Yenf0a71bc2017-05-17 12:28:28 -070079import com.android.phone.vvm.CarrierVvmPackageInstalledReceiver;
James.cf Linc9f35a42020-01-15 02:35:22 +080080import com.android.services.telephony.rcs.TelephonyRcsService;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070081
Jack Yu1a2fc352017-07-14 17:14:37 -070082import java.io.FileDescriptor;
83import java.io.PrintWriter;
Jordan Liuff2ccd72019-07-23 15:54:41 -070084import java.lang.annotation.Retention;
85import java.lang.annotation.RetentionPolicy;
chen xubaf9fe52019-07-02 17:28:24 -070086import java.util.List;
Jack Yu1a2fc352017-07-14 17:14:37 -070087
Santos Cordon7d4ddf62013-07-10 11:58:08 -070088/**
89 * Global state for the telephony subsystem when running in the primary
90 * phone process.
91 */
Sailesh Nepalbf900542014-07-15 16:18:32 -070092public class PhoneGlobals extends ContextWrapper {
Jack Yu1a2fc352017-07-14 17:14:37 -070093 public static final String LOG_TAG = "PhoneGlobals";
Santos Cordon7d4ddf62013-07-10 11:58:08 -070094
95 /**
96 * Phone app-wide debug level:
97 * 0 - no debug logging
98 * 1 - normal debug logging if ro.debuggable is set (which is true in
99 * "eng" and "userdebug" builds but not "user" builds)
100 * 2 - ultra-verbose debug logging
101 *
102 * Most individual classes in the phone app have a local DBG constant,
103 * typically set to
104 * (PhoneApp.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1)
105 * or else
106 * (PhoneApp.DBG_LEVEL >= 2)
107 * depending on the desired verbosity.
108 *
109 * ***** DO NOT SUBMIT WITH DBG_LEVEL > 0 *************
110 */
Andrew Lee88b51e22014-10-29 15:48:51 -0700111 public static final int DBG_LEVEL = 0;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700112
113 private static final boolean DBG =
114 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
115 private static final boolean VDBG = (PhoneGlobals.DBG_LEVEL >= 2);
116
117 // Message codes; see mHandler below.
118 private static final int EVENT_SIM_NETWORK_LOCKED = 3;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700119 private static final int EVENT_SIM_STATE_CHANGED = 8;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700120 private static final int EVENT_DATA_ROAMING_DISCONNECTED = 10;
Jordan Liuff2ccd72019-07-23 15:54:41 -0700121 private static final int EVENT_DATA_ROAMING_CONNECTED = 11;
122 private static final int EVENT_DATA_ROAMING_OK = 12;
123 private static final int EVENT_UNSOL_CDMA_INFO_RECORD = 13;
Jordan Liuff2ccd72019-07-23 15:54:41 -0700124 private static final int EVENT_DATA_ROAMING_SETTINGS_CHANGED = 15;
125 private static final int EVENT_MOBILE_DATA_SETTINGS_CHANGED = 16;
Lei Liu863eac02020-06-24 20:12:55 +0800126 private static final int EVENT_CARRIER_CONFIG_CHANGED = 17;
Sarah Chin072ffb22022-05-03 17:58:59 -0700127 private static final int EVENT_MULTI_SIM_CONFIG_CHANGED = 18;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700128
129 // The MMI codes are also used by the InCallScreen.
130 public static final int MMI_INITIATE = 51;
131 public static final int MMI_COMPLETE = 52;
132 public static final int MMI_CANCEL = 53;
133 // Don't use message codes larger than 99 here; those are reserved for
134 // the individual Activities of the Phone UI.
135
Santos Cordone18902f2016-03-22 17:16:04 -0700136 public static final int AIRPLANE_ON = 1;
137 public static final int AIRPLANE_OFF = 0;
138
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700139 /**
140 * Allowable values for the wake lock code.
141 * SLEEP means the device can be put to sleep.
142 * PARTIAL means wake the processor, but we display can be kept off.
143 * FULL means wake both the processor and the display.
144 */
145 public enum WakeState {
146 SLEEP,
147 PARTIAL,
148 FULL
149 }
150
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700151 private static PhoneGlobals sMe;
152
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700153 CallManager mCM;
Santos Cordon63a84242013-07-23 13:32:52 -0700154 CallNotifier notifier;
Santos Cordon63a84242013-07-23 13:32:52 -0700155 NotificationMgr notificationMgr;
James.cf Linc9f35a42020-01-15 02:35:22 +0800156 TelephonyRcsService mTelephonyRcsService;
Andrew Lee9431b832015-03-09 18:46:45 -0700157 public PhoneInterfaceManager phoneMgr;
James.cf Linaf3183c2019-10-24 00:59:00 +0800158 public ImsRcsController imsRcsController;
Hunsuk Choi3b742d62021-10-25 19:48:34 +0000159 public ImsStateCallbackController mImsStateCallbackController;
joonhunshincffb7fc2021-11-28 07:32:01 +0000160 public ImsProvisioningController mImsProvisioningController;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800161 CarrierConfigLoader configLoader;
Santos Cordon63a84242013-07-23 13:32:52 -0700162
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530163 private Phone phoneInEcm;
Santos Cordon63a84242013-07-23 13:32:52 -0700164
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700165 static boolean sVoiceCapable = true;
166
Santos Cordonc593d002015-06-03 15:41:15 -0700167 // TODO: Remove, no longer used.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700168 CdmaPhoneCallState cdmaPhoneCallState;
169
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700170 // The currently-active PUK entry activity and progress dialog.
171 // Normally, these are the Emergency Dialer and the subsequent
172 // progress dialog. null if there is are no such objects in
173 // the foreground.
174 private Activity mPUKEntryActivity;
175 private ProgressDialog mPUKEntryProgressDialog;
176
Jordan Liuff2ccd72019-07-23 15:54:41 -0700177 /** @hide */
178 @Retention(RetentionPolicy.SOURCE)
179 @IntDef(prefix = {"ROAMING_NOTIFICATION_"},
180 value = {
181 ROAMING_NOTIFICATION_NO_NOTIFICATION,
182 ROAMING_NOTIFICATION_CONNECTED,
183 ROAMING_NOTIFICATION_DISCONNECTED})
184 public @interface RoamingNotification {}
185
186 private static final int ROAMING_NOTIFICATION_NO_NOTIFICATION = 0;
187 private static final int ROAMING_NOTIFICATION_CONNECTED = 1;
188 private static final int ROAMING_NOTIFICATION_DISCONNECTED = 2;
189
190 @RoamingNotification
191 private int mPrevRoamingNotification = ROAMING_NOTIFICATION_NO_NOTIFICATION;
Jack Yu953f9352017-05-18 14:41:06 -0700192
Ling Ma317906d2022-12-05 15:58:14 -0800193 /** Operator numerics for which we've shown is-roaming notifications. **/
194 private ArraySet<String> mPrevRoamingOperatorNumerics = new ArraySet<>();
195
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700196 private WakeState mWakeState = WakeState.SLEEP;
197
198 private PowerManager mPowerManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700199 private PowerManager.WakeLock mWakeLock;
200 private PowerManager.WakeLock mPartialWakeLock;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700201 private KeyguardManager mKeyguardManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700202
Jack Yu1a2fc352017-07-14 17:14:37 -0700203 private int mDefaultDataSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
204 private final LocalLog mDataRoamingNotifLog = new LocalLog(50);
205
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700206 // Broadcast receiver for various intent broadcasts (see onCreate())
207 private final BroadcastReceiver mReceiver = new PhoneAppBroadcastReceiver();
208
Ta-wei Yenf0a71bc2017-05-17 12:28:28 -0700209 private final CarrierVvmPackageInstalledReceiver mCarrierVvmPackageInstalledReceiver =
210 new CarrierVvmPackageInstalledReceiver();
211
Jack Yu1a2fc352017-07-14 17:14:37 -0700212 private final SettingsObserver mSettingsObserver;
Siim Sammul182d61b2021-05-04 16:01:42 +0100213 private BinderCallsStats.SettingsObserver mBinderCallsSettingsObserver;
Jack Yu1a2fc352017-07-14 17:14:37 -0700214
Sarah Chin072ffb22022-05-03 17:58:59 -0700215 // Mapping of phone ID to the associated TelephonyCallback. These should be registered without
216 // fine or coarse location since we only use ServiceState for
217 private PhoneAppCallback[] mTelephonyCallbacks;
218
219 private class PhoneAppCallback extends TelephonyCallback implements
220 TelephonyCallback.ServiceStateListener {
221 private final int mSubId;
222
223 PhoneAppCallback(int subId) {
224 mSubId = subId;
225 }
226
227 @Override
228 public void onServiceStateChanged(ServiceState serviceState) {
229 // Note when registering that we should be registering with INCLUDE_LOCATION_DATA_NONE.
230 // PhoneGlobals only uses the state and roaming status, which does not require location.
231 handleServiceStateChanged(serviceState, mSubId);
232 }
233
234 public int getSubId() {
235 return mSubId;
236 }
237 }
238
Nazish Tabassum160d2112019-12-23 17:31:33 +0530239 private static class EventSimStateChangedBag {
240 final int mPhoneId;
241 final String mIccStatus;
242
243 EventSimStateChangedBag(int phoneId, String iccStatus) {
244 mPhoneId = phoneId;
245 mIccStatus = iccStatus;
246 }
247 }
248
Jordan Liu06bdef12021-03-24 17:25:46 -0700249 // Some carrier config settings disable the network lock screen, so we call handleSimLock
250 // when either SIM_LOCK or CARRIER_CONFIG changes so that no matter which one happens first,
251 // we still do the right thing
252 private void handleSimLock(int subType, Phone phone) {
253 PersistableBundle cc = getCarrierConfigForSubId(phone.getSubId());
254 if (!CarrierConfigManager.isConfigForIdentifiedCarrier(cc)) {
255 // If we only have the default carrier config just return, to avoid popping up the
256 // the SIM lock screen when it's disabled by the carrier.
257 Log.i(LOG_TAG, "Not showing 'SIM network unlock' screen. Carrier config not loaded");
258 return;
259 }
260 if (cc.getBoolean(CarrierConfigManager.KEY_IGNORE_SIM_NETWORK_LOCKED_EVENTS_BOOL)) {
261 // Some products don't have the concept of a "SIM network lock"
262 Log.i(LOG_TAG, "Not showing 'SIM network unlock' screen. Disabled by carrier config");
263 return;
264 }
265
266 // if passed in subType is unknown, retrieve it here.
267 if (subType == -1) {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000268 final UiccPort uiccPort = phone.getUiccPort();
269 if (uiccPort == null) {
Jordan Liu06bdef12021-03-24 17:25:46 -0700270 Log.e(LOG_TAG,
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000271 "handleSimLock: uiccPort for phone " + phone.getPhoneId() + " is null");
Jordan Liu06bdef12021-03-24 17:25:46 -0700272 return;
273 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000274 final UiccProfile uiccProfile = uiccPort.getUiccProfile();
Jordan Liu06bdef12021-03-24 17:25:46 -0700275 if (uiccProfile == null) {
276 Log.e(LOG_TAG,
277 "handleSimLock: uiccProfile for phone " + phone.getPhoneId() + " is null");
278 return;
279 }
280 subType = uiccProfile.getApplication(
281 uiccProfile.mCurrentAppType).getPersoSubState().ordinal();
282 }
283 // Normal case: show the "SIM network unlock" PIN entry screen.
284 // The user won't be able to do anything else until
285 // they enter a valid SIM network PIN.
286 Log.i(LOG_TAG, "show sim depersonal panel");
287 IccNetworkDepersonalizationPanel.showDialog(phone, subType);
288 }
289
290 private boolean isSimLocked(Phone phone) {
291 TelephonyManager tm = getSystemService(TelephonyManager.class);
292 return tm.createForSubscriptionId(phone.getSubId()).getSimState()
293 == TelephonyManager.SIM_STATE_NETWORK_LOCKED;
294 }
295
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700296 Handler mHandler = new Handler() {
297 @Override
298 public void handleMessage(Message msg) {
299 PhoneConstants.State phoneState;
Jack Yu1a2fc352017-07-14 17:14:37 -0700300 if (VDBG) Log.v(LOG_TAG, "event=" + msg.what);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700301 switch (msg.what) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700302 // TODO: This event should be handled by the lock screen, just
303 // like the "SIM missing" and "Sim locked" cases (bug 1804111).
304 case EVENT_SIM_NETWORK_LOCKED:
Jordan Liu06bdef12021-03-24 17:25:46 -0700305 int subType = (Integer) ((AsyncResult) msg.obj).result;
306 Phone phone = (Phone) ((AsyncResult) msg.obj).userObj;
307 handleSimLock(subType, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700308 break;
309
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700310 case EVENT_DATA_ROAMING_DISCONNECTED:
Jordan Liuff2ccd72019-07-23 15:54:41 -0700311 notificationMgr.showDataRoamingNotification(msg.arg1, false);
312 break;
313
314 case EVENT_DATA_ROAMING_CONNECTED:
315 notificationMgr.showDataRoamingNotification(msg.arg1, true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700316 break;
317
318 case EVENT_DATA_ROAMING_OK:
Jordan Liuff2ccd72019-07-23 15:54:41 -0700319 notificationMgr.hideDataRoamingNotification();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700320 break;
321
322 case MMI_COMPLETE:
323 onMMIComplete((AsyncResult) msg.obj);
324 break;
325
326 case MMI_CANCEL:
Stuart Scottdcf40a92014-12-09 10:45:01 -0800327 PhoneUtils.cancelMmiCode(mCM.getFgPhone());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700328 break;
329
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700330 case EVENT_SIM_STATE_CHANGED:
331 // Marks the event where the SIM goes into ready state.
332 // Right now, this is only used for the PUK-unlocking
333 // process.
Nazish Tabassum160d2112019-12-23 17:31:33 +0530334 EventSimStateChangedBag bag = (EventSimStateChangedBag)msg.obj;
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700335 if (IccCardConstants.INTENT_VALUE_ICC_READY.equals(bag.mIccStatus)
Nazish Tabassum59126a92020-07-03 21:44:49 +0530336 || IccCardConstants.INTENT_VALUE_ICC_LOADED.equals(bag.mIccStatus)
337 || IccCardConstants.INTENT_VALUE_ICC_NOT_READY.equals(bag.mIccStatus)
338 || IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(bag.mIccStatus)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700339 // when the right event is triggered and there
340 // are UI objects in the foreground, we close
341 // them to display the lock panel.
342 if (mPUKEntryActivity != null) {
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700343 Log.i(LOG_TAG, "Dismiss puk entry activity");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700344 mPUKEntryActivity.finish();
345 mPUKEntryActivity = null;
346 }
347 if (mPUKEntryProgressDialog != null) {
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700348 Log.i(LOG_TAG, "Dismiss puk progress dialog");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700349 mPUKEntryProgressDialog.dismiss();
350 mPUKEntryProgressDialog = null;
351 }
Nazish Tabassum59126a92020-07-03 21:44:49 +0530352 Log.i(LOG_TAG, "Dismissing depersonal panel" + (bag.mIccStatus));
Nazish Tabassum160d2112019-12-23 17:31:33 +0530353 IccNetworkDepersonalizationPanel.dialogDismiss(bag.mPhoneId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700354 }
355 break;
356
357 case EVENT_UNSOL_CDMA_INFO_RECORD:
358 //TODO: handle message here;
359 break;
Jack Yu1a2fc352017-07-14 17:14:37 -0700360 case EVENT_DATA_ROAMING_SETTINGS_CHANGED:
361 case EVENT_MOBILE_DATA_SETTINGS_CHANGED:
362 updateDataRoamingStatus();
363 break;
Lei Liu863eac02020-06-24 20:12:55 +0800364 case EVENT_CARRIER_CONFIG_CHANGED:
365 int subId = (Integer) msg.obj;
366 // The voicemail number could be overridden by carrier config, so need to
367 // refresh the message waiting (voicemail) indicator.
368 refreshMwiIndicator(subId);
Jordan Liu06bdef12021-03-24 17:25:46 -0700369 phone = getPhone(subId);
Sarah Chin072ffb22022-05-03 17:58:59 -0700370 if (phone != null) {
371 if (isSimLocked(phone)) {
372 // pass in subType=-1 so handleSimLock can find the actual subType if
373 // needed. This is safe as valid values for subType are >= 0
374 handleSimLock(-1, phone);
375 }
376 TelephonyManager tm = getSystemService(TelephonyManager.class);
377 PhoneAppCallback callback = mTelephonyCallbacks[phone.getPhoneId()];
378 // TODO: We may need to figure out a way to unregister if subId is invalid
379 tm.createForSubscriptionId(callback.getSubId())
380 .unregisterTelephonyCallback(callback);
381 callback = new PhoneAppCallback(subId);
382 tm.createForSubscriptionId(subId).registerTelephonyCallback(
Ling Ma317906d2022-12-05 15:58:14 -0800383 TelephonyManager.INCLUDE_LOCATION_DATA_COARSE, mHandler::post,
Sarah Chin072ffb22022-05-03 17:58:59 -0700384 callback);
385 mTelephonyCallbacks[phone.getPhoneId()] = callback;
386 }
387 break;
388 case EVENT_MULTI_SIM_CONFIG_CHANGED:
389 int activeModems = (int) ((AsyncResult) msg.obj).result;
390 TelephonyManager tm = getSystemService(TelephonyManager.class);
391 // Unregister all previous callbacks
392 for (int phoneId = 0; phoneId < mTelephonyCallbacks.length; phoneId++) {
393 PhoneAppCallback callback = mTelephonyCallbacks[phoneId];
394 if (callback != null) {
395 tm.createForSubscriptionId(callback.getSubId())
396 .unregisterTelephonyCallback(callback);
397 mTelephonyCallbacks[phoneId] = null;
398 }
399 }
400 // Register callbacks for all active modems
401 for (int phoneId = 0; phoneId < activeModems; phoneId++) {
402 int sub = PhoneFactory.getPhone(phoneId).getSubId();
403 PhoneAppCallback callback = new PhoneAppCallback(sub);
404 tm.createForSubscriptionId(sub).registerTelephonyCallback(
405 TelephonyManager.INCLUDE_LOCATION_DATA_NONE, mHandler::post,
406 callback);
407 mTelephonyCallbacks[phoneId] = callback;
Jordan Liu06bdef12021-03-24 17:25:46 -0700408 }
Lei Liu863eac02020-06-24 20:12:55 +0800409 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700410 }
411 }
412 };
413
414 public PhoneGlobals(Context context) {
415 super(context);
416 sMe = this;
Jack Yu1a2fc352017-07-14 17:14:37 -0700417 mSettingsObserver = new SettingsObserver(context, mHandler);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700418 }
419
420 public void onCreate() {
421 if (VDBG) Log.v(LOG_TAG, "onCreate()...");
422
423 ContentResolver resolver = getContentResolver();
424
Hall Liuaa4211e2021-01-20 15:43:39 -0800425 // Initialize the shim from frameworks/opt/telephony into packages/services/Telephony.
426 TelephonyLocalConnection.setInstance(new LocalConnectionImpl(this));
427
Sarah Chin072ffb22022-05-03 17:58:59 -0700428 TelephonyManager tm = getSystemService(TelephonyManager.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700429 // Cache the "voice capable" flag.
430 // This flag currently comes from a resource (which is
431 // overrideable on a per-product basis):
Sarah Chin072ffb22022-05-03 17:58:59 -0700432 sVoiceCapable = tm.isVoiceCapable();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700433 // ...but this might eventually become a PackageManager "system
434 // feature" instead, in which case we'd do something like:
435 // sVoiceCapable =
436 // getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY_VOICE_CALLS);
437
Stuart Scottdcf40a92014-12-09 10:45:01 -0800438 if (mCM == null) {
Nathan Haroldf9df6ea2019-03-08 11:54:22 -0800439 // Initialize AnomalyReporter early so that it can be used
440 AnomalyReporter.initialize(this);
Nathan Harold0db5c5e2019-01-22 20:18:56 -0800441
Youming Ye47a6adc2018-11-19 15:33:36 -0800442 // Inject telephony component factory if configured using other jars.
443 XmlResourceParser parser = getResources().getXml(R.xml.telephony_injection);
444 TelephonyComponentFactory.getInstance().injectTheComponentFactory(parser);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700445 // Initialize the telephony framework
446 PhoneFactory.makeDefaultPhones(this);
447
Brad Ebinger05f52c22019-12-05 13:03:21 -0800448 // Only bring up ImsResolver if the device supports having an IMS stack.
449 if (getPackageManager().hasSystemFeature(
450 PackageManager.FEATURE_TELEPHONY_IMS)) {
451 // Get the package name of the default IMS implementation.
452 String defaultImsMmtelPackage = getResources().getString(
453 R.string.config_ims_mmtel_package);
454 String defaultImsRcsPackage = getResources().getString(
455 R.string.config_ims_rcs_package);
Brad Ebingerd1947d82021-05-17 20:54:49 +0000456 ImsResolver.make(this, defaultImsMmtelPackage,
Brad Ebingere3ae65a2020-09-11 12:45:11 -0700457 defaultImsRcsPackage, PhoneFactory.getPhones().length,
458 new ImsFeatureBinderRepository());
Brad Ebingerd1947d82021-05-17 20:54:49 +0000459 ImsResolver.getInstance().initialize();
Tyler Gunn92479152021-01-20 16:30:10 -0800460
461 // With the IMS phone created, load static config.xml values from the phone process
462 // so that it can be provided to the ImsPhoneCallTracker.
463 for (Phone p : PhoneFactory.getPhones()) {
464 Phone imsPhone = p.getImsPhone();
465 if (imsPhone != null && imsPhone instanceof ImsPhone) {
466 ImsPhone theImsPhone = (ImsPhone) imsPhone;
467 if (theImsPhone.getCallTracker() instanceof ImsPhoneCallTracker) {
468 ImsPhoneCallTracker ict = (ImsPhoneCallTracker)
469 theImsPhone.getCallTracker();
470
471 ImsPhoneCallTracker.Config config = new ImsPhoneCallTracker.Config();
472 config.isD2DCommunicationSupported = getResources().getBoolean(
473 R.bool.config_use_device_to_device_communication);
474 ict.setConfig(config);
475 }
476 }
477 }
Hui Wang068ab862020-10-31 05:12:53 +0000478 RcsProvisioningMonitor.make(this);
Brad Ebinger05f52c22019-12-05 13:03:21 -0800479 }
480
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700481 // Start TelephonyDebugService After the default phone is created.
482 Intent intent = new Intent(this, TelephonyDebugService.class);
483 startService(intent);
484
485 mCM = CallManager.getInstance();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700486
487 // Create the NotificationMgr singleton, which is used to display
488 // status bar icons and control other status bar behavior.
489 notificationMgr = NotificationMgr.init(this);
490
Anthony Lee03ebdfc2015-07-27 08:12:02 -0700491 // Create an instance of CdmaPhoneCallState and initialize it to IDLE
492 cdmaPhoneCallState = new CdmaPhoneCallState();
493 cdmaPhoneCallState.CdmaPhoneCallStateInit();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700494
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700495 // before registering for phone state changes
496 mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
497 mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, LOG_TAG);
498 // lock used to keep the processor awake, when we don't care for the display.
499 mPartialWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK
500 | PowerManager.ON_AFTER_RELEASE, LOG_TAG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700501
502 mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
503
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -0800504 phoneMgr = PhoneInterfaceManager.init(this);
Santos Cordon406c0342013-08-28 00:07:47 -0700505
James.cf Linaf3183c2019-10-24 00:59:00 +0800506 imsRcsController = ImsRcsController.init(this);
507
James.cf Linc9f35a42020-01-15 02:35:22 +0800508 if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY_IMS)) {
Hunsuk Choi3b742d62021-10-25 19:48:34 +0000509 mImsStateCallbackController =
510 ImsStateCallbackController.make(this, PhoneFactory.getPhones().length);
Brad Ebingera68a4972020-01-30 17:31:23 -0800511 mTelephonyRcsService = new TelephonyRcsService(this,
512 PhoneFactory.getPhones().length);
513 mTelephonyRcsService.initialize();
James.cf Linc9f35a42020-01-15 02:35:22 +0800514 imsRcsController.setRcsService(mTelephonyRcsService);
joonhunshincffb7fc2021-11-28 07:32:01 +0000515 mImsProvisioningController =
516 ImsProvisioningController.make(this, PhoneFactory.getPhones().length);
James.cf Linc9f35a42020-01-15 02:35:22 +0800517 }
518
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800519 configLoader = CarrierConfigLoader.init(this);
520
Jack Yu1a2fc352017-07-14 17:14:37 -0700521 // Create the CallNotifier singleton, which handles
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700522 // asynchronous events from the telephony layer (like
523 // launching the incoming-call UI when an incoming call comes
524 // in.)
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800525 notifier = CallNotifier.init(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700526
Stuart Scottdcf40a92014-12-09 10:45:01 -0800527 PhoneUtils.registerIccStatus(mHandler, EVENT_SIM_NETWORK_LOCKED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700528
529 // register for MMI/USSD
530 mCM.registerForMmiComplete(mHandler, MMI_COMPLETE, null);
531
Qiong Liucc3fe812019-09-23 15:25:44 +0800532 // Initialize cell status using current airplane mode.
Taesu Lee62f826d2020-08-10 16:01:18 +0900533 handleAirplaneModeChange(
534 Settings.Global.getInt(
535 getContentResolver(),
536 Settings.Global.AIRPLANE_MODE_ON,
537 AIRPLANE_OFF)
538 == AIRPLANE_ON);
Qiong Liucc3fe812019-09-23 15:25:44 +0800539
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700540 // Register for misc other intent broadcasts.
541 IntentFilter intentFilter =
542 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700543 intentFilter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
544 intentFilter.addAction(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700545 intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
Jack Yu1a2fc352017-07-14 17:14:37 -0700546 intentFilter.addAction(TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED);
547 intentFilter.addAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700548 registerReceiver(mReceiver, intentFilter);
549
Sarah Chin072ffb22022-05-03 17:58:59 -0700550 PhoneConfigurationManager.registerForMultiSimConfigChange(
551 mHandler, EVENT_MULTI_SIM_CONFIG_CHANGED, null);
552
553 mTelephonyCallbacks = new PhoneAppCallback[tm.getSupportedModemCount()];
Fangyuan Li7f5c2752022-06-29 18:02:31 +0800554 if (tm.getSupportedModemCount() > 0) {
555 for (Phone phone : PhoneFactory.getPhones()) {
556 int subId = phone.getSubId();
557 PhoneAppCallback callback = new PhoneAppCallback(subId);
558 tm.createForSubscriptionId(subId).registerTelephonyCallback(
559 TelephonyManager.INCLUDE_LOCATION_DATA_NONE, mHandler::post, callback);
560 mTelephonyCallbacks[phone.getPhoneId()] = callback;
561 }
Sarah Chin072ffb22022-05-03 17:58:59 -0700562 }
Ta-wei Yenf0a71bc2017-05-17 12:28:28 -0700563 mCarrierVvmPackageInstalledReceiver.register(this);
564
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700565 //set the default values for the preferences in the phone.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700566 PreferenceManager.setDefaultValues(this, R.xml.call_feature_setting, false);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700567 }
568
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700569 // XXX pre-load the SimProvider so that it's ready
570 resolver.getType(Uri.parse("content://icc/adn"));
571
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700572 // TODO: Register for Cdma Information Records
573 // phone.registerCdmaInformationRecord(mHandler, EVENT_UNSOL_CDMA_INFO_RECORD, null);
574
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700575 // Read HAC settings and configure audio hardware
576 if (getResources().getBoolean(R.bool.hac_enabled)) {
Stuart Scottdcf40a92014-12-09 10:45:01 -0800577 int hac = android.provider.Settings.System.getInt(
578 getContentResolver(),
579 android.provider.Settings.System.HEARING_AID,
580 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700581 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Jordan Liuf71aeef2019-08-21 11:46:09 -0700582 audioManager.setParameters(
583 SettingsConstants.HAC_KEY + "=" + (hac == SettingsConstants.HAC_ENABLED
584 ? SettingsConstants.HAC_VAL_ON : SettingsConstants.HAC_VAL_OFF));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700585 }
Siim Sammul182d61b2021-05-04 16:01:42 +0100586
587 // Start tracking Binder latency for the phone process.
588 mBinderCallsSettingsObserver = new BinderCallsStats.SettingsObserver(
589 getApplicationContext(),
Siim Sammula6950092021-06-22 17:14:03 +0100590 new BinderCallsStats(
591 new BinderCallsStats.Injector(),
592 com.android.internal.os.BinderLatencyProto.Dims.TELEPHONY));
Santos Cordonff506f52013-11-21 19:13:19 -0800593 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700594
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700595 /**
596 * Returns the singleton instance of the PhoneApp.
597 */
Sailesh Nepal1eaf22b2014-02-22 17:00:49 -0800598 public static PhoneGlobals getInstance() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700599 if (sMe == null) {
600 throw new IllegalStateException("No PhoneGlobals here!");
601 }
602 return sMe;
603 }
604
605 /**
Stuart Scottdcf40a92014-12-09 10:45:01 -0800606 * Returns the default phone.
607 *
Andrew Lee385019f2014-11-24 14:19:50 -0800608 * WARNING: This method should be used carefully, now that there may be multiple phones.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700609 */
Andrew Lee83383e42014-10-31 12:42:28 -0700610 public static Phone getPhone() {
Stuart Scottdcf40a92014-12-09 10:45:01 -0800611 return PhoneFactory.getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700612 }
613
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800614 public static Phone getPhone(int subId) {
615 return PhoneFactory.getPhone(SubscriptionManager.getPhoneId(subId));
Andrew Lee385019f2014-11-24 14:19:50 -0800616 }
617
Santos Cordonde10b752013-09-19 04:11:33 -0700618 /* package */ CallManager getCallManager() {
619 return mCM;
620 }
621
Chris Manton4e9fa912015-06-19 11:26:57 -0700622 public PersistableBundle getCarrierConfig() {
Shishir Agrawald3480e02016-01-25 13:05:49 -0800623 return getCarrierConfigForSubId(SubscriptionManager.getDefaultSubscriptionId());
Jonathan Basseri89b0ab42015-05-01 10:52:40 -0700624 }
625
Chris Manton4e9fa912015-06-19 11:26:57 -0700626 public PersistableBundle getCarrierConfigForSubId(int subId) {
Philip P. Moltmann0079aae2020-03-05 16:24:02 -0800627 return configLoader.getConfigForSubIdWithFeature(subId, getOpPackageName(),
628 getAttributionTag());
Junda Liu605148f2015-04-28 15:23:40 -0700629 }
630
Jack Yu1a2fc352017-07-14 17:14:37 -0700631 private void registerSettingsObserver() {
632 mSettingsObserver.unobserve();
633 String dataRoamingSetting = Settings.Global.DATA_ROAMING;
634 String mobileDataSetting = Settings.Global.MOBILE_DATA;
635 if (TelephonyManager.getDefault().getSimCount() > 1) {
636 int subId = mDefaultDataSubId;
637 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
638 dataRoamingSetting += subId;
639 mobileDataSetting += subId;
640 }
641 }
642
643 // Listen for user data roaming setting changed event
644 mSettingsObserver.observe(Settings.Global.getUriFor(dataRoamingSetting),
645 EVENT_DATA_ROAMING_SETTINGS_CHANGED);
646
647 // Listen for mobile data setting changed event
648 mSettingsObserver.observe(Settings.Global.getUriFor(mobileDataSetting),
649 EVENT_MOBILE_DATA_SETTINGS_CHANGED);
650 }
651
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700652 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700653 * Sets the activity responsible for un-PUK-blocking the device
654 * so that we may close it when we receive a positive result.
655 * mPUKEntryActivity is also used to indicate to the device that
656 * we are trying to un-PUK-lock the phone. In other words, iff
657 * it is NOT null, then we are trying to unlock and waiting for
658 * the SIM to move to READY state.
659 *
660 * @param activity is the activity to close when PUK has
661 * finished unlocking. Can be set to null to indicate the unlock
662 * or SIM READYing process is over.
663 */
664 void setPukEntryActivity(Activity activity) {
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700665 Log.i(LOG_TAG, "setPukEntryActivity - set to " + (activity == null ? "null" : "activity"));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700666 mPUKEntryActivity = activity;
667 }
668
669 Activity getPUKEntryActivity() {
670 return mPUKEntryActivity;
671 }
672
673 /**
674 * Sets the dialog responsible for notifying the user of un-PUK-
675 * blocking - SIM READYing progress, so that we may dismiss it
676 * when we receive a positive result.
677 *
678 * @param dialog indicates the progress dialog informing the user
679 * of the state of the device. Dismissed upon completion of
680 * READYing process
681 */
682 void setPukEntryProgressDialog(ProgressDialog dialog) {
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700683 Log.i(LOG_TAG, "setPukEntryProgressDialog - set to "
684 + (dialog == null ? "null" : "activity"));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700685 mPUKEntryProgressDialog = dialog;
686 }
687
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700688 KeyguardManager getKeyguardManager() {
689 return mKeyguardManager;
690 }
691
692 private void onMMIComplete(AsyncResult r) {
693 if (VDBG) Log.d(LOG_TAG, "onMMIComplete()...");
694 MmiCode mmiCode = (MmiCode) r.result;
Stuart Scottdcf40a92014-12-09 10:45:01 -0800695 PhoneUtils.displayMMIComplete(mmiCode.getPhone(), getInstance(), mmiCode, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700696 }
697
fionaxu80126972017-02-01 17:01:26 -0800698 private void initForNewRadioTechnology() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700699 if (DBG) Log.d(LOG_TAG, "initForNewRadioTechnology...");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700700 notifier.updateCallNotifierRegistrationsAfterRadioTechnologyChange();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700701 }
702
Taesu Lee62f826d2020-08-10 16:01:18 +0900703 private void handleAirplaneModeChange(boolean isAirplaneNewlyOn) {
704 int cellState =
705 Settings.Global.getInt(
706 getContentResolver(), Settings.Global.CELL_ON, PhoneConstants.CELL_ON_FLAG);
Chris Mantona09f3632016-02-09 14:48:42 -0800707 switch (cellState) {
708 case PhoneConstants.CELL_OFF_FLAG:
709 // Airplane mode does not affect the cell radio if user
710 // has turned it off.
711 break;
712 case PhoneConstants.CELL_ON_FLAG:
Taesu Lee62f826d2020-08-10 16:01:18 +0900713 maybeTurnCellOff(isAirplaneNewlyOn);
Chris Mantona09f3632016-02-09 14:48:42 -0800714 break;
715 case PhoneConstants.CELL_OFF_DUE_TO_AIRPLANE_MODE_FLAG:
Taesu Lee62f826d2020-08-10 16:01:18 +0900716 maybeTurnCellOn(isAirplaneNewlyOn);
Chris Mantona09f3632016-02-09 14:48:42 -0800717 break;
718 }
Hall Liu7f56e1a2019-07-10 14:41:14 -0700719 for (Phone phone : PhoneFactory.getPhones()) {
720 phone.getServiceStateTracker().onAirplaneModeChanged(isAirplaneNewlyOn);
721 }
Chris Mantona09f3632016-02-09 14:48:42 -0800722 }
723
724 /*
725 * Returns true if the radio must be turned off when entering airplane mode.
726 */
Taesu Lee62f826d2020-08-10 16:01:18 +0900727 private boolean isCellOffInAirplaneMode() {
728 String airplaneModeRadios =
729 Settings.Global.getString(
730 getContentResolver(), Settings.Global.AIRPLANE_MODE_RADIOS);
Chris Mantona09f3632016-02-09 14:48:42 -0800731 return airplaneModeRadios == null
732 || airplaneModeRadios.contains(Settings.Global.RADIO_CELL);
733 }
734
Taesu Lee62f826d2020-08-10 16:01:18 +0900735 private void setRadioPowerOff() {
Chris Mantona09f3632016-02-09 14:48:42 -0800736 Log.i(LOG_TAG, "Turning radio off - airplane");
Taesu Lee62f826d2020-08-10 16:01:18 +0900737 Settings.Global.putInt(
738 getContentResolver(),
739 Settings.Global.CELL_ON,
740 PhoneConstants.CELL_OFF_DUE_TO_AIRPLANE_MODE_FLAG);
Chris Mantona09f3632016-02-09 14:48:42 -0800741 Settings.Global.putInt(getContentResolver(), Settings.Global.ENABLE_CELLULAR_ON_BOOT, 0);
Taesu Lee62f826d2020-08-10 16:01:18 +0900742 TelephonyProperties.airplane_mode_on(true); // true means int value 1
Chris Mantona09f3632016-02-09 14:48:42 -0800743 PhoneUtils.setRadioPower(false);
Ling Ma317906d2022-12-05 15:58:14 -0800744 clearCacheOnRadioOff();
745 }
746
747 /** Clear fields on power off radio **/
748 private void clearCacheOnRadioOff() {
749 // Re-show is-roaming notifications after APM mode
750 mPrevRoamingOperatorNumerics.clear();
Chris Mantona09f3632016-02-09 14:48:42 -0800751 }
752
Taesu Lee62f826d2020-08-10 16:01:18 +0900753 private void setRadioPowerOn() {
Chris Mantona09f3632016-02-09 14:48:42 -0800754 Log.i(LOG_TAG, "Turning radio on - airplane");
Taesu Lee62f826d2020-08-10 16:01:18 +0900755 Settings.Global.putInt(
756 getContentResolver(), Settings.Global.CELL_ON, PhoneConstants.CELL_ON_FLAG);
757 Settings.Global.putInt(getContentResolver(), Settings.Global.ENABLE_CELLULAR_ON_BOOT, 1);
Meng Wang61307622019-10-23 14:08:43 -0700758 TelephonyProperties.airplane_mode_on(false); // false means int value 0
Chris Mantona09f3632016-02-09 14:48:42 -0800759 PhoneUtils.setRadioPower(true);
760 }
761
Taesu Lee62f826d2020-08-10 16:01:18 +0900762 private void maybeTurnCellOff(boolean isAirplaneNewlyOn) {
Chris Mantona09f3632016-02-09 14:48:42 -0800763 if (isAirplaneNewlyOn) {
Santos Cordone18902f2016-03-22 17:16:04 -0700764 // If we are trying to turn off the radio, make sure there are no active
765 // emergency calls. If there are, switch airplane mode back to off.
Taesu Lee62f826d2020-08-10 16:01:18 +0900766 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Tyler Gunn900d8fd2018-09-21 09:22:12 -0700767
768 if (tm != null && tm.isInEmergencyCall()) {
Santos Cordone18902f2016-03-22 17:16:04 -0700769 // Switch airplane mode back to off.
Jordan Liu0e819b12019-08-28 15:33:05 -0700770 ConnectivityManager cm =
Taesu Lee62f826d2020-08-10 16:01:18 +0900771 (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
Jordan Liu0e819b12019-08-28 15:33:05 -0700772 cm.setAirplaneMode(false);
Santos Cordone18902f2016-03-22 17:16:04 -0700773 Toast.makeText(this, R.string.radio_off_during_emergency_call, Toast.LENGTH_LONG)
774 .show();
775 Log.i(LOG_TAG, "Ignoring airplane mode: emergency call. Turning airplane off");
Taesu Lee62f826d2020-08-10 16:01:18 +0900776 } else if (isCellOffInAirplaneMode()) {
777 setRadioPowerOff();
Santos Cordone18902f2016-03-22 17:16:04 -0700778 } else {
Chris Mantona09f3632016-02-09 14:48:42 -0800779 Log.i(LOG_TAG, "Ignoring airplane mode: settings prevent cell radio power off");
Santos Cordone18902f2016-03-22 17:16:04 -0700780 }
Chris Mantona09f3632016-02-09 14:48:42 -0800781 }
782 }
783
Taesu Lee62f826d2020-08-10 16:01:18 +0900784 private void maybeTurnCellOn(boolean isAirplaneNewlyOn) {
Chris Mantona09f3632016-02-09 14:48:42 -0800785 if (!isAirplaneNewlyOn) {
Taesu Lee62f826d2020-08-10 16:01:18 +0900786 setRadioPowerOn();
Santos Cordone18902f2016-03-22 17:16:04 -0700787 }
788 }
789
Santos Cordon593ab382013-08-06 21:58:23 -0700790 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700791 * Receiver for misc intent broadcasts the Phone app cares about.
792 */
793 private class PhoneAppBroadcastReceiver extends BroadcastReceiver {
794 @Override
795 public void onReceive(Context context, Intent intent) {
796 String action = intent.getAction();
797 if (action.equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) {
Taesu Lee3dc572b2020-08-07 18:16:22 +0900798 boolean airplaneMode = intent.getBooleanExtra("state", false);
Taesu Lee62f826d2020-08-10 16:01:18 +0900799 handleAirplaneModeChange(airplaneMode);
Amit Mahajanf5d92c82018-11-02 17:44:40 -0700800 } else if (action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) {
801 // re-register as it may be a new IccCard
802 int phoneId = intent.getIntExtra(PhoneConstants.PHONE_KEY,
803 SubscriptionManager.INVALID_PHONE_INDEX);
804 if (SubscriptionManager.isValidPhoneId(phoneId)) {
805 PhoneUtils.unregisterIccStatus(mHandler, phoneId);
806 PhoneUtils.registerIccStatus(mHandler, EVENT_SIM_NETWORK_LOCKED, phoneId);
807 }
Nazish Tabassum160d2112019-12-23 17:31:33 +0530808 String iccStatus = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);
809 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SIM_STATE_CHANGED,
810 new EventSimStateChangedBag(phoneId, iccStatus)));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700811 } else if (action.equals(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED)) {
812 String newPhone = intent.getStringExtra(PhoneConstants.PHONE_NAME_KEY);
fionaxu80126972017-02-01 17:01:26 -0800813 Log.d(LOG_TAG, "Radio technology switched. Now " + newPhone + " is active.");
814 initForNewRadioTechnology();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700815 } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) {
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530816 int phoneId = intent.getIntExtra(PhoneConstants.PHONE_KEY, 0);
Srikanth Chintala079912a2016-03-03 16:35:01 +0530817 phoneInEcm = PhoneFactory.getPhone(phoneId);
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530818 Log.d(LOG_TAG, "Emergency Callback Mode. phoneId:" + phoneId);
819 if (phoneInEcm != null) {
820 if (TelephonyCapabilities.supportsEcm(phoneInEcm)) {
821 Log.d(LOG_TAG, "Emergency Callback Mode arrived in PhoneApp.");
822 // Start Emergency Callback Mode service
Daniel Bright30efde42020-01-09 22:11:15 -0800823 if (intent.getBooleanExtra(
824 TelephonyManager.EXTRA_PHONE_IN_ECM_STATE, false)) {
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530825 context.startService(new Intent(context,
826 EmergencyCallbackModeService.class));
827 } else {
828 phoneInEcm = null;
829 }
830 } else {
831 // It doesn't make sense to get ACTION_EMERGENCY_CALLBACK_MODE_CHANGED
832 // on a device that doesn't support ECM in the first place.
833 Log.e(LOG_TAG, "Got ACTION_EMERGENCY_CALLBACK_MODE_CHANGED, but "
834 + "ECM isn't supported for phone: " + phoneInEcm.getPhoneName());
835 phoneInEcm = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700836 }
837 } else {
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530838 Log.w(LOG_TAG, "phoneInEcm is null.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700839 }
Jack Yu1a2fc352017-07-14 17:14:37 -0700840 } else if (action.equals(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED)) {
841 // Roaming status could be overridden by carrier config, so we need to update it.
842 if (VDBG) Log.v(LOG_TAG, "carrier config changed.");
843 updateDataRoamingStatus();
chen xubaf9fe52019-07-02 17:28:24 -0700844 updateLimitedSimFunctionForDualSim();
Lei Liu863eac02020-06-24 20:12:55 +0800845 int subId = intent.getIntExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX,
846 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
847 if (SubscriptionManager.isValidSubscriptionId(subId)) {
848 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CARRIER_CONFIG_CHANGED,
849 new Integer(subId)));
850 }
Jack Yu1a2fc352017-07-14 17:14:37 -0700851 } else if (action.equals(TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED)) {
852 // We also need to pay attention when default data subscription changes.
853 if (VDBG) Log.v(LOG_TAG, "default data sub changed.");
854 mDefaultDataSubId = SubscriptionManager.getDefaultDataSubscriptionId();
855 registerSettingsObserver();
856 Phone phone = getPhone(mDefaultDataSubId);
857 if (phone != null) {
858 updateDataRoamingStatus();
859 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700860 }
861 }
862 }
863
Sarah Chin072ffb22022-05-03 17:58:59 -0700864 private void handleServiceStateChanged(ServiceState serviceState, int subId) {
Jack Yu1a2fc352017-07-14 17:14:37 -0700865 if (VDBG) Log.v(LOG_TAG, "handleServiceStateChanged");
Sarah Chin072ffb22022-05-03 17:58:59 -0700866 int state = serviceState.getState();
867 notificationMgr.updateNetworkSelection(state, subId);
Jack Yu1a2fc352017-07-14 17:14:37 -0700868
Sarah Chin072ffb22022-05-03 17:58:59 -0700869 if (VDBG) {
870 Log.v(LOG_TAG, "subId=" + subId + ", mDefaultDataSubId="
871 + mDefaultDataSubId + ", ss roaming=" + serviceState.getDataRoaming());
872 }
873 if (subId == mDefaultDataSubId) {
Ling Ma317906d2022-12-05 15:58:14 -0800874 updateDataRoamingStatus(serviceState.getOperatorNumeric());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700875 }
876 }
877
Jack Yu1a2fc352017-07-14 17:14:37 -0700878 /**
879 * When roaming, if mobile data cannot be established due to data roaming not enabled, we need
880 * to notify the user so they can enable it through settings. Vise versa if the condition
881 * changes, we need to dismiss the notification.
882 */
883 private void updateDataRoamingStatus() {
Ling Ma317906d2022-12-05 15:58:14 -0800884 updateDataRoamingStatus(null /*roamingOperatorNumeric*/);
885 }
886
887 /**
888 * When roaming, if mobile data cannot be established due to data roaming not enabled, we need
889 * to notify the user so they can enable it through settings. Vise versa if the condition
890 * changes, we need to dismiss the notification.
891 * @param roamingOperatorNumeric The operator numeric for the current roaming. {@code null} if
892 * the current roaming operator numeric didn't change.
893 */
894 private void updateDataRoamingStatus(@Nullable String roamingOperatorNumeric) {
Jack Yu1a2fc352017-07-14 17:14:37 -0700895 if (VDBG) Log.v(LOG_TAG, "updateDataRoamingStatus");
896 Phone phone = getPhone(mDefaultDataSubId);
897 if (phone == null) {
898 Log.w(LOG_TAG, "Can't get phone with sub id = " + mDefaultDataSubId);
899 return;
900 }
901
Jack Yu3e2ac6e2022-03-26 13:59:45 -0700902 boolean dataAllowed;
903 boolean notAllowedDueToRoamingOff;
Sarah Chine04784a2022-10-31 20:32:34 -0700904 List<DataDisallowedReason> reasons = phone.getDataNetworkController()
905 .getInternetDataDisallowedReasons();
Jack Yu7968c6d2022-07-31 00:43:21 -0700906 dataAllowed = reasons.isEmpty();
907 notAllowedDueToRoamingOff = (reasons.size() == 1
908 && reasons.contains(DataDisallowedReason.ROAMING_DISABLED));
Ling Ma317906d2022-12-05 15:58:14 -0800909 mDataRoamingNotifLog.log("dataAllowed=" + dataAllowed + ", reasons=" + reasons
910 + ", roamingOperatorNumeric=" + roamingOperatorNumeric);
911 if (VDBG) {
912 Log.v(LOG_TAG, "dataAllowed=" + dataAllowed + ", reasons=" + reasons
913 + ", roamingOperatorNumeric=" + roamingOperatorNumeric);
914 }
Jack Yu3e2ac6e2022-03-26 13:59:45 -0700915
916 if (!dataAllowed && notAllowedDueToRoamingOff) {
Ling Ma4e15c322022-12-07 11:20:53 -0800917 // Don't show roaming notification if we've already shown for this MccMnc
918 if (roamingOperatorNumeric != null
919 && !mPrevRoamingOperatorNumerics.add(roamingOperatorNumeric)) {
920 Log.d(LOG_TAG, "Skip roaming disconnected notification since already shown in "
921 + "MccMnc " + roamingOperatorNumeric);
922 return;
923 }
Jordan Liuff2ccd72019-07-23 15:54:41 -0700924 // No need to show it again if we never cancelled it explicitly.
925 if (mPrevRoamingNotification == ROAMING_NOTIFICATION_DISCONNECTED) return;
Jack Yu1a2fc352017-07-14 17:14:37 -0700926 // If the only reason of no data is data roaming disabled, then we notify the user
927 // so the user can turn on data roaming.
Jordan Liuff2ccd72019-07-23 15:54:41 -0700928 mPrevRoamingNotification = ROAMING_NOTIFICATION_DISCONNECTED;
Jack Yu1a2fc352017-07-14 17:14:37 -0700929 Log.d(LOG_TAG, "Show roaming disconnected notification");
Jordan Liuff2ccd72019-07-23 15:54:41 -0700930 mDataRoamingNotifLog.log("Show roaming off.");
Pengquan Meng8783d932017-10-16 14:57:40 -0700931 Message msg = mHandler.obtainMessage(EVENT_DATA_ROAMING_DISCONNECTED);
932 msg.arg1 = mDefaultDataSubId;
933 msg.sendToTarget();
Ling Ma317906d2022-12-05 15:58:14 -0800934 } else if (dataAllowed && dataIsNowRoaming(mDefaultDataSubId)) {
Ling Ma4e15c322022-12-07 11:20:53 -0800935 boolean isShowRoamingNotificationEnabled = getCarrierConfigForSubId(mDefaultDataSubId)
936 .getBoolean(CarrierConfigManager
937 .KEY_SHOW_DATA_CONNECTED_ROAMING_NOTIFICATION_BOOL);
938 if (!isShowRoamingNotificationEnabled) return;
939 // Don't show roaming notification if we've already shown for this MccMnc
940 if (roamingOperatorNumeric != null
941 && !mPrevRoamingOperatorNumerics.add(roamingOperatorNumeric)) {
942 Log.d(LOG_TAG, "Skip roaming connected notification since already shown in "
943 + "MccMnc " + roamingOperatorNumeric);
944 return;
945 }
Jordan Liuff2ccd72019-07-23 15:54:41 -0700946 // No need to show it again if we never cancelled it explicitly, or carrier config
947 // indicates this is not needed.
948 if (mPrevRoamingNotification == ROAMING_NOTIFICATION_CONNECTED) return;
949 mPrevRoamingNotification = ROAMING_NOTIFICATION_CONNECTED;
950 Log.d(LOG_TAG, "Show roaming connected notification");
951 mDataRoamingNotifLog.log("Show roaming on.");
952 Message msg = mHandler.obtainMessage(EVENT_DATA_ROAMING_CONNECTED);
953 msg.arg1 = mDefaultDataSubId;
954 msg.sendToTarget();
955 } else if (mPrevRoamingNotification != ROAMING_NOTIFICATION_NO_NOTIFICATION) {
956 // Otherwise we either 1) we are not roaming or 2) roaming is off but ROAMING_DISABLED
957 // is not the only data disable reason. In this case we dismiss the notification we
958 // showed earlier.
959 mPrevRoamingNotification = ROAMING_NOTIFICATION_NO_NOTIFICATION;
960 Log.d(LOG_TAG, "Dismiss roaming notification");
Jack Yu3e2ac6e2022-03-26 13:59:45 -0700961 mDataRoamingNotifLog.log("Hide. data allowed=" + dataAllowed);
Jack Yu1a2fc352017-07-14 17:14:37 -0700962 mHandler.sendEmptyMessage(EVENT_DATA_ROAMING_OK);
963 }
964 }
965
Jordan Liuff2ccd72019-07-23 15:54:41 -0700966 /**
967 *
968 * @param subId to check roaming on
969 * @return whether we have transitioned to dataRoaming
970 */
971 private boolean dataIsNowRoaming(int subId) {
Sarah Chine04784a2022-10-31 20:32:34 -0700972 return getPhone(subId).getServiceState().getDataRoaming();
Jordan Liuff2ccd72019-07-23 15:54:41 -0700973 }
974
chen xubaf9fe52019-07-02 17:28:24 -0700975 private void updateLimitedSimFunctionForDualSim() {
976 if (DBG) Log.d(LOG_TAG, "updateLimitedSimFunctionForDualSim");
977 // check conditions to display limited SIM function notification under dual SIM
978 SubscriptionManager subMgr = (SubscriptionManager) getSystemService(
979 Context.TELEPHONY_SUBSCRIPTION_SERVICE);
980 List<SubscriptionInfo> subList = subMgr.getActiveSubscriptionInfoList(false);
981 if (subList != null && subList.size() > 1) {
982 CarrierConfigManager configMgr = (CarrierConfigManager)
983 getSystemService(Context.CARRIER_CONFIG_SERVICE);
984 for (SubscriptionInfo info : subList) {
985 PersistableBundle b = configMgr.getConfigForSubId(info.getSubscriptionId());
986 if (b != null) {
987 if (b.getBoolean(CarrierConfigManager
988 .KEY_LIMITED_SIM_FUNCTION_NOTIFICATION_FOR_DSDS_BOOL)) {
989 notificationMgr.showLimitedSimFunctionWarningNotification(
990 info.getSubscriptionId(),
991 info.getDisplayName().toString());
992 } else {
993 notificationMgr.dismissLimitedSimFunctionWarningNotification(
994 info.getSubscriptionId());
995 }
996 }
997 }
998 } else {
999 // cancel notifications for all subs
1000 notificationMgr.dismissLimitedSimFunctionWarningNotification(
1001 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
1002 }
1003 notificationMgr.dismissLimitedSimFunctionWarningNotificationForInactiveSubs();
1004
1005 }
1006
Sandeep Kuntade73a6a2014-10-15 18:45:56 +05301007 public Phone getPhoneInEcm() {
1008 return phoneInEcm;
1009 }
1010
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001011 /**
Tyler Gunn9c1071f2014-12-09 10:07:54 -08001012 * Triggers a refresh of the message waiting (voicemail) indicator.
1013 *
1014 * @param subId the subscription id we should refresh the notification for.
1015 */
1016 public void refreshMwiIndicator(int subId) {
1017 notificationMgr.refreshMwi(subId);
1018 }
1019
1020 /**
Pengquan Mengdd9ac822018-09-20 15:25:35 -07001021 * Called when the network selection on the subscription {@code subId} is changed by the user.
1022 *
1023 * @param subId the subscription id.
1024 */
1025 public void onNetworkSelectionChanged(int subId) {
1026 Phone phone = getPhone(subId);
1027 if (phone != null) {
1028 notificationMgr.updateNetworkSelection(phone.getServiceState().getState(), subId);
1029 } else {
1030 Log.w(LOG_TAG, "onNetworkSelectionChanged on null phone, subId: " + subId);
1031 }
1032 }
1033
1034 /**
James.cf Lin4b784aa2021-01-31 03:25:15 +08001035 * @return whether the device supports RCS User Capability Exchange or not.
1036 */
1037 public boolean getDeviceUceEnabled() {
1038 return (mTelephonyRcsService == null) ? false : mTelephonyRcsService.isDeviceUceEnabled();
1039 }
1040
1041 /**
1042 * Set the device supports RCS User Capability Exchange.
1043 * @param isEnabled true if the device supports UCE.
1044 */
1045 public void setDeviceUceEnabled(boolean isEnabled) {
1046 if (mTelephonyRcsService != null) {
1047 mTelephonyRcsService.setDeviceUceEnabled(isEnabled);
1048 }
1049 }
1050
1051 /**
Jack Yu1a2fc352017-07-14 17:14:37 -07001052 * Dump the state of the object, add calls to other objects as desired.
1053 *
1054 * @param fd File descriptor
1055 * @param printWriter Print writer
1056 * @param args Arguments
1057 */
1058 public void dump(FileDescriptor fd, PrintWriter printWriter, String[] args) {
1059 IndentingPrintWriter pw = new IndentingPrintWriter(printWriter, " ");
1060 pw.println("------- PhoneGlobals -------");
1061 pw.increaseIndent();
Jordan Liuff2ccd72019-07-23 15:54:41 -07001062 pw.println("mPrevRoamingNotification=" + mPrevRoamingNotification);
Jack Yu1a2fc352017-07-14 17:14:37 -07001063 pw.println("mDefaultDataSubId=" + mDefaultDataSubId);
1064 pw.println("mDataRoamingNotifLog:");
Keun young Parkdebb8532019-05-30 16:35:27 -07001065 pw.println("isSmsCapable=" + TelephonyManager.from(this).isSmsCapable());
Jack Yu1a2fc352017-07-14 17:14:37 -07001066 pw.increaseIndent();
1067 mDataRoamingNotifLog.dump(fd, pw, args);
1068 pw.decreaseIndent();
Brad Ebinger05f52c22019-12-05 13:03:21 -08001069 pw.println("ImsResolver:");
1070 pw.increaseIndent();
1071 try {
Brad Ebingerd1947d82021-05-17 20:54:49 +00001072 if (ImsResolver.getInstance() != null) ImsResolver.getInstance().dump(fd, pw, args);
Brad Ebinger05f52c22019-12-05 13:03:21 -08001073 } catch (Exception e) {
1074 e.printStackTrace();
1075 }
1076 pw.decreaseIndent();
Brad Ebingera68a4972020-01-30 17:31:23 -08001077 pw.println("RcsService:");
1078 try {
1079 if (mTelephonyRcsService != null) mTelephonyRcsService.dump(fd, pw, args);
1080 } catch (Exception e) {
1081 e.printStackTrace();
1082 }
Hunsuk Choi89bd22c2021-11-01 13:04:54 +00001083 pw.println("ImsStateCallbackController:");
1084 try {
1085 if (mImsStateCallbackController != null) mImsStateCallbackController.dump(pw);
1086 } catch (Exception e) {
1087 e.printStackTrace();
1088 }
Jack Yu1a2fc352017-07-14 17:14:37 -07001089 pw.decreaseIndent();
Ling Ma317906d2022-12-05 15:58:14 -08001090 pw.println("mPrevRoamingOperatorNumerics:" + mPrevRoamingOperatorNumerics);
Jack Yu1a2fc352017-07-14 17:14:37 -07001091 pw.println("------- End PhoneGlobals -------");
1092 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001093}