blob: 2ee5d25d463fd580818b86a0997e8f4970bbd4e6 [file] [log] [blame]
Ihab Awad542e0ea2014-05-16 10:22:16 -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;
Ihab Awad542e0ea2014-05-16 10:22:16 -070018
Tyler Gunn45382162015-05-06 08:52:27 -070019import com.android.internal.os.SomeArgs;
Tyler Gunnef9f6f92014-09-12 22:16:17 -070020import com.android.internal.telecom.IVideoCallback;
21import com.android.internal.telecom.IVideoProvider;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070022
Ihab Awad542e0ea2014-05-16 10:22:16 -070023import android.net.Uri;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070024import android.os.Handler;
25import android.os.IBinder;
26import android.os.Message;
27import android.os.RemoteException;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070028import android.view.Surface;
Ihab Awad542e0ea2014-05-16 10:22:16 -070029
Santos Cordonb6939982014-06-04 20:20:58 -070030import java.util.ArrayList;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070031import java.util.Collections;
Tyler Gunn75958422015-04-15 14:23:42 -070032import java.util.HashMap;
Santos Cordonb6939982014-06-04 20:20:58 -070033import java.util.List;
Ihab Awad542e0ea2014-05-16 10:22:16 -070034import java.util.Set;
Jay Shrauner229e3822014-08-15 09:23:07 -070035import java.util.concurrent.ConcurrentHashMap;
Ihab Awad542e0ea2014-05-16 10:22:16 -070036
37/**
38 * Represents a connection to a remote endpoint that carries voice traffic.
Ihab Awad6107bab2014-08-18 09:23:25 -070039 * <p>
40 * Implementations create a custom subclass of {@code Connection} and return it to the framework
41 * as the return value of
42 * {@link ConnectionService#onCreateIncomingConnection(PhoneAccountHandle, ConnectionRequest)}
43 * or
44 * {@link ConnectionService#onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
45 * Implementations are then responsible for updating the state of the {@code Connection}, and
46 * must call {@link #destroy()} to signal to the framework that the {@code Connection} is no
47 * longer used and associated resources may be recovered.
Ihab Awad542e0ea2014-05-16 10:22:16 -070048 */
Tyler Gunndf2cbc82015-04-20 09:13:01 -070049public abstract class Connection implements Conferenceable {
Ihab Awad542e0ea2014-05-16 10:22:16 -070050
Ihab Awadb19a0bc2014-08-07 19:46:01 -070051 public static final int STATE_INITIALIZING = 0;
52
53 public static final int STATE_NEW = 1;
54
55 public static final int STATE_RINGING = 2;
56
57 public static final int STATE_DIALING = 3;
58
59 public static final int STATE_ACTIVE = 4;
60
61 public static final int STATE_HOLDING = 5;
62
63 public static final int STATE_DISCONNECTED = 6;
64
Ihab Awad5c9c86e2014-11-12 13:41:16 -080065 /** Connection can currently be put on hold or unheld. */
66 public static final int CAPABILITY_HOLD = 0x00000001;
67
68 /** Connection supports the hold feature. */
69 public static final int CAPABILITY_SUPPORT_HOLD = 0x00000002;
70
71 /**
72 * Connections within a conference can be merged. A {@link ConnectionService} has the option to
73 * add a {@link Conference} before the child {@link Connection}s are merged. This is how
74 * CDMA-based {@link Connection}s are implemented. For these unmerged {@link Conference}s, this
75 * capability allows a merge button to be shown while the conference is in the foreground
76 * of the in-call UI.
77 * <p>
78 * This is only intended for use by a {@link Conference}.
79 */
80 public static final int CAPABILITY_MERGE_CONFERENCE = 0x00000004;
81
82 /**
83 * Connections within a conference can be swapped between foreground and background.
84 * See {@link #CAPABILITY_MERGE_CONFERENCE} for additional information.
85 * <p>
86 * This is only intended for use by a {@link Conference}.
87 */
88 public static final int CAPABILITY_SWAP_CONFERENCE = 0x00000008;
89
90 /**
91 * @hide
92 */
93 public static final int CAPABILITY_UNUSED = 0x00000010;
94
95 /** Connection supports responding via text option. */
96 public static final int CAPABILITY_RESPOND_VIA_TEXT = 0x00000020;
97
98 /** Connection can be muted. */
99 public static final int CAPABILITY_MUTE = 0x00000040;
100
101 /**
102 * Connection supports conference management. This capability only applies to
103 * {@link Conference}s which can have {@link Connection}s as children.
104 */
105 public static final int CAPABILITY_MANAGE_CONFERENCE = 0x00000080;
106
107 /**
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700108 * Local device supports receiving video.
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800109 */
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700110 public static final int CAPABILITY_SUPPORTS_VT_LOCAL_RX = 0x00000100;
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800111
112 /**
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700113 * Local device supports transmitting video.
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800114 */
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700115 public static final int CAPABILITY_SUPPORTS_VT_LOCAL_TX = 0x00000200;
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800116
117 /**
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700118 * Local device supports bidirectional video calling.
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800119 */
Andrew Lee9a8f9ce2015-04-10 18:09:46 -0700120 public static final int CAPABILITY_SUPPORTS_VT_LOCAL_BIDIRECTIONAL =
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700121 CAPABILITY_SUPPORTS_VT_LOCAL_RX | CAPABILITY_SUPPORTS_VT_LOCAL_TX;
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800122
123 /**
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700124 * Remote device supports receiving video.
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800125 */
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700126 public static final int CAPABILITY_SUPPORTS_VT_REMOTE_RX = 0x00000400;
127
128 /**
129 * Remote device supports transmitting video.
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700130 */
131 public static final int CAPABILITY_SUPPORTS_VT_REMOTE_TX = 0x00000800;
132
133 /**
134 * Remote device supports bidirectional video calling.
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700135 */
Andrew Lee9a8f9ce2015-04-10 18:09:46 -0700136 public static final int CAPABILITY_SUPPORTS_VT_REMOTE_BIDIRECTIONAL =
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700137 CAPABILITY_SUPPORTS_VT_REMOTE_RX | CAPABILITY_SUPPORTS_VT_REMOTE_TX;
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800138
139 /**
140 * Connection is able to be separated from its parent {@code Conference}, if any.
141 */
142 public static final int CAPABILITY_SEPARATE_FROM_CONFERENCE = 0x00001000;
143
144 /**
145 * Connection is able to be individually disconnected when in a {@code Conference}.
146 */
147 public static final int CAPABILITY_DISCONNECT_FROM_CONFERENCE = 0x00002000;
148
149 /**
150 * Whether the call is a generic conference, where we do not know the precise state of
151 * participants in the conference (eg. on CDMA).
152 *
153 * @hide
154 */
155 public static final int CAPABILITY_GENERIC_CONFERENCE = 0x00004000;
156
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700157 /**
158 * Connection is using high definition audio.
159 * @hide
160 */
161 public static final int CAPABILITY_HIGH_DEF_AUDIO = 0x00008000;
162
163 /**
164 * Connection is using WIFI.
165 * @hide
166 */
Tyler Gunnd11a3152015-03-18 13:09:14 -0700167 public static final int CAPABILITY_WIFI = 0x00010000;
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700168
Tyler Gunn068085b2015-02-06 13:56:52 -0800169 /**
170 * Indicates that the current device callback number should be shown.
171 *
172 * @hide
173 */
Tyler Gunn96d6c402015-03-18 12:39:23 -0700174 public static final int CAPABILITY_SHOW_CALLBACK_NUMBER = 0x00020000;
Tyler Gunn068085b2015-02-06 13:56:52 -0800175
Tyler Gunn96d6c402015-03-18 12:39:23 -0700176 /**
Dong Zhou89f41eb2015-03-15 11:59:49 -0500177 * Speed up audio setup for MT call.
178 * @hide
Tyler Gunn96d6c402015-03-18 12:39:23 -0700179 */
180 public static final int CAPABILITY_SPEED_UP_MT_AUDIO = 0x00040000;
Tyler Gunn068085b2015-02-06 13:56:52 -0800181
Rekha Kumar07366812015-03-24 16:42:31 -0700182 /**
Tyler Gunnb5e0cfb2015-04-07 16:10:51 -0700183 * Call can be upgraded to a video call.
Rekha Kumar07366812015-03-24 16:42:31 -0700184 */
185 public static final int CAPABILITY_CAN_UPGRADE_TO_VIDEO = 0x00080000;
186
Tyler Gunnb5e0cfb2015-04-07 16:10:51 -0700187 /**
188 * For video calls, indicates whether the outgoing video for the call can be paused using
Yorke Lee32f24732015-05-12 16:18:03 -0700189 * the {@link android.telecom.VideoProfile#STATE_PAUSED} VideoState.
Tyler Gunnb5e0cfb2015-04-07 16:10:51 -0700190 */
191 public static final int CAPABILITY_CAN_PAUSE_VIDEO = 0x00100000;
192
Tyler Gunn96d6c402015-03-18 12:39:23 -0700193 //**********************************************************************************************
Tyler Gunnb5e0cfb2015-04-07 16:10:51 -0700194 // Next CAPABILITY value: 0x00200000
Tyler Gunn96d6c402015-03-18 12:39:23 -0700195 //**********************************************************************************************
Tyler Gunn068085b2015-02-06 13:56:52 -0800196
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700197 // Flag controlling whether PII is emitted into the logs
198 private static final boolean PII_DEBUG = Log.isLoggable(android.util.Log.DEBUG);
199
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800200 /**
201 * Whether the given capabilities support the specified capability.
202 *
203 * @param capabilities A capability bit field.
204 * @param capability The capability to check capabilities for.
205 * @return Whether the specified capability is supported.
206 * @hide
207 */
208 public static boolean can(int capabilities, int capability) {
209 return (capabilities & capability) != 0;
210 }
211
212 /**
213 * Whether the capabilities of this {@code Connection} supports the specified capability.
214 *
215 * @param capability The capability to check capabilities for.
216 * @return Whether the specified capability is supported.
217 * @hide
218 */
219 public boolean can(int capability) {
220 return can(mConnectionCapabilities, capability);
221 }
222
223 /**
224 * Removes the specified capability from the set of capabilities of this {@code Connection}.
225 *
226 * @param capability The capability to remove from the set.
227 * @hide
228 */
229 public void removeCapability(int capability) {
230 mConnectionCapabilities &= ~capability;
231 }
232
233 /**
234 * Adds the specified capability to the set of capabilities of this {@code Connection}.
235 *
236 * @param capability The capability to add to the set.
237 * @hide
238 */
239 public void addCapability(int capability) {
240 mConnectionCapabilities |= capability;
241 }
242
243
244 public static String capabilitiesToString(int capabilities) {
245 StringBuilder builder = new StringBuilder();
246 builder.append("[Capabilities:");
247 if (can(capabilities, CAPABILITY_HOLD)) {
248 builder.append(" CAPABILITY_HOLD");
249 }
250 if (can(capabilities, CAPABILITY_SUPPORT_HOLD)) {
251 builder.append(" CAPABILITY_SUPPORT_HOLD");
252 }
253 if (can(capabilities, CAPABILITY_MERGE_CONFERENCE)) {
254 builder.append(" CAPABILITY_MERGE_CONFERENCE");
255 }
256 if (can(capabilities, CAPABILITY_SWAP_CONFERENCE)) {
257 builder.append(" CAPABILITY_SWAP_CONFERENCE");
258 }
259 if (can(capabilities, CAPABILITY_RESPOND_VIA_TEXT)) {
260 builder.append(" CAPABILITY_RESPOND_VIA_TEXT");
261 }
262 if (can(capabilities, CAPABILITY_MUTE)) {
263 builder.append(" CAPABILITY_MUTE");
264 }
265 if (can(capabilities, CAPABILITY_MANAGE_CONFERENCE)) {
266 builder.append(" CAPABILITY_MANAGE_CONFERENCE");
267 }
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700268 if (can(capabilities, CAPABILITY_SUPPORTS_VT_LOCAL_RX)) {
269 builder.append(" CAPABILITY_SUPPORTS_VT_LOCAL_RX");
270 }
271 if (can(capabilities, CAPABILITY_SUPPORTS_VT_LOCAL_TX)) {
272 builder.append(" CAPABILITY_SUPPORTS_VT_LOCAL_TX");
273 }
Andrew Lee9a8f9ce2015-04-10 18:09:46 -0700274 if (can(capabilities, CAPABILITY_SUPPORTS_VT_LOCAL_BIDIRECTIONAL)) {
275 builder.append(" CAPABILITY_SUPPORTS_VT_LOCAL_BIDIRECTIONAL");
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800276 }
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700277 if (can(capabilities, CAPABILITY_SUPPORTS_VT_REMOTE_RX)) {
278 builder.append(" CAPABILITY_SUPPORTS_VT_REMOTE_RX");
279 }
280 if (can(capabilities, CAPABILITY_SUPPORTS_VT_REMOTE_TX)) {
281 builder.append(" CAPABILITY_SUPPORTS_VT_REMOTE_TX");
282 }
Andrew Lee9a8f9ce2015-04-10 18:09:46 -0700283 if (can(capabilities, CAPABILITY_SUPPORTS_VT_REMOTE_BIDIRECTIONAL)) {
284 builder.append(" CAPABILITY_SUPPORTS_VT_REMOTE_BIDIRECTIONAL");
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800285 }
Andrew Lee80fff3c2014-11-25 17:36:51 -0800286 if (can(capabilities, CAPABILITY_HIGH_DEF_AUDIO)) {
287 builder.append(" CAPABILITY_HIGH_DEF_AUDIO");
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800288 }
Andrew Lee1a8ae3e2015-02-02 13:42:38 -0800289 if (can(capabilities, CAPABILITY_WIFI)) {
290 builder.append(" CAPABILITY_WIFI");
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800291 }
292 if (can(capabilities, CAPABILITY_GENERIC_CONFERENCE)) {
293 builder.append(" CAPABILITY_GENERIC_CONFERENCE");
294 }
Tyler Gunn068085b2015-02-06 13:56:52 -0800295 if (can(capabilities, CAPABILITY_SHOW_CALLBACK_NUMBER)) {
296 builder.append(" CAPABILITY_SHOW_CALLBACK_NUMBER");
297 }
Dong Zhou89f41eb2015-03-15 11:59:49 -0500298 if (can(capabilities, CAPABILITY_SPEED_UP_MT_AUDIO)) {
Tyler Gunnd11a3152015-03-18 13:09:14 -0700299 builder.append(" CAPABILITY_SPEED_UP_MT_AUDIO");
Dong Zhou89f41eb2015-03-15 11:59:49 -0500300 }
Rekha Kumar07366812015-03-24 16:42:31 -0700301 if (can(capabilities, CAPABILITY_CAN_UPGRADE_TO_VIDEO)) {
302 builder.append(" CAPABILITY_CAN_UPGRADE_TO_VIDEO");
303 }
Tyler Gunnb5e0cfb2015-04-07 16:10:51 -0700304 if (can(capabilities, CAPABILITY_CAN_PAUSE_VIDEO)) {
305 builder.append(" CAPABILITY_CAN_PAUSE_VIDEO");
306 }
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800307 builder.append("]");
308 return builder.toString();
309 }
310
Sailesh Nepal091768c2014-06-30 15:15:23 -0700311 /** @hide */
Sailesh Nepal61203862014-07-11 14:50:13 -0700312 public abstract static class Listener {
Ihab Awad542e0ea2014-05-16 10:22:16 -0700313 public void onStateChanged(Connection c, int state) {}
Andrew Lee100e2932014-09-08 15:34:24 -0700314 public void onAddressChanged(Connection c, Uri newAddress, int presentation) {}
Sailesh Nepal61203862014-07-11 14:50:13 -0700315 public void onCallerDisplayNameChanged(
316 Connection c, String callerDisplayName, int presentation) {}
Tyler Gunnaa07df82014-07-17 07:50:22 -0700317 public void onVideoStateChanged(Connection c, int videoState) {}
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700318 public void onDisconnected(Connection c, DisconnectCause disconnectCause) {}
Sailesh Nepal091768c2014-06-30 15:15:23 -0700319 public void onPostDialWait(Connection c, String remaining) {}
Nancy Chen27d1c2d2014-12-15 16:12:50 -0800320 public void onPostDialChar(Connection c, char nextChar) {}
Andrew Lee100e2932014-09-08 15:34:24 -0700321 public void onRingbackRequested(Connection c, boolean ringback) {}
Sailesh Nepal61203862014-07-11 14:50:13 -0700322 public void onDestroyed(Connection c) {}
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800323 public void onConnectionCapabilitiesChanged(Connection c, int capabilities) {}
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700324 public void onVideoProviderChanged(
325 Connection c, VideoProvider videoProvider) {}
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700326 public void onAudioModeIsVoipChanged(Connection c, boolean isVoip) {}
327 public void onStatusHintsChanged(Connection c, StatusHints statusHints) {}
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800328 public void onConferenceablesChanged(
Tyler Gunndf2cbc82015-04-20 09:13:01 -0700329 Connection c, List<Conferenceable> conferenceables) {}
Santos Cordon823fd3c2014-08-07 18:35:18 -0700330 public void onConferenceChanged(Connection c, Conference conference) {}
Tyler Gunn3bffcf72014-10-28 13:51:27 -0700331 /** @hide */
Tyler Gunnab4650c2014-11-06 20:06:23 -0800332 public void onConferenceParticipantsChanged(Connection c,
333 List<ConferenceParticipant> participants) {}
Tyler Gunn8a2b1192015-01-29 11:47:24 -0800334 public void onConferenceStarted() {}
Anthony Lee17455a32015-04-24 15:25:29 -0700335 public void onConferenceMergeFailed(Connection c) {}
Ihab Awad542e0ea2014-05-16 10:22:16 -0700336 }
337
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700338 public static abstract class VideoProvider {
Ihab Awad542e0ea2014-05-16 10:22:16 -0700339
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700340 /**
341 * Video is not being received (no protocol pause was issued).
342 */
343 public static final int SESSION_EVENT_RX_PAUSE = 1;
Evan Charltonbf11f982014-07-20 22:06:28 -0700344
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700345 /**
346 * Video reception has resumed after a SESSION_EVENT_RX_PAUSE.
347 */
348 public static final int SESSION_EVENT_RX_RESUME = 2;
349
350 /**
351 * Video transmission has begun. This occurs after a negotiated start of video transmission
352 * when the underlying protocol has actually begun transmitting video to the remote party.
353 */
354 public static final int SESSION_EVENT_TX_START = 3;
355
356 /**
357 * Video transmission has stopped. This occurs after a negotiated stop of video transmission
358 * when the underlying protocol has actually stopped transmitting video to the remote party.
359 */
360 public static final int SESSION_EVENT_TX_STOP = 4;
361
362 /**
363 * A camera failure has occurred for the selected camera. The In-Call UI can use this as a
364 * cue to inform the user the camera is not available.
365 */
366 public static final int SESSION_EVENT_CAMERA_FAILURE = 5;
367
368 /**
369 * Issued after {@code SESSION_EVENT_CAMERA_FAILURE} when the camera is once again ready for
370 * operation. The In-Call UI can use this as a cue to inform the user that the camera has
371 * become available again.
372 */
373 public static final int SESSION_EVENT_CAMERA_READY = 6;
374
375 /**
376 * Session modify request was successful.
377 */
378 public static final int SESSION_MODIFY_REQUEST_SUCCESS = 1;
379
380 /**
381 * Session modify request failed.
382 */
383 public static final int SESSION_MODIFY_REQUEST_FAIL = 2;
384
385 /**
386 * Session modify request ignored due to invalid parameters.
387 */
388 public static final int SESSION_MODIFY_REQUEST_INVALID = 3;
389
Rekha Kumar07366812015-03-24 16:42:31 -0700390 /**
391 * Session modify request timed out.
392 */
393 public static final int SESSION_MODIFY_REQUEST_TIMED_OUT = 4;
394
395 /**
396 * Session modify request rejected by remote UE.
397 */
398 public static final int SESSION_MODIFY_REQUEST_REJECTED_BY_REMOTE = 5;
399
Tyler Gunn75958422015-04-15 14:23:42 -0700400 private static final int MSG_ADD_VIDEO_CALLBACK = 1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700401 private static final int MSG_SET_CAMERA = 2;
402 private static final int MSG_SET_PREVIEW_SURFACE = 3;
403 private static final int MSG_SET_DISPLAY_SURFACE = 4;
404 private static final int MSG_SET_DEVICE_ORIENTATION = 5;
405 private static final int MSG_SET_ZOOM = 6;
406 private static final int MSG_SEND_SESSION_MODIFY_REQUEST = 7;
407 private static final int MSG_SEND_SESSION_MODIFY_RESPONSE = 8;
408 private static final int MSG_REQUEST_CAMERA_CAPABILITIES = 9;
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800409 private static final int MSG_REQUEST_CONNECTION_DATA_USAGE = 10;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700410 private static final int MSG_SET_PAUSE_IMAGE = 11;
Tyler Gunn75958422015-04-15 14:23:42 -0700411 private static final int MSG_REMOVE_VIDEO_CALLBACK = 12;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700412
413 private final VideoProvider.VideoProviderHandler
414 mMessageHandler = new VideoProvider.VideoProviderHandler();
415 private final VideoProvider.VideoProviderBinder mBinder;
Tyler Gunn75958422015-04-15 14:23:42 -0700416
417 /**
418 * Stores a list of the video callbacks, keyed by IBinder.
419 */
420 private HashMap<IBinder, IVideoCallback> mVideoCallbacks = new HashMap<>();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700421
422 /**
423 * Default handler used to consolidate binder method calls onto a single thread.
424 */
425 private final class VideoProviderHandler extends Handler {
426 @Override
427 public void handleMessage(Message msg) {
428 switch (msg.what) {
Tyler Gunn75958422015-04-15 14:23:42 -0700429 case MSG_ADD_VIDEO_CALLBACK: {
430 IBinder binder = (IBinder) msg.obj;
431 IVideoCallback callback = IVideoCallback.Stub
432 .asInterface((IBinder) msg.obj);
433 if (mVideoCallbacks.containsKey(binder)) {
434 Log.i(this, "addVideoProvider - skipped; already present.");
435 break;
436 }
437 mVideoCallbacks.put(binder, callback);
438 Log.i(this, "addVideoProvider "+ mVideoCallbacks.size());
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700439 break;
Tyler Gunn75958422015-04-15 14:23:42 -0700440 }
441 case MSG_REMOVE_VIDEO_CALLBACK: {
442 IBinder binder = (IBinder) msg.obj;
443 IVideoCallback callback = IVideoCallback.Stub
444 .asInterface((IBinder) msg.obj);
445 if (!mVideoCallbacks.containsKey(binder)) {
446 Log.i(this, "removeVideoProvider - skipped; not present.");
447 break;
448 }
449 mVideoCallbacks.remove(binder);
450 break;
451 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700452 case MSG_SET_CAMERA:
453 onSetCamera((String) msg.obj);
454 break;
455 case MSG_SET_PREVIEW_SURFACE:
456 onSetPreviewSurface((Surface) msg.obj);
457 break;
458 case MSG_SET_DISPLAY_SURFACE:
459 onSetDisplaySurface((Surface) msg.obj);
460 break;
461 case MSG_SET_DEVICE_ORIENTATION:
462 onSetDeviceOrientation(msg.arg1);
463 break;
464 case MSG_SET_ZOOM:
465 onSetZoom((Float) msg.obj);
466 break;
Tyler Gunn45382162015-05-06 08:52:27 -0700467 case MSG_SEND_SESSION_MODIFY_REQUEST: {
468 SomeArgs args = (SomeArgs) msg.obj;
469 try {
470 onSendSessionModifyRequest((VideoProfile) args.arg1,
471 (VideoProfile) args.arg2);
472 } finally {
473 args.recycle();
474 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700475 break;
Tyler Gunn45382162015-05-06 08:52:27 -0700476 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700477 case MSG_SEND_SESSION_MODIFY_RESPONSE:
478 onSendSessionModifyResponse((VideoProfile) msg.obj);
479 break;
480 case MSG_REQUEST_CAMERA_CAPABILITIES:
481 onRequestCameraCapabilities();
482 break;
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800483 case MSG_REQUEST_CONNECTION_DATA_USAGE:
484 onRequestConnectionDataUsage();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700485 break;
486 case MSG_SET_PAUSE_IMAGE:
Yorke Lee32f24732015-05-12 16:18:03 -0700487 onSetPauseImage((Uri) msg.obj);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700488 break;
489 default:
490 break;
491 }
492 }
493 }
494
495 /**
496 * IVideoProvider stub implementation.
497 */
498 private final class VideoProviderBinder extends IVideoProvider.Stub {
Tyler Gunn75958422015-04-15 14:23:42 -0700499 public void addVideoCallback(IBinder videoCallbackBinder) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700500 mMessageHandler.obtainMessage(
Tyler Gunn75958422015-04-15 14:23:42 -0700501 MSG_ADD_VIDEO_CALLBACK, videoCallbackBinder).sendToTarget();
502 }
503
504 public void removeVideoCallback(IBinder videoCallbackBinder) {
505 mMessageHandler.obtainMessage(
506 MSG_REMOVE_VIDEO_CALLBACK, videoCallbackBinder).sendToTarget();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700507 }
508
509 public void setCamera(String cameraId) {
510 mMessageHandler.obtainMessage(MSG_SET_CAMERA, cameraId).sendToTarget();
511 }
512
513 public void setPreviewSurface(Surface surface) {
514 mMessageHandler.obtainMessage(MSG_SET_PREVIEW_SURFACE, surface).sendToTarget();
515 }
516
517 public void setDisplaySurface(Surface surface) {
518 mMessageHandler.obtainMessage(MSG_SET_DISPLAY_SURFACE, surface).sendToTarget();
519 }
520
521 public void setDeviceOrientation(int rotation) {
Rekha Kumar07366812015-03-24 16:42:31 -0700522 mMessageHandler.obtainMessage(
523 MSG_SET_DEVICE_ORIENTATION, rotation, 0).sendToTarget();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700524 }
525
526 public void setZoom(float value) {
527 mMessageHandler.obtainMessage(MSG_SET_ZOOM, value).sendToTarget();
528 }
529
Tyler Gunn45382162015-05-06 08:52:27 -0700530 public void sendSessionModifyRequest(VideoProfile fromProfile, VideoProfile toProfile) {
531 SomeArgs args = SomeArgs.obtain();
532 args.arg1 = fromProfile;
533 args.arg2 = toProfile;
534 mMessageHandler.obtainMessage(MSG_SEND_SESSION_MODIFY_REQUEST, args).sendToTarget();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700535 }
536
537 public void sendSessionModifyResponse(VideoProfile responseProfile) {
538 mMessageHandler.obtainMessage(
539 MSG_SEND_SESSION_MODIFY_RESPONSE, responseProfile).sendToTarget();
540 }
541
542 public void requestCameraCapabilities() {
543 mMessageHandler.obtainMessage(MSG_REQUEST_CAMERA_CAPABILITIES).sendToTarget();
544 }
545
546 public void requestCallDataUsage() {
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800547 mMessageHandler.obtainMessage(MSG_REQUEST_CONNECTION_DATA_USAGE).sendToTarget();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700548 }
549
Yorke Lee32f24732015-05-12 16:18:03 -0700550 public void setPauseImage(Uri uri) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700551 mMessageHandler.obtainMessage(MSG_SET_PAUSE_IMAGE, uri).sendToTarget();
552 }
553 }
554
555 public VideoProvider() {
556 mBinder = new VideoProvider.VideoProviderBinder();
557 }
558
559 /**
560 * Returns binder object which can be used across IPC methods.
561 * @hide
562 */
563 public final IVideoProvider getInterface() {
564 return mBinder;
565 }
566
567 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800568 * Sets the camera to be used for video recording in a video connection.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700569 *
570 * @param cameraId The id of the camera.
571 */
572 public abstract void onSetCamera(String cameraId);
573
574 /**
575 * Sets the surface to be used for displaying a preview of what the user's camera is
576 * currently capturing. When video transmission is enabled, this is the video signal which
577 * is sent to the remote device.
578 *
579 * @param surface The surface.
580 */
581 public abstract void onSetPreviewSurface(Surface surface);
582
583 /**
584 * Sets the surface to be used for displaying the video received from the remote device.
585 *
586 * @param surface The surface.
587 */
588 public abstract void onSetDisplaySurface(Surface surface);
589
590 /**
591 * Sets the device orientation, in degrees. Assumes that a standard portrait orientation of
592 * the device is 0 degrees.
593 *
594 * @param rotation The device orientation, in degrees.
595 */
596 public abstract void onSetDeviceOrientation(int rotation);
597
598 /**
599 * Sets camera zoom ratio.
600 *
601 * @param value The camera zoom ratio.
602 */
603 public abstract void onSetZoom(float value);
604
605 /**
606 * Issues a request to modify the properties of the current session. The request is
607 * sent to the remote device where it it handled by the In-Call UI.
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800608 * Some examples of session modification requests: upgrade connection from audio to video,
609 * downgrade connection from video to audio, pause video.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700610 *
Tyler Gunn45382162015-05-06 08:52:27 -0700611 * @param fromProfile The video properties prior to the request.
612 * @param toProfile The video properties with the requested changes made.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700613 */
Tyler Gunn45382162015-05-06 08:52:27 -0700614 public abstract void onSendSessionModifyRequest(VideoProfile fromProfile,
615 VideoProfile toProfile);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700616
617 /**te
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800618 * Provides a response to a request to change the current connection session video
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700619 * properties.
620 * This is in response to a request the InCall UI has received via the InCall UI.
621 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800622 * @param responseProfile The response connection video properties.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700623 */
624 public abstract void onSendSessionModifyResponse(VideoProfile responseProfile);
625
626 /**
627 * Issues a request to the video provider to retrieve the camera capabilities.
628 * Camera capabilities are reported back to the caller via the In-Call UI.
629 */
630 public abstract void onRequestCameraCapabilities();
631
632 /**
633 * Issues a request to the video telephony framework to retrieve the cumulative data usage
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800634 * for the current connection. Data usage is reported back to the caller via the
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700635 * InCall UI.
636 */
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800637 public abstract void onRequestConnectionDataUsage();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700638
639 /**
640 * Provides the video telephony framework with the URI of an image to be displayed to remote
641 * devices when the video signal is paused.
642 *
643 * @param uri URI of image to display.
644 */
Yorke Lee32f24732015-05-12 16:18:03 -0700645 public abstract void onSetPauseImage(Uri uri);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700646
647 /**
Tyler Gunn75958422015-04-15 14:23:42 -0700648 * Invokes callback method defined in listening {@link InCallService} implementations.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700649 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800650 * @param videoProfile The requested video connection profile.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700651 */
652 public void receiveSessionModifyRequest(VideoProfile videoProfile) {
Tyler Gunn75958422015-04-15 14:23:42 -0700653 if (mVideoCallbacks != null) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700654 try {
Tyler Gunn75958422015-04-15 14:23:42 -0700655 for (IVideoCallback callback : mVideoCallbacks.values()) {
656 callback.receiveSessionModifyRequest(videoProfile);
657 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700658 } catch (RemoteException ignored) {
659 }
660 }
661 }
662
663 /**
Tyler Gunn75958422015-04-15 14:23:42 -0700664 * Invokes callback method defined in listening {@link InCallService} implementations.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700665 *
666 * @param status Status of the session modify request. Valid values are
667 * {@link VideoProvider#SESSION_MODIFY_REQUEST_SUCCESS},
668 * {@link VideoProvider#SESSION_MODIFY_REQUEST_FAIL},
669 * {@link VideoProvider#SESSION_MODIFY_REQUEST_INVALID}
670 * @param requestedProfile The original request which was sent to the remote device.
671 * @param responseProfile The actual profile changes made by the remote device.
672 */
673 public void receiveSessionModifyResponse(int status,
674 VideoProfile requestedProfile, VideoProfile responseProfile) {
Tyler Gunn75958422015-04-15 14:23:42 -0700675 if (mVideoCallbacks != null) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700676 try {
Tyler Gunn75958422015-04-15 14:23:42 -0700677 for (IVideoCallback callback : mVideoCallbacks.values()) {
678 callback.receiveSessionModifyResponse(status, requestedProfile,
679 responseProfile);
680 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700681 } catch (RemoteException ignored) {
682 }
683 }
684 }
685
686 /**
Tyler Gunn75958422015-04-15 14:23:42 -0700687 * Invokes callback method defined in listening {@link InCallService} implementations.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700688 *
689 * Valid values are: {@link VideoProvider#SESSION_EVENT_RX_PAUSE},
690 * {@link VideoProvider#SESSION_EVENT_RX_RESUME},
691 * {@link VideoProvider#SESSION_EVENT_TX_START},
692 * {@link VideoProvider#SESSION_EVENT_TX_STOP}
693 *
694 * @param event The event.
695 */
696 public void handleCallSessionEvent(int event) {
Tyler Gunn75958422015-04-15 14:23:42 -0700697 if (mVideoCallbacks != null) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700698 try {
Tyler Gunn75958422015-04-15 14:23:42 -0700699 for (IVideoCallback callback : mVideoCallbacks.values()) {
700 callback.handleCallSessionEvent(event);
701 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700702 } catch (RemoteException ignored) {
703 }
704 }
705 }
706
707 /**
Tyler Gunn75958422015-04-15 14:23:42 -0700708 * Invokes callback method defined in listening {@link InCallService} implementations.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700709 *
710 * @param width The updated peer video width.
711 * @param height The updated peer video height.
712 */
713 public void changePeerDimensions(int width, int height) {
Tyler Gunn75958422015-04-15 14:23:42 -0700714 if (mVideoCallbacks != null) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700715 try {
Tyler Gunn75958422015-04-15 14:23:42 -0700716 for (IVideoCallback callback : mVideoCallbacks.values()) {
717 callback.changePeerDimensions(width, height);
718 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700719 } catch (RemoteException ignored) {
720 }
721 }
722 }
723
724 /**
Tyler Gunn75958422015-04-15 14:23:42 -0700725 * Invokes callback method defined in listening {@link InCallService} implementations.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700726 *
727 * @param dataUsage The updated data usage.
728 */
Yorke Lee32f24732015-05-12 16:18:03 -0700729 public void setCallDataUsage(long dataUsage) {
Tyler Gunn75958422015-04-15 14:23:42 -0700730 if (mVideoCallbacks != null) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700731 try {
Tyler Gunn75958422015-04-15 14:23:42 -0700732 for (IVideoCallback callback : mVideoCallbacks.values()) {
733 callback.changeCallDataUsage(dataUsage);
734 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700735 } catch (RemoteException ignored) {
736 }
737 }
738 }
739
740 /**
Tyler Gunn75958422015-04-15 14:23:42 -0700741 * Invokes callback method defined in listening {@link InCallService} implementations.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700742 *
Yorke Lee32f24732015-05-12 16:18:03 -0700743 * @param dataUsage The updated data usage.
744 * @deprecated - Use {@link #setCallDataUsage(long)} instead.
745 * @hide
746 */
747 public void changeCallDataUsage(long dataUsage) {
748 setCallDataUsage(dataUsage);
749 }
750
751 /**
752 * Invokes callback method defined in listening {@link InCallService} implementations.
753 *
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700754 * @param cameraCapabilities The changed camera capabilities.
755 */
Yorke Lee400470f2015-05-12 13:31:25 -0700756 public void changeCameraCapabilities(VideoProfile.CameraCapabilities cameraCapabilities) {
Tyler Gunn75958422015-04-15 14:23:42 -0700757 if (mVideoCallbacks != null) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700758 try {
Tyler Gunn75958422015-04-15 14:23:42 -0700759 for (IVideoCallback callback : mVideoCallbacks.values()) {
760 callback.changeCameraCapabilities(cameraCapabilities);
761 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700762 } catch (RemoteException ignored) {
763 }
764 }
765 }
Rekha Kumar07366812015-03-24 16:42:31 -0700766
767 /**
Tyler Gunn75958422015-04-15 14:23:42 -0700768 * Invokes callback method defined in listening {@link InCallService} implementations.
Rekha Kumar07366812015-03-24 16:42:31 -0700769 *
Yorke Lee32f24732015-05-12 16:18:03 -0700770 * Allowed values:
771 * {@link VideoProfile#QUALITY_HIGH},
772 * {@link VideoProfile#QUALITY_MEDIUM},
773 * {@link VideoProfile#QUALITY_LOW},
774 * {@link VideoProfile#QUALITY_DEFAULT}.
775 *
Rekha Kumar07366812015-03-24 16:42:31 -0700776 * @param videoQuality The updated video quality.
777 */
778 public void changeVideoQuality(int videoQuality) {
Tyler Gunn75958422015-04-15 14:23:42 -0700779 if (mVideoCallbacks != null) {
Rekha Kumar07366812015-03-24 16:42:31 -0700780 try {
Tyler Gunn75958422015-04-15 14:23:42 -0700781 for (IVideoCallback callback : mVideoCallbacks.values()) {
782 callback.changeVideoQuality(videoQuality);
783 }
Rekha Kumar07366812015-03-24 16:42:31 -0700784 } catch (RemoteException ignored) {
785 }
786 }
787 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700788 }
789
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700790 private final Listener mConnectionDeathListener = new Listener() {
791 @Override
792 public void onDestroyed(Connection c) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800793 if (mConferenceables.remove(c)) {
794 fireOnConferenceableConnectionsChanged();
795 }
796 }
797 };
798
799 private final Conference.Listener mConferenceDeathListener = new Conference.Listener() {
800 @Override
801 public void onDestroyed(Conference c) {
802 if (mConferenceables.remove(c)) {
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700803 fireOnConferenceableConnectionsChanged();
804 }
805 }
806 };
807
Jay Shrauner229e3822014-08-15 09:23:07 -0700808 /**
809 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
810 * load factor before resizing, 1 means we only expect a single thread to
811 * access the map so make only a single shard
812 */
813 private final Set<Listener> mListeners = Collections.newSetFromMap(
814 new ConcurrentHashMap<Listener, Boolean>(8, 0.9f, 1));
Tyler Gunndf2cbc82015-04-20 09:13:01 -0700815 private final List<Conferenceable> mConferenceables = new ArrayList<>();
816 private final List<Conferenceable> mUnmodifiableConferenceables =
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800817 Collections.unmodifiableList(mConferenceables);
Santos Cordonb6939982014-06-04 20:20:58 -0700818
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700819 private int mState = STATE_NEW;
820 private AudioState mAudioState;
Andrew Lee100e2932014-09-08 15:34:24 -0700821 private Uri mAddress;
822 private int mAddressPresentation;
Sailesh Nepal61203862014-07-11 14:50:13 -0700823 private String mCallerDisplayName;
824 private int mCallerDisplayNamePresentation;
Andrew Lee100e2932014-09-08 15:34:24 -0700825 private boolean mRingbackRequested = false;
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800826 private int mConnectionCapabilities;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700827 private VideoProvider mVideoProvider;
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700828 private boolean mAudioModeIsVoip;
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700829 private StatusHints mStatusHints;
Tyler Gunnaa07df82014-07-17 07:50:22 -0700830 private int mVideoState;
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700831 private DisconnectCause mDisconnectCause;
Santos Cordon823fd3c2014-08-07 18:35:18 -0700832 private Conference mConference;
833 private ConnectionService mConnectionService;
Ihab Awad542e0ea2014-05-16 10:22:16 -0700834
835 /**
836 * Create a new Connection.
837 */
Santos Cordonf2951102014-07-20 19:06:29 -0700838 public Connection() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -0700839
840 /**
Andrew Lee100e2932014-09-08 15:34:24 -0700841 * @return The address (e.g., phone number) to which this Connection is currently communicating.
Ihab Awad542e0ea2014-05-16 10:22:16 -0700842 */
Andrew Lee100e2932014-09-08 15:34:24 -0700843 public final Uri getAddress() {
844 return mAddress;
Ihab Awad542e0ea2014-05-16 10:22:16 -0700845 }
846
847 /**
Andrew Lee100e2932014-09-08 15:34:24 -0700848 * @return The presentation requirements for the address.
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700849 * See {@link TelecomManager} for valid values.
Sailesh Nepal61203862014-07-11 14:50:13 -0700850 */
Andrew Lee100e2932014-09-08 15:34:24 -0700851 public final int getAddressPresentation() {
852 return mAddressPresentation;
Sailesh Nepal61203862014-07-11 14:50:13 -0700853 }
854
855 /**
856 * @return The caller display name (CNAP).
857 */
858 public final String getCallerDisplayName() {
859 return mCallerDisplayName;
860 }
861
862 /**
Nancy Chen9d568c02014-09-08 14:17:59 -0700863 * @return The presentation requirements for the handle.
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700864 * See {@link TelecomManager} for valid values.
Sailesh Nepal61203862014-07-11 14:50:13 -0700865 */
866 public final int getCallerDisplayNamePresentation() {
867 return mCallerDisplayNamePresentation;
868 }
869
870 /**
Ihab Awad542e0ea2014-05-16 10:22:16 -0700871 * @return The state of this Connection.
Ihab Awad542e0ea2014-05-16 10:22:16 -0700872 */
873 public final int getState() {
874 return mState;
875 }
876
877 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800878 * Returns the video state of the connection.
Yorke Lee32f24732015-05-12 16:18:03 -0700879 * Valid values: {@link VideoProfile#STATE_AUDIO_ONLY},
880 * {@link VideoProfile#STATE_BIDIRECTIONAL},
881 * {@link VideoProfile#STATE_TX_ENABLED},
882 * {@link VideoProfile#STATE_RX_ENABLED}.
Tyler Gunnaa07df82014-07-17 07:50:22 -0700883 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800884 * @return The video state of the connection.
Tyler Gunn27d1e252014-08-21 16:38:40 -0700885 * @hide
Tyler Gunnaa07df82014-07-17 07:50:22 -0700886 */
887 public final int getVideoState() {
888 return mVideoState;
889 }
890
891 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800892 * @return The audio state of the connection, describing how its audio is currently
Ihab Awad542e0ea2014-05-16 10:22:16 -0700893 * being routed by the system. This is {@code null} if this Connection
894 * does not directly know about its audio state.
895 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700896 public final AudioState getAudioState() {
897 return mAudioState;
Ihab Awad542e0ea2014-05-16 10:22:16 -0700898 }
899
900 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700901 * @return The conference that this connection is a part of. Null if it is not part of any
902 * conference.
903 */
904 public final Conference getConference() {
905 return mConference;
906 }
907
908 /**
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700909 * Returns whether this connection is requesting that the system play a ringback tone
910 * on its behalf.
911 */
Andrew Lee100e2932014-09-08 15:34:24 -0700912 public final boolean isRingbackRequested() {
913 return mRingbackRequested;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700914 }
915
916 /**
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700917 * @return True if the connection's audio mode is VOIP.
918 */
919 public final boolean getAudioModeIsVoip() {
920 return mAudioModeIsVoip;
921 }
922
923 /**
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700924 * @return The status hints for this connection.
925 */
926 public final StatusHints getStatusHints() {
927 return mStatusHints;
928 }
929
930 /**
Ihab Awad542e0ea2014-05-16 10:22:16 -0700931 * Assign a listener to be notified of state changes.
932 *
933 * @param l A listener.
934 * @return This Connection.
935 *
936 * @hide
937 */
938 public final Connection addConnectionListener(Listener l) {
Santos Cordond34e5712014-08-05 18:54:03 +0000939 mListeners.add(l);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700940 return this;
941 }
942
943 /**
944 * Remove a previously assigned listener that was being notified of state changes.
945 *
946 * @param l A Listener.
947 * @return This Connection.
948 *
949 * @hide
950 */
951 public final Connection removeConnectionListener(Listener l) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700952 if (l != null) {
953 mListeners.remove(l);
954 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700955 return this;
956 }
957
958 /**
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700959 * @return The {@link DisconnectCause} for this connection.
Evan Charltonbf11f982014-07-20 22:06:28 -0700960 */
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700961 public final DisconnectCause getDisconnectCause() {
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700962 return mDisconnectCause;
Evan Charltonbf11f982014-07-20 22:06:28 -0700963 }
964
965 /**
Ihab Awad542e0ea2014-05-16 10:22:16 -0700966 * Inform this Connection that the state of its audio output has been changed externally.
967 *
968 * @param state The new audio state.
Sailesh Nepal400cc482014-06-26 12:04:00 -0700969 * @hide
Ihab Awad542e0ea2014-05-16 10:22:16 -0700970 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700971 final void setAudioState(AudioState state) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800972 checkImmutable();
Ihab Awad60ac30b2014-05-20 22:32:12 -0700973 Log.d(this, "setAudioState %s", state);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700974 mAudioState = state;
Nancy Chen354b2bd2014-09-08 18:27:26 -0700975 onAudioStateChanged(state);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700976 }
977
978 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700979 * @param state An integer value of a {@code STATE_*} constant.
Ihab Awad542e0ea2014-05-16 10:22:16 -0700980 * @return A string representation of the value.
981 */
982 public static String stateToString(int state) {
983 switch (state) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700984 case STATE_INITIALIZING:
985 return "STATE_INITIALIZING";
986 case STATE_NEW:
987 return "STATE_NEW";
988 case STATE_RINGING:
989 return "STATE_RINGING";
990 case STATE_DIALING:
991 return "STATE_DIALING";
992 case STATE_ACTIVE:
993 return "STATE_ACTIVE";
994 case STATE_HOLDING:
995 return "STATE_HOLDING";
996 case STATE_DISCONNECTED:
Ihab Awad542e0ea2014-05-16 10:22:16 -0700997 return "DISCONNECTED";
998 default:
Ihab Awad60ac30b2014-05-20 22:32:12 -0700999 Log.wtf(Connection.class, "Unknown state %d", state);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001000 return "UNKNOWN";
1001 }
1002 }
1003
1004 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001005 * Returns the connection's capabilities, as a bit mask of the {@code CAPABILITY_*} constants.
Ihab Awad52a28f62014-06-18 10:26:34 -07001006 */
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001007 public final int getConnectionCapabilities() {
1008 return mConnectionCapabilities;
Ihab Awad52a28f62014-06-18 10:26:34 -07001009 }
1010
1011 /**
Andrew Lee100e2932014-09-08 15:34:24 -07001012 * Sets the value of the {@link #getAddress()} property.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001013 *
Andrew Lee100e2932014-09-08 15:34:24 -07001014 * @param address The new address.
1015 * @param presentation The presentation requirements for the address.
Tyler Gunnef9f6f92014-09-12 22:16:17 -07001016 * See {@link TelecomManager} for valid values.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001017 */
Andrew Lee100e2932014-09-08 15:34:24 -07001018 public final void setAddress(Uri address, int presentation) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001019 checkImmutable();
Andrew Lee100e2932014-09-08 15:34:24 -07001020 Log.d(this, "setAddress %s", address);
1021 mAddress = address;
1022 mAddressPresentation = presentation;
Santos Cordond34e5712014-08-05 18:54:03 +00001023 for (Listener l : mListeners) {
Andrew Lee100e2932014-09-08 15:34:24 -07001024 l.onAddressChanged(this, address, presentation);
Santos Cordond34e5712014-08-05 18:54:03 +00001025 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001026 }
1027
1028 /**
Sailesh Nepal61203862014-07-11 14:50:13 -07001029 * Sets the caller display name (CNAP).
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001030 *
Sailesh Nepal61203862014-07-11 14:50:13 -07001031 * @param callerDisplayName The new display name.
Nancy Chen9d568c02014-09-08 14:17:59 -07001032 * @param presentation The presentation requirements for the handle.
Tyler Gunnef9f6f92014-09-12 22:16:17 -07001033 * See {@link TelecomManager} for valid values.
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001034 */
Sailesh Nepal61203862014-07-11 14:50:13 -07001035 public final void setCallerDisplayName(String callerDisplayName, int presentation) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001036 checkImmutable();
Sailesh Nepal61203862014-07-11 14:50:13 -07001037 Log.d(this, "setCallerDisplayName %s", callerDisplayName);
Santos Cordond34e5712014-08-05 18:54:03 +00001038 mCallerDisplayName = callerDisplayName;
1039 mCallerDisplayNamePresentation = presentation;
1040 for (Listener l : mListeners) {
1041 l.onCallerDisplayNameChanged(this, callerDisplayName, presentation);
1042 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001043 }
1044
1045 /**
Tyler Gunnaa07df82014-07-17 07:50:22 -07001046 * Set the video state for the connection.
Yorke Lee32f24732015-05-12 16:18:03 -07001047 * Valid values: {@link VideoProfile#STATE_AUDIO_ONLY},
1048 * {@link VideoProfile#STATE_BIDIRECTIONAL},
1049 * {@link VideoProfile#STATE_TX_ENABLED},
1050 * {@link VideoProfile#STATE_RX_ENABLED}.
Tyler Gunnaa07df82014-07-17 07:50:22 -07001051 *
1052 * @param videoState The new video state.
1053 */
1054 public final void setVideoState(int videoState) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001055 checkImmutable();
Tyler Gunnaa07df82014-07-17 07:50:22 -07001056 Log.d(this, "setVideoState %d", videoState);
Santos Cordond34e5712014-08-05 18:54:03 +00001057 mVideoState = videoState;
1058 for (Listener l : mListeners) {
1059 l.onVideoStateChanged(this, mVideoState);
1060 }
Tyler Gunnaa07df82014-07-17 07:50:22 -07001061 }
1062
1063 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001064 * Sets state to active (e.g., an ongoing connection where two or more parties can actively
Ihab Awad542e0ea2014-05-16 10:22:16 -07001065 * communicate).
1066 */
Sailesh Nepal400cc482014-06-26 12:04:00 -07001067 public final void setActive() {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001068 checkImmutable();
Andrew Lee100e2932014-09-08 15:34:24 -07001069 setRingbackRequested(false);
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001070 setState(STATE_ACTIVE);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001071 }
1072
1073 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001074 * Sets state to ringing (e.g., an inbound ringing connection).
Ihab Awad542e0ea2014-05-16 10:22:16 -07001075 */
Sailesh Nepal400cc482014-06-26 12:04:00 -07001076 public final void setRinging() {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001077 checkImmutable();
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001078 setState(STATE_RINGING);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001079 }
1080
1081 /**
Evan Charltonbf11f982014-07-20 22:06:28 -07001082 * Sets state to initializing (this Connection is not yet ready to be used).
1083 */
1084 public final void setInitializing() {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001085 checkImmutable();
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001086 setState(STATE_INITIALIZING);
Evan Charltonbf11f982014-07-20 22:06:28 -07001087 }
1088
1089 /**
1090 * Sets state to initialized (the Connection has been set up and is now ready to be used).
1091 */
1092 public final void setInitialized() {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001093 checkImmutable();
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001094 setState(STATE_NEW);
Evan Charltonbf11f982014-07-20 22:06:28 -07001095 }
1096
1097 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001098 * Sets state to dialing (e.g., dialing an outbound connection).
Ihab Awad542e0ea2014-05-16 10:22:16 -07001099 */
Sailesh Nepal400cc482014-06-26 12:04:00 -07001100 public final void setDialing() {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001101 checkImmutable();
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001102 setState(STATE_DIALING);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001103 }
1104
1105 /**
1106 * Sets state to be on hold.
1107 */
Sailesh Nepal400cc482014-06-26 12:04:00 -07001108 public final void setOnHold() {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001109 checkImmutable();
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001110 setState(STATE_HOLDING);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001111 }
1112
1113 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001114 * Sets the video connection provider.
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001115 * @param videoProvider The video provider.
Andrew Lee5ffbe8b82014-06-20 16:29:33 -07001116 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001117 public final void setVideoProvider(VideoProvider videoProvider) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001118 checkImmutable();
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001119 mVideoProvider = videoProvider;
Santos Cordond34e5712014-08-05 18:54:03 +00001120 for (Listener l : mListeners) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001121 l.onVideoProviderChanged(this, videoProvider);
Santos Cordond34e5712014-08-05 18:54:03 +00001122 }
Andrew Lee5ffbe8b82014-06-20 16:29:33 -07001123 }
1124
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001125 public final VideoProvider getVideoProvider() {
1126 return mVideoProvider;
Andrew Leea27a1932014-07-09 17:07:13 -07001127 }
1128
Andrew Lee5ffbe8b82014-06-20 16:29:33 -07001129 /**
Sailesh Nepal091768c2014-06-30 15:15:23 -07001130 * Sets state to disconnected.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001131 *
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001132 * @param disconnectCause The reason for the disconnection, as specified by
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001133 * {@link DisconnectCause}.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001134 */
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001135 public final void setDisconnected(DisconnectCause disconnectCause) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001136 checkImmutable();
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001137 mDisconnectCause = disconnectCause;
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001138 setState(STATE_DISCONNECTED);
mike dooleyf34519b2014-09-16 17:33:40 -07001139 Log.d(this, "Disconnected with cause %s", disconnectCause);
Santos Cordond34e5712014-08-05 18:54:03 +00001140 for (Listener l : mListeners) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001141 l.onDisconnected(this, disconnectCause);
Santos Cordond34e5712014-08-05 18:54:03 +00001142 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001143 }
1144
1145 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001146 * Informs listeners that this {@code Connection} is in a post-dial wait state. This is done
1147 * when (a) the {@code Connection} is issuing a DTMF sequence; (b) it has encountered a "wait"
1148 * character; and (c) it wishes to inform the In-Call app that it is waiting for the end-user
1149 * to send an {@link #onPostDialContinue(boolean)} signal.
1150 *
1151 * @param remaining The DTMF character sequence remaining to be emitted once the
1152 * {@link #onPostDialContinue(boolean)} is received, including any "wait" characters
1153 * that remaining sequence may contain.
Sailesh Nepal091768c2014-06-30 15:15:23 -07001154 */
1155 public final void setPostDialWait(String remaining) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001156 checkImmutable();
Santos Cordond34e5712014-08-05 18:54:03 +00001157 for (Listener l : mListeners) {
1158 l.onPostDialWait(this, remaining);
1159 }
Sailesh Nepal091768c2014-06-30 15:15:23 -07001160 }
1161
1162 /**
Nancy Chen27d1c2d2014-12-15 16:12:50 -08001163 * Informs listeners that this {@code Connection} has processed a character in the post-dial
1164 * started state. This is done when (a) the {@code Connection} is issuing a DTMF sequence;
Sailesh Nepal1ed85612015-01-31 15:17:19 -08001165 * and (b) it wishes to signal Telecom to play the corresponding DTMF tone locally.
Nancy Chen27d1c2d2014-12-15 16:12:50 -08001166 *
1167 * @param nextChar The DTMF character that was just processed by the {@code Connection}.
Nancy Chen27d1c2d2014-12-15 16:12:50 -08001168 */
Sailesh Nepal1ed85612015-01-31 15:17:19 -08001169 public final void setNextPostDialChar(char nextChar) {
Nancy Chen27d1c2d2014-12-15 16:12:50 -08001170 checkImmutable();
1171 for (Listener l : mListeners) {
1172 l.onPostDialChar(this, nextChar);
1173 }
1174 }
1175
1176 /**
Ihab Awadf8358972014-05-28 16:46:42 -07001177 * Requests that the framework play a ringback tone. This is to be invoked by implementations
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001178 * that do not play a ringback tone themselves in the connection's audio stream.
Ihab Awadf8358972014-05-28 16:46:42 -07001179 *
1180 * @param ringback Whether the ringback tone is to be played.
1181 */
Andrew Lee100e2932014-09-08 15:34:24 -07001182 public final void setRingbackRequested(boolean ringback) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001183 checkImmutable();
Andrew Lee100e2932014-09-08 15:34:24 -07001184 if (mRingbackRequested != ringback) {
1185 mRingbackRequested = ringback;
Santos Cordond34e5712014-08-05 18:54:03 +00001186 for (Listener l : mListeners) {
Andrew Lee100e2932014-09-08 15:34:24 -07001187 l.onRingbackRequested(this, ringback);
Santos Cordond34e5712014-08-05 18:54:03 +00001188 }
1189 }
Ihab Awadf8358972014-05-28 16:46:42 -07001190 }
1191
1192 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001193 * Sets the connection's capabilities as a bit mask of the {@code CAPABILITY_*} constants.
Sailesh Nepal1a7061b2014-07-09 21:03:20 -07001194 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001195 * @param connectionCapabilities The new connection capabilities.
Santos Cordonb6939982014-06-04 20:20:58 -07001196 */
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001197 public final void setConnectionCapabilities(int connectionCapabilities) {
1198 checkImmutable();
1199 if (mConnectionCapabilities != connectionCapabilities) {
1200 mConnectionCapabilities = connectionCapabilities;
Santos Cordond34e5712014-08-05 18:54:03 +00001201 for (Listener l : mListeners) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001202 l.onConnectionCapabilitiesChanged(this, mConnectionCapabilities);
Santos Cordond34e5712014-08-05 18:54:03 +00001203 }
1204 }
Santos Cordonb6939982014-06-04 20:20:58 -07001205 }
1206
1207 /**
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001208 * Tears down the Connection object.
Santos Cordonb6939982014-06-04 20:20:58 -07001209 */
Evan Charlton36a71342014-07-19 16:31:02 -07001210 public final void destroy() {
Jay Shrauner229e3822014-08-15 09:23:07 -07001211 for (Listener l : mListeners) {
1212 l.onDestroyed(this);
Santos Cordond34e5712014-08-05 18:54:03 +00001213 }
Santos Cordonb6939982014-06-04 20:20:58 -07001214 }
1215
1216 /**
Sailesh Nepal33aaae42014-07-07 22:49:44 -07001217 * Requests that the framework use VOIP audio mode for this connection.
1218 *
1219 * @param isVoip True if the audio mode is VOIP.
1220 */
1221 public final void setAudioModeIsVoip(boolean isVoip) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001222 checkImmutable();
Santos Cordond34e5712014-08-05 18:54:03 +00001223 mAudioModeIsVoip = isVoip;
1224 for (Listener l : mListeners) {
1225 l.onAudioModeIsVoipChanged(this, isVoip);
1226 }
Sailesh Nepal33aaae42014-07-07 22:49:44 -07001227 }
1228
1229 /**
Sailesh Nepale7ef59a2014-07-08 21:48:22 -07001230 * Sets the label and icon status to display in the in-call UI.
1231 *
1232 * @param statusHints The status label and icon to set.
1233 */
1234 public final void setStatusHints(StatusHints statusHints) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001235 checkImmutable();
Santos Cordond34e5712014-08-05 18:54:03 +00001236 mStatusHints = statusHints;
1237 for (Listener l : mListeners) {
1238 l.onStatusHintsChanged(this, statusHints);
1239 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -07001240 }
1241
1242 /**
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001243 * Sets the connections with which this connection can be conferenced.
1244 *
1245 * @param conferenceableConnections The set of connections this connection can conference with.
1246 */
1247 public final void setConferenceableConnections(List<Connection> conferenceableConnections) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001248 checkImmutable();
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001249 clearConferenceableList();
1250 for (Connection c : conferenceableConnections) {
1251 // If statement checks for duplicates in input. It makes it N^2 but we're dealing with a
1252 // small amount of items here.
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001253 if (!mConferenceables.contains(c)) {
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001254 c.addConnectionListener(mConnectionDeathListener);
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001255 mConferenceables.add(c);
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001256 }
1257 }
1258 fireOnConferenceableConnectionsChanged();
1259 }
1260
1261 /**
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001262 * Similar to {@link #setConferenceableConnections(java.util.List)}, sets a list of connections
1263 * or conferences with which this connection can be conferenced.
1264 *
1265 * @param conferenceables The conferenceables.
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001266 */
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001267 public final void setConferenceables(List<Conferenceable> conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001268 clearConferenceableList();
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001269 for (Conferenceable c : conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001270 // If statement checks for duplicates in input. It makes it N^2 but we're dealing with a
1271 // small amount of items here.
1272 if (!mConferenceables.contains(c)) {
1273 if (c instanceof Connection) {
1274 Connection connection = (Connection) c;
1275 connection.addConnectionListener(mConnectionDeathListener);
1276 } else if (c instanceof Conference) {
1277 Conference conference = (Conference) c;
1278 conference.addListener(mConferenceDeathListener);
1279 }
1280 mConferenceables.add(c);
1281 }
1282 }
1283 fireOnConferenceableConnectionsChanged();
1284 }
1285
1286 /**
1287 * Returns the connections or conferences with which this connection can be conferenced.
1288 */
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001289 public final List<Conferenceable> getConferenceables() {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001290 return mUnmodifiableConferenceables;
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001291 }
1292
Evan Charlton8635c572014-09-24 14:04:51 -07001293 /*
Santos Cordon823fd3c2014-08-07 18:35:18 -07001294 * @hide
1295 */
1296 public final void setConnectionService(ConnectionService connectionService) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001297 checkImmutable();
Santos Cordon823fd3c2014-08-07 18:35:18 -07001298 if (mConnectionService != null) {
1299 Log.e(this, new Exception(), "Trying to set ConnectionService on a connection " +
1300 "which is already associated with another ConnectionService.");
1301 } else {
1302 mConnectionService = connectionService;
1303 }
1304 }
1305
1306 /**
1307 * @hide
1308 */
1309 public final void unsetConnectionService(ConnectionService connectionService) {
1310 if (mConnectionService != connectionService) {
1311 Log.e(this, new Exception(), "Trying to remove ConnectionService from a Connection " +
1312 "that does not belong to the ConnectionService.");
1313 } else {
1314 mConnectionService = null;
1315 }
1316 }
1317
1318 /**
Santos Cordonaf1b2962014-10-16 19:23:54 -07001319 * @hide
1320 */
1321 public final ConnectionService getConnectionService() {
1322 return mConnectionService;
1323 }
1324
1325 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001326 * Sets the conference that this connection is a part of. This will fail if the connection is
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001327 * already part of a conference. {@link #resetConference} to un-set the conference first.
Santos Cordon823fd3c2014-08-07 18:35:18 -07001328 *
1329 * @param conference The conference.
1330 * @return {@code true} if the conference was successfully set.
1331 * @hide
1332 */
1333 public final boolean setConference(Conference conference) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001334 checkImmutable();
Santos Cordon823fd3c2014-08-07 18:35:18 -07001335 // We check to see if it is already part of another conference.
Santos Cordon0159ac02014-08-21 14:28:11 -07001336 if (mConference == null) {
Santos Cordon823fd3c2014-08-07 18:35:18 -07001337 mConference = conference;
Santos Cordon0159ac02014-08-21 14:28:11 -07001338 if (mConnectionService != null && mConnectionService.containsConference(conference)) {
1339 fireConferenceChanged();
1340 }
Santos Cordon823fd3c2014-08-07 18:35:18 -07001341 return true;
1342 }
1343 return false;
1344 }
1345
1346 /**
1347 * Resets the conference that this connection is a part of.
1348 * @hide
1349 */
1350 public final void resetConference() {
1351 if (mConference != null) {
Santos Cordon0159ac02014-08-21 14:28:11 -07001352 Log.d(this, "Conference reset");
Santos Cordon823fd3c2014-08-07 18:35:18 -07001353 mConference = null;
1354 fireConferenceChanged();
1355 }
1356 }
1357
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001358 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001359 * Notifies this Connection that the {@link #getAudioState()} property has a new value.
Sailesh Nepal400cc482014-06-26 12:04:00 -07001360 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001361 * @param state The new connection audio state.
Sailesh Nepal400cc482014-06-26 12:04:00 -07001362 */
Nancy Chen354b2bd2014-09-08 18:27:26 -07001363 public void onAudioStateChanged(AudioState state) {}
Sailesh Nepal400cc482014-06-26 12:04:00 -07001364
1365 /**
Evan Charltonbf11f982014-07-20 22:06:28 -07001366 * Notifies this Connection of an internal state change. This method is called after the
1367 * state is changed.
Ihab Awadf8358972014-05-28 16:46:42 -07001368 *
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001369 * @param state The new state, one of the {@code STATE_*} constants.
Ihab Awadf8358972014-05-28 16:46:42 -07001370 */
Nancy Chen354b2bd2014-09-08 18:27:26 -07001371 public void onStateChanged(int state) {}
Ihab Awadf8358972014-05-28 16:46:42 -07001372
1373 /**
Ihab Awad542e0ea2014-05-16 10:22:16 -07001374 * Notifies this Connection of a request to play a DTMF tone.
1375 *
1376 * @param c A DTMF character.
1377 */
Santos Cordonf2951102014-07-20 19:06:29 -07001378 public void onPlayDtmfTone(char c) {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001379
1380 /**
1381 * Notifies this Connection of a request to stop any currently playing DTMF tones.
1382 */
Santos Cordonf2951102014-07-20 19:06:29 -07001383 public void onStopDtmfTone() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001384
1385 /**
1386 * Notifies this Connection of a request to disconnect.
1387 */
Santos Cordonf2951102014-07-20 19:06:29 -07001388 public void onDisconnect() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001389
1390 /**
Tyler Gunn3b4b1dc2014-11-04 14:53:37 -08001391 * Notifies this Connection of a request to disconnect a participant of the conference managed
1392 * by the connection.
1393 *
1394 * @param endpoint the {@link Uri} of the participant to disconnect.
1395 * @hide
1396 */
1397 public void onDisconnectConferenceParticipant(Uri endpoint) {}
1398
1399 /**
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001400 * Notifies this Connection of a request to separate from its parent conference.
Santos Cordonb6939982014-06-04 20:20:58 -07001401 */
Santos Cordonf2951102014-07-20 19:06:29 -07001402 public void onSeparate() {}
Santos Cordonb6939982014-06-04 20:20:58 -07001403
1404 /**
Ihab Awad542e0ea2014-05-16 10:22:16 -07001405 * Notifies this Connection of a request to abort.
1406 */
Santos Cordonf2951102014-07-20 19:06:29 -07001407 public void onAbort() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001408
1409 /**
1410 * Notifies this Connection of a request to hold.
1411 */
Santos Cordonf2951102014-07-20 19:06:29 -07001412 public void onHold() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001413
1414 /**
1415 * Notifies this Connection of a request to exit a hold state.
1416 */
Santos Cordonf2951102014-07-20 19:06:29 -07001417 public void onUnhold() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001418
1419 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001420 * Notifies this Connection, which is in {@link #STATE_RINGING}, of
Santos Cordond34e5712014-08-05 18:54:03 +00001421 * a request to accept.
Andrew Lee8da4c3c2014-07-16 10:11:42 -07001422 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001423 * @param videoState The video state in which to answer the connection.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001424 */
Santos Cordonf2951102014-07-20 19:06:29 -07001425 public void onAnswer(int videoState) {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001426
1427 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001428 * Notifies this Connection, which is in {@link #STATE_RINGING}, of
Tyler Gunnbe74de02014-08-29 14:51:48 -07001429 * a request to accept.
1430 */
1431 public void onAnswer() {
1432 onAnswer(VideoProfile.VideoState.AUDIO_ONLY);
1433 }
1434
1435 /**
1436 * Notifies this Connection, which is in {@link #STATE_RINGING}, of
Santos Cordond34e5712014-08-05 18:54:03 +00001437 * a request to reject.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001438 */
Santos Cordonf2951102014-07-20 19:06:29 -07001439 public void onReject() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001440
Evan Charlton6dea4ac2014-06-03 14:07:13 -07001441 /**
1442 * Notifies this Connection whether the user wishes to proceed with the post-dial DTMF codes.
1443 */
Santos Cordonf2951102014-07-20 19:06:29 -07001444 public void onPostDialContinue(boolean proceed) {}
Evan Charlton6dea4ac2014-06-03 14:07:13 -07001445
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001446 static String toLogSafePhoneNumber(String number) {
1447 // For unknown number, log empty string.
1448 if (number == null) {
1449 return "";
1450 }
1451
1452 if (PII_DEBUG) {
1453 // When PII_DEBUG is true we emit PII.
1454 return number;
1455 }
1456
1457 // Do exactly same thing as Uri#toSafeString() does, which will enable us to compare
1458 // sanitized phone numbers.
1459 StringBuilder builder = new StringBuilder();
1460 for (int i = 0; i < number.length(); i++) {
1461 char c = number.charAt(i);
1462 if (c == '-' || c == '@' || c == '.') {
1463 builder.append(c);
1464 } else {
1465 builder.append('x');
1466 }
1467 }
1468 return builder.toString();
1469 }
1470
Ihab Awad542e0ea2014-05-16 10:22:16 -07001471 private void setState(int state) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001472 checkImmutable();
Ihab Awad6107bab2014-08-18 09:23:25 -07001473 if (mState == STATE_DISCONNECTED && mState != state) {
1474 Log.d(this, "Connection already DISCONNECTED; cannot transition out of this state.");
Evan Charltonbf11f982014-07-20 22:06:28 -07001475 return;
Sailesh Nepal400cc482014-06-26 12:04:00 -07001476 }
Evan Charltonbf11f982014-07-20 22:06:28 -07001477 if (mState != state) {
1478 Log.d(this, "setState: %s", stateToString(state));
1479 mState = state;
Nancy Chen354b2bd2014-09-08 18:27:26 -07001480 onStateChanged(state);
Evan Charltonbf11f982014-07-20 22:06:28 -07001481 for (Listener l : mListeners) {
1482 l.onStateChanged(this, state);
1483 }
Evan Charltonbf11f982014-07-20 22:06:28 -07001484 }
1485 }
1486
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001487 private static class FailureSignalingConnection extends Connection {
Ihab Awad90e34e32014-12-01 16:23:17 -08001488 private boolean mImmutable = false;
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001489 public FailureSignalingConnection(DisconnectCause disconnectCause) {
1490 setDisconnected(disconnectCause);
Ihab Awad90e34e32014-12-01 16:23:17 -08001491 mImmutable = true;
Ihab Awad6107bab2014-08-18 09:23:25 -07001492 }
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001493
1494 public void checkImmutable() {
Ihab Awad90e34e32014-12-01 16:23:17 -08001495 if (mImmutable) {
1496 throw new UnsupportedOperationException("Connection is immutable");
1497 }
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001498 }
Ihab Awad6107bab2014-08-18 09:23:25 -07001499 }
1500
Evan Charltonbf11f982014-07-20 22:06:28 -07001501 /**
Ihab Awad6107bab2014-08-18 09:23:25 -07001502 * Return a {@code Connection} which represents a failed connection attempt. The returned
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001503 * {@code Connection} will have a {@link android.telecom.DisconnectCause} and as specified,
1504 * and a {@link #getState()} of {@link #STATE_DISCONNECTED}.
Ihab Awad6107bab2014-08-18 09:23:25 -07001505 * <p>
1506 * The returned {@code Connection} can be assumed to {@link #destroy()} itself when appropriate,
1507 * so users of this method need not maintain a reference to its return value to destroy it.
Evan Charltonbf11f982014-07-20 22:06:28 -07001508 *
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001509 * @param disconnectCause The disconnect cause, ({@see android.telecomm.DisconnectCause}).
Ihab Awad6107bab2014-08-18 09:23:25 -07001510 * @return A {@code Connection} which indicates failure.
Evan Charltonbf11f982014-07-20 22:06:28 -07001511 */
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001512 public static Connection createFailedConnection(DisconnectCause disconnectCause) {
1513 return new FailureSignalingConnection(disconnectCause);
Evan Charltonbf11f982014-07-20 22:06:28 -07001514 }
1515
Evan Charltonbf11f982014-07-20 22:06:28 -07001516 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001517 * Override to throw an {@link UnsupportedOperationException} if this {@code Connection} is
1518 * not intended to be mutated, e.g., if it is a marker for failure. Only for framework use;
1519 * this should never be un-@hide-den.
1520 *
1521 * @hide
1522 */
1523 public void checkImmutable() {}
1524
1525 /**
Ihab Awad6107bab2014-08-18 09:23:25 -07001526 * Return a {@code Connection} which represents a canceled connection attempt. The returned
1527 * {@code Connection} will have state {@link #STATE_DISCONNECTED}, and cannot be moved out of
1528 * that state. This connection should not be used for anything, and no other
1529 * {@code Connection}s should be attempted.
1530 * <p>
Ihab Awad6107bab2014-08-18 09:23:25 -07001531 * so users of this method need not maintain a reference to its return value to destroy it.
Evan Charltonbf11f982014-07-20 22:06:28 -07001532 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001533 * @return A {@code Connection} which indicates that the underlying connection should
1534 * be canceled.
Evan Charltonbf11f982014-07-20 22:06:28 -07001535 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001536 public static Connection createCanceledConnection() {
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001537 return new FailureSignalingConnection(new DisconnectCause(DisconnectCause.CANCELED));
Ihab Awad542e0ea2014-05-16 10:22:16 -07001538 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001539
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001540 private final void fireOnConferenceableConnectionsChanged() {
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001541 for (Listener l : mListeners) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001542 l.onConferenceablesChanged(this, getConferenceables());
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001543 }
1544 }
1545
Santos Cordon823fd3c2014-08-07 18:35:18 -07001546 private final void fireConferenceChanged() {
1547 for (Listener l : mListeners) {
1548 l.onConferenceChanged(this, mConference);
1549 }
1550 }
1551
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001552 private final void clearConferenceableList() {
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001553 for (Conferenceable c : mConferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001554 if (c instanceof Connection) {
1555 Connection connection = (Connection) c;
1556 connection.removeConnectionListener(mConnectionDeathListener);
1557 } else if (c instanceof Conference) {
1558 Conference conference = (Conference) c;
1559 conference.removeListener(mConferenceDeathListener);
1560 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001561 }
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001562 mConferenceables.clear();
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001563 }
Tyler Gunn3bffcf72014-10-28 13:51:27 -07001564
1565 /**
Anthony Lee17455a32015-04-24 15:25:29 -07001566 * Notifies listeners that the merge request failed.
1567 *
1568 * @hide
1569 */
1570 protected final void notifyConferenceMergeFailed() {
1571 for (Listener l : mListeners) {
1572 l.onConferenceMergeFailed(this);
1573 }
1574 }
1575
1576 /**
Tyler Gunnab4650c2014-11-06 20:06:23 -08001577 * Notifies listeners of a change to conference participant(s).
Tyler Gunn3bffcf72014-10-28 13:51:27 -07001578 *
Tyler Gunnab4650c2014-11-06 20:06:23 -08001579 * @param conferenceParticipants The participants.
Tyler Gunn3bffcf72014-10-28 13:51:27 -07001580 * @hide
1581 */
Tyler Gunnab4650c2014-11-06 20:06:23 -08001582 protected final void updateConferenceParticipants(
1583 List<ConferenceParticipant> conferenceParticipants) {
Tyler Gunn3bffcf72014-10-28 13:51:27 -07001584 for (Listener l : mListeners) {
Tyler Gunnab4650c2014-11-06 20:06:23 -08001585 l.onConferenceParticipantsChanged(this, conferenceParticipants);
Tyler Gunn3bffcf72014-10-28 13:51:27 -07001586 }
1587 }
Tyler Gunn8a2b1192015-01-29 11:47:24 -08001588
1589 /**
1590 * Notifies listeners that a conference call has been started.
Jay Shrauner55b97522015-04-09 15:15:43 -07001591 * @hide
Tyler Gunn8a2b1192015-01-29 11:47:24 -08001592 */
1593 protected void notifyConferenceStarted() {
1594 for (Listener l : mListeners) {
1595 l.onConferenceStarted();
1596 }
1597 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001598}