Fix BT call state issue

CallAudioManager sets the BT end-of-call tone flag to true after it
calls startTone at the end of a call, no matter whether the call
actually started or not. This causes our call reports to the BT stack to
perpetually be in the disconnecting stack, which apparently is bad.

Fix this so that we only set the flag if the tone actually plays.

Test: unit, manual
Change-Id: I72058667ee6d722fbe5bf87f036a95220cf0a34f
Fixes: 120477414
(cherry picked from commit ee99e09fb328f08d017cd848f0966ee701d3d74b)
diff --git a/src/com/android/server/telecom/CallAudioManager.java b/src/com/android/server/telecom/CallAudioManager.java
index a8f2bc8..b68a851 100644
--- a/src/com/android/server/telecom/CallAudioManager.java
+++ b/src/com/android/server/telecom/CallAudioManager.java
@@ -727,9 +727,11 @@
             Log.d(this, "Found a disconnected call with tone to play %d.", toneToPlay);
 
             if (toneToPlay != InCallTonePlayer.TONE_INVALID) {
-                mPlayerFactory.createPlayer(toneToPlay).startTone();
-                mCallsManager.onDisconnectedTonePlaying(true);
-                mIsDisconnectedTonePlaying = true;
+                boolean didToneStart = mPlayerFactory.createPlayer(toneToPlay).startTone();
+                if (didToneStart) {
+                    mCallsManager.onDisconnectedTonePlaying(true);
+                    mIsDisconnectedTonePlaying = true;
+                }
             }
         }
     }
diff --git a/src/com/android/server/telecom/InCallTonePlayer.java b/src/com/android/server/telecom/InCallTonePlayer.java
index 0cc7ad2..98d5bba 100644
--- a/src/com/android/server/telecom/InCallTonePlayer.java
+++ b/src/com/android/server/telecom/InCallTonePlayer.java
@@ -427,11 +427,11 @@
     }
 
     @VisibleForTesting
-    public void startTone() {
+    public boolean startTone() {
         // Skip playing the end call tone if the volume is silenced.
         if (mToneId == TONE_CALL_ENDED && !mAudioManagerAdapter.isVolumeOverZero()) {
             Log.i(this, "startTone: skip end-call tone as device is silenced.");
-            return;
+            return false;
         }
 
         sTonesPlaying++;
@@ -447,6 +447,7 @@
         }
 
         super.start();
+        return true;
     }
 
     @Override
diff --git a/tests/src/com/android/server/telecom/tests/CallAudioManagerTest.java b/tests/src/com/android/server/telecom/tests/CallAudioManagerTest.java
index e4b22ec..01add22 100644
--- a/tests/src/com/android/server/telecom/tests/CallAudioManagerTest.java
+++ b/tests/src/com/android/server/telecom/tests/CallAudioManagerTest.java
@@ -77,7 +77,7 @@
             InCallTonePlayer mockInCallTonePlayer = mock(InCallTonePlayer.class);
             doAnswer((invocation2) -> {
                 mCallAudioManager.setIsTonePlaying(true);
-                return null;
+                return true;
             }).when(mockInCallTonePlayer).startTone();
             return mockInCallTonePlayer;
         }).when(mPlayerFactory).createPlayer(anyInt());
diff --git a/tests/src/com/android/server/telecom/tests/InCallTonePlayerTest.java b/tests/src/com/android/server/telecom/tests/InCallTonePlayerTest.java
index 7718da8..eba494f 100644
--- a/tests/src/com/android/server/telecom/tests/InCallTonePlayerTest.java
+++ b/tests/src/com/android/server/telecom/tests/InCallTonePlayerTest.java
@@ -16,6 +16,8 @@
 
 package com.android.server.telecom.tests;
 
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.ArgumentMatchers.eq;
@@ -114,7 +116,7 @@
     public void testNoEndCallToneInSilence() {
         when(mAudioManagerAdapter.isVolumeOverZero()).thenReturn(false);
         InCallTonePlayer player = mFactory.createPlayer(InCallTonePlayer.TONE_CALL_ENDED);
-        player.startTone();
+        assertFalse(player.startTone());
 
         // Verify we didn't play a tone.
         verify(mCallAudioManager, never()).setIsTonePlaying(eq(true));
@@ -126,7 +128,7 @@
     public void testEndCallToneWhenNotSilenced() {
         when(mAudioManagerAdapter.isVolumeOverZero()).thenReturn(true);
         InCallTonePlayer player = mFactory.createPlayer(InCallTonePlayer.TONE_CALL_ENDED);
-        player.startTone();
+        assertTrue(player.startTone());
 
         // Verify we did play a tone.
         verify(mCallAudioManager).setIsTonePlaying(eq(true));
diff --git a/tests/src/com/android/server/telecom/tests/RingerTest.java b/tests/src/com/android/server/telecom/tests/RingerTest.java
index ab5ddef..02ee137 100644
--- a/tests/src/com/android/server/telecom/tests/RingerTest.java
+++ b/tests/src/com/android/server/telecom/tests/RingerTest.java
@@ -123,6 +123,7 @@
         when(mockAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL);
         NotificationManager notificationManager =
                 (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
+        when(mockTonePlayer.startTone()).thenReturn(true);
         when(notificationManager.matchesCallFilter(any(Bundle.class))).thenReturn(true);
         mRingerUnderTest = new Ringer(mockPlayerFactory, mContext, mockSystemSettingsUtil,
                 mockRingtonePlayer, mockRingtoneFactory, mockVibrator, spyVibrationEffectProxy,