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