blob: abbd8165b8182b141ca149971b863168c991d0cf [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;
52import android.util.LocalLog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053import android.util.Log;
Santos Cordone18902f2016-03-22 17:16:04 -070054import android.widget.Toast;
Ta-wei Yenb29425b2016-09-21 17:28:14 -070055
Brad Ebingere3ae65a2020-09-11 12:45:11 -070056import com.android.ims.ImsFeatureBinderRepository;
Siim Sammul182d61b2021-05-04 16:01:42 +010057import com.android.internal.os.BinderCallsStats;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070058import com.android.internal.telephony.CallManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059import com.android.internal.telephony.IccCardConstants;
60import com.android.internal.telephony.MmiCode;
61import com.android.internal.telephony.Phone;
Sarah Chin072ffb22022-05-03 17:58:59 -070062import com.android.internal.telephony.PhoneConfigurationManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063import com.android.internal.telephony.PhoneConstants;
64import com.android.internal.telephony.PhoneFactory;
Jack Yu1a2fc352017-07-14 17:14:37 -070065import com.android.internal.telephony.SettingsObserver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070066import com.android.internal.telephony.TelephonyCapabilities;
Youming Ye47a6adc2018-11-19 15:33:36 -080067import com.android.internal.telephony.TelephonyComponentFactory;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068import com.android.internal.telephony.TelephonyIntents;
Jack Yu3e2ac6e2022-03-26 13:59:45 -070069import com.android.internal.telephony.data.DataEvaluation.DataDisallowedReason;
Brad Ebinger05f52c22019-12-05 13:03:21 -080070import com.android.internal.telephony.ims.ImsResolver;
Tyler Gunn92479152021-01-20 16:30:10 -080071import com.android.internal.telephony.imsphone.ImsPhone;
72import com.android.internal.telephony.imsphone.ImsPhoneCallTracker;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +000073import com.android.internal.telephony.uicc.UiccPort;
Jordan Liu06bdef12021-03-24 17:25:46 -070074import com.android.internal.telephony.uicc.UiccProfile;
Jack Yu1a2fc352017-07-14 17:14:37 -070075import com.android.internal.util.IndentingPrintWriter;
Andrew Leefb7f92e2015-02-26 16:23:32 -080076import com.android.phone.settings.SettingsConstants;
Ta-wei Yenf0a71bc2017-05-17 12:28:28 -070077import com.android.phone.vvm.CarrierVvmPackageInstalledReceiver;
James.cf Linc9f35a42020-01-15 02:35:22 +080078import com.android.services.telephony.rcs.TelephonyRcsService;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070079
Jack Yu1a2fc352017-07-14 17:14:37 -070080import java.io.FileDescriptor;
81import java.io.PrintWriter;
Jordan Liuff2ccd72019-07-23 15:54:41 -070082import java.lang.annotation.Retention;
83import java.lang.annotation.RetentionPolicy;
chen xubaf9fe52019-07-02 17:28:24 -070084import java.util.List;
Jack Yu1a2fc352017-07-14 17:14:37 -070085
Santos Cordon7d4ddf62013-07-10 11:58:08 -070086/**
87 * Global state for the telephony subsystem when running in the primary
88 * phone process.
89 */
Sailesh Nepalbf900542014-07-15 16:18:32 -070090public class PhoneGlobals extends ContextWrapper {
Jack Yu1a2fc352017-07-14 17:14:37 -070091 public static final String LOG_TAG = "PhoneGlobals";
Santos Cordon7d4ddf62013-07-10 11:58:08 -070092
93 /**
94 * Phone app-wide debug level:
95 * 0 - no debug logging
96 * 1 - normal debug logging if ro.debuggable is set (which is true in
97 * "eng" and "userdebug" builds but not "user" builds)
98 * 2 - ultra-verbose debug logging
99 *
100 * Most individual classes in the phone app have a local DBG constant,
101 * typically set to
102 * (PhoneApp.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1)
103 * or else
104 * (PhoneApp.DBG_LEVEL >= 2)
105 * depending on the desired verbosity.
106 *
107 * ***** DO NOT SUBMIT WITH DBG_LEVEL > 0 *************
108 */
Andrew Lee88b51e22014-10-29 15:48:51 -0700109 public static final int DBG_LEVEL = 0;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700110
111 private static final boolean DBG =
112 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
113 private static final boolean VDBG = (PhoneGlobals.DBG_LEVEL >= 2);
114
115 // Message codes; see mHandler below.
116 private static final int EVENT_SIM_NETWORK_LOCKED = 3;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700117 private static final int EVENT_SIM_STATE_CHANGED = 8;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700118 private static final int EVENT_DATA_ROAMING_DISCONNECTED = 10;
Jordan Liuff2ccd72019-07-23 15:54:41 -0700119 private static final int EVENT_DATA_ROAMING_CONNECTED = 11;
120 private static final int EVENT_DATA_ROAMING_OK = 12;
121 private static final int EVENT_UNSOL_CDMA_INFO_RECORD = 13;
Jordan Liuff2ccd72019-07-23 15:54:41 -0700122 private static final int EVENT_DATA_ROAMING_SETTINGS_CHANGED = 15;
123 private static final int EVENT_MOBILE_DATA_SETTINGS_CHANGED = 16;
Lei Liu863eac02020-06-24 20:12:55 +0800124 private static final int EVENT_CARRIER_CONFIG_CHANGED = 17;
Sarah Chin072ffb22022-05-03 17:58:59 -0700125 private static final int EVENT_MULTI_SIM_CONFIG_CHANGED = 18;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700126
127 // The MMI codes are also used by the InCallScreen.
128 public static final int MMI_INITIATE = 51;
129 public static final int MMI_COMPLETE = 52;
130 public static final int MMI_CANCEL = 53;
131 // Don't use message codes larger than 99 here; those are reserved for
132 // the individual Activities of the Phone UI.
133
Santos Cordone18902f2016-03-22 17:16:04 -0700134 public static final int AIRPLANE_ON = 1;
135 public static final int AIRPLANE_OFF = 0;
136
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700137 /**
138 * Allowable values for the wake lock code.
139 * SLEEP means the device can be put to sleep.
140 * PARTIAL means wake the processor, but we display can be kept off.
141 * FULL means wake both the processor and the display.
142 */
143 public enum WakeState {
144 SLEEP,
145 PARTIAL,
146 FULL
147 }
148
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700149 private static PhoneGlobals sMe;
150
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700151 CallManager mCM;
Santos Cordon63a84242013-07-23 13:32:52 -0700152 CallNotifier notifier;
Santos Cordon63a84242013-07-23 13:32:52 -0700153 NotificationMgr notificationMgr;
James.cf Linc9f35a42020-01-15 02:35:22 +0800154 TelephonyRcsService mTelephonyRcsService;
Andrew Lee9431b832015-03-09 18:46:45 -0700155 public PhoneInterfaceManager phoneMgr;
James.cf Linaf3183c2019-10-24 00:59:00 +0800156 public ImsRcsController imsRcsController;
Hunsuk Choi3b742d62021-10-25 19:48:34 +0000157 public ImsStateCallbackController mImsStateCallbackController;
joonhunshincffb7fc2021-11-28 07:32:01 +0000158 public ImsProvisioningController mImsProvisioningController;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800159 CarrierConfigLoader configLoader;
Santos Cordon63a84242013-07-23 13:32:52 -0700160
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530161 private Phone phoneInEcm;
Santos Cordon63a84242013-07-23 13:32:52 -0700162
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700163 static boolean sVoiceCapable = true;
164
Santos Cordonc593d002015-06-03 15:41:15 -0700165 // TODO: Remove, no longer used.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700166 CdmaPhoneCallState cdmaPhoneCallState;
167
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700168 // The currently-active PUK entry activity and progress dialog.
169 // Normally, these are the Emergency Dialer and the subsequent
170 // progress dialog. null if there is are no such objects in
171 // the foreground.
172 private Activity mPUKEntryActivity;
173 private ProgressDialog mPUKEntryProgressDialog;
174
Jordan Liuff2ccd72019-07-23 15:54:41 -0700175 /** @hide */
176 @Retention(RetentionPolicy.SOURCE)
177 @IntDef(prefix = {"ROAMING_NOTIFICATION_"},
178 value = {
179 ROAMING_NOTIFICATION_NO_NOTIFICATION,
180 ROAMING_NOTIFICATION_CONNECTED,
181 ROAMING_NOTIFICATION_DISCONNECTED})
182 public @interface RoamingNotification {}
183
184 private static final int ROAMING_NOTIFICATION_NO_NOTIFICATION = 0;
185 private static final int ROAMING_NOTIFICATION_CONNECTED = 1;
186 private static final int ROAMING_NOTIFICATION_DISCONNECTED = 2;
187
188 @RoamingNotification
189 private int mPrevRoamingNotification = ROAMING_NOTIFICATION_NO_NOTIFICATION;
Jack Yu953f9352017-05-18 14:41:06 -0700190
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700191 private WakeState mWakeState = WakeState.SLEEP;
192
193 private PowerManager mPowerManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700194 private PowerManager.WakeLock mWakeLock;
195 private PowerManager.WakeLock mPartialWakeLock;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700196 private KeyguardManager mKeyguardManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700197
Jack Yu1a2fc352017-07-14 17:14:37 -0700198 private int mDefaultDataSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
199 private final LocalLog mDataRoamingNotifLog = new LocalLog(50);
200
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700201 // Broadcast receiver for various intent broadcasts (see onCreate())
202 private final BroadcastReceiver mReceiver = new PhoneAppBroadcastReceiver();
203
Ta-wei Yenf0a71bc2017-05-17 12:28:28 -0700204 private final CarrierVvmPackageInstalledReceiver mCarrierVvmPackageInstalledReceiver =
205 new CarrierVvmPackageInstalledReceiver();
206
Jack Yu1a2fc352017-07-14 17:14:37 -0700207 private final SettingsObserver mSettingsObserver;
Siim Sammul182d61b2021-05-04 16:01:42 +0100208 private BinderCallsStats.SettingsObserver mBinderCallsSettingsObserver;
Jack Yu1a2fc352017-07-14 17:14:37 -0700209
Sarah Chin072ffb22022-05-03 17:58:59 -0700210 // Mapping of phone ID to the associated TelephonyCallback. These should be registered without
211 // fine or coarse location since we only use ServiceState for
212 private PhoneAppCallback[] mTelephonyCallbacks;
213
214 private class PhoneAppCallback extends TelephonyCallback implements
215 TelephonyCallback.ServiceStateListener {
216 private final int mSubId;
217
218 PhoneAppCallback(int subId) {
219 mSubId = subId;
220 }
221
222 @Override
223 public void onServiceStateChanged(ServiceState serviceState) {
224 // Note when registering that we should be registering with INCLUDE_LOCATION_DATA_NONE.
225 // PhoneGlobals only uses the state and roaming status, which does not require location.
226 handleServiceStateChanged(serviceState, mSubId);
227 }
228
229 public int getSubId() {
230 return mSubId;
231 }
232 }
233
Nazish Tabassum160d2112019-12-23 17:31:33 +0530234 private static class EventSimStateChangedBag {
235 final int mPhoneId;
236 final String mIccStatus;
237
238 EventSimStateChangedBag(int phoneId, String iccStatus) {
239 mPhoneId = phoneId;
240 mIccStatus = iccStatus;
241 }
242 }
243
Jordan Liu06bdef12021-03-24 17:25:46 -0700244 // Some carrier config settings disable the network lock screen, so we call handleSimLock
245 // when either SIM_LOCK or CARRIER_CONFIG changes so that no matter which one happens first,
246 // we still do the right thing
247 private void handleSimLock(int subType, Phone phone) {
248 PersistableBundle cc = getCarrierConfigForSubId(phone.getSubId());
249 if (!CarrierConfigManager.isConfigForIdentifiedCarrier(cc)) {
250 // If we only have the default carrier config just return, to avoid popping up the
251 // the SIM lock screen when it's disabled by the carrier.
252 Log.i(LOG_TAG, "Not showing 'SIM network unlock' screen. Carrier config not loaded");
253 return;
254 }
255 if (cc.getBoolean(CarrierConfigManager.KEY_IGNORE_SIM_NETWORK_LOCKED_EVENTS_BOOL)) {
256 // Some products don't have the concept of a "SIM network lock"
257 Log.i(LOG_TAG, "Not showing 'SIM network unlock' screen. Disabled by carrier config");
258 return;
259 }
260
261 // if passed in subType is unknown, retrieve it here.
262 if (subType == -1) {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000263 final UiccPort uiccPort = phone.getUiccPort();
264 if (uiccPort == null) {
Jordan Liu06bdef12021-03-24 17:25:46 -0700265 Log.e(LOG_TAG,
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000266 "handleSimLock: uiccPort for phone " + phone.getPhoneId() + " is null");
Jordan Liu06bdef12021-03-24 17:25:46 -0700267 return;
268 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000269 final UiccProfile uiccProfile = uiccPort.getUiccProfile();
Jordan Liu06bdef12021-03-24 17:25:46 -0700270 if (uiccProfile == null) {
271 Log.e(LOG_TAG,
272 "handleSimLock: uiccProfile for phone " + phone.getPhoneId() + " is null");
273 return;
274 }
275 subType = uiccProfile.getApplication(
276 uiccProfile.mCurrentAppType).getPersoSubState().ordinal();
277 }
278 // Normal case: show the "SIM network unlock" PIN entry screen.
279 // The user won't be able to do anything else until
280 // they enter a valid SIM network PIN.
281 Log.i(LOG_TAG, "show sim depersonal panel");
282 IccNetworkDepersonalizationPanel.showDialog(phone, subType);
283 }
284
285 private boolean isSimLocked(Phone phone) {
286 TelephonyManager tm = getSystemService(TelephonyManager.class);
287 return tm.createForSubscriptionId(phone.getSubId()).getSimState()
288 == TelephonyManager.SIM_STATE_NETWORK_LOCKED;
289 }
290
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700291 Handler mHandler = new Handler() {
292 @Override
293 public void handleMessage(Message msg) {
294 PhoneConstants.State phoneState;
Jack Yu1a2fc352017-07-14 17:14:37 -0700295 if (VDBG) Log.v(LOG_TAG, "event=" + msg.what);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700296 switch (msg.what) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700297 // TODO: This event should be handled by the lock screen, just
298 // like the "SIM missing" and "Sim locked" cases (bug 1804111).
299 case EVENT_SIM_NETWORK_LOCKED:
Jordan Liu06bdef12021-03-24 17:25:46 -0700300 int subType = (Integer) ((AsyncResult) msg.obj).result;
301 Phone phone = (Phone) ((AsyncResult) msg.obj).userObj;
302 handleSimLock(subType, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700303 break;
304
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700305 case EVENT_DATA_ROAMING_DISCONNECTED:
Jordan Liuff2ccd72019-07-23 15:54:41 -0700306 notificationMgr.showDataRoamingNotification(msg.arg1, false);
307 break;
308
309 case EVENT_DATA_ROAMING_CONNECTED:
310 notificationMgr.showDataRoamingNotification(msg.arg1, true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700311 break;
312
313 case EVENT_DATA_ROAMING_OK:
Jordan Liuff2ccd72019-07-23 15:54:41 -0700314 notificationMgr.hideDataRoamingNotification();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700315 break;
316
317 case MMI_COMPLETE:
318 onMMIComplete((AsyncResult) msg.obj);
319 break;
320
321 case MMI_CANCEL:
Stuart Scottdcf40a92014-12-09 10:45:01 -0800322 PhoneUtils.cancelMmiCode(mCM.getFgPhone());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700323 break;
324
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700325 case EVENT_SIM_STATE_CHANGED:
326 // Marks the event where the SIM goes into ready state.
327 // Right now, this is only used for the PUK-unlocking
328 // process.
Nazish Tabassum160d2112019-12-23 17:31:33 +0530329 EventSimStateChangedBag bag = (EventSimStateChangedBag)msg.obj;
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700330 if (IccCardConstants.INTENT_VALUE_ICC_READY.equals(bag.mIccStatus)
Nazish Tabassum59126a92020-07-03 21:44:49 +0530331 || IccCardConstants.INTENT_VALUE_ICC_LOADED.equals(bag.mIccStatus)
332 || IccCardConstants.INTENT_VALUE_ICC_NOT_READY.equals(bag.mIccStatus)
333 || IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(bag.mIccStatus)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700334 // when the right event is triggered and there
335 // are UI objects in the foreground, we close
336 // them to display the lock panel.
337 if (mPUKEntryActivity != null) {
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700338 Log.i(LOG_TAG, "Dismiss puk entry activity");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700339 mPUKEntryActivity.finish();
340 mPUKEntryActivity = null;
341 }
342 if (mPUKEntryProgressDialog != null) {
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700343 Log.i(LOG_TAG, "Dismiss puk progress dialog");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700344 mPUKEntryProgressDialog.dismiss();
345 mPUKEntryProgressDialog = null;
346 }
Nazish Tabassum59126a92020-07-03 21:44:49 +0530347 Log.i(LOG_TAG, "Dismissing depersonal panel" + (bag.mIccStatus));
Nazish Tabassum160d2112019-12-23 17:31:33 +0530348 IccNetworkDepersonalizationPanel.dialogDismiss(bag.mPhoneId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700349 }
350 break;
351
352 case EVENT_UNSOL_CDMA_INFO_RECORD:
353 //TODO: handle message here;
354 break;
Jack Yu1a2fc352017-07-14 17:14:37 -0700355 case EVENT_DATA_ROAMING_SETTINGS_CHANGED:
356 case EVENT_MOBILE_DATA_SETTINGS_CHANGED:
357 updateDataRoamingStatus();
358 break;
Lei Liu863eac02020-06-24 20:12:55 +0800359 case EVENT_CARRIER_CONFIG_CHANGED:
360 int subId = (Integer) msg.obj;
361 // The voicemail number could be overridden by carrier config, so need to
362 // refresh the message waiting (voicemail) indicator.
363 refreshMwiIndicator(subId);
Jordan Liu06bdef12021-03-24 17:25:46 -0700364 phone = getPhone(subId);
Sarah Chin072ffb22022-05-03 17:58:59 -0700365 if (phone != null) {
366 if (isSimLocked(phone)) {
367 // pass in subType=-1 so handleSimLock can find the actual subType if
368 // needed. This is safe as valid values for subType are >= 0
369 handleSimLock(-1, phone);
370 }
371 TelephonyManager tm = getSystemService(TelephonyManager.class);
372 PhoneAppCallback callback = mTelephonyCallbacks[phone.getPhoneId()];
373 // TODO: We may need to figure out a way to unregister if subId is invalid
374 tm.createForSubscriptionId(callback.getSubId())
375 .unregisterTelephonyCallback(callback);
376 callback = new PhoneAppCallback(subId);
377 tm.createForSubscriptionId(subId).registerTelephonyCallback(
378 TelephonyManager.INCLUDE_LOCATION_DATA_NONE, mHandler::post,
379 callback);
380 mTelephonyCallbacks[phone.getPhoneId()] = callback;
381 }
382 break;
383 case EVENT_MULTI_SIM_CONFIG_CHANGED:
384 int activeModems = (int) ((AsyncResult) msg.obj).result;
385 TelephonyManager tm = getSystemService(TelephonyManager.class);
386 // Unregister all previous callbacks
387 for (int phoneId = 0; phoneId < mTelephonyCallbacks.length; phoneId++) {
388 PhoneAppCallback callback = mTelephonyCallbacks[phoneId];
389 if (callback != null) {
390 tm.createForSubscriptionId(callback.getSubId())
391 .unregisterTelephonyCallback(callback);
392 mTelephonyCallbacks[phoneId] = null;
393 }
394 }
395 // Register callbacks for all active modems
396 for (int phoneId = 0; phoneId < activeModems; phoneId++) {
397 int sub = PhoneFactory.getPhone(phoneId).getSubId();
398 PhoneAppCallback callback = new PhoneAppCallback(sub);
399 tm.createForSubscriptionId(sub).registerTelephonyCallback(
400 TelephonyManager.INCLUDE_LOCATION_DATA_NONE, mHandler::post,
401 callback);
402 mTelephonyCallbacks[phoneId] = callback;
Jordan Liu06bdef12021-03-24 17:25:46 -0700403 }
Lei Liu863eac02020-06-24 20:12:55 +0800404 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700405 }
406 }
407 };
408
409 public PhoneGlobals(Context context) {
410 super(context);
411 sMe = this;
Jack Yu1a2fc352017-07-14 17:14:37 -0700412 mSettingsObserver = new SettingsObserver(context, mHandler);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700413 }
414
415 public void onCreate() {
416 if (VDBG) Log.v(LOG_TAG, "onCreate()...");
417
418 ContentResolver resolver = getContentResolver();
419
Hall Liuaa4211e2021-01-20 15:43:39 -0800420 // Initialize the shim from frameworks/opt/telephony into packages/services/Telephony.
421 TelephonyLocalConnection.setInstance(new LocalConnectionImpl(this));
422
Sarah Chin072ffb22022-05-03 17:58:59 -0700423 TelephonyManager tm = getSystemService(TelephonyManager.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700424 // Cache the "voice capable" flag.
425 // This flag currently comes from a resource (which is
426 // overrideable on a per-product basis):
Sarah Chin072ffb22022-05-03 17:58:59 -0700427 sVoiceCapable = tm.isVoiceCapable();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700428 // ...but this might eventually become a PackageManager "system
429 // feature" instead, in which case we'd do something like:
430 // sVoiceCapable =
431 // getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY_VOICE_CALLS);
432
Stuart Scottdcf40a92014-12-09 10:45:01 -0800433 if (mCM == null) {
Nathan Haroldf9df6ea2019-03-08 11:54:22 -0800434 // Initialize AnomalyReporter early so that it can be used
435 AnomalyReporter.initialize(this);
Nathan Harold0db5c5e2019-01-22 20:18:56 -0800436
Youming Ye47a6adc2018-11-19 15:33:36 -0800437 // Inject telephony component factory if configured using other jars.
438 XmlResourceParser parser = getResources().getXml(R.xml.telephony_injection);
439 TelephonyComponentFactory.getInstance().injectTheComponentFactory(parser);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700440 // Initialize the telephony framework
441 PhoneFactory.makeDefaultPhones(this);
442
Brad Ebinger05f52c22019-12-05 13:03:21 -0800443 // Only bring up ImsResolver if the device supports having an IMS stack.
444 if (getPackageManager().hasSystemFeature(
445 PackageManager.FEATURE_TELEPHONY_IMS)) {
446 // Get the package name of the default IMS implementation.
447 String defaultImsMmtelPackage = getResources().getString(
448 R.string.config_ims_mmtel_package);
449 String defaultImsRcsPackage = getResources().getString(
450 R.string.config_ims_rcs_package);
Brad Ebingerd1947d82021-05-17 20:54:49 +0000451 ImsResolver.make(this, defaultImsMmtelPackage,
Brad Ebingere3ae65a2020-09-11 12:45:11 -0700452 defaultImsRcsPackage, PhoneFactory.getPhones().length,
453 new ImsFeatureBinderRepository());
Brad Ebingerd1947d82021-05-17 20:54:49 +0000454 ImsResolver.getInstance().initialize();
Tyler Gunn92479152021-01-20 16:30:10 -0800455
456 // With the IMS phone created, load static config.xml values from the phone process
457 // so that it can be provided to the ImsPhoneCallTracker.
458 for (Phone p : PhoneFactory.getPhones()) {
459 Phone imsPhone = p.getImsPhone();
460 if (imsPhone != null && imsPhone instanceof ImsPhone) {
461 ImsPhone theImsPhone = (ImsPhone) imsPhone;
462 if (theImsPhone.getCallTracker() instanceof ImsPhoneCallTracker) {
463 ImsPhoneCallTracker ict = (ImsPhoneCallTracker)
464 theImsPhone.getCallTracker();
465
466 ImsPhoneCallTracker.Config config = new ImsPhoneCallTracker.Config();
467 config.isD2DCommunicationSupported = getResources().getBoolean(
468 R.bool.config_use_device_to_device_communication);
469 ict.setConfig(config);
470 }
471 }
472 }
Hui Wang068ab862020-10-31 05:12:53 +0000473 RcsProvisioningMonitor.make(this);
Brad Ebinger05f52c22019-12-05 13:03:21 -0800474 }
475
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700476 // Start TelephonyDebugService After the default phone is created.
477 Intent intent = new Intent(this, TelephonyDebugService.class);
478 startService(intent);
479
480 mCM = CallManager.getInstance();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700481
482 // Create the NotificationMgr singleton, which is used to display
483 // status bar icons and control other status bar behavior.
484 notificationMgr = NotificationMgr.init(this);
485
Anthony Lee03ebdfc2015-07-27 08:12:02 -0700486 // Create an instance of CdmaPhoneCallState and initialize it to IDLE
487 cdmaPhoneCallState = new CdmaPhoneCallState();
488 cdmaPhoneCallState.CdmaPhoneCallStateInit();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700489
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700490 // before registering for phone state changes
491 mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
492 mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, LOG_TAG);
493 // lock used to keep the processor awake, when we don't care for the display.
494 mPartialWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK
495 | PowerManager.ON_AFTER_RELEASE, LOG_TAG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700496
497 mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
498
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -0800499 phoneMgr = PhoneInterfaceManager.init(this);
Santos Cordon406c0342013-08-28 00:07:47 -0700500
James.cf Linaf3183c2019-10-24 00:59:00 +0800501 imsRcsController = ImsRcsController.init(this);
502
James.cf Linc9f35a42020-01-15 02:35:22 +0800503 if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY_IMS)) {
Hunsuk Choi3b742d62021-10-25 19:48:34 +0000504 mImsStateCallbackController =
505 ImsStateCallbackController.make(this, PhoneFactory.getPhones().length);
Brad Ebingera68a4972020-01-30 17:31:23 -0800506 mTelephonyRcsService = new TelephonyRcsService(this,
507 PhoneFactory.getPhones().length);
508 mTelephonyRcsService.initialize();
James.cf Linc9f35a42020-01-15 02:35:22 +0800509 imsRcsController.setRcsService(mTelephonyRcsService);
joonhunshincffb7fc2021-11-28 07:32:01 +0000510 mImsProvisioningController =
511 ImsProvisioningController.make(this, PhoneFactory.getPhones().length);
James.cf Linc9f35a42020-01-15 02:35:22 +0800512 }
513
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800514 configLoader = CarrierConfigLoader.init(this);
515
Jack Yu1a2fc352017-07-14 17:14:37 -0700516 // Create the CallNotifier singleton, which handles
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700517 // asynchronous events from the telephony layer (like
518 // launching the incoming-call UI when an incoming call comes
519 // in.)
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800520 notifier = CallNotifier.init(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700521
Stuart Scottdcf40a92014-12-09 10:45:01 -0800522 PhoneUtils.registerIccStatus(mHandler, EVENT_SIM_NETWORK_LOCKED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700523
524 // register for MMI/USSD
525 mCM.registerForMmiComplete(mHandler, MMI_COMPLETE, null);
526
Qiong Liucc3fe812019-09-23 15:25:44 +0800527 // Initialize cell status using current airplane mode.
Taesu Lee62f826d2020-08-10 16:01:18 +0900528 handleAirplaneModeChange(
529 Settings.Global.getInt(
530 getContentResolver(),
531 Settings.Global.AIRPLANE_MODE_ON,
532 AIRPLANE_OFF)
533 == AIRPLANE_ON);
Qiong Liucc3fe812019-09-23 15:25:44 +0800534
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700535 // Register for misc other intent broadcasts.
536 IntentFilter intentFilter =
537 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700538 intentFilter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
539 intentFilter.addAction(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700540 intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
Jack Yu1a2fc352017-07-14 17:14:37 -0700541 intentFilter.addAction(TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED);
542 intentFilter.addAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700543 registerReceiver(mReceiver, intentFilter);
544
Sarah Chin072ffb22022-05-03 17:58:59 -0700545 PhoneConfigurationManager.registerForMultiSimConfigChange(
546 mHandler, EVENT_MULTI_SIM_CONFIG_CHANGED, null);
547
548 mTelephonyCallbacks = new PhoneAppCallback[tm.getSupportedModemCount()];
Fangyuan Li7f5c2752022-06-29 18:02:31 +0800549 if (tm.getSupportedModemCount() > 0) {
550 for (Phone phone : PhoneFactory.getPhones()) {
551 int subId = phone.getSubId();
552 PhoneAppCallback callback = new PhoneAppCallback(subId);
553 tm.createForSubscriptionId(subId).registerTelephonyCallback(
554 TelephonyManager.INCLUDE_LOCATION_DATA_NONE, mHandler::post, callback);
555 mTelephonyCallbacks[phone.getPhoneId()] = callback;
556 }
Sarah Chin072ffb22022-05-03 17:58:59 -0700557 }
Ta-wei Yenf0a71bc2017-05-17 12:28:28 -0700558 mCarrierVvmPackageInstalledReceiver.register(this);
559
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700560 //set the default values for the preferences in the phone.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700561 PreferenceManager.setDefaultValues(this, R.xml.call_feature_setting, false);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700562 }
563
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700564 // XXX pre-load the SimProvider so that it's ready
565 resolver.getType(Uri.parse("content://icc/adn"));
566
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700567 // TODO: Register for Cdma Information Records
568 // phone.registerCdmaInformationRecord(mHandler, EVENT_UNSOL_CDMA_INFO_RECORD, null);
569
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700570 // Read HAC settings and configure audio hardware
571 if (getResources().getBoolean(R.bool.hac_enabled)) {
Stuart Scottdcf40a92014-12-09 10:45:01 -0800572 int hac = android.provider.Settings.System.getInt(
573 getContentResolver(),
574 android.provider.Settings.System.HEARING_AID,
575 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700576 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Jordan Liuf71aeef2019-08-21 11:46:09 -0700577 audioManager.setParameters(
578 SettingsConstants.HAC_KEY + "=" + (hac == SettingsConstants.HAC_ENABLED
579 ? SettingsConstants.HAC_VAL_ON : SettingsConstants.HAC_VAL_OFF));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700580 }
Siim Sammul182d61b2021-05-04 16:01:42 +0100581
582 // Start tracking Binder latency for the phone process.
583 mBinderCallsSettingsObserver = new BinderCallsStats.SettingsObserver(
584 getApplicationContext(),
Siim Sammula6950092021-06-22 17:14:03 +0100585 new BinderCallsStats(
586 new BinderCallsStats.Injector(),
587 com.android.internal.os.BinderLatencyProto.Dims.TELEPHONY));
Santos Cordonff506f52013-11-21 19:13:19 -0800588 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700589
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700590 /**
591 * Returns the singleton instance of the PhoneApp.
592 */
Sailesh Nepal1eaf22b2014-02-22 17:00:49 -0800593 public static PhoneGlobals getInstance() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700594 if (sMe == null) {
595 throw new IllegalStateException("No PhoneGlobals here!");
596 }
597 return sMe;
598 }
599
600 /**
Stuart Scottdcf40a92014-12-09 10:45:01 -0800601 * Returns the default phone.
602 *
Andrew Lee385019f2014-11-24 14:19:50 -0800603 * WARNING: This method should be used carefully, now that there may be multiple phones.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700604 */
Andrew Lee83383e42014-10-31 12:42:28 -0700605 public static Phone getPhone() {
Stuart Scottdcf40a92014-12-09 10:45:01 -0800606 return PhoneFactory.getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700607 }
608
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800609 public static Phone getPhone(int subId) {
610 return PhoneFactory.getPhone(SubscriptionManager.getPhoneId(subId));
Andrew Lee385019f2014-11-24 14:19:50 -0800611 }
612
Santos Cordonde10b752013-09-19 04:11:33 -0700613 /* package */ CallManager getCallManager() {
614 return mCM;
615 }
616
Chris Manton4e9fa912015-06-19 11:26:57 -0700617 public PersistableBundle getCarrierConfig() {
Shishir Agrawald3480e02016-01-25 13:05:49 -0800618 return getCarrierConfigForSubId(SubscriptionManager.getDefaultSubscriptionId());
Jonathan Basseri89b0ab42015-05-01 10:52:40 -0700619 }
620
Chris Manton4e9fa912015-06-19 11:26:57 -0700621 public PersistableBundle getCarrierConfigForSubId(int subId) {
Philip P. Moltmann0079aae2020-03-05 16:24:02 -0800622 return configLoader.getConfigForSubIdWithFeature(subId, getOpPackageName(),
623 getAttributionTag());
Junda Liu605148f2015-04-28 15:23:40 -0700624 }
625
Jack Yu1a2fc352017-07-14 17:14:37 -0700626 private void registerSettingsObserver() {
627 mSettingsObserver.unobserve();
628 String dataRoamingSetting = Settings.Global.DATA_ROAMING;
629 String mobileDataSetting = Settings.Global.MOBILE_DATA;
630 if (TelephonyManager.getDefault().getSimCount() > 1) {
631 int subId = mDefaultDataSubId;
632 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
633 dataRoamingSetting += subId;
634 mobileDataSetting += subId;
635 }
636 }
637
638 // Listen for user data roaming setting changed event
639 mSettingsObserver.observe(Settings.Global.getUriFor(dataRoamingSetting),
640 EVENT_DATA_ROAMING_SETTINGS_CHANGED);
641
642 // Listen for mobile data setting changed event
643 mSettingsObserver.observe(Settings.Global.getUriFor(mobileDataSetting),
644 EVENT_MOBILE_DATA_SETTINGS_CHANGED);
645 }
646
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700647 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700648 * Sets the activity responsible for un-PUK-blocking the device
649 * so that we may close it when we receive a positive result.
650 * mPUKEntryActivity is also used to indicate to the device that
651 * we are trying to un-PUK-lock the phone. In other words, iff
652 * it is NOT null, then we are trying to unlock and waiting for
653 * the SIM to move to READY state.
654 *
655 * @param activity is the activity to close when PUK has
656 * finished unlocking. Can be set to null to indicate the unlock
657 * or SIM READYing process is over.
658 */
659 void setPukEntryActivity(Activity activity) {
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700660 Log.i(LOG_TAG, "setPukEntryActivity - set to " + (activity == null ? "null" : "activity"));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700661 mPUKEntryActivity = activity;
662 }
663
664 Activity getPUKEntryActivity() {
665 return mPUKEntryActivity;
666 }
667
668 /**
669 * Sets the dialog responsible for notifying the user of un-PUK-
670 * blocking - SIM READYing progress, so that we may dismiss it
671 * when we receive a positive result.
672 *
673 * @param dialog indicates the progress dialog informing the user
674 * of the state of the device. Dismissed upon completion of
675 * READYing process
676 */
677 void setPukEntryProgressDialog(ProgressDialog dialog) {
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700678 Log.i(LOG_TAG, "setPukEntryProgressDialog - set to "
679 + (dialog == null ? "null" : "activity"));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700680 mPUKEntryProgressDialog = dialog;
681 }
682
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700683 KeyguardManager getKeyguardManager() {
684 return mKeyguardManager;
685 }
686
687 private void onMMIComplete(AsyncResult r) {
688 if (VDBG) Log.d(LOG_TAG, "onMMIComplete()...");
689 MmiCode mmiCode = (MmiCode) r.result;
Stuart Scottdcf40a92014-12-09 10:45:01 -0800690 PhoneUtils.displayMMIComplete(mmiCode.getPhone(), getInstance(), mmiCode, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700691 }
692
fionaxu80126972017-02-01 17:01:26 -0800693 private void initForNewRadioTechnology() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700694 if (DBG) Log.d(LOG_TAG, "initForNewRadioTechnology...");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700695 notifier.updateCallNotifierRegistrationsAfterRadioTechnologyChange();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700696 }
697
Taesu Lee62f826d2020-08-10 16:01:18 +0900698 private void handleAirplaneModeChange(boolean isAirplaneNewlyOn) {
699 int cellState =
700 Settings.Global.getInt(
701 getContentResolver(), Settings.Global.CELL_ON, PhoneConstants.CELL_ON_FLAG);
Chris Mantona09f3632016-02-09 14:48:42 -0800702 switch (cellState) {
703 case PhoneConstants.CELL_OFF_FLAG:
704 // Airplane mode does not affect the cell radio if user
705 // has turned it off.
706 break;
707 case PhoneConstants.CELL_ON_FLAG:
Taesu Lee62f826d2020-08-10 16:01:18 +0900708 maybeTurnCellOff(isAirplaneNewlyOn);
Chris Mantona09f3632016-02-09 14:48:42 -0800709 break;
710 case PhoneConstants.CELL_OFF_DUE_TO_AIRPLANE_MODE_FLAG:
Taesu Lee62f826d2020-08-10 16:01:18 +0900711 maybeTurnCellOn(isAirplaneNewlyOn);
Chris Mantona09f3632016-02-09 14:48:42 -0800712 break;
713 }
Hall Liu7f56e1a2019-07-10 14:41:14 -0700714 for (Phone phone : PhoneFactory.getPhones()) {
715 phone.getServiceStateTracker().onAirplaneModeChanged(isAirplaneNewlyOn);
716 }
Chris Mantona09f3632016-02-09 14:48:42 -0800717 }
718
719 /*
720 * Returns true if the radio must be turned off when entering airplane mode.
721 */
Taesu Lee62f826d2020-08-10 16:01:18 +0900722 private boolean isCellOffInAirplaneMode() {
723 String airplaneModeRadios =
724 Settings.Global.getString(
725 getContentResolver(), Settings.Global.AIRPLANE_MODE_RADIOS);
Chris Mantona09f3632016-02-09 14:48:42 -0800726 return airplaneModeRadios == null
727 || airplaneModeRadios.contains(Settings.Global.RADIO_CELL);
728 }
729
Taesu Lee62f826d2020-08-10 16:01:18 +0900730 private void setRadioPowerOff() {
Chris Mantona09f3632016-02-09 14:48:42 -0800731 Log.i(LOG_TAG, "Turning radio off - airplane");
Taesu Lee62f826d2020-08-10 16:01:18 +0900732 Settings.Global.putInt(
733 getContentResolver(),
734 Settings.Global.CELL_ON,
735 PhoneConstants.CELL_OFF_DUE_TO_AIRPLANE_MODE_FLAG);
Chris Mantona09f3632016-02-09 14:48:42 -0800736 Settings.Global.putInt(getContentResolver(), Settings.Global.ENABLE_CELLULAR_ON_BOOT, 0);
Taesu Lee62f826d2020-08-10 16:01:18 +0900737 TelephonyProperties.airplane_mode_on(true); // true means int value 1
Chris Mantona09f3632016-02-09 14:48:42 -0800738 PhoneUtils.setRadioPower(false);
739 }
740
Taesu Lee62f826d2020-08-10 16:01:18 +0900741 private void setRadioPowerOn() {
Chris Mantona09f3632016-02-09 14:48:42 -0800742 Log.i(LOG_TAG, "Turning radio on - airplane");
Taesu Lee62f826d2020-08-10 16:01:18 +0900743 Settings.Global.putInt(
744 getContentResolver(), Settings.Global.CELL_ON, PhoneConstants.CELL_ON_FLAG);
745 Settings.Global.putInt(getContentResolver(), Settings.Global.ENABLE_CELLULAR_ON_BOOT, 1);
Meng Wang61307622019-10-23 14:08:43 -0700746 TelephonyProperties.airplane_mode_on(false); // false means int value 0
Chris Mantona09f3632016-02-09 14:48:42 -0800747 PhoneUtils.setRadioPower(true);
748 }
749
Taesu Lee62f826d2020-08-10 16:01:18 +0900750 private void maybeTurnCellOff(boolean isAirplaneNewlyOn) {
Chris Mantona09f3632016-02-09 14:48:42 -0800751 if (isAirplaneNewlyOn) {
Santos Cordone18902f2016-03-22 17:16:04 -0700752 // If we are trying to turn off the radio, make sure there are no active
753 // emergency calls. If there are, switch airplane mode back to off.
Taesu Lee62f826d2020-08-10 16:01:18 +0900754 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Tyler Gunn900d8fd2018-09-21 09:22:12 -0700755
756 if (tm != null && tm.isInEmergencyCall()) {
Santos Cordone18902f2016-03-22 17:16:04 -0700757 // Switch airplane mode back to off.
Jordan Liu0e819b12019-08-28 15:33:05 -0700758 ConnectivityManager cm =
Taesu Lee62f826d2020-08-10 16:01:18 +0900759 (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
Jordan Liu0e819b12019-08-28 15:33:05 -0700760 cm.setAirplaneMode(false);
Santos Cordone18902f2016-03-22 17:16:04 -0700761 Toast.makeText(this, R.string.radio_off_during_emergency_call, Toast.LENGTH_LONG)
762 .show();
763 Log.i(LOG_TAG, "Ignoring airplane mode: emergency call. Turning airplane off");
Taesu Lee62f826d2020-08-10 16:01:18 +0900764 } else if (isCellOffInAirplaneMode()) {
765 setRadioPowerOff();
Santos Cordone18902f2016-03-22 17:16:04 -0700766 } else {
Chris Mantona09f3632016-02-09 14:48:42 -0800767 Log.i(LOG_TAG, "Ignoring airplane mode: settings prevent cell radio power off");
Santos Cordone18902f2016-03-22 17:16:04 -0700768 }
Chris Mantona09f3632016-02-09 14:48:42 -0800769 }
770 }
771
Taesu Lee62f826d2020-08-10 16:01:18 +0900772 private void maybeTurnCellOn(boolean isAirplaneNewlyOn) {
Chris Mantona09f3632016-02-09 14:48:42 -0800773 if (!isAirplaneNewlyOn) {
Taesu Lee62f826d2020-08-10 16:01:18 +0900774 setRadioPowerOn();
Santos Cordone18902f2016-03-22 17:16:04 -0700775 }
776 }
777
Santos Cordon593ab382013-08-06 21:58:23 -0700778 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700779 * Receiver for misc intent broadcasts the Phone app cares about.
780 */
781 private class PhoneAppBroadcastReceiver extends BroadcastReceiver {
782 @Override
783 public void onReceive(Context context, Intent intent) {
784 String action = intent.getAction();
785 if (action.equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) {
Taesu Lee3dc572b2020-08-07 18:16:22 +0900786 boolean airplaneMode = intent.getBooleanExtra("state", false);
Taesu Lee62f826d2020-08-10 16:01:18 +0900787 handleAirplaneModeChange(airplaneMode);
Amit Mahajanf5d92c82018-11-02 17:44:40 -0700788 } else if (action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) {
789 // re-register as it may be a new IccCard
790 int phoneId = intent.getIntExtra(PhoneConstants.PHONE_KEY,
791 SubscriptionManager.INVALID_PHONE_INDEX);
792 if (SubscriptionManager.isValidPhoneId(phoneId)) {
793 PhoneUtils.unregisterIccStatus(mHandler, phoneId);
794 PhoneUtils.registerIccStatus(mHandler, EVENT_SIM_NETWORK_LOCKED, phoneId);
795 }
Nazish Tabassum160d2112019-12-23 17:31:33 +0530796 String iccStatus = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);
797 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SIM_STATE_CHANGED,
798 new EventSimStateChangedBag(phoneId, iccStatus)));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700799 } else if (action.equals(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED)) {
800 String newPhone = intent.getStringExtra(PhoneConstants.PHONE_NAME_KEY);
fionaxu80126972017-02-01 17:01:26 -0800801 Log.d(LOG_TAG, "Radio technology switched. Now " + newPhone + " is active.");
802 initForNewRadioTechnology();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700803 } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) {
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530804 int phoneId = intent.getIntExtra(PhoneConstants.PHONE_KEY, 0);
Srikanth Chintala079912a2016-03-03 16:35:01 +0530805 phoneInEcm = PhoneFactory.getPhone(phoneId);
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530806 Log.d(LOG_TAG, "Emergency Callback Mode. phoneId:" + phoneId);
807 if (phoneInEcm != null) {
808 if (TelephonyCapabilities.supportsEcm(phoneInEcm)) {
809 Log.d(LOG_TAG, "Emergency Callback Mode arrived in PhoneApp.");
810 // Start Emergency Callback Mode service
Daniel Bright30efde42020-01-09 22:11:15 -0800811 if (intent.getBooleanExtra(
812 TelephonyManager.EXTRA_PHONE_IN_ECM_STATE, false)) {
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530813 context.startService(new Intent(context,
814 EmergencyCallbackModeService.class));
815 } else {
816 phoneInEcm = null;
817 }
818 } else {
819 // It doesn't make sense to get ACTION_EMERGENCY_CALLBACK_MODE_CHANGED
820 // on a device that doesn't support ECM in the first place.
821 Log.e(LOG_TAG, "Got ACTION_EMERGENCY_CALLBACK_MODE_CHANGED, but "
822 + "ECM isn't supported for phone: " + phoneInEcm.getPhoneName());
823 phoneInEcm = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700824 }
825 } else {
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530826 Log.w(LOG_TAG, "phoneInEcm is null.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700827 }
Jack Yu1a2fc352017-07-14 17:14:37 -0700828 } else if (action.equals(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED)) {
829 // Roaming status could be overridden by carrier config, so we need to update it.
830 if (VDBG) Log.v(LOG_TAG, "carrier config changed.");
831 updateDataRoamingStatus();
chen xubaf9fe52019-07-02 17:28:24 -0700832 updateLimitedSimFunctionForDualSim();
Lei Liu863eac02020-06-24 20:12:55 +0800833 int subId = intent.getIntExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX,
834 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
835 if (SubscriptionManager.isValidSubscriptionId(subId)) {
836 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CARRIER_CONFIG_CHANGED,
837 new Integer(subId)));
838 }
Jack Yu1a2fc352017-07-14 17:14:37 -0700839 } else if (action.equals(TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED)) {
840 // We also need to pay attention when default data subscription changes.
841 if (VDBG) Log.v(LOG_TAG, "default data sub changed.");
842 mDefaultDataSubId = SubscriptionManager.getDefaultDataSubscriptionId();
843 registerSettingsObserver();
844 Phone phone = getPhone(mDefaultDataSubId);
845 if (phone != null) {
846 updateDataRoamingStatus();
847 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700848 }
849 }
850 }
851
Sarah Chin072ffb22022-05-03 17:58:59 -0700852 private void handleServiceStateChanged(ServiceState serviceState, int subId) {
Jack Yu1a2fc352017-07-14 17:14:37 -0700853 if (VDBG) Log.v(LOG_TAG, "handleServiceStateChanged");
Sarah Chin072ffb22022-05-03 17:58:59 -0700854 int state = serviceState.getState();
855 notificationMgr.updateNetworkSelection(state, subId);
Jack Yu1a2fc352017-07-14 17:14:37 -0700856
Sarah Chin072ffb22022-05-03 17:58:59 -0700857 if (VDBG) {
858 Log.v(LOG_TAG, "subId=" + subId + ", mDefaultDataSubId="
859 + mDefaultDataSubId + ", ss roaming=" + serviceState.getDataRoaming());
860 }
861 if (subId == mDefaultDataSubId) {
862 updateDataRoamingStatus();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700863 }
864 }
865
Jack Yu1a2fc352017-07-14 17:14:37 -0700866 /**
Jordan Liuff2ccd72019-07-23 15:54:41 -0700867 * @return whether or not we should show a notification when connecting to data roaming if the
868 * user has data roaming enabled
869 */
870 private boolean shouldShowDataConnectedRoaming(int subId) {
871 PersistableBundle config = getCarrierConfigForSubId(subId);
872 return config.getBoolean(CarrierConfigManager
873 .KEY_SHOW_DATA_CONNECTED_ROAMING_NOTIFICATION_BOOL);
874 }
875
876 /**
Jack Yu1a2fc352017-07-14 17:14:37 -0700877 * When roaming, if mobile data cannot be established due to data roaming not enabled, we need
878 * to notify the user so they can enable it through settings. Vise versa if the condition
879 * changes, we need to dismiss the notification.
880 */
881 private void updateDataRoamingStatus() {
882 if (VDBG) Log.v(LOG_TAG, "updateDataRoamingStatus");
883 Phone phone = getPhone(mDefaultDataSubId);
884 if (phone == null) {
885 Log.w(LOG_TAG, "Can't get phone with sub id = " + mDefaultDataSubId);
886 return;
887 }
888
Jack Yu3e2ac6e2022-03-26 13:59:45 -0700889 boolean dataAllowed;
890 boolean notAllowedDueToRoamingOff;
Sarah Chine04784a2022-10-31 20:32:34 -0700891 List<DataDisallowedReason> reasons = phone.getDataNetworkController()
892 .getInternetDataDisallowedReasons();
Jack Yu7968c6d2022-07-31 00:43:21 -0700893 dataAllowed = reasons.isEmpty();
894 notAllowedDueToRoamingOff = (reasons.size() == 1
895 && reasons.contains(DataDisallowedReason.ROAMING_DISABLED));
896 mDataRoamingNotifLog.log("dataAllowed=" + dataAllowed + ", reasons=" + reasons);
897 if (VDBG) Log.v(LOG_TAG, "dataAllowed=" + dataAllowed + ", reasons=" + reasons);
Jack Yu3e2ac6e2022-03-26 13:59:45 -0700898
899 if (!dataAllowed && notAllowedDueToRoamingOff) {
Jordan Liuff2ccd72019-07-23 15:54:41 -0700900 // No need to show it again if we never cancelled it explicitly.
901 if (mPrevRoamingNotification == ROAMING_NOTIFICATION_DISCONNECTED) return;
Jack Yu1a2fc352017-07-14 17:14:37 -0700902 // If the only reason of no data is data roaming disabled, then we notify the user
903 // so the user can turn on data roaming.
Jordan Liuff2ccd72019-07-23 15:54:41 -0700904 mPrevRoamingNotification = ROAMING_NOTIFICATION_DISCONNECTED;
Jack Yu1a2fc352017-07-14 17:14:37 -0700905 Log.d(LOG_TAG, "Show roaming disconnected notification");
Jordan Liuff2ccd72019-07-23 15:54:41 -0700906 mDataRoamingNotifLog.log("Show roaming off.");
Pengquan Meng8783d932017-10-16 14:57:40 -0700907 Message msg = mHandler.obtainMessage(EVENT_DATA_ROAMING_DISCONNECTED);
908 msg.arg1 = mDefaultDataSubId;
909 msg.sendToTarget();
Jordan Liuff2ccd72019-07-23 15:54:41 -0700910 } else if (dataAllowed && dataIsNowRoaming(mDefaultDataSubId)
911 && shouldShowDataConnectedRoaming(mDefaultDataSubId)) {
912 // No need to show it again if we never cancelled it explicitly, or carrier config
913 // indicates this is not needed.
914 if (mPrevRoamingNotification == ROAMING_NOTIFICATION_CONNECTED) return;
915 mPrevRoamingNotification = ROAMING_NOTIFICATION_CONNECTED;
916 Log.d(LOG_TAG, "Show roaming connected notification");
917 mDataRoamingNotifLog.log("Show roaming on.");
918 Message msg = mHandler.obtainMessage(EVENT_DATA_ROAMING_CONNECTED);
919 msg.arg1 = mDefaultDataSubId;
920 msg.sendToTarget();
921 } else if (mPrevRoamingNotification != ROAMING_NOTIFICATION_NO_NOTIFICATION) {
922 // Otherwise we either 1) we are not roaming or 2) roaming is off but ROAMING_DISABLED
923 // is not the only data disable reason. In this case we dismiss the notification we
924 // showed earlier.
925 mPrevRoamingNotification = ROAMING_NOTIFICATION_NO_NOTIFICATION;
926 Log.d(LOG_TAG, "Dismiss roaming notification");
Jack Yu3e2ac6e2022-03-26 13:59:45 -0700927 mDataRoamingNotifLog.log("Hide. data allowed=" + dataAllowed);
Jack Yu1a2fc352017-07-14 17:14:37 -0700928 mHandler.sendEmptyMessage(EVENT_DATA_ROAMING_OK);
929 }
930 }
931
Jordan Liuff2ccd72019-07-23 15:54:41 -0700932 /**
933 *
934 * @param subId to check roaming on
935 * @return whether we have transitioned to dataRoaming
936 */
937 private boolean dataIsNowRoaming(int subId) {
Sarah Chine04784a2022-10-31 20:32:34 -0700938 return getPhone(subId).getServiceState().getDataRoaming();
Jordan Liuff2ccd72019-07-23 15:54:41 -0700939 }
940
chen xubaf9fe52019-07-02 17:28:24 -0700941 private void updateLimitedSimFunctionForDualSim() {
942 if (DBG) Log.d(LOG_TAG, "updateLimitedSimFunctionForDualSim");
943 // check conditions to display limited SIM function notification under dual SIM
944 SubscriptionManager subMgr = (SubscriptionManager) getSystemService(
945 Context.TELEPHONY_SUBSCRIPTION_SERVICE);
946 List<SubscriptionInfo> subList = subMgr.getActiveSubscriptionInfoList(false);
947 if (subList != null && subList.size() > 1) {
948 CarrierConfigManager configMgr = (CarrierConfigManager)
949 getSystemService(Context.CARRIER_CONFIG_SERVICE);
950 for (SubscriptionInfo info : subList) {
951 PersistableBundle b = configMgr.getConfigForSubId(info.getSubscriptionId());
952 if (b != null) {
953 if (b.getBoolean(CarrierConfigManager
954 .KEY_LIMITED_SIM_FUNCTION_NOTIFICATION_FOR_DSDS_BOOL)) {
955 notificationMgr.showLimitedSimFunctionWarningNotification(
956 info.getSubscriptionId(),
957 info.getDisplayName().toString());
958 } else {
959 notificationMgr.dismissLimitedSimFunctionWarningNotification(
960 info.getSubscriptionId());
961 }
962 }
963 }
964 } else {
965 // cancel notifications for all subs
966 notificationMgr.dismissLimitedSimFunctionWarningNotification(
967 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
968 }
969 notificationMgr.dismissLimitedSimFunctionWarningNotificationForInactiveSubs();
970
971 }
972
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530973 public Phone getPhoneInEcm() {
974 return phoneInEcm;
975 }
976
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700977 /**
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800978 * Triggers a refresh of the message waiting (voicemail) indicator.
979 *
980 * @param subId the subscription id we should refresh the notification for.
981 */
982 public void refreshMwiIndicator(int subId) {
983 notificationMgr.refreshMwi(subId);
984 }
985
986 /**
Pengquan Mengdd9ac822018-09-20 15:25:35 -0700987 * Called when the network selection on the subscription {@code subId} is changed by the user.
988 *
989 * @param subId the subscription id.
990 */
991 public void onNetworkSelectionChanged(int subId) {
992 Phone phone = getPhone(subId);
993 if (phone != null) {
994 notificationMgr.updateNetworkSelection(phone.getServiceState().getState(), subId);
995 } else {
996 Log.w(LOG_TAG, "onNetworkSelectionChanged on null phone, subId: " + subId);
997 }
998 }
999
1000 /**
James.cf Lin4b784aa2021-01-31 03:25:15 +08001001 * @return whether the device supports RCS User Capability Exchange or not.
1002 */
1003 public boolean getDeviceUceEnabled() {
1004 return (mTelephonyRcsService == null) ? false : mTelephonyRcsService.isDeviceUceEnabled();
1005 }
1006
1007 /**
1008 * Set the device supports RCS User Capability Exchange.
1009 * @param isEnabled true if the device supports UCE.
1010 */
1011 public void setDeviceUceEnabled(boolean isEnabled) {
1012 if (mTelephonyRcsService != null) {
1013 mTelephonyRcsService.setDeviceUceEnabled(isEnabled);
1014 }
1015 }
1016
1017 /**
Jack Yu1a2fc352017-07-14 17:14:37 -07001018 * Dump the state of the object, add calls to other objects as desired.
1019 *
1020 * @param fd File descriptor
1021 * @param printWriter Print writer
1022 * @param args Arguments
1023 */
1024 public void dump(FileDescriptor fd, PrintWriter printWriter, String[] args) {
1025 IndentingPrintWriter pw = new IndentingPrintWriter(printWriter, " ");
1026 pw.println("------- PhoneGlobals -------");
1027 pw.increaseIndent();
Jordan Liuff2ccd72019-07-23 15:54:41 -07001028 pw.println("mPrevRoamingNotification=" + mPrevRoamingNotification);
Jack Yu1a2fc352017-07-14 17:14:37 -07001029 pw.println("mDefaultDataSubId=" + mDefaultDataSubId);
1030 pw.println("mDataRoamingNotifLog:");
Keun young Parkdebb8532019-05-30 16:35:27 -07001031 pw.println("isSmsCapable=" + TelephonyManager.from(this).isSmsCapable());
Jack Yu1a2fc352017-07-14 17:14:37 -07001032 pw.increaseIndent();
1033 mDataRoamingNotifLog.dump(fd, pw, args);
1034 pw.decreaseIndent();
Brad Ebinger05f52c22019-12-05 13:03:21 -08001035 pw.println("ImsResolver:");
1036 pw.increaseIndent();
1037 try {
Brad Ebingerd1947d82021-05-17 20:54:49 +00001038 if (ImsResolver.getInstance() != null) ImsResolver.getInstance().dump(fd, pw, args);
Brad Ebinger05f52c22019-12-05 13:03:21 -08001039 } catch (Exception e) {
1040 e.printStackTrace();
1041 }
1042 pw.decreaseIndent();
Brad Ebingera68a4972020-01-30 17:31:23 -08001043 pw.println("RcsService:");
1044 try {
1045 if (mTelephonyRcsService != null) mTelephonyRcsService.dump(fd, pw, args);
1046 } catch (Exception e) {
1047 e.printStackTrace();
1048 }
Hunsuk Choi89bd22c2021-11-01 13:04:54 +00001049 pw.println("ImsStateCallbackController:");
1050 try {
1051 if (mImsStateCallbackController != null) mImsStateCallbackController.dump(pw);
1052 } catch (Exception e) {
1053 e.printStackTrace();
1054 }
Jack Yu1a2fc352017-07-14 17:14:37 -07001055 pw.decreaseIndent();
1056 pw.println("------- End PhoneGlobals -------");
1057 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001058}