| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 17 | package android.telecom; |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 18 | |
| Evan Charlton | 0e094d9 | 2014-11-08 15:49:16 -0800 | [diff] [blame] | 19 | import android.annotation.SystemApi; |
| 20 | |
| Ihab Awad | 50e3506 | 2014-09-30 09:17:03 -0700 | [diff] [blame] | 21 | import java.util.ArrayList; |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 22 | import java.util.Collections; |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 23 | import java.util.List; |
| 24 | import java.util.Set; |
| 25 | import java.util.concurrent.CopyOnWriteArrayList; |
| 26 | import java.util.concurrent.CopyOnWriteArraySet; |
| 27 | |
| 28 | /** |
| 29 | * Represents a conference call which can contain any number of {@link Connection} objects. |
| Evan Charlton | 0e094d9 | 2014-11-08 15:49:16 -0800 | [diff] [blame] | 30 | * @hide |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 31 | */ |
| Evan Charlton | 0e094d9 | 2014-11-08 15:49:16 -0800 | [diff] [blame] | 32 | @SystemApi |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 33 | public abstract class Conference { |
| 34 | |
| 35 | /** @hide */ |
| 36 | public abstract static class Listener { |
| 37 | public void onStateChanged(Conference conference, int oldState, int newState) {} |
| Andrew Lee | 7f3d41f | 2014-09-11 17:33:16 -0700 | [diff] [blame] | 38 | public void onDisconnected(Conference conference, DisconnectCause disconnectCause) {} |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 39 | public void onConnectionAdded(Conference conference, Connection connection) {} |
| 40 | public void onConnectionRemoved(Conference conference, Connection connection) {} |
| Ihab Awad | 50e3506 | 2014-09-30 09:17:03 -0700 | [diff] [blame] | 41 | public void onConferenceableConnectionsChanged( |
| 42 | Conference conference, List<Connection> conferenceableConnections) {} |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 43 | public void onDestroyed(Conference conference) {} |
| 44 | public void onCapabilitiesChanged(Conference conference, int capabilities) {} |
| 45 | } |
| 46 | |
| 47 | private final Set<Listener> mListeners = new CopyOnWriteArraySet<>(); |
| 48 | private final List<Connection> mChildConnections = new CopyOnWriteArrayList<>(); |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 49 | private final List<Connection> mUnmodifiableChildConnections = |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 50 | Collections.unmodifiableList(mChildConnections); |
| Ihab Awad | 50e3506 | 2014-09-30 09:17:03 -0700 | [diff] [blame] | 51 | private final List<Connection> mConferenceableConnections = new ArrayList<>(); |
| 52 | private final List<Connection> mUnmodifiableConferenceableConnections = |
| 53 | Collections.unmodifiableList(mConferenceableConnections); |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 54 | |
| 55 | private PhoneAccountHandle mPhoneAccount; |
| Yorke Lee | a0d3ca9 | 2014-09-15 19:18:13 -0700 | [diff] [blame] | 56 | private AudioState mAudioState; |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 57 | private int mState = Connection.STATE_NEW; |
| Andrew Lee | 7f3d41f | 2014-09-11 17:33:16 -0700 | [diff] [blame] | 58 | private DisconnectCause mDisconnectCause; |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 59 | private int mCapabilities; |
| 60 | private String mDisconnectMessage; |
| 61 | |
| Ihab Awad | 50e3506 | 2014-09-30 09:17:03 -0700 | [diff] [blame] | 62 | private final Connection.Listener mConnectionDeathListener = new Connection.Listener() { |
| 63 | @Override |
| 64 | public void onDestroyed(Connection c) { |
| 65 | if (mConferenceableConnections.remove(c)) { |
| 66 | fireOnConferenceableConnectionsChanged(); |
| 67 | } |
| 68 | } |
| 69 | }; |
| 70 | |
| Nancy Chen | 56fc25d | 2014-09-09 12:24:51 -0700 | [diff] [blame] | 71 | /** |
| 72 | * Constructs a new Conference with a mandatory {@link PhoneAccountHandle} |
| 73 | * |
| 74 | * @param phoneAccount The {@code PhoneAccountHandle} associated with the conference. |
| 75 | */ |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 76 | public Conference(PhoneAccountHandle phoneAccount) { |
| 77 | mPhoneAccount = phoneAccount; |
| 78 | } |
| 79 | |
| Nancy Chen | 56fc25d | 2014-09-09 12:24:51 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Returns the {@link PhoneAccountHandle} the conference call is being placed through. |
| 82 | * |
| 83 | * @return A {@code PhoneAccountHandle} object representing the PhoneAccount of the conference. |
| 84 | */ |
| Nancy Chen | ea38cca | 2014-09-05 16:38:49 -0700 | [diff] [blame] | 85 | public final PhoneAccountHandle getPhoneAccountHandle() { |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 86 | return mPhoneAccount; |
| 87 | } |
| 88 | |
| Nancy Chen | 56fc25d | 2014-09-09 12:24:51 -0700 | [diff] [blame] | 89 | /** |
| 90 | * Returns the list of connections currently associated with the conference call. |
| 91 | * |
| 92 | * @return A list of {@code Connection} objects which represent the children of the conference. |
| 93 | */ |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 94 | public final List<Connection> getConnections() { |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 95 | return mUnmodifiableChildConnections; |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| Nancy Chen | 56fc25d | 2014-09-09 12:24:51 -0700 | [diff] [blame] | 98 | /** |
| 99 | * Gets the state of the conference call. See {@link Connection} for valid values. |
| 100 | * |
| 101 | * @return A constant representing the state the conference call is currently in. |
| 102 | */ |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 103 | public final int getState() { |
| 104 | return mState; |
| 105 | } |
| 106 | |
| Nancy Chen | 56fc25d | 2014-09-09 12:24:51 -0700 | [diff] [blame] | 107 | /** |
| 108 | * Returns the capabilities of a conference. See {@link PhoneCapabilities} for valid values. |
| 109 | * |
| 110 | * @return A bitmask of the {@code PhoneCapabilities} of the conference call. |
| 111 | */ |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 112 | public final int getCapabilities() { |
| 113 | return mCapabilities; |
| 114 | } |
| 115 | |
| 116 | /** |
| Yorke Lee | a0d3ca9 | 2014-09-15 19:18:13 -0700 | [diff] [blame] | 117 | * @return The audio state of the conference, describing how its audio is currently |
| 118 | * being routed by the system. This is {@code null} if this Conference |
| 119 | * does not directly know about its audio state. |
| 120 | */ |
| 121 | public final AudioState getAudioState() { |
| 122 | return mAudioState; |
| 123 | } |
| 124 | |
| 125 | /** |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 126 | * Invoked when the Conference and all it's {@link Connection}s should be disconnected. |
| 127 | */ |
| 128 | public void onDisconnect() {} |
| 129 | |
| 130 | /** |
| 131 | * Invoked when the specified {@link Connection} should be separated from the conference call. |
| 132 | * |
| 133 | * @param connection The connection to separate. |
| 134 | */ |
| 135 | public void onSeparate(Connection connection) {} |
| 136 | |
| 137 | /** |
| Ihab Awad | 50e3506 | 2014-09-30 09:17:03 -0700 | [diff] [blame] | 138 | * Invoked when the specified {@link Connection} should merged with the conference call. |
| 139 | * |
| 140 | * @param connection The {@code Connection} to merge. |
| 141 | */ |
| 142 | public void onMerge(Connection connection) {} |
| 143 | |
| 144 | /** |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 145 | * Invoked when the conference should be put on hold. |
| 146 | */ |
| 147 | public void onHold() {} |
| 148 | |
| 149 | /** |
| 150 | * Invoked when the conference should be moved from hold to active. |
| 151 | */ |
| 152 | public void onUnhold() {} |
| 153 | |
| 154 | /** |
| Santos Cordon | a486804 | 2014-09-04 17:39:22 -0700 | [diff] [blame] | 155 | * Invoked when the child calls should be merged. Only invoked if the conference contains the |
| 156 | * capability {@link PhoneCapabilities#MERGE_CONFERENCE}. |
| 157 | */ |
| 158 | public void onMerge() {} |
| 159 | |
| 160 | /** |
| 161 | * Invoked when the child calls should be swapped. Only invoked if the conference contains the |
| 162 | * capability {@link PhoneCapabilities#SWAP_CONFERENCE}. |
| 163 | */ |
| 164 | public void onSwap() {} |
| 165 | |
| 166 | /** |
| Yorke Lee | a0d3ca9 | 2014-09-15 19:18:13 -0700 | [diff] [blame] | 167 | * Notifies this conference of a request to play a DTMF tone. |
| 168 | * |
| 169 | * @param c A DTMF character. |
| 170 | */ |
| 171 | public void onPlayDtmfTone(char c) {} |
| 172 | |
| 173 | /** |
| 174 | * Notifies this conference of a request to stop any currently playing DTMF tones. |
| 175 | */ |
| 176 | public void onStopDtmfTone() {} |
| 177 | |
| 178 | /** |
| 179 | * Notifies this conference that the {@link #getAudioState()} property has a new value. |
| 180 | * |
| 181 | * @param state The new call audio state. |
| 182 | */ |
| 183 | public void onAudioStateChanged(AudioState state) {} |
| 184 | |
| 185 | /** |
| Andrew Lee | 46f7f5d | 2014-11-06 17:00:25 -0800 | [diff] [blame] | 186 | * Notifies this conference that a connection has been added to it. |
| 187 | * |
| 188 | * @param connection The newly added connection. |
| 189 | */ |
| 190 | public void onConnectionAdded(Connection connection) {} |
| 191 | |
| 192 | /** |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 193 | * Sets state to be on hold. |
| 194 | */ |
| 195 | public final void setOnHold() { |
| 196 | setState(Connection.STATE_HOLDING); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Sets state to be active. |
| 201 | */ |
| 202 | public final void setActive() { |
| 203 | setState(Connection.STATE_ACTIVE); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Sets state to disconnected. |
| 208 | * |
| Andrew Lee | 7f3d41f | 2014-09-11 17:33:16 -0700 | [diff] [blame] | 209 | * @param disconnectCause The reason for the disconnection, as described by |
| 210 | * {@link android.telecom.DisconnectCause}. |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 211 | */ |
| Andrew Lee | 7f3d41f | 2014-09-11 17:33:16 -0700 | [diff] [blame] | 212 | public final void setDisconnected(DisconnectCause disconnectCause) { |
| 213 | mDisconnectCause = disconnectCause;; |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 214 | setState(Connection.STATE_DISCONNECTED); |
| 215 | for (Listener l : mListeners) { |
| Andrew Lee | 7f3d41f | 2014-09-11 17:33:16 -0700 | [diff] [blame] | 216 | l.onDisconnected(this, mDisconnectCause); |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | /** |
| mike dooley | 1cf14ac | 2014-11-04 10:59:53 -0800 | [diff] [blame] | 221 | * @return The {@link DisconnectCause} for this connection. |
| 222 | */ |
| 223 | public final DisconnectCause getDisconnectCause() { |
| 224 | return mDisconnectCause; |
| 225 | } |
| 226 | |
| 227 | /** |
| Nancy Chen | 56fc25d | 2014-09-09 12:24:51 -0700 | [diff] [blame] | 228 | * Sets the capabilities of a conference. See {@link PhoneCapabilities} for valid values. |
| 229 | * |
| 230 | * @param capabilities A bitmask of the {@code PhoneCapabilities} of the conference call. |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 231 | */ |
| 232 | public final void setCapabilities(int capabilities) { |
| 233 | if (capabilities != mCapabilities) { |
| 234 | mCapabilities = capabilities; |
| 235 | |
| 236 | for (Listener l : mListeners) { |
| 237 | l.onCapabilitiesChanged(this, mCapabilities); |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Adds the specified connection as a child of this conference. |
| 244 | * |
| 245 | * @param connection The connection to add. |
| 246 | * @return True if the connection was successfully added. |
| 247 | */ |
| Santos Cordon | a486804 | 2014-09-04 17:39:22 -0700 | [diff] [blame] | 248 | public final boolean addConnection(Connection connection) { |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 249 | if (connection != null && !mChildConnections.contains(connection)) { |
| 250 | if (connection.setConference(this)) { |
| 251 | mChildConnections.add(connection); |
| Andrew Lee | 46f7f5d | 2014-11-06 17:00:25 -0800 | [diff] [blame] | 252 | onConnectionAdded(connection); |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 253 | for (Listener l : mListeners) { |
| 254 | l.onConnectionAdded(this, connection); |
| 255 | } |
| 256 | return true; |
| 257 | } |
| 258 | } |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Removes the specified connection as a child of this conference. |
| 264 | * |
| 265 | * @param connection The connection to remove. |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 266 | */ |
| Santos Cordon | a486804 | 2014-09-04 17:39:22 -0700 | [diff] [blame] | 267 | public final void removeConnection(Connection connection) { |
| Santos Cordon | 0159ac0 | 2014-08-21 14:28:11 -0700 | [diff] [blame] | 268 | Log.d(this, "removing %s from %s", connection, mChildConnections); |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 269 | if (connection != null && mChildConnections.remove(connection)) { |
| 270 | connection.resetConference(); |
| 271 | for (Listener l : mListeners) { |
| 272 | l.onConnectionRemoved(this, connection); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /** |
| Ihab Awad | 50e3506 | 2014-09-30 09:17:03 -0700 | [diff] [blame] | 278 | * Sets the connections with which this connection can be conferenced. |
| 279 | * |
| 280 | * @param conferenceableConnections The set of connections this connection can conference with. |
| 281 | */ |
| 282 | public final void setConferenceableConnections(List<Connection> conferenceableConnections) { |
| 283 | clearConferenceableList(); |
| 284 | for (Connection c : conferenceableConnections) { |
| 285 | // If statement checks for duplicates in input. It makes it N^2 but we're dealing with a |
| 286 | // small amount of items here. |
| 287 | if (!mConferenceableConnections.contains(c)) { |
| 288 | c.addConnectionListener(mConnectionDeathListener); |
| 289 | mConferenceableConnections.add(c); |
| 290 | } |
| 291 | } |
| 292 | fireOnConferenceableConnectionsChanged(); |
| 293 | } |
| 294 | |
| 295 | private final void fireOnConferenceableConnectionsChanged() { |
| 296 | for (Listener l : mListeners) { |
| 297 | l.onConferenceableConnectionsChanged(this, getConferenceableConnections()); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Returns the connections with which this connection can be conferenced. |
| 303 | */ |
| 304 | public final List<Connection> getConferenceableConnections() { |
| 305 | return mUnmodifiableConferenceableConnections; |
| 306 | } |
| 307 | |
| 308 | /** |
| Nancy Chen | ea38cca | 2014-09-05 16:38:49 -0700 | [diff] [blame] | 309 | * Tears down the conference object and any of its current connections. |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 310 | */ |
| Santos Cordon | a486804 | 2014-09-04 17:39:22 -0700 | [diff] [blame] | 311 | public final void destroy() { |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 312 | Log.d(this, "destroying conference : %s", this); |
| 313 | // Tear down the children. |
| Santos Cordon | 0159ac0 | 2014-08-21 14:28:11 -0700 | [diff] [blame] | 314 | for (Connection connection : mChildConnections) { |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 315 | Log.d(this, "removing connection %s", connection); |
| 316 | removeConnection(connection); |
| 317 | } |
| 318 | |
| 319 | // If not yet disconnected, set the conference call as disconnected first. |
| 320 | if (mState != Connection.STATE_DISCONNECTED) { |
| 321 | Log.d(this, "setting to disconnected"); |
| Andrew Lee | 7f3d41f | 2014-09-11 17:33:16 -0700 | [diff] [blame] | 322 | setDisconnected(new DisconnectCause(DisconnectCause.LOCAL)); |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | // ...and notify. |
| 326 | for (Listener l : mListeners) { |
| 327 | l.onDestroyed(this); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Add a listener to be notified of a state change. |
| 333 | * |
| 334 | * @param listener The new listener. |
| 335 | * @return This conference. |
| 336 | * @hide |
| 337 | */ |
| 338 | public final Conference addListener(Listener listener) { |
| 339 | mListeners.add(listener); |
| 340 | return this; |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Removes the specified listener. |
| 345 | * |
| 346 | * @param listener The listener to remove. |
| 347 | * @return This conference. |
| 348 | * @hide |
| 349 | */ |
| 350 | public final Conference removeListener(Listener listener) { |
| 351 | mListeners.remove(listener); |
| 352 | return this; |
| 353 | } |
| 354 | |
| Yorke Lee | a0d3ca9 | 2014-09-15 19:18:13 -0700 | [diff] [blame] | 355 | /** |
| Tyler Gunn | 4a57b9b | 2014-10-30 14:27:48 -0700 | [diff] [blame] | 356 | * Retrieves the primary connection associated with the conference. The primary connection is |
| 357 | * the connection from which the conference will retrieve its current state. |
| 358 | * |
| 359 | * @return The primary connection. |
| 360 | */ |
| 361 | public Connection getPrimaryConnection() { |
| 362 | if (mUnmodifiableChildConnections == null || mUnmodifiableChildConnections.isEmpty()) { |
| 363 | return null; |
| 364 | } |
| 365 | return mUnmodifiableChildConnections.get(0); |
| 366 | } |
| 367 | |
| 368 | /** |
| Yorke Lee | a0d3ca9 | 2014-09-15 19:18:13 -0700 | [diff] [blame] | 369 | * Inform this Conference that the state of its audio output has been changed externally. |
| 370 | * |
| 371 | * @param state The new audio state. |
| 372 | * @hide |
| 373 | */ |
| 374 | final void setAudioState(AudioState state) { |
| 375 | Log.d(this, "setAudioState %s", state); |
| 376 | mAudioState = state; |
| 377 | onAudioStateChanged(state); |
| 378 | } |
| 379 | |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 380 | private void setState(int newState) { |
| 381 | if (newState != Connection.STATE_ACTIVE && |
| 382 | newState != Connection.STATE_HOLDING && |
| 383 | newState != Connection.STATE_DISCONNECTED) { |
| 384 | Log.w(this, "Unsupported state transition for Conference call.", |
| 385 | Connection.stateToString(newState)); |
| 386 | return; |
| 387 | } |
| 388 | |
| 389 | if (mState != newState) { |
| 390 | int oldState = mState; |
| 391 | mState = newState; |
| 392 | for (Listener l : mListeners) { |
| 393 | l.onStateChanged(this, oldState, newState); |
| 394 | } |
| 395 | } |
| 396 | } |
| Ihab Awad | 50e3506 | 2014-09-30 09:17:03 -0700 | [diff] [blame] | 397 | |
| 398 | private final void clearConferenceableList() { |
| 399 | for (Connection c : mConferenceableConnections) { |
| 400 | c.removeConnectionListener(mConnectionDeathListener); |
| 401 | } |
| 402 | mConferenceableConnections.clear(); |
| 403 | } |
| Santos Cordon | 823fd3c | 2014-08-07 18:35:18 -0700 | [diff] [blame] | 404 | } |