PhoneGlobals.java revision ae45ce811eff03bdfe78c492b2c21942edf6477a
1a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn/*
2a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn * Copyright (C) 2006 The Android Open Source Project
3a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn *
4a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn * Licensed under the Apache License, Version 2.0 (the "License");
5a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn * you may not use this file except in compliance with the License.
6a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn * You may obtain a copy of the License at
7a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn *
8a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn *      http://www.apache.org/licenses/LICENSE-2.0
9a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn *
10a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn * Unless required by applicable law or agreed to in writing, software
11a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn * distributed under the License is distributed on an "AS IS" BASIS,
12a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn * See the License for the specific language governing permissions and
14a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn * limitations under the License.
15a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn */
16a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
17a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornpackage com.android.phone;
18a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
19a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.app.Activity;
20a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.app.KeyguardManager;
21a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.app.PendingIntent;
22a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.app.ProgressDialog;
23a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.bluetooth.BluetoothAdapter;
24a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.bluetooth.BluetoothHeadset;
25a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.bluetooth.BluetoothProfile;
26a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.bluetooth.IBluetoothHeadsetPhone;
27a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.content.ActivityNotFoundException;
28a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.content.BroadcastReceiver;
29a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.content.ComponentName;
30a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.content.ContentResolver;
31a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.content.Context;
32a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.content.ContextWrapper;
33a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.content.Intent;
34a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.content.IntentFilter;
35a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.content.ServiceConnection;
36a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.content.res.Configuration;
37a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.media.AudioManager;
38a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.net.Uri;
39a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.os.AsyncResult;
40a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.os.Binder;
41a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.os.Handler;
42a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.os.IBinder;
43a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.os.IPowerManager;
44a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.os.LocalPowerManager;
45a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.os.Message;
46a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.os.PowerManager;
47a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.os.RemoteException;
48a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.os.ServiceManager;
49a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.os.SystemClock;
50a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.os.SystemProperties;
51a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.os.UpdateLock;
52a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.os.UserHandle;
53a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.preference.PreferenceManager;
54a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.provider.Settings.System;
55a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.telephony.ServiceState;
56a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.text.TextUtils;
57a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.util.Log;
58a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.util.Slog;
59a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport android.view.KeyEvent;
60a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
61a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport com.android.internal.telephony.Call;
62a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport com.android.internal.telephony.CallManager;
63a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport com.android.internal.telephony.IccCard;
64a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport com.android.internal.telephony.IccCardConstants;
65a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport com.android.internal.telephony.MmiCode;
66a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport com.android.internal.telephony.Phone;
67a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport com.android.internal.telephony.PhoneConstants;
68a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport com.android.internal.telephony.PhoneFactory;
69a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport com.android.internal.telephony.TelephonyCapabilities;
70a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport com.android.internal.telephony.TelephonyIntents;
71a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport com.android.internal.telephony.cdma.TtyIntent;
72ae45ce811eff03bdfe78c492b2c21942edf6477aBen Murdochimport com.android.phone.common.CallLogAsync;
73a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport com.android.phone.OtaUtils.CdmaOtaScreenState;
74a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornimport com.android.server.sip.SipService;
75a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
76a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn/**
77a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn * Global state for the telephony subsystem when running in the primary
78a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn * phone process.
79a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn */
80a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackbornpublic class PhoneGlobals extends ContextWrapper
81a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        implements AccelerometerListener.OrientationListener {
82a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /* package */ static final String LOG_TAG = "PhoneApp";
83a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
84a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
85a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Phone app-wide debug level:
86a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *   0 - no debug logging
87a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *   1 - normal debug logging if ro.debuggable is set (which is true in
88a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *       "eng" and "userdebug" builds but not "user" builds)
89a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *   2 - ultra-verbose debug logging
90a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
91a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Most individual classes in the phone app have a local DBG constant,
92a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * typically set to
93a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *   (PhoneApp.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1)
94a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * or else
95a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *   (PhoneApp.DBG_LEVEL >= 2)
96a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * depending on the desired verbosity.
97a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
98a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * ***** DO NOT SUBMIT WITH DBG_LEVEL > 0 *************
99a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
100a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /* package */ static final int DBG_LEVEL = 0;
101a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
102a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private static final boolean DBG =
103a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
104a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private static final boolean VDBG = (PhoneGlobals.DBG_LEVEL >= 2);
105a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
106a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // Message codes; see mHandler below.
107a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private static final int EVENT_SIM_NETWORK_LOCKED = 3;
108a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private static final int EVENT_WIRED_HEADSET_PLUG = 7;
109a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private static final int EVENT_SIM_STATE_CHANGED = 8;
110a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private static final int EVENT_UPDATE_INCALL_NOTIFICATION = 9;
111a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private static final int EVENT_DATA_ROAMING_DISCONNECTED = 10;
112a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private static final int EVENT_DATA_ROAMING_OK = 11;
113a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private static final int EVENT_UNSOL_CDMA_INFO_RECORD = 12;
114a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private static final int EVENT_DOCK_STATE_CHANGED = 13;
115a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private static final int EVENT_TTY_PREFERRED_MODE_CHANGED = 14;
116a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private static final int EVENT_TTY_MODE_GET = 15;
117a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private static final int EVENT_TTY_MODE_SET = 16;
118a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private static final int EVENT_START_SIP_SERVICE = 17;
119a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
120a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // The MMI codes are also used by the InCallScreen.
121a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    public static final int MMI_INITIATE = 51;
122a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    public static final int MMI_COMPLETE = 52;
123a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    public static final int MMI_CANCEL = 53;
124a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // Don't use message codes larger than 99 here; those are reserved for
125a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // the individual Activities of the Phone UI.
126a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
127a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
128a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Allowable values for the poke lock code (timeout between a user activity and the
129a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * going to sleep), please refer to {@link com.android.server.power.PowerManagerService}
130a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * for additional reference.
131a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *   SHORT uses the short delay for the timeout (SHORT_KEYLIGHT_DELAY, 6 sec)
132a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *   MEDIUM uses the medium delay for the timeout (MEDIUM_KEYLIGHT_DELAY, 15 sec)
133a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *   DEFAULT is the system-wide default delay for the timeout (1 min)
134a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
135a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    public enum ScreenTimeoutDuration {
136a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        SHORT,
137a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        MEDIUM,
138a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        DEFAULT
139a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
140a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
141a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
142a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Allowable values for the wake lock code.
143a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *   SLEEP means the device can be put to sleep.
144a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *   PARTIAL means wake the processor, but we display can be kept off.
145a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *   FULL means wake both the processor and the display.
146a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
147a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    public enum WakeState {
148a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        SLEEP,
149a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        PARTIAL,
150a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        FULL
151a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
152a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
153a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
154a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Intent Action used for hanging up the current call from Notification bar. This will
155a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * choose first ringing call, first active call, or first background call (typically in
156a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * HOLDING state).
157a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
158a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    public static final String ACTION_HANG_UP_ONGOING_CALL =
159a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            "com.android.phone.ACTION_HANG_UP_ONGOING_CALL";
160a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
161a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
162a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Intent Action used for making a phone call from Notification bar.
163a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * This is for missed call notifications.
164a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
165a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    public static final String ACTION_CALL_BACK_FROM_NOTIFICATION =
166a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            "com.android.phone.ACTION_CALL_BACK_FROM_NOTIFICATION";
167a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
168a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
169a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Intent Action used for sending a SMS from notification bar.
170a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * This is for missed call notifications.
171a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
172a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    public static final String ACTION_SEND_SMS_FROM_NOTIFICATION =
173a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            "com.android.phone.ACTION_SEND_SMS_FROM_NOTIFICATION";
174a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
175a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private static PhoneGlobals sMe;
176a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
177a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // A few important fields we expose to the rest of the package
178a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // directly (rather than thru set/get methods) for efficiency.
179a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    Phone phone;
180a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    CallController callController;
181a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    InCallUiState inCallUiState;
182a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    CallerInfoCache callerInfoCache;
183a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    CallNotifier notifier;
184a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    NotificationMgr notificationMgr;
185a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    Ringer ringer;
186a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    IBluetoothHeadsetPhone mBluetoothPhone;
187a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    PhoneInterfaceManager phoneMgr;
188a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    CallManager mCM;
189a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    int mBluetoothHeadsetState = BluetoothProfile.STATE_DISCONNECTED;
190a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    int mBluetoothHeadsetAudioState = BluetoothHeadset.STATE_AUDIO_DISCONNECTED;
191a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    boolean mShowBluetoothIndication = false;
192a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    static int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
193a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    static boolean sVoiceCapable = true;
194a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
195a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // Internal PhoneApp Call state tracker
196a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    CdmaPhoneCallState cdmaPhoneCallState;
197a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
198a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // The InCallScreen instance (or null if the InCallScreen hasn't been
199a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // created yet.)
200a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private InCallScreen mInCallScreen;
201a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
202a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // The currently-active PUK entry activity and progress dialog.
203a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // Normally, these are the Emergency Dialer and the subsequent
204a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // progress dialog.  null if there is are no such objects in
205a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // the foreground.
206a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private Activity mPUKEntryActivity;
207a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private ProgressDialog mPUKEntryProgressDialog;
208a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
209a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private boolean mIsSimPinEnabled;
210a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private String mCachedSimPin;
211a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
212a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // True if a wired headset is currently plugged in, based on the state
213a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // from the latest Intent.ACTION_HEADSET_PLUG broadcast we received in
214a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // mReceiver.onReceive().
215a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private boolean mIsHeadsetPlugged;
216a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
217a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // True if the keyboard is currently *not* hidden
218a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // Gets updated whenever there is a Configuration change
219a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private boolean mIsHardKeyboardOpen;
220a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
221a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // True if we are beginning a call, but the phone state has not changed yet
222a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private boolean mBeginningCall;
223a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
224a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // Last phone state seen by updatePhoneState()
225a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private PhoneConstants.State mLastPhoneState = PhoneConstants.State.IDLE;
226a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
227a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private WakeState mWakeState = WakeState.SLEEP;
228a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
229a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
230a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Timeout setting used by PokeLock.
231a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
232a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * This variable won't be effective when proximity sensor is available in the device.
233a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
234a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * @see ScreenTimeoutDuration
235a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
236a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private ScreenTimeoutDuration mScreenTimeoutDuration = ScreenTimeoutDuration.DEFAULT;
237a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
238a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Used to set/unset {@link LocalPowerManager#POKE_LOCK_IGNORE_TOUCH_EVENTS} toward PokeLock.
239a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
240a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * This variable won't be effective when proximity sensor is available in the device.
241a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
242a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private boolean mIgnoreTouchUserActivity = false;
243a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private final IBinder mPokeLockToken = new Binder();
244a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
245a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private PowerManager mPowerManager;
246a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private IPowerManager mPowerManagerService;
247a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private PowerManager.WakeLock mWakeLock;
248a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private PowerManager.WakeLock mPartialWakeLock;
249a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private PowerManager.WakeLock mProximityWakeLock;
250a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private KeyguardManager mKeyguardManager;
251a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private AccelerometerListener mAccelerometerListener;
252a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private int mOrientation = AccelerometerListener.ORIENTATION_UNKNOWN;
253a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
254a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private UpdateLock mUpdateLock;
255a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
256a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // Broadcast receiver for various intent broadcasts (see onCreate())
257a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private final BroadcastReceiver mReceiver = new PhoneAppBroadcastReceiver();
258a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
259a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // Broadcast receiver purely for ACTION_MEDIA_BUTTON broadcasts
260a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private final BroadcastReceiver mMediaButtonReceiver = new MediaButtonBroadcastReceiver();
261a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
262a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /** boolean indicating restoring mute state on InCallScreen.onResume() */
263a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private boolean mShouldRestoreMuteOnInCallResume;
264a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
265a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
266a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * The singleton OtaUtils instance used for OTASP calls.
267a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
268a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * The OtaUtils instance is created lazily the first time we need to
269a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * make an OTASP call, regardless of whether it's an interactive or
270a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * non-interactive OTASP call.
271a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
272a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    public OtaUtils otaUtils;
273a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
274a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // Following are the CDMA OTA information Objects used during OTA Call.
275a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // cdmaOtaProvisionData object store static OTA information that needs
276a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // to be maintained even during Slider open/close scenarios.
277a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // cdmaOtaConfigData object stores configuration info to control visiblity
278a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // of each OTA Screens.
279a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // cdmaOtaScreenState object store OTA Screen State information.
280a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    public OtaUtils.CdmaOtaProvisionData cdmaOtaProvisionData;
281a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    public OtaUtils.CdmaOtaConfigData cdmaOtaConfigData;
282a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    public OtaUtils.CdmaOtaScreenState cdmaOtaScreenState;
283a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    public OtaUtils.CdmaOtaInCallScreenUiState cdmaOtaInCallScreenUiState;
284a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
285a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // TTY feature enabled on this platform
286a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private boolean mTtyEnabled;
287a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // Current TTY operating mode selected by user
288a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private int mPreferredTtyMode = Phone.TTY_MODE_OFF;
289a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
290a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
291a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Set the restore mute state flag. Used when we are setting the mute state
292a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * OUTSIDE of user interaction {@link PhoneUtils#startNewCall(Phone)}
293a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
294a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /*package*/void setRestoreMuteOnInCallResume (boolean mode) {
295a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        mShouldRestoreMuteOnInCallResume = mode;
296a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
297a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
298a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
299a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Get the restore mute state flag.
300a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * This is used by the InCallScreen {@link InCallScreen#onResume()} to figure
301a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * out if we need to restore the mute state for the current active call.
302a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
303a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /*package*/boolean getRestoreMuteOnInCallResume () {
304a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        return mShouldRestoreMuteOnInCallResume;
305a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
306a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
307a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    Handler mHandler = new Handler() {
308a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        @Override
309a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        public void handleMessage(Message msg) {
310a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            PhoneConstants.State phoneState;
311a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            switch (msg.what) {
312a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // Starts the SIP service. It's a no-op if SIP API is not supported
313a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // on the deivce.
314a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // TODO: Having the phone process host the SIP service is only
315a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // temporary. Will move it to a persistent communication process
316a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // later.
317a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                case EVENT_START_SIP_SERVICE:
318a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    SipService.start(getApplicationContext());
319a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    break;
320a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
321a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // TODO: This event should be handled by the lock screen, just
322a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // like the "SIM missing" and "Sim locked" cases (bug 1804111).
323a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                case EVENT_SIM_NETWORK_LOCKED:
324a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    if (getResources().getBoolean(R.bool.ignore_sim_network_locked_events)) {
325a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        // Some products don't have the concept of a "SIM network lock"
326a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        Log.i(LOG_TAG, "Ignoring EVENT_SIM_NETWORK_LOCKED event; "
327a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                              + "not showing 'SIM network unlock' PIN entry screen");
328a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    } else {
329a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        // Normal case: show the "SIM network unlock" PIN entry screen.
330a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        // The user won't be able to do anything else until
331a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        // they enter a valid SIM network PIN.
332a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        Log.i(LOG_TAG, "show sim depersonal panel");
333a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        IccNetworkDepersonalizationPanel ndpPanel =
334a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                                new IccNetworkDepersonalizationPanel(PhoneGlobals.getInstance());
335a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        ndpPanel.show();
336a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    }
337a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    break;
338a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
339a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                case EVENT_UPDATE_INCALL_NOTIFICATION:
340a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // Tell the NotificationMgr to update the "ongoing
341a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // call" icon in the status bar, if necessary.
342a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // Currently, this is triggered by a bluetooth headset
343a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // state change (since the status bar icon needs to
344a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // turn blue when bluetooth is active.)
345a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    if (DBG) Log.d (LOG_TAG, "- updating in-call notification from handler...");
346a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    notificationMgr.updateInCallNotification();
347a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    break;
348a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
349a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                case EVENT_DATA_ROAMING_DISCONNECTED:
350a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    notificationMgr.showDataDisconnectedRoaming();
351a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    break;
352a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
353a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                case EVENT_DATA_ROAMING_OK:
354a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    notificationMgr.hideDataDisconnectedRoaming();
355a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    break;
356a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
357a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                case MMI_COMPLETE:
358a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    onMMIComplete((AsyncResult) msg.obj);
359a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    break;
360a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
361a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                case MMI_CANCEL:
362a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    PhoneUtils.cancelMmiCode(phone);
363a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    break;
364a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
365a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                case EVENT_WIRED_HEADSET_PLUG:
366a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // Since the presence of a wired headset or bluetooth affects the
367a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // speakerphone, update the "speaker" state.  We ONLY want to do
368a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // this on the wired headset connect / disconnect events for now
369a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // though, so we're only triggering on EVENT_WIRED_HEADSET_PLUG.
370a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
371a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    phoneState = mCM.getState();
372a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // Do not change speaker state if phone is not off hook
373a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    if (phoneState == PhoneConstants.State.OFFHOOK && !isBluetoothHeadsetAudioOn()) {
374a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        if (!isHeadsetPlugged()) {
375a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                            // if the state is "not connected", restore the speaker state.
376a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                            PhoneUtils.restoreSpeakerMode(getApplicationContext());
377a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        } else {
378a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                            // if the state is "connected", force the speaker off without
379a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                            // storing the state.
380a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                            PhoneUtils.turnOnSpeaker(getApplicationContext(), false, false);
381a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        }
382a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    }
383a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // Update the Proximity sensor based on headset state
384a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    updateProximitySensorMode(phoneState);
385a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
386a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // Force TTY state update according to new headset state
387a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    if (mTtyEnabled) {
388a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        sendMessage(obtainMessage(EVENT_TTY_PREFERRED_MODE_CHANGED, 0));
389a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    }
390a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    break;
391a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
392a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                case EVENT_SIM_STATE_CHANGED:
393a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // Marks the event where the SIM goes into ready state.
394a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // Right now, this is only used for the PUK-unlocking
395a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // process.
396a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    if (msg.obj.equals(IccCardConstants.INTENT_VALUE_ICC_READY)) {
397a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        // when the right event is triggered and there
398a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        // are UI objects in the foreground, we close
399a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        // them to display the lock panel.
400a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        if (mPUKEntryActivity != null) {
401a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                            mPUKEntryActivity.finish();
402a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                            mPUKEntryActivity = null;
403a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        }
404a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        if (mPUKEntryProgressDialog != null) {
405a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                            mPUKEntryProgressDialog.dismiss();
406a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                            mPUKEntryProgressDialog = null;
407a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        }
408a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    }
409a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    break;
410a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
411a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                case EVENT_UNSOL_CDMA_INFO_RECORD:
412a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    //TODO: handle message here;
413a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    break;
414a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
415a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                case EVENT_DOCK_STATE_CHANGED:
416a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // If the phone is docked/undocked during a call, and no wired or BT headset
417a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // is connected: turn on/off the speaker accordingly.
418a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    boolean inDockMode = false;
419a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
420a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        inDockMode = true;
421a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    }
422a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    if (VDBG) Log.d(LOG_TAG, "received EVENT_DOCK_STATE_CHANGED. Phone inDock = "
423a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                            + inDockMode);
424a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
425a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    phoneState = mCM.getState();
426a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    if (phoneState == PhoneConstants.State.OFFHOOK &&
427a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        !isHeadsetPlugged() && !isBluetoothHeadsetAudioOn()) {
428a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        PhoneUtils.turnOnSpeaker(getApplicationContext(), inDockMode, true);
429a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        updateInCallScreen();  // Has no effect if the InCallScreen isn't visible
430a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    }
431a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    break;
432a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
433a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                case EVENT_TTY_PREFERRED_MODE_CHANGED:
434a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // TTY mode is only applied if a headset is connected
435a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    int ttyMode;
436a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    if (isHeadsetPlugged()) {
437a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        ttyMode = mPreferredTtyMode;
438a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    } else {
439a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        ttyMode = Phone.TTY_MODE_OFF;
440a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    }
441a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    phone.setTTYMode(ttyMode, mHandler.obtainMessage(EVENT_TTY_MODE_SET));
442a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    break;
443a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
444a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                case EVENT_TTY_MODE_GET:
445a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    handleQueryTTYModeResponse(msg);
446a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    break;
447a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
448a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                case EVENT_TTY_MODE_SET:
449a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    handleSetTTYModeResponse(msg);
450a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    break;
451a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            }
452a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
453a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    };
454a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
455a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    public PhoneGlobals(Context context) {
456a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        super(context);
457a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        sMe = this;
458a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
459a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
460a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    public void onCreate() {
461a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (VDBG) Log.v(LOG_TAG, "onCreate()...");
462a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
463a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        ContentResolver resolver = getContentResolver();
464a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
465a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // Cache the "voice capable" flag.
466a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // This flag currently comes from a resource (which is
467a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // overrideable on a per-product basis):
468a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        sVoiceCapable =
469a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);
470a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // ...but this might eventually become a PackageManager "system
471a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // feature" instead, in which case we'd do something like:
472a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // sVoiceCapable =
473a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        //   getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY_VOICE_CALLS);
474a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
475a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (phone == null) {
476a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // Initialize the telephony framework
477a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            PhoneFactory.makeDefaultPhones(this);
478a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
479a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // Get the default phone
480a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            phone = PhoneFactory.getDefaultPhone();
481a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
482a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // Start TelephonyDebugService After the default phone is created.
483a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            Intent intent = new Intent(this, TelephonyDebugService.class);
484a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            startService(intent);
485a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
486a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            mCM = CallManager.getInstance();
487a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            mCM.registerPhone(phone);
488a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
489a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // Create the NotificationMgr singleton, which is used to display
490a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // status bar icons and control other status bar behavior.
491a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            notificationMgr = NotificationMgr.init(this);
492a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
493a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            phoneMgr = PhoneInterfaceManager.init(this, phone);
494a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
495a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            mHandler.sendEmptyMessage(EVENT_START_SIP_SERVICE);
496a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
497a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            int phoneType = phone.getPhoneType();
498a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
499a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
500a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // Create an instance of CdmaPhoneCallState and initialize it to IDLE
501a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                cdmaPhoneCallState = new CdmaPhoneCallState();
502a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                cdmaPhoneCallState.CdmaPhoneCallStateInit();
503a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            }
504a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
505a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (BluetoothAdapter.getDefaultAdapter() != null) {
506a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // Start BluetoothPhoneService even if device is not voice capable.
507a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // The device can still support VOIP.
508a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                startService(new Intent(this, BluetoothPhoneService.class));
509a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                bindService(new Intent(this, BluetoothPhoneService.class),
510a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                            mBluetoothPhoneConnection, 0);
511a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            } else {
512a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // Device is not bluetooth capable
513a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                mBluetoothPhone = null;
514a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            }
515a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
516a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            ringer = Ringer.init(this);
517a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
518a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // before registering for phone state changes
519a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
520a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK
521a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    | PowerManager.ACQUIRE_CAUSES_WAKEUP,
522a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    LOG_TAG);
523a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // lock used to keep the processor awake, when we don't care for the display.
524a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            mPartialWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK
525a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    | PowerManager.ON_AFTER_RELEASE, LOG_TAG);
526a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // Wake lock used to control proximity sensor behavior.
527a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (mPowerManager.isWakeLockLevelSupported(
528a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK)) {
529a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                mProximityWakeLock = mPowerManager.newWakeLock(
530a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, LOG_TAG);
531a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            }
532a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (DBG) Log.d(LOG_TAG, "onCreate: mProximityWakeLock: " + mProximityWakeLock);
533a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
534a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // create mAccelerometerListener only if we are using the proximity sensor
535a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (proximitySensorModeEnabled()) {
536a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                mAccelerometerListener = new AccelerometerListener(this, this);
537a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            }
538a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
539a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
540a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
541a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // get a handle to the service so that we can use it later when we
542a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // want to set the poke lock.
543a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            mPowerManagerService = IPowerManager.Stub.asInterface(
544a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    ServiceManager.getService("power"));
545a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
546a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // Get UpdateLock to suppress system-update related events (e.g. dialog show-up)
547a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // during phone calls.
548a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            mUpdateLock = new UpdateLock("phone");
549a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
550a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (DBG) Log.d(LOG_TAG, "onCreate: mUpdateLock: " + mUpdateLock);
551a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
552a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // Create the CallController singleton, which is the interface
553a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // to the telephony layer for user-initiated telephony functionality
554a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // (like making outgoing calls.)
555a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            callController = CallController.init(this);
556a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // ...and also the InCallUiState instance, used by the CallController to
557a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // keep track of some "persistent state" of the in-call UI.
558a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            inCallUiState = InCallUiState.init(this);
559a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
560a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // Create the CallerInfoCache singleton, which remembers custom ring tone and
561a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // send-to-voicemail settings.
562a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            //
563a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // The asynchronous caching will start just after this call.
564a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            callerInfoCache = CallerInfoCache.init(this);
565a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
566a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // Create the CallNotifer singleton, which handles
567a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // asynchronous events from the telephony layer (like
568a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // launching the incoming-call UI when an incoming call comes
569a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // in.)
570a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            notifier = CallNotifier.init(this, phone, ringer, new CallLogAsync());
571a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
572a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // register for ICC status
573a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            IccCard sim = phone.getIccCard();
574a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (sim != null) {
575a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                if (VDBG) Log.v(LOG_TAG, "register for ICC status");
576a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                sim.registerForNetworkLocked(mHandler, EVENT_SIM_NETWORK_LOCKED, null);
577a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            }
578a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
579a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // register for MMI/USSD
580a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            mCM.registerForMmiComplete(mHandler, MMI_COMPLETE, null);
581a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
582a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // register connection tracking to PhoneUtils
583a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            PhoneUtils.initializeConnectionHandler(mCM);
584a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
585a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // Read platform settings for TTY feature
586a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            mTtyEnabled = getResources().getBoolean(R.bool.tty_enabled);
587a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
588a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // Register for misc other intent broadcasts.
589a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            IntentFilter intentFilter =
590a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
591a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            intentFilter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
592a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            intentFilter.addAction(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED);
593a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            intentFilter.addAction(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED);
594a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            intentFilter.addAction(Intent.ACTION_HEADSET_PLUG);
595a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            intentFilter.addAction(Intent.ACTION_DOCK_EVENT);
596a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            intentFilter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
597a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            intentFilter.addAction(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);
598a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            intentFilter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
599a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
600a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (mTtyEnabled) {
601a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                intentFilter.addAction(TtyIntent.TTY_PREFERRED_MODE_CHANGE_ACTION);
602a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            }
603a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            intentFilter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
604a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            registerReceiver(mReceiver, intentFilter);
605a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
606a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // Use a separate receiver for ACTION_MEDIA_BUTTON broadcasts,
607a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // since we need to manually adjust its priority (to make sure
608a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // we get these intents *before* the media player.)
609a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            IntentFilter mediaButtonIntentFilter =
610a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
611a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // TODO verify the independent priority doesn't need to be handled thanks to the
612a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            //  private intent handler registration
613a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // Make sure we're higher priority than the media player's
614a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // MediaButtonIntentReceiver (which currently has the default
615a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // priority of zero; see apps/Music/AndroidManifest.xml.)
616a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            mediaButtonIntentFilter.setPriority(1);
617a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            //
618a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            registerReceiver(mMediaButtonReceiver, mediaButtonIntentFilter);
619a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // register the component so it gets priority for calls
620a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
621a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            am.registerMediaButtonEventReceiverForCalls(new ComponentName(this.getPackageName(),
622a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    MediaButtonBroadcastReceiver.class.getName()));
623a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
624a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            //set the default values for the preferences in the phone.
625a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            PreferenceManager.setDefaultValues(this, R.xml.network_setting, false);
626a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
627a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            PreferenceManager.setDefaultValues(this, R.xml.call_feature_setting, false);
628a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
629a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // Make sure the audio mode (along with some
630a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // audio-mode-related state of our own) is initialized
631a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // correctly, given the current state of the phone.
632a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            PhoneUtils.setAudioMode(mCM);
633a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
634a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
635a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (TelephonyCapabilities.supportsOtasp(phone)) {
636a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            cdmaOtaProvisionData = new OtaUtils.CdmaOtaProvisionData();
637a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            cdmaOtaConfigData = new OtaUtils.CdmaOtaConfigData();
638a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            cdmaOtaScreenState = new OtaUtils.CdmaOtaScreenState();
639a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            cdmaOtaInCallScreenUiState = new OtaUtils.CdmaOtaInCallScreenUiState();
640a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
641a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
642a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // XXX pre-load the SimProvider so that it's ready
643a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        resolver.getType(Uri.parse("content://icc/adn"));
644a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
645a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // start with the default value to set the mute state.
646a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        mShouldRestoreMuteOnInCallResume = false;
647a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
648a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // TODO: Register for Cdma Information Records
649a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // phone.registerCdmaInformationRecord(mHandler, EVENT_UNSOL_CDMA_INFO_RECORD, null);
650a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
651a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // Read TTY settings and store it into BP NV.
652a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // AP owns (i.e. stores) the TTY setting in AP settings database and pushes the setting
653a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // to BP at power up (BP does not need to make the TTY setting persistent storage).
654a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // This way, there is a single owner (i.e AP) for the TTY setting in the phone.
655a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (mTtyEnabled) {
656a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            mPreferredTtyMode = android.provider.Settings.Secure.getInt(
657a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    phone.getContext().getContentResolver(),
658a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    android.provider.Settings.Secure.PREFERRED_TTY_MODE,
659a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    Phone.TTY_MODE_OFF);
660a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            mHandler.sendMessage(mHandler.obtainMessage(EVENT_TTY_PREFERRED_MODE_CHANGED, 0));
661a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
662a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // Read HAC settings and configure audio hardware
663a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (getResources().getBoolean(R.bool.hac_enabled)) {
664a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            int hac = android.provider.Settings.System.getInt(phone.getContext().getContentResolver(),
665a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                                                              android.provider.Settings.System.HEARING_AID,
666a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                                                              0);
667a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
668a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            audioManager.setParameter(CallFeaturesSetting.HAC_KEY, hac != 0 ?
669a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                                      CallFeaturesSetting.HAC_VAL_ON :
670a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                                      CallFeaturesSetting.HAC_VAL_OFF);
671a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
672a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn   }
673a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
674a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    public void onConfigurationChanged(Configuration newConfig) {
675a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
676a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            mIsHardKeyboardOpen = true;
677a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        } else {
678a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            mIsHardKeyboardOpen = false;
679a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
680a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
681a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // Update the Proximity sensor based on keyboard state
682a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        updateProximitySensorMode(mCM.getState());
683a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
684a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
685a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
686a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Returns the singleton instance of the PhoneApp.
687a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
688a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    static PhoneGlobals getInstance() {
689a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (sMe == null) {
690a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            throw new IllegalStateException("No PhoneGlobals here!");
691a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
692a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        return sMe;
693a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
694a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
695a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
696a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Returns the singleton instance of the PhoneApp if running as the
697a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * primary user, otherwise null.
698a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
699a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    static PhoneGlobals getInstanceIfPrimary() {
700a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        return sMe;
701a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
702a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
703a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
704a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Returns the Phone associated with this instance
705a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
706a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    static Phone getPhone() {
707a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        return getInstance().phone;
708a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
709a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
710a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    Ringer getRinger() {
711a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        return ringer;
712a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
713a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
714a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    IBluetoothHeadsetPhone getBluetoothPhoneService() {
715a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        return mBluetoothPhone;
716a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
717a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
718a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    boolean isBluetoothHeadsetAudioOn() {
719a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        return (mBluetoothHeadsetAudioState != BluetoothHeadset.STATE_AUDIO_DISCONNECTED);
720a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
721a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
722a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
723a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Returns an Intent that can be used to go to the "Call log"
724a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * UI (aka CallLogActivity) in the Contacts app.
725a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
726a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Watch out: there's no guarantee that the system has any activity to
727a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * handle this intent.  (In particular there may be no "Call log" at
728a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * all on on non-voice-capable devices.)
729a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
730a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /* package */ static Intent createCallLogIntent() {
731a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        Intent intent = new Intent(Intent.ACTION_VIEW, null);
732a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        intent.setType("vnd.android.cursor.dir/calls");
733a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        return intent;
734a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
735a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
736a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
737a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Return an Intent that can be used to bring up the in-call screen.
738a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
739a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * This intent can only be used from within the Phone app, since the
740a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * InCallScreen is not exported from our AndroidManifest.
741a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
742a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /* package */ static Intent createInCallIntent() {
743a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        Intent intent = new Intent(Intent.ACTION_MAIN, null);
744a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
745a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
746a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
747a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        intent.setClassName("com.android.phone", getCallScreenClassName());
748a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        return intent;
749a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
750a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
751a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
752a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Variation of createInCallIntent() that also specifies whether the
753a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * DTMF dialpad should be initially visible when the InCallScreen
754a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * comes up.
755a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
756a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /* package */ static Intent createInCallIntent(boolean showDialpad) {
757a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        Intent intent = createInCallIntent();
758a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        intent.putExtra(InCallScreen.SHOW_DIALPAD_EXTRA, showDialpad);
759a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        return intent;
760a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
761a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
762a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
763a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Returns PendingIntent for hanging up ongoing phone call. This will typically be used from
764a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Notification context.
765a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
766a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /* package */ static PendingIntent createHangUpOngoingCallPendingIntent(Context context) {
767a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        Intent intent = new Intent(PhoneGlobals.ACTION_HANG_UP_ONGOING_CALL, null,
768a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                context, NotificationBroadcastReceiver.class);
769a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        return PendingIntent.getBroadcast(context, 0, intent, 0);
770a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
771a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
772a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /* package */ static PendingIntent getCallBackPendingIntent(Context context, String number) {
773a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        Intent intent = new Intent(ACTION_CALL_BACK_FROM_NOTIFICATION,
774a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                Uri.fromParts(Constants.SCHEME_TEL, number, null),
775a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                context, NotificationBroadcastReceiver.class);
776a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        return PendingIntent.getBroadcast(context, 0, intent, 0);
777a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
778a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
779a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /* package */ static PendingIntent getSendSmsFromNotificationPendingIntent(
780a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            Context context, String number) {
781a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        Intent intent = new Intent(ACTION_SEND_SMS_FROM_NOTIFICATION,
782a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                Uri.fromParts(Constants.SCHEME_SMSTO, number, null),
783a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                context, NotificationBroadcastReceiver.class);
784a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        return PendingIntent.getBroadcast(context, 0, intent, 0);
785a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
786a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
787a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private static String getCallScreenClassName() {
788a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        return InCallScreen.class.getName();
789a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
790a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
791a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
792a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Starts the InCallScreen Activity.
793a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
794a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /* package */ void displayCallScreen() {
795a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (VDBG) Log.d(LOG_TAG, "displayCallScreen()...");
796a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
797a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // On non-voice-capable devices we shouldn't ever be trying to
798a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // bring up the InCallScreen in the first place.
799a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (!sVoiceCapable) {
800a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            Log.w(LOG_TAG, "displayCallScreen() not allowed: non-voice-capable device",
801a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                  new Throwable("stack dump"));  // Include a stack trace since this warning
802a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                                                 // indicates a bug in our caller
803a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            return;
804a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
805a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
806a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        try {
807a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            startActivity(createInCallIntent());
808a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        } catch (ActivityNotFoundException e) {
809a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // It's possible that the in-call UI might not exist (like on
810a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // non-voice-capable devices), so don't crash if someone
811a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // accidentally tries to bring it up...
812a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            Log.w(LOG_TAG, "displayCallScreen: transition to InCallScreen failed: " + e);
813a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
814a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        Profiler.callScreenRequested();
815a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
816a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
817a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    boolean isSimPinEnabled() {
818a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        return mIsSimPinEnabled;
819a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
820a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
821a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    boolean authenticateAgainstCachedSimPin(String pin) {
822a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        return (mCachedSimPin != null && mCachedSimPin.equals(pin));
823a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
824a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
825a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    void setCachedSimPin(String pin) {
826a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        mCachedSimPin = pin;
827a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
828a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
829a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    void setInCallScreenInstance(InCallScreen inCallScreen) {
830a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        mInCallScreen = inCallScreen;
831a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
832a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
833a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
834a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * @return true if the in-call UI is running as the foreground
835a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * activity.  (In other words, from the perspective of the
836a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * InCallScreen activity, return true between onResume() and
837a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * onPause().)
838a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
839a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Note this method will return false if the screen is currently off,
840a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * even if the InCallScreen *was* in the foreground just before the
841a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * screen turned off.  (This is because the foreground activity is
842a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * always "paused" while the screen is off.)
843a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
844a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    boolean isShowingCallScreen() {
845a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (mInCallScreen == null) return false;
846a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        return mInCallScreen.isForegroundActivity();
847a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
848a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
849a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
850a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * @return true if the in-call UI is running as the foreground activity, or,
851a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * it went to background due to screen being turned off. This might be useful
852a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * to determine if the in-call screen went to background because of other
853a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * activities, or its proximity sensor state or manual power-button press.
854a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
855a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Here are some examples.
856a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
857a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * - If you want to know if the activity is in foreground or screen is turned off
858a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *   from the in-call UI (i.e. though it is not "foreground" anymore it will become
859a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *   so after screen being turned on), check
860a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *   {@link #isShowingCallScreenForProximity()} is true or not.
861a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *   {@link #updateProximitySensorMode(com.android.internal.telephony.PhoneConstants.State)} is
862a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *   doing this.
863a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
864a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * - If you want to know if the activity is not in foreground just because screen
865a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *   is turned off (not due to other activity's interference), check
866a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *   {@link #isShowingCallScreen()} is false *and* {@link #isShowingCallScreenForProximity()}
867a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *   is true. InCallScreen#onDisconnect() is doing this check.
868a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
869a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * @see #isShowingCallScreen()
870a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
871a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * TODO: come up with better naming..
872a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
873a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    boolean isShowingCallScreenForProximity() {
874a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (mInCallScreen == null) return false;
875a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        return mInCallScreen.isForegroundActivityForProximity();
876a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
877a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
878a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
879a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Dismisses the in-call UI.
880a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
881a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * This also ensures that you won't be able to get back to the in-call
882a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * UI via the BACK button (since this call removes the InCallScreen
883a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * from the activity history.)
884a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * For OTA Call, it call InCallScreen api to handle OTA Call End scenario
885a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * to display OTA Call End screen.
886a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
887a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /* package */ void dismissCallScreen() {
888a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (mInCallScreen != null) {
889a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if ((TelephonyCapabilities.supportsOtasp(phone)) &&
890a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    (mInCallScreen.isOtaCallInActiveState()
891a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    || mInCallScreen.isOtaCallInEndState()
892a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    || ((cdmaOtaScreenState != null)
893a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    && (cdmaOtaScreenState.otaScreenState
894a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                            != CdmaOtaScreenState.OtaScreenState.OTA_STATUS_UNDEFINED)))) {
895a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // TODO: During OTA Call, display should not become dark to
896a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // allow user to see OTA UI update. Phone app needs to hold
897a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // a SCREEN_DIM_WAKE_LOCK wake lock during the entire OTA call.
898a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                wakeUpScreen();
899a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // If InCallScreen is not in foreground we resume it to show the OTA call end screen
900a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // Fire off the InCallScreen intent
901a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                displayCallScreen();
902a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
903a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                mInCallScreen.handleOtaCallEnd();
904a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                return;
905a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            } else {
906a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                mInCallScreen.finish();
907a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            }
908a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
909a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
910a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
911a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
912a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Handles OTASP-related events from the telephony layer.
913a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
914a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * While an OTASP call is active, the CallNotifier forwards
915a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * OTASP-related telephony events to this method.
916a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
917a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    void handleOtaspEvent(Message msg) {
918a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (DBG) Log.d(LOG_TAG, "handleOtaspEvent(message " + msg + ")...");
919a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
920a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (otaUtils == null) {
921a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // We shouldn't be getting OTASP events without ever
922a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // having started the OTASP call in the first place!
923a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            Log.w(LOG_TAG, "handleOtaEvents: got an event but otaUtils is null! "
924a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                  + "message = " + msg);
925a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            return;
926a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
927a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
928a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        otaUtils.onOtaProvisionStatusChanged((AsyncResult) msg.obj);
929a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
930a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
931a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
932a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Similarly, handle the disconnect event of an OTASP call
933a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * by forwarding it to the OtaUtils instance.
934a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
935a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /* package */ void handleOtaspDisconnect() {
936a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (DBG) Log.d(LOG_TAG, "handleOtaspDisconnect()...");
937a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
938a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (otaUtils == null) {
939a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // We shouldn't be getting OTASP events without ever
940a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // having started the OTASP call in the first place!
941a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            Log.w(LOG_TAG, "handleOtaspDisconnect: otaUtils is null!");
942a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            return;
943a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
944a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
945a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        otaUtils.onOtaspDisconnect();
946a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
947a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
948a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
949a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Sets the activity responsible for un-PUK-blocking the device
950a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * so that we may close it when we receive a positive result.
951a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * mPUKEntryActivity is also used to indicate to the device that
952a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * we are trying to un-PUK-lock the phone. In other words, iff
953a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * it is NOT null, then we are trying to unlock and waiting for
954a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * the SIM to move to READY state.
955a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
956a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * @param activity is the activity to close when PUK has
957a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * finished unlocking. Can be set to null to indicate the unlock
958a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * or SIM READYing process is over.
959a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
960a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    void setPukEntryActivity(Activity activity) {
961a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        mPUKEntryActivity = activity;
962a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
963a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
964a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    Activity getPUKEntryActivity() {
965a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        return mPUKEntryActivity;
966a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
967a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
968a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
969a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Sets the dialog responsible for notifying the user of un-PUK-
970a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * blocking - SIM READYing progress, so that we may dismiss it
971a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * when we receive a positive result.
972a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
973a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * @param dialog indicates the progress dialog informing the user
974a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * of the state of the device.  Dismissed upon completion of
975a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * READYing process
976a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
977a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    void setPukEntryProgressDialog(ProgressDialog dialog) {
978a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        mPUKEntryProgressDialog = dialog;
979a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
980a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
981a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    ProgressDialog getPUKEntryProgressDialog() {
982a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        return mPUKEntryProgressDialog;
983a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
984a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
985a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
986a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Controls how quickly the screen times out.
987a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
988a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * This is no-op when the device supports proximity sensor.
989a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
990a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * The poke lock controls how long it takes before the screen powers
991a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * down, and therefore has no immediate effect when the current
992a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * WakeState (see {@link PhoneGlobals#requestWakeState}) is FULL.
993a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * If we're in a state where the screen *is* allowed to turn off,
994a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * though, the poke lock will determine the timeout interval (long or
995a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * short).
996a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
997a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /* package */ void setScreenTimeout(ScreenTimeoutDuration duration) {
998a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (VDBG) Log.d(LOG_TAG, "setScreenTimeout(" + duration + ")...");
999a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1000a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // stick with default timeout if we are using the proximity sensor
1001a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (proximitySensorModeEnabled()) {
1002a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            return;
1003a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1004a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1005a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // make sure we don't set the poke lock repeatedly so that we
1006a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // avoid triggering the userActivity calls in
1007a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // PowerManagerService.setPokeLock().
1008a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (duration == mScreenTimeoutDuration) {
1009a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            return;
1010a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1011a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        mScreenTimeoutDuration = duration;
1012a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        updatePokeLock();
1013a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1014a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1015a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
1016a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Update the state of the poke lock held by the phone app,
1017a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * based on the current desired screen timeout and the
1018a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * current "ignore user activity on touch" flag.
1019a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
1020a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private void updatePokeLock() {
1021a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // Caller must take care of the check. This block is purely for safety.
1022a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (proximitySensorModeEnabled()) {
1023a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            Log.wtf(LOG_TAG, "PokeLock should not be used when proximity sensor is available on"
1024a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    + " the device.");
1025a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            return;
1026a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1027a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1028a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // This is kind of convoluted, but the basic thing to remember is
1029a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // that the poke lock just sends a message to the screen to tell
1030a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // it to stay on for a while.
1031a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // The default is 0, for a long timeout and should be set that way
1032a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // when we are heading back into a the keyguard / screen off
1033a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // state, and also when we're trying to keep the screen alive
1034a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // while ringing.  We'll also want to ignore the cheek events
1035a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // regardless of the timeout duration.
1036a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // The short timeout is really used whenever we want to give up
1037a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // the screen lock, such as when we're in call.
1038a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        int pokeLockSetting = 0;
1039a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        switch (mScreenTimeoutDuration) {
1040a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            case SHORT:
1041a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // Set the poke lock to timeout the display after a short
1042a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // timeout (5s). This ensures that the screen goes to sleep
1043a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // as soon as acceptably possible after we the wake lock
1044a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // has been released.
1045a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                pokeLockSetting |= LocalPowerManager.POKE_LOCK_SHORT_TIMEOUT;
1046a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                break;
1047a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1048a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            case MEDIUM:
1049a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // Set the poke lock to timeout the display after a medium
1050a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // timeout (15s). This ensures that the screen goes to sleep
1051a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // as soon as acceptably possible after we the wake lock
1052a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // has been released.
1053a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                pokeLockSetting |= LocalPowerManager.POKE_LOCK_MEDIUM_TIMEOUT;
1054a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                break;
1055a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1056a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            case DEFAULT:
1057a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            default:
1058a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // set the poke lock to timeout the display after a long
1059a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // delay by default.
1060a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // TODO: it may be nice to be able to disable cheek presses
1061a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // for long poke locks (emergency dialer, for instance).
1062a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                break;
1063a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1064a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1065a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (mIgnoreTouchUserActivity) {
1066a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            pokeLockSetting |= LocalPowerManager.POKE_LOCK_IGNORE_TOUCH_EVENTS;
1067a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1068a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1069a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // Send the request
1070a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        try {
1071a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            mPowerManagerService.setPokeLock(pokeLockSetting, mPokeLockToken, LOG_TAG);
1072a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        } catch (RemoteException e) {
1073a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            Log.w(LOG_TAG, "mPowerManagerService.setPokeLock() failed: " + e);
1074a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1075a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1076a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1077a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
1078a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Controls whether or not the screen is allowed to sleep.
1079a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
1080a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Once sleep is allowed (WakeState is SLEEP), it will rely on the
1081a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * settings for the poke lock to determine when to timeout and let
1082a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * the device sleep {@link PhoneGlobals#setScreenTimeout}.
1083a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
1084a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * @param ws tells the device to how to wake.
1085a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
1086a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /* package */ void requestWakeState(WakeState ws) {
1087a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (VDBG) Log.d(LOG_TAG, "requestWakeState(" + ws + ")...");
1088a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        synchronized (this) {
1089a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (mWakeState != ws) {
1090a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                switch (ws) {
1091a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    case PARTIAL:
1092a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        // acquire the processor wake lock, and release the FULL
1093a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        // lock if it is being held.
1094a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        mPartialWakeLock.acquire();
1095a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        if (mWakeLock.isHeld()) {
1096a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                            mWakeLock.release();
1097a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        }
1098a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        break;
1099a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    case FULL:
1100a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        // acquire the full wake lock, and release the PARTIAL
1101a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        // lock if it is being held.
1102a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        mWakeLock.acquire();
1103a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        if (mPartialWakeLock.isHeld()) {
1104a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                            mPartialWakeLock.release();
1105a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        }
1106a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        break;
1107a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    case SLEEP:
1108a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    default:
1109a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        // release both the PARTIAL and FULL locks.
1110a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        if (mWakeLock.isHeld()) {
1111a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                            mWakeLock.release();
1112a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        }
1113a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        if (mPartialWakeLock.isHeld()) {
1114a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                            mPartialWakeLock.release();
1115a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        }
1116a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        break;
1117a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                }
1118a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                mWakeState = ws;
1119a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            }
1120a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1121a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1122a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1123a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
1124a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * If we are not currently keeping the screen on, then poke the power
1125a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * manager to wake up the screen for the user activity timeout duration.
1126a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
1127a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /* package */ void wakeUpScreen() {
1128a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        synchronized (this) {
1129a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (mWakeState == WakeState.SLEEP) {
1130a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                if (DBG) Log.d(LOG_TAG, "pulse screen lock");
1131a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                mPowerManager.wakeUp(SystemClock.uptimeMillis());
1132a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            }
1133a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1134a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1135a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1136a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
1137a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Sets the wake state and screen timeout based on the current state
1138a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * of the phone, and the current state of the in-call UI.
1139a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
1140a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * This method is a "UI Policy" wrapper around
1141a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * {@link PhoneGlobals#requestWakeState} and {@link PhoneGlobals#setScreenTimeout}.
1142a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
1143a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * It's safe to call this method regardless of the state of the Phone
1144a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * (e.g. whether or not it's idle), and regardless of the state of the
1145a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Phone UI (e.g. whether or not the InCallScreen is active.)
1146a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
1147a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /* package */ void updateWakeState() {
1148a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        PhoneConstants.State state = mCM.getState();
1149a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1150a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // True if the in-call UI is the foreground activity.
1151a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // (Note this will be false if the screen is currently off,
1152a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // since in that case *no* activity is in the foreground.)
1153a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        boolean isShowingCallScreen = isShowingCallScreen();
1154a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1155a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // True if the InCallScreen's DTMF dialer is currently opened.
1156a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // (Note this does NOT imply whether or not the InCallScreen
1157a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // itself is visible.)
1158a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        boolean isDialerOpened = (mInCallScreen != null) && mInCallScreen.isDialerOpened();
1159a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1160a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // True if the speakerphone is in use.  (If so, we *always* use
1161a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // the default timeout.  Since the user is obviously not holding
1162a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // the phone up to his/her face, we don't need to worry about
1163a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // false touches, and thus don't need to turn the screen off so
1164a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // aggressively.)
1165a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // Note that we need to make a fresh call to this method any
1166a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // time the speaker state changes.  (That happens in
1167a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // PhoneUtils.turnOnSpeaker().)
1168a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        boolean isSpeakerInUse = (state == PhoneConstants.State.OFFHOOK) && PhoneUtils.isSpeakerOn(this);
1169a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1170a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // TODO (bug 1440854): The screen timeout *might* also need to
1171a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // depend on the bluetooth state, but this isn't as clear-cut as
1172a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // the speaker state (since while using BT it's common for the
1173a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // user to put the phone straight into a pocket, in which case the
1174a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // timeout should probably still be short.)
1175a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1176a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (DBG) Log.d(LOG_TAG, "updateWakeState: callscreen " + isShowingCallScreen
1177a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                       + ", dialer " + isDialerOpened
1178a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                       + ", speaker " + isSpeakerInUse + "...");
1179a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1180a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        //
1181a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // (1) Set the screen timeout.
1182a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        //
1183a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // Note that the "screen timeout" value we determine here is
1184a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // meaningless if the screen is forced on (see (2) below.)
1185a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        //
1186a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1187a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // Historical note: In froyo and earlier, we checked here for a special
1188a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // case: the in-call UI being active, the speaker off, and the DTMF dialpad
1189a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // not visible.  In that case, with no touchable UI onscreen at all (for
1190a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // non-prox-sensor devices at least), we could assume the user was probably
1191a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // holding the phone up to their face and *not* actually looking at the
1192a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // screen.  So we'd switch to a special screen timeout value
1193a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // (ScreenTimeoutDuration.MEDIUM), purely to save battery life.
1194a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        //
1195a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // On current devices, we can rely on the proximity sensor to turn the
1196a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // screen off in this case, so we use the system-wide default timeout
1197a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // unconditionally.
1198a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        setScreenTimeout(ScreenTimeoutDuration.DEFAULT);
1199a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1200a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        //
1201a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // (2) Decide whether to force the screen on or not.
1202a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        //
1203a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // Force the screen to be on if the phone is ringing or dialing,
1204a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // or if we're displaying the "Call ended" UI for a connection in
1205a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // the "disconnected" state.
1206a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // However, if the phone is disconnected while the user is in the
1207a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // middle of selecting a quick response message, we should not force
1208a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // the screen to be on.
1209a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        //
1210a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        boolean isRinging = (state == PhoneConstants.State.RINGING);
1211a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        boolean isDialing = (phone.getForegroundCall().getState() == Call.State.DIALING);
1212a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        boolean showingQuickResponseDialog = (mInCallScreen != null) &&
1213a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                mInCallScreen.isQuickResponseDialogShowing();
1214a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        boolean showingDisconnectedConnection =
1215a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                PhoneUtils.hasDisconnectedConnections(phone) && isShowingCallScreen;
1216a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        boolean keepScreenOn = isRinging || isDialing ||
1217a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                (showingDisconnectedConnection && !showingQuickResponseDialog);
1218a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (DBG) Log.d(LOG_TAG, "updateWakeState: keepScreenOn = " + keepScreenOn
1219a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                       + " (isRinging " + isRinging
1220a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                       + ", isDialing " + isDialing
1221a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                       + ", showingQuickResponse " + showingQuickResponseDialog
1222a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                       + ", showingDisc " + showingDisconnectedConnection + ")");
1223a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // keepScreenOn == true means we'll hold a full wake lock:
1224a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        requestWakeState(keepScreenOn ? WakeState.FULL : WakeState.SLEEP);
1225a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1226a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1227a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
1228a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Wrapper around the PowerManagerService.preventScreenOn() API.
1229a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * This allows the in-call UI to prevent the screen from turning on
1230a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * even if a subsequent call to updateWakeState() causes us to acquire
1231a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * a full wake lock.
1232a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
1233a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /* package */ void preventScreenOn(boolean prevent) {
1234a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (VDBG) Log.d(LOG_TAG, "- preventScreenOn(" + prevent + ")...");
1235a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        try {
1236a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            mPowerManagerService.preventScreenOn(prevent);
1237a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        } catch (RemoteException e) {
1238a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            Log.w(LOG_TAG, "mPowerManagerService.preventScreenOn() failed: " + e);
1239a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1240a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1241a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1242a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
1243a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Sets or clears the flag that tells the PowerManager that touch
1244a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * (and cheek) events should NOT be considered "user activity".
1245a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
1246a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * This method is no-op when proximity sensor is available on the device.
1247a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
1248a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Since the in-call UI is totally insensitive to touch in most
1249a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * states, we set this flag whenever the InCallScreen is in the
1250a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * foreground.  (Otherwise, repeated unintentional touches could
1251a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * prevent the device from going to sleep.)
1252a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
1253a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * There *are* some some touch events that really do count as user
1254a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * activity, though.  For those, we need to manually poke the
1255a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * PowerManager's userActivity method; see pokeUserActivity().
1256a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
1257a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /* package */ void setIgnoreTouchUserActivity(boolean ignore) {
1258a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (VDBG) Log.d(LOG_TAG, "setIgnoreTouchUserActivity(" + ignore + ")...");
1259a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // stick with default timeout if we are using the proximity sensor
1260a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (proximitySensorModeEnabled()) {
1261a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            return;
1262a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1263a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1264a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        mIgnoreTouchUserActivity = ignore;
1265a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        updatePokeLock();
1266a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1267a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1268a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
1269a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Manually pokes the PowerManager's userActivity method.  Since we
1270a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * hold the POKE_LOCK_IGNORE_TOUCH_EVENTS poke lock while
1271a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * the InCallScreen is active, we need to do this for touch events
1272a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * that really do count as user activity (like pressing any
1273a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * onscreen UI elements.)
1274a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
1275a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /* package */ void pokeUserActivity() {
1276a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (VDBG) Log.d(LOG_TAG, "pokeUserActivity()...");
1277a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        mPowerManager.userActivity(SystemClock.uptimeMillis(), false);
1278a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1279a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1280a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
1281a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Set when a new outgoing call is beginning, so we can update
1282a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * the proximity sensor state.
1283a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Cleared when the InCallScreen is no longer in the foreground,
1284a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * in case the call fails without changing the telephony state.
1285a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
1286a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /* package */ void setBeginningCall(boolean beginning) {
1287a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // Note that we are beginning a new call, for proximity sensor support
1288a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        mBeginningCall = beginning;
1289a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // Update the Proximity sensor based on mBeginningCall state
1290a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        updateProximitySensorMode(mCM.getState());
1291a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1292a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1293a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
1294a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Updates the wake lock used to control proximity sensor behavior,
1295a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * based on the current state of the phone.  This method is called
1296a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * from the CallNotifier on any phone state change.
1297a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
1298a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * On devices that have a proximity sensor, to avoid false touches
1299a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * during a call, we hold a PROXIMITY_SCREEN_OFF_WAKE_LOCK wake lock
1300a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * whenever the phone is off hook.  (When held, that wake lock causes
1301a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * the screen to turn off automatically when the sensor detects an
1302a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * object close to the screen.)
1303a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
1304a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * This method is a no-op for devices that don't have a proximity
1305a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * sensor.
1306a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
1307a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Note this method doesn't care if the InCallScreen is the foreground
1308a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * activity or not.  That's because we want the proximity sensor to be
1309a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * enabled any time the phone is in use, to avoid false cheek events
1310a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * for whatever app you happen to be running.
1311a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
1312a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Proximity wake lock will *not* be held if any one of the
1313a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * conditions is true while on a call:
1314a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * 1) If the audio is routed via Bluetooth
1315a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * 2) If a wired headset is connected
1316a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * 3) if the speaker is ON
1317a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * 4) If the slider is open(i.e. the hardkeyboard is *not* hidden)
1318a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
1319a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * @param state current state of the phone (see {@link Phone#State})
1320a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
1321a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /* package */ void updateProximitySensorMode(PhoneConstants.State state) {
1322a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (VDBG) Log.d(LOG_TAG, "updateProximitySensorMode: state = " + state);
1323a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1324a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (proximitySensorModeEnabled()) {
1325a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            synchronized (mProximityWakeLock) {
1326a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // turn proximity sensor off and turn screen on immediately if
1327a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // we are using a headset, the keyboard is open, or the device
1328a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // is being held in a horizontal position.
1329a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                boolean screenOnImmediately = (isHeadsetPlugged()
1330a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                                               || PhoneUtils.isSpeakerOn(this)
1331a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                                               || isBluetoothHeadsetAudioOn()
1332a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                                               || mIsHardKeyboardOpen);
1333a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1334a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // We do not keep the screen off when the user is outside in-call screen and we are
1335a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // horizontal, but we do not force it on when we become horizontal until the
1336a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // proximity sensor goes negative.
1337a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                boolean horizontal =
1338a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        (mOrientation == AccelerometerListener.ORIENTATION_HORIZONTAL);
1339a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                screenOnImmediately |= !isShowingCallScreenForProximity() && horizontal;
1340a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1341a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // We do not keep the screen off when dialpad is visible, we are horizontal, and
1342a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // the in-call screen is being shown.
1343a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // At that moment we're pretty sure users want to use it, instead of letting the
1344a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // proximity sensor turn off the screen by their hands.
1345a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                boolean dialpadVisible = false;
1346a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                if (mInCallScreen != null) {
1347a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    dialpadVisible =
1348a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                            mInCallScreen.getUpdatedInCallControlState().dialpadEnabled
1349a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                            && mInCallScreen.getUpdatedInCallControlState().dialpadVisible
1350a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                            && isShowingCallScreen();
1351a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                }
1352a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                screenOnImmediately |= dialpadVisible && horizontal;
1353a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1354a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                if (((state == PhoneConstants.State.OFFHOOK) || mBeginningCall) && !screenOnImmediately) {
1355a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // Phone is in use!  Arrange for the screen to turn off
1356a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // automatically when the sensor detects a close object.
1357a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    if (!mProximityWakeLock.isHeld()) {
1358a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        if (DBG) Log.d(LOG_TAG, "updateProximitySensorMode: acquiring...");
1359a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        mProximityWakeLock.acquire();
1360a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    } else {
1361a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        if (VDBG) Log.d(LOG_TAG, "updateProximitySensorMode: lock already held.");
1362a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    }
1363a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                } else {
1364a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // Phone is either idle, or ringing.  We don't want any
1365a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // special proximity sensor behavior in either case.
1366a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    if (mProximityWakeLock.isHeld()) {
1367a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        if (DBG) Log.d(LOG_TAG, "updateProximitySensorMode: releasing...");
1368a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        // Wait until user has moved the phone away from his head if we are
1369a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        // releasing due to the phone call ending.
1370a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        // Qtherwise, turn screen on immediately
1371a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        int flags =
1372a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                            (screenOnImmediately ? 0 : PowerManager.WAIT_FOR_PROXIMITY_NEGATIVE);
1373a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        mProximityWakeLock.release(flags);
1374a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    } else {
1375a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        if (VDBG) {
1376a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                            Log.d(LOG_TAG, "updateProximitySensorMode: lock already released.");
1377a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        }
1378a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    }
1379a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                }
1380a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            }
1381a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1382a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1383a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1384a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    @Override
1385a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    public void orientationChanged(int orientation) {
1386a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        mOrientation = orientation;
1387a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        updateProximitySensorMode(mCM.getState());
1388a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1389a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1390a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
1391a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Notifies the phone app when the phone state changes.
1392a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
1393a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * This method will updates various states inside Phone app (e.g. proximity sensor mode,
1394a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * accelerometer listener state, update-lock state, etc.)
1395a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
1396a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /* package */ void updatePhoneState(PhoneConstants.State state) {
1397a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (state != mLastPhoneState) {
1398a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            mLastPhoneState = state;
1399a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            updateProximitySensorMode(state);
1400a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1401a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // Try to acquire or release UpdateLock.
1402a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            //
1403a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // Watch out: we don't release the lock here when the screen is still in foreground.
1404a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // At that time InCallScreen will release it on onPause().
1405a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (state != PhoneConstants.State.IDLE) {
1406a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // UpdateLock is a recursive lock, while we may get "acquire" request twice and
1407a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // "release" request once for a single call (RINGING + OFFHOOK and IDLE).
1408a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // We need to manually ensure the lock is just acquired once for each (and this
1409a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // will prevent other possible buggy situations too).
1410a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                if (!mUpdateLock.isHeld()) {
1411a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    mUpdateLock.acquire();
1412a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                }
1413a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            } else {
1414a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                if (!isShowingCallScreen()) {
1415a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    if (!mUpdateLock.isHeld()) {
1416a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        mUpdateLock.release();
1417a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    }
1418a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                } else {
1419a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // For this case InCallScreen will take care of the release() call.
1420a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                }
1421a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            }
1422a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1423a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (mAccelerometerListener != null) {
1424a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // use accelerometer to augment proximity sensor when in call
1425a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                mOrientation = AccelerometerListener.ORIENTATION_UNKNOWN;
1426a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                mAccelerometerListener.enable(state == PhoneConstants.State.OFFHOOK);
1427a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            }
1428a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // clear our beginning call flag
1429a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            mBeginningCall = false;
1430a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // While we are in call, the in-call screen should dismiss the keyguard.
1431a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // This allows the user to press Home to go directly home without going through
1432a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // an insecure lock screen.
1433a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // But we do not want to do this if there is no active call so we do not
1434a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // bypass the keyguard if the call is not answered or declined.
1435a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (mInCallScreen != null) {
1436a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                mInCallScreen.updateKeyguardPolicy(state == PhoneConstants.State.OFFHOOK);
1437a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            }
1438a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1439a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1440a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1441a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /* package */ PhoneConstants.State getPhoneState() {
1442a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        return mLastPhoneState;
1443a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1444a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1445a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
1446a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Returns UpdateLock object.
1447a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
1448a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /* package */ UpdateLock getUpdateLock() {
1449a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        return mUpdateLock;
1450a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1451a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1452a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
1453a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * @return true if this device supports the "proximity sensor
1454a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * auto-lock" feature while in-call (see updateProximitySensorMode()).
1455a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
1456a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /* package */ boolean proximitySensorModeEnabled() {
1457a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        return (mProximityWakeLock != null);
1458a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1459a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1460a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    KeyguardManager getKeyguardManager() {
1461a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        return mKeyguardManager;
1462a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1463a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1464a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private void onMMIComplete(AsyncResult r) {
1465a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (VDBG) Log.d(LOG_TAG, "onMMIComplete()...");
1466a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        MmiCode mmiCode = (MmiCode) r.result;
1467a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        PhoneUtils.displayMMIComplete(phone, getInstance(), mmiCode, null, null);
1468a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1469a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1470a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private void initForNewRadioTechnology() {
1471a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (DBG) Log.d(LOG_TAG, "initForNewRadioTechnology...");
1472a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1473a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn         if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1474a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // Create an instance of CdmaPhoneCallState and initialize it to IDLE
1475a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            cdmaPhoneCallState = new CdmaPhoneCallState();
1476a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            cdmaPhoneCallState.CdmaPhoneCallStateInit();
1477a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1478a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (TelephonyCapabilities.supportsOtasp(phone)) {
1479a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            //create instances of CDMA OTA data classes
1480a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (cdmaOtaProvisionData == null) {
1481a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                cdmaOtaProvisionData = new OtaUtils.CdmaOtaProvisionData();
1482a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            }
1483a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (cdmaOtaConfigData == null) {
1484a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                cdmaOtaConfigData = new OtaUtils.CdmaOtaConfigData();
1485a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            }
1486a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (cdmaOtaScreenState == null) {
1487a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                cdmaOtaScreenState = new OtaUtils.CdmaOtaScreenState();
1488a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            }
1489a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (cdmaOtaInCallScreenUiState == null) {
1490a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                cdmaOtaInCallScreenUiState = new OtaUtils.CdmaOtaInCallScreenUiState();
1491a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            }
1492a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        } else {
1493a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            //Clean up OTA data in GSM/UMTS. It is valid only for CDMA
1494a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            clearOtaState();
1495a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1496a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1497a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        ringer.updateRingerContextAfterRadioTechnologyChange(this.phone);
1498a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        notifier.updateCallNotifierRegistrationsAfterRadioTechnologyChange();
1499a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (mBluetoothPhone != null) {
1500a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            try {
1501a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                mBluetoothPhone.updateBtHandsfreeAfterRadioTechnologyChange();
1502a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            } catch (RemoteException e) {
1503a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                Log.e(LOG_TAG, Log.getStackTraceString(new Throwable()));
1504a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            }
1505a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1506a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (mInCallScreen != null) {
1507a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            mInCallScreen.updateAfterRadioTechnologyChange();
1508a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1509a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1510a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // Update registration for ICC status after radio technology change
1511a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        IccCard sim = phone.getIccCard();
1512a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (sim != null) {
1513a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (DBG) Log.d(LOG_TAG, "Update registration for ICC status...");
1514a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1515a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            //Register all events new to the new active phone
1516a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            sim.registerForNetworkLocked(mHandler, EVENT_SIM_NETWORK_LOCKED, null);
1517a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1518a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1519a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1520a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1521a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
1522a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * @return true if a wired headset is currently plugged in.
1523a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
1524a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * @see Intent.ACTION_HEADSET_PLUG (which we listen for in mReceiver.onReceive())
1525a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
1526a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    boolean isHeadsetPlugged() {
1527a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        return mIsHeadsetPlugged;
1528a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1529a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1530a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
1531a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * @return true if the onscreen UI should currently be showing the
1532a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * special "bluetooth is active" indication in a couple of places (in
1533a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * which UI elements turn blue and/or show the bluetooth logo.)
1534a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
1535a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * This depends on the BluetoothHeadset state *and* the current
1536a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * telephony state; see shouldShowBluetoothIndication().
1537a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
1538a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * @see CallCard
1539a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * @see NotificationMgr.updateInCallNotification
1540a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
1541a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /* package */ boolean showBluetoothIndication() {
1542a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        return mShowBluetoothIndication;
1543a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1544a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1545a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
1546a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Recomputes the mShowBluetoothIndication flag based on the current
1547a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * bluetooth state and current telephony state.
1548a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
1549a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * This needs to be called any time the bluetooth headset state or the
1550a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * telephony state changes.
1551a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
1552a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * @param forceUiUpdate if true, force the UI elements that care
1553a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *                      about this flag to update themselves.
1554a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
1555a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /* package */ void updateBluetoothIndication(boolean forceUiUpdate) {
1556a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        mShowBluetoothIndication = shouldShowBluetoothIndication(mBluetoothHeadsetState,
1557a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                                                                 mBluetoothHeadsetAudioState,
1558a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                                                                 mCM);
1559a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (forceUiUpdate) {
1560a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // Post Handler messages to the various components that might
1561a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // need to be refreshed based on the new state.
1562a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (isShowingCallScreen()) mInCallScreen.requestUpdateBluetoothIndication();
1563a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (DBG) Log.d (LOG_TAG, "- updating in-call notification for BT state change...");
1564a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            mHandler.sendEmptyMessage(EVENT_UPDATE_INCALL_NOTIFICATION);
1565a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1566a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1567a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // Update the Proximity sensor based on Bluetooth audio state
1568a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        updateProximitySensorMode(mCM.getState());
1569a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1570a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1571a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
1572a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * UI policy helper function for the couple of places in the UI that
1573a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * have some way of indicating that "bluetooth is in use."
1574a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
1575a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * @return true if the onscreen UI should indicate that "bluetooth is in use",
1576a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *         based on the specified bluetooth headset state, and the
1577a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *         current state of the phone.
1578a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * @see showBluetoothIndication()
1579a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
1580a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private static boolean shouldShowBluetoothIndication(int bluetoothState,
1581a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                                                         int bluetoothAudioState,
1582a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                                                         CallManager cm) {
1583a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // We want the UI to indicate that "bluetooth is in use" in two
1584a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // slightly different cases:
1585a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        //
1586a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // (a) The obvious case: if a bluetooth headset is currently in
1587a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        //     use for an ongoing call.
1588a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        //
1589a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // (b) The not-so-obvious case: if an incoming call is ringing,
1590a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        //     and we expect that audio *will* be routed to a bluetooth
1591a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        //     headset once the call is answered.
1592a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1593a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        switch (cm.getState()) {
1594a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            case OFFHOOK:
1595a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // This covers normal active calls, and also the case if
1596a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // the foreground call is DIALING or ALERTING.  In this
1597a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // case, bluetooth is considered "active" if a headset
1598a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // is connected *and* audio is being routed to it.
1599a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                return ((bluetoothState == BluetoothHeadset.STATE_CONNECTED)
1600a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        && (bluetoothAudioState == BluetoothHeadset.STATE_AUDIO_CONNECTED));
1601a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1602a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            case RINGING:
1603a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // If an incoming call is ringing, we're *not* yet routing
1604a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // audio to the headset (since there's no in-call audio
1605a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // yet!)  In this case, if a bluetooth headset is
1606a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // connected at all, we assume that it'll become active
1607a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // once the user answers the phone.
1608a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                return (bluetoothState == BluetoothHeadset.STATE_CONNECTED);
1609a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1610a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            default:  // Presumably IDLE
1611a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                return false;
1612a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1613a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1614a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1615a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1616a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
1617a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Receiver for misc intent broadcasts the Phone app cares about.
1618a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
1619a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private class PhoneAppBroadcastReceiver extends BroadcastReceiver {
1620a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        @Override
1621a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        public void onReceive(Context context, Intent intent) {
1622a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            String action = intent.getAction();
1623a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (action.equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) {
1624a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                boolean enabled = System.getInt(getContentResolver(),
1625a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        System.AIRPLANE_MODE_ON, 0) == 0;
1626a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                phone.setRadioPower(enabled);
1627a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            } else if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) {
1628a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                mBluetoothHeadsetState = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE,
1629a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                                                          BluetoothHeadset.STATE_DISCONNECTED);
1630a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                if (VDBG) Log.d(LOG_TAG, "mReceiver: HEADSET_STATE_CHANGED_ACTION");
1631a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                if (VDBG) Log.d(LOG_TAG, "==> new state: " + mBluetoothHeadsetState);
1632a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                updateBluetoothIndication(true);  // Also update any visible UI if necessary
1633a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            } else if (action.equals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)) {
1634a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                mBluetoothHeadsetAudioState =
1635a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        intent.getIntExtra(BluetoothHeadset.EXTRA_STATE,
1636a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                                           BluetoothHeadset.STATE_AUDIO_DISCONNECTED);
1637a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                if (VDBG) Log.d(LOG_TAG, "mReceiver: HEADSET_AUDIO_STATE_CHANGED_ACTION");
1638a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                if (VDBG) Log.d(LOG_TAG, "==> new state: " + mBluetoothHeadsetAudioState);
1639a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                updateBluetoothIndication(true);  // Also update any visible UI if necessary
1640a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            } else if (action.equals(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED)) {
1641a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                if (VDBG) Log.d(LOG_TAG, "mReceiver: ACTION_ANY_DATA_CONNECTION_STATE_CHANGED");
1642a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                if (VDBG) Log.d(LOG_TAG, "- state: " + intent.getStringExtra(PhoneConstants.STATE_KEY));
1643a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                if (VDBG) Log.d(LOG_TAG, "- reason: "
1644a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                                + intent.getStringExtra(PhoneConstants.STATE_CHANGE_REASON_KEY));
1645a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1646a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // The "data disconnected due to roaming" notification is shown
1647a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // if (a) you have the "data roaming" feature turned off, and
1648a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // (b) you just lost data connectivity because you're roaming.
1649a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                boolean disconnectedDueToRoaming =
1650a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        !phone.getDataRoamingEnabled()
1651a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        && "DISCONNECTED".equals(intent.getStringExtra(PhoneConstants.STATE_KEY))
1652a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        && Phone.REASON_ROAMING_ON.equals(
1653a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                            intent.getStringExtra(PhoneConstants.STATE_CHANGE_REASON_KEY));
1654a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                mHandler.sendEmptyMessage(disconnectedDueToRoaming
1655a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                                          ? EVENT_DATA_ROAMING_DISCONNECTED
1656a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                                          : EVENT_DATA_ROAMING_OK);
1657a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            } else if (action.equals(Intent.ACTION_HEADSET_PLUG)) {
1658a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                if (VDBG) Log.d(LOG_TAG, "mReceiver: ACTION_HEADSET_PLUG");
1659a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                if (VDBG) Log.d(LOG_TAG, "    state: " + intent.getIntExtra("state", 0));
1660a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                if (VDBG) Log.d(LOG_TAG, "    name: " + intent.getStringExtra("name"));
1661a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                mIsHeadsetPlugged = (intent.getIntExtra("state", 0) == 1);
1662a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                mHandler.sendMessage(mHandler.obtainMessage(EVENT_WIRED_HEADSET_PLUG, 0));
1663a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            } else if ((action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) &&
1664a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    (mPUKEntryActivity != null)) {
1665a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // if an attempt to un-PUK-lock the device was made, while we're
1666a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // receiving this state change notification, notify the handler.
1667a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // NOTE: This is ONLY triggered if an attempt to un-PUK-lock has
1668a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // been attempted.
1669a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                mHandler.sendMessage(mHandler.obtainMessage(EVENT_SIM_STATE_CHANGED,
1670a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE)));
1671a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            } else if (action.equals(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED)) {
1672a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                String newPhone = intent.getStringExtra(PhoneConstants.PHONE_NAME_KEY);
1673a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                Log.d(LOG_TAG, "Radio technology switched. Now " + newPhone + " is active.");
1674a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                initForNewRadioTechnology();
1675a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            } else if (action.equals(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED)) {
1676a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                handleServiceStateChanged(intent);
1677a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) {
1678a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                if (TelephonyCapabilities.supportsEcm(phone)) {
1679a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    Log.d(LOG_TAG, "Emergency Callback Mode arrived in PhoneApp.");
1680a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // Start Emergency Callback Mode service
1681a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    if (intent.getBooleanExtra("phoneinECMState", false)) {
1682a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        context.startService(new Intent(context,
1683a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                                EmergencyCallbackModeService.class));
1684a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    }
1685a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                } else {
1686a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // It doesn't make sense to get ACTION_EMERGENCY_CALLBACK_MODE_CHANGED
1687a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // on a device that doesn't support ECM in the first place.
1688a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    Log.e(LOG_TAG, "Got ACTION_EMERGENCY_CALLBACK_MODE_CHANGED, "
1689a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                          + "but ECM isn't supported for phone: " + phone.getPhoneName());
1690a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                }
1691a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            } else if (action.equals(Intent.ACTION_DOCK_EVENT)) {
1692a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                mDockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
1693a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        Intent.EXTRA_DOCK_STATE_UNDOCKED);
1694a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                if (VDBG) Log.d(LOG_TAG, "ACTION_DOCK_EVENT -> mDockState = " + mDockState);
1695a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                mHandler.sendMessage(mHandler.obtainMessage(EVENT_DOCK_STATE_CHANGED, 0));
1696a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            } else if (action.equals(TtyIntent.TTY_PREFERRED_MODE_CHANGE_ACTION)) {
1697a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                mPreferredTtyMode = intent.getIntExtra(TtyIntent.TTY_PREFFERED_MODE,
1698a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                                                       Phone.TTY_MODE_OFF);
1699a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                if (VDBG) Log.d(LOG_TAG, "mReceiver: TTY_PREFERRED_MODE_CHANGE_ACTION");
1700a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                if (VDBG) Log.d(LOG_TAG, "    mode: " + mPreferredTtyMode);
1701a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                mHandler.sendMessage(mHandler.obtainMessage(EVENT_TTY_PREFERRED_MODE_CHANGED, 0));
1702a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            } else if (action.equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1703a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                int ringerMode = intent.getIntExtra(AudioManager.EXTRA_RINGER_MODE,
1704a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        AudioManager.RINGER_MODE_NORMAL);
1705a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                if (ringerMode == AudioManager.RINGER_MODE_SILENT) {
1706a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    notifier.silenceRinger();
1707a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                }
1708a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            }
1709a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1710a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1711a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1712a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
1713a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Broadcast receiver for the ACTION_MEDIA_BUTTON broadcast intent.
1714a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
1715a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * This functionality isn't lumped in with the other intents in
1716a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * PhoneAppBroadcastReceiver because we instantiate this as a totally
1717a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * separate BroadcastReceiver instance, since we need to manually
1718a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * adjust its IntentFilter's priority (to make sure we get these
1719a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * intents *before* the media player.)
1720a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
1721a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private class MediaButtonBroadcastReceiver extends BroadcastReceiver {
1722a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        @Override
1723a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        public void onReceive(Context context, Intent intent) {
1724a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
1725a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (VDBG) Log.d(LOG_TAG,
1726a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                           "MediaButtonBroadcastReceiver.onReceive()...  event = " + event);
1727a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if ((event != null)
1728a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                && (event.getKeyCode() == KeyEvent.KEYCODE_HEADSETHOOK)) {
1729a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                if (VDBG) Log.d(LOG_TAG, "MediaButtonBroadcastReceiver: HEADSETHOOK");
1730a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                boolean consumed = PhoneUtils.handleHeadsetHook(phone, event);
1731a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                if (VDBG) Log.d(LOG_TAG, "==> handleHeadsetHook(): consumed = " + consumed);
1732a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                if (consumed) {
1733a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // If a headset is attached and the press is consumed, also update
1734a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // any UI items (such as an InCallScreen mute button) that may need to
1735a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // be updated if their state changed.
1736a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    updateInCallScreen();  // Has no effect if the InCallScreen isn't visible
1737a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    abortBroadcast();
1738a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                }
1739a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            } else {
1740a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                if (mCM.getState() != PhoneConstants.State.IDLE) {
1741a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // If the phone is anything other than completely idle,
1742a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // then we consume and ignore any media key events,
1743a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // Otherwise it is too easy to accidentally start
1744a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    // playing music while a phone call is in progress.
1745a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    if (VDBG) Log.d(LOG_TAG, "MediaButtonBroadcastReceiver: consumed");
1746a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    abortBroadcast();
1747a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                }
1748a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            }
1749a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1750a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1751a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1752a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
1753a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Accepts broadcast Intents which will be prepared by {@link NotificationMgr} and thus
1754a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * sent from framework's notification mechanism (which is outside Phone context).
1755a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * This should be visible from outside, but shouldn't be in "exported" state.
1756a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
1757a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * TODO: If possible merge this into PhoneAppBroadcastReceiver.
1758a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
1759a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    public static class NotificationBroadcastReceiver extends BroadcastReceiver {
1760a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        @Override
1761a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        public void onReceive(Context context, Intent intent) {
1762a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            String action = intent.getAction();
1763a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // TODO: use "if (VDBG)" here.
1764a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            Log.d(LOG_TAG, "Broadcast from Notification: " + action);
1765a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1766a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (action.equals(ACTION_HANG_UP_ONGOING_CALL)) {
1767a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                PhoneUtils.hangup(PhoneGlobals.getInstance().mCM);
1768a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            } else if (action.equals(ACTION_CALL_BACK_FROM_NOTIFICATION)) {
1769a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // Collapse the expanded notification and the notification item itself.
1770a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                closeSystemDialogs(context);
1771a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                clearMissedCallNotification(context);
1772a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1773a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED, intent.getData());
1774a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1775a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1776a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                context.startActivity(callIntent);
1777a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            } else if (action.equals(ACTION_SEND_SMS_FROM_NOTIFICATION)) {
1778a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                // Collapse the expanded notification and the notification item itself.
1779a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                closeSystemDialogs(context);
1780a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                clearMissedCallNotification(context);
1781a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1782a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                Intent smsIntent = new Intent(Intent.ACTION_SENDTO, intent.getData());
1783a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                smsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1784a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                context.startActivity(smsIntent);
1785a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            } else {
1786a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                Log.w(LOG_TAG, "Received hang-up request from notification,"
1787a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        + " but there's no call the system can hang up.");
1788a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            }
1789a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1790a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1791a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        private void closeSystemDialogs(Context context) {
1792a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
1793a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            context.sendBroadcastAsUser(intent, UserHandle.ALL);
1794a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1795a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1796a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        private void clearMissedCallNotification(Context context) {
1797a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            Intent clearIntent = new Intent(context, ClearMissedCallsService.class);
1798a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            clearIntent.setAction(ClearMissedCallsService.ACTION_CLEAR_MISSED_CALLS);
1799a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            context.startService(clearIntent);
1800a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1801a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1802a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1803a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private void handleServiceStateChanged(Intent intent) {
1804a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        /**
1805a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn         * This used to handle updating EriTextWidgetProvider this routine
1806a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn         * and and listening for ACTION_SERVICE_STATE_CHANGED intents could
1807a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn         * be removed. But leaving just in case it might be needed in the near
1808a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn         * future.
1809a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn         */
1810a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1811a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        // If service just returned, start sending out the queued messages
1812a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        ServiceState ss = ServiceState.newFromBundle(intent.getExtras());
1813a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1814a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (ss != null) {
1815a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            int state = ss.getState();
1816a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            notificationMgr.updateNetworkSelection(state);
1817a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1818a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1819a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1820a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    public boolean isOtaCallInActiveState() {
1821a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        boolean otaCallActive = false;
1822a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (mInCallScreen != null) {
1823a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            otaCallActive = mInCallScreen.isOtaCallInActiveState();
1824a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1825a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (VDBG) Log.d(LOG_TAG, "- isOtaCallInActiveState " + otaCallActive);
1826a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        return otaCallActive;
1827a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1828a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1829a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    public boolean isOtaCallInEndState() {
1830a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        boolean otaCallEnded = false;
1831a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (mInCallScreen != null) {
1832a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            otaCallEnded = mInCallScreen.isOtaCallInEndState();
1833a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1834a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (VDBG) Log.d(LOG_TAG, "- isOtaCallInEndState " + otaCallEnded);
1835a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        return otaCallEnded;
1836a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1837a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1838a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // it is safe to call clearOtaState() even if the InCallScreen isn't active
1839a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    public void clearOtaState() {
1840a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (DBG) Log.d(LOG_TAG, "- clearOtaState ...");
1841a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if ((mInCallScreen != null)
1842a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                && (otaUtils != null)) {
1843a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            otaUtils.cleanOtaScreen(true);
1844a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (DBG) Log.d(LOG_TAG, "  - clearOtaState clears OTA screen");
1845a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1846a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1847a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1848a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // it is safe to call dismissOtaDialogs() even if the InCallScreen isn't active
1849a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    public void dismissOtaDialogs() {
1850a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (DBG) Log.d(LOG_TAG, "- dismissOtaDialogs ...");
1851a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if ((mInCallScreen != null)
1852a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                && (otaUtils != null)) {
1853a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            otaUtils.dismissAllOtaDialogs();
1854a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (DBG) Log.d(LOG_TAG, "  - dismissOtaDialogs clears OTA dialogs");
1855a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1856a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1857a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1858a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    // it is safe to call clearInCallScreenMode() even if the InCallScreen isn't active
1859a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    public void clearInCallScreenMode() {
1860a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (DBG) Log.d(LOG_TAG, "- clearInCallScreenMode ...");
1861a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (mInCallScreen != null) {
1862a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            mInCallScreen.resetInCallScreenMode();
1863a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1864a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1865a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1866a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
1867a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Force the in-call UI to refresh itself, if it's currently visible.
1868a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
1869a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * This method can be used any time there's a state change anywhere in
1870a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * the phone app that needs to be reflected in the onscreen UI.
1871a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
1872a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Note that it's *not* necessary to manually refresh the in-call UI
1873a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * (via this method) for regular telephony state changes like
1874a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * DIALING -> ALERTING -> ACTIVE, since the InCallScreen already
1875a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * listens for those state changes itself.
1876a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
1877a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * This method does *not* force the in-call UI to come up if it's not
1878a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * already visible.  To do that, use displayCallScreen().
1879a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
1880a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /* package */ void updateInCallScreen() {
1881a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (DBG) Log.d(LOG_TAG, "- updateInCallScreen()...");
1882a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (mInCallScreen != null) {
1883a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // Post an updateScreen() request.  Note that the
1884a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // updateScreen() call will end up being a no-op if the
1885a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // InCallScreen isn't the foreground activity.
1886a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            mInCallScreen.requestUpdateScreen();
1887a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1888a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1889a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1890a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private void handleQueryTTYModeResponse(Message msg) {
1891a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        AsyncResult ar = (AsyncResult) msg.obj;
1892a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (ar.exception != null) {
1893a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (DBG) Log.d(LOG_TAG, "handleQueryTTYModeResponse: Error getting TTY state.");
1894a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        } else {
1895a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (DBG) Log.d(LOG_TAG,
1896a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                           "handleQueryTTYModeResponse: TTY enable state successfully queried.");
1897a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1898a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            int ttymode = ((int[]) ar.result)[0];
1899a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (DBG) Log.d(LOG_TAG, "handleQueryTTYModeResponse:ttymode=" + ttymode);
1900a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1901a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            Intent ttyModeChanged = new Intent(TtyIntent.TTY_ENABLED_CHANGE_ACTION);
1902a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            ttyModeChanged.putExtra("ttyEnabled", ttymode != Phone.TTY_MODE_OFF);
1903a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            sendBroadcastAsUser(ttyModeChanged, UserHandle.ALL);
1904a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1905a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            String audioTtyMode;
1906a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            switch (ttymode) {
1907a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            case Phone.TTY_MODE_FULL:
1908a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                audioTtyMode = "tty_full";
1909a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                break;
1910a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            case Phone.TTY_MODE_VCO:
1911a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                audioTtyMode = "tty_vco";
1912a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                break;
1913a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            case Phone.TTY_MODE_HCO:
1914a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                audioTtyMode = "tty_hco";
1915a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                break;
1916a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            case Phone.TTY_MODE_OFF:
1917a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            default:
1918a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                audioTtyMode = "tty_off";
1919a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                break;
1920a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            }
1921a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
1922a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            audioManager.setParameters("tty_mode="+audioTtyMode);
1923a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1924a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1925a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1926a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private void handleSetTTYModeResponse(Message msg) {
1927a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        AsyncResult ar = (AsyncResult) msg.obj;
1928a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1929a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (ar.exception != null) {
1930a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (DBG) Log.d (LOG_TAG,
1931a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    "handleSetTTYModeResponse: Error setting TTY mode, ar.exception"
1932a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    + ar.exception);
1933a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1934a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        phone.queryTTYMode(mHandler.obtainMessage(EVENT_TTY_MODE_GET));
1935a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1936a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1937a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /* package */ void clearUserActivityTimeout() {
1938a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        try {
1939a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            mPowerManagerService.clearUserActivityTimeout(SystemClock.uptimeMillis(),
1940a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    10*1000 /* 10 sec */);
1941a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        } catch (RemoteException ex) {
1942a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // System process is dead.
1943a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1944a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1945a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1946a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
1947a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * "Call origin" may be used by Contacts app to specify where the phone call comes from.
1948a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Currently, the only permitted value for this extra is {@link #ALLOWED_EXTRA_CALL_ORIGIN}.
1949a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Any other value will be ignored, to make sure that malicious apps can't trick the in-call
1950a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * UI into launching some random other app after a call ends.
1951a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
1952a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * TODO: make this more generic. Note that we should let the "origin" specify its package
1953a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * while we are now assuming it is "com.android.contacts"
1954a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
1955a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    public static final String EXTRA_CALL_ORIGIN = "com.android.phone.CALL_ORIGIN";
1956716a9d4202fb2813352857128d0c0fcc310a040fDianne Hackborn    private static final String DEFAULT_CALL_ORIGIN_PACKAGE = "com.android.dialer";
1957a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private static final String ALLOWED_EXTRA_CALL_ORIGIN =
1958716a9d4202fb2813352857128d0c0fcc310a040fDianne Hackborn            "com.android.dialer.DialtactsActivity";
1959a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
1960a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Used to determine if the preserved call origin is fresh enough.
1961a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
1962a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private static final long CALL_ORIGIN_EXPIRATION_MILLIS = 30 * 1000;
1963a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1964a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    public void setLatestActiveCallOrigin(String callOrigin) {
1965a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        inCallUiState.latestActiveCallOrigin = callOrigin;
1966a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (callOrigin != null) {
1967a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            inCallUiState.latestActiveCallOriginTimeStamp = SystemClock.elapsedRealtime();
1968a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        } else {
1969a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            inCallUiState.latestActiveCallOriginTimeStamp = 0;
1970a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1971a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
1972a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
1973a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
1974a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Reset call origin depending on its timestamp.
1975a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
1976a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * See if the current call origin preserved by the app is fresh enough or not. If it is,
1977a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * previous call origin will be used as is. If not, call origin will be reset.
1978a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     *
1979a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * This will be effective especially for 3rd party apps which want to bypass phone calls with
1980a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * their own telephone lines. In that case Phone app may finish the phone call once and make
1981a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * another for the external apps, which will drop call origin information in Intent.
1982a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * Even in that case we are sure the second phone call should be initiated just after the first
1983a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * phone call, so here we restore it from the previous information iff the second call is done
1984a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * fairly soon.
1985a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
1986a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    public void resetLatestActiveCallOrigin() {
1987a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        final long callOriginTimestamp = inCallUiState.latestActiveCallOriginTimeStamp;
1988a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        final long currentTimestamp = SystemClock.elapsedRealtime();
1989a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (VDBG) {
1990a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            Log.d(LOG_TAG, "currentTimeMillis: " + currentTimestamp
1991a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    + ", saved timestamp for call origin: " + callOriginTimestamp);
1992a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
1993a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (inCallUiState.latestActiveCallOriginTimeStamp > 0
1994a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                && (currentTimestamp - callOriginTimestamp < CALL_ORIGIN_EXPIRATION_MILLIS)) {
1995a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (VDBG) {
1996a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                Log.d(LOG_TAG, "Resume previous call origin (" +
1997a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                        inCallUiState.latestActiveCallOrigin + ")");
1998a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            }
1999a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // Do nothing toward call origin itself but update the timestamp just in case.
2000a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            inCallUiState.latestActiveCallOriginTimeStamp = currentTimestamp;
2001a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        } else {
2002a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (VDBG) Log.d(LOG_TAG, "Drop previous call origin and set the current one to null");
2003a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            setLatestActiveCallOrigin(null);
2004a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
2005a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
2006a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
2007a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /**
2008a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * @return Intent which will be used when in-call UI is shown and the phone call is hang up.
2009a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * By default CallLog screen will be introduced, but the destination may change depending on
2010a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     * its latest call origin state.
2011a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn     */
2012a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    public Intent createPhoneEndIntentUsingCallOrigin() {
2013a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        if (TextUtils.equals(inCallUiState.latestActiveCallOrigin, ALLOWED_EXTRA_CALL_ORIGIN)) {
2014a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (VDBG) Log.d(LOG_TAG, "Valid latestActiveCallOrigin("
2015a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    + inCallUiState.latestActiveCallOrigin + ") was found. "
2016a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    + "Go back to the previous screen.");
2017a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // Right now we just launch the Activity which launched in-call UI. Note that we're
2018716a9d4202fb2813352857128d0c0fcc310a040fDianne Hackborn            // assuming the origin is from "com.android.dialer", which may be incorrect in the
2019a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            // future.
2020a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            final Intent intent = new Intent();
2021a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            intent.setClassName(DEFAULT_CALL_ORIGIN_PACKAGE, inCallUiState.latestActiveCallOrigin);
2022a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            return intent;
2023a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        } else {
2024a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            if (VDBG) Log.d(LOG_TAG, "Current latestActiveCallOrigin ("
2025a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    + inCallUiState.latestActiveCallOrigin + ") is not valid. "
2026a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn                    + "Just use CallLog as a default destination.");
2027a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            return PhoneGlobals.createCallLogIntent();
2028a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
2029a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    }
2030a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
2031a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    /** Service connection */
2032a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    private final ServiceConnection mBluetoothPhoneConnection = new ServiceConnection() {
2033a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
2034a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        /** Handle the task of binding the local object to the service */
2035a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        public void onServiceConnected(ComponentName className, IBinder service) {
2036a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            Log.i(LOG_TAG, "Headset phone created, binding local service.");
2037a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            mBluetoothPhone = IBluetoothHeadsetPhone.Stub.asInterface(service);
2038a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
2039a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn
2040a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        /** Handle the task of cleaning up the local binding */
2041a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        public void onServiceDisconnected(ComponentName className) {
2042a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            Log.i(LOG_TAG, "Headset phone disconnected, cleaning local binding.");
2043a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn            mBluetoothPhone = null;
2044a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn        }
2045a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn    };
2046a1a9601840e50e18ff8ca4be9b888e592287577bDianne Hackborn}
2047