blob: 06b896969719f5b705984c319fb4f35ad5b9d34d [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;
26import android.content.ActivityNotFoundException;
27import android.content.BroadcastReceiver;
28import android.content.ComponentName;
29import android.content.ContentResolver;
30import android.content.Context;
31import android.content.ContextWrapper;
32import android.content.Intent;
33import android.content.IntentFilter;
34import android.content.ServiceConnection;
35import android.content.res.Configuration;
36import android.media.AudioManager;
37import android.net.Uri;
38import android.os.AsyncResult;
39import android.os.Binder;
40import android.os.Handler;
41import android.os.IBinder;
42import android.os.IPowerManager;
43import android.os.Message;
44import android.os.PowerManager;
45import android.os.RemoteException;
46import android.os.ServiceManager;
47import android.os.SystemClock;
48import android.os.SystemProperties;
49import android.os.UpdateLock;
50import android.os.UserHandle;
51import android.preference.PreferenceManager;
52import android.provider.Settings.System;
53import android.telephony.ServiceState;
54import android.text.TextUtils;
55import android.util.Log;
56import android.util.Slog;
57import android.view.KeyEvent;
58
59import com.android.internal.telephony.Call;
60import com.android.internal.telephony.CallManager;
61import com.android.internal.telephony.IccCard;
62import com.android.internal.telephony.IccCardConstants;
63import com.android.internal.telephony.MmiCode;
64import com.android.internal.telephony.Phone;
65import com.android.internal.telephony.PhoneConstants;
66import com.android.internal.telephony.PhoneFactory;
67import com.android.internal.telephony.TelephonyCapabilities;
68import com.android.internal.telephony.TelephonyIntents;
69import com.android.internal.telephony.cdma.TtyIntent;
70import com.android.phone.common.CallLogAsync;
71import com.android.phone.OtaUtils.CdmaOtaScreenState;
Santos Cordon593ab382013-08-06 21:58:23 -070072import com.android.phone.WiredHeadsetManager.WiredHeadsetListener;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070073import com.android.server.sip.SipService;
Santos Cordon593ab382013-08-06 21:58:23 -070074import com.android.services.telephony.common.AudioMode;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070075
76/**
77 * Global state for the telephony subsystem when running in the primary
78 * phone process.
79 */
Santos Cordonfc309812013-08-20 18:33:16 -070080public class PhoneGlobals extends ContextWrapper implements WiredHeadsetListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070081 /* package */ static final String LOG_TAG = "PhoneApp";
82
83 /**
84 * Phone app-wide debug level:
85 * 0 - no debug logging
86 * 1 - normal debug logging if ro.debuggable is set (which is true in
87 * "eng" and "userdebug" builds but not "user" builds)
88 * 2 - ultra-verbose debug logging
89 *
90 * Most individual classes in the phone app have a local DBG constant,
91 * typically set to
92 * (PhoneApp.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1)
93 * or else
94 * (PhoneApp.DBG_LEVEL >= 2)
95 * depending on the desired verbosity.
96 *
97 * ***** DO NOT SUBMIT WITH DBG_LEVEL > 0 *************
98 */
99 /* package */ static final int DBG_LEVEL = 0;
100
101 private static final boolean DBG =
102 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
103 private static final boolean VDBG = (PhoneGlobals.DBG_LEVEL >= 2);
104
105 // Message codes; see mHandler below.
106 private static final int EVENT_SIM_NETWORK_LOCKED = 3;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700107 private static final int EVENT_SIM_STATE_CHANGED = 8;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700108 private static final int EVENT_DATA_ROAMING_DISCONNECTED = 10;
109 private static final int EVENT_DATA_ROAMING_OK = 11;
110 private static final int EVENT_UNSOL_CDMA_INFO_RECORD = 12;
111 private static final int EVENT_DOCK_STATE_CHANGED = 13;
112 private static final int EVENT_TTY_PREFERRED_MODE_CHANGED = 14;
113 private static final int EVENT_TTY_MODE_GET = 15;
114 private static final int EVENT_TTY_MODE_SET = 16;
115 private static final int EVENT_START_SIP_SERVICE = 17;
116
117 // The MMI codes are also used by the InCallScreen.
118 public static final int MMI_INITIATE = 51;
119 public static final int MMI_COMPLETE = 52;
120 public static final int MMI_CANCEL = 53;
121 // Don't use message codes larger than 99 here; those are reserved for
122 // the individual Activities of the Phone UI.
123
124 /**
125 * Allowable values for the wake lock code.
126 * SLEEP means the device can be put to sleep.
127 * PARTIAL means wake the processor, but we display can be kept off.
128 * FULL means wake both the processor and the display.
129 */
130 public enum WakeState {
131 SLEEP,
132 PARTIAL,
133 FULL
134 }
135
136 /**
137 * Intent Action used for hanging up the current call from Notification bar. This will
138 * choose first ringing call, first active call, or first background call (typically in
139 * HOLDING state).
140 */
141 public static final String ACTION_HANG_UP_ONGOING_CALL =
142 "com.android.phone.ACTION_HANG_UP_ONGOING_CALL";
143
144 /**
145 * Intent Action used for making a phone call from Notification bar.
146 * This is for missed call notifications.
147 */
148 public static final String ACTION_CALL_BACK_FROM_NOTIFICATION =
149 "com.android.phone.ACTION_CALL_BACK_FROM_NOTIFICATION";
150
151 /**
152 * Intent Action used for sending a SMS from notification bar.
153 * This is for missed call notifications.
154 */
155 public static final String ACTION_SEND_SMS_FROM_NOTIFICATION =
156 "com.android.phone.ACTION_SEND_SMS_FROM_NOTIFICATION";
157
158 private static PhoneGlobals sMe;
159
160 // A few important fields we expose to the rest of the package
161 // directly (rather than thru set/get methods) for efficiency.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700162 CallController callController;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700163 CallManager mCM;
Santos Cordon63a84242013-07-23 13:32:52 -0700164 CallNotifier notifier;
165 CallerInfoCache callerInfoCache;
Santos Cordon63a84242013-07-23 13:32:52 -0700166 NotificationMgr notificationMgr;
167 Phone phone;
168 PhoneInterfaceManager phoneMgr;
169
Santos Cordon9b7bac72013-08-06 08:04:52 -0700170 private AudioRouter audioRouter;
Santos Cordon27a3c1f2013-08-06 07:49:27 -0700171 private BluetoothManager bluetoothManager;
Santos Cordon63a84242013-07-23 13:32:52 -0700172 private CallCommandService callCommandService;
Santos Cordon69a69192013-08-22 14:25:42 -0700173 private CallGatewayManager callGatewayManager;
Santos Cordon63a84242013-07-23 13:32:52 -0700174 private CallHandlerServiceProxy callHandlerServiceProxy;
175 private CallModeler callModeler;
176 private CallStateMonitor callStateMonitor;
Santos Cordon2eaff902013-08-05 04:37:55 -0700177 private DTMFTonePlayer dtmfTonePlayer;
Christine Chenee09a492013-08-06 16:02:29 -0700178 private RejectWithTextMessageManager rejectWithTextMessageManager;
Santos Cordon63a84242013-07-23 13:32:52 -0700179 private IBluetoothHeadsetPhone mBluetoothPhone;
180 private Ringer ringer;
Santos Cordon593ab382013-08-06 21:58:23 -0700181 private WiredHeadsetManager wiredHeadsetManager;
Santos Cordon63a84242013-07-23 13:32:52 -0700182
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700183 static int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
184 static boolean sVoiceCapable = true;
185
186 // Internal PhoneApp Call state tracker
187 CdmaPhoneCallState cdmaPhoneCallState;
188
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700189 // The currently-active PUK entry activity and progress dialog.
190 // Normally, these are the Emergency Dialer and the subsequent
191 // progress dialog. null if there is are no such objects in
192 // the foreground.
193 private Activity mPUKEntryActivity;
194 private ProgressDialog mPUKEntryProgressDialog;
195
196 private boolean mIsSimPinEnabled;
197 private String mCachedSimPin;
198
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700199 // True if we are beginning a call, but the phone state has not changed yet
200 private boolean mBeginningCall;
201
202 // Last phone state seen by updatePhoneState()
203 private PhoneConstants.State mLastPhoneState = PhoneConstants.State.IDLE;
204
205 private WakeState mWakeState = WakeState.SLEEP;
206
207 private PowerManager mPowerManager;
208 private IPowerManager mPowerManagerService;
209 private PowerManager.WakeLock mWakeLock;
210 private PowerManager.WakeLock mPartialWakeLock;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700211 private KeyguardManager mKeyguardManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700212
213 private UpdateLock mUpdateLock;
214
215 // Broadcast receiver for various intent broadcasts (see onCreate())
216 private final BroadcastReceiver mReceiver = new PhoneAppBroadcastReceiver();
217
218 // Broadcast receiver purely for ACTION_MEDIA_BUTTON broadcasts
219 private final BroadcastReceiver mMediaButtonReceiver = new MediaButtonBroadcastReceiver();
220
221 /** boolean indicating restoring mute state on InCallScreen.onResume() */
222 private boolean mShouldRestoreMuteOnInCallResume;
223
224 /**
225 * The singleton OtaUtils instance used for OTASP calls.
226 *
227 * The OtaUtils instance is created lazily the first time we need to
228 * make an OTASP call, regardless of whether it's an interactive or
229 * non-interactive OTASP call.
230 */
231 public OtaUtils otaUtils;
232
233 // Following are the CDMA OTA information Objects used during OTA Call.
234 // cdmaOtaProvisionData object store static OTA information that needs
235 // to be maintained even during Slider open/close scenarios.
236 // cdmaOtaConfigData object stores configuration info to control visiblity
237 // of each OTA Screens.
238 // cdmaOtaScreenState object store OTA Screen State information.
239 public OtaUtils.CdmaOtaProvisionData cdmaOtaProvisionData;
240 public OtaUtils.CdmaOtaConfigData cdmaOtaConfigData;
241 public OtaUtils.CdmaOtaScreenState cdmaOtaScreenState;
242 public OtaUtils.CdmaOtaInCallScreenUiState cdmaOtaInCallScreenUiState;
243
244 // TTY feature enabled on this platform
245 private boolean mTtyEnabled;
246 // Current TTY operating mode selected by user
247 private int mPreferredTtyMode = Phone.TTY_MODE_OFF;
248
249 /**
250 * Set the restore mute state flag. Used when we are setting the mute state
251 * OUTSIDE of user interaction {@link PhoneUtils#startNewCall(Phone)}
252 */
253 /*package*/void setRestoreMuteOnInCallResume (boolean mode) {
254 mShouldRestoreMuteOnInCallResume = mode;
255 }
256
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700257 Handler mHandler = new Handler() {
258 @Override
259 public void handleMessage(Message msg) {
260 PhoneConstants.State phoneState;
261 switch (msg.what) {
262 // Starts the SIP service. It's a no-op if SIP API is not supported
263 // on the deivce.
264 // TODO: Having the phone process host the SIP service is only
265 // temporary. Will move it to a persistent communication process
266 // later.
267 case EVENT_START_SIP_SERVICE:
268 SipService.start(getApplicationContext());
269 break;
270
271 // TODO: This event should be handled by the lock screen, just
272 // like the "SIM missing" and "Sim locked" cases (bug 1804111).
273 case EVENT_SIM_NETWORK_LOCKED:
274 if (getResources().getBoolean(R.bool.ignore_sim_network_locked_events)) {
275 // Some products don't have the concept of a "SIM network lock"
276 Log.i(LOG_TAG, "Ignoring EVENT_SIM_NETWORK_LOCKED event; "
277 + "not showing 'SIM network unlock' PIN entry screen");
278 } else {
279 // Normal case: show the "SIM network unlock" PIN entry screen.
280 // The user won't be able to do anything else until
281 // they enter a valid SIM network PIN.
282 Log.i(LOG_TAG, "show sim depersonal panel");
283 IccNetworkDepersonalizationPanel ndpPanel =
284 new IccNetworkDepersonalizationPanel(PhoneGlobals.getInstance());
285 ndpPanel.show();
286 }
287 break;
288
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700289 case EVENT_DATA_ROAMING_DISCONNECTED:
290 notificationMgr.showDataDisconnectedRoaming();
291 break;
292
293 case EVENT_DATA_ROAMING_OK:
294 notificationMgr.hideDataDisconnectedRoaming();
295 break;
296
297 case MMI_COMPLETE:
298 onMMIComplete((AsyncResult) msg.obj);
299 break;
300
301 case MMI_CANCEL:
302 PhoneUtils.cancelMmiCode(phone);
303 break;
304
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700305 case EVENT_SIM_STATE_CHANGED:
306 // Marks the event where the SIM goes into ready state.
307 // Right now, this is only used for the PUK-unlocking
308 // process.
309 if (msg.obj.equals(IccCardConstants.INTENT_VALUE_ICC_READY)) {
310 // when the right event is triggered and there
311 // are UI objects in the foreground, we close
312 // them to display the lock panel.
313 if (mPUKEntryActivity != null) {
314 mPUKEntryActivity.finish();
315 mPUKEntryActivity = null;
316 }
317 if (mPUKEntryProgressDialog != null) {
318 mPUKEntryProgressDialog.dismiss();
319 mPUKEntryProgressDialog = null;
320 }
321 }
322 break;
323
324 case EVENT_UNSOL_CDMA_INFO_RECORD:
325 //TODO: handle message here;
326 break;
327
328 case EVENT_DOCK_STATE_CHANGED:
329 // If the phone is docked/undocked during a call, and no wired or BT headset
330 // is connected: turn on/off the speaker accordingly.
331 boolean inDockMode = false;
332 if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
333 inDockMode = true;
334 }
335 if (VDBG) Log.d(LOG_TAG, "received EVENT_DOCK_STATE_CHANGED. Phone inDock = "
336 + inDockMode);
337
338 phoneState = mCM.getState();
339 if (phoneState == PhoneConstants.State.OFFHOOK &&
Santos Cordon593ab382013-08-06 21:58:23 -0700340 !wiredHeadsetManager.isHeadsetPlugged() &&
341 !bluetoothManager.isBluetoothHeadsetAudioOn()) {
342 audioRouter.setSpeaker(inDockMode);
343
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700344 PhoneUtils.turnOnSpeaker(getApplicationContext(), inDockMode, true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700345 }
346 break;
347
348 case EVENT_TTY_PREFERRED_MODE_CHANGED:
349 // TTY mode is only applied if a headset is connected
350 int ttyMode;
Santos Cordon593ab382013-08-06 21:58:23 -0700351 if (wiredHeadsetManager.isHeadsetPlugged()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700352 ttyMode = mPreferredTtyMode;
353 } else {
354 ttyMode = Phone.TTY_MODE_OFF;
355 }
356 phone.setTTYMode(ttyMode, mHandler.obtainMessage(EVENT_TTY_MODE_SET));
357 break;
358
359 case EVENT_TTY_MODE_GET:
360 handleQueryTTYModeResponse(msg);
361 break;
362
363 case EVENT_TTY_MODE_SET:
364 handleSetTTYModeResponse(msg);
365 break;
366 }
367 }
368 };
369
370 public PhoneGlobals(Context context) {
371 super(context);
372 sMe = this;
373 }
374
375 public void onCreate() {
376 if (VDBG) Log.v(LOG_TAG, "onCreate()...");
377
378 ContentResolver resolver = getContentResolver();
379
380 // Cache the "voice capable" flag.
381 // This flag currently comes from a resource (which is
382 // overrideable on a per-product basis):
383 sVoiceCapable =
384 getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
385 // ...but this might eventually become a PackageManager "system
386 // feature" instead, in which case we'd do something like:
387 // sVoiceCapable =
388 // getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY_VOICE_CALLS);
389
390 if (phone == null) {
391 // Initialize the telephony framework
392 PhoneFactory.makeDefaultPhones(this);
393
394 // Get the default phone
395 phone = PhoneFactory.getDefaultPhone();
396
397 // Start TelephonyDebugService After the default phone is created.
398 Intent intent = new Intent(this, TelephonyDebugService.class);
399 startService(intent);
400
401 mCM = CallManager.getInstance();
402 mCM.registerPhone(phone);
403
404 // Create the NotificationMgr singleton, which is used to display
405 // status bar icons and control other status bar behavior.
406 notificationMgr = NotificationMgr.init(this);
407
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700408 mHandler.sendEmptyMessage(EVENT_START_SIP_SERVICE);
409
410 int phoneType = phone.getPhoneType();
411
412 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
413 // Create an instance of CdmaPhoneCallState and initialize it to IDLE
414 cdmaPhoneCallState = new CdmaPhoneCallState();
415 cdmaPhoneCallState.CdmaPhoneCallStateInit();
416 }
417
418 if (BluetoothAdapter.getDefaultAdapter() != null) {
419 // Start BluetoothPhoneService even if device is not voice capable.
420 // The device can still support VOIP.
421 startService(new Intent(this, BluetoothPhoneService.class));
422 bindService(new Intent(this, BluetoothPhoneService.class),
423 mBluetoothPhoneConnection, 0);
424 } else {
425 // Device is not bluetooth capable
426 mBluetoothPhone = null;
427 }
428
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700429 // before registering for phone state changes
430 mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
431 mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, LOG_TAG);
432 // lock used to keep the processor awake, when we don't care for the display.
433 mPartialWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK
434 | PowerManager.ON_AFTER_RELEASE, LOG_TAG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700435
436 mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
437
438 // get a handle to the service so that we can use it later when we
439 // want to set the poke lock.
440 mPowerManagerService = IPowerManager.Stub.asInterface(
441 ServiceManager.getService("power"));
442
443 // Get UpdateLock to suppress system-update related events (e.g. dialog show-up)
444 // during phone calls.
445 mUpdateLock = new UpdateLock("phone");
446
447 if (DBG) Log.d(LOG_TAG, "onCreate: mUpdateLock: " + mUpdateLock);
448
449 CallLogger callLogger = new CallLogger(this, new CallLogAsync());
450
Santos Cordon69a69192013-08-22 14:25:42 -0700451 callGatewayManager = new CallGatewayManager();
452
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700453 // Create the CallController singleton, which is the interface
454 // to the telephony layer for user-initiated telephony functionality
455 // (like making outgoing calls.)
Santos Cordon69a69192013-08-22 14:25:42 -0700456 callController = CallController.init(this, callLogger, callGatewayManager);
457
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700458 // Create the CallerInfoCache singleton, which remembers custom ring tone and
459 // send-to-voicemail settings.
460 //
461 // The asynchronous caching will start just after this call.
462 callerInfoCache = CallerInfoCache.init(this);
463
464 // Monitors call activity from the telephony layer
465 callStateMonitor = new CallStateMonitor(mCM);
466
Christine Chenee09a492013-08-06 16:02:29 -0700467 // Rejects calls with TextMessages
468 rejectWithTextMessageManager = new RejectWithTextMessageManager();
469
Santos Cordon63a84242013-07-23 13:32:52 -0700470 // Creates call models for use with CallHandlerService.
Santos Cordon69a69192013-08-22 14:25:42 -0700471 callModeler = new CallModeler(callStateMonitor, mCM, rejectWithTextMessageManager,
472 callGatewayManager);
Santos Cordon63a84242013-07-23 13:32:52 -0700473
Santos Cordon2eaff902013-08-05 04:37:55 -0700474 // Plays DTMF Tones
475 dtmfTonePlayer = new DTMFTonePlayer(mCM, callModeler);
476
Santos Cordon593ab382013-08-06 21:58:23 -0700477 // Manages wired headset state
478 wiredHeadsetManager = new WiredHeadsetManager(this);
479 wiredHeadsetManager.addWiredHeadsetListener(this);
480
Santos Cordon2c2d3cf2013-08-08 03:53:47 -0700481 // Bluetooth manager
482 bluetoothManager = new BluetoothManager(this, mCM, callModeler);
Santos Cordon2c2d3cf2013-08-08 03:53:47 -0700483
484 ringer = Ringer.init(this, bluetoothManager);
485
Santos Cordon9b7bac72013-08-06 08:04:52 -0700486 // Audio router
Santos Cordon593ab382013-08-06 21:58:23 -0700487 audioRouter = new AudioRouter(this, bluetoothManager, wiredHeadsetManager, mCM);
Santos Cordon9b7bac72013-08-06 08:04:52 -0700488
Santos Cordon249efd02013-08-05 03:33:56 -0700489 // Service used by in-call UI to control calls
Santos Cordon9b7bac72013-08-06 08:04:52 -0700490 callCommandService = new CallCommandService(this, mCM, callModeler, dtmfTonePlayer,
Christine Chenee09a492013-08-06 16:02:29 -0700491 audioRouter, rejectWithTextMessageManager);
Santos Cordon249efd02013-08-05 03:33:56 -0700492
Santos Cordon89647a62013-07-16 13:38:09 -0700493 // Sends call state to the UI
Santos Cordon63a84242013-07-23 13:32:52 -0700494 callHandlerServiceProxy = new CallHandlerServiceProxy(this, callModeler,
Santos Cordon593ab382013-08-06 21:58:23 -0700495 callCommandService, audioRouter);
Santos Cordon89647a62013-07-16 13:38:09 -0700496
Santos Cordon406c0342013-08-28 00:07:47 -0700497 phoneMgr = PhoneInterfaceManager.init(this, phone, callHandlerServiceProxy);
498
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700499 // Create the CallNotifer singleton, which handles
500 // asynchronous events from the telephony layer (like
501 // launching the incoming-call UI when an incoming call comes
502 // in.)
Santos Cordon27a3c1f2013-08-06 07:49:27 -0700503 notifier = CallNotifier.init(this, phone, ringer, callLogger, callStateMonitor,
Santos Cordona5d5db82013-09-15 13:00:34 -0700504 bluetoothManager, callModeler);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700505
506 // register for ICC status
507 IccCard sim = phone.getIccCard();
508 if (sim != null) {
509 if (VDBG) Log.v(LOG_TAG, "register for ICC status");
510 sim.registerForNetworkLocked(mHandler, EVENT_SIM_NETWORK_LOCKED, null);
511 }
512
513 // register for MMI/USSD
514 mCM.registerForMmiComplete(mHandler, MMI_COMPLETE, null);
515
516 // register connection tracking to PhoneUtils
517 PhoneUtils.initializeConnectionHandler(mCM);
518
519 // Read platform settings for TTY feature
520 mTtyEnabled = getResources().getBoolean(R.bool.tty_enabled);
521
522 // Register for misc other intent broadcasts.
523 IntentFilter intentFilter =
524 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700525 intentFilter.addAction(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700526 intentFilter.addAction(Intent.ACTION_DOCK_EVENT);
527 intentFilter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
528 intentFilter.addAction(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);
529 intentFilter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
530 intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
531 if (mTtyEnabled) {
532 intentFilter.addAction(TtyIntent.TTY_PREFERRED_MODE_CHANGE_ACTION);
533 }
534 intentFilter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
535 registerReceiver(mReceiver, intentFilter);
536
537 // Use a separate receiver for ACTION_MEDIA_BUTTON broadcasts,
538 // since we need to manually adjust its priority (to make sure
539 // we get these intents *before* the media player.)
540 IntentFilter mediaButtonIntentFilter =
541 new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
542 // TODO verify the independent priority doesn't need to be handled thanks to the
543 // private intent handler registration
544 // Make sure we're higher priority than the media player's
545 // MediaButtonIntentReceiver (which currently has the default
546 // priority of zero; see apps/Music/AndroidManifest.xml.)
547 mediaButtonIntentFilter.setPriority(1);
548 //
549 registerReceiver(mMediaButtonReceiver, mediaButtonIntentFilter);
550 // register the component so it gets priority for calls
551 AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
552 am.registerMediaButtonEventReceiverForCalls(new ComponentName(this.getPackageName(),
553 MediaButtonBroadcastReceiver.class.getName()));
554
555 //set the default values for the preferences in the phone.
556 PreferenceManager.setDefaultValues(this, R.xml.network_setting, false);
557
558 PreferenceManager.setDefaultValues(this, R.xml.call_feature_setting, false);
559
560 // Make sure the audio mode (along with some
561 // audio-mode-related state of our own) is initialized
562 // correctly, given the current state of the phone.
563 PhoneUtils.setAudioMode(mCM);
564 }
565
566 if (TelephonyCapabilities.supportsOtasp(phone)) {
567 cdmaOtaProvisionData = new OtaUtils.CdmaOtaProvisionData();
568 cdmaOtaConfigData = new OtaUtils.CdmaOtaConfigData();
569 cdmaOtaScreenState = new OtaUtils.CdmaOtaScreenState();
570 cdmaOtaInCallScreenUiState = new OtaUtils.CdmaOtaInCallScreenUiState();
571 }
572
573 // XXX pre-load the SimProvider so that it's ready
574 resolver.getType(Uri.parse("content://icc/adn"));
575
576 // start with the default value to set the mute state.
577 mShouldRestoreMuteOnInCallResume = false;
578
579 // TODO: Register for Cdma Information Records
580 // phone.registerCdmaInformationRecord(mHandler, EVENT_UNSOL_CDMA_INFO_RECORD, null);
581
582 // Read TTY settings and store it into BP NV.
583 // AP owns (i.e. stores) the TTY setting in AP settings database and pushes the setting
584 // to BP at power up (BP does not need to make the TTY setting persistent storage).
585 // This way, there is a single owner (i.e AP) for the TTY setting in the phone.
586 if (mTtyEnabled) {
587 mPreferredTtyMode = android.provider.Settings.Secure.getInt(
588 phone.getContext().getContentResolver(),
589 android.provider.Settings.Secure.PREFERRED_TTY_MODE,
590 Phone.TTY_MODE_OFF);
591 mHandler.sendMessage(mHandler.obtainMessage(EVENT_TTY_PREFERRED_MODE_CHANGED, 0));
592 }
593 // Read HAC settings and configure audio hardware
594 if (getResources().getBoolean(R.bool.hac_enabled)) {
595 int hac = android.provider.Settings.System.getInt(phone.getContext().getContentResolver(),
596 android.provider.Settings.System.HEARING_AID,
597 0);
598 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
599 audioManager.setParameter(CallFeaturesSetting.HAC_KEY, hac != 0 ?
600 CallFeaturesSetting.HAC_VAL_ON :
601 CallFeaturesSetting.HAC_VAL_OFF);
602 }
603 }
604
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700605 /**
606 * Returns the singleton instance of the PhoneApp.
607 */
608 static PhoneGlobals getInstance() {
609 if (sMe == null) {
610 throw new IllegalStateException("No PhoneGlobals here!");
611 }
612 return sMe;
613 }
614
615 /**
616 * Returns the singleton instance of the PhoneApp if running as the
617 * primary user, otherwise null.
618 */
619 static PhoneGlobals getInstanceIfPrimary() {
620 return sMe;
621 }
622
623 /**
624 * Returns the Phone associated with this instance
625 */
626 static Phone getPhone() {
627 return getInstance().phone;
628 }
629
630 Ringer getRinger() {
631 return ringer;
632 }
633
634 IBluetoothHeadsetPhone getBluetoothPhoneService() {
635 return mBluetoothPhone;
636 }
637
Santos Cordon27a3c1f2013-08-06 07:49:27 -0700638 /* package */ BluetoothManager getBluetoothManager() {
639 return bluetoothManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700640 }
641
Santos Cordon593ab382013-08-06 21:58:23 -0700642 /* package */ WiredHeadsetManager getWiredHeadsetManager() {
643 return wiredHeadsetManager;
644 }
645
646 /* package */ AudioRouter getAudioRouter() {
647 return audioRouter;
648 }
649
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700650 /* package */ CallModeler getCallModeler() {
651 return callModeler;
652 }
653
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700654 /**
655 * Returns an Intent that can be used to go to the "Call log"
656 * UI (aka CallLogActivity) in the Contacts app.
657 *
658 * Watch out: there's no guarantee that the system has any activity to
659 * handle this intent. (In particular there may be no "Call log" at
660 * all on on non-voice-capable devices.)
661 */
662 /* package */ static Intent createCallLogIntent() {
663 Intent intent = new Intent(Intent.ACTION_VIEW, null);
664 intent.setType("vnd.android.cursor.dir/calls");
665 return intent;
666 }
667
Yorke Leeca6ec3b2013-08-29 14:21:43 -0700668 /* package */static PendingIntent createPendingCallLogIntent(Context context) {
669 final Intent callLogIntent = PhoneGlobals.createCallLogIntent();
670 final TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
671 taskStackBuilder.addNextIntent(callLogIntent);
672 return taskStackBuilder.getPendingIntent(0, 0);
673 }
674
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700675 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700676 * Returns PendingIntent for hanging up ongoing phone call. This will typically be used from
677 * Notification context.
678 */
679 /* package */ static PendingIntent createHangUpOngoingCallPendingIntent(Context context) {
680 Intent intent = new Intent(PhoneGlobals.ACTION_HANG_UP_ONGOING_CALL, null,
681 context, NotificationBroadcastReceiver.class);
682 return PendingIntent.getBroadcast(context, 0, intent, 0);
683 }
684
685 /* package */ static PendingIntent getCallBackPendingIntent(Context context, String number) {
686 Intent intent = new Intent(ACTION_CALL_BACK_FROM_NOTIFICATION,
687 Uri.fromParts(Constants.SCHEME_TEL, number, null),
688 context, NotificationBroadcastReceiver.class);
689 return PendingIntent.getBroadcast(context, 0, intent, 0);
690 }
691
692 /* package */ static PendingIntent getSendSmsFromNotificationPendingIntent(
693 Context context, String number) {
694 Intent intent = new Intent(ACTION_SEND_SMS_FROM_NOTIFICATION,
695 Uri.fromParts(Constants.SCHEME_SMSTO, number, null),
696 context, NotificationBroadcastReceiver.class);
697 return PendingIntent.getBroadcast(context, 0, intent, 0);
698 }
699
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700700 boolean isSimPinEnabled() {
701 return mIsSimPinEnabled;
702 }
703
704 boolean authenticateAgainstCachedSimPin(String pin) {
705 return (mCachedSimPin != null && mCachedSimPin.equals(pin));
706 }
707
708 void setCachedSimPin(String pin) {
709 mCachedSimPin = pin;
710 }
711
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700712 /**
713 * Handles OTASP-related events from the telephony layer.
714 *
715 * While an OTASP call is active, the CallNotifier forwards
716 * OTASP-related telephony events to this method.
717 */
718 void handleOtaspEvent(Message msg) {
719 if (DBG) Log.d(LOG_TAG, "handleOtaspEvent(message " + msg + ")...");
720
721 if (otaUtils == null) {
722 // We shouldn't be getting OTASP events without ever
723 // having started the OTASP call in the first place!
724 Log.w(LOG_TAG, "handleOtaEvents: got an event but otaUtils is null! "
725 + "message = " + msg);
726 return;
727 }
728
729 otaUtils.onOtaProvisionStatusChanged((AsyncResult) msg.obj);
730 }
731
732 /**
733 * Similarly, handle the disconnect event of an OTASP call
734 * by forwarding it to the OtaUtils instance.
735 */
736 /* package */ void handleOtaspDisconnect() {
737 if (DBG) Log.d(LOG_TAG, "handleOtaspDisconnect()...");
738
739 if (otaUtils == null) {
740 // We shouldn't be getting OTASP events without ever
741 // having started the OTASP call in the first place!
742 Log.w(LOG_TAG, "handleOtaspDisconnect: otaUtils is null!");
743 return;
744 }
745
746 otaUtils.onOtaspDisconnect();
747 }
748
749 /**
750 * Sets the activity responsible for un-PUK-blocking the device
751 * so that we may close it when we receive a positive result.
752 * mPUKEntryActivity is also used to indicate to the device that
753 * we are trying to un-PUK-lock the phone. In other words, iff
754 * it is NOT null, then we are trying to unlock and waiting for
755 * the SIM to move to READY state.
756 *
757 * @param activity is the activity to close when PUK has
758 * finished unlocking. Can be set to null to indicate the unlock
759 * or SIM READYing process is over.
760 */
761 void setPukEntryActivity(Activity activity) {
762 mPUKEntryActivity = activity;
763 }
764
765 Activity getPUKEntryActivity() {
766 return mPUKEntryActivity;
767 }
768
769 /**
770 * Sets the dialog responsible for notifying the user of un-PUK-
771 * blocking - SIM READYing progress, so that we may dismiss it
772 * when we receive a positive result.
773 *
774 * @param dialog indicates the progress dialog informing the user
775 * of the state of the device. Dismissed upon completion of
776 * READYing process
777 */
778 void setPukEntryProgressDialog(ProgressDialog dialog) {
779 mPUKEntryProgressDialog = dialog;
780 }
781
782 ProgressDialog getPUKEntryProgressDialog() {
783 return mPUKEntryProgressDialog;
784 }
785
786 /**
787 * Controls whether or not the screen is allowed to sleep.
788 *
789 * Once sleep is allowed (WakeState is SLEEP), it will rely on the
790 * settings for the poke lock to determine when to timeout and let
791 * the device sleep {@link PhoneGlobals#setScreenTimeout}.
792 *
793 * @param ws tells the device to how to wake.
794 */
795 /* package */ void requestWakeState(WakeState ws) {
796 if (VDBG) Log.d(LOG_TAG, "requestWakeState(" + ws + ")...");
797 synchronized (this) {
798 if (mWakeState != ws) {
799 switch (ws) {
800 case PARTIAL:
801 // acquire the processor wake lock, and release the FULL
802 // lock if it is being held.
803 mPartialWakeLock.acquire();
804 if (mWakeLock.isHeld()) {
805 mWakeLock.release();
806 }
807 break;
808 case FULL:
809 // acquire the full wake lock, and release the PARTIAL
810 // lock if it is being held.
811 mWakeLock.acquire();
812 if (mPartialWakeLock.isHeld()) {
813 mPartialWakeLock.release();
814 }
815 break;
816 case SLEEP:
817 default:
818 // release both the PARTIAL and FULL locks.
819 if (mWakeLock.isHeld()) {
820 mWakeLock.release();
821 }
822 if (mPartialWakeLock.isHeld()) {
823 mPartialWakeLock.release();
824 }
825 break;
826 }
827 mWakeState = ws;
828 }
829 }
830 }
831
832 /**
833 * If we are not currently keeping the screen on, then poke the power
834 * manager to wake up the screen for the user activity timeout duration.
835 */
836 /* package */ void wakeUpScreen() {
837 synchronized (this) {
838 if (mWakeState == WakeState.SLEEP) {
839 if (DBG) Log.d(LOG_TAG, "pulse screen lock");
840 mPowerManager.wakeUp(SystemClock.uptimeMillis());
841 }
842 }
843 }
844
845 /**
846 * Sets the wake state and screen timeout based on the current state
847 * of the phone, and the current state of the in-call UI.
848 *
849 * This method is a "UI Policy" wrapper around
850 * {@link PhoneGlobals#requestWakeState} and {@link PhoneGlobals#setScreenTimeout}.
851 *
852 * It's safe to call this method regardless of the state of the Phone
853 * (e.g. whether or not it's idle), and regardless of the state of the
854 * Phone UI (e.g. whether or not the InCallScreen is active.)
855 */
856 /* package */ void updateWakeState() {
857 PhoneConstants.State state = mCM.getState();
858
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700859 // True if the speakerphone is in use. (If so, we *always* use
860 // the default timeout. Since the user is obviously not holding
861 // the phone up to his/her face, we don't need to worry about
862 // false touches, and thus don't need to turn the screen off so
863 // aggressively.)
864 // Note that we need to make a fresh call to this method any
865 // time the speaker state changes. (That happens in
866 // PhoneUtils.turnOnSpeaker().)
867 boolean isSpeakerInUse = (state == PhoneConstants.State.OFFHOOK) && PhoneUtils.isSpeakerOn(this);
868
869 // TODO (bug 1440854): The screen timeout *might* also need to
870 // depend on the bluetooth state, but this isn't as clear-cut as
871 // the speaker state (since while using BT it's common for the
872 // user to put the phone straight into a pocket, in which case the
873 // timeout should probably still be short.)
874
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700875 // Decide whether to force the screen on or not.
876 //
877 // Force the screen to be on if the phone is ringing or dialing,
878 // or if we're displaying the "Call ended" UI for a connection in
879 // the "disconnected" state.
880 // However, if the phone is disconnected while the user is in the
881 // middle of selecting a quick response message, we should not force
882 // the screen to be on.
883 //
884 boolean isRinging = (state == PhoneConstants.State.RINGING);
885 boolean isDialing = (phone.getForegroundCall().getState() == Call.State.DIALING);
Jay Shrauner6fe8fd62013-09-16 19:39:30 -0700886 boolean keepScreenOn = isRinging || isDialing;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700887 // keepScreenOn == true means we'll hold a full wake lock:
888 requestWakeState(keepScreenOn ? WakeState.FULL : WakeState.SLEEP);
889 }
890
891 /**
892 * Manually pokes the PowerManager's userActivity method. Since we
893 * set the {@link WindowManager.LayoutParams#INPUT_FEATURE_DISABLE_USER_ACTIVITY}
894 * flag while the InCallScreen is active when there is no proximity sensor,
895 * we need to do this for touch events that really do count as user activity
896 * (like pressing any onscreen UI elements.)
897 */
898 /* package */ void pokeUserActivity() {
899 if (VDBG) Log.d(LOG_TAG, "pokeUserActivity()...");
900 mPowerManager.userActivity(SystemClock.uptimeMillis(), false);
901 }
902
903 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700904 * Notifies the phone app when the phone state changes.
905 *
Santos Cordonfc309812013-08-20 18:33:16 -0700906 * This method will updates various states inside Phone app (e.g. update-lock state, etc.)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700907 */
908 /* package */ void updatePhoneState(PhoneConstants.State state) {
909 if (state != mLastPhoneState) {
910 mLastPhoneState = state;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700911
912 // Try to acquire or release UpdateLock.
913 //
914 // Watch out: we don't release the lock here when the screen is still in foreground.
915 // At that time InCallScreen will release it on onPause().
916 if (state != PhoneConstants.State.IDLE) {
917 // UpdateLock is a recursive lock, while we may get "acquire" request twice and
918 // "release" request once for a single call (RINGING + OFFHOOK and IDLE).
919 // We need to manually ensure the lock is just acquired once for each (and this
920 // will prevent other possible buggy situations too).
921 if (!mUpdateLock.isHeld()) {
922 mUpdateLock.acquire();
923 }
924 } else {
Jay Shrauner6fe8fd62013-09-16 19:39:30 -0700925 if (!mUpdateLock.isHeld()) {
926 mUpdateLock.release();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700927 } else {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700928 }
929 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700930 }
931 }
932
933 /* package */ PhoneConstants.State getPhoneState() {
934 return mLastPhoneState;
935 }
936
937 /**
938 * Returns UpdateLock object.
939 */
940 /* package */ UpdateLock getUpdateLock() {
941 return mUpdateLock;
942 }
943
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700944 KeyguardManager getKeyguardManager() {
945 return mKeyguardManager;
946 }
947
948 private void onMMIComplete(AsyncResult r) {
949 if (VDBG) Log.d(LOG_TAG, "onMMIComplete()...");
950 MmiCode mmiCode = (MmiCode) r.result;
951 PhoneUtils.displayMMIComplete(phone, getInstance(), mmiCode, null, null);
952 }
953
954 private void initForNewRadioTechnology() {
955 if (DBG) Log.d(LOG_TAG, "initForNewRadioTechnology...");
956
957 if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
958 // Create an instance of CdmaPhoneCallState and initialize it to IDLE
959 cdmaPhoneCallState = new CdmaPhoneCallState();
960 cdmaPhoneCallState.CdmaPhoneCallStateInit();
961 }
962 if (TelephonyCapabilities.supportsOtasp(phone)) {
963 //create instances of CDMA OTA data classes
964 if (cdmaOtaProvisionData == null) {
965 cdmaOtaProvisionData = new OtaUtils.CdmaOtaProvisionData();
966 }
967 if (cdmaOtaConfigData == null) {
968 cdmaOtaConfigData = new OtaUtils.CdmaOtaConfigData();
969 }
970 if (cdmaOtaScreenState == null) {
971 cdmaOtaScreenState = new OtaUtils.CdmaOtaScreenState();
972 }
973 if (cdmaOtaInCallScreenUiState == null) {
974 cdmaOtaInCallScreenUiState = new OtaUtils.CdmaOtaInCallScreenUiState();
975 }
976 } else {
977 //Clean up OTA data in GSM/UMTS. It is valid only for CDMA
978 clearOtaState();
979 }
980
981 ringer.updateRingerContextAfterRadioTechnologyChange(this.phone);
982 notifier.updateCallNotifierRegistrationsAfterRadioTechnologyChange();
983 callStateMonitor.updateAfterRadioTechnologyChange();
984
985 if (mBluetoothPhone != null) {
986 try {
987 mBluetoothPhone.updateBtHandsfreeAfterRadioTechnologyChange();
988 } catch (RemoteException e) {
989 Log.e(LOG_TAG, Log.getStackTraceString(new Throwable()));
990 }
991 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700992
993 // Update registration for ICC status after radio technology change
994 IccCard sim = phone.getIccCard();
995 if (sim != null) {
996 if (DBG) Log.d(LOG_TAG, "Update registration for ICC status...");
997
998 //Register all events new to the new active phone
999 sim.registerForNetworkLocked(mHandler, EVENT_SIM_NETWORK_LOCKED, null);
1000 }
1001 }
1002
1003
1004 /**
Santos Cordon593ab382013-08-06 21:58:23 -07001005 * This is called when the wired headset state changes.
1006 */
1007 @Override
1008 public void onWiredHeadsetConnection(boolean pluggedIn) {
1009 PhoneConstants.State phoneState = mCM.getState();
1010
Santos Cordon593ab382013-08-06 21:58:23 -07001011 // Force TTY state update according to new headset state
1012 if (mTtyEnabled) {
1013 mHandler.sendMessage(mHandler.obtainMessage(EVENT_TTY_PREFERRED_MODE_CHANGED, 0));
1014 }
1015 }
1016
1017 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001018 * Receiver for misc intent broadcasts the Phone app cares about.
1019 */
1020 private class PhoneAppBroadcastReceiver extends BroadcastReceiver {
1021 @Override
1022 public void onReceive(Context context, Intent intent) {
1023 String action = intent.getAction();
1024 if (action.equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) {
1025 boolean enabled = System.getInt(getContentResolver(),
1026 System.AIRPLANE_MODE_ON, 0) == 0;
1027 phone.setRadioPower(enabled);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001028 } else if (action.equals(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED)) {
1029 if (VDBG) Log.d(LOG_TAG, "mReceiver: ACTION_ANY_DATA_CONNECTION_STATE_CHANGED");
1030 if (VDBG) Log.d(LOG_TAG, "- state: " + intent.getStringExtra(PhoneConstants.STATE_KEY));
1031 if (VDBG) Log.d(LOG_TAG, "- reason: "
1032 + intent.getStringExtra(PhoneConstants.STATE_CHANGE_REASON_KEY));
1033
1034 // The "data disconnected due to roaming" notification is shown
1035 // if (a) you have the "data roaming" feature turned off, and
1036 // (b) you just lost data connectivity because you're roaming.
1037 boolean disconnectedDueToRoaming =
1038 !phone.getDataRoamingEnabled()
1039 && "DISCONNECTED".equals(intent.getStringExtra(PhoneConstants.STATE_KEY))
1040 && Phone.REASON_ROAMING_ON.equals(
1041 intent.getStringExtra(PhoneConstants.STATE_CHANGE_REASON_KEY));
1042 mHandler.sendEmptyMessage(disconnectedDueToRoaming
1043 ? EVENT_DATA_ROAMING_DISCONNECTED
1044 : EVENT_DATA_ROAMING_OK);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001045 } else if ((action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) &&
1046 (mPUKEntryActivity != null)) {
1047 // if an attempt to un-PUK-lock the device was made, while we're
1048 // receiving this state change notification, notify the handler.
1049 // NOTE: This is ONLY triggered if an attempt to un-PUK-lock has
1050 // been attempted.
1051 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SIM_STATE_CHANGED,
1052 intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE)));
1053 } else if (action.equals(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED)) {
1054 String newPhone = intent.getStringExtra(PhoneConstants.PHONE_NAME_KEY);
1055 Log.d(LOG_TAG, "Radio technology switched. Now " + newPhone + " is active.");
1056 initForNewRadioTechnology();
1057 } else if (action.equals(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED)) {
1058 handleServiceStateChanged(intent);
1059 } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) {
1060 if (TelephonyCapabilities.supportsEcm(phone)) {
1061 Log.d(LOG_TAG, "Emergency Callback Mode arrived in PhoneApp.");
1062 // Start Emergency Callback Mode service
1063 if (intent.getBooleanExtra("phoneinECMState", false)) {
1064 context.startService(new Intent(context,
1065 EmergencyCallbackModeService.class));
1066 }
1067 } else {
1068 // It doesn't make sense to get ACTION_EMERGENCY_CALLBACK_MODE_CHANGED
1069 // on a device that doesn't support ECM in the first place.
1070 Log.e(LOG_TAG, "Got ACTION_EMERGENCY_CALLBACK_MODE_CHANGED, "
1071 + "but ECM isn't supported for phone: " + phone.getPhoneName());
1072 }
1073 } else if (action.equals(Intent.ACTION_DOCK_EVENT)) {
1074 mDockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
1075 Intent.EXTRA_DOCK_STATE_UNDOCKED);
1076 if (VDBG) Log.d(LOG_TAG, "ACTION_DOCK_EVENT -> mDockState = " + mDockState);
1077 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DOCK_STATE_CHANGED, 0));
1078 } else if (action.equals(TtyIntent.TTY_PREFERRED_MODE_CHANGE_ACTION)) {
1079 mPreferredTtyMode = intent.getIntExtra(TtyIntent.TTY_PREFFERED_MODE,
1080 Phone.TTY_MODE_OFF);
1081 if (VDBG) Log.d(LOG_TAG, "mReceiver: TTY_PREFERRED_MODE_CHANGE_ACTION");
1082 if (VDBG) Log.d(LOG_TAG, " mode: " + mPreferredTtyMode);
1083 mHandler.sendMessage(mHandler.obtainMessage(EVENT_TTY_PREFERRED_MODE_CHANGED, 0));
1084 } else if (action.equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1085 int ringerMode = intent.getIntExtra(AudioManager.EXTRA_RINGER_MODE,
1086 AudioManager.RINGER_MODE_NORMAL);
1087 if (ringerMode == AudioManager.RINGER_MODE_SILENT) {
1088 notifier.silenceRinger();
1089 }
1090 }
1091 }
1092 }
1093
1094 /**
1095 * Broadcast receiver for the ACTION_MEDIA_BUTTON broadcast intent.
1096 *
1097 * This functionality isn't lumped in with the other intents in
1098 * PhoneAppBroadcastReceiver because we instantiate this as a totally
1099 * separate BroadcastReceiver instance, since we need to manually
1100 * adjust its IntentFilter's priority (to make sure we get these
1101 * intents *before* the media player.)
1102 */
1103 private class MediaButtonBroadcastReceiver extends BroadcastReceiver {
1104 @Override
1105 public void onReceive(Context context, Intent intent) {
1106 KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
1107 if (VDBG) Log.d(LOG_TAG,
1108 "MediaButtonBroadcastReceiver.onReceive()... event = " + event);
1109 if ((event != null)
1110 && (event.getKeyCode() == KeyEvent.KEYCODE_HEADSETHOOK)) {
1111 if (VDBG) Log.d(LOG_TAG, "MediaButtonBroadcastReceiver: HEADSETHOOK");
1112 boolean consumed = PhoneUtils.handleHeadsetHook(phone, event);
1113 if (VDBG) Log.d(LOG_TAG, "==> handleHeadsetHook(): consumed = " + consumed);
1114 if (consumed) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001115 abortBroadcast();
1116 }
1117 } else {
1118 if (mCM.getState() != PhoneConstants.State.IDLE) {
1119 // If the phone is anything other than completely idle,
1120 // then we consume and ignore any media key events,
1121 // Otherwise it is too easy to accidentally start
1122 // playing music while a phone call is in progress.
1123 if (VDBG) Log.d(LOG_TAG, "MediaButtonBroadcastReceiver: consumed");
1124 abortBroadcast();
1125 }
1126 }
1127 }
1128 }
1129
1130 /**
1131 * Accepts broadcast Intents which will be prepared by {@link NotificationMgr} and thus
1132 * sent from framework's notification mechanism (which is outside Phone context).
1133 * This should be visible from outside, but shouldn't be in "exported" state.
1134 *
1135 * TODO: If possible merge this into PhoneAppBroadcastReceiver.
1136 */
1137 public static class NotificationBroadcastReceiver extends BroadcastReceiver {
1138 @Override
1139 public void onReceive(Context context, Intent intent) {
1140 String action = intent.getAction();
1141 // TODO: use "if (VDBG)" here.
1142 Log.d(LOG_TAG, "Broadcast from Notification: " + action);
1143
1144 if (action.equals(ACTION_HANG_UP_ONGOING_CALL)) {
1145 PhoneUtils.hangup(PhoneGlobals.getInstance().mCM);
1146 } else if (action.equals(ACTION_CALL_BACK_FROM_NOTIFICATION)) {
1147 // Collapse the expanded notification and the notification item itself.
1148 closeSystemDialogs(context);
1149 clearMissedCallNotification(context);
1150
1151 Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED, intent.getData());
1152 callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1153 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1154 context.startActivity(callIntent);
1155 } else if (action.equals(ACTION_SEND_SMS_FROM_NOTIFICATION)) {
1156 // Collapse the expanded notification and the notification item itself.
1157 closeSystemDialogs(context);
1158 clearMissedCallNotification(context);
1159
1160 Intent smsIntent = new Intent(Intent.ACTION_SENDTO, intent.getData());
1161 smsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1162 context.startActivity(smsIntent);
1163 } else {
1164 Log.w(LOG_TAG, "Received hang-up request from notification,"
1165 + " but there's no call the system can hang up.");
1166 }
1167 }
1168
1169 private void closeSystemDialogs(Context context) {
1170 Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
1171 context.sendBroadcastAsUser(intent, UserHandle.ALL);
1172 }
1173
1174 private void clearMissedCallNotification(Context context) {
1175 Intent clearIntent = new Intent(context, ClearMissedCallsService.class);
1176 clearIntent.setAction(ClearMissedCallsService.ACTION_CLEAR_MISSED_CALLS);
1177 context.startService(clearIntent);
1178 }
1179 }
1180
1181 private void handleServiceStateChanged(Intent intent) {
1182 /**
1183 * This used to handle updating EriTextWidgetProvider this routine
1184 * and and listening for ACTION_SERVICE_STATE_CHANGED intents could
1185 * be removed. But leaving just in case it might be needed in the near
1186 * future.
1187 */
1188
1189 // If service just returned, start sending out the queued messages
1190 ServiceState ss = ServiceState.newFromBundle(intent.getExtras());
1191
1192 if (ss != null) {
1193 int state = ss.getState();
1194 notificationMgr.updateNetworkSelection(state);
1195 }
1196 }
1197
1198 public boolean isOtaCallInActiveState() {
1199 boolean otaCallActive = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001200 if (VDBG) Log.d(LOG_TAG, "- isOtaCallInActiveState " + otaCallActive);
1201 return otaCallActive;
1202 }
1203
1204 public boolean isOtaCallInEndState() {
1205 boolean otaCallEnded = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001206 if (VDBG) Log.d(LOG_TAG, "- isOtaCallInEndState " + otaCallEnded);
1207 return otaCallEnded;
1208 }
1209
1210 // it is safe to call clearOtaState() even if the InCallScreen isn't active
1211 public void clearOtaState() {
1212 if (DBG) Log.d(LOG_TAG, "- clearOtaState ...");
Jay Shrauner6fe8fd62013-09-16 19:39:30 -07001213 if (otaUtils != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001214 otaUtils.cleanOtaScreen(true);
1215 if (DBG) Log.d(LOG_TAG, " - clearOtaState clears OTA screen");
1216 }
1217 }
1218
1219 // it is safe to call dismissOtaDialogs() even if the InCallScreen isn't active
1220 public void dismissOtaDialogs() {
1221 if (DBG) Log.d(LOG_TAG, "- dismissOtaDialogs ...");
Jay Shrauner6fe8fd62013-09-16 19:39:30 -07001222 if (otaUtils != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001223 otaUtils.dismissAllOtaDialogs();
1224 if (DBG) Log.d(LOG_TAG, " - dismissOtaDialogs clears OTA dialogs");
1225 }
1226 }
1227
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001228 private void handleQueryTTYModeResponse(Message msg) {
1229 AsyncResult ar = (AsyncResult) msg.obj;
1230 if (ar.exception != null) {
1231 if (DBG) Log.d(LOG_TAG, "handleQueryTTYModeResponse: Error getting TTY state.");
1232 } else {
1233 if (DBG) Log.d(LOG_TAG,
1234 "handleQueryTTYModeResponse: TTY enable state successfully queried.");
1235
1236 int ttymode = ((int[]) ar.result)[0];
1237 if (DBG) Log.d(LOG_TAG, "handleQueryTTYModeResponse:ttymode=" + ttymode);
1238
1239 Intent ttyModeChanged = new Intent(TtyIntent.TTY_ENABLED_CHANGE_ACTION);
1240 ttyModeChanged.putExtra("ttyEnabled", ttymode != Phone.TTY_MODE_OFF);
1241 sendBroadcastAsUser(ttyModeChanged, UserHandle.ALL);
1242
1243 String audioTtyMode;
1244 switch (ttymode) {
1245 case Phone.TTY_MODE_FULL:
1246 audioTtyMode = "tty_full";
1247 break;
1248 case Phone.TTY_MODE_VCO:
1249 audioTtyMode = "tty_vco";
1250 break;
1251 case Phone.TTY_MODE_HCO:
1252 audioTtyMode = "tty_hco";
1253 break;
1254 case Phone.TTY_MODE_OFF:
1255 default:
1256 audioTtyMode = "tty_off";
1257 break;
1258 }
1259 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
1260 audioManager.setParameters("tty_mode="+audioTtyMode);
1261 }
1262 }
1263
1264 private void handleSetTTYModeResponse(Message msg) {
1265 AsyncResult ar = (AsyncResult) msg.obj;
1266
1267 if (ar.exception != null) {
1268 if (DBG) Log.d (LOG_TAG,
1269 "handleSetTTYModeResponse: Error setting TTY mode, ar.exception"
1270 + ar.exception);
1271 }
1272 phone.queryTTYMode(mHandler.obtainMessage(EVENT_TTY_MODE_GET));
1273 }
1274
1275 /**
1276 * "Call origin" may be used by Contacts app to specify where the phone call comes from.
1277 * Currently, the only permitted value for this extra is {@link #ALLOWED_EXTRA_CALL_ORIGIN}.
1278 * Any other value will be ignored, to make sure that malicious apps can't trick the in-call
1279 * UI into launching some random other app after a call ends.
1280 *
1281 * TODO: make this more generic. Note that we should let the "origin" specify its package
1282 * while we are now assuming it is "com.android.contacts"
1283 */
1284 public static final String EXTRA_CALL_ORIGIN = "com.android.phone.CALL_ORIGIN";
1285 private static final String DEFAULT_CALL_ORIGIN_PACKAGE = "com.android.dialer";
1286 private static final String ALLOWED_EXTRA_CALL_ORIGIN =
1287 "com.android.dialer.DialtactsActivity";
1288 /**
1289 * Used to determine if the preserved call origin is fresh enough.
1290 */
1291 private static final long CALL_ORIGIN_EXPIRATION_MILLIS = 30 * 1000;
1292
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001293 /** Service connection */
1294 private final ServiceConnection mBluetoothPhoneConnection = new ServiceConnection() {
1295
1296 /** Handle the task of binding the local object to the service */
1297 public void onServiceConnected(ComponentName className, IBinder service) {
1298 Log.i(LOG_TAG, "Headset phone created, binding local service.");
1299 mBluetoothPhone = IBluetoothHeadsetPhone.Stub.asInterface(service);
1300 }
1301
1302 /** Handle the task of cleaning up the local binding */
1303 public void onServiceDisconnected(ComponentName className) {
1304 Log.i(LOG_TAG, "Headset phone disconnected, cleaning local binding.");
1305 mBluetoothPhone = null;
1306 }
1307 };
Santos Cordon83570472013-09-06 15:45:10 -07001308
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001309}