1b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project/*
2b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project * Copyright (C) 2006 The Android Open Source Project
3b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project *
4b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project * you may not use this file except in compliance with the License.
6b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project * You may obtain a copy of the License at
7b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project *
8b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project *
10b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project * See the License for the specific language governing permissions and
14b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project * limitations under the License.
15b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project */
16b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
17b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectpackage com.android.phone;
18b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
19b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.app.Activity;
20b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.app.Application;
21b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.app.KeyguardManager;
22b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.app.ProgressDialog;
23e22715b7e908074c33140eded331b01946026d4eMike Lockwoodimport android.app.StatusBarManager;
24cec56587c1d3c90014848d7adfe817bef9c325eaNick Pellyimport android.bluetooth.BluetoothAdapter;
25b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.bluetooth.BluetoothHeadset;
26b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.content.BroadcastReceiver;
27b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.content.ContentResolver;
28b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.content.Context;
29b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.content.Intent;
30b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.content.IntentFilter;
31d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillaiimport android.content.res.Configuration;
32b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.media.AudioManager;
33b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.net.Uri;
34b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.os.AsyncResult;
35b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.os.Binder;
36b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.os.Handler;
37b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.os.IBinder;
38b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.os.IPowerManager;
39b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.os.LocalPowerManager;
40b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.os.Message;
41b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.os.PowerManager;
42b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.os.RemoteException;
43b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.os.ServiceManager;
44b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.os.SystemClock;
45b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.os.SystemProperties;
46b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.preference.PreferenceManager;
47b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.provider.Settings.System;
48404edc94de563aef5fd5ba48be9114a970cb93bbWink Savilleimport android.telephony.ServiceState;
49b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.util.Config;
50b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.util.Log;
51b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.view.KeyEvent;
52b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.widget.Toast;
53b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
54ee77dd0ec7fcbe315a34ef6625854a8affe31885David Brownimport com.android.internal.telephony.Call;
55a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Savilleimport com.android.internal.telephony.IccCard;
56a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Savilleimport com.android.internal.telephony.MmiCode;
57a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Savilleimport com.android.internal.telephony.Phone;
58a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Savilleimport com.android.internal.telephony.PhoneFactory;
59a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Savilleimport com.android.internal.telephony.TelephonyIntents;
60ee77dd0ec7fcbe315a34ef6625854a8affe31885David Brownimport com.android.internal.telephony.cdma.EriInfo;
616bdc4d568f008ca476c744d52056dde3e8db27f8Abhishek Pillaiimport com.android.phone.OtaUtils.CdmaOtaScreenState;
62dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurentimport com.android.internal.telephony.cdma.TtyIntent;
63a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville
64b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project/**
65b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project * Top-level Application class for the Phone app.
66b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project */
6703adaac30038029e90dbbb266bacb3f2abb32397Mike Lockwoodpublic class PhoneApp extends Application implements AccelerometerListener.OrientationListener {
68b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /* package */ static final String LOG_TAG = "PhoneApp";
69796c70c99bc39295bf685061056f06ab8949c742David Brown
70796c70c99bc39295bf685061056f06ab8949c742David Brown    /**
71796c70c99bc39295bf685061056f06ab8949c742David Brown     * Phone app-wide debug level:
72796c70c99bc39295bf685061056f06ab8949c742David Brown     *   0 - no debug logging
73796c70c99bc39295bf685061056f06ab8949c742David Brown     *   1 - normal debug logging if ro.debuggable is set (which is true in
74796c70c99bc39295bf685061056f06ab8949c742David Brown     *       "eng" and "userdebug" builds but not "user" builds)
75796c70c99bc39295bf685061056f06ab8949c742David Brown     *   2 - ultra-verbose debug logging
76796c70c99bc39295bf685061056f06ab8949c742David Brown     *
77796c70c99bc39295bf685061056f06ab8949c742David Brown     * Most individual classes in the phone app have a local DBG constant,
78796c70c99bc39295bf685061056f06ab8949c742David Brown     * typically set to
79796c70c99bc39295bf685061056f06ab8949c742David Brown     *   (PhoneApp.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1)
80796c70c99bc39295bf685061056f06ab8949c742David Brown     * or else
81796c70c99bc39295bf685061056f06ab8949c742David Brown     *   (PhoneApp.DBG_LEVEL >= 2)
82796c70c99bc39295bf685061056f06ab8949c742David Brown     * depending on the desired verbosity.
83796c70c99bc39295bf685061056f06ab8949c742David Brown     */
84796c70c99bc39295bf685061056f06ab8949c742David Brown    /* package */ static final int DBG_LEVEL = 1;
85796c70c99bc39295bf685061056f06ab8949c742David Brown
86796c70c99bc39295bf685061056f06ab8949c742David Brown    private static final boolean DBG =
87796c70c99bc39295bf685061056f06ab8949c742David Brown            (PhoneApp.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
88796c70c99bc39295bf685061056f06ab8949c742David Brown    private static final boolean VDBG = (PhoneApp.DBG_LEVEL >= 2);
89b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
90b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    // Message codes; see mHandler below.
91b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private static final int EVENT_SIM_NETWORK_LOCKED = 3;
92b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private static final int EVENT_WIRED_HEADSET_PLUG = 7;
93b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private static final int EVENT_SIM_STATE_CHANGED = 8;
948bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project    private static final int EVENT_UPDATE_INCALL_NOTIFICATION = 9;
954079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project    private static final int EVENT_DATA_ROAMING_DISCONNECTED = 10;
964079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project    private static final int EVENT_DATA_ROAMING_OK = 11;
97404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville    private static final int EVENT_UNSOL_CDMA_INFO_RECORD = 12;
982eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu    private static final int EVENT_DOCK_STATE_CHANGED = 13;
99fb6e861345db8c4213280c7f32aa4e2eac4b0d11Eric Laurent    private static final int EVENT_TTY_PREFERRED_MODE_CHANGED = 14;
100fb6e861345db8c4213280c7f32aa4e2eac4b0d11Eric Laurent    private static final int EVENT_TTY_MODE_GET = 15;
101fb6e861345db8c4213280c7f32aa4e2eac4b0d11Eric Laurent    private static final int EVENT_TTY_MODE_SET = 16;
102b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
103b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    // The MMI codes are also used by the InCallScreen.
1044079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project    public static final int MMI_INITIATE = 51;
1054079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project    public static final int MMI_COMPLETE = 52;
1064079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project    public static final int MMI_CANCEL = 53;
107b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    // Don't use message codes larger than 99 here; those are reserved for
108b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    // the individual Activities of the Phone UI.
109b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
110b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /**
111b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * Allowable values for the poke lock code (timeout between a user activity and the
112b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * going to sleep), please refer to {@link com.android.server.PowerManagerService}
113b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * for additional reference.
114b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *   SHORT uses the short delay for the timeout (SHORT_KEYLIGHT_DELAY, 6 sec)
115b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *   MEDIUM uses the medium delay for the timeout (MEDIUM_KEYLIGHT_DELAY, 15 sec)
116b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *   DEFAULT is the system-wide default delay for the timeout (1 min)
117b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     */
118b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    public enum ScreenTimeoutDuration {
119b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        SHORT,
120b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        MEDIUM,
121b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        DEFAULT
122b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
123b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
124b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /**
125b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * Allowable values for the wake lock code.
126b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *   SLEEP means the device can be put to sleep.
127b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *   PARTIAL means wake the processor, but we display can be kept off.
128b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *   FULL means wake both the processor and the display.
129b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     */
130b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    public enum WakeState {
131b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        SLEEP,
132b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        PARTIAL,
133b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        FULL
134b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
135b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
136b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private static PhoneApp sMe;
137b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
138b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    // A few important fields we expose to the rest of the package
139b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    // directly (rather than thru set/get methods) for efficiency.
140b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    Phone phone;
141b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    CallNotifier notifier;
142b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    Ringer ringer;
143b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    BluetoothHandsfree mBtHandsfree;
144b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    PhoneInterfaceManager phoneMgr;
1458bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project    int mBluetoothHeadsetState = BluetoothHeadset.STATE_ERROR;
1464b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project    int mBluetoothHeadsetAudioState = BluetoothHeadset.STATE_ERROR;
1478bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project    boolean mShowBluetoothIndication = false;
14803adaac30038029e90dbbb266bacb3f2abb32397Mike Lockwood    static int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
149b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1500fb2b4b8eff811fd3243aefdf536e7ba2b10f4e9Wink Saville    // Internal PhoneApp Call state tracker
1510fb2b4b8eff811fd3243aefdf536e7ba2b10f4e9Wink Saville    CdmaPhoneCallState cdmaPhoneCallState;
1520fb2b4b8eff811fd3243aefdf536e7ba2b10f4e9Wink Saville
153b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    // The InCallScreen instance (or null if the InCallScreen hasn't been
154b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    // created yet.)
155b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private InCallScreen mInCallScreen;
156b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
157b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    // The currently-active PUK entry activity and progress dialog.
158b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    // Normally, these are the Emergency Dialer and the subsequent
159b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    // progress dialog.  null if there is are no such objects in
160b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    // the foreground.
161b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private Activity mPUKEntryActivity;
162b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private ProgressDialog mPUKEntryProgressDialog;
163b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
164b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private boolean mIsSimPinEnabled;
165b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private String mCachedSimPin;
166b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
167b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    // True if a wired headset is currently plugged in, based on the state
168b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    // from the latest Intent.ACTION_HEADSET_PLUG broadcast we received in
169b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    // mReceiver.onReceive().
170b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private boolean mIsHeadsetPlugged;
171b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
172d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillai    // True if the keyboard is currently *not* hidden
173d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillai    // Gets updated whenever there is a Configuration change
174d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillai    private boolean mIsHardKeyboardOpen;
175d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillai
176445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood    // True if we are beginning a call, but the phone state has not changed yet
177445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood    private boolean mBeginningCall;
178445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood
179445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood    // Last phone state seen by updatePhoneState()
180445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood    Phone.State mLastPhoneState = Phone.State.IDLE;
181445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood
182b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private WakeState mWakeState = WakeState.SLEEP;
1834472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown    private ScreenTimeoutDuration mScreenTimeoutDuration = ScreenTimeoutDuration.DEFAULT;
1844472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown    private boolean mIgnoreTouchUserActivity = false;
185b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private IBinder mPokeLockToken = new Binder();
186b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private IPowerManager mPowerManagerService;
187b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private PowerManager.WakeLock mWakeLock;
188b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private PowerManager.WakeLock mPartialWakeLock;
18958de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown    private PowerManager.WakeLock mProximityWakeLock;
190b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private KeyguardManager mKeyguardManager;
191e22715b7e908074c33140eded331b01946026d4eMike Lockwood    private StatusBarManager mStatusBarManager;
1920327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood    private int mStatusBarDisableCount;
19303adaac30038029e90dbbb266bacb3f2abb32397Mike Lockwood    private AccelerometerListener mAccelerometerListener;
19403adaac30038029e90dbbb266bacb3f2abb32397Mike Lockwood    private int mOrientation = AccelerometerListener.ORIENTATION_UNKNOWN;
195b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
196b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    // Broadcast receiver for various intent broadcasts (see onCreate())
197b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private final BroadcastReceiver mReceiver = new PhoneAppBroadcastReceiver();
198b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
199b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    // Broadcast receiver purely for ACTION_MEDIA_BUTTON broadcasts
200b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private final BroadcastReceiver mMediaButtonReceiver = new MediaButtonBroadcastReceiver();
201b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
202b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /** boolean indicating restoring mute state on InCallScreen.onResume() */
203b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private boolean mShouldRestoreMuteOnInCallResume;
204b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
205af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville    // Following are the CDMA OTA information Objects used during OTA Call.
206af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville    // cdmaOtaProvisionData object store static OTA information that needs
207af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville    // to be maintained even during Slider open/close scenarios.
208af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville    // cdmaOtaConfigData object stores configuration info to control visiblity
209af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville    // of each OTA Screens.
210af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville    // cdmaOtaScreenState object store OTA Screen State information.
211af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville    public OtaUtils.CdmaOtaProvisionData cdmaOtaProvisionData;
212af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville    public OtaUtils.CdmaOtaConfigData cdmaOtaConfigData;
213af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville    public OtaUtils.CdmaOtaScreenState cdmaOtaScreenState;
214f6eb7893215a875863e1e0200e4aa3174137f76cWink Saville    public OtaUtils.CdmaOtaInCallScreenUiState cdmaOtaInCallScreenUiState;
215af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville
216dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent    // TTY feature enabled on this platform
217dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent    private boolean mTtyEnabled;
218dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent    // Current TTY operating mode selected by user
219dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent    private int mPreferredTtyMode = Phone.TTY_MODE_OFF;
220dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent
221b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /**
222b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * Set the restore mute state flag. Used when we are setting the mute state
223b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * OUTSIDE of user interaction {@link PhoneUtils#startNewCall(Phone)}
224b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     */
225b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /*package*/void setRestoreMuteOnInCallResume (boolean mode) {
226b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mShouldRestoreMuteOnInCallResume = mode;
227b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
228b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
229b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /**
230b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * Get the restore mute state flag.
231b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * This is used by the InCallScreen {@link InCallScreen#onResume()} to figure
232b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * out if we need to restore the mute state for the current active call.
233b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     */
234b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /*package*/boolean getRestoreMuteOnInCallResume () {
235b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return mShouldRestoreMuteOnInCallResume;
236b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
237b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
238b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    Handler mHandler = new Handler() {
239b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        @Override
240b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        public void handleMessage(Message msg) {
2412eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu            Phone.State phoneState;
242b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            switch (msg.what) {
243b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
244027987172fa2033d10657a03cbce661d2546f0f4David Brown                // TODO: This event should be handled by the lock screen, just
245027987172fa2033d10657a03cbce661d2546f0f4David Brown                // like the "SIM missing" and "Sim locked" cases (bug 1804111).
246b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                case EVENT_SIM_NETWORK_LOCKED:
24709328f1bd9acf6b16a9cb79d7d9fa59d322ac6c2David Brown                    if (getResources().getBoolean(R.bool.ignore_sim_network_locked_events)) {
24809328f1bd9acf6b16a9cb79d7d9fa59d322ac6c2David Brown                        // Some products don't have the concept of a "SIM network lock"
24909328f1bd9acf6b16a9cb79d7d9fa59d322ac6c2David Brown                        Log.i(LOG_TAG, "Ignoring EVENT_SIM_NETWORK_LOCKED event; "
25009328f1bd9acf6b16a9cb79d7d9fa59d322ac6c2David Brown                              + "not showing 'SIM network unlock' PIN entry screen");
25109328f1bd9acf6b16a9cb79d7d9fa59d322ac6c2David Brown                    } else {
25209328f1bd9acf6b16a9cb79d7d9fa59d322ac6c2David Brown                        // Normal case: show the "SIM network unlock" PIN entry screen.
25309328f1bd9acf6b16a9cb79d7d9fa59d322ac6c2David Brown                        // The user won't be able to do anything else until
25409328f1bd9acf6b16a9cb79d7d9fa59d322ac6c2David Brown                        // they enter a valid SIM network PIN.
25509328f1bd9acf6b16a9cb79d7d9fa59d322ac6c2David Brown                        Log.i(LOG_TAG, "show sim depersonal panel");
25609328f1bd9acf6b16a9cb79d7d9fa59d322ac6c2David Brown                        IccNetworkDepersonalizationPanel ndpPanel =
25709328f1bd9acf6b16a9cb79d7d9fa59d322ac6c2David Brown                                new IccNetworkDepersonalizationPanel(PhoneApp.getInstance());
25809328f1bd9acf6b16a9cb79d7d9fa59d322ac6c2David Brown                        ndpPanel.show();
25909328f1bd9acf6b16a9cb79d7d9fa59d322ac6c2David Brown                    }
260b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    break;
261b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2628bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project                case EVENT_UPDATE_INCALL_NOTIFICATION:
2638bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project                    // Tell the NotificationMgr to update the "ongoing
2648bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project                    // call" icon in the status bar, if necessary.
2658bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project                    // Currently, this is triggered by a bluetooth headset
2668bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project                    // state change (since the status bar icon needs to
2678bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project                    // turn blue when bluetooth is active.)
2688bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project                    NotificationMgr.getDefault().updateInCallNotification();
2698bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project                    break;
2708bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project
2714079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project                case EVENT_DATA_ROAMING_DISCONNECTED:
2724079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project                    NotificationMgr.getDefault().showDataDisconnectedRoaming();
273b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    break;
2744079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project
2754079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project                case EVENT_DATA_ROAMING_OK:
2764079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project                    NotificationMgr.getDefault().hideDataDisconnectedRoaming();
2774079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project                    break;
2784079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project
279b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                case MMI_COMPLETE:
280b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    onMMIComplete((AsyncResult) msg.obj);
281b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    break;
282b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
283b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                case MMI_CANCEL:
284b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    PhoneUtils.cancelMmiCode(phone);
285b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    break;
286b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
287b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                case EVENT_WIRED_HEADSET_PLUG:
288b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    // Since the presence of a wired headset or bluetooth affects the
289b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    // speakerphone, update the "speaker" state.  We ONLY want to do
290b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    // this on the wired headset connect / disconnect events for now
291b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    // though, so we're only triggering on EVENT_WIRED_HEADSET_PLUG.
2924a8cbde060e7876a5560346215c11078a9efc90eEric Laurent
2932eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu                    phoneState = phone.getState();
2944a8cbde060e7876a5560346215c11078a9efc90eEric Laurent                    // Do not change speaker state if phone is not off hook
2954a8cbde060e7876a5560346215c11078a9efc90eEric Laurent                    if (phoneState == Phone.State.OFFHOOK) {
2960ff0a8d3bc27947fb5e7b881fa28de75c3e40b3dEric Laurent                        if (mBtHandsfree == null || !mBtHandsfree.isAudioOn()) {
2974a8cbde060e7876a5560346215c11078a9efc90eEric Laurent                            if (!isHeadsetPlugged()) {
2984a8cbde060e7876a5560346215c11078a9efc90eEric Laurent                                // if the state is "not connected", restore the speaker state.
2994a8cbde060e7876a5560346215c11078a9efc90eEric Laurent                                PhoneUtils.restoreSpeakerMode(getApplicationContext());
3004a8cbde060e7876a5560346215c11078a9efc90eEric Laurent                            } else {
3014a8cbde060e7876a5560346215c11078a9efc90eEric Laurent                                // if the state is "connected", force the speaker off without
3024a8cbde060e7876a5560346215c11078a9efc90eEric Laurent                                // storing the state.
3034a8cbde060e7876a5560346215c11078a9efc90eEric Laurent                                PhoneUtils.turnOnSpeaker(getApplicationContext(), false, false);
3044a8cbde060e7876a5560346215c11078a9efc90eEric Laurent                            }
305425b8e3c9846d5e0e76466604b35cad8856d79deEric Laurent                        }
306b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    }
307d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillai                    // Update the Proximity sensor based on headset state
3084a8cbde060e7876a5560346215c11078a9efc90eEric Laurent                    updateProximitySensorMode(phoneState);
309dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent
310dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                    // Force TTY state update according to new headset state
311dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                    if (mTtyEnabled) {
312dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                        sendMessage(obtainMessage(EVENT_TTY_PREFERRED_MODE_CHANGED, 0));
313dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                    }
314b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    break;
315b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
316b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                case EVENT_SIM_STATE_CHANGED:
317b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    // Marks the event where the SIM goes into ready state.
318b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    // Right now, this is only used for the PUK-unlocking
319b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    // process.
320a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville                    if (msg.obj.equals(IccCard.INTENT_VALUE_ICC_READY)) {
321b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        // when the right event is triggered and there
322b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        // are UI objects in the foreground, we close
323b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        // them to display the lock panel.
324b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        if (mPUKEntryActivity != null) {
325b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                            mPUKEntryActivity.finish();
326b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                            mPUKEntryActivity = null;
327b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        }
328b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        if (mPUKEntryProgressDialog != null) {
329b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                            mPUKEntryProgressDialog.dismiss();
330b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                            mPUKEntryProgressDialog = null;
331b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        }
332b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    }
333b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    break;
334404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville
335404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville                case EVENT_UNSOL_CDMA_INFO_RECORD:
336404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville                    //TODO: handle message here;
337404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville                    break;
3382eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu
3392eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu                case EVENT_DOCK_STATE_CHANGED:
3402eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu                    // If the phone is docked/undocked during a call, and no wired or BT headset
3412eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu                    // is connected: turn on/off the speaker accordingly.
3422eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu                    boolean inDockMode = false;
3432eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu                    if (mDockState == Intent.EXTRA_DOCK_STATE_DESK ||
3442eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu                            mDockState == Intent.EXTRA_DOCK_STATE_CAR) {
3452eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu                        inDockMode = true;
3462eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu                    }
3472eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu                    if (VDBG) Log.d(LOG_TAG, "received EVENT_DOCK_STATE_CHANGED. Phone inDock = "
3482eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu                            + inDockMode);
3492eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu
3502eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu                    phoneState = phone.getState();
3512eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu                    if (phoneState == Phone.State.OFFHOOK &&
3522eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu                            !isHeadsetPlugged() &&
3532eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu                            !(mBtHandsfree != null && mBtHandsfree.isAudioOn())) {
3542eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu                        PhoneUtils.turnOnSpeaker(getApplicationContext(), inDockMode, true);
3552eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu
3562eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu                        if (mInCallScreen != null) {
3572eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu                            mInCallScreen.requestUpdateTouchUi();
3582eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu                        }
3592eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu                    }
360fb6e861345db8c4213280c7f32aa4e2eac4b0d11Eric Laurent
361dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                case EVENT_TTY_PREFERRED_MODE_CHANGED:
362dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                    // TTY mode is only applied if a headset is connected
363dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                    int ttyMode;
364dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                    if (isHeadsetPlugged()) {
365dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                        ttyMode = mPreferredTtyMode;
366dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                    } else {
367dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                        ttyMode = Phone.TTY_MODE_OFF;
368dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                    }
369dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                    phone.setTTYMode(ttyMode, mHandler.obtainMessage(EVENT_TTY_MODE_SET));
370dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                    break;
371dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent
372dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                case EVENT_TTY_MODE_GET:
373dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                    handleQueryTTYModeResponse(msg);
374dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                    break;
375dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent
376dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                case EVENT_TTY_MODE_SET:
377dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                    handleSetTTYModeResponse(msg);
3782eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu                    break;
379b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
380b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
381b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    };
382b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
383b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    public PhoneApp() {
384b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        sMe = this;
385b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
386b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
387b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    @Override
388b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    public void onCreate() {
389027987172fa2033d10657a03cbce661d2546f0f4David Brown        if (VDBG) Log.v(LOG_TAG, "onCreate()...");
390b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
391b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        ContentResolver resolver = getContentResolver();
392b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
393b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (phone == null) {
394b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // Initialize the telephony framework
395b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            PhoneFactory.makeDefaultPhones(this);
396b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
397b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // Get the default phone
398b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            phone = PhoneFactory.getDefaultPhone();
399b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
400b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            NotificationMgr.init(this);
401b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
402b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            phoneMgr = new PhoneInterfaceManager(this, phone);
403c9d9ed30aa547b79b81adc13a4d148a003b6ee62w
40479b9f6e30e8402b103f1ec3ed9dda1fcad2b1cd5Tammo Spalink            int phoneType = phone.getPhoneType();
40579b9f6e30e8402b103f1ec3ed9dda1fcad2b1cd5Tammo Spalink
40679b9f6e30e8402b103f1ec3ed9dda1fcad2b1cd5Tammo Spalink            if (phoneType == Phone.PHONE_TYPE_CDMA) {
407c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                // Create an instance of CdmaPhoneCallState and initialize it to IDLE
408c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                cdmaPhoneCallState = new CdmaPhoneCallState();
409c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                cdmaPhoneCallState.CdmaPhoneCallStateInit();
410c9d9ed30aa547b79b81adc13a4d148a003b6ee62w            }
411c9d9ed30aa547b79b81adc13a4d148a003b6ee62w
412cec56587c1d3c90014848d7adfe817bef9c325eaNick Pelly            if (BluetoothAdapter.getDefaultAdapter() != null) {
413b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                mBtHandsfree = new BluetoothHandsfree(this, phone);
414b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                startService(new Intent(this, BluetoothHeadsetService.class));
415b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            } else {
416b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // Device is not bluetooth capable
417b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                mBtHandsfree = null;
418b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
419b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
420b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            ringer = new Ringer(phone);
421b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
422b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // before registering for phone state changes
423b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
424b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
42530b5eaaa481befad78cfcaaae024ca90e0616630Joe Onorato                    | PowerManager.ACQUIRE_CAUSES_WAKEUP,
42630b5eaaa481befad78cfcaaae024ca90e0616630Joe Onorato                    LOG_TAG);
427b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // lock used to keep the processor awake, when we don't care for the display.
428b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mPartialWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK
429b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    | PowerManager.ON_AFTER_RELEASE, LOG_TAG);
43058de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown            // Wake lock used to control proximity sensor behavior.
43158de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown            if ((pm.getSupportedWakeLockFlags()
43258de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown                 & PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) != 0x0) {
43358de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown                mProximityWakeLock =
43458de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown                        pm.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, LOG_TAG);
43558de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown            }
436027987172fa2033d10657a03cbce661d2546f0f4David Brown            if (DBG) Log.d(LOG_TAG, "onCreate: mProximityWakeLock: " + mProximityWakeLock);
43758de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown
43803adaac30038029e90dbbb266bacb3f2abb32397Mike Lockwood            // create mAccelerometerListener only if we are using the proximity sensor
43903adaac30038029e90dbbb266bacb3f2abb32397Mike Lockwood            if (proximitySensorModeEnabled()) {
44003adaac30038029e90dbbb266bacb3f2abb32397Mike Lockwood                mAccelerometerListener = new AccelerometerListener(this, this);
44103adaac30038029e90dbbb266bacb3f2abb32397Mike Lockwood            }
44203adaac30038029e90dbbb266bacb3f2abb32397Mike Lockwood
443b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
444e22715b7e908074c33140eded331b01946026d4eMike Lockwood            mStatusBarManager = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
445b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
446b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // get a handle to the service so that we can use it later when we
447b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // want to set the poke lock.
448b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mPowerManagerService = IPowerManager.Stub.asInterface(
449b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    ServiceManager.getService("power"));
450b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
45111aceea577d724e61056a646ef6ac8c6430613eeNicolas Catania            notifier = new CallNotifier(this, phone, ringer, mBtHandsfree, new CallLogAsync());
452b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
453a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville            // register for ICC status
454a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville            IccCard sim = phone.getIccCard();
455b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            if (sim != null) {
456027987172fa2033d10657a03cbce661d2546f0f4David Brown                if (VDBG) Log.v(LOG_TAG, "register for ICC status");
457b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                sim.registerForNetworkLocked(mHandler, EVENT_SIM_NETWORK_LOCKED, null);
458b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
459b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
460b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // register for MMI/USSD
46179b9f6e30e8402b103f1ec3ed9dda1fcad2b1cd5Tammo Spalink            if (phoneType == Phone.PHONE_TYPE_GSM) {
462fa3fbeedf018e40f8d6696a382bbf316219cf4aeReli Tal                phone.registerForMmiComplete(mHandler, MMI_COMPLETE, null);
463fa3fbeedf018e40f8d6696a382bbf316219cf4aeReli Tal            }
464b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
465b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // register connection tracking to PhoneUtils
466b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            PhoneUtils.initializeConnectionHandler(phone);
467b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
468dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent            // Read platform settings for TTY feature
469dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent            mTtyEnabled = getResources().getBoolean(R.bool.tty_enabled);
470dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent
471b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // Register for misc other intent broadcasts.
472b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            IntentFilter intentFilter =
473b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
47418e0a07cc700234f5a02fe10eee9b3ea10db58b3Nick Pelly            intentFilter.addAction(BluetoothHeadset.ACTION_STATE_CHANGED);
47518e0a07cc700234f5a02fe10eee9b3ea10db58b3Nick Pelly            intentFilter.addAction(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED);
476b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            intentFilter.addAction(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED);
477b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            intentFilter.addAction(Intent.ACTION_HEADSET_PLUG);
4782eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu            intentFilter.addAction(Intent.ACTION_DOCK_EVENT);
479b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            intentFilter.addAction(Intent.ACTION_BATTERY_LOW);
480b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            intentFilter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
481a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville            intentFilter.addAction(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);
482404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville            intentFilter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
483abeff00b5242e795baa8f5271525c5911f649993Wink Saville            intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
484dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent            if (mTtyEnabled) {
485dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                intentFilter.addAction(TtyIntent.TTY_PREFERRED_MODE_CHANGE_ACTION);
486dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent            }
48747765dadd119fedd04f40f4474147b4ca0416e68David Ingram            intentFilter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
488b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            registerReceiver(mReceiver, intentFilter);
489b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
490b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // Use a separate receiver for ACTION_MEDIA_BUTTON broadcasts,
491b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // since we need to manually adjust its priority (to make sure
492b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // we get these intents *before* the media player.)
493b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            IntentFilter mediaButtonIntentFilter =
494b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
495b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            //
496b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // Make sure we're higher priority than the media player's
497b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // MediaButtonIntentReceiver (which currently has the default
498b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // priority of zero; see apps/Music/AndroidManifest.xml.)
499b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mediaButtonIntentFilter.setPriority(1);
500b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            //
501b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            registerReceiver(mMediaButtonReceiver, mediaButtonIntentFilter);
502b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
503b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            //set the default values for the preferences in the phone.
504b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            PreferenceManager.setDefaultValues(this, R.xml.network_setting, false);
505cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause
506cd0189299f484dcbd51400b994daf585923e9e8dDavid Krause            PreferenceManager.setDefaultValues(this, R.xml.call_feature_setting, false);
507b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
508b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // Make sure the audio mode (along with some
509b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // audio-mode-related state of our own) is initialized
510b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // correctly, given the current state of the phone.
511b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            switch (phone.getState()) {
512b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                case IDLE:
513b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    if (DBG) Log.d(LOG_TAG, "Resetting audio state/mode: IDLE");
514b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    PhoneUtils.setAudioControlState(PhoneUtils.AUDIO_IDLE);
515b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    PhoneUtils.setAudioMode(this, AudioManager.MODE_NORMAL);
516b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    break;
517b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                case RINGING:
518b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    if (DBG) Log.d(LOG_TAG, "Resetting audio state/mode: RINGING");
519b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    PhoneUtils.setAudioControlState(PhoneUtils.AUDIO_RINGING);
520b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    PhoneUtils.setAudioMode(this, AudioManager.MODE_RINGTONE);
521b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    break;
522b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                case OFFHOOK:
523b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    if (DBG) Log.d(LOG_TAG, "Resetting audio state/mode: OFFHOOK");
524b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    PhoneUtils.setAudioControlState(PhoneUtils.AUDIO_OFFHOOK);
525b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    PhoneUtils.setAudioMode(this, AudioManager.MODE_IN_CALL);
526b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    break;
527b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
528b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
529b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
53079b9f6e30e8402b103f1ec3ed9dda1fcad2b1cd5Tammo Spalink        boolean phoneIsCdma = (phone.getPhoneType() == Phone.PHONE_TYPE_CDMA);
53179b9f6e30e8402b103f1ec3ed9dda1fcad2b1cd5Tammo Spalink
53279b9f6e30e8402b103f1ec3ed9dda1fcad2b1cd5Tammo Spalink        if (phoneIsCdma) {
533af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville            cdmaOtaProvisionData = new OtaUtils.CdmaOtaProvisionData();
534af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville            cdmaOtaConfigData = new OtaUtils.CdmaOtaConfigData();
535af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville            cdmaOtaScreenState = new OtaUtils.CdmaOtaScreenState();
536f6eb7893215a875863e1e0200e4aa3174137f76cWink Saville            cdmaOtaInCallScreenUiState = new OtaUtils.CdmaOtaInCallScreenUiState();
537af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville        }
538af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville
539b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // XXX pre-load the SimProvider so that it's ready
540a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville        resolver.getType(Uri.parse("content://icc/adn"));
541b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
542b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // start with the default value to set the mute state.
543b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mShouldRestoreMuteOnInCallResume = false;
544404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville
5459fd5e84fd6f5c2c5f488bf08d8dd9a2c0aab1370Wink Saville        // TODO: Register for Cdma Information Records
5460fb2b4b8eff811fd3243aefdf536e7ba2b10f4e9Wink Saville        // phone.registerCdmaInformationRecord(mHandler, EVENT_UNSOL_CDMA_INFO_RECORD, null);
5470fb2b4b8eff811fd3243aefdf536e7ba2b10f4e9Wink Saville
5487fe7f7e57aaa38290c37ab48867d34a279d198f3w        // Read TTY settings and store it into BP NV.
5497fe7f7e57aaa38290c37ab48867d34a279d198f3w        // AP owns (i.e. stores) the TTY setting in AP settings database and pushes the setting
5507fe7f7e57aaa38290c37ab48867d34a279d198f3w        // to BP at power up (BP does not need to make the TTY setting persistent storage).
5517fe7f7e57aaa38290c37ab48867d34a279d198f3w        // This way, there is a single owner (i.e AP) for the TTY setting in the phone.
552dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent        if (mTtyEnabled) {
553dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent            mPreferredTtyMode = android.provider.Settings.Secure.getInt(
5547fe7f7e57aaa38290c37ab48867d34a279d198f3w                    phone.getContext().getContentResolver(),
5557fe7f7e57aaa38290c37ab48867d34a279d198f3w                    android.provider.Settings.Secure.PREFERRED_TTY_MODE,
5567fe7f7e57aaa38290c37ab48867d34a279d198f3w                    Phone.TTY_MODE_OFF);
557dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent            mHandler.sendMessage(mHandler.obtainMessage(EVENT_TTY_PREFERRED_MODE_CHANGED, 0));
558dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent        }
559dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent        // Read HAC settings and configure audio hardware
560dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent        if (getResources().getBoolean(R.bool.hac_enabled)) {
56106376849cdac54b573be9204837b451ef8013cc6Eric Laurent            int hac = android.provider.Settings.System.getInt(phone.getContext().getContentResolver(),
56206376849cdac54b573be9204837b451ef8013cc6Eric Laurent                                                              android.provider.Settings.System.HEARING_AID,
56306376849cdac54b573be9204837b451ef8013cc6Eric Laurent                                                              0);
564dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent            AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
56506376849cdac54b573be9204837b451ef8013cc6Eric Laurent            audioManager.setParameter(CallFeaturesSetting.HAC_KEY, hac != 0 ?
56606376849cdac54b573be9204837b451ef8013cc6Eric Laurent                                      CallFeaturesSetting.HAC_VAL_ON :
56706376849cdac54b573be9204837b451ef8013cc6Eric Laurent                                      CallFeaturesSetting.HAC_VAL_OFF);
5687fe7f7e57aaa38290c37ab48867d34a279d198f3w        }
569b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project   }
570b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
571d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillai    @Override
572d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillai    public void onConfigurationChanged(Configuration newConfig) {
573d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillai        if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
574d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillai            mIsHardKeyboardOpen = true;
575d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillai        } else {
576d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillai            mIsHardKeyboardOpen = false;
577d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillai        }
578d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillai
579d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillai        // Update the Proximity sensor based on keyboard state
580d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillai        updateProximitySensorMode(phone.getState());
581d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillai        super.onConfigurationChanged(newConfig);
582d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillai    }
583d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillai
584b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /**
585b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * Returns the singleton instance of the PhoneApp.
586b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     */
587b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    static PhoneApp getInstance() {
588b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return sMe;
589b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
590b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
591b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    Ringer getRinger() {
592b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return ringer;
593b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
594b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
595b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    BluetoothHandsfree getBluetoothHandsfree() {
596b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return mBtHandsfree;
597b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
598b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
599b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    static Intent createCallLogIntent() {
600b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        Intent  intent = new Intent(Intent.ACTION_VIEW, null);
601b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        intent.setType("vnd.android.cursor.dir/calls");
602b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return intent;
603b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
604b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
605b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /**
606b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * Return an Intent that can be used to bring up the in-call screen.
607b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *
608b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * This intent can only be used from within the Phone app, since the
609b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * InCallScreen is not exported from our AndroidManifest.
610b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     */
611b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /* package */ static Intent createInCallIntent() {
612b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        Intent intent = new Intent(Intent.ACTION_MAIN, null);
613b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
614b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
615b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
616b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        intent.setClassName("com.android.phone", getCallScreenClassName());
617b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return intent;
618b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
619b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
620b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /**
621b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * Variation of createInCallIntent() that also specifies whether the
622b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * DTMF dialpad should be initially visible when the InCallScreen
623b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * comes up.
624b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     */
625b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /* package */ static Intent createInCallIntent(boolean showDialpad) {
626b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        Intent intent = createInCallIntent();
627b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        intent.putExtra(InCallScreen.SHOW_DIALPAD_EXTRA, showDialpad);
628b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return intent;
629b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
630b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
631b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    static String getCallScreenClassName() {
632b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return InCallScreen.class.getName();
633b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
634b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
635b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /**
636b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * Starts the InCallScreen Activity.
637b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     */
638b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    void displayCallScreen() {
639796c70c99bc39295bf685061056f06ab8949c742David Brown        if (VDBG) Log.d(LOG_TAG, "displayCallScreen()...");
640b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        startActivity(createInCallIntent());
641b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        Profiler.callScreenRequested();
642b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
643b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
644b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /**
645b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * Helper function to check for one special feature of the CALL key:
646b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * Normally, when the phone is idle, CALL takes you to the call log
647b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * (see the handler for KEYCODE_CALL in PhoneWindow.onKeyUp().)
648b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * But if the phone is in use (either off-hook or ringing) we instead
649b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * handle the CALL button by taking you to the in-call UI.
650b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *
651b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * @return true if we intercepted the CALL keypress (i.e. the phone
652b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *              was in use)
653b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *
654b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * @see DialerActivity#onCreate
655b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     */
656b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    boolean handleInCallOrRinging() {
657b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (phone.getState() != Phone.State.IDLE) {
658b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // Phone is OFFHOOK or RINGING.
659b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            if (DBG) Log.v(LOG_TAG,
660b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                           "handleInCallOrRinging: show call screen");
661b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            displayCallScreen();
662b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            return true;
663b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
664b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return false;
665b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
666b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
667b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    boolean isSimPinEnabled() {
668b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return mIsSimPinEnabled;
669b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
670b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
671b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    boolean authenticateAgainstCachedSimPin(String pin) {
672b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return (mCachedSimPin != null && mCachedSimPin.equals(pin));
673b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
674b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
675b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    void setCachedSimPin(String pin) {
676b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mCachedSimPin = pin;
677b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
678b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
679b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    void setInCallScreenInstance(InCallScreen inCallScreen) {
680b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mInCallScreen = inCallScreen;
681b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
682b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
683b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /**
684b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * @return true if the in-call UI is running as the foreground
685b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * activity.  (In other words, from the perspective of the
686b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * InCallScreen activity, return true between onResume() and
687b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * onPause().)
688b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *
689b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * Note this method will return false if the screen is currently off,
690b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * even if the InCallScreen *was* in the foreground just before the
691b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * screen turned off.  (This is because the foreground activity is
692b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * always "paused" while the screen is off.)
693b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     */
694b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    boolean isShowingCallScreen() {
695b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (mInCallScreen == null) return false;
696b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return mInCallScreen.isForegroundActivity();
697b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
698b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
699b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /**
700b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * Dismisses the in-call UI.
701b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *
702b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * This also ensures that you won't be able to get back to the in-call
703b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * UI via the BACK button (since this call removes the InCallScreen
704b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * from the activity history.)
705af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville     * For OTA Call, it call InCallScreen api to handle OTA Call End scenario
706af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville     * to display OTA Call End screen.
707b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     */
708b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    void dismissCallScreen() {
709b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (mInCallScreen != null) {
7106bdc4d568f008ca476c744d52056dde3e8db27f8Abhishek Pillai            if (mInCallScreen.isOtaCallInActiveState()
7116bdc4d568f008ca476c744d52056dde3e8db27f8Abhishek Pillai                    || mInCallScreen.isOtaCallInEndState()
7126bdc4d568f008ca476c744d52056dde3e8db27f8Abhishek Pillai                    || ((cdmaOtaScreenState != null)
7136bdc4d568f008ca476c744d52056dde3e8db27f8Abhishek Pillai                    && (cdmaOtaScreenState.otaScreenState
7146bdc4d568f008ca476c744d52056dde3e8db27f8Abhishek Pillai                            != CdmaOtaScreenState.OtaScreenState.OTA_STATUS_UNDEFINED))) {
7159fd5e84fd6f5c2c5f488bf08d8dd9a2c0aab1370Wink Saville                // TODO: During OTA Call, display should not become dark to
7169fd5e84fd6f5c2c5f488bf08d8dd9a2c0aab1370Wink Saville                // allow user to see OTA UI update. Phone app needs to hold
7179fd5e84fd6f5c2c5f488bf08d8dd9a2c0aab1370Wink Saville                // a SCREEN_DIM_WAKE_LOCK wake lock during the entire OTA call.
718af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville                wakeUpScreen();
7196bdc4d568f008ca476c744d52056dde3e8db27f8Abhishek Pillai                // If InCallScreen is not in foreground we resume it to show the OTA call end screen
7206bdc4d568f008ca476c744d52056dde3e8db27f8Abhishek Pillai                // Fire off the InCallScreen intent
7216bdc4d568f008ca476c744d52056dde3e8db27f8Abhishek Pillai                displayCallScreen();
7226bdc4d568f008ca476c744d52056dde3e8db27f8Abhishek Pillai
723af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville                mInCallScreen.handleOtaCallEnd();
724af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville                return;
725af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville            } else {
726af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville                mInCallScreen.finish();
727af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville            }
728b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
729b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
730b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
731b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /**
732af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville     * Handle OTA events
733af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville     *
734af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville     * When OTA call is active and display becomes dark, then CallNotifier will
735af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville     * handle OTA Events by calling this api which then calls OtaUtil function.
736af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville     */
737af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville    void handleOtaEvents(Message msg) {
738af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville
739af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville        if (DBG) Log.d(LOG_TAG, "Enter handleOtaEvents");
740af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville        if ((mInCallScreen != null) && (!isShowingCallScreen())) {
741af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville            if (mInCallScreen.otaUtils != null) {
742af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville                mInCallScreen.otaUtils.onOtaProvisionStatusChanged((AsyncResult) msg.obj);
743af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville            }
744af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville        }
745af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville    }
746af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville
747af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville
748af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville    /**
749b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * Sets the activity responsible for un-PUK-blocking the device
750b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * so that we may close it when we receive a positive result.
751b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * mPUKEntryActivity is also used to indicate to the device that
752b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * we are trying to un-PUK-lock the phone. In other words, iff
753b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * it is NOT null, then we are trying to unlock and waiting for
754b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * the SIM to move to READY state.
755b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *
756b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * @param activity is the activity to close when PUK has
757b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * finished unlocking. Can be set to null to indicate the unlock
758b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * or SIM READYing process is over.
759b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     */
760b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    void setPukEntryActivity(Activity activity) {
761b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mPUKEntryActivity = activity;
762b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
763b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
764b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    Activity getPUKEntryActivity() {
765b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return mPUKEntryActivity;
766b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
767b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
768b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /**
769b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * Sets the dialog responsible for notifying the user of un-PUK-
770b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * blocking - SIM READYing progress, so that we may dismiss it
771b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * when we receive a positive result.
772b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *
773b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * @param dialog indicates the progress dialog informing the user
774b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * of the state of the device.  Dismissed upon completion of
775b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * READYing process
776b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     */
777b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    void setPukEntryProgressDialog(ProgressDialog dialog) {
778b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mPUKEntryProgressDialog = dialog;
779b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
780b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
781b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    ProgressDialog getPUKEntryProgressDialog() {
782b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return mPUKEntryProgressDialog;
783b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
784b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
785b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /**
7860327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood     * Disables the status bar.  This is used by the phone app when in-call UI is active.
7870327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood     *
7880327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood     * Any call to this method MUST be followed (eventually)
7890327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood     * by a corresponding reenableStatusBar() call.
7900327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood     */
7910327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood    /* package */ void disableStatusBar() {
7920327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood        if (DBG) Log.d(LOG_TAG, "disable status bar");
7930327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood        synchronized (this) {
7940327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood            if (mStatusBarDisableCount++ == 0) {
7950327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood               if (DBG)  Log.d(LOG_TAG, "StatusBarManager.DISABLE_EXPAND");
7960327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood                mStatusBarManager.disable(StatusBarManager.DISABLE_EXPAND);
7970327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood            }
7980327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood        }
7990327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood    }
8000327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood
8010327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood    /**
8020327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood     * Re-enables the status bar after a previous disableStatusBar() call.
8030327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood     *
8040327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood     * Any call to this method MUST correspond to (i.e. be balanced with)
8050327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood     * a previous disableStatusBar() call.
8060327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood     */
8070327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood    /* package */ void reenableStatusBar() {
8080327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood        if (DBG) Log.d(LOG_TAG, "re-enable status bar");
8090327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood        synchronized (this) {
8100327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood            if (mStatusBarDisableCount > 0) {
8110327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood                if (--mStatusBarDisableCount == 0) {
8120327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood                    if (DBG) Log.d(LOG_TAG, "StatusBarManager.DISABLE_NONE");
8130327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood                    mStatusBarManager.disable(StatusBarManager.DISABLE_NONE);
8140327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood                }
8150327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood            } else {
8160327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood                Log.e(LOG_TAG, "mStatusBarDisableCount is already zero");
8170327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood            }
8180327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood        }
8190327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood    }
8200327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood
8210327eebefa8d277d8ce3226ebbe884310efa0bedMike Lockwood    /**
822b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * Controls how quickly the screen times out.
823b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *
824b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * The poke lock controls how long it takes before the screen powers
825b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * down, and therefore has no immediate effect when the current
826b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * WakeState (see {@link PhoneApp#requestWakeState}) is FULL.
827b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * If we're in a state where the screen *is* allowed to turn off,
828b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * though, the poke lock will determine the timeout interval (long or
829b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * short).
830b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *
831b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * @param shortPokeLock tells the device the timeout duration to use
832b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * before going to sleep
833b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * {@link com.android.server.PowerManagerService#SHORT_KEYLIGHT_DELAY}.
834b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     */
835b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /* package */ void setScreenTimeout(ScreenTimeoutDuration duration) {
836796c70c99bc39295bf685061056f06ab8949c742David Brown        if (VDBG) Log.d(LOG_TAG, "setScreenTimeout(" + duration + ")...");
837b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
838b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // make sure we don't set the poke lock repeatedly so that we
839b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // avoid triggering the userActivity calls in
840b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // PowerManagerService.setPokeLock().
8414472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown        if (duration == mScreenTimeoutDuration) {
842b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            return;
843b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
844039a8e51aa4a3ed27ad788dd5f1e043446517d92Mike Lockwood        // stick with default timeout if we are using the proximity sensor
845039a8e51aa4a3ed27ad788dd5f1e043446517d92Mike Lockwood        if (proximitySensorModeEnabled()) {
846039a8e51aa4a3ed27ad788dd5f1e043446517d92Mike Lockwood            return;
847039a8e51aa4a3ed27ad788dd5f1e043446517d92Mike Lockwood        }
8484472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown        mScreenTimeoutDuration = duration;
8494472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown        updatePokeLock();
8504472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown    }
851b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
8524472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown    /**
8534472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown     * Update the state of the poke lock held by the phone app,
8544472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown     * based on the current desired screen timeout and the
8554472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown     * current "ignore user activity on touch" flag.
8564472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown     */
8574472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown    private void updatePokeLock() {
858b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // This is kind of convoluted, but the basic thing to remember is
859b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // that the poke lock just sends a message to the screen to tell
860b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // it to stay on for a while.
861b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // The default is 0, for a long timeout and should be set that way
862b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // when we are heading back into a the keyguard / screen off
863b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // state, and also when we're trying to keep the screen alive
864b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // while ringing.  We'll also want to ignore the cheek events
865b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // regardless of the timeout duration.
866b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // The short timeout is really used whenever we want to give up
867b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // the screen lock, such as when we're in call.
868b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        int pokeLockSetting = LocalPowerManager.POKE_LOCK_IGNORE_CHEEK_EVENTS;
8694472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown        switch (mScreenTimeoutDuration) {
870b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            case SHORT:
871b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // Set the poke lock to timeout the display after a short
872b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // timeout (5s). This ensures that the screen goes to sleep
873b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // as soon as acceptably possible after we the wake lock
874b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // has been released.
875b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                pokeLockSetting |= LocalPowerManager.POKE_LOCK_SHORT_TIMEOUT;
876b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                break;
877b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
878b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            case MEDIUM:
879b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // Set the poke lock to timeout the display after a medium
880b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // timeout (15s). This ensures that the screen goes to sleep
881b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // as soon as acceptably possible after we the wake lock
882b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // has been released.
883b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                pokeLockSetting |= LocalPowerManager.POKE_LOCK_MEDIUM_TIMEOUT;
884b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                break;
885b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
886b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            case DEFAULT:
887b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            default:
888b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // set the poke lock to timeout the display after a long
889b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // delay by default.
890b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // TODO: it may be nice to be able to disable cheek presses
891b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // for long poke locks (emergency dialer, for instance).
892b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                break;
893b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
894b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
8954472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown        if (mIgnoreTouchUserActivity) {
8964472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown            pokeLockSetting |= LocalPowerManager.POKE_LOCK_IGNORE_TOUCH_AND_CHEEK_EVENTS;
8974472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown        }
8984472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown
899b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Send the request
900b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        try {
901b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mPowerManagerService.setPokeLock(pokeLockSetting, mPokeLockToken, LOG_TAG);
902b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        } catch (RemoteException e) {
9034472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown            Log.w(LOG_TAG, "mPowerManagerService.setPokeLock() failed: " + e);
904b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
905b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
906b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
907b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /**
908b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * Controls whether or not the screen is allowed to sleep.
909b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *
910b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * Once sleep is allowed (WakeState is SLEEP), it will rely on the
911b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * settings for the poke lock to determine when to timeout and let
912b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * the device sleep {@link PhoneApp#setScreenTimeout}.
913b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *
914b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * @param ws tells the device to how to wake.
915b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     */
916b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /* package */ void requestWakeState(WakeState ws) {
917796c70c99bc39295bf685061056f06ab8949c742David Brown        if (VDBG) Log.d(LOG_TAG, "requestWakeState(" + ws + ")...");
9187cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood        synchronized (this) {
9197cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood            if (mWakeState != ws) {
9207cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                switch (ws) {
9217cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                    case PARTIAL:
9227cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                        // acquire the processor wake lock, and release the FULL
9237cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                        // lock if it is being held.
9247cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                        mPartialWakeLock.acquire();
9257cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                        if (mWakeLock.isHeld()) {
9267cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                            mWakeLock.release();
9277cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                        }
9287cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                        break;
9297cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                    case FULL:
9307cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                        // acquire the full wake lock, and release the PARTIAL
9317cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                        // lock if it is being held.
9327cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                        mWakeLock.acquire();
9337cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                        if (mPartialWakeLock.isHeld()) {
9347cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                            mPartialWakeLock.release();
9357cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                        }
9367cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                        break;
9377cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                    case SLEEP:
9387cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                    default:
9397cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                        // release both the PARTIAL and FULL locks.
9407cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                        if (mWakeLock.isHeld()) {
9417cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                            mWakeLock.release();
9427cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                        }
9437cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                        if (mPartialWakeLock.isHeld()) {
9447cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                            mPartialWakeLock.release();
9457cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                        }
9467cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                        break;
9477cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                }
9487cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                mWakeState = ws;
949b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
950b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
951b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
952b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
953b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /**
954b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * If we are not currently keeping the screen on, then poke the power
955b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * manager to wake up the screen for the user activity timeout duration.
956b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     */
957b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /* package */ void wakeUpScreen() {
9587cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood        synchronized (this) {
9597cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood            if (mWakeState == WakeState.SLEEP) {
9607cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                if (DBG) Log.d(LOG_TAG, "pulse screen lock");
9617cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                try {
9627cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                    mPowerManagerService.userActivityWithForce(SystemClock.uptimeMillis(), false, true);
9637cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                } catch (RemoteException ex) {
9647cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                    // Ignore -- the system process is dead.
9657cef385a85853b4e3a3a61af4314aa008bb60dd8Mike Lockwood                }
966b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
967b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
968b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
969b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
970b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /**
971b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * Sets the wake state and screen timeout based on the current state
972b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * of the phone, and the current state of the in-call UI.
973b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *
974b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * This method is a "UI Policy" wrapper around
975b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * {@link PhoneApp#requestWakeState} and {@link PhoneApp#setScreenTimeout}.
976b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *
977b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * It's safe to call this method regardless of the state of the Phone
978b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * (e.g. whether or not it's idle), and regardless of the state of the
979b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * Phone UI (e.g. whether or not the InCallScreen is active.)
980b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     */
981b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /* package */ void updateWakeState() {
982b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        Phone.State state = phone.getState();
983b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
984b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // True if the in-call UI is the foreground activity.
985b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // (Note this will be false if the screen is currently off,
986b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // since in that case *no* activity is in the foreground.)
987b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        boolean isShowingCallScreen = isShowingCallScreen();
988b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
989b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // True if the InCallScreen's DTMF dialer is currently opened.
990b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // (Note this does NOT imply whether or not the InCallScreen
991b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // itself is visible.)
992b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        boolean isDialerOpened = (mInCallScreen != null) && mInCallScreen.isDialerOpened();
993b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
994b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // True if the speakerphone is in use.  (If so, we *always* use
995b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // the default timeout.  Since the user is obviously not holding
996b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // the phone up to his/her face, we don't need to worry about
997b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // false touches, and thus don't need to turn the screen off so
998b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // aggressively.)
999b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Note that we need to make a fresh call to this method any
1000b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // time the speaker state changes.  (That happens in
1001b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // PhoneUtils.turnOnSpeaker().)
1002b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        boolean isSpeakerInUse = (state == Phone.State.OFFHOOK) && PhoneUtils.isSpeakerOn(this);
1003b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1004b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // TODO (bug 1440854): The screen timeout *might* also need to
1005b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // depend on the bluetooth state, but this isn't as clear-cut as
1006b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // the speaker state (since while using BT it's common for the
1007b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // user to put the phone straight into a pocket, in which case the
1008b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // timeout should probably still be short.)
1009b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1010796c70c99bc39295bf685061056f06ab8949c742David Brown        if (DBG) Log.d(LOG_TAG, "updateWakeState: callscreen " + isShowingCallScreen
1011796c70c99bc39295bf685061056f06ab8949c742David Brown                       + ", dialer " + isDialerOpened
1012796c70c99bc39295bf685061056f06ab8949c742David Brown                       + ", speaker " + isSpeakerInUse + "...");
1013b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1014b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        //
1015b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // (1) Set the screen timeout.
1016b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        //
1017b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Note that the "screen timeout" value we determine here is
1018b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // meaningless if the screen is forced on (see (2) below.)
1019b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        //
1020b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (!isShowingCallScreen || isSpeakerInUse) {
1021b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // Use the system-wide default timeout.
1022b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            setScreenTimeout(ScreenTimeoutDuration.DEFAULT);
1023b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        } else {
1024b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // We're on the in-call screen, and *not* using the speakerphone.
1025b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            if (isDialerOpened) {
1026b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // The DTMF dialpad is up.  This case is special because
1027b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // the in-call UI has its own "touch lock" mechanism to
1028b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // disable the dialpad after a very short amount of idle
1029b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // time (to avoid false touches from the user's face while
1030b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // in-call.)
1031b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                //
1032b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // In this case the *physical* screen just uses the
1033b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // system-wide default timeout.
1034b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                setScreenTimeout(ScreenTimeoutDuration.DEFAULT);
1035b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            } else {
1036b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // We're on the in-call screen, and not using the DTMF dialpad.
1037b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // There's actually no touchable UI onscreen at all in
1038b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // this state.  Also, the user is (most likely) not
1039b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // looking at the screen at all, since they're probably
1040b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // holding the phone up to their face.  Here we use a
1041b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // special screen timeout value specific to the in-call
1042b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // screen, purely to save battery life.
1043b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                setScreenTimeout(ScreenTimeoutDuration.MEDIUM);
1044b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1045b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
1046b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1047b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        //
1048b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // (2) Decide whether to force the screen on or not.
1049b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        //
1050ee77dd0ec7fcbe315a34ef6625854a8affe31885David Brown        // Force the screen to be on if the phone is ringing or dialing,
1051ee77dd0ec7fcbe315a34ef6625854a8affe31885David Brown        // or if we're displaying the "Call ended" UI for a connection in
1052ee77dd0ec7fcbe315a34ef6625854a8affe31885David Brown        // the "disconnected" state.
1053b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        //
1054b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        boolean isRinging = (state == Phone.State.RINGING);
1055ee77dd0ec7fcbe315a34ef6625854a8affe31885David Brown        boolean isDialing = (phone.getForegroundCall().getState() == Call.State.DIALING);
1056b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        boolean showingDisconnectedConnection =
1057b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                PhoneUtils.hasDisconnectedConnections(phone) && isShowingCallScreen;
1058ee77dd0ec7fcbe315a34ef6625854a8affe31885David Brown        boolean keepScreenOn = isRinging || isDialing || showingDisconnectedConnection;
1059b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (DBG) Log.d(LOG_TAG, "updateWakeState: keepScreenOn = " + keepScreenOn
1060b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                       + " (isRinging " + isRinging
1061ee77dd0ec7fcbe315a34ef6625854a8affe31885David Brown                       + ", isDialing " + isDialing
1062796c70c99bc39295bf685061056f06ab8949c742David Brown                       + ", showingDisc " + showingDisconnectedConnection + ")");
1063b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // keepScreenOn == true means we'll hold a full wake lock:
1064b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        requestWakeState(keepScreenOn ? WakeState.FULL : WakeState.SLEEP);
1065b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
1066b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1067b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /**
1068b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * Wrapper around the PowerManagerService.preventScreenOn() API.
1069b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * This allows the in-call UI to prevent the screen from turning on
1070b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * even if a subsequent call to updateWakeState() causes us to acquire
1071b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * a full wake lock.
1072b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     */
1073b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /* package */ void preventScreenOn(boolean prevent) {
1074796c70c99bc39295bf685061056f06ab8949c742David Brown        if (VDBG) Log.d(LOG_TAG, "- preventScreenOn(" + prevent + ")...");
1075b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        try {
1076b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mPowerManagerService.preventScreenOn(prevent);
1077b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        } catch (RemoteException e) {
1078b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            Log.w(LOG_TAG, "mPowerManagerService.preventScreenOn() failed: " + e);
1079b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
1080b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
1081b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
10824472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown    /**
10834472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown     * Sets or clears the flag that tells the PowerManager that touch
10844472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown     * (and cheek) events should NOT be considered "user activity".
10854472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown     *
10864472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown     * Since the in-call UI is totally insensitive to touch in most
10874472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown     * states, we set this flag whenever the InCallScreen is in the
10884472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown     * foreground.  (Otherwise, repeated unintentional touches could
10894472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown     * prevent the device from going to sleep.)
10904472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown     *
10914472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown     * There *are* some some touch events that really do count as user
10924472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown     * activity, though.  For those, we need to manually poke the
10934472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown     * PowerManager's userActivity method; see pokeUserActivity().
10944472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown     */
10954472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown    /* package */ void setIgnoreTouchUserActivity(boolean ignore) {
10964472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown        if (VDBG) Log.d(LOG_TAG, "setIgnoreTouchUserActivity(" + ignore + ")...");
10974472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown        mIgnoreTouchUserActivity = ignore;
10984472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown        updatePokeLock();
10994472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown    }
11004472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown
11014472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown    /**
11024472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown     * Manually pokes the PowerManager's userActivity method.  Since we
11034472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown     * hold the POKE_LOCK_IGNORE_TOUCH_AND_CHEEK_EVENTS poke lock while
11044472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown     * the InCallScreen is active, we need to do this for touch events
11054472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown     * that really do count as user activity (like DTMF key presses, or
11064472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown     * unlocking the "touch lock" overlay.)
11074472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown     */
11084472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown    /* package */ void pokeUserActivity() {
11094472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown        if (VDBG) Log.d(LOG_TAG, "pokeUserActivity()...");
11104472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown        try {
11114472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown            mPowerManagerService.userActivity(SystemClock.uptimeMillis(), false);
11124472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown        } catch (RemoteException e) {
11134472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown            Log.w(LOG_TAG, "mPowerManagerService.userActivity() failed: " + e);
11144472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown        }
11154472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown    }
11164472ce7a7e8568b4bb66098c4e4a2e71099167acDavid Brown
111758de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown    /**
1118445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood     * Set when a new outgoing call is beginning, so we can update
1119445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood     * the proximity sensor state.
1120445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood     * Cleared when the InCallScreen is no longer in the foreground,
1121445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood     * in case the call fails without changing the telephony state.
1122445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood     */
1123445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood    /* package */ void setBeginningCall(boolean beginning) {
1124445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood        // Note that we are beginning a new call, for proximity sensor support
1125445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood        mBeginningCall = beginning;
1126445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood        // Update the Proximity sensor based on mBeginningCall state
1127445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood        updateProximitySensorMode(phone.getState());
1128445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood    }
1129445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood
1130445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood    /**
113158de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown     * Updates the wake lock used to control proximity sensor behavior,
113258de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown     * based on the current state of the phone.  This method is called
113358de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown     * from the CallNotifier on any phone state change.
113458de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown     *
113558de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown     * On devices that have a proximity sensor, to avoid false touches
113658de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown     * during a call, we hold a PROXIMITY_SCREEN_OFF_WAKE_LOCK wake lock
113758de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown     * whenever the phone is off hook.  (When held, that wake lock causes
113858de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown     * the screen to turn off automatically when the sensor detects an
113958de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown     * object close to the screen.)
114058de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown     *
114158de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown     * This method is a no-op for devices that don't have a proximity
114258de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown     * sensor.
114358de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown     *
114458de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown     * Note this method doesn't care if the InCallScreen is the foreground
114558de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown     * activity or not.  That's because we want the proximity sensor to be
114658de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown     * enabled any time the phone is in use, to avoid false cheek events
114758de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown     * for whatever app you happen to be running.
114858de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown     *
1149d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillai     * Proximity wake lock will *not* be held if any one of the
1150d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillai     * conditions is true while on a call:
1151d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillai     * 1) If the audio is routed via Bluetooth
1152d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillai     * 2) If a wired headset is connected
1153d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillai     * 3) if the speaker is ON
1154d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillai     * 4) If the slider is open(i.e. the hardkeyboard is *not* hidden)
115558de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown     *
115658de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown     * @param state current state of the phone (see {@link Phone#State})
115758de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown     */
115858de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown    /* package */ void updateProximitySensorMode(Phone.State state) {
1159445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood        if (VDBG) Log.d(LOG_TAG, "updateProximitySensorMode: state = " + state);
1160d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillai
1161039a8e51aa4a3ed27ad788dd5f1e043446517d92Mike Lockwood        if (proximitySensorModeEnabled()) {
116228644752309f4cf5ff68ddc8f4bbb66064f1bcefMike Lockwood            synchronized (mProximityWakeLock) {
11635bcbca1162b1b462d15ebb4a5b2859a076d382e2Mike Lockwood                // turn proximity sensor off and turn screen on immediately if
116403adaac30038029e90dbbb266bacb3f2abb32397Mike Lockwood                // we are using a headset, the keyboard is open, or the device
116503adaac30038029e90dbbb266bacb3f2abb32397Mike Lockwood                // is being held in a horizontal position.
11665bcbca1162b1b462d15ebb4a5b2859a076d382e2Mike Lockwood                boolean screenOnImmediately = (isHeadsetPlugged()
11675bcbca1162b1b462d15ebb4a5b2859a076d382e2Mike Lockwood                            || PhoneUtils.isSpeakerOn(this)
11685bcbca1162b1b462d15ebb4a5b2859a076d382e2Mike Lockwood                            || ((mBtHandsfree != null) && mBtHandsfree.isAudioOn())
116903adaac30038029e90dbbb266bacb3f2abb32397Mike Lockwood                            || mIsHardKeyboardOpen
117003adaac30038029e90dbbb266bacb3f2abb32397Mike Lockwood                            || mOrientation == AccelerometerListener.ORIENTATION_HORIZONTAL);
11715bcbca1162b1b462d15ebb4a5b2859a076d382e2Mike Lockwood
117203adaac30038029e90dbbb266bacb3f2abb32397Mike Lockwood                if (((state == Phone.State.OFFHOOK) || mBeginningCall) && !screenOnImmediately) {
117328644752309f4cf5ff68ddc8f4bbb66064f1bcefMike Lockwood                    // Phone is in use!  Arrange for the screen to turn off
117428644752309f4cf5ff68ddc8f4bbb66064f1bcefMike Lockwood                    // automatically when the sensor detects a close object.
117528644752309f4cf5ff68ddc8f4bbb66064f1bcefMike Lockwood                    if (!mProximityWakeLock.isHeld()) {
117628644752309f4cf5ff68ddc8f4bbb66064f1bcefMike Lockwood                        if (DBG) Log.d(LOG_TAG, "updateProximitySensorMode: acquiring...");
117728644752309f4cf5ff68ddc8f4bbb66064f1bcefMike Lockwood                        mProximityWakeLock.acquire();
117828644752309f4cf5ff68ddc8f4bbb66064f1bcefMike Lockwood                    } else {
117928644752309f4cf5ff68ddc8f4bbb66064f1bcefMike Lockwood                        if (VDBG) Log.d(LOG_TAG, "updateProximitySensorMode: lock already held.");
118028644752309f4cf5ff68ddc8f4bbb66064f1bcefMike Lockwood                    }
1181f0f6d973992a988c0fc801ba80f826c8edf02c03David Brown                } else {
118228644752309f4cf5ff68ddc8f4bbb66064f1bcefMike Lockwood                    // Phone is either idle, or ringing.  We don't want any
118328644752309f4cf5ff68ddc8f4bbb66064f1bcefMike Lockwood                    // special proximity sensor behavior in either case.
118428644752309f4cf5ff68ddc8f4bbb66064f1bcefMike Lockwood                    if (mProximityWakeLock.isHeld()) {
118528644752309f4cf5ff68ddc8f4bbb66064f1bcefMike Lockwood                        if (DBG) Log.d(LOG_TAG, "updateProximitySensorMode: releasing...");
11865bcbca1162b1b462d15ebb4a5b2859a076d382e2Mike Lockwood                        // Wait until user has moved the phone away from his head if we are
11875bcbca1162b1b462d15ebb4a5b2859a076d382e2Mike Lockwood                        // releasing due to the phone call ending.
11885bcbca1162b1b462d15ebb4a5b2859a076d382e2Mike Lockwood                        // Qtherwise, turn screen on immediately
11895bcbca1162b1b462d15ebb4a5b2859a076d382e2Mike Lockwood                        int flags =
11905bcbca1162b1b462d15ebb4a5b2859a076d382e2Mike Lockwood                            (screenOnImmediately ? 0 : PowerManager.WAIT_FOR_PROXIMITY_NEGATIVE);
11915bcbca1162b1b462d15ebb4a5b2859a076d382e2Mike Lockwood                        mProximityWakeLock.release(flags);
119228644752309f4cf5ff68ddc8f4bbb66064f1bcefMike Lockwood                    } else {
119328644752309f4cf5ff68ddc8f4bbb66064f1bcefMike Lockwood                        if (VDBG) {
119428644752309f4cf5ff68ddc8f4bbb66064f1bcefMike Lockwood                            Log.d(LOG_TAG, "updateProximitySensorMode: lock already released.");
119528644752309f4cf5ff68ddc8f4bbb66064f1bcefMike Lockwood                        }
119628644752309f4cf5ff68ddc8f4bbb66064f1bcefMike Lockwood                    }
119758de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown                }
119858de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown            }
119958de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown        }
120058de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown    }
120158de0c5d10bbf48f6316a12495cd1c176a11a6feDavid Brown
120203adaac30038029e90dbbb266bacb3f2abb32397Mike Lockwood    public void orientationChanged(int orientation) {
120303adaac30038029e90dbbb266bacb3f2abb32397Mike Lockwood        mOrientation = orientation;
120403adaac30038029e90dbbb266bacb3f2abb32397Mike Lockwood        updateProximitySensorMode(phone.getState());
120503adaac30038029e90dbbb266bacb3f2abb32397Mike Lockwood    }
120603adaac30038029e90dbbb266bacb3f2abb32397Mike Lockwood
1207f0f6d973992a988c0fc801ba80f826c8edf02c03David Brown    /**
1208445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood     * Notifies the phone app when the phone state changes.
1209445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood     * Currently used only for proximity sensor support.
1210445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood     */
1211445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood    /* package */ void updatePhoneState(Phone.State state) {
1212445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood        if (state != mLastPhoneState) {
1213445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood            mLastPhoneState = state;
1214445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood            updateProximitySensorMode(state);
121503adaac30038029e90dbbb266bacb3f2abb32397Mike Lockwood            if (mAccelerometerListener != null) {
121603adaac30038029e90dbbb266bacb3f2abb32397Mike Lockwood                // use accelerometer to augment proximity sensor when in call
121703adaac30038029e90dbbb266bacb3f2abb32397Mike Lockwood                mOrientation = AccelerometerListener.ORIENTATION_UNKNOWN;
121803adaac30038029e90dbbb266bacb3f2abb32397Mike Lockwood                mAccelerometerListener.enable(state == Phone.State.OFFHOOK);
121903adaac30038029e90dbbb266bacb3f2abb32397Mike Lockwood            }
1220445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood            // clear our beginning call flag
1221445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood            mBeginningCall = false;
12226a8928a7265b6586b561e60ee93b23f5fa6f30eaMike Lockwood            // While we are in call, the in-call screen should dismiss the keyguard.
12236a8928a7265b6586b561e60ee93b23f5fa6f30eaMike Lockwood            // This allows the user to press Home to go directly home without going through
12246a8928a7265b6586b561e60ee93b23f5fa6f30eaMike Lockwood            // an insecure lock screen.
12256a8928a7265b6586b561e60ee93b23f5fa6f30eaMike Lockwood            // But we do not want to do this if there is no active call so we do not
12266a8928a7265b6586b561e60ee93b23f5fa6f30eaMike Lockwood            // bypass the keyguard if the call is not answered or declined.
12276a8928a7265b6586b561e60ee93b23f5fa6f30eaMike Lockwood            if (mInCallScreen != null) {
12286a8928a7265b6586b561e60ee93b23f5fa6f30eaMike Lockwood                mInCallScreen.updateKeyguardPolicy(state == Phone.State.OFFHOOK);
12296a8928a7265b6586b561e60ee93b23f5fa6f30eaMike Lockwood            }
1230445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood        }
1231445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood    }
1232445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood
12336a8928a7265b6586b561e60ee93b23f5fa6f30eaMike Lockwood    /* package */ Phone.State getPhoneState() {
12346a8928a7265b6586b561e60ee93b23f5fa6f30eaMike Lockwood        return mLastPhoneState;
12356a8928a7265b6586b561e60ee93b23f5fa6f30eaMike Lockwood    }
12366a8928a7265b6586b561e60ee93b23f5fa6f30eaMike Lockwood
1237445c9030dc93750a8209ace086ea3820ee33f0b1Mike Lockwood    /**
1238f0f6d973992a988c0fc801ba80f826c8edf02c03David Brown     * @return true if this device supports the "proximity sensor
1239f0f6d973992a988c0fc801ba80f826c8edf02c03David Brown     * auto-lock" feature while in-call (see updateProximitySensorMode()).
1240f0f6d973992a988c0fc801ba80f826c8edf02c03David Brown     */
1241f0f6d973992a988c0fc801ba80f826c8edf02c03David Brown    /* package */ boolean proximitySensorModeEnabled() {
1242f0f6d973992a988c0fc801ba80f826c8edf02c03David Brown        return (mProximityWakeLock != null);
1243f0f6d973992a988c0fc801ba80f826c8edf02c03David Brown    }
1244f0f6d973992a988c0fc801ba80f826c8edf02c03David Brown
1245b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    KeyguardManager getKeyguardManager() {
1246b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return mKeyguardManager;
1247b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
1248b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1249b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private void onMMIComplete(AsyncResult r) {
1250796c70c99bc39295bf685061056f06ab8949c742David Brown        if (VDBG) Log.d(LOG_TAG, "onMMIComplete()...");
1251b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        MmiCode mmiCode = (MmiCode) r.result;
1252b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        PhoneUtils.displayMMIComplete(phone, getInstance(), mmiCode, null, null);
1253b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
1254b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1255a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville    private void initForNewRadioTechnology() {
125687258b2b589c3afea4b89b840251012378e0d3b4David Brown        if (DBG) Log.d(LOG_TAG, "initForNewRadioTechnology...");
1257a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville
125879b9f6e30e8402b103f1ec3ed9dda1fcad2b1cd5Tammo Spalink        if (phone.getPhoneType() == Phone.PHONE_TYPE_CDMA) {
125960877272381161201c6969dea501e683705d6e32w            // Create an instance of CdmaPhoneCallState and initialize it to IDLE
126060877272381161201c6969dea501e683705d6e32w            cdmaPhoneCallState = new CdmaPhoneCallState();
126160877272381161201c6969dea501e683705d6e32w            cdmaPhoneCallState.CdmaPhoneCallStateInit();
126260877272381161201c6969dea501e683705d6e32w
126360877272381161201c6969dea501e683705d6e32w            //create instances of CDMA OTA data classes
126460877272381161201c6969dea501e683705d6e32w            if (cdmaOtaProvisionData == null) {
126560877272381161201c6969dea501e683705d6e32w                cdmaOtaProvisionData = new OtaUtils.CdmaOtaProvisionData();
126660877272381161201c6969dea501e683705d6e32w            }
126760877272381161201c6969dea501e683705d6e32w            if (cdmaOtaConfigData == null) {
126860877272381161201c6969dea501e683705d6e32w                cdmaOtaConfigData = new OtaUtils.CdmaOtaConfigData();
126960877272381161201c6969dea501e683705d6e32w            }
127060877272381161201c6969dea501e683705d6e32w            if (cdmaOtaScreenState == null) {
127160877272381161201c6969dea501e683705d6e32w                cdmaOtaScreenState = new OtaUtils.CdmaOtaScreenState();
127260877272381161201c6969dea501e683705d6e32w            }
12736646249c7f8414cd1e4e05f211b2a752fae59dddJohn Joseph            if (cdmaOtaInCallScreenUiState == null) {
12746646249c7f8414cd1e4e05f211b2a752fae59dddJohn Joseph                cdmaOtaInCallScreenUiState = new OtaUtils.CdmaOtaInCallScreenUiState();
12756646249c7f8414cd1e4e05f211b2a752fae59dddJohn Joseph            }
127660877272381161201c6969dea501e683705d6e32w        }
127760877272381161201c6969dea501e683705d6e32w
1278a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville        ringer.updateRingerContextAfterRadioTechnologyChange(this.phone);
1279a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville        notifier.updateCallNotifierRegistrationsAfterRadioTechnologyChange();
128087258b2b589c3afea4b89b840251012378e0d3b4David Brown        if (mBtHandsfree != null) {
1281a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville            mBtHandsfree.updateBtHandsfreeAfterRadioTechnologyChange();
1282a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville        }
128387258b2b589c3afea4b89b840251012378e0d3b4David Brown        if (mInCallScreen != null) {
128487258b2b589c3afea4b89b840251012378e0d3b4David Brown            mInCallScreen.updateAfterRadioTechnologyChange();
128587258b2b589c3afea4b89b840251012378e0d3b4David Brown        }
1286a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville
128787258b2b589c3afea4b89b840251012378e0d3b4David Brown        // Update registration for ICC status after radio technology change
1288a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville        IccCard sim = phone.getIccCard();
1289a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville        if (sim != null) {
129087258b2b589c3afea4b89b840251012378e0d3b4David Brown            if (DBG) Log.d(LOG_TAG, "Update registration for ICC status...");
1291a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville
1292a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville            //Register all events new to the new active phone
1293a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville            sim.registerForNetworkLocked(mHandler, EVENT_SIM_NETWORK_LOCKED, null);
1294a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville        }
1295a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville    }
1296a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville
1297404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville
1298404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville    /**
1299b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * @return true if a wired headset is currently plugged in.
1300b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *
1301b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * @see Intent.ACTION_HEADSET_PLUG (which we listen for in mReceiver.onReceive())
1302b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     */
1303b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    boolean isHeadsetPlugged() {
1304b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return mIsHeadsetPlugged;
1305b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
1306b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
13078bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project    /**
13088bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project     * @return true if the onscreen UI should currently be showing the
13098bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project     * special "bluetooth is active" indication in a couple of places (in
13108bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project     * which UI elements turn blue and/or show the bluetooth logo.)
13118bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project     *
13128bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project     * This depends on the BluetoothHeadset state *and* the current
13138bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project     * telephony state; see shouldShowBluetoothIndication().
13148bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project     *
13158bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project     * @see CallCard
13168bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project     * @see NotificationMgr.updateInCallNotification
13178bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project     */
13188bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project    /* package */ boolean showBluetoothIndication() {
13198bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project        return mShowBluetoothIndication;
13208bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project    }
13218bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project
13228bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project    /**
13238bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project     * Recomputes the mShowBluetoothIndication flag based on the current
13248bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project     * bluetooth state and current telephony state.
13258bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project     *
13268bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project     * This needs to be called any time the bluetooth headset state or the
13278bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project     * telephony state changes.
13284b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project     *
13294b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project     * @param forceUiUpdate if true, force the UI elements that care
13304b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project     *                      about this flag to update themselves.
13318bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project     */
13324b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project    /* package */ void updateBluetoothIndication(boolean forceUiUpdate) {
13338bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project        mShowBluetoothIndication = shouldShowBluetoothIndication(mBluetoothHeadsetState,
13344b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project                                                                 mBluetoothHeadsetAudioState,
13358bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project                                                                 phone);
13364b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project        if (forceUiUpdate) {
13374b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project            // Post Handler messages to the various components that might
13384b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project            // need to be refreshed based on the new state.
1339325cc2ced6f1ff5fb1708abfcc5e9c73ac0cd962David Brown            if (isShowingCallScreen()) mInCallScreen.requestUpdateBluetoothIndication();
13404b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project            mHandler.sendEmptyMessage(EVENT_UPDATE_INCALL_NOTIFICATION);
13414b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project        }
1342d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillai
1343d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillai        // Update the Proximity sensor based on Bluetooth audio state
1344d3744ff481f7d96669740b015a39d7441208152bAbhishek Pillai        updateProximitySensorMode(phone.getState());
13458bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project    }
13468bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project
13478bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project    /**
13488bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project     * UI policy helper function for the couple of places in the UI that
13498bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project     * have some way of indicating that "bluetooth is in use."
13508bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project     *
13518bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project     * @return true if the onscreen UI should indicate that "bluetooth is in use",
13528bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project     *         based on the specified bluetooth headset state, and the
13538bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project     *         current state of the phone.
13548bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project     * @see showBluetoothIndication()
13558bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project     */
13568bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project    private static boolean shouldShowBluetoothIndication(int bluetoothState,
13574b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project                                                         int bluetoothAudioState,
13588bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project                                                         Phone phone) {
13598bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project        // We want the UI to indicate that "bluetooth is in use" in two
13608bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project        // slightly different cases:
13618bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project        //
13628bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project        // (a) The obvious case: if a bluetooth headset is currently in
13638bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project        //     use for an ongoing call.
13648bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project        //
13658bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project        // (b) The not-so-obvious case: if an incoming call is ringing,
13668bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project        //     and we expect that audio *will* be routed to a bluetooth
13678bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project        //     headset once the call is answered.
13688bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project
13698bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project        switch (phone.getState()) {
13708bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project            case OFFHOOK:
13718bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project                // This covers normal active calls, and also the case if
13724b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project                // the foreground call is DIALING or ALERTING.  In this
13734b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project                // case, bluetooth is considered "active" if a headset
13744b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project                // is connected *and* audio is being routed to it.
13754b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project                return ((bluetoothState == BluetoothHeadset.STATE_CONNECTED)
13764b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project                        && (bluetoothAudioState == BluetoothHeadset.AUDIO_STATE_CONNECTED));
13778bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project
13788bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project            case RINGING:
13798bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project                // If an incoming call is ringing, we're *not* yet routing
13808bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project                // audio to the headset (since there's no in-call audio
13818bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project                // yet!)  In this case, if a bluetooth headset is
13828bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project                // connected at all, we assume that it'll become active
13838bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project                // once the user answers the phone.
13848bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project                return (bluetoothState == BluetoothHeadset.STATE_CONNECTED);
13858bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project
13868bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project            default:  // Presumably IDLE
13878bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project                return false;
13888bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project        }
13898bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project    }
13908bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project
1391b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1392b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /**
1393b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * Receiver for misc intent broadcasts the Phone app cares about.
1394b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     */
1395b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private class PhoneAppBroadcastReceiver extends BroadcastReceiver {
1396b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        @Override
1397b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        public void onReceive(Context context, Intent intent) {
1398b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            String action = intent.getAction();
1399b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            if (action.equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) {
1400b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                boolean enabled = System.getInt(getContentResolver(),
1401b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        System.AIRPLANE_MODE_ON, 0) == 0;
1402b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                phone.setRadioPower(enabled);
140318e0a07cc700234f5a02fe10eee9b3ea10db58b3Nick Pelly            } else if (action.equals(BluetoothHeadset.ACTION_STATE_CHANGED)) {
140418e0a07cc700234f5a02fe10eee9b3ea10db58b3Nick Pelly                mBluetoothHeadsetState = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE,
14058bb467d9a1106dedd79e42166c7b6e9fc9a897a7The Android Open Source Project                                                            BluetoothHeadset.STATE_ERROR);
1406796c70c99bc39295bf685061056f06ab8949c742David Brown                if (VDBG) Log.d(LOG_TAG, "mReceiver: HEADSET_STATE_CHANGED_ACTION");
1407796c70c99bc39295bf685061056f06ab8949c742David Brown                if (VDBG) Log.d(LOG_TAG, "==> new state: " + mBluetoothHeadsetState);
14084b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project                updateBluetoothIndication(true);  // Also update any visible UI if necessary
140918e0a07cc700234f5a02fe10eee9b3ea10db58b3Nick Pelly            } else if (action.equals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)) {
14104b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project                mBluetoothHeadsetAudioState =
141118e0a07cc700234f5a02fe10eee9b3ea10db58b3Nick Pelly                        intent.getIntExtra(BluetoothHeadset.EXTRA_AUDIO_STATE,
14124b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project                                           BluetoothHeadset.STATE_ERROR);
1413796c70c99bc39295bf685061056f06ab8949c742David Brown                if (VDBG) Log.d(LOG_TAG, "mReceiver: HEADSET_AUDIO_STATE_CHANGED_ACTION");
1414796c70c99bc39295bf685061056f06ab8949c742David Brown                if (VDBG) Log.d(LOG_TAG, "==> new state: " + mBluetoothHeadsetAudioState);
14154b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project                updateBluetoothIndication(true);  // Also update any visible UI if necessary
1416b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            } else if (action.equals(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED)) {
141787258b2b589c3afea4b89b840251012378e0d3b4David Brown                if (VDBG) Log.d(LOG_TAG, "mReceiver: ACTION_ANY_DATA_CONNECTION_STATE_CHANGED");
141887258b2b589c3afea4b89b840251012378e0d3b4David Brown                if (VDBG) Log.d(LOG_TAG, "- state: " + intent.getStringExtra(Phone.STATE_KEY));
141987258b2b589c3afea4b89b840251012378e0d3b4David Brown                if (VDBG) Log.d(LOG_TAG, "- reason: "
142087258b2b589c3afea4b89b840251012378e0d3b4David Brown                                + intent.getStringExtra(Phone.STATE_CHANGE_REASON_KEY));
14214079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project
14224079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project                // The "data disconnected due to roaming" notification is
14234079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project                // visible if you've lost data connectivity because you're
14244079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project                // roaming and you have the "data roaming" feature turned off.
14254079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project                boolean disconnectedDueToRoaming = false;
1426b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if ("DISCONNECTED".equals(intent.getStringExtra(Phone.STATE_KEY))) {
1427b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    String reason = intent.getStringExtra(Phone.STATE_CHANGE_REASON_KEY);
1428b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    if (Phone.REASON_ROAMING_ON.equals(reason)) {
14294079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project                        // We just lost our data connection, and the reason
14304079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project                        // is that we started roaming.  This implies that
14314079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project                        // the user has data roaming turned off.
14324079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project                        disconnectedDueToRoaming = true;
1433b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    }
1434b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
14354079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project                mHandler.sendEmptyMessage(disconnectedDueToRoaming
14364079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project                                          ? EVENT_DATA_ROAMING_DISCONNECTED
14374079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project                                          : EVENT_DATA_ROAMING_OK);
1438b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            } else if (action.equals(Intent.ACTION_HEADSET_PLUG)) {
1439796c70c99bc39295bf685061056f06ab8949c742David Brown                if (VDBG) Log.d(LOG_TAG, "mReceiver: ACTION_HEADSET_PLUG");
1440796c70c99bc39295bf685061056f06ab8949c742David Brown                if (VDBG) Log.d(LOG_TAG, "    state: " + intent.getIntExtra("state", 0));
1441796c70c99bc39295bf685061056f06ab8949c742David Brown                if (VDBG) Log.d(LOG_TAG, "    name: " + intent.getStringExtra("name"));
1442b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                mIsHeadsetPlugged = (intent.getIntExtra("state", 0) == 1);
1443b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                mHandler.sendMessage(mHandler.obtainMessage(EVENT_WIRED_HEADSET_PLUG, 0));
1444b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            } else if (action.equals(Intent.ACTION_BATTERY_LOW)) {
1445796c70c99bc39295bf685061056f06ab8949c742David Brown                if (VDBG) Log.d(LOG_TAG, "mReceiver: ACTION_BATTERY_LOW");
1446b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                notifier.sendBatteryLow();  // Play a warning tone if in-call
1447b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            } else if ((action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) &&
1448b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    (mPUKEntryActivity != null)) {
1449b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // if an attempt to un-PUK-lock the device was made, while we're
1450b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // receiving this state change notification, notify the handler.
1451b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // NOTE: This is ONLY triggered if an attempt to un-PUK-lock has
1452b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // been attempted.
1453b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                mHandler.sendMessage(mHandler.obtainMessage(EVENT_SIM_STATE_CHANGED,
1454a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville                        intent.getStringExtra(IccCard.INTENT_KEY_ICC_STATE)));
1455a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville            } else if (action.equals(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED)) {
1456a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville                String newPhone = intent.getStringExtra(Phone.PHONE_NAME_KEY);
1457a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville                Log.d(LOG_TAG, "Radio technology switched. Now " + newPhone + " is active.");
1458a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville                initForNewRadioTechnology();
1459404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville            } else if (action.equals(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED)) {
1460404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville                handleServiceStateChanged(intent);
1461abeff00b5242e795baa8f5271525c5911f649993Wink Saville            } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) {
146279b9f6e30e8402b103f1ec3ed9dda1fcad2b1cd5Tammo Spalink                if (phone.getPhoneType() == Phone.PHONE_TYPE_CDMA) {
14634e879989bb6e23763802ceb128e8785f8f063ac0Robert Kowalski                    Log.d(LOG_TAG, "Emergency Callback Mode arrived in PhoneApp.");
14644e879989bb6e23763802ceb128e8785f8f063ac0Robert Kowalski                    // Start Emergency Callback Mode service
14654e879989bb6e23763802ceb128e8785f8f063ac0Robert Kowalski                    if (intent.getBooleanExtra("phoneinECMState", false)) {
14664e879989bb6e23763802ceb128e8785f8f063ac0Robert Kowalski                        context.startService(new Intent(context,
14674e879989bb6e23763802ceb128e8785f8f063ac0Robert Kowalski                                EmergencyCallbackModeService.class));
14684e879989bb6e23763802ceb128e8785f8f063ac0Robert Kowalski                    }
14694e879989bb6e23763802ceb128e8785f8f063ac0Robert Kowalski                } else {
14704e879989bb6e23763802ceb128e8785f8f063ac0Robert Kowalski                    Log.e(LOG_TAG, "Error! Emergency Callback Mode not supported for " +
14714e879989bb6e23763802ceb128e8785f8f063ac0Robert Kowalski                            phone.getPhoneName() + " phones");
14724e879989bb6e23763802ceb128e8785f8f063ac0Robert Kowalski                }
14732eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu            } else if (action.equals(Intent.ACTION_DOCK_EVENT)) {
14742eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu                mDockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
14752eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu                        Intent.EXTRA_DOCK_STATE_UNDOCKED);
14762eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu                if (VDBG) Log.d(LOG_TAG, "ACTION_DOCK_EVENT -> mDockState = " + mDockState);
14772eba6fabd0bedaf2a59da643ce475586b62f0ff9Peng Zhu                mHandler.sendMessage(mHandler.obtainMessage(EVENT_DOCK_STATE_CHANGED, 0));
1478dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent            } else if (action.equals(TtyIntent.TTY_PREFERRED_MODE_CHANGE_ACTION)) {
1479dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                mPreferredTtyMode = intent.getIntExtra(TtyIntent.TTY_PREFFERED_MODE,
1480dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                                                       Phone.TTY_MODE_OFF);
1481dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                if (VDBG) Log.d(LOG_TAG, "mReceiver: TTY_PREFERRED_MODE_CHANGE_ACTION");
1482dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                if (VDBG) Log.d(LOG_TAG, "    mode: " + mPreferredTtyMode);
1483dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                mHandler.sendMessage(mHandler.obtainMessage(EVENT_TTY_PREFERRED_MODE_CHANGED, 0));
148447765dadd119fedd04f40f4474147b4ca0416e68David Ingram            } else if (action.equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
148547765dadd119fedd04f40f4474147b4ca0416e68David Ingram                int ringerMode = intent.getIntExtra(AudioManager.EXTRA_RINGER_MODE,
148647765dadd119fedd04f40f4474147b4ca0416e68David Ingram                        AudioManager.RINGER_MODE_NORMAL);
148747765dadd119fedd04f40f4474147b4ca0416e68David Ingram                if(ringerMode == AudioManager.RINGER_MODE_SILENT) {
148847765dadd119fedd04f40f4474147b4ca0416e68David Ingram                    notifier.silenceRinger();
148947765dadd119fedd04f40f4474147b4ca0416e68David Ingram                }
1490b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1491b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
1492b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
1493b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1494b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /**
1495b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * Broadcast receiver for the ACTION_MEDIA_BUTTON broadcast intent.
1496b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *
1497b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * This functionality isn't lumped in with the other intents in
1498b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * PhoneAppBroadcastReceiver because we instantiate this as a totally
1499b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * separate BroadcastReceiver instance, since we need to manually
1500b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * adjust its IntentFilter's priority (to make sure we get these
1501b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * intents *before* the media player.)
1502b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     */
1503b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private class MediaButtonBroadcastReceiver extends BroadcastReceiver {
1504b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        @Override
1505b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        public void onReceive(Context context, Intent intent) {
1506b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
1507796c70c99bc39295bf685061056f06ab8949c742David Brown            if (VDBG) Log.d(LOG_TAG,
1508b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                           "MediaButtonBroadcastReceiver.onReceive()...  event = " + event);
1509b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            if ((event != null)
1510893d0233193b2095293d890a9ad1c73b1dc6a9a2mah                && (event.getKeyCode() == KeyEvent.KEYCODE_HEADSETHOOK)) {
1511893d0233193b2095293d890a9ad1c73b1dc6a9a2mah                if (VDBG) Log.d(LOG_TAG, "MediaButtonBroadcastReceiver: HEADSETHOOK");
1512893d0233193b2095293d890a9ad1c73b1dc6a9a2mah                boolean consumed = PhoneUtils.handleHeadsetHook(phone, event);
1513893d0233193b2095293d890a9ad1c73b1dc6a9a2mah                if (VDBG) Log.d(LOG_TAG, "==> handleHeadsetHook(): consumed = " + consumed);
1514893d0233193b2095293d890a9ad1c73b1dc6a9a2mah                if (consumed) {
1515893d0233193b2095293d890a9ad1c73b1dc6a9a2mah                    // If a headset is attached and the press is consumed, also update
1516893d0233193b2095293d890a9ad1c73b1dc6a9a2mah                    // any UI items (such as an InCallScreen mute button) that may need to
1517893d0233193b2095293d890a9ad1c73b1dc6a9a2mah                    // be updated if their state changed.
1518893d0233193b2095293d890a9ad1c73b1dc6a9a2mah                    if (isShowingCallScreen()) {
1519893d0233193b2095293d890a9ad1c73b1dc6a9a2mah                        updateInCallScreenTouchUi();
1520b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    }
1521893d0233193b2095293d890a9ad1c73b1dc6a9a2mah                    abortBroadcast();
1522893d0233193b2095293d890a9ad1c73b1dc6a9a2mah                }
1523893d0233193b2095293d890a9ad1c73b1dc6a9a2mah            } else {
1524893d0233193b2095293d890a9ad1c73b1dc6a9a2mah                if (phone.getState() != Phone.State.IDLE) {
1525893d0233193b2095293d890a9ad1c73b1dc6a9a2mah                    // If the phone is anything other than completely idle,
1526893d0233193b2095293d890a9ad1c73b1dc6a9a2mah                    // then we consume and ignore any media key events,
1527893d0233193b2095293d890a9ad1c73b1dc6a9a2mah                    // Otherwise it is too easy to accidentally start
1528893d0233193b2095293d890a9ad1c73b1dc6a9a2mah                    // playing music while a phone call is in progress.
1529893d0233193b2095293d890a9ad1c73b1dc6a9a2mah                    if (VDBG) Log.d(LOG_TAG, "MediaButtonBroadcastReceiver: consumed");
1530b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    abortBroadcast();
1531b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
1532b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1533b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
1534b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
1535404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville
1536404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville    private void handleServiceStateChanged(Intent intent) {
15374e41cdf8b65434a324f3e83be1095689ddabd153Wink Saville        /**
15384e41cdf8b65434a324f3e83be1095689ddabd153Wink Saville         * This used to handle updating EriTextWidgetProvider this routine
15394e41cdf8b65434a324f3e83be1095689ddabd153Wink Saville         * and and listening for ACTION_SERVICE_STATE_CHANGED intents could
15404e41cdf8b65434a324f3e83be1095689ddabd153Wink Saville         * be removed. But leaving just in case it might be needed in the near
15414e41cdf8b65434a324f3e83be1095689ddabd153Wink Saville         * future.
15424e41cdf8b65434a324f3e83be1095689ddabd153Wink Saville         */
15434e41cdf8b65434a324f3e83be1095689ddabd153Wink Saville
1544404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville        // If service just returned, start sending out the queued messages
1545404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville        ServiceState ss = ServiceState.newFromBundle(intent.getExtras());
1546404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville
1547404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville        boolean hasService = true;
1548404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville        boolean isCdma = false;
1549404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville        String eriText = "";
1550404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville
1551404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville        if (ss != null) {
1552404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville            int state = ss.getState();
155399d10538d840f0103c36dafa44db9f649ca884c8johnwang            NotificationMgr.getDefault().updateNetworkSelection(state);
1554404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville            switch (state) {
1555404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville                case ServiceState.STATE_OUT_OF_SERVICE:
1556404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville                case ServiceState.STATE_POWER_OFF:
1557404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville                    hasService = false;
1558404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville                    break;
1559404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville            }
1560404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville        } else {
1561404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville            hasService = false;
1562404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville        }
1563404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville    }
1564af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville
1565af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville    public boolean isOtaCallInActiveState() {
1566af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville        boolean otaCallActive = false;
1567af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville        if (mInCallScreen != null) {
1568af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville            otaCallActive = mInCallScreen.isOtaCallInActiveState();
1569af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville        }
15701e8a3579f8e49fd1f1a078d6be6864a6a8bac306David Brown        if (VDBG) Log.d(LOG_TAG, "- isOtaCallInActiveState " + otaCallActive);
1571af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville        return otaCallActive;
1572af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville    }
1573af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville
1574af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville    public boolean isOtaCallInEndState() {
1575af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville        boolean otaCallEnded = false;
1576af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville        if (mInCallScreen != null) {
1577af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville            otaCallEnded = mInCallScreen.isOtaCallInEndState();
1578af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville        }
15791e8a3579f8e49fd1f1a078d6be6864a6a8bac306David Brown        if (VDBG) Log.d(LOG_TAG, "- isOtaCallInEndState " + otaCallEnded);
1580af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville        return otaCallEnded;
1581af684393e2c6d0c051b9d5a189c2cd1de2506831Wink Saville    }
158273caead845b30580010a7502e4b7551d21dfa265Matthew Sun
158373caead845b30580010a7502e4b7551d21dfa265Matthew Sun    // it is safe to call clearOtaState() even if the InCallScreen isn't active
158473caead845b30580010a7502e4b7551d21dfa265Matthew Sun    public void clearOtaState() {
15851e8a3579f8e49fd1f1a078d6be6864a6a8bac306David Brown        if (DBG) Log.d(LOG_TAG, "- clearOtaState ...");
158673caead845b30580010a7502e4b7551d21dfa265Matthew Sun        if ((mInCallScreen != null)
158773caead845b30580010a7502e4b7551d21dfa265Matthew Sun                && (mInCallScreen.otaUtils != null)) {
15884a5a966f0d43e04d336be1b94182346399a931f6Wink Saville            mInCallScreen.otaUtils.cleanOtaScreen(true);
15891e8a3579f8e49fd1f1a078d6be6864a6a8bac306David Brown            if (DBG) Log.d(LOG_TAG, "  - clearOtaState clears OTA screen");
159073caead845b30580010a7502e4b7551d21dfa265Matthew Sun        }
159173caead845b30580010a7502e4b7551d21dfa265Matthew Sun    }
159273caead845b30580010a7502e4b7551d21dfa265Matthew Sun
159373caead845b30580010a7502e4b7551d21dfa265Matthew Sun    // it is safe to call dismissOtaDialogs() even if the InCallScreen isn't active
159473caead845b30580010a7502e4b7551d21dfa265Matthew Sun    public void dismissOtaDialogs() {
15951e8a3579f8e49fd1f1a078d6be6864a6a8bac306David Brown        if (DBG) Log.d(LOG_TAG, "- dismissOtaDialogs ...");
159673caead845b30580010a7502e4b7551d21dfa265Matthew Sun        if ((mInCallScreen != null)
159773caead845b30580010a7502e4b7551d21dfa265Matthew Sun                && (mInCallScreen.otaUtils != null)) {
159873caead845b30580010a7502e4b7551d21dfa265Matthew Sun            mInCallScreen.otaUtils.dismissAllOtaDialogs();
15991e8a3579f8e49fd1f1a078d6be6864a6a8bac306David Brown            if (DBG) Log.d(LOG_TAG, "  - dismissOtaDialogs clears OTA dialogs");
160073caead845b30580010a7502e4b7551d21dfa265Matthew Sun        }
160173caead845b30580010a7502e4b7551d21dfa265Matthew Sun    }
160273caead845b30580010a7502e4b7551d21dfa265Matthew Sun
160373caead845b30580010a7502e4b7551d21dfa265Matthew Sun    // it is safe to call clearInCallScreenMode() even if the InCallScreen isn't active
160473caead845b30580010a7502e4b7551d21dfa265Matthew Sun    public void clearInCallScreenMode() {
16051e8a3579f8e49fd1f1a078d6be6864a6a8bac306David Brown        if (DBG) Log.d(LOG_TAG, "- clearInCallScreenMode ...");
160673caead845b30580010a7502e4b7551d21dfa265Matthew Sun        if (mInCallScreen != null) {
160773caead845b30580010a7502e4b7551d21dfa265Matthew Sun            mInCallScreen.resetInCallScreenMode();
160873caead845b30580010a7502e4b7551d21dfa265Matthew Sun        }
160973caead845b30580010a7502e4b7551d21dfa265Matthew Sun    }
161039a84144262f12df44861d746788b9de92259e4fAbhishek Pillai
161139a84144262f12df44861d746788b9de92259e4fAbhishek Pillai    // Update InCallScreen's touch UI. It is safe to call even if InCallScreen isn't active
161239a84144262f12df44861d746788b9de92259e4fAbhishek Pillai    public void updateInCallScreenTouchUi() {
161339a84144262f12df44861d746788b9de92259e4fAbhishek Pillai        if (DBG) Log.d(LOG_TAG, "- updateInCallScreenTouchUi ...");
161439a84144262f12df44861d746788b9de92259e4fAbhishek Pillai        if (mInCallScreen != null) {
161539a84144262f12df44861d746788b9de92259e4fAbhishek Pillai            mInCallScreen.requestUpdateTouchUi();
161639a84144262f12df44861d746788b9de92259e4fAbhishek Pillai        }
161739a84144262f12df44861d746788b9de92259e4fAbhishek Pillai    }
1618dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent
1619dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent    private void handleQueryTTYModeResponse(Message msg) {
1620dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent        AsyncResult ar = (AsyncResult) msg.obj;
1621dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent        if (ar.exception != null) {
1622dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent            if (DBG) Log.d(LOG_TAG, "handleQueryTTYModeResponse: Error getting TTY state.");
1623dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent        } else {
1624dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent            if (DBG) Log.d(LOG_TAG,
1625dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                           "handleQueryTTYModeResponse: TTY enable state successfully queried.");
1626dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent
1627dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent            int ttymode = ((int[]) ar.result)[0];
1628dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent            if (DBG) Log.d(LOG_TAG, "handleQueryTTYModeResponse:ttymode=" + ttymode);
1629dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent
1630dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent            Intent ttyModeChanged = new Intent(TtyIntent.TTY_ENABLED_CHANGE_ACTION);
1631dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent            ttyModeChanged.putExtra("ttyEnabled", ttymode != Phone.TTY_MODE_OFF);
1632dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent            sendBroadcast(ttyModeChanged);
1633dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent
1634dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent            String audioTtyMode;
1635dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent            switch (ttymode) {
1636dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent            case Phone.TTY_MODE_FULL:
1637dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                audioTtyMode = "tty_full";
1638dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                break;
1639dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent            case Phone.TTY_MODE_VCO:
1640dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                audioTtyMode = "tty_vco";
1641dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                break;
1642dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent            case Phone.TTY_MODE_HCO:
1643dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                audioTtyMode = "tty_hco";
1644dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                break;
1645dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent            case Phone.TTY_MODE_OFF:
1646dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent            default:
1647dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                audioTtyMode = "tty_off";
1648dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                break;
1649dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent            }
1650dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent            AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
1651dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent            audioManager.setParameters("tty_mode="+audioTtyMode);
1652dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent        }
1653dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent    }
1654dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent
1655dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent    private void handleSetTTYModeResponse(Message msg) {
1656dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent        AsyncResult ar = (AsyncResult) msg.obj;
1657dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent
1658dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent        if (ar.exception != null) {
1659dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent            if (DBG) Log.d (LOG_TAG,
1660dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                    "handleSetTTYModeResponse: Error setting TTY mode, ar.exception"
1661dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent                    + ar.exception);
1662dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent        }
1663dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent        phone.queryTTYMode(mHandler.obtainMessage(EVENT_TTY_MODE_GET));
1664dc4b8d2f09ca215cc4a29513e56cd254d0c6d5f8Eric Laurent    }
166530b5eaaa481befad78cfcaaae024ca90e0616630Joe Onorato
166630b5eaaa481befad78cfcaaae024ca90e0616630Joe Onorato    /* package */ void clearUserActivityTimeout() {
166730b5eaaa481befad78cfcaaae024ca90e0616630Joe Onorato        try {
166830b5eaaa481befad78cfcaaae024ca90e0616630Joe Onorato            mPowerManagerService.clearUserActivityTimeout(SystemClock.uptimeMillis(),
166930b5eaaa481befad78cfcaaae024ca90e0616630Joe Onorato                    10*1000 /* 10 sec */);
167030b5eaaa481befad78cfcaaae024ca90e0616630Joe Onorato        } catch (RemoteException ex) {
167130b5eaaa481befad78cfcaaae024ca90e0616630Joe Onorato            // System process is dead.
167230b5eaaa481befad78cfcaaae024ca90e0616630Joe Onorato        }
167330b5eaaa481befad78cfcaaae024ca90e0616630Joe Onorato    }
1674b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project}
1675