Fixing issue with adding existing connections which start in a conference.

When adding an existing connection to Telecom, Telephony uses an @hide
version of the API which allows the connection's parent to be specified
at the same time.  This was problematic for IMS conference calls wrapped
by a connection manager because they would NEVER get set as part of the
conference.

To fix this, in the RemoteConnectionService we stash the associated parent
for the new existing connection in the connection extras, and we use it
here to restore the desired parent in Telecom when adding the existing
connection.

Also, added some logs to that it is possible to see which calls another
call are eligible to be conferenced with.

Test: Manual testing of IMS conference calls via remote connection service
API
Bug: 188420526
Change-Id: I7b055e2f47ec79235cef08ac3e2e6cbc1ae13e12

Change-Id: Ie0a80fe29cb6bfa62d5d138111fec6b94628d7ee
diff --git a/src/com/android/server/telecom/Call.java b/src/com/android/server/telecom/Call.java
index 8fae923..0218124 100644
--- a/src/com/android/server/telecom/Call.java
+++ b/src/com/android/server/telecom/Call.java
@@ -85,6 +85,7 @@
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
 
 /**
  *  Encapsulates all aspects of a given phone call throughout its lifecycle, starting
@@ -3160,6 +3161,13 @@
     void setConferenceableCalls(List<Call> conferenceableCalls) {
         mConferenceableCalls.clear();
         mConferenceableCalls.addAll(conferenceableCalls);
+        String confCallIds = "";
+        if (!conferenceableCalls.isEmpty()) {
+            confCallIds = conferenceableCalls.stream()
+                    .map(c -> c.getId())
+                    .collect(Collectors.joining(","));
+        }
+        Log.addEvent(this, LogUtils.Events.CONF_CALLS_CHANGED, confCallIds);
 
         for (Listener l : mListeners) {
             l.onConferenceableCallsChanged(this);
diff --git a/src/com/android/server/telecom/ConnectionServiceWrapper.java b/src/com/android/server/telecom/ConnectionServiceWrapper.java
index 21c6844..da2669c 100755
--- a/src/com/android/server/telecom/ConnectionServiceWrapper.java
+++ b/src/com/android/server/telecom/ConnectionServiceWrapper.java
@@ -920,6 +920,45 @@
                         } else {
                             connectIdToCheck = callId;
                         }
+
+                        // Handle the case where an existing connection was added by Telephony via
+                        // a connection manager.  The remote connection service API does not include
+                        // the ability to specify a parent connection when adding an existing
+                        // connection, so we stash the desired parent in the connection extras.
+                        if (connectionExtras != null
+                                && connectionExtras.containsKey(
+                                        Connection.EXTRA_ADD_TO_CONFERENCE_ID)
+                                && connection.getParentCallId() == null) {
+                            String parentId = connectionExtras.getString(
+                                    Connection.EXTRA_ADD_TO_CONFERENCE_ID);
+                            Log.i(ConnectionServiceWrapper.this, "addExistingConnection: remote "
+                                    + "connection will auto-add to parent %s", parentId);
+                            // Replace parcelable connection instance, swapping the new desired
+                            // parent in.
+                            connection = new ParcelableConnection(
+                                    connection.getPhoneAccount(),
+                                    connection.getState(),
+                                    connection.getConnectionCapabilities(),
+                                    connection.getConnectionProperties(),
+                                    connection.getSupportedAudioRoutes(),
+                                    connection.getHandle(),
+                                    connection.getHandlePresentation(),
+                                    connection.getCallerDisplayName(),
+                                    connection.getCallerDisplayNamePresentation(),
+                                    connection.getVideoProvider(),
+                                    connection.getVideoState(),
+                                    connection.isRingbackRequested(),
+                                    connection.getIsVoipAudioMode(),
+                                    connection.getConnectTimeMillis(),
+                                    connection.getConnectElapsedTimeMillis(),
+                                    connection.getStatusHints(),
+                                    connection.getDisconnectCause(),
+                                    connection.getConferenceableConnectionIds(),
+                                    connection.getExtras(),
+                                    parentId,
+                                    connection.getCallDirection(),
+                                    connection.getCallerNumberVerificationStatus());
+                        }
                         // Check to see if this Connection has already been added.
                         Call alreadyAddedConnection = mCallsManager
                                 .getAlreadyAddedConnection(connectIdToCheck);
diff --git a/src/com/android/server/telecom/LogUtils.java b/src/com/android/server/telecom/LogUtils.java
index 138e441..f53f239 100644
--- a/src/com/android/server/telecom/LogUtils.java
+++ b/src/com/android/server/telecom/LogUtils.java
@@ -138,6 +138,7 @@
         public static final String REMOVE_CHILD = "REMOVE_CHILD";
         public static final String SET_PARENT = "SET_PARENT";
         public static final String CONF_STATE_CHANGED = "CONF_STATE_CHANGED";
+        public static final String CONF_CALLS_CHANGED = "CONF_CALLS_CHANGED";
         public static final String CALL_DIRECTION_CHANGED = "CALL_DIRECTION_CHANGED";
         public static final String MUTE = "MUTE";
         public static final String UNMUTE = "UNMUTE";