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