Fix regression for answering a ringing call when there are 2 calls already.

In the case where there is already a held and active call and a new
incoming call is being answered, previous logic was to disconnect the
held call and then hold the previous foreground call before answering
the incoming call.

This CL fixes a regression in that functionality.

Test: Manual
Bug: 74939079
Change-Id: I150fa51cd0e67d5e39e9cf09268c50ba66d570c2
diff --git a/src/com/android/server/telecom/CallsManager.java b/src/com/android/server/telecom/CallsManager.java
index 64da6ac..7463194 100644
--- a/src/com/android/server/telecom/CallsManager.java
+++ b/src/com/android/server/telecom/CallsManager.java
@@ -1460,8 +1460,21 @@
             Call activeCall = (Call) mConnectionSvrFocusMgr.getCurrentFocusCall();
             Log.d(this, "Incoming call = %s Ongoing call %s", call, activeCall);
             if (activeCall != null && activeCall != call) {
-                // Hold the telephony call even if it doesn't have the hold capability.
-                if (canHold(activeCall)) {
+                // We purposely don't check if the active call CAN current hold, but rather we check
+                // whether it CAN support hold.  Consider this scenario:
+                // Call A - Active (CAPABILITY_SUPPORT_HOLD, but not CAPABILITY_HOLD)
+                // Call B - Held (CAPABILITY_SUPPORT_HOLD, but not CAPABILITY_HOLD)
+                // Call C - Incoming call
+                // In this scenario we are going to first disconnect the held call (Call B), which
+                // will mean that the active call (Call A) will now support hold.
+                if (supportsHold(activeCall)) {
+                    Call heldCall = getHeldCall();
+                    if (heldCall != null) {
+                        Log.i(this, "Disconnecting held call %s before holding active call.",
+                                heldCall);
+                        heldCall.disconnect();
+                    }
+
                     Log.d(this, "Answer %s, hold %s", call, activeCall);
                     activeCall.hold();
                 } else {
@@ -3733,6 +3746,10 @@
         return call.can(Connection.CAPABILITY_HOLD);
     }
 
+    private boolean supportsHold(Call call) {
+        return call.can(Connection.CAPABILITY_SUPPORT_HOLD);
+    }
+
     private final class ActionSetCallState implements PendingAction {
 
         private final Call mCall;