Allow ConnectionService to ask Telecomm for ringback

Cherry picked from branch lmp-preview-dev because checking the changes into master required an API update, whereas the relevant APIs are @hide-ed in the source branch.

Implements the necessary wiring to respond to a ConnectionService that
asks Telecomm to play ringbacks on its behalf.

Bug: 15190301
Change-Id: Ic6a6c031aa92df1f3587daf74f24d090cd21245b
(cherry picked from commit 50a57136b3aa876c8311b58e1e11720a337fe1cc)
diff --git a/src/com/android/telecomm/Call.java b/src/com/android/telecomm/Call.java
index 2bcc978..cd64ff8 100644
--- a/src/com/android/telecomm/Call.java
+++ b/src/com/android/telecomm/Call.java
@@ -58,6 +58,7 @@
         void onFailedOutgoingCall(Call call, boolean isAborted);
         void onSuccessfulIncomingCall(Call call, CallInfo callInfo);
         void onFailedIncomingCall(Call call);
+        void onRequestingRingback(Call call, boolean requestingRingback);
     }
 
     private static final OnQueryCompleteListener sCallerInfoQueryListener =
@@ -172,6 +173,9 @@
     /** The latest token used with a contact info query. */
     private int mQueryToken = 0;
 
+    /** Whether this call is requesting that Telecomm play the ringback tone on its behalf. */
+    private boolean mRequestingRingback = false;
+
     /** Incoming call-info to use when direct-to-voicemail query finishes. */
     private CallInfo mPendingDirectToVoicemailCallInfo;
 
@@ -234,6 +238,17 @@
         }
     }
 
+    void setRequestingRingback(boolean requestingRingback) {
+        mRequestingRingback = requestingRingback;
+        for (Listener l : mListeners) {
+            l.onRequestingRingback(this, mRequestingRingback);
+        }
+    }
+
+    boolean isRequestingRingback() {
+        return mRequestingRingback;
+    }
+
     Uri getHandle() {
         return mHandle;
     }
diff --git a/src/com/android/telecomm/CallServiceWrapper.java b/src/com/android/telecomm/CallServiceWrapper.java
index 9b96890..9b5e2c3 100644
--- a/src/com/android/telecomm/CallServiceWrapper.java
+++ b/src/com/android/telecomm/CallServiceWrapper.java
@@ -56,6 +56,7 @@
         private static final int MSG_SET_DIALING = 6;
         private static final int MSG_SET_DISCONNECTED = 7;
         private static final int MSG_SET_ON_HOLD = 8;
+        private static final int MSG_SET_REQUESTING_RINGBACK = 9;
 
         private final Handler mHandler = new Handler() {
             @Override
@@ -152,6 +153,20 @@
                             Log.w(this, "setOnHold, unknown call id: %s", msg.obj);
                         }
                         break;
+                    case MSG_SET_REQUESTING_RINGBACK:
+                        SomeArgs args = (SomeArgs) msg.obj;
+                        try {
+                            call = mCallIdMapper.getCall(args.arg1);
+                            boolean ringback = (boolean) args.arg2;
+                            if (call != null) {
+                                call.setRequestingRingback(ringback);
+                            } else {
+                                Log.w(this, "setRingback, unknown call id: %s", args.arg1);
+                            }
+                        } finally {
+                            args.recycle();
+                        }
+                        break;
                 }
             }
         };
@@ -227,6 +242,16 @@
             mCallIdMapper.checkValidCallId(callId);
             mHandler.obtainMessage(MSG_SET_ON_HOLD, callId).sendToTarget();
         }
+
+        /** {@inheritDoc} */
+        @Override
+        public void setRequestingRingback(String callId, boolean ringback) {
+            mCallIdMapper.checkValidCallId(callId);
+            SomeArgs args = SomeArgs.obtain();
+            args.arg1 = callId;
+            args.arg2 = ringback;
+            mHandler.obtainMessage(MSG_SET_REQUESTING_RINGBACK, args).sendToTarget();
+        }
     }
 
     private final Adapter mAdapter = new Adapter();
diff --git a/src/com/android/telecomm/CallsManager.java b/src/com/android/telecomm/CallsManager.java
index e1d3044..e8b49e5 100644
--- a/src/com/android/telecomm/CallsManager.java
+++ b/src/com/android/telecomm/CallsManager.java
@@ -60,6 +60,7 @@
         void onIncomingCallRejected(Call call);
         void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
         void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState);
+        void onRequestingRingback(Call call, boolean ringback);
     }
 
     private static final CallsManager INSTANCE = new CallsManager();
@@ -158,6 +159,13 @@
         call.removeListener(this);
     }
 
+    @Override
+    public void onRequestingRingback(Call call, boolean ringback) {
+        for (CallsManagerListener listener : mListeners) {
+            listener.onRequestingRingback(call, ringback);
+        }
+    }
+
     ImmutableCollection<Call> getCalls() {
         return ImmutableList.copyOf(mCalls);
     }
diff --git a/src/com/android/telecomm/CallsManagerListenerBase.java b/src/com/android/telecomm/CallsManagerListenerBase.java
index 8286857..d3ea758 100644
--- a/src/com/android/telecomm/CallsManagerListenerBase.java
+++ b/src/com/android/telecomm/CallsManagerListenerBase.java
@@ -70,4 +70,8 @@
     @Override
     public void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
     }
+
+    @Override
+    public void onRequestingRingback(Call call, boolean ringback) {
+    }
 }
diff --git a/src/com/android/telecomm/RingbackPlayer.java b/src/com/android/telecomm/RingbackPlayer.java
index 4bb8ff2..8fd6697 100644
--- a/src/com/android/telecomm/RingbackPlayer.java
+++ b/src/com/android/telecomm/RingbackPlayer.java
@@ -49,21 +49,6 @@
 
     /** {@inheritDoc} */
     @Override
-    public void onCallStateChanged(Call call, CallState oldState, CallState newState) {
-        // 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 (oldState == CallState.DIALING) {
-                stopRingbackForCall(call);
-            }
-        }
-    }
-
-    /** {@inheritDoc} */
-    @Override
     public void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall) {
         if (oldForegroundCall != null) {
             stopRingbackForCall(oldForegroundCall);
@@ -81,15 +66,20 @@
             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);
+        }
+    }
 
-            // Treat as ending or begining dialing based on the state transition.
-            if (shouldStartRinging(call)) {
-                startRingbackForCall(call);
-            } else if (newCallService == null) {
-                stopRingbackForCall(call);
-            }
+    @Override
+    public void onRequestingRingback(Call call, boolean ignored) {
+        if (shouldStartRinging(call)) {
+            startRingbackForCall(call);
+        } else {
+            stopRingbackForCall(call);
         }
     }
 
@@ -144,8 +134,10 @@
         }
     }
 
-    private static boolean shouldStartRinging(Call call) {
+    private boolean shouldStartRinging(Call call) {
         return call != null
-                && call.getState() == CallState.DIALING;
+                && mCallsManager.getForegroundCall() == call
+                && call.getState() == CallState.DIALING
+                && call.isRequestingRingback();
     }
 }