blob: ddaedcdbc366c1f027933410c61ce009302c69fd [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;
Rekha Kumar07366812015-03-24 16:42:31 -070020import android.telecom.Connection.VideoProvider;
Evan Charlton0e094d92014-11-08 15:49:16 -080021
Ihab Awad50e35062014-09-30 09:17:03 -070022import java.util.ArrayList;
Santos Cordon823fd3c2014-08-07 18:35:18 -070023import java.util.Collections;
Santos Cordon823fd3c2014-08-07 18:35:18 -070024import java.util.List;
Rekha Kumar07366812015-03-24 16:42:31 -070025import java.util.Locale;
Santos Cordon823fd3c2014-08-07 18:35:18 -070026import java.util.Set;
27import java.util.concurrent.CopyOnWriteArrayList;
28import java.util.concurrent.CopyOnWriteArraySet;
29
30/**
31 * Represents a conference call which can contain any number of {@link Connection} objects.
Evan Charlton0e094d92014-11-08 15:49:16 -080032 * @hide
Santos Cordon823fd3c2014-08-07 18:35:18 -070033 */
Evan Charlton0e094d92014-11-08 15:49:16 -080034@SystemApi
Tyler Gunn6d76ca02014-11-17 15:49:51 -080035public abstract class Conference implements IConferenceable {
Santos Cordon823fd3c2014-08-07 18:35:18 -070036
Tyler Gunncd5d33c2015-01-12 09:02:01 -080037 /**
38 * Used to indicate that the conference connection time is not specified. If not specified,
39 * Telecom will set the connect time.
40 */
41 public static long CONNECT_TIME_NOT_SPECIFIED = 0;
42
Santos Cordon823fd3c2014-08-07 18:35:18 -070043 /** @hide */
44 public abstract static class Listener {
45 public void onStateChanged(Conference conference, int oldState, int newState) {}
Andrew Lee7f3d41f2014-09-11 17:33:16 -070046 public void onDisconnected(Conference conference, DisconnectCause disconnectCause) {}
Santos Cordon823fd3c2014-08-07 18:35:18 -070047 public void onConnectionAdded(Conference conference, Connection connection) {}
48 public void onConnectionRemoved(Conference conference, Connection connection) {}
Ihab Awad50e35062014-09-30 09:17:03 -070049 public void onConferenceableConnectionsChanged(
50 Conference conference, List<Connection> conferenceableConnections) {}
Santos Cordon823fd3c2014-08-07 18:35:18 -070051 public void onDestroyed(Conference conference) {}
Ihab Awad5c9c86e2014-11-12 13:41:16 -080052 public void onConnectionCapabilitiesChanged(
53 Conference conference, int connectionCapabilities) {}
Rekha Kumar07366812015-03-24 16:42:31 -070054 public void onVideoStateChanged(Conference c, int videoState) { }
55 public void onVideoProviderChanged(Conference c, Connection.VideoProvider videoProvider) {}
Santos Cordon823fd3c2014-08-07 18:35:18 -070056 }
57
58 private final Set<Listener> mListeners = new CopyOnWriteArraySet<>();
59 private final List<Connection> mChildConnections = new CopyOnWriteArrayList<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -070060 private final List<Connection> mUnmodifiableChildConnections =
Santos Cordon823fd3c2014-08-07 18:35:18 -070061 Collections.unmodifiableList(mChildConnections);
Ihab Awad50e35062014-09-30 09:17:03 -070062 private final List<Connection> mConferenceableConnections = new ArrayList<>();
63 private final List<Connection> mUnmodifiableConferenceableConnections =
64 Collections.unmodifiableList(mConferenceableConnections);
Santos Cordon823fd3c2014-08-07 18:35:18 -070065
Anju Mathapatif9c4d842014-12-17 11:11:21 -080066 protected PhoneAccountHandle mPhoneAccount;
Yorke Leea0d3ca92014-09-15 19:18:13 -070067 private AudioState mAudioState;
Santos Cordon823fd3c2014-08-07 18:35:18 -070068 private int mState = Connection.STATE_NEW;
Andrew Lee7f3d41f2014-09-11 17:33:16 -070069 private DisconnectCause mDisconnectCause;
Ihab Awad5c9c86e2014-11-12 13:41:16 -080070 private int mConnectionCapabilities;
Santos Cordon823fd3c2014-08-07 18:35:18 -070071 private String mDisconnectMessage;
Tyler Gunncd5d33c2015-01-12 09:02:01 -080072 private long mConnectTimeMillis = CONNECT_TIME_NOT_SPECIFIED;
Santos Cordon823fd3c2014-08-07 18:35:18 -070073
Ihab Awad50e35062014-09-30 09:17:03 -070074 private final Connection.Listener mConnectionDeathListener = new Connection.Listener() {
75 @Override
76 public void onDestroyed(Connection c) {
77 if (mConferenceableConnections.remove(c)) {
78 fireOnConferenceableConnectionsChanged();
79 }
80 }
81 };
82
Nancy Chen56fc25d2014-09-09 12:24:51 -070083 /**
84 * Constructs a new Conference with a mandatory {@link PhoneAccountHandle}
85 *
86 * @param phoneAccount The {@code PhoneAccountHandle} associated with the conference.
87 */
Santos Cordon823fd3c2014-08-07 18:35:18 -070088 public Conference(PhoneAccountHandle phoneAccount) {
89 mPhoneAccount = phoneAccount;
90 }
91
Nancy Chen56fc25d2014-09-09 12:24:51 -070092 /**
93 * Returns the {@link PhoneAccountHandle} the conference call is being placed through.
94 *
95 * @return A {@code PhoneAccountHandle} object representing the PhoneAccount of the conference.
96 */
Nancy Chenea38cca2014-09-05 16:38:49 -070097 public final PhoneAccountHandle getPhoneAccountHandle() {
Santos Cordon823fd3c2014-08-07 18:35:18 -070098 return mPhoneAccount;
99 }
100
Nancy Chen56fc25d2014-09-09 12:24:51 -0700101 /**
102 * Returns the list of connections currently associated with the conference call.
103 *
104 * @return A list of {@code Connection} objects which represent the children of the conference.
105 */
Santos Cordon823fd3c2014-08-07 18:35:18 -0700106 public final List<Connection> getConnections() {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700107 return mUnmodifiableChildConnections;
Santos Cordon823fd3c2014-08-07 18:35:18 -0700108 }
109
Nancy Chen56fc25d2014-09-09 12:24:51 -0700110 /**
111 * Gets the state of the conference call. See {@link Connection} for valid values.
112 *
113 * @return A constant representing the state the conference call is currently in.
114 */
Santos Cordon823fd3c2014-08-07 18:35:18 -0700115 public final int getState() {
116 return mState;
117 }
118
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800119 /** @hide */
120 @Deprecated public final int getCapabilities() {
121 return getConnectionCapabilities();
122 }
123
Nancy Chen56fc25d2014-09-09 12:24:51 -0700124 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800125 * Returns the capabilities of a conference. See {@code CAPABILITY_*} constants in class
126 * {@link Connection} for valid values.
Nancy Chen56fc25d2014-09-09 12:24:51 -0700127 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800128 * @return A bitmask of the capabilities of the conference call.
Nancy Chen56fc25d2014-09-09 12:24:51 -0700129 */
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800130 public final int getConnectionCapabilities() {
131 return mConnectionCapabilities;
132 }
133
134 /**
135 * Whether the given capabilities support the specified capability.
136 *
137 * @param capabilities A capability bit field.
138 * @param capability The capability to check capabilities for.
139 * @return Whether the specified capability is supported.
140 * @hide
141 */
142 public static boolean can(int capabilities, int capability) {
143 return (capabilities & capability) != 0;
144 }
145
146 /**
147 * Whether the capabilities of this {@code Connection} supports the specified capability.
148 *
149 * @param capability The capability to check capabilities for.
150 * @return Whether the specified capability is supported.
151 * @hide
152 */
153 public boolean can(int capability) {
154 return can(mConnectionCapabilities, capability);
155 }
156
157 /**
158 * Removes the specified capability from the set of capabilities of this {@code Conference}.
159 *
160 * @param capability The capability to remove from the set.
161 * @hide
162 */
163 public void removeCapability(int capability) {
164 mConnectionCapabilities &= ~capability;
165 }
166
167 /**
168 * Adds the specified capability to the set of capabilities of this {@code Conference}.
169 *
170 * @param capability The capability to add to the set.
171 * @hide
172 */
173 public void addCapability(int capability) {
174 mConnectionCapabilities |= capability;
Santos Cordon823fd3c2014-08-07 18:35:18 -0700175 }
176
177 /**
Yorke Leea0d3ca92014-09-15 19:18:13 -0700178 * @return The audio state of the conference, describing how its audio is currently
179 * being routed by the system. This is {@code null} if this Conference
180 * does not directly know about its audio state.
181 */
182 public final AudioState getAudioState() {
183 return mAudioState;
184 }
185
186 /**
Rekha Kumar07366812015-03-24 16:42:31 -0700187 * Returns VideoProvider of the primary call. This can be null.
188 * @hide
189 */
190 public VideoProvider getVideoProvider() {
191 return null;
192 }
193
194 /**
195 * Returns video state of the primary call.
196 * @hide
197 */
198 public int getVideoState() {
199 return VideoProfile.VideoState.AUDIO_ONLY;
200 }
201
202 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700203 * Invoked when the Conference and all it's {@link Connection}s should be disconnected.
204 */
205 public void onDisconnect() {}
206
207 /**
208 * Invoked when the specified {@link Connection} should be separated from the conference call.
209 *
210 * @param connection The connection to separate.
211 */
212 public void onSeparate(Connection connection) {}
213
214 /**
Ihab Awad50e35062014-09-30 09:17:03 -0700215 * Invoked when the specified {@link Connection} should merged with the conference call.
216 *
217 * @param connection The {@code Connection} to merge.
218 */
219 public void onMerge(Connection connection) {}
220
221 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700222 * Invoked when the conference should be put on hold.
223 */
224 public void onHold() {}
225
226 /**
227 * Invoked when the conference should be moved from hold to active.
228 */
229 public void onUnhold() {}
230
231 /**
Santos Cordona4868042014-09-04 17:39:22 -0700232 * Invoked when the child calls should be merged. Only invoked if the conference contains the
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800233 * capability {@link Connection#CAPABILITY_MERGE_CONFERENCE}.
Santos Cordona4868042014-09-04 17:39:22 -0700234 */
235 public void onMerge() {}
236
237 /**
238 * Invoked when the child calls should be swapped. Only invoked if the conference contains the
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800239 * capability {@link Connection#CAPABILITY_SWAP_CONFERENCE}.
Santos Cordona4868042014-09-04 17:39:22 -0700240 */
241 public void onSwap() {}
242
243 /**
Yorke Leea0d3ca92014-09-15 19:18:13 -0700244 * Notifies this conference of a request to play a DTMF tone.
245 *
246 * @param c A DTMF character.
247 */
248 public void onPlayDtmfTone(char c) {}
249
250 /**
251 * Notifies this conference of a request to stop any currently playing DTMF tones.
252 */
253 public void onStopDtmfTone() {}
254
255 /**
256 * Notifies this conference that the {@link #getAudioState()} property has a new value.
257 *
258 * @param state The new call audio state.
259 */
260 public void onAudioStateChanged(AudioState state) {}
261
262 /**
Andrew Lee46f7f5d2014-11-06 17:00:25 -0800263 * Notifies this conference that a connection has been added to it.
264 *
265 * @param connection The newly added connection.
266 */
267 public void onConnectionAdded(Connection connection) {}
268
269 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700270 * Sets state to be on hold.
271 */
272 public final void setOnHold() {
273 setState(Connection.STATE_HOLDING);
274 }
275
276 /**
277 * Sets state to be active.
278 */
279 public final void setActive() {
280 setState(Connection.STATE_ACTIVE);
281 }
282
283 /**
284 * Sets state to disconnected.
285 *
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700286 * @param disconnectCause The reason for the disconnection, as described by
287 * {@link android.telecom.DisconnectCause}.
Santos Cordon823fd3c2014-08-07 18:35:18 -0700288 */
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700289 public final void setDisconnected(DisconnectCause disconnectCause) {
290 mDisconnectCause = disconnectCause;;
Santos Cordon823fd3c2014-08-07 18:35:18 -0700291 setState(Connection.STATE_DISCONNECTED);
292 for (Listener l : mListeners) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700293 l.onDisconnected(this, mDisconnectCause);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700294 }
295 }
296
297 /**
mike dooley1cf14ac2014-11-04 10:59:53 -0800298 * @return The {@link DisconnectCause} for this connection.
299 */
300 public final DisconnectCause getDisconnectCause() {
301 return mDisconnectCause;
302 }
303
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800304 /** @hide */
305 @Deprecated public final void setCapabilities(int connectionCapabilities) {
306 setConnectionCapabilities(connectionCapabilities);
307 }
308
mike dooley1cf14ac2014-11-04 10:59:53 -0800309 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800310 * Sets the capabilities of a conference. See {@code CAPABILITY_*} constants of class
311 * {@link Connection} for valid values.
Nancy Chen56fc25d2014-09-09 12:24:51 -0700312 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800313 * @param connectionCapabilities A bitmask of the {@code PhoneCapabilities} of the conference call.
Santos Cordon823fd3c2014-08-07 18:35:18 -0700314 */
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800315 public final void setConnectionCapabilities(int connectionCapabilities) {
316 if (connectionCapabilities != mConnectionCapabilities) {
317 mConnectionCapabilities = connectionCapabilities;
Santos Cordon823fd3c2014-08-07 18:35:18 -0700318
319 for (Listener l : mListeners) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800320 l.onConnectionCapabilitiesChanged(this, mConnectionCapabilities);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700321 }
322 }
323 }
324
325 /**
326 * Adds the specified connection as a child of this conference.
327 *
328 * @param connection The connection to add.
329 * @return True if the connection was successfully added.
330 */
Santos Cordona4868042014-09-04 17:39:22 -0700331 public final boolean addConnection(Connection connection) {
Rekha Kumar07366812015-03-24 16:42:31 -0700332 Log.d(this, "Connection=%s, connection=", connection);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700333 if (connection != null && !mChildConnections.contains(connection)) {
334 if (connection.setConference(this)) {
335 mChildConnections.add(connection);
Andrew Lee46f7f5d2014-11-06 17:00:25 -0800336 onConnectionAdded(connection);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700337 for (Listener l : mListeners) {
338 l.onConnectionAdded(this, connection);
339 }
340 return true;
341 }
342 }
343 return false;
344 }
345
346 /**
347 * Removes the specified connection as a child of this conference.
348 *
349 * @param connection The connection to remove.
Santos Cordon823fd3c2014-08-07 18:35:18 -0700350 */
Santos Cordona4868042014-09-04 17:39:22 -0700351 public final void removeConnection(Connection connection) {
Santos Cordon0159ac02014-08-21 14:28:11 -0700352 Log.d(this, "removing %s from %s", connection, mChildConnections);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700353 if (connection != null && mChildConnections.remove(connection)) {
354 connection.resetConference();
355 for (Listener l : mListeners) {
356 l.onConnectionRemoved(this, connection);
357 }
358 }
359 }
360
361 /**
Ihab Awad50e35062014-09-30 09:17:03 -0700362 * Sets the connections with which this connection can be conferenced.
363 *
364 * @param conferenceableConnections The set of connections this connection can conference with.
365 */
366 public final void setConferenceableConnections(List<Connection> conferenceableConnections) {
367 clearConferenceableList();
368 for (Connection c : conferenceableConnections) {
369 // If statement checks for duplicates in input. It makes it N^2 but we're dealing with a
370 // small amount of items here.
371 if (!mConferenceableConnections.contains(c)) {
372 c.addConnectionListener(mConnectionDeathListener);
373 mConferenceableConnections.add(c);
374 }
375 }
376 fireOnConferenceableConnectionsChanged();
377 }
378
Rekha Kumar07366812015-03-24 16:42:31 -0700379 /**
380 * Set the video state for the conference.
381 * Valid values: {@link VideoProfile.VideoState#AUDIO_ONLY},
382 * {@link VideoProfile.VideoState#BIDIRECTIONAL},
383 * {@link VideoProfile.VideoState#TX_ENABLED},
384 * {@link VideoProfile.VideoState#RX_ENABLED}.
385 *
386 * @param videoState The new video state.
387 * @hide
388 */
389 public final void setVideoState(Connection c, int videoState) {
390 Log.d(this, "setVideoState Conference: %s Connection: %s VideoState: %s",
391 this, c, videoState);
392 for (Listener l : mListeners) {
393 l.onVideoStateChanged(this, videoState);
394 }
395 }
396
397 /**
398 * Sets the video connection provider.
399 *
400 * @param videoProvider The video provider.
401 * @hide
402 */
403 public final void setVideoProvider(Connection c, Connection.VideoProvider videoProvider) {
404 Log.d(this, "setVideoProvider Conference: %s Connection: %s VideoState: %s",
405 this, c, videoProvider);
406 for (Listener l : mListeners) {
407 l.onVideoProviderChanged(this, videoProvider);
408 }
409 }
410
Ihab Awad50e35062014-09-30 09:17:03 -0700411 private final void fireOnConferenceableConnectionsChanged() {
412 for (Listener l : mListeners) {
413 l.onConferenceableConnectionsChanged(this, getConferenceableConnections());
414 }
415 }
416
417 /**
418 * Returns the connections with which this connection can be conferenced.
419 */
420 public final List<Connection> getConferenceableConnections() {
421 return mUnmodifiableConferenceableConnections;
422 }
423
424 /**
Nancy Chenea38cca2014-09-05 16:38:49 -0700425 * Tears down the conference object and any of its current connections.
Santos Cordon823fd3c2014-08-07 18:35:18 -0700426 */
Santos Cordona4868042014-09-04 17:39:22 -0700427 public final void destroy() {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700428 Log.d(this, "destroying conference : %s", this);
429 // Tear down the children.
Santos Cordon0159ac02014-08-21 14:28:11 -0700430 for (Connection connection : mChildConnections) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700431 Log.d(this, "removing connection %s", connection);
432 removeConnection(connection);
433 }
434
435 // If not yet disconnected, set the conference call as disconnected first.
436 if (mState != Connection.STATE_DISCONNECTED) {
437 Log.d(this, "setting to disconnected");
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700438 setDisconnected(new DisconnectCause(DisconnectCause.LOCAL));
Santos Cordon823fd3c2014-08-07 18:35:18 -0700439 }
440
441 // ...and notify.
442 for (Listener l : mListeners) {
443 l.onDestroyed(this);
444 }
445 }
446
447 /**
448 * Add a listener to be notified of a state change.
449 *
450 * @param listener The new listener.
451 * @return This conference.
452 * @hide
453 */
454 public final Conference addListener(Listener listener) {
455 mListeners.add(listener);
456 return this;
457 }
458
459 /**
460 * Removes the specified listener.
461 *
462 * @param listener The listener to remove.
463 * @return This conference.
464 * @hide
465 */
466 public final Conference removeListener(Listener listener) {
467 mListeners.remove(listener);
468 return this;
469 }
470
Yorke Leea0d3ca92014-09-15 19:18:13 -0700471 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700472 * Retrieves the primary connection associated with the conference. The primary connection is
473 * the connection from which the conference will retrieve its current state.
474 *
475 * @return The primary connection.
476 */
477 public Connection getPrimaryConnection() {
478 if (mUnmodifiableChildConnections == null || mUnmodifiableChildConnections.isEmpty()) {
479 return null;
480 }
481 return mUnmodifiableChildConnections.get(0);
482 }
483
484 /**
Tyler Gunncd5d33c2015-01-12 09:02:01 -0800485 * Sets the connect time of the {@code Conference}.
486 *
487 * @param connectTimeMillis The connection time, in milliseconds.
488 */
489 public void setConnectTimeMillis(long connectTimeMillis) {
490 mConnectTimeMillis = connectTimeMillis;
491 }
492
493 /**
494 * Retrieves the connect time of the {@code Conference}, if specified. A value of
495 * {@link #CONNECT_TIME_NOT_SPECIFIED} indicates that Telecom should determine the start time
496 * of the conference.
497 *
498 * @return The time the {@code Conference} has been connected.
499 */
500 public long getConnectTimeMillis() {
501 return mConnectTimeMillis;
502 }
503
504 /**
Yorke Leea0d3ca92014-09-15 19:18:13 -0700505 * Inform this Conference that the state of its audio output has been changed externally.
506 *
507 * @param state The new audio state.
508 * @hide
509 */
510 final void setAudioState(AudioState state) {
511 Log.d(this, "setAudioState %s", state);
512 mAudioState = state;
513 onAudioStateChanged(state);
514 }
515
Santos Cordon823fd3c2014-08-07 18:35:18 -0700516 private void setState(int newState) {
517 if (newState != Connection.STATE_ACTIVE &&
518 newState != Connection.STATE_HOLDING &&
519 newState != Connection.STATE_DISCONNECTED) {
520 Log.w(this, "Unsupported state transition for Conference call.",
521 Connection.stateToString(newState));
522 return;
523 }
524
525 if (mState != newState) {
526 int oldState = mState;
527 mState = newState;
528 for (Listener l : mListeners) {
529 l.onStateChanged(this, oldState, newState);
530 }
531 }
532 }
Ihab Awad50e35062014-09-30 09:17:03 -0700533
534 private final void clearConferenceableList() {
535 for (Connection c : mConferenceableConnections) {
536 c.removeConnectionListener(mConnectionDeathListener);
537 }
538 mConferenceableConnections.clear();
539 }
Rekha Kumar07366812015-03-24 16:42:31 -0700540
541 @Override
542 public String toString() {
543 return String.format(Locale.US,
544 "[State: %s,Capabilites: %s, VideoState: %s, VideoProvider: %s, ThisObject %s]",
545 Connection.stateToString(mState),
546 Call.Details.capabilitiesToString(mConnectionCapabilities),
547 getVideoState(),
548 getVideoProvider(),
549 super.toString());
550 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700551}