blob: 5c9759763233a583374a0c9ddf8b790cd16b7b64 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
Jordan Liuff2ccd72019-07-23 15:54:41 -070019import android.annotation.IntDef;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070020import android.app.Activity;
21import android.app.KeyguardManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.app.ProgressDialog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import android.content.BroadcastReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import android.content.ContentResolver;
25import android.content.Context;
26import android.content.ContextWrapper;
27import android.content.Intent;
28import android.content.IntentFilter;
Brad Ebinger05f52c22019-12-05 13:03:21 -080029import android.content.pm.PackageManager;
Youming Ye47a6adc2018-11-19 15:33:36 -080030import android.content.res.XmlResourceParser;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070031import android.media.AudioManager;
Santos Cordone18902f2016-03-22 17:16:04 -070032import android.net.ConnectivityManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070033import android.net.Uri;
Brad Ebinger533c6c12017-08-02 11:38:48 -070034import android.net.sip.SipManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070035import android.os.AsyncResult;
Junda Liu605148f2015-04-28 15:23:40 -070036import android.os.Bundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070037import android.os.Handler;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.os.Message;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070039import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.os.PowerManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.os.SystemProperties;
Brad Ebinger3fa43462016-04-12 16:06:48 -070042import android.os.UserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.preference.PreferenceManager;
Sanket Padawe4c699232016-02-09 11:07:22 -080044import android.provider.Settings;
Meng Wang61307622019-10-23 14:08:43 -070045import android.sysprop.TelephonyProperties;
Tyler Gunn900d8fd2018-09-21 09:22:12 -070046import android.telecom.TelecomManager;
Nathan Haroldf9df6ea2019-03-08 11:54:22 -080047import android.telephony.AnomalyReporter;
Junda Liu12f7d802015-05-01 12:06:44 -070048import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070049import android.telephony.ServiceState;
chen xubaf9fe52019-07-02 17:28:24 -070050import android.telephony.SubscriptionInfo;
Andrew Lee385019f2014-11-24 14:19:50 -080051import android.telephony.SubscriptionManager;
Jack Yu1a2fc352017-07-14 17:14:37 -070052import android.telephony.TelephonyManager;
Jack Yu311536f2018-11-26 11:20:48 -080053import android.telephony.data.ApnSetting;
Jack Yu1a2fc352017-07-14 17:14:37 -070054import android.util.LocalLog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055import android.util.Log;
Santos Cordone18902f2016-03-22 17:16:04 -070056import android.widget.Toast;
Ta-wei Yenb29425b2016-09-21 17:28:14 -070057
Brad Ebingere3ae65a2020-09-11 12:45:11 -070058import com.android.ims.ImsFeatureBinderRepository;
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;
63import 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 Yu1a2fc352017-07-14 17:14:37 -070069import com.android.internal.telephony.dataconnection.DataConnectionReasons;
70import com.android.internal.telephony.dataconnection.DataConnectionReasons.DataDisallowedReasonType;
Brad Ebinger05f52c22019-12-05 13:03:21 -080071import com.android.internal.telephony.ims.ImsResolver;
Tyler Gunn92479152021-01-20 16:30:10 -080072import com.android.internal.telephony.imsphone.ImsPhone;
73import com.android.internal.telephony.imsphone.ImsPhoneCallTracker;
Jack Yu1a2fc352017-07-14 17:14:37 -070074import com.android.internal.util.IndentingPrintWriter;
Andrew Leefb7f92e2015-02-26 16:23:32 -080075import com.android.phone.settings.SettingsConstants;
Ta-wei Yenf0a71bc2017-05-17 12:28:28 -070076import com.android.phone.vvm.CarrierVvmPackageInstalledReceiver;
James.cf Linc9f35a42020-01-15 02:35:22 +080077import com.android.services.telephony.rcs.TelephonyRcsService;
Brad Ebinger28626122017-08-10 14:13:35 -070078import com.android.services.telephony.sip.SipAccountRegistry;
Brad Ebinger3fa43462016-04-12 16:06:48 -070079import com.android.services.telephony.sip.SipUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070080
Jack Yu1a2fc352017-07-14 17:14:37 -070081import java.io.FileDescriptor;
82import java.io.PrintWriter;
Jordan Liuff2ccd72019-07-23 15:54:41 -070083import java.lang.annotation.Retention;
84import java.lang.annotation.RetentionPolicy;
chen xubaf9fe52019-07-02 17:28:24 -070085import java.util.List;
Jack Yu1a2fc352017-07-14 17:14:37 -070086
Santos Cordon7d4ddf62013-07-10 11:58:08 -070087/**
88 * Global state for the telephony subsystem when running in the primary
89 * phone process.
90 */
Sailesh Nepalbf900542014-07-15 16:18:32 -070091public class PhoneGlobals extends ContextWrapper {
Jack Yu1a2fc352017-07-14 17:14:37 -070092 public static final String LOG_TAG = "PhoneGlobals";
Santos Cordon7d4ddf62013-07-10 11:58:08 -070093
94 /**
95 * Phone app-wide debug level:
96 * 0 - no debug logging
97 * 1 - normal debug logging if ro.debuggable is set (which is true in
98 * "eng" and "userdebug" builds but not "user" builds)
99 * 2 - ultra-verbose debug logging
100 *
101 * Most individual classes in the phone app have a local DBG constant,
102 * typically set to
103 * (PhoneApp.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1)
104 * or else
105 * (PhoneApp.DBG_LEVEL >= 2)
106 * depending on the desired verbosity.
107 *
108 * ***** DO NOT SUBMIT WITH DBG_LEVEL > 0 *************
109 */
Andrew Lee88b51e22014-10-29 15:48:51 -0700110 public static final int DBG_LEVEL = 0;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700111
112 private static final boolean DBG =
113 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
114 private static final boolean VDBG = (PhoneGlobals.DBG_LEVEL >= 2);
115
116 // Message codes; see mHandler below.
117 private static final int EVENT_SIM_NETWORK_LOCKED = 3;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700118 private static final int EVENT_SIM_STATE_CHANGED = 8;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700119 private static final int EVENT_DATA_ROAMING_DISCONNECTED = 10;
Jordan Liuff2ccd72019-07-23 15:54:41 -0700120 private static final int EVENT_DATA_ROAMING_CONNECTED = 11;
121 private static final int EVENT_DATA_ROAMING_OK = 12;
122 private static final int EVENT_UNSOL_CDMA_INFO_RECORD = 13;
123 private static final int EVENT_RESTART_SIP = 14;
124 private static final int EVENT_DATA_ROAMING_SETTINGS_CHANGED = 15;
125 private static final int EVENT_MOBILE_DATA_SETTINGS_CHANGED = 16;
Lei Liu863eac02020-06-24 20:12:55 +0800126 private static final int EVENT_CARRIER_CONFIG_CHANGED = 17;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700127
128 // The MMI codes are also used by the InCallScreen.
129 public static final int MMI_INITIATE = 51;
130 public static final int MMI_COMPLETE = 52;
131 public static final int MMI_CANCEL = 53;
132 // Don't use message codes larger than 99 here; those are reserved for
133 // the individual Activities of the Phone UI.
134
Santos Cordone18902f2016-03-22 17:16:04 -0700135 public static final int AIRPLANE_ON = 1;
136 public static final int AIRPLANE_OFF = 0;
137
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700138 /**
139 * Allowable values for the wake lock code.
140 * SLEEP means the device can be put to sleep.
141 * PARTIAL means wake the processor, but we display can be kept off.
142 * FULL means wake both the processor and the display.
143 */
144 public enum WakeState {
145 SLEEP,
146 PARTIAL,
147 FULL
148 }
149
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700150 private static PhoneGlobals sMe;
151
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700152 CallManager mCM;
Santos Cordon63a84242013-07-23 13:32:52 -0700153 CallNotifier notifier;
154 CallerInfoCache callerInfoCache;
Santos Cordon63a84242013-07-23 13:32:52 -0700155 NotificationMgr notificationMgr;
Brad Ebinger05f52c22019-12-05 13:03:21 -0800156 ImsResolver mImsResolver;
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;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800160 CarrierConfigLoader configLoader;
Santos Cordon63a84242013-07-23 13:32:52 -0700161
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530162 private Phone phoneInEcm;
Santos Cordon63a84242013-07-23 13:32:52 -0700163
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700164 static boolean sVoiceCapable = true;
165
Santos Cordonc593d002015-06-03 15:41:15 -0700166 // TODO: Remove, no longer used.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700167 CdmaPhoneCallState cdmaPhoneCallState;
168
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700169 // The currently-active PUK entry activity and progress dialog.
170 // Normally, these are the Emergency Dialer and the subsequent
171 // progress dialog. null if there is are no such objects in
172 // the foreground.
173 private Activity mPUKEntryActivity;
174 private ProgressDialog mPUKEntryProgressDialog;
175
Jordan Liuff2ccd72019-07-23 15:54:41 -0700176 /** @hide */
177 @Retention(RetentionPolicy.SOURCE)
178 @IntDef(prefix = {"ROAMING_NOTIFICATION_"},
179 value = {
180 ROAMING_NOTIFICATION_NO_NOTIFICATION,
181 ROAMING_NOTIFICATION_CONNECTED,
182 ROAMING_NOTIFICATION_DISCONNECTED})
183 public @interface RoamingNotification {}
184
185 private static final int ROAMING_NOTIFICATION_NO_NOTIFICATION = 0;
186 private static final int ROAMING_NOTIFICATION_CONNECTED = 1;
187 private static final int ROAMING_NOTIFICATION_DISCONNECTED = 2;
188
189 @RoamingNotification
190 private int mPrevRoamingNotification = ROAMING_NOTIFICATION_NO_NOTIFICATION;
Jack Yu953f9352017-05-18 14:41:06 -0700191
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700192 private WakeState mWakeState = WakeState.SLEEP;
193
194 private PowerManager mPowerManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700195 private PowerManager.WakeLock mWakeLock;
196 private PowerManager.WakeLock mPartialWakeLock;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700197 private KeyguardManager mKeyguardManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700198
Jack Yu1a2fc352017-07-14 17:14:37 -0700199 private int mDefaultDataSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
200 private final LocalLog mDataRoamingNotifLog = new LocalLog(50);
201
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700202 // Broadcast receiver for various intent broadcasts (see onCreate())
203 private final BroadcastReceiver mReceiver = new PhoneAppBroadcastReceiver();
Brad Ebinger533c6c12017-08-02 11:38:48 -0700204 // Broadcast receiver for SIP based intents (see onCreate())
Brad Ebinger28626122017-08-10 14:13:35 -0700205 private final SipReceiver mSipReceiver = new SipReceiver();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700206
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;
211
Nazish Tabassum160d2112019-12-23 17:31:33 +0530212 private static class EventSimStateChangedBag {
213 final int mPhoneId;
214 final String mIccStatus;
215
216 EventSimStateChangedBag(int phoneId, String iccStatus) {
217 mPhoneId = phoneId;
218 mIccStatus = iccStatus;
219 }
220 }
221
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700222 Handler mHandler = new Handler() {
223 @Override
224 public void handleMessage(Message msg) {
225 PhoneConstants.State phoneState;
Jack Yu1a2fc352017-07-14 17:14:37 -0700226 if (VDBG) Log.v(LOG_TAG, "event=" + msg.what);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700227 switch (msg.what) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700228 // TODO: This event should be handled by the lock screen, just
229 // like the "SIM missing" and "Sim locked" cases (bug 1804111).
230 case EVENT_SIM_NETWORK_LOCKED:
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700231 if (getCarrierConfig().getBoolean(
232 CarrierConfigManager.KEY_IGNORE_SIM_NETWORK_LOCKED_EVENTS_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700233 // Some products don't have the concept of a "SIM network lock"
234 Log.i(LOG_TAG, "Ignoring EVENT_SIM_NETWORK_LOCKED event; "
Eric Erfanian945409f2018-02-21 19:26:15 +0000235 + "not showing 'SIM network unlock' PIN entry screen");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700236 } else {
237 // Normal case: show the "SIM network unlock" PIN entry screen.
238 // The user won't be able to do anything else until
239 // they enter a valid SIM network PIN.
240 Log.i(LOG_TAG, "show sim depersonal panel");
Biljana Maneva96291112018-01-30 13:12:59 +0800241 Phone phone = (Phone) ((AsyncResult) msg.obj).userObj;
Nazish Tabassum160d2112019-12-23 17:31:33 +0530242 int subType = (Integer)((AsyncResult)msg.obj).result;
243 IccNetworkDepersonalizationPanel.showDialog(phone, subType);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700244 }
245 break;
246
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700247 case EVENT_DATA_ROAMING_DISCONNECTED:
Jordan Liuff2ccd72019-07-23 15:54:41 -0700248 notificationMgr.showDataRoamingNotification(msg.arg1, false);
249 break;
250
251 case EVENT_DATA_ROAMING_CONNECTED:
252 notificationMgr.showDataRoamingNotification(msg.arg1, true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700253 break;
254
255 case EVENT_DATA_ROAMING_OK:
Jordan Liuff2ccd72019-07-23 15:54:41 -0700256 notificationMgr.hideDataRoamingNotification();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700257 break;
258
259 case MMI_COMPLETE:
260 onMMIComplete((AsyncResult) msg.obj);
261 break;
262
263 case MMI_CANCEL:
Stuart Scottdcf40a92014-12-09 10:45:01 -0800264 PhoneUtils.cancelMmiCode(mCM.getFgPhone());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700265 break;
266
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700267 case EVENT_SIM_STATE_CHANGED:
268 // Marks the event where the SIM goes into ready state.
269 // Right now, this is only used for the PUK-unlocking
270 // process.
Nazish Tabassum160d2112019-12-23 17:31:33 +0530271 EventSimStateChangedBag bag = (EventSimStateChangedBag)msg.obj;
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700272 if (IccCardConstants.INTENT_VALUE_ICC_READY.equals(bag.mIccStatus)
Nazish Tabassum59126a92020-07-03 21:44:49 +0530273 || IccCardConstants.INTENT_VALUE_ICC_LOADED.equals(bag.mIccStatus)
274 || IccCardConstants.INTENT_VALUE_ICC_NOT_READY.equals(bag.mIccStatus)
275 || IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(bag.mIccStatus)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700276 // when the right event is triggered and there
277 // are UI objects in the foreground, we close
278 // them to display the lock panel.
279 if (mPUKEntryActivity != null) {
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700280 Log.i(LOG_TAG, "Dismiss puk entry activity");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700281 mPUKEntryActivity.finish();
282 mPUKEntryActivity = null;
283 }
284 if (mPUKEntryProgressDialog != null) {
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700285 Log.i(LOG_TAG, "Dismiss puk progress dialog");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700286 mPUKEntryProgressDialog.dismiss();
287 mPUKEntryProgressDialog = null;
288 }
Nazish Tabassum59126a92020-07-03 21:44:49 +0530289 Log.i(LOG_TAG, "Dismissing depersonal panel" + (bag.mIccStatus));
Nazish Tabassum160d2112019-12-23 17:31:33 +0530290 IccNetworkDepersonalizationPanel.dialogDismiss(bag.mPhoneId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700291 }
292 break;
293
294 case EVENT_UNSOL_CDMA_INFO_RECORD:
295 //TODO: handle message here;
296 break;
Brad Ebinger3fa43462016-04-12 16:06:48 -0700297 case EVENT_RESTART_SIP:
298 // This should only run if the Phone process crashed and was restarted. We do
299 // not want this running if the device is still in the FBE encrypted state.
Brad Ebinger28626122017-08-10 14:13:35 -0700300 // This is the same procedure that is triggered in the SipIncomingCallReceiver
Brad Ebinger3fa43462016-04-12 16:06:48 -0700301 // upon BOOT_COMPLETED.
Meng Wangc04cd2d2019-10-04 12:00:06 -0700302 UserManager userManager =
303 (UserManager) sMe.getSystemService(Context.USER_SERVICE);
Brad Ebinger3fa43462016-04-12 16:06:48 -0700304 if (userManager != null && userManager.isUserUnlocked()) {
305 SipUtil.startSipService();
306 }
307 break;
Jack Yu1a2fc352017-07-14 17:14:37 -0700308 case EVENT_DATA_ROAMING_SETTINGS_CHANGED:
309 case EVENT_MOBILE_DATA_SETTINGS_CHANGED:
310 updateDataRoamingStatus();
311 break;
Lei Liu863eac02020-06-24 20:12:55 +0800312 case EVENT_CARRIER_CONFIG_CHANGED:
313 int subId = (Integer) msg.obj;
314 // The voicemail number could be overridden by carrier config, so need to
315 // refresh the message waiting (voicemail) indicator.
316 refreshMwiIndicator(subId);
317 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700318 }
319 }
320 };
321
322 public PhoneGlobals(Context context) {
323 super(context);
324 sMe = this;
Jack Yu1a2fc352017-07-14 17:14:37 -0700325 mSettingsObserver = new SettingsObserver(context, mHandler);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700326 }
327
328 public void onCreate() {
329 if (VDBG) Log.v(LOG_TAG, "onCreate()...");
330
331 ContentResolver resolver = getContentResolver();
332
333 // Cache the "voice capable" flag.
334 // This flag currently comes from a resource (which is
335 // overrideable on a per-product basis):
Michele87262742020-01-16 12:23:53 -0800336 sVoiceCapable = ((TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE))
337 .isVoiceCapable();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700338 // ...but this might eventually become a PackageManager "system
339 // feature" instead, in which case we'd do something like:
340 // sVoiceCapable =
341 // getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY_VOICE_CALLS);
342
Stuart Scottdcf40a92014-12-09 10:45:01 -0800343 if (mCM == null) {
Nathan Haroldf9df6ea2019-03-08 11:54:22 -0800344 // Initialize AnomalyReporter early so that it can be used
345 AnomalyReporter.initialize(this);
Nathan Harold0db5c5e2019-01-22 20:18:56 -0800346
Youming Ye47a6adc2018-11-19 15:33:36 -0800347 // Inject telephony component factory if configured using other jars.
348 XmlResourceParser parser = getResources().getXml(R.xml.telephony_injection);
349 TelephonyComponentFactory.getInstance().injectTheComponentFactory(parser);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700350 // Initialize the telephony framework
351 PhoneFactory.makeDefaultPhones(this);
352
Brad Ebinger05f52c22019-12-05 13:03:21 -0800353 // Only bring up ImsResolver if the device supports having an IMS stack.
354 if (getPackageManager().hasSystemFeature(
355 PackageManager.FEATURE_TELEPHONY_IMS)) {
356 // Get the package name of the default IMS implementation.
357 String defaultImsMmtelPackage = getResources().getString(
358 R.string.config_ims_mmtel_package);
359 String defaultImsRcsPackage = getResources().getString(
360 R.string.config_ims_rcs_package);
361 mImsResolver = new ImsResolver(this, defaultImsMmtelPackage,
Brad Ebingere3ae65a2020-09-11 12:45:11 -0700362 defaultImsRcsPackage, PhoneFactory.getPhones().length,
363 new ImsFeatureBinderRepository());
Brad Ebinger05f52c22019-12-05 13:03:21 -0800364 mImsResolver.initialize();
Tyler Gunn92479152021-01-20 16:30:10 -0800365
366 // With the IMS phone created, load static config.xml values from the phone process
367 // so that it can be provided to the ImsPhoneCallTracker.
368 for (Phone p : PhoneFactory.getPhones()) {
369 Phone imsPhone = p.getImsPhone();
370 if (imsPhone != null && imsPhone instanceof ImsPhone) {
371 ImsPhone theImsPhone = (ImsPhone) imsPhone;
372 if (theImsPhone.getCallTracker() instanceof ImsPhoneCallTracker) {
373 ImsPhoneCallTracker ict = (ImsPhoneCallTracker)
374 theImsPhone.getCallTracker();
375
376 ImsPhoneCallTracker.Config config = new ImsPhoneCallTracker.Config();
377 config.isD2DCommunicationSupported = getResources().getBoolean(
378 R.bool.config_use_device_to_device_communication);
379 ict.setConfig(config);
380 }
381 }
382 }
Hui Wang068ab862020-10-31 05:12:53 +0000383 RcsProvisioningMonitor.make(this);
Brad Ebinger05f52c22019-12-05 13:03:21 -0800384 }
385
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700386 // Start TelephonyDebugService After the default phone is created.
387 Intent intent = new Intent(this, TelephonyDebugService.class);
388 startService(intent);
389
390 mCM = CallManager.getInstance();
Stuart Scottdcf40a92014-12-09 10:45:01 -0800391 for (Phone phone : PhoneFactory.getPhones()) {
392 mCM.registerPhone(phone);
Stuart Scottdcf40a92014-12-09 10:45:01 -0800393 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700394
395 // Create the NotificationMgr singleton, which is used to display
396 // status bar icons and control other status bar behavior.
397 notificationMgr = NotificationMgr.init(this);
398
Brad Ebinger3fa43462016-04-12 16:06:48 -0700399 // If PhoneGlobals has crashed and is being restarted, then restart.
400 mHandler.sendEmptyMessage(EVENT_RESTART_SIP);
401
Anthony Lee03ebdfc2015-07-27 08:12:02 -0700402 // Create an instance of CdmaPhoneCallState and initialize it to IDLE
403 cdmaPhoneCallState = new CdmaPhoneCallState();
404 cdmaPhoneCallState.CdmaPhoneCallStateInit();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700405
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700406 // before registering for phone state changes
407 mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
408 mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, LOG_TAG);
409 // lock used to keep the processor awake, when we don't care for the display.
410 mPartialWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK
411 | PowerManager.ON_AFTER_RELEASE, LOG_TAG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700412
413 mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
414
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700415 // Create the CallerInfoCache singleton, which remembers custom ring tone and
416 // send-to-voicemail settings.
417 //
418 // The asynchronous caching will start just after this call.
419 callerInfoCache = CallerInfoCache.init(this);
420
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -0800421 phoneMgr = PhoneInterfaceManager.init(this);
Santos Cordon406c0342013-08-28 00:07:47 -0700422
James.cf Linaf3183c2019-10-24 00:59:00 +0800423 imsRcsController = ImsRcsController.init(this);
424
James.cf Linc9f35a42020-01-15 02:35:22 +0800425 if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY_IMS)) {
Brad Ebingera68a4972020-01-30 17:31:23 -0800426 mTelephonyRcsService = new TelephonyRcsService(this,
427 PhoneFactory.getPhones().length);
428 mTelephonyRcsService.initialize();
James.cf Linc9f35a42020-01-15 02:35:22 +0800429 imsRcsController.setRcsService(mTelephonyRcsService);
430 }
431
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800432 configLoader = CarrierConfigLoader.init(this);
433
Jack Yu1a2fc352017-07-14 17:14:37 -0700434 // Create the CallNotifier singleton, which handles
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700435 // asynchronous events from the telephony layer (like
436 // launching the incoming-call UI when an incoming call comes
437 // in.)
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800438 notifier = CallNotifier.init(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700439
Stuart Scottdcf40a92014-12-09 10:45:01 -0800440 PhoneUtils.registerIccStatus(mHandler, EVENT_SIM_NETWORK_LOCKED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700441
442 // register for MMI/USSD
443 mCM.registerForMmiComplete(mHandler, MMI_COMPLETE, null);
444
Qiong Liucc3fe812019-09-23 15:25:44 +0800445 // Initialize cell status using current airplane mode.
446 handleAirplaneModeChange(this, Settings.Global.getInt(getContentResolver(),
447 Settings.Global.AIRPLANE_MODE_ON, AIRPLANE_OFF));
448
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700449 // Register for misc other intent broadcasts.
450 IntentFilter intentFilter =
451 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700452 intentFilter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
453 intentFilter.addAction(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);
454 intentFilter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
455 intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
Jack Yu1a2fc352017-07-14 17:14:37 -0700456 intentFilter.addAction(TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED);
457 intentFilter.addAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700458 registerReceiver(mReceiver, intentFilter);
459
Brad Ebinger533c6c12017-08-02 11:38:48 -0700460 IntentFilter sipIntentFilter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
Brad Ebinger533c6c12017-08-02 11:38:48 -0700461 sipIntentFilter.addAction(SipManager.ACTION_SIP_SERVICE_UP);
462 sipIntentFilter.addAction(SipManager.ACTION_SIP_CALL_OPTION_CHANGED);
Grace Jiab95592a2020-01-15 14:12:24 -0800463 sipIntentFilter.addAction(SipManager.ACTION_SIP_REMOVE_PROFILE);
Brad Ebinger28626122017-08-10 14:13:35 -0700464 registerReceiver(mSipReceiver, sipIntentFilter);
Brad Ebinger533c6c12017-08-02 11:38:48 -0700465
Ta-wei Yenf0a71bc2017-05-17 12:28:28 -0700466 mCarrierVvmPackageInstalledReceiver.register(this);
467
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700468 //set the default values for the preferences in the phone.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700469 PreferenceManager.setDefaultValues(this, R.xml.call_feature_setting, false);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700470 }
471
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700472 // XXX pre-load the SimProvider so that it's ready
473 resolver.getType(Uri.parse("content://icc/adn"));
474
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700475 // TODO: Register for Cdma Information Records
476 // phone.registerCdmaInformationRecord(mHandler, EVENT_UNSOL_CDMA_INFO_RECORD, null);
477
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700478 // Read HAC settings and configure audio hardware
479 if (getResources().getBoolean(R.bool.hac_enabled)) {
Stuart Scottdcf40a92014-12-09 10:45:01 -0800480 int hac = android.provider.Settings.System.getInt(
481 getContentResolver(),
482 android.provider.Settings.System.HEARING_AID,
483 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700484 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Jordan Liuf71aeef2019-08-21 11:46:09 -0700485 audioManager.setParameters(
486 SettingsConstants.HAC_KEY + "=" + (hac == SettingsConstants.HAC_ENABLED
487 ? SettingsConstants.HAC_VAL_ON : SettingsConstants.HAC_VAL_OFF));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700488 }
Santos Cordonff506f52013-11-21 19:13:19 -0800489 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700490
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700491 /**
492 * Returns the singleton instance of the PhoneApp.
493 */
Sailesh Nepal1eaf22b2014-02-22 17:00:49 -0800494 public static PhoneGlobals getInstance() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700495 if (sMe == null) {
496 throw new IllegalStateException("No PhoneGlobals here!");
497 }
498 return sMe;
499 }
500
501 /**
Stuart Scottdcf40a92014-12-09 10:45:01 -0800502 * Returns the default phone.
503 *
Andrew Lee385019f2014-11-24 14:19:50 -0800504 * WARNING: This method should be used carefully, now that there may be multiple phones.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700505 */
Andrew Lee83383e42014-10-31 12:42:28 -0700506 public static Phone getPhone() {
Stuart Scottdcf40a92014-12-09 10:45:01 -0800507 return PhoneFactory.getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700508 }
509
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800510 public static Phone getPhone(int subId) {
511 return PhoneFactory.getPhone(SubscriptionManager.getPhoneId(subId));
Andrew Lee385019f2014-11-24 14:19:50 -0800512 }
513
Brad Ebinger05f52c22019-12-05 13:03:21 -0800514 public ImsResolver getImsResolver() {
515 return mImsResolver;
516 }
517
Santos Cordonde10b752013-09-19 04:11:33 -0700518 /* package */ CallManager getCallManager() {
519 return mCM;
520 }
521
Chris Manton4e9fa912015-06-19 11:26:57 -0700522 public PersistableBundle getCarrierConfig() {
Shishir Agrawald3480e02016-01-25 13:05:49 -0800523 return getCarrierConfigForSubId(SubscriptionManager.getDefaultSubscriptionId());
Jonathan Basseri89b0ab42015-05-01 10:52:40 -0700524 }
525
Chris Manton4e9fa912015-06-19 11:26:57 -0700526 public PersistableBundle getCarrierConfigForSubId(int subId) {
Philip P. Moltmann0079aae2020-03-05 16:24:02 -0800527 return configLoader.getConfigForSubIdWithFeature(subId, getOpPackageName(),
528 getAttributionTag());
Junda Liu605148f2015-04-28 15:23:40 -0700529 }
530
Jack Yu1a2fc352017-07-14 17:14:37 -0700531 private void registerSettingsObserver() {
532 mSettingsObserver.unobserve();
533 String dataRoamingSetting = Settings.Global.DATA_ROAMING;
534 String mobileDataSetting = Settings.Global.MOBILE_DATA;
535 if (TelephonyManager.getDefault().getSimCount() > 1) {
536 int subId = mDefaultDataSubId;
537 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
538 dataRoamingSetting += subId;
539 mobileDataSetting += subId;
540 }
541 }
542
543 // Listen for user data roaming setting changed event
544 mSettingsObserver.observe(Settings.Global.getUriFor(dataRoamingSetting),
545 EVENT_DATA_ROAMING_SETTINGS_CHANGED);
546
547 // Listen for mobile data setting changed event
548 mSettingsObserver.observe(Settings.Global.getUriFor(mobileDataSetting),
549 EVENT_MOBILE_DATA_SETTINGS_CHANGED);
550 }
551
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700552 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700553 * Sets the activity responsible for un-PUK-blocking the device
554 * so that we may close it when we receive a positive result.
555 * mPUKEntryActivity is also used to indicate to the device that
556 * we are trying to un-PUK-lock the phone. In other words, iff
557 * it is NOT null, then we are trying to unlock and waiting for
558 * the SIM to move to READY state.
559 *
560 * @param activity is the activity to close when PUK has
561 * finished unlocking. Can be set to null to indicate the unlock
562 * or SIM READYing process is over.
563 */
564 void setPukEntryActivity(Activity activity) {
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700565 Log.i(LOG_TAG, "setPukEntryActivity - set to " + (activity == null ? "null" : "activity"));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700566 mPUKEntryActivity = activity;
567 }
568
569 Activity getPUKEntryActivity() {
570 return mPUKEntryActivity;
571 }
572
573 /**
574 * Sets the dialog responsible for notifying the user of un-PUK-
575 * blocking - SIM READYing progress, so that we may dismiss it
576 * when we receive a positive result.
577 *
578 * @param dialog indicates the progress dialog informing the user
579 * of the state of the device. Dismissed upon completion of
580 * READYing process
581 */
582 void setPukEntryProgressDialog(ProgressDialog dialog) {
Tyler Gunn3fc09c02020-05-08 16:35:55 -0700583 Log.i(LOG_TAG, "setPukEntryProgressDialog - set to "
584 + (dialog == null ? "null" : "activity"));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700585 mPUKEntryProgressDialog = dialog;
586 }
587
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700588 KeyguardManager getKeyguardManager() {
589 return mKeyguardManager;
590 }
591
592 private void onMMIComplete(AsyncResult r) {
593 if (VDBG) Log.d(LOG_TAG, "onMMIComplete()...");
594 MmiCode mmiCode = (MmiCode) r.result;
Stuart Scottdcf40a92014-12-09 10:45:01 -0800595 PhoneUtils.displayMMIComplete(mmiCode.getPhone(), getInstance(), mmiCode, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700596 }
597
fionaxu80126972017-02-01 17:01:26 -0800598 private void initForNewRadioTechnology() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700599 if (DBG) Log.d(LOG_TAG, "initForNewRadioTechnology...");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700600 notifier.updateCallNotifierRegistrationsAfterRadioTechnologyChange();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700601 }
602
Chris Mantona09f3632016-02-09 14:48:42 -0800603 private void handleAirplaneModeChange(Context context, int newMode) {
604 int cellState = Settings.Global.getInt(context.getContentResolver(),
605 Settings.Global.CELL_ON, PhoneConstants.CELL_ON_FLAG);
606 boolean isAirplaneNewlyOn = (newMode == 1);
607 switch (cellState) {
608 case PhoneConstants.CELL_OFF_FLAG:
609 // Airplane mode does not affect the cell radio if user
610 // has turned it off.
611 break;
612 case PhoneConstants.CELL_ON_FLAG:
613 maybeTurnCellOff(context, isAirplaneNewlyOn);
614 break;
615 case PhoneConstants.CELL_OFF_DUE_TO_AIRPLANE_MODE_FLAG:
616 maybeTurnCellOn(context, isAirplaneNewlyOn);
617 break;
618 }
Hall Liu7f56e1a2019-07-10 14:41:14 -0700619 for (Phone phone : PhoneFactory.getPhones()) {
620 phone.getServiceStateTracker().onAirplaneModeChanged(isAirplaneNewlyOn);
621 }
Chris Mantona09f3632016-02-09 14:48:42 -0800622 }
623
624 /*
625 * Returns true if the radio must be turned off when entering airplane mode.
626 */
627 private boolean isCellOffInAirplaneMode(Context context) {
628 String airplaneModeRadios = Settings.Global.getString(context.getContentResolver(),
629 Settings.Global.AIRPLANE_MODE_RADIOS);
630 return airplaneModeRadios == null
631 || airplaneModeRadios.contains(Settings.Global.RADIO_CELL);
632 }
633
634 private void setRadioPowerOff(Context context) {
635 Log.i(LOG_TAG, "Turning radio off - airplane");
636 Settings.Global.putInt(context.getContentResolver(), Settings.Global.CELL_ON,
Eric Erfanian945409f2018-02-21 19:26:15 +0000637 PhoneConstants.CELL_OFF_DUE_TO_AIRPLANE_MODE_FLAG);
Meng Wang61307622019-10-23 14:08:43 -0700638 TelephonyProperties.airplane_mode_on(true); // true means int value 1
Chris Mantona09f3632016-02-09 14:48:42 -0800639 Settings.Global.putInt(getContentResolver(), Settings.Global.ENABLE_CELLULAR_ON_BOOT, 0);
640 PhoneUtils.setRadioPower(false);
641 }
642
643 private void setRadioPowerOn(Context context) {
644 Log.i(LOG_TAG, "Turning radio on - airplane");
645 Settings.Global.putInt(context.getContentResolver(), Settings.Global.CELL_ON,
646 PhoneConstants.CELL_ON_FLAG);
647 Settings.Global.putInt(getContentResolver(), Settings.Global.ENABLE_CELLULAR_ON_BOOT,
648 1);
Meng Wang61307622019-10-23 14:08:43 -0700649 TelephonyProperties.airplane_mode_on(false); // false means int value 0
Chris Mantona09f3632016-02-09 14:48:42 -0800650 PhoneUtils.setRadioPower(true);
651 }
652
653 private void maybeTurnCellOff(Context context, boolean isAirplaneNewlyOn) {
654 if (isAirplaneNewlyOn) {
Santos Cordone18902f2016-03-22 17:16:04 -0700655 // If we are trying to turn off the radio, make sure there are no active
656 // emergency calls. If there are, switch airplane mode back to off.
Tyler Gunn900d8fd2018-09-21 09:22:12 -0700657 TelecomManager tm = (TelecomManager) context.getSystemService(TELECOM_SERVICE);
658
659 if (tm != null && tm.isInEmergencyCall()) {
Santos Cordone18902f2016-03-22 17:16:04 -0700660 // Switch airplane mode back to off.
Jordan Liu0e819b12019-08-28 15:33:05 -0700661 ConnectivityManager cm =
662 (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
663 cm.setAirplaneMode(false);
Santos Cordone18902f2016-03-22 17:16:04 -0700664 Toast.makeText(this, R.string.radio_off_during_emergency_call, Toast.LENGTH_LONG)
665 .show();
666 Log.i(LOG_TAG, "Ignoring airplane mode: emergency call. Turning airplane off");
Chris Mantona09f3632016-02-09 14:48:42 -0800667 } else if (isCellOffInAirplaneMode(context)) {
668 setRadioPowerOff(context);
Santos Cordone18902f2016-03-22 17:16:04 -0700669 } else {
Chris Mantona09f3632016-02-09 14:48:42 -0800670 Log.i(LOG_TAG, "Ignoring airplane mode: settings prevent cell radio power off");
Santos Cordone18902f2016-03-22 17:16:04 -0700671 }
Chris Mantona09f3632016-02-09 14:48:42 -0800672 }
673 }
674
675 private void maybeTurnCellOn(Context context, boolean isAirplaneNewlyOn) {
676 if (!isAirplaneNewlyOn) {
677 setRadioPowerOn(context);
Santos Cordone18902f2016-03-22 17:16:04 -0700678 }
679 }
680
Santos Cordon593ab382013-08-06 21:58:23 -0700681 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700682 * Receiver for misc intent broadcasts the Phone app cares about.
683 */
684 private class PhoneAppBroadcastReceiver extends BroadcastReceiver {
685 @Override
686 public void onReceive(Context context, Intent intent) {
687 String action = intent.getAction();
688 if (action.equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) {
Santos Cordone18902f2016-03-22 17:16:04 -0700689 int airplaneMode = Settings.Global.getInt(getContentResolver(),
690 Settings.Global.AIRPLANE_MODE_ON, AIRPLANE_OFF);
691 // Treat any non-OFF values as ON.
692 if (airplaneMode != AIRPLANE_OFF) {
693 airplaneMode = AIRPLANE_ON;
694 }
Chris Mantona09f3632016-02-09 14:48:42 -0800695 handleAirplaneModeChange(context, airplaneMode);
Amit Mahajanf5d92c82018-11-02 17:44:40 -0700696 } else if (action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) {
697 // re-register as it may be a new IccCard
698 int phoneId = intent.getIntExtra(PhoneConstants.PHONE_KEY,
699 SubscriptionManager.INVALID_PHONE_INDEX);
700 if (SubscriptionManager.isValidPhoneId(phoneId)) {
701 PhoneUtils.unregisterIccStatus(mHandler, phoneId);
702 PhoneUtils.registerIccStatus(mHandler, EVENT_SIM_NETWORK_LOCKED, phoneId);
703 }
Nazish Tabassum160d2112019-12-23 17:31:33 +0530704 String iccStatus = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);
705 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SIM_STATE_CHANGED,
706 new EventSimStateChangedBag(phoneId, iccStatus)));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700707 } else if (action.equals(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED)) {
708 String newPhone = intent.getStringExtra(PhoneConstants.PHONE_NAME_KEY);
fionaxu80126972017-02-01 17:01:26 -0800709 Log.d(LOG_TAG, "Radio technology switched. Now " + newPhone + " is active.");
710 initForNewRadioTechnology();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700711 } else if (action.equals(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED)) {
712 handleServiceStateChanged(intent);
713 } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) {
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530714 int phoneId = intent.getIntExtra(PhoneConstants.PHONE_KEY, 0);
Srikanth Chintala079912a2016-03-03 16:35:01 +0530715 phoneInEcm = PhoneFactory.getPhone(phoneId);
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530716 Log.d(LOG_TAG, "Emergency Callback Mode. phoneId:" + phoneId);
717 if (phoneInEcm != null) {
718 if (TelephonyCapabilities.supportsEcm(phoneInEcm)) {
719 Log.d(LOG_TAG, "Emergency Callback Mode arrived in PhoneApp.");
720 // Start Emergency Callback Mode service
Daniel Bright30efde42020-01-09 22:11:15 -0800721 if (intent.getBooleanExtra(
722 TelephonyManager.EXTRA_PHONE_IN_ECM_STATE, false)) {
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530723 context.startService(new Intent(context,
724 EmergencyCallbackModeService.class));
725 } else {
726 phoneInEcm = null;
727 }
728 } else {
729 // It doesn't make sense to get ACTION_EMERGENCY_CALLBACK_MODE_CHANGED
730 // on a device that doesn't support ECM in the first place.
731 Log.e(LOG_TAG, "Got ACTION_EMERGENCY_CALLBACK_MODE_CHANGED, but "
732 + "ECM isn't supported for phone: " + phoneInEcm.getPhoneName());
733 phoneInEcm = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700734 }
735 } else {
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530736 Log.w(LOG_TAG, "phoneInEcm is null.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700737 }
Jack Yu1a2fc352017-07-14 17:14:37 -0700738 } else if (action.equals(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED)) {
739 // Roaming status could be overridden by carrier config, so we need to update it.
740 if (VDBG) Log.v(LOG_TAG, "carrier config changed.");
741 updateDataRoamingStatus();
chen xubaf9fe52019-07-02 17:28:24 -0700742 updateLimitedSimFunctionForDualSim();
Lei Liu863eac02020-06-24 20:12:55 +0800743 int subId = intent.getIntExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX,
744 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
745 if (SubscriptionManager.isValidSubscriptionId(subId)) {
746 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CARRIER_CONFIG_CHANGED,
747 new Integer(subId)));
748 }
Jack Yu1a2fc352017-07-14 17:14:37 -0700749 } else if (action.equals(TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED)) {
750 // We also need to pay attention when default data subscription changes.
751 if (VDBG) Log.v(LOG_TAG, "default data sub changed.");
752 mDefaultDataSubId = SubscriptionManager.getDefaultDataSubscriptionId();
753 registerSettingsObserver();
754 Phone phone = getPhone(mDefaultDataSubId);
755 if (phone != null) {
756 updateDataRoamingStatus();
757 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700758 }
759 }
760 }
761
Brad Ebinger28626122017-08-10 14:13:35 -0700762 private class SipReceiver extends BroadcastReceiver {
763
764 @Override
765 public void onReceive(Context context, Intent intent) {
766 String action = intent.getAction();
767
768 SipAccountRegistry sipAccountRegistry = SipAccountRegistry.getInstance();
769 if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
770 SipUtil.startSipService();
771 } else if (action.equals(SipManager.ACTION_SIP_SERVICE_UP)
772 || action.equals(SipManager.ACTION_SIP_CALL_OPTION_CHANGED)) {
773 sipAccountRegistry.setup(context);
Grace Jiab95592a2020-01-15 14:12:24 -0800774 } else if (action.equals(SipManager.ACTION_SIP_REMOVE_PROFILE)) {
Brad Ebinger28626122017-08-10 14:13:35 -0700775 if (DBG) {
776 Log.d(LOG_TAG, "SIP_REMOVE_PHONE "
777 + intent.getStringExtra(SipManager.EXTRA_LOCAL_URI));
778 }
779 sipAccountRegistry.removeSipProfile(intent.getStringExtra(
780 SipManager.EXTRA_LOCAL_URI));
781 } else {
782 if (DBG) Log.d(LOG_TAG, "onReceive, action not processed: " + action);
783 }
784 }
785 }
786
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700787 private void handleServiceStateChanged(Intent intent) {
788 /**
789 * This used to handle updating EriTextWidgetProvider this routine
790 * and and listening for ACTION_SERVICE_STATE_CHANGED intents could
791 * be removed. But leaving just in case it might be needed in the near
792 * future.
793 */
794
Jack Yu1a2fc352017-07-14 17:14:37 -0700795 if (VDBG) Log.v(LOG_TAG, "handleServiceStateChanged");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700796 // If service just returned, start sending out the queued messages
Santos Cordonc593d002015-06-03 15:41:15 -0700797 Bundle extras = intent.getExtras();
798 if (extras != null) {
799 ServiceState ss = ServiceState.newFromBundle(extras);
800 if (ss != null) {
801 int state = ss.getState();
Jayachandran C2ef9a482017-05-12 22:07:47 -0700802 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
803 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
804 notificationMgr.updateNetworkSelection(state, subId);
Jack Yu1a2fc352017-07-14 17:14:37 -0700805
806 if (VDBG) {
807 Log.v(LOG_TAG, "subId=" + subId + ",mDefaultDataSubId="
808 + mDefaultDataSubId + ",ss roaming=" + ss.getDataRoaming());
809 }
810 if (subId == mDefaultDataSubId) {
811 updateDataRoamingStatus();
812 }
Santos Cordonc593d002015-06-03 15:41:15 -0700813 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700814 }
815 }
816
Jack Yu1a2fc352017-07-14 17:14:37 -0700817 /**
Jordan Liuff2ccd72019-07-23 15:54:41 -0700818 * @return whether or not we should show a notification when connecting to data roaming if the
819 * user has data roaming enabled
820 */
821 private boolean shouldShowDataConnectedRoaming(int subId) {
822 PersistableBundle config = getCarrierConfigForSubId(subId);
823 return config.getBoolean(CarrierConfigManager
824 .KEY_SHOW_DATA_CONNECTED_ROAMING_NOTIFICATION_BOOL);
825 }
826
827 /**
Jack Yu1a2fc352017-07-14 17:14:37 -0700828 * When roaming, if mobile data cannot be established due to data roaming not enabled, we need
829 * to notify the user so they can enable it through settings. Vise versa if the condition
830 * changes, we need to dismiss the notification.
831 */
832 private void updateDataRoamingStatus() {
833 if (VDBG) Log.v(LOG_TAG, "updateDataRoamingStatus");
834 Phone phone = getPhone(mDefaultDataSubId);
835 if (phone == null) {
836 Log.w(LOG_TAG, "Can't get phone with sub id = " + mDefaultDataSubId);
837 return;
838 }
839
840 DataConnectionReasons reasons = new DataConnectionReasons();
Jack Yu311536f2018-11-26 11:20:48 -0800841 boolean dataAllowed = phone.isDataAllowed(ApnSetting.TYPE_DEFAULT, reasons);
Jack Yu1a2fc352017-07-14 17:14:37 -0700842 mDataRoamingNotifLog.log("dataAllowed=" + dataAllowed + ", reasons=" + reasons);
843 if (VDBG) Log.v(LOG_TAG, "dataAllowed=" + dataAllowed + ", reasons=" + reasons);
Jordan Liuff2ccd72019-07-23 15:54:41 -0700844 if (!dataAllowed && reasons.containsOnly(DataDisallowedReasonType.ROAMING_DISABLED)) {
845 // No need to show it again if we never cancelled it explicitly.
846 if (mPrevRoamingNotification == ROAMING_NOTIFICATION_DISCONNECTED) return;
Jack Yu1a2fc352017-07-14 17:14:37 -0700847 // If the only reason of no data is data roaming disabled, then we notify the user
848 // so the user can turn on data roaming.
Jordan Liuff2ccd72019-07-23 15:54:41 -0700849 mPrevRoamingNotification = ROAMING_NOTIFICATION_DISCONNECTED;
Jack Yu1a2fc352017-07-14 17:14:37 -0700850 Log.d(LOG_TAG, "Show roaming disconnected notification");
Jordan Liuff2ccd72019-07-23 15:54:41 -0700851 mDataRoamingNotifLog.log("Show roaming off.");
Pengquan Meng8783d932017-10-16 14:57:40 -0700852 Message msg = mHandler.obtainMessage(EVENT_DATA_ROAMING_DISCONNECTED);
853 msg.arg1 = mDefaultDataSubId;
854 msg.sendToTarget();
Jordan Liuff2ccd72019-07-23 15:54:41 -0700855 } else if (dataAllowed && dataIsNowRoaming(mDefaultDataSubId)
856 && shouldShowDataConnectedRoaming(mDefaultDataSubId)) {
857 // No need to show it again if we never cancelled it explicitly, or carrier config
858 // indicates this is not needed.
859 if (mPrevRoamingNotification == ROAMING_NOTIFICATION_CONNECTED) return;
860 mPrevRoamingNotification = ROAMING_NOTIFICATION_CONNECTED;
861 Log.d(LOG_TAG, "Show roaming connected notification");
862 mDataRoamingNotifLog.log("Show roaming on.");
863 Message msg = mHandler.obtainMessage(EVENT_DATA_ROAMING_CONNECTED);
864 msg.arg1 = mDefaultDataSubId;
865 msg.sendToTarget();
866 } else if (mPrevRoamingNotification != ROAMING_NOTIFICATION_NO_NOTIFICATION) {
867 // Otherwise we either 1) we are not roaming or 2) roaming is off but ROAMING_DISABLED
868 // is not the only data disable reason. In this case we dismiss the notification we
869 // showed earlier.
870 mPrevRoamingNotification = ROAMING_NOTIFICATION_NO_NOTIFICATION;
871 Log.d(LOG_TAG, "Dismiss roaming notification");
Jack Yu1a2fc352017-07-14 17:14:37 -0700872 mDataRoamingNotifLog.log("Hide. data allowed=" + dataAllowed + ", reasons=" + reasons);
873 mHandler.sendEmptyMessage(EVENT_DATA_ROAMING_OK);
874 }
875 }
876
Jordan Liuff2ccd72019-07-23 15:54:41 -0700877 /**
878 *
879 * @param subId to check roaming on
880 * @return whether we have transitioned to dataRoaming
881 */
882 private boolean dataIsNowRoaming(int subId) {
883 return getPhone(subId).getServiceState().getDataRoaming();
884 }
885
chen xubaf9fe52019-07-02 17:28:24 -0700886 private void updateLimitedSimFunctionForDualSim() {
887 if (DBG) Log.d(LOG_TAG, "updateLimitedSimFunctionForDualSim");
888 // check conditions to display limited SIM function notification under dual SIM
889 SubscriptionManager subMgr = (SubscriptionManager) getSystemService(
890 Context.TELEPHONY_SUBSCRIPTION_SERVICE);
891 List<SubscriptionInfo> subList = subMgr.getActiveSubscriptionInfoList(false);
892 if (subList != null && subList.size() > 1) {
893 CarrierConfigManager configMgr = (CarrierConfigManager)
894 getSystemService(Context.CARRIER_CONFIG_SERVICE);
895 for (SubscriptionInfo info : subList) {
896 PersistableBundle b = configMgr.getConfigForSubId(info.getSubscriptionId());
897 if (b != null) {
898 if (b.getBoolean(CarrierConfigManager
899 .KEY_LIMITED_SIM_FUNCTION_NOTIFICATION_FOR_DSDS_BOOL)) {
900 notificationMgr.showLimitedSimFunctionWarningNotification(
901 info.getSubscriptionId(),
902 info.getDisplayName().toString());
903 } else {
904 notificationMgr.dismissLimitedSimFunctionWarningNotification(
905 info.getSubscriptionId());
906 }
907 }
908 }
909 } else {
910 // cancel notifications for all subs
911 notificationMgr.dismissLimitedSimFunctionWarningNotification(
912 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
913 }
914 notificationMgr.dismissLimitedSimFunctionWarningNotificationForInactiveSubs();
915
916 }
917
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530918 public Phone getPhoneInEcm() {
919 return phoneInEcm;
920 }
921
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700922 /**
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800923 * Triggers a refresh of the message waiting (voicemail) indicator.
924 *
925 * @param subId the subscription id we should refresh the notification for.
926 */
927 public void refreshMwiIndicator(int subId) {
928 notificationMgr.refreshMwi(subId);
929 }
930
931 /**
Pengquan Mengdd9ac822018-09-20 15:25:35 -0700932 * Called when the network selection on the subscription {@code subId} is changed by the user.
933 *
934 * @param subId the subscription id.
935 */
936 public void onNetworkSelectionChanged(int subId) {
937 Phone phone = getPhone(subId);
938 if (phone != null) {
939 notificationMgr.updateNetworkSelection(phone.getServiceState().getState(), subId);
940 } else {
941 Log.w(LOG_TAG, "onNetworkSelectionChanged on null phone, subId: " + subId);
942 }
943 }
944
945 /**
Jack Yu1a2fc352017-07-14 17:14:37 -0700946 * Dump the state of the object, add calls to other objects as desired.
947 *
948 * @param fd File descriptor
949 * @param printWriter Print writer
950 * @param args Arguments
951 */
952 public void dump(FileDescriptor fd, PrintWriter printWriter, String[] args) {
953 IndentingPrintWriter pw = new IndentingPrintWriter(printWriter, " ");
954 pw.println("------- PhoneGlobals -------");
955 pw.increaseIndent();
Jordan Liuff2ccd72019-07-23 15:54:41 -0700956 pw.println("mPrevRoamingNotification=" + mPrevRoamingNotification);
Jack Yu1a2fc352017-07-14 17:14:37 -0700957 pw.println("mDefaultDataSubId=" + mDefaultDataSubId);
958 pw.println("mDataRoamingNotifLog:");
Keun young Parkdebb8532019-05-30 16:35:27 -0700959 pw.println("isSmsCapable=" + TelephonyManager.from(this).isSmsCapable());
Jack Yu1a2fc352017-07-14 17:14:37 -0700960 pw.increaseIndent();
961 mDataRoamingNotifLog.dump(fd, pw, args);
962 pw.decreaseIndent();
Brad Ebinger05f52c22019-12-05 13:03:21 -0800963 pw.println("ImsResolver:");
964 pw.increaseIndent();
965 try {
966 if (mImsResolver != null) mImsResolver.dump(fd, pw, args);
967 } catch (Exception e) {
968 e.printStackTrace();
969 }
970 pw.decreaseIndent();
Brad Ebingera68a4972020-01-30 17:31:23 -0800971 pw.println("RcsService:");
972 try {
973 if (mTelephonyRcsService != null) mTelephonyRcsService.dump(fd, pw, args);
974 } catch (Exception e) {
975 e.printStackTrace();
976 }
Jack Yu1a2fc352017-07-14 17:14:37 -0700977 pw.decreaseIndent();
978 pw.println("------- End PhoneGlobals -------");
979 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700980}