blob: 1d61c8e8f74d6b21b6a40724009f12cbad695978 [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
19import android.app.Activity;
20import android.app.KeyguardManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070021import android.app.ProgressDialog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.content.BroadcastReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import android.content.ContentResolver;
24import android.content.Context;
25import android.content.ContextWrapper;
26import android.content.Intent;
27import android.content.IntentFilter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070028import android.media.AudioManager;
Santos Cordone18902f2016-03-22 17:16:04 -070029import android.net.ConnectivityManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070030import android.net.Uri;
31import android.os.AsyncResult;
Junda Liu605148f2015-04-28 15:23:40 -070032import android.os.Bundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070033import android.os.Handler;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070034import android.os.Message;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070035import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070036import android.os.PowerManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070037import android.os.SystemClock;
38import android.os.SystemProperties;
39import android.os.UpdateLock;
Brad Ebinger3fa43462016-04-12 16:06:48 -070040import android.os.UserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.preference.PreferenceManager;
Sanket Padawe4c699232016-02-09 11:07:22 -080042import android.provider.Settings;
Junda Liu12f7d802015-05-01 12:06:44 -070043import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.telephony.ServiceState;
Andrew Lee385019f2014-11-24 14:19:50 -080045import android.telephony.SubscriptionManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070046import android.util.Log;
Santos Cordone18902f2016-03-22 17:16:04 -070047import android.widget.Toast;
Ta-wei Yenb29425b2016-09-21 17:28:14 -070048
Santos Cordon7d4ddf62013-07-10 11:58:08 -070049import com.android.internal.telephony.Call;
50import com.android.internal.telephony.CallManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import com.android.internal.telephony.IccCardConstants;
52import com.android.internal.telephony.MmiCode;
53import com.android.internal.telephony.Phone;
54import com.android.internal.telephony.PhoneConstants;
55import com.android.internal.telephony.PhoneFactory;
56import com.android.internal.telephony.TelephonyCapabilities;
57import com.android.internal.telephony.TelephonyIntents;
Santos Cordon352ff652014-05-30 01:41:45 -070058import com.android.phone.common.CallLogAsync;
Andrew Leefb7f92e2015-02-26 16:23:32 -080059import com.android.phone.settings.SettingsConstants;
Ta-wei Yenf0a71bc2017-05-17 12:28:28 -070060import com.android.phone.vvm.CarrierVvmPackageInstalledReceiver;
Brad Ebinger3fa43462016-04-12 16:06:48 -070061import com.android.services.telephony.sip.SipUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070062
63/**
64 * Global state for the telephony subsystem when running in the primary
65 * phone process.
66 */
Sailesh Nepalbf900542014-07-15 16:18:32 -070067public class PhoneGlobals extends ContextWrapper {
Andrew Lee83383e42014-10-31 12:42:28 -070068 public static final String LOG_TAG = "PhoneApp";
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069
70 /**
71 * Phone app-wide debug level:
72 * 0 - no debug logging
73 * 1 - normal debug logging if ro.debuggable is set (which is true in
74 * "eng" and "userdebug" builds but not "user" builds)
75 * 2 - ultra-verbose debug logging
76 *
77 * Most individual classes in the phone app have a local DBG constant,
78 * typically set to
79 * (PhoneApp.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1)
80 * or else
81 * (PhoneApp.DBG_LEVEL >= 2)
82 * depending on the desired verbosity.
83 *
84 * ***** DO NOT SUBMIT WITH DBG_LEVEL > 0 *************
85 */
Andrew Lee88b51e22014-10-29 15:48:51 -070086 public static final int DBG_LEVEL = 0;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070087
88 private static final boolean DBG =
89 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
90 private static final boolean VDBG = (PhoneGlobals.DBG_LEVEL >= 2);
91
92 // Message codes; see mHandler below.
93 private static final int EVENT_SIM_NETWORK_LOCKED = 3;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070094 private static final int EVENT_SIM_STATE_CHANGED = 8;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070095 private static final int EVENT_DATA_ROAMING_DISCONNECTED = 10;
96 private static final int EVENT_DATA_ROAMING_OK = 11;
97 private static final int EVENT_UNSOL_CDMA_INFO_RECORD = 12;
Brad Ebinger3fa43462016-04-12 16:06:48 -070098 private static final int EVENT_RESTART_SIP = 13;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070099
100 // The MMI codes are also used by the InCallScreen.
101 public static final int MMI_INITIATE = 51;
102 public static final int MMI_COMPLETE = 52;
103 public static final int MMI_CANCEL = 53;
104 // Don't use message codes larger than 99 here; those are reserved for
105 // the individual Activities of the Phone UI.
106
Santos Cordone18902f2016-03-22 17:16:04 -0700107 public static final int AIRPLANE_ON = 1;
108 public static final int AIRPLANE_OFF = 0;
109
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700110 /**
111 * Allowable values for the wake lock code.
112 * SLEEP means the device can be put to sleep.
113 * PARTIAL means wake the processor, but we display can be kept off.
114 * FULL means wake both the processor and the display.
115 */
116 public enum WakeState {
117 SLEEP,
118 PARTIAL,
119 FULL
120 }
121
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700122 private static PhoneGlobals sMe;
123
124 // A few important fields we expose to the rest of the package
125 // directly (rather than thru set/get methods) for efficiency.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700126 CallController callController;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700127 CallManager mCM;
Santos Cordon63a84242013-07-23 13:32:52 -0700128 CallNotifier notifier;
129 CallerInfoCache callerInfoCache;
Santos Cordon63a84242013-07-23 13:32:52 -0700130 NotificationMgr notificationMgr;
Andrew Lee9431b832015-03-09 18:46:45 -0700131 public PhoneInterfaceManager phoneMgr;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800132 CarrierConfigLoader configLoader;
Santos Cordon63a84242013-07-23 13:32:52 -0700133
Santos Cordon69a69192013-08-22 14:25:42 -0700134 private CallGatewayManager callGatewayManager;
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530135 private Phone phoneInEcm;
Santos Cordon63a84242013-07-23 13:32:52 -0700136
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700137 static boolean sVoiceCapable = true;
138
Santos Cordonc593d002015-06-03 15:41:15 -0700139 // TODO: Remove, no longer used.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700140 CdmaPhoneCallState cdmaPhoneCallState;
141
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700142 // The currently-active PUK entry activity and progress dialog.
143 // Normally, these are the Emergency Dialer and the subsequent
144 // progress dialog. null if there is are no such objects in
145 // the foreground.
146 private Activity mPUKEntryActivity;
147 private ProgressDialog mPUKEntryProgressDialog;
148
Jack Yu953f9352017-05-18 14:41:06 -0700149 private boolean mDataDisconnectedDueToRoaming = false;
150
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700151 private WakeState mWakeState = WakeState.SLEEP;
152
153 private PowerManager mPowerManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700154 private PowerManager.WakeLock mWakeLock;
155 private PowerManager.WakeLock mPartialWakeLock;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700156 private KeyguardManager mKeyguardManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700157
158 private UpdateLock mUpdateLock;
159
160 // Broadcast receiver for various intent broadcasts (see onCreate())
161 private final BroadcastReceiver mReceiver = new PhoneAppBroadcastReceiver();
162
Ta-wei Yenf0a71bc2017-05-17 12:28:28 -0700163 private final CarrierVvmPackageInstalledReceiver mCarrierVvmPackageInstalledReceiver =
164 new CarrierVvmPackageInstalledReceiver();
165
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700166 Handler mHandler = new Handler() {
167 @Override
168 public void handleMessage(Message msg) {
169 PhoneConstants.State phoneState;
170 switch (msg.what) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700171 // TODO: This event should be handled by the lock screen, just
172 // like the "SIM missing" and "Sim locked" cases (bug 1804111).
173 case EVENT_SIM_NETWORK_LOCKED:
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700174 if (getCarrierConfig().getBoolean(
175 CarrierConfigManager.KEY_IGNORE_SIM_NETWORK_LOCKED_EVENTS_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700176 // Some products don't have the concept of a "SIM network lock"
177 Log.i(LOG_TAG, "Ignoring EVENT_SIM_NETWORK_LOCKED event; "
178 + "not showing 'SIM network unlock' PIN entry screen");
179 } else {
180 // Normal case: show the "SIM network unlock" PIN entry screen.
181 // The user won't be able to do anything else until
182 // they enter a valid SIM network PIN.
183 Log.i(LOG_TAG, "show sim depersonal panel");
Tyler Gunn52a37072015-08-24 14:23:19 -0700184 IccNetworkDepersonalizationPanel.showDialog();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700185 }
186 break;
187
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700188 case EVENT_DATA_ROAMING_DISCONNECTED:
189 notificationMgr.showDataDisconnectedRoaming();
190 break;
191
192 case EVENT_DATA_ROAMING_OK:
193 notificationMgr.hideDataDisconnectedRoaming();
194 break;
195
196 case MMI_COMPLETE:
197 onMMIComplete((AsyncResult) msg.obj);
198 break;
199
200 case MMI_CANCEL:
Stuart Scottdcf40a92014-12-09 10:45:01 -0800201 PhoneUtils.cancelMmiCode(mCM.getFgPhone());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700202 break;
203
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700204 case EVENT_SIM_STATE_CHANGED:
205 // Marks the event where the SIM goes into ready state.
206 // Right now, this is only used for the PUK-unlocking
207 // process.
208 if (msg.obj.equals(IccCardConstants.INTENT_VALUE_ICC_READY)) {
209 // when the right event is triggered and there
210 // are UI objects in the foreground, we close
211 // them to display the lock panel.
212 if (mPUKEntryActivity != null) {
213 mPUKEntryActivity.finish();
214 mPUKEntryActivity = null;
215 }
216 if (mPUKEntryProgressDialog != null) {
217 mPUKEntryProgressDialog.dismiss();
218 mPUKEntryProgressDialog = null;
219 }
220 }
221 break;
222
223 case EVENT_UNSOL_CDMA_INFO_RECORD:
224 //TODO: handle message here;
225 break;
Brad Ebinger3fa43462016-04-12 16:06:48 -0700226 case EVENT_RESTART_SIP:
227 // This should only run if the Phone process crashed and was restarted. We do
228 // not want this running if the device is still in the FBE encrypted state.
229 // This is the same procedure that is triggered in the SipBroadcastReceiver
230 // upon BOOT_COMPLETED.
231 UserManager userManager = UserManager.get(sMe);
232 if (userManager != null && userManager.isUserUnlocked()) {
233 SipUtil.startSipService();
234 }
235 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700236 }
237 }
238 };
239
240 public PhoneGlobals(Context context) {
241 super(context);
242 sMe = this;
243 }
244
245 public void onCreate() {
246 if (VDBG) Log.v(LOG_TAG, "onCreate()...");
247
248 ContentResolver resolver = getContentResolver();
249
250 // Cache the "voice capable" flag.
251 // This flag currently comes from a resource (which is
252 // overrideable on a per-product basis):
253 sVoiceCapable =
254 getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
255 // ...but this might eventually become a PackageManager "system
256 // feature" instead, in which case we'd do something like:
257 // sVoiceCapable =
258 // getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY_VOICE_CALLS);
259
Stuart Scottdcf40a92014-12-09 10:45:01 -0800260 if (mCM == null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700261 // Initialize the telephony framework
262 PhoneFactory.makeDefaultPhones(this);
263
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700264 // Start TelephonyDebugService After the default phone is created.
265 Intent intent = new Intent(this, TelephonyDebugService.class);
266 startService(intent);
267
268 mCM = CallManager.getInstance();
Stuart Scottdcf40a92014-12-09 10:45:01 -0800269 for (Phone phone : PhoneFactory.getPhones()) {
270 mCM.registerPhone(phone);
Stuart Scottdcf40a92014-12-09 10:45:01 -0800271 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700272
273 // Create the NotificationMgr singleton, which is used to display
274 // status bar icons and control other status bar behavior.
275 notificationMgr = NotificationMgr.init(this);
276
Brad Ebinger3fa43462016-04-12 16:06:48 -0700277 // If PhoneGlobals has crashed and is being restarted, then restart.
278 mHandler.sendEmptyMessage(EVENT_RESTART_SIP);
279
Anthony Lee03ebdfc2015-07-27 08:12:02 -0700280 // Create an instance of CdmaPhoneCallState and initialize it to IDLE
281 cdmaPhoneCallState = new CdmaPhoneCallState();
282 cdmaPhoneCallState.CdmaPhoneCallStateInit();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700283
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700284 // before registering for phone state changes
285 mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
286 mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, LOG_TAG);
287 // lock used to keep the processor awake, when we don't care for the display.
288 mPartialWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK
289 | PowerManager.ON_AFTER_RELEASE, LOG_TAG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700290
291 mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
292
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700293 // Get UpdateLock to suppress system-update related events (e.g. dialog show-up)
294 // during phone calls.
295 mUpdateLock = new UpdateLock("phone");
296
297 if (DBG) Log.d(LOG_TAG, "onCreate: mUpdateLock: " + mUpdateLock);
298
299 CallLogger callLogger = new CallLogger(this, new CallLogAsync());
300
Jay Shrauner21a75342013-11-25 16:14:43 -0800301 callGatewayManager = CallGatewayManager.getInstance();
Santos Cordon69a69192013-08-22 14:25:42 -0700302
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700303 // Create the CallController singleton, which is the interface
304 // to the telephony layer for user-initiated telephony functionality
305 // (like making outgoing calls.)
Santos Cordon69a69192013-08-22 14:25:42 -0700306 callController = CallController.init(this, callLogger, callGatewayManager);
307
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700308 // Create the CallerInfoCache singleton, which remembers custom ring tone and
309 // send-to-voicemail settings.
310 //
311 // The asynchronous caching will start just after this call.
312 callerInfoCache = CallerInfoCache.init(this);
313
Stuart Scottdcf40a92014-12-09 10:45:01 -0800314 phoneMgr = PhoneInterfaceManager.init(this, PhoneFactory.getDefaultPhone());
Santos Cordon406c0342013-08-28 00:07:47 -0700315
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800316 configLoader = CarrierConfigLoader.init(this);
317
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700318 // Create the CallNotifer singleton, which handles
319 // asynchronous events from the telephony layer (like
320 // launching the incoming-call UI when an incoming call comes
321 // in.)
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800322 notifier = CallNotifier.init(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700323
Stuart Scottdcf40a92014-12-09 10:45:01 -0800324 PhoneUtils.registerIccStatus(mHandler, EVENT_SIM_NETWORK_LOCKED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700325
326 // register for MMI/USSD
327 mCM.registerForMmiComplete(mHandler, MMI_COMPLETE, null);
328
329 // register connection tracking to PhoneUtils
330 PhoneUtils.initializeConnectionHandler(mCM);
331
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700332 // Register for misc other intent broadcasts.
333 IntentFilter intentFilter =
334 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700335 intentFilter.addAction(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700336 intentFilter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
337 intentFilter.addAction(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);
338 intentFilter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
339 intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700340 registerReceiver(mReceiver, intentFilter);
341
Ta-wei Yenf0a71bc2017-05-17 12:28:28 -0700342 mCarrierVvmPackageInstalledReceiver.register(this);
343
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700344 //set the default values for the preferences in the phone.
pkanward702e542017-02-08 15:44:54 -0800345 PreferenceManager.setDefaultValues(this, R.xml.network_setting_fragment, false);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700346
347 PreferenceManager.setDefaultValues(this, R.xml.call_feature_setting, false);
348
349 // Make sure the audio mode (along with some
350 // audio-mode-related state of our own) is initialized
351 // correctly, given the current state of the phone.
352 PhoneUtils.setAudioMode(mCM);
353 }
354
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700355 // XXX pre-load the SimProvider so that it's ready
356 resolver.getType(Uri.parse("content://icc/adn"));
357
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700358 // TODO: Register for Cdma Information Records
359 // phone.registerCdmaInformationRecord(mHandler, EVENT_UNSOL_CDMA_INFO_RECORD, null);
360
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700361 // Read HAC settings and configure audio hardware
362 if (getResources().getBoolean(R.bool.hac_enabled)) {
Stuart Scottdcf40a92014-12-09 10:45:01 -0800363 int hac = android.provider.Settings.System.getInt(
364 getContentResolver(),
365 android.provider.Settings.System.HEARING_AID,
366 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700367 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andrew Leefb7f92e2015-02-26 16:23:32 -0800368 audioManager.setParameter(SettingsConstants.HAC_KEY,
369 hac == SettingsConstants.HAC_ENABLED
370 ? SettingsConstants.HAC_VAL_ON : SettingsConstants.HAC_VAL_OFF);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700371 }
Santos Cordonff506f52013-11-21 19:13:19 -0800372 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700373
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700374 /**
375 * Returns the singleton instance of the PhoneApp.
376 */
Sailesh Nepal1eaf22b2014-02-22 17:00:49 -0800377 public static PhoneGlobals getInstance() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700378 if (sMe == null) {
379 throw new IllegalStateException("No PhoneGlobals here!");
380 }
381 return sMe;
382 }
383
384 /**
385 * Returns the singleton instance of the PhoneApp if running as the
386 * primary user, otherwise null.
387 */
388 static PhoneGlobals getInstanceIfPrimary() {
389 return sMe;
390 }
391
392 /**
Stuart Scottdcf40a92014-12-09 10:45:01 -0800393 * Returns the default phone.
394 *
Andrew Lee385019f2014-11-24 14:19:50 -0800395 * WARNING: This method should be used carefully, now that there may be multiple phones.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700396 */
Andrew Lee83383e42014-10-31 12:42:28 -0700397 public static Phone getPhone() {
Stuart Scottdcf40a92014-12-09 10:45:01 -0800398 return PhoneFactory.getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700399 }
400
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800401 public static Phone getPhone(int subId) {
402 return PhoneFactory.getPhone(SubscriptionManager.getPhoneId(subId));
Andrew Lee385019f2014-11-24 14:19:50 -0800403 }
404
Santos Cordonde10b752013-09-19 04:11:33 -0700405 /* package */ CallManager getCallManager() {
406 return mCM;
407 }
408
Chris Manton4e9fa912015-06-19 11:26:57 -0700409 public PersistableBundle getCarrierConfig() {
Shishir Agrawald3480e02016-01-25 13:05:49 -0800410 return getCarrierConfigForSubId(SubscriptionManager.getDefaultSubscriptionId());
Jonathan Basseri89b0ab42015-05-01 10:52:40 -0700411 }
412
Chris Manton4e9fa912015-06-19 11:26:57 -0700413 public PersistableBundle getCarrierConfigForSubId(int subId) {
Jonathan Basseri89b0ab42015-05-01 10:52:40 -0700414 return configLoader.getConfigForSubId(subId);
Junda Liu605148f2015-04-28 15:23:40 -0700415 }
416
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700417 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700418 * Sets the activity responsible for un-PUK-blocking the device
419 * so that we may close it when we receive a positive result.
420 * mPUKEntryActivity is also used to indicate to the device that
421 * we are trying to un-PUK-lock the phone. In other words, iff
422 * it is NOT null, then we are trying to unlock and waiting for
423 * the SIM to move to READY state.
424 *
425 * @param activity is the activity to close when PUK has
426 * finished unlocking. Can be set to null to indicate the unlock
427 * or SIM READYing process is over.
428 */
429 void setPukEntryActivity(Activity activity) {
430 mPUKEntryActivity = activity;
431 }
432
433 Activity getPUKEntryActivity() {
434 return mPUKEntryActivity;
435 }
436
437 /**
438 * Sets the dialog responsible for notifying the user of un-PUK-
439 * blocking - SIM READYing progress, so that we may dismiss it
440 * when we receive a positive result.
441 *
442 * @param dialog indicates the progress dialog informing the user
443 * of the state of the device. Dismissed upon completion of
444 * READYing process
445 */
446 void setPukEntryProgressDialog(ProgressDialog dialog) {
447 mPUKEntryProgressDialog = dialog;
448 }
449
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700450 /**
451 * Controls whether or not the screen is allowed to sleep.
452 *
453 * Once sleep is allowed (WakeState is SLEEP), it will rely on the
454 * settings for the poke lock to determine when to timeout and let
455 * the device sleep {@link PhoneGlobals#setScreenTimeout}.
456 *
457 * @param ws tells the device to how to wake.
458 */
459 /* package */ void requestWakeState(WakeState ws) {
460 if (VDBG) Log.d(LOG_TAG, "requestWakeState(" + ws + ")...");
461 synchronized (this) {
462 if (mWakeState != ws) {
463 switch (ws) {
464 case PARTIAL:
465 // acquire the processor wake lock, and release the FULL
466 // lock if it is being held.
467 mPartialWakeLock.acquire();
468 if (mWakeLock.isHeld()) {
469 mWakeLock.release();
470 }
471 break;
472 case FULL:
473 // acquire the full wake lock, and release the PARTIAL
474 // lock if it is being held.
475 mWakeLock.acquire();
476 if (mPartialWakeLock.isHeld()) {
477 mPartialWakeLock.release();
478 }
479 break;
480 case SLEEP:
481 default:
482 // release both the PARTIAL and FULL locks.
483 if (mWakeLock.isHeld()) {
484 mWakeLock.release();
485 }
486 if (mPartialWakeLock.isHeld()) {
487 mPartialWakeLock.release();
488 }
489 break;
490 }
491 mWakeState = ws;
492 }
493 }
494 }
495
496 /**
497 * If we are not currently keeping the screen on, then poke the power
498 * manager to wake up the screen for the user activity timeout duration.
499 */
500 /* package */ void wakeUpScreen() {
501 synchronized (this) {
502 if (mWakeState == WakeState.SLEEP) {
503 if (DBG) Log.d(LOG_TAG, "pulse screen lock");
Dianne Hackborn148769b2015-07-13 17:55:47 -0700504 mPowerManager.wakeUp(SystemClock.uptimeMillis(), "android.phone:WAKE");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700505 }
506 }
507 }
508
509 /**
510 * Sets the wake state and screen timeout based on the current state
511 * of the phone, and the current state of the in-call UI.
512 *
513 * This method is a "UI Policy" wrapper around
514 * {@link PhoneGlobals#requestWakeState} and {@link PhoneGlobals#setScreenTimeout}.
515 *
516 * It's safe to call this method regardless of the state of the Phone
517 * (e.g. whether or not it's idle), and regardless of the state of the
518 * Phone UI (e.g. whether or not the InCallScreen is active.)
519 */
520 /* package */ void updateWakeState() {
521 PhoneConstants.State state = mCM.getState();
522
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700523 // True if the speakerphone is in use. (If so, we *always* use
524 // the default timeout. Since the user is obviously not holding
525 // the phone up to his/her face, we don't need to worry about
526 // false touches, and thus don't need to turn the screen off so
527 // aggressively.)
528 // Note that we need to make a fresh call to this method any
529 // time the speaker state changes. (That happens in
530 // PhoneUtils.turnOnSpeaker().)
531 boolean isSpeakerInUse = (state == PhoneConstants.State.OFFHOOK) && PhoneUtils.isSpeakerOn(this);
532
533 // TODO (bug 1440854): The screen timeout *might* also need to
534 // depend on the bluetooth state, but this isn't as clear-cut as
535 // the speaker state (since while using BT it's common for the
536 // user to put the phone straight into a pocket, in which case the
537 // timeout should probably still be short.)
538
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700539 // Decide whether to force the screen on or not.
540 //
541 // Force the screen to be on if the phone is ringing or dialing,
542 // or if we're displaying the "Call ended" UI for a connection in
543 // the "disconnected" state.
544 // However, if the phone is disconnected while the user is in the
545 // middle of selecting a quick response message, we should not force
546 // the screen to be on.
547 //
548 boolean isRinging = (state == PhoneConstants.State.RINGING);
Stuart Scottdcf40a92014-12-09 10:45:01 -0800549 boolean isDialing = (mCM.getFgPhone().getForegroundCall().getState() == Call.State.DIALING);
Jay Shrauner6fe8fd62013-09-16 19:39:30 -0700550 boolean keepScreenOn = isRinging || isDialing;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700551 // keepScreenOn == true means we'll hold a full wake lock:
552 requestWakeState(keepScreenOn ? WakeState.FULL : WakeState.SLEEP);
553 }
554
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700555 KeyguardManager getKeyguardManager() {
556 return mKeyguardManager;
557 }
558
559 private void onMMIComplete(AsyncResult r) {
560 if (VDBG) Log.d(LOG_TAG, "onMMIComplete()...");
561 MmiCode mmiCode = (MmiCode) r.result;
Stuart Scottdcf40a92014-12-09 10:45:01 -0800562 PhoneUtils.displayMMIComplete(mmiCode.getPhone(), getInstance(), mmiCode, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700563 }
564
fionaxu80126972017-02-01 17:01:26 -0800565 private void initForNewRadioTechnology() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700566 if (DBG) Log.d(LOG_TAG, "initForNewRadioTechnology...");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700567 notifier.updateCallNotifierRegistrationsAfterRadioTechnologyChange();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700568 }
569
Chris Mantona09f3632016-02-09 14:48:42 -0800570 private void handleAirplaneModeChange(Context context, int newMode) {
571 int cellState = Settings.Global.getInt(context.getContentResolver(),
572 Settings.Global.CELL_ON, PhoneConstants.CELL_ON_FLAG);
573 boolean isAirplaneNewlyOn = (newMode == 1);
574 switch (cellState) {
575 case PhoneConstants.CELL_OFF_FLAG:
576 // Airplane mode does not affect the cell radio if user
577 // has turned it off.
578 break;
579 case PhoneConstants.CELL_ON_FLAG:
580 maybeTurnCellOff(context, isAirplaneNewlyOn);
581 break;
582 case PhoneConstants.CELL_OFF_DUE_TO_AIRPLANE_MODE_FLAG:
583 maybeTurnCellOn(context, isAirplaneNewlyOn);
584 break;
585 }
586 }
587
588 /*
589 * Returns true if the radio must be turned off when entering airplane mode.
590 */
591 private boolean isCellOffInAirplaneMode(Context context) {
592 String airplaneModeRadios = Settings.Global.getString(context.getContentResolver(),
593 Settings.Global.AIRPLANE_MODE_RADIOS);
594 return airplaneModeRadios == null
595 || airplaneModeRadios.contains(Settings.Global.RADIO_CELL);
596 }
597
598 private void setRadioPowerOff(Context context) {
599 Log.i(LOG_TAG, "Turning radio off - airplane");
600 Settings.Global.putInt(context.getContentResolver(), Settings.Global.CELL_ON,
601 PhoneConstants.CELL_OFF_DUE_TO_AIRPLANE_MODE_FLAG);
Sooraj Sasindran798bafa2017-04-03 22:49:00 -0700602 SystemProperties.set("persist.radio.airplane_mode_on", "1");
Chris Mantona09f3632016-02-09 14:48:42 -0800603 Settings.Global.putInt(getContentResolver(), Settings.Global.ENABLE_CELLULAR_ON_BOOT, 0);
604 PhoneUtils.setRadioPower(false);
605 }
606
607 private void setRadioPowerOn(Context context) {
608 Log.i(LOG_TAG, "Turning radio on - airplane");
609 Settings.Global.putInt(context.getContentResolver(), Settings.Global.CELL_ON,
610 PhoneConstants.CELL_ON_FLAG);
611 Settings.Global.putInt(getContentResolver(), Settings.Global.ENABLE_CELLULAR_ON_BOOT,
612 1);
Sooraj Sasindran798bafa2017-04-03 22:49:00 -0700613 SystemProperties.set("persist.radio.airplane_mode_on", "0");
Chris Mantona09f3632016-02-09 14:48:42 -0800614 PhoneUtils.setRadioPower(true);
615 }
616
617 private void maybeTurnCellOff(Context context, boolean isAirplaneNewlyOn) {
618 if (isAirplaneNewlyOn) {
Santos Cordone18902f2016-03-22 17:16:04 -0700619 // If we are trying to turn off the radio, make sure there are no active
620 // emergency calls. If there are, switch airplane mode back to off.
621 if (PhoneUtils.isInEmergencyCall(mCM)) {
622 // Switch airplane mode back to off.
623 ConnectivityManager.from(this).setAirplaneMode(false);
624 Toast.makeText(this, R.string.radio_off_during_emergency_call, Toast.LENGTH_LONG)
625 .show();
626 Log.i(LOG_TAG, "Ignoring airplane mode: emergency call. Turning airplane off");
Chris Mantona09f3632016-02-09 14:48:42 -0800627 } else if (isCellOffInAirplaneMode(context)) {
628 setRadioPowerOff(context);
Santos Cordone18902f2016-03-22 17:16:04 -0700629 } else {
Chris Mantona09f3632016-02-09 14:48:42 -0800630 Log.i(LOG_TAG, "Ignoring airplane mode: settings prevent cell radio power off");
Santos Cordone18902f2016-03-22 17:16:04 -0700631 }
Chris Mantona09f3632016-02-09 14:48:42 -0800632 }
633 }
634
635 private void maybeTurnCellOn(Context context, boolean isAirplaneNewlyOn) {
636 if (!isAirplaneNewlyOn) {
637 setRadioPowerOn(context);
Santos Cordone18902f2016-03-22 17:16:04 -0700638 }
639 }
640
Santos Cordon593ab382013-08-06 21:58:23 -0700641 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700642 * Receiver for misc intent broadcasts the Phone app cares about.
643 */
644 private class PhoneAppBroadcastReceiver extends BroadcastReceiver {
645 @Override
646 public void onReceive(Context context, Intent intent) {
647 String action = intent.getAction();
648 if (action.equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) {
Santos Cordone18902f2016-03-22 17:16:04 -0700649 int airplaneMode = Settings.Global.getInt(getContentResolver(),
650 Settings.Global.AIRPLANE_MODE_ON, AIRPLANE_OFF);
651 // Treat any non-OFF values as ON.
652 if (airplaneMode != AIRPLANE_OFF) {
653 airplaneMode = AIRPLANE_ON;
654 }
Chris Mantona09f3632016-02-09 14:48:42 -0800655 handleAirplaneModeChange(context, airplaneMode);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700656 } else if (action.equals(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED)) {
Stuart Scottdcf40a92014-12-09 10:45:01 -0800657 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
658 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Jack Yu2b21d1a2017-05-02 10:22:59 -0700659 int phoneId = SubscriptionManager.getPhoneId(subId);
Jack Yu930a2512017-04-26 17:06:07 -0700660 final String apnType = intent.getStringExtra(PhoneConstants.DATA_APN_TYPE_KEY);
661 final String state = intent.getStringExtra(PhoneConstants.STATE_KEY);
662 final String reason = intent.getStringExtra(PhoneConstants.STATE_CHANGE_REASON_KEY);
Stuart Scottdcf40a92014-12-09 10:45:01 -0800663 if (VDBG) {
664 Log.d(LOG_TAG, "mReceiver: ACTION_ANY_DATA_CONNECTION_STATE_CHANGED");
665 Log.d(LOG_TAG, "- state: " + state);
Jack Yu930a2512017-04-26 17:06:07 -0700666 Log.d(LOG_TAG, "- reason: " + reason);
Stuart Scottdcf40a92014-12-09 10:45:01 -0800667 Log.d(LOG_TAG, "- subId: " + subId);
Stuart Scottdcf40a92014-12-09 10:45:01 -0800668 }
Jack Yu2b21d1a2017-05-02 10:22:59 -0700669 Phone phone = SubscriptionManager.isValidPhoneId(phoneId) ?
670 PhoneFactory.getPhone(phoneId) : PhoneFactory.getDefaultPhone();
Santos Cordonc593d002015-06-03 15:41:15 -0700671
Jack Yu930a2512017-04-26 17:06:07 -0700672 // If the apn type of data connection state changed event is NOT default,
673 // ignore the broadcast intent and avoid action.
674 if (!PhoneConstants.APN_TYPE_DEFAULT.equals(apnType)) {
675 if (VDBG) Log.d(LOG_TAG, "Ignore broadcast intent as not default apn type");
Honggang0a05b652015-09-08 10:48:30 +0800676 return;
677 }
Jack Yu930a2512017-04-26 17:06:07 -0700678
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700679 // The "data disconnected due to roaming" notification is shown
680 // if (a) you have the "data roaming" feature turned off, and
681 // (b) you just lost data connectivity because you're roaming.
Jack Yu953f9352017-05-18 14:41:06 -0700682 // (c) if we haven't shown the notification for this disconnection earlier.
Sanket Padawed9a427e2017-06-08 12:03:07 -0700683 // (d) if data was enabled for the sim
Jack Yu953f9352017-05-18 14:41:06 -0700684 if (!mDataDisconnectedDueToRoaming
685 && PhoneConstants.DataState.DISCONNECTED.name().equals(state)
Jack Yu930a2512017-04-26 17:06:07 -0700686 && Phone.REASON_ROAMING_ON.equals(reason)
Sanket Padawed9a427e2017-06-08 12:03:07 -0700687 && !phone.getDataRoamingEnabled()
688 && phone.getDataEnabled()) {
Jack Yu930a2512017-04-26 17:06:07 -0700689 // Notify the user that data call is disconnected due to roaming. Note that
690 // calling this multiple times will not cause multiple notifications.
691 mHandler.sendEmptyMessage(EVENT_DATA_ROAMING_DISCONNECTED);
Jack Yu953f9352017-05-18 14:41:06 -0700692 mDataDisconnectedDueToRoaming = true;
693 } else if (mDataDisconnectedDueToRoaming
694 && PhoneConstants.DataState.CONNECTED.name().equals(state)) {
Jack Yu930a2512017-04-26 17:06:07 -0700695 // Cancel the notification when data is available. Note it is okay to call this
696 // even if the notification doesn't exist.
697 mHandler.sendEmptyMessage(EVENT_DATA_ROAMING_OK);
Jack Yu953f9352017-05-18 14:41:06 -0700698 mDataDisconnectedDueToRoaming = false;
Yorke Lee4d2db1c2014-11-06 11:37:09 -0800699 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700700 } else if ((action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) &&
701 (mPUKEntryActivity != null)) {
702 // if an attempt to un-PUK-lock the device was made, while we're
703 // receiving this state change notification, notify the handler.
704 // NOTE: This is ONLY triggered if an attempt to un-PUK-lock has
705 // been attempted.
706 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SIM_STATE_CHANGED,
707 intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE)));
708 } else if (action.equals(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED)) {
709 String newPhone = intent.getStringExtra(PhoneConstants.PHONE_NAME_KEY);
fionaxu80126972017-02-01 17:01:26 -0800710 Log.d(LOG_TAG, "Radio technology switched. Now " + newPhone + " is active.");
711 initForNewRadioTechnology();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700712 } else if (action.equals(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED)) {
713 handleServiceStateChanged(intent);
714 } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) {
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530715 int phoneId = intent.getIntExtra(PhoneConstants.PHONE_KEY, 0);
Srikanth Chintala079912a2016-03-03 16:35:01 +0530716 phoneInEcm = PhoneFactory.getPhone(phoneId);
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530717 Log.d(LOG_TAG, "Emergency Callback Mode. phoneId:" + phoneId);
718 if (phoneInEcm != null) {
719 if (TelephonyCapabilities.supportsEcm(phoneInEcm)) {
720 Log.d(LOG_TAG, "Emergency Callback Mode arrived in PhoneApp.");
721 // Start Emergency Callback Mode service
722 if (intent.getBooleanExtra("phoneinECMState", false)) {
723 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 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700738 }
739 }
740 }
741
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700742 private void handleServiceStateChanged(Intent intent) {
743 /**
744 * This used to handle updating EriTextWidgetProvider this routine
745 * and and listening for ACTION_SERVICE_STATE_CHANGED intents could
746 * be removed. But leaving just in case it might be needed in the near
747 * future.
748 */
749
750 // If service just returned, start sending out the queued messages
Santos Cordonc593d002015-06-03 15:41:15 -0700751 Bundle extras = intent.getExtras();
752 if (extras != null) {
753 ServiceState ss = ServiceState.newFromBundle(extras);
754 if (ss != null) {
755 int state = ss.getState();
Jayachandran C2ef9a482017-05-12 22:07:47 -0700756 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
757 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
758 notificationMgr.updateNetworkSelection(state, subId);
Santos Cordonc593d002015-06-03 15:41:15 -0700759 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700760 }
761 }
762
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530763 public Phone getPhoneInEcm() {
764 return phoneInEcm;
765 }
766
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700767 /**
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800768 * Triggers a refresh of the message waiting (voicemail) indicator.
769 *
770 * @param subId the subscription id we should refresh the notification for.
771 */
772 public void refreshMwiIndicator(int subId) {
773 notificationMgr.refreshMwi(subId);
774 }
775
776 /**
Nancy Chenbb49d412015-07-23 13:54:16 -0700777 * Dismisses the message waiting (voicemail) indicator.
778 *
779 * @param subId the subscription id we should dismiss the notification for.
780 */
781 public void clearMwiIndicator(int subId) {
Ta-wei Yen63106682016-07-11 14:11:27 -0700782 // Setting voiceMessageCount to 0 will remove the current notification and clear the system
783 // cached value.
Ta-wei Yen7df340d2016-07-25 10:59:53 -0700784 Phone phone = getPhone(subId);
785 if (phone == null) {
786 Log.w(LOG_TAG, "clearMwiIndicator on null phone, subId:" + subId);
787 } else {
788 phone.setVoiceMessageCount(0);
789 }
Nancy Chenbb49d412015-07-23 13:54:16 -0700790 }
Ta-wei Yenb29425b2016-09-21 17:28:14 -0700791
792 /**
793 * Enables or disables the visual voicemail check for message waiting indicator. Default value
794 * is true. MWI is the traditional voicemail notification which should be suppressed if visual
795 * voicemail is active. {@link NotificationMgr#updateMwi(int, boolean, boolean)} currently
796 * checks the {@link android.provider.VoicemailContract.Status#CONFIGURATION_STATE} to suppress
797 * the MWI, but there are several issues. b/31229016 is a bug that when the device boots the
798 * configuration state will be cleared and the MWI for voicemail that arrives when the device
799 * is offline will be cleared, even if the account cannot be activated. A full solution will be
800 * adding a setMwiEnabled() method and stop checking the configuration state, but that is too
801 * risky at this moment. This is a temporary workaround to shut down the configuration state
802 * check if visual voicemail cannot be activated.
803 * <p>TODO(twyen): implement the setMwiEnabled() mentioned above.
804 *
805 * @param subId the account to set the enabled state
806 */
807 public void setShouldCheckVisualVoicemailConfigurationForMwi(int subId, boolean enabled) {
808 notificationMgr.setShouldCheckVisualVoicemailConfigurationForMwi(subId, enabled);
809 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700810}