blob: 37e62b1a14ded4d3e1bef72eb6f8c6b94665b767 [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
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700149 private WakeState mWakeState = WakeState.SLEEP;
150
151 private PowerManager mPowerManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700152 private PowerManager.WakeLock mWakeLock;
153 private PowerManager.WakeLock mPartialWakeLock;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700154 private KeyguardManager mKeyguardManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700155
156 private UpdateLock mUpdateLock;
157
158 // Broadcast receiver for various intent broadcasts (see onCreate())
159 private final BroadcastReceiver mReceiver = new PhoneAppBroadcastReceiver();
160
Ta-wei Yenf0a71bc2017-05-17 12:28:28 -0700161 private final CarrierVvmPackageInstalledReceiver mCarrierVvmPackageInstalledReceiver =
162 new CarrierVvmPackageInstalledReceiver();
163
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700164 Handler mHandler = new Handler() {
165 @Override
166 public void handleMessage(Message msg) {
167 PhoneConstants.State phoneState;
168 switch (msg.what) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700169 // TODO: This event should be handled by the lock screen, just
170 // like the "SIM missing" and "Sim locked" cases (bug 1804111).
171 case EVENT_SIM_NETWORK_LOCKED:
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700172 if (getCarrierConfig().getBoolean(
173 CarrierConfigManager.KEY_IGNORE_SIM_NETWORK_LOCKED_EVENTS_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700174 // Some products don't have the concept of a "SIM network lock"
175 Log.i(LOG_TAG, "Ignoring EVENT_SIM_NETWORK_LOCKED event; "
176 + "not showing 'SIM network unlock' PIN entry screen");
177 } else {
178 // Normal case: show the "SIM network unlock" PIN entry screen.
179 // The user won't be able to do anything else until
180 // they enter a valid SIM network PIN.
181 Log.i(LOG_TAG, "show sim depersonal panel");
Tyler Gunn52a37072015-08-24 14:23:19 -0700182 IccNetworkDepersonalizationPanel.showDialog();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700183 }
184 break;
185
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700186 case EVENT_DATA_ROAMING_DISCONNECTED:
187 notificationMgr.showDataDisconnectedRoaming();
188 break;
189
190 case EVENT_DATA_ROAMING_OK:
191 notificationMgr.hideDataDisconnectedRoaming();
192 break;
193
194 case MMI_COMPLETE:
195 onMMIComplete((AsyncResult) msg.obj);
196 break;
197
198 case MMI_CANCEL:
Stuart Scottdcf40a92014-12-09 10:45:01 -0800199 PhoneUtils.cancelMmiCode(mCM.getFgPhone());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700200 break;
201
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700202 case EVENT_SIM_STATE_CHANGED:
203 // Marks the event where the SIM goes into ready state.
204 // Right now, this is only used for the PUK-unlocking
205 // process.
206 if (msg.obj.equals(IccCardConstants.INTENT_VALUE_ICC_READY)) {
207 // when the right event is triggered and there
208 // are UI objects in the foreground, we close
209 // them to display the lock panel.
210 if (mPUKEntryActivity != null) {
211 mPUKEntryActivity.finish();
212 mPUKEntryActivity = null;
213 }
214 if (mPUKEntryProgressDialog != null) {
215 mPUKEntryProgressDialog.dismiss();
216 mPUKEntryProgressDialog = null;
217 }
218 }
219 break;
220
221 case EVENT_UNSOL_CDMA_INFO_RECORD:
222 //TODO: handle message here;
223 break;
Brad Ebinger3fa43462016-04-12 16:06:48 -0700224 case EVENT_RESTART_SIP:
225 // This should only run if the Phone process crashed and was restarted. We do
226 // not want this running if the device is still in the FBE encrypted state.
227 // This is the same procedure that is triggered in the SipBroadcastReceiver
228 // upon BOOT_COMPLETED.
229 UserManager userManager = UserManager.get(sMe);
230 if (userManager != null && userManager.isUserUnlocked()) {
231 SipUtil.startSipService();
232 }
233 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700234 }
235 }
236 };
237
238 public PhoneGlobals(Context context) {
239 super(context);
240 sMe = this;
241 }
242
243 public void onCreate() {
244 if (VDBG) Log.v(LOG_TAG, "onCreate()...");
245
246 ContentResolver resolver = getContentResolver();
247
248 // Cache the "voice capable" flag.
249 // This flag currently comes from a resource (which is
250 // overrideable on a per-product basis):
251 sVoiceCapable =
252 getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
253 // ...but this might eventually become a PackageManager "system
254 // feature" instead, in which case we'd do something like:
255 // sVoiceCapable =
256 // getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY_VOICE_CALLS);
257
Stuart Scottdcf40a92014-12-09 10:45:01 -0800258 if (mCM == null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700259 // Initialize the telephony framework
260 PhoneFactory.makeDefaultPhones(this);
261
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700262 // Start TelephonyDebugService After the default phone is created.
263 Intent intent = new Intent(this, TelephonyDebugService.class);
264 startService(intent);
265
266 mCM = CallManager.getInstance();
Stuart Scottdcf40a92014-12-09 10:45:01 -0800267 for (Phone phone : PhoneFactory.getPhones()) {
268 mCM.registerPhone(phone);
Stuart Scottdcf40a92014-12-09 10:45:01 -0800269 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700270
271 // Create the NotificationMgr singleton, which is used to display
272 // status bar icons and control other status bar behavior.
273 notificationMgr = NotificationMgr.init(this);
274
Brad Ebinger3fa43462016-04-12 16:06:48 -0700275 // If PhoneGlobals has crashed and is being restarted, then restart.
276 mHandler.sendEmptyMessage(EVENT_RESTART_SIP);
277
Anthony Lee03ebdfc2015-07-27 08:12:02 -0700278 // Create an instance of CdmaPhoneCallState and initialize it to IDLE
279 cdmaPhoneCallState = new CdmaPhoneCallState();
280 cdmaPhoneCallState.CdmaPhoneCallStateInit();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700281
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700282 // before registering for phone state changes
283 mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
284 mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, LOG_TAG);
285 // lock used to keep the processor awake, when we don't care for the display.
286 mPartialWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK
287 | PowerManager.ON_AFTER_RELEASE, LOG_TAG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700288
289 mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
290
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700291 // Get UpdateLock to suppress system-update related events (e.g. dialog show-up)
292 // during phone calls.
293 mUpdateLock = new UpdateLock("phone");
294
295 if (DBG) Log.d(LOG_TAG, "onCreate: mUpdateLock: " + mUpdateLock);
296
297 CallLogger callLogger = new CallLogger(this, new CallLogAsync());
298
Jay Shrauner21a75342013-11-25 16:14:43 -0800299 callGatewayManager = CallGatewayManager.getInstance();
Santos Cordon69a69192013-08-22 14:25:42 -0700300
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700301 // Create the CallController singleton, which is the interface
302 // to the telephony layer for user-initiated telephony functionality
303 // (like making outgoing calls.)
Santos Cordon69a69192013-08-22 14:25:42 -0700304 callController = CallController.init(this, callLogger, callGatewayManager);
305
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700306 // Create the CallerInfoCache singleton, which remembers custom ring tone and
307 // send-to-voicemail settings.
308 //
309 // The asynchronous caching will start just after this call.
310 callerInfoCache = CallerInfoCache.init(this);
311
Stuart Scottdcf40a92014-12-09 10:45:01 -0800312 phoneMgr = PhoneInterfaceManager.init(this, PhoneFactory.getDefaultPhone());
Santos Cordon406c0342013-08-28 00:07:47 -0700313
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800314 configLoader = CarrierConfigLoader.init(this);
315
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700316 // Create the CallNotifer singleton, which handles
317 // asynchronous events from the telephony layer (like
318 // launching the incoming-call UI when an incoming call comes
319 // in.)
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800320 notifier = CallNotifier.init(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700321
Stuart Scottdcf40a92014-12-09 10:45:01 -0800322 PhoneUtils.registerIccStatus(mHandler, EVENT_SIM_NETWORK_LOCKED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700323
324 // register for MMI/USSD
325 mCM.registerForMmiComplete(mHandler, MMI_COMPLETE, null);
326
327 // register connection tracking to PhoneUtils
328 PhoneUtils.initializeConnectionHandler(mCM);
329
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700330 // Register for misc other intent broadcasts.
331 IntentFilter intentFilter =
332 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700333 intentFilter.addAction(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700334 intentFilter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
335 intentFilter.addAction(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);
336 intentFilter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
337 intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700338 registerReceiver(mReceiver, intentFilter);
339
Ta-wei Yenf0a71bc2017-05-17 12:28:28 -0700340 mCarrierVvmPackageInstalledReceiver.register(this);
341
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700342 //set the default values for the preferences in the phone.
pkanward702e542017-02-08 15:44:54 -0800343 PreferenceManager.setDefaultValues(this, R.xml.network_setting_fragment, false);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700344
345 PreferenceManager.setDefaultValues(this, R.xml.call_feature_setting, false);
346
347 // Make sure the audio mode (along with some
348 // audio-mode-related state of our own) is initialized
349 // correctly, given the current state of the phone.
350 PhoneUtils.setAudioMode(mCM);
351 }
352
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700353 // XXX pre-load the SimProvider so that it's ready
354 resolver.getType(Uri.parse("content://icc/adn"));
355
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700356 // TODO: Register for Cdma Information Records
357 // phone.registerCdmaInformationRecord(mHandler, EVENT_UNSOL_CDMA_INFO_RECORD, null);
358
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700359 // Read HAC settings and configure audio hardware
360 if (getResources().getBoolean(R.bool.hac_enabled)) {
Stuart Scottdcf40a92014-12-09 10:45:01 -0800361 int hac = android.provider.Settings.System.getInt(
362 getContentResolver(),
363 android.provider.Settings.System.HEARING_AID,
364 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700365 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andrew Leefb7f92e2015-02-26 16:23:32 -0800366 audioManager.setParameter(SettingsConstants.HAC_KEY,
367 hac == SettingsConstants.HAC_ENABLED
368 ? SettingsConstants.HAC_VAL_ON : SettingsConstants.HAC_VAL_OFF);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700369 }
Santos Cordonff506f52013-11-21 19:13:19 -0800370 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700371
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700372 /**
373 * Returns the singleton instance of the PhoneApp.
374 */
Sailesh Nepal1eaf22b2014-02-22 17:00:49 -0800375 public static PhoneGlobals getInstance() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700376 if (sMe == null) {
377 throw new IllegalStateException("No PhoneGlobals here!");
378 }
379 return sMe;
380 }
381
382 /**
383 * Returns the singleton instance of the PhoneApp if running as the
384 * primary user, otherwise null.
385 */
386 static PhoneGlobals getInstanceIfPrimary() {
387 return sMe;
388 }
389
390 /**
Stuart Scottdcf40a92014-12-09 10:45:01 -0800391 * Returns the default phone.
392 *
Andrew Lee385019f2014-11-24 14:19:50 -0800393 * WARNING: This method should be used carefully, now that there may be multiple phones.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700394 */
Andrew Lee83383e42014-10-31 12:42:28 -0700395 public static Phone getPhone() {
Stuart Scottdcf40a92014-12-09 10:45:01 -0800396 return PhoneFactory.getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700397 }
398
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800399 public static Phone getPhone(int subId) {
400 return PhoneFactory.getPhone(SubscriptionManager.getPhoneId(subId));
Andrew Lee385019f2014-11-24 14:19:50 -0800401 }
402
Santos Cordonde10b752013-09-19 04:11:33 -0700403 /* package */ CallManager getCallManager() {
404 return mCM;
405 }
406
Chris Manton4e9fa912015-06-19 11:26:57 -0700407 public PersistableBundle getCarrierConfig() {
Shishir Agrawald3480e02016-01-25 13:05:49 -0800408 return getCarrierConfigForSubId(SubscriptionManager.getDefaultSubscriptionId());
Jonathan Basseri89b0ab42015-05-01 10:52:40 -0700409 }
410
Chris Manton4e9fa912015-06-19 11:26:57 -0700411 public PersistableBundle getCarrierConfigForSubId(int subId) {
Jonathan Basseri89b0ab42015-05-01 10:52:40 -0700412 return configLoader.getConfigForSubId(subId);
Junda Liu605148f2015-04-28 15:23:40 -0700413 }
414
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700415 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700416 * Sets the activity responsible for un-PUK-blocking the device
417 * so that we may close it when we receive a positive result.
418 * mPUKEntryActivity is also used to indicate to the device that
419 * we are trying to un-PUK-lock the phone. In other words, iff
420 * it is NOT null, then we are trying to unlock and waiting for
421 * the SIM to move to READY state.
422 *
423 * @param activity is the activity to close when PUK has
424 * finished unlocking. Can be set to null to indicate the unlock
425 * or SIM READYing process is over.
426 */
427 void setPukEntryActivity(Activity activity) {
428 mPUKEntryActivity = activity;
429 }
430
431 Activity getPUKEntryActivity() {
432 return mPUKEntryActivity;
433 }
434
435 /**
436 * Sets the dialog responsible for notifying the user of un-PUK-
437 * blocking - SIM READYing progress, so that we may dismiss it
438 * when we receive a positive result.
439 *
440 * @param dialog indicates the progress dialog informing the user
441 * of the state of the device. Dismissed upon completion of
442 * READYing process
443 */
444 void setPukEntryProgressDialog(ProgressDialog dialog) {
445 mPUKEntryProgressDialog = dialog;
446 }
447
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700448 /**
449 * Controls whether or not the screen is allowed to sleep.
450 *
451 * Once sleep is allowed (WakeState is SLEEP), it will rely on the
452 * settings for the poke lock to determine when to timeout and let
453 * the device sleep {@link PhoneGlobals#setScreenTimeout}.
454 *
455 * @param ws tells the device to how to wake.
456 */
457 /* package */ void requestWakeState(WakeState ws) {
458 if (VDBG) Log.d(LOG_TAG, "requestWakeState(" + ws + ")...");
459 synchronized (this) {
460 if (mWakeState != ws) {
461 switch (ws) {
462 case PARTIAL:
463 // acquire the processor wake lock, and release the FULL
464 // lock if it is being held.
465 mPartialWakeLock.acquire();
466 if (mWakeLock.isHeld()) {
467 mWakeLock.release();
468 }
469 break;
470 case FULL:
471 // acquire the full wake lock, and release the PARTIAL
472 // lock if it is being held.
473 mWakeLock.acquire();
474 if (mPartialWakeLock.isHeld()) {
475 mPartialWakeLock.release();
476 }
477 break;
478 case SLEEP:
479 default:
480 // release both the PARTIAL and FULL locks.
481 if (mWakeLock.isHeld()) {
482 mWakeLock.release();
483 }
484 if (mPartialWakeLock.isHeld()) {
485 mPartialWakeLock.release();
486 }
487 break;
488 }
489 mWakeState = ws;
490 }
491 }
492 }
493
494 /**
495 * If we are not currently keeping the screen on, then poke the power
496 * manager to wake up the screen for the user activity timeout duration.
497 */
498 /* package */ void wakeUpScreen() {
499 synchronized (this) {
500 if (mWakeState == WakeState.SLEEP) {
501 if (DBG) Log.d(LOG_TAG, "pulse screen lock");
Dianne Hackborn148769b2015-07-13 17:55:47 -0700502 mPowerManager.wakeUp(SystemClock.uptimeMillis(), "android.phone:WAKE");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700503 }
504 }
505 }
506
507 /**
508 * Sets the wake state and screen timeout based on the current state
509 * of the phone, and the current state of the in-call UI.
510 *
511 * This method is a "UI Policy" wrapper around
512 * {@link PhoneGlobals#requestWakeState} and {@link PhoneGlobals#setScreenTimeout}.
513 *
514 * It's safe to call this method regardless of the state of the Phone
515 * (e.g. whether or not it's idle), and regardless of the state of the
516 * Phone UI (e.g. whether or not the InCallScreen is active.)
517 */
518 /* package */ void updateWakeState() {
519 PhoneConstants.State state = mCM.getState();
520
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700521 // True if the speakerphone is in use. (If so, we *always* use
522 // the default timeout. Since the user is obviously not holding
523 // the phone up to his/her face, we don't need to worry about
524 // false touches, and thus don't need to turn the screen off so
525 // aggressively.)
526 // Note that we need to make a fresh call to this method any
527 // time the speaker state changes. (That happens in
528 // PhoneUtils.turnOnSpeaker().)
529 boolean isSpeakerInUse = (state == PhoneConstants.State.OFFHOOK) && PhoneUtils.isSpeakerOn(this);
530
531 // TODO (bug 1440854): The screen timeout *might* also need to
532 // depend on the bluetooth state, but this isn't as clear-cut as
533 // the speaker state (since while using BT it's common for the
534 // user to put the phone straight into a pocket, in which case the
535 // timeout should probably still be short.)
536
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700537 // Decide whether to force the screen on or not.
538 //
539 // Force the screen to be on if the phone is ringing or dialing,
540 // or if we're displaying the "Call ended" UI for a connection in
541 // the "disconnected" state.
542 // However, if the phone is disconnected while the user is in the
543 // middle of selecting a quick response message, we should not force
544 // the screen to be on.
545 //
546 boolean isRinging = (state == PhoneConstants.State.RINGING);
Stuart Scottdcf40a92014-12-09 10:45:01 -0800547 boolean isDialing = (mCM.getFgPhone().getForegroundCall().getState() == Call.State.DIALING);
Jay Shrauner6fe8fd62013-09-16 19:39:30 -0700548 boolean keepScreenOn = isRinging || isDialing;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700549 // keepScreenOn == true means we'll hold a full wake lock:
550 requestWakeState(keepScreenOn ? WakeState.FULL : WakeState.SLEEP);
551 }
552
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700553 KeyguardManager getKeyguardManager() {
554 return mKeyguardManager;
555 }
556
557 private void onMMIComplete(AsyncResult r) {
558 if (VDBG) Log.d(LOG_TAG, "onMMIComplete()...");
559 MmiCode mmiCode = (MmiCode) r.result;
Stuart Scottdcf40a92014-12-09 10:45:01 -0800560 PhoneUtils.displayMMIComplete(mmiCode.getPhone(), getInstance(), mmiCode, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700561 }
562
fionaxu80126972017-02-01 17:01:26 -0800563 private void initForNewRadioTechnology() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700564 if (DBG) Log.d(LOG_TAG, "initForNewRadioTechnology...");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700565 notifier.updateCallNotifierRegistrationsAfterRadioTechnologyChange();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700566 }
567
Chris Mantona09f3632016-02-09 14:48:42 -0800568 private void handleAirplaneModeChange(Context context, int newMode) {
569 int cellState = Settings.Global.getInt(context.getContentResolver(),
570 Settings.Global.CELL_ON, PhoneConstants.CELL_ON_FLAG);
571 boolean isAirplaneNewlyOn = (newMode == 1);
572 switch (cellState) {
573 case PhoneConstants.CELL_OFF_FLAG:
574 // Airplane mode does not affect the cell radio if user
575 // has turned it off.
576 break;
577 case PhoneConstants.CELL_ON_FLAG:
578 maybeTurnCellOff(context, isAirplaneNewlyOn);
579 break;
580 case PhoneConstants.CELL_OFF_DUE_TO_AIRPLANE_MODE_FLAG:
581 maybeTurnCellOn(context, isAirplaneNewlyOn);
582 break;
583 }
584 }
585
586 /*
587 * Returns true if the radio must be turned off when entering airplane mode.
588 */
589 private boolean isCellOffInAirplaneMode(Context context) {
590 String airplaneModeRadios = Settings.Global.getString(context.getContentResolver(),
591 Settings.Global.AIRPLANE_MODE_RADIOS);
592 return airplaneModeRadios == null
593 || airplaneModeRadios.contains(Settings.Global.RADIO_CELL);
594 }
595
596 private void setRadioPowerOff(Context context) {
597 Log.i(LOG_TAG, "Turning radio off - airplane");
598 Settings.Global.putInt(context.getContentResolver(), Settings.Global.CELL_ON,
599 PhoneConstants.CELL_OFF_DUE_TO_AIRPLANE_MODE_FLAG);
Sooraj Sasindran798bafa2017-04-03 22:49:00 -0700600 SystemProperties.set("persist.radio.airplane_mode_on", "1");
Chris Mantona09f3632016-02-09 14:48:42 -0800601 Settings.Global.putInt(getContentResolver(), Settings.Global.ENABLE_CELLULAR_ON_BOOT, 0);
602 PhoneUtils.setRadioPower(false);
603 }
604
605 private void setRadioPowerOn(Context context) {
606 Log.i(LOG_TAG, "Turning radio on - airplane");
607 Settings.Global.putInt(context.getContentResolver(), Settings.Global.CELL_ON,
608 PhoneConstants.CELL_ON_FLAG);
609 Settings.Global.putInt(getContentResolver(), Settings.Global.ENABLE_CELLULAR_ON_BOOT,
610 1);
Sooraj Sasindran798bafa2017-04-03 22:49:00 -0700611 SystemProperties.set("persist.radio.airplane_mode_on", "0");
Chris Mantona09f3632016-02-09 14:48:42 -0800612 PhoneUtils.setRadioPower(true);
613 }
614
615 private void maybeTurnCellOff(Context context, boolean isAirplaneNewlyOn) {
616 if (isAirplaneNewlyOn) {
Santos Cordone18902f2016-03-22 17:16:04 -0700617 // If we are trying to turn off the radio, make sure there are no active
618 // emergency calls. If there are, switch airplane mode back to off.
619 if (PhoneUtils.isInEmergencyCall(mCM)) {
620 // Switch airplane mode back to off.
621 ConnectivityManager.from(this).setAirplaneMode(false);
622 Toast.makeText(this, R.string.radio_off_during_emergency_call, Toast.LENGTH_LONG)
623 .show();
624 Log.i(LOG_TAG, "Ignoring airplane mode: emergency call. Turning airplane off");
Chris Mantona09f3632016-02-09 14:48:42 -0800625 } else if (isCellOffInAirplaneMode(context)) {
626 setRadioPowerOff(context);
Santos Cordone18902f2016-03-22 17:16:04 -0700627 } else {
Chris Mantona09f3632016-02-09 14:48:42 -0800628 Log.i(LOG_TAG, "Ignoring airplane mode: settings prevent cell radio power off");
Santos Cordone18902f2016-03-22 17:16:04 -0700629 }
Chris Mantona09f3632016-02-09 14:48:42 -0800630 }
631 }
632
633 private void maybeTurnCellOn(Context context, boolean isAirplaneNewlyOn) {
634 if (!isAirplaneNewlyOn) {
635 setRadioPowerOn(context);
Santos Cordone18902f2016-03-22 17:16:04 -0700636 }
637 }
638
Santos Cordon593ab382013-08-06 21:58:23 -0700639 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700640 * Receiver for misc intent broadcasts the Phone app cares about.
641 */
642 private class PhoneAppBroadcastReceiver extends BroadcastReceiver {
643 @Override
644 public void onReceive(Context context, Intent intent) {
645 String action = intent.getAction();
646 if (action.equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) {
Santos Cordone18902f2016-03-22 17:16:04 -0700647 int airplaneMode = Settings.Global.getInt(getContentResolver(),
648 Settings.Global.AIRPLANE_MODE_ON, AIRPLANE_OFF);
649 // Treat any non-OFF values as ON.
650 if (airplaneMode != AIRPLANE_OFF) {
651 airplaneMode = AIRPLANE_ON;
652 }
Chris Mantona09f3632016-02-09 14:48:42 -0800653 handleAirplaneModeChange(context, airplaneMode);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700654 } else if (action.equals(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED)) {
Stuart Scottdcf40a92014-12-09 10:45:01 -0800655 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
656 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Jack Yu2b21d1a2017-05-02 10:22:59 -0700657 int phoneId = SubscriptionManager.getPhoneId(subId);
Jack Yu930a2512017-04-26 17:06:07 -0700658 final String apnType = intent.getStringExtra(PhoneConstants.DATA_APN_TYPE_KEY);
659 final String state = intent.getStringExtra(PhoneConstants.STATE_KEY);
660 final String reason = intent.getStringExtra(PhoneConstants.STATE_CHANGE_REASON_KEY);
Stuart Scottdcf40a92014-12-09 10:45:01 -0800661 if (VDBG) {
662 Log.d(LOG_TAG, "mReceiver: ACTION_ANY_DATA_CONNECTION_STATE_CHANGED");
663 Log.d(LOG_TAG, "- state: " + state);
Jack Yu930a2512017-04-26 17:06:07 -0700664 Log.d(LOG_TAG, "- reason: " + reason);
Stuart Scottdcf40a92014-12-09 10:45:01 -0800665 Log.d(LOG_TAG, "- subId: " + subId);
Stuart Scottdcf40a92014-12-09 10:45:01 -0800666 }
Jack Yu2b21d1a2017-05-02 10:22:59 -0700667 Phone phone = SubscriptionManager.isValidPhoneId(phoneId) ?
668 PhoneFactory.getPhone(phoneId) : PhoneFactory.getDefaultPhone();
Santos Cordonc593d002015-06-03 15:41:15 -0700669
Jack Yu930a2512017-04-26 17:06:07 -0700670 // If the apn type of data connection state changed event is NOT default,
671 // ignore the broadcast intent and avoid action.
672 if (!PhoneConstants.APN_TYPE_DEFAULT.equals(apnType)) {
673 if (VDBG) Log.d(LOG_TAG, "Ignore broadcast intent as not default apn type");
Honggang0a05b652015-09-08 10:48:30 +0800674 return;
675 }
Jack Yu930a2512017-04-26 17:06:07 -0700676
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700677 // The "data disconnected due to roaming" notification is shown
678 // if (a) you have the "data roaming" feature turned off, and
679 // (b) you just lost data connectivity because you're roaming.
Jack Yu930a2512017-04-26 17:06:07 -0700680 if (PhoneConstants.DataState.DISCONNECTED.name().equals(state)
681 && Phone.REASON_ROAMING_ON.equals(reason)
Jack Yu2b21d1a2017-05-02 10:22:59 -0700682 && !phone.getDataRoamingEnabled()) {
Jack Yu930a2512017-04-26 17:06:07 -0700683 // Notify the user that data call is disconnected due to roaming. Note that
684 // calling this multiple times will not cause multiple notifications.
685 mHandler.sendEmptyMessage(EVENT_DATA_ROAMING_DISCONNECTED);
686 } else if (PhoneConstants.DataState.CONNECTED.name().equals(state)) {
687 // Cancel the notification when data is available. Note it is okay to call this
688 // even if the notification doesn't exist.
689 mHandler.sendEmptyMessage(EVENT_DATA_ROAMING_OK);
Yorke Lee4d2db1c2014-11-06 11:37:09 -0800690 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700691 } else if ((action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) &&
692 (mPUKEntryActivity != null)) {
693 // if an attempt to un-PUK-lock the device was made, while we're
694 // receiving this state change notification, notify the handler.
695 // NOTE: This is ONLY triggered if an attempt to un-PUK-lock has
696 // been attempted.
697 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SIM_STATE_CHANGED,
698 intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE)));
699 } else if (action.equals(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED)) {
700 String newPhone = intent.getStringExtra(PhoneConstants.PHONE_NAME_KEY);
fionaxu80126972017-02-01 17:01:26 -0800701 Log.d(LOG_TAG, "Radio technology switched. Now " + newPhone + " is active.");
702 initForNewRadioTechnology();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700703 } else if (action.equals(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED)) {
704 handleServiceStateChanged(intent);
705 } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) {
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530706 int phoneId = intent.getIntExtra(PhoneConstants.PHONE_KEY, 0);
Srikanth Chintala079912a2016-03-03 16:35:01 +0530707 phoneInEcm = PhoneFactory.getPhone(phoneId);
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530708 Log.d(LOG_TAG, "Emergency Callback Mode. phoneId:" + phoneId);
709 if (phoneInEcm != null) {
710 if (TelephonyCapabilities.supportsEcm(phoneInEcm)) {
711 Log.d(LOG_TAG, "Emergency Callback Mode arrived in PhoneApp.");
712 // Start Emergency Callback Mode service
713 if (intent.getBooleanExtra("phoneinECMState", false)) {
714 context.startService(new Intent(context,
715 EmergencyCallbackModeService.class));
716 } else {
717 phoneInEcm = null;
718 }
719 } else {
720 // It doesn't make sense to get ACTION_EMERGENCY_CALLBACK_MODE_CHANGED
721 // on a device that doesn't support ECM in the first place.
722 Log.e(LOG_TAG, "Got ACTION_EMERGENCY_CALLBACK_MODE_CHANGED, but "
723 + "ECM isn't supported for phone: " + phoneInEcm.getPhoneName());
724 phoneInEcm = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700725 }
726 } else {
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530727 Log.w(LOG_TAG, "phoneInEcm is null.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700728 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700729 }
730 }
731 }
732
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700733 private void handleServiceStateChanged(Intent intent) {
734 /**
735 * This used to handle updating EriTextWidgetProvider this routine
736 * and and listening for ACTION_SERVICE_STATE_CHANGED intents could
737 * be removed. But leaving just in case it might be needed in the near
738 * future.
739 */
740
741 // If service just returned, start sending out the queued messages
Santos Cordonc593d002015-06-03 15:41:15 -0700742 Bundle extras = intent.getExtras();
743 if (extras != null) {
744 ServiceState ss = ServiceState.newFromBundle(extras);
745 if (ss != null) {
746 int state = ss.getState();
747 notificationMgr.updateNetworkSelection(state);
748 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700749 }
750 }
751
Sandeep Kuntade73a6a2014-10-15 18:45:56 +0530752 public Phone getPhoneInEcm() {
753 return phoneInEcm;
754 }
755
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700756 /**
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800757 * Triggers a refresh of the message waiting (voicemail) indicator.
758 *
759 * @param subId the subscription id we should refresh the notification for.
760 */
761 public void refreshMwiIndicator(int subId) {
762 notificationMgr.refreshMwi(subId);
763 }
764
765 /**
Nancy Chenbb49d412015-07-23 13:54:16 -0700766 * Dismisses the message waiting (voicemail) indicator.
767 *
768 * @param subId the subscription id we should dismiss the notification for.
769 */
770 public void clearMwiIndicator(int subId) {
Ta-wei Yen63106682016-07-11 14:11:27 -0700771 // Setting voiceMessageCount to 0 will remove the current notification and clear the system
772 // cached value.
Ta-wei Yen7df340d2016-07-25 10:59:53 -0700773 Phone phone = getPhone(subId);
774 if (phone == null) {
775 Log.w(LOG_TAG, "clearMwiIndicator on null phone, subId:" + subId);
776 } else {
777 phone.setVoiceMessageCount(0);
778 }
Nancy Chenbb49d412015-07-23 13:54:16 -0700779 }
Ta-wei Yenb29425b2016-09-21 17:28:14 -0700780
781 /**
782 * Enables or disables the visual voicemail check for message waiting indicator. Default value
783 * is true. MWI is the traditional voicemail notification which should be suppressed if visual
784 * voicemail is active. {@link NotificationMgr#updateMwi(int, boolean, boolean)} currently
785 * checks the {@link android.provider.VoicemailContract.Status#CONFIGURATION_STATE} to suppress
786 * the MWI, but there are several issues. b/31229016 is a bug that when the device boots the
787 * configuration state will be cleared and the MWI for voicemail that arrives when the device
788 * is offline will be cleared, even if the account cannot be activated. A full solution will be
789 * adding a setMwiEnabled() method and stop checking the configuration state, but that is too
790 * risky at this moment. This is a temporary workaround to shut down the configuration state
791 * check if visual voicemail cannot be activated.
792 * <p>TODO(twyen): implement the setMwiEnabled() mentioned above.
793 *
794 * @param subId the account to set the enabled state
795 */
796 public void setShouldCheckVisualVoicemailConfigurationForMwi(int subId, boolean enabled) {
797 notificationMgr.setShouldCheckVisualVoicemailConfigurationForMwi(subId, enabled);
798 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700799}