Fix issue where incoming call ringing persiste on BT headset.

b/358249447 fixed some issues with the separate binding to BT ICS code
which meant we'd always unbind from BT before sending the onCallRemoved
update.

However, that uncovered a latent issue in the original code.  In
CallAudioManager we decide if a disconnect tone should be played or not;
if no disconnect tone is played, then the BT ICS unbind future will never
get the "tone finished" callback and the 5 sec timeout on that future
will end up being hit to cause the state update to the BT ICS.  We never
noticed this in the past because the state update always happened AFTER
Telecom unbinded from BT ICS, so it was a no-op.  Now that we're sending
the state correctly before unbinding, this is indeed an issue.

To remedy this, CallAudioManager will complete the disconnect tone future
immediately for disconnected calls when no tone is played.

Flag: com.android.server.telecom.flags.separately_bind_to_bt_incall_service
Fixes: 368495329
Test: Verify disconnect tone playback remains on BT headset and unbind
takes place.
Test: Verify incoming call state update to BT occurs immediately with
no delay.

Change-Id: Iada5e5b08e2885ff4d8ee3781760f84d2d58e2fb
diff --git a/src/com/android/server/telecom/CallAudioManager.java b/src/com/android/server/telecom/CallAudioManager.java
index fafd87f..8c2f631 100644
--- a/src/com/android/server/telecom/CallAudioManager.java
+++ b/src/com/android/server/telecom/CallAudioManager.java
@@ -144,6 +144,12 @@
         updateForegroundCall();
         if (shouldPlayDisconnectTone(oldState, newState)) {
             playToneForDisconnectedCall(call);
+        } else {
+            if (newState == CallState.DISCONNECTED) {
+                // This call is not disconnected, but it won't generate a disconnect tone, so
+                // complete the future to ensure we unbind from BT promptly.
+                completeDisconnectToneFuture(call);
+            }
         }
 
         onCallLeavingState(call, oldState);
@@ -1089,6 +1095,10 @@
         CompletableFuture<Void> disconnectedToneFuture = mCallsManager.getInCallController()
                 .getDisconnectedToneBtFutures().get(call.getId());
         if (disconnectedToneFuture != null) {
+            Log.i(this,
+                    "completeDisconnectToneFuture: completing deferred disconnect tone future for"
+                            + " call %s",
+                    call.getId());
             disconnectedToneFuture.complete(null);
         }
     }
diff --git a/tests/src/com/android/server/telecom/tests/CallAudioManagerTest.java b/tests/src/com/android/server/telecom/tests/CallAudioManagerTest.java
index 1d641ba..d1a3eb6 100644
--- a/tests/src/com/android/server/telecom/tests/CallAudioManagerTest.java
+++ b/tests/src/com/android/server/telecom/tests/CallAudioManagerTest.java
@@ -53,6 +53,7 @@
 import com.android.server.telecom.CallState;
 import com.android.server.telecom.CallsManager;
 import com.android.server.telecom.DtmfLocalTonePlayer;
+import com.android.server.telecom.InCallController;
 import com.android.server.telecom.InCallTonePlayer;
 import com.android.server.telecom.RingbackPlayer;
 import com.android.server.telecom.Ringer;
@@ -77,6 +78,7 @@
 @RunWith(JUnit4.class)
 public class CallAudioManagerTest extends TelecomTestCase {
     @Mock private CallAudioRouteStateMachine mCallAudioRouteStateMachine;
+    @Mock private InCallController mInCallController;
     @Mock private CallsManager mCallsManager;
     @Mock private CallAudioModeStateMachine mCallAudioModeStateMachine;
     @Mock private InCallTonePlayer.Factory mPlayerFactory;
@@ -103,6 +105,8 @@
             return mockInCallTonePlayer;
         }).when(mPlayerFactory).createPlayer(any(Call.class), anyInt());
         when(mCallsManager.getLock()).thenReturn(mLock);
+        when(mCallsManager.getInCallController()).thenReturn(mInCallController);
+        when(mInCallController.getBtBindingFuture(any(Call.class))).thenReturn(null);
         when(mFlags.ensureAudioModeUpdatesOnForegroundCallChange()).thenReturn(true);
         mCallAudioManager = new CallAudioManager(
                 mCallAudioRouteStateMachine,