BluetoothHandsfree.java revision f5945125c4e044b860f89a370fe376b97bd91d37
1b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project/*
2b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project * Copyright (C) 2008 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.bluetooth.AtCommandHandler;
20b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.bluetooth.AtCommandResult;
21b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.bluetooth.AtParser;
22f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lanimport android.bluetooth.BluetoothA2dp;
23db065e9834bcad9b0cb16c272b4b272dee8bdf62Nick Pellyimport android.bluetooth.BluetoothAdapter;
24b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.bluetooth.BluetoothDevice;
254079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Projectimport android.bluetooth.BluetoothHeadset;
26b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.bluetooth.HeadsetBase;
27b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.bluetooth.ScoSocket;
28b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.content.ActivityNotFoundException;
29b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.content.BroadcastReceiver;
30b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.content.Context;
31b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.content.Intent;
32b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.content.IntentFilter;
33b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.media.AudioManager;
34b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.net.Uri;
35b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.os.AsyncResult;
36b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.os.Bundle;
37b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.os.Handler;
38b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.os.Message;
39b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.os.PowerManager;
40b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.os.PowerManager.WakeLock;
41b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.os.SystemProperties;
42b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.telephony.PhoneNumberUtils;
43b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.telephony.ServiceState;
44404edc94de563aef5fd5ba48be9114a970cb93bbWink Savilleimport android.telephony.SignalStrength;
45b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport android.util.Log;
46b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
47b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport com.android.internal.telephony.Call;
48b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport com.android.internal.telephony.Connection;
49b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport com.android.internal.telephony.Phone;
50b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport com.android.internal.telephony.TelephonyIntents;
518058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wangimport com.android.internal.telephony.CallManager;
52b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
53b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectimport java.util.LinkedList;
54f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan
55b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project/**
56b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project * Bluetooth headset manager for the Phone app.
57b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project * @hide
58b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project */
59b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Projectpublic class BluetoothHandsfree {
60b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private static final String TAG = "BT HS/HF";
618eb32ddb60cfe1a0d0733735c30ecfc814452806Jackson Fan    private static final boolean DBG = (PhoneApp.DBG_LEVEL >= 1)
628eb32ddb60cfe1a0d0733735c30ecfc814452806Jackson Fan            && (SystemProperties.getInt("ro.debuggable", 0) == 1);
638eb32ddb60cfe1a0d0733735c30ecfc814452806Jackson Fan    private static final boolean VDBG = (PhoneApp.DBG_LEVEL >= 2);  // even more logging
64b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
65b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    public static final int TYPE_UNKNOWN           = 0;
66b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    public static final int TYPE_HEADSET           = 1;
67b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    public static final int TYPE_HANDSFREE         = 2;
68b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
694079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project    private final Context mContext;
708058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang    private final CallManager mCM;
71f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan    private final BluetoothA2dp mA2dp;
72f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan
73f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan    private BluetoothDevice mA2dpDevice;
74f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan    private int mA2dpState;
75f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan
76b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private ServiceState mServiceState;
77b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private HeadsetBase mHeadset;  // null when not connected
78b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private int mHeadsetType;
79b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private boolean mAudioPossible;
80b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private ScoSocket mIncomingSco;
81b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private ScoSocket mOutgoingSco;
82b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private ScoSocket mConnectedSco;
83b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
84b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private AudioManager mAudioManager;
85b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private PowerManager mPowerManager;
86b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
87f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan    private boolean mPendingSco;  // waiting for a2dp sink to suspend before establishing SCO
88ab377a6e67c195c230db42e98d597bf4deba14bdEric Laurent    private boolean mA2dpSuspended;
89b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private boolean mUserWantsAudio;
90b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private WakeLock mStartCallWakeLock;  // held while waiting for the intent to start call
91b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private WakeLock mStartVoiceRecognitionWakeLock;  // held while waiting for voice recognition
92b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
93b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    // AT command state
94c9d9ed30aa547b79b81adc13a4d148a003b6ee62w    private static final int GSM_MAX_CONNECTIONS = 6;  // Max connections allowed by GSM
95c9d9ed30aa547b79b81adc13a4d148a003b6ee62w    private static final int CDMA_MAX_CONNECTIONS = 2;  // Max connections allowed by CDMA
96b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
97b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private long mBgndEarliestConnectionTime = 0;
98b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private boolean mClip = false;  // Calling Line Information Presentation
99b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private boolean mIndicatorsEnabled = false;
100b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private boolean mCmee = false;  // Extended Error reporting
101b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private long[] mClccTimestamps; // Timestamps associated with each clcc index
102b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private boolean[] mClccUsed;     // Is this clcc index in use
103b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private boolean mWaitingForCallStart;
104b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private boolean mWaitingForVoiceRecognition;
1050966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly    // do not connect audio until service connection is established
1060966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly    // for 3-way supported devices, this is after AT+CHLD
1070966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly    // for non-3-way supported devices, this is after AT+CMER (see spec)
1080966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly    private boolean mServiceConnectionEstablished;
109db065e9834bcad9b0cb16c272b4b272dee8bdf62Nick Pelly
1101dad0387bf2f280c12bdac172604e9779134fbcdJaikumar Ganesh    private final BluetoothPhoneState mBluetoothPhoneState;  // for CIND and CIEV updates
111b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private final BluetoothAtPhonebook mPhonebook;
1121dad0387bf2f280c12bdac172604e9779134fbcdJaikumar Ganesh    private Phone.State mPhoneState = Phone.State.IDLE;
113487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh    CdmaPhoneCallState.PhoneCallState mCdmaThreeWayCallState =
114487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                                            CdmaPhoneCallState.PhoneCallState.IDLE;
115b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
116b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private DebugThread mDebugThread;
117b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private int mScoGain = Integer.MIN_VALUE;
118b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
119b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private static Intent sVoiceCommandIntent;
120b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
121b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    // Audio parameters
122b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private static final String HEADSET_NREC = "bt_headset_nrec";
123b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private static final String HEADSET_NAME = "bt_headset_name";
124b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
125b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private int mRemoteBrsf = 0;
126b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private int mLocalBrsf = 0;
127b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
128c9d9ed30aa547b79b81adc13a4d148a003b6ee62w    // CDMA specific flag used in context with BT devices having display capabilities
129c9d9ed30aa547b79b81adc13a4d148a003b6ee62w    // to show which Caller is active. This state might not be always true as in CDMA
130c9d9ed30aa547b79b81adc13a4d148a003b6ee62w    // networks if a caller drops off no update is provided to the Phone.
131c9d9ed30aa547b79b81adc13a4d148a003b6ee62w    // This flag is just used as a toggle to provide a update to the BT device to specify
132c9d9ed30aa547b79b81adc13a4d148a003b6ee62w    // which caller is active.
133c9d9ed30aa547b79b81adc13a4d148a003b6ee62w    private boolean mCdmaIsSecondCallActive = false;
134c9d9ed30aa547b79b81adc13a4d148a003b6ee62w
135b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /* Constants from Bluetooth Specification Hands-Free profile version 1.5 */
1366967e2d953bc077c99c4831946201f3d333b833fNick Pelly    private static final int BRSF_AG_THREE_WAY_CALLING = 1 << 0;
1376967e2d953bc077c99c4831946201f3d333b833fNick Pelly    private static final int BRSF_AG_EC_NR = 1 << 1;
1386967e2d953bc077c99c4831946201f3d333b833fNick Pelly    private static final int BRSF_AG_VOICE_RECOG = 1 << 2;
1396967e2d953bc077c99c4831946201f3d333b833fNick Pelly    private static final int BRSF_AG_IN_BAND_RING = 1 << 3;
1406967e2d953bc077c99c4831946201f3d333b833fNick Pelly    private static final int BRSF_AG_VOICE_TAG_NUMBE = 1 << 4;
1416967e2d953bc077c99c4831946201f3d333b833fNick Pelly    private static final int BRSF_AG_REJECT_CALL = 1 << 5;
1426967e2d953bc077c99c4831946201f3d333b833fNick Pelly    private static final int BRSF_AG_ENHANCED_CALL_STATUS = 1 <<  6;
1436967e2d953bc077c99c4831946201f3d333b833fNick Pelly    private static final int BRSF_AG_ENHANCED_CALL_CONTROL = 1 << 7;
1446967e2d953bc077c99c4831946201f3d333b833fNick Pelly    private static final int BRSF_AG_ENHANCED_ERR_RESULT_CODES = 1 << 8;
1456967e2d953bc077c99c4831946201f3d333b833fNick Pelly
1466967e2d953bc077c99c4831946201f3d333b833fNick Pelly    private static final int BRSF_HF_EC_NR = 1 << 0;
1476967e2d953bc077c99c4831946201f3d333b833fNick Pelly    private static final int BRSF_HF_CW_THREE_WAY_CALLING = 1 << 1;
1486967e2d953bc077c99c4831946201f3d333b833fNick Pelly    private static final int BRSF_HF_CLIP = 1 << 2;
1496967e2d953bc077c99c4831946201f3d333b833fNick Pelly    private static final int BRSF_HF_VOICE_REG_ACT = 1 << 3;
1506967e2d953bc077c99c4831946201f3d333b833fNick Pelly    private static final int BRSF_HF_REMOTE_VOL_CONTROL = 1 << 4;
1516967e2d953bc077c99c4831946201f3d333b833fNick Pelly    private static final int BRSF_HF_ENHANCED_CALL_STATUS = 1 <<  5;
1526967e2d953bc077c99c4831946201f3d333b833fNick Pelly    private static final int BRSF_HF_ENHANCED_CALL_CONTROL = 1 << 6;
153b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
154b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    public static String typeToString(int type) {
155b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        switch (type) {
156b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        case TYPE_UNKNOWN:
157b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            return "unknown";
158b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        case TYPE_HEADSET:
159b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            return "headset";
160b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        case TYPE_HANDSFREE:
161b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            return "handsfree";
162b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
163b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return null;
164b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
165b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1668058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang    public BluetoothHandsfree(Context context, CallManager cm) {
1678058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang        mCM = cm;
168b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mContext = context;
169cec56587c1d3c90014848d7adfe817bef9c325eaNick Pelly        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
170db065e9834bcad9b0cb16c272b4b272dee8bdf62Nick Pelly        boolean bluetoothCapable = (adapter != null);
171b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mHeadset = null;  // nothing connected yet
172f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan        mA2dp = new BluetoothA2dp(mContext);
173f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan        mA2dpState = BluetoothA2dp.STATE_DISCONNECTED;
174f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan        mA2dpDevice = null;
175ab377a6e67c195c230db42e98d597bf4deba14bdEric Laurent        mA2dpSuspended = false;
176b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
177b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
178b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mStartCallWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
179b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                                                       TAG + ":StartCall");
180b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mStartCallWakeLock.setReferenceCounted(false);
181b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mStartVoiceRecognitionWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
182b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                                                       TAG + ":VoiceRecognition");
183b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mStartVoiceRecognitionWakeLock.setReferenceCounted(false);
184b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
185b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mLocalBrsf = BRSF_AG_THREE_WAY_CALLING |
186b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                     BRSF_AG_EC_NR |
187b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                     BRSF_AG_REJECT_CALL |
188b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                     BRSF_AG_ENHANCED_CALL_STATUS;
1894b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project
190b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (sVoiceCommandIntent == null) {
191b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            sVoiceCommandIntent = new Intent(Intent.ACTION_VOICE_COMMAND);
192b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            sVoiceCommandIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
193b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
1946967e2d953bc077c99c4831946201f3d333b833fNick Pelly        if (mContext.getPackageManager().resolveActivity(sVoiceCommandIntent, 0) != null &&
195b8dbab241df3aa3487c1bdb488fd4e0b694d2d9aEric Laurent                BluetoothHeadset.isBluetoothVoiceDialingEnabled(mContext)) {
196b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mLocalBrsf |= BRSF_AG_VOICE_RECOG;
197b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
198b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1991dad0387bf2f280c12bdac172604e9779134fbcdJaikumar Ganesh        mBluetoothPhoneState = new BluetoothPhoneState();
200b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mUserWantsAudio = true;
201b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mPhonebook = new BluetoothAtPhonebook(mContext, this);
202b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
203c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        cdmaSetSecondCallState(false);
204f5945125c4e044b860f89a370fe376b97bd91d37Jaikumar Ganesh
205f5945125c4e044b860f89a370fe376b97bd91d37Jaikumar Ganesh        if (bluetoothCapable) {
206f5945125c4e044b860f89a370fe376b97bd91d37Jaikumar Ganesh            resetAtState();
207f5945125c4e044b860f89a370fe376b97bd91d37Jaikumar Ganesh        }
208f5945125c4e044b860f89a370fe376b97bd91d37Jaikumar Ganesh
209b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
210b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
211b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /* package */ synchronized void onBluetoothEnabled() {
212b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        /* Bluez has a bug where it will always accept and then orphan
213b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project         * incoming SCO connections, regardless of whether we have a listening
214b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project         * SCO socket. So the best thing to do is always run a listening socket
215b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project         * while bluetooth is on so that at least we can diconnect it
216b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project         * immediately when we don't want it.
217b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project         */
218b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (mIncomingSco == null) {
219b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mIncomingSco = createScoSocket();
220b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mIncomingSco.accept();
221b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
222b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
223b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
224b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /* package */ synchronized void onBluetoothDisabled() {
225a1478a9074b126d13124c99b7543b2518b3de3b7Eric Laurent        audioOff();
226b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (mIncomingSco != null) {
227b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mIncomingSco.close();
228b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mIncomingSco = null;
229b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
230b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
231b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
232b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private boolean isHeadsetConnected() {
233b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (mHeadset == null) {
234b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            return false;
235b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
236b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return mHeadset.isConnected();
237b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
238b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
239f6adf1a33b12c9900b3ce9c15130642ce515ae91Jaikumar Ganesh    /* package */ synchronized void connectHeadset(HeadsetBase headset, int headsetType) {
240b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mHeadset = headset;
241b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mHeadsetType = headsetType;
242b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (mHeadsetType == TYPE_HEADSET) {
243b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            initializeHeadsetAtParser();
244b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        } else {
245b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            initializeHandsfreeAtParser();
246b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
247b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        headset.startEventThread();
248b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        configAudioParameters();
249b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
250b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (inDebug()) {
251b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            startDebug();
252b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
253b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
254b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (isIncallAudio()) {
255b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            audioOn();
256b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
257b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
258b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
259b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /* returns true if there is some kind of in-call audio we may wish to route
260b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * bluetooth to */
261b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private boolean isIncallAudio() {
2628058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang        Call.State state = mCM.getActiveFgCallState();
263b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
264b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return (state == Call.State.ACTIVE || state == Call.State.ALERTING);
265b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
266b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
267819fc69ac472a8d77be8e5dd79a21434c6de8a37Jaikumar Ganesh    /* package */ synchronized void disconnectHeadset() {
268819fc69ac472a8d77be8e5dd79a21434c6de8a37Jaikumar Ganesh        // Close off the SCO sockets
269819fc69ac472a8d77be8e5dd79a21434c6de8a37Jaikumar Ganesh        audioOff();
270b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mHeadset = null;
271b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        stopDebug();
272b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        resetAtState();
273b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
274b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
275bfc44512504ebc93c101ddb394719840f2d25072Jaikumar Ganesh    /* package */ synchronized void resetAtState() {
276b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mClip = false;
277b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mIndicatorsEnabled = false;
2780966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly        mServiceConnectionEstablished = false;
279b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mCmee = false;
280c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        mClccTimestamps = new long[GSM_MAX_CONNECTIONS];
281c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        mClccUsed = new boolean[GSM_MAX_CONNECTIONS];
282c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        for (int i = 0; i < GSM_MAX_CONNECTIONS; i++) {
283b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mClccUsed[i] = false;
284b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
285b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mRemoteBrsf = 0;
286f5945125c4e044b860f89a370fe376b97bd91d37Jaikumar Ganesh        mPhonebook.resetAtState();
287b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
288b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
289b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private void configAudioParameters() {
290db065e9834bcad9b0cb16c272b4b272dee8bdf62Nick Pelly        String name = mHeadset.getRemoteDevice().getName();
291b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (name == null) {
292b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            name = "<unknown>";
293b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
294aa23e1c3c758bad23d8b6709147cc1ff7cd1e43cEric Laurent        mAudioManager.setParameters(HEADSET_NAME+"="+name+";"+HEADSET_NREC+"=on");
295b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
296b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
297b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
298b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /** Represents the data that we send in a +CIND or +CIEV command to the HF
299b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     */
300b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private class BluetoothPhoneState {
301b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // 0: no service
302b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // 1: service
303b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private int mService;
304b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
305b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // 0: no active call
306b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // 1: active call (where active means audio is routed - not held call)
307b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private int mCall;
308b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
309b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // 0: not in call setup
310b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // 1: incoming call setup
311b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // 2: outgoing call setup
312b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // 3: remote party being alerted in an outgoing call setup
313b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private int mCallsetup;
314b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
315b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // 0: no calls held
316b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // 1: held call and active call
317b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // 2: held call only
318b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private int mCallheld;
319b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
320b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // cellular signal strength of AG: 0-5
321b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private int mSignal;
322b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
323b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // cellular signal strength in CSQ rssi scale
324b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private int mRssi;  // for CSQ
325b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
326b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // 0: roaming not active (home)
327b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // 1: roaming active
328b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private int mRoam;
329b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
330b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // battery charge of AG: 0-5
331b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private int mBattchg;
332b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
333b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // 0: not registered
334b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // 1: registered, home network
335b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // 5: registered, roaming
336b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private int mStat;  // for CREG
337b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
338b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private String mRingingNumber;  // Context for in-progress RING's
339b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private int    mRingingType;
340b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private boolean mIgnoreRing = false;
3411ec7e6b9b3bf1dfc384676244bec1edb50fedc58Jaikumar Ganesh        private boolean mStopRing = false;
342b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
343b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private static final int SERVICE_STATE_CHANGED = 1;
344487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh        private static final int PRECISE_CALL_STATE_CHANGED = 2;
345b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private static final int RING = 3;
346a76113dcb0def438d0b3019aeb88fdbfbfbe90d4Paul Berman        private static final int PHONE_CDMA_CALL_WAITING = 4;
347b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
348b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private Handler mStateChangeHandler = new Handler() {
349b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
350b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public void handleMessage(Message msg) {
351b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                switch(msg.what) {
352b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                case RING:
353b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    AtCommandResult result = ring();
354b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    if (result != null) {
355b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        sendURC(result.toString());
356b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    }
357b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    break;
358b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                case SERVICE_STATE_CHANGED:
359b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    ServiceState state = (ServiceState) ((AsyncResult) msg.obj).result;
360b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    updateServiceState(sendUpdate(), state);
361b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    break;
362487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                case PRECISE_CALL_STATE_CHANGED:
363a76113dcb0def438d0b3019aeb88fdbfbfbe90d4Paul Berman                case PHONE_CDMA_CALL_WAITING:
364b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    Connection connection = null;
365b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    if (((AsyncResult) msg.obj).result instanceof Connection) {
366b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        connection = (Connection) ((AsyncResult) msg.obj).result;
367b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    }
368487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                    handlePreciseCallStateChange(sendUpdate(), connection);
369b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    break;
370b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
371b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
372b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        };
373b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
374b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private BluetoothPhoneState() {
375b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // init members
3768058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang            // TODO May consider to repalce the default phone's state and signal
3778058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang            //      by CallManagter's state and signal
3788058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang            updateServiceState(false, mCM.getDefaultPhone().getServiceState());
379487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh            handlePreciseCallStateChange(false, null);
380b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mBattchg = 5;  // There is currently no API to get battery level
381b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                           // on demand, so set to 5 and wait for an update
3828058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang            mSignal = asuToSignal(mCM.getDefaultPhone().getSignalStrength());
383b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
384b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // register for updates
3858058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang            mCM.registerForServiceStateChanged(mStateChangeHandler,
386b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                                                  SERVICE_STATE_CHANGED, null);
3878058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang            mCM.registerForPreciseCallStateChanged(mStateChangeHandler,
388487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                    PRECISE_CALL_STATE_CHANGED, null);
3898058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang            mCM.registerForCallWaiting(mStateChangeHandler,
3908058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                PHONE_CDMA_CALL_WAITING, null);
3918058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang
392b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
393b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            filter.addAction(TelephonyIntents.ACTION_SIGNAL_STRENGTH_CHANGED);
394f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan            filter.addAction(BluetoothA2dp.ACTION_SINK_STATE_CHANGED);
395b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mContext.registerReceiver(mStateReceiver, filter);
396b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
397b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
398a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville        private void updateBtPhoneStateAfterRadioTechnologyChange() {
3998eb32ddb60cfe1a0d0733735c30ecfc814452806Jackson Fan            if(VDBG) Log.d(TAG, "updateBtPhoneStateAfterRadioTechnologyChange...");
400a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville
401a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville            //Unregister all events from the old obsolete phone
4028058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang            mCM.unregisterForServiceStateChanged(mStateChangeHandler);
4038058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang            mCM.unregisterForPreciseCallStateChanged(mStateChangeHandler);
4048058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang            mCM.unregisterForCallWaiting(mStateChangeHandler);
405a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville
406a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville            //Register all events new to the new active phone
4078058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang            mCM.registerForServiceStateChanged(mStateChangeHandler,
40819dd8f9c63ba7471c76fc31847a8063d18a83b6dJaikumar Ganesh                                                  SERVICE_STATE_CHANGED, null);
4098058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang            mCM.registerForPreciseCallStateChanged(mStateChangeHandler,
410487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                    PRECISE_CALL_STATE_CHANGED, null);
4118058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang            mCM.registerForCallWaiting(mStateChangeHandler,
4128058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                PHONE_CDMA_CALL_WAITING, null);
413a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville        }
414a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville
415b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private boolean sendUpdate() {
416b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            return isHeadsetConnected() && mHeadsetType == TYPE_HANDSFREE && mIndicatorsEnabled;
417b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
418b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
419b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private boolean sendClipUpdate() {
420b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            return isHeadsetConnected() && mHeadsetType == TYPE_HANDSFREE && mClip;
421b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
422b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
4231ec7e6b9b3bf1dfc384676244bec1edb50fedc58Jaikumar Ganesh        private void stopRing() {
4241ec7e6b9b3bf1dfc384676244bec1edb50fedc58Jaikumar Ganesh            mStopRing = true;
4251ec7e6b9b3bf1dfc384676244bec1edb50fedc58Jaikumar Ganesh        }
4261ec7e6b9b3bf1dfc384676244bec1edb50fedc58Jaikumar Ganesh
427b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        /* convert [0,31] ASU signal strength to the [0,5] expected by
428b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project         * bluetooth devices. Scale is similar to status bar policy
429b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project         */
43034b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh        private int gsmAsuToSignal(SignalStrength signalStrength) {
43134b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh            int asu = signalStrength.getGsmSignalStrength();
432b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            if      (asu >= 16) return 5;
433b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            else if (asu >= 8)  return 4;
434b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            else if (asu >= 4)  return 3;
435b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            else if (asu >= 2)  return 2;
436b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            else if (asu >= 1)  return 1;
437b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            else                return 0;
438b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
439b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
44034b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh        /**
44134b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh         * Convert the cdma / evdo db levels to appropriate icon level.
44234b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh         * The scale is similar to the one used in status bar policy.
44334b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh         *
44434b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh         * @param signalStrength
44534b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh         * @return the icon level
446404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville         */
44734b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh        private int cdmaDbmEcioToSignal(SignalStrength signalStrength) {
44834b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh            int levelDbm = 0;
44934b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh            int levelEcio = 0;
45034b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh            int cdmaIconLevel = 0;
45134b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh            int evdoIconLevel = 0;
45234b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh            int cdmaDbm = signalStrength.getCdmaDbm();
45334b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh            int cdmaEcio = signalStrength.getCdmaEcio();
45434b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh
45534b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh            if (cdmaDbm >= -75) levelDbm = 4;
45634b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh            else if (cdmaDbm >= -85) levelDbm = 3;
45734b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh            else if (cdmaDbm >= -95) levelDbm = 2;
45834b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh            else if (cdmaDbm >= -100) levelDbm = 1;
45934b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh            else levelDbm = 0;
46034b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh
46134b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh            // Ec/Io are in dB*10
46234b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh            if (cdmaEcio >= -90) levelEcio = 4;
46334b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh            else if (cdmaEcio >= -110) levelEcio = 3;
46434b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh            else if (cdmaEcio >= -130) levelEcio = 2;
46534b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh            else if (cdmaEcio >= -150) levelEcio = 1;
46634b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh            else levelEcio = 0;
46734b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh
46834b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh            cdmaIconLevel = (levelDbm < levelEcio) ? levelDbm : levelEcio;
46934b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh
47013df4bba7e39d516317d005b4b917b1f1c6baf8dJaikumar Ganesh            if (mServiceState != null &&
471b1164d370e8d83a8a4f3cbdc73dffc087254cabdJaikumar Ganesh                  (mServiceState.getRadioTechnology() == ServiceState.RADIO_TECHNOLOGY_EVDO_0 ||
472b1164d370e8d83a8a4f3cbdc73dffc087254cabdJaikumar Ganesh                   mServiceState.getRadioTechnology() == ServiceState.RADIO_TECHNOLOGY_EVDO_A)) {
47334b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh                  int evdoEcio = signalStrength.getEvdoEcio();
47434b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh                  int evdoSnr = signalStrength.getEvdoSnr();
47534b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh                  int levelEvdoEcio = 0;
47634b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh                  int levelEvdoSnr = 0;
47734b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh
47834b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh                  // Ec/Io are in dB*10
47934b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh                  if (evdoEcio >= -650) levelEvdoEcio = 4;
48034b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh                  else if (evdoEcio >= -750) levelEvdoEcio = 3;
48134b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh                  else if (evdoEcio >= -900) levelEvdoEcio = 2;
48234b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh                  else if (evdoEcio >= -1050) levelEvdoEcio = 1;
48334b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh                  else levelEvdoEcio = 0;
48434b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh
48534b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh                  if (evdoSnr > 7) levelEvdoSnr = 4;
48634b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh                  else if (evdoSnr > 5) levelEvdoSnr = 3;
48734b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh                  else if (evdoSnr > 3) levelEvdoSnr = 2;
48834b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh                  else if (evdoSnr > 1) levelEvdoSnr = 1;
48934b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh                  else levelEvdoSnr = 0;
49034b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh
49134b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh                  evdoIconLevel = (levelEvdoEcio < levelEvdoSnr) ? levelEvdoEcio : levelEvdoSnr;
49234b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh            }
49334b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh            // TODO(): There is a bug open regarding what should be sent.
49434b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh            return (cdmaIconLevel > evdoIconLevel) ?  cdmaIconLevel : evdoIconLevel;
49534b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh
496404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville        }
497404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville
498404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville
499404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville        private int asuToSignal(SignalStrength signalStrength) {
50034b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh            if (signalStrength.isGsm()) {
50134b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh                return gsmAsuToSignal(signalStrength);
502404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville            } else {
50334b9266fbd8c9c4d52e8faf708e02037a7ddcc4cJaikumar Ganesh                return cdmaDbmEcioToSignal(signalStrength);
504404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville            }
505404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville        }
506404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville
507404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville
508b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        /* convert [0,5] signal strength to a rssi signal strength for CSQ
509b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project         * which is [0,31]. Despite the same scale, this is not the same value
510b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project         * as ASU.
511b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project         */
512b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private int signalToRssi(int signal) {
513b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // using C4A suggested values
514b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            switch (signal) {
515b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            case 0: return 0;
516b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            case 1: return 4;
517b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            case 2: return 8;
518b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            case 3: return 13;
519b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            case 4: return 19;
520b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            case 5: return 31;
521b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
522b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            return 0;
523b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
524b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
525b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
526b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private final BroadcastReceiver mStateReceiver = new BroadcastReceiver() {
527b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
528b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public void onReceive(Context context, Intent intent) {
529b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
530b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    updateBatteryState(intent);
531f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                } else if (intent.getAction().equals(
532f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                            TelephonyIntents.ACTION_SIGNAL_STRENGTH_CHANGED)) {
533b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    updateSignalState(intent);
534f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                } else if (intent.getAction().equals(BluetoothA2dp.ACTION_SINK_STATE_CHANGED)) {
535f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                    int state = intent.getIntExtra(BluetoothA2dp.EXTRA_SINK_STATE,
536f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                            BluetoothA2dp.STATE_DISCONNECTED);
537f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                    int oldState = intent.getIntExtra(BluetoothA2dp.EXTRA_PREVIOUS_SINK_STATE,
538f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                            BluetoothA2dp.STATE_DISCONNECTED);
539f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                    BluetoothDevice device =
540f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                            intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
541105bb2145aba70c03b43bfab906c1b0551387309Jaikumar Ganesh
542105bb2145aba70c03b43bfab906c1b0551387309Jaikumar Ganesh                    // We are only concerned about Connected sinks to suspend and resume
543105bb2145aba70c03b43bfab906c1b0551387309Jaikumar Ganesh                    // them. We can safely ignore SINK_STATE_CHANGE for other devices.
544105bb2145aba70c03b43bfab906c1b0551387309Jaikumar Ganesh                    if (mA2dpDevice != null && !device.equals(mA2dpDevice)) return;
545105bb2145aba70c03b43bfab906c1b0551387309Jaikumar Ganesh
546f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                    synchronized (BluetoothHandsfree.this) {
547f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                        mA2dpState = state;
548105bb2145aba70c03b43bfab906c1b0551387309Jaikumar Ganesh                        if (state == BluetoothA2dp.STATE_DISCONNECTED) {
549105bb2145aba70c03b43bfab906c1b0551387309Jaikumar Ganesh                            mA2dpDevice = null;
550105bb2145aba70c03b43bfab906c1b0551387309Jaikumar Ganesh                        } else {
551105bb2145aba70c03b43bfab906c1b0551387309Jaikumar Ganesh                            mA2dpDevice = device;
552105bb2145aba70c03b43bfab906c1b0551387309Jaikumar Ganesh                        }
553ab377a6e67c195c230db42e98d597bf4deba14bdEric Laurent                        if (oldState == BluetoothA2dp.STATE_PLAYING &&
554ab377a6e67c195c230db42e98d597bf4deba14bdEric Laurent                            mA2dpState == BluetoothA2dp.STATE_CONNECTED) {
555ab377a6e67c195c230db42e98d597bf4deba14bdEric Laurent                            if (mA2dpSuspended) {
556ab377a6e67c195c230db42e98d597bf4deba14bdEric Laurent                                if (mPendingSco) {
557ab377a6e67c195c230db42e98d597bf4deba14bdEric Laurent                                    mHandler.removeMessages(MESSAGE_CHECK_PENDING_SCO);
558ab377a6e67c195c230db42e98d597bf4deba14bdEric Laurent                                    if (DBG) log("A2DP suspended, completing SCO");
559ab377a6e67c195c230db42e98d597bf4deba14bdEric Laurent                                    mOutgoingSco = createScoSocket();
560ab377a6e67c195c230db42e98d597bf4deba14bdEric Laurent                                    if (!mOutgoingSco.connect(
561f0316a1fb5c00585d0fe4b500723557afabec70bEric Olsen                                            mHeadset.getRemoteDevice().getAddress(),
562f0316a1fb5c00585d0fe4b500723557afabec70bEric Olsen                                            mHeadset.getRemoteDevice().getName())) {
563ab377a6e67c195c230db42e98d597bf4deba14bdEric Laurent                                        mOutgoingSco = null;
564ab377a6e67c195c230db42e98d597bf4deba14bdEric Laurent                                    }
565ab377a6e67c195c230db42e98d597bf4deba14bdEric Laurent                                    mPendingSco = false;
566f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                                }
567f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                            }
568f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                        }
569f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                    }
570b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
571b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
572b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        };
573b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
574b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private synchronized void updateBatteryState(Intent intent) {
575b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            int batteryLevel = intent.getIntExtra("level", -1);
576b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            int scale = intent.getIntExtra("scale", -1);
577b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            if (batteryLevel == -1 || scale == -1) {
578b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return;  // ignore
579b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
580b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            batteryLevel = batteryLevel * 5 / scale;
581b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            if (mBattchg != batteryLevel) {
582b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                mBattchg = batteryLevel;
583b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (sendUpdate()) {
584b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    sendURC("+CIEV: 7," + mBattchg);
585b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
586b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
587b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
588b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
589b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private synchronized void updateSignalState(Intent intent) {
590404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville            // NOTE this function is called by the BroadcastReceiver mStateReceiver after intent
591404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville            // ACTION_SIGNAL_STRENGTH_CHANGED and by the DebugThread mDebugThread
592d16be9780f29c031e3d854fde5b92f05392836e3Jaikumar Ganesh            if (mHeadset == null) {
593d16be9780f29c031e3d854fde5b92f05392836e3Jaikumar Ganesh                return;
594d16be9780f29c031e3d854fde5b92f05392836e3Jaikumar Ganesh            }
595d16be9780f29c031e3d854fde5b92f05392836e3Jaikumar Ganesh
596404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville            SignalStrength signalStrength = SignalStrength.newFromBundle(intent.getExtras());
597b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            int signal;
598404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville
599404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville            if (signalStrength != null) {
600404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville                signal = asuToSignal(signalStrength);
601404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville                mRssi = signalToRssi(signal);  // no unsolicited CSQ
602404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville                if (signal != mSignal) {
603404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville                    mSignal = signal;
604404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville                    if (sendUpdate()) {
605404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville                        sendURC("+CIEV: 5," + mSignal);
606404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville                    }
607b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
608404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville            } else {
609404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville                Log.e(TAG, "Signal Strength null");
610b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
611b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
612b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
613b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private synchronized void updateServiceState(boolean sendUpdate, ServiceState state) {
614b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            int service = state.getState() == ServiceState.STATE_IN_SERVICE ? 1 : 0;
615b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            int roam = state.getRoaming() ? 1 : 0;
616b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            int stat;
617b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            AtCommandResult result = new AtCommandResult(AtCommandResult.UNSOLICITED);
61813df4bba7e39d516317d005b4b917b1f1c6baf8dJaikumar Ganesh            mServiceState = state;
619b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            if (service == 0) {
620b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                stat = 0;
621b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            } else {
622b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                stat = (roam == 1) ? 5 : 1;
623b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
624b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
625b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            if (service != mService) {
626b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                mService = service;
627b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (sendUpdate) {
628b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    result.addResponse("+CIEV: 1," + mService);
629b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
630b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
631b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            if (roam != mRoam) {
632b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                mRoam = roam;
633b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (sendUpdate) {
634b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    result.addResponse("+CIEV: 6," + mRoam);
635b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
636b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
637b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            if (stat != mStat) {
638b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                mStat = stat;
639b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (sendUpdate) {
640b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    result.addResponse(toCregString());
641b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
642b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
643b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
644b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            sendURC(result.toString());
645b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
646b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
647487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh        private synchronized void handlePreciseCallStateChange(boolean sendUpdate,
648487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                Connection connection) {
649b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            int call = 0;
650b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            int callsetup = 0;
651b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            int callheld = 0;
652b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            int prevCallsetup = mCallsetup;
653b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            AtCommandResult result = new AtCommandResult(AtCommandResult.UNSOLICITED);
6548058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang            Call foregroundCall = mCM.getActiveFgCall();
6558058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang            Call backgroundCall = mCM.getFirstActiveBgCall();
6568058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang            Call ringingCall = mCM.getFirstActiveRingingCall();
657b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
6588eb32ddb60cfe1a0d0733735c30ecfc814452806Jackson Fan            if (VDBG) log("updatePhoneState()");
659b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
660487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh            // This function will get called when the Precise Call State
661487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh            // {@link Call.State} changes. Hence, we might get this update
662487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh            // even if the {@link Phone.state} is same as before.
663487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh            // Check for the same.
664487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh
6658058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang            Phone.State newState = mCM.getState();
6661dad0387bf2f280c12bdac172604e9779134fbcdJaikumar Ganesh            if (newState != mPhoneState) {
6671dad0387bf2f280c12bdac172604e9779134fbcdJaikumar Ganesh                mPhoneState = newState;
6681dad0387bf2f280c12bdac172604e9779134fbcdJaikumar Ganesh                switch (mPhoneState) {
6691dad0387bf2f280c12bdac172604e9779134fbcdJaikumar Ganesh                case IDLE:
6701dad0387bf2f280c12bdac172604e9779134fbcdJaikumar Ganesh                    mUserWantsAudio = true;  // out of call - reset state
6711dad0387bf2f280c12bdac172604e9779134fbcdJaikumar Ganesh                    audioOff();
6721dad0387bf2f280c12bdac172604e9779134fbcdJaikumar Ganesh                    break;
6731dad0387bf2f280c12bdac172604e9779134fbcdJaikumar Ganesh                default:
6741dad0387bf2f280c12bdac172604e9779134fbcdJaikumar Ganesh                    callStarted();
6751dad0387bf2f280c12bdac172604e9779134fbcdJaikumar Ganesh                }
676b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
677b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
6788058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang            switch(foregroundCall.getState()) {
679b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            case ACTIVE:
680b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                call = 1;
681b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                mAudioPossible = true;
682b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                break;
683b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            case DIALING:
684b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                callsetup = 2;
685a41427328932deb7cbadf586a1ec58e0ba26a932Jaikumar Ganesh                mAudioPossible = true;
6863904e979ce9a501acbe6d8739393767a495388e8Jaikumar Ganesh                // We also need to send a Call started indication
6873904e979ce9a501acbe6d8739393767a495388e8Jaikumar Ganesh                // for cases where the 2nd MO was initiated was
6883904e979ce9a501acbe6d8739393767a495388e8Jaikumar Ganesh                // from a *BT hands free* and is waiting for a
6893904e979ce9a501acbe6d8739393767a495388e8Jaikumar Ganesh                // +BLND: OK response
6903904e979ce9a501acbe6d8739393767a495388e8Jaikumar Ganesh                // There is a special case handling of the same case
6913904e979ce9a501acbe6d8739393767a495388e8Jaikumar Ganesh                // for CDMA below
6928058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                if (mCM.getFgPhone().getPhoneType() == Phone.PHONE_TYPE_GSM) {
6933904e979ce9a501acbe6d8739393767a495388e8Jaikumar Ganesh                    callStarted();
6943904e979ce9a501acbe6d8739393767a495388e8Jaikumar Ganesh                }
695b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                break;
696b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            case ALERTING:
697b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                callsetup = 3;
698b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // Open the SCO channel for the outgoing call.
699b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                audioOn();
700b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                mAudioPossible = true;
701b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                break;
70281a26f368128024620b0dc7b0eb32debf3fe3419Jaikumar Ganesh            case DISCONNECTING:
70381a26f368128024620b0dc7b0eb32debf3fe3419Jaikumar Ganesh                // This is a transient state, we don't want to send
70481a26f368128024620b0dc7b0eb32debf3fe3419Jaikumar Ganesh                // any AT commands during this state.
70581a26f368128024620b0dc7b0eb32debf3fe3419Jaikumar Ganesh                call = mCall;
70681a26f368128024620b0dc7b0eb32debf3fe3419Jaikumar Ganesh                callsetup = mCallsetup;
70781a26f368128024620b0dc7b0eb32debf3fe3419Jaikumar Ganesh                callheld = mCallheld;
70881a26f368128024620b0dc7b0eb32debf3fe3419Jaikumar Ganesh                break;
709b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            default:
710b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                mAudioPossible = false;
711b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
712b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
7138058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang            switch(ringingCall.getState()) {
714b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            case INCOMING:
715b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            case WAITING:
716b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                callsetup = 1;
717b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                break;
71881a26f368128024620b0dc7b0eb32debf3fe3419Jaikumar Ganesh            case DISCONNECTING:
71981a26f368128024620b0dc7b0eb32debf3fe3419Jaikumar Ganesh                // This is a transient state, we don't want to send
72081a26f368128024620b0dc7b0eb32debf3fe3419Jaikumar Ganesh                // any AT commands during this state.
72181a26f368128024620b0dc7b0eb32debf3fe3419Jaikumar Ganesh                call = mCall;
72281a26f368128024620b0dc7b0eb32debf3fe3419Jaikumar Ganesh                callsetup = mCallsetup;
72381a26f368128024620b0dc7b0eb32debf3fe3419Jaikumar Ganesh                callheld = mCallheld;
72481a26f368128024620b0dc7b0eb32debf3fe3419Jaikumar Ganesh                break;
725b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
726b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
7278058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang            switch(backgroundCall.getState()) {
728b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            case HOLDING:
729b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (call == 1) {
730b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    callheld = 1;
731b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                } else {
732b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    call = 1;
733b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    callheld = 2;
734b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
735b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                break;
73681a26f368128024620b0dc7b0eb32debf3fe3419Jaikumar Ganesh            case DISCONNECTING:
73781a26f368128024620b0dc7b0eb32debf3fe3419Jaikumar Ganesh                // This is a transient state, we don't want to send
73881a26f368128024620b0dc7b0eb32debf3fe3419Jaikumar Ganesh                // any AT commands during this state.
73981a26f368128024620b0dc7b0eb32debf3fe3419Jaikumar Ganesh                call = mCall;
74081a26f368128024620b0dc7b0eb32debf3fe3419Jaikumar Ganesh                callsetup = mCallsetup;
74181a26f368128024620b0dc7b0eb32debf3fe3419Jaikumar Ganesh                callheld = mCallheld;
74281a26f368128024620b0dc7b0eb32debf3fe3419Jaikumar Ganesh                break;
743b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
744b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
745b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            if (mCall != call) {
746b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (call == 1) {
747b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    // This means that a call has transitioned from NOT ACTIVE to ACTIVE.
748b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    // Switch on audio.
749b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    audioOn();
750b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
751b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                mCall = call;
752b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (sendUpdate) {
753b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    result.addResponse("+CIEV: 2," + mCall);
754b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
755b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
756b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            if (mCallsetup != callsetup) {
757b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                mCallsetup = callsetup;
758b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (sendUpdate) {
7594b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project                    // If mCall = 0, send CIEV
7604b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project                    // mCall = 1, mCallsetup = 0, send CIEV
7614b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project                    // mCall = 1, mCallsetup = 1, send CIEV after CCWA,
7624b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project                    // if 3 way supported.
7634b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project                    // mCall = 1, mCallsetup = 2 / 3 -> send CIEV,
7644b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project                    // if 3 way is supported
7654b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project                    if (mCall != 1 || mCallsetup == 0 ||
7664b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project                        mCallsetup != 1 && (mRemoteBrsf & BRSF_HF_CW_THREE_WAY_CALLING) != 0x0) {
767b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        result.addResponse("+CIEV: 3," + mCallsetup);
768b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    }
769b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
770b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
771b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
7728058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang            if (mCM.getDefaultPhone().getPhoneType() == Phone.PHONE_TYPE_CDMA) {
773c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                PhoneApp app = PhoneApp.getInstance();
77460877272381161201c6969dea501e683705d6e32w                if (app.cdmaPhoneCallState != null) {
775487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                    CdmaPhoneCallState.PhoneCallState currCdmaThreeWayCallState =
77660877272381161201c6969dea501e683705d6e32w                            app.cdmaPhoneCallState.getCurrentCallState();
777487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                    CdmaPhoneCallState.PhoneCallState prevCdmaThreeWayCallState =
778ed1d155825eb32990fde95eef9d89a7260e4c3f1w                        app.cdmaPhoneCallState.getPreviousCallState();
779ed1d155825eb32990fde95eef9d89a7260e4c3f1w
780487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                    callheld = getCdmaCallHeldStatus(currCdmaThreeWayCallState,
781487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                                                     prevCdmaThreeWayCallState);
782487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh
783487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                    if (mCdmaThreeWayCallState != currCdmaThreeWayCallState) {
784487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                        // In CDMA, the network does not provide any feedback
785487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                        // to the phone when the 2nd MO call goes through the
786487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                        // stages of DIALING > ALERTING -> ACTIVE we fake the
787487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                        // sequence
788487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                        if ((currCdmaThreeWayCallState ==
789487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                                CdmaPhoneCallState.PhoneCallState.THRWAY_ACTIVE)
790487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                                    && app.cdmaPhoneCallState.IsThreeWayCallOrigStateDialing()) {
791487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                            mAudioPossible = true;
792487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                            if (sendUpdate) {
793487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                                if ((mRemoteBrsf & BRSF_HF_CW_THREE_WAY_CALLING) != 0x0) {
794487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                                    result.addResponse("+CIEV: 3,2");
795487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                                    result.addResponse("+CIEV: 3,3");
796487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                                    result.addResponse("+CIEV: 3,0");
797487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                                }
79860877272381161201c6969dea501e683705d6e32w                            }
799487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                            // We also need to send a Call started indication
800487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                            // for cases where the 2nd MO was initiated was
801487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                            // from a *BT hands free* and is waiting for a
802487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                            // +BLND: OK response
803487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                            callStarted();
804c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                        }
805c9d9ed30aa547b79b81adc13a4d148a003b6ee62w
806487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                        // In CDMA, the network does not provide any feedback to
807487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                        // the phone when a user merges a 3way call or swaps
808487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                        // between two calls we need to send a CIEV response
809487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                        // indicating that a call state got changed which should
810487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                        // trigger a CLCC update request from the BT client.
811487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                        if (currCdmaThreeWayCallState ==
812487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                                CdmaPhoneCallState.PhoneCallState.CONF_CALL) {
813487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                            mAudioPossible = true;
814487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                            if (sendUpdate) {
815487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                                if ((mRemoteBrsf & BRSF_HF_CW_THREE_WAY_CALLING) != 0x0) {
816487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                                    result.addResponse("+CIEV: 2,1");
817487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                                    result.addResponse("+CIEV: 3,0");
818487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                                }
81960877272381161201c6969dea501e683705d6e32w                            }
820c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                        }
821c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                    }
822487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                    mCdmaThreeWayCallState = currCdmaThreeWayCallState;
823c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                }
824c9d9ed30aa547b79b81adc13a4d148a003b6ee62w            }
825c9d9ed30aa547b79b81adc13a4d148a003b6ee62w
826b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            boolean callsSwitched =
8278058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                (callheld == 1 && ! (backgroundCall.getEarliestConnectTime() ==
828b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    mBgndEarliestConnectionTime));
829b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
8308058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang            mBgndEarliestConnectionTime = backgroundCall.getEarliestConnectTime();
831b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
832b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            if (mCallheld != callheld || callsSwitched) {
833b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                mCallheld = callheld;
834b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (sendUpdate) {
835b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    result.addResponse("+CIEV: 4," + mCallheld);
836b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
837b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
838b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
839b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            if (callsetup == 1 && callsetup != prevCallsetup) {
840b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // new incoming call
841b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                String number = null;
842b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                int type = 128;
843b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // find incoming phone number and type
844b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (connection == null) {
8458058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                    connection = ringingCall.getEarliestConnection();
846b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    if (connection == null) {
847b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        Log.e(TAG, "Could not get a handle on Connection object for new " +
848b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                              "incoming call");
849b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    }
850b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
851b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (connection != null) {
852b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    number = connection.getAddress();
853b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    if (number != null) {
854b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        type = PhoneNumberUtils.toaFromString(number);
855b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    }
856b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
857b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (number == null) {
858b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    number = "";
859b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
860b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if ((call != 0 || callheld != 0) && sendUpdate) {
861b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    // call waiting
862b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    if ((mRemoteBrsf & BRSF_HF_CW_THREE_WAY_CALLING) != 0x0) {
863b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        result.addResponse("+CCWA: \"" + number + "\"," + type);
8644b8337277ec2e375c3536b97c40e6617a7b12990The Android Open Source Project                        result.addResponse("+CIEV: 3," + callsetup);
865b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    }
866b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                } else {
867b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    // regular new incoming call
868b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    mRingingNumber = number;
869b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    mRingingType = type;
870b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    mIgnoreRing = false;
8711ec7e6b9b3bf1dfc384676244bec1edb50fedc58Jaikumar Ganesh                    mStopRing = false;
872b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
873a23972e3605fb235897250fd4edc2b70be13e00dNick Pelly                    if ((mLocalBrsf & BRSF_AG_IN_BAND_RING) != 0x0) {
874a23972e3605fb235897250fd4edc2b70be13e00dNick Pelly                        audioOn();
875a23972e3605fb235897250fd4edc2b70be13e00dNick Pelly                    }
876b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    result.addResult(ring());
877b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
878b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
879b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            sendURC(result.toString());
880b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
881b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
882487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh        private int getCdmaCallHeldStatus(CdmaPhoneCallState.PhoneCallState currState,
883487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                                  CdmaPhoneCallState.PhoneCallState prevState) {
884487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh            int callheld;
885487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh            // Update the Call held information
886487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh            if (currState == CdmaPhoneCallState.PhoneCallState.CONF_CALL) {
887487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                if (prevState == CdmaPhoneCallState.PhoneCallState.THRWAY_ACTIVE) {
888487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                    callheld = 0; //0: no calls held, as now *both* the caller are active
889487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                } else {
890487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                    callheld = 1; //1: held call and active call, as on answering a
891487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                            // Call Waiting, one of the caller *is* put on hold
892487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                }
893487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh            } else if (currState == CdmaPhoneCallState.PhoneCallState.THRWAY_ACTIVE) {
894487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                callheld = 1; //1: held call and active call, as on make a 3 Way Call
895487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                        // the first caller *is* put on hold
896487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh            } else {
897487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh                callheld = 0; //0: no calls held as this is a SINGLE_ACTIVE call
898487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh            }
899487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh            return callheld;
900487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh        }
901487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh
902487f0e7aa6b2db7212e76713199d4812a61c4625Jaikumar Ganesh
903b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private AtCommandResult ring() {
9048058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang            if (!mIgnoreRing && !mStopRing && mCM.getFirstActiveRingingCall().isRinging()) {
905b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                AtCommandResult result = new AtCommandResult(AtCommandResult.UNSOLICITED);
906b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                result.addResponse("RING");
907b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (sendClipUpdate()) {
908b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    result.addResponse("+CLIP: \"" + mRingingNumber + "\"," + mRingingType);
909b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
910b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
911b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                Message msg = mStateChangeHandler.obtainMessage(RING);
912b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                mStateChangeHandler.sendMessageDelayed(msg, 3000);
913b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return result;
914b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
915b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            return null;
916b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
917b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
918b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private synchronized String toCregString() {
919b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            return new String("+CREG: 1," + mStat);
920b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
921b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
922b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private synchronized AtCommandResult toCindResult() {
923b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            AtCommandResult result = new AtCommandResult(AtCommandResult.OK);
924d16be9780f29c031e3d854fde5b92f05392836e3Jaikumar Ganesh            mSignal = asuToSignal(mCM.getDefaultPhone().getSignalStrength());
925d16be9780f29c031e3d854fde5b92f05392836e3Jaikumar Ganesh
926b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            String status = "+CIND: " + mService + "," + mCall + "," + mCallsetup + "," +
927b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                            mCallheld + "," + mSignal + "," + mRoam + "," + mBattchg;
928b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            result.addResponse(status);
929b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            return result;
930b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
931b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
932b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private synchronized AtCommandResult toCsqResult() {
933b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            AtCommandResult result = new AtCommandResult(AtCommandResult.OK);
934b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            String status = "+CSQ: " + mRssi + ",99";
935b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            result.addResponse(status);
936b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            return result;
937b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
938b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
939b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
940b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private synchronized AtCommandResult getCindTestResult() {
941b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            return new AtCommandResult("+CIND: (\"service\",(0-1))," + "(\"call\",(0-1))," +
942b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        "(\"callsetup\",(0-3)),(\"callheld\",(0-2)),(\"signal\",(0-5))," +
943b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        "(\"roam\",(0-1)),(\"battchg\",(0-5))");
944b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
945b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
946b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private synchronized void ignoreRing() {
947b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mCallsetup = 0;
948b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mIgnoreRing = true;
949b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            if (sendUpdate()) {
950b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                sendURC("+CIEV: 3," + mCallsetup);
951b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
952b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
953b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
954b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    };
955b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
956b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private static final int SCO_ACCEPTED = 1;
957b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private static final int SCO_CONNECTED = 2;
958b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private static final int SCO_CLOSED = 3;
959b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private static final int CHECK_CALL_STARTED = 4;
960b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private static final int CHECK_VOICE_RECOGNITION_STARTED = 5;
961f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan    private static final int MESSAGE_CHECK_PENDING_SCO = 6;
962b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
963b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private final Handler mHandler = new Handler() {
964b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        @Override
9657757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh        public void handleMessage(Message msg) {
9667757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh            synchronized (BluetoothHandsfree.this) {
9677757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                switch (msg.what) {
9687757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                case SCO_ACCEPTED:
9697757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                    if (msg.arg1 == ScoSocket.STATE_CONNECTED) {
9707757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                        if (isHeadsetConnected() && (mAudioPossible || allowAudioAnytime()) &&
9717757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                                mConnectedSco == null) {
9727757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                            Log.i(TAG, "Routing audio for incoming SCO connection");
9737757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                            mConnectedSco = (ScoSocket)msg.obj;
9747757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                            mAudioManager.setBluetoothScoOn(true);
975db065e9834bcad9b0cb16c272b4b272dee8bdf62Nick Pelly                            broadcastAudioStateIntent(BluetoothHeadset.AUDIO_STATE_CONNECTED,
976db065e9834bcad9b0cb16c272b4b272dee8bdf62Nick Pelly                                    mHeadset.getRemoteDevice());
9777757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                        } else {
9787757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                            Log.i(TAG, "Rejecting incoming SCO connection");
9797757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                            ((ScoSocket)msg.obj).close();
9807757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                        }
9817757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                    } // else error trying to accept, try again
9827757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                    mIncomingSco = createScoSocket();
9837757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                    mIncomingSco.accept();
9847757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                    break;
9857757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                case SCO_CONNECTED:
9867757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                    if (msg.arg1 == ScoSocket.STATE_CONNECTED && isHeadsetConnected() &&
987b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                            mConnectedSco == null) {
9888eb32ddb60cfe1a0d0733735c30ecfc814452806Jackson Fan                        if (VDBG) log("Routing audio for outgoing SCO conection");
989b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        mConnectedSco = (ScoSocket)msg.obj;
990b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        mAudioManager.setBluetoothScoOn(true);
991db065e9834bcad9b0cb16c272b4b272dee8bdf62Nick Pelly                        broadcastAudioStateIntent(BluetoothHeadset.AUDIO_STATE_CONNECTED,
992db065e9834bcad9b0cb16c272b4b272dee8bdf62Nick Pelly                                mHeadset.getRemoteDevice());
9937757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                    } else if (msg.arg1 == ScoSocket.STATE_CONNECTED) {
9948eb32ddb60cfe1a0d0733735c30ecfc814452806Jackson Fan                        if (VDBG) log("Rejecting new connected outgoing SCO socket");
995b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        ((ScoSocket)msg.obj).close();
9967757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                        mOutgoingSco.close();
997b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    }
998b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    mOutgoingSco = null;
9997757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                    break;
10007757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                case SCO_CLOSED:
10017757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                    if (mConnectedSco == (ScoSocket)msg.obj) {
1002819fc69ac472a8d77be8e5dd79a21434c6de8a37Jaikumar Ganesh                        mConnectedSco.close();
10037757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                        mConnectedSco = null;
10047757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                        mAudioManager.setBluetoothScoOn(false);
1005db065e9834bcad9b0cb16c272b4b272dee8bdf62Nick Pelly                        broadcastAudioStateIntent(BluetoothHeadset.AUDIO_STATE_DISCONNECTED,
1006819fc69ac472a8d77be8e5dd79a21434c6de8a37Jaikumar Ganesh                               mHeadset.getRemoteDevice());
10077757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                    } else if (mOutgoingSco == (ScoSocket)msg.obj) {
1008819fc69ac472a8d77be8e5dd79a21434c6de8a37Jaikumar Ganesh                        mOutgoingSco.close();
10097757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                        mOutgoingSco = null;
1010b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    }
10117757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                    break;
10127757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                case CHECK_CALL_STARTED:
10137757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                    if (mWaitingForCallStart) {
10147757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                        mWaitingForCallStart = false;
10157757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                        Log.e(TAG, "Timeout waiting for call to start");
10167757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                        sendURC("ERROR");
10177757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                        if (mStartCallWakeLock.isHeld()) {
10187757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                            mStartCallWakeLock.release();
10197757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                        }
10207757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                    }
10217757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                    break;
10227757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                case CHECK_VOICE_RECOGNITION_STARTED:
10237757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                    if (mWaitingForVoiceRecognition) {
10247757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                        mWaitingForVoiceRecognition = false;
10257757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                        Log.e(TAG, "Timeout waiting for voice recognition to start");
10267757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                        sendURC("ERROR");
10277757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                    }
10287757ebca8f27cd580bc85b6afe66ca43122b8c39Jaikumar Ganesh                    break;
1029f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                case MESSAGE_CHECK_PENDING_SCO:
1030f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                    if (mPendingSco && isA2dpMultiProfile()) {
1031f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                        Log.w(TAG, "Timeout suspending A2DP for SCO (mA2dpState = " +
1032f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                                mA2dpState + "). Starting SCO anyway");
1033f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                        mOutgoingSco = createScoSocket();
1034819fc69ac472a8d77be8e5dd79a21434c6de8a37Jaikumar Ganesh                        if (!(isHeadsetConnected() &&
1035819fc69ac472a8d77be8e5dd79a21434c6de8a37Jaikumar Ganesh                                mOutgoingSco.connect(mHeadset.getRemoteDevice().getAddress(),
1036819fc69ac472a8d77be8e5dd79a21434c6de8a37Jaikumar Ganesh                                 mHeadset.getRemoteDevice().getName()))) {
1037f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                            mOutgoingSco = null;
1038f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                        }
1039f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                        mPendingSco = false;
1040f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                    }
1041f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                    break;
1042b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
1043b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1044b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
1045b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    };
1046b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1047b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private ScoSocket createScoSocket() {
1048b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return new ScoSocket(mPowerManager, mHandler, SCO_ACCEPTED, SCO_CONNECTED, SCO_CLOSED);
1049b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
1050b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1051db065e9834bcad9b0cb16c272b4b272dee8bdf62Nick Pelly    private void broadcastAudioStateIntent(int state, BluetoothDevice device) {
10524079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project        if (VDBG) log("broadcastAudioStateIntent(" + state + ")");
105318e0a07cc700234f5a02fe10eee9b3ea10db58b3Nick Pelly        Intent intent = new Intent(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED);
105418e0a07cc700234f5a02fe10eee9b3ea10db58b3Nick Pelly        intent.putExtra(BluetoothHeadset.EXTRA_AUDIO_STATE, state);
105518e0a07cc700234f5a02fe10eee9b3ea10db58b3Nick Pelly        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
10564079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project        mContext.sendBroadcast(intent, android.Manifest.permission.BLUETOOTH);
10574079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project    }
10584079f559beb3e414036056b4b33ad40de5e89c4aThe Android Open Source Project
1059a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville    void updateBtHandsfreeAfterRadioTechnologyChange() {
10608eb32ddb60cfe1a0d0733735c30ecfc814452806Jackson Fan        if(VDBG) Log.d(TAG, "updateBtHandsfreeAfterRadioTechnologyChange...");
1061a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville
10621dad0387bf2f280c12bdac172604e9779134fbcdJaikumar Ganesh        mBluetoothPhoneState.updateBtPhoneStateAfterRadioTechnologyChange();
1063a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville    }
1064a69de9f9a2e99fe08f0009bb218b95ed7985c575Wink Saville
1065b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /** Request to establish SCO (audio) connection to bluetooth
1066b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * headset/handsfree, if one is connected. Does not block.
1067b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * Returns false if the user has requested audio off, or if there
1068b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * is some other immediate problem that will prevent BT audio.
1069b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     */
1070b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /* package */ synchronized boolean audioOn() {
1071b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (VDBG) log("audioOn()");
1072b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (!isHeadsetConnected()) {
1073b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            if (DBG) log("audioOn(): headset is not connected!");
1074b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            return false;
1075b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
10760966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly        if (mHeadsetType == TYPE_HANDSFREE && !mServiceConnectionEstablished) {
10770966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly            if (DBG) log("audioOn(): service connection not yet established!");
10780966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly            return false;
10790966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly        }
1080b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1081b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (mConnectedSco != null) {
1082b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            if (DBG) log("audioOn(): audio is already connected");
1083b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            return true;
1084b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
1085b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1086b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (!mUserWantsAudio) {
1087b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            if (DBG) log("audioOn(): user requested no audio, ignoring");
1088b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            return false;
1089b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
1090b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1091b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (mOutgoingSco != null) {
1092b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            if (DBG) log("audioOn(): outgoing SCO already in progress");
1093b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            return true;
1094b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
1095f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan
1096f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan        if (mPendingSco) {
1097f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan            if (DBG) log("audioOn(): SCO already pending");
1098f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan            return true;
1099f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan        }
1100f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan
1101ab377a6e67c195c230db42e98d597bf4deba14bdEric Laurent        mA2dpSuspended = false;
1102ab377a6e67c195c230db42e98d597bf4deba14bdEric Laurent        mPendingSco = false;
1103f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan        if (isA2dpMultiProfile() && mA2dpState == BluetoothA2dp.STATE_PLAYING) {
1104f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan            if (DBG) log("suspending A2DP stream for SCO");
1105ab377a6e67c195c230db42e98d597bf4deba14bdEric Laurent            mA2dpSuspended = mA2dp.suspendSink(mA2dpDevice);
1106ab377a6e67c195c230db42e98d597bf4deba14bdEric Laurent            if (mA2dpSuspended) {
1107ab377a6e67c195c230db42e98d597bf4deba14bdEric Laurent                mPendingSco = true;
1108f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                Message msg = mHandler.obtainMessage(MESSAGE_CHECK_PENDING_SCO);
1109f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                mHandler.sendMessageDelayed(msg, 2000);
1110f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan            } else {
1111f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                Log.w(TAG, "Could not suspend A2DP stream for SCO, going ahead with SCO");
1112337c7eb41d344b39efcf740cdc579257152a1c58Nick Pelly            }
1113337c7eb41d344b39efcf740cdc579257152a1c58Nick Pelly        }
1114337c7eb41d344b39efcf740cdc579257152a1c58Nick Pelly
1115337c7eb41d344b39efcf740cdc579257152a1c58Nick Pelly        if (!mPendingSco) {
1116337c7eb41d344b39efcf740cdc579257152a1c58Nick Pelly            mOutgoingSco = createScoSocket();
1117f0316a1fb5c00585d0fe4b500723557afabec70bEric Olsen            if (!mOutgoingSco.connect(mHeadset.getRemoteDevice().getAddress(),
1118f0316a1fb5c00585d0fe4b500723557afabec70bEric Olsen                    mHeadset.getRemoteDevice().getName())) {
1119337c7eb41d344b39efcf740cdc579257152a1c58Nick Pelly                mOutgoingSco = null;
1120f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan            }
1121b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
1122b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1123b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return true;
1124b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
1125b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1126b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /** Used to indicate the user requested BT audio on.
1127b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *  This will establish SCO (BT audio), even if the user requested it off
1128b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *  previously on this call.
1129b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     */
1130b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /* package */ synchronized void userWantsAudioOn() {
1131b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mUserWantsAudio = true;
1132b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        audioOn();
1133b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
1134b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /** Used to indicate the user requested BT audio off.
1135b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *  This will prevent us from establishing BT audio again during this call
1136b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *  if audioOn() is called.
1137b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     */
1138b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /* package */ synchronized void userWantsAudioOff() {
1139b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mUserWantsAudio = false;
1140b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        audioOff();
1141b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
1142b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1143b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /** Request to disconnect SCO (audio) connection to bluetooth
1144b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * headset/handsfree, if one is connected. Does not block.
1145b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     */
1146b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /* package */ synchronized void audioOff() {
1147819fc69ac472a8d77be8e5dd79a21434c6de8a37Jaikumar Ganesh        if (VDBG) log("audioOff(): mPendingSco: " + mPendingSco +
1148819fc69ac472a8d77be8e5dd79a21434c6de8a37Jaikumar Ganesh                ", mConnectedSco: " + mConnectedSco +
1149819fc69ac472a8d77be8e5dd79a21434c6de8a37Jaikumar Ganesh                ", mOutgoingSco: " + mOutgoingSco  +
1150819fc69ac472a8d77be8e5dd79a21434c6de8a37Jaikumar Ganesh                ", mA2dpState: " + mA2dpState +
1151819fc69ac472a8d77be8e5dd79a21434c6de8a37Jaikumar Ganesh                ", mA2dpSuspended: " + mA2dpSuspended +
1152819fc69ac472a8d77be8e5dd79a21434c6de8a37Jaikumar Ganesh                ", mIncomingSco:" + mIncomingSco);
1153ab377a6e67c195c230db42e98d597bf4deba14bdEric Laurent
1154ab377a6e67c195c230db42e98d597bf4deba14bdEric Laurent        if (mA2dpSuspended) {
1155a1478a9074b126d13124c99b7543b2518b3de3b7Eric Laurent            if (isA2dpMultiProfile()) {
1156a1478a9074b126d13124c99b7543b2518b3de3b7Eric Laurent              if (DBG) log("resuming A2DP stream after disconnecting SCO");
1157a1478a9074b126d13124c99b7543b2518b3de3b7Eric Laurent              mA2dp.resumeSink(mA2dpDevice);
1158a1478a9074b126d13124c99b7543b2518b3de3b7Eric Laurent            }
1159ab377a6e67c195c230db42e98d597bf4deba14bdEric Laurent            mA2dpSuspended = false;
1160ab377a6e67c195c230db42e98d597bf4deba14bdEric Laurent        }
1161b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1162310e6fb854504637a4cefd123d8dd387660cf811Nick Pelly        mPendingSco = false;
1163310e6fb854504637a4cefd123d8dd387660cf811Nick Pelly
1164b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (mConnectedSco != null) {
1165c335ba22b8d739eddaed89b78fad4520abf36b19Nick Pelly            BluetoothDevice device = null;
1166c335ba22b8d739eddaed89b78fad4520abf36b19Nick Pelly            if (mHeadset != null) {
1167c335ba22b8d739eddaed89b78fad4520abf36b19Nick Pelly                device = mHeadset.getRemoteDevice();
1168c335ba22b8d739eddaed89b78fad4520abf36b19Nick Pelly            }
1169b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mConnectedSco.close();
1170b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mConnectedSco = null;
11711b69b5d702539a74d1511d7467737eddc24b7744Nick Pelly            mAudioManager.setBluetoothScoOn(false);
11721b69b5d702539a74d1511d7467737eddc24b7744Nick Pelly            broadcastAudioStateIntent(BluetoothHeadset.AUDIO_STATE_DISCONNECTED, device);
1173b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
1174b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (mOutgoingSco != null) {
1175b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mOutgoingSco.close();
1176b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mOutgoingSco = null;
1177b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
1178819fc69ac472a8d77be8e5dd79a21434c6de8a37Jaikumar Ganesh
1179b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
1180b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1181b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /* package */ boolean isAudioOn() {
1182b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return (mConnectedSco != null);
1183b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
1184b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1185f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan    private boolean isA2dpMultiProfile() {
1186f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan        return mA2dp != null && mHeadset != null && mA2dpDevice != null &&
1187f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan                mA2dpDevice.equals(mHeadset.getRemoteDevice());
1188f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan    }
1189f9f946bc1e3ba287f0fa8f47a662e42f20892526Zhu Lan
1190b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /* package */ void ignoreRing() {
11911dad0387bf2f280c12bdac172604e9779134fbcdJaikumar Ganesh        mBluetoothPhoneState.ignoreRing();
1192b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
1193b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1194b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private void sendURC(String urc) {
1195b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (isHeadsetConnected()) {
1196b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mHeadset.sendURC(urc);
1197b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
1198b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
1199b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1200b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /** helper to redial last dialled number */
1201b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private AtCommandResult redial() {
1202b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        String number = mPhonebook.getLastDialledNumber();
1203b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (number == null) {
1204b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // spec seems to suggest sending ERROR if we dont have a
1205b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // number to redial
12068eb32ddb60cfe1a0d0733735c30ecfc814452806Jackson Fan            if (VDBG) log("Bluetooth redial requested (+BLDN), but no previous " +
1207b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                  "outgoing calls found. Ignoring");
1208b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            return new AtCommandResult(AtCommandResult.ERROR);
1209b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
1210b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
1211b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                Uri.fromParts("tel", number, null));
1212b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1213b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mContext.startActivity(intent);
1214b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1215b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // We do not immediately respond OK, wait until we get a phone state
1216b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // update. If we return OK now and the handsfree immeidately requests
1217b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // our phone state it will say we are not in call yet which confuses
1218b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // some devices
1219b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        expectCallStart();
1220b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return new AtCommandResult(AtCommandResult.UNSOLICITED);  // send nothing
1221b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
1222b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1223b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /** Build the +CLCC result
1224b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *  The complexity arises from the fact that we need to maintain the same
1225b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *  CLCC index even as a call moves between states. */
1226c9d9ed30aa547b79b81adc13a4d148a003b6ee62w    private synchronized AtCommandResult gsmGetClccResult() {
1227b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Collect all known connections
1228c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        Connection[] clccConnections = new Connection[GSM_MAX_CONNECTIONS];  // indexed by CLCC index
1229b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        LinkedList<Connection> newConnections = new LinkedList<Connection>();
1230b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        LinkedList<Connection> connections = new LinkedList<Connection>();
1231a50e10e2efadac960987eaadc0938c6f92d3ee90John Wang
12328058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang        Call foregroundCall = mCM.getActiveFgCall();
12338058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang        Call backgroundCall = mCM.getFirstActiveBgCall();
12348058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang        Call ringingCall = mCM.getFirstActiveRingingCall();
12358058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang
12368058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang        if (ringingCall.getState().isAlive()) {
12378058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang            connections.addAll(ringingCall.getConnections());
1238b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
12398058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang        if (foregroundCall.getState().isAlive()) {
12408058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang            connections.addAll(foregroundCall.getConnections());
1241b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
12428058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang        if (backgroundCall.getState().isAlive()) {
12438058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang            connections.addAll(backgroundCall.getConnections());
1244b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
1245b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1246b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Mark connections that we already known about
1247c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        boolean clccUsed[] = new boolean[GSM_MAX_CONNECTIONS];
1248c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        for (int i = 0; i < GSM_MAX_CONNECTIONS; i++) {
1249b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            clccUsed[i] = mClccUsed[i];
1250b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mClccUsed[i] = false;
1251b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
1252b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        for (Connection c : connections) {
1253b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            boolean found = false;
1254b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            long timestamp = c.getCreateTime();
1255c9d9ed30aa547b79b81adc13a4d148a003b6ee62w            for (int i = 0; i < GSM_MAX_CONNECTIONS; i++) {
1256b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (clccUsed[i] && timestamp == mClccTimestamps[i]) {
1257b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    mClccUsed[i] = true;
1258b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    found = true;
1259b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    clccConnections[i] = c;
1260b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    break;
1261b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
1262b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1263b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            if (!found) {
1264b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                newConnections.add(c);
1265b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1266b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
1267b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1268b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Find a CLCC index for new connections
1269b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        while (!newConnections.isEmpty()) {
1270b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // Find lowest empty index
1271b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            int i = 0;
1272b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            while (mClccUsed[i]) i++;
1273b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // Find earliest connection
1274b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            long earliestTimestamp = newConnections.get(0).getCreateTime();
1275b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            Connection earliestConnection = newConnections.get(0);
1276b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            for (int j = 0; j < newConnections.size(); j++) {
1277b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                long timestamp = newConnections.get(j).getCreateTime();
1278b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (timestamp < earliestTimestamp) {
1279b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    earliestTimestamp = timestamp;
1280b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    earliestConnection = newConnections.get(j);
1281b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
1282b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1283b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1284b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // update
1285b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mClccUsed[i] = true;
1286b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mClccTimestamps[i] = earliestTimestamp;
1287b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            clccConnections[i] = earliestConnection;
1288b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            newConnections.remove(earliestConnection);
1289b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
1290b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1291b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Build CLCC
1292b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        AtCommandResult result = new AtCommandResult(AtCommandResult.OK);
1293b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        for (int i = 0; i < clccConnections.length; i++) {
1294b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            if (mClccUsed[i]) {
1295b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                String clccEntry = connectionToClccEntry(i, clccConnections[i]);
1296b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (clccEntry != null) {
1297b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    result.addResponse(clccEntry);
1298b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
1299b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1300b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
1301b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1302b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return result;
1303b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
1304b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1305b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /** Convert a Connection object into a single +CLCC result */
1306b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private String connectionToClccEntry(int index, Connection c) {
1307b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        int state;
1308b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        switch (c.getState()) {
1309b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        case ACTIVE:
1310b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            state = 0;
1311b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            break;
1312b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        case HOLDING:
1313b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            state = 1;
1314b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            break;
1315b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        case DIALING:
1316b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            state = 2;
1317b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            break;
1318b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        case ALERTING:
1319b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            state = 3;
1320b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            break;
1321b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        case INCOMING:
1322b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            state = 4;
1323b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            break;
1324b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        case WAITING:
1325b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            state = 5;
1326b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            break;
1327b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        default:
1328b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            return null;  // bad state
1329b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
1330b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1331b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        int mpty = 0;
1332b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        Call call = c.getCall();
1333b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (call != null) {
1334b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mpty = call.isMultiparty() ? 1 : 0;
1335b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
1336b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1337b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        int direction = c.isIncoming() ? 1 : 0;
1338b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1339b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        String number = c.getAddress();
1340b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        int type = -1;
1341b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (number != null) {
1342b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            type = PhoneNumberUtils.toaFromString(number);
1343b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
1344b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1345b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        String result = "+CLCC: " + (index + 1) + "," + direction + "," + state + ",0," + mpty;
1346b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (number != null) {
1347b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            result += ",\"" + number + "\"," + type;
1348b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
1349b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return result;
1350b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
1351c9d9ed30aa547b79b81adc13a4d148a003b6ee62w
1352c9d9ed30aa547b79b81adc13a4d148a003b6ee62w    /** Build the +CLCC result for CDMA
1353c9d9ed30aa547b79b81adc13a4d148a003b6ee62w     *  The complexity arises from the fact that we need to maintain the same
1354c9d9ed30aa547b79b81adc13a4d148a003b6ee62w     *  CLCC index even as a call moves between states. */
1355c9d9ed30aa547b79b81adc13a4d148a003b6ee62w    private synchronized AtCommandResult cdmaGetClccResult() {
1356c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        // In CDMA at one time a user can have only two live/active connections
1357c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        Connection[] clccConnections = new Connection[CDMA_MAX_CONNECTIONS];// indexed by CLCC index
13588058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang        Call foregroundCall = mCM.getActiveFgCall();
13598058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang        Call ringingCall = mCM.getFirstActiveRingingCall();
1360c9d9ed30aa547b79b81adc13a4d148a003b6ee62w
13618058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang        Call.State ringingCallState = ringingCall.getState();
1362c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        // If the Ringing Call state is INCOMING, that means this is the very first call
1363c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        // hence there should not be any Foreground Call
1364c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        if (ringingCallState == Call.State.INCOMING) {
13658eb32ddb60cfe1a0d0733735c30ecfc814452806Jackson Fan            if (VDBG) log("Filling clccConnections[0] for INCOMING state");
13668058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang            clccConnections[0] = ringingCall.getLatestConnection();
13678058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang        } else if (foregroundCall.getState().isAlive()) {
1368c9d9ed30aa547b79b81adc13a4d148a003b6ee62w            // Getting Foreground Call connection based on Call state
13698058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang            if (ringingCall.isRinging()) {
13708eb32ddb60cfe1a0d0733735c30ecfc814452806Jackson Fan                if (VDBG) log("Filling clccConnections[0] & [1] for CALL WAITING state");
13718058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                clccConnections[0] = foregroundCall.getEarliestConnection();
13728058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                clccConnections[1] = ringingCall.getLatestConnection();
1373c9d9ed30aa547b79b81adc13a4d148a003b6ee62w            } else {
13748058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                if (foregroundCall.getConnections().size() <= 1) {
1375c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                    // Single call scenario
13768eb32ddb60cfe1a0d0733735c30ecfc814452806Jackson Fan                    if (VDBG) log("Filling clccConnections[0] with ForgroundCall latest connection");
13778058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                    clccConnections[0] = foregroundCall.getLatestConnection();
1378c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                } else {
1379c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                    // Multiple Call scenario. This would be true for both
1380c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                    // CONF_CALL and THRWAY_ACTIVE state
13818eb32ddb60cfe1a0d0733735c30ecfc814452806Jackson Fan                    if (VDBG) log("Filling clccConnections[0] & [1] with ForgroundCall connections");
13828058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                    clccConnections[0] = foregroundCall.getEarliestConnection();
13838058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                    clccConnections[1] = foregroundCall.getLatestConnection();
1384c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                }
1385c9d9ed30aa547b79b81adc13a4d148a003b6ee62w            }
1386c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        }
1387c9d9ed30aa547b79b81adc13a4d148a003b6ee62w
1388c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        // Update the mCdmaIsSecondCallActive flag based on the Phone call state
1389c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        if (PhoneApp.getInstance().cdmaPhoneCallState.getCurrentCallState()
1390c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                == CdmaPhoneCallState.PhoneCallState.SINGLE_ACTIVE) {
1391c9d9ed30aa547b79b81adc13a4d148a003b6ee62w            cdmaSetSecondCallState(false);
1392c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        } else if (PhoneApp.getInstance().cdmaPhoneCallState.getCurrentCallState()
1393c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                == CdmaPhoneCallState.PhoneCallState.THRWAY_ACTIVE) {
1394c9d9ed30aa547b79b81adc13a4d148a003b6ee62w            cdmaSetSecondCallState(true);
1395c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        }
1396c9d9ed30aa547b79b81adc13a4d148a003b6ee62w
1397c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        // Build CLCC
1398c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        AtCommandResult result = new AtCommandResult(AtCommandResult.OK);
1399c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        for (int i = 0; (i < clccConnections.length) && (clccConnections[i] != null); i++) {
1400c9d9ed30aa547b79b81adc13a4d148a003b6ee62w            String clccEntry = cdmaConnectionToClccEntry(i, clccConnections[i]);
1401c9d9ed30aa547b79b81adc13a4d148a003b6ee62w            if (clccEntry != null) {
1402c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                result.addResponse(clccEntry);
1403c9d9ed30aa547b79b81adc13a4d148a003b6ee62w            }
1404c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        }
1405c9d9ed30aa547b79b81adc13a4d148a003b6ee62w
1406c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        return result;
1407c9d9ed30aa547b79b81adc13a4d148a003b6ee62w    }
1408c9d9ed30aa547b79b81adc13a4d148a003b6ee62w
1409c9d9ed30aa547b79b81adc13a4d148a003b6ee62w    /** Convert a Connection object into a single +CLCC result for CDMA phones */
1410c9d9ed30aa547b79b81adc13a4d148a003b6ee62w    private String cdmaConnectionToClccEntry(int index, Connection c) {
1411c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        int state;
1412c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        PhoneApp app = PhoneApp.getInstance();
1413c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        CdmaPhoneCallState.PhoneCallState currCdmaCallState =
1414c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                app.cdmaPhoneCallState.getCurrentCallState();
1415c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        CdmaPhoneCallState.PhoneCallState prevCdmaCallState =
1416c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                app.cdmaPhoneCallState.getPreviousCallState();
1417c9d9ed30aa547b79b81adc13a4d148a003b6ee62w
1418c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        if ((prevCdmaCallState == CdmaPhoneCallState.PhoneCallState.THRWAY_ACTIVE)
1419c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                && (currCdmaCallState == CdmaPhoneCallState.PhoneCallState.CONF_CALL)) {
1420c9d9ed30aa547b79b81adc13a4d148a003b6ee62w            // If the current state is reached after merging two calls
1421c9d9ed30aa547b79b81adc13a4d148a003b6ee62w            // we set the state of all the connections as ACTIVE
1422c9d9ed30aa547b79b81adc13a4d148a003b6ee62w            state = 0;
1423c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        } else {
1424c9d9ed30aa547b79b81adc13a4d148a003b6ee62w            switch (c.getState()) {
1425c9d9ed30aa547b79b81adc13a4d148a003b6ee62w            case ACTIVE:
1426c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                // For CDMA since both the connections are set as active by FW after accepting
1427c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                // a Call waiting or making a 3 way call, we need to set the state specifically
1428c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                // to ACTIVE/HOLDING based on the mCdmaIsSecondCallActive flag. This way the
1429c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                // CLCC result will allow BT devices to enable the swap or merge options
1430c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                if (index == 0) { // For the 1st active connection
1431c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                    state = mCdmaIsSecondCallActive ? 1 : 0;
1432c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                } else { // for the 2nd active connection
1433c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                    state = mCdmaIsSecondCallActive ? 0 : 1;
1434c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                }
1435c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                break;
1436c9d9ed30aa547b79b81adc13a4d148a003b6ee62w            case HOLDING:
1437c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                state = 1;
1438c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                break;
1439c9d9ed30aa547b79b81adc13a4d148a003b6ee62w            case DIALING:
1440c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                state = 2;
1441c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                break;
1442c9d9ed30aa547b79b81adc13a4d148a003b6ee62w            case ALERTING:
1443c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                state = 3;
1444c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                break;
1445c9d9ed30aa547b79b81adc13a4d148a003b6ee62w            case INCOMING:
1446c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                state = 4;
1447c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                break;
1448c9d9ed30aa547b79b81adc13a4d148a003b6ee62w            case WAITING:
1449c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                state = 5;
1450c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                break;
1451c9d9ed30aa547b79b81adc13a4d148a003b6ee62w            default:
1452c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                return null;  // bad state
1453c9d9ed30aa547b79b81adc13a4d148a003b6ee62w            }
1454c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        }
1455c9d9ed30aa547b79b81adc13a4d148a003b6ee62w
1456c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        int mpty = 0;
14573eb2a4ae98eb737004d40f7ce03a8d83883fb079Kuanting Zhu        if (currCdmaCallState == CdmaPhoneCallState.PhoneCallState.CONF_CALL) {
1458c9d9ed30aa547b79b81adc13a4d148a003b6ee62w            mpty = 1;
14593eb2a4ae98eb737004d40f7ce03a8d83883fb079Kuanting Zhu        } else {
14603eb2a4ae98eb737004d40f7ce03a8d83883fb079Kuanting Zhu            mpty = 0;
1461c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        }
1462c9d9ed30aa547b79b81adc13a4d148a003b6ee62w
1463c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        int direction = c.isIncoming() ? 1 : 0;
1464c9d9ed30aa547b79b81adc13a4d148a003b6ee62w
1465c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        String number = c.getAddress();
1466c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        int type = -1;
1467c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        if (number != null) {
1468c9d9ed30aa547b79b81adc13a4d148a003b6ee62w            type = PhoneNumberUtils.toaFromString(number);
1469c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        }
1470c9d9ed30aa547b79b81adc13a4d148a003b6ee62w
1471c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        String result = "+CLCC: " + (index + 1) + "," + direction + "," + state + ",0," + mpty;
1472c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        if (number != null) {
1473c9d9ed30aa547b79b81adc13a4d148a003b6ee62w            result += ",\"" + number + "\"," + type;
1474c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        }
1475c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        return result;
1476c9d9ed30aa547b79b81adc13a4d148a003b6ee62w    }
1477c9d9ed30aa547b79b81adc13a4d148a003b6ee62w
1478b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /**
1479b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * Register AT Command handlers to implement the Headset profile
1480b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     */
1481b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private void initializeHeadsetAtParser() {
14828eb32ddb60cfe1a0d0733735c30ecfc814452806Jackson Fan        if (VDBG) log("Registering Headset AT commands");
1483b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        AtParser parser = mHeadset.getAtParser();
1484b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Headset's usually only have one button, which is meant to cause the
1485b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // HS to send us AT+CKPD=200 or AT+CKPD.
1486b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register("+CKPD", new AtCommandHandler() {
1487b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            private AtCommandResult headsetButtonPress() {
14888058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                if (mCM.getFirstActiveRingingCall().isRinging()) {
1489b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    // Answer the call
14901ec7e6b9b3bf1dfc384676244bec1edb50fedc58Jaikumar Ganesh                    mBluetoothPhoneState.stopRing();
14911ec7e6b9b3bf1dfc384676244bec1edb50fedc58Jaikumar Ganesh                    sendURC("OK");
14928058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                    PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1493a23972e3605fb235897250fd4edc2b70be13e00dNick Pelly                    // If in-band ring tone is supported, SCO connection will already
1494a23972e3605fb235897250fd4edc2b70be13e00dNick Pelly                    // be up and the following call will just return.
1495b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    audioOn();
14961ec7e6b9b3bf1dfc384676244bec1edb50fedc58Jaikumar Ganesh                    return new AtCommandResult(AtCommandResult.UNSOLICITED);
14978058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                } else if (mCM.hasActiveFgCall()) {
1498b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    if (!isAudioOn()) {
1499b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        // Transfer audio from AG to HS
1500b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        audioOn();
1501b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    } else {
1502b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        if (mHeadset.getDirection() == HeadsetBase.DIRECTION_INCOMING &&
1503b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                          (System.currentTimeMillis() - mHeadset.getConnectTimestamp()) < 5000) {
1504b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                            // Headset made a recent ACL connection to us - and
1505b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                            // made a mandatory AT+CKPD request to connect
1506b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                            // audio which races with our automatic audio
1507b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                            // setup.  ignore
1508b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        } else {
1509b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                            // Hang up the call
1510b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                            audioOff();
15118343169cc89621d46dce86449f5ee1ff5d3a4919John Wang                            PhoneUtils.hangup(PhoneApp.getInstance().mCM);
1512b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        }
1513b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    }
15141ec7e6b9b3bf1dfc384676244bec1edb50fedc58Jaikumar Ganesh                    return new AtCommandResult(AtCommandResult.OK);
1515b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                } else {
1516b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    // No current call - redial last number
1517b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    return redial();
1518b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
1519b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1520b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1521b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleActionCommand() {
1522b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return headsetButtonPress();
1523b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1524b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1525b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleSetCommand(Object[] args) {
1526b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return headsetButtonPress();
1527b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1528b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
1529b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
1530b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1531b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /**
1532b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     * Register AT Command handlers to implement the Handsfree profile
1533b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     */
1534b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private void initializeHandsfreeAtParser() {
15358eb32ddb60cfe1a0d0733735c30ecfc814452806Jackson Fan        if (VDBG) log("Registering Handsfree AT commands");
1536b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        AtParser parser = mHeadset.getAtParser();
15378058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang        final Phone phone = mCM.getDefaultPhone();
1538b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1539b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Answer
1540b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register('A', new AtCommandHandler() {
1541b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1542b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleBasicCommand(String args) {
15431ec7e6b9b3bf1dfc384676244bec1edb50fedc58Jaikumar Ganesh                sendURC("OK");
15441ec7e6b9b3bf1dfc384676244bec1edb50fedc58Jaikumar Ganesh                mBluetoothPhoneState.stopRing();
15458058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
15461ec7e6b9b3bf1dfc384676244bec1edb50fedc58Jaikumar Ganesh                return new AtCommandResult(AtCommandResult.UNSOLICITED);
1547b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1548b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
1549b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register('D', new AtCommandHandler() {
1550b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1551b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleBasicCommand(String args) {
1552b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (args.length() > 0) {
1553b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    if (args.charAt(0) == '>') {
1554b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        // Yuck - memory dialling requested.
1555b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        // Just dial last number for now
1556b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        if (args.startsWith(">9999")) {   // for PTS test
1557b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                            return new AtCommandResult(AtCommandResult.ERROR);
1558b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        }
1559b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        return redial();
1560b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    } else {
1561b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        // Remove trailing ';'
1562b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        if (args.charAt(args.length() - 1) == ';') {
1563b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                            args = args.substring(0, args.length() - 1);
1564b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        }
1565b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
1566b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                                Uri.fromParts("tel", args, null));
1567b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1568b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        mContext.startActivity(intent);
1569b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1570b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        expectCallStart();
1571b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        return new AtCommandResult(AtCommandResult.UNSOLICITED);  // send nothing
1572b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    }
1573b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
1574b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return new AtCommandResult(AtCommandResult.ERROR);
1575b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1576b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
1577b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1578b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Hang-up command
1579b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register("+CHUP", new AtCommandHandler() {
1580b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1581b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleActionCommand() {
1582586668fe6a1259083fbbc67de8ecac850c1475f1Jaikumar Ganesh                sendURC("OK");
15838058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                if (mCM.hasActiveFgCall()) {
15848058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                    PhoneUtils.hangupActiveCall(mCM.getActiveFgCall());
15858058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                } else if (mCM.hasActiveRingingCall()) {
15868058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                    PhoneUtils.hangupRingingCall(mCM.getFirstActiveRingingCall());
15878058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                } else if (mCM.hasActiveBgCall()) {
15888058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                    PhoneUtils.hangupHoldingCall(mCM.getFirstActiveBgCall());
1589b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
1590586668fe6a1259083fbbc67de8ecac850c1475f1Jaikumar Ganesh                return new AtCommandResult(AtCommandResult.UNSOLICITED);
1591b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1592b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
1593b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1594b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Bluetooth Retrieve Supported Features command
1595b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register("+BRSF", new AtCommandHandler() {
1596b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            private AtCommandResult sendBRSF() {
1597b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return new AtCommandResult("+BRSF: " + mLocalBrsf);
1598b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1599b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1600b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleSetCommand(Object[] args) {
1601b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // AT+BRSF=<handsfree supported features bitmap>
1602b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // Handsfree is telling us which features it supports. We
1603b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // send the features we support
1604b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (args.length == 1 && (args[0] instanceof Integer)) {
1605b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    mRemoteBrsf = (Integer) args[0];
1606b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                } else {
1607b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    Log.w(TAG, "HF didn't sent BRSF assuming 0");
1608b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
1609b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return sendBRSF();
1610b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1611b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1612b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleActionCommand() {
1613b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // This seems to be out of spec, but lets do the nice thing
1614b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return sendBRSF();
1615b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1616b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1617b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleReadCommand() {
1618b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // This seems to be out of spec, but lets do the nice thing
1619b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return sendBRSF();
1620b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1621b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
1622b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1623b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Call waiting notification on/off
1624b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register("+CCWA", new AtCommandHandler() {
1625b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1626b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleActionCommand() {
1627b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // Seems to be out of spec, but lets return nicely
1628b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return new AtCommandResult(AtCommandResult.OK);
1629b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1630b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1631b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleReadCommand() {
1632b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // Call waiting is always on
1633b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return new AtCommandResult("+CCWA: 1");
1634b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1635b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1636b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleSetCommand(Object[] args) {
1637b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // AT+CCWA=<n>
1638b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // Handsfree is trying to enable/disable call waiting. We
1639b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // cannot disable in the current implementation.
1640b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return new AtCommandResult(AtCommandResult.OK);
1641b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1642b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1643b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleTestCommand() {
1644b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // Request for range of supported CCWA paramters
1645b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return new AtCommandResult("+CCWA: (\"n\",(1))");
1646b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1647b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
1648b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1649b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Mobile Equipment Event Reporting enable/disable command
1650b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Of the full 3GPP syntax paramters (mode, keyp, disp, ind, bfr) we
1651b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // only support paramter ind (disable/enable evert reporting using
1652b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // +CDEV)
1653b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register("+CMER", new AtCommandHandler() {
1654b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1655b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleReadCommand() {
1656b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return new AtCommandResult(
1657b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        "+CMER: 3,0,0," + (mIndicatorsEnabled ? "1" : "0"));
1658b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1659b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1660b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleSetCommand(Object[] args) {
1661b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (args.length < 4) {
1662b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    // This is a syntax error
1663b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    return new AtCommandResult(AtCommandResult.ERROR);
1664b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                } else if (args[0].equals(3) && args[1].equals(0) &&
1665b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                           args[2].equals(0)) {
16660966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly                    boolean valid = false;
1667b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    if (args[3].equals(0)) {
1668b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        mIndicatorsEnabled = false;
16690966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly                        valid = true;
1670b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    } else if (args[3].equals(1)) {
1671b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        mIndicatorsEnabled = true;
16720966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly                        valid = true;
16730966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly                    }
16740966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly                    if (valid) {
16750966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly                        if ((mRemoteBrsf & BRSF_HF_CW_THREE_WAY_CALLING) == 0x0) {
16760966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly                            mServiceConnectionEstablished = true;
16770966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly                            sendURC("OK");  // send immediately, then initiate audio
16780966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly                            if (isIncallAudio()) {
16790966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly                                audioOn();
16800966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly                            }
16810966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly                            // only send OK once
16820966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly                            return new AtCommandResult(AtCommandResult.UNSOLICITED);
16830966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly                        } else {
16840966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly                            return new AtCommandResult(AtCommandResult.OK);
16850966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly                        }
1686b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    }
1687b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
16880966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly                return reportCmeError(BluetoothCmeError.OPERATION_NOT_SUPPORTED);
1689b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1690b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1691b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleTestCommand() {
1692b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return new AtCommandResult("+CMER: (3),(0),(0),(0-1)");
1693b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1694b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
1695b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1696b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Mobile Equipment Error Reporting enable/disable
1697b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register("+CMEE", new AtCommandHandler() {
1698b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1699b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleActionCommand() {
1700b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // out of spec, assume they want to enable
1701b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                mCmee = true;
1702b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return new AtCommandResult(AtCommandResult.OK);
1703b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1704b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1705b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleReadCommand() {
1706b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return new AtCommandResult("+CMEE: " + (mCmee ? "1" : "0"));
1707b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1708b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1709b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleSetCommand(Object[] args) {
1710b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // AT+CMEE=<n>
1711b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (args.length == 0) {
1712b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    // <n> ommitted - default to 0
1713b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    mCmee = false;
1714b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    return new AtCommandResult(AtCommandResult.OK);
1715b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                } else if (!(args[0] instanceof Integer)) {
1716b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    // Syntax error
1717b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    return new AtCommandResult(AtCommandResult.ERROR);
1718b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                } else {
1719b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    mCmee = ((Integer)args[0] == 1);
1720b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    return new AtCommandResult(AtCommandResult.OK);
1721b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
1722b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1723b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1724b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleTestCommand() {
1725b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // Probably not required but spec, but no harm done
1726b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return new AtCommandResult("+CMEE: (0-1)");
1727b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1728b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
1729b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1730b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Bluetooth Last Dialled Number
1731b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register("+BLDN", new AtCommandHandler() {
1732b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1733b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleActionCommand() {
1734b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return redial();
1735b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1736b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
1737b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1738b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Indicator Update command
1739b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register("+CIND", new AtCommandHandler() {
1740b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1741b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleReadCommand() {
17421dad0387bf2f280c12bdac172604e9779134fbcdJaikumar Ganesh                return mBluetoothPhoneState.toCindResult();
1743b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1744b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1745b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleTestCommand() {
17461dad0387bf2f280c12bdac172604e9779134fbcdJaikumar Ganesh                return mBluetoothPhoneState.getCindTestResult();
1747b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1748b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
1749b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1750b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Query Signal Quality (legacy)
1751b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register("+CSQ", new AtCommandHandler() {
1752b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1753b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleActionCommand() {
17541dad0387bf2f280c12bdac172604e9779134fbcdJaikumar Ganesh                return mBluetoothPhoneState.toCsqResult();
1755b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1756b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
1757b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1758b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Query network registration state
1759b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register("+CREG", new AtCommandHandler() {
1760b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1761b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleReadCommand() {
17621dad0387bf2f280c12bdac172604e9779134fbcdJaikumar Ganesh                return new AtCommandResult(mBluetoothPhoneState.toCregString());
1763b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1764b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
1765b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1766b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Send DTMF. I don't know if we are also expected to play the DTMF tone
1767b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // locally, right now we don't
1768b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register("+VTS", new AtCommandHandler() {
1769b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1770b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleSetCommand(Object[] args) {
1771b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (args.length >= 1) {
1772b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    char c;
1773b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    if (args[0] instanceof Integer) {
1774b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        c = ((Integer) args[0]).toString().charAt(0);
1775b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    } else {
1776b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        c = ((String) args[0]).charAt(0);
1777b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    }
1778b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    if (isValidDtmf(c)) {
17798058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                        phone.sendDtmf(c);
1780b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        return new AtCommandResult(AtCommandResult.OK);
1781b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    }
1782b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
1783b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return new AtCommandResult(AtCommandResult.ERROR);
1784b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1785b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            private boolean isValidDtmf(char c) {
1786b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                switch (c) {
1787b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                case '#':
1788b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                case '*':
1789b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    return true;
1790b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                default:
1791b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    if (Character.digit(c, 14) != -1) {
1792b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        return true;  // 0-9 and A-D
1793b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    }
1794b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    return false;
1795b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
1796b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1797b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
1798b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1799b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // List calls
1800b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register("+CLCC", new AtCommandHandler() {
1801b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1802b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleActionCommand() {
18038058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                int phoneType = phone.getPhoneType();
180479b9f6e30e8402b103f1ec3ed9dda1fcad2b1cd5Tammo Spalink                if (phoneType == Phone.PHONE_TYPE_CDMA) {
1805c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                    return cdmaGetClccResult();
180679b9f6e30e8402b103f1ec3ed9dda1fcad2b1cd5Tammo Spalink                } else if (phoneType == Phone.PHONE_TYPE_GSM) {
1807c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                    return gsmGetClccResult();
180879b9f6e30e8402b103f1ec3ed9dda1fcad2b1cd5Tammo Spalink                } else {
180979b9f6e30e8402b103f1ec3ed9dda1fcad2b1cd5Tammo Spalink                    throw new IllegalStateException("Unexpected phone type: " + phoneType);
1810c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                }
1811b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1812b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
1813b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1814b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Call Hold and Multiparty Handling command
1815b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register("+CHLD", new AtCommandHandler() {
1816b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1817b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleSetCommand(Object[] args) {
18188058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                int phoneType = phone.getPhoneType();
18198058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                Call ringingCall = mCM.getFirstActiveRingingCall();
18208058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                Call backgroundCall = mCM.getFirstActiveBgCall();
18218058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang
1822b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (args.length >= 1) {
1823b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    if (args[0].equals(0)) {
1824b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        boolean result;
18258058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                        if (ringingCall.isRinging()) {
18268058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                            result = PhoneUtils.hangupRingingCall(ringingCall);
1827b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        } else {
18288058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                            result = PhoneUtils.hangupHoldingCall(backgroundCall);
1829b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        }
1830b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        if (result) {
1831b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                            return new AtCommandResult(AtCommandResult.OK);
1832b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        } else {
1833b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                            return new AtCommandResult(AtCommandResult.ERROR);
1834b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        }
1835b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    } else if (args[0].equals(1)) {
183679b9f6e30e8402b103f1ec3ed9dda1fcad2b1cd5Tammo Spalink                        if (phoneType == Phone.PHONE_TYPE_CDMA) {
18378058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                            if (ringingCall.isRinging()) {
1838c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                                // If there is Call waiting then answer the call and
1839c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                                // put the first call on hold.
18408eb32ddb60cfe1a0d0733735c30ecfc814452806Jackson Fan                                if (VDBG) log("CHLD:1 Callwaiting Answer call");
18418058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                                PhoneUtils.answerCall(ringingCall);
1842a50e10e2efadac960987eaadc0938c6f92d3ee90John Wang                                PhoneUtils.setMute(false);
1843c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                                // Setting the second callers state flag to TRUE (i.e. active)
1844c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                                cdmaSetSecondCallState(true);
1845c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                            } else {
1846c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                                // If there is no Call waiting then just hangup
1847c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                                // the active call. In CDMA this mean that the complete
1848c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                                // call session would be ended
18498eb32ddb60cfe1a0d0733735c30ecfc814452806Jackson Fan                                if (VDBG) log("CHLD:1 Hangup Call");
18508343169cc89621d46dce86449f5ee1ff5d3a4919John Wang                                PhoneUtils.hangup(PhoneApp.getInstance().mCM);
1851c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                            }
1852b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                            return new AtCommandResult(AtCommandResult.OK);
185379b9f6e30e8402b103f1ec3ed9dda1fcad2b1cd5Tammo Spalink                        } else if (phoneType == Phone.PHONE_TYPE_GSM) {
1854a76113dcb0def438d0b3019aeb88fdbfbfbe90d4Paul Berman                            // Hangup active call, answer held call
18558343169cc89621d46dce86449f5ee1ff5d3a4919John Wang                            if (PhoneUtils.answerAndEndActive(
18568058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                                    PhoneApp.getInstance().mCM, ringingCall)) {
1857a76113dcb0def438d0b3019aeb88fdbfbfbe90d4Paul Berman                                return new AtCommandResult(AtCommandResult.OK);
1858a76113dcb0def438d0b3019aeb88fdbfbfbe90d4Paul Berman                            } else {
1859a76113dcb0def438d0b3019aeb88fdbfbfbe90d4Paul Berman                                return new AtCommandResult(AtCommandResult.ERROR);
1860a76113dcb0def438d0b3019aeb88fdbfbfbe90d4Paul Berman                            }
186179b9f6e30e8402b103f1ec3ed9dda1fcad2b1cd5Tammo Spalink                        } else {
186279b9f6e30e8402b103f1ec3ed9dda1fcad2b1cd5Tammo Spalink                            throw new IllegalStateException("Unexpected phone type: " + phoneType);
1863b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        }
1864b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    } else if (args[0].equals(2)) {
186579b9f6e30e8402b103f1ec3ed9dda1fcad2b1cd5Tammo Spalink                        if (phoneType == Phone.PHONE_TYPE_CDMA) {
1866a76113dcb0def438d0b3019aeb88fdbfbfbe90d4Paul Berman                            // For CDMA, the way we switch to a new incoming call is by
1867a76113dcb0def438d0b3019aeb88fdbfbfbe90d4Paul Berman                            // calling PhoneUtils.answerCall(). switchAndHoldActive() won't
1868a76113dcb0def438d0b3019aeb88fdbfbfbe90d4Paul Berman                            // properly update the call state within telephony.
1869c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                            // If the Phone state is already in CONF_CALL then we simply send
1870c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                            // a flash cmd by calling switchHoldingAndActive()
18718058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                            if (ringingCall.isRinging()) {
18728eb32ddb60cfe1a0d0733735c30ecfc814452806Jackson Fan                                if (VDBG) log("CHLD:2 Callwaiting Answer call");
18738058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                                PhoneUtils.answerCall(ringingCall);
1874a50e10e2efadac960987eaadc0938c6f92d3ee90John Wang                                PhoneUtils.setMute(false);
1875c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                                // Setting the second callers state flag to TRUE (i.e. active)
1876c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                                cdmaSetSecondCallState(true);
1877c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                            } else if (PhoneApp.getInstance().cdmaPhoneCallState
1878c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                                    .getCurrentCallState()
1879c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                                    == CdmaPhoneCallState.PhoneCallState.CONF_CALL) {
18808eb32ddb60cfe1a0d0733735c30ecfc814452806Jackson Fan                                if (VDBG) log("CHLD:2 Swap Calls");
18818058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                                PhoneUtils.switchHoldingAndActive(backgroundCall);
1882c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                                // Toggle the second callers active state flag
1883c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                                cdmaSwapSecondCallState();
1884c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                            }
188579b9f6e30e8402b103f1ec3ed9dda1fcad2b1cd5Tammo Spalink                        } else if (phoneType == Phone.PHONE_TYPE_GSM) {
18868058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                            PhoneUtils.switchHoldingAndActive(backgroundCall);
188779b9f6e30e8402b103f1ec3ed9dda1fcad2b1cd5Tammo Spalink                        } else {
188879b9f6e30e8402b103f1ec3ed9dda1fcad2b1cd5Tammo Spalink                            throw new IllegalStateException("Unexpected phone type: " + phoneType);
1889a76113dcb0def438d0b3019aeb88fdbfbfbe90d4Paul Berman                        }
1890b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        return new AtCommandResult(AtCommandResult.OK);
1891b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    } else if (args[0].equals(3)) {
189279b9f6e30e8402b103f1ec3ed9dda1fcad2b1cd5Tammo Spalink                        if (phoneType == Phone.PHONE_TYPE_CDMA) {
1893c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                            // For CDMA, we need to check if the call is in THRWAY_ACTIVE state
1894c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                            if (PhoneApp.getInstance().cdmaPhoneCallState.getCurrentCallState()
1895c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                                    == CdmaPhoneCallState.PhoneCallState.THRWAY_ACTIVE) {
18968eb32ddb60cfe1a0d0733735c30ecfc814452806Jackson Fan                                if (VDBG) log("CHLD:3 Merge Calls");
1897b045b9344f339170d134cf814357361d96c349cfHung-ying Tyan                                PhoneUtils.mergeCalls();
1898c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                            }
189979b9f6e30e8402b103f1ec3ed9dda1fcad2b1cd5Tammo Spalink                        } else if (phoneType == Phone.PHONE_TYPE_GSM) {
19008058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                            if (mCM.hasActiveFgCall() && mCM.hasActiveBgCall()) {
1901b045b9344f339170d134cf814357361d96c349cfHung-ying Tyan                                PhoneUtils.mergeCalls();
1902c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                            }
190379b9f6e30e8402b103f1ec3ed9dda1fcad2b1cd5Tammo Spalink                        } else {
190479b9f6e30e8402b103f1ec3ed9dda1fcad2b1cd5Tammo Spalink                            throw new IllegalStateException("Unexpected phone type: " + phoneType);
1905b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        }
1906b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        return new AtCommandResult(AtCommandResult.OK);
1907b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    }
1908b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
1909b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return new AtCommandResult(AtCommandResult.ERROR);
1910b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1911b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1912b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleTestCommand() {
19130966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly                mServiceConnectionEstablished = true;
1914d2b5e78809bc2532b2377ba2c351d48710097c0cNick Pelly                sendURC("+CHLD: (0,1,2,3)");
1915d2b5e78809bc2532b2377ba2c351d48710097c0cNick Pelly                sendURC("OK");  // send reply first, then connect audio
19160966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly                if (isIncallAudio()) {
19170966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly                    audioOn();
19180966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly                }
19190966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly                // already replied
19200966f42d45611b961635c8d23fc4a4df0f5e29c4Nick Pelly                return new AtCommandResult(AtCommandResult.UNSOLICITED);
1921b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1922b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
1923b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1924b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Get Network operator name
1925b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register("+COPS", new AtCommandHandler() {
1926b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1927b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleReadCommand() {
19288058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                String operatorName = phone.getServiceState().getOperatorAlphaLong();
1929b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (operatorName != null) {
1930b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    if (operatorName.length() > 16) {
1931b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        operatorName = operatorName.substring(0, 16);
1932b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    }
1933b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    return new AtCommandResult(
1934b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                            "+COPS: 0,0,\"" + operatorName + "\"");
1935b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                } else {
1936b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    return new AtCommandResult(
1937b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                            "+COPS: 0,0,\"UNKNOWN\",0");
1938b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
1939b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1940b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1941b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleSetCommand(Object[] args) {
1942b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // Handsfree only supports AT+COPS=3,0
1943b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (args.length != 2 || !(args[0] instanceof Integer)
1944b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    || !(args[1] instanceof Integer)) {
1945b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    // syntax error
1946b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    return new AtCommandResult(AtCommandResult.ERROR);
1947b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                } else if ((Integer)args[0] != 3 || (Integer)args[1] != 0) {
1948b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    return reportCmeError(BluetoothCmeError.OPERATION_NOT_SUPPORTED);
1949b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                } else {
1950b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    return new AtCommandResult(AtCommandResult.OK);
1951b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
1952b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1953b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1954b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleTestCommand() {
1955b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // Out of spec, but lets be friendly
1956b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return new AtCommandResult("+COPS: (3),(0)");
1957b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1958b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
1959b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1960b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Mobile PIN
1961b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // AT+CPIN is not in the handsfree spec (although it is in 3GPP)
1962b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register("+CPIN", new AtCommandHandler() {
1963b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1964b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleReadCommand() {
1965b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return new AtCommandResult("+CPIN: READY");
1966b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1967b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
1968b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1969b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Bluetooth Response and Hold
1970b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Only supported on PDC (Japan) and CDMA networks.
1971b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register("+BTRH", new AtCommandHandler() {
1972b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1973b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleReadCommand() {
1974b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // Replying with just OK indicates no response and hold
1975b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // features in use now
1976b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return new AtCommandResult(AtCommandResult.OK);
1977b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1978b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1979b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleSetCommand(Object[] args) {
1980b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // Neeed PDC or CDMA
1981b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return new AtCommandResult(AtCommandResult.ERROR);
1982b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1983b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
1984b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
1985b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Request International Mobile Subscriber Identity (IMSI)
1986b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Not in bluetooth handset spec
1987b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register("+CIMI", new AtCommandHandler() {
1988b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
1989b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleActionCommand() {
1990b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // AT+CIMI
19918058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                String imsi = phone.getSubscriberId();
1992b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (imsi == null || imsi.length() == 0) {
1993b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    return reportCmeError(BluetoothCmeError.SIM_FAILURE);
1994b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                } else {
1995b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    return new AtCommandResult(imsi);
1996b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
1997b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
1998b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
1999b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2000b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Calling Line Identification Presentation
2001b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register("+CLIP", new AtCommandHandler() {
2002b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
2003b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleReadCommand() {
2004b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // Currently assumes the network is provisioned for CLIP
2005b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return new AtCommandResult("+CLIP: " + (mClip ? "1" : "0") + ",1");
2006b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
2007b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
2008b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleSetCommand(Object[] args) {
2009b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // AT+CLIP=<n>
2010b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (args.length >= 1 && (args[0].equals(0) || args[0].equals(1))) {
2011b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    mClip = args[0].equals(1);
2012b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    return new AtCommandResult(AtCommandResult.OK);
2013b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                } else {
2014b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    return new AtCommandResult(AtCommandResult.ERROR);
2015b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
2016b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
2017b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
2018b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleTestCommand() {
2019b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return new AtCommandResult("+CLIP: (0-1)");
2020b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
2021b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
2022b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2023b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // AT+CGSN - Returns the device IMEI number.
2024b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register("+CGSN", new AtCommandHandler() {
2025b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
2026b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleActionCommand() {
2027b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // Get the IMEI of the device.
20288058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                // phone will not be NULL at this point.
20298058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                return new AtCommandResult("+CGSN: " + phone.getDeviceId());
2030b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
2031b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
2032b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2033b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // AT+CGMM - Query Model Information
2034b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register("+CGMM", new AtCommandHandler() {
2035b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
2036b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleActionCommand() {
2037b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // Return the Model Information.
2038b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                String model = SystemProperties.get("ro.product.model");
2039b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (model != null) {
2040b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    return new AtCommandResult("+CGMM: " + model);
2041b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                } else {
2042b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    return new AtCommandResult(AtCommandResult.ERROR);
2043b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
2044b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
2045b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
2046b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2047b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // AT+CGMI - Query Manufacturer Information
2048b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register("+CGMI", new AtCommandHandler() {
2049b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
2050b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleActionCommand() {
2051b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // Return the Model Information.
2052b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                String manuf = SystemProperties.get("ro.product.manufacturer");
2053b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (manuf != null) {
2054b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    return new AtCommandResult("+CGMI: " + manuf);
2055b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                } else {
2056b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    return new AtCommandResult(AtCommandResult.ERROR);
2057b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
2058b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
2059b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
2060b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2061b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Noise Reduction and Echo Cancellation control
2062b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register("+NREC", new AtCommandHandler() {
2063b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
2064b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleSetCommand(Object[] args) {
2065b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (args[0].equals(0)) {
2066aa23e1c3c758bad23d8b6709147cc1ff7cd1e43cEric Laurent                    mAudioManager.setParameters(HEADSET_NREC+"=off");
2067b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    return new AtCommandResult(AtCommandResult.OK);
2068b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                } else if (args[0].equals(1)) {
2069aa23e1c3c758bad23d8b6709147cc1ff7cd1e43cEric Laurent                    mAudioManager.setParameters(HEADSET_NREC+"=on");
2070b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    return new AtCommandResult(AtCommandResult.OK);
2071b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
2072b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return new AtCommandResult(AtCommandResult.ERROR);
2073b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
2074b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
2075b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2076b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Voice recognition (dialing)
2077b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register("+BVRA", new AtCommandHandler() {
2078b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
2079b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleSetCommand(Object[] args) {
2080b8dbab241df3aa3487c1bdb488fd4e0b694d2d9aEric Laurent                if (!BluetoothHeadset.isBluetoothVoiceDialingEnabled(mContext)) {
20816967e2d953bc077c99c4831946201f3d333b833fNick Pelly                    return new AtCommandResult(AtCommandResult.ERROR);
20826967e2d953bc077c99c4831946201f3d333b833fNick Pelly                }
2083b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (args.length >= 1 && args[0].equals(1)) {
2084b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    synchronized (BluetoothHandsfree.this) {
2085b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        if (!mWaitingForVoiceRecognition) {
2086b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                            try {
2087b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                                mContext.startActivity(sVoiceCommandIntent);
2088b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                            } catch (ActivityNotFoundException e) {
2089b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                                return new AtCommandResult(AtCommandResult.ERROR);
2090b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                            }
2091b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                            expectVoiceRecognition();
2092b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        }
2093b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    }
2094b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    return new AtCommandResult(AtCommandResult.UNSOLICITED);  // send nothing yet
2095b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                } else if (args.length >= 1 && args[0].equals(0)) {
2096b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    audioOff();
2097b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    return new AtCommandResult(AtCommandResult.OK);
2098b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
2099b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return new AtCommandResult(AtCommandResult.ERROR);
2100b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
2101b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
2102b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleTestCommand() {
21036967e2d953bc077c99c4831946201f3d333b833fNick Pelly                return new AtCommandResult("+BVRA: (0-1)");
2104b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
2105b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
2106b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2107b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Retrieve Subscriber Number
2108b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register("+CNUM", new AtCommandHandler() {
2109b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
2110b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleActionCommand() {
21118058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                String number = phone.getLine1Number();
2112b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (number == null) {
2113b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    return new AtCommandResult(AtCommandResult.OK);
2114b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
2115b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return new AtCommandResult("+CNUM: ,\"" + number + "\"," +
2116b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        PhoneNumberUtils.toaFromString(number) + ",,4");
2117b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
2118b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
2119b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2120b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Microphone Gain
2121b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register("+VGM", new AtCommandHandler() {
2122b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
2123b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleSetCommand(Object[] args) {
2124b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // AT+VGM=<gain>    in range [0,15]
2125b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // Headset/Handsfree is reporting its current gain setting
2126b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return new AtCommandResult(AtCommandResult.OK);
2127b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
2128b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
2129b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2130b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Speaker Gain
2131b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register("+VGS", new AtCommandHandler() {
2132b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
2133b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleSetCommand(Object[] args) {
2134b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                // AT+VGS=<gain>    in range [0,15]
2135b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (args.length != 1 || !(args[0] instanceof Integer)) {
2136b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    return new AtCommandResult(AtCommandResult.ERROR);
2137b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
2138b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                mScoGain = (Integer) args[0];
2139b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                int flag =  mAudioManager.isBluetoothScoOn() ? AudioManager.FLAG_SHOW_UI:0;
2140b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2141b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                mAudioManager.setStreamVolume(AudioManager.STREAM_BLUETOOTH_SCO, mScoGain, flag);
2142b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return new AtCommandResult(AtCommandResult.OK);
2143b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
2144b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
2145b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2146b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        // Phone activity status
2147b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        parser.register("+CPAS", new AtCommandHandler() {
2148b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            @Override
2149b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            public AtCommandResult handleActionCommand() {
2150b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                int status = 0;
21518058bbc4c7d0d56e133bd2fb9dfc559004e4d61aJohn Wang                switch (mCM.getState()) {
2152b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                case IDLE:
2153b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    status = 0;
2154b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    break;
2155b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                case RINGING:
2156b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    status = 3;
2157b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    break;
2158b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                case OFFHOOK:
2159b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    status = 4;
2160b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    break;
2161b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
2162b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                return new AtCommandResult("+CPAS: " + status);
2163b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
2164b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        });
2165b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mPhonebook.register(parser);
2166b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
2167b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2168b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    public void sendScoGainUpdate(int gain) {
2169b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (mScoGain != gain && (mRemoteBrsf & BRSF_HF_REMOTE_VOL_CONTROL) != 0x0) {
2170b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            sendURC("+VGS:" + gain);
2171b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mScoGain = gain;
2172b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
2173b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
2174b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2175b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    public AtCommandResult reportCmeError(int error) {
2176b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (mCmee) {
2177b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            AtCommandResult result = new AtCommandResult(AtCommandResult.UNSOLICITED);
2178b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            result.addResponse("+CME ERROR: " + error);
2179b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            return result;
2180b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        } else {
2181b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            return new AtCommandResult(AtCommandResult.ERROR);
2182b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
2183b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
2184b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2185b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private static final int START_CALL_TIMEOUT = 10000;  // ms
2186b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2187b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private synchronized void expectCallStart() {
2188b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mWaitingForCallStart = true;
2189b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        Message msg = Message.obtain(mHandler, CHECK_CALL_STARTED);
2190b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mHandler.sendMessageDelayed(msg, START_CALL_TIMEOUT);
2191b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (!mStartCallWakeLock.isHeld()) {
2192b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mStartCallWakeLock.acquire(START_CALL_TIMEOUT);
2193b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
2194b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
2195b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2196b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private synchronized void callStarted() {
2197b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (mWaitingForCallStart) {
2198b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mWaitingForCallStart = false;
2199b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            sendURC("OK");
2200b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            if (mStartCallWakeLock.isHeld()) {
2201b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                mStartCallWakeLock.release();
2202b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
2203b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
2204b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
2205b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2206b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private static final int START_VOICE_RECOGNITION_TIMEOUT = 5000;  // ms
2207b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2208b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private synchronized void expectVoiceRecognition() {
2209b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mWaitingForVoiceRecognition = true;
2210b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        Message msg = Message.obtain(mHandler, CHECK_VOICE_RECOGNITION_STARTED);
2211b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        mHandler.sendMessageDelayed(msg, START_VOICE_RECOGNITION_TIMEOUT);
2212b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (!mStartVoiceRecognitionWakeLock.isHeld()) {
2213b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mStartVoiceRecognitionWakeLock.acquire(START_VOICE_RECOGNITION_TIMEOUT);
2214b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
2215b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
2216b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2217b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /* package */ synchronized boolean startVoiceRecognition() {
2218b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (mWaitingForVoiceRecognition) {
2219b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // HF initiated
2220b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mWaitingForVoiceRecognition = false;
2221b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            sendURC("OK");
2222b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        } else {
2223b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            // AG initiated
2224b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            sendURC("+BVRA: 1");
2225b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
2226b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        boolean ret = audioOn();
2227b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (mStartVoiceRecognitionWakeLock.isHeld()) {
2228b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mStartVoiceRecognitionWakeLock.release();
2229b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
2230b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return ret;
2231b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
2232b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2233b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /* package */ synchronized boolean stopVoiceRecognition() {
2234b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        sendURC("+BVRA: 0");
2235b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        audioOff();
2236b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return true;
2237b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
2238b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2239b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private boolean inDebug() {
2240b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return DBG && SystemProperties.getBoolean(DebugThread.DEBUG_HANDSFREE, false);
2241b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
2242b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2243b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private boolean allowAudioAnytime() {
2244b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        return inDebug() && SystemProperties.getBoolean(DebugThread.DEBUG_HANDSFREE_AUDIO_ANYTIME,
2245b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                false);
2246b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
2247b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2248b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private void startDebug() {
2249b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (DBG && mDebugThread == null) {
2250b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mDebugThread = new DebugThread();
2251b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mDebugThread.start();
2252b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
2253b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
2254b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2255b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private void stopDebug() {
2256b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        if (mDebugThread != null) {
2257b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mDebugThread.interrupt();
2258b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            mDebugThread = null;
2259b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
2260b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
2261b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2262b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    /** Debug thread to read debug properties - runs when debug.bt.hfp is true
2263b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *  at the time a bluetooth handsfree device is connected. Debug properties
2264b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project     *  are polled and mock updates sent every 1 second */
2265b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private class DebugThread extends Thread {
2266b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        /** Turns on/off handsfree profile debugging mode */
2267b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private static final String DEBUG_HANDSFREE = "debug.bt.hfp";
2268b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2269b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        /** Mock battery level change - use 0 to 5 */
2270b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private static final String DEBUG_HANDSFREE_BATTERY = "debug.bt.hfp.battery";
2271b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2272b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        /** Mock no cellular service when false */
2273b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private static final String DEBUG_HANDSFREE_SERVICE = "debug.bt.hfp.service";
2274b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2275b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        /** Mock cellular roaming when true */
2276b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private static final String DEBUG_HANDSFREE_ROAM = "debug.bt.hfp.roam";
2277b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2278b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        /** false to true transition will force an audio (SCO) connection to
2279b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project         *  be established. true to false will force audio to be disconnected
2280b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project         */
2281b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private static final String DEBUG_HANDSFREE_AUDIO = "debug.bt.hfp.audio";
2282b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2283b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        /** true allows incoming SCO connection out of call.
2284b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project         */
2285b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private static final String DEBUG_HANDSFREE_AUDIO_ANYTIME = "debug.bt.hfp.audio_anytime";
2286b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2287b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        /** Mock signal strength change in ASU - use 0 to 31 */
2288b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private static final String DEBUG_HANDSFREE_SIGNAL = "debug.bt.hfp.signal";
2289b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2290b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        /** Debug AT+CLCC: print +CLCC result */
2291b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private static final String DEBUG_HANDSFREE_CLCC = "debug.bt.hfp.clcc";
2292b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2293b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        /** Debug AT+BSIR - Send In Band Ringtones Unsolicited AT command.
2294b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project         * debug.bt.unsol.inband = 0 => AT+BSIR = 0 sent by the AG
2295b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project         * debug.bt.unsol.inband = 1 => AT+BSIR = 0 sent by the AG
2296b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project         * Other values are ignored.
2297b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project         */
2298b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2299b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        private static final String DEBUG_UNSOL_INBAND_RINGTONE =
2300b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            "debug.bt.unsol.inband";
2301b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2302b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        @Override
2303b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        public void run() {
2304b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            boolean oldService = true;
2305b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            boolean oldRoam = false;
2306b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            boolean oldAudio = false;
2307b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2308b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            while (!isInterrupted() && inDebug()) {
2309b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                int batteryLevel = SystemProperties.getInt(DEBUG_HANDSFREE_BATTERY, -1);
2310b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (batteryLevel >= 0 && batteryLevel <= 5) {
2311b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    Intent intent = new Intent();
2312b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    intent.putExtra("level", batteryLevel);
2313b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    intent.putExtra("scale", 5);
23141dad0387bf2f280c12bdac172604e9779134fbcdJaikumar Ganesh                    mBluetoothPhoneState.updateBatteryState(intent);
2315b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
2316b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2317b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                boolean serviceStateChanged = false;
2318b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (SystemProperties.getBoolean(DEBUG_HANDSFREE_SERVICE, true) != oldService) {
2319b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    oldService = !oldService;
2320b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    serviceStateChanged = true;
2321b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
2322b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (SystemProperties.getBoolean(DEBUG_HANDSFREE_ROAM, false) != oldRoam) {
2323b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    oldRoam = !oldRoam;
2324b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    serviceStateChanged = true;
2325b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
2326b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (serviceStateChanged) {
2327b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    Bundle b = new Bundle();
2328b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    b.putInt("state", oldService ? 0 : 1);
2329b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    b.putBoolean("roaming", oldRoam);
23301dad0387bf2f280c12bdac172604e9779134fbcdJaikumar Ganesh                    mBluetoothPhoneState.updateServiceState(true, ServiceState.newFromBundle(b));
2331b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
2332b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2333b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (SystemProperties.getBoolean(DEBUG_HANDSFREE_AUDIO, false) != oldAudio) {
2334b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    oldAudio = !oldAudio;
2335b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    if (oldAudio) {
2336b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        audioOn();
2337b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    } else {
2338b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        audioOff();
2339b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    }
2340b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
2341b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2342b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                int signalLevel = SystemProperties.getInt(DEBUG_HANDSFREE_SIGNAL, -1);
2343b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (signalLevel >= 0 && signalLevel <= 31) {
2344404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville                    SignalStrength signalStrength = new SignalStrength(signalLevel, -1, -1, -1,
2345404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville                            -1, -1, -1, true);
2346b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    Intent intent = new Intent();
2347404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville                    Bundle data = new Bundle();
2348404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville                    signalStrength.fillInNotifierBundle(data);
2349404edc94de563aef5fd5ba48be9114a970cb93bbWink Saville                    intent.putExtras(data);
23501dad0387bf2f280c12bdac172604e9779134fbcdJaikumar Ganesh                    mBluetoothPhoneState.updateSignalState(intent);
2351b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
2352b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2353b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (SystemProperties.getBoolean(DEBUG_HANDSFREE_CLCC, false)) {
2354c9d9ed30aa547b79b81adc13a4d148a003b6ee62w                    log(gsmGetClccResult().toString());
2355b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
2356b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                try {
2357b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    sleep(1000);  // 1 second
2358b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                } catch (InterruptedException e) {
2359b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    break;
2360b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
2361b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2362b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                int inBandRing =
2363b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    SystemProperties.getInt(DEBUG_UNSOL_INBAND_RINGTONE, -1);
2364b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                if (inBandRing == 0 || inBandRing == 1) {
2365b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    AtCommandResult result =
2366b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                        new AtCommandResult(AtCommandResult.UNSOLICITED);
2367b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    result.addResponse("+BSIR: " + inBandRing);
2368b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                    sendURC(result.toString());
2369b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project                }
2370b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project            }
2371b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        }
2372b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
2373b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project
2374c9d9ed30aa547b79b81adc13a4d148a003b6ee62w    public void cdmaSwapSecondCallState() {
23758eb32ddb60cfe1a0d0733735c30ecfc814452806Jackson Fan        if (VDBG) log("cdmaSetSecondCallState: Toggling mCdmaIsSecondCallActive");
2376c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        mCdmaIsSecondCallActive = !mCdmaIsSecondCallActive;
2377c9d9ed30aa547b79b81adc13a4d148a003b6ee62w    }
2378c9d9ed30aa547b79b81adc13a4d148a003b6ee62w
2379c9d9ed30aa547b79b81adc13a4d148a003b6ee62w    public void cdmaSetSecondCallState(boolean state) {
23808eb32ddb60cfe1a0d0733735c30ecfc814452806Jackson Fan        if (VDBG) log("cdmaSetSecondCallState: Setting mCdmaIsSecondCallActive to " + state);
2381c9d9ed30aa547b79b81adc13a4d148a003b6ee62w        mCdmaIsSecondCallActive = state;
2382c9d9ed30aa547b79b81adc13a4d148a003b6ee62w    }
2383c9d9ed30aa547b79b81adc13a4d148a003b6ee62w
2384b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    private static void log(String msg) {
2385b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project        Log.d(TAG, msg);
2386b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project    }
2387b16363f5fc191b769e88c364243e34b92eb22688The Android Open Source Project}
2388