Merge "Properly restart ringtone when BT device connected" into rvc-dev am: 60d4335966 am: 09c03c40b2

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/services/Telecomm/+/12089048

Change-Id: I923dadee2709562f92e9124daedd8e5bfbf11e53
diff --git a/src/com/android/server/telecom/AsyncRingtonePlayer.java b/src/com/android/server/telecom/AsyncRingtonePlayer.java
index 53c5f07..bf2472f 100644
--- a/src/com/android/server/telecom/AsyncRingtonePlayer.java
+++ b/src/com/android/server/telecom/AsyncRingtonePlayer.java
@@ -255,4 +255,8 @@
             }
         }
     }
+
+    public boolean isPlaying() {
+        return mRingtone != null;
+    }
 }
diff --git a/src/com/android/server/telecom/CallAudioManager.java b/src/com/android/server/telecom/CallAudioManager.java
index dac5b4e..a6509b4 100644
--- a/src/com/android/server/telecom/CallAudioManager.java
+++ b/src/com/android/server/telecom/CallAudioManager.java
@@ -449,6 +449,10 @@
         }
     }
 
+    public boolean isRingtonePlaying() {
+        return mRinger.isRinging();
+    }
+
     @VisibleForTesting
     public boolean startRinging() {
         synchronized (mCallsManager.getLock()) {
diff --git a/src/com/android/server/telecom/CallAudioModeStateMachine.java b/src/com/android/server/telecom/CallAudioModeStateMachine.java
index 32598be..2aa9d5d 100644
--- a/src/com/android/server/telecom/CallAudioModeStateMachine.java
+++ b/src/com/android/server/telecom/CallAudioModeStateMachine.java
@@ -358,7 +358,7 @@
         private boolean mHasFocus = false;
 
         private void tryStartRinging() {
-            if (mHasFocus) {
+            if (mHasFocus && mCallAudioManager.isRingtonePlaying()) {
                 Log.i(LOG_TAG, "RingingFocusState#tryStartRinging -- audio focus previously"
                         + " acquired and ringtone already playing -- skipping.");
                 return;
diff --git a/src/com/android/server/telecom/Ringer.java b/src/com/android/server/telecom/Ringer.java
index d02d940..a769a94 100644
--- a/src/com/android/server/telecom/Ringer.java
+++ b/src/com/android/server/telecom/Ringer.java
@@ -458,6 +458,10 @@
         }
     }
 
+    public boolean isRinging() {
+        return mRingtonePlayer.isPlaying();
+    }
+
     private boolean shouldRingForContact(Uri contactUri) {
         final NotificationManager manager =
                 (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
diff --git a/tests/src/com/android/server/telecom/tests/CallAudioModeStateMachineTest.java b/tests/src/com/android/server/telecom/tests/CallAudioModeStateMachineTest.java
index 4fc4358..38f58fd 100644
--- a/tests/src/com/android/server/telecom/tests/CallAudioModeStateMachineTest.java
+++ b/tests/src/com/android/server/telecom/tests/CallAudioModeStateMachineTest.java
@@ -210,6 +210,7 @@
         verify(mCallAudioManager).setCallAudioRouteFocusState(
                 CallAudioRouteStateMachine.RINGING_FOCUS);
 
+        when(mCallAudioManager.isRingtonePlaying()).thenReturn(true);
         sm.sendMessage(CallAudioModeStateMachine.RINGER_MODE_CHANGE);
         waitForHandlerAction(sm.getHandler(), TEST_TIMEOUT);
 
@@ -217,6 +218,44 @@
         verify(mCallAudioManager, times(1)).startRinging();
     }
 
+    @SmallTest
+    @Test
+    public void testStartRingingAfterHfpConnectedIfNotAlreadyPlaying() {
+        CallAudioModeStateMachine sm = new CallAudioModeStateMachine(mSystemStateHelper,
+                mAudioManager, mTestThread.getLooper());
+        sm.setCallAudioManager(mCallAudioManager);
+        sm.sendMessage(CallAudioModeStateMachine.ABANDON_FOCUS_FOR_TESTING);
+        waitForHandlerAction(sm.getHandler(), TEST_TIMEOUT);
+
+        resetMocks();
+        when(mCallAudioManager.startRinging()).thenReturn(true);
+
+        sm.sendMessage(CallAudioModeStateMachine.NEW_RINGING_CALL, new Builder()
+                .setHasActiveOrDialingCalls(false)
+                .setHasRingingCalls(true)
+                .setHasHoldingCalls(false)
+                .setIsTonePlaying(false)
+                .setForegroundCallIsVoip(false)
+                .setSession(null)
+                .build());
+        waitForHandlerAction(sm.getHandler(), TEST_TIMEOUT);
+
+        assertEquals(CallAudioModeStateMachine.RING_STATE_NAME, sm.getCurrentStateName());
+
+        verify(mAudioManager).requestAudioFocusForCall(AudioManager.STREAM_RING,
+                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
+        verify(mAudioManager).setMode(AudioManager.MODE_RINGTONE);
+        verify(mCallAudioManager).setCallAudioRouteFocusState(
+                CallAudioRouteStateMachine.RINGING_FOCUS);
+
+        when(mCallAudioManager.isRingtonePlaying()).thenReturn(false);
+        sm.sendMessage(CallAudioModeStateMachine.RINGER_MODE_CHANGE);
+        waitForHandlerAction(sm.getHandler(), TEST_TIMEOUT);
+
+        // Make sure we do try and start ringing again, since the ringtone wasn't already playing.
+        verify(mCallAudioManager, times(2)).startRinging();
+    }
+
     private void resetMocks() {
         clearInvocations(mCallAudioManager, mAudioManager);
     }