Impl. CallEndpoint changes for TransactionalServiceWrapper

- update CallEndpointController to call into TSW for updates
- impl TSW CallControl && CallEventCallback binder methods
- create a Transaction to change call endpoints

bug: 262912490
Test: CTS tests
Change-Id: I6860f0061421c19b6afdf50e31a2d1db783ca471
diff --git a/src/com/android/server/telecom/CallAudioRouteStateMachine.java b/src/com/android/server/telecom/CallAudioRouteStateMachine.java
index f9419b8..f56320f 100644
--- a/src/com/android/server/telecom/CallAudioRouteStateMachine.java
+++ b/src/com/android/server/telecom/CallAudioRouteStateMachine.java
@@ -1856,12 +1856,7 @@
         Set<Call> calls = mCallsManager.getTrackedCalls();
         for (Call call : calls) {
             if (call != null && call.getConnectionService() != null) {
-                if (call.isTransactionalCall() && call.getTransactionServiceWrapper() != null) {
-                    call.getTransactionServiceWrapper().onCallAudioStateChanged(call,
-                            newCallAudioState);
-                } else {
-                    call.getConnectionService().onCallAudioStateChanged(call, newCallAudioState);
-                }
+                call.getConnectionService().onCallAudioStateChanged(call, newCallAudioState);
             }
         }
     }
diff --git a/src/com/android/server/telecom/CallEndpointController.java b/src/com/android/server/telecom/CallEndpointController.java
index 98bb611..72aad60 100644
--- a/src/com/android/server/telecom/CallEndpointController.java
+++ b/src/com/android/server/telecom/CallEndpointController.java
@@ -164,6 +164,10 @@
             if (call != null && call.getConnectionService() != null) {
                 call.getConnectionService().onCallEndpointChanged(call, mActiveCallEndpoint);
             }
+            else if (call != null && call.getTransactionServiceWrapper() != null) {
+                call.getTransactionServiceWrapper()
+                        .onCallEndpointChanged(call, mActiveCallEndpoint);
+            }
         }
     }
 
@@ -176,6 +180,10 @@
                 call.getConnectionService().onAvailableCallEndpointsChanged(call,
                         mAvailableCallEndpoints);
             }
+            else if (call != null && call.getTransactionServiceWrapper() != null) {
+                call.getTransactionServiceWrapper()
+                        .onAvailableCallEndpointsChanged(call, mAvailableCallEndpoints);
+            }
         }
     }
 
@@ -187,6 +195,9 @@
             if (call != null && call.getConnectionService() != null) {
                 call.getConnectionService().onMuteStateChanged(call, isMuted);
             }
+            else if (call != null && call.getTransactionServiceWrapper() != null) {
+                call.getTransactionServiceWrapper().onMuteStateChanged(call, isMuted);
+            }
         }
     }
 
diff --git a/src/com/android/server/telecom/TransactionalServiceWrapper.java b/src/com/android/server/telecom/TransactionalServiceWrapper.java
index 1b108dc..5cc1872 100644
--- a/src/com/android/server/telecom/TransactionalServiceWrapper.java
+++ b/src/com/android/server/telecom/TransactionalServiceWrapper.java
@@ -26,7 +26,7 @@
 import android.os.OutcomeReceiver;
 import android.os.RemoteException;
 import android.os.ResultReceiver;
-import android.telecom.CallAudioState;
+import android.telecom.CallEndpoint;
 import android.telecom.CallException;
 import android.telecom.CallStreamingService;
 import android.telecom.DisconnectCause;
@@ -38,9 +38,10 @@
 import com.android.internal.telecom.ICallControl;
 import com.android.internal.telecom.ICallEventCallback;
 import com.android.server.telecom.voip.CallEventCallbackAckTransaction;
+import com.android.server.telecom.voip.EndpointChangeTransaction;
+import com.android.server.telecom.voip.HoldCallTransaction;
 import com.android.server.telecom.voip.EndCallTransaction;
 import com.android.server.telecom.voip.HoldActiveCallForNewCallTransaction;
-import com.android.server.telecom.voip.HoldCallTransaction;
 import com.android.server.telecom.voip.ParallelTransaction;
 import com.android.server.telecom.voip.RequestFocusTransaction;
 import com.android.server.telecom.voip.SerialTransaction;
@@ -52,6 +53,7 @@
 import java.util.Hashtable;
 import java.util.List;
 import java.util.Locale;
+import java.util.Set;
 
 /**
  * Implements {@link android.telecom.CallEventCallback} and {@link android.telecom.CallControl}
@@ -265,6 +267,17 @@
                 callback.send(CODE_CALL_IS_NOT_BEING_TRACKED, new Bundle());
             }
         }
+
+        @Override
+        public void requestCallEndpointChange(CallEndpoint endpoint, ResultReceiver callback) {
+            try {
+                Log.startSession("TSW.rCEC");
+                addTransactionsToManager(new EndpointChangeTransaction(endpoint, mCallsManager),
+                        callback);
+            } finally {
+                Log.endSession();
+            }
+        }
     };
 
     private void addTransactionsToManager(VoipCallTransaction transaction,
@@ -363,6 +376,7 @@
                         public void onResult(VoipCallTransactionResult result) {
                             mCallsManager.markCallAsOnHold(call);
                         }
+
                         @Override
                         public void onError(CallException exception) {
                             Log.i(TAG, "onSetInactive: onError: with e=[%e]", exception);
@@ -459,11 +473,29 @@
         }
     }
 
-    // TODO:: replace with onCallEndpointChanged when CLs are merged
-    public void onCallAudioStateChanged(Call call, CallAudioState callAudioState) {
+    public void onCallEndpointChanged(Call call, CallEndpoint endpoint) {
         if (call != null) {
             try {
-                mICallEventCallback.onCallAudioStateChanged(call.getId(), callAudioState);
+                mICallEventCallback.onCallEndpointChanged(call.getId(), endpoint);
+            } catch (RemoteException e) {
+            }
+        }
+    }
+
+    public void onAvailableCallEndpointsChanged(Call call, Set<CallEndpoint> endpoints) {
+        if (call != null) {
+            try {
+                mICallEventCallback.onAvailableCallEndpointsChanged(call.getId(),
+                        endpoints.stream().toList());
+            } catch (RemoteException e) {
+            }
+        }
+    }
+
+    public void onMuteStateChanged(Call call, boolean isMuted) {
+        if (call != null) {
+            try {
+                mICallEventCallback.onMuteStateChanged(call.getId(), isMuted);
             } catch (RemoteException e) {
             }
         }
diff --git a/src/com/android/server/telecom/voip/EndpointChangeTransaction.java b/src/com/android/server/telecom/voip/EndpointChangeTransaction.java
new file mode 100644
index 0000000..88630c8
--- /dev/null
+++ b/src/com/android/server/telecom/voip/EndpointChangeTransaction.java
@@ -0,0 +1,58 @@
+
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.server.telecom.voip;
+
+import android.os.Bundle;
+import android.os.ResultReceiver;
+import android.telecom.CallEndpoint;
+import android.util.Log;
+
+import com.android.server.telecom.CallsManager;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionStage;
+
+public class EndpointChangeTransaction extends VoipCallTransaction {
+    private static final String TAG = EndpointChangeTransaction.class.getSimpleName();
+    private final CallEndpoint mCallEndpoint;
+    private final CallsManager mCallsManager;
+
+    public EndpointChangeTransaction(CallEndpoint endpoint, CallsManager callsManager) {
+        mCallEndpoint = endpoint;
+        mCallsManager = callsManager;
+    }
+
+    @Override
+    public CompletionStage<VoipCallTransactionResult> processTransaction(Void v) {
+        Log.i(TAG, "processTransaction");
+        CompletableFuture<VoipCallTransactionResult> future = new CompletableFuture<>();
+        mCallsManager.requestCallEndpointChange(mCallEndpoint, new ResultReceiver(null) {
+            @Override
+            protected void onReceiveResult(int resultCode, Bundle resultData) {
+                Log.i(TAG, "processTransaction: code=" + resultCode);
+                if (resultCode == CallEndpoint.ENDPOINT_OPERATION_SUCCESS) {
+                    future.complete(new VoipCallTransactionResult(
+                            VoipCallTransactionResult.RESULT_SUCCEED, null));
+                } else {
+                    future.complete(new VoipCallTransactionResult(
+                            VoipCallTransactionResult.RESULT_FAILED, null));
+                }
+            }
+        });
+        return future;
+    }
+}
diff --git a/testapps/transactionalVoipApp/src/com/android/server/telecom/transactionalVoipApp/MyVoipCall.java b/testapps/transactionalVoipApp/src/com/android/server/telecom/transactionalVoipApp/MyVoipCall.java
index b5f8aef..95493e9 100644
--- a/testapps/transactionalVoipApp/src/com/android/server/telecom/transactionalVoipApp/MyVoipCall.java
+++ b/testapps/transactionalVoipApp/src/com/android/server/telecom/transactionalVoipApp/MyVoipCall.java
@@ -18,9 +18,11 @@
 
 
 import android.telecom.CallAudioState;
+import android.telecom.CallEndpoint;
 import android.telecom.CallControl;
 import android.telecom.CallEventCallback;
 import android.util.Log;
+import java.util.List;
 
 import androidx.annotation.NonNull;
 
@@ -71,18 +73,32 @@
     }
 
     @Override
-    public void onCallAudioStateChanged(@NonNull CallAudioState callAudioState) {
-        Log.i(TAG, String.format("onCallAudioStateChanged: state=[%s]", callAudioState.toString()));
-    }
-
-    @Override
     public void onCallStreamingStarted(@NonNull Consumer<Boolean> wasCompleted) {
         Log.i(TAG, String.format("onCallStreamingStarted: callId=[%s]", mCallId));
+        wasCompleted.accept(Boolean.TRUE);
     }
 
     @Override
     public void onCallStreamingFailed(int reason) {
-        Log.i(TAG, String.format("onCallStreamingFailed: callId[%s], reason=[%s]", mCallId,
-                reason));
+        Log.i(TAG, String.format("onCallStreamingFailed: id=[%s], reason=[%d]", mCallId, reason));
+    }
+
+    @Override
+    public void onCallEndpointChanged(@NonNull CallEndpoint newCallEndpoint) {
+        Log.i(TAG, String.format("onCallEndpointChanged: endpoint=[%s]", newCallEndpoint));
+    }
+
+    @Override
+    public void onAvailableCallEndpointsChanged(
+            @NonNull List<CallEndpoint> availableEndpoints) {
+        Log.i(TAG, String.format("onAvailableCallEndpointsChanged: callId=[%s]", mCallId));
+        for (CallEndpoint endpoint : availableEndpoints) {
+            Log.i(TAG, String.format("endpoint=[%s]", endpoint));
+        }
+    }
+
+    @Override
+    public void onMuteStateChanged(boolean isMuted) {
+
     }
 }