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