blob: 99e73ff9f13c63c75fd6edbe2834c1dbbddf1f49 [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;
34import android.os.AsyncResult;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070035import android.os.Handler;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070036import android.os.Message;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070037import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.os.PowerManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070039import android.os.SystemProperties;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.preference.PreferenceManager;
Sanket Padawe4c699232016-02-09 11:07:22 -080041import android.provider.Settings;
Meng Wang61307622019-10-23 14:08:43 -070042import android.sysprop.TelephonyProperties;
Tyler Gunn900d8fd2018-09-21 09:22:12 -070043import android.telecom.TelecomManager;
Nathan Haroldf9df6ea2019-03-08 11:54:22 -080044import android.telephony.AnomalyReporter;
Junda Liu12f7d802015-05-01 12:06:44 -070045import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070046import android.telephony.ServiceState;
chen xubaf9fe52019-07-02 17:28:24 -070047import android.telephony.SubscriptionInfo;
Andrew Lee385019f2014-11-24 14:19:50 -080048import android.telephony.SubscriptionManager;
Sarah Chin072ffb22022-05-03 17:58:59 -070049import android.telephony.TelephonyCallback;
Hall Liuaa4211e2021-01-20 15:43:39 -080050import android.telephony.TelephonyLocalConnection;
Jack Yu1a2fc352017-07-14 17:14:37 -070051import android.telephony.TelephonyManager;
Jack Yu311536f2018-11-26 11:20:48 -080052import android.telephony.data.ApnSetting;
Jack Yu1a2fc352017-07-14 17:14:37 -070053import android.util.LocalLog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import android.util.Log;
Santos Cordone18902f2016-03-22 17:16:04 -070055import android.widget.Toast;
Ta-wei Yenb29425b2016-09-21 17:28:14 -070056
Brad Ebingere3ae65a2020-09-11 12:45:11 -070057import com.android.ims.ImsFeatureBinderRepository;
Siim Sammul182d61b2021-05-04 16:01:42 +010058import com.android.internal.os.BinderCallsStats;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059import com.android.internal.telephony.CallManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060import com.android.internal.telephony.IccCardConstants;
61import com.android.internal.telephony.MmiCode;
62import com.android.internal.telephony.Phone;
Sarah Chin072ffb22022-05-03 17:58:59 -070063import com.android.internal.telephony.PhoneConfigurationManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070064import com.android.internal.telephony.PhoneConstants;
65import com.android.internal.telephony.PhoneFactory;
Jack Yu1a2fc352017-07-14 17:14:37 -070066import com.android.internal.telephony.SettingsObserver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070067import com.android.internal.telephony.TelephonyCapabilities;
Youming Ye47a6adc2018-11-19 15:33:36 -080068import com.android.internal.telephony.TelephonyComponentFactory;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069import com.android.internal.telephony.TelephonyIntents;
Jack Yu3e2ac6e2022-03-26 13:59:45 -070070import com.android.internal.telephony.data.DataEvaluation.DataDisallowedReason;
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;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +000076import com.android.internal.telephony.uicc.UiccPort;
Jordan Liu06bdef12021-03-24 17:25:46 -070077import 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;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070082
Jack Yu1a2fc352017-07-14 17:14:37 -070083import java.io.FileDescriptor;
84import java.io.PrintWriter;
Jordan Liuff2ccd72019-07-23 15:54:41 -070085import java.lang.annotation.Retention;
86import java.lang.annotation.RetentionPolicy;
chen xubaf9fe52019-07-02 17:28:24 -070087import java.util.List;
Jack Yu1a2fc352017-07-14 17:14:37 -070088
Santos Cordon7d4ddf62013-07-10 11:58:08 -070089/**
90 * Global state for the telephony subsystem when running in the primary
91 * phone process.
92 */
Sailesh Nepalbf900542014-07-15 16:18:32 -070093public class PhoneGlobals extends ContextWrapper {
Jack Yu1a2fc352017-07-14 17:14:37 -070094 public static final String LOG_TAG = "PhoneGlobals";
Santos Cordon7d4ddf62013-07-10 11:58:08 -070095
96 /**
97 * Phone app-wide debug level:
98 * 0 - no debug logging
99 * 1 - normal debug logging if ro.debuggable is set (which is true in
100 * "eng" and "userdebug" builds but not "user" builds)
101 * 2 - ultra-verbose debug logging
102 *
103 * Most individual classes in the phone app have a local DBG constant,
104 * typically set to
105 * (PhoneApp.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1)
106 * or else
107 * (PhoneApp.DBG_LEVEL >= 2)
108 * depending on the desired verbosity.
109 *
110 * ***** DO NOT SUBMIT WITH DBG_LEVEL > 0 *************
111 */
Andrew Lee88b51e22014-10-29 15:48:51 -0700112 public static final int DBG_LEVEL = 0;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700113
114 private static final boolean DBG =
115 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
116 private static final boolean VDBG = (PhoneGlobals.DBG_LEVEL >= 2);
117
118 // Message codes; see mHandler below.
119 private static final int EVENT_SIM_NETWORK_LOCKED = 3;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700120 private static final int EVENT_SIM_STATE_CHANGED = 8;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700121 private static final int EVENT_DATA_ROAMING_DISCONNECTED = 10;
Jordan Liuff2ccd72019-07-23 15:54:41 -0700122 private static final int EVENT_DATA_ROAMING_CONNECTED = 11;
123 private static final int EVENT_DATA_ROAMING_OK = 12;
124 private static final int EVENT_UNSOL_CDMA_INFO_RECORD = 13;
Jordan Liuff2ccd72019-07-23 15:54:41 -0700125 private static final int EVENT_DATA_ROAMING_SETTINGS_CHANGED = 15;
126 private static final int EVENT_MOBILE_DATA_SETTINGS_CHANGED = 16;
Lei Liu863eac02020-06-24 20:12:55 +0800127 private static final int EVENT_CARRIER_CONFIG_CHANGED = 17;
Sarah Chin072ffb22022-05-03 17:58:59 -0700128 private static final int EVENT_MULTI_SIM_CONFIG_CHANGED = 18;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700129
130 // The MMI codes are also used by the InCallScreen.
131 public static final int MMI_INITIATE = 51;
132 public static final int MMI_COMPLETE = 52;
133 public static final int MMI_CANCEL = 53;
134 // Don't use message codes larger than 99 here; those are reserved for
135 // the individual Activities of the Phone UI.
136
Santos Cordone18902f2016-03-22 17:16:04 -0700137 public static final int AIRPLANE_ON = 1;
138 public static final int AIRPLANE_OFF = 0;
139
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700140 /**
141 * Allowable values for the wake lock code.
142 * SLEEP means the device can be put to sleep.
143 * PARTIAL means wake the processor, but we display can be kept off.
144 * FULL means wake both the processor and the display.
145 */
146 public enum WakeState {
147 SLEEP,
148 PARTIAL,
149 FULL
150 }
151
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700152 private static PhoneGlobals sMe;
153
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700154 CallManager mCM;
Santos Cordon63a84242013-07-23 13:32:52 -0700155 CallNotifier notifier;
Santos Cordon63a84242013-07-23 13:32:52 -0700156 NotificationMgr notificationMgr;
James.cf Linc9f35a42020-01-15 02:35:22 +0800157 TelephonyRcsService mTelephonyRcsService;
Andrew Lee9431b832015-03-09 18:46:45 -0700158 public PhoneInterfaceManager phoneMgr;
James.cf Linaf3183c2019-10-24 00:59:00 +0800159 public ImsRcsController imsRcsController;
Hunsuk Choi3b742d62021-10-25 19:48:34 +0000160 public ImsStateCallbackController mImsStateCallbackController;
joonhunshincffb7fc2021-11-28 07:32:01 +0000161 public ImsProvisioningController mImsProvisioningController;
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();
206
Ta-wei Yenf0a71bc2017-05-17 12:28:28 -0700207 private final CarrierVvmPackageInstalledReceiver mCarrierVvmPackageInstalledReceiver =
208 new CarrierVvmPackageInstalledReceiver();
209
Jack Yu1a2fc352017-07-14 17:14:37 -0700210 private final SettingsObserver mSettingsObserver;
Siim Sammul182d61b2021-05-04 16:01:42 +0100211 private BinderCallsStats.SettingsObserver mBinderCallsSettingsObserver;
Jack Yu1a2fc352017-07-14 17:14:37 -0700212
Sarah Chin072ffb22022-05-03 17:58:59 -0700213 // Mapping of phone ID to the associated TelephonyCallback. These should be registered without
214 // fine or coarse location since we only use ServiceState for
215 private PhoneAppCallback[] mTelephonyCallbacks;
216
217 private class PhoneAppCallback extends TelephonyCallback implements
218 TelephonyCallback.ServiceStateListener {
219 private final int mSubId;
220
221 PhoneAppCallback(int subId) {
222 mSubId = subId;
223 }
224
225 @Override
226 public void onServiceStateChanged(ServiceState serviceState) {
227 // Note when registering that we should be registering with INCLUDE_LOCATION_DATA_NONE.
228 // PhoneGlobals only uses the state and roaming status, which does not require location.
229 handleServiceStateChanged(serviceState, mSubId);
230 }
231
232 public int getSubId() {
233 return mSubId;
234 }
235 }
236
Nazish Tabassum160d2112019-12-23 17:31:33 +0530237 private static class EventSimStateChangedBag {
238 final int mPhoneId;
239 final String mIccStatus;
240
241 EventSimStateChangedBag(int phoneId, String iccStatus) {
242 mPhoneId = phoneId;
243 mIccStatus = iccStatus;
244 }
245 }
246
Jordan Liu06bdef12021-03-24 17:25:46 -0700247 // Some carrier config settings disable the network lock screen, so we call handleSimLock
248 // when either SIM_LOCK or CARRIER_CONFIG changes so that no matter which one happens first,
249 // we still do the right thing
250 private void handleSimLock(int subType, Phone phone) {
251 PersistableBundle cc = getCarrierConfigForSubId(phone.getSubId());
252 if (!CarrierConfigManager.isConfigForIdentifiedCarrier(cc)) {
253 // If we only have the default carrier config just return, to avoid popping up the
254 // the SIM lock screen when it's disabled by the carrier.
255 Log.i(LOG_TAG, "Not showing 'SIM network unlock' screen. Carrier config not loaded");
256 return;
257 }
258 if (cc.getBoolean(CarrierConfigManager.KEY_IGNORE_SIM_NETWORK_LOCKED_EVENTS_BOOL)) {
259 // Some products don't have the concept of a "SIM network lock"
260 Log.i(LOG_TAG, "Not showing 'SIM network unlock' screen. Disabled by carrier config");
261 return;
262 }
263
264 // if passed in subType is unknown, retrieve it here.
265 if (subType == -1) {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000266 final UiccPort uiccPort = phone.getUiccPort();
267 if (uiccPort == null) {
Jordan Liu06bdef12021-03-24 17:25:46 -0700268 Log.e(LOG_TAG,
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000269 "handleSimLock: uiccPort for phone " + phone.getPhoneId() + " is null");
Jordan Liu06bdef12021-03-24 17:25:46 -0700270 return;
271 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000272 final UiccProfile uiccProfile = uiccPort.getUiccProfile();
Jordan Liu06bdef12021-03-24 17:25:46 -0700273 if (uiccProfile == null) {
274 Log.e(LOG_TAG,
275 "handleSimLock: uiccProfile for phone " + phone.getPhoneId() + " is null");
276 return;
277 }
278 subType = uiccProfile.getApplication(
279 uiccProfile.mCurrentAppType).getPersoSubState().ordinal();
280 }
281 // Normal case: show the "SIM network unlock" PIN entry screen.
282 // The user won't be able to do anything else until
283 // they enter a valid SIM network PIN.
284 Log.i(LOG_TAG, "show sim depersonal panel");
285 IccNetworkDepersonalizationPanel.showDialog(phone, subType);
286 }
287
288 private boolean isSimLocked(Phone phone) {
289 TelephonyManager tm = getSystemService(TelephonyManager.class);
290 return tm.createForSubscriptionId(phone.getSubId()).getSimState()
291 == TelephonyManager.SIM_STATE_NETWORK_LOCKED;
292 }
293
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700294 Handler mHandler = new Handler() {
295 @Override
296 public void handleMessage(Message msg) {
297 PhoneConstants.State phoneState;
Jack Yu1a2fc352017-07-14 17:14:37 -0700298 if (VDBG) Log.v(LOG_TAG, "event=" + msg.what);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700299 switch (msg.what) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700300 // TODO: This event should be handled by the lock screen, just
301 // like the "SIM missing" and "Sim locked" cases (bug 1804111).
302 case EVENT_SIM_NETWORK_LOCKED:
Jordan Liu06bdef12021-03-24 17:25:46 -0700303 int subType = (Integer) ((AsyncResult) msg.obj).result;
304 Phone phone = (Phone) ((AsyncResult) msg.obj).userObj;
305 handleSimLock(subType, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700306 break;
307
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700308 case EVENT_DATA_ROAMING_DISCONNECTED:
Jordan Liuff2ccd72019-07-23 15:54:41 -0700309 notificationMgr.showDataRoamingNotification(msg.arg1, false);
310 break;
311
312 case EVENT_DATA_ROAMING_CONNECTED:
313 notificationMgr.showDataRoamingNotification(msg.arg1, true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700314 break;
315
316 case EVENT_DATA_ROAMING_OK:
Jordan Liuff2ccd72019-07-23 15:54:41 -0700317 notificationMgr.hideDataRoamingNotification();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700318 break;
319
320 case MMI_COMPLETE:
321 onMMIComplete((AsyncResult) msg.obj);
322 break;
323
324 case MMI_CANCEL:
Stuart Scottdcf40a92014-12-09 10:45:01 -0800325 PhoneUtils.cancelMmiCode(mCM.getFgPhone());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700326 break;
327
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700328 case EVENT_SIM_STATE_CHANGED:
329 // Marks the event where the SIM goes into ready state.
330 // Right now, this is only used for the PUK-unlocking
331 // process.
Nazish Tabassum160d2112019-12-23 17:31:33 +0530332 EventSimStateChangedBag bag = (EventSimStateChangedBag)msg.obj;
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700333 if (IccCardConstants.INTENT_VALUE_ICC_READY.equals(bag.mIccStatus)
Nazish Tabassum59126a92020-07-03 21:44:49 +0530334 || IccCardConstants.INTENT_VALUE_ICC_LOADED.equals(bag.mIccStatus)
335 || IccCardConstants.INTENT_VALUE_ICC_NOT_READY.equals(bag.mIccStatus)
336 || IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(bag.mIccStatus)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700337 // when the right event is triggered and there
338 // are UI objects in the foreground, we close
339 // them to display the lock panel.
340 if (mPUKEntryActivity != null) {
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700341 Log.i(LOG_TAG, "Dismiss puk entry activity");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700342 mPUKEntryActivity.finish();
343 mPUKEntryActivity = null;
344 }
345 if (mPUKEntryProgressDialog != null) {
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700346 Log.i(LOG_TAG, "Dismiss puk progress dialog");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700347 mPUKEntryProgressDialog.dismiss();
348 mPUKEntryProgressDialog = null;
349 }
Nazish Tabassum59126a92020-07-03 21:44:49 +0530350 Log.i(LOG_TAG, "Dismissing depersonal panel" + (bag.mIccStatus));
Nazish Tabassum160d2112019-12-23 17:31:33 +0530351 IccNetworkDepersonalizationPanel.dialogDismiss(bag.mPhoneId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700352 }
353 break;
354
355 case EVENT_UNSOL_CDMA_INFO_RECORD:
356 //TODO: handle message here;
357 break;
Jack Yu1a2fc352017-07-14 17:14:37 -0700358 case EVENT_DATA_ROAMING_SETTINGS_CHANGED:
359 case EVENT_MOBILE_DATA_SETTINGS_CHANGED:
360 updateDataRoamingStatus();
361 break;
Lei Liu863eac02020-06-24 20:12:55 +0800362 case EVENT_CARRIER_CONFIG_CHANGED:
363 int subId = (Integer) msg.obj;
364 // The voicemail number could be overridden by carrier config, so need to
365 // refresh the message waiting (voicemail) indicator.
366 refreshMwiIndicator(subId);
Jordan Liu06bdef12021-03-24 17:25:46 -0700367 phone = getPhone(subId);
Sarah Chin072ffb22022-05-03 17:58:59 -0700368 if (phone != null) {
369 if (isSimLocked(phone)) {
370 // pass in subType=-1 so handleSimLock can find the actual subType if
371 // needed. This is safe as valid values for subType are >= 0
372 handleSimLock(-1, phone);
373 }
374 TelephonyManager tm = getSystemService(TelephonyManager.class);
375 PhoneAppCallback callback = mTelephonyCallbacks[phone.getPhoneId()];
376 // TODO: We may need to figure out a way to unregister if subId is invalid
377 tm.createForSubscriptionId(callback.getSubId())
378 .unregisterTelephonyCallback(callback);
379 callback = new PhoneAppCallback(subId);
380 tm.createForSubscriptionId(subId).registerTelephonyCallback(
381 TelephonyManager.INCLUDE_LOCATION_DATA_NONE, mHandler::post,
382 callback);
383 mTelephonyCallbacks[phone.getPhoneId()] = callback;
384 }
385 break;
386 case EVENT_MULTI_SIM_CONFIG_CHANGED:
387 int activeModems = (int) ((AsyncResult) msg.obj).result;
388 TelephonyManager tm = getSystemService(TelephonyManager.class);
389 // Unregister all previous callbacks
390 for (int phoneId = 0; phoneId < mTelephonyCallbacks.length; phoneId++) {
391 PhoneAppCallback callback = mTelephonyCallbacks[phoneId];
392 if (callback != null) {
393 tm.createForSubscriptionId(callback.getSubId())
394 .unregisterTelephonyCallback(callback);
395 mTelephonyCallbacks[phoneId] = null;
396 }
397 }
398 // Register callbacks for all active modems
399 for (int phoneId = 0; phoneId < activeModems; phoneId++) {
400 int sub = PhoneFactory.getPhone(phoneId).getSubId();
401 PhoneAppCallback callback = new PhoneAppCallback(sub);
402 tm.createForSubscriptionId(sub).registerTelephonyCallback(
403 TelephonyManager.INCLUDE_LOCATION_DATA_NONE, mHandler::post,
404 callback);
405 mTelephonyCallbacks[phoneId] = callback;
Jordan Liu06bdef12021-03-24 17:25:46 -0700406 }
Lei Liu863eac02020-06-24 20:12:55 +0800407 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700408 }
409 }
410 };
411
412 public PhoneGlobals(Context context) {
413 super(context);
414 sMe = this;
Jack Yu1a2fc352017-07-14 17:14:37 -0700415 mSettingsObserver = new SettingsObserver(context, mHandler);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700416 }
417
418 public void onCreate() {
419 if (VDBG) Log.v(LOG_TAG, "onCreate()...");
420
421 ContentResolver resolver = getContentResolver();
422
Hall Liuaa4211e2021-01-20 15:43:39 -0800423 // Initialize the shim from frameworks/opt/telephony into packages/services/Telephony.
424 TelephonyLocalConnection.setInstance(new LocalConnectionImpl(this));
425
Sarah Chin072ffb22022-05-03 17:58:59 -0700426 TelephonyManager tm = getSystemService(TelephonyManager.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700427 // Cache the "voice capable" flag.
428 // This flag currently comes from a resource (which is
429 // overrideable on a per-product basis):
Sarah Chin072ffb22022-05-03 17:58:59 -0700430 sVoiceCapable = tm.isVoiceCapable();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700431 // ...but this might eventually become a PackageManager "system
432 // feature" instead, in which case we'd do something like:
433 // sVoiceCapable =
434 // getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY_VOICE_CALLS);
435
Stuart Scottdcf40a92014-12-09 10:45:01 -0800436 if (mCM == null) {
Nathan Haroldf9df6ea2019-03-08 11:54:22 -0800437 // Initialize AnomalyReporter early so that it can be used
438 AnomalyReporter.initialize(this);
Nathan Harold0db5c5e2019-01-22 20:18:56 -0800439
Youming Ye47a6adc2018-11-19 15:33:36 -0800440 // Inject telephony component factory if configured using other jars.
441 XmlResourceParser parser = getResources().getXml(R.xml.telephony_injection);
442 TelephonyComponentFactory.getInstance().injectTheComponentFactory(parser);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700443 // Initialize the telephony framework
444 PhoneFactory.makeDefaultPhones(this);
445
Brad Ebinger05f52c22019-12-05 13:03:21 -0800446 // Only bring up ImsResolver if the device supports having an IMS stack.
447 if (getPackageManager().hasSystemFeature(
448 PackageManager.FEATURE_TELEPHONY_IMS)) {
449 // Get the package name of the default IMS implementation.
450 String defaultImsMmtelPackage = getResources().getString(
451 R.string.config_ims_mmtel_package);
452 String defaultImsRcsPackage = getResources().getString(
453 R.string.config_ims_rcs_package);
Brad Ebingerd1947d82021-05-17 20:54:49 +0000454 ImsResolver.make(this, defaultImsMmtelPackage,
Brad Ebingere3ae65a2020-09-11 12:45:11 -0700455 defaultImsRcsPackage, PhoneFactory.getPhones().length,
456 new ImsFeatureBinderRepository());
Brad Ebingerd1947d82021-05-17 20:54:49 +0000457 ImsResolver.getInstance().initialize();
Tyler Gunn92479152021-01-20 16:30:10 -0800458
459 // With the IMS phone created, load static config.xml values from the phone process
460 // so that it can be provided to the ImsPhoneCallTracker.
461 for (Phone p : PhoneFactory.getPhones()) {
462 Phone imsPhone = p.getImsPhone();
463 if (imsPhone != null && imsPhone instanceof ImsPhone) {
464 ImsPhone theImsPhone = (ImsPhone) imsPhone;
465 if (theImsPhone.getCallTracker() instanceof ImsPhoneCallTracker) {
466 ImsPhoneCallTracker ict = (ImsPhoneCallTracker)
467 theImsPhone.getCallTracker();
468
469 ImsPhoneCallTracker.Config config = new ImsPhoneCallTracker.Config();
470 config.isD2DCommunicationSupported = getResources().getBoolean(
471 R.bool.config_use_device_to_device_communication);
472 ict.setConfig(config);
473 }
474 }
475 }
Hui Wang068ab862020-10-31 05:12:53 +0000476 RcsProvisioningMonitor.make(this);
Brad Ebinger05f52c22019-12-05 13:03:21 -0800477 }
478
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700479 // Start TelephonyDebugService After the default phone is created.
480 Intent intent = new Intent(this, TelephonyDebugService.class);
481 startService(intent);
482
483 mCM = CallManager.getInstance();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700484
485 // Create the NotificationMgr singleton, which is used to display
486 // status bar icons and control other status bar behavior.
487 notificationMgr = NotificationMgr.init(this);
488
Anthony Lee03ebdfc2015-07-27 08:12:02 -0700489 // Create an instance of CdmaPhoneCallState and initialize it to IDLE
490 cdmaPhoneCallState = new CdmaPhoneCallState();
491 cdmaPhoneCallState.CdmaPhoneCallStateInit();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700492
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700493 // before registering for phone state changes
494 mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
495 mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, LOG_TAG);
496 // lock used to keep the processor awake, when we don't care for the display.
497 mPartialWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK
498 | PowerManager.ON_AFTER_RELEASE, LOG_TAG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700499
500 mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
501
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -0800502 phoneMgr = PhoneInterfaceManager.init(this);
Santos Cordon406c0342013-08-28 00:07:47 -0700503
James.cf Linaf3183c2019-10-24 00:59:00 +0800504 imsRcsController = ImsRcsController.init(this);
505
James.cf Linc9f35a42020-01-15 02:35:22 +0800506 if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY_IMS)) {
Hunsuk Choi3b742d62021-10-25 19:48:34 +0000507 mImsStateCallbackController =
508 ImsStateCallbackController.make(this, PhoneFactory.getPhones().length);
Brad Ebingera68a4972020-01-30 17:31:23 -0800509 mTelephonyRcsService = new TelephonyRcsService(this,
510 PhoneFactory.getPhones().length);
511 mTelephonyRcsService.initialize();
James.cf Linc9f35a42020-01-15 02:35:22 +0800512 imsRcsController.setRcsService(mTelephonyRcsService);
joonhunshincffb7fc2021-11-28 07:32:01 +0000513 mImsProvisioningController =
514 ImsProvisioningController.make(this, PhoneFactory.getPhones().length);
James.cf Linc9f35a42020-01-15 02:35:22 +0800515 }
516
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800517 configLoader = CarrierConfigLoader.init(this);
518
Jack Yu1a2fc352017-07-14 17:14:37 -0700519 // Create the CallNotifier singleton, which handles
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700520 // asynchronous events from the telephony layer (like
521 // launching the incoming-call UI when an incoming call comes
522 // in.)
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800523 notifier = CallNotifier.init(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700524
Stuart Scottdcf40a92014-12-09 10:45:01 -0800525 PhoneUtils.registerIccStatus(mHandler, EVENT_SIM_NETWORK_LOCKED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700526
527 // register for MMI/USSD
528 mCM.registerForMmiComplete(mHandler, MMI_COMPLETE, null);
529
Qiong Liucc3fe812019-09-23 15:25:44 +0800530 // Initialize cell status using current airplane mode.
Taesu Lee62f826d2020-08-10 16:01:18 +0900531 handleAirplaneModeChange(
532 Settings.Global.getInt(
533 getContentResolver(),
534 Settings.Global.AIRPLANE_MODE_ON,
535 AIRPLANE_OFF)
536 == AIRPLANE_ON);
Qiong Liucc3fe812019-09-23 15:25:44 +0800537
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700538 // Register for misc other intent broadcasts.
539 IntentFilter intentFilter =
540 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700541 intentFilter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
542 intentFilter.addAction(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700543 intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
Jack Yu1a2fc352017-07-14 17:14:37 -0700544 intentFilter.addAction(TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED);
545 intentFilter.addAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700546 registerReceiver(mReceiver, intentFilter);
547
Sarah Chin072ffb22022-05-03 17:58:59 -0700548 PhoneConfigurationManager.registerForMultiSimConfigChange(
549 mHandler, EVENT_MULTI_SIM_CONFIG_CHANGED, null);
550
551 mTelephonyCallbacks = new PhoneAppCallback[tm.getSupportedModemCount()];
552
553 for (Phone phone : PhoneFactory.getPhones()) {
554 int subId = phone.getSubId();
555 PhoneAppCallback callback = new PhoneAppCallback(subId);
556 tm.createForSubscriptionId(subId).registerTelephonyCallback(
557 TelephonyManager.INCLUDE_LOCATION_DATA_NONE, mHandler::post, callback);
558 mTelephonyCallbacks[phone.getPhoneId()] = callback;
559 }
560
Ta-wei Yenf0a71bc2017-05-17 12:28:28 -0700561 mCarrierVvmPackageInstalledReceiver.register(this);
562
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700563 //set the default values for the preferences in the phone.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700564 PreferenceManager.setDefaultValues(this, R.xml.call_feature_setting, false);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700565 }
566
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700567 // XXX pre-load the SimProvider so that it's ready
568 resolver.getType(Uri.parse("content://icc/adn"));
569
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700570 // TODO: Register for Cdma Information Records
571 // phone.registerCdmaInformationRecord(mHandler, EVENT_UNSOL_CDMA_INFO_RECORD, null);
572
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700573 // Read HAC settings and configure audio hardware
574 if (getResources().getBoolean(R.bool.hac_enabled)) {
Stuart Scottdcf40a92014-12-09 10:45:01 -0800575 int hac = android.provider.Settings.System.getInt(
576 getContentResolver(),
577 android.provider.Settings.System.HEARING_AID,
578 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700579 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Jordan Liuf71aeef2019-08-21 11:46:09 -0700580 audioManager.setParameters(
581 SettingsConstants.HAC_KEY + "=" + (hac == SettingsConstants.HAC_ENABLED
582 ? SettingsConstants.HAC_VAL_ON : SettingsConstants.HAC_VAL_OFF));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700583 }
Siim Sammul182d61b2021-05-04 16:01:42 +0100584
585 // Start tracking Binder latency for the phone process.
586 mBinderCallsSettingsObserver = new BinderCallsStats.SettingsObserver(
587 getApplicationContext(),
Siim Sammula6950092021-06-22 17:14:03 +0100588 new BinderCallsStats(
589 new BinderCallsStats.Injector(),
590 com.android.internal.os.BinderLatencyProto.Dims.TELEPHONY));
Santos Cordonff506f52013-11-21 19:13:19 -0800591 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700592
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700593 /**
594 * Returns the singleton instance of the PhoneApp.
595 */
Sailesh Nepal1eaf22b2014-02-22 17:00:49 -0800596 public static PhoneGlobals getInstance() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700597 if (sMe == null) {
598 throw new IllegalStateException("No PhoneGlobals here!");
599 }
600 return sMe;
601 }
602
603 /**
Stuart Scottdcf40a92014-12-09 10:45:01 -0800604 * Returns the default phone.
605 *
Andrew Lee385019f2014-11-24 14:19:50 -0800606 * WARNING: This method should be used carefully, now that there may be multiple phones.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700607 */
Andrew Lee83383e42014-10-31 12:42:28 -0700608 public static Phone getPhone() {
Stuart Scottdcf40a92014-12-09 10:45:01 -0800609 return PhoneFactory.getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700610 }
611
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800612 public static Phone getPhone(int subId) {
613 return PhoneFactory.getPhone(SubscriptionManager.getPhoneId(subId));
Andrew Lee385019f2014-11-24 14:19:50 -0800614 }
615
Santos Cordonde10b752013-09-19 04:11:33 -0700616 /* package */ CallManager getCallManager() {
617 return mCM;
618 }
619
Chris Manton4e9fa912015-06-19 11:26:57 -0700620 public PersistableBundle getCarrierConfig() {
Shishir Agrawald3480e02016-01-25 13:05:49 -0800621 return getCarrierConfigForSubId(SubscriptionManager.getDefaultSubscriptionId());
Jonathan Basseri89b0ab42015-05-01 10:52:40 -0700622 }
623
Chris Manton4e9fa912015-06-19 11:26:57 -0700624 public PersistableBundle getCarrierConfigForSubId(int subId) {
Philip P. Moltmann0079aae2020-03-05 16:24:02 -0800625 return configLoader.getConfigForSubIdWithFeature(subId, getOpPackageName(),
626 getAttributionTag());
Junda Liu605148f2015-04-28 15:23:40 -0700627 }
628
Jack Yu1a2fc352017-07-14 17:14:37 -0700629 private void registerSettingsObserver() {
630 mSettingsObserver.unobserve();
631 String dataRoamingSetting = Settings.Global.DATA_ROAMING;
632 String mobileDataSetting = Settings.Global.MOBILE_DATA;
633 if (TelephonyManager.getDefault().getSimCount() > 1) {
634 int subId = mDefaultDataSubId;
635 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
636 dataRoamingSetting += subId;
637 mobileDataSetting += subId;
638 }
639 }
640
641 // Listen for user data roaming setting changed event
642 mSettingsObserver.observe(Settings.Global.getUriFor(dataRoamingSetting),
643 EVENT_DATA_ROAMING_SETTINGS_CHANGED);
644
645 // Listen for mobile data setting changed event
646 mSettingsObserver.observe(Settings.Global.getUriFor(mobileDataSetting),
647 EVENT_MOBILE_DATA_SETTINGS_CHANGED);
648 }
649
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700650 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700651 * Sets the activity responsible for un-PUK-blocking the device
652 * so that we may close it when we receive a positive result.
653 * mPUKEntryActivity is also used to indicate to the device that
654 * we are trying to un-PUK-lock the phone. In other words, iff
655 * it is NOT null, then we are trying to unlock and waiting for
656 * the SIM to move to READY state.
657 *
658 * @param activity is the activity to close when PUK has
659 * finished unlocking. Can be set to null to indicate the unlock
660 * or SIM READYing process is over.
661 */
662 void setPukEntryActivity(Activity activity) {
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700663 Log.i(LOG_TAG, "setPukEntryActivity - set to " + (activity == null ? "null" : "activity"));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700664 mPUKEntryActivity = activity;
665 }
666
667 Activity getPUKEntryActivity() {
668 return mPUKEntryActivity;
669 }
670
671 /**
672 * Sets the dialog responsible for notifying the user of un-PUK-
673 * blocking - SIM READYing progress, so that we may dismiss it
674 * when we receive a positive result.
675 *
676 * @param dialog indicates the progress dialog informing the user
677 * of the state of the device. Dismissed upon completion of
678 * READYing process
679 */
680 void setPukEntryProgressDialog(ProgressDialog dialog) {
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700681 Log.i(LOG_TAG, "setPukEntryProgressDialog - set to "
682 + (dialog == null ? "null" : "activity"));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700683 mPUKEntryProgressDialog = dialog;
684 }
685
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700686 KeyguardManager getKeyguardManager() {
687 return mKeyguardManager;
688 }
689
690 private void onMMIComplete(AsyncResult r) {
691 if (VDBG) Log.d(LOG_TAG, "onMMIComplete()...");
692 MmiCode mmiCode = (MmiCode) r.result;
Stuart Scottdcf40a92014-12-09 10:45:01 -0800693 PhoneUtils.displayMMIComplete(mmiCode.getPhone(), getInstance(), mmiCode, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700694 }
695
fionaxu80126972017-02-01 17:01:26 -0800696 private void initForNewRadioTechnology() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700697 if (DBG) Log.d(LOG_TAG, "initForNewRadioTechnology...");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700698 notifier.updateCallNotifierRegistrationsAfterRadioTechnologyChange();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700699 }
700
Taesu Lee62f826d2020-08-10 16:01:18 +0900701 private void handleAirplaneModeChange(boolean isAirplaneNewlyOn) {
702 int cellState =
703 Settings.Global.getInt(
704 getContentResolver(), Settings.Global.CELL_ON, PhoneConstants.CELL_ON_FLAG);
Chris Mantona09f3632016-02-09 14:48:42 -0800705 switch (cellState) {
706 case PhoneConstants.CELL_OFF_FLAG:
707 // Airplane mode does not affect the cell radio if user
708 // has turned it off.
709 break;
710 case PhoneConstants.CELL_ON_FLAG:
Taesu Lee62f826d2020-08-10 16:01:18 +0900711 maybeTurnCellOff(isAirplaneNewlyOn);
Chris Mantona09f3632016-02-09 14:48:42 -0800712 break;
713 case PhoneConstants.CELL_OFF_DUE_TO_AIRPLANE_MODE_FLAG:
Taesu Lee62f826d2020-08-10 16:01:18 +0900714 maybeTurnCellOn(isAirplaneNewlyOn);
Chris Mantona09f3632016-02-09 14:48:42 -0800715 break;
716 }
Hall Liu7f56e1a2019-07-10 14:41:14 -0700717 for (Phone phone : PhoneFactory.getPhones()) {
718 phone.getServiceStateTracker().onAirplaneModeChanged(isAirplaneNewlyOn);
719 }
Chris Mantona09f3632016-02-09 14:48:42 -0800720 }
721
722 /*
723 * Returns true if the radio must be turned off when entering airplane mode.
724 */
Taesu Lee62f826d2020-08-10 16:01:18 +0900725 private boolean isCellOffInAirplaneMode() {
726 String airplaneModeRadios =
727 Settings.Global.getString(
728 getContentResolver(), Settings.Global.AIRPLANE_MODE_RADIOS);
Chris Mantona09f3632016-02-09 14:48:42 -0800729 return airplaneModeRadios == null
730 || airplaneModeRadios.contains(Settings.Global.RADIO_CELL);
731 }
732
Taesu Lee62f826d2020-08-10 16:01:18 +0900733 private void setRadioPowerOff() {
Chris Mantona09f3632016-02-09 14:48:42 -0800734 Log.i(LOG_TAG, "Turning radio off - airplane");
Taesu Lee62f826d2020-08-10 16:01:18 +0900735 Settings.Global.putInt(
736 getContentResolver(),
737 Settings.Global.CELL_ON,
738 PhoneConstants.CELL_OFF_DUE_TO_AIRPLANE_MODE_FLAG);
Chris Mantona09f3632016-02-09 14:48:42 -0800739 Settings.Global.putInt(getContentResolver(), Settings.Global.ENABLE_CELLULAR_ON_BOOT, 0);
Taesu Lee62f826d2020-08-10 16:01:18 +0900740 TelephonyProperties.airplane_mode_on(true); // true means int value 1
Chris Mantona09f3632016-02-09 14:48:42 -0800741 PhoneUtils.setRadioPower(false);
742 }
743
Taesu Lee62f826d2020-08-10 16:01:18 +0900744 private void setRadioPowerOn() {
Chris Mantona09f3632016-02-09 14:48:42 -0800745 Log.i(LOG_TAG, "Turning radio on - airplane");
Taesu Lee62f826d2020-08-10 16:01:18 +0900746 Settings.Global.putInt(
747 getContentResolver(), Settings.Global.CELL_ON, PhoneConstants.CELL_ON_FLAG);
748 Settings.Global.putInt(getContentResolver(), Settings.Global.ENABLE_CELLULAR_ON_BOOT, 1);
Meng Wang61307622019-10-23 14:08:43 -0700749 TelephonyProperties.airplane_mode_on(false); // false means int value 0
Chris Mantona09f3632016-02-09 14:48:42 -0800750 PhoneUtils.setRadioPower(true);
751 }
752
Taesu Lee62f826d2020-08-10 16:01:18 +0900753 private void maybeTurnCellOff(boolean isAirplaneNewlyOn) {
Chris Mantona09f3632016-02-09 14:48:42 -0800754 if (isAirplaneNewlyOn) {
Santos Cordone18902f2016-03-22 17:16:04 -0700755 // If we are trying to turn off the radio, make sure there are no active
756 // emergency calls. If there are, switch airplane mode back to off.
Taesu Lee62f826d2020-08-10 16:01:18 +0900757 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Tyler Gunn900d8fd2018-09-21 09:22:12 -0700758
759 if (tm != null && tm.isInEmergencyCall()) {
Santos Cordone18902f2016-03-22 17:16:04 -0700760 // Switch airplane mode back to off.
Jordan Liu0e819b12019-08-28 15:33:05 -0700761 ConnectivityManager cm =
Taesu Lee62f826d2020-08-10 16:01:18 +0900762 (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
Jordan Liu0e819b12019-08-28 15:33:05 -0700763 cm.setAirplaneMode(false);
Santos Cordone18902f2016-03-22 17:16:04 -0700764 Toast.makeText(this, R.string.radio_off_during_emergency_call, Toast.LENGTH_LONG)
765 .show();
766 Log.i(LOG_TAG, "Ignoring airplane mode: emergency call. Turning airplane off");
Taesu Lee62f826d2020-08-10 16:01:18 +0900767 } else if (isCellOffInAirplaneMode()) {
768 setRadioPowerOff();
Santos Cordone18902f2016-03-22 17:16:04 -0700769 } else {
Chris Mantona09f3632016-02-09 14:48:42 -0800770 Log.i(LOG_TAG, "Ignoring airplane mode: settings prevent cell radio power off");
Santos Cordone18902f2016-03-22 17:16:04 -0700771 }
Chris Mantona09f3632016-02-09 14:48:42 -0800772 }
773 }
774
Taesu Lee62f826d2020-08-10 16:01:18 +0900775 private void maybeTurnCellOn(boolean isAirplaneNewlyOn) {
Chris Mantona09f3632016-02-09 14:48:42 -0800776 if (!isAirplaneNewlyOn) {
Taesu Lee62f826d2020-08-10 16:01:18 +0900777 setRadioPowerOn();
Santos Cordone18902f2016-03-22 17:16:04 -0700778 }
779 }
780
Santos Cordon593ab382013-08-06 21:58:23 -0700781 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700782 * Receiver for misc intent broadcasts the Phone app cares about.
783 */
784 private class PhoneAppBroadcastReceiver extends BroadcastReceiver {
785 @Override
786 public void onReceive(Context context, Intent intent) {
787 String action = intent.getAction();
788 if (action.equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) {
Taesu Lee3dc572b2020-08-07 18:16:22 +0900789 boolean airplaneMode = intent.getBooleanExtra("state", false);
Taesu Lee62f826d2020-08-10 16:01:18 +0900790 handleAirplaneModeChange(airplaneMode);
Amit Mahajanf5d92c82018-11-02 17:44:40 -0700791 } else if (action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) {
792 // re-register as it may be a new IccCard
793 int phoneId = intent.getIntExtra(PhoneConstants.PHONE_KEY,
794 SubscriptionManager.INVALID_PHONE_INDEX);
795 if (SubscriptionManager.isValidPhoneId(phoneId)) {
796 PhoneUtils.unregisterIccStatus(mHandler, phoneId);
797 PhoneUtils.registerIccStatus(mHandler, EVENT_SIM_NETWORK_LOCKED, phoneId);
798 }
Nazish Tabassum160d2112019-12-23 17:31:33 +0530799 String iccStatus = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);
800 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SIM_STATE_CHANGED,
801 new EventSimStateChangedBag(phoneId, iccStatus)));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700802 } else if (action.equals(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED)) {
803 String newPhone = intent.getStringExtra(PhoneConstants.PHONE_NAME_KEY);
fionaxu80126972017-02-01 17:01:26 -0800804 Log.d(LOG_TAG, "Radio technology switched. Now " + newPhone + " is active.");
805 initForNewRadioTechnology();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700806 } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) {
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530807 int phoneId = intent.getIntExtra(PhoneConstants.PHONE_KEY, 0);
Srikanth Chintala079912a2016-03-03 16:35:01 +0530808 phoneInEcm = PhoneFactory.getPhone(phoneId);
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530809 Log.d(LOG_TAG, "Emergency Callback Mode. phoneId:" + phoneId);
810 if (phoneInEcm != null) {
811 if (TelephonyCapabilities.supportsEcm(phoneInEcm)) {
812 Log.d(LOG_TAG, "Emergency Callback Mode arrived in PhoneApp.");
813 // Start Emergency Callback Mode service
Daniel Bright30efde42020-01-09 22:11:15 -0800814 if (intent.getBooleanExtra(
815 TelephonyManager.EXTRA_PHONE_IN_ECM_STATE, false)) {
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530816 context.startService(new Intent(context,
817 EmergencyCallbackModeService.class));
818 } else {
819 phoneInEcm = null;
820 }
821 } else {
822 // It doesn't make sense to get ACTION_EMERGENCY_CALLBACK_MODE_CHANGED
823 // on a device that doesn't support ECM in the first place.
824 Log.e(LOG_TAG, "Got ACTION_EMERGENCY_CALLBACK_MODE_CHANGED, but "
825 + "ECM isn't supported for phone: " + phoneInEcm.getPhoneName());
826 phoneInEcm = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700827 }
828 } else {
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530829 Log.w(LOG_TAG, "phoneInEcm is null.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700830 }
Jack Yu1a2fc352017-07-14 17:14:37 -0700831 } else if (action.equals(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED)) {
832 // Roaming status could be overridden by carrier config, so we need to update it.
833 if (VDBG) Log.v(LOG_TAG, "carrier config changed.");
834 updateDataRoamingStatus();
chen xubaf9fe52019-07-02 17:28:24 -0700835 updateLimitedSimFunctionForDualSim();
Lei Liu863eac02020-06-24 20:12:55 +0800836 int subId = intent.getIntExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX,
837 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
838 if (SubscriptionManager.isValidSubscriptionId(subId)) {
839 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CARRIER_CONFIG_CHANGED,
840 new Integer(subId)));
841 }
Jack Yu1a2fc352017-07-14 17:14:37 -0700842 } else if (action.equals(TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED)) {
843 // We also need to pay attention when default data subscription changes.
844 if (VDBG) Log.v(LOG_TAG, "default data sub changed.");
845 mDefaultDataSubId = SubscriptionManager.getDefaultDataSubscriptionId();
846 registerSettingsObserver();
847 Phone phone = getPhone(mDefaultDataSubId);
848 if (phone != null) {
849 updateDataRoamingStatus();
850 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700851 }
852 }
853 }
854
Sarah Chin072ffb22022-05-03 17:58:59 -0700855 private void handleServiceStateChanged(ServiceState serviceState, int subId) {
Jack Yu1a2fc352017-07-14 17:14:37 -0700856 if (VDBG) Log.v(LOG_TAG, "handleServiceStateChanged");
Sarah Chin072ffb22022-05-03 17:58:59 -0700857 int state = serviceState.getState();
858 notificationMgr.updateNetworkSelection(state, subId);
Jack Yu1a2fc352017-07-14 17:14:37 -0700859
Sarah Chin072ffb22022-05-03 17:58:59 -0700860 if (VDBG) {
861 Log.v(LOG_TAG, "subId=" + subId + ", mDefaultDataSubId="
862 + mDefaultDataSubId + ", ss roaming=" + serviceState.getDataRoaming());
863 }
864 if (subId == mDefaultDataSubId) {
865 updateDataRoamingStatus();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700866 }
867 }
868
Jack Yu1a2fc352017-07-14 17:14:37 -0700869 /**
Jordan Liuff2ccd72019-07-23 15:54:41 -0700870 * @return whether or not we should show a notification when connecting to data roaming if the
871 * user has data roaming enabled
872 */
873 private boolean shouldShowDataConnectedRoaming(int subId) {
874 PersistableBundle config = getCarrierConfigForSubId(subId);
875 return config.getBoolean(CarrierConfigManager
876 .KEY_SHOW_DATA_CONNECTED_ROAMING_NOTIFICATION_BOOL);
877 }
878
879 /**
Jack Yu1a2fc352017-07-14 17:14:37 -0700880 * When roaming, if mobile data cannot be established due to data roaming not enabled, we need
881 * to notify the user so they can enable it through settings. Vise versa if the condition
882 * changes, we need to dismiss the notification.
883 */
884 private void updateDataRoamingStatus() {
885 if (VDBG) Log.v(LOG_TAG, "updateDataRoamingStatus");
886 Phone phone = getPhone(mDefaultDataSubId);
887 if (phone == null) {
888 Log.w(LOG_TAG, "Can't get phone with sub id = " + mDefaultDataSubId);
889 return;
890 }
891
Jack Yu3e2ac6e2022-03-26 13:59:45 -0700892 boolean dataAllowed;
893 boolean notAllowedDueToRoamingOff;
894 if (phone.isUsingNewDataStack()) {
895 List<DataDisallowedReason> reasons = phone.getDataNetworkController()
896 .getInternetDataDisallowedReasons();
897 dataAllowed = reasons.isEmpty();
898 notAllowedDueToRoamingOff = (reasons.size() == 1
899 && reasons.contains(DataDisallowedReason.ROAMING_DISABLED));
900 mDataRoamingNotifLog.log("dataAllowed=" + dataAllowed + ", reasons=" + reasons);
901 if (VDBG) Log.v(LOG_TAG, "dataAllowed=" + dataAllowed + ", reasons=" + reasons);
902 } else {
903 DataConnectionReasons reasons = new DataConnectionReasons();
904 dataAllowed = phone.isDataAllowed(ApnSetting.TYPE_DEFAULT, reasons);
905 notAllowedDueToRoamingOff = reasons.containsOnly(
906 DataDisallowedReasonType.ROAMING_DISABLED);
907 mDataRoamingNotifLog.log("dataAllowed=" + dataAllowed + ", reasons=" + reasons);
908 if (VDBG) Log.v(LOG_TAG, "dataAllowed=" + dataAllowed + ", reasons=" + reasons);
909 }
910
911 if (!dataAllowed && notAllowedDueToRoamingOff) {
Jordan Liuff2ccd72019-07-23 15:54:41 -0700912 // No need to show it again if we never cancelled it explicitly.
913 if (mPrevRoamingNotification == ROAMING_NOTIFICATION_DISCONNECTED) return;
Jack Yu1a2fc352017-07-14 17:14:37 -0700914 // If the only reason of no data is data roaming disabled, then we notify the user
915 // so the user can turn on data roaming.
Jordan Liuff2ccd72019-07-23 15:54:41 -0700916 mPrevRoamingNotification = ROAMING_NOTIFICATION_DISCONNECTED;
Jack Yu1a2fc352017-07-14 17:14:37 -0700917 Log.d(LOG_TAG, "Show roaming disconnected notification");
Jordan Liuff2ccd72019-07-23 15:54:41 -0700918 mDataRoamingNotifLog.log("Show roaming off.");
Pengquan Meng8783d932017-10-16 14:57:40 -0700919 Message msg = mHandler.obtainMessage(EVENT_DATA_ROAMING_DISCONNECTED);
920 msg.arg1 = mDefaultDataSubId;
921 msg.sendToTarget();
Jordan Liuff2ccd72019-07-23 15:54:41 -0700922 } else if (dataAllowed && dataIsNowRoaming(mDefaultDataSubId)
923 && shouldShowDataConnectedRoaming(mDefaultDataSubId)) {
924 // No need to show it again if we never cancelled it explicitly, or carrier config
925 // indicates this is not needed.
926 if (mPrevRoamingNotification == ROAMING_NOTIFICATION_CONNECTED) return;
927 mPrevRoamingNotification = ROAMING_NOTIFICATION_CONNECTED;
928 Log.d(LOG_TAG, "Show roaming connected notification");
929 mDataRoamingNotifLog.log("Show roaming on.");
930 Message msg = mHandler.obtainMessage(EVENT_DATA_ROAMING_CONNECTED);
931 msg.arg1 = mDefaultDataSubId;
932 msg.sendToTarget();
933 } else if (mPrevRoamingNotification != ROAMING_NOTIFICATION_NO_NOTIFICATION) {
934 // Otherwise we either 1) we are not roaming or 2) roaming is off but ROAMING_DISABLED
935 // is not the only data disable reason. In this case we dismiss the notification we
936 // showed earlier.
937 mPrevRoamingNotification = ROAMING_NOTIFICATION_NO_NOTIFICATION;
938 Log.d(LOG_TAG, "Dismiss roaming notification");
Jack Yu3e2ac6e2022-03-26 13:59:45 -0700939 mDataRoamingNotifLog.log("Hide. data allowed=" + dataAllowed);
Jack Yu1a2fc352017-07-14 17:14:37 -0700940 mHandler.sendEmptyMessage(EVENT_DATA_ROAMING_OK);
941 }
942 }
943
Jordan Liuff2ccd72019-07-23 15:54:41 -0700944 /**
945 *
946 * @param subId to check roaming on
947 * @return whether we have transitioned to dataRoaming
948 */
949 private boolean dataIsNowRoaming(int subId) {
950 return getPhone(subId).getServiceState().getDataRoaming();
951 }
952
chen xubaf9fe52019-07-02 17:28:24 -0700953 private void updateLimitedSimFunctionForDualSim() {
954 if (DBG) Log.d(LOG_TAG, "updateLimitedSimFunctionForDualSim");
955 // check conditions to display limited SIM function notification under dual SIM
956 SubscriptionManager subMgr = (SubscriptionManager) getSystemService(
957 Context.TELEPHONY_SUBSCRIPTION_SERVICE);
958 List<SubscriptionInfo> subList = subMgr.getActiveSubscriptionInfoList(false);
959 if (subList != null && subList.size() > 1) {
960 CarrierConfigManager configMgr = (CarrierConfigManager)
961 getSystemService(Context.CARRIER_CONFIG_SERVICE);
962 for (SubscriptionInfo info : subList) {
963 PersistableBundle b = configMgr.getConfigForSubId(info.getSubscriptionId());
964 if (b != null) {
965 if (b.getBoolean(CarrierConfigManager
966 .KEY_LIMITED_SIM_FUNCTION_NOTIFICATION_FOR_DSDS_BOOL)) {
967 notificationMgr.showLimitedSimFunctionWarningNotification(
968 info.getSubscriptionId(),
969 info.getDisplayName().toString());
970 } else {
971 notificationMgr.dismissLimitedSimFunctionWarningNotification(
972 info.getSubscriptionId());
973 }
974 }
975 }
976 } else {
977 // cancel notifications for all subs
978 notificationMgr.dismissLimitedSimFunctionWarningNotification(
979 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
980 }
981 notificationMgr.dismissLimitedSimFunctionWarningNotificationForInactiveSubs();
982
983 }
984
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530985 public Phone getPhoneInEcm() {
986 return phoneInEcm;
987 }
988
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700989 /**
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800990 * Triggers a refresh of the message waiting (voicemail) indicator.
991 *
992 * @param subId the subscription id we should refresh the notification for.
993 */
994 public void refreshMwiIndicator(int subId) {
995 notificationMgr.refreshMwi(subId);
996 }
997
998 /**
Pengquan Mengdd9ac822018-09-20 15:25:35 -0700999 * Called when the network selection on the subscription {@code subId} is changed by the user.
1000 *
1001 * @param subId the subscription id.
1002 */
1003 public void onNetworkSelectionChanged(int subId) {
1004 Phone phone = getPhone(subId);
1005 if (phone != null) {
1006 notificationMgr.updateNetworkSelection(phone.getServiceState().getState(), subId);
1007 } else {
1008 Log.w(LOG_TAG, "onNetworkSelectionChanged on null phone, subId: " + subId);
1009 }
1010 }
1011
1012 /**
James.cf Lin4b784aa2021-01-31 03:25:15 +08001013 * @return whether the device supports RCS User Capability Exchange or not.
1014 */
1015 public boolean getDeviceUceEnabled() {
1016 return (mTelephonyRcsService == null) ? false : mTelephonyRcsService.isDeviceUceEnabled();
1017 }
1018
1019 /**
1020 * Set the device supports RCS User Capability Exchange.
1021 * @param isEnabled true if the device supports UCE.
1022 */
1023 public void setDeviceUceEnabled(boolean isEnabled) {
1024 if (mTelephonyRcsService != null) {
1025 mTelephonyRcsService.setDeviceUceEnabled(isEnabled);
1026 }
1027 }
1028
1029 /**
Jack Yu1a2fc352017-07-14 17:14:37 -07001030 * Dump the state of the object, add calls to other objects as desired.
1031 *
1032 * @param fd File descriptor
1033 * @param printWriter Print writer
1034 * @param args Arguments
1035 */
1036 public void dump(FileDescriptor fd, PrintWriter printWriter, String[] args) {
1037 IndentingPrintWriter pw = new IndentingPrintWriter(printWriter, " ");
1038 pw.println("------- PhoneGlobals -------");
1039 pw.increaseIndent();
Jordan Liuff2ccd72019-07-23 15:54:41 -07001040 pw.println("mPrevRoamingNotification=" + mPrevRoamingNotification);
Jack Yu1a2fc352017-07-14 17:14:37 -07001041 pw.println("mDefaultDataSubId=" + mDefaultDataSubId);
1042 pw.println("mDataRoamingNotifLog:");
Keun young Parkdebb8532019-05-30 16:35:27 -07001043 pw.println("isSmsCapable=" + TelephonyManager.from(this).isSmsCapable());
Jack Yu1a2fc352017-07-14 17:14:37 -07001044 pw.increaseIndent();
1045 mDataRoamingNotifLog.dump(fd, pw, args);
1046 pw.decreaseIndent();
Brad Ebinger05f52c22019-12-05 13:03:21 -08001047 pw.println("ImsResolver:");
1048 pw.increaseIndent();
1049 try {
Brad Ebingerd1947d82021-05-17 20:54:49 +00001050 if (ImsResolver.getInstance() != null) ImsResolver.getInstance().dump(fd, pw, args);
Brad Ebinger05f52c22019-12-05 13:03:21 -08001051 } catch (Exception e) {
1052 e.printStackTrace();
1053 }
1054 pw.decreaseIndent();
Brad Ebingera68a4972020-01-30 17:31:23 -08001055 pw.println("RcsService:");
1056 try {
1057 if (mTelephonyRcsService != null) mTelephonyRcsService.dump(fd, pw, args);
1058 } catch (Exception e) {
1059 e.printStackTrace();
1060 }
Hunsuk Choi89bd22c2021-11-01 13:04:54 +00001061 pw.println("ImsStateCallbackController:");
1062 try {
1063 if (mImsStateCallbackController != null) mImsStateCallbackController.dump(pw);
1064 } catch (Exception e) {
1065 e.printStackTrace();
1066 }
Jack Yu1a2fc352017-07-14 17:14:37 -07001067 pw.decreaseIndent();
1068 pw.println("------- End PhoneGlobals -------");
1069 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001070}