blob: e7cb28c4f77cde4c0b1379190f3dbc0b478fe5c3 [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;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +000077import com.android.internal.telephony.uicc.UiccPort;
Jordan Liu06bdef12021-03-24 17:25:46 -070078import com.android.internal.telephony.uicc.UiccProfile;
Jack Yu1a2fc352017-07-14 17:14:37 -070079import com.android.internal.util.IndentingPrintWriter;
Andrew Leefb7f92e2015-02-26 16:23:32 -080080import com.android.phone.settings.SettingsConstants;
Ta-wei Yenf0a71bc2017-05-17 12:28:28 -070081import com.android.phone.vvm.CarrierVvmPackageInstalledReceiver;
James.cf Linc9f35a42020-01-15 02:35:22 +080082import com.android.services.telephony.rcs.TelephonyRcsService;
Brad Ebinger28626122017-08-10 14:13:35 -070083import com.android.services.telephony.sip.SipAccountRegistry;
Brad Ebinger3fa43462016-04-12 16:06:48 -070084import com.android.services.telephony.sip.SipUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070085
Jack Yu1a2fc352017-07-14 17:14:37 -070086import java.io.FileDescriptor;
87import java.io.PrintWriter;
Jordan Liuff2ccd72019-07-23 15:54:41 -070088import java.lang.annotation.Retention;
89import java.lang.annotation.RetentionPolicy;
chen xubaf9fe52019-07-02 17:28:24 -070090import java.util.List;
Jack Yu1a2fc352017-07-14 17:14:37 -070091
Santos Cordon7d4ddf62013-07-10 11:58:08 -070092/**
93 * Global state for the telephony subsystem when running in the primary
94 * phone process.
95 */
Sailesh Nepalbf900542014-07-15 16:18:32 -070096public class PhoneGlobals extends ContextWrapper {
Jack Yu1a2fc352017-07-14 17:14:37 -070097 public static final String LOG_TAG = "PhoneGlobals";
Santos Cordon7d4ddf62013-07-10 11:58:08 -070098
99 /**
100 * Phone app-wide debug level:
101 * 0 - no debug logging
102 * 1 - normal debug logging if ro.debuggable is set (which is true in
103 * "eng" and "userdebug" builds but not "user" builds)
104 * 2 - ultra-verbose debug logging
105 *
106 * Most individual classes in the phone app have a local DBG constant,
107 * typically set to
108 * (PhoneApp.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1)
109 * or else
110 * (PhoneApp.DBG_LEVEL >= 2)
111 * depending on the desired verbosity.
112 *
113 * ***** DO NOT SUBMIT WITH DBG_LEVEL > 0 *************
114 */
Andrew Lee88b51e22014-10-29 15:48:51 -0700115 public static final int DBG_LEVEL = 0;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700116
117 private static final boolean DBG =
118 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
119 private static final boolean VDBG = (PhoneGlobals.DBG_LEVEL >= 2);
120
121 // Message codes; see mHandler below.
122 private static final int EVENT_SIM_NETWORK_LOCKED = 3;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700123 private static final int EVENT_SIM_STATE_CHANGED = 8;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700124 private static final int EVENT_DATA_ROAMING_DISCONNECTED = 10;
Jordan Liuff2ccd72019-07-23 15:54:41 -0700125 private static final int EVENT_DATA_ROAMING_CONNECTED = 11;
126 private static final int EVENT_DATA_ROAMING_OK = 12;
127 private static final int EVENT_UNSOL_CDMA_INFO_RECORD = 13;
128 private static final int EVENT_RESTART_SIP = 14;
129 private static final int EVENT_DATA_ROAMING_SETTINGS_CHANGED = 15;
130 private static final int EVENT_MOBILE_DATA_SETTINGS_CHANGED = 16;
Lei Liu863eac02020-06-24 20:12:55 +0800131 private static final int EVENT_CARRIER_CONFIG_CHANGED = 17;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700132
133 // The MMI codes are also used by the InCallScreen.
134 public static final int MMI_INITIATE = 51;
135 public static final int MMI_COMPLETE = 52;
136 public static final int MMI_CANCEL = 53;
137 // Don't use message codes larger than 99 here; those are reserved for
138 // the individual Activities of the Phone UI.
139
Santos Cordone18902f2016-03-22 17:16:04 -0700140 public static final int AIRPLANE_ON = 1;
141 public static final int AIRPLANE_OFF = 0;
142
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700143 /**
144 * Allowable values for the wake lock code.
145 * SLEEP means the device can be put to sleep.
146 * PARTIAL means wake the processor, but we display can be kept off.
147 * FULL means wake both the processor and the display.
148 */
149 public enum WakeState {
150 SLEEP,
151 PARTIAL,
152 FULL
153 }
154
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700155 private static PhoneGlobals sMe;
156
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700157 CallManager mCM;
Santos Cordon63a84242013-07-23 13:32:52 -0700158 CallNotifier notifier;
Santos Cordon63a84242013-07-23 13:32:52 -0700159 NotificationMgr notificationMgr;
James.cf Linc9f35a42020-01-15 02:35:22 +0800160 TelephonyRcsService mTelephonyRcsService;
Andrew Lee9431b832015-03-09 18:46:45 -0700161 public PhoneInterfaceManager phoneMgr;
James.cf Linaf3183c2019-10-24 00:59:00 +0800162 public ImsRcsController imsRcsController;
Hunsuk Choi3b742d62021-10-25 19:48:34 +0000163 public ImsStateCallbackController mImsStateCallbackController;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800164 CarrierConfigLoader configLoader;
Santos Cordon63a84242013-07-23 13:32:52 -0700165
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530166 private Phone phoneInEcm;
Santos Cordon63a84242013-07-23 13:32:52 -0700167
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700168 static boolean sVoiceCapable = true;
169
Santos Cordonc593d002015-06-03 15:41:15 -0700170 // TODO: Remove, no longer used.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700171 CdmaPhoneCallState cdmaPhoneCallState;
172
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700173 // The currently-active PUK entry activity and progress dialog.
174 // Normally, these are the Emergency Dialer and the subsequent
175 // progress dialog. null if there is are no such objects in
176 // the foreground.
177 private Activity mPUKEntryActivity;
178 private ProgressDialog mPUKEntryProgressDialog;
179
Jordan Liuff2ccd72019-07-23 15:54:41 -0700180 /** @hide */
181 @Retention(RetentionPolicy.SOURCE)
182 @IntDef(prefix = {"ROAMING_NOTIFICATION_"},
183 value = {
184 ROAMING_NOTIFICATION_NO_NOTIFICATION,
185 ROAMING_NOTIFICATION_CONNECTED,
186 ROAMING_NOTIFICATION_DISCONNECTED})
187 public @interface RoamingNotification {}
188
189 private static final int ROAMING_NOTIFICATION_NO_NOTIFICATION = 0;
190 private static final int ROAMING_NOTIFICATION_CONNECTED = 1;
191 private static final int ROAMING_NOTIFICATION_DISCONNECTED = 2;
192
193 @RoamingNotification
194 private int mPrevRoamingNotification = ROAMING_NOTIFICATION_NO_NOTIFICATION;
Jack Yu953f9352017-05-18 14:41:06 -0700195
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();
Brad Ebinger533c6c12017-08-02 11:38:48 -0700208 // Broadcast receiver for SIP based intents (see onCreate())
Brad Ebinger28626122017-08-10 14:13:35 -0700209 private final SipReceiver mSipReceiver = new SipReceiver();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700210
Ta-wei Yenf0a71bc2017-05-17 12:28:28 -0700211 private final CarrierVvmPackageInstalledReceiver mCarrierVvmPackageInstalledReceiver =
212 new CarrierVvmPackageInstalledReceiver();
213
Jack Yu1a2fc352017-07-14 17:14:37 -0700214 private final SettingsObserver mSettingsObserver;
Siim Sammul182d61b2021-05-04 16:01:42 +0100215 private BinderCallsStats.SettingsObserver mBinderCallsSettingsObserver;
Jack Yu1a2fc352017-07-14 17:14:37 -0700216
Nazish Tabassum160d2112019-12-23 17:31:33 +0530217 private static class EventSimStateChangedBag {
218 final int mPhoneId;
219 final String mIccStatus;
220
221 EventSimStateChangedBag(int phoneId, String iccStatus) {
222 mPhoneId = phoneId;
223 mIccStatus = iccStatus;
224 }
225 }
226
Jordan Liu06bdef12021-03-24 17:25:46 -0700227 // Some carrier config settings disable the network lock screen, so we call handleSimLock
228 // when either SIM_LOCK or CARRIER_CONFIG changes so that no matter which one happens first,
229 // we still do the right thing
230 private void handleSimLock(int subType, Phone phone) {
231 PersistableBundle cc = getCarrierConfigForSubId(phone.getSubId());
232 if (!CarrierConfigManager.isConfigForIdentifiedCarrier(cc)) {
233 // If we only have the default carrier config just return, to avoid popping up the
234 // the SIM lock screen when it's disabled by the carrier.
235 Log.i(LOG_TAG, "Not showing 'SIM network unlock' screen. Carrier config not loaded");
236 return;
237 }
238 if (cc.getBoolean(CarrierConfigManager.KEY_IGNORE_SIM_NETWORK_LOCKED_EVENTS_BOOL)) {
239 // Some products don't have the concept of a "SIM network lock"
240 Log.i(LOG_TAG, "Not showing 'SIM network unlock' screen. Disabled by carrier config");
241 return;
242 }
243
244 // if passed in subType is unknown, retrieve it here.
245 if (subType == -1) {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000246 final UiccPort uiccPort = phone.getUiccPort();
247 if (uiccPort == null) {
Jordan Liu06bdef12021-03-24 17:25:46 -0700248 Log.e(LOG_TAG,
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000249 "handleSimLock: uiccPort for phone " + phone.getPhoneId() + " is null");
Jordan Liu06bdef12021-03-24 17:25:46 -0700250 return;
251 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000252 final UiccProfile uiccProfile = uiccPort.getUiccProfile();
Jordan Liu06bdef12021-03-24 17:25:46 -0700253 if (uiccProfile == null) {
254 Log.e(LOG_TAG,
255 "handleSimLock: uiccProfile for phone " + phone.getPhoneId() + " is null");
256 return;
257 }
258 subType = uiccProfile.getApplication(
259 uiccProfile.mCurrentAppType).getPersoSubState().ordinal();
260 }
261 // Normal case: show the "SIM network unlock" PIN entry screen.
262 // The user won't be able to do anything else until
263 // they enter a valid SIM network PIN.
264 Log.i(LOG_TAG, "show sim depersonal panel");
265 IccNetworkDepersonalizationPanel.showDialog(phone, subType);
266 }
267
268 private boolean isSimLocked(Phone phone) {
269 TelephonyManager tm = getSystemService(TelephonyManager.class);
270 return tm.createForSubscriptionId(phone.getSubId()).getSimState()
271 == TelephonyManager.SIM_STATE_NETWORK_LOCKED;
272 }
273
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700274 Handler mHandler = new Handler() {
275 @Override
276 public void handleMessage(Message msg) {
277 PhoneConstants.State phoneState;
Jack Yu1a2fc352017-07-14 17:14:37 -0700278 if (VDBG) Log.v(LOG_TAG, "event=" + msg.what);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700279 switch (msg.what) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700280 // TODO: This event should be handled by the lock screen, just
281 // like the "SIM missing" and "Sim locked" cases (bug 1804111).
282 case EVENT_SIM_NETWORK_LOCKED:
Jordan Liu06bdef12021-03-24 17:25:46 -0700283 int subType = (Integer) ((AsyncResult) msg.obj).result;
284 Phone phone = (Phone) ((AsyncResult) msg.obj).userObj;
285 handleSimLock(subType, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700286 break;
287
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700288 case EVENT_DATA_ROAMING_DISCONNECTED:
Jordan Liuff2ccd72019-07-23 15:54:41 -0700289 notificationMgr.showDataRoamingNotification(msg.arg1, false);
290 break;
291
292 case EVENT_DATA_ROAMING_CONNECTED:
293 notificationMgr.showDataRoamingNotification(msg.arg1, true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700294 break;
295
296 case EVENT_DATA_ROAMING_OK:
Jordan Liuff2ccd72019-07-23 15:54:41 -0700297 notificationMgr.hideDataRoamingNotification();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700298 break;
299
300 case MMI_COMPLETE:
301 onMMIComplete((AsyncResult) msg.obj);
302 break;
303
304 case MMI_CANCEL:
Stuart Scottdcf40a92014-12-09 10:45:01 -0800305 PhoneUtils.cancelMmiCode(mCM.getFgPhone());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700306 break;
307
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700308 case EVENT_SIM_STATE_CHANGED:
309 // Marks the event where the SIM goes into ready state.
310 // Right now, this is only used for the PUK-unlocking
311 // process.
Nazish Tabassum160d2112019-12-23 17:31:33 +0530312 EventSimStateChangedBag bag = (EventSimStateChangedBag)msg.obj;
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700313 if (IccCardConstants.INTENT_VALUE_ICC_READY.equals(bag.mIccStatus)
Nazish Tabassum59126a92020-07-03 21:44:49 +0530314 || IccCardConstants.INTENT_VALUE_ICC_LOADED.equals(bag.mIccStatus)
315 || IccCardConstants.INTENT_VALUE_ICC_NOT_READY.equals(bag.mIccStatus)
316 || IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(bag.mIccStatus)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700317 // when the right event is triggered and there
318 // are UI objects in the foreground, we close
319 // them to display the lock panel.
320 if (mPUKEntryActivity != null) {
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700321 Log.i(LOG_TAG, "Dismiss puk entry activity");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700322 mPUKEntryActivity.finish();
323 mPUKEntryActivity = null;
324 }
325 if (mPUKEntryProgressDialog != null) {
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700326 Log.i(LOG_TAG, "Dismiss puk progress dialog");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700327 mPUKEntryProgressDialog.dismiss();
328 mPUKEntryProgressDialog = null;
329 }
Nazish Tabassum59126a92020-07-03 21:44:49 +0530330 Log.i(LOG_TAG, "Dismissing depersonal panel" + (bag.mIccStatus));
Nazish Tabassum160d2112019-12-23 17:31:33 +0530331 IccNetworkDepersonalizationPanel.dialogDismiss(bag.mPhoneId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700332 }
333 break;
334
335 case EVENT_UNSOL_CDMA_INFO_RECORD:
336 //TODO: handle message here;
337 break;
Brad Ebinger3fa43462016-04-12 16:06:48 -0700338 case EVENT_RESTART_SIP:
339 // This should only run if the Phone process crashed and was restarted. We do
340 // not want this running if the device is still in the FBE encrypted state.
Brad Ebinger28626122017-08-10 14:13:35 -0700341 // This is the same procedure that is triggered in the SipIncomingCallReceiver
Brad Ebinger3fa43462016-04-12 16:06:48 -0700342 // upon BOOT_COMPLETED.
Meng Wangc04cd2d2019-10-04 12:00:06 -0700343 UserManager userManager =
344 (UserManager) sMe.getSystemService(Context.USER_SERVICE);
Brad Ebinger3fa43462016-04-12 16:06:48 -0700345 if (userManager != null && userManager.isUserUnlocked()) {
346 SipUtil.startSipService();
347 }
348 break;
Jack Yu1a2fc352017-07-14 17:14:37 -0700349 case EVENT_DATA_ROAMING_SETTINGS_CHANGED:
350 case EVENT_MOBILE_DATA_SETTINGS_CHANGED:
351 updateDataRoamingStatus();
352 break;
Lei Liu863eac02020-06-24 20:12:55 +0800353 case EVENT_CARRIER_CONFIG_CHANGED:
354 int subId = (Integer) msg.obj;
355 // The voicemail number could be overridden by carrier config, so need to
356 // refresh the message waiting (voicemail) indicator.
357 refreshMwiIndicator(subId);
Jordan Liu06bdef12021-03-24 17:25:46 -0700358 phone = getPhone(subId);
359 if (phone != null && isSimLocked(phone)) {
360 // pass in subType=-1 so handleSimLock can find the actual subType if
361 // needed. This is safe as valid values for subType are >= 0
362 handleSimLock(-1, phone);
363 }
Lei Liu863eac02020-06-24 20:12:55 +0800364 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700365 }
366 }
367 };
368
369 public PhoneGlobals(Context context) {
370 super(context);
371 sMe = this;
Jack Yu1a2fc352017-07-14 17:14:37 -0700372 mSettingsObserver = new SettingsObserver(context, mHandler);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700373 }
374
375 public void onCreate() {
376 if (VDBG) Log.v(LOG_TAG, "onCreate()...");
377
378 ContentResolver resolver = getContentResolver();
379
Hall Liuaa4211e2021-01-20 15:43:39 -0800380 // Initialize the shim from frameworks/opt/telephony into packages/services/Telephony.
381 TelephonyLocalConnection.setInstance(new LocalConnectionImpl(this));
382
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700383 // Cache the "voice capable" flag.
384 // This flag currently comes from a resource (which is
385 // overrideable on a per-product basis):
Michele87262742020-01-16 12:23:53 -0800386 sVoiceCapable = ((TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE))
387 .isVoiceCapable();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700388 // ...but this might eventually become a PackageManager "system
389 // feature" instead, in which case we'd do something like:
390 // sVoiceCapable =
391 // getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY_VOICE_CALLS);
392
Stuart Scottdcf40a92014-12-09 10:45:01 -0800393 if (mCM == null) {
Nathan Haroldf9df6ea2019-03-08 11:54:22 -0800394 // Initialize AnomalyReporter early so that it can be used
395 AnomalyReporter.initialize(this);
Nathan Harold0db5c5e2019-01-22 20:18:56 -0800396
Youming Ye47a6adc2018-11-19 15:33:36 -0800397 // Inject telephony component factory if configured using other jars.
398 XmlResourceParser parser = getResources().getXml(R.xml.telephony_injection);
399 TelephonyComponentFactory.getInstance().injectTheComponentFactory(parser);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700400 // Initialize the telephony framework
401 PhoneFactory.makeDefaultPhones(this);
402
Brad Ebinger05f52c22019-12-05 13:03:21 -0800403 // Only bring up ImsResolver if the device supports having an IMS stack.
404 if (getPackageManager().hasSystemFeature(
405 PackageManager.FEATURE_TELEPHONY_IMS)) {
406 // Get the package name of the default IMS implementation.
407 String defaultImsMmtelPackage = getResources().getString(
408 R.string.config_ims_mmtel_package);
409 String defaultImsRcsPackage = getResources().getString(
410 R.string.config_ims_rcs_package);
Brad Ebingerd1947d82021-05-17 20:54:49 +0000411 ImsResolver.make(this, defaultImsMmtelPackage,
Brad Ebingere3ae65a2020-09-11 12:45:11 -0700412 defaultImsRcsPackage, PhoneFactory.getPhones().length,
413 new ImsFeatureBinderRepository());
Brad Ebingerd1947d82021-05-17 20:54:49 +0000414 ImsResolver.getInstance().initialize();
Tyler Gunn92479152021-01-20 16:30:10 -0800415
416 // With the IMS phone created, load static config.xml values from the phone process
417 // so that it can be provided to the ImsPhoneCallTracker.
418 for (Phone p : PhoneFactory.getPhones()) {
419 Phone imsPhone = p.getImsPhone();
420 if (imsPhone != null && imsPhone instanceof ImsPhone) {
421 ImsPhone theImsPhone = (ImsPhone) imsPhone;
422 if (theImsPhone.getCallTracker() instanceof ImsPhoneCallTracker) {
423 ImsPhoneCallTracker ict = (ImsPhoneCallTracker)
424 theImsPhone.getCallTracker();
425
426 ImsPhoneCallTracker.Config config = new ImsPhoneCallTracker.Config();
427 config.isD2DCommunicationSupported = getResources().getBoolean(
428 R.bool.config_use_device_to_device_communication);
429 ict.setConfig(config);
430 }
431 }
432 }
Hui Wang068ab862020-10-31 05:12:53 +0000433 RcsProvisioningMonitor.make(this);
Brad Ebinger05f52c22019-12-05 13:03:21 -0800434 }
435
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700436 // Start TelephonyDebugService After the default phone is created.
437 Intent intent = new Intent(this, TelephonyDebugService.class);
438 startService(intent);
439
440 mCM = CallManager.getInstance();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700441
442 // Create the NotificationMgr singleton, which is used to display
443 // status bar icons and control other status bar behavior.
444 notificationMgr = NotificationMgr.init(this);
445
Brad Ebinger3fa43462016-04-12 16:06:48 -0700446 // If PhoneGlobals has crashed and is being restarted, then restart.
447 mHandler.sendEmptyMessage(EVENT_RESTART_SIP);
448
Anthony Lee03ebdfc2015-07-27 08:12:02 -0700449 // Create an instance of CdmaPhoneCallState and initialize it to IDLE
450 cdmaPhoneCallState = new CdmaPhoneCallState();
451 cdmaPhoneCallState.CdmaPhoneCallStateInit();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700452
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700453 // before registering for phone state changes
454 mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
455 mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, LOG_TAG);
456 // lock used to keep the processor awake, when we don't care for the display.
457 mPartialWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK
458 | PowerManager.ON_AFTER_RELEASE, LOG_TAG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700459
460 mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
461
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -0800462 phoneMgr = PhoneInterfaceManager.init(this);
Santos Cordon406c0342013-08-28 00:07:47 -0700463
James.cf Linaf3183c2019-10-24 00:59:00 +0800464 imsRcsController = ImsRcsController.init(this);
465
James.cf Linc9f35a42020-01-15 02:35:22 +0800466 if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY_IMS)) {
Hunsuk Choi3b742d62021-10-25 19:48:34 +0000467 mImsStateCallbackController =
468 ImsStateCallbackController.make(this, PhoneFactory.getPhones().length);
Brad Ebingera68a4972020-01-30 17:31:23 -0800469 mTelephonyRcsService = new TelephonyRcsService(this,
470 PhoneFactory.getPhones().length);
471 mTelephonyRcsService.initialize();
James.cf Linc9f35a42020-01-15 02:35:22 +0800472 imsRcsController.setRcsService(mTelephonyRcsService);
473 }
474
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800475 configLoader = CarrierConfigLoader.init(this);
476
Jack Yu1a2fc352017-07-14 17:14:37 -0700477 // Create the CallNotifier singleton, which handles
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700478 // asynchronous events from the telephony layer (like
479 // launching the incoming-call UI when an incoming call comes
480 // in.)
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800481 notifier = CallNotifier.init(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700482
Stuart Scottdcf40a92014-12-09 10:45:01 -0800483 PhoneUtils.registerIccStatus(mHandler, EVENT_SIM_NETWORK_LOCKED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700484
485 // register for MMI/USSD
486 mCM.registerForMmiComplete(mHandler, MMI_COMPLETE, null);
487
Qiong Liucc3fe812019-09-23 15:25:44 +0800488 // Initialize cell status using current airplane mode.
Taesu Lee62f826d2020-08-10 16:01:18 +0900489 handleAirplaneModeChange(
490 Settings.Global.getInt(
491 getContentResolver(),
492 Settings.Global.AIRPLANE_MODE_ON,
493 AIRPLANE_OFF)
494 == AIRPLANE_ON);
Qiong Liucc3fe812019-09-23 15:25:44 +0800495
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700496 // Register for misc other intent broadcasts.
497 IntentFilter intentFilter =
498 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700499 intentFilter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
500 intentFilter.addAction(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);
501 intentFilter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
502 intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
Jack Yu1a2fc352017-07-14 17:14:37 -0700503 intentFilter.addAction(TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED);
504 intentFilter.addAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700505 registerReceiver(mReceiver, intentFilter);
506
Brad Ebinger533c6c12017-08-02 11:38:48 -0700507 IntentFilter sipIntentFilter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
Brad Ebinger533c6c12017-08-02 11:38:48 -0700508 sipIntentFilter.addAction(SipManager.ACTION_SIP_SERVICE_UP);
509 sipIntentFilter.addAction(SipManager.ACTION_SIP_CALL_OPTION_CHANGED);
Grace Jiab95592a2020-01-15 14:12:24 -0800510 sipIntentFilter.addAction(SipManager.ACTION_SIP_REMOVE_PROFILE);
Brad Ebinger28626122017-08-10 14:13:35 -0700511 registerReceiver(mSipReceiver, sipIntentFilter);
Brad Ebinger533c6c12017-08-02 11:38:48 -0700512
Ta-wei Yenf0a71bc2017-05-17 12:28:28 -0700513 mCarrierVvmPackageInstalledReceiver.register(this);
514
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700515 //set the default values for the preferences in the phone.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700516 PreferenceManager.setDefaultValues(this, R.xml.call_feature_setting, false);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700517 }
518
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700519 // XXX pre-load the SimProvider so that it's ready
520 resolver.getType(Uri.parse("content://icc/adn"));
521
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700522 // TODO: Register for Cdma Information Records
523 // phone.registerCdmaInformationRecord(mHandler, EVENT_UNSOL_CDMA_INFO_RECORD, null);
524
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700525 // Read HAC settings and configure audio hardware
526 if (getResources().getBoolean(R.bool.hac_enabled)) {
Stuart Scottdcf40a92014-12-09 10:45:01 -0800527 int hac = android.provider.Settings.System.getInt(
528 getContentResolver(),
529 android.provider.Settings.System.HEARING_AID,
530 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700531 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Jordan Liuf71aeef2019-08-21 11:46:09 -0700532 audioManager.setParameters(
533 SettingsConstants.HAC_KEY + "=" + (hac == SettingsConstants.HAC_ENABLED
534 ? SettingsConstants.HAC_VAL_ON : SettingsConstants.HAC_VAL_OFF));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700535 }
Siim Sammul182d61b2021-05-04 16:01:42 +0100536
537 // Start tracking Binder latency for the phone process.
538 mBinderCallsSettingsObserver = new BinderCallsStats.SettingsObserver(
539 getApplicationContext(),
Siim Sammula6950092021-06-22 17:14:03 +0100540 new BinderCallsStats(
541 new BinderCallsStats.Injector(),
542 com.android.internal.os.BinderLatencyProto.Dims.TELEPHONY));
Santos Cordonff506f52013-11-21 19:13:19 -0800543 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700544
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700545 /**
546 * Returns the singleton instance of the PhoneApp.
547 */
Sailesh Nepal1eaf22b2014-02-22 17:00:49 -0800548 public static PhoneGlobals getInstance() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700549 if (sMe == null) {
550 throw new IllegalStateException("No PhoneGlobals here!");
551 }
552 return sMe;
553 }
554
555 /**
Stuart Scottdcf40a92014-12-09 10:45:01 -0800556 * Returns the default phone.
557 *
Andrew Lee385019f2014-11-24 14:19:50 -0800558 * WARNING: This method should be used carefully, now that there may be multiple phones.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700559 */
Andrew Lee83383e42014-10-31 12:42:28 -0700560 public static Phone getPhone() {
Stuart Scottdcf40a92014-12-09 10:45:01 -0800561 return PhoneFactory.getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700562 }
563
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800564 public static Phone getPhone(int subId) {
565 return PhoneFactory.getPhone(SubscriptionManager.getPhoneId(subId));
Andrew Lee385019f2014-11-24 14:19:50 -0800566 }
567
Santos Cordonde10b752013-09-19 04:11:33 -0700568 /* package */ CallManager getCallManager() {
569 return mCM;
570 }
571
Chris Manton4e9fa912015-06-19 11:26:57 -0700572 public PersistableBundle getCarrierConfig() {
Shishir Agrawald3480e02016-01-25 13:05:49 -0800573 return getCarrierConfigForSubId(SubscriptionManager.getDefaultSubscriptionId());
Jonathan Basseri89b0ab42015-05-01 10:52:40 -0700574 }
575
Chris Manton4e9fa912015-06-19 11:26:57 -0700576 public PersistableBundle getCarrierConfigForSubId(int subId) {
Philip P. Moltmann0079aae2020-03-05 16:24:02 -0800577 return configLoader.getConfigForSubIdWithFeature(subId, getOpPackageName(),
578 getAttributionTag());
Junda Liu605148f2015-04-28 15:23:40 -0700579 }
580
Jack Yu1a2fc352017-07-14 17:14:37 -0700581 private void registerSettingsObserver() {
582 mSettingsObserver.unobserve();
583 String dataRoamingSetting = Settings.Global.DATA_ROAMING;
584 String mobileDataSetting = Settings.Global.MOBILE_DATA;
585 if (TelephonyManager.getDefault().getSimCount() > 1) {
586 int subId = mDefaultDataSubId;
587 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
588 dataRoamingSetting += subId;
589 mobileDataSetting += subId;
590 }
591 }
592
593 // Listen for user data roaming setting changed event
594 mSettingsObserver.observe(Settings.Global.getUriFor(dataRoamingSetting),
595 EVENT_DATA_ROAMING_SETTINGS_CHANGED);
596
597 // Listen for mobile data setting changed event
598 mSettingsObserver.observe(Settings.Global.getUriFor(mobileDataSetting),
599 EVENT_MOBILE_DATA_SETTINGS_CHANGED);
600 }
601
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700602 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700603 * Sets the activity responsible for un-PUK-blocking the device
604 * so that we may close it when we receive a positive result.
605 * mPUKEntryActivity is also used to indicate to the device that
606 * we are trying to un-PUK-lock the phone. In other words, iff
607 * it is NOT null, then we are trying to unlock and waiting for
608 * the SIM to move to READY state.
609 *
610 * @param activity is the activity to close when PUK has
611 * finished unlocking. Can be set to null to indicate the unlock
612 * or SIM READYing process is over.
613 */
614 void setPukEntryActivity(Activity activity) {
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700615 Log.i(LOG_TAG, "setPukEntryActivity - set to " + (activity == null ? "null" : "activity"));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700616 mPUKEntryActivity = activity;
617 }
618
619 Activity getPUKEntryActivity() {
620 return mPUKEntryActivity;
621 }
622
623 /**
624 * Sets the dialog responsible for notifying the user of un-PUK-
625 * blocking - SIM READYing progress, so that we may dismiss it
626 * when we receive a positive result.
627 *
628 * @param dialog indicates the progress dialog informing the user
629 * of the state of the device. Dismissed upon completion of
630 * READYing process
631 */
632 void setPukEntryProgressDialog(ProgressDialog dialog) {
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700633 Log.i(LOG_TAG, "setPukEntryProgressDialog - set to "
634 + (dialog == null ? "null" : "activity"));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700635 mPUKEntryProgressDialog = dialog;
636 }
637
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700638 KeyguardManager getKeyguardManager() {
639 return mKeyguardManager;
640 }
641
642 private void onMMIComplete(AsyncResult r) {
643 if (VDBG) Log.d(LOG_TAG, "onMMIComplete()...");
644 MmiCode mmiCode = (MmiCode) r.result;
Stuart Scottdcf40a92014-12-09 10:45:01 -0800645 PhoneUtils.displayMMIComplete(mmiCode.getPhone(), getInstance(), mmiCode, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700646 }
647
fionaxu80126972017-02-01 17:01:26 -0800648 private void initForNewRadioTechnology() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700649 if (DBG) Log.d(LOG_TAG, "initForNewRadioTechnology...");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700650 notifier.updateCallNotifierRegistrationsAfterRadioTechnologyChange();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700651 }
652
Taesu Lee62f826d2020-08-10 16:01:18 +0900653 private void handleAirplaneModeChange(boolean isAirplaneNewlyOn) {
654 int cellState =
655 Settings.Global.getInt(
656 getContentResolver(), Settings.Global.CELL_ON, PhoneConstants.CELL_ON_FLAG);
Chris Mantona09f3632016-02-09 14:48:42 -0800657 switch (cellState) {
658 case PhoneConstants.CELL_OFF_FLAG:
659 // Airplane mode does not affect the cell radio if user
660 // has turned it off.
661 break;
662 case PhoneConstants.CELL_ON_FLAG:
Taesu Lee62f826d2020-08-10 16:01:18 +0900663 maybeTurnCellOff(isAirplaneNewlyOn);
Chris Mantona09f3632016-02-09 14:48:42 -0800664 break;
665 case PhoneConstants.CELL_OFF_DUE_TO_AIRPLANE_MODE_FLAG:
Taesu Lee62f826d2020-08-10 16:01:18 +0900666 maybeTurnCellOn(isAirplaneNewlyOn);
Chris Mantona09f3632016-02-09 14:48:42 -0800667 break;
668 }
Hall Liu7f56e1a2019-07-10 14:41:14 -0700669 for (Phone phone : PhoneFactory.getPhones()) {
670 phone.getServiceStateTracker().onAirplaneModeChanged(isAirplaneNewlyOn);
671 }
Chris Mantona09f3632016-02-09 14:48:42 -0800672 }
673
674 /*
675 * Returns true if the radio must be turned off when entering airplane mode.
676 */
Taesu Lee62f826d2020-08-10 16:01:18 +0900677 private boolean isCellOffInAirplaneMode() {
678 String airplaneModeRadios =
679 Settings.Global.getString(
680 getContentResolver(), Settings.Global.AIRPLANE_MODE_RADIOS);
Chris Mantona09f3632016-02-09 14:48:42 -0800681 return airplaneModeRadios == null
682 || airplaneModeRadios.contains(Settings.Global.RADIO_CELL);
683 }
684
Taesu Lee62f826d2020-08-10 16:01:18 +0900685 private void setRadioPowerOff() {
Chris Mantona09f3632016-02-09 14:48:42 -0800686 Log.i(LOG_TAG, "Turning radio off - airplane");
Taesu Lee62f826d2020-08-10 16:01:18 +0900687 Settings.Global.putInt(
688 getContentResolver(),
689 Settings.Global.CELL_ON,
690 PhoneConstants.CELL_OFF_DUE_TO_AIRPLANE_MODE_FLAG);
Chris Mantona09f3632016-02-09 14:48:42 -0800691 Settings.Global.putInt(getContentResolver(), Settings.Global.ENABLE_CELLULAR_ON_BOOT, 0);
Taesu Lee62f826d2020-08-10 16:01:18 +0900692 TelephonyProperties.airplane_mode_on(true); // true means int value 1
Chris Mantona09f3632016-02-09 14:48:42 -0800693 PhoneUtils.setRadioPower(false);
694 }
695
Taesu Lee62f826d2020-08-10 16:01:18 +0900696 private void setRadioPowerOn() {
Chris Mantona09f3632016-02-09 14:48:42 -0800697 Log.i(LOG_TAG, "Turning radio on - airplane");
Taesu Lee62f826d2020-08-10 16:01:18 +0900698 Settings.Global.putInt(
699 getContentResolver(), Settings.Global.CELL_ON, PhoneConstants.CELL_ON_FLAG);
700 Settings.Global.putInt(getContentResolver(), Settings.Global.ENABLE_CELLULAR_ON_BOOT, 1);
Meng Wang61307622019-10-23 14:08:43 -0700701 TelephonyProperties.airplane_mode_on(false); // false means int value 0
Chris Mantona09f3632016-02-09 14:48:42 -0800702 PhoneUtils.setRadioPower(true);
703 }
704
Taesu Lee62f826d2020-08-10 16:01:18 +0900705 private void maybeTurnCellOff(boolean isAirplaneNewlyOn) {
Chris Mantona09f3632016-02-09 14:48:42 -0800706 if (isAirplaneNewlyOn) {
Santos Cordone18902f2016-03-22 17:16:04 -0700707 // If we are trying to turn off the radio, make sure there are no active
708 // emergency calls. If there are, switch airplane mode back to off.
Taesu Lee62f826d2020-08-10 16:01:18 +0900709 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Tyler Gunn900d8fd2018-09-21 09:22:12 -0700710
711 if (tm != null && tm.isInEmergencyCall()) {
Santos Cordone18902f2016-03-22 17:16:04 -0700712 // Switch airplane mode back to off.
Jordan Liu0e819b12019-08-28 15:33:05 -0700713 ConnectivityManager cm =
Taesu Lee62f826d2020-08-10 16:01:18 +0900714 (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
Jordan Liu0e819b12019-08-28 15:33:05 -0700715 cm.setAirplaneMode(false);
Santos Cordone18902f2016-03-22 17:16:04 -0700716 Toast.makeText(this, R.string.radio_off_during_emergency_call, Toast.LENGTH_LONG)
717 .show();
718 Log.i(LOG_TAG, "Ignoring airplane mode: emergency call. Turning airplane off");
Taesu Lee62f826d2020-08-10 16:01:18 +0900719 } else if (isCellOffInAirplaneMode()) {
720 setRadioPowerOff();
Santos Cordone18902f2016-03-22 17:16:04 -0700721 } else {
Chris Mantona09f3632016-02-09 14:48:42 -0800722 Log.i(LOG_TAG, "Ignoring airplane mode: settings prevent cell radio power off");
Santos Cordone18902f2016-03-22 17:16:04 -0700723 }
Chris Mantona09f3632016-02-09 14:48:42 -0800724 }
725 }
726
Taesu Lee62f826d2020-08-10 16:01:18 +0900727 private void maybeTurnCellOn(boolean isAirplaneNewlyOn) {
Chris Mantona09f3632016-02-09 14:48:42 -0800728 if (!isAirplaneNewlyOn) {
Taesu Lee62f826d2020-08-10 16:01:18 +0900729 setRadioPowerOn();
Santos Cordone18902f2016-03-22 17:16:04 -0700730 }
731 }
732
Santos Cordon593ab382013-08-06 21:58:23 -0700733 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700734 * Receiver for misc intent broadcasts the Phone app cares about.
735 */
736 private class PhoneAppBroadcastReceiver extends BroadcastReceiver {
737 @Override
738 public void onReceive(Context context, Intent intent) {
739 String action = intent.getAction();
740 if (action.equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) {
Taesu Lee3dc572b2020-08-07 18:16:22 +0900741 boolean airplaneMode = intent.getBooleanExtra("state", false);
Taesu Lee62f826d2020-08-10 16:01:18 +0900742 handleAirplaneModeChange(airplaneMode);
Amit Mahajanf5d92c82018-11-02 17:44:40 -0700743 } else if (action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) {
744 // re-register as it may be a new IccCard
745 int phoneId = intent.getIntExtra(PhoneConstants.PHONE_KEY,
746 SubscriptionManager.INVALID_PHONE_INDEX);
747 if (SubscriptionManager.isValidPhoneId(phoneId)) {
748 PhoneUtils.unregisterIccStatus(mHandler, phoneId);
749 PhoneUtils.registerIccStatus(mHandler, EVENT_SIM_NETWORK_LOCKED, phoneId);
750 }
Nazish Tabassum160d2112019-12-23 17:31:33 +0530751 String iccStatus = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);
752 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SIM_STATE_CHANGED,
753 new EventSimStateChangedBag(phoneId, iccStatus)));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700754 } else if (action.equals(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED)) {
755 String newPhone = intent.getStringExtra(PhoneConstants.PHONE_NAME_KEY);
fionaxu80126972017-02-01 17:01:26 -0800756 Log.d(LOG_TAG, "Radio technology switched. Now " + newPhone + " is active.");
757 initForNewRadioTechnology();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700758 } else if (action.equals(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED)) {
759 handleServiceStateChanged(intent);
760 } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) {
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530761 int phoneId = intent.getIntExtra(PhoneConstants.PHONE_KEY, 0);
Srikanth Chintala079912a2016-03-03 16:35:01 +0530762 phoneInEcm = PhoneFactory.getPhone(phoneId);
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530763 Log.d(LOG_TAG, "Emergency Callback Mode. phoneId:" + phoneId);
764 if (phoneInEcm != null) {
765 if (TelephonyCapabilities.supportsEcm(phoneInEcm)) {
766 Log.d(LOG_TAG, "Emergency Callback Mode arrived in PhoneApp.");
767 // Start Emergency Callback Mode service
Daniel Bright30efde42020-01-09 22:11:15 -0800768 if (intent.getBooleanExtra(
769 TelephonyManager.EXTRA_PHONE_IN_ECM_STATE, false)) {
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530770 context.startService(new Intent(context,
771 EmergencyCallbackModeService.class));
772 } else {
773 phoneInEcm = null;
774 }
775 } else {
776 // It doesn't make sense to get ACTION_EMERGENCY_CALLBACK_MODE_CHANGED
777 // on a device that doesn't support ECM in the first place.
778 Log.e(LOG_TAG, "Got ACTION_EMERGENCY_CALLBACK_MODE_CHANGED, but "
779 + "ECM isn't supported for phone: " + phoneInEcm.getPhoneName());
780 phoneInEcm = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700781 }
782 } else {
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530783 Log.w(LOG_TAG, "phoneInEcm is null.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700784 }
Jack Yu1a2fc352017-07-14 17:14:37 -0700785 } else if (action.equals(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED)) {
786 // Roaming status could be overridden by carrier config, so we need to update it.
787 if (VDBG) Log.v(LOG_TAG, "carrier config changed.");
788 updateDataRoamingStatus();
chen xubaf9fe52019-07-02 17:28:24 -0700789 updateLimitedSimFunctionForDualSim();
Lei Liu863eac02020-06-24 20:12:55 +0800790 int subId = intent.getIntExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX,
791 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
792 if (SubscriptionManager.isValidSubscriptionId(subId)) {
793 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CARRIER_CONFIG_CHANGED,
794 new Integer(subId)));
795 }
Jack Yu1a2fc352017-07-14 17:14:37 -0700796 } else if (action.equals(TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED)) {
797 // We also need to pay attention when default data subscription changes.
798 if (VDBG) Log.v(LOG_TAG, "default data sub changed.");
799 mDefaultDataSubId = SubscriptionManager.getDefaultDataSubscriptionId();
800 registerSettingsObserver();
801 Phone phone = getPhone(mDefaultDataSubId);
802 if (phone != null) {
803 updateDataRoamingStatus();
804 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700805 }
806 }
807 }
808
Brad Ebinger28626122017-08-10 14:13:35 -0700809 private class SipReceiver extends BroadcastReceiver {
810
811 @Override
812 public void onReceive(Context context, Intent intent) {
813 String action = intent.getAction();
814
815 SipAccountRegistry sipAccountRegistry = SipAccountRegistry.getInstance();
816 if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
817 SipUtil.startSipService();
818 } else if (action.equals(SipManager.ACTION_SIP_SERVICE_UP)
819 || action.equals(SipManager.ACTION_SIP_CALL_OPTION_CHANGED)) {
820 sipAccountRegistry.setup(context);
Grace Jiab95592a2020-01-15 14:12:24 -0800821 } else if (action.equals(SipManager.ACTION_SIP_REMOVE_PROFILE)) {
Brad Ebinger28626122017-08-10 14:13:35 -0700822 if (DBG) {
823 Log.d(LOG_TAG, "SIP_REMOVE_PHONE "
824 + intent.getStringExtra(SipManager.EXTRA_LOCAL_URI));
825 }
826 sipAccountRegistry.removeSipProfile(intent.getStringExtra(
827 SipManager.EXTRA_LOCAL_URI));
828 } else {
829 if (DBG) Log.d(LOG_TAG, "onReceive, action not processed: " + action);
830 }
831 }
832 }
833
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700834 private void handleServiceStateChanged(Intent intent) {
835 /**
836 * This used to handle updating EriTextWidgetProvider this routine
837 * and and listening for ACTION_SERVICE_STATE_CHANGED intents could
838 * be removed. But leaving just in case it might be needed in the near
839 * future.
840 */
841
Jack Yu1a2fc352017-07-14 17:14:37 -0700842 if (VDBG) Log.v(LOG_TAG, "handleServiceStateChanged");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700843 // If service just returned, start sending out the queued messages
Santos Cordonc593d002015-06-03 15:41:15 -0700844 Bundle extras = intent.getExtras();
845 if (extras != null) {
846 ServiceState ss = ServiceState.newFromBundle(extras);
847 if (ss != null) {
848 int state = ss.getState();
Jayachandran C2ef9a482017-05-12 22:07:47 -0700849 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
850 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
851 notificationMgr.updateNetworkSelection(state, subId);
Jack Yu1a2fc352017-07-14 17:14:37 -0700852
853 if (VDBG) {
854 Log.v(LOG_TAG, "subId=" + subId + ",mDefaultDataSubId="
855 + mDefaultDataSubId + ",ss roaming=" + ss.getDataRoaming());
856 }
857 if (subId == mDefaultDataSubId) {
858 updateDataRoamingStatus();
859 }
Santos Cordonc593d002015-06-03 15:41:15 -0700860 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700861 }
862 }
863
Jack Yu1a2fc352017-07-14 17:14:37 -0700864 /**
Jordan Liuff2ccd72019-07-23 15:54:41 -0700865 * @return whether or not we should show a notification when connecting to data roaming if the
866 * user has data roaming enabled
867 */
868 private boolean shouldShowDataConnectedRoaming(int subId) {
869 PersistableBundle config = getCarrierConfigForSubId(subId);
870 return config.getBoolean(CarrierConfigManager
871 .KEY_SHOW_DATA_CONNECTED_ROAMING_NOTIFICATION_BOOL);
872 }
873
874 /**
Jack Yu1a2fc352017-07-14 17:14:37 -0700875 * When roaming, if mobile data cannot be established due to data roaming not enabled, we need
876 * to notify the user so they can enable it through settings. Vise versa if the condition
877 * changes, we need to dismiss the notification.
878 */
879 private void updateDataRoamingStatus() {
880 if (VDBG) Log.v(LOG_TAG, "updateDataRoamingStatus");
881 Phone phone = getPhone(mDefaultDataSubId);
882 if (phone == null) {
883 Log.w(LOG_TAG, "Can't get phone with sub id = " + mDefaultDataSubId);
884 return;
885 }
886
887 DataConnectionReasons reasons = new DataConnectionReasons();
Jack Yu311536f2018-11-26 11:20:48 -0800888 boolean dataAllowed = phone.isDataAllowed(ApnSetting.TYPE_DEFAULT, reasons);
Jack Yu1a2fc352017-07-14 17:14:37 -0700889 mDataRoamingNotifLog.log("dataAllowed=" + dataAllowed + ", reasons=" + reasons);
890 if (VDBG) Log.v(LOG_TAG, "dataAllowed=" + dataAllowed + ", reasons=" + reasons);
Jordan Liuff2ccd72019-07-23 15:54:41 -0700891 if (!dataAllowed && reasons.containsOnly(DataDisallowedReasonType.ROAMING_DISABLED)) {
892 // No need to show it again if we never cancelled it explicitly.
893 if (mPrevRoamingNotification == ROAMING_NOTIFICATION_DISCONNECTED) return;
Jack Yu1a2fc352017-07-14 17:14:37 -0700894 // If the only reason of no data is data roaming disabled, then we notify the user
895 // so the user can turn on data roaming.
Jordan Liuff2ccd72019-07-23 15:54:41 -0700896 mPrevRoamingNotification = ROAMING_NOTIFICATION_DISCONNECTED;
Jack Yu1a2fc352017-07-14 17:14:37 -0700897 Log.d(LOG_TAG, "Show roaming disconnected notification");
Jordan Liuff2ccd72019-07-23 15:54:41 -0700898 mDataRoamingNotifLog.log("Show roaming off.");
Pengquan Meng8783d932017-10-16 14:57:40 -0700899 Message msg = mHandler.obtainMessage(EVENT_DATA_ROAMING_DISCONNECTED);
900 msg.arg1 = mDefaultDataSubId;
901 msg.sendToTarget();
Jordan Liuff2ccd72019-07-23 15:54:41 -0700902 } else if (dataAllowed && dataIsNowRoaming(mDefaultDataSubId)
903 && shouldShowDataConnectedRoaming(mDefaultDataSubId)) {
904 // No need to show it again if we never cancelled it explicitly, or carrier config
905 // indicates this is not needed.
906 if (mPrevRoamingNotification == ROAMING_NOTIFICATION_CONNECTED) return;
907 mPrevRoamingNotification = ROAMING_NOTIFICATION_CONNECTED;
908 Log.d(LOG_TAG, "Show roaming connected notification");
909 mDataRoamingNotifLog.log("Show roaming on.");
910 Message msg = mHandler.obtainMessage(EVENT_DATA_ROAMING_CONNECTED);
911 msg.arg1 = mDefaultDataSubId;
912 msg.sendToTarget();
913 } else if (mPrevRoamingNotification != ROAMING_NOTIFICATION_NO_NOTIFICATION) {
914 // Otherwise we either 1) we are not roaming or 2) roaming is off but ROAMING_DISABLED
915 // is not the only data disable reason. In this case we dismiss the notification we
916 // showed earlier.
917 mPrevRoamingNotification = ROAMING_NOTIFICATION_NO_NOTIFICATION;
918 Log.d(LOG_TAG, "Dismiss roaming notification");
Jack Yu1a2fc352017-07-14 17:14:37 -0700919 mDataRoamingNotifLog.log("Hide. data allowed=" + dataAllowed + ", reasons=" + reasons);
920 mHandler.sendEmptyMessage(EVENT_DATA_ROAMING_OK);
921 }
922 }
923
Jordan Liuff2ccd72019-07-23 15:54:41 -0700924 /**
925 *
926 * @param subId to check roaming on
927 * @return whether we have transitioned to dataRoaming
928 */
929 private boolean dataIsNowRoaming(int subId) {
930 return getPhone(subId).getServiceState().getDataRoaming();
931 }
932
chen xubaf9fe52019-07-02 17:28:24 -0700933 private void updateLimitedSimFunctionForDualSim() {
934 if (DBG) Log.d(LOG_TAG, "updateLimitedSimFunctionForDualSim");
935 // check conditions to display limited SIM function notification under dual SIM
936 SubscriptionManager subMgr = (SubscriptionManager) getSystemService(
937 Context.TELEPHONY_SUBSCRIPTION_SERVICE);
938 List<SubscriptionInfo> subList = subMgr.getActiveSubscriptionInfoList(false);
939 if (subList != null && subList.size() > 1) {
940 CarrierConfigManager configMgr = (CarrierConfigManager)
941 getSystemService(Context.CARRIER_CONFIG_SERVICE);
942 for (SubscriptionInfo info : subList) {
943 PersistableBundle b = configMgr.getConfigForSubId(info.getSubscriptionId());
944 if (b != null) {
945 if (b.getBoolean(CarrierConfigManager
946 .KEY_LIMITED_SIM_FUNCTION_NOTIFICATION_FOR_DSDS_BOOL)) {
947 notificationMgr.showLimitedSimFunctionWarningNotification(
948 info.getSubscriptionId(),
949 info.getDisplayName().toString());
950 } else {
951 notificationMgr.dismissLimitedSimFunctionWarningNotification(
952 info.getSubscriptionId());
953 }
954 }
955 }
956 } else {
957 // cancel notifications for all subs
958 notificationMgr.dismissLimitedSimFunctionWarningNotification(
959 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
960 }
961 notificationMgr.dismissLimitedSimFunctionWarningNotificationForInactiveSubs();
962
963 }
964
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530965 public Phone getPhoneInEcm() {
966 return phoneInEcm;
967 }
968
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700969 /**
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800970 * Triggers a refresh of the message waiting (voicemail) indicator.
971 *
972 * @param subId the subscription id we should refresh the notification for.
973 */
974 public void refreshMwiIndicator(int subId) {
975 notificationMgr.refreshMwi(subId);
976 }
977
978 /**
Pengquan Mengdd9ac822018-09-20 15:25:35 -0700979 * Called when the network selection on the subscription {@code subId} is changed by the user.
980 *
981 * @param subId the subscription id.
982 */
983 public void onNetworkSelectionChanged(int subId) {
984 Phone phone = getPhone(subId);
985 if (phone != null) {
986 notificationMgr.updateNetworkSelection(phone.getServiceState().getState(), subId);
987 } else {
988 Log.w(LOG_TAG, "onNetworkSelectionChanged on null phone, subId: " + subId);
989 }
990 }
991
992 /**
James.cf Lin4b784aa2021-01-31 03:25:15 +0800993 * @return whether the device supports RCS User Capability Exchange or not.
994 */
995 public boolean getDeviceUceEnabled() {
996 return (mTelephonyRcsService == null) ? false : mTelephonyRcsService.isDeviceUceEnabled();
997 }
998
999 /**
1000 * Set the device supports RCS User Capability Exchange.
1001 * @param isEnabled true if the device supports UCE.
1002 */
1003 public void setDeviceUceEnabled(boolean isEnabled) {
1004 if (mTelephonyRcsService != null) {
1005 mTelephonyRcsService.setDeviceUceEnabled(isEnabled);
1006 }
1007 }
1008
1009 /**
Jack Yu1a2fc352017-07-14 17:14:37 -07001010 * Dump the state of the object, add calls to other objects as desired.
1011 *
1012 * @param fd File descriptor
1013 * @param printWriter Print writer
1014 * @param args Arguments
1015 */
1016 public void dump(FileDescriptor fd, PrintWriter printWriter, String[] args) {
1017 IndentingPrintWriter pw = new IndentingPrintWriter(printWriter, " ");
1018 pw.println("------- PhoneGlobals -------");
1019 pw.increaseIndent();
Jordan Liuff2ccd72019-07-23 15:54:41 -07001020 pw.println("mPrevRoamingNotification=" + mPrevRoamingNotification);
Jack Yu1a2fc352017-07-14 17:14:37 -07001021 pw.println("mDefaultDataSubId=" + mDefaultDataSubId);
1022 pw.println("mDataRoamingNotifLog:");
Keun young Parkdebb8532019-05-30 16:35:27 -07001023 pw.println("isSmsCapable=" + TelephonyManager.from(this).isSmsCapable());
Jack Yu1a2fc352017-07-14 17:14:37 -07001024 pw.increaseIndent();
1025 mDataRoamingNotifLog.dump(fd, pw, args);
1026 pw.decreaseIndent();
Brad Ebinger05f52c22019-12-05 13:03:21 -08001027 pw.println("ImsResolver:");
1028 pw.increaseIndent();
1029 try {
Brad Ebingerd1947d82021-05-17 20:54:49 +00001030 if (ImsResolver.getInstance() != null) ImsResolver.getInstance().dump(fd, pw, args);
Brad Ebinger05f52c22019-12-05 13:03:21 -08001031 } catch (Exception e) {
1032 e.printStackTrace();
1033 }
1034 pw.decreaseIndent();
Brad Ebingera68a4972020-01-30 17:31:23 -08001035 pw.println("RcsService:");
1036 try {
1037 if (mTelephonyRcsService != null) mTelephonyRcsService.dump(fd, pw, args);
1038 } catch (Exception e) {
1039 e.printStackTrace();
1040 }
Hunsuk Choi89bd22c2021-11-01 13:04:54 +00001041 pw.println("ImsStateCallbackController:");
1042 try {
1043 if (mImsStateCallbackController != null) mImsStateCallbackController.dump(pw);
1044 } catch (Exception e) {
1045 e.printStackTrace();
1046 }
Jack Yu1a2fc352017-07-14 17:14:37 -07001047 pw.decreaseIndent();
1048 pw.println("------- End PhoneGlobals -------");
1049 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001050}