blob: f66ebce7151d2ced2c60c6235d14e32a6cff4e2b [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 {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071 /* package */ static final String LOG_TAG = "PhoneApp";
72
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 */
89 /* package */ static final int DBG_LEVEL = 0;
90
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;
146 private IBluetoothHeadsetPhone mBluetoothPhone;
147 private Ringer ringer;
148
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700149 static int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
150 static boolean sVoiceCapable = true;
151
152 // Internal PhoneApp Call state tracker
153 CdmaPhoneCallState cdmaPhoneCallState;
154
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700155 // The currently-active PUK entry activity and progress dialog.
156 // Normally, these are the Emergency Dialer and the subsequent
157 // progress dialog. null if there is are no such objects in
158 // the foreground.
159 private Activity mPUKEntryActivity;
160 private ProgressDialog mPUKEntryProgressDialog;
161
162 private boolean mIsSimPinEnabled;
163 private String mCachedSimPin;
164
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700165 // True if we are beginning a call, but the phone state has not changed yet
166 private boolean mBeginningCall;
167
168 // Last phone state seen by updatePhoneState()
169 private PhoneConstants.State mLastPhoneState = PhoneConstants.State.IDLE;
170
171 private WakeState mWakeState = WakeState.SLEEP;
172
173 private PowerManager mPowerManager;
174 private IPowerManager mPowerManagerService;
175 private PowerManager.WakeLock mWakeLock;
176 private PowerManager.WakeLock mPartialWakeLock;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700177 private KeyguardManager mKeyguardManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700178
179 private UpdateLock mUpdateLock;
180
181 // Broadcast receiver for various intent broadcasts (see onCreate())
182 private final BroadcastReceiver mReceiver = new PhoneAppBroadcastReceiver();
183
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700184 /** boolean indicating restoring mute state on InCallScreen.onResume() */
185 private boolean mShouldRestoreMuteOnInCallResume;
186
187 /**
188 * The singleton OtaUtils instance used for OTASP calls.
189 *
190 * The OtaUtils instance is created lazily the first time we need to
191 * make an OTASP call, regardless of whether it's an interactive or
192 * non-interactive OTASP call.
193 */
194 public OtaUtils otaUtils;
195
196 // Following are the CDMA OTA information Objects used during OTA Call.
197 // cdmaOtaProvisionData object store static OTA information that needs
198 // to be maintained even during Slider open/close scenarios.
199 // cdmaOtaConfigData object stores configuration info to control visiblity
200 // of each OTA Screens.
201 // cdmaOtaScreenState object store OTA Screen State information.
202 public OtaUtils.CdmaOtaProvisionData cdmaOtaProvisionData;
203 public OtaUtils.CdmaOtaConfigData cdmaOtaConfigData;
204 public OtaUtils.CdmaOtaScreenState cdmaOtaScreenState;
205 public OtaUtils.CdmaOtaInCallScreenUiState cdmaOtaInCallScreenUiState;
206
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700207 /**
208 * Set the restore mute state flag. Used when we are setting the mute state
209 * OUTSIDE of user interaction {@link PhoneUtils#startNewCall(Phone)}
210 */
211 /*package*/void setRestoreMuteOnInCallResume (boolean mode) {
212 mShouldRestoreMuteOnInCallResume = mode;
213 }
214
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700215 Handler mHandler = new Handler() {
216 @Override
217 public void handleMessage(Message msg) {
218 PhoneConstants.State phoneState;
219 switch (msg.what) {
220 // Starts the SIP service. It's a no-op if SIP API is not supported
221 // on the deivce.
222 // TODO: Having the phone process host the SIP service is only
223 // temporary. Will move it to a persistent communication process
224 // later.
225 case EVENT_START_SIP_SERVICE:
226 SipService.start(getApplicationContext());
227 break;
228
229 // TODO: This event should be handled by the lock screen, just
230 // like the "SIM missing" and "Sim locked" cases (bug 1804111).
231 case EVENT_SIM_NETWORK_LOCKED:
232 if (getResources().getBoolean(R.bool.ignore_sim_network_locked_events)) {
233 // Some products don't have the concept of a "SIM network lock"
234 Log.i(LOG_TAG, "Ignoring EVENT_SIM_NETWORK_LOCKED event; "
235 + "not showing 'SIM network unlock' PIN entry screen");
236 } else {
237 // Normal case: show the "SIM network unlock" PIN entry screen.
238 // The user won't be able to do anything else until
239 // they enter a valid SIM network PIN.
240 Log.i(LOG_TAG, "show sim depersonal panel");
241 IccNetworkDepersonalizationPanel ndpPanel =
242 new IccNetworkDepersonalizationPanel(PhoneGlobals.getInstance());
243 ndpPanel.show();
244 }
245 break;
246
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700247 case EVENT_DATA_ROAMING_DISCONNECTED:
248 notificationMgr.showDataDisconnectedRoaming();
249 break;
250
251 case EVENT_DATA_ROAMING_OK:
252 notificationMgr.hideDataDisconnectedRoaming();
253 break;
254
255 case MMI_COMPLETE:
256 onMMIComplete((AsyncResult) msg.obj);
257 break;
258
259 case MMI_CANCEL:
260 PhoneUtils.cancelMmiCode(phone);
261 break;
262
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700263 case EVENT_SIM_STATE_CHANGED:
264 // Marks the event where the SIM goes into ready state.
265 // Right now, this is only used for the PUK-unlocking
266 // process.
267 if (msg.obj.equals(IccCardConstants.INTENT_VALUE_ICC_READY)) {
268 // when the right event is triggered and there
269 // are UI objects in the foreground, we close
270 // them to display the lock panel.
271 if (mPUKEntryActivity != null) {
272 mPUKEntryActivity.finish();
273 mPUKEntryActivity = null;
274 }
275 if (mPUKEntryProgressDialog != null) {
276 mPUKEntryProgressDialog.dismiss();
277 mPUKEntryProgressDialog = null;
278 }
279 }
280 break;
281
282 case EVENT_UNSOL_CDMA_INFO_RECORD:
283 //TODO: handle message here;
284 break;
285
286 case EVENT_DOCK_STATE_CHANGED:
287 // If the phone is docked/undocked during a call, and no wired or BT headset
288 // is connected: turn on/off the speaker accordingly.
289 boolean inDockMode = false;
290 if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
291 inDockMode = true;
292 }
293 if (VDBG) Log.d(LOG_TAG, "received EVENT_DOCK_STATE_CHANGED. Phone inDock = "
294 + inDockMode);
295
296 phoneState = mCM.getState();
297 if (phoneState == PhoneConstants.State.OFFHOOK &&
Santos Cordon593ab382013-08-06 21:58:23 -0700298 !bluetoothManager.isBluetoothHeadsetAudioOn()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700299 PhoneUtils.turnOnSpeaker(getApplicationContext(), inDockMode, true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700300 }
301 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700302 }
303 }
304 };
305
306 public PhoneGlobals(Context context) {
307 super(context);
308 sMe = this;
309 }
310
311 public void onCreate() {
312 if (VDBG) Log.v(LOG_TAG, "onCreate()...");
313
314 ContentResolver resolver = getContentResolver();
315
316 // Cache the "voice capable" flag.
317 // This flag currently comes from a resource (which is
318 // overrideable on a per-product basis):
319 sVoiceCapable =
320 getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
321 // ...but this might eventually become a PackageManager "system
322 // feature" instead, in which case we'd do something like:
323 // sVoiceCapable =
324 // getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY_VOICE_CALLS);
325
326 if (phone == null) {
327 // Initialize the telephony framework
328 PhoneFactory.makeDefaultPhones(this);
329
330 // Get the default phone
331 phone = PhoneFactory.getDefaultPhone();
332
333 // Start TelephonyDebugService After the default phone is created.
334 Intent intent = new Intent(this, TelephonyDebugService.class);
335 startService(intent);
336
337 mCM = CallManager.getInstance();
338 mCM.registerPhone(phone);
339
340 // Create the NotificationMgr singleton, which is used to display
341 // status bar icons and control other status bar behavior.
342 notificationMgr = NotificationMgr.init(this);
343
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700344 mHandler.sendEmptyMessage(EVENT_START_SIP_SERVICE);
345
346 int phoneType = phone.getPhoneType();
347
348 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
349 // Create an instance of CdmaPhoneCallState and initialize it to IDLE
350 cdmaPhoneCallState = new CdmaPhoneCallState();
351 cdmaPhoneCallState.CdmaPhoneCallStateInit();
352 }
353
354 if (BluetoothAdapter.getDefaultAdapter() != null) {
355 // Start BluetoothPhoneService even if device is not voice capable.
356 // The device can still support VOIP.
357 startService(new Intent(this, BluetoothPhoneService.class));
358 bindService(new Intent(this, BluetoothPhoneService.class),
359 mBluetoothPhoneConnection, 0);
360 } else {
361 // Device is not bluetooth capable
362 mBluetoothPhone = null;
363 }
364
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700365 // before registering for phone state changes
366 mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
367 mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, LOG_TAG);
368 // lock used to keep the processor awake, when we don't care for the display.
369 mPartialWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK
370 | PowerManager.ON_AFTER_RELEASE, LOG_TAG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700371
372 mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
373
374 // get a handle to the service so that we can use it later when we
375 // want to set the poke lock.
376 mPowerManagerService = IPowerManager.Stub.asInterface(
377 ServiceManager.getService("power"));
378
379 // Get UpdateLock to suppress system-update related events (e.g. dialog show-up)
380 // during phone calls.
381 mUpdateLock = new UpdateLock("phone");
382
383 if (DBG) Log.d(LOG_TAG, "onCreate: mUpdateLock: " + mUpdateLock);
384
385 CallLogger callLogger = new CallLogger(this, new CallLogAsync());
386
Jay Shrauner21a75342013-11-25 16:14:43 -0800387 callGatewayManager = CallGatewayManager.getInstance();
Santos Cordon69a69192013-08-22 14:25:42 -0700388
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700389 // Create the CallController singleton, which is the interface
390 // to the telephony layer for user-initiated telephony functionality
391 // (like making outgoing calls.)
Santos Cordon69a69192013-08-22 14:25:42 -0700392 callController = CallController.init(this, callLogger, callGatewayManager);
393
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700394 // Create the CallerInfoCache singleton, which remembers custom ring tone and
395 // send-to-voicemail settings.
396 //
397 // The asynchronous caching will start just after this call.
398 callerInfoCache = CallerInfoCache.init(this);
399
400 // Monitors call activity from the telephony layer
401 callStateMonitor = new CallStateMonitor(mCM);
402
Santos Cordon2c2d3cf2013-08-08 03:53:47 -0700403 // Bluetooth manager
Sailesh Nepal23d9ed72014-07-03 09:40:26 -0700404 bluetoothManager = new BluetoothManager();
Santos Cordon2c2d3cf2013-08-08 03:53:47 -0700405
406 ringer = Ringer.init(this, bluetoothManager);
407
Sailesh Nepal194161e2014-07-03 08:57:44 -0700408 phoneMgr = PhoneInterfaceManager.init(this, phone);
Santos Cordon406c0342013-08-28 00:07:47 -0700409
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700410 // Create the CallNotifer singleton, which handles
411 // asynchronous events from the telephony layer (like
412 // launching the incoming-call UI when an incoming call comes
413 // in.)
Santos Cordon27a3c1f2013-08-06 07:49:27 -0700414 notifier = CallNotifier.init(this, phone, ringer, callLogger, callStateMonitor,
Sailesh Nepal23d9ed72014-07-03 09:40:26 -0700415 bluetoothManager);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700416
417 // register for ICC status
418 IccCard sim = phone.getIccCard();
419 if (sim != null) {
420 if (VDBG) Log.v(LOG_TAG, "register for ICC status");
421 sim.registerForNetworkLocked(mHandler, EVENT_SIM_NETWORK_LOCKED, null);
422 }
423
424 // register for MMI/USSD
425 mCM.registerForMmiComplete(mHandler, MMI_COMPLETE, null);
426
427 // register connection tracking to PhoneUtils
428 PhoneUtils.initializeConnectionHandler(mCM);
429
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700430 // Register for misc other intent broadcasts.
431 IntentFilter intentFilter =
432 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700433 intentFilter.addAction(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700434 intentFilter.addAction(Intent.ACTION_DOCK_EVENT);
435 intentFilter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
436 intentFilter.addAction(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);
437 intentFilter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
438 intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700439 intentFilter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
440 registerReceiver(mReceiver, intentFilter);
441
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700442 //set the default values for the preferences in the phone.
443 PreferenceManager.setDefaultValues(this, R.xml.network_setting, false);
444
445 PreferenceManager.setDefaultValues(this, R.xml.call_feature_setting, false);
446
447 // Make sure the audio mode (along with some
448 // audio-mode-related state of our own) is initialized
449 // correctly, given the current state of the phone.
450 PhoneUtils.setAudioMode(mCM);
451 }
452
453 if (TelephonyCapabilities.supportsOtasp(phone)) {
454 cdmaOtaProvisionData = new OtaUtils.CdmaOtaProvisionData();
455 cdmaOtaConfigData = new OtaUtils.CdmaOtaConfigData();
456 cdmaOtaScreenState = new OtaUtils.CdmaOtaScreenState();
457 cdmaOtaInCallScreenUiState = new OtaUtils.CdmaOtaInCallScreenUiState();
458 }
459
460 // XXX pre-load the SimProvider so that it's ready
461 resolver.getType(Uri.parse("content://icc/adn"));
462
463 // start with the default value to set the mute state.
464 mShouldRestoreMuteOnInCallResume = false;
465
466 // TODO: Register for Cdma Information Records
467 // phone.registerCdmaInformationRecord(mHandler, EVENT_UNSOL_CDMA_INFO_RECORD, null);
468
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700469 // Read HAC settings and configure audio hardware
470 if (getResources().getBoolean(R.bool.hac_enabled)) {
471 int hac = android.provider.Settings.System.getInt(phone.getContext().getContentResolver(),
472 android.provider.Settings.System.HEARING_AID,
473 0);
474 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
475 audioManager.setParameter(CallFeaturesSetting.HAC_KEY, hac != 0 ?
476 CallFeaturesSetting.HAC_VAL_ON :
477 CallFeaturesSetting.HAC_VAL_OFF);
478 }
Santos Cordonff506f52013-11-21 19:13:19 -0800479 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700480
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700481 /**
482 * Returns the singleton instance of the PhoneApp.
483 */
Sailesh Nepal1eaf22b2014-02-22 17:00:49 -0800484 public static PhoneGlobals getInstance() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700485 if (sMe == null) {
486 throw new IllegalStateException("No PhoneGlobals here!");
487 }
488 return sMe;
489 }
490
491 /**
492 * Returns the singleton instance of the PhoneApp if running as the
493 * primary user, otherwise null.
494 */
495 static PhoneGlobals getInstanceIfPrimary() {
496 return sMe;
497 }
498
499 /**
500 * Returns the Phone associated with this instance
501 */
502 static Phone getPhone() {
503 return getInstance().phone;
504 }
505
506 Ringer getRinger() {
507 return ringer;
508 }
509
510 IBluetoothHeadsetPhone getBluetoothPhoneService() {
511 return mBluetoothPhone;
512 }
513
Santos Cordon27a3c1f2013-08-06 07:49:27 -0700514 /* package */ BluetoothManager getBluetoothManager() {
515 return bluetoothManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700516 }
517
Santos Cordonde10b752013-09-19 04:11:33 -0700518 /* package */ CallManager getCallManager() {
519 return mCM;
520 }
521
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700522 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700523 * Returns PendingIntent for hanging up ongoing phone call. This will typically be used from
524 * Notification context.
525 */
526 /* package */ static PendingIntent createHangUpOngoingCallPendingIntent(Context context) {
527 Intent intent = new Intent(PhoneGlobals.ACTION_HANG_UP_ONGOING_CALL, null,
528 context, NotificationBroadcastReceiver.class);
529 return PendingIntent.getBroadcast(context, 0, intent, 0);
530 }
531
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700532 boolean isSimPinEnabled() {
533 return mIsSimPinEnabled;
534 }
535
536 boolean authenticateAgainstCachedSimPin(String pin) {
537 return (mCachedSimPin != null && mCachedSimPin.equals(pin));
538 }
539
540 void setCachedSimPin(String pin) {
541 mCachedSimPin = pin;
542 }
543
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700544 /**
545 * Handles OTASP-related events from the telephony layer.
546 *
547 * While an OTASP call is active, the CallNotifier forwards
548 * OTASP-related telephony events to this method.
549 */
550 void handleOtaspEvent(Message msg) {
551 if (DBG) Log.d(LOG_TAG, "handleOtaspEvent(message " + msg + ")...");
552
553 if (otaUtils == null) {
554 // We shouldn't be getting OTASP events without ever
555 // having started the OTASP call in the first place!
556 Log.w(LOG_TAG, "handleOtaEvents: got an event but otaUtils is null! "
557 + "message = " + msg);
558 return;
559 }
560
561 otaUtils.onOtaProvisionStatusChanged((AsyncResult) msg.obj);
562 }
563
564 /**
565 * Similarly, handle the disconnect event of an OTASP call
566 * by forwarding it to the OtaUtils instance.
567 */
568 /* package */ void handleOtaspDisconnect() {
569 if (DBG) Log.d(LOG_TAG, "handleOtaspDisconnect()...");
570
571 if (otaUtils == null) {
572 // We shouldn't be getting OTASP events without ever
573 // having started the OTASP call in the first place!
574 Log.w(LOG_TAG, "handleOtaspDisconnect: otaUtils is null!");
575 return;
576 }
577
578 otaUtils.onOtaspDisconnect();
579 }
580
581 /**
582 * Sets the activity responsible for un-PUK-blocking the device
583 * so that we may close it when we receive a positive result.
584 * mPUKEntryActivity is also used to indicate to the device that
585 * we are trying to un-PUK-lock the phone. In other words, iff
586 * it is NOT null, then we are trying to unlock and waiting for
587 * the SIM to move to READY state.
588 *
589 * @param activity is the activity to close when PUK has
590 * finished unlocking. Can be set to null to indicate the unlock
591 * or SIM READYing process is over.
592 */
593 void setPukEntryActivity(Activity activity) {
594 mPUKEntryActivity = activity;
595 }
596
597 Activity getPUKEntryActivity() {
598 return mPUKEntryActivity;
599 }
600
601 /**
602 * Sets the dialog responsible for notifying the user of un-PUK-
603 * blocking - SIM READYing progress, so that we may dismiss it
604 * when we receive a positive result.
605 *
606 * @param dialog indicates the progress dialog informing the user
607 * of the state of the device. Dismissed upon completion of
608 * READYing process
609 */
610 void setPukEntryProgressDialog(ProgressDialog dialog) {
611 mPUKEntryProgressDialog = dialog;
612 }
613
614 ProgressDialog getPUKEntryProgressDialog() {
615 return mPUKEntryProgressDialog;
616 }
617
618 /**
619 * Controls whether or not the screen is allowed to sleep.
620 *
621 * Once sleep is allowed (WakeState is SLEEP), it will rely on the
622 * settings for the poke lock to determine when to timeout and let
623 * the device sleep {@link PhoneGlobals#setScreenTimeout}.
624 *
625 * @param ws tells the device to how to wake.
626 */
627 /* package */ void requestWakeState(WakeState ws) {
628 if (VDBG) Log.d(LOG_TAG, "requestWakeState(" + ws + ")...");
629 synchronized (this) {
630 if (mWakeState != ws) {
631 switch (ws) {
632 case PARTIAL:
633 // acquire the processor wake lock, and release the FULL
634 // lock if it is being held.
635 mPartialWakeLock.acquire();
636 if (mWakeLock.isHeld()) {
637 mWakeLock.release();
638 }
639 break;
640 case FULL:
641 // acquire the full wake lock, and release the PARTIAL
642 // lock if it is being held.
643 mWakeLock.acquire();
644 if (mPartialWakeLock.isHeld()) {
645 mPartialWakeLock.release();
646 }
647 break;
648 case SLEEP:
649 default:
650 // release both the PARTIAL and FULL locks.
651 if (mWakeLock.isHeld()) {
652 mWakeLock.release();
653 }
654 if (mPartialWakeLock.isHeld()) {
655 mPartialWakeLock.release();
656 }
657 break;
658 }
659 mWakeState = ws;
660 }
661 }
662 }
663
664 /**
665 * If we are not currently keeping the screen on, then poke the power
666 * manager to wake up the screen for the user activity timeout duration.
667 */
668 /* package */ void wakeUpScreen() {
669 synchronized (this) {
670 if (mWakeState == WakeState.SLEEP) {
671 if (DBG) Log.d(LOG_TAG, "pulse screen lock");
672 mPowerManager.wakeUp(SystemClock.uptimeMillis());
673 }
674 }
675 }
676
677 /**
678 * Sets the wake state and screen timeout based on the current state
679 * of the phone, and the current state of the in-call UI.
680 *
681 * This method is a "UI Policy" wrapper around
682 * {@link PhoneGlobals#requestWakeState} and {@link PhoneGlobals#setScreenTimeout}.
683 *
684 * It's safe to call this method regardless of the state of the Phone
685 * (e.g. whether or not it's idle), and regardless of the state of the
686 * Phone UI (e.g. whether or not the InCallScreen is active.)
687 */
688 /* package */ void updateWakeState() {
689 PhoneConstants.State state = mCM.getState();
690
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700691 // True if the speakerphone is in use. (If so, we *always* use
692 // the default timeout. Since the user is obviously not holding
693 // the phone up to his/her face, we don't need to worry about
694 // false touches, and thus don't need to turn the screen off so
695 // aggressively.)
696 // Note that we need to make a fresh call to this method any
697 // time the speaker state changes. (That happens in
698 // PhoneUtils.turnOnSpeaker().)
699 boolean isSpeakerInUse = (state == PhoneConstants.State.OFFHOOK) && PhoneUtils.isSpeakerOn(this);
700
701 // TODO (bug 1440854): The screen timeout *might* also need to
702 // depend on the bluetooth state, but this isn't as clear-cut as
703 // the speaker state (since while using BT it's common for the
704 // user to put the phone straight into a pocket, in which case the
705 // timeout should probably still be short.)
706
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700707 // Decide whether to force the screen on or not.
708 //
709 // Force the screen to be on if the phone is ringing or dialing,
710 // or if we're displaying the "Call ended" UI for a connection in
711 // the "disconnected" state.
712 // However, if the phone is disconnected while the user is in the
713 // middle of selecting a quick response message, we should not force
714 // the screen to be on.
715 //
716 boolean isRinging = (state == PhoneConstants.State.RINGING);
717 boolean isDialing = (phone.getForegroundCall().getState() == Call.State.DIALING);
Jay Shrauner6fe8fd62013-09-16 19:39:30 -0700718 boolean keepScreenOn = isRinging || isDialing;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700719 // keepScreenOn == true means we'll hold a full wake lock:
720 requestWakeState(keepScreenOn ? WakeState.FULL : WakeState.SLEEP);
721 }
722
723 /**
724 * Manually pokes the PowerManager's userActivity method. Since we
725 * set the {@link WindowManager.LayoutParams#INPUT_FEATURE_DISABLE_USER_ACTIVITY}
726 * flag while the InCallScreen is active when there is no proximity sensor,
727 * we need to do this for touch events that really do count as user activity
728 * (like pressing any onscreen UI elements.)
729 */
730 /* package */ void pokeUserActivity() {
731 if (VDBG) Log.d(LOG_TAG, "pokeUserActivity()...");
732 mPowerManager.userActivity(SystemClock.uptimeMillis(), false);
733 }
734
735 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700736 * Notifies the phone app when the phone state changes.
737 *
Santos Cordonfc309812013-08-20 18:33:16 -0700738 * This method will updates various states inside Phone app (e.g. update-lock state, etc.)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700739 */
740 /* package */ void updatePhoneState(PhoneConstants.State state) {
741 if (state != mLastPhoneState) {
742 mLastPhoneState = state;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700743
744 // Try to acquire or release UpdateLock.
745 //
746 // Watch out: we don't release the lock here when the screen is still in foreground.
747 // At that time InCallScreen will release it on onPause().
748 if (state != PhoneConstants.State.IDLE) {
749 // UpdateLock is a recursive lock, while we may get "acquire" request twice and
750 // "release" request once for a single call (RINGING + OFFHOOK and IDLE).
751 // We need to manually ensure the lock is just acquired once for each (and this
752 // will prevent other possible buggy situations too).
753 if (!mUpdateLock.isHeld()) {
754 mUpdateLock.acquire();
755 }
756 } else {
Jay Shraunera5d13212013-09-19 13:37:43 -0700757 if (mUpdateLock.isHeld()) {
Jay Shrauner6fe8fd62013-09-16 19:39:30 -0700758 mUpdateLock.release();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700759 }
760 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700761 }
762 }
763
764 /* package */ PhoneConstants.State getPhoneState() {
765 return mLastPhoneState;
766 }
767
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700768 KeyguardManager getKeyguardManager() {
769 return mKeyguardManager;
770 }
771
772 private void onMMIComplete(AsyncResult r) {
773 if (VDBG) Log.d(LOG_TAG, "onMMIComplete()...");
774 MmiCode mmiCode = (MmiCode) r.result;
775 PhoneUtils.displayMMIComplete(phone, getInstance(), mmiCode, null, null);
776 }
777
778 private void initForNewRadioTechnology() {
779 if (DBG) Log.d(LOG_TAG, "initForNewRadioTechnology...");
780
781 if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
782 // Create an instance of CdmaPhoneCallState and initialize it to IDLE
783 cdmaPhoneCallState = new CdmaPhoneCallState();
784 cdmaPhoneCallState.CdmaPhoneCallStateInit();
785 }
786 if (TelephonyCapabilities.supportsOtasp(phone)) {
787 //create instances of CDMA OTA data classes
788 if (cdmaOtaProvisionData == null) {
789 cdmaOtaProvisionData = new OtaUtils.CdmaOtaProvisionData();
790 }
791 if (cdmaOtaConfigData == null) {
792 cdmaOtaConfigData = new OtaUtils.CdmaOtaConfigData();
793 }
794 if (cdmaOtaScreenState == null) {
795 cdmaOtaScreenState = new OtaUtils.CdmaOtaScreenState();
796 }
797 if (cdmaOtaInCallScreenUiState == null) {
798 cdmaOtaInCallScreenUiState = new OtaUtils.CdmaOtaInCallScreenUiState();
799 }
800 } else {
801 //Clean up OTA data in GSM/UMTS. It is valid only for CDMA
802 clearOtaState();
803 }
804
805 ringer.updateRingerContextAfterRadioTechnologyChange(this.phone);
806 notifier.updateCallNotifierRegistrationsAfterRadioTechnologyChange();
807 callStateMonitor.updateAfterRadioTechnologyChange();
808
809 if (mBluetoothPhone != null) {
810 try {
811 mBluetoothPhone.updateBtHandsfreeAfterRadioTechnologyChange();
812 } catch (RemoteException e) {
813 Log.e(LOG_TAG, Log.getStackTraceString(new Throwable()));
814 }
815 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700816
817 // Update registration for ICC status after radio technology change
818 IccCard sim = phone.getIccCard();
819 if (sim != null) {
820 if (DBG) Log.d(LOG_TAG, "Update registration for ICC status...");
821
822 //Register all events new to the new active phone
823 sim.registerForNetworkLocked(mHandler, EVENT_SIM_NETWORK_LOCKED, null);
824 }
825 }
826
Santos Cordon593ab382013-08-06 21:58:23 -0700827 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700828 * Receiver for misc intent broadcasts the Phone app cares about.
829 */
830 private class PhoneAppBroadcastReceiver extends BroadcastReceiver {
831 @Override
832 public void onReceive(Context context, Intent intent) {
833 String action = intent.getAction();
834 if (action.equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) {
835 boolean enabled = System.getInt(getContentResolver(),
836 System.AIRPLANE_MODE_ON, 0) == 0;
837 phone.setRadioPower(enabled);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700838 } else if (action.equals(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED)) {
839 if (VDBG) Log.d(LOG_TAG, "mReceiver: ACTION_ANY_DATA_CONNECTION_STATE_CHANGED");
840 if (VDBG) Log.d(LOG_TAG, "- state: " + intent.getStringExtra(PhoneConstants.STATE_KEY));
841 if (VDBG) Log.d(LOG_TAG, "- reason: "
842 + intent.getStringExtra(PhoneConstants.STATE_CHANGE_REASON_KEY));
843
844 // The "data disconnected due to roaming" notification is shown
845 // if (a) you have the "data roaming" feature turned off, and
846 // (b) you just lost data connectivity because you're roaming.
847 boolean disconnectedDueToRoaming =
848 !phone.getDataRoamingEnabled()
849 && "DISCONNECTED".equals(intent.getStringExtra(PhoneConstants.STATE_KEY))
850 && Phone.REASON_ROAMING_ON.equals(
851 intent.getStringExtra(PhoneConstants.STATE_CHANGE_REASON_KEY));
852 mHandler.sendEmptyMessage(disconnectedDueToRoaming
853 ? EVENT_DATA_ROAMING_DISCONNECTED
854 : EVENT_DATA_ROAMING_OK);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700855 } else if ((action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) &&
856 (mPUKEntryActivity != null)) {
857 // if an attempt to un-PUK-lock the device was made, while we're
858 // receiving this state change notification, notify the handler.
859 // NOTE: This is ONLY triggered if an attempt to un-PUK-lock has
860 // been attempted.
861 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SIM_STATE_CHANGED,
862 intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE)));
863 } else if (action.equals(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED)) {
864 String newPhone = intent.getStringExtra(PhoneConstants.PHONE_NAME_KEY);
865 Log.d(LOG_TAG, "Radio technology switched. Now " + newPhone + " is active.");
866 initForNewRadioTechnology();
867 } else if (action.equals(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED)) {
868 handleServiceStateChanged(intent);
869 } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) {
870 if (TelephonyCapabilities.supportsEcm(phone)) {
871 Log.d(LOG_TAG, "Emergency Callback Mode arrived in PhoneApp.");
872 // Start Emergency Callback Mode service
873 if (intent.getBooleanExtra("phoneinECMState", false)) {
874 context.startService(new Intent(context,
875 EmergencyCallbackModeService.class));
876 }
877 } else {
878 // It doesn't make sense to get ACTION_EMERGENCY_CALLBACK_MODE_CHANGED
879 // on a device that doesn't support ECM in the first place.
880 Log.e(LOG_TAG, "Got ACTION_EMERGENCY_CALLBACK_MODE_CHANGED, "
881 + "but ECM isn't supported for phone: " + phone.getPhoneName());
882 }
883 } else if (action.equals(Intent.ACTION_DOCK_EVENT)) {
884 mDockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
885 Intent.EXTRA_DOCK_STATE_UNDOCKED);
886 if (VDBG) Log.d(LOG_TAG, "ACTION_DOCK_EVENT -> mDockState = " + mDockState);
887 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DOCK_STATE_CHANGED, 0));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700888 } else if (action.equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
889 int ringerMode = intent.getIntExtra(AudioManager.EXTRA_RINGER_MODE,
890 AudioManager.RINGER_MODE_NORMAL);
891 if (ringerMode == AudioManager.RINGER_MODE_SILENT) {
892 notifier.silenceRinger();
893 }
894 }
895 }
896 }
897
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700898 /**
899 * Accepts broadcast Intents which will be prepared by {@link NotificationMgr} and thus
900 * sent from framework's notification mechanism (which is outside Phone context).
901 * This should be visible from outside, but shouldn't be in "exported" state.
902 *
903 * TODO: If possible merge this into PhoneAppBroadcastReceiver.
904 */
905 public static class NotificationBroadcastReceiver extends BroadcastReceiver {
906 @Override
907 public void onReceive(Context context, Intent intent) {
908 String action = intent.getAction();
909 // TODO: use "if (VDBG)" here.
910 Log.d(LOG_TAG, "Broadcast from Notification: " + action);
911
912 if (action.equals(ACTION_HANG_UP_ONGOING_CALL)) {
913 PhoneUtils.hangup(PhoneGlobals.getInstance().mCM);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700914 } else {
915 Log.w(LOG_TAG, "Received hang-up request from notification,"
916 + " but there's no call the system can hang up.");
917 }
918 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700919 }
920
921 private void handleServiceStateChanged(Intent intent) {
922 /**
923 * This used to handle updating EriTextWidgetProvider this routine
924 * and and listening for ACTION_SERVICE_STATE_CHANGED intents could
925 * be removed. But leaving just in case it might be needed in the near
926 * future.
927 */
928
929 // If service just returned, start sending out the queued messages
930 ServiceState ss = ServiceState.newFromBundle(intent.getExtras());
931
932 if (ss != null) {
933 int state = ss.getState();
934 notificationMgr.updateNetworkSelection(state);
935 }
936 }
937
938 public boolean isOtaCallInActiveState() {
939 boolean otaCallActive = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700940 if (VDBG) Log.d(LOG_TAG, "- isOtaCallInActiveState " + otaCallActive);
941 return otaCallActive;
942 }
943
944 public boolean isOtaCallInEndState() {
945 boolean otaCallEnded = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700946 if (VDBG) Log.d(LOG_TAG, "- isOtaCallInEndState " + otaCallEnded);
947 return otaCallEnded;
948 }
949
950 // it is safe to call clearOtaState() even if the InCallScreen isn't active
951 public void clearOtaState() {
952 if (DBG) Log.d(LOG_TAG, "- clearOtaState ...");
Jay Shrauner6fe8fd62013-09-16 19:39:30 -0700953 if (otaUtils != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700954 otaUtils.cleanOtaScreen(true);
955 if (DBG) Log.d(LOG_TAG, " - clearOtaState clears OTA screen");
956 }
957 }
958
959 // it is safe to call dismissOtaDialogs() even if the InCallScreen isn't active
960 public void dismissOtaDialogs() {
961 if (DBG) Log.d(LOG_TAG, "- dismissOtaDialogs ...");
Jay Shrauner6fe8fd62013-09-16 19:39:30 -0700962 if (otaUtils != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700963 otaUtils.dismissAllOtaDialogs();
964 if (DBG) Log.d(LOG_TAG, " - dismissOtaDialogs clears OTA dialogs");
965 }
966 }
967
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700968 /**
969 * "Call origin" may be used by Contacts app to specify where the phone call comes from.
970 * Currently, the only permitted value for this extra is {@link #ALLOWED_EXTRA_CALL_ORIGIN}.
971 * Any other value will be ignored, to make sure that malicious apps can't trick the in-call
972 * UI into launching some random other app after a call ends.
973 *
974 * TODO: make this more generic. Note that we should let the "origin" specify its package
975 * while we are now assuming it is "com.android.contacts"
976 */
977 public static final String EXTRA_CALL_ORIGIN = "com.android.phone.CALL_ORIGIN";
978 private static final String DEFAULT_CALL_ORIGIN_PACKAGE = "com.android.dialer";
979 private static final String ALLOWED_EXTRA_CALL_ORIGIN =
980 "com.android.dialer.DialtactsActivity";
981 /**
982 * Used to determine if the preserved call origin is fresh enough.
983 */
984 private static final long CALL_ORIGIN_EXPIRATION_MILLIS = 30 * 1000;
985
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700986 /** Service connection */
987 private final ServiceConnection mBluetoothPhoneConnection = new ServiceConnection() {
988
989 /** Handle the task of binding the local object to the service */
990 public void onServiceConnected(ComponentName className, IBinder service) {
991 Log.i(LOG_TAG, "Headset phone created, binding local service.");
992 mBluetoothPhone = IBluetoothHeadsetPhone.Stub.asInterface(service);
993 }
994
995 /** Handle the task of cleaning up the local binding */
996 public void onServiceDisconnected(ComponentName className) {
997 Log.i(LOG_TAG, "Headset phone disconnected, cleaning local binding.");
998 mBluetoothPhone = null;
999 }
1000 };
Santos Cordon83570472013-09-06 15:45:10 -07001001
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001002}