diff --git a/telecomm/java/android/telecom/Conference.java b/telecomm/java/android/telecom/Conference.java index 9b350c1978a29..15cb786fba1ce 100644 --- a/telecomm/java/android/telecom/Conference.java +++ b/telecomm/java/android/telecom/Conference.java @@ -16,6 +16,7 @@ package android.telecom; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Set; @@ -33,6 +34,8 @@ public abstract class Conference { public void onDisconnected(Conference conference, DisconnectCause disconnectCause) {} public void onConnectionAdded(Conference conference, Connection connection) {} public void onConnectionRemoved(Conference conference, Connection connection) {} + public void onConferenceableConnectionsChanged( + Conference conference, List conferenceableConnections) {} public void onDestroyed(Conference conference) {} public void onCapabilitiesChanged(Conference conference, int capabilities) {} } @@ -41,6 +44,9 @@ public abstract class Conference { private final List mChildConnections = new CopyOnWriteArrayList<>(); private final List mUnmodifiableChildConnections = Collections.unmodifiableList(mChildConnections); + private final List mConferenceableConnections = new ArrayList<>(); + private final List mUnmodifiableConferenceableConnections = + Collections.unmodifiableList(mConferenceableConnections); private PhoneAccountHandle mPhoneAccount; private AudioState mAudioState; @@ -49,6 +55,15 @@ public abstract class Conference { private int mCapabilities; private String mDisconnectMessage; + private final Connection.Listener mConnectionDeathListener = new Connection.Listener() { + @Override + public void onDestroyed(Connection c) { + if (mConferenceableConnections.remove(c)) { + fireOnConferenceableConnectionsChanged(); + } + } + }; + /** * Constructs a new Conference with a mandatory {@link PhoneAccountHandle} * @@ -115,6 +130,13 @@ public abstract class Conference { */ public void onSeparate(Connection connection) {} + /** + * Invoked when the specified {@link Connection} should merged with the conference call. + * + * @param connection The {@code Connection} to merge. + */ + public void onMerge(Connection connection) {} + /** * Invoked when the conference should be put on hold. */ @@ -233,6 +255,37 @@ public abstract class Conference { } } + /** + * Sets the connections with which this connection can be conferenced. + * + * @param conferenceableConnections The set of connections this connection can conference with. + */ + public final void setConferenceableConnections(List conferenceableConnections) { + clearConferenceableList(); + for (Connection c : conferenceableConnections) { + // If statement checks for duplicates in input. It makes it N^2 but we're dealing with a + // small amount of items here. + if (!mConferenceableConnections.contains(c)) { + c.addConnectionListener(mConnectionDeathListener); + mConferenceableConnections.add(c); + } + } + fireOnConferenceableConnectionsChanged(); + } + + private final void fireOnConferenceableConnectionsChanged() { + for (Listener l : mListeners) { + l.onConferenceableConnectionsChanged(this, getConferenceableConnections()); + } + } + + /** + * Returns the connections with which this connection can be conferenced. + */ + public final List getConferenceableConnections() { + return mUnmodifiableConferenceableConnections; + } + /** * Tears down the conference object and any of its current connections. */ @@ -309,4 +362,11 @@ public abstract class Conference { } } } + + private final void clearConferenceableList() { + for (Connection c : mConferenceableConnections) { + c.removeConnectionListener(mConnectionDeathListener); + } + mConferenceableConnections.clear(); + } } diff --git a/telecomm/java/android/telecom/Connection.java b/telecomm/java/android/telecom/Connection.java index 7979e44a0d4f5..2227a04d567b8 100644 --- a/telecomm/java/android/telecom/Connection.java +++ b/telecomm/java/android/telecom/Connection.java @@ -867,7 +867,7 @@ public abstract class Connection { } /** - * Obtains the connections with which this connection can be conferenced. + * Returns the connections with which this connection can be conferenced. */ public final List getConferenceableConnections() { return mUnmodifiableConferenceableConnections; @@ -1097,7 +1097,7 @@ public abstract class Connection { private final void fireOnConferenceableConnectionsChanged() { for (Listener l : mListeners) { - l.onConferenceableConnectionsChanged(this, mConferenceableConnections); + l.onConferenceableConnectionsChanged(this, getConferenceableConnections()); } } diff --git a/telecomm/java/android/telecom/ConnectionService.java b/telecomm/java/android/telecom/ConnectionService.java index d41ac6a5298f2..ab9da0898649b 100644 --- a/telecomm/java/android/telecom/ConnectionService.java +++ b/telecomm/java/android/telecom/ConnectionService.java @@ -356,6 +356,14 @@ public abstract class ConnectionService extends Service { public void onConnectionRemoved(Conference conference, Connection connection) { } + @Override + public void onConferenceableConnectionsChanged( + Conference conference, List conferenceableConnections) { + mAdapter.setConferenceableConnections( + mIdByConference.get(conference), + createConnectionIdList(conferenceableConnections)); + } + @Override public void onDestroyed(Conference conference) { removeConference(conference); @@ -635,19 +643,25 @@ public abstract class ConnectionService extends Service { private void conference(String callId1, String callId2) { Log.d(this, "conference %s, %s", callId1, callId2); - Connection connection1 = findConnectionForAction(callId1, "conference"); - if (connection1 == getNullConnection()) { - Log.w(this, "Connection1 missing in conference request %s.", callId1); - return; - } - Connection connection2 = findConnectionForAction(callId2, "conference"); if (connection2 == getNullConnection()) { Log.w(this, "Connection2 missing in conference request %s.", callId2); return; } - onConference(connection1, connection2); + Connection connection1 = findConnectionForAction(callId1, "conference"); + if (connection1 == getNullConnection()) { + Conference conference1 = findConferenceForAction(callId1, "addConnection"); + if (conference1 == getNullConference()) { + Log.w(this, + "Connection1 or Conference1 missing in conference request %s.", + callId1); + } else { + conference1.onMerge(connection2); + } + } else { + onConference(connection1, connection2); + } } private void splitFromConference(String callId) { diff --git a/telecomm/java/android/telecom/RemoteConference.java b/telecomm/java/android/telecom/RemoteConference.java index f5e0732f43a9e..eba75808164f5 100644 --- a/telecomm/java/android/telecom/RemoteConference.java +++ b/telecomm/java/android/telecom/RemoteConference.java @@ -20,6 +20,7 @@ import com.android.internal.telecom.IConnectionService; import android.os.RemoteException; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Set; @@ -37,6 +38,9 @@ public final class RemoteConference { public void onConnectionAdded(RemoteConference conference, RemoteConnection connection) {} public void onConnectionRemoved(RemoteConference conference, RemoteConnection connection) {} public void onCapabilitiesChanged(RemoteConference conference, int capabilities) {} + public void onConferenceableConnectionsChanged( + RemoteConference conference, + List conferenceableConnections) {} public void onDestroyed(RemoteConference conference) {} } @@ -47,6 +51,9 @@ public final class RemoteConference { private final List mChildConnections = new CopyOnWriteArrayList<>(); private final List mUnmodifiableChildConnections = Collections.unmodifiableList(mChildConnections); + private final List mConferenceableConnections = new ArrayList<>(); + private final List mUnmodifiableConferenceableConnections = + Collections.unmodifiableList(mConferenceableConnections); private int mState = Connection.STATE_NEW; private DisconnectCause mDisconnectCause; @@ -124,6 +131,15 @@ public final class RemoteConference { } } + /** @hide */ + void setConferenceableConnections(List conferenceableConnections) { + mConferenceableConnections.clear(); + mConferenceableConnections.addAll(conferenceableConnections); + for (Callback c : mCallbacks) { + c.onConferenceableConnectionsChanged(this, mUnmodifiableConferenceableConnections); + } + } + /** {@hide} */ void setDisconnected(DisconnectCause disconnectCause) { if (mState != Connection.STATE_DISCONNECTED) { @@ -216,6 +232,10 @@ public final class RemoteConference { } } + public List getConferenceableConnections() { + return mUnmodifiableConferenceableConnections; + } + public final void registerCallback(Callback callback) { mCallbacks.add(callback); } diff --git a/telecomm/java/android/telecom/RemoteConnectionService.java b/telecomm/java/android/telecom/RemoteConnectionService.java index d959e50dda5f1..328dc8691c53e 100644 --- a/telecomm/java/android/telecom/RemoteConnectionService.java +++ b/telecomm/java/android/telecom/RemoteConnectionService.java @@ -278,8 +278,13 @@ final class RemoteConnectionService { } } - findConnectionForAction(callId, "setConferenceableConnections") - .setConferenceableConnections(conferenceable); + if (hasConnection(callId)) { + findConnectionForAction(callId, "setConferenceableConnections") + .setConferenceableConnections(conferenceable); + } else { + findConferenceForAction(callId, "setConferenceableConnections") + .setConferenceableConnections(conferenceable); + } } }; @@ -358,6 +363,10 @@ final class RemoteConnectionService { } } + private boolean hasConnection(String callId) { + return mConferenceById.containsKey(callId); + } + private RemoteConnection findConnectionForAction( String callId, String action) { if (mConnectionById.containsKey(callId)) {