Ensure BT headset state updation before connect.

Two BT fixes here:
1. Correct the force updation of BT headset.
  Looks like one of the partner CL: ag/703545 inadvertently overrode the
  |force| flag that was added in ag/565413 to update the BT headset with
  correct phone state on query.  Correcting the logic to update the BT
  headset with phone state to honor the |force| flag regardless of the
  other flags.
2. For outgoing calls, update the BT headset that there is an outgoing
   call being placed even before the call reaches the DIALING state.
   This helps in ensuring that the connect_audio invoked reaches after
   BT headset knows that there is an outgoing call being placed.

BUG: 23324160
Bug: 22996930
Bug: 23595828
Change-Id: Ib19712fbf357b8355de2132be643585e24d9a59c
diff --git a/src/com/android/server/telecom/BluetoothPhoneServiceImpl.java b/src/com/android/server/telecom/BluetoothPhoneServiceImpl.java
index 1fc7a6c..9deffbe 100644
--- a/src/com/android/server/telecom/BluetoothPhoneServiceImpl.java
+++ b/src/com/android/server/telecom/BluetoothPhoneServiceImpl.java
@@ -642,7 +642,10 @@
 
         int numActiveCalls = activeCall == null ? 0 : 1;
         int numHeldCalls = mCallsManager.getNumHeldCalls();
-        boolean callsSwitched = (numHeldCalls == 2);
+        // Intermediate state for GSM calls which are in the process of being swapped.
+        // TODO: Should we be hardcoding this value to 2 or should we check if all top level calls
+        //       are held?
+        boolean callsPendingSwitch = (numHeldCalls == 2);
 
         // For conference calls which support swapping the active call within the conference
         // (namely CDMA calls) we need to expose that as a held call in order for the BT device
@@ -670,13 +673,14 @@
         }
 
         if (mBluetoothHeadset != null &&
-                (numActiveCalls != mNumActiveCalls ||
-                 numHeldCalls != mNumHeldCalls ||
-                 bluetoothCallState != mBluetoothCallState ||
-                 !TextUtils.equals(ringingAddress, mRingingAddress) ||
-                 ringingAddressType != mRingingAddressType ||
-                 (heldCall != mOldHeldCall && !ignoreHeldCallChange) ||
-                 force) && !callsSwitched) {
+                (force ||
+                        (!callsPendingSwitch &&
+                                (numActiveCalls != mNumActiveCalls ||
+                                 numHeldCalls != mNumHeldCalls ||
+                                 bluetoothCallState != mBluetoothCallState ||
+                                 !TextUtils.equals(ringingAddress, mRingingAddress) ||
+                                 ringingAddressType != mRingingAddressType ||
+                                 (heldCall != mOldHeldCall && !ignoreHeldCallChange))))) {
 
             // If the call is transitioning into the alerting state, send DIALING first.
             // Some devices expect to see a DIALING state prior to seeing an ALERTING state
@@ -738,7 +742,7 @@
     private int getBluetoothCallStateForUpdate() {
         CallsManager callsManager = mCallsManager;
         Call ringingCall = mCallsManager.getRingingCall();
-        Call dialingCall = mCallsManager.getDialingCall();
+        Call dialingCall = mCallsManager.getOutgoingCall();
 
         //
         // !! WARNING !!
diff --git a/src/com/android/server/telecom/CallsManager.java b/src/com/android/server/telecom/CallsManager.java
index 8ec5202..498fd86 100644
--- a/src/com/android/server/telecom/CallsManager.java
+++ b/src/com/android/server/telecom/CallsManager.java
@@ -1131,6 +1131,10 @@
         return count;
     }
 
+    Call getOutgoingCall() {
+        return getFirstCallWithState(OUTGOING_CALL_STATES);
+    }
+
     Call getFirstCallWithState(int... states) {
         return getFirstCallWithState(null, states);
     }