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