Don't start ringback until CallService ACK

Only start playing the ringback tone once a CallService has
acknowledged that it can be handling the call.

Bug: 13902624

Change-Id: I702dff71e4042ae87dd9aa2c46d53e9bdd66f321
diff --git a/src/com/android/telecomm/RingbackPlayer.java b/src/com/android/telecomm/RingbackPlayer.java
index df4d861..c3db04d 100644
--- a/src/com/android/telecomm/RingbackPlayer.java
+++ b/src/com/android/telecomm/RingbackPlayer.java
@@ -54,7 +54,7 @@
         if (mCallsManager.getForegroundCall() == call) {
 
             // Treat as ending or begining dialing based on the state transition.
-            if (newState == CallState.DIALING) {
+            if (shouldStartRinging(call)) {
                 startRingbackForCall(call);
             } else if (oldState == CallState.DIALING) {
                 stopRingbackForCall(call);
@@ -69,11 +69,30 @@
             stopRingbackForCall(oldForegroundCall);
         }
 
-        if (newForegroundCall != null && newForegroundCall.getState() == CallState.DIALING) {
+        if (shouldStartRinging(newForegroundCall)) {
             startRingbackForCall(newForegroundCall);
         }
     }
 
+    @Override
+    public void onCallServiceChanged(
+            Call call,
+            CallServiceWrapper oldCallServiceWrapper,
+            CallServiceWrapper newCallService) {
+
+        super.onCallServiceChanged(call, oldCallServiceWrapper, newCallService);
+        // Only operate on the foreground call.
+        if (mCallsManager.getForegroundCall() == call) {
+
+            // Treat as ending or begining dialing based on the state transition.
+            if (shouldStartRinging(call)) {
+                startRingbackForCall(call);
+            } else if (newCallService == null) {
+                stopRingbackForCall(call);
+            }
+        }
+    }
+
     /**
      * Starts ringback for the specified dialing call as needed.
      *
@@ -91,7 +110,7 @@
 
         mCall = call;
         if (mTonePlayer == null) {
-            Log.d(this, "Playing the ringback tone.");
+            Log.d(this, "Playing the ringback tone for %s.", call);
             mTonePlayer = mPlayerFactory.createPlayer(InCallTonePlayer.TONE_RING_BACK);
             mTonePlayer.startTone();
         }
@@ -113,10 +132,16 @@
             if (mTonePlayer == null) {
                 Log.w(this, "No player found to stop.");
             } else {
-                Log.i(this, "Stopping the ringback tone.");
+                Log.i(this, "Stopping the ringback tone for %s.", call);
                 mTonePlayer.stopTone();
                 mTonePlayer = null;
             }
         }
     }
+
+    private static boolean shouldStartRinging(Call call) {
+        return call != null
+                && call.getState() == CallState.DIALING
+                && call.getCallService() != null;
+    }
 }