blob: e957924b8100dde239fe424419984fee1771a917 [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;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070037import android.os.Handler;
38import android.os.IBinder;
39import android.os.IPowerManager;
40import android.os.Message;
41import android.os.PowerManager;
42import android.os.RemoteException;
43import android.os.ServiceManager;
44import android.os.SystemClock;
45import android.os.SystemProperties;
46import android.os.UpdateLock;
47import android.os.UserHandle;
48import android.preference.PreferenceManager;
49import android.provider.Settings.System;
50import android.telephony.ServiceState;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import android.util.Log;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052
53import com.android.internal.telephony.Call;
54import com.android.internal.telephony.CallManager;
55import com.android.internal.telephony.IccCard;
56import com.android.internal.telephony.IccCardConstants;
57import com.android.internal.telephony.MmiCode;
58import com.android.internal.telephony.Phone;
59import com.android.internal.telephony.PhoneConstants;
60import com.android.internal.telephony.PhoneFactory;
61import com.android.internal.telephony.TelephonyCapabilities;
62import com.android.internal.telephony.TelephonyIntents;
Santos Cordon352ff652014-05-30 01:41:45 -070063import com.android.phone.common.CallLogAsync;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070064import com.android.server.sip.SipService;
65
66/**
67 * Global state for the telephony subsystem when running in the primary
68 * phone process.
69 */
Sailesh Nepalbf900542014-07-15 16:18:32 -070070public class PhoneGlobals extends ContextWrapper {
Andrew Lee83383e42014-10-31 12:42:28 -070071 public static final String LOG_TAG = "PhoneApp";
Santos Cordon7d4ddf62013-07-10 11:58:08 -070072
73 /**
74 * Phone app-wide debug level:
75 * 0 - no debug logging
76 * 1 - normal debug logging if ro.debuggable is set (which is true in
77 * "eng" and "userdebug" builds but not "user" builds)
78 * 2 - ultra-verbose debug logging
79 *
80 * Most individual classes in the phone app have a local DBG constant,
81 * typically set to
82 * (PhoneApp.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1)
83 * or else
84 * (PhoneApp.DBG_LEVEL >= 2)
85 * depending on the desired verbosity.
86 *
87 * ***** DO NOT SUBMIT WITH DBG_LEVEL > 0 *************
88 */
Andrew Lee88b51e22014-10-29 15:48:51 -070089 public static final int DBG_LEVEL = 0;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070090
91 private static final boolean DBG =
92 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
93 private static final boolean VDBG = (PhoneGlobals.DBG_LEVEL >= 2);
94
95 // Message codes; see mHandler below.
96 private static final int EVENT_SIM_NETWORK_LOCKED = 3;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070097 private static final int EVENT_SIM_STATE_CHANGED = 8;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070098 private static final int EVENT_DATA_ROAMING_DISCONNECTED = 10;
99 private static final int EVENT_DATA_ROAMING_OK = 11;
100 private static final int EVENT_UNSOL_CDMA_INFO_RECORD = 12;
101 private static final int EVENT_DOCK_STATE_CHANGED = 13;
Sailesh Nepalbf900542014-07-15 16:18:32 -0700102 private static final int EVENT_START_SIP_SERVICE = 14;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700103
104 // The MMI codes are also used by the InCallScreen.
105 public static final int MMI_INITIATE = 51;
106 public static final int MMI_COMPLETE = 52;
107 public static final int MMI_CANCEL = 53;
108 // Don't use message codes larger than 99 here; those are reserved for
109 // the individual Activities of the Phone UI.
110
111 /**
112 * Allowable values for the wake lock code.
113 * SLEEP means the device can be put to sleep.
114 * PARTIAL means wake the processor, but we display can be kept off.
115 * FULL means wake both the processor and the display.
116 */
117 public enum WakeState {
118 SLEEP,
119 PARTIAL,
120 FULL
121 }
122
123 /**
124 * Intent Action used for hanging up the current call from Notification bar. This will
125 * choose first ringing call, first active call, or first background call (typically in
126 * HOLDING state).
127 */
128 public static final String ACTION_HANG_UP_ONGOING_CALL =
129 "com.android.phone.ACTION_HANG_UP_ONGOING_CALL";
130
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700131 private static PhoneGlobals sMe;
132
133 // A few important fields we expose to the rest of the package
134 // directly (rather than thru set/get methods) for efficiency.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700135 CallController callController;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700136 CallManager mCM;
Santos Cordon63a84242013-07-23 13:32:52 -0700137 CallNotifier notifier;
138 CallerInfoCache callerInfoCache;
Santos Cordon63a84242013-07-23 13:32:52 -0700139 NotificationMgr notificationMgr;
140 Phone phone;
141 PhoneInterfaceManager phoneMgr;
142
Santos Cordon27a3c1f2013-08-06 07:49:27 -0700143 private BluetoothManager bluetoothManager;
Santos Cordon69a69192013-08-22 14:25:42 -0700144 private CallGatewayManager callGatewayManager;
Santos Cordon63a84242013-07-23 13:32:52 -0700145 private CallStateMonitor callStateMonitor;
Santos Cordon63a84242013-07-23 13:32:52 -0700146
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700147 static int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
148 static boolean sVoiceCapable = true;
149
150 // Internal PhoneApp Call state tracker
151 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
160 private boolean mIsSimPinEnabled;
161 private String mCachedSimPin;
162
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700163 // True if we are beginning a call, but the phone state has not changed yet
164 private boolean mBeginningCall;
Yorke Lee4d2db1c2014-11-06 11:37:09 -0800165 private boolean mDataDisconnectedDueToRoaming = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700166
167 // Last phone state seen by updatePhoneState()
168 private PhoneConstants.State mLastPhoneState = PhoneConstants.State.IDLE;
169
170 private WakeState mWakeState = WakeState.SLEEP;
171
172 private PowerManager mPowerManager;
173 private IPowerManager mPowerManagerService;
174 private PowerManager.WakeLock mWakeLock;
175 private PowerManager.WakeLock mPartialWakeLock;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700176 private KeyguardManager mKeyguardManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700177
178 private UpdateLock mUpdateLock;
179
180 // Broadcast receiver for various intent broadcasts (see onCreate())
181 private final BroadcastReceiver mReceiver = new PhoneAppBroadcastReceiver();
182
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700183 /** boolean indicating restoring mute state on InCallScreen.onResume() */
184 private boolean mShouldRestoreMuteOnInCallResume;
185
186 /**
187 * The singleton OtaUtils instance used for OTASP calls.
188 *
189 * The OtaUtils instance is created lazily the first time we need to
190 * make an OTASP call, regardless of whether it's an interactive or
191 * non-interactive OTASP call.
192 */
193 public OtaUtils otaUtils;
194
195 // Following are the CDMA OTA information Objects used during OTA Call.
196 // cdmaOtaProvisionData object store static OTA information that needs
197 // to be maintained even during Slider open/close scenarios.
198 // cdmaOtaConfigData object stores configuration info to control visiblity
199 // of each OTA Screens.
200 // cdmaOtaScreenState object store OTA Screen State information.
201 public OtaUtils.CdmaOtaProvisionData cdmaOtaProvisionData;
202 public OtaUtils.CdmaOtaConfigData cdmaOtaConfigData;
203 public OtaUtils.CdmaOtaScreenState cdmaOtaScreenState;
204 public OtaUtils.CdmaOtaInCallScreenUiState cdmaOtaInCallScreenUiState;
205
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700206 /**
207 * Set the restore mute state flag. Used when we are setting the mute state
208 * OUTSIDE of user interaction {@link PhoneUtils#startNewCall(Phone)}
209 */
210 /*package*/void setRestoreMuteOnInCallResume (boolean mode) {
211 mShouldRestoreMuteOnInCallResume = mode;
212 }
213
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700214 Handler mHandler = new Handler() {
215 @Override
216 public void handleMessage(Message msg) {
217 PhoneConstants.State phoneState;
218 switch (msg.what) {
219 // Starts the SIP service. It's a no-op if SIP API is not supported
220 // on the deivce.
221 // TODO: Having the phone process host the SIP service is only
222 // temporary. Will move it to a persistent communication process
223 // later.
224 case EVENT_START_SIP_SERVICE:
225 SipService.start(getApplicationContext());
226 break;
227
228 // TODO: This event should be handled by the lock screen, just
229 // like the "SIM missing" and "Sim locked" cases (bug 1804111).
230 case EVENT_SIM_NETWORK_LOCKED:
231 if (getResources().getBoolean(R.bool.ignore_sim_network_locked_events)) {
232 // Some products don't have the concept of a "SIM network lock"
233 Log.i(LOG_TAG, "Ignoring EVENT_SIM_NETWORK_LOCKED event; "
234 + "not showing 'SIM network unlock' PIN entry screen");
235 } else {
236 // Normal case: show the "SIM network unlock" PIN entry screen.
237 // The user won't be able to do anything else until
238 // they enter a valid SIM network PIN.
239 Log.i(LOG_TAG, "show sim depersonal panel");
240 IccNetworkDepersonalizationPanel ndpPanel =
241 new IccNetworkDepersonalizationPanel(PhoneGlobals.getInstance());
242 ndpPanel.show();
243 }
244 break;
245
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700246 case EVENT_DATA_ROAMING_DISCONNECTED:
247 notificationMgr.showDataDisconnectedRoaming();
248 break;
249
250 case EVENT_DATA_ROAMING_OK:
251 notificationMgr.hideDataDisconnectedRoaming();
252 break;
253
254 case MMI_COMPLETE:
255 onMMIComplete((AsyncResult) msg.obj);
256 break;
257
258 case MMI_CANCEL:
259 PhoneUtils.cancelMmiCode(phone);
260 break;
261
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700262 case EVENT_SIM_STATE_CHANGED:
263 // Marks the event where the SIM goes into ready state.
264 // Right now, this is only used for the PUK-unlocking
265 // process.
266 if (msg.obj.equals(IccCardConstants.INTENT_VALUE_ICC_READY)) {
267 // when the right event is triggered and there
268 // are UI objects in the foreground, we close
269 // them to display the lock panel.
270 if (mPUKEntryActivity != null) {
271 mPUKEntryActivity.finish();
272 mPUKEntryActivity = null;
273 }
274 if (mPUKEntryProgressDialog != null) {
275 mPUKEntryProgressDialog.dismiss();
276 mPUKEntryProgressDialog = null;
277 }
278 }
279 break;
280
281 case EVENT_UNSOL_CDMA_INFO_RECORD:
282 //TODO: handle message here;
283 break;
284
285 case EVENT_DOCK_STATE_CHANGED:
286 // If the phone is docked/undocked during a call, and no wired or BT headset
287 // is connected: turn on/off the speaker accordingly.
288 boolean inDockMode = false;
289 if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
290 inDockMode = true;
291 }
292 if (VDBG) Log.d(LOG_TAG, "received EVENT_DOCK_STATE_CHANGED. Phone inDock = "
293 + inDockMode);
294
295 phoneState = mCM.getState();
296 if (phoneState == PhoneConstants.State.OFFHOOK &&
Santos Cordon593ab382013-08-06 21:58:23 -0700297 !bluetoothManager.isBluetoothHeadsetAudioOn()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700298 PhoneUtils.turnOnSpeaker(getApplicationContext(), inDockMode, true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700299 }
300 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700301 }
302 }
303 };
304
305 public PhoneGlobals(Context context) {
306 super(context);
307 sMe = this;
308 }
309
310 public void onCreate() {
311 if (VDBG) Log.v(LOG_TAG, "onCreate()...");
312
313 ContentResolver resolver = getContentResolver();
314
315 // Cache the "voice capable" flag.
316 // This flag currently comes from a resource (which is
317 // overrideable on a per-product basis):
318 sVoiceCapable =
319 getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
320 // ...but this might eventually become a PackageManager "system
321 // feature" instead, in which case we'd do something like:
322 // sVoiceCapable =
323 // getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY_VOICE_CALLS);
324
325 if (phone == null) {
326 // Initialize the telephony framework
327 PhoneFactory.makeDefaultPhones(this);
328
329 // Get the default phone
330 phone = PhoneFactory.getDefaultPhone();
331
332 // Start TelephonyDebugService After the default phone is created.
333 Intent intent = new Intent(this, TelephonyDebugService.class);
334 startService(intent);
335
336 mCM = CallManager.getInstance();
337 mCM.registerPhone(phone);
338
339 // Create the NotificationMgr singleton, which is used to display
340 // status bar icons and control other status bar behavior.
341 notificationMgr = NotificationMgr.init(this);
342
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700343 mHandler.sendEmptyMessage(EVENT_START_SIP_SERVICE);
344
345 int phoneType = phone.getPhoneType();
346
347 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
348 // Create an instance of CdmaPhoneCallState and initialize it to IDLE
349 cdmaPhoneCallState = new CdmaPhoneCallState();
350 cdmaPhoneCallState.CdmaPhoneCallStateInit();
351 }
352
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700353 // before registering for phone state changes
354 mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
355 mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, LOG_TAG);
356 // lock used to keep the processor awake, when we don't care for the display.
357 mPartialWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK
358 | PowerManager.ON_AFTER_RELEASE, LOG_TAG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700359
360 mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
361
362 // get a handle to the service so that we can use it later when we
363 // want to set the poke lock.
364 mPowerManagerService = IPowerManager.Stub.asInterface(
365 ServiceManager.getService("power"));
366
367 // Get UpdateLock to suppress system-update related events (e.g. dialog show-up)
368 // during phone calls.
369 mUpdateLock = new UpdateLock("phone");
370
371 if (DBG) Log.d(LOG_TAG, "onCreate: mUpdateLock: " + mUpdateLock);
372
373 CallLogger callLogger = new CallLogger(this, new CallLogAsync());
374
Jay Shrauner21a75342013-11-25 16:14:43 -0800375 callGatewayManager = CallGatewayManager.getInstance();
Santos Cordon69a69192013-08-22 14:25:42 -0700376
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700377 // Create the CallController singleton, which is the interface
378 // to the telephony layer for user-initiated telephony functionality
379 // (like making outgoing calls.)
Santos Cordon69a69192013-08-22 14:25:42 -0700380 callController = CallController.init(this, callLogger, callGatewayManager);
381
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700382 // Create the CallerInfoCache singleton, which remembers custom ring tone and
383 // send-to-voicemail settings.
384 //
385 // The asynchronous caching will start just after this call.
386 callerInfoCache = CallerInfoCache.init(this);
387
388 // Monitors call activity from the telephony layer
389 callStateMonitor = new CallStateMonitor(mCM);
390
Santos Cordon2c2d3cf2013-08-08 03:53:47 -0700391 // Bluetooth manager
Sailesh Nepal23d9ed72014-07-03 09:40:26 -0700392 bluetoothManager = new BluetoothManager();
Santos Cordon2c2d3cf2013-08-08 03:53:47 -0700393
Sailesh Nepal194161e2014-07-03 08:57:44 -0700394 phoneMgr = PhoneInterfaceManager.init(this, phone);
Santos Cordon406c0342013-08-28 00:07:47 -0700395
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700396 // Create the CallNotifer singleton, which handles
397 // asynchronous events from the telephony layer (like
398 // launching the incoming-call UI when an incoming call comes
399 // in.)
Santos Cordon5422a8d2014-09-12 04:20:56 -0700400 notifier = CallNotifier.init(this, phone, callLogger, callStateMonitor,
Sailesh Nepal23d9ed72014-07-03 09:40:26 -0700401 bluetoothManager);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700402
403 // register for ICC status
404 IccCard sim = phone.getIccCard();
405 if (sim != null) {
406 if (VDBG) Log.v(LOG_TAG, "register for ICC status");
407 sim.registerForNetworkLocked(mHandler, EVENT_SIM_NETWORK_LOCKED, null);
408 }
409
410 // register for MMI/USSD
411 mCM.registerForMmiComplete(mHandler, MMI_COMPLETE, null);
412
413 // register connection tracking to PhoneUtils
414 PhoneUtils.initializeConnectionHandler(mCM);
415
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700416 // Register for misc other intent broadcasts.
417 IntentFilter intentFilter =
418 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700419 intentFilter.addAction(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700420 intentFilter.addAction(Intent.ACTION_DOCK_EVENT);
421 intentFilter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
422 intentFilter.addAction(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);
423 intentFilter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
424 intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700425 registerReceiver(mReceiver, intentFilter);
426
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700427 //set the default values for the preferences in the phone.
428 PreferenceManager.setDefaultValues(this, R.xml.network_setting, false);
429
430 PreferenceManager.setDefaultValues(this, R.xml.call_feature_setting, false);
431
432 // Make sure the audio mode (along with some
433 // audio-mode-related state of our own) is initialized
434 // correctly, given the current state of the phone.
435 PhoneUtils.setAudioMode(mCM);
436 }
437
Santos Cordon52bc81b2014-10-07 19:55:12 -0700438 cdmaOtaProvisionData = new OtaUtils.CdmaOtaProvisionData();
439 cdmaOtaConfigData = new OtaUtils.CdmaOtaConfigData();
440 cdmaOtaScreenState = new OtaUtils.CdmaOtaScreenState();
441 cdmaOtaInCallScreenUiState = new OtaUtils.CdmaOtaInCallScreenUiState();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700442
443 // XXX pre-load the SimProvider so that it's ready
444 resolver.getType(Uri.parse("content://icc/adn"));
445
446 // start with the default value to set the mute state.
447 mShouldRestoreMuteOnInCallResume = false;
448
449 // TODO: Register for Cdma Information Records
450 // phone.registerCdmaInformationRecord(mHandler, EVENT_UNSOL_CDMA_INFO_RECORD, null);
451
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700452 // Read HAC settings and configure audio hardware
453 if (getResources().getBoolean(R.bool.hac_enabled)) {
454 int hac = android.provider.Settings.System.getInt(phone.getContext().getContentResolver(),
455 android.provider.Settings.System.HEARING_AID,
456 0);
457 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
458 audioManager.setParameter(CallFeaturesSetting.HAC_KEY, hac != 0 ?
459 CallFeaturesSetting.HAC_VAL_ON :
460 CallFeaturesSetting.HAC_VAL_OFF);
461 }
Santos Cordonff506f52013-11-21 19:13:19 -0800462 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700463
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700464 /**
465 * Returns the singleton instance of the PhoneApp.
466 */
Sailesh Nepal1eaf22b2014-02-22 17:00:49 -0800467 public static PhoneGlobals getInstance() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700468 if (sMe == null) {
469 throw new IllegalStateException("No PhoneGlobals here!");
470 }
471 return sMe;
472 }
473
474 /**
475 * Returns the singleton instance of the PhoneApp if running as the
476 * primary user, otherwise null.
477 */
478 static PhoneGlobals getInstanceIfPrimary() {
479 return sMe;
480 }
481
482 /**
483 * Returns the Phone associated with this instance
484 */
Andrew Lee83383e42014-10-31 12:42:28 -0700485 public static Phone getPhone() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700486 return getInstance().phone;
487 }
488
Santos Cordon27a3c1f2013-08-06 07:49:27 -0700489 /* package */ BluetoothManager getBluetoothManager() {
490 return bluetoothManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700491 }
492
Santos Cordonde10b752013-09-19 04:11:33 -0700493 /* package */ CallManager getCallManager() {
494 return mCM;
495 }
496
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700497 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700498 * Returns PendingIntent for hanging up ongoing phone call. This will typically be used from
499 * Notification context.
500 */
501 /* package */ static PendingIntent createHangUpOngoingCallPendingIntent(Context context) {
502 Intent intent = new Intent(PhoneGlobals.ACTION_HANG_UP_ONGOING_CALL, null,
503 context, NotificationBroadcastReceiver.class);
504 return PendingIntent.getBroadcast(context, 0, intent, 0);
505 }
506
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700507 boolean isSimPinEnabled() {
508 return mIsSimPinEnabled;
509 }
510
511 boolean authenticateAgainstCachedSimPin(String pin) {
512 return (mCachedSimPin != null && mCachedSimPin.equals(pin));
513 }
514
515 void setCachedSimPin(String pin) {
516 mCachedSimPin = pin;
517 }
518
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700519 /**
520 * Handles OTASP-related events from the telephony layer.
521 *
522 * While an OTASP call is active, the CallNotifier forwards
523 * OTASP-related telephony events to this method.
524 */
525 void handleOtaspEvent(Message msg) {
526 if (DBG) Log.d(LOG_TAG, "handleOtaspEvent(message " + msg + ")...");
527
528 if (otaUtils == null) {
529 // We shouldn't be getting OTASP events without ever
530 // having started the OTASP call in the first place!
531 Log.w(LOG_TAG, "handleOtaEvents: got an event but otaUtils is null! "
532 + "message = " + msg);
533 return;
534 }
535
536 otaUtils.onOtaProvisionStatusChanged((AsyncResult) msg.obj);
537 }
538
539 /**
540 * Similarly, handle the disconnect event of an OTASP call
541 * by forwarding it to the OtaUtils instance.
542 */
543 /* package */ void handleOtaspDisconnect() {
544 if (DBG) Log.d(LOG_TAG, "handleOtaspDisconnect()...");
545
546 if (otaUtils == null) {
547 // We shouldn't be getting OTASP events without ever
548 // having started the OTASP call in the first place!
549 Log.w(LOG_TAG, "handleOtaspDisconnect: otaUtils is null!");
550 return;
551 }
552
553 otaUtils.onOtaspDisconnect();
554 }
555
556 /**
557 * Sets the activity responsible for un-PUK-blocking the device
558 * so that we may close it when we receive a positive result.
559 * mPUKEntryActivity is also used to indicate to the device that
560 * we are trying to un-PUK-lock the phone. In other words, iff
561 * it is NOT null, then we are trying to unlock and waiting for
562 * the SIM to move to READY state.
563 *
564 * @param activity is the activity to close when PUK has
565 * finished unlocking. Can be set to null to indicate the unlock
566 * or SIM READYing process is over.
567 */
568 void setPukEntryActivity(Activity activity) {
569 mPUKEntryActivity = activity;
570 }
571
572 Activity getPUKEntryActivity() {
573 return mPUKEntryActivity;
574 }
575
576 /**
577 * Sets the dialog responsible for notifying the user of un-PUK-
578 * blocking - SIM READYing progress, so that we may dismiss it
579 * when we receive a positive result.
580 *
581 * @param dialog indicates the progress dialog informing the user
582 * of the state of the device. Dismissed upon completion of
583 * READYing process
584 */
585 void setPukEntryProgressDialog(ProgressDialog dialog) {
586 mPUKEntryProgressDialog = dialog;
587 }
588
589 ProgressDialog getPUKEntryProgressDialog() {
590 return mPUKEntryProgressDialog;
591 }
592
593 /**
594 * Controls whether or not the screen is allowed to sleep.
595 *
596 * Once sleep is allowed (WakeState is SLEEP), it will rely on the
597 * settings for the poke lock to determine when to timeout and let
598 * the device sleep {@link PhoneGlobals#setScreenTimeout}.
599 *
600 * @param ws tells the device to how to wake.
601 */
602 /* package */ void requestWakeState(WakeState ws) {
603 if (VDBG) Log.d(LOG_TAG, "requestWakeState(" + ws + ")...");
604 synchronized (this) {
605 if (mWakeState != ws) {
606 switch (ws) {
607 case PARTIAL:
608 // acquire the processor wake lock, and release the FULL
609 // lock if it is being held.
610 mPartialWakeLock.acquire();
611 if (mWakeLock.isHeld()) {
612 mWakeLock.release();
613 }
614 break;
615 case FULL:
616 // acquire the full wake lock, and release the PARTIAL
617 // lock if it is being held.
618 mWakeLock.acquire();
619 if (mPartialWakeLock.isHeld()) {
620 mPartialWakeLock.release();
621 }
622 break;
623 case SLEEP:
624 default:
625 // release both the PARTIAL and FULL locks.
626 if (mWakeLock.isHeld()) {
627 mWakeLock.release();
628 }
629 if (mPartialWakeLock.isHeld()) {
630 mPartialWakeLock.release();
631 }
632 break;
633 }
634 mWakeState = ws;
635 }
636 }
637 }
638
639 /**
640 * If we are not currently keeping the screen on, then poke the power
641 * manager to wake up the screen for the user activity timeout duration.
642 */
643 /* package */ void wakeUpScreen() {
644 synchronized (this) {
645 if (mWakeState == WakeState.SLEEP) {
646 if (DBG) Log.d(LOG_TAG, "pulse screen lock");
647 mPowerManager.wakeUp(SystemClock.uptimeMillis());
648 }
649 }
650 }
651
652 /**
653 * Sets the wake state and screen timeout based on the current state
654 * of the phone, and the current state of the in-call UI.
655 *
656 * This method is a "UI Policy" wrapper around
657 * {@link PhoneGlobals#requestWakeState} and {@link PhoneGlobals#setScreenTimeout}.
658 *
659 * It's safe to call this method regardless of the state of the Phone
660 * (e.g. whether or not it's idle), and regardless of the state of the
661 * Phone UI (e.g. whether or not the InCallScreen is active.)
662 */
663 /* package */ void updateWakeState() {
664 PhoneConstants.State state = mCM.getState();
665
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700666 // True if the speakerphone is in use. (If so, we *always* use
667 // the default timeout. Since the user is obviously not holding
668 // the phone up to his/her face, we don't need to worry about
669 // false touches, and thus don't need to turn the screen off so
670 // aggressively.)
671 // Note that we need to make a fresh call to this method any
672 // time the speaker state changes. (That happens in
673 // PhoneUtils.turnOnSpeaker().)
674 boolean isSpeakerInUse = (state == PhoneConstants.State.OFFHOOK) && PhoneUtils.isSpeakerOn(this);
675
676 // TODO (bug 1440854): The screen timeout *might* also need to
677 // depend on the bluetooth state, but this isn't as clear-cut as
678 // the speaker state (since while using BT it's common for the
679 // user to put the phone straight into a pocket, in which case the
680 // timeout should probably still be short.)
681
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700682 // Decide whether to force the screen on or not.
683 //
684 // Force the screen to be on if the phone is ringing or dialing,
685 // or if we're displaying the "Call ended" UI for a connection in
686 // the "disconnected" state.
687 // However, if the phone is disconnected while the user is in the
688 // middle of selecting a quick response message, we should not force
689 // the screen to be on.
690 //
691 boolean isRinging = (state == PhoneConstants.State.RINGING);
692 boolean isDialing = (phone.getForegroundCall().getState() == Call.State.DIALING);
Jay Shrauner6fe8fd62013-09-16 19:39:30 -0700693 boolean keepScreenOn = isRinging || isDialing;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700694 // keepScreenOn == true means we'll hold a full wake lock:
695 requestWakeState(keepScreenOn ? WakeState.FULL : WakeState.SLEEP);
696 }
697
698 /**
699 * Manually pokes the PowerManager's userActivity method. Since we
700 * set the {@link WindowManager.LayoutParams#INPUT_FEATURE_DISABLE_USER_ACTIVITY}
701 * flag while the InCallScreen is active when there is no proximity sensor,
702 * we need to do this for touch events that really do count as user activity
703 * (like pressing any onscreen UI elements.)
704 */
705 /* package */ void pokeUserActivity() {
706 if (VDBG) Log.d(LOG_TAG, "pokeUserActivity()...");
707 mPowerManager.userActivity(SystemClock.uptimeMillis(), false);
708 }
709
710 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700711 * Notifies the phone app when the phone state changes.
712 *
Santos Cordonfc309812013-08-20 18:33:16 -0700713 * This method will updates various states inside Phone app (e.g. update-lock state, etc.)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700714 */
715 /* package */ void updatePhoneState(PhoneConstants.State state) {
716 if (state != mLastPhoneState) {
717 mLastPhoneState = state;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700718
719 // Try to acquire or release UpdateLock.
720 //
721 // Watch out: we don't release the lock here when the screen is still in foreground.
722 // At that time InCallScreen will release it on onPause().
723 if (state != PhoneConstants.State.IDLE) {
724 // UpdateLock is a recursive lock, while we may get "acquire" request twice and
725 // "release" request once for a single call (RINGING + OFFHOOK and IDLE).
726 // We need to manually ensure the lock is just acquired once for each (and this
727 // will prevent other possible buggy situations too).
728 if (!mUpdateLock.isHeld()) {
729 mUpdateLock.acquire();
730 }
731 } else {
Jay Shraunera5d13212013-09-19 13:37:43 -0700732 if (mUpdateLock.isHeld()) {
Jay Shrauner6fe8fd62013-09-16 19:39:30 -0700733 mUpdateLock.release();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700734 }
735 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700736 }
737 }
738
739 /* package */ PhoneConstants.State getPhoneState() {
740 return mLastPhoneState;
741 }
742
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700743 KeyguardManager getKeyguardManager() {
744 return mKeyguardManager;
745 }
746
747 private void onMMIComplete(AsyncResult r) {
748 if (VDBG) Log.d(LOG_TAG, "onMMIComplete()...");
749 MmiCode mmiCode = (MmiCode) r.result;
750 PhoneUtils.displayMMIComplete(phone, getInstance(), mmiCode, null, null);
751 }
752
753 private void initForNewRadioTechnology() {
754 if (DBG) Log.d(LOG_TAG, "initForNewRadioTechnology...");
755
756 if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
757 // Create an instance of CdmaPhoneCallState and initialize it to IDLE
758 cdmaPhoneCallState = new CdmaPhoneCallState();
759 cdmaPhoneCallState.CdmaPhoneCallStateInit();
760 }
Santos Cordon52bc81b2014-10-07 19:55:12 -0700761 if (!TelephonyCapabilities.supportsOtasp(phone)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700762 //Clean up OTA data in GSM/UMTS. It is valid only for CDMA
763 clearOtaState();
764 }
765
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700766 notifier.updateCallNotifierRegistrationsAfterRadioTechnologyChange();
767 callStateMonitor.updateAfterRadioTechnologyChange();
768
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700769 // Update registration for ICC status after radio technology change
770 IccCard sim = phone.getIccCard();
771 if (sim != null) {
772 if (DBG) Log.d(LOG_TAG, "Update registration for ICC status...");
773
774 //Register all events new to the new active phone
775 sim.registerForNetworkLocked(mHandler, EVENT_SIM_NETWORK_LOCKED, null);
776 }
777 }
778
Santos Cordon593ab382013-08-06 21:58:23 -0700779 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700780 * Receiver for misc intent broadcasts the Phone app cares about.
781 */
782 private class PhoneAppBroadcastReceiver extends BroadcastReceiver {
783 @Override
784 public void onReceive(Context context, Intent intent) {
785 String action = intent.getAction();
786 if (action.equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) {
787 boolean enabled = System.getInt(getContentResolver(),
788 System.AIRPLANE_MODE_ON, 0) == 0;
789 phone.setRadioPower(enabled);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700790 } else if (action.equals(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED)) {
791 if (VDBG) Log.d(LOG_TAG, "mReceiver: ACTION_ANY_DATA_CONNECTION_STATE_CHANGED");
792 if (VDBG) Log.d(LOG_TAG, "- state: " + intent.getStringExtra(PhoneConstants.STATE_KEY));
793 if (VDBG) Log.d(LOG_TAG, "- reason: "
794 + intent.getStringExtra(PhoneConstants.STATE_CHANGE_REASON_KEY));
795
796 // The "data disconnected due to roaming" notification is shown
797 // if (a) you have the "data roaming" feature turned off, and
798 // (b) you just lost data connectivity because you're roaming.
799 boolean disconnectedDueToRoaming =
800 !phone.getDataRoamingEnabled()
801 && "DISCONNECTED".equals(intent.getStringExtra(PhoneConstants.STATE_KEY))
802 && Phone.REASON_ROAMING_ON.equals(
803 intent.getStringExtra(PhoneConstants.STATE_CHANGE_REASON_KEY));
Yorke Lee4d2db1c2014-11-06 11:37:09 -0800804 if (mDataDisconnectedDueToRoaming != disconnectedDueToRoaming) {
805 mDataDisconnectedDueToRoaming = disconnectedDueToRoaming;
806 mHandler.sendEmptyMessage(disconnectedDueToRoaming
807 ? EVENT_DATA_ROAMING_DISCONNECTED : EVENT_DATA_ROAMING_OK);
808 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700809 } else if ((action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) &&
810 (mPUKEntryActivity != null)) {
811 // if an attempt to un-PUK-lock the device was made, while we're
812 // receiving this state change notification, notify the handler.
813 // NOTE: This is ONLY triggered if an attempt to un-PUK-lock has
814 // been attempted.
815 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SIM_STATE_CHANGED,
816 intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE)));
817 } else if (action.equals(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED)) {
818 String newPhone = intent.getStringExtra(PhoneConstants.PHONE_NAME_KEY);
819 Log.d(LOG_TAG, "Radio technology switched. Now " + newPhone + " is active.");
820 initForNewRadioTechnology();
821 } else if (action.equals(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED)) {
822 handleServiceStateChanged(intent);
823 } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) {
824 if (TelephonyCapabilities.supportsEcm(phone)) {
825 Log.d(LOG_TAG, "Emergency Callback Mode arrived in PhoneApp.");
826 // Start Emergency Callback Mode service
827 if (intent.getBooleanExtra("phoneinECMState", false)) {
828 context.startService(new Intent(context,
829 EmergencyCallbackModeService.class));
830 }
831 } else {
832 // It doesn't make sense to get ACTION_EMERGENCY_CALLBACK_MODE_CHANGED
833 // on a device that doesn't support ECM in the first place.
834 Log.e(LOG_TAG, "Got ACTION_EMERGENCY_CALLBACK_MODE_CHANGED, "
835 + "but ECM isn't supported for phone: " + phone.getPhoneName());
836 }
837 } else if (action.equals(Intent.ACTION_DOCK_EVENT)) {
838 mDockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
839 Intent.EXTRA_DOCK_STATE_UNDOCKED);
840 if (VDBG) Log.d(LOG_TAG, "ACTION_DOCK_EVENT -> mDockState = " + mDockState);
841 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DOCK_STATE_CHANGED, 0));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700842 }
843 }
844 }
845
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700846 /**
847 * Accepts broadcast Intents which will be prepared by {@link NotificationMgr} and thus
848 * sent from framework's notification mechanism (which is outside Phone context).
849 * This should be visible from outside, but shouldn't be in "exported" state.
850 *
851 * TODO: If possible merge this into PhoneAppBroadcastReceiver.
852 */
853 public static class NotificationBroadcastReceiver extends BroadcastReceiver {
854 @Override
855 public void onReceive(Context context, Intent intent) {
856 String action = intent.getAction();
857 // TODO: use "if (VDBG)" here.
858 Log.d(LOG_TAG, "Broadcast from Notification: " + action);
859
860 if (action.equals(ACTION_HANG_UP_ONGOING_CALL)) {
861 PhoneUtils.hangup(PhoneGlobals.getInstance().mCM);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700862 } else {
863 Log.w(LOG_TAG, "Received hang-up request from notification,"
864 + " but there's no call the system can hang up.");
865 }
866 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700867 }
868
869 private void handleServiceStateChanged(Intent intent) {
870 /**
871 * This used to handle updating EriTextWidgetProvider this routine
872 * and and listening for ACTION_SERVICE_STATE_CHANGED intents could
873 * be removed. But leaving just in case it might be needed in the near
874 * future.
875 */
876
877 // If service just returned, start sending out the queued messages
878 ServiceState ss = ServiceState.newFromBundle(intent.getExtras());
879
880 if (ss != null) {
881 int state = ss.getState();
882 notificationMgr.updateNetworkSelection(state);
883 }
884 }
885
886 public boolean isOtaCallInActiveState() {
887 boolean otaCallActive = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700888 if (VDBG) Log.d(LOG_TAG, "- isOtaCallInActiveState " + otaCallActive);
889 return otaCallActive;
890 }
891
892 public boolean isOtaCallInEndState() {
893 boolean otaCallEnded = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700894 if (VDBG) Log.d(LOG_TAG, "- isOtaCallInEndState " + otaCallEnded);
895 return otaCallEnded;
896 }
897
898 // it is safe to call clearOtaState() even if the InCallScreen isn't active
899 public void clearOtaState() {
900 if (DBG) Log.d(LOG_TAG, "- clearOtaState ...");
Jay Shrauner6fe8fd62013-09-16 19:39:30 -0700901 if (otaUtils != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700902 otaUtils.cleanOtaScreen(true);
903 if (DBG) Log.d(LOG_TAG, " - clearOtaState clears OTA screen");
904 }
905 }
906
907 // it is safe to call dismissOtaDialogs() even if the InCallScreen isn't active
908 public void dismissOtaDialogs() {
909 if (DBG) Log.d(LOG_TAG, "- dismissOtaDialogs ...");
Jay Shrauner6fe8fd62013-09-16 19:39:30 -0700910 if (otaUtils != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700911 otaUtils.dismissAllOtaDialogs();
912 if (DBG) Log.d(LOG_TAG, " - dismissOtaDialogs clears OTA dialogs");
913 }
914 }
915
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700916 /**
917 * "Call origin" may be used by Contacts app to specify where the phone call comes from.
918 * Currently, the only permitted value for this extra is {@link #ALLOWED_EXTRA_CALL_ORIGIN}.
919 * Any other value will be ignored, to make sure that malicious apps can't trick the in-call
920 * UI into launching some random other app after a call ends.
921 *
922 * TODO: make this more generic. Note that we should let the "origin" specify its package
923 * while we are now assuming it is "com.android.contacts"
924 */
925 public static final String EXTRA_CALL_ORIGIN = "com.android.phone.CALL_ORIGIN";
926 private static final String DEFAULT_CALL_ORIGIN_PACKAGE = "com.android.dialer";
927 private static final String ALLOWED_EXTRA_CALL_ORIGIN =
928 "com.android.dialer.DialtactsActivity";
929 /**
930 * Used to determine if the preserved call origin is fresh enough.
931 */
932 private static final long CALL_ORIGIN_EXPIRATION_MILLIS = 30 * 1000;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700933}