Merge remote-tracking branch 'caf/LA.BF64.1.2.2_rb4.7' into c6
diff --git a/src/com/android/server/telecom/BluetoothPhoneServiceImpl.java b/src/com/android/server/telecom/BluetoothPhoneServiceImpl.java
index eda75ac..678ad94 100644
--- a/src/com/android/server/telecom/BluetoothPhoneServiceImpl.java
+++ b/src/com/android/server/telecom/BluetoothPhoneServiceImpl.java
@@ -1183,7 +1183,7 @@
         int bluetoothCallState = CALL_STATE_IDLE;
         if (ringingCall != null) {
             bluetoothCallState = CALL_STATE_INCOMING;
-        } else if (dialingCall != null) {
+        } else if (dialingCall != null && dialingCall.getState() == CallState.DIALING) {
             bluetoothCallState = CALL_STATE_ALERTING;
         }
         return bluetoothCallState;
diff --git a/src/com/android/server/telecom/CallsManager.java b/src/com/android/server/telecom/CallsManager.java
index d027086..cbdc143 100644
--- a/src/com/android/server/telecom/CallsManager.java
+++ b/src/com/android/server/telecom/CallsManager.java
@@ -1601,6 +1601,11 @@
                     break;
                 }
 
+                // If only call in call list is held call it's also a foreground call
+                if (mCalls.size() == 1 && call.getState() == CallState.ON_HOLD) {
+                    newForegroundCall = call;
+                }
+
                 if ((call.isAlive() && call.getState() != CallState.ON_HOLD)
                      || call.getState() == CallState.RINGING) {
                     newForegroundCall = call;
@@ -1629,6 +1634,11 @@
                     break;
                 }
 
+                // If only call in call list is held call it's also a foreground call
+                if (mCalls.size() == 1 && call.getState() == CallState.ON_HOLD) {
+                    newForegroundCall = call;
+                }
+
                 if ((call.isAlive() && call.getState() != CallState.ON_HOLD)
                      || call.getState() == CallState.RINGING) {
                     newForegroundCall = call;
@@ -1637,6 +1647,16 @@
             }
         }
 
+        /* In the case where there are 2 calls, when the Hold button is pressed for active
+         * call, as an intermediate state we would have both calls in held state before
+         * the background call is moved to active state. In such an intermediate stage
+         * updating the newForegroundCall to null causes issues with abandoning audio
+         * focus. Skip updating the foreground call with null in such cases. */
+        if (newForegroundCall == null && getFirstCallWithState(CallState.ON_HOLD) != null) {
+            Log.v(this, "Skip updating foreground call in intermediate stage");
+            newForegroundCall = mForegroundCall;
+        }
+
         if (newForegroundCall != mForegroundCall) {
             Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
             Call oldForegroundCall = mForegroundCall;