blob: 026e798cca8cdf4d1099df2c86a4dd452fe91aa8 [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;
21import android.app.PendingIntent;
22import android.app.ProgressDialog;
Yorke Leeca6ec3b2013-08-29 14:21:43 -070023import android.app.TaskStackBuilder;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import android.bluetooth.BluetoothAdapter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070025import android.bluetooth.IBluetoothHeadsetPhone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070026import android.content.BroadcastReceiver;
27import android.content.ComponentName;
28import android.content.ContentResolver;
29import android.content.Context;
30import android.content.ContextWrapper;
31import android.content.Intent;
32import android.content.IntentFilter;
33import android.content.ServiceConnection;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070034import android.media.AudioManager;
35import android.net.Uri;
36import android.os.AsyncResult;
Junda Liu605148f2015-04-28 15:23:40 -070037import android.os.Bundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.os.Handler;
39import android.os.IBinder;
40import android.os.IPowerManager;
41import android.os.Message;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070042import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.os.PowerManager;
44import android.os.RemoteException;
45import android.os.ServiceManager;
46import android.os.SystemClock;
47import android.os.SystemProperties;
48import android.os.UpdateLock;
49import android.os.UserHandle;
50import android.preference.PreferenceManager;
51import android.provider.Settings.System;
Junda Liu12f7d802015-05-01 12:06:44 -070052import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053import android.telephony.ServiceState;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080054import android.telephony.SubscriptionInfo;
Andrew Lee385019f2014-11-24 14:19:50 -080055import android.telephony.SubscriptionManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056import android.util.Log;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057
58import com.android.internal.telephony.Call;
59import com.android.internal.telephony.CallManager;
60import com.android.internal.telephony.IccCard;
61import com.android.internal.telephony.IccCardConstants;
62import com.android.internal.telephony.MmiCode;
63import com.android.internal.telephony.Phone;
64import com.android.internal.telephony.PhoneConstants;
65import com.android.internal.telephony.PhoneFactory;
Andrew Lee385019f2014-11-24 14:19:50 -080066import com.android.internal.telephony.SubscriptionController;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070067import com.android.internal.telephony.TelephonyCapabilities;
68import com.android.internal.telephony.TelephonyIntents;
Santos Cordon352ff652014-05-30 01:41:45 -070069import com.android.phone.common.CallLogAsync;
Andrew Leefb7f92e2015-02-26 16:23:32 -080070import com.android.phone.settings.SettingsConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071import com.android.server.sip.SipService;
Santos Cordon76aaf482015-04-08 10:58:27 -070072import com.android.services.telephony.activation.SimActivationManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070073
Andrew Lee385019f2014-11-24 14:19:50 -080074import java.util.ArrayList;
75import java.util.List;
76
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077/**
78 * Global state for the telephony subsystem when running in the primary
79 * phone process.
80 */
Sailesh Nepalbf900542014-07-15 16:18:32 -070081public class PhoneGlobals extends ContextWrapper {
Andrew Lee83383e42014-10-31 12:42:28 -070082 public static final String LOG_TAG = "PhoneApp";
Santos Cordon7d4ddf62013-07-10 11:58:08 -070083
84 /**
85 * Phone app-wide debug level:
86 * 0 - no debug logging
87 * 1 - normal debug logging if ro.debuggable is set (which is true in
88 * "eng" and "userdebug" builds but not "user" builds)
89 * 2 - ultra-verbose debug logging
90 *
91 * Most individual classes in the phone app have a local DBG constant,
92 * typically set to
93 * (PhoneApp.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1)
94 * or else
95 * (PhoneApp.DBG_LEVEL >= 2)
96 * depending on the desired verbosity.
97 *
98 * ***** DO NOT SUBMIT WITH DBG_LEVEL > 0 *************
99 */
Andrew Lee88b51e22014-10-29 15:48:51 -0700100 public static final int DBG_LEVEL = 0;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700101
102 private static final boolean DBG =
103 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
104 private static final boolean VDBG = (PhoneGlobals.DBG_LEVEL >= 2);
105
106 // Message codes; see mHandler below.
107 private static final int EVENT_SIM_NETWORK_LOCKED = 3;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700108 private static final int EVENT_SIM_STATE_CHANGED = 8;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700109 private static final int EVENT_DATA_ROAMING_DISCONNECTED = 10;
110 private static final int EVENT_DATA_ROAMING_OK = 11;
111 private static final int EVENT_UNSOL_CDMA_INFO_RECORD = 12;
Bryce Lee5ccda612015-12-04 15:44:16 -0800112 private static final int EVENT_START_SIP_SERVICE = 13;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700113
114 // The MMI codes are also used by the InCallScreen.
115 public static final int MMI_INITIATE = 51;
116 public static final int MMI_COMPLETE = 52;
117 public static final int MMI_CANCEL = 53;
118 // Don't use message codes larger than 99 here; those are reserved for
119 // the individual Activities of the Phone UI.
120
121 /**
122 * Allowable values for the wake lock code.
123 * SLEEP means the device can be put to sleep.
124 * PARTIAL means wake the processor, but we display can be kept off.
125 * FULL means wake both the processor and the display.
126 */
127 public enum WakeState {
128 SLEEP,
129 PARTIAL,
130 FULL
131 }
132
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700133 private static PhoneGlobals sMe;
134
135 // A few important fields we expose to the rest of the package
136 // directly (rather than thru set/get methods) for efficiency.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700137 CallController callController;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700138 CallManager mCM;
Santos Cordon63a84242013-07-23 13:32:52 -0700139 CallNotifier notifier;
140 CallerInfoCache callerInfoCache;
Santos Cordon63a84242013-07-23 13:32:52 -0700141 NotificationMgr notificationMgr;
Andrew Lee9431b832015-03-09 18:46:45 -0700142 public PhoneInterfaceManager phoneMgr;
Santos Cordon76aaf482015-04-08 10:58:27 -0700143 public SimActivationManager simActivationManager;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800144 CarrierConfigLoader configLoader;
Santos Cordon63a84242013-07-23 13:32:52 -0700145
Santos Cordon69a69192013-08-22 14:25:42 -0700146 private CallGatewayManager callGatewayManager;
Santos Cordon63a84242013-07-23 13:32:52 -0700147
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700148 static boolean sVoiceCapable = true;
149
Santos Cordonc593d002015-06-03 15:41:15 -0700150 // TODO: Remove, no longer used.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700151 CdmaPhoneCallState cdmaPhoneCallState;
152
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700153 // The currently-active PUK entry activity and progress dialog.
154 // Normally, these are the Emergency Dialer and the subsequent
155 // progress dialog. null if there is are no such objects in
156 // the foreground.
157 private Activity mPUKEntryActivity;
158 private ProgressDialog mPUKEntryProgressDialog;
159
Yorke Lee4d2db1c2014-11-06 11:37:09 -0800160 private boolean mDataDisconnectedDueToRoaming = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700161
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700162 private WakeState mWakeState = WakeState.SLEEP;
163
164 private PowerManager mPowerManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700165 private PowerManager.WakeLock mWakeLock;
166 private PowerManager.WakeLock mPartialWakeLock;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700167 private KeyguardManager mKeyguardManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700168
169 private UpdateLock mUpdateLock;
170
171 // Broadcast receiver for various intent broadcasts (see onCreate())
172 private final BroadcastReceiver mReceiver = new PhoneAppBroadcastReceiver();
173
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700174 /**
175 * The singleton OtaUtils instance used for OTASP calls.
176 *
177 * The OtaUtils instance is created lazily the first time we need to
178 * make an OTASP call, regardless of whether it's an interactive or
179 * non-interactive OTASP call.
180 */
181 public OtaUtils otaUtils;
182
183 // Following are the CDMA OTA information Objects used during OTA Call.
184 // cdmaOtaProvisionData object store static OTA information that needs
185 // to be maintained even during Slider open/close scenarios.
186 // cdmaOtaConfigData object stores configuration info to control visiblity
187 // of each OTA Screens.
188 // cdmaOtaScreenState object store OTA Screen State information.
189 public OtaUtils.CdmaOtaProvisionData cdmaOtaProvisionData;
190 public OtaUtils.CdmaOtaConfigData cdmaOtaConfigData;
191 public OtaUtils.CdmaOtaScreenState cdmaOtaScreenState;
192 public OtaUtils.CdmaOtaInCallScreenUiState cdmaOtaInCallScreenUiState;
193
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700194 Handler mHandler = new Handler() {
195 @Override
196 public void handleMessage(Message msg) {
197 PhoneConstants.State phoneState;
198 switch (msg.what) {
199 // Starts the SIP service. It's a no-op if SIP API is not supported
200 // on the deivce.
201 // TODO: Having the phone process host the SIP service is only
202 // temporary. Will move it to a persistent communication process
203 // later.
204 case EVENT_START_SIP_SERVICE:
205 SipService.start(getApplicationContext());
206 break;
207
208 // TODO: This event should be handled by the lock screen, just
209 // like the "SIM missing" and "Sim locked" cases (bug 1804111).
210 case EVENT_SIM_NETWORK_LOCKED:
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700211 if (getCarrierConfig().getBoolean(
212 CarrierConfigManager.KEY_IGNORE_SIM_NETWORK_LOCKED_EVENTS_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700213 // Some products don't have the concept of a "SIM network lock"
214 Log.i(LOG_TAG, "Ignoring EVENT_SIM_NETWORK_LOCKED event; "
215 + "not showing 'SIM network unlock' PIN entry screen");
216 } else {
217 // Normal case: show the "SIM network unlock" PIN entry screen.
218 // The user won't be able to do anything else until
219 // they enter a valid SIM network PIN.
220 Log.i(LOG_TAG, "show sim depersonal panel");
Tyler Gunn52a37072015-08-24 14:23:19 -0700221 IccNetworkDepersonalizationPanel.showDialog();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700222 }
223 break;
224
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700225 case EVENT_DATA_ROAMING_DISCONNECTED:
226 notificationMgr.showDataDisconnectedRoaming();
227 break;
228
229 case EVENT_DATA_ROAMING_OK:
230 notificationMgr.hideDataDisconnectedRoaming();
231 break;
232
233 case MMI_COMPLETE:
234 onMMIComplete((AsyncResult) msg.obj);
235 break;
236
237 case MMI_CANCEL:
Stuart Scottdcf40a92014-12-09 10:45:01 -0800238 PhoneUtils.cancelMmiCode(mCM.getFgPhone());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700239 break;
240
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700241 case EVENT_SIM_STATE_CHANGED:
242 // Marks the event where the SIM goes into ready state.
243 // Right now, this is only used for the PUK-unlocking
244 // process.
245 if (msg.obj.equals(IccCardConstants.INTENT_VALUE_ICC_READY)) {
246 // when the right event is triggered and there
247 // are UI objects in the foreground, we close
248 // them to display the lock panel.
249 if (mPUKEntryActivity != null) {
250 mPUKEntryActivity.finish();
251 mPUKEntryActivity = null;
252 }
253 if (mPUKEntryProgressDialog != null) {
254 mPUKEntryProgressDialog.dismiss();
255 mPUKEntryProgressDialog = null;
256 }
257 }
258 break;
259
260 case EVENT_UNSOL_CDMA_INFO_RECORD:
261 //TODO: handle message here;
262 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700263 }
264 }
265 };
266
267 public PhoneGlobals(Context context) {
268 super(context);
269 sMe = this;
270 }
271
272 public void onCreate() {
273 if (VDBG) Log.v(LOG_TAG, "onCreate()...");
274
275 ContentResolver resolver = getContentResolver();
276
277 // Cache the "voice capable" flag.
278 // This flag currently comes from a resource (which is
279 // overrideable on a per-product basis):
280 sVoiceCapable =
281 getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
282 // ...but this might eventually become a PackageManager "system
283 // feature" instead, in which case we'd do something like:
284 // sVoiceCapable =
285 // getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY_VOICE_CALLS);
286
Stuart Scottdcf40a92014-12-09 10:45:01 -0800287 if (mCM == null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700288 // Initialize the telephony framework
289 PhoneFactory.makeDefaultPhones(this);
290
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700291 // Start TelephonyDebugService After the default phone is created.
292 Intent intent = new Intent(this, TelephonyDebugService.class);
293 startService(intent);
294
295 mCM = CallManager.getInstance();
Stuart Scottdcf40a92014-12-09 10:45:01 -0800296 for (Phone phone : PhoneFactory.getPhones()) {
297 mCM.registerPhone(phone);
Stuart Scottdcf40a92014-12-09 10:45:01 -0800298 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700299
300 // Create the NotificationMgr singleton, which is used to display
301 // status bar icons and control other status bar behavior.
302 notificationMgr = NotificationMgr.init(this);
303
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700304 mHandler.sendEmptyMessage(EVENT_START_SIP_SERVICE);
305
Anthony Lee03ebdfc2015-07-27 08:12:02 -0700306 // Create an instance of CdmaPhoneCallState and initialize it to IDLE
307 cdmaPhoneCallState = new CdmaPhoneCallState();
308 cdmaPhoneCallState.CdmaPhoneCallStateInit();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700309
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700310 // before registering for phone state changes
311 mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
312 mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, LOG_TAG);
313 // lock used to keep the processor awake, when we don't care for the display.
314 mPartialWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK
315 | PowerManager.ON_AFTER_RELEASE, LOG_TAG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700316
317 mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
318
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700319 // Get UpdateLock to suppress system-update related events (e.g. dialog show-up)
320 // during phone calls.
321 mUpdateLock = new UpdateLock("phone");
322
323 if (DBG) Log.d(LOG_TAG, "onCreate: mUpdateLock: " + mUpdateLock);
324
325 CallLogger callLogger = new CallLogger(this, new CallLogAsync());
326
Jay Shrauner21a75342013-11-25 16:14:43 -0800327 callGatewayManager = CallGatewayManager.getInstance();
Santos Cordon69a69192013-08-22 14:25:42 -0700328
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700329 // Create the CallController singleton, which is the interface
330 // to the telephony layer for user-initiated telephony functionality
331 // (like making outgoing calls.)
Santos Cordon69a69192013-08-22 14:25:42 -0700332 callController = CallController.init(this, callLogger, callGatewayManager);
333
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700334 // Create the CallerInfoCache singleton, which remembers custom ring tone and
335 // send-to-voicemail settings.
336 //
337 // The asynchronous caching will start just after this call.
338 callerInfoCache = CallerInfoCache.init(this);
339
Stuart Scottdcf40a92014-12-09 10:45:01 -0800340 phoneMgr = PhoneInterfaceManager.init(this, PhoneFactory.getDefaultPhone());
Santos Cordon406c0342013-08-28 00:07:47 -0700341
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800342 configLoader = CarrierConfigLoader.init(this);
343
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700344 // Create the CallNotifer singleton, which handles
345 // asynchronous events from the telephony layer (like
346 // launching the incoming-call UI when an incoming call comes
347 // in.)
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800348 notifier = CallNotifier.init(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700349
Stuart Scottdcf40a92014-12-09 10:45:01 -0800350 PhoneUtils.registerIccStatus(mHandler, EVENT_SIM_NETWORK_LOCKED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700351
352 // register for MMI/USSD
353 mCM.registerForMmiComplete(mHandler, MMI_COMPLETE, null);
354
355 // register connection tracking to PhoneUtils
356 PhoneUtils.initializeConnectionHandler(mCM);
357
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700358 // Register for misc other intent broadcasts.
359 IntentFilter intentFilter =
360 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700361 intentFilter.addAction(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700362 intentFilter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
363 intentFilter.addAction(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);
364 intentFilter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
365 intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700366 registerReceiver(mReceiver, intentFilter);
367
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700368 //set the default values for the preferences in the phone.
369 PreferenceManager.setDefaultValues(this, R.xml.network_setting, false);
370
371 PreferenceManager.setDefaultValues(this, R.xml.call_feature_setting, false);
372
373 // Make sure the audio mode (along with some
374 // audio-mode-related state of our own) is initialized
375 // correctly, given the current state of the phone.
376 PhoneUtils.setAudioMode(mCM);
377 }
378
Santos Cordon52bc81b2014-10-07 19:55:12 -0700379 cdmaOtaProvisionData = new OtaUtils.CdmaOtaProvisionData();
380 cdmaOtaConfigData = new OtaUtils.CdmaOtaConfigData();
381 cdmaOtaScreenState = new OtaUtils.CdmaOtaScreenState();
382 cdmaOtaInCallScreenUiState = new OtaUtils.CdmaOtaInCallScreenUiState();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700383
Santos Cordon76aaf482015-04-08 10:58:27 -0700384 simActivationManager = new SimActivationManager();
385
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700386 // XXX pre-load the SimProvider so that it's ready
387 resolver.getType(Uri.parse("content://icc/adn"));
388
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700389 // TODO: Register for Cdma Information Records
390 // phone.registerCdmaInformationRecord(mHandler, EVENT_UNSOL_CDMA_INFO_RECORD, null);
391
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700392 // Read HAC settings and configure audio hardware
393 if (getResources().getBoolean(R.bool.hac_enabled)) {
Stuart Scottdcf40a92014-12-09 10:45:01 -0800394 int hac = android.provider.Settings.System.getInt(
395 getContentResolver(),
396 android.provider.Settings.System.HEARING_AID,
397 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700398 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andrew Leefb7f92e2015-02-26 16:23:32 -0800399 audioManager.setParameter(SettingsConstants.HAC_KEY,
400 hac == SettingsConstants.HAC_ENABLED
401 ? SettingsConstants.HAC_VAL_ON : SettingsConstants.HAC_VAL_OFF);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700402 }
Santos Cordonff506f52013-11-21 19:13:19 -0800403 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700404
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700405 /**
406 * Returns the singleton instance of the PhoneApp.
407 */
Sailesh Nepal1eaf22b2014-02-22 17:00:49 -0800408 public static PhoneGlobals getInstance() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700409 if (sMe == null) {
410 throw new IllegalStateException("No PhoneGlobals here!");
411 }
412 return sMe;
413 }
414
415 /**
416 * Returns the singleton instance of the PhoneApp if running as the
417 * primary user, otherwise null.
418 */
419 static PhoneGlobals getInstanceIfPrimary() {
420 return sMe;
421 }
422
423 /**
Stuart Scottdcf40a92014-12-09 10:45:01 -0800424 * Returns the default phone.
425 *
Andrew Lee385019f2014-11-24 14:19:50 -0800426 * WARNING: This method should be used carefully, now that there may be multiple phones.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700427 */
Andrew Lee83383e42014-10-31 12:42:28 -0700428 public static Phone getPhone() {
Stuart Scottdcf40a92014-12-09 10:45:01 -0800429 return PhoneFactory.getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700430 }
431
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800432 public static Phone getPhone(int subId) {
433 return PhoneFactory.getPhone(SubscriptionManager.getPhoneId(subId));
Andrew Lee385019f2014-11-24 14:19:50 -0800434 }
435
Santos Cordonde10b752013-09-19 04:11:33 -0700436 /* package */ CallManager getCallManager() {
437 return mCM;
438 }
439
Chris Manton4e9fa912015-06-19 11:26:57 -0700440 public PersistableBundle getCarrierConfig() {
Shishir Agrawald3480e02016-01-25 13:05:49 -0800441 return getCarrierConfigForSubId(SubscriptionManager.getDefaultSubscriptionId());
Jonathan Basseri89b0ab42015-05-01 10:52:40 -0700442 }
443
Chris Manton4e9fa912015-06-19 11:26:57 -0700444 public PersistableBundle getCarrierConfigForSubId(int subId) {
Jonathan Basseri89b0ab42015-05-01 10:52:40 -0700445 return configLoader.getConfigForSubId(subId);
Junda Liu605148f2015-04-28 15:23:40 -0700446 }
447
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700448 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700449 * Handles OTASP-related events from the telephony layer.
450 *
451 * While an OTASP call is active, the CallNotifier forwards
452 * OTASP-related telephony events to this method.
453 */
454 void handleOtaspEvent(Message msg) {
455 if (DBG) Log.d(LOG_TAG, "handleOtaspEvent(message " + msg + ")...");
456
457 if (otaUtils == null) {
458 // We shouldn't be getting OTASP events without ever
459 // having started the OTASP call in the first place!
460 Log.w(LOG_TAG, "handleOtaEvents: got an event but otaUtils is null! "
461 + "message = " + msg);
462 return;
463 }
464
465 otaUtils.onOtaProvisionStatusChanged((AsyncResult) msg.obj);
466 }
467
468 /**
469 * Similarly, handle the disconnect event of an OTASP call
470 * by forwarding it to the OtaUtils instance.
471 */
472 /* package */ void handleOtaspDisconnect() {
473 if (DBG) Log.d(LOG_TAG, "handleOtaspDisconnect()...");
474
475 if (otaUtils == null) {
476 // We shouldn't be getting OTASP events without ever
477 // having started the OTASP call in the first place!
478 Log.w(LOG_TAG, "handleOtaspDisconnect: otaUtils is null!");
479 return;
480 }
481
482 otaUtils.onOtaspDisconnect();
483 }
484
485 /**
486 * Sets the activity responsible for un-PUK-blocking the device
487 * so that we may close it when we receive a positive result.
488 * mPUKEntryActivity is also used to indicate to the device that
489 * we are trying to un-PUK-lock the phone. In other words, iff
490 * it is NOT null, then we are trying to unlock and waiting for
491 * the SIM to move to READY state.
492 *
493 * @param activity is the activity to close when PUK has
494 * finished unlocking. Can be set to null to indicate the unlock
495 * or SIM READYing process is over.
496 */
497 void setPukEntryActivity(Activity activity) {
498 mPUKEntryActivity = activity;
499 }
500
501 Activity getPUKEntryActivity() {
502 return mPUKEntryActivity;
503 }
504
505 /**
506 * Sets the dialog responsible for notifying the user of un-PUK-
507 * blocking - SIM READYing progress, so that we may dismiss it
508 * when we receive a positive result.
509 *
510 * @param dialog indicates the progress dialog informing the user
511 * of the state of the device. Dismissed upon completion of
512 * READYing process
513 */
514 void setPukEntryProgressDialog(ProgressDialog dialog) {
515 mPUKEntryProgressDialog = dialog;
516 }
517
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700518 /**
519 * Controls whether or not the screen is allowed to sleep.
520 *
521 * Once sleep is allowed (WakeState is SLEEP), it will rely on the
522 * settings for the poke lock to determine when to timeout and let
523 * the device sleep {@link PhoneGlobals#setScreenTimeout}.
524 *
525 * @param ws tells the device to how to wake.
526 */
527 /* package */ void requestWakeState(WakeState ws) {
528 if (VDBG) Log.d(LOG_TAG, "requestWakeState(" + ws + ")...");
529 synchronized (this) {
530 if (mWakeState != ws) {
531 switch (ws) {
532 case PARTIAL:
533 // acquire the processor wake lock, and release the FULL
534 // lock if it is being held.
535 mPartialWakeLock.acquire();
536 if (mWakeLock.isHeld()) {
537 mWakeLock.release();
538 }
539 break;
540 case FULL:
541 // acquire the full wake lock, and release the PARTIAL
542 // lock if it is being held.
543 mWakeLock.acquire();
544 if (mPartialWakeLock.isHeld()) {
545 mPartialWakeLock.release();
546 }
547 break;
548 case SLEEP:
549 default:
550 // release both the PARTIAL and FULL locks.
551 if (mWakeLock.isHeld()) {
552 mWakeLock.release();
553 }
554 if (mPartialWakeLock.isHeld()) {
555 mPartialWakeLock.release();
556 }
557 break;
558 }
559 mWakeState = ws;
560 }
561 }
562 }
563
564 /**
565 * If we are not currently keeping the screen on, then poke the power
566 * manager to wake up the screen for the user activity timeout duration.
567 */
568 /* package */ void wakeUpScreen() {
569 synchronized (this) {
570 if (mWakeState == WakeState.SLEEP) {
571 if (DBG) Log.d(LOG_TAG, "pulse screen lock");
Dianne Hackborn148769b2015-07-13 17:55:47 -0700572 mPowerManager.wakeUp(SystemClock.uptimeMillis(), "android.phone:WAKE");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700573 }
574 }
575 }
576
577 /**
578 * Sets the wake state and screen timeout based on the current state
579 * of the phone, and the current state of the in-call UI.
580 *
581 * This method is a "UI Policy" wrapper around
582 * {@link PhoneGlobals#requestWakeState} and {@link PhoneGlobals#setScreenTimeout}.
583 *
584 * It's safe to call this method regardless of the state of the Phone
585 * (e.g. whether or not it's idle), and regardless of the state of the
586 * Phone UI (e.g. whether or not the InCallScreen is active.)
587 */
588 /* package */ void updateWakeState() {
589 PhoneConstants.State state = mCM.getState();
590
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700591 // True if the speakerphone is in use. (If so, we *always* use
592 // the default timeout. Since the user is obviously not holding
593 // the phone up to his/her face, we don't need to worry about
594 // false touches, and thus don't need to turn the screen off so
595 // aggressively.)
596 // Note that we need to make a fresh call to this method any
597 // time the speaker state changes. (That happens in
598 // PhoneUtils.turnOnSpeaker().)
599 boolean isSpeakerInUse = (state == PhoneConstants.State.OFFHOOK) && PhoneUtils.isSpeakerOn(this);
600
601 // TODO (bug 1440854): The screen timeout *might* also need to
602 // depend on the bluetooth state, but this isn't as clear-cut as
603 // the speaker state (since while using BT it's common for the
604 // user to put the phone straight into a pocket, in which case the
605 // timeout should probably still be short.)
606
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700607 // Decide whether to force the screen on or not.
608 //
609 // Force the screen to be on if the phone is ringing or dialing,
610 // or if we're displaying the "Call ended" UI for a connection in
611 // the "disconnected" state.
612 // However, if the phone is disconnected while the user is in the
613 // middle of selecting a quick response message, we should not force
614 // the screen to be on.
615 //
616 boolean isRinging = (state == PhoneConstants.State.RINGING);
Stuart Scottdcf40a92014-12-09 10:45:01 -0800617 boolean isDialing = (mCM.getFgPhone().getForegroundCall().getState() == Call.State.DIALING);
Jay Shrauner6fe8fd62013-09-16 19:39:30 -0700618 boolean keepScreenOn = isRinging || isDialing;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700619 // keepScreenOn == true means we'll hold a full wake lock:
620 requestWakeState(keepScreenOn ? WakeState.FULL : WakeState.SLEEP);
621 }
622
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700623 KeyguardManager getKeyguardManager() {
624 return mKeyguardManager;
625 }
626
627 private void onMMIComplete(AsyncResult r) {
628 if (VDBG) Log.d(LOG_TAG, "onMMIComplete()...");
629 MmiCode mmiCode = (MmiCode) r.result;
Stuart Scottdcf40a92014-12-09 10:45:01 -0800630 PhoneUtils.displayMMIComplete(mmiCode.getPhone(), getInstance(), mmiCode, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700631 }
632
Stuart Scottdcf40a92014-12-09 10:45:01 -0800633 private void initForNewRadioTechnology(int phoneId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700634 if (DBG) Log.d(LOG_TAG, "initForNewRadioTechnology...");
635
Stuart Scottdcf40a92014-12-09 10:45:01 -0800636 final Phone phone = PhoneFactory.getPhone(phoneId);
Santos Cordonc593d002015-06-03 15:41:15 -0700637 if (phone == null || !TelephonyCapabilities.supportsOtasp(phone)) {
638 // Clean up OTA for non-CDMA since it is only valid for CDMA.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700639 clearOtaState();
640 }
641
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700642 notifier.updateCallNotifierRegistrationsAfterRadioTechnologyChange();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700643 }
644
Santos Cordon593ab382013-08-06 21:58:23 -0700645 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700646 * Receiver for misc intent broadcasts the Phone app cares about.
647 */
648 private class PhoneAppBroadcastReceiver extends BroadcastReceiver {
649 @Override
650 public void onReceive(Context context, Intent intent) {
651 String action = intent.getAction();
652 if (action.equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) {
653 boolean enabled = System.getInt(getContentResolver(),
654 System.AIRPLANE_MODE_ON, 0) == 0;
Stuart Scottdcf40a92014-12-09 10:45:01 -0800655 PhoneUtils.setRadioPower(enabled);
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);
659 int phoneId = SubscriptionManager.getPhoneId(subId);
660 String state = intent.getStringExtra(PhoneConstants.STATE_KEY);
661 if (VDBG) {
662 Log.d(LOG_TAG, "mReceiver: ACTION_ANY_DATA_CONNECTION_STATE_CHANGED");
663 Log.d(LOG_TAG, "- state: " + state);
664 Log.d(LOG_TAG, "- reason: "
665 + intent.getStringExtra(PhoneConstants.STATE_CHANGE_REASON_KEY));
666 Log.d(LOG_TAG, "- subId: " + subId);
667 Log.d(LOG_TAG, "- phoneId: " + phoneId);
668 }
669 Phone phone = SubscriptionManager.isValidPhoneId(phoneId) ?
670 PhoneFactory.getPhone(phoneId) : PhoneFactory.getDefaultPhone();
Santos Cordonc593d002015-06-03 15:41:15 -0700671
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700672 // The "data disconnected due to roaming" notification is shown
673 // if (a) you have the "data roaming" feature turned off, and
674 // (b) you just lost data connectivity because you're roaming.
675 boolean disconnectedDueToRoaming =
676 !phone.getDataRoamingEnabled()
Stuart Scottdcf40a92014-12-09 10:45:01 -0800677 && PhoneConstants.DataState.DISCONNECTED.equals(state)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700678 && Phone.REASON_ROAMING_ON.equals(
679 intent.getStringExtra(PhoneConstants.STATE_CHANGE_REASON_KEY));
Yorke Lee4d2db1c2014-11-06 11:37:09 -0800680 if (mDataDisconnectedDueToRoaming != disconnectedDueToRoaming) {
681 mDataDisconnectedDueToRoaming = disconnectedDueToRoaming;
682 mHandler.sendEmptyMessage(disconnectedDueToRoaming
683 ? EVENT_DATA_ROAMING_DISCONNECTED : EVENT_DATA_ROAMING_OK);
684 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700685 } else if ((action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) &&
686 (mPUKEntryActivity != null)) {
687 // if an attempt to un-PUK-lock the device was made, while we're
688 // receiving this state change notification, notify the handler.
689 // NOTE: This is ONLY triggered if an attempt to un-PUK-lock has
690 // been attempted.
691 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SIM_STATE_CHANGED,
692 intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE)));
693 } else if (action.equals(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED)) {
694 String newPhone = intent.getStringExtra(PhoneConstants.PHONE_NAME_KEY);
Stuart Scottdcf40a92014-12-09 10:45:01 -0800695 int phoneId = intent.getIntExtra(PhoneConstants.PHONE_KEY,
696 SubscriptionManager.INVALID_PHONE_INDEX);
697 Log.d(LOG_TAG, "Radio technology switched. Now " + newPhone + " (" + phoneId
698 + ") is active.");
699 initForNewRadioTechnology(phoneId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700700 } else if (action.equals(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED)) {
701 handleServiceStateChanged(intent);
702 } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) {
Stuart Scottdcf40a92014-12-09 10:45:01 -0800703 if (TelephonyCapabilities.supportsEcm(mCM.getFgPhone())) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700704 Log.d(LOG_TAG, "Emergency Callback Mode arrived in PhoneApp.");
705 // Start Emergency Callback Mode service
706 if (intent.getBooleanExtra("phoneinECMState", false)) {
707 context.startService(new Intent(context,
708 EmergencyCallbackModeService.class));
709 }
710 } else {
711 // It doesn't make sense to get ACTION_EMERGENCY_CALLBACK_MODE_CHANGED
712 // on a device that doesn't support ECM in the first place.
713 Log.e(LOG_TAG, "Got ACTION_EMERGENCY_CALLBACK_MODE_CHANGED, "
Stuart Scottdcf40a92014-12-09 10:45:01 -0800714 + "but ECM isn't supported for phone: "
715 + mCM.getFgPhone().getPhoneName());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700716 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700717 }
718 }
719 }
720
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700721 private void handleServiceStateChanged(Intent intent) {
722 /**
723 * This used to handle updating EriTextWidgetProvider this routine
724 * and and listening for ACTION_SERVICE_STATE_CHANGED intents could
725 * be removed. But leaving just in case it might be needed in the near
726 * future.
727 */
728
729 // If service just returned, start sending out the queued messages
Santos Cordonc593d002015-06-03 15:41:15 -0700730 Bundle extras = intent.getExtras();
731 if (extras != null) {
732 ServiceState ss = ServiceState.newFromBundle(extras);
733 if (ss != null) {
734 int state = ss.getState();
735 notificationMgr.updateNetworkSelection(state);
736 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700737 }
738 }
739
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700740 // it is safe to call clearOtaState() even if the InCallScreen isn't active
741 public void clearOtaState() {
742 if (DBG) Log.d(LOG_TAG, "- clearOtaState ...");
Jay Shrauner6fe8fd62013-09-16 19:39:30 -0700743 if (otaUtils != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700744 otaUtils.cleanOtaScreen(true);
745 if (DBG) Log.d(LOG_TAG, " - clearOtaState clears OTA screen");
746 }
747 }
748
749 // it is safe to call dismissOtaDialogs() even if the InCallScreen isn't active
750 public void dismissOtaDialogs() {
751 if (DBG) Log.d(LOG_TAG, "- dismissOtaDialogs ...");
Jay Shrauner6fe8fd62013-09-16 19:39:30 -0700752 if (otaUtils != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700753 otaUtils.dismissAllOtaDialogs();
754 if (DBG) Log.d(LOG_TAG, " - dismissOtaDialogs clears OTA dialogs");
755 }
756 }
757
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700758 /**
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800759 * Triggers a refresh of the message waiting (voicemail) indicator.
760 *
761 * @param subId the subscription id we should refresh the notification for.
762 */
763 public void refreshMwiIndicator(int subId) {
764 notificationMgr.refreshMwi(subId);
765 }
766
767 /**
Nancy Chenbb49d412015-07-23 13:54:16 -0700768 * Dismisses the message waiting (voicemail) indicator.
769 *
770 * @param subId the subscription id we should dismiss the notification for.
771 */
772 public void clearMwiIndicator(int subId) {
773 notificationMgr.updateMwi(subId, false);
774 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700775}