blob: 5371481325551dbc1fa8eadec46366d6dc453e03 [file] [log] [blame]
Santos Cordon823fd3c2014-08-07 18:35:18 -07001/*
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 Gunnef9f6f92014-09-12 22:16:17 -070017package android.telecom;
Santos Cordon823fd3c2014-08-07 18:35:18 -070018
Evan Charlton0e094d92014-11-08 15:49:16 -080019import android.annotation.SystemApi;
20
Ihab Awad50e35062014-09-30 09:17:03 -070021import java.util.ArrayList;
Santos Cordon823fd3c2014-08-07 18:35:18 -070022import java.util.Collections;
Santos Cordon823fd3c2014-08-07 18:35:18 -070023import java.util.List;
24import java.util.Set;
25import java.util.concurrent.CopyOnWriteArrayList;
26import java.util.concurrent.CopyOnWriteArraySet;
27
28/**
29 * Represents a conference call which can contain any number of {@link Connection} objects.
Evan Charlton0e094d92014-11-08 15:49:16 -080030 * @hide
Santos Cordon823fd3c2014-08-07 18:35:18 -070031 */
Evan Charlton0e094d92014-11-08 15:49:16 -080032@SystemApi
Tyler Gunn6d76ca02014-11-17 15:49:51 -080033public abstract class Conference implements IConferenceable {
Santos Cordon823fd3c2014-08-07 18:35:18 -070034
35 /** @hide */
36 public abstract static class Listener {
37 public void onStateChanged(Conference conference, int oldState, int newState) {}
Andrew Lee7f3d41f2014-09-11 17:33:16 -070038 public void onDisconnected(Conference conference, DisconnectCause disconnectCause) {}
Santos Cordon823fd3c2014-08-07 18:35:18 -070039 public void onConnectionAdded(Conference conference, Connection connection) {}
40 public void onConnectionRemoved(Conference conference, Connection connection) {}
Ihab Awad50e35062014-09-30 09:17:03 -070041 public void onConferenceableConnectionsChanged(
42 Conference conference, List<Connection> conferenceableConnections) {}
Santos Cordon823fd3c2014-08-07 18:35:18 -070043 public void onDestroyed(Conference conference) {}
Ihab Awad5c9c86e2014-11-12 13:41:16 -080044 public void onConnectionCapabilitiesChanged(
45 Conference conference, int connectionCapabilities) {}
Santos Cordon823fd3c2014-08-07 18:35:18 -070046 }
47
48 private final Set<Listener> mListeners = new CopyOnWriteArraySet<>();
49 private final List<Connection> mChildConnections = new CopyOnWriteArrayList<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -070050 private final List<Connection> mUnmodifiableChildConnections =
Santos Cordon823fd3c2014-08-07 18:35:18 -070051 Collections.unmodifiableList(mChildConnections);
Ihab Awad50e35062014-09-30 09:17:03 -070052 private final List<Connection> mConferenceableConnections = new ArrayList<>();
53 private final List<Connection> mUnmodifiableConferenceableConnections =
54 Collections.unmodifiableList(mConferenceableConnections);
Santos Cordon823fd3c2014-08-07 18:35:18 -070055
56 private PhoneAccountHandle mPhoneAccount;
Yorke Leea0d3ca92014-09-15 19:18:13 -070057 private AudioState mAudioState;
Santos Cordon823fd3c2014-08-07 18:35:18 -070058 private int mState = Connection.STATE_NEW;
Andrew Lee7f3d41f2014-09-11 17:33:16 -070059 private DisconnectCause mDisconnectCause;
Ihab Awad5c9c86e2014-11-12 13:41:16 -080060 private int mConnectionCapabilities;
Santos Cordon823fd3c2014-08-07 18:35:18 -070061 private String mDisconnectMessage;
62
Ihab Awad50e35062014-09-30 09:17:03 -070063 private final Connection.Listener mConnectionDeathListener = new Connection.Listener() {
64 @Override
65 public void onDestroyed(Connection c) {
66 if (mConferenceableConnections.remove(c)) {
67 fireOnConferenceableConnectionsChanged();
68 }
69 }
70 };
71
Nancy Chen56fc25d2014-09-09 12:24:51 -070072 /**
73 * Constructs a new Conference with a mandatory {@link PhoneAccountHandle}
74 *
75 * @param phoneAccount The {@code PhoneAccountHandle} associated with the conference.
76 */
Santos Cordon823fd3c2014-08-07 18:35:18 -070077 public Conference(PhoneAccountHandle phoneAccount) {
78 mPhoneAccount = phoneAccount;
79 }
80
Nancy Chen56fc25d2014-09-09 12:24:51 -070081 /**
82 * Returns the {@link PhoneAccountHandle} the conference call is being placed through.
83 *
84 * @return A {@code PhoneAccountHandle} object representing the PhoneAccount of the conference.
85 */
Nancy Chenea38cca2014-09-05 16:38:49 -070086 public final PhoneAccountHandle getPhoneAccountHandle() {
Santos Cordon823fd3c2014-08-07 18:35:18 -070087 return mPhoneAccount;
88 }
89
Nancy Chen56fc25d2014-09-09 12:24:51 -070090 /**
91 * Returns the list of connections currently associated with the conference call.
92 *
93 * @return A list of {@code Connection} objects which represent the children of the conference.
94 */
Santos Cordon823fd3c2014-08-07 18:35:18 -070095 public final List<Connection> getConnections() {
Ihab Awadb8e85c72014-08-23 20:34:57 -070096 return mUnmodifiableChildConnections;
Santos Cordon823fd3c2014-08-07 18:35:18 -070097 }
98
Nancy Chen56fc25d2014-09-09 12:24:51 -070099 /**
100 * Gets the state of the conference call. See {@link Connection} for valid values.
101 *
102 * @return A constant representing the state the conference call is currently in.
103 */
Santos Cordon823fd3c2014-08-07 18:35:18 -0700104 public final int getState() {
105 return mState;
106 }
107
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800108 /** @hide */
109 @Deprecated public final int getCapabilities() {
110 return getConnectionCapabilities();
111 }
112
Nancy Chen56fc25d2014-09-09 12:24:51 -0700113 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800114 * Returns the capabilities of a conference. See {@code CAPABILITY_*} constants in class
115 * {@link Connection} for valid values.
Nancy Chen56fc25d2014-09-09 12:24:51 -0700116 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800117 * @return A bitmask of the capabilities of the conference call.
Nancy Chen56fc25d2014-09-09 12:24:51 -0700118 */
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800119 public final int getConnectionCapabilities() {
120 return mConnectionCapabilities;
121 }
122
123 /**
124 * Whether the given capabilities support the specified capability.
125 *
126 * @param capabilities A capability bit field.
127 * @param capability The capability to check capabilities for.
128 * @return Whether the specified capability is supported.
129 * @hide
130 */
131 public static boolean can(int capabilities, int capability) {
132 return (capabilities & capability) != 0;
133 }
134
135 /**
136 * Whether the capabilities of this {@code Connection} supports the specified capability.
137 *
138 * @param capability The capability to check capabilities for.
139 * @return Whether the specified capability is supported.
140 * @hide
141 */
142 public boolean can(int capability) {
143 return can(mConnectionCapabilities, capability);
144 }
145
146 /**
147 * Removes the specified capability from the set of capabilities of this {@code Conference}.
148 *
149 * @param capability The capability to remove from the set.
150 * @hide
151 */
152 public void removeCapability(int capability) {
153 mConnectionCapabilities &= ~capability;
154 }
155
156 /**
157 * Adds the specified capability to the set of capabilities of this {@code Conference}.
158 *
159 * @param capability The capability to add to the set.
160 * @hide
161 */
162 public void addCapability(int capability) {
163 mConnectionCapabilities |= capability;
Santos Cordon823fd3c2014-08-07 18:35:18 -0700164 }
165
166 /**
Yorke Leea0d3ca92014-09-15 19:18:13 -0700167 * @return The audio state of the conference, describing how its audio is currently
168 * being routed by the system. This is {@code null} if this Conference
169 * does not directly know about its audio state.
170 */
171 public final AudioState getAudioState() {
172 return mAudioState;
173 }
174
175 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700176 * Invoked when the Conference and all it's {@link Connection}s should be disconnected.
177 */
178 public void onDisconnect() {}
179
180 /**
181 * Invoked when the specified {@link Connection} should be separated from the conference call.
182 *
183 * @param connection The connection to separate.
184 */
185 public void onSeparate(Connection connection) {}
186
187 /**
Ihab Awad50e35062014-09-30 09:17:03 -0700188 * Invoked when the specified {@link Connection} should merged with the conference call.
189 *
190 * @param connection The {@code Connection} to merge.
191 */
192 public void onMerge(Connection connection) {}
193
194 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700195 * Invoked when the conference should be put on hold.
196 */
197 public void onHold() {}
198
199 /**
200 * Invoked when the conference should be moved from hold to active.
201 */
202 public void onUnhold() {}
203
204 /**
Santos Cordona4868042014-09-04 17:39:22 -0700205 * Invoked when the child calls should be merged. Only invoked if the conference contains the
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800206 * capability {@link Connection#CAPABILITY_MERGE_CONFERENCE}.
Santos Cordona4868042014-09-04 17:39:22 -0700207 */
208 public void onMerge() {}
209
210 /**
211 * Invoked when the child calls should be swapped. Only invoked if the conference contains the
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800212 * capability {@link Connection#CAPABILITY_SWAP_CONFERENCE}.
Santos Cordona4868042014-09-04 17:39:22 -0700213 */
214 public void onSwap() {}
215
216 /**
Yorke Leea0d3ca92014-09-15 19:18:13 -0700217 * Notifies this conference of a request to play a DTMF tone.
218 *
219 * @param c A DTMF character.
220 */
221 public void onPlayDtmfTone(char c) {}
222
223 /**
224 * Notifies this conference of a request to stop any currently playing DTMF tones.
225 */
226 public void onStopDtmfTone() {}
227
228 /**
229 * Notifies this conference that the {@link #getAudioState()} property has a new value.
230 *
231 * @param state The new call audio state.
232 */
233 public void onAudioStateChanged(AudioState state) {}
234
235 /**
Andrew Lee46f7f5d2014-11-06 17:00:25 -0800236 * Notifies this conference that a connection has been added to it.
237 *
238 * @param connection The newly added connection.
239 */
240 public void onConnectionAdded(Connection connection) {}
241
242 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700243 * Sets state to be on hold.
244 */
245 public final void setOnHold() {
246 setState(Connection.STATE_HOLDING);
247 }
248
249 /**
250 * Sets state to be active.
251 */
252 public final void setActive() {
253 setState(Connection.STATE_ACTIVE);
254 }
255
256 /**
257 * Sets state to disconnected.
258 *
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700259 * @param disconnectCause The reason for the disconnection, as described by
260 * {@link android.telecom.DisconnectCause}.
Santos Cordon823fd3c2014-08-07 18:35:18 -0700261 */
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700262 public final void setDisconnected(DisconnectCause disconnectCause) {
263 mDisconnectCause = disconnectCause;;
Santos Cordon823fd3c2014-08-07 18:35:18 -0700264 setState(Connection.STATE_DISCONNECTED);
265 for (Listener l : mListeners) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700266 l.onDisconnected(this, mDisconnectCause);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700267 }
268 }
269
270 /**
mike dooley1cf14ac2014-11-04 10:59:53 -0800271 * @return The {@link DisconnectCause} for this connection.
272 */
273 public final DisconnectCause getDisconnectCause() {
274 return mDisconnectCause;
275 }
276
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800277 /** @hide */
278 @Deprecated public final void setCapabilities(int connectionCapabilities) {
279 setConnectionCapabilities(connectionCapabilities);
280 }
281
mike dooley1cf14ac2014-11-04 10:59:53 -0800282 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800283 * Sets the capabilities of a conference. See {@code CAPABILITY_*} constants of class
284 * {@link Connection} for valid values.
Nancy Chen56fc25d2014-09-09 12:24:51 -0700285 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800286 * @param connectionCapabilities A bitmask of the {@code PhoneCapabilities} of the conference call.
Santos Cordon823fd3c2014-08-07 18:35:18 -0700287 */
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800288 public final void setConnectionCapabilities(int connectionCapabilities) {
289 if (connectionCapabilities != mConnectionCapabilities) {
290 mConnectionCapabilities = connectionCapabilities;
Santos Cordon823fd3c2014-08-07 18:35:18 -0700291
292 for (Listener l : mListeners) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800293 l.onConnectionCapabilitiesChanged(this, mConnectionCapabilities);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700294 }
295 }
296 }
297
298 /**
299 * Adds the specified connection as a child of this conference.
300 *
301 * @param connection The connection to add.
302 * @return True if the connection was successfully added.
303 */
Santos Cordona4868042014-09-04 17:39:22 -0700304 public final boolean addConnection(Connection connection) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700305 if (connection != null && !mChildConnections.contains(connection)) {
306 if (connection.setConference(this)) {
307 mChildConnections.add(connection);
Andrew Lee46f7f5d2014-11-06 17:00:25 -0800308 onConnectionAdded(connection);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700309 for (Listener l : mListeners) {
310 l.onConnectionAdded(this, connection);
311 }
312 return true;
313 }
314 }
315 return false;
316 }
317
318 /**
319 * Removes the specified connection as a child of this conference.
320 *
321 * @param connection The connection to remove.
Santos Cordon823fd3c2014-08-07 18:35:18 -0700322 */
Santos Cordona4868042014-09-04 17:39:22 -0700323 public final void removeConnection(Connection connection) {
Santos Cordon0159ac02014-08-21 14:28:11 -0700324 Log.d(this, "removing %s from %s", connection, mChildConnections);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700325 if (connection != null && mChildConnections.remove(connection)) {
326 connection.resetConference();
327 for (Listener l : mListeners) {
328 l.onConnectionRemoved(this, connection);
329 }
330 }
331 }
332
333 /**
Ihab Awad50e35062014-09-30 09:17:03 -0700334 * Sets the connections with which this connection can be conferenced.
335 *
336 * @param conferenceableConnections The set of connections this connection can conference with.
337 */
338 public final void setConferenceableConnections(List<Connection> conferenceableConnections) {
339 clearConferenceableList();
340 for (Connection c : conferenceableConnections) {
341 // If statement checks for duplicates in input. It makes it N^2 but we're dealing with a
342 // small amount of items here.
343 if (!mConferenceableConnections.contains(c)) {
344 c.addConnectionListener(mConnectionDeathListener);
345 mConferenceableConnections.add(c);
346 }
347 }
348 fireOnConferenceableConnectionsChanged();
349 }
350
351 private final void fireOnConferenceableConnectionsChanged() {
352 for (Listener l : mListeners) {
353 l.onConferenceableConnectionsChanged(this, getConferenceableConnections());
354 }
355 }
356
357 /**
358 * Returns the connections with which this connection can be conferenced.
359 */
360 public final List<Connection> getConferenceableConnections() {
361 return mUnmodifiableConferenceableConnections;
362 }
363
364 /**
Nancy Chenea38cca2014-09-05 16:38:49 -0700365 * Tears down the conference object and any of its current connections.
Santos Cordon823fd3c2014-08-07 18:35:18 -0700366 */
Santos Cordona4868042014-09-04 17:39:22 -0700367 public final void destroy() {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700368 Log.d(this, "destroying conference : %s", this);
369 // Tear down the children.
Santos Cordon0159ac02014-08-21 14:28:11 -0700370 for (Connection connection : mChildConnections) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700371 Log.d(this, "removing connection %s", connection);
372 removeConnection(connection);
373 }
374
375 // If not yet disconnected, set the conference call as disconnected first.
376 if (mState != Connection.STATE_DISCONNECTED) {
377 Log.d(this, "setting to disconnected");
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700378 setDisconnected(new DisconnectCause(DisconnectCause.LOCAL));
Santos Cordon823fd3c2014-08-07 18:35:18 -0700379 }
380
381 // ...and notify.
382 for (Listener l : mListeners) {
383 l.onDestroyed(this);
384 }
385 }
386
387 /**
388 * Add a listener to be notified of a state change.
389 *
390 * @param listener The new listener.
391 * @return This conference.
392 * @hide
393 */
394 public final Conference addListener(Listener listener) {
395 mListeners.add(listener);
396 return this;
397 }
398
399 /**
400 * Removes the specified listener.
401 *
402 * @param listener The listener to remove.
403 * @return This conference.
404 * @hide
405 */
406 public final Conference removeListener(Listener listener) {
407 mListeners.remove(listener);
408 return this;
409 }
410
Yorke Leea0d3ca92014-09-15 19:18:13 -0700411 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700412 * Retrieves the primary connection associated with the conference. The primary connection is
413 * the connection from which the conference will retrieve its current state.
414 *
415 * @return The primary connection.
416 */
417 public Connection getPrimaryConnection() {
418 if (mUnmodifiableChildConnections == null || mUnmodifiableChildConnections.isEmpty()) {
419 return null;
420 }
421 return mUnmodifiableChildConnections.get(0);
422 }
423
424 /**
Yorke Leea0d3ca92014-09-15 19:18:13 -0700425 * Inform this Conference that the state of its audio output has been changed externally.
426 *
427 * @param state The new audio state.
428 * @hide
429 */
430 final void setAudioState(AudioState state) {
431 Log.d(this, "setAudioState %s", state);
432 mAudioState = state;
433 onAudioStateChanged(state);
434 }
435
Santos Cordon823fd3c2014-08-07 18:35:18 -0700436 private void setState(int newState) {
437 if (newState != Connection.STATE_ACTIVE &&
438 newState != Connection.STATE_HOLDING &&
439 newState != Connection.STATE_DISCONNECTED) {
440 Log.w(this, "Unsupported state transition for Conference call.",
441 Connection.stateToString(newState));
442 return;
443 }
444
445 if (mState != newState) {
446 int oldState = mState;
447 mState = newState;
448 for (Listener l : mListeners) {
449 l.onStateChanged(this, oldState, newState);
450 }
451 }
452 }
Ihab Awad50e35062014-09-30 09:17:03 -0700453
454 private final void clearConferenceableList() {
455 for (Connection c : mConferenceableConnections) {
456 c.removeConnectionListener(mConnectionDeathListener);
457 }
458 mConferenceableConnections.clear();
459 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700460}