blob: e79d8418d5d08d3ed86c4c177ef0d4d6c246a3c9 [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
Tyler Gunndee56a82016-03-23 16:06:34 -070019import android.annotation.NonNull;
Santos Cordon6b7f9552015-05-27 17:21:45 -070020import android.annotation.Nullable;
Santos Cordon5d2e4f22015-05-12 12:32:51 -070021import android.annotation.SystemApi;
Santos Cordon6b7f9552015-05-27 17:21:45 -070022import android.os.Bundle;
Tyler Gunn3fa819c2017-08-04 09:27:26 -070023import android.os.SystemClock;
Rekha Kumar07366812015-03-24 16:42:31 -070024import android.telecom.Connection.VideoProvider;
Tyler Gunndee56a82016-03-23 16:06:34 -070025import android.util.ArraySet;
Evan Charlton0e094d92014-11-08 15:49:16 -080026
Ihab Awad50e35062014-09-30 09:17:03 -070027import java.util.ArrayList;
Tyler Gunn071be6f2016-05-10 14:52:33 -070028import java.util.Arrays;
Santos Cordon823fd3c2014-08-07 18:35:18 -070029import java.util.Collections;
Santos Cordon823fd3c2014-08-07 18:35:18 -070030import java.util.List;
Rekha Kumar07366812015-03-24 16:42:31 -070031import java.util.Locale;
Tyler Gunnec5b6e32016-12-01 19:40:30 -080032import java.util.Objects;
Santos Cordon823fd3c2014-08-07 18:35:18 -070033import java.util.Set;
34import java.util.concurrent.CopyOnWriteArrayList;
35import java.util.concurrent.CopyOnWriteArraySet;
36
37/**
38 * Represents a conference call which can contain any number of {@link Connection} objects.
39 */
Yorke Leeabfcfdc2015-05-13 18:55:18 -070040public abstract class Conference extends Conferenceable {
Santos Cordon823fd3c2014-08-07 18:35:18 -070041
Tyler Gunncd5d33c2015-01-12 09:02:01 -080042 /**
43 * Used to indicate that the conference connection time is not specified. If not specified,
44 * Telecom will set the connect time.
45 */
Jay Shrauner164a0ac2015-04-14 18:16:10 -070046 public static final long CONNECT_TIME_NOT_SPECIFIED = 0;
Tyler Gunncd5d33c2015-01-12 09:02:01 -080047
Santos Cordon823fd3c2014-08-07 18:35:18 -070048 /** @hide */
49 public abstract static class Listener {
50 public void onStateChanged(Conference conference, int oldState, int newState) {}
Andrew Lee7f3d41f2014-09-11 17:33:16 -070051 public void onDisconnected(Conference conference, DisconnectCause disconnectCause) {}
Santos Cordon823fd3c2014-08-07 18:35:18 -070052 public void onConnectionAdded(Conference conference, Connection connection) {}
53 public void onConnectionRemoved(Conference conference, Connection connection) {}
Ihab Awad50e35062014-09-30 09:17:03 -070054 public void onConferenceableConnectionsChanged(
55 Conference conference, List<Connection> conferenceableConnections) {}
Santos Cordon823fd3c2014-08-07 18:35:18 -070056 public void onDestroyed(Conference conference) {}
Ihab Awad5c9c86e2014-11-12 13:41:16 -080057 public void onConnectionCapabilitiesChanged(
58 Conference conference, int connectionCapabilities) {}
Tyler Gunn720c6642016-03-22 09:02:47 -070059 public void onConnectionPropertiesChanged(
60 Conference conference, int connectionProperties) {}
Rekha Kumar07366812015-03-24 16:42:31 -070061 public void onVideoStateChanged(Conference c, int videoState) { }
62 public void onVideoProviderChanged(Conference c, Connection.VideoProvider videoProvider) {}
Andrew Leeedc625f2015-04-14 13:38:12 -070063 public void onStatusHintsChanged(Conference conference, StatusHints statusHints) {}
Tyler Gunndee56a82016-03-23 16:06:34 -070064 public void onExtrasChanged(Conference c, Bundle extras) {}
65 public void onExtrasRemoved(Conference c, List<String> keys) {}
Santos Cordon823fd3c2014-08-07 18:35:18 -070066 }
67
68 private final Set<Listener> mListeners = new CopyOnWriteArraySet<>();
69 private final List<Connection> mChildConnections = new CopyOnWriteArrayList<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -070070 private final List<Connection> mUnmodifiableChildConnections =
Santos Cordon823fd3c2014-08-07 18:35:18 -070071 Collections.unmodifiableList(mChildConnections);
Ihab Awad50e35062014-09-30 09:17:03 -070072 private final List<Connection> mConferenceableConnections = new ArrayList<>();
73 private final List<Connection> mUnmodifiableConferenceableConnections =
74 Collections.unmodifiableList(mConferenceableConnections);
Santos Cordon823fd3c2014-08-07 18:35:18 -070075
Jack Yu67140302015-12-10 12:27:58 -080076 private String mTelecomCallId;
Jay Shrauner164a0ac2015-04-14 18:16:10 -070077 private PhoneAccountHandle mPhoneAccount;
Yorke Lee4af59352015-05-13 14:14:54 -070078 private CallAudioState mCallAudioState;
Santos Cordon823fd3c2014-08-07 18:35:18 -070079 private int mState = Connection.STATE_NEW;
Andrew Lee7f3d41f2014-09-11 17:33:16 -070080 private DisconnectCause mDisconnectCause;
Ihab Awad5c9c86e2014-11-12 13:41:16 -080081 private int mConnectionCapabilities;
Tyler Gunn720c6642016-03-22 09:02:47 -070082 private int mConnectionProperties;
Santos Cordon823fd3c2014-08-07 18:35:18 -070083 private String mDisconnectMessage;
Tyler Gunncd5d33c2015-01-12 09:02:01 -080084 private long mConnectTimeMillis = CONNECT_TIME_NOT_SPECIFIED;
Tyler Gunn3fa819c2017-08-04 09:27:26 -070085 private long mConnectElapsedTimeMillis = CONNECT_TIME_NOT_SPECIFIED;
Andrew Leeedc625f2015-04-14 13:38:12 -070086 private StatusHints mStatusHints;
Santos Cordon6b7f9552015-05-27 17:21:45 -070087 private Bundle mExtras;
Tyler Gunndee56a82016-03-23 16:06:34 -070088 private Set<String> mPreviousExtraKeys;
Brad Ebinger4fa6a012016-06-14 17:04:01 -070089 private final Object mExtrasLock = new Object();
Santos Cordon823fd3c2014-08-07 18:35:18 -070090
Ihab Awad50e35062014-09-30 09:17:03 -070091 private final Connection.Listener mConnectionDeathListener = new Connection.Listener() {
92 @Override
93 public void onDestroyed(Connection c) {
94 if (mConferenceableConnections.remove(c)) {
95 fireOnConferenceableConnectionsChanged();
96 }
97 }
98 };
99
Nancy Chen56fc25d2014-09-09 12:24:51 -0700100 /**
101 * Constructs a new Conference with a mandatory {@link PhoneAccountHandle}
102 *
103 * @param phoneAccount The {@code PhoneAccountHandle} associated with the conference.
104 */
Santos Cordon823fd3c2014-08-07 18:35:18 -0700105 public Conference(PhoneAccountHandle phoneAccount) {
106 mPhoneAccount = phoneAccount;
107 }
108
Nancy Chen56fc25d2014-09-09 12:24:51 -0700109 /**
Jack Yu67140302015-12-10 12:27:58 -0800110 * Returns the telecom internal call ID associated with this conference.
111 *
112 * @return The telecom call ID.
113 * @hide
114 */
115 public final String getTelecomCallId() {
116 return mTelecomCallId;
117 }
118
119 /**
120 * Sets the telecom internal call ID associated with this conference.
121 *
122 * @param telecomCallId The telecom call ID.
123 * @hide
124 */
125 public final void setTelecomCallId(String telecomCallId) {
126 mTelecomCallId = telecomCallId;
127 }
128
129 /**
Nancy Chen56fc25d2014-09-09 12:24:51 -0700130 * Returns the {@link PhoneAccountHandle} the conference call is being placed through.
131 *
132 * @return A {@code PhoneAccountHandle} object representing the PhoneAccount of the conference.
133 */
Nancy Chenea38cca2014-09-05 16:38:49 -0700134 public final PhoneAccountHandle getPhoneAccountHandle() {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700135 return mPhoneAccount;
136 }
137
Nancy Chen56fc25d2014-09-09 12:24:51 -0700138 /**
139 * Returns the list of connections currently associated with the conference call.
140 *
141 * @return A list of {@code Connection} objects which represent the children of the conference.
142 */
Santos Cordon823fd3c2014-08-07 18:35:18 -0700143 public final List<Connection> getConnections() {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700144 return mUnmodifiableChildConnections;
Santos Cordon823fd3c2014-08-07 18:35:18 -0700145 }
146
Nancy Chen56fc25d2014-09-09 12:24:51 -0700147 /**
148 * Gets the state of the conference call. See {@link Connection} for valid values.
149 *
150 * @return A constant representing the state the conference call is currently in.
151 */
Santos Cordon823fd3c2014-08-07 18:35:18 -0700152 public final int getState() {
153 return mState;
154 }
155
Nancy Chen56fc25d2014-09-09 12:24:51 -0700156 /**
Santos Cordon5d2e4f22015-05-12 12:32:51 -0700157 * Returns the capabilities of the conference. See {@code CAPABILITY_*} constants in class
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800158 * {@link Connection} for valid values.
Nancy Chen56fc25d2014-09-09 12:24:51 -0700159 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800160 * @return A bitmask of the capabilities of the conference call.
Nancy Chen56fc25d2014-09-09 12:24:51 -0700161 */
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800162 public final int getConnectionCapabilities() {
163 return mConnectionCapabilities;
164 }
165
166 /**
Tyler Gunn720c6642016-03-22 09:02:47 -0700167 * Returns the properties of the conference. See {@code PROPERTY_*} constants in class
168 * {@link Connection} for valid values.
169 *
170 * @return A bitmask of the properties of the conference call.
171 */
172 public final int getConnectionProperties() {
173 return mConnectionProperties;
174 }
175
176 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800177 * Whether the given capabilities support the specified capability.
178 *
179 * @param capabilities A capability bit field.
180 * @param capability The capability to check capabilities for.
181 * @return Whether the specified capability is supported.
182 * @hide
183 */
184 public static boolean can(int capabilities, int capability) {
185 return (capabilities & capability) != 0;
186 }
187
188 /**
189 * Whether the capabilities of this {@code Connection} supports the specified capability.
190 *
191 * @param capability The capability to check capabilities for.
192 * @return Whether the specified capability is supported.
193 * @hide
194 */
195 public boolean can(int capability) {
196 return can(mConnectionCapabilities, capability);
197 }
198
199 /**
200 * Removes the specified capability from the set of capabilities of this {@code Conference}.
201 *
202 * @param capability The capability to remove from the set.
203 * @hide
204 */
205 public void removeCapability(int capability) {
Omkar Kolangadea0f46a92015-03-23 17:51:16 -0700206 int newCapabilities = mConnectionCapabilities;
207 newCapabilities &= ~capability;
208
209 setConnectionCapabilities(newCapabilities);
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800210 }
211
212 /**
213 * Adds the specified capability to the set of capabilities of this {@code Conference}.
214 *
215 * @param capability The capability to add to the set.
216 * @hide
217 */
218 public void addCapability(int capability) {
Omkar Kolangadea0f46a92015-03-23 17:51:16 -0700219 int newCapabilities = mConnectionCapabilities;
220 newCapabilities |= capability;
221
222 setConnectionCapabilities(newCapabilities);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700223 }
224
225 /**
Yorke Leea0d3ca92014-09-15 19:18:13 -0700226 * @return The audio state of the conference, describing how its audio is currently
227 * being routed by the system. This is {@code null} if this Conference
228 * does not directly know about its audio state.
Yorke Lee4af59352015-05-13 14:14:54 -0700229 * @deprecated Use {@link #getCallAudioState()} instead.
230 * @hide
Yorke Leea0d3ca92014-09-15 19:18:13 -0700231 */
Yorke Lee4af59352015-05-13 14:14:54 -0700232 @Deprecated
233 @SystemApi
Yorke Leea0d3ca92014-09-15 19:18:13 -0700234 public final AudioState getAudioState() {
Yorke Lee4af59352015-05-13 14:14:54 -0700235 return new AudioState(mCallAudioState);
236 }
237
238 /**
239 * @return The audio state of the conference, describing how its audio is currently
240 * being routed by the system. This is {@code null} if this Conference
241 * does not directly know about its audio state.
242 */
243 public final CallAudioState getCallAudioState() {
244 return mCallAudioState;
Yorke Leea0d3ca92014-09-15 19:18:13 -0700245 }
246
247 /**
Rekha Kumar07366812015-03-24 16:42:31 -0700248 * Returns VideoProvider of the primary call. This can be null.
Rekha Kumar07366812015-03-24 16:42:31 -0700249 */
250 public VideoProvider getVideoProvider() {
251 return null;
252 }
253
254 /**
255 * Returns video state of the primary call.
Rekha Kumar07366812015-03-24 16:42:31 -0700256 */
257 public int getVideoState() {
Tyler Gunn87b73f32015-06-03 10:09:59 -0700258 return VideoProfile.STATE_AUDIO_ONLY;
Rekha Kumar07366812015-03-24 16:42:31 -0700259 }
260
261 /**
Tyler Gunn9c0eb0b2016-06-29 11:23:25 -0700262 * Notifies the {@link Conference} when the Conference and all it's {@link Connection}s should
263 * be disconnected.
Santos Cordon823fd3c2014-08-07 18:35:18 -0700264 */
265 public void onDisconnect() {}
266
267 /**
Tyler Gunn9c0eb0b2016-06-29 11:23:25 -0700268 * Notifies the {@link Conference} when the specified {@link Connection} should be separated
269 * from the conference call.
Santos Cordon823fd3c2014-08-07 18:35:18 -0700270 *
271 * @param connection The connection to separate.
272 */
273 public void onSeparate(Connection connection) {}
274
275 /**
Tyler Gunn9c0eb0b2016-06-29 11:23:25 -0700276 * Notifies the {@link Conference} when the specified {@link Connection} should merged with the
277 * conference call.
Ihab Awad50e35062014-09-30 09:17:03 -0700278 *
279 * @param connection The {@code Connection} to merge.
280 */
281 public void onMerge(Connection connection) {}
282
283 /**
Tyler Gunn9c0eb0b2016-06-29 11:23:25 -0700284 * Notifies the {@link Conference} when it should be put on hold.
Santos Cordon823fd3c2014-08-07 18:35:18 -0700285 */
286 public void onHold() {}
287
288 /**
Tyler Gunn9c0eb0b2016-06-29 11:23:25 -0700289 * Notifies the {@link Conference} when it should be moved from a held to active state.
Santos Cordon823fd3c2014-08-07 18:35:18 -0700290 */
291 public void onUnhold() {}
292
293 /**
Tyler Gunn9c0eb0b2016-06-29 11:23:25 -0700294 * Notifies the {@link Conference} when the child calls should be merged. Only invoked if the
295 * conference contains the capability {@link Connection#CAPABILITY_MERGE_CONFERENCE}.
Santos Cordona4868042014-09-04 17:39:22 -0700296 */
297 public void onMerge() {}
298
299 /**
Tyler Gunn9c0eb0b2016-06-29 11:23:25 -0700300 * Notifies the {@link Conference} when the child calls should be swapped. Only invoked if the
301 * conference contains the capability {@link Connection#CAPABILITY_SWAP_CONFERENCE}.
Santos Cordona4868042014-09-04 17:39:22 -0700302 */
303 public void onSwap() {}
304
305 /**
Tyler Gunn9c0eb0b2016-06-29 11:23:25 -0700306 * Notifies the {@link Conference} of a request to play a DTMF tone.
Yorke Leea0d3ca92014-09-15 19:18:13 -0700307 *
308 * @param c A DTMF character.
309 */
310 public void onPlayDtmfTone(char c) {}
311
312 /**
Tyler Gunn9c0eb0b2016-06-29 11:23:25 -0700313 * Notifies the {@link Conference} of a request to stop any currently playing DTMF tones.
Yorke Leea0d3ca92014-09-15 19:18:13 -0700314 */
315 public void onStopDtmfTone() {}
316
317 /**
Tyler Gunn9c0eb0b2016-06-29 11:23:25 -0700318 * Notifies the {@link Conference} that the {@link #getAudioState()} property has a new value.
Yorke Leea0d3ca92014-09-15 19:18:13 -0700319 *
320 * @param state The new call audio state.
Yorke Lee4af59352015-05-13 14:14:54 -0700321 * @deprecated Use {@link #onCallAudioStateChanged(CallAudioState)} instead.
322 * @hide
Yorke Leea0d3ca92014-09-15 19:18:13 -0700323 */
Yorke Lee4af59352015-05-13 14:14:54 -0700324 @SystemApi
325 @Deprecated
Yorke Leea0d3ca92014-09-15 19:18:13 -0700326 public void onAudioStateChanged(AudioState state) {}
327
328 /**
Tyler Gunn9c0eb0b2016-06-29 11:23:25 -0700329 * Notifies the {@link Conference} that the {@link #getCallAudioState()} property has a new
330 * value.
Yorke Lee4af59352015-05-13 14:14:54 -0700331 *
332 * @param state The new call audio state.
333 */
334 public void onCallAudioStateChanged(CallAudioState state) {}
335
336 /**
Tyler Gunn9c0eb0b2016-06-29 11:23:25 -0700337 * Notifies the {@link Conference} that a {@link Connection} has been added to it.
Andrew Lee46f7f5d2014-11-06 17:00:25 -0800338 *
339 * @param connection The newly added connection.
340 */
341 public void onConnectionAdded(Connection connection) {}
342
343 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700344 * Sets state to be on hold.
345 */
346 public final void setOnHold() {
347 setState(Connection.STATE_HOLDING);
348 }
349
350 /**
Tyler Gunnd46595a2015-06-01 14:29:11 -0700351 * Sets state to be dialing.
352 */
353 public final void setDialing() {
354 setState(Connection.STATE_DIALING);
355 }
356
357 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700358 * Sets state to be active.
359 */
360 public final void setActive() {
361 setState(Connection.STATE_ACTIVE);
362 }
363
364 /**
365 * Sets state to disconnected.
366 *
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700367 * @param disconnectCause The reason for the disconnection, as described by
368 * {@link android.telecom.DisconnectCause}.
Santos Cordon823fd3c2014-08-07 18:35:18 -0700369 */
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700370 public final void setDisconnected(DisconnectCause disconnectCause) {
371 mDisconnectCause = disconnectCause;;
Santos Cordon823fd3c2014-08-07 18:35:18 -0700372 setState(Connection.STATE_DISCONNECTED);
373 for (Listener l : mListeners) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700374 l.onDisconnected(this, mDisconnectCause);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700375 }
376 }
377
378 /**
mike dooley1cf14ac2014-11-04 10:59:53 -0800379 * @return The {@link DisconnectCause} for this connection.
380 */
381 public final DisconnectCause getDisconnectCause() {
382 return mDisconnectCause;
383 }
384
385 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800386 * Sets the capabilities of a conference. See {@code CAPABILITY_*} constants of class
387 * {@link Connection} for valid values.
Nancy Chen56fc25d2014-09-09 12:24:51 -0700388 *
Tyler Gunn720c6642016-03-22 09:02:47 -0700389 * @param connectionCapabilities A bitmask of the {@code Capabilities} of the conference call.
Santos Cordon823fd3c2014-08-07 18:35:18 -0700390 */
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800391 public final void setConnectionCapabilities(int connectionCapabilities) {
392 if (connectionCapabilities != mConnectionCapabilities) {
393 mConnectionCapabilities = connectionCapabilities;
Santos Cordon823fd3c2014-08-07 18:35:18 -0700394
395 for (Listener l : mListeners) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800396 l.onConnectionCapabilitiesChanged(this, mConnectionCapabilities);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700397 }
398 }
399 }
400
401 /**
Tyler Gunn720c6642016-03-22 09:02:47 -0700402 * Sets the properties of a conference. See {@code PROPERTY_*} constants of class
403 * {@link Connection} for valid values.
404 *
405 * @param connectionProperties A bitmask of the {@code Properties} of the conference call.
406 */
407 public final void setConnectionProperties(int connectionProperties) {
408 if (connectionProperties != mConnectionProperties) {
409 mConnectionProperties = connectionProperties;
410
411 for (Listener l : mListeners) {
412 l.onConnectionPropertiesChanged(this, mConnectionProperties);
413 }
414 }
415 }
416
417 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700418 * Adds the specified connection as a child of this conference.
419 *
420 * @param connection The connection to add.
421 * @return True if the connection was successfully added.
422 */
Santos Cordona4868042014-09-04 17:39:22 -0700423 public final boolean addConnection(Connection connection) {
Rekha Kumar07366812015-03-24 16:42:31 -0700424 Log.d(this, "Connection=%s, connection=", connection);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700425 if (connection != null && !mChildConnections.contains(connection)) {
426 if (connection.setConference(this)) {
427 mChildConnections.add(connection);
Andrew Lee46f7f5d2014-11-06 17:00:25 -0800428 onConnectionAdded(connection);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700429 for (Listener l : mListeners) {
430 l.onConnectionAdded(this, connection);
431 }
432 return true;
433 }
434 }
435 return false;
436 }
437
438 /**
439 * Removes the specified connection as a child of this conference.
440 *
441 * @param connection The connection to remove.
Santos Cordon823fd3c2014-08-07 18:35:18 -0700442 */
Santos Cordona4868042014-09-04 17:39:22 -0700443 public final void removeConnection(Connection connection) {
Santos Cordon0159ac02014-08-21 14:28:11 -0700444 Log.d(this, "removing %s from %s", connection, mChildConnections);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700445 if (connection != null && mChildConnections.remove(connection)) {
446 connection.resetConference();
447 for (Listener l : mListeners) {
448 l.onConnectionRemoved(this, connection);
449 }
450 }
451 }
452
453 /**
Ihab Awad50e35062014-09-30 09:17:03 -0700454 * Sets the connections with which this connection can be conferenced.
455 *
456 * @param conferenceableConnections The set of connections this connection can conference with.
457 */
458 public final void setConferenceableConnections(List<Connection> conferenceableConnections) {
Tyler Gunnec5b6e32016-12-01 19:40:30 -0800459 if (Objects.equals(mConferenceableConnections, conferenceableConnections)) {
460 return;
461 }
462
Ihab Awad50e35062014-09-30 09:17:03 -0700463 clearConferenceableList();
464 for (Connection c : conferenceableConnections) {
465 // If statement checks for duplicates in input. It makes it N^2 but we're dealing with a
466 // small amount of items here.
467 if (!mConferenceableConnections.contains(c)) {
468 c.addConnectionListener(mConnectionDeathListener);
469 mConferenceableConnections.add(c);
470 }
471 }
472 fireOnConferenceableConnectionsChanged();
473 }
474
Rekha Kumar07366812015-03-24 16:42:31 -0700475 /**
476 * Set the video state for the conference.
Yorke Lee32f24732015-05-12 16:18:03 -0700477 * Valid values: {@link VideoProfile#STATE_AUDIO_ONLY},
478 * {@link VideoProfile#STATE_BIDIRECTIONAL},
479 * {@link VideoProfile#STATE_TX_ENABLED},
480 * {@link VideoProfile#STATE_RX_ENABLED}.
Rekha Kumar07366812015-03-24 16:42:31 -0700481 *
482 * @param videoState The new video state.
Rekha Kumar07366812015-03-24 16:42:31 -0700483 */
484 public final void setVideoState(Connection c, int videoState) {
485 Log.d(this, "setVideoState Conference: %s Connection: %s VideoState: %s",
486 this, c, videoState);
487 for (Listener l : mListeners) {
488 l.onVideoStateChanged(this, videoState);
489 }
490 }
491
492 /**
493 * Sets the video connection provider.
494 *
495 * @param videoProvider The video provider.
Rekha Kumar07366812015-03-24 16:42:31 -0700496 */
497 public final void setVideoProvider(Connection c, Connection.VideoProvider videoProvider) {
498 Log.d(this, "setVideoProvider Conference: %s Connection: %s VideoState: %s",
499 this, c, videoProvider);
500 for (Listener l : mListeners) {
501 l.onVideoProviderChanged(this, videoProvider);
502 }
503 }
504
Ihab Awad50e35062014-09-30 09:17:03 -0700505 private final void fireOnConferenceableConnectionsChanged() {
506 for (Listener l : mListeners) {
507 l.onConferenceableConnectionsChanged(this, getConferenceableConnections());
508 }
509 }
510
511 /**
512 * Returns the connections with which this connection can be conferenced.
513 */
514 public final List<Connection> getConferenceableConnections() {
515 return mUnmodifiableConferenceableConnections;
516 }
517
518 /**
Nancy Chenea38cca2014-09-05 16:38:49 -0700519 * Tears down the conference object and any of its current connections.
Santos Cordon823fd3c2014-08-07 18:35:18 -0700520 */
Santos Cordona4868042014-09-04 17:39:22 -0700521 public final void destroy() {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700522 Log.d(this, "destroying conference : %s", this);
523 // Tear down the children.
Santos Cordon0159ac02014-08-21 14:28:11 -0700524 for (Connection connection : mChildConnections) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700525 Log.d(this, "removing connection %s", connection);
526 removeConnection(connection);
527 }
528
529 // If not yet disconnected, set the conference call as disconnected first.
530 if (mState != Connection.STATE_DISCONNECTED) {
531 Log.d(this, "setting to disconnected");
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700532 setDisconnected(new DisconnectCause(DisconnectCause.LOCAL));
Santos Cordon823fd3c2014-08-07 18:35:18 -0700533 }
534
535 // ...and notify.
536 for (Listener l : mListeners) {
537 l.onDestroyed(this);
538 }
539 }
540
541 /**
542 * Add a listener to be notified of a state change.
543 *
544 * @param listener The new listener.
545 * @return This conference.
546 * @hide
547 */
548 public final Conference addListener(Listener listener) {
549 mListeners.add(listener);
550 return this;
551 }
552
553 /**
554 * Removes the specified listener.
555 *
556 * @param listener The listener to remove.
557 * @return This conference.
558 * @hide
559 */
560 public final Conference removeListener(Listener listener) {
561 mListeners.remove(listener);
562 return this;
563 }
564
Yorke Leea0d3ca92014-09-15 19:18:13 -0700565 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700566 * Retrieves the primary connection associated with the conference. The primary connection is
567 * the connection from which the conference will retrieve its current state.
568 *
569 * @return The primary connection.
Santos Cordon5d2e4f22015-05-12 12:32:51 -0700570 * @hide
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700571 */
Santos Cordon5d2e4f22015-05-12 12:32:51 -0700572 @SystemApi
Santos Cordon4055d642015-05-12 14:19:24 -0700573 public Connection getPrimaryConnection() {
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700574 if (mUnmodifiableChildConnections == null || mUnmodifiableChildConnections.isEmpty()) {
575 return null;
576 }
577 return mUnmodifiableChildConnections.get(0);
578 }
579
580 /**
Santos Cordon5d2e4f22015-05-12 12:32:51 -0700581 * @hide
582 * @deprecated Use {@link #setConnectionTime}.
Tyler Gunncd5d33c2015-01-12 09:02:01 -0800583 */
Santos Cordon5d2e4f22015-05-12 12:32:51 -0700584 @Deprecated
585 @SystemApi
586 public final void setConnectTimeMillis(long connectTimeMillis) {
587 setConnectionTime(connectTimeMillis);
Tyler Gunncd5d33c2015-01-12 09:02:01 -0800588 }
589
590 /**
Tyler Gunn3fa819c2017-08-04 09:27:26 -0700591 * Sets the connection start time of the {@code Conference}. Should be specified in wall-clock
592 * time returned by {@link System#currentTimeMillis()}.
593 * <p>
594 * When setting the connection time, you should always set the connection elapsed time via
595 * {@link #setConnectionElapsedTime(long)}.
Santos Cordon5d2e4f22015-05-12 12:32:51 -0700596 *
597 * @param connectionTimeMillis The connection time, in milliseconds.
598 */
599 public final void setConnectionTime(long connectionTimeMillis) {
600 mConnectTimeMillis = connectionTimeMillis;
601 }
602
603 /**
Tyler Gunn3fa819c2017-08-04 09:27:26 -0700604 * Sets the elapsed time since system boot when the {@link Conference} was connected.
605 * This is used to determine the duration of the {@link Conference}.
606 * <p>
607 * When setting the connection elapsed time, you should always set the connection time via
608 * {@link #setConnectionTime(long)}.
609 *
610 * @param connectionElapsedTime The connection time, as measured by
611 * {@link SystemClock#elapsedRealtime()}.
612 */
613 public final void setConnectionElapsedTime(long connectionElapsedTime) {
614 mConnectElapsedTimeMillis = connectionElapsedTime;
615 }
616
617 /**
Santos Cordon5d2e4f22015-05-12 12:32:51 -0700618 * @hide
619 * @deprecated Use {@link #getConnectionTime}.
620 */
621 @Deprecated
622 @SystemApi
623 public final long getConnectTimeMillis() {
624 return getConnectionTime();
625 }
626
627 /**
628 * Retrieves the connection start time of the {@code Conference}, if specified. A value of
Tyler Gunncd5d33c2015-01-12 09:02:01 -0800629 * {@link #CONNECT_TIME_NOT_SPECIFIED} indicates that Telecom should determine the start time
630 * of the conference.
631 *
Santos Cordon5d2e4f22015-05-12 12:32:51 -0700632 * @return The time at which the {@code Conference} was connected.
Tyler Gunncd5d33c2015-01-12 09:02:01 -0800633 */
Santos Cordon5d2e4f22015-05-12 12:32:51 -0700634 public final long getConnectionTime() {
Tyler Gunncd5d33c2015-01-12 09:02:01 -0800635 return mConnectTimeMillis;
636 }
637
638 /**
Tyler Gunn3fa819c2017-08-04 09:27:26 -0700639 * Retrieves the connection start time of the {@link Conference}, if specified. A value of
640 * {@link #CONNECT_TIME_NOT_SPECIFIED} indicates that Telecom should determine the start time
641 * of the conference.
642 *
643 * This is based on the value of {@link SystemClock#elapsedRealtime()} to ensure that it is not
644 * impacted by wall clock changes (user initiated, network initiated, time zone change, etc).
645 *
646 * @return The elapsed time at which the {@link Conference} was connected.
647 * @hide
648 */
649 public final long getConnectElapsedTime() {
650 return mConnectElapsedTimeMillis;
651 }
652
653 /**
Yorke Leea0d3ca92014-09-15 19:18:13 -0700654 * Inform this Conference that the state of its audio output has been changed externally.
655 *
656 * @param state The new audio state.
657 * @hide
658 */
Yorke Lee4af59352015-05-13 14:14:54 -0700659 final void setCallAudioState(CallAudioState state) {
660 Log.d(this, "setCallAudioState %s", state);
661 mCallAudioState = state;
662 onAudioStateChanged(getAudioState());
663 onCallAudioStateChanged(state);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700664 }
665
Santos Cordon823fd3c2014-08-07 18:35:18 -0700666 private void setState(int newState) {
667 if (newState != Connection.STATE_ACTIVE &&
668 newState != Connection.STATE_HOLDING &&
669 newState != Connection.STATE_DISCONNECTED) {
670 Log.w(this, "Unsupported state transition for Conference call.",
671 Connection.stateToString(newState));
672 return;
673 }
674
675 if (mState != newState) {
676 int oldState = mState;
677 mState = newState;
678 for (Listener l : mListeners) {
679 l.onStateChanged(this, oldState, newState);
680 }
681 }
682 }
Ihab Awad50e35062014-09-30 09:17:03 -0700683
684 private final void clearConferenceableList() {
685 for (Connection c : mConferenceableConnections) {
686 c.removeConnectionListener(mConnectionDeathListener);
687 }
688 mConferenceableConnections.clear();
689 }
Rekha Kumar07366812015-03-24 16:42:31 -0700690
691 @Override
692 public String toString() {
693 return String.format(Locale.US,
694 "[State: %s,Capabilites: %s, VideoState: %s, VideoProvider: %s, ThisObject %s]",
695 Connection.stateToString(mState),
696 Call.Details.capabilitiesToString(mConnectionCapabilities),
697 getVideoState(),
698 getVideoProvider(),
699 super.toString());
700 }
Andrew Lee0f51da32015-04-16 13:11:55 -0700701
Andrew Leeedc625f2015-04-14 13:38:12 -0700702 /**
703 * Sets the label and icon status to display in the InCall UI.
704 *
705 * @param statusHints The status label and icon to set.
706 */
707 public final void setStatusHints(StatusHints statusHints) {
708 mStatusHints = statusHints;
709 for (Listener l : mListeners) {
710 l.onStatusHintsChanged(this, statusHints);
711 }
712 }
713
714 /**
715 * @return The status hints for this conference.
716 */
717 public final StatusHints getStatusHints() {
718 return mStatusHints;
719 }
Santos Cordon6b7f9552015-05-27 17:21:45 -0700720
721 /**
Tyler Gunndee56a82016-03-23 16:06:34 -0700722 * Replaces all the extras associated with this {@code Conference}.
723 * <p>
724 * New or existing keys are replaced in the {@code Conference} extras. Keys which are no longer
725 * in the new extras, but were present the last time {@code setExtras} was called are removed.
726 * <p>
Tyler Gunn9c0eb0b2016-06-29 11:23:25 -0700727 * Alternatively you may use the {@link #putExtras(Bundle)}, and
728 * {@link #removeExtras(String...)} methods to modify the extras.
729 * <p>
Tyler Gunndee56a82016-03-23 16:06:34 -0700730 * No assumptions should be made as to how an In-Call UI or service will handle these extras.
Tyler Gunn9c0eb0b2016-06-29 11:23:25 -0700731 * Keys should be fully qualified (e.g., com.example.extras.MY_EXTRA) to avoid conflicts.
Santos Cordon6b7f9552015-05-27 17:21:45 -0700732 *
Tyler Gunndee56a82016-03-23 16:06:34 -0700733 * @param extras The extras associated with this {@code Conference}.
Santos Cordon6b7f9552015-05-27 17:21:45 -0700734 */
735 public final void setExtras(@Nullable Bundle extras) {
Brad Ebinger4fa6a012016-06-14 17:04:01 -0700736 // Keeping putExtras and removeExtras in the same lock so that this operation happens as a
737 // block instead of letting other threads put/remove while this method is running.
738 synchronized (mExtrasLock) {
739 // Add/replace any new or changed extras values.
740 putExtras(extras);
741 // If we have used "setExtras" in the past, compare the key set from the last invocation
742 // to the current one and remove any keys that went away.
743 if (mPreviousExtraKeys != null) {
744 List<String> toRemove = new ArrayList<String>();
745 for (String oldKey : mPreviousExtraKeys) {
746 if (extras == null || !extras.containsKey(oldKey)) {
747 toRemove.add(oldKey);
748 }
749 }
Tyler Gunndee56a82016-03-23 16:06:34 -0700750
Brad Ebinger4fa6a012016-06-14 17:04:01 -0700751 if (!toRemove.isEmpty()) {
752 removeExtras(toRemove);
Tyler Gunndee56a82016-03-23 16:06:34 -0700753 }
754 }
755
Brad Ebinger4fa6a012016-06-14 17:04:01 -0700756 // Track the keys the last time set called setExtras. This way, the next time setExtras
757 // is called we can see if the caller has removed any extras values.
758 if (mPreviousExtraKeys == null) {
759 mPreviousExtraKeys = new ArraySet<String>();
Tyler Gunndee56a82016-03-23 16:06:34 -0700760 }
Brad Ebinger4fa6a012016-06-14 17:04:01 -0700761 mPreviousExtraKeys.clear();
762 if (extras != null) {
763 mPreviousExtraKeys.addAll(extras.keySet());
764 }
Tyler Gunna8fb8ab2016-03-29 10:24:22 -0700765 }
Tyler Gunndee56a82016-03-23 16:06:34 -0700766 }
767
768 /**
769 * Adds some extras to this {@link Conference}. Existing keys are replaced and new ones are
770 * added.
771 * <p>
772 * No assumptions should be made as to how an In-Call UI or service will handle these extras.
773 * Keys should be fully qualified (e.g., com.example.MY_EXTRA) to avoid conflicts.
774 *
775 * @param extras The extras to add.
776 */
777 public final void putExtras(@NonNull Bundle extras) {
778 if (extras == null) {
779 return;
780 }
781
Brad Ebinger4fa6a012016-06-14 17:04:01 -0700782 // Creating a Bundle clone so we don't have to synchronize on mExtrasLock while calling
783 // onExtrasChanged.
784 Bundle listenersBundle;
785 synchronized (mExtrasLock) {
786 if (mExtras == null) {
787 mExtras = new Bundle();
788 }
789 mExtras.putAll(extras);
790 listenersBundle = new Bundle(mExtras);
Tyler Gunndee56a82016-03-23 16:06:34 -0700791 }
Tyler Gunndee56a82016-03-23 16:06:34 -0700792
Santos Cordon6b7f9552015-05-27 17:21:45 -0700793 for (Listener l : mListeners) {
Brad Ebinger4fa6a012016-06-14 17:04:01 -0700794 l.onExtrasChanged(this, new Bundle(listenersBundle));
Santos Cordon6b7f9552015-05-27 17:21:45 -0700795 }
796 }
797
798 /**
Tyler Gunndee56a82016-03-23 16:06:34 -0700799 * Adds a boolean extra to this {@link Conference}.
800 *
801 * @param key The extra key.
802 * @param value The value.
803 * @hide
804 */
805 public final void putExtra(String key, boolean value) {
806 Bundle newExtras = new Bundle();
807 newExtras.putBoolean(key, value);
808 putExtras(newExtras);
809 }
810
811 /**
812 * Adds an integer extra to this {@link Conference}.
813 *
814 * @param key The extra key.
815 * @param value The value.
816 * @hide
817 */
818 public final void putExtra(String key, int value) {
819 Bundle newExtras = new Bundle();
820 newExtras.putInt(key, value);
821 putExtras(newExtras);
822 }
823
824 /**
825 * Adds a string extra to this {@link Conference}.
826 *
827 * @param key The extra key.
828 * @param value The value.
829 * @hide
830 */
831 public final void putExtra(String key, String value) {
832 Bundle newExtras = new Bundle();
833 newExtras.putString(key, value);
834 putExtras(newExtras);
835 }
836
837 /**
Tyler Gunn071be6f2016-05-10 14:52:33 -0700838 * Removes extras from this {@link Conference}.
Tyler Gunndee56a82016-03-23 16:06:34 -0700839 *
Tyler Gunn071be6f2016-05-10 14:52:33 -0700840 * @param keys The keys of the extras to remove.
Tyler Gunndee56a82016-03-23 16:06:34 -0700841 */
842 public final void removeExtras(List<String> keys) {
843 if (keys == null || keys.isEmpty()) {
844 return;
845 }
846
Brad Ebinger4fa6a012016-06-14 17:04:01 -0700847 synchronized (mExtrasLock) {
848 if (mExtras != null) {
849 for (String key : keys) {
850 mExtras.remove(key);
851 }
Tyler Gunndee56a82016-03-23 16:06:34 -0700852 }
853 }
854
Brad Ebinger4fa6a012016-06-14 17:04:01 -0700855 List<String> unmodifiableKeys = Collections.unmodifiableList(keys);
Tyler Gunndee56a82016-03-23 16:06:34 -0700856 for (Listener l : mListeners) {
Brad Ebinger4fa6a012016-06-14 17:04:01 -0700857 l.onExtrasRemoved(this, unmodifiableKeys);
Tyler Gunndee56a82016-03-23 16:06:34 -0700858 }
859 }
860
861 /**
Tyler Gunn071be6f2016-05-10 14:52:33 -0700862 * Removes extras from this {@link Conference}.
863 *
864 * @param keys The keys of the extras to remove.
865 */
866 public final void removeExtras(String ... keys) {
867 removeExtras(Arrays.asList(keys));
868 }
869
870 /**
Tyler Gunndee56a82016-03-23 16:06:34 -0700871 * Returns the extras associated with this conference.
Tyler Gunn2cbe2b52016-05-04 15:48:10 +0000872 * <p>
873 * Extras should be updated using {@link #putExtras(Bundle)} and {@link #removeExtras(List)}.
874 * <p>
875 * Telecom or an {@link InCallService} can also update the extras via
876 * {@link android.telecom.Call#putExtras(Bundle)}, and
877 * {@link Call#removeExtras(List)}.
878 * <p>
879 * The conference is notified of changes to the extras made by Telecom or an
880 * {@link InCallService} by {@link #onExtrasChanged(Bundle)}.
Tyler Gunndee56a82016-03-23 16:06:34 -0700881 *
882 * @return The extras associated with this connection.
Santos Cordon6b7f9552015-05-27 17:21:45 -0700883 */
884 public final Bundle getExtras() {
885 return mExtras;
886 }
Tyler Gunndee56a82016-03-23 16:06:34 -0700887
888 /**
889 * Notifies this {@link Conference} of a change to the extras made outside the
890 * {@link ConnectionService}.
891 * <p>
892 * These extras changes can originate from Telecom itself, or from an {@link InCallService} via
893 * {@link android.telecom.Call#putExtras(Bundle)}, and
894 * {@link Call#removeExtras(List)}.
895 *
896 * @param extras The new extras bundle.
897 */
898 public void onExtrasChanged(Bundle extras) {}
899
900 /**
901 * Handles a change to extras received from Telecom.
902 *
903 * @param extras The new extras.
904 * @hide
905 */
906 final void handleExtrasChanged(Bundle extras) {
Brad Ebinger4fa6a012016-06-14 17:04:01 -0700907 Bundle b = null;
908 synchronized (mExtrasLock) {
909 mExtras = extras;
910 if (mExtras != null) {
911 b = new Bundle(mExtras);
912 }
913 }
914 onExtrasChanged(b);
Tyler Gunndee56a82016-03-23 16:06:34 -0700915 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700916}