blob: 1a1c32195c8af74cea74bad579c0f765e34b8971 [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;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070020import android.app.Activity;
21import android.app.KeyguardManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.app.ProgressDialog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import android.content.BroadcastReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import android.content.ContentResolver;
25import android.content.Context;
26import android.content.ContextWrapper;
27import android.content.Intent;
28import android.content.IntentFilter;
Brad Ebinger05f52c22019-12-05 13:03:21 -080029import android.content.pm.PackageManager;
Youming Ye47a6adc2018-11-19 15:33:36 -080030import android.content.res.XmlResourceParser;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070031import android.media.AudioManager;
Santos Cordone18902f2016-03-22 17:16:04 -070032import android.net.ConnectivityManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070033import android.net.Uri;
Brad Ebinger533c6c12017-08-02 11:38:48 -070034import android.net.sip.SipManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070035import android.os.AsyncResult;
Junda Liu605148f2015-04-28 15:23:40 -070036import android.os.Bundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070037import android.os.Handler;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.os.Message;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070039import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.os.PowerManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.os.SystemProperties;
Brad Ebinger3fa43462016-04-12 16:06:48 -070042import android.os.UserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.preference.PreferenceManager;
Sanket Padawe4c699232016-02-09 11:07:22 -080044import android.provider.Settings;
Meng Wang61307622019-10-23 14:08:43 -070045import android.sysprop.TelephonyProperties;
Tyler Gunn900d8fd2018-09-21 09:22:12 -070046import android.telecom.TelecomManager;
Nathan Haroldf9df6ea2019-03-08 11:54:22 -080047import android.telephony.AnomalyReporter;
Junda Liu12f7d802015-05-01 12:06:44 -070048import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070049import android.telephony.ServiceState;
chen xubaf9fe52019-07-02 17:28:24 -070050import android.telephony.SubscriptionInfo;
Andrew Lee385019f2014-11-24 14:19:50 -080051import android.telephony.SubscriptionManager;
Hall Liuaa4211e2021-01-20 15:43:39 -080052import android.telephony.TelephonyLocalConnection;
Jack Yu1a2fc352017-07-14 17:14:37 -070053import android.telephony.TelephonyManager;
Jack Yu311536f2018-11-26 11:20:48 -080054import android.telephony.data.ApnSetting;
Jack Yu1a2fc352017-07-14 17:14:37 -070055import android.util.LocalLog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056import android.util.Log;
Santos Cordone18902f2016-03-22 17:16:04 -070057import android.widget.Toast;
Ta-wei Yenb29425b2016-09-21 17:28:14 -070058
Brad Ebingere3ae65a2020-09-11 12:45:11 -070059import com.android.ims.ImsFeatureBinderRepository;
Siim Sammul182d61b2021-05-04 16:01:42 +010060import com.android.internal.os.BinderCallsStats;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import com.android.internal.telephony.CallManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070062import com.android.internal.telephony.IccCardConstants;
63import com.android.internal.telephony.MmiCode;
64import com.android.internal.telephony.Phone;
65import 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 Yu1a2fc352017-07-14 17:14:37 -070071import com.android.internal.telephony.dataconnection.DataConnectionReasons;
72import com.android.internal.telephony.dataconnection.DataConnectionReasons.DataDisallowedReasonType;
Brad Ebinger05f52c22019-12-05 13:03:21 -080073import com.android.internal.telephony.ims.ImsResolver;
Tyler Gunn92479152021-01-20 16:30:10 -080074import com.android.internal.telephony.imsphone.ImsPhone;
75import com.android.internal.telephony.imsphone.ImsPhoneCallTracker;
Jordan Liu06bdef12021-03-24 17:25:46 -070076import com.android.internal.telephony.uicc.UiccCard;
77import com.android.internal.telephony.uicc.UiccProfile;
Jack Yu1a2fc352017-07-14 17:14:37 -070078import com.android.internal.util.IndentingPrintWriter;
Andrew Leefb7f92e2015-02-26 16:23:32 -080079import com.android.phone.settings.SettingsConstants;
Ta-wei Yenf0a71bc2017-05-17 12:28:28 -070080import com.android.phone.vvm.CarrierVvmPackageInstalledReceiver;
James.cf Linc9f35a42020-01-15 02:35:22 +080081import com.android.services.telephony.rcs.TelephonyRcsService;
Brad Ebinger28626122017-08-10 14:13:35 -070082import com.android.services.telephony.sip.SipAccountRegistry;
Brad Ebinger3fa43462016-04-12 16:06:48 -070083import com.android.services.telephony.sip.SipUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070084
Jack Yu1a2fc352017-07-14 17:14:37 -070085import java.io.FileDescriptor;
86import java.io.PrintWriter;
Jordan Liuff2ccd72019-07-23 15:54:41 -070087import java.lang.annotation.Retention;
88import java.lang.annotation.RetentionPolicy;
chen xubaf9fe52019-07-02 17:28:24 -070089import java.util.List;
Jack Yu1a2fc352017-07-14 17:14:37 -070090
Santos Cordon7d4ddf62013-07-10 11:58:08 -070091/**
92 * Global state for the telephony subsystem when running in the primary
93 * phone process.
94 */
Sailesh Nepalbf900542014-07-15 16:18:32 -070095public class PhoneGlobals extends ContextWrapper {
Jack Yu1a2fc352017-07-14 17:14:37 -070096 public static final String LOG_TAG = "PhoneGlobals";
Santos Cordon7d4ddf62013-07-10 11:58:08 -070097
98 /**
99 * Phone app-wide debug level:
100 * 0 - no debug logging
101 * 1 - normal debug logging if ro.debuggable is set (which is true in
102 * "eng" and "userdebug" builds but not "user" builds)
103 * 2 - ultra-verbose debug logging
104 *
105 * Most individual classes in the phone app have a local DBG constant,
106 * typically set to
107 * (PhoneApp.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1)
108 * or else
109 * (PhoneApp.DBG_LEVEL >= 2)
110 * depending on the desired verbosity.
111 *
112 * ***** DO NOT SUBMIT WITH DBG_LEVEL > 0 *************
113 */
Andrew Lee88b51e22014-10-29 15:48:51 -0700114 public static final int DBG_LEVEL = 0;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700115
116 private static final boolean DBG =
117 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
118 private static final boolean VDBG = (PhoneGlobals.DBG_LEVEL >= 2);
119
120 // Message codes; see mHandler below.
121 private static final int EVENT_SIM_NETWORK_LOCKED = 3;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700122 private static final int EVENT_SIM_STATE_CHANGED = 8;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700123 private static final int EVENT_DATA_ROAMING_DISCONNECTED = 10;
Jordan Liuff2ccd72019-07-23 15:54:41 -0700124 private static final int EVENT_DATA_ROAMING_CONNECTED = 11;
125 private static final int EVENT_DATA_ROAMING_OK = 12;
126 private static final int EVENT_UNSOL_CDMA_INFO_RECORD = 13;
127 private static final int EVENT_RESTART_SIP = 14;
128 private static final int EVENT_DATA_ROAMING_SETTINGS_CHANGED = 15;
129 private static final int EVENT_MOBILE_DATA_SETTINGS_CHANGED = 16;
Lei Liu863eac02020-06-24 20:12:55 +0800130 private static final int EVENT_CARRIER_CONFIG_CHANGED = 17;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700131
132 // The MMI codes are also used by the InCallScreen.
133 public static final int MMI_INITIATE = 51;
134 public static final int MMI_COMPLETE = 52;
135 public static final int MMI_CANCEL = 53;
136 // Don't use message codes larger than 99 here; those are reserved for
137 // the individual Activities of the Phone UI.
138
Santos Cordone18902f2016-03-22 17:16:04 -0700139 public static final int AIRPLANE_ON = 1;
140 public static final int AIRPLANE_OFF = 0;
141
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700142 /**
143 * Allowable values for the wake lock code.
144 * SLEEP means the device can be put to sleep.
145 * PARTIAL means wake the processor, but we display can be kept off.
146 * FULL means wake both the processor and the display.
147 */
148 public enum WakeState {
149 SLEEP,
150 PARTIAL,
151 FULL
152 }
153
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700154 private static PhoneGlobals sMe;
155
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700156 CallManager mCM;
Santos Cordon63a84242013-07-23 13:32:52 -0700157 CallNotifier notifier;
Santos Cordon63a84242013-07-23 13:32:52 -0700158 NotificationMgr notificationMgr;
James.cf Linc9f35a42020-01-15 02:35:22 +0800159 TelephonyRcsService mTelephonyRcsService;
Andrew Lee9431b832015-03-09 18:46:45 -0700160 public PhoneInterfaceManager phoneMgr;
James.cf Linaf3183c2019-10-24 00:59:00 +0800161 public ImsRcsController imsRcsController;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800162 CarrierConfigLoader configLoader;
Santos Cordon63a84242013-07-23 13:32:52 -0700163
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530164 private Phone phoneInEcm;
Santos Cordon63a84242013-07-23 13:32:52 -0700165
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700166 static boolean sVoiceCapable = true;
167
Santos Cordonc593d002015-06-03 15:41:15 -0700168 // TODO: Remove, no longer used.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700169 CdmaPhoneCallState cdmaPhoneCallState;
170
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700171 // The currently-active PUK entry activity and progress dialog.
172 // Normally, these are the Emergency Dialer and the subsequent
173 // progress dialog. null if there is are no such objects in
174 // the foreground.
175 private Activity mPUKEntryActivity;
176 private ProgressDialog mPUKEntryProgressDialog;
177
Jordan Liuff2ccd72019-07-23 15:54:41 -0700178 /** @hide */
179 @Retention(RetentionPolicy.SOURCE)
180 @IntDef(prefix = {"ROAMING_NOTIFICATION_"},
181 value = {
182 ROAMING_NOTIFICATION_NO_NOTIFICATION,
183 ROAMING_NOTIFICATION_CONNECTED,
184 ROAMING_NOTIFICATION_DISCONNECTED})
185 public @interface RoamingNotification {}
186
187 private static final int ROAMING_NOTIFICATION_NO_NOTIFICATION = 0;
188 private static final int ROAMING_NOTIFICATION_CONNECTED = 1;
189 private static final int ROAMING_NOTIFICATION_DISCONNECTED = 2;
190
191 @RoamingNotification
192 private int mPrevRoamingNotification = ROAMING_NOTIFICATION_NO_NOTIFICATION;
Jack Yu953f9352017-05-18 14:41:06 -0700193
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700194 private WakeState mWakeState = WakeState.SLEEP;
195
196 private PowerManager mPowerManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700197 private PowerManager.WakeLock mWakeLock;
198 private PowerManager.WakeLock mPartialWakeLock;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700199 private KeyguardManager mKeyguardManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700200
Jack Yu1a2fc352017-07-14 17:14:37 -0700201 private int mDefaultDataSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
202 private final LocalLog mDataRoamingNotifLog = new LocalLog(50);
203
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700204 // Broadcast receiver for various intent broadcasts (see onCreate())
205 private final BroadcastReceiver mReceiver = new PhoneAppBroadcastReceiver();
Brad Ebinger533c6c12017-08-02 11:38:48 -0700206 // Broadcast receiver for SIP based intents (see onCreate())
Brad Ebinger28626122017-08-10 14:13:35 -0700207 private final SipReceiver mSipReceiver = new SipReceiver();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700208
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
Nazish Tabassum160d2112019-12-23 17:31:33 +0530215 private static class EventSimStateChangedBag {
216 final int mPhoneId;
217 final String mIccStatus;
218
219 EventSimStateChangedBag(int phoneId, String iccStatus) {
220 mPhoneId = phoneId;
221 mIccStatus = iccStatus;
222 }
223 }
224
Jordan Liu06bdef12021-03-24 17:25:46 -0700225 // Some carrier config settings disable the network lock screen, so we call handleSimLock
226 // when either SIM_LOCK or CARRIER_CONFIG changes so that no matter which one happens first,
227 // we still do the right thing
228 private void handleSimLock(int subType, Phone phone) {
229 PersistableBundle cc = getCarrierConfigForSubId(phone.getSubId());
230 if (!CarrierConfigManager.isConfigForIdentifiedCarrier(cc)) {
231 // If we only have the default carrier config just return, to avoid popping up the
232 // the SIM lock screen when it's disabled by the carrier.
233 Log.i(LOG_TAG, "Not showing 'SIM network unlock' screen. Carrier config not loaded");
234 return;
235 }
236 if (cc.getBoolean(CarrierConfigManager.KEY_IGNORE_SIM_NETWORK_LOCKED_EVENTS_BOOL)) {
237 // Some products don't have the concept of a "SIM network lock"
238 Log.i(LOG_TAG, "Not showing 'SIM network unlock' screen. Disabled by carrier config");
239 return;
240 }
241
242 // if passed in subType is unknown, retrieve it here.
243 if (subType == -1) {
244 final UiccCard uiccCard = phone.getUiccCard();
245 if (uiccCard == null) {
246 Log.e(LOG_TAG,
247 "handleSimLock: uiccCard for phone " + phone.getPhoneId() + " is null");
248 return;
249 }
250 final UiccProfile uiccProfile = uiccCard.getUiccProfile();
251 if (uiccProfile == null) {
252 Log.e(LOG_TAG,
253 "handleSimLock: uiccProfile for phone " + phone.getPhoneId() + " is null");
254 return;
255 }
256 subType = uiccProfile.getApplication(
257 uiccProfile.mCurrentAppType).getPersoSubState().ordinal();
258 }
259 // Normal case: show the "SIM network unlock" PIN entry screen.
260 // The user won't be able to do anything else until
261 // they enter a valid SIM network PIN.
262 Log.i(LOG_TAG, "show sim depersonal panel");
263 IccNetworkDepersonalizationPanel.showDialog(phone, subType);
264 }
265
266 private boolean isSimLocked(Phone phone) {
267 TelephonyManager tm = getSystemService(TelephonyManager.class);
268 return tm.createForSubscriptionId(phone.getSubId()).getSimState()
269 == TelephonyManager.SIM_STATE_NETWORK_LOCKED;
270 }
271
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700272 Handler mHandler = new Handler() {
273 @Override
274 public void handleMessage(Message msg) {
275 PhoneConstants.State phoneState;
Jack Yu1a2fc352017-07-14 17:14:37 -0700276 if (VDBG) Log.v(LOG_TAG, "event=" + msg.what);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700277 switch (msg.what) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700278 // TODO: This event should be handled by the lock screen, just
279 // like the "SIM missing" and "Sim locked" cases (bug 1804111).
280 case EVENT_SIM_NETWORK_LOCKED:
Jordan Liu06bdef12021-03-24 17:25:46 -0700281 int subType = (Integer) ((AsyncResult) msg.obj).result;
282 Phone phone = (Phone) ((AsyncResult) msg.obj).userObj;
283 handleSimLock(subType, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700284 break;
285
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700286 case EVENT_DATA_ROAMING_DISCONNECTED:
Jordan Liuff2ccd72019-07-23 15:54:41 -0700287 notificationMgr.showDataRoamingNotification(msg.arg1, false);
288 break;
289
290 case EVENT_DATA_ROAMING_CONNECTED:
291 notificationMgr.showDataRoamingNotification(msg.arg1, true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700292 break;
293
294 case EVENT_DATA_ROAMING_OK:
Jordan Liuff2ccd72019-07-23 15:54:41 -0700295 notificationMgr.hideDataRoamingNotification();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700296 break;
297
298 case MMI_COMPLETE:
299 onMMIComplete((AsyncResult) msg.obj);
300 break;
301
302 case MMI_CANCEL:
Stuart Scottdcf40a92014-12-09 10:45:01 -0800303 PhoneUtils.cancelMmiCode(mCM.getFgPhone());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700304 break;
305
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700306 case EVENT_SIM_STATE_CHANGED:
307 // Marks the event where the SIM goes into ready state.
308 // Right now, this is only used for the PUK-unlocking
309 // process.
Nazish Tabassum160d2112019-12-23 17:31:33 +0530310 EventSimStateChangedBag bag = (EventSimStateChangedBag)msg.obj;
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700311 if (IccCardConstants.INTENT_VALUE_ICC_READY.equals(bag.mIccStatus)
Nazish Tabassum59126a92020-07-03 21:44:49 +0530312 || IccCardConstants.INTENT_VALUE_ICC_LOADED.equals(bag.mIccStatus)
313 || IccCardConstants.INTENT_VALUE_ICC_NOT_READY.equals(bag.mIccStatus)
314 || IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(bag.mIccStatus)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700315 // when the right event is triggered and there
316 // are UI objects in the foreground, we close
317 // them to display the lock panel.
318 if (mPUKEntryActivity != null) {
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700319 Log.i(LOG_TAG, "Dismiss puk entry activity");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700320 mPUKEntryActivity.finish();
321 mPUKEntryActivity = null;
322 }
323 if (mPUKEntryProgressDialog != null) {
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700324 Log.i(LOG_TAG, "Dismiss puk progress dialog");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700325 mPUKEntryProgressDialog.dismiss();
326 mPUKEntryProgressDialog = null;
327 }
Nazish Tabassum59126a92020-07-03 21:44:49 +0530328 Log.i(LOG_TAG, "Dismissing depersonal panel" + (bag.mIccStatus));
Nazish Tabassum160d2112019-12-23 17:31:33 +0530329 IccNetworkDepersonalizationPanel.dialogDismiss(bag.mPhoneId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700330 }
331 break;
332
333 case EVENT_UNSOL_CDMA_INFO_RECORD:
334 //TODO: handle message here;
335 break;
Brad Ebinger3fa43462016-04-12 16:06:48 -0700336 case EVENT_RESTART_SIP:
337 // This should only run if the Phone process crashed and was restarted. We do
338 // not want this running if the device is still in the FBE encrypted state.
Brad Ebinger28626122017-08-10 14:13:35 -0700339 // This is the same procedure that is triggered in the SipIncomingCallReceiver
Brad Ebinger3fa43462016-04-12 16:06:48 -0700340 // upon BOOT_COMPLETED.
Meng Wangc04cd2d2019-10-04 12:00:06 -0700341 UserManager userManager =
342 (UserManager) sMe.getSystemService(Context.USER_SERVICE);
Brad Ebinger3fa43462016-04-12 16:06:48 -0700343 if (userManager != null && userManager.isUserUnlocked()) {
344 SipUtil.startSipService();
345 }
346 break;
Jack Yu1a2fc352017-07-14 17:14:37 -0700347 case EVENT_DATA_ROAMING_SETTINGS_CHANGED:
348 case EVENT_MOBILE_DATA_SETTINGS_CHANGED:
349 updateDataRoamingStatus();
350 break;
Lei Liu863eac02020-06-24 20:12:55 +0800351 case EVENT_CARRIER_CONFIG_CHANGED:
352 int subId = (Integer) msg.obj;
353 // The voicemail number could be overridden by carrier config, so need to
354 // refresh the message waiting (voicemail) indicator.
355 refreshMwiIndicator(subId);
Jordan Liu06bdef12021-03-24 17:25:46 -0700356 phone = getPhone(subId);
357 if (phone != null && isSimLocked(phone)) {
358 // pass in subType=-1 so handleSimLock can find the actual subType if
359 // needed. This is safe as valid values for subType are >= 0
360 handleSimLock(-1, phone);
361 }
Lei Liu863eac02020-06-24 20:12:55 +0800362 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700363 }
364 }
365 };
366
367 public PhoneGlobals(Context context) {
368 super(context);
369 sMe = this;
Jack Yu1a2fc352017-07-14 17:14:37 -0700370 mSettingsObserver = new SettingsObserver(context, mHandler);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700371 }
372
373 public void onCreate() {
374 if (VDBG) Log.v(LOG_TAG, "onCreate()...");
375
376 ContentResolver resolver = getContentResolver();
377
Hall Liuaa4211e2021-01-20 15:43:39 -0800378 // Initialize the shim from frameworks/opt/telephony into packages/services/Telephony.
379 TelephonyLocalConnection.setInstance(new LocalConnectionImpl(this));
380
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700381 // Cache the "voice capable" flag.
382 // This flag currently comes from a resource (which is
383 // overrideable on a per-product basis):
Michele87262742020-01-16 12:23:53 -0800384 sVoiceCapable = ((TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE))
385 .isVoiceCapable();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700386 // ...but this might eventually become a PackageManager "system
387 // feature" instead, in which case we'd do something like:
388 // sVoiceCapable =
389 // getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY_VOICE_CALLS);
390
Stuart Scottdcf40a92014-12-09 10:45:01 -0800391 if (mCM == null) {
Nathan Haroldf9df6ea2019-03-08 11:54:22 -0800392 // Initialize AnomalyReporter early so that it can be used
393 AnomalyReporter.initialize(this);
Nathan Harold0db5c5e2019-01-22 20:18:56 -0800394
Youming Ye47a6adc2018-11-19 15:33:36 -0800395 // Inject telephony component factory if configured using other jars.
396 XmlResourceParser parser = getResources().getXml(R.xml.telephony_injection);
397 TelephonyComponentFactory.getInstance().injectTheComponentFactory(parser);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700398 // Initialize the telephony framework
399 PhoneFactory.makeDefaultPhones(this);
400
Brad Ebinger05f52c22019-12-05 13:03:21 -0800401 // Only bring up ImsResolver if the device supports having an IMS stack.
402 if (getPackageManager().hasSystemFeature(
403 PackageManager.FEATURE_TELEPHONY_IMS)) {
404 // Get the package name of the default IMS implementation.
405 String defaultImsMmtelPackage = getResources().getString(
406 R.string.config_ims_mmtel_package);
407 String defaultImsRcsPackage = getResources().getString(
408 R.string.config_ims_rcs_package);
Brad Ebingerd1947d82021-05-17 20:54:49 +0000409 ImsResolver.make(this, defaultImsMmtelPackage,
Brad Ebingere3ae65a2020-09-11 12:45:11 -0700410 defaultImsRcsPackage, PhoneFactory.getPhones().length,
411 new ImsFeatureBinderRepository());
Brad Ebingerd1947d82021-05-17 20:54:49 +0000412 ImsResolver.getInstance().initialize();
Tyler Gunn92479152021-01-20 16:30:10 -0800413
414 // With the IMS phone created, load static config.xml values from the phone process
415 // so that it can be provided to the ImsPhoneCallTracker.
416 for (Phone p : PhoneFactory.getPhones()) {
417 Phone imsPhone = p.getImsPhone();
418 if (imsPhone != null && imsPhone instanceof ImsPhone) {
419 ImsPhone theImsPhone = (ImsPhone) imsPhone;
420 if (theImsPhone.getCallTracker() instanceof ImsPhoneCallTracker) {
421 ImsPhoneCallTracker ict = (ImsPhoneCallTracker)
422 theImsPhone.getCallTracker();
423
424 ImsPhoneCallTracker.Config config = new ImsPhoneCallTracker.Config();
425 config.isD2DCommunicationSupported = getResources().getBoolean(
426 R.bool.config_use_device_to_device_communication);
427 ict.setConfig(config);
428 }
429 }
430 }
Hui Wang068ab862020-10-31 05:12:53 +0000431 RcsProvisioningMonitor.make(this);
Brad Ebinger05f52c22019-12-05 13:03:21 -0800432 }
433
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700434 // Start TelephonyDebugService After the default phone is created.
435 Intent intent = new Intent(this, TelephonyDebugService.class);
436 startService(intent);
437
438 mCM = CallManager.getInstance();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700439
440 // Create the NotificationMgr singleton, which is used to display
441 // status bar icons and control other status bar behavior.
442 notificationMgr = NotificationMgr.init(this);
443
Brad Ebinger3fa43462016-04-12 16:06:48 -0700444 // If PhoneGlobals has crashed and is being restarted, then restart.
445 mHandler.sendEmptyMessage(EVENT_RESTART_SIP);
446
Anthony Lee03ebdfc2015-07-27 08:12:02 -0700447 // Create an instance of CdmaPhoneCallState and initialize it to IDLE
448 cdmaPhoneCallState = new CdmaPhoneCallState();
449 cdmaPhoneCallState.CdmaPhoneCallStateInit();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700450
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700451 // before registering for phone state changes
452 mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
453 mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, LOG_TAG);
454 // lock used to keep the processor awake, when we don't care for the display.
455 mPartialWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK
456 | PowerManager.ON_AFTER_RELEASE, LOG_TAG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700457
458 mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
459
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -0800460 phoneMgr = PhoneInterfaceManager.init(this);
Santos Cordon406c0342013-08-28 00:07:47 -0700461
James.cf Linaf3183c2019-10-24 00:59:00 +0800462 imsRcsController = ImsRcsController.init(this);
463
James.cf Linc9f35a42020-01-15 02:35:22 +0800464 if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY_IMS)) {
Brad Ebingera68a4972020-01-30 17:31:23 -0800465 mTelephonyRcsService = new TelephonyRcsService(this,
466 PhoneFactory.getPhones().length);
467 mTelephonyRcsService.initialize();
James.cf Linc9f35a42020-01-15 02:35:22 +0800468 imsRcsController.setRcsService(mTelephonyRcsService);
469 }
470
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800471 configLoader = CarrierConfigLoader.init(this);
472
Jack Yu1a2fc352017-07-14 17:14:37 -0700473 // Create the CallNotifier singleton, which handles
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700474 // asynchronous events from the telephony layer (like
475 // launching the incoming-call UI when an incoming call comes
476 // in.)
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800477 notifier = CallNotifier.init(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700478
Stuart Scottdcf40a92014-12-09 10:45:01 -0800479 PhoneUtils.registerIccStatus(mHandler, EVENT_SIM_NETWORK_LOCKED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700480
481 // register for MMI/USSD
482 mCM.registerForMmiComplete(mHandler, MMI_COMPLETE, null);
483
Qiong Liucc3fe812019-09-23 15:25:44 +0800484 // Initialize cell status using current airplane mode.
Taesu Lee62f826d2020-08-10 16:01:18 +0900485 handleAirplaneModeChange(
486 Settings.Global.getInt(
487 getContentResolver(),
488 Settings.Global.AIRPLANE_MODE_ON,
489 AIRPLANE_OFF)
490 == AIRPLANE_ON);
Qiong Liucc3fe812019-09-23 15:25:44 +0800491
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700492 // Register for misc other intent broadcasts.
493 IntentFilter intentFilter =
494 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700495 intentFilter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
496 intentFilter.addAction(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);
497 intentFilter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
498 intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
Jack Yu1a2fc352017-07-14 17:14:37 -0700499 intentFilter.addAction(TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED);
500 intentFilter.addAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700501 registerReceiver(mReceiver, intentFilter);
502
Brad Ebinger533c6c12017-08-02 11:38:48 -0700503 IntentFilter sipIntentFilter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
Brad Ebinger533c6c12017-08-02 11:38:48 -0700504 sipIntentFilter.addAction(SipManager.ACTION_SIP_SERVICE_UP);
505 sipIntentFilter.addAction(SipManager.ACTION_SIP_CALL_OPTION_CHANGED);
Grace Jiab95592a2020-01-15 14:12:24 -0800506 sipIntentFilter.addAction(SipManager.ACTION_SIP_REMOVE_PROFILE);
Brad Ebinger28626122017-08-10 14:13:35 -0700507 registerReceiver(mSipReceiver, sipIntentFilter);
Brad Ebinger533c6c12017-08-02 11:38:48 -0700508
Ta-wei Yenf0a71bc2017-05-17 12:28:28 -0700509 mCarrierVvmPackageInstalledReceiver.register(this);
510
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700511 //set the default values for the preferences in the phone.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700512 PreferenceManager.setDefaultValues(this, R.xml.call_feature_setting, false);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700513 }
514
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700515 // XXX pre-load the SimProvider so that it's ready
516 resolver.getType(Uri.parse("content://icc/adn"));
517
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700518 // TODO: Register for Cdma Information Records
519 // phone.registerCdmaInformationRecord(mHandler, EVENT_UNSOL_CDMA_INFO_RECORD, null);
520
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700521 // Read HAC settings and configure audio hardware
522 if (getResources().getBoolean(R.bool.hac_enabled)) {
Stuart Scottdcf40a92014-12-09 10:45:01 -0800523 int hac = android.provider.Settings.System.getInt(
524 getContentResolver(),
525 android.provider.Settings.System.HEARING_AID,
526 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700527 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Jordan Liuf71aeef2019-08-21 11:46:09 -0700528 audioManager.setParameters(
529 SettingsConstants.HAC_KEY + "=" + (hac == SettingsConstants.HAC_ENABLED
530 ? SettingsConstants.HAC_VAL_ON : SettingsConstants.HAC_VAL_OFF));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700531 }
Siim Sammul182d61b2021-05-04 16:01:42 +0100532
533 // Start tracking Binder latency for the phone process.
534 mBinderCallsSettingsObserver = new BinderCallsStats.SettingsObserver(
535 getApplicationContext(),
Siim Sammula6950092021-06-22 17:14:03 +0100536 new BinderCallsStats(
537 new BinderCallsStats.Injector(),
538 com.android.internal.os.BinderLatencyProto.Dims.TELEPHONY));
Santos Cordonff506f52013-11-21 19:13:19 -0800539 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700540
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700541 /**
542 * Returns the singleton instance of the PhoneApp.
543 */
Sailesh Nepal1eaf22b2014-02-22 17:00:49 -0800544 public static PhoneGlobals getInstance() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700545 if (sMe == null) {
546 throw new IllegalStateException("No PhoneGlobals here!");
547 }
548 return sMe;
549 }
550
551 /**
Stuart Scottdcf40a92014-12-09 10:45:01 -0800552 * Returns the default phone.
553 *
Andrew Lee385019f2014-11-24 14:19:50 -0800554 * WARNING: This method should be used carefully, now that there may be multiple phones.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700555 */
Andrew Lee83383e42014-10-31 12:42:28 -0700556 public static Phone getPhone() {
Stuart Scottdcf40a92014-12-09 10:45:01 -0800557 return PhoneFactory.getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700558 }
559
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800560 public static Phone getPhone(int subId) {
561 return PhoneFactory.getPhone(SubscriptionManager.getPhoneId(subId));
Andrew Lee385019f2014-11-24 14:19:50 -0800562 }
563
Santos Cordonde10b752013-09-19 04:11:33 -0700564 /* package */ CallManager getCallManager() {
565 return mCM;
566 }
567
Chris Manton4e9fa912015-06-19 11:26:57 -0700568 public PersistableBundle getCarrierConfig() {
Shishir Agrawald3480e02016-01-25 13:05:49 -0800569 return getCarrierConfigForSubId(SubscriptionManager.getDefaultSubscriptionId());
Jonathan Basseri89b0ab42015-05-01 10:52:40 -0700570 }
571
Chris Manton4e9fa912015-06-19 11:26:57 -0700572 public PersistableBundle getCarrierConfigForSubId(int subId) {
Philip P. Moltmann0079aae2020-03-05 16:24:02 -0800573 return configLoader.getConfigForSubIdWithFeature(subId, getOpPackageName(),
574 getAttributionTag());
Junda Liu605148f2015-04-28 15:23:40 -0700575 }
576
Jack Yu1a2fc352017-07-14 17:14:37 -0700577 private void registerSettingsObserver() {
578 mSettingsObserver.unobserve();
579 String dataRoamingSetting = Settings.Global.DATA_ROAMING;
580 String mobileDataSetting = Settings.Global.MOBILE_DATA;
581 if (TelephonyManager.getDefault().getSimCount() > 1) {
582 int subId = mDefaultDataSubId;
583 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
584 dataRoamingSetting += subId;
585 mobileDataSetting += subId;
586 }
587 }
588
589 // Listen for user data roaming setting changed event
590 mSettingsObserver.observe(Settings.Global.getUriFor(dataRoamingSetting),
591 EVENT_DATA_ROAMING_SETTINGS_CHANGED);
592
593 // Listen for mobile data setting changed event
594 mSettingsObserver.observe(Settings.Global.getUriFor(mobileDataSetting),
595 EVENT_MOBILE_DATA_SETTINGS_CHANGED);
596 }
597
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700598 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700599 * Sets the activity responsible for un-PUK-blocking the device
600 * so that we may close it when we receive a positive result.
601 * mPUKEntryActivity is also used to indicate to the device that
602 * we are trying to un-PUK-lock the phone. In other words, iff
603 * it is NOT null, then we are trying to unlock and waiting for
604 * the SIM to move to READY state.
605 *
606 * @param activity is the activity to close when PUK has
607 * finished unlocking. Can be set to null to indicate the unlock
608 * or SIM READYing process is over.
609 */
610 void setPukEntryActivity(Activity activity) {
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700611 Log.i(LOG_TAG, "setPukEntryActivity - set to " + (activity == null ? "null" : "activity"));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700612 mPUKEntryActivity = activity;
613 }
614
615 Activity getPUKEntryActivity() {
616 return mPUKEntryActivity;
617 }
618
619 /**
620 * Sets the dialog responsible for notifying the user of un-PUK-
621 * blocking - SIM READYing progress, so that we may dismiss it
622 * when we receive a positive result.
623 *
624 * @param dialog indicates the progress dialog informing the user
625 * of the state of the device. Dismissed upon completion of
626 * READYing process
627 */
628 void setPukEntryProgressDialog(ProgressDialog dialog) {
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700629 Log.i(LOG_TAG, "setPukEntryProgressDialog - set to "
630 + (dialog == null ? "null" : "activity"));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700631 mPUKEntryProgressDialog = dialog;
632 }
633
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700634 KeyguardManager getKeyguardManager() {
635 return mKeyguardManager;
636 }
637
638 private void onMMIComplete(AsyncResult r) {
639 if (VDBG) Log.d(LOG_TAG, "onMMIComplete()...");
640 MmiCode mmiCode = (MmiCode) r.result;
Stuart Scottdcf40a92014-12-09 10:45:01 -0800641 PhoneUtils.displayMMIComplete(mmiCode.getPhone(), getInstance(), mmiCode, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700642 }
643
fionaxu80126972017-02-01 17:01:26 -0800644 private void initForNewRadioTechnology() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700645 if (DBG) Log.d(LOG_TAG, "initForNewRadioTechnology...");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700646 notifier.updateCallNotifierRegistrationsAfterRadioTechnologyChange();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700647 }
648
Taesu Lee62f826d2020-08-10 16:01:18 +0900649 private void handleAirplaneModeChange(boolean isAirplaneNewlyOn) {
650 int cellState =
651 Settings.Global.getInt(
652 getContentResolver(), Settings.Global.CELL_ON, PhoneConstants.CELL_ON_FLAG);
Chris Mantona09f3632016-02-09 14:48:42 -0800653 switch (cellState) {
654 case PhoneConstants.CELL_OFF_FLAG:
655 // Airplane mode does not affect the cell radio if user
656 // has turned it off.
657 break;
658 case PhoneConstants.CELL_ON_FLAG:
Taesu Lee62f826d2020-08-10 16:01:18 +0900659 maybeTurnCellOff(isAirplaneNewlyOn);
Chris Mantona09f3632016-02-09 14:48:42 -0800660 break;
661 case PhoneConstants.CELL_OFF_DUE_TO_AIRPLANE_MODE_FLAG:
Taesu Lee62f826d2020-08-10 16:01:18 +0900662 maybeTurnCellOn(isAirplaneNewlyOn);
Chris Mantona09f3632016-02-09 14:48:42 -0800663 break;
664 }
Hall Liu7f56e1a2019-07-10 14:41:14 -0700665 for (Phone phone : PhoneFactory.getPhones()) {
666 phone.getServiceStateTracker().onAirplaneModeChanged(isAirplaneNewlyOn);
667 }
Chris Mantona09f3632016-02-09 14:48:42 -0800668 }
669
670 /*
671 * Returns true if the radio must be turned off when entering airplane mode.
672 */
Taesu Lee62f826d2020-08-10 16:01:18 +0900673 private boolean isCellOffInAirplaneMode() {
674 String airplaneModeRadios =
675 Settings.Global.getString(
676 getContentResolver(), Settings.Global.AIRPLANE_MODE_RADIOS);
Chris Mantona09f3632016-02-09 14:48:42 -0800677 return airplaneModeRadios == null
678 || airplaneModeRadios.contains(Settings.Global.RADIO_CELL);
679 }
680
Taesu Lee62f826d2020-08-10 16:01:18 +0900681 private void setRadioPowerOff() {
Chris Mantona09f3632016-02-09 14:48:42 -0800682 Log.i(LOG_TAG, "Turning radio off - airplane");
Taesu Lee62f826d2020-08-10 16:01:18 +0900683 Settings.Global.putInt(
684 getContentResolver(),
685 Settings.Global.CELL_ON,
686 PhoneConstants.CELL_OFF_DUE_TO_AIRPLANE_MODE_FLAG);
Chris Mantona09f3632016-02-09 14:48:42 -0800687 Settings.Global.putInt(getContentResolver(), Settings.Global.ENABLE_CELLULAR_ON_BOOT, 0);
Taesu Lee62f826d2020-08-10 16:01:18 +0900688 TelephonyProperties.airplane_mode_on(true); // true means int value 1
Chris Mantona09f3632016-02-09 14:48:42 -0800689 PhoneUtils.setRadioPower(false);
690 }
691
Taesu Lee62f826d2020-08-10 16:01:18 +0900692 private void setRadioPowerOn() {
Chris Mantona09f3632016-02-09 14:48:42 -0800693 Log.i(LOG_TAG, "Turning radio on - airplane");
Taesu Lee62f826d2020-08-10 16:01:18 +0900694 Settings.Global.putInt(
695 getContentResolver(), Settings.Global.CELL_ON, PhoneConstants.CELL_ON_FLAG);
696 Settings.Global.putInt(getContentResolver(), Settings.Global.ENABLE_CELLULAR_ON_BOOT, 1);
Meng Wang61307622019-10-23 14:08:43 -0700697 TelephonyProperties.airplane_mode_on(false); // false means int value 0
Chris Mantona09f3632016-02-09 14:48:42 -0800698 PhoneUtils.setRadioPower(true);
699 }
700
Taesu Lee62f826d2020-08-10 16:01:18 +0900701 private void maybeTurnCellOff(boolean isAirplaneNewlyOn) {
Chris Mantona09f3632016-02-09 14:48:42 -0800702 if (isAirplaneNewlyOn) {
Santos Cordone18902f2016-03-22 17:16:04 -0700703 // If we are trying to turn off the radio, make sure there are no active
704 // emergency calls. If there are, switch airplane mode back to off.
Taesu Lee62f826d2020-08-10 16:01:18 +0900705 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Tyler Gunn900d8fd2018-09-21 09:22:12 -0700706
707 if (tm != null && tm.isInEmergencyCall()) {
Santos Cordone18902f2016-03-22 17:16:04 -0700708 // Switch airplane mode back to off.
Jordan Liu0e819b12019-08-28 15:33:05 -0700709 ConnectivityManager cm =
Taesu Lee62f826d2020-08-10 16:01:18 +0900710 (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
Jordan Liu0e819b12019-08-28 15:33:05 -0700711 cm.setAirplaneMode(false);
Santos Cordone18902f2016-03-22 17:16:04 -0700712 Toast.makeText(this, R.string.radio_off_during_emergency_call, Toast.LENGTH_LONG)
713 .show();
714 Log.i(LOG_TAG, "Ignoring airplane mode: emergency call. Turning airplane off");
Taesu Lee62f826d2020-08-10 16:01:18 +0900715 } else if (isCellOffInAirplaneMode()) {
716 setRadioPowerOff();
Santos Cordone18902f2016-03-22 17:16:04 -0700717 } else {
Chris Mantona09f3632016-02-09 14:48:42 -0800718 Log.i(LOG_TAG, "Ignoring airplane mode: settings prevent cell radio power off");
Santos Cordone18902f2016-03-22 17:16:04 -0700719 }
Chris Mantona09f3632016-02-09 14:48:42 -0800720 }
721 }
722
Taesu Lee62f826d2020-08-10 16:01:18 +0900723 private void maybeTurnCellOn(boolean isAirplaneNewlyOn) {
Chris Mantona09f3632016-02-09 14:48:42 -0800724 if (!isAirplaneNewlyOn) {
Taesu Lee62f826d2020-08-10 16:01:18 +0900725 setRadioPowerOn();
Santos Cordone18902f2016-03-22 17:16:04 -0700726 }
727 }
728
Santos Cordon593ab382013-08-06 21:58:23 -0700729 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700730 * Receiver for misc intent broadcasts the Phone app cares about.
731 */
732 private class PhoneAppBroadcastReceiver extends BroadcastReceiver {
733 @Override
734 public void onReceive(Context context, Intent intent) {
735 String action = intent.getAction();
736 if (action.equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) {
Taesu Lee3dc572b2020-08-07 18:16:22 +0900737 boolean airplaneMode = intent.getBooleanExtra("state", false);
Taesu Lee62f826d2020-08-10 16:01:18 +0900738 handleAirplaneModeChange(airplaneMode);
Amit Mahajanf5d92c82018-11-02 17:44:40 -0700739 } else if (action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) {
740 // re-register as it may be a new IccCard
741 int phoneId = intent.getIntExtra(PhoneConstants.PHONE_KEY,
742 SubscriptionManager.INVALID_PHONE_INDEX);
743 if (SubscriptionManager.isValidPhoneId(phoneId)) {
744 PhoneUtils.unregisterIccStatus(mHandler, phoneId);
745 PhoneUtils.registerIccStatus(mHandler, EVENT_SIM_NETWORK_LOCKED, phoneId);
746 }
Nazish Tabassum160d2112019-12-23 17:31:33 +0530747 String iccStatus = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);
748 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SIM_STATE_CHANGED,
749 new EventSimStateChangedBag(phoneId, iccStatus)));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700750 } else if (action.equals(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED)) {
751 String newPhone = intent.getStringExtra(PhoneConstants.PHONE_NAME_KEY);
fionaxu80126972017-02-01 17:01:26 -0800752 Log.d(LOG_TAG, "Radio technology switched. Now " + newPhone + " is active.");
753 initForNewRadioTechnology();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700754 } else if (action.equals(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED)) {
755 handleServiceStateChanged(intent);
756 } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) {
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530757 int phoneId = intent.getIntExtra(PhoneConstants.PHONE_KEY, 0);
Srikanth Chintala079912a2016-03-03 16:35:01 +0530758 phoneInEcm = PhoneFactory.getPhone(phoneId);
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530759 Log.d(LOG_TAG, "Emergency Callback Mode. phoneId:" + phoneId);
760 if (phoneInEcm != null) {
761 if (TelephonyCapabilities.supportsEcm(phoneInEcm)) {
762 Log.d(LOG_TAG, "Emergency Callback Mode arrived in PhoneApp.");
763 // Start Emergency Callback Mode service
Daniel Bright30efde42020-01-09 22:11:15 -0800764 if (intent.getBooleanExtra(
765 TelephonyManager.EXTRA_PHONE_IN_ECM_STATE, false)) {
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530766 context.startService(new Intent(context,
767 EmergencyCallbackModeService.class));
768 } else {
769 phoneInEcm = null;
770 }
771 } else {
772 // It doesn't make sense to get ACTION_EMERGENCY_CALLBACK_MODE_CHANGED
773 // on a device that doesn't support ECM in the first place.
774 Log.e(LOG_TAG, "Got ACTION_EMERGENCY_CALLBACK_MODE_CHANGED, but "
775 + "ECM isn't supported for phone: " + phoneInEcm.getPhoneName());
776 phoneInEcm = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700777 }
778 } else {
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530779 Log.w(LOG_TAG, "phoneInEcm is null.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700780 }
Jack Yu1a2fc352017-07-14 17:14:37 -0700781 } else if (action.equals(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED)) {
782 // Roaming status could be overridden by carrier config, so we need to update it.
783 if (VDBG) Log.v(LOG_TAG, "carrier config changed.");
784 updateDataRoamingStatus();
chen xubaf9fe52019-07-02 17:28:24 -0700785 updateLimitedSimFunctionForDualSim();
Lei Liu863eac02020-06-24 20:12:55 +0800786 int subId = intent.getIntExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX,
787 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
788 if (SubscriptionManager.isValidSubscriptionId(subId)) {
789 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CARRIER_CONFIG_CHANGED,
790 new Integer(subId)));
791 }
Jack Yu1a2fc352017-07-14 17:14:37 -0700792 } else if (action.equals(TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED)) {
793 // We also need to pay attention when default data subscription changes.
794 if (VDBG) Log.v(LOG_TAG, "default data sub changed.");
795 mDefaultDataSubId = SubscriptionManager.getDefaultDataSubscriptionId();
796 registerSettingsObserver();
797 Phone phone = getPhone(mDefaultDataSubId);
798 if (phone != null) {
799 updateDataRoamingStatus();
800 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700801 }
802 }
803 }
804
Brad Ebinger28626122017-08-10 14:13:35 -0700805 private class SipReceiver extends BroadcastReceiver {
806
807 @Override
808 public void onReceive(Context context, Intent intent) {
809 String action = intent.getAction();
810
811 SipAccountRegistry sipAccountRegistry = SipAccountRegistry.getInstance();
812 if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
813 SipUtil.startSipService();
814 } else if (action.equals(SipManager.ACTION_SIP_SERVICE_UP)
815 || action.equals(SipManager.ACTION_SIP_CALL_OPTION_CHANGED)) {
816 sipAccountRegistry.setup(context);
Grace Jiab95592a2020-01-15 14:12:24 -0800817 } else if (action.equals(SipManager.ACTION_SIP_REMOVE_PROFILE)) {
Brad Ebinger28626122017-08-10 14:13:35 -0700818 if (DBG) {
819 Log.d(LOG_TAG, "SIP_REMOVE_PHONE "
820 + intent.getStringExtra(SipManager.EXTRA_LOCAL_URI));
821 }
822 sipAccountRegistry.removeSipProfile(intent.getStringExtra(
823 SipManager.EXTRA_LOCAL_URI));
824 } else {
825 if (DBG) Log.d(LOG_TAG, "onReceive, action not processed: " + action);
826 }
827 }
828 }
829
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700830 private void handleServiceStateChanged(Intent intent) {
831 /**
832 * This used to handle updating EriTextWidgetProvider this routine
833 * and and listening for ACTION_SERVICE_STATE_CHANGED intents could
834 * be removed. But leaving just in case it might be needed in the near
835 * future.
836 */
837
Jack Yu1a2fc352017-07-14 17:14:37 -0700838 if (VDBG) Log.v(LOG_TAG, "handleServiceStateChanged");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700839 // If service just returned, start sending out the queued messages
Santos Cordonc593d002015-06-03 15:41:15 -0700840 Bundle extras = intent.getExtras();
841 if (extras != null) {
842 ServiceState ss = ServiceState.newFromBundle(extras);
843 if (ss != null) {
844 int state = ss.getState();
Jayachandran C2ef9a482017-05-12 22:07:47 -0700845 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
846 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
847 notificationMgr.updateNetworkSelection(state, subId);
Jack Yu1a2fc352017-07-14 17:14:37 -0700848
849 if (VDBG) {
850 Log.v(LOG_TAG, "subId=" + subId + ",mDefaultDataSubId="
851 + mDefaultDataSubId + ",ss roaming=" + ss.getDataRoaming());
852 }
853 if (subId == mDefaultDataSubId) {
854 updateDataRoamingStatus();
855 }
Santos Cordonc593d002015-06-03 15:41:15 -0700856 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700857 }
858 }
859
Jack Yu1a2fc352017-07-14 17:14:37 -0700860 /**
Jordan Liuff2ccd72019-07-23 15:54:41 -0700861 * @return whether or not we should show a notification when connecting to data roaming if the
862 * user has data roaming enabled
863 */
864 private boolean shouldShowDataConnectedRoaming(int subId) {
865 PersistableBundle config = getCarrierConfigForSubId(subId);
866 return config.getBoolean(CarrierConfigManager
867 .KEY_SHOW_DATA_CONNECTED_ROAMING_NOTIFICATION_BOOL);
868 }
869
870 /**
Jack Yu1a2fc352017-07-14 17:14:37 -0700871 * When roaming, if mobile data cannot be established due to data roaming not enabled, we need
872 * to notify the user so they can enable it through settings. Vise versa if the condition
873 * changes, we need to dismiss the notification.
874 */
875 private void updateDataRoamingStatus() {
876 if (VDBG) Log.v(LOG_TAG, "updateDataRoamingStatus");
877 Phone phone = getPhone(mDefaultDataSubId);
878 if (phone == null) {
879 Log.w(LOG_TAG, "Can't get phone with sub id = " + mDefaultDataSubId);
880 return;
881 }
882
883 DataConnectionReasons reasons = new DataConnectionReasons();
Jack Yu311536f2018-11-26 11:20:48 -0800884 boolean dataAllowed = phone.isDataAllowed(ApnSetting.TYPE_DEFAULT, reasons);
Jack Yu1a2fc352017-07-14 17:14:37 -0700885 mDataRoamingNotifLog.log("dataAllowed=" + dataAllowed + ", reasons=" + reasons);
886 if (VDBG) Log.v(LOG_TAG, "dataAllowed=" + dataAllowed + ", reasons=" + reasons);
Jordan Liuff2ccd72019-07-23 15:54:41 -0700887 if (!dataAllowed && reasons.containsOnly(DataDisallowedReasonType.ROAMING_DISABLED)) {
888 // No need to show it again if we never cancelled it explicitly.
889 if (mPrevRoamingNotification == ROAMING_NOTIFICATION_DISCONNECTED) return;
Jack Yu1a2fc352017-07-14 17:14:37 -0700890 // If the only reason of no data is data roaming disabled, then we notify the user
891 // so the user can turn on data roaming.
Jordan Liuff2ccd72019-07-23 15:54:41 -0700892 mPrevRoamingNotification = ROAMING_NOTIFICATION_DISCONNECTED;
Jack Yu1a2fc352017-07-14 17:14:37 -0700893 Log.d(LOG_TAG, "Show roaming disconnected notification");
Jordan Liuff2ccd72019-07-23 15:54:41 -0700894 mDataRoamingNotifLog.log("Show roaming off.");
Pengquan Meng8783d932017-10-16 14:57:40 -0700895 Message msg = mHandler.obtainMessage(EVENT_DATA_ROAMING_DISCONNECTED);
896 msg.arg1 = mDefaultDataSubId;
897 msg.sendToTarget();
Jordan Liuff2ccd72019-07-23 15:54:41 -0700898 } else if (dataAllowed && dataIsNowRoaming(mDefaultDataSubId)
899 && shouldShowDataConnectedRoaming(mDefaultDataSubId)) {
900 // No need to show it again if we never cancelled it explicitly, or carrier config
901 // indicates this is not needed.
902 if (mPrevRoamingNotification == ROAMING_NOTIFICATION_CONNECTED) return;
903 mPrevRoamingNotification = ROAMING_NOTIFICATION_CONNECTED;
904 Log.d(LOG_TAG, "Show roaming connected notification");
905 mDataRoamingNotifLog.log("Show roaming on.");
906 Message msg = mHandler.obtainMessage(EVENT_DATA_ROAMING_CONNECTED);
907 msg.arg1 = mDefaultDataSubId;
908 msg.sendToTarget();
909 } else if (mPrevRoamingNotification != ROAMING_NOTIFICATION_NO_NOTIFICATION) {
910 // Otherwise we either 1) we are not roaming or 2) roaming is off but ROAMING_DISABLED
911 // is not the only data disable reason. In this case we dismiss the notification we
912 // showed earlier.
913 mPrevRoamingNotification = ROAMING_NOTIFICATION_NO_NOTIFICATION;
914 Log.d(LOG_TAG, "Dismiss roaming notification");
Jack Yu1a2fc352017-07-14 17:14:37 -0700915 mDataRoamingNotifLog.log("Hide. data allowed=" + dataAllowed + ", reasons=" + reasons);
916 mHandler.sendEmptyMessage(EVENT_DATA_ROAMING_OK);
917 }
918 }
919
Jordan Liuff2ccd72019-07-23 15:54:41 -0700920 /**
921 *
922 * @param subId to check roaming on
923 * @return whether we have transitioned to dataRoaming
924 */
925 private boolean dataIsNowRoaming(int subId) {
926 return getPhone(subId).getServiceState().getDataRoaming();
927 }
928
chen xubaf9fe52019-07-02 17:28:24 -0700929 private void updateLimitedSimFunctionForDualSim() {
930 if (DBG) Log.d(LOG_TAG, "updateLimitedSimFunctionForDualSim");
931 // check conditions to display limited SIM function notification under dual SIM
932 SubscriptionManager subMgr = (SubscriptionManager) getSystemService(
933 Context.TELEPHONY_SUBSCRIPTION_SERVICE);
934 List<SubscriptionInfo> subList = subMgr.getActiveSubscriptionInfoList(false);
935 if (subList != null && subList.size() > 1) {
936 CarrierConfigManager configMgr = (CarrierConfigManager)
937 getSystemService(Context.CARRIER_CONFIG_SERVICE);
938 for (SubscriptionInfo info : subList) {
939 PersistableBundle b = configMgr.getConfigForSubId(info.getSubscriptionId());
940 if (b != null) {
941 if (b.getBoolean(CarrierConfigManager
942 .KEY_LIMITED_SIM_FUNCTION_NOTIFICATION_FOR_DSDS_BOOL)) {
943 notificationMgr.showLimitedSimFunctionWarningNotification(
944 info.getSubscriptionId(),
945 info.getDisplayName().toString());
946 } else {
947 notificationMgr.dismissLimitedSimFunctionWarningNotification(
948 info.getSubscriptionId());
949 }
950 }
951 }
952 } else {
953 // cancel notifications for all subs
954 notificationMgr.dismissLimitedSimFunctionWarningNotification(
955 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
956 }
957 notificationMgr.dismissLimitedSimFunctionWarningNotificationForInactiveSubs();
958
959 }
960
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530961 public Phone getPhoneInEcm() {
962 return phoneInEcm;
963 }
964
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700965 /**
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800966 * Triggers a refresh of the message waiting (voicemail) indicator.
967 *
968 * @param subId the subscription id we should refresh the notification for.
969 */
970 public void refreshMwiIndicator(int subId) {
971 notificationMgr.refreshMwi(subId);
972 }
973
974 /**
Pengquan Mengdd9ac822018-09-20 15:25:35 -0700975 * Called when the network selection on the subscription {@code subId} is changed by the user.
976 *
977 * @param subId the subscription id.
978 */
979 public void onNetworkSelectionChanged(int subId) {
980 Phone phone = getPhone(subId);
981 if (phone != null) {
982 notificationMgr.updateNetworkSelection(phone.getServiceState().getState(), subId);
983 } else {
984 Log.w(LOG_TAG, "onNetworkSelectionChanged on null phone, subId: " + subId);
985 }
986 }
987
988 /**
James.cf Lin4b784aa2021-01-31 03:25:15 +0800989 * @return whether the device supports RCS User Capability Exchange or not.
990 */
991 public boolean getDeviceUceEnabled() {
992 return (mTelephonyRcsService == null) ? false : mTelephonyRcsService.isDeviceUceEnabled();
993 }
994
995 /**
996 * Set the device supports RCS User Capability Exchange.
997 * @param isEnabled true if the device supports UCE.
998 */
999 public void setDeviceUceEnabled(boolean isEnabled) {
1000 if (mTelephonyRcsService != null) {
1001 mTelephonyRcsService.setDeviceUceEnabled(isEnabled);
1002 }
1003 }
1004
1005 /**
Jack Yu1a2fc352017-07-14 17:14:37 -07001006 * Dump the state of the object, add calls to other objects as desired.
1007 *
1008 * @param fd File descriptor
1009 * @param printWriter Print writer
1010 * @param args Arguments
1011 */
1012 public void dump(FileDescriptor fd, PrintWriter printWriter, String[] args) {
1013 IndentingPrintWriter pw = new IndentingPrintWriter(printWriter, " ");
1014 pw.println("------- PhoneGlobals -------");
1015 pw.increaseIndent();
Jordan Liuff2ccd72019-07-23 15:54:41 -07001016 pw.println("mPrevRoamingNotification=" + mPrevRoamingNotification);
Jack Yu1a2fc352017-07-14 17:14:37 -07001017 pw.println("mDefaultDataSubId=" + mDefaultDataSubId);
1018 pw.println("mDataRoamingNotifLog:");
Keun young Parkdebb8532019-05-30 16:35:27 -07001019 pw.println("isSmsCapable=" + TelephonyManager.from(this).isSmsCapable());
Jack Yu1a2fc352017-07-14 17:14:37 -07001020 pw.increaseIndent();
1021 mDataRoamingNotifLog.dump(fd, pw, args);
1022 pw.decreaseIndent();
Brad Ebinger05f52c22019-12-05 13:03:21 -08001023 pw.println("ImsResolver:");
1024 pw.increaseIndent();
1025 try {
Brad Ebingerd1947d82021-05-17 20:54:49 +00001026 if (ImsResolver.getInstance() != null) ImsResolver.getInstance().dump(fd, pw, args);
Brad Ebinger05f52c22019-12-05 13:03:21 -08001027 } catch (Exception e) {
1028 e.printStackTrace();
1029 }
1030 pw.decreaseIndent();
Brad Ebingera68a4972020-01-30 17:31:23 -08001031 pw.println("RcsService:");
1032 try {
1033 if (mTelephonyRcsService != null) mTelephonyRcsService.dump(fd, pw, args);
1034 } catch (Exception e) {
1035 e.printStackTrace();
1036 }
Jack Yu1a2fc352017-07-14 17:14:37 -07001037 pw.decreaseIndent();
1038 pw.println("------- End PhoneGlobals -------");
1039 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001040}