blob: f39f813494f417bc2228034b8b89acb5fee990f4 [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
Yorke Lee4af59352015-05-13 14:14:54 -070023import android.annotation.SystemApi;
Ihab Awad542e0ea2014-05-16 10:22:16 -070024import android.net.Uri;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070025import android.os.Handler;
26import android.os.IBinder;
27import android.os.Message;
28import android.os.RemoteException;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070029import android.view.Surface;
Ihab Awad542e0ea2014-05-16 10:22:16 -070030
Santos Cordonb6939982014-06-04 20:20:58 -070031import java.util.ArrayList;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070032import java.util.Collections;
Tyler Gunn75958422015-04-15 14:23:42 -070033import java.util.HashMap;
Santos Cordonb6939982014-06-04 20:20:58 -070034import java.util.List;
Ihab Awad542e0ea2014-05-16 10:22:16 -070035import java.util.Set;
Jay Shrauner229e3822014-08-15 09:23:07 -070036import java.util.concurrent.ConcurrentHashMap;
Ihab Awad542e0ea2014-05-16 10:22:16 -070037
38/**
39 * Represents a connection to a remote endpoint that carries voice traffic.
Ihab Awad6107bab2014-08-18 09:23:25 -070040 * <p>
41 * Implementations create a custom subclass of {@code Connection} and return it to the framework
42 * as the return value of
43 * {@link ConnectionService#onCreateIncomingConnection(PhoneAccountHandle, ConnectionRequest)}
44 * or
45 * {@link ConnectionService#onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
46 * Implementations are then responsible for updating the state of the {@code Connection}, and
47 * must call {@link #destroy()} to signal to the framework that the {@code Connection} is no
48 * longer used and associated resources may be recovered.
Ihab Awad542e0ea2014-05-16 10:22:16 -070049 */
Tyler Gunndf2cbc82015-04-20 09:13:01 -070050public abstract class Connection implements Conferenceable {
Ihab Awad542e0ea2014-05-16 10:22:16 -070051
Ihab Awadb19a0bc2014-08-07 19:46:01 -070052 public static final int STATE_INITIALIZING = 0;
53
54 public static final int STATE_NEW = 1;
55
56 public static final int STATE_RINGING = 2;
57
58 public static final int STATE_DIALING = 3;
59
60 public static final int STATE_ACTIVE = 4;
61
62 public static final int STATE_HOLDING = 5;
63
64 public static final int STATE_DISCONNECTED = 6;
65
Ihab Awad5c9c86e2014-11-12 13:41:16 -080066 /** Connection can currently be put on hold or unheld. */
67 public static final int CAPABILITY_HOLD = 0x00000001;
68
69 /** Connection supports the hold feature. */
70 public static final int CAPABILITY_SUPPORT_HOLD = 0x00000002;
71
72 /**
73 * Connections within a conference can be merged. A {@link ConnectionService} has the option to
74 * add a {@link Conference} before the child {@link Connection}s are merged. This is how
75 * CDMA-based {@link Connection}s are implemented. For these unmerged {@link Conference}s, this
76 * capability allows a merge button to be shown while the conference is in the foreground
77 * of the in-call UI.
78 * <p>
79 * This is only intended for use by a {@link Conference}.
80 */
81 public static final int CAPABILITY_MERGE_CONFERENCE = 0x00000004;
82
83 /**
84 * Connections within a conference can be swapped between foreground and background.
85 * See {@link #CAPABILITY_MERGE_CONFERENCE} for additional information.
86 * <p>
87 * This is only intended for use by a {@link Conference}.
88 */
89 public static final int CAPABILITY_SWAP_CONFERENCE = 0x00000008;
90
91 /**
92 * @hide
93 */
94 public static final int CAPABILITY_UNUSED = 0x00000010;
95
96 /** Connection supports responding via text option. */
97 public static final int CAPABILITY_RESPOND_VIA_TEXT = 0x00000020;
98
99 /** Connection can be muted. */
100 public static final int CAPABILITY_MUTE = 0x00000040;
101
102 /**
103 * Connection supports conference management. This capability only applies to
104 * {@link Conference}s which can have {@link Connection}s as children.
105 */
106 public static final int CAPABILITY_MANAGE_CONFERENCE = 0x00000080;
107
108 /**
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700109 * Local device supports receiving video.
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800110 */
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700111 public static final int CAPABILITY_SUPPORTS_VT_LOCAL_RX = 0x00000100;
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800112
113 /**
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700114 * Local device supports transmitting video.
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800115 */
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700116 public static final int CAPABILITY_SUPPORTS_VT_LOCAL_TX = 0x00000200;
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800117
118 /**
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700119 * Local device supports bidirectional video calling.
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800120 */
Andrew Lee9a8f9ce2015-04-10 18:09:46 -0700121 public static final int CAPABILITY_SUPPORTS_VT_LOCAL_BIDIRECTIONAL =
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700122 CAPABILITY_SUPPORTS_VT_LOCAL_RX | CAPABILITY_SUPPORTS_VT_LOCAL_TX;
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800123
124 /**
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700125 * Remote device supports receiving video.
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800126 */
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700127 public static final int CAPABILITY_SUPPORTS_VT_REMOTE_RX = 0x00000400;
128
129 /**
130 * Remote device supports transmitting video.
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700131 */
132 public static final int CAPABILITY_SUPPORTS_VT_REMOTE_TX = 0x00000800;
133
134 /**
135 * Remote device supports bidirectional video calling.
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700136 */
Andrew Lee9a8f9ce2015-04-10 18:09:46 -0700137 public static final int CAPABILITY_SUPPORTS_VT_REMOTE_BIDIRECTIONAL =
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700138 CAPABILITY_SUPPORTS_VT_REMOTE_RX | CAPABILITY_SUPPORTS_VT_REMOTE_TX;
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800139
140 /**
141 * Connection is able to be separated from its parent {@code Conference}, if any.
142 */
143 public static final int CAPABILITY_SEPARATE_FROM_CONFERENCE = 0x00001000;
144
145 /**
146 * Connection is able to be individually disconnected when in a {@code Conference}.
147 */
148 public static final int CAPABILITY_DISCONNECT_FROM_CONFERENCE = 0x00002000;
149
150 /**
151 * Whether the call is a generic conference, where we do not know the precise state of
152 * participants in the conference (eg. on CDMA).
153 *
154 * @hide
155 */
156 public static final int CAPABILITY_GENERIC_CONFERENCE = 0x00004000;
157
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700158 /**
159 * Connection is using high definition audio.
160 * @hide
161 */
162 public static final int CAPABILITY_HIGH_DEF_AUDIO = 0x00008000;
163
164 /**
165 * Connection is using WIFI.
166 * @hide
167 */
Tyler Gunnd11a3152015-03-18 13:09:14 -0700168 public static final int CAPABILITY_WIFI = 0x00010000;
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700169
Tyler Gunn068085b2015-02-06 13:56:52 -0800170 /**
171 * Indicates that the current device callback number should be shown.
172 *
173 * @hide
174 */
Tyler Gunn96d6c402015-03-18 12:39:23 -0700175 public static final int CAPABILITY_SHOW_CALLBACK_NUMBER = 0x00020000;
Tyler Gunn068085b2015-02-06 13:56:52 -0800176
Tyler Gunn96d6c402015-03-18 12:39:23 -0700177 /**
Dong Zhou89f41eb2015-03-15 11:59:49 -0500178 * Speed up audio setup for MT call.
179 * @hide
Tyler Gunn96d6c402015-03-18 12:39:23 -0700180 */
181 public static final int CAPABILITY_SPEED_UP_MT_AUDIO = 0x00040000;
Tyler Gunn068085b2015-02-06 13:56:52 -0800182
Rekha Kumar07366812015-03-24 16:42:31 -0700183 /**
Tyler Gunnb5e0cfb2015-04-07 16:10:51 -0700184 * Call can be upgraded to a video call.
Rekha Kumar07366812015-03-24 16:42:31 -0700185 */
186 public static final int CAPABILITY_CAN_UPGRADE_TO_VIDEO = 0x00080000;
187
Tyler Gunnb5e0cfb2015-04-07 16:10:51 -0700188 /**
189 * For video calls, indicates whether the outgoing video for the call can be paused using
Yorke Lee32f24732015-05-12 16:18:03 -0700190 * the {@link android.telecom.VideoProfile#STATE_PAUSED} VideoState.
Tyler Gunnb5e0cfb2015-04-07 16:10:51 -0700191 */
192 public static final int CAPABILITY_CAN_PAUSE_VIDEO = 0x00100000;
193
Tyler Gunn96d6c402015-03-18 12:39:23 -0700194 //**********************************************************************************************
Tyler Gunnb5e0cfb2015-04-07 16:10:51 -0700195 // Next CAPABILITY value: 0x00200000
Tyler Gunn96d6c402015-03-18 12:39:23 -0700196 //**********************************************************************************************
Tyler Gunn068085b2015-02-06 13:56:52 -0800197
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700198 // Flag controlling whether PII is emitted into the logs
199 private static final boolean PII_DEBUG = Log.isLoggable(android.util.Log.DEBUG);
200
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800201 /**
202 * Whether the given capabilities support the specified capability.
203 *
204 * @param capabilities A capability bit field.
205 * @param capability The capability to check capabilities for.
206 * @return Whether the specified capability is supported.
207 * @hide
208 */
209 public static boolean can(int capabilities, int capability) {
210 return (capabilities & capability) != 0;
211 }
212
213 /**
214 * Whether the capabilities of this {@code Connection} supports the specified capability.
215 *
216 * @param capability The capability to check capabilities for.
217 * @return Whether the specified capability is supported.
218 * @hide
219 */
220 public boolean can(int capability) {
221 return can(mConnectionCapabilities, capability);
222 }
223
224 /**
225 * Removes the specified capability from the set of capabilities of this {@code Connection}.
226 *
227 * @param capability The capability to remove from the set.
228 * @hide
229 */
230 public void removeCapability(int capability) {
231 mConnectionCapabilities &= ~capability;
232 }
233
234 /**
235 * Adds the specified capability to the set of capabilities of this {@code Connection}.
236 *
237 * @param capability The capability to add to the set.
238 * @hide
239 */
240 public void addCapability(int capability) {
241 mConnectionCapabilities |= capability;
242 }
243
244
245 public static String capabilitiesToString(int capabilities) {
246 StringBuilder builder = new StringBuilder();
247 builder.append("[Capabilities:");
248 if (can(capabilities, CAPABILITY_HOLD)) {
249 builder.append(" CAPABILITY_HOLD");
250 }
251 if (can(capabilities, CAPABILITY_SUPPORT_HOLD)) {
252 builder.append(" CAPABILITY_SUPPORT_HOLD");
253 }
254 if (can(capabilities, CAPABILITY_MERGE_CONFERENCE)) {
255 builder.append(" CAPABILITY_MERGE_CONFERENCE");
256 }
257 if (can(capabilities, CAPABILITY_SWAP_CONFERENCE)) {
258 builder.append(" CAPABILITY_SWAP_CONFERENCE");
259 }
260 if (can(capabilities, CAPABILITY_RESPOND_VIA_TEXT)) {
261 builder.append(" CAPABILITY_RESPOND_VIA_TEXT");
262 }
263 if (can(capabilities, CAPABILITY_MUTE)) {
264 builder.append(" CAPABILITY_MUTE");
265 }
266 if (can(capabilities, CAPABILITY_MANAGE_CONFERENCE)) {
267 builder.append(" CAPABILITY_MANAGE_CONFERENCE");
268 }
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700269 if (can(capabilities, CAPABILITY_SUPPORTS_VT_LOCAL_RX)) {
270 builder.append(" CAPABILITY_SUPPORTS_VT_LOCAL_RX");
271 }
272 if (can(capabilities, CAPABILITY_SUPPORTS_VT_LOCAL_TX)) {
273 builder.append(" CAPABILITY_SUPPORTS_VT_LOCAL_TX");
274 }
Andrew Lee9a8f9ce2015-04-10 18:09:46 -0700275 if (can(capabilities, CAPABILITY_SUPPORTS_VT_LOCAL_BIDIRECTIONAL)) {
276 builder.append(" CAPABILITY_SUPPORTS_VT_LOCAL_BIDIRECTIONAL");
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800277 }
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700278 if (can(capabilities, CAPABILITY_SUPPORTS_VT_REMOTE_RX)) {
279 builder.append(" CAPABILITY_SUPPORTS_VT_REMOTE_RX");
280 }
281 if (can(capabilities, CAPABILITY_SUPPORTS_VT_REMOTE_TX)) {
282 builder.append(" CAPABILITY_SUPPORTS_VT_REMOTE_TX");
283 }
Andrew Lee9a8f9ce2015-04-10 18:09:46 -0700284 if (can(capabilities, CAPABILITY_SUPPORTS_VT_REMOTE_BIDIRECTIONAL)) {
285 builder.append(" CAPABILITY_SUPPORTS_VT_REMOTE_BIDIRECTIONAL");
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800286 }
Andrew Lee80fff3c2014-11-25 17:36:51 -0800287 if (can(capabilities, CAPABILITY_HIGH_DEF_AUDIO)) {
288 builder.append(" CAPABILITY_HIGH_DEF_AUDIO");
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800289 }
Andrew Lee1a8ae3e2015-02-02 13:42:38 -0800290 if (can(capabilities, CAPABILITY_WIFI)) {
291 builder.append(" CAPABILITY_WIFI");
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800292 }
293 if (can(capabilities, CAPABILITY_GENERIC_CONFERENCE)) {
294 builder.append(" CAPABILITY_GENERIC_CONFERENCE");
295 }
Tyler Gunn068085b2015-02-06 13:56:52 -0800296 if (can(capabilities, CAPABILITY_SHOW_CALLBACK_NUMBER)) {
297 builder.append(" CAPABILITY_SHOW_CALLBACK_NUMBER");
298 }
Dong Zhou89f41eb2015-03-15 11:59:49 -0500299 if (can(capabilities, CAPABILITY_SPEED_UP_MT_AUDIO)) {
Tyler Gunnd11a3152015-03-18 13:09:14 -0700300 builder.append(" CAPABILITY_SPEED_UP_MT_AUDIO");
Dong Zhou89f41eb2015-03-15 11:59:49 -0500301 }
Rekha Kumar07366812015-03-24 16:42:31 -0700302 if (can(capabilities, CAPABILITY_CAN_UPGRADE_TO_VIDEO)) {
303 builder.append(" CAPABILITY_CAN_UPGRADE_TO_VIDEO");
304 }
Tyler Gunnb5e0cfb2015-04-07 16:10:51 -0700305 if (can(capabilities, CAPABILITY_CAN_PAUSE_VIDEO)) {
306 builder.append(" CAPABILITY_CAN_PAUSE_VIDEO");
307 }
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800308 builder.append("]");
309 return builder.toString();
310 }
311
Sailesh Nepal091768c2014-06-30 15:15:23 -0700312 /** @hide */
Sailesh Nepal61203862014-07-11 14:50:13 -0700313 public abstract static class Listener {
Ihab Awad542e0ea2014-05-16 10:22:16 -0700314 public void onStateChanged(Connection c, int state) {}
Andrew Lee100e2932014-09-08 15:34:24 -0700315 public void onAddressChanged(Connection c, Uri newAddress, int presentation) {}
Sailesh Nepal61203862014-07-11 14:50:13 -0700316 public void onCallerDisplayNameChanged(
317 Connection c, String callerDisplayName, int presentation) {}
Tyler Gunnaa07df82014-07-17 07:50:22 -0700318 public void onVideoStateChanged(Connection c, int videoState) {}
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700319 public void onDisconnected(Connection c, DisconnectCause disconnectCause) {}
Sailesh Nepal091768c2014-06-30 15:15:23 -0700320 public void onPostDialWait(Connection c, String remaining) {}
Nancy Chen27d1c2d2014-12-15 16:12:50 -0800321 public void onPostDialChar(Connection c, char nextChar) {}
Andrew Lee100e2932014-09-08 15:34:24 -0700322 public void onRingbackRequested(Connection c, boolean ringback) {}
Sailesh Nepal61203862014-07-11 14:50:13 -0700323 public void onDestroyed(Connection c) {}
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800324 public void onConnectionCapabilitiesChanged(Connection c, int capabilities) {}
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700325 public void onVideoProviderChanged(
326 Connection c, VideoProvider videoProvider) {}
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700327 public void onAudioModeIsVoipChanged(Connection c, boolean isVoip) {}
328 public void onStatusHintsChanged(Connection c, StatusHints statusHints) {}
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800329 public void onConferenceablesChanged(
Tyler Gunndf2cbc82015-04-20 09:13:01 -0700330 Connection c, List<Conferenceable> conferenceables) {}
Santos Cordon823fd3c2014-08-07 18:35:18 -0700331 public void onConferenceChanged(Connection c, Conference conference) {}
Tyler Gunn3bffcf72014-10-28 13:51:27 -0700332 /** @hide */
Tyler Gunnab4650c2014-11-06 20:06:23 -0800333 public void onConferenceParticipantsChanged(Connection c,
334 List<ConferenceParticipant> participants) {}
Tyler Gunn8a2b1192015-01-29 11:47:24 -0800335 public void onConferenceStarted() {}
Anthony Lee17455a32015-04-24 15:25:29 -0700336 public void onConferenceMergeFailed(Connection c) {}
Ihab Awad542e0ea2014-05-16 10:22:16 -0700337 }
338
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700339 public static abstract class VideoProvider {
Ihab Awad542e0ea2014-05-16 10:22:16 -0700340
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700341 /**
342 * Video is not being received (no protocol pause was issued).
343 */
344 public static final int SESSION_EVENT_RX_PAUSE = 1;
Evan Charltonbf11f982014-07-20 22:06:28 -0700345
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700346 /**
347 * Video reception has resumed after a SESSION_EVENT_RX_PAUSE.
348 */
349 public static final int SESSION_EVENT_RX_RESUME = 2;
350
351 /**
352 * Video transmission has begun. This occurs after a negotiated start of video transmission
353 * when the underlying protocol has actually begun transmitting video to the remote party.
354 */
355 public static final int SESSION_EVENT_TX_START = 3;
356
357 /**
358 * Video transmission has stopped. This occurs after a negotiated stop of video transmission
359 * when the underlying protocol has actually stopped transmitting video to the remote party.
360 */
361 public static final int SESSION_EVENT_TX_STOP = 4;
362
363 /**
364 * A camera failure has occurred for the selected camera. The In-Call UI can use this as a
365 * cue to inform the user the camera is not available.
366 */
367 public static final int SESSION_EVENT_CAMERA_FAILURE = 5;
368
369 /**
370 * Issued after {@code SESSION_EVENT_CAMERA_FAILURE} when the camera is once again ready for
371 * operation. The In-Call UI can use this as a cue to inform the user that the camera has
372 * become available again.
373 */
374 public static final int SESSION_EVENT_CAMERA_READY = 6;
375
376 /**
377 * Session modify request was successful.
378 */
379 public static final int SESSION_MODIFY_REQUEST_SUCCESS = 1;
380
381 /**
382 * Session modify request failed.
383 */
384 public static final int SESSION_MODIFY_REQUEST_FAIL = 2;
385
386 /**
387 * Session modify request ignored due to invalid parameters.
388 */
389 public static final int SESSION_MODIFY_REQUEST_INVALID = 3;
390
Rekha Kumar07366812015-03-24 16:42:31 -0700391 /**
392 * Session modify request timed out.
393 */
394 public static final int SESSION_MODIFY_REQUEST_TIMED_OUT = 4;
395
396 /**
397 * Session modify request rejected by remote UE.
398 */
399 public static final int SESSION_MODIFY_REQUEST_REJECTED_BY_REMOTE = 5;
400
Tyler Gunn75958422015-04-15 14:23:42 -0700401 private static final int MSG_ADD_VIDEO_CALLBACK = 1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700402 private static final int MSG_SET_CAMERA = 2;
403 private static final int MSG_SET_PREVIEW_SURFACE = 3;
404 private static final int MSG_SET_DISPLAY_SURFACE = 4;
405 private static final int MSG_SET_DEVICE_ORIENTATION = 5;
406 private static final int MSG_SET_ZOOM = 6;
407 private static final int MSG_SEND_SESSION_MODIFY_REQUEST = 7;
408 private static final int MSG_SEND_SESSION_MODIFY_RESPONSE = 8;
409 private static final int MSG_REQUEST_CAMERA_CAPABILITIES = 9;
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800410 private static final int MSG_REQUEST_CONNECTION_DATA_USAGE = 10;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700411 private static final int MSG_SET_PAUSE_IMAGE = 11;
Tyler Gunn75958422015-04-15 14:23:42 -0700412 private static final int MSG_REMOVE_VIDEO_CALLBACK = 12;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700413
414 private final VideoProvider.VideoProviderHandler
415 mMessageHandler = new VideoProvider.VideoProviderHandler();
416 private final VideoProvider.VideoProviderBinder mBinder;
Tyler Gunn75958422015-04-15 14:23:42 -0700417
418 /**
419 * Stores a list of the video callbacks, keyed by IBinder.
420 */
421 private HashMap<IBinder, IVideoCallback> mVideoCallbacks = new HashMap<>();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700422
423 /**
424 * Default handler used to consolidate binder method calls onto a single thread.
425 */
426 private final class VideoProviderHandler extends Handler {
427 @Override
428 public void handleMessage(Message msg) {
429 switch (msg.what) {
Tyler Gunn75958422015-04-15 14:23:42 -0700430 case MSG_ADD_VIDEO_CALLBACK: {
431 IBinder binder = (IBinder) msg.obj;
432 IVideoCallback callback = IVideoCallback.Stub
433 .asInterface((IBinder) msg.obj);
434 if (mVideoCallbacks.containsKey(binder)) {
435 Log.i(this, "addVideoProvider - skipped; already present.");
436 break;
437 }
438 mVideoCallbacks.put(binder, callback);
439 Log.i(this, "addVideoProvider "+ mVideoCallbacks.size());
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700440 break;
Tyler Gunn75958422015-04-15 14:23:42 -0700441 }
442 case MSG_REMOVE_VIDEO_CALLBACK: {
443 IBinder binder = (IBinder) msg.obj;
444 IVideoCallback callback = IVideoCallback.Stub
445 .asInterface((IBinder) msg.obj);
446 if (!mVideoCallbacks.containsKey(binder)) {
447 Log.i(this, "removeVideoProvider - skipped; not present.");
448 break;
449 }
450 mVideoCallbacks.remove(binder);
451 break;
452 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700453 case MSG_SET_CAMERA:
454 onSetCamera((String) msg.obj);
455 break;
456 case MSG_SET_PREVIEW_SURFACE:
457 onSetPreviewSurface((Surface) msg.obj);
458 break;
459 case MSG_SET_DISPLAY_SURFACE:
460 onSetDisplaySurface((Surface) msg.obj);
461 break;
462 case MSG_SET_DEVICE_ORIENTATION:
463 onSetDeviceOrientation(msg.arg1);
464 break;
465 case MSG_SET_ZOOM:
466 onSetZoom((Float) msg.obj);
467 break;
Tyler Gunn45382162015-05-06 08:52:27 -0700468 case MSG_SEND_SESSION_MODIFY_REQUEST: {
469 SomeArgs args = (SomeArgs) msg.obj;
470 try {
471 onSendSessionModifyRequest((VideoProfile) args.arg1,
472 (VideoProfile) args.arg2);
473 } finally {
474 args.recycle();
475 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700476 break;
Tyler Gunn45382162015-05-06 08:52:27 -0700477 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700478 case MSG_SEND_SESSION_MODIFY_RESPONSE:
479 onSendSessionModifyResponse((VideoProfile) msg.obj);
480 break;
481 case MSG_REQUEST_CAMERA_CAPABILITIES:
482 onRequestCameraCapabilities();
483 break;
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800484 case MSG_REQUEST_CONNECTION_DATA_USAGE:
485 onRequestConnectionDataUsage();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700486 break;
487 case MSG_SET_PAUSE_IMAGE:
Yorke Lee32f24732015-05-12 16:18:03 -0700488 onSetPauseImage((Uri) msg.obj);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700489 break;
490 default:
491 break;
492 }
493 }
494 }
495
496 /**
497 * IVideoProvider stub implementation.
498 */
499 private final class VideoProviderBinder extends IVideoProvider.Stub {
Tyler Gunn75958422015-04-15 14:23:42 -0700500 public void addVideoCallback(IBinder videoCallbackBinder) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700501 mMessageHandler.obtainMessage(
Tyler Gunn75958422015-04-15 14:23:42 -0700502 MSG_ADD_VIDEO_CALLBACK, videoCallbackBinder).sendToTarget();
503 }
504
505 public void removeVideoCallback(IBinder videoCallbackBinder) {
506 mMessageHandler.obtainMessage(
507 MSG_REMOVE_VIDEO_CALLBACK, videoCallbackBinder).sendToTarget();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700508 }
509
510 public void setCamera(String cameraId) {
511 mMessageHandler.obtainMessage(MSG_SET_CAMERA, cameraId).sendToTarget();
512 }
513
514 public void setPreviewSurface(Surface surface) {
515 mMessageHandler.obtainMessage(MSG_SET_PREVIEW_SURFACE, surface).sendToTarget();
516 }
517
518 public void setDisplaySurface(Surface surface) {
519 mMessageHandler.obtainMessage(MSG_SET_DISPLAY_SURFACE, surface).sendToTarget();
520 }
521
522 public void setDeviceOrientation(int rotation) {
Rekha Kumar07366812015-03-24 16:42:31 -0700523 mMessageHandler.obtainMessage(
524 MSG_SET_DEVICE_ORIENTATION, rotation, 0).sendToTarget();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700525 }
526
527 public void setZoom(float value) {
528 mMessageHandler.obtainMessage(MSG_SET_ZOOM, value).sendToTarget();
529 }
530
Tyler Gunn45382162015-05-06 08:52:27 -0700531 public void sendSessionModifyRequest(VideoProfile fromProfile, VideoProfile toProfile) {
532 SomeArgs args = SomeArgs.obtain();
533 args.arg1 = fromProfile;
534 args.arg2 = toProfile;
535 mMessageHandler.obtainMessage(MSG_SEND_SESSION_MODIFY_REQUEST, args).sendToTarget();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700536 }
537
538 public void sendSessionModifyResponse(VideoProfile responseProfile) {
539 mMessageHandler.obtainMessage(
540 MSG_SEND_SESSION_MODIFY_RESPONSE, responseProfile).sendToTarget();
541 }
542
543 public void requestCameraCapabilities() {
544 mMessageHandler.obtainMessage(MSG_REQUEST_CAMERA_CAPABILITIES).sendToTarget();
545 }
546
547 public void requestCallDataUsage() {
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800548 mMessageHandler.obtainMessage(MSG_REQUEST_CONNECTION_DATA_USAGE).sendToTarget();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700549 }
550
Yorke Lee32f24732015-05-12 16:18:03 -0700551 public void setPauseImage(Uri uri) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700552 mMessageHandler.obtainMessage(MSG_SET_PAUSE_IMAGE, uri).sendToTarget();
553 }
554 }
555
556 public VideoProvider() {
557 mBinder = new VideoProvider.VideoProviderBinder();
558 }
559
560 /**
561 * Returns binder object which can be used across IPC methods.
562 * @hide
563 */
564 public final IVideoProvider getInterface() {
565 return mBinder;
566 }
567
568 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800569 * Sets the camera to be used for video recording in a video connection.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700570 *
571 * @param cameraId The id of the camera.
572 */
573 public abstract void onSetCamera(String cameraId);
574
575 /**
576 * Sets the surface to be used for displaying a preview of what the user's camera is
577 * currently capturing. When video transmission is enabled, this is the video signal which
578 * is sent to the remote device.
579 *
580 * @param surface The surface.
581 */
582 public abstract void onSetPreviewSurface(Surface surface);
583
584 /**
585 * Sets the surface to be used for displaying the video received from the remote device.
586 *
587 * @param surface The surface.
588 */
589 public abstract void onSetDisplaySurface(Surface surface);
590
591 /**
592 * Sets the device orientation, in degrees. Assumes that a standard portrait orientation of
593 * the device is 0 degrees.
594 *
595 * @param rotation The device orientation, in degrees.
596 */
597 public abstract void onSetDeviceOrientation(int rotation);
598
599 /**
600 * Sets camera zoom ratio.
601 *
602 * @param value The camera zoom ratio.
603 */
604 public abstract void onSetZoom(float value);
605
606 /**
607 * Issues a request to modify the properties of the current session. The request is
608 * sent to the remote device where it it handled by the In-Call UI.
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800609 * Some examples of session modification requests: upgrade connection from audio to video,
610 * downgrade connection from video to audio, pause video.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700611 *
Tyler Gunn45382162015-05-06 08:52:27 -0700612 * @param fromProfile The video properties prior to the request.
613 * @param toProfile The video properties with the requested changes made.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700614 */
Tyler Gunn45382162015-05-06 08:52:27 -0700615 public abstract void onSendSessionModifyRequest(VideoProfile fromProfile,
616 VideoProfile toProfile);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700617
618 /**te
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800619 * Provides a response to a request to change the current connection session video
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700620 * properties.
621 * This is in response to a request the InCall UI has received via the InCall UI.
622 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800623 * @param responseProfile The response connection video properties.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700624 */
625 public abstract void onSendSessionModifyResponse(VideoProfile responseProfile);
626
627 /**
628 * Issues a request to the video provider to retrieve the camera capabilities.
629 * Camera capabilities are reported back to the caller via the In-Call UI.
630 */
631 public abstract void onRequestCameraCapabilities();
632
633 /**
634 * Issues a request to the video telephony framework to retrieve the cumulative data usage
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800635 * for the current connection. Data usage is reported back to the caller via the
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700636 * InCall UI.
637 */
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800638 public abstract void onRequestConnectionDataUsage();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700639
640 /**
641 * Provides the video telephony framework with the URI of an image to be displayed to remote
642 * devices when the video signal is paused.
643 *
644 * @param uri URI of image to display.
645 */
Yorke Lee32f24732015-05-12 16:18:03 -0700646 public abstract void onSetPauseImage(Uri uri);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700647
648 /**
Tyler Gunn75958422015-04-15 14:23:42 -0700649 * Invokes callback method defined in listening {@link InCallService} implementations.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700650 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800651 * @param videoProfile The requested video connection profile.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700652 */
653 public void receiveSessionModifyRequest(VideoProfile videoProfile) {
Tyler Gunn75958422015-04-15 14:23:42 -0700654 if (mVideoCallbacks != null) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700655 try {
Tyler Gunn75958422015-04-15 14:23:42 -0700656 for (IVideoCallback callback : mVideoCallbacks.values()) {
657 callback.receiveSessionModifyRequest(videoProfile);
658 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700659 } catch (RemoteException ignored) {
660 }
661 }
662 }
663
664 /**
Tyler Gunn75958422015-04-15 14:23:42 -0700665 * Invokes callback method defined in listening {@link InCallService} implementations.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700666 *
667 * @param status Status of the session modify request. Valid values are
668 * {@link VideoProvider#SESSION_MODIFY_REQUEST_SUCCESS},
669 * {@link VideoProvider#SESSION_MODIFY_REQUEST_FAIL},
670 * {@link VideoProvider#SESSION_MODIFY_REQUEST_INVALID}
671 * @param requestedProfile The original request which was sent to the remote device.
672 * @param responseProfile The actual profile changes made by the remote device.
673 */
674 public void receiveSessionModifyResponse(int status,
675 VideoProfile requestedProfile, VideoProfile responseProfile) {
Tyler Gunn75958422015-04-15 14:23:42 -0700676 if (mVideoCallbacks != null) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700677 try {
Tyler Gunn75958422015-04-15 14:23:42 -0700678 for (IVideoCallback callback : mVideoCallbacks.values()) {
679 callback.receiveSessionModifyResponse(status, requestedProfile,
680 responseProfile);
681 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700682 } catch (RemoteException ignored) {
683 }
684 }
685 }
686
687 /**
Tyler Gunn75958422015-04-15 14:23:42 -0700688 * Invokes callback method defined in listening {@link InCallService} implementations.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700689 *
690 * Valid values are: {@link VideoProvider#SESSION_EVENT_RX_PAUSE},
691 * {@link VideoProvider#SESSION_EVENT_RX_RESUME},
692 * {@link VideoProvider#SESSION_EVENT_TX_START},
693 * {@link VideoProvider#SESSION_EVENT_TX_STOP}
694 *
695 * @param event The event.
696 */
697 public void handleCallSessionEvent(int event) {
Tyler Gunn75958422015-04-15 14:23:42 -0700698 if (mVideoCallbacks != null) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700699 try {
Tyler Gunn75958422015-04-15 14:23:42 -0700700 for (IVideoCallback callback : mVideoCallbacks.values()) {
701 callback.handleCallSessionEvent(event);
702 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700703 } catch (RemoteException ignored) {
704 }
705 }
706 }
707
708 /**
Tyler Gunn75958422015-04-15 14:23:42 -0700709 * Invokes callback method defined in listening {@link InCallService} implementations.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700710 *
711 * @param width The updated peer video width.
712 * @param height The updated peer video height.
713 */
714 public void changePeerDimensions(int width, int height) {
Tyler Gunn75958422015-04-15 14:23:42 -0700715 if (mVideoCallbacks != null) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700716 try {
Tyler Gunn75958422015-04-15 14:23:42 -0700717 for (IVideoCallback callback : mVideoCallbacks.values()) {
718 callback.changePeerDimensions(width, height);
719 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700720 } catch (RemoteException ignored) {
721 }
722 }
723 }
724
725 /**
Tyler Gunn75958422015-04-15 14:23:42 -0700726 * Invokes callback method defined in listening {@link InCallService} implementations.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700727 *
728 * @param dataUsage The updated data usage.
729 */
Yorke Lee32f24732015-05-12 16:18:03 -0700730 public void setCallDataUsage(long dataUsage) {
Tyler Gunn75958422015-04-15 14:23:42 -0700731 if (mVideoCallbacks != null) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700732 try {
Tyler Gunn75958422015-04-15 14:23:42 -0700733 for (IVideoCallback callback : mVideoCallbacks.values()) {
734 callback.changeCallDataUsage(dataUsage);
735 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700736 } catch (RemoteException ignored) {
737 }
738 }
739 }
740
741 /**
Tyler Gunn75958422015-04-15 14:23:42 -0700742 * Invokes callback method defined in listening {@link InCallService} implementations.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700743 *
Yorke Lee32f24732015-05-12 16:18:03 -0700744 * @param dataUsage The updated data usage.
745 * @deprecated - Use {@link #setCallDataUsage(long)} instead.
746 * @hide
747 */
748 public void changeCallDataUsage(long dataUsage) {
749 setCallDataUsage(dataUsage);
750 }
751
752 /**
753 * Invokes callback method defined in listening {@link InCallService} implementations.
754 *
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700755 * @param cameraCapabilities The changed camera capabilities.
756 */
Yorke Lee400470f2015-05-12 13:31:25 -0700757 public void changeCameraCapabilities(VideoProfile.CameraCapabilities cameraCapabilities) {
Tyler Gunn75958422015-04-15 14:23:42 -0700758 if (mVideoCallbacks != null) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700759 try {
Tyler Gunn75958422015-04-15 14:23:42 -0700760 for (IVideoCallback callback : mVideoCallbacks.values()) {
761 callback.changeCameraCapabilities(cameraCapabilities);
762 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700763 } catch (RemoteException ignored) {
764 }
765 }
766 }
Rekha Kumar07366812015-03-24 16:42:31 -0700767
768 /**
Tyler Gunn75958422015-04-15 14:23:42 -0700769 * Invokes callback method defined in listening {@link InCallService} implementations.
Rekha Kumar07366812015-03-24 16:42:31 -0700770 *
Yorke Lee32f24732015-05-12 16:18:03 -0700771 * Allowed values:
772 * {@link VideoProfile#QUALITY_HIGH},
773 * {@link VideoProfile#QUALITY_MEDIUM},
774 * {@link VideoProfile#QUALITY_LOW},
775 * {@link VideoProfile#QUALITY_DEFAULT}.
776 *
Rekha Kumar07366812015-03-24 16:42:31 -0700777 * @param videoQuality The updated video quality.
778 */
779 public void changeVideoQuality(int videoQuality) {
Tyler Gunn75958422015-04-15 14:23:42 -0700780 if (mVideoCallbacks != null) {
Rekha Kumar07366812015-03-24 16:42:31 -0700781 try {
Tyler Gunn75958422015-04-15 14:23:42 -0700782 for (IVideoCallback callback : mVideoCallbacks.values()) {
783 callback.changeVideoQuality(videoQuality);
784 }
Rekha Kumar07366812015-03-24 16:42:31 -0700785 } catch (RemoteException ignored) {
786 }
787 }
788 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700789 }
790
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700791 private final Listener mConnectionDeathListener = new Listener() {
792 @Override
793 public void onDestroyed(Connection c) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800794 if (mConferenceables.remove(c)) {
795 fireOnConferenceableConnectionsChanged();
796 }
797 }
798 };
799
800 private final Conference.Listener mConferenceDeathListener = new Conference.Listener() {
801 @Override
802 public void onDestroyed(Conference c) {
803 if (mConferenceables.remove(c)) {
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700804 fireOnConferenceableConnectionsChanged();
805 }
806 }
807 };
808
Jay Shrauner229e3822014-08-15 09:23:07 -0700809 /**
810 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
811 * load factor before resizing, 1 means we only expect a single thread to
812 * access the map so make only a single shard
813 */
814 private final Set<Listener> mListeners = Collections.newSetFromMap(
815 new ConcurrentHashMap<Listener, Boolean>(8, 0.9f, 1));
Tyler Gunndf2cbc82015-04-20 09:13:01 -0700816 private final List<Conferenceable> mConferenceables = new ArrayList<>();
817 private final List<Conferenceable> mUnmodifiableConferenceables =
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800818 Collections.unmodifiableList(mConferenceables);
Santos Cordonb6939982014-06-04 20:20:58 -0700819
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700820 private int mState = STATE_NEW;
Yorke Lee4af59352015-05-13 14:14:54 -0700821 private CallAudioState mCallAudioState;
Andrew Lee100e2932014-09-08 15:34:24 -0700822 private Uri mAddress;
823 private int mAddressPresentation;
Sailesh Nepal61203862014-07-11 14:50:13 -0700824 private String mCallerDisplayName;
825 private int mCallerDisplayNamePresentation;
Andrew Lee100e2932014-09-08 15:34:24 -0700826 private boolean mRingbackRequested = false;
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800827 private int mConnectionCapabilities;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700828 private VideoProvider mVideoProvider;
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700829 private boolean mAudioModeIsVoip;
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700830 private StatusHints mStatusHints;
Tyler Gunnaa07df82014-07-17 07:50:22 -0700831 private int mVideoState;
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700832 private DisconnectCause mDisconnectCause;
Santos Cordon823fd3c2014-08-07 18:35:18 -0700833 private Conference mConference;
834 private ConnectionService mConnectionService;
Ihab Awad542e0ea2014-05-16 10:22:16 -0700835
836 /**
837 * Create a new Connection.
838 */
Santos Cordonf2951102014-07-20 19:06:29 -0700839 public Connection() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -0700840
841 /**
Andrew Lee100e2932014-09-08 15:34:24 -0700842 * @return The address (e.g., phone number) to which this Connection is currently communicating.
Ihab Awad542e0ea2014-05-16 10:22:16 -0700843 */
Andrew Lee100e2932014-09-08 15:34:24 -0700844 public final Uri getAddress() {
845 return mAddress;
Ihab Awad542e0ea2014-05-16 10:22:16 -0700846 }
847
848 /**
Andrew Lee100e2932014-09-08 15:34:24 -0700849 * @return The presentation requirements for the address.
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700850 * See {@link TelecomManager} for valid values.
Sailesh Nepal61203862014-07-11 14:50:13 -0700851 */
Andrew Lee100e2932014-09-08 15:34:24 -0700852 public final int getAddressPresentation() {
853 return mAddressPresentation;
Sailesh Nepal61203862014-07-11 14:50:13 -0700854 }
855
856 /**
857 * @return The caller display name (CNAP).
858 */
859 public final String getCallerDisplayName() {
860 return mCallerDisplayName;
861 }
862
863 /**
Nancy Chen9d568c02014-09-08 14:17:59 -0700864 * @return The presentation requirements for the handle.
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700865 * See {@link TelecomManager} for valid values.
Sailesh Nepal61203862014-07-11 14:50:13 -0700866 */
867 public final int getCallerDisplayNamePresentation() {
868 return mCallerDisplayNamePresentation;
869 }
870
871 /**
Ihab Awad542e0ea2014-05-16 10:22:16 -0700872 * @return The state of this Connection.
Ihab Awad542e0ea2014-05-16 10:22:16 -0700873 */
874 public final int getState() {
875 return mState;
876 }
877
878 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800879 * Returns the video state of the connection.
Yorke Lee32f24732015-05-12 16:18:03 -0700880 * Valid values: {@link VideoProfile#STATE_AUDIO_ONLY},
881 * {@link VideoProfile#STATE_BIDIRECTIONAL},
882 * {@link VideoProfile#STATE_TX_ENABLED},
883 * {@link VideoProfile#STATE_RX_ENABLED}.
Tyler Gunnaa07df82014-07-17 07:50:22 -0700884 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800885 * @return The video state of the connection.
Tyler Gunn27d1e252014-08-21 16:38:40 -0700886 * @hide
Tyler Gunnaa07df82014-07-17 07:50:22 -0700887 */
888 public final int getVideoState() {
889 return mVideoState;
890 }
891
892 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800893 * @return The audio state of the connection, describing how its audio is currently
Ihab Awad542e0ea2014-05-16 10:22:16 -0700894 * being routed by the system. This is {@code null} if this Connection
895 * does not directly know about its audio state.
Yorke Lee4af59352015-05-13 14:14:54 -0700896 * @deprecated Use {@link #getCallAudioState()} instead.
897 * @hide
Ihab Awad542e0ea2014-05-16 10:22:16 -0700898 */
Yorke Lee4af59352015-05-13 14:14:54 -0700899 @SystemApi
900 @Deprecated
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700901 public final AudioState getAudioState() {
Yorke Lee4af59352015-05-13 14:14:54 -0700902 return new AudioState(mCallAudioState);
903 }
904
905 /**
906 * @return The audio state of the connection, describing how its audio is currently
907 * being routed by the system. This is {@code null} if this Connection
908 * does not directly know about its audio state.
909 */
910 public final CallAudioState getCallAudioState() {
911 return mCallAudioState;
Ihab Awad542e0ea2014-05-16 10:22:16 -0700912 }
913
914 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700915 * @return The conference that this connection is a part of. Null if it is not part of any
916 * conference.
917 */
918 public final Conference getConference() {
919 return mConference;
920 }
921
922 /**
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700923 * Returns whether this connection is requesting that the system play a ringback tone
924 * on its behalf.
925 */
Andrew Lee100e2932014-09-08 15:34:24 -0700926 public final boolean isRingbackRequested() {
927 return mRingbackRequested;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700928 }
929
930 /**
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700931 * @return True if the connection's audio mode is VOIP.
932 */
933 public final boolean getAudioModeIsVoip() {
934 return mAudioModeIsVoip;
935 }
936
937 /**
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700938 * @return The status hints for this connection.
939 */
940 public final StatusHints getStatusHints() {
941 return mStatusHints;
942 }
943
944 /**
Ihab Awad542e0ea2014-05-16 10:22:16 -0700945 * Assign a listener to be notified of state changes.
946 *
947 * @param l A listener.
948 * @return This Connection.
949 *
950 * @hide
951 */
952 public final Connection addConnectionListener(Listener l) {
Santos Cordond34e5712014-08-05 18:54:03 +0000953 mListeners.add(l);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700954 return this;
955 }
956
957 /**
958 * Remove a previously assigned listener that was being notified of state changes.
959 *
960 * @param l A Listener.
961 * @return This Connection.
962 *
963 * @hide
964 */
965 public final Connection removeConnectionListener(Listener l) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700966 if (l != null) {
967 mListeners.remove(l);
968 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700969 return this;
970 }
971
972 /**
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700973 * @return The {@link DisconnectCause} for this connection.
Evan Charltonbf11f982014-07-20 22:06:28 -0700974 */
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700975 public final DisconnectCause getDisconnectCause() {
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700976 return mDisconnectCause;
Evan Charltonbf11f982014-07-20 22:06:28 -0700977 }
978
979 /**
Ihab Awad542e0ea2014-05-16 10:22:16 -0700980 * Inform this Connection that the state of its audio output has been changed externally.
981 *
982 * @param state The new audio state.
Sailesh Nepal400cc482014-06-26 12:04:00 -0700983 * @hide
Ihab Awad542e0ea2014-05-16 10:22:16 -0700984 */
Yorke Lee4af59352015-05-13 14:14:54 -0700985 final void setCallAudioState(CallAudioState state) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800986 checkImmutable();
Ihab Awad60ac30b2014-05-20 22:32:12 -0700987 Log.d(this, "setAudioState %s", state);
Yorke Lee4af59352015-05-13 14:14:54 -0700988 mCallAudioState = state;
989 onAudioStateChanged(getAudioState());
990 onCallAudioStateChanged(state);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700991 }
992
993 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700994 * @param state An integer value of a {@code STATE_*} constant.
Ihab Awad542e0ea2014-05-16 10:22:16 -0700995 * @return A string representation of the value.
996 */
997 public static String stateToString(int state) {
998 switch (state) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700999 case STATE_INITIALIZING:
1000 return "STATE_INITIALIZING";
1001 case STATE_NEW:
1002 return "STATE_NEW";
1003 case STATE_RINGING:
1004 return "STATE_RINGING";
1005 case STATE_DIALING:
1006 return "STATE_DIALING";
1007 case STATE_ACTIVE:
1008 return "STATE_ACTIVE";
1009 case STATE_HOLDING:
1010 return "STATE_HOLDING";
1011 case STATE_DISCONNECTED:
Ihab Awad542e0ea2014-05-16 10:22:16 -07001012 return "DISCONNECTED";
1013 default:
Ihab Awad60ac30b2014-05-20 22:32:12 -07001014 Log.wtf(Connection.class, "Unknown state %d", state);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001015 return "UNKNOWN";
1016 }
1017 }
1018
1019 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001020 * Returns the connection's capabilities, as a bit mask of the {@code CAPABILITY_*} constants.
Ihab Awad52a28f62014-06-18 10:26:34 -07001021 */
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001022 public final int getConnectionCapabilities() {
1023 return mConnectionCapabilities;
Ihab Awad52a28f62014-06-18 10:26:34 -07001024 }
1025
1026 /**
Andrew Lee100e2932014-09-08 15:34:24 -07001027 * Sets the value of the {@link #getAddress()} property.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001028 *
Andrew Lee100e2932014-09-08 15:34:24 -07001029 * @param address The new address.
1030 * @param presentation The presentation requirements for the address.
Tyler Gunnef9f6f92014-09-12 22:16:17 -07001031 * See {@link TelecomManager} for valid values.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001032 */
Andrew Lee100e2932014-09-08 15:34:24 -07001033 public final void setAddress(Uri address, int presentation) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001034 checkImmutable();
Andrew Lee100e2932014-09-08 15:34:24 -07001035 Log.d(this, "setAddress %s", address);
1036 mAddress = address;
1037 mAddressPresentation = presentation;
Santos Cordond34e5712014-08-05 18:54:03 +00001038 for (Listener l : mListeners) {
Andrew Lee100e2932014-09-08 15:34:24 -07001039 l.onAddressChanged(this, address, presentation);
Santos Cordond34e5712014-08-05 18:54:03 +00001040 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001041 }
1042
1043 /**
Sailesh Nepal61203862014-07-11 14:50:13 -07001044 * Sets the caller display name (CNAP).
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001045 *
Sailesh Nepal61203862014-07-11 14:50:13 -07001046 * @param callerDisplayName The new display name.
Nancy Chen9d568c02014-09-08 14:17:59 -07001047 * @param presentation The presentation requirements for the handle.
Tyler Gunnef9f6f92014-09-12 22:16:17 -07001048 * See {@link TelecomManager} for valid values.
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001049 */
Sailesh Nepal61203862014-07-11 14:50:13 -07001050 public final void setCallerDisplayName(String callerDisplayName, int presentation) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001051 checkImmutable();
Sailesh Nepal61203862014-07-11 14:50:13 -07001052 Log.d(this, "setCallerDisplayName %s", callerDisplayName);
Santos Cordond34e5712014-08-05 18:54:03 +00001053 mCallerDisplayName = callerDisplayName;
1054 mCallerDisplayNamePresentation = presentation;
1055 for (Listener l : mListeners) {
1056 l.onCallerDisplayNameChanged(this, callerDisplayName, presentation);
1057 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001058 }
1059
1060 /**
Tyler Gunnaa07df82014-07-17 07:50:22 -07001061 * Set the video state for the connection.
Yorke Lee32f24732015-05-12 16:18:03 -07001062 * Valid values: {@link VideoProfile#STATE_AUDIO_ONLY},
1063 * {@link VideoProfile#STATE_BIDIRECTIONAL},
1064 * {@link VideoProfile#STATE_TX_ENABLED},
1065 * {@link VideoProfile#STATE_RX_ENABLED}.
Tyler Gunnaa07df82014-07-17 07:50:22 -07001066 *
1067 * @param videoState The new video state.
1068 */
1069 public final void setVideoState(int videoState) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001070 checkImmutable();
Tyler Gunnaa07df82014-07-17 07:50:22 -07001071 Log.d(this, "setVideoState %d", videoState);
Santos Cordond34e5712014-08-05 18:54:03 +00001072 mVideoState = videoState;
1073 for (Listener l : mListeners) {
1074 l.onVideoStateChanged(this, mVideoState);
1075 }
Tyler Gunnaa07df82014-07-17 07:50:22 -07001076 }
1077
1078 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001079 * Sets state to active (e.g., an ongoing connection where two or more parties can actively
Ihab Awad542e0ea2014-05-16 10:22:16 -07001080 * communicate).
1081 */
Sailesh Nepal400cc482014-06-26 12:04:00 -07001082 public final void setActive() {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001083 checkImmutable();
Andrew Lee100e2932014-09-08 15:34:24 -07001084 setRingbackRequested(false);
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001085 setState(STATE_ACTIVE);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001086 }
1087
1088 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001089 * Sets state to ringing (e.g., an inbound ringing connection).
Ihab Awad542e0ea2014-05-16 10:22:16 -07001090 */
Sailesh Nepal400cc482014-06-26 12:04:00 -07001091 public final void setRinging() {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001092 checkImmutable();
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001093 setState(STATE_RINGING);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001094 }
1095
1096 /**
Evan Charltonbf11f982014-07-20 22:06:28 -07001097 * Sets state to initializing (this Connection is not yet ready to be used).
1098 */
1099 public final void setInitializing() {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001100 checkImmutable();
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001101 setState(STATE_INITIALIZING);
Evan Charltonbf11f982014-07-20 22:06:28 -07001102 }
1103
1104 /**
1105 * Sets state to initialized (the Connection has been set up and is now ready to be used).
1106 */
1107 public final void setInitialized() {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001108 checkImmutable();
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001109 setState(STATE_NEW);
Evan Charltonbf11f982014-07-20 22:06:28 -07001110 }
1111
1112 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001113 * Sets state to dialing (e.g., dialing an outbound connection).
Ihab Awad542e0ea2014-05-16 10:22:16 -07001114 */
Sailesh Nepal400cc482014-06-26 12:04:00 -07001115 public final void setDialing() {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001116 checkImmutable();
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001117 setState(STATE_DIALING);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001118 }
1119
1120 /**
1121 * Sets state to be on hold.
1122 */
Sailesh Nepal400cc482014-06-26 12:04:00 -07001123 public final void setOnHold() {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001124 checkImmutable();
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001125 setState(STATE_HOLDING);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001126 }
1127
1128 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001129 * Sets the video connection provider.
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001130 * @param videoProvider The video provider.
Andrew Lee5ffbe8b2014-06-20 16:29:33 -07001131 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001132 public final void setVideoProvider(VideoProvider videoProvider) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001133 checkImmutable();
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001134 mVideoProvider = videoProvider;
Santos Cordond34e5712014-08-05 18:54:03 +00001135 for (Listener l : mListeners) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001136 l.onVideoProviderChanged(this, videoProvider);
Santos Cordond34e5712014-08-05 18:54:03 +00001137 }
Andrew Lee5ffbe8b2014-06-20 16:29:33 -07001138 }
1139
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001140 public final VideoProvider getVideoProvider() {
1141 return mVideoProvider;
Andrew Leea27a1932014-07-09 17:07:13 -07001142 }
1143
Andrew Lee5ffbe8b2014-06-20 16:29:33 -07001144 /**
Sailesh Nepal091768c2014-06-30 15:15:23 -07001145 * Sets state to disconnected.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001146 *
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001147 * @param disconnectCause The reason for the disconnection, as specified by
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001148 * {@link DisconnectCause}.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001149 */
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001150 public final void setDisconnected(DisconnectCause disconnectCause) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001151 checkImmutable();
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001152 mDisconnectCause = disconnectCause;
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001153 setState(STATE_DISCONNECTED);
mike dooleyf34519b2014-09-16 17:33:40 -07001154 Log.d(this, "Disconnected with cause %s", disconnectCause);
Santos Cordond34e5712014-08-05 18:54:03 +00001155 for (Listener l : mListeners) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001156 l.onDisconnected(this, disconnectCause);
Santos Cordond34e5712014-08-05 18:54:03 +00001157 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001158 }
1159
1160 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001161 * Informs listeners that this {@code Connection} is in a post-dial wait state. This is done
1162 * when (a) the {@code Connection} is issuing a DTMF sequence; (b) it has encountered a "wait"
1163 * character; and (c) it wishes to inform the In-Call app that it is waiting for the end-user
1164 * to send an {@link #onPostDialContinue(boolean)} signal.
1165 *
1166 * @param remaining The DTMF character sequence remaining to be emitted once the
1167 * {@link #onPostDialContinue(boolean)} is received, including any "wait" characters
1168 * that remaining sequence may contain.
Sailesh Nepal091768c2014-06-30 15:15:23 -07001169 */
1170 public final void setPostDialWait(String remaining) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001171 checkImmutable();
Santos Cordond34e5712014-08-05 18:54:03 +00001172 for (Listener l : mListeners) {
1173 l.onPostDialWait(this, remaining);
1174 }
Sailesh Nepal091768c2014-06-30 15:15:23 -07001175 }
1176
1177 /**
Nancy Chen27d1c2d2014-12-15 16:12:50 -08001178 * Informs listeners that this {@code Connection} has processed a character in the post-dial
1179 * started state. This is done when (a) the {@code Connection} is issuing a DTMF sequence;
Sailesh Nepal1ed85612015-01-31 15:17:19 -08001180 * and (b) it wishes to signal Telecom to play the corresponding DTMF tone locally.
Nancy Chen27d1c2d2014-12-15 16:12:50 -08001181 *
1182 * @param nextChar The DTMF character that was just processed by the {@code Connection}.
Nancy Chen27d1c2d2014-12-15 16:12:50 -08001183 */
Sailesh Nepal1ed85612015-01-31 15:17:19 -08001184 public final void setNextPostDialChar(char nextChar) {
Nancy Chen27d1c2d2014-12-15 16:12:50 -08001185 checkImmutable();
1186 for (Listener l : mListeners) {
1187 l.onPostDialChar(this, nextChar);
1188 }
1189 }
1190
1191 /**
Ihab Awadf8358972014-05-28 16:46:42 -07001192 * Requests that the framework play a ringback tone. This is to be invoked by implementations
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001193 * that do not play a ringback tone themselves in the connection's audio stream.
Ihab Awadf8358972014-05-28 16:46:42 -07001194 *
1195 * @param ringback Whether the ringback tone is to be played.
1196 */
Andrew Lee100e2932014-09-08 15:34:24 -07001197 public final void setRingbackRequested(boolean ringback) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001198 checkImmutable();
Andrew Lee100e2932014-09-08 15:34:24 -07001199 if (mRingbackRequested != ringback) {
1200 mRingbackRequested = ringback;
Santos Cordond34e5712014-08-05 18:54:03 +00001201 for (Listener l : mListeners) {
Andrew Lee100e2932014-09-08 15:34:24 -07001202 l.onRingbackRequested(this, ringback);
Santos Cordond34e5712014-08-05 18:54:03 +00001203 }
1204 }
Ihab Awadf8358972014-05-28 16:46:42 -07001205 }
1206
1207 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001208 * Sets the connection's capabilities as a bit mask of the {@code CAPABILITY_*} constants.
Sailesh Nepal1a7061b2014-07-09 21:03:20 -07001209 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001210 * @param connectionCapabilities The new connection capabilities.
Santos Cordonb6939982014-06-04 20:20:58 -07001211 */
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001212 public final void setConnectionCapabilities(int connectionCapabilities) {
1213 checkImmutable();
1214 if (mConnectionCapabilities != connectionCapabilities) {
1215 mConnectionCapabilities = connectionCapabilities;
Santos Cordond34e5712014-08-05 18:54:03 +00001216 for (Listener l : mListeners) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001217 l.onConnectionCapabilitiesChanged(this, mConnectionCapabilities);
Santos Cordond34e5712014-08-05 18:54:03 +00001218 }
1219 }
Santos Cordonb6939982014-06-04 20:20:58 -07001220 }
1221
1222 /**
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001223 * Tears down the Connection object.
Santos Cordonb6939982014-06-04 20:20:58 -07001224 */
Evan Charlton36a71342014-07-19 16:31:02 -07001225 public final void destroy() {
Jay Shrauner229e3822014-08-15 09:23:07 -07001226 for (Listener l : mListeners) {
1227 l.onDestroyed(this);
Santos Cordond34e5712014-08-05 18:54:03 +00001228 }
Santos Cordonb6939982014-06-04 20:20:58 -07001229 }
1230
1231 /**
Sailesh Nepal33aaae42014-07-07 22:49:44 -07001232 * Requests that the framework use VOIP audio mode for this connection.
1233 *
1234 * @param isVoip True if the audio mode is VOIP.
1235 */
1236 public final void setAudioModeIsVoip(boolean isVoip) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001237 checkImmutable();
Santos Cordond34e5712014-08-05 18:54:03 +00001238 mAudioModeIsVoip = isVoip;
1239 for (Listener l : mListeners) {
1240 l.onAudioModeIsVoipChanged(this, isVoip);
1241 }
Sailesh Nepal33aaae42014-07-07 22:49:44 -07001242 }
1243
1244 /**
Sailesh Nepale7ef59a2014-07-08 21:48:22 -07001245 * Sets the label and icon status to display in the in-call UI.
1246 *
1247 * @param statusHints The status label and icon to set.
1248 */
1249 public final void setStatusHints(StatusHints statusHints) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001250 checkImmutable();
Santos Cordond34e5712014-08-05 18:54:03 +00001251 mStatusHints = statusHints;
1252 for (Listener l : mListeners) {
1253 l.onStatusHintsChanged(this, statusHints);
1254 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -07001255 }
1256
1257 /**
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001258 * Sets the connections with which this connection can be conferenced.
1259 *
1260 * @param conferenceableConnections The set of connections this connection can conference with.
1261 */
1262 public final void setConferenceableConnections(List<Connection> conferenceableConnections) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001263 checkImmutable();
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001264 clearConferenceableList();
1265 for (Connection c : conferenceableConnections) {
1266 // If statement checks for duplicates in input. It makes it N^2 but we're dealing with a
1267 // small amount of items here.
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001268 if (!mConferenceables.contains(c)) {
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001269 c.addConnectionListener(mConnectionDeathListener);
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001270 mConferenceables.add(c);
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001271 }
1272 }
1273 fireOnConferenceableConnectionsChanged();
1274 }
1275
1276 /**
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001277 * Similar to {@link #setConferenceableConnections(java.util.List)}, sets a list of connections
1278 * or conferences with which this connection can be conferenced.
1279 *
1280 * @param conferenceables The conferenceables.
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001281 */
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001282 public final void setConferenceables(List<Conferenceable> conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001283 clearConferenceableList();
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001284 for (Conferenceable c : conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001285 // If statement checks for duplicates in input. It makes it N^2 but we're dealing with a
1286 // small amount of items here.
1287 if (!mConferenceables.contains(c)) {
1288 if (c instanceof Connection) {
1289 Connection connection = (Connection) c;
1290 connection.addConnectionListener(mConnectionDeathListener);
1291 } else if (c instanceof Conference) {
1292 Conference conference = (Conference) c;
1293 conference.addListener(mConferenceDeathListener);
1294 }
1295 mConferenceables.add(c);
1296 }
1297 }
1298 fireOnConferenceableConnectionsChanged();
1299 }
1300
1301 /**
1302 * Returns the connections or conferences with which this connection can be conferenced.
1303 */
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001304 public final List<Conferenceable> getConferenceables() {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001305 return mUnmodifiableConferenceables;
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001306 }
1307
Evan Charlton8635c572014-09-24 14:04:51 -07001308 /*
Santos Cordon823fd3c2014-08-07 18:35:18 -07001309 * @hide
1310 */
1311 public final void setConnectionService(ConnectionService connectionService) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001312 checkImmutable();
Santos Cordon823fd3c2014-08-07 18:35:18 -07001313 if (mConnectionService != null) {
1314 Log.e(this, new Exception(), "Trying to set ConnectionService on a connection " +
1315 "which is already associated with another ConnectionService.");
1316 } else {
1317 mConnectionService = connectionService;
1318 }
1319 }
1320
1321 /**
1322 * @hide
1323 */
1324 public final void unsetConnectionService(ConnectionService connectionService) {
1325 if (mConnectionService != connectionService) {
1326 Log.e(this, new Exception(), "Trying to remove ConnectionService from a Connection " +
1327 "that does not belong to the ConnectionService.");
1328 } else {
1329 mConnectionService = null;
1330 }
1331 }
1332
1333 /**
Santos Cordonaf1b2962014-10-16 19:23:54 -07001334 * @hide
1335 */
1336 public final ConnectionService getConnectionService() {
1337 return mConnectionService;
1338 }
1339
1340 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001341 * Sets the conference that this connection is a part of. This will fail if the connection is
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001342 * already part of a conference. {@link #resetConference} to un-set the conference first.
Santos Cordon823fd3c2014-08-07 18:35:18 -07001343 *
1344 * @param conference The conference.
1345 * @return {@code true} if the conference was successfully set.
1346 * @hide
1347 */
1348 public final boolean setConference(Conference conference) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001349 checkImmutable();
Santos Cordon823fd3c2014-08-07 18:35:18 -07001350 // We check to see if it is already part of another conference.
Santos Cordon0159ac02014-08-21 14:28:11 -07001351 if (mConference == null) {
Santos Cordon823fd3c2014-08-07 18:35:18 -07001352 mConference = conference;
Santos Cordon0159ac02014-08-21 14:28:11 -07001353 if (mConnectionService != null && mConnectionService.containsConference(conference)) {
1354 fireConferenceChanged();
1355 }
Santos Cordon823fd3c2014-08-07 18:35:18 -07001356 return true;
1357 }
1358 return false;
1359 }
1360
1361 /**
1362 * Resets the conference that this connection is a part of.
1363 * @hide
1364 */
1365 public final void resetConference() {
1366 if (mConference != null) {
Santos Cordon0159ac02014-08-21 14:28:11 -07001367 Log.d(this, "Conference reset");
Santos Cordon823fd3c2014-08-07 18:35:18 -07001368 mConference = null;
1369 fireConferenceChanged();
1370 }
1371 }
1372
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001373 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001374 * Notifies this Connection that the {@link #getAudioState()} property has a new value.
Sailesh Nepal400cc482014-06-26 12:04:00 -07001375 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001376 * @param state The new connection audio state.
Yorke Lee4af59352015-05-13 14:14:54 -07001377 * @deprecated Use {@link #onCallAudioStateChanged(CallAudioState)} instead.
1378 * @hide
Sailesh Nepal400cc482014-06-26 12:04:00 -07001379 */
Yorke Lee4af59352015-05-13 14:14:54 -07001380 @SystemApi
1381 @Deprecated
Nancy Chen354b2bd2014-09-08 18:27:26 -07001382 public void onAudioStateChanged(AudioState state) {}
Sailesh Nepal400cc482014-06-26 12:04:00 -07001383
1384 /**
Yorke Lee4af59352015-05-13 14:14:54 -07001385 * Notifies this Connection that the {@link #getCallAudioState()} property has a new value.
1386 *
1387 * @param state The new connection audio state.
1388 */
1389 public void onCallAudioStateChanged(CallAudioState state) {}
1390
1391 /**
Evan Charltonbf11f982014-07-20 22:06:28 -07001392 * Notifies this Connection of an internal state change. This method is called after the
1393 * state is changed.
Ihab Awadf8358972014-05-28 16:46:42 -07001394 *
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001395 * @param state The new state, one of the {@code STATE_*} constants.
Ihab Awadf8358972014-05-28 16:46:42 -07001396 */
Nancy Chen354b2bd2014-09-08 18:27:26 -07001397 public void onStateChanged(int state) {}
Ihab Awadf8358972014-05-28 16:46:42 -07001398
1399 /**
Ihab Awad542e0ea2014-05-16 10:22:16 -07001400 * Notifies this Connection of a request to play a DTMF tone.
1401 *
1402 * @param c A DTMF character.
1403 */
Santos Cordonf2951102014-07-20 19:06:29 -07001404 public void onPlayDtmfTone(char c) {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001405
1406 /**
1407 * Notifies this Connection of a request to stop any currently playing DTMF tones.
1408 */
Santos Cordonf2951102014-07-20 19:06:29 -07001409 public void onStopDtmfTone() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001410
1411 /**
1412 * Notifies this Connection of a request to disconnect.
1413 */
Santos Cordonf2951102014-07-20 19:06:29 -07001414 public void onDisconnect() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001415
1416 /**
Tyler Gunn3b4b1dc2014-11-04 14:53:37 -08001417 * Notifies this Connection of a request to disconnect a participant of the conference managed
1418 * by the connection.
1419 *
1420 * @param endpoint the {@link Uri} of the participant to disconnect.
1421 * @hide
1422 */
1423 public void onDisconnectConferenceParticipant(Uri endpoint) {}
1424
1425 /**
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001426 * Notifies this Connection of a request to separate from its parent conference.
Santos Cordonb6939982014-06-04 20:20:58 -07001427 */
Santos Cordonf2951102014-07-20 19:06:29 -07001428 public void onSeparate() {}
Santos Cordonb6939982014-06-04 20:20:58 -07001429
1430 /**
Ihab Awad542e0ea2014-05-16 10:22:16 -07001431 * Notifies this Connection of a request to abort.
1432 */
Santos Cordonf2951102014-07-20 19:06:29 -07001433 public void onAbort() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001434
1435 /**
1436 * Notifies this Connection of a request to hold.
1437 */
Santos Cordonf2951102014-07-20 19:06:29 -07001438 public void onHold() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001439
1440 /**
1441 * Notifies this Connection of a request to exit a hold state.
1442 */
Santos Cordonf2951102014-07-20 19:06:29 -07001443 public void onUnhold() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001444
1445 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001446 * Notifies this Connection, which is in {@link #STATE_RINGING}, of
Santos Cordond34e5712014-08-05 18:54:03 +00001447 * a request to accept.
Andrew Lee8da4c3c2014-07-16 10:11:42 -07001448 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001449 * @param videoState The video state in which to answer the connection.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001450 */
Santos Cordonf2951102014-07-20 19:06:29 -07001451 public void onAnswer(int videoState) {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001452
1453 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001454 * Notifies this Connection, which is in {@link #STATE_RINGING}, of
Tyler Gunnbe74de02014-08-29 14:51:48 -07001455 * a request to accept.
1456 */
1457 public void onAnswer() {
1458 onAnswer(VideoProfile.VideoState.AUDIO_ONLY);
1459 }
1460
1461 /**
1462 * Notifies this Connection, which is in {@link #STATE_RINGING}, of
Santos Cordond34e5712014-08-05 18:54:03 +00001463 * a request to reject.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001464 */
Santos Cordonf2951102014-07-20 19:06:29 -07001465 public void onReject() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001466
Evan Charlton6dea4ac2014-06-03 14:07:13 -07001467 /**
1468 * Notifies this Connection whether the user wishes to proceed with the post-dial DTMF codes.
1469 */
Santos Cordonf2951102014-07-20 19:06:29 -07001470 public void onPostDialContinue(boolean proceed) {}
Evan Charlton6dea4ac2014-06-03 14:07:13 -07001471
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001472 static String toLogSafePhoneNumber(String number) {
1473 // For unknown number, log empty string.
1474 if (number == null) {
1475 return "";
1476 }
1477
1478 if (PII_DEBUG) {
1479 // When PII_DEBUG is true we emit PII.
1480 return number;
1481 }
1482
1483 // Do exactly same thing as Uri#toSafeString() does, which will enable us to compare
1484 // sanitized phone numbers.
1485 StringBuilder builder = new StringBuilder();
1486 for (int i = 0; i < number.length(); i++) {
1487 char c = number.charAt(i);
1488 if (c == '-' || c == '@' || c == '.') {
1489 builder.append(c);
1490 } else {
1491 builder.append('x');
1492 }
1493 }
1494 return builder.toString();
1495 }
1496
Ihab Awad542e0ea2014-05-16 10:22:16 -07001497 private void setState(int state) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001498 checkImmutable();
Ihab Awad6107bab2014-08-18 09:23:25 -07001499 if (mState == STATE_DISCONNECTED && mState != state) {
1500 Log.d(this, "Connection already DISCONNECTED; cannot transition out of this state.");
Evan Charltonbf11f982014-07-20 22:06:28 -07001501 return;
Sailesh Nepal400cc482014-06-26 12:04:00 -07001502 }
Evan Charltonbf11f982014-07-20 22:06:28 -07001503 if (mState != state) {
1504 Log.d(this, "setState: %s", stateToString(state));
1505 mState = state;
Nancy Chen354b2bd2014-09-08 18:27:26 -07001506 onStateChanged(state);
Evan Charltonbf11f982014-07-20 22:06:28 -07001507 for (Listener l : mListeners) {
1508 l.onStateChanged(this, state);
1509 }
Evan Charltonbf11f982014-07-20 22:06:28 -07001510 }
1511 }
1512
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001513 private static class FailureSignalingConnection extends Connection {
Ihab Awad90e34e32014-12-01 16:23:17 -08001514 private boolean mImmutable = false;
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001515 public FailureSignalingConnection(DisconnectCause disconnectCause) {
1516 setDisconnected(disconnectCause);
Ihab Awad90e34e32014-12-01 16:23:17 -08001517 mImmutable = true;
Ihab Awad6107bab2014-08-18 09:23:25 -07001518 }
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001519
1520 public void checkImmutable() {
Ihab Awad90e34e32014-12-01 16:23:17 -08001521 if (mImmutable) {
1522 throw new UnsupportedOperationException("Connection is immutable");
1523 }
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001524 }
Ihab Awad6107bab2014-08-18 09:23:25 -07001525 }
1526
Evan Charltonbf11f982014-07-20 22:06:28 -07001527 /**
Ihab Awad6107bab2014-08-18 09:23:25 -07001528 * Return a {@code Connection} which represents a failed connection attempt. The returned
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001529 * {@code Connection} will have a {@link android.telecom.DisconnectCause} and as specified,
1530 * and a {@link #getState()} of {@link #STATE_DISCONNECTED}.
Ihab Awad6107bab2014-08-18 09:23:25 -07001531 * <p>
1532 * The returned {@code Connection} can be assumed to {@link #destroy()} itself when appropriate,
1533 * so users of this method need not maintain a reference to its return value to destroy it.
Evan Charltonbf11f982014-07-20 22:06:28 -07001534 *
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001535 * @param disconnectCause The disconnect cause, ({@see android.telecomm.DisconnectCause}).
Ihab Awad6107bab2014-08-18 09:23:25 -07001536 * @return A {@code Connection} which indicates failure.
Evan Charltonbf11f982014-07-20 22:06:28 -07001537 */
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001538 public static Connection createFailedConnection(DisconnectCause disconnectCause) {
1539 return new FailureSignalingConnection(disconnectCause);
Evan Charltonbf11f982014-07-20 22:06:28 -07001540 }
1541
Evan Charltonbf11f982014-07-20 22:06:28 -07001542 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001543 * Override to throw an {@link UnsupportedOperationException} if this {@code Connection} is
1544 * not intended to be mutated, e.g., if it is a marker for failure. Only for framework use;
1545 * this should never be un-@hide-den.
1546 *
1547 * @hide
1548 */
1549 public void checkImmutable() {}
1550
1551 /**
Ihab Awad6107bab2014-08-18 09:23:25 -07001552 * Return a {@code Connection} which represents a canceled connection attempt. The returned
1553 * {@code Connection} will have state {@link #STATE_DISCONNECTED}, and cannot be moved out of
1554 * that state. This connection should not be used for anything, and no other
1555 * {@code Connection}s should be attempted.
1556 * <p>
Ihab Awad6107bab2014-08-18 09:23:25 -07001557 * so users of this method need not maintain a reference to its return value to destroy it.
Evan Charltonbf11f982014-07-20 22:06:28 -07001558 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001559 * @return A {@code Connection} which indicates that the underlying connection should
1560 * be canceled.
Evan Charltonbf11f982014-07-20 22:06:28 -07001561 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001562 public static Connection createCanceledConnection() {
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001563 return new FailureSignalingConnection(new DisconnectCause(DisconnectCause.CANCELED));
Ihab Awad542e0ea2014-05-16 10:22:16 -07001564 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001565
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001566 private final void fireOnConferenceableConnectionsChanged() {
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001567 for (Listener l : mListeners) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001568 l.onConferenceablesChanged(this, getConferenceables());
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001569 }
1570 }
1571
Santos Cordon823fd3c2014-08-07 18:35:18 -07001572 private final void fireConferenceChanged() {
1573 for (Listener l : mListeners) {
1574 l.onConferenceChanged(this, mConference);
1575 }
1576 }
1577
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001578 private final void clearConferenceableList() {
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001579 for (Conferenceable c : mConferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001580 if (c instanceof Connection) {
1581 Connection connection = (Connection) c;
1582 connection.removeConnectionListener(mConnectionDeathListener);
1583 } else if (c instanceof Conference) {
1584 Conference conference = (Conference) c;
1585 conference.removeListener(mConferenceDeathListener);
1586 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001587 }
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001588 mConferenceables.clear();
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001589 }
Tyler Gunn3bffcf72014-10-28 13:51:27 -07001590
1591 /**
Anthony Lee17455a32015-04-24 15:25:29 -07001592 * Notifies listeners that the merge request failed.
1593 *
1594 * @hide
1595 */
1596 protected final void notifyConferenceMergeFailed() {
1597 for (Listener l : mListeners) {
1598 l.onConferenceMergeFailed(this);
1599 }
1600 }
1601
1602 /**
Tyler Gunnab4650c2014-11-06 20:06:23 -08001603 * Notifies listeners of a change to conference participant(s).
Tyler Gunn3bffcf72014-10-28 13:51:27 -07001604 *
Tyler Gunnab4650c2014-11-06 20:06:23 -08001605 * @param conferenceParticipants The participants.
Tyler Gunn3bffcf72014-10-28 13:51:27 -07001606 * @hide
1607 */
Tyler Gunnab4650c2014-11-06 20:06:23 -08001608 protected final void updateConferenceParticipants(
1609 List<ConferenceParticipant> conferenceParticipants) {
Tyler Gunn3bffcf72014-10-28 13:51:27 -07001610 for (Listener l : mListeners) {
Tyler Gunnab4650c2014-11-06 20:06:23 -08001611 l.onConferenceParticipantsChanged(this, conferenceParticipants);
Tyler Gunn3bffcf72014-10-28 13:51:27 -07001612 }
1613 }
Tyler Gunn8a2b1192015-01-29 11:47:24 -08001614
1615 /**
1616 * Notifies listeners that a conference call has been started.
Jay Shrauner55b97522015-04-09 15:15:43 -07001617 * @hide
Tyler Gunn8a2b1192015-01-29 11:47:24 -08001618 */
1619 protected void notifyConferenceStarted() {
1620 for (Listener l : mListeners) {
1621 l.onConferenceStarted();
1622 }
1623 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001624}