blob: cd10050935b1085064c50d70f601bbf37019d559 [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 Gunnef9f6f92014-09-12 22:16:17 -070019import com.android.internal.telecom.IVideoCallback;
20import com.android.internal.telecom.IVideoProvider;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070021
Ihab Awad542e0ea2014-05-16 10:22:16 -070022import android.net.Uri;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070023import android.os.Handler;
24import android.os.IBinder;
25import android.os.Message;
26import android.os.RemoteException;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070027import android.view.Surface;
Ihab Awad542e0ea2014-05-16 10:22:16 -070028
Santos Cordonb6939982014-06-04 20:20:58 -070029import java.util.ArrayList;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070030import java.util.Collections;
Tyler Gunn75958422015-04-15 14:23:42 -070031import java.util.HashMap;
Santos Cordonb6939982014-06-04 20:20:58 -070032import java.util.List;
Ihab Awad542e0ea2014-05-16 10:22:16 -070033import java.util.Set;
Jay Shrauner229e3822014-08-15 09:23:07 -070034import java.util.concurrent.ConcurrentHashMap;
Ihab Awad542e0ea2014-05-16 10:22:16 -070035
36/**
37 * Represents a connection to a remote endpoint that carries voice traffic.
Ihab Awad6107bab2014-08-18 09:23:25 -070038 * <p>
39 * Implementations create a custom subclass of {@code Connection} and return it to the framework
40 * as the return value of
41 * {@link ConnectionService#onCreateIncomingConnection(PhoneAccountHandle, ConnectionRequest)}
42 * or
43 * {@link ConnectionService#onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
44 * Implementations are then responsible for updating the state of the {@code Connection}, and
45 * must call {@link #destroy()} to signal to the framework that the {@code Connection} is no
46 * longer used and associated resources may be recovered.
Ihab Awad542e0ea2014-05-16 10:22:16 -070047 */
Tyler Gunndf2cbc82015-04-20 09:13:01 -070048public abstract class Connection implements Conferenceable {
Ihab Awad542e0ea2014-05-16 10:22:16 -070049
Ihab Awadb19a0bc2014-08-07 19:46:01 -070050 public static final int STATE_INITIALIZING = 0;
51
52 public static final int STATE_NEW = 1;
53
54 public static final int STATE_RINGING = 2;
55
56 public static final int STATE_DIALING = 3;
57
58 public static final int STATE_ACTIVE = 4;
59
60 public static final int STATE_HOLDING = 5;
61
62 public static final int STATE_DISCONNECTED = 6;
63
Ihab Awad5c9c86e2014-11-12 13:41:16 -080064 /** Connection can currently be put on hold or unheld. */
65 public static final int CAPABILITY_HOLD = 0x00000001;
66
67 /** Connection supports the hold feature. */
68 public static final int CAPABILITY_SUPPORT_HOLD = 0x00000002;
69
70 /**
71 * Connections within a conference can be merged. A {@link ConnectionService} has the option to
72 * add a {@link Conference} before the child {@link Connection}s are merged. This is how
73 * CDMA-based {@link Connection}s are implemented. For these unmerged {@link Conference}s, this
74 * capability allows a merge button to be shown while the conference is in the foreground
75 * of the in-call UI.
76 * <p>
77 * This is only intended for use by a {@link Conference}.
78 */
79 public static final int CAPABILITY_MERGE_CONFERENCE = 0x00000004;
80
81 /**
82 * Connections within a conference can be swapped between foreground and background.
83 * See {@link #CAPABILITY_MERGE_CONFERENCE} for additional information.
84 * <p>
85 * This is only intended for use by a {@link Conference}.
86 */
87 public static final int CAPABILITY_SWAP_CONFERENCE = 0x00000008;
88
89 /**
90 * @hide
91 */
92 public static final int CAPABILITY_UNUSED = 0x00000010;
93
94 /** Connection supports responding via text option. */
95 public static final int CAPABILITY_RESPOND_VIA_TEXT = 0x00000020;
96
97 /** Connection can be muted. */
98 public static final int CAPABILITY_MUTE = 0x00000040;
99
100 /**
101 * Connection supports conference management. This capability only applies to
102 * {@link Conference}s which can have {@link Connection}s as children.
103 */
104 public static final int CAPABILITY_MANAGE_CONFERENCE = 0x00000080;
105
106 /**
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700107 * Local device supports receiving video.
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800108 * @hide
109 */
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 * @hide
115 */
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 * @hide
121 */
Andrew Lee9a8f9ce2015-04-10 18:09:46 -0700122 public static final int CAPABILITY_SUPPORTS_VT_LOCAL_BIDIRECTIONAL =
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700123 CAPABILITY_SUPPORTS_VT_LOCAL_RX | CAPABILITY_SUPPORTS_VT_LOCAL_TX;
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800124
125 /**
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700126 * Remote device supports receiving video.
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800127 * @hide
128 */
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700129 public static final int CAPABILITY_SUPPORTS_VT_REMOTE_RX = 0x00000400;
130
131 /**
132 * Remote device supports transmitting video.
133 * @hide
134 */
135 public static final int CAPABILITY_SUPPORTS_VT_REMOTE_TX = 0x00000800;
136
137 /**
138 * Remote device supports bidirectional video calling.
139 * @hide
140 */
Andrew Lee9a8f9ce2015-04-10 18:09:46 -0700141 public static final int CAPABILITY_SUPPORTS_VT_REMOTE_BIDIRECTIONAL =
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700142 CAPABILITY_SUPPORTS_VT_REMOTE_RX | CAPABILITY_SUPPORTS_VT_REMOTE_TX;
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800143
144 /**
145 * Connection is able to be separated from its parent {@code Conference}, if any.
146 */
147 public static final int CAPABILITY_SEPARATE_FROM_CONFERENCE = 0x00001000;
148
149 /**
150 * Connection is able to be individually disconnected when in a {@code Conference}.
151 */
152 public static final int CAPABILITY_DISCONNECT_FROM_CONFERENCE = 0x00002000;
153
154 /**
155 * Whether the call is a generic conference, where we do not know the precise state of
156 * participants in the conference (eg. on CDMA).
157 *
158 * @hide
159 */
160 public static final int CAPABILITY_GENERIC_CONFERENCE = 0x00004000;
161
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700162 /**
163 * Connection is using high definition audio.
164 * @hide
165 */
166 public static final int CAPABILITY_HIGH_DEF_AUDIO = 0x00008000;
167
168 /**
169 * Connection is using WIFI.
170 * @hide
171 */
Tyler Gunnd11a3152015-03-18 13:09:14 -0700172 public static final int CAPABILITY_WIFI = 0x00010000;
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700173
Tyler Gunn068085b2015-02-06 13:56:52 -0800174 /**
175 * Indicates that the current device callback number should be shown.
176 *
177 * @hide
178 */
Tyler Gunn96d6c402015-03-18 12:39:23 -0700179 public static final int CAPABILITY_SHOW_CALLBACK_NUMBER = 0x00020000;
Tyler Gunn068085b2015-02-06 13:56:52 -0800180
Tyler Gunn96d6c402015-03-18 12:39:23 -0700181 /**
Dong Zhou89f41eb2015-03-15 11:59:49 -0500182 * Speed up audio setup for MT call.
183 * @hide
Tyler Gunn96d6c402015-03-18 12:39:23 -0700184 */
185 public static final int CAPABILITY_SPEED_UP_MT_AUDIO = 0x00040000;
Tyler Gunn068085b2015-02-06 13:56:52 -0800186
Rekha Kumar07366812015-03-24 16:42:31 -0700187 /**
Tyler Gunnb5e0cfb2015-04-07 16:10:51 -0700188 * Call can be upgraded to a video call.
Rekha Kumar07366812015-03-24 16:42:31 -0700189 * @hide
190 */
191 public static final int CAPABILITY_CAN_UPGRADE_TO_VIDEO = 0x00080000;
192
Tyler Gunnb5e0cfb2015-04-07 16:10:51 -0700193 /**
194 * For video calls, indicates whether the outgoing video for the call can be paused using
195 * the {@link android.telecom.VideoProfile.VideoState#PAUSED} VideoState.
196 * @hide
197 */
198 public static final int CAPABILITY_CAN_PAUSE_VIDEO = 0x00100000;
199
Tyler Gunn96d6c402015-03-18 12:39:23 -0700200 //**********************************************************************************************
Tyler Gunnb5e0cfb2015-04-07 16:10:51 -0700201 // Next CAPABILITY value: 0x00200000
Tyler Gunn96d6c402015-03-18 12:39:23 -0700202 //**********************************************************************************************
Tyler Gunn068085b2015-02-06 13:56:52 -0800203
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700204 // Flag controlling whether PII is emitted into the logs
205 private static final boolean PII_DEBUG = Log.isLoggable(android.util.Log.DEBUG);
206
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800207 /**
208 * Whether the given capabilities support the specified capability.
209 *
210 * @param capabilities A capability bit field.
211 * @param capability The capability to check capabilities for.
212 * @return Whether the specified capability is supported.
213 * @hide
214 */
215 public static boolean can(int capabilities, int capability) {
216 return (capabilities & capability) != 0;
217 }
218
219 /**
220 * Whether the capabilities of this {@code Connection} supports the specified capability.
221 *
222 * @param capability The capability to check capabilities for.
223 * @return Whether the specified capability is supported.
224 * @hide
225 */
226 public boolean can(int capability) {
227 return can(mConnectionCapabilities, capability);
228 }
229
230 /**
231 * Removes the specified capability from the set of capabilities of this {@code Connection}.
232 *
233 * @param capability The capability to remove from the set.
234 * @hide
235 */
236 public void removeCapability(int capability) {
237 mConnectionCapabilities &= ~capability;
238 }
239
240 /**
241 * Adds the specified capability to the set of capabilities of this {@code Connection}.
242 *
243 * @param capability The capability to add to the set.
244 * @hide
245 */
246 public void addCapability(int capability) {
247 mConnectionCapabilities |= capability;
248 }
249
250
251 public static String capabilitiesToString(int capabilities) {
252 StringBuilder builder = new StringBuilder();
253 builder.append("[Capabilities:");
254 if (can(capabilities, CAPABILITY_HOLD)) {
255 builder.append(" CAPABILITY_HOLD");
256 }
257 if (can(capabilities, CAPABILITY_SUPPORT_HOLD)) {
258 builder.append(" CAPABILITY_SUPPORT_HOLD");
259 }
260 if (can(capabilities, CAPABILITY_MERGE_CONFERENCE)) {
261 builder.append(" CAPABILITY_MERGE_CONFERENCE");
262 }
263 if (can(capabilities, CAPABILITY_SWAP_CONFERENCE)) {
264 builder.append(" CAPABILITY_SWAP_CONFERENCE");
265 }
266 if (can(capabilities, CAPABILITY_RESPOND_VIA_TEXT)) {
267 builder.append(" CAPABILITY_RESPOND_VIA_TEXT");
268 }
269 if (can(capabilities, CAPABILITY_MUTE)) {
270 builder.append(" CAPABILITY_MUTE");
271 }
272 if (can(capabilities, CAPABILITY_MANAGE_CONFERENCE)) {
273 builder.append(" CAPABILITY_MANAGE_CONFERENCE");
274 }
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700275 if (can(capabilities, CAPABILITY_SUPPORTS_VT_LOCAL_RX)) {
276 builder.append(" CAPABILITY_SUPPORTS_VT_LOCAL_RX");
277 }
278 if (can(capabilities, CAPABILITY_SUPPORTS_VT_LOCAL_TX)) {
279 builder.append(" CAPABILITY_SUPPORTS_VT_LOCAL_TX");
280 }
Andrew Lee9a8f9ce2015-04-10 18:09:46 -0700281 if (can(capabilities, CAPABILITY_SUPPORTS_VT_LOCAL_BIDIRECTIONAL)) {
282 builder.append(" CAPABILITY_SUPPORTS_VT_LOCAL_BIDIRECTIONAL");
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800283 }
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700284 if (can(capabilities, CAPABILITY_SUPPORTS_VT_REMOTE_RX)) {
285 builder.append(" CAPABILITY_SUPPORTS_VT_REMOTE_RX");
286 }
287 if (can(capabilities, CAPABILITY_SUPPORTS_VT_REMOTE_TX)) {
288 builder.append(" CAPABILITY_SUPPORTS_VT_REMOTE_TX");
289 }
Andrew Lee9a8f9ce2015-04-10 18:09:46 -0700290 if (can(capabilities, CAPABILITY_SUPPORTS_VT_REMOTE_BIDIRECTIONAL)) {
291 builder.append(" CAPABILITY_SUPPORTS_VT_REMOTE_BIDIRECTIONAL");
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800292 }
Andrew Lee80fff3c2014-11-25 17:36:51 -0800293 if (can(capabilities, CAPABILITY_HIGH_DEF_AUDIO)) {
294 builder.append(" CAPABILITY_HIGH_DEF_AUDIO");
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800295 }
Andrew Lee1a8ae3e2015-02-02 13:42:38 -0800296 if (can(capabilities, CAPABILITY_WIFI)) {
297 builder.append(" CAPABILITY_WIFI");
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800298 }
299 if (can(capabilities, CAPABILITY_GENERIC_CONFERENCE)) {
300 builder.append(" CAPABILITY_GENERIC_CONFERENCE");
301 }
Tyler Gunn068085b2015-02-06 13:56:52 -0800302 if (can(capabilities, CAPABILITY_SHOW_CALLBACK_NUMBER)) {
303 builder.append(" CAPABILITY_SHOW_CALLBACK_NUMBER");
304 }
Dong Zhou89f41eb2015-03-15 11:59:49 -0500305 if (can(capabilities, CAPABILITY_SPEED_UP_MT_AUDIO)) {
Tyler Gunnd11a3152015-03-18 13:09:14 -0700306 builder.append(" CAPABILITY_SPEED_UP_MT_AUDIO");
Dong Zhou89f41eb2015-03-15 11:59:49 -0500307 }
Rekha Kumar07366812015-03-24 16:42:31 -0700308 if (can(capabilities, CAPABILITY_CAN_UPGRADE_TO_VIDEO)) {
309 builder.append(" CAPABILITY_CAN_UPGRADE_TO_VIDEO");
310 }
Tyler Gunnb5e0cfb2015-04-07 16:10:51 -0700311 if (can(capabilities, CAPABILITY_CAN_PAUSE_VIDEO)) {
312 builder.append(" CAPABILITY_CAN_PAUSE_VIDEO");
313 }
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800314 builder.append("]");
315 return builder.toString();
316 }
317
Sailesh Nepal091768c2014-06-30 15:15:23 -0700318 /** @hide */
Sailesh Nepal61203862014-07-11 14:50:13 -0700319 public abstract static class Listener {
Ihab Awad542e0ea2014-05-16 10:22:16 -0700320 public void onStateChanged(Connection c, int state) {}
Andrew Lee100e2932014-09-08 15:34:24 -0700321 public void onAddressChanged(Connection c, Uri newAddress, int presentation) {}
Sailesh Nepal61203862014-07-11 14:50:13 -0700322 public void onCallerDisplayNameChanged(
323 Connection c, String callerDisplayName, int presentation) {}
Tyler Gunnaa07df82014-07-17 07:50:22 -0700324 public void onVideoStateChanged(Connection c, int videoState) {}
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700325 public void onDisconnected(Connection c, DisconnectCause disconnectCause) {}
Sailesh Nepal091768c2014-06-30 15:15:23 -0700326 public void onPostDialWait(Connection c, String remaining) {}
Nancy Chen27d1c2d2014-12-15 16:12:50 -0800327 public void onPostDialChar(Connection c, char nextChar) {}
Andrew Lee100e2932014-09-08 15:34:24 -0700328 public void onRingbackRequested(Connection c, boolean ringback) {}
Sailesh Nepal61203862014-07-11 14:50:13 -0700329 public void onDestroyed(Connection c) {}
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800330 public void onConnectionCapabilitiesChanged(Connection c, int capabilities) {}
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700331 public void onVideoProviderChanged(
332 Connection c, VideoProvider videoProvider) {}
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700333 public void onAudioModeIsVoipChanged(Connection c, boolean isVoip) {}
334 public void onStatusHintsChanged(Connection c, StatusHints statusHints) {}
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800335 public void onConferenceablesChanged(
Tyler Gunndf2cbc82015-04-20 09:13:01 -0700336 Connection c, List<Conferenceable> conferenceables) {}
Santos Cordon823fd3c2014-08-07 18:35:18 -0700337 public void onConferenceChanged(Connection c, Conference conference) {}
Tyler Gunn3bffcf72014-10-28 13:51:27 -0700338 /** @hide */
Tyler Gunnab4650c2014-11-06 20:06:23 -0800339 public void onConferenceParticipantsChanged(Connection c,
340 List<ConferenceParticipant> participants) {}
Tyler Gunn8a2b1192015-01-29 11:47:24 -0800341 public void onConferenceStarted() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -0700342 }
343
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700344 public static abstract class VideoProvider {
Ihab Awad542e0ea2014-05-16 10:22:16 -0700345
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700346 /**
347 * Video is not being received (no protocol pause was issued).
348 */
349 public static final int SESSION_EVENT_RX_PAUSE = 1;
Evan Charltonbf11f982014-07-20 22:06:28 -0700350
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700351 /**
352 * Video reception has resumed after a SESSION_EVENT_RX_PAUSE.
353 */
354 public static final int SESSION_EVENT_RX_RESUME = 2;
355
356 /**
357 * Video transmission has begun. This occurs after a negotiated start of video transmission
358 * when the underlying protocol has actually begun transmitting video to the remote party.
359 */
360 public static final int SESSION_EVENT_TX_START = 3;
361
362 /**
363 * Video transmission has stopped. This occurs after a negotiated stop of video transmission
364 * when the underlying protocol has actually stopped transmitting video to the remote party.
365 */
366 public static final int SESSION_EVENT_TX_STOP = 4;
367
368 /**
369 * A camera failure has occurred for the selected camera. The In-Call UI can use this as a
370 * cue to inform the user the camera is not available.
371 */
372 public static final int SESSION_EVENT_CAMERA_FAILURE = 5;
373
374 /**
375 * Issued after {@code SESSION_EVENT_CAMERA_FAILURE} when the camera is once again ready for
376 * operation. The In-Call UI can use this as a cue to inform the user that the camera has
377 * become available again.
378 */
379 public static final int SESSION_EVENT_CAMERA_READY = 6;
380
381 /**
382 * Session modify request was successful.
383 */
384 public static final int SESSION_MODIFY_REQUEST_SUCCESS = 1;
385
386 /**
387 * Session modify request failed.
388 */
389 public static final int SESSION_MODIFY_REQUEST_FAIL = 2;
390
391 /**
392 * Session modify request ignored due to invalid parameters.
393 */
394 public static final int SESSION_MODIFY_REQUEST_INVALID = 3;
395
Rekha Kumar07366812015-03-24 16:42:31 -0700396 /**
397 * Session modify request timed out.
398 */
399 public static final int SESSION_MODIFY_REQUEST_TIMED_OUT = 4;
400
401 /**
402 * Session modify request rejected by remote UE.
403 */
404 public static final int SESSION_MODIFY_REQUEST_REJECTED_BY_REMOTE = 5;
405
Tyler Gunn75958422015-04-15 14:23:42 -0700406 private static final int MSG_ADD_VIDEO_CALLBACK = 1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700407 private static final int MSG_SET_CAMERA = 2;
408 private static final int MSG_SET_PREVIEW_SURFACE = 3;
409 private static final int MSG_SET_DISPLAY_SURFACE = 4;
410 private static final int MSG_SET_DEVICE_ORIENTATION = 5;
411 private static final int MSG_SET_ZOOM = 6;
412 private static final int MSG_SEND_SESSION_MODIFY_REQUEST = 7;
413 private static final int MSG_SEND_SESSION_MODIFY_RESPONSE = 8;
414 private static final int MSG_REQUEST_CAMERA_CAPABILITIES = 9;
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800415 private static final int MSG_REQUEST_CONNECTION_DATA_USAGE = 10;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700416 private static final int MSG_SET_PAUSE_IMAGE = 11;
Tyler Gunn75958422015-04-15 14:23:42 -0700417 private static final int MSG_REMOVE_VIDEO_CALLBACK = 12;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700418
419 private final VideoProvider.VideoProviderHandler
420 mMessageHandler = new VideoProvider.VideoProviderHandler();
421 private final VideoProvider.VideoProviderBinder mBinder;
Tyler Gunn75958422015-04-15 14:23:42 -0700422
423 /**
424 * Stores a list of the video callbacks, keyed by IBinder.
425 */
426 private HashMap<IBinder, IVideoCallback> mVideoCallbacks = new HashMap<>();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700427
428 /**
429 * Default handler used to consolidate binder method calls onto a single thread.
430 */
431 private final class VideoProviderHandler extends Handler {
432 @Override
433 public void handleMessage(Message msg) {
434 switch (msg.what) {
Tyler Gunn75958422015-04-15 14:23:42 -0700435 case MSG_ADD_VIDEO_CALLBACK: {
436 IBinder binder = (IBinder) msg.obj;
437 IVideoCallback callback = IVideoCallback.Stub
438 .asInterface((IBinder) msg.obj);
439 if (mVideoCallbacks.containsKey(binder)) {
440 Log.i(this, "addVideoProvider - skipped; already present.");
441 break;
442 }
443 mVideoCallbacks.put(binder, callback);
444 Log.i(this, "addVideoProvider "+ mVideoCallbacks.size());
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700445 break;
Tyler Gunn75958422015-04-15 14:23:42 -0700446 }
447 case MSG_REMOVE_VIDEO_CALLBACK: {
448 IBinder binder = (IBinder) msg.obj;
449 IVideoCallback callback = IVideoCallback.Stub
450 .asInterface((IBinder) msg.obj);
451 if (!mVideoCallbacks.containsKey(binder)) {
452 Log.i(this, "removeVideoProvider - skipped; not present.");
453 break;
454 }
455 mVideoCallbacks.remove(binder);
456 break;
457 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700458 case MSG_SET_CAMERA:
459 onSetCamera((String) msg.obj);
460 break;
461 case MSG_SET_PREVIEW_SURFACE:
462 onSetPreviewSurface((Surface) msg.obj);
463 break;
464 case MSG_SET_DISPLAY_SURFACE:
465 onSetDisplaySurface((Surface) msg.obj);
466 break;
467 case MSG_SET_DEVICE_ORIENTATION:
468 onSetDeviceOrientation(msg.arg1);
469 break;
470 case MSG_SET_ZOOM:
471 onSetZoom((Float) msg.obj);
472 break;
473 case MSG_SEND_SESSION_MODIFY_REQUEST:
474 onSendSessionModifyRequest((VideoProfile) msg.obj);
475 break;
476 case MSG_SEND_SESSION_MODIFY_RESPONSE:
477 onSendSessionModifyResponse((VideoProfile) msg.obj);
478 break;
479 case MSG_REQUEST_CAMERA_CAPABILITIES:
480 onRequestCameraCapabilities();
481 break;
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800482 case MSG_REQUEST_CONNECTION_DATA_USAGE:
483 onRequestConnectionDataUsage();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700484 break;
485 case MSG_SET_PAUSE_IMAGE:
486 onSetPauseImage((String) msg.obj);
487 break;
488 default:
489 break;
490 }
491 }
492 }
493
494 /**
495 * IVideoProvider stub implementation.
496 */
497 private final class VideoProviderBinder extends IVideoProvider.Stub {
Tyler Gunn75958422015-04-15 14:23:42 -0700498 public void addVideoCallback(IBinder videoCallbackBinder) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700499 mMessageHandler.obtainMessage(
Tyler Gunn75958422015-04-15 14:23:42 -0700500 MSG_ADD_VIDEO_CALLBACK, videoCallbackBinder).sendToTarget();
501 }
502
503 public void removeVideoCallback(IBinder videoCallbackBinder) {
504 mMessageHandler.obtainMessage(
505 MSG_REMOVE_VIDEO_CALLBACK, videoCallbackBinder).sendToTarget();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700506 }
507
508 public void setCamera(String cameraId) {
509 mMessageHandler.obtainMessage(MSG_SET_CAMERA, cameraId).sendToTarget();
510 }
511
512 public void setPreviewSurface(Surface surface) {
513 mMessageHandler.obtainMessage(MSG_SET_PREVIEW_SURFACE, surface).sendToTarget();
514 }
515
516 public void setDisplaySurface(Surface surface) {
517 mMessageHandler.obtainMessage(MSG_SET_DISPLAY_SURFACE, surface).sendToTarget();
518 }
519
520 public void setDeviceOrientation(int rotation) {
Rekha Kumar07366812015-03-24 16:42:31 -0700521 mMessageHandler.obtainMessage(
522 MSG_SET_DEVICE_ORIENTATION, rotation, 0).sendToTarget();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700523 }
524
525 public void setZoom(float value) {
526 mMessageHandler.obtainMessage(MSG_SET_ZOOM, value).sendToTarget();
527 }
528
529 public void sendSessionModifyRequest(VideoProfile requestProfile) {
530 mMessageHandler.obtainMessage(
531 MSG_SEND_SESSION_MODIFY_REQUEST, requestProfile).sendToTarget();
532 }
533
534 public void sendSessionModifyResponse(VideoProfile responseProfile) {
535 mMessageHandler.obtainMessage(
536 MSG_SEND_SESSION_MODIFY_RESPONSE, responseProfile).sendToTarget();
537 }
538
539 public void requestCameraCapabilities() {
540 mMessageHandler.obtainMessage(MSG_REQUEST_CAMERA_CAPABILITIES).sendToTarget();
541 }
542
543 public void requestCallDataUsage() {
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800544 mMessageHandler.obtainMessage(MSG_REQUEST_CONNECTION_DATA_USAGE).sendToTarget();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700545 }
546
547 public void setPauseImage(String uri) {
548 mMessageHandler.obtainMessage(MSG_SET_PAUSE_IMAGE, uri).sendToTarget();
549 }
550 }
551
552 public VideoProvider() {
553 mBinder = new VideoProvider.VideoProviderBinder();
554 }
555
556 /**
557 * Returns binder object which can be used across IPC methods.
558 * @hide
559 */
560 public final IVideoProvider getInterface() {
561 return mBinder;
562 }
563
564 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800565 * Sets the camera to be used for video recording in a video connection.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700566 *
567 * @param cameraId The id of the camera.
568 */
569 public abstract void onSetCamera(String cameraId);
570
571 /**
572 * Sets the surface to be used for displaying a preview of what the user's camera is
573 * currently capturing. When video transmission is enabled, this is the video signal which
574 * is sent to the remote device.
575 *
576 * @param surface The surface.
577 */
578 public abstract void onSetPreviewSurface(Surface surface);
579
580 /**
581 * Sets the surface to be used for displaying the video received from the remote device.
582 *
583 * @param surface The surface.
584 */
585 public abstract void onSetDisplaySurface(Surface surface);
586
587 /**
588 * Sets the device orientation, in degrees. Assumes that a standard portrait orientation of
589 * the device is 0 degrees.
590 *
591 * @param rotation The device orientation, in degrees.
592 */
593 public abstract void onSetDeviceOrientation(int rotation);
594
595 /**
596 * Sets camera zoom ratio.
597 *
598 * @param value The camera zoom ratio.
599 */
600 public abstract void onSetZoom(float value);
601
602 /**
603 * Issues a request to modify the properties of the current session. The request is
604 * sent to the remote device where it it handled by the In-Call UI.
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800605 * Some examples of session modification requests: upgrade connection from audio to video,
606 * downgrade connection from video to audio, pause video.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700607 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800608 * @param requestProfile The requested connection video properties.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700609 */
610 public abstract void onSendSessionModifyRequest(VideoProfile requestProfile);
611
612 /**te
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800613 * Provides a response to a request to change the current connection session video
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700614 * properties.
615 * This is in response to a request the InCall UI has received via the InCall UI.
616 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800617 * @param responseProfile The response connection video properties.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700618 */
619 public abstract void onSendSessionModifyResponse(VideoProfile responseProfile);
620
621 /**
622 * Issues a request to the video provider to retrieve the camera capabilities.
623 * Camera capabilities are reported back to the caller via the In-Call UI.
624 */
625 public abstract void onRequestCameraCapabilities();
626
627 /**
628 * Issues a request to the video telephony framework to retrieve the cumulative data usage
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800629 * for the current connection. Data usage is reported back to the caller via the
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700630 * InCall UI.
631 */
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800632 public abstract void onRequestConnectionDataUsage();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700633
634 /**
635 * Provides the video telephony framework with the URI of an image to be displayed to remote
636 * devices when the video signal is paused.
637 *
638 * @param uri URI of image to display.
639 */
640 public abstract void onSetPauseImage(String uri);
641
642 /**
Tyler Gunn75958422015-04-15 14:23:42 -0700643 * Invokes callback method defined in listening {@link InCallService} implementations.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700644 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800645 * @param videoProfile The requested video connection profile.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700646 */
647 public void receiveSessionModifyRequest(VideoProfile videoProfile) {
Tyler Gunn75958422015-04-15 14:23:42 -0700648 if (mVideoCallbacks != null) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700649 try {
Tyler Gunn75958422015-04-15 14:23:42 -0700650 for (IVideoCallback callback : mVideoCallbacks.values()) {
651 callback.receiveSessionModifyRequest(videoProfile);
652 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700653 } catch (RemoteException ignored) {
654 }
655 }
656 }
657
658 /**
Tyler Gunn75958422015-04-15 14:23:42 -0700659 * Invokes callback method defined in listening {@link InCallService} implementations.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700660 *
661 * @param status Status of the session modify request. Valid values are
662 * {@link VideoProvider#SESSION_MODIFY_REQUEST_SUCCESS},
663 * {@link VideoProvider#SESSION_MODIFY_REQUEST_FAIL},
664 * {@link VideoProvider#SESSION_MODIFY_REQUEST_INVALID}
665 * @param requestedProfile The original request which was sent to the remote device.
666 * @param responseProfile The actual profile changes made by the remote device.
667 */
668 public void receiveSessionModifyResponse(int status,
669 VideoProfile requestedProfile, VideoProfile responseProfile) {
Tyler Gunn75958422015-04-15 14:23:42 -0700670 if (mVideoCallbacks != null) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700671 try {
Tyler Gunn75958422015-04-15 14:23:42 -0700672 for (IVideoCallback callback : mVideoCallbacks.values()) {
673 callback.receiveSessionModifyResponse(status, requestedProfile,
674 responseProfile);
675 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700676 } catch (RemoteException ignored) {
677 }
678 }
679 }
680
681 /**
Tyler Gunn75958422015-04-15 14:23:42 -0700682 * Invokes callback method defined in listening {@link InCallService} implementations.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700683 *
684 * Valid values are: {@link VideoProvider#SESSION_EVENT_RX_PAUSE},
685 * {@link VideoProvider#SESSION_EVENT_RX_RESUME},
686 * {@link VideoProvider#SESSION_EVENT_TX_START},
687 * {@link VideoProvider#SESSION_EVENT_TX_STOP}
688 *
689 * @param event The event.
690 */
691 public void handleCallSessionEvent(int event) {
Tyler Gunn75958422015-04-15 14:23:42 -0700692 if (mVideoCallbacks != null) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700693 try {
Tyler Gunn75958422015-04-15 14:23:42 -0700694 for (IVideoCallback callback : mVideoCallbacks.values()) {
695 callback.handleCallSessionEvent(event);
696 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700697 } catch (RemoteException ignored) {
698 }
699 }
700 }
701
702 /**
Tyler Gunn75958422015-04-15 14:23:42 -0700703 * Invokes callback method defined in listening {@link InCallService} implementations.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700704 *
705 * @param width The updated peer video width.
706 * @param height The updated peer video height.
707 */
708 public void changePeerDimensions(int width, int height) {
Tyler Gunn75958422015-04-15 14:23:42 -0700709 if (mVideoCallbacks != null) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700710 try {
Tyler Gunn75958422015-04-15 14:23:42 -0700711 for (IVideoCallback callback : mVideoCallbacks.values()) {
712 callback.changePeerDimensions(width, height);
713 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700714 } catch (RemoteException ignored) {
715 }
716 }
717 }
718
719 /**
Tyler Gunn75958422015-04-15 14:23:42 -0700720 * Invokes callback method defined in listening {@link InCallService} implementations.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700721 *
722 * @param dataUsage The updated data usage.
723 */
Rekha Kumar07366812015-03-24 16:42:31 -0700724 public void changeCallDataUsage(long dataUsage) {
Tyler Gunn75958422015-04-15 14:23:42 -0700725 if (mVideoCallbacks != null) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700726 try {
Tyler Gunn75958422015-04-15 14:23:42 -0700727 for (IVideoCallback callback : mVideoCallbacks.values()) {
728 callback.changeCallDataUsage(dataUsage);
729 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700730 } catch (RemoteException ignored) {
731 }
732 }
733 }
734
735 /**
Tyler Gunn75958422015-04-15 14:23:42 -0700736 * Invokes callback method defined in listening {@link InCallService} implementations.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700737 *
738 * @param cameraCapabilities The changed camera capabilities.
739 */
740 public void changeCameraCapabilities(CameraCapabilities cameraCapabilities) {
Tyler Gunn75958422015-04-15 14:23:42 -0700741 if (mVideoCallbacks != null) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700742 try {
Tyler Gunn75958422015-04-15 14:23:42 -0700743 for (IVideoCallback callback : mVideoCallbacks.values()) {
744 callback.changeCameraCapabilities(cameraCapabilities);
745 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700746 } catch (RemoteException ignored) {
747 }
748 }
749 }
Rekha Kumar07366812015-03-24 16:42:31 -0700750
751 /**
Tyler Gunn75958422015-04-15 14:23:42 -0700752 * Invokes callback method defined in listening {@link InCallService} implementations.
Rekha Kumar07366812015-03-24 16:42:31 -0700753 *
754 * @param videoQuality The updated video quality.
755 */
756 public void changeVideoQuality(int videoQuality) {
Tyler Gunn75958422015-04-15 14:23:42 -0700757 if (mVideoCallbacks != null) {
Rekha Kumar07366812015-03-24 16:42:31 -0700758 try {
Tyler Gunn75958422015-04-15 14:23:42 -0700759 for (IVideoCallback callback : mVideoCallbacks.values()) {
760 callback.changeVideoQuality(videoQuality);
761 }
Rekha Kumar07366812015-03-24 16:42:31 -0700762 } catch (RemoteException ignored) {
763 }
764 }
765 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700766 }
767
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700768 private final Listener mConnectionDeathListener = new Listener() {
769 @Override
770 public void onDestroyed(Connection c) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800771 if (mConferenceables.remove(c)) {
772 fireOnConferenceableConnectionsChanged();
773 }
774 }
775 };
776
777 private final Conference.Listener mConferenceDeathListener = new Conference.Listener() {
778 @Override
779 public void onDestroyed(Conference c) {
780 if (mConferenceables.remove(c)) {
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700781 fireOnConferenceableConnectionsChanged();
782 }
783 }
784 };
785
Jay Shrauner229e3822014-08-15 09:23:07 -0700786 /**
787 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
788 * load factor before resizing, 1 means we only expect a single thread to
789 * access the map so make only a single shard
790 */
791 private final Set<Listener> mListeners = Collections.newSetFromMap(
792 new ConcurrentHashMap<Listener, Boolean>(8, 0.9f, 1));
Tyler Gunndf2cbc82015-04-20 09:13:01 -0700793 private final List<Conferenceable> mConferenceables = new ArrayList<>();
794 private final List<Conferenceable> mUnmodifiableConferenceables =
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800795 Collections.unmodifiableList(mConferenceables);
Santos Cordonb6939982014-06-04 20:20:58 -0700796
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700797 private int mState = STATE_NEW;
798 private AudioState mAudioState;
Andrew Lee100e2932014-09-08 15:34:24 -0700799 private Uri mAddress;
800 private int mAddressPresentation;
Sailesh Nepal61203862014-07-11 14:50:13 -0700801 private String mCallerDisplayName;
802 private int mCallerDisplayNamePresentation;
Andrew Lee100e2932014-09-08 15:34:24 -0700803 private boolean mRingbackRequested = false;
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800804 private int mConnectionCapabilities;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700805 private VideoProvider mVideoProvider;
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700806 private boolean mAudioModeIsVoip;
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700807 private StatusHints mStatusHints;
Tyler Gunnaa07df82014-07-17 07:50:22 -0700808 private int mVideoState;
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700809 private DisconnectCause mDisconnectCause;
Santos Cordon823fd3c2014-08-07 18:35:18 -0700810 private Conference mConference;
811 private ConnectionService mConnectionService;
Ihab Awad542e0ea2014-05-16 10:22:16 -0700812
813 /**
814 * Create a new Connection.
815 */
Santos Cordonf2951102014-07-20 19:06:29 -0700816 public Connection() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -0700817
818 /**
Andrew Lee100e2932014-09-08 15:34:24 -0700819 * @return The address (e.g., phone number) to which this Connection is currently communicating.
Ihab Awad542e0ea2014-05-16 10:22:16 -0700820 */
Andrew Lee100e2932014-09-08 15:34:24 -0700821 public final Uri getAddress() {
822 return mAddress;
Ihab Awad542e0ea2014-05-16 10:22:16 -0700823 }
824
825 /**
Andrew Lee100e2932014-09-08 15:34:24 -0700826 * @return The presentation requirements for the address.
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700827 * See {@link TelecomManager} for valid values.
Sailesh Nepal61203862014-07-11 14:50:13 -0700828 */
Andrew Lee100e2932014-09-08 15:34:24 -0700829 public final int getAddressPresentation() {
830 return mAddressPresentation;
Sailesh Nepal61203862014-07-11 14:50:13 -0700831 }
832
833 /**
834 * @return The caller display name (CNAP).
835 */
836 public final String getCallerDisplayName() {
837 return mCallerDisplayName;
838 }
839
840 /**
Nancy Chen9d568c02014-09-08 14:17:59 -0700841 * @return The presentation requirements for the handle.
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700842 * See {@link TelecomManager} for valid values.
Sailesh Nepal61203862014-07-11 14:50:13 -0700843 */
844 public final int getCallerDisplayNamePresentation() {
845 return mCallerDisplayNamePresentation;
846 }
847
848 /**
Ihab Awad542e0ea2014-05-16 10:22:16 -0700849 * @return The state of this Connection.
Ihab Awad542e0ea2014-05-16 10:22:16 -0700850 */
851 public final int getState() {
852 return mState;
853 }
854
855 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800856 * Returns the video state of the connection.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700857 * Valid values: {@link VideoProfile.VideoState#AUDIO_ONLY},
858 * {@link VideoProfile.VideoState#BIDIRECTIONAL},
859 * {@link VideoProfile.VideoState#TX_ENABLED},
860 * {@link VideoProfile.VideoState#RX_ENABLED}.
Tyler Gunnaa07df82014-07-17 07:50:22 -0700861 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800862 * @return The video state of the connection.
Tyler Gunn27d1e252014-08-21 16:38:40 -0700863 * @hide
Tyler Gunnaa07df82014-07-17 07:50:22 -0700864 */
865 public final int getVideoState() {
866 return mVideoState;
867 }
868
869 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800870 * @return The audio state of the connection, describing how its audio is currently
Ihab Awad542e0ea2014-05-16 10:22:16 -0700871 * being routed by the system. This is {@code null} if this Connection
872 * does not directly know about its audio state.
873 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700874 public final AudioState getAudioState() {
875 return mAudioState;
Ihab Awad542e0ea2014-05-16 10:22:16 -0700876 }
877
878 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700879 * @return The conference that this connection is a part of. Null if it is not part of any
880 * conference.
881 */
882 public final Conference getConference() {
883 return mConference;
884 }
885
886 /**
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700887 * Returns whether this connection is requesting that the system play a ringback tone
888 * on its behalf.
889 */
Andrew Lee100e2932014-09-08 15:34:24 -0700890 public final boolean isRingbackRequested() {
891 return mRingbackRequested;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700892 }
893
894 /**
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700895 * @return True if the connection's audio mode is VOIP.
896 */
897 public final boolean getAudioModeIsVoip() {
898 return mAudioModeIsVoip;
899 }
900
901 /**
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700902 * @return The status hints for this connection.
903 */
904 public final StatusHints getStatusHints() {
905 return mStatusHints;
906 }
907
908 /**
Ihab Awad542e0ea2014-05-16 10:22:16 -0700909 * Assign a listener to be notified of state changes.
910 *
911 * @param l A listener.
912 * @return This Connection.
913 *
914 * @hide
915 */
916 public final Connection addConnectionListener(Listener l) {
Santos Cordond34e5712014-08-05 18:54:03 +0000917 mListeners.add(l);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700918 return this;
919 }
920
921 /**
922 * Remove a previously assigned listener that was being notified of state changes.
923 *
924 * @param l A Listener.
925 * @return This Connection.
926 *
927 * @hide
928 */
929 public final Connection removeConnectionListener(Listener l) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700930 if (l != null) {
931 mListeners.remove(l);
932 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700933 return this;
934 }
935
936 /**
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700937 * @return The {@link DisconnectCause} for this connection.
Evan Charltonbf11f982014-07-20 22:06:28 -0700938 */
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700939 public final DisconnectCause getDisconnectCause() {
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700940 return mDisconnectCause;
Evan Charltonbf11f982014-07-20 22:06:28 -0700941 }
942
943 /**
Ihab Awad542e0ea2014-05-16 10:22:16 -0700944 * Inform this Connection that the state of its audio output has been changed externally.
945 *
946 * @param state The new audio state.
Sailesh Nepal400cc482014-06-26 12:04:00 -0700947 * @hide
Ihab Awad542e0ea2014-05-16 10:22:16 -0700948 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700949 final void setAudioState(AudioState state) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800950 checkImmutable();
Ihab Awad60ac30b2014-05-20 22:32:12 -0700951 Log.d(this, "setAudioState %s", state);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700952 mAudioState = state;
Nancy Chen354b2bd2014-09-08 18:27:26 -0700953 onAudioStateChanged(state);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700954 }
955
956 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700957 * @param state An integer value of a {@code STATE_*} constant.
Ihab Awad542e0ea2014-05-16 10:22:16 -0700958 * @return A string representation of the value.
959 */
960 public static String stateToString(int state) {
961 switch (state) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700962 case STATE_INITIALIZING:
963 return "STATE_INITIALIZING";
964 case STATE_NEW:
965 return "STATE_NEW";
966 case STATE_RINGING:
967 return "STATE_RINGING";
968 case STATE_DIALING:
969 return "STATE_DIALING";
970 case STATE_ACTIVE:
971 return "STATE_ACTIVE";
972 case STATE_HOLDING:
973 return "STATE_HOLDING";
974 case STATE_DISCONNECTED:
Ihab Awad542e0ea2014-05-16 10:22:16 -0700975 return "DISCONNECTED";
976 default:
Ihab Awad60ac30b2014-05-20 22:32:12 -0700977 Log.wtf(Connection.class, "Unknown state %d", state);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700978 return "UNKNOWN";
979 }
980 }
981
982 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800983 * Returns the connection's capabilities, as a bit mask of the {@code CAPABILITY_*} constants.
Ihab Awad52a28f62014-06-18 10:26:34 -0700984 */
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800985 public final int getConnectionCapabilities() {
986 return mConnectionCapabilities;
Ihab Awad52a28f62014-06-18 10:26:34 -0700987 }
988
989 /**
Andrew Lee100e2932014-09-08 15:34:24 -0700990 * Sets the value of the {@link #getAddress()} property.
Ihab Awad542e0ea2014-05-16 10:22:16 -0700991 *
Andrew Lee100e2932014-09-08 15:34:24 -0700992 * @param address The new address.
993 * @param presentation The presentation requirements for the address.
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700994 * See {@link TelecomManager} for valid values.
Ihab Awad542e0ea2014-05-16 10:22:16 -0700995 */
Andrew Lee100e2932014-09-08 15:34:24 -0700996 public final void setAddress(Uri address, int presentation) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800997 checkImmutable();
Andrew Lee100e2932014-09-08 15:34:24 -0700998 Log.d(this, "setAddress %s", address);
999 mAddress = address;
1000 mAddressPresentation = presentation;
Santos Cordond34e5712014-08-05 18:54:03 +00001001 for (Listener l : mListeners) {
Andrew Lee100e2932014-09-08 15:34:24 -07001002 l.onAddressChanged(this, address, presentation);
Santos Cordond34e5712014-08-05 18:54:03 +00001003 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001004 }
1005
1006 /**
Sailesh Nepal61203862014-07-11 14:50:13 -07001007 * Sets the caller display name (CNAP).
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001008 *
Sailesh Nepal61203862014-07-11 14:50:13 -07001009 * @param callerDisplayName The new display name.
Nancy Chen9d568c02014-09-08 14:17:59 -07001010 * @param presentation The presentation requirements for the handle.
Tyler Gunnef9f6f92014-09-12 22:16:17 -07001011 * See {@link TelecomManager} for valid values.
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001012 */
Sailesh Nepal61203862014-07-11 14:50:13 -07001013 public final void setCallerDisplayName(String callerDisplayName, int presentation) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001014 checkImmutable();
Sailesh Nepal61203862014-07-11 14:50:13 -07001015 Log.d(this, "setCallerDisplayName %s", callerDisplayName);
Santos Cordond34e5712014-08-05 18:54:03 +00001016 mCallerDisplayName = callerDisplayName;
1017 mCallerDisplayNamePresentation = presentation;
1018 for (Listener l : mListeners) {
1019 l.onCallerDisplayNameChanged(this, callerDisplayName, presentation);
1020 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001021 }
1022
1023 /**
Tyler Gunnaa07df82014-07-17 07:50:22 -07001024 * Set the video state for the connection.
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001025 * Valid values: {@link VideoProfile.VideoState#AUDIO_ONLY},
1026 * {@link VideoProfile.VideoState#BIDIRECTIONAL},
1027 * {@link VideoProfile.VideoState#TX_ENABLED},
1028 * {@link VideoProfile.VideoState#RX_ENABLED}.
Tyler Gunnaa07df82014-07-17 07:50:22 -07001029 *
1030 * @param videoState The new video state.
Tyler Gunn27d1e252014-08-21 16:38:40 -07001031 * @hide
Tyler Gunnaa07df82014-07-17 07:50:22 -07001032 */
1033 public final void setVideoState(int videoState) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001034 checkImmutable();
Tyler Gunnaa07df82014-07-17 07:50:22 -07001035 Log.d(this, "setVideoState %d", videoState);
Santos Cordond34e5712014-08-05 18:54:03 +00001036 mVideoState = videoState;
1037 for (Listener l : mListeners) {
1038 l.onVideoStateChanged(this, mVideoState);
1039 }
Tyler Gunnaa07df82014-07-17 07:50:22 -07001040 }
1041
1042 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001043 * Sets state to active (e.g., an ongoing connection where two or more parties can actively
Ihab Awad542e0ea2014-05-16 10:22:16 -07001044 * communicate).
1045 */
Sailesh Nepal400cc482014-06-26 12:04:00 -07001046 public final void setActive() {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001047 checkImmutable();
Andrew Lee100e2932014-09-08 15:34:24 -07001048 setRingbackRequested(false);
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001049 setState(STATE_ACTIVE);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001050 }
1051
1052 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001053 * Sets state to ringing (e.g., an inbound ringing connection).
Ihab Awad542e0ea2014-05-16 10:22:16 -07001054 */
Sailesh Nepal400cc482014-06-26 12:04:00 -07001055 public final void setRinging() {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001056 checkImmutable();
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001057 setState(STATE_RINGING);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001058 }
1059
1060 /**
Evan Charltonbf11f982014-07-20 22:06:28 -07001061 * Sets state to initializing (this Connection is not yet ready to be used).
1062 */
1063 public final void setInitializing() {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001064 checkImmutable();
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001065 setState(STATE_INITIALIZING);
Evan Charltonbf11f982014-07-20 22:06:28 -07001066 }
1067
1068 /**
1069 * Sets state to initialized (the Connection has been set up and is now ready to be used).
1070 */
1071 public final void setInitialized() {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001072 checkImmutable();
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001073 setState(STATE_NEW);
Evan Charltonbf11f982014-07-20 22:06:28 -07001074 }
1075
1076 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001077 * Sets state to dialing (e.g., dialing an outbound connection).
Ihab Awad542e0ea2014-05-16 10:22:16 -07001078 */
Sailesh Nepal400cc482014-06-26 12:04:00 -07001079 public final void setDialing() {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001080 checkImmutable();
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001081 setState(STATE_DIALING);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001082 }
1083
1084 /**
1085 * Sets state to be on hold.
1086 */
Sailesh Nepal400cc482014-06-26 12:04:00 -07001087 public final void setOnHold() {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001088 checkImmutable();
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001089 setState(STATE_HOLDING);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001090 }
1091
1092 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001093 * Sets the video connection provider.
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001094 * @param videoProvider The video provider.
Tyler Gunn27d1e252014-08-21 16:38:40 -07001095 * @hide
Andrew Lee5ffbe8b82014-06-20 16:29:33 -07001096 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001097 public final void setVideoProvider(VideoProvider videoProvider) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001098 checkImmutable();
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001099 mVideoProvider = videoProvider;
Santos Cordond34e5712014-08-05 18:54:03 +00001100 for (Listener l : mListeners) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001101 l.onVideoProviderChanged(this, videoProvider);
Santos Cordond34e5712014-08-05 18:54:03 +00001102 }
Andrew Lee5ffbe8b82014-06-20 16:29:33 -07001103 }
1104
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001105 public final VideoProvider getVideoProvider() {
1106 return mVideoProvider;
Andrew Leea27a1932014-07-09 17:07:13 -07001107 }
1108
Andrew Lee5ffbe8b82014-06-20 16:29:33 -07001109 /**
Sailesh Nepal091768c2014-06-30 15:15:23 -07001110 * Sets state to disconnected.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001111 *
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001112 * @param disconnectCause The reason for the disconnection, as specified by
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001113 * {@link DisconnectCause}.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001114 */
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001115 public final void setDisconnected(DisconnectCause disconnectCause) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001116 checkImmutable();
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001117 mDisconnectCause = disconnectCause;
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001118 setState(STATE_DISCONNECTED);
mike dooleyf34519b2014-09-16 17:33:40 -07001119 Log.d(this, "Disconnected with cause %s", disconnectCause);
Santos Cordond34e5712014-08-05 18:54:03 +00001120 for (Listener l : mListeners) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001121 l.onDisconnected(this, disconnectCause);
Santos Cordond34e5712014-08-05 18:54:03 +00001122 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001123 }
1124
1125 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001126 * Informs listeners that this {@code Connection} is in a post-dial wait state. This is done
1127 * when (a) the {@code Connection} is issuing a DTMF sequence; (b) it has encountered a "wait"
1128 * character; and (c) it wishes to inform the In-Call app that it is waiting for the end-user
1129 * to send an {@link #onPostDialContinue(boolean)} signal.
1130 *
1131 * @param remaining The DTMF character sequence remaining to be emitted once the
1132 * {@link #onPostDialContinue(boolean)} is received, including any "wait" characters
1133 * that remaining sequence may contain.
Sailesh Nepal091768c2014-06-30 15:15:23 -07001134 */
1135 public final void setPostDialWait(String remaining) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001136 checkImmutable();
Santos Cordond34e5712014-08-05 18:54:03 +00001137 for (Listener l : mListeners) {
1138 l.onPostDialWait(this, remaining);
1139 }
Sailesh Nepal091768c2014-06-30 15:15:23 -07001140 }
1141
1142 /**
Nancy Chen27d1c2d2014-12-15 16:12:50 -08001143 * Informs listeners that this {@code Connection} has processed a character in the post-dial
1144 * started state. This is done when (a) the {@code Connection} is issuing a DTMF sequence;
Sailesh Nepal1ed85612015-01-31 15:17:19 -08001145 * and (b) it wishes to signal Telecom to play the corresponding DTMF tone locally.
Nancy Chen27d1c2d2014-12-15 16:12:50 -08001146 *
1147 * @param nextChar The DTMF character that was just processed by the {@code Connection}.
Nancy Chen27d1c2d2014-12-15 16:12:50 -08001148 */
Sailesh Nepal1ed85612015-01-31 15:17:19 -08001149 public final void setNextPostDialChar(char nextChar) {
Nancy Chen27d1c2d2014-12-15 16:12:50 -08001150 checkImmutable();
1151 for (Listener l : mListeners) {
1152 l.onPostDialChar(this, nextChar);
1153 }
1154 }
1155
1156 /**
Ihab Awadf8358972014-05-28 16:46:42 -07001157 * Requests that the framework play a ringback tone. This is to be invoked by implementations
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001158 * that do not play a ringback tone themselves in the connection's audio stream.
Ihab Awadf8358972014-05-28 16:46:42 -07001159 *
1160 * @param ringback Whether the ringback tone is to be played.
1161 */
Andrew Lee100e2932014-09-08 15:34:24 -07001162 public final void setRingbackRequested(boolean ringback) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001163 checkImmutable();
Andrew Lee100e2932014-09-08 15:34:24 -07001164 if (mRingbackRequested != ringback) {
1165 mRingbackRequested = ringback;
Santos Cordond34e5712014-08-05 18:54:03 +00001166 for (Listener l : mListeners) {
Andrew Lee100e2932014-09-08 15:34:24 -07001167 l.onRingbackRequested(this, ringback);
Santos Cordond34e5712014-08-05 18:54:03 +00001168 }
1169 }
Ihab Awadf8358972014-05-28 16:46:42 -07001170 }
1171
1172 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001173 * Sets the connection's capabilities as a bit mask of the {@code CAPABILITY_*} constants.
Sailesh Nepal1a7061b2014-07-09 21:03:20 -07001174 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001175 * @param connectionCapabilities The new connection capabilities.
Santos Cordonb6939982014-06-04 20:20:58 -07001176 */
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001177 public final void setConnectionCapabilities(int connectionCapabilities) {
1178 checkImmutable();
1179 if (mConnectionCapabilities != connectionCapabilities) {
1180 mConnectionCapabilities = connectionCapabilities;
Santos Cordond34e5712014-08-05 18:54:03 +00001181 for (Listener l : mListeners) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001182 l.onConnectionCapabilitiesChanged(this, mConnectionCapabilities);
Santos Cordond34e5712014-08-05 18:54:03 +00001183 }
1184 }
Santos Cordonb6939982014-06-04 20:20:58 -07001185 }
1186
1187 /**
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001188 * Tears down the Connection object.
Santos Cordonb6939982014-06-04 20:20:58 -07001189 */
Evan Charlton36a71342014-07-19 16:31:02 -07001190 public final void destroy() {
Jay Shrauner229e3822014-08-15 09:23:07 -07001191 for (Listener l : mListeners) {
1192 l.onDestroyed(this);
Santos Cordond34e5712014-08-05 18:54:03 +00001193 }
Santos Cordonb6939982014-06-04 20:20:58 -07001194 }
1195
1196 /**
Sailesh Nepal33aaae42014-07-07 22:49:44 -07001197 * Requests that the framework use VOIP audio mode for this connection.
1198 *
1199 * @param isVoip True if the audio mode is VOIP.
1200 */
1201 public final void setAudioModeIsVoip(boolean isVoip) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001202 checkImmutable();
Santos Cordond34e5712014-08-05 18:54:03 +00001203 mAudioModeIsVoip = isVoip;
1204 for (Listener l : mListeners) {
1205 l.onAudioModeIsVoipChanged(this, isVoip);
1206 }
Sailesh Nepal33aaae42014-07-07 22:49:44 -07001207 }
1208
1209 /**
Sailesh Nepale7ef59a2014-07-08 21:48:22 -07001210 * Sets the label and icon status to display in the in-call UI.
1211 *
1212 * @param statusHints The status label and icon to set.
1213 */
1214 public final void setStatusHints(StatusHints statusHints) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001215 checkImmutable();
Santos Cordond34e5712014-08-05 18:54:03 +00001216 mStatusHints = statusHints;
1217 for (Listener l : mListeners) {
1218 l.onStatusHintsChanged(this, statusHints);
1219 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -07001220 }
1221
1222 /**
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001223 * Sets the connections with which this connection can be conferenced.
1224 *
1225 * @param conferenceableConnections The set of connections this connection can conference with.
1226 */
1227 public final void setConferenceableConnections(List<Connection> conferenceableConnections) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001228 checkImmutable();
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001229 clearConferenceableList();
1230 for (Connection c : conferenceableConnections) {
1231 // If statement checks for duplicates in input. It makes it N^2 but we're dealing with a
1232 // small amount of items here.
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001233 if (!mConferenceables.contains(c)) {
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001234 c.addConnectionListener(mConnectionDeathListener);
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001235 mConferenceables.add(c);
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001236 }
1237 }
1238 fireOnConferenceableConnectionsChanged();
1239 }
1240
1241 /**
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001242 * Similar to {@link #setConferenceableConnections(java.util.List)}, sets a list of connections
1243 * or conferences with which this connection can be conferenced.
1244 *
1245 * @param conferenceables The conferenceables.
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001246 */
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001247 public final void setConferenceables(List<Conferenceable> conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001248 clearConferenceableList();
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001249 for (Conferenceable c : conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001250 // If statement checks for duplicates in input. It makes it N^2 but we're dealing with a
1251 // small amount of items here.
1252 if (!mConferenceables.contains(c)) {
1253 if (c instanceof Connection) {
1254 Connection connection = (Connection) c;
1255 connection.addConnectionListener(mConnectionDeathListener);
1256 } else if (c instanceof Conference) {
1257 Conference conference = (Conference) c;
1258 conference.addListener(mConferenceDeathListener);
1259 }
1260 mConferenceables.add(c);
1261 }
1262 }
1263 fireOnConferenceableConnectionsChanged();
1264 }
1265
1266 /**
1267 * Returns the connections or conferences with which this connection can be conferenced.
1268 */
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001269 public final List<Conferenceable> getConferenceables() {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001270 return mUnmodifiableConferenceables;
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001271 }
1272
Evan Charlton8635c572014-09-24 14:04:51 -07001273 /*
Santos Cordon823fd3c2014-08-07 18:35:18 -07001274 * @hide
1275 */
1276 public final void setConnectionService(ConnectionService connectionService) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001277 checkImmutable();
Santos Cordon823fd3c2014-08-07 18:35:18 -07001278 if (mConnectionService != null) {
1279 Log.e(this, new Exception(), "Trying to set ConnectionService on a connection " +
1280 "which is already associated with another ConnectionService.");
1281 } else {
1282 mConnectionService = connectionService;
1283 }
1284 }
1285
1286 /**
1287 * @hide
1288 */
1289 public final void unsetConnectionService(ConnectionService connectionService) {
1290 if (mConnectionService != connectionService) {
1291 Log.e(this, new Exception(), "Trying to remove ConnectionService from a Connection " +
1292 "that does not belong to the ConnectionService.");
1293 } else {
1294 mConnectionService = null;
1295 }
1296 }
1297
1298 /**
Santos Cordonaf1b2962014-10-16 19:23:54 -07001299 * @hide
1300 */
1301 public final ConnectionService getConnectionService() {
1302 return mConnectionService;
1303 }
1304
1305 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001306 * Sets the conference that this connection is a part of. This will fail if the connection is
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001307 * already part of a conference. {@link #resetConference} to un-set the conference first.
Santos Cordon823fd3c2014-08-07 18:35:18 -07001308 *
1309 * @param conference The conference.
1310 * @return {@code true} if the conference was successfully set.
1311 * @hide
1312 */
1313 public final boolean setConference(Conference conference) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001314 checkImmutable();
Santos Cordon823fd3c2014-08-07 18:35:18 -07001315 // We check to see if it is already part of another conference.
Santos Cordon0159ac02014-08-21 14:28:11 -07001316 if (mConference == null) {
Santos Cordon823fd3c2014-08-07 18:35:18 -07001317 mConference = conference;
Santos Cordon0159ac02014-08-21 14:28:11 -07001318 if (mConnectionService != null && mConnectionService.containsConference(conference)) {
1319 fireConferenceChanged();
1320 }
Santos Cordon823fd3c2014-08-07 18:35:18 -07001321 return true;
1322 }
1323 return false;
1324 }
1325
1326 /**
1327 * Resets the conference that this connection is a part of.
1328 * @hide
1329 */
1330 public final void resetConference() {
1331 if (mConference != null) {
Santos Cordon0159ac02014-08-21 14:28:11 -07001332 Log.d(this, "Conference reset");
Santos Cordon823fd3c2014-08-07 18:35:18 -07001333 mConference = null;
1334 fireConferenceChanged();
1335 }
1336 }
1337
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001338 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001339 * Notifies this Connection that the {@link #getAudioState()} property has a new value.
Sailesh Nepal400cc482014-06-26 12:04:00 -07001340 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001341 * @param state The new connection audio state.
Sailesh Nepal400cc482014-06-26 12:04:00 -07001342 */
Nancy Chen354b2bd2014-09-08 18:27:26 -07001343 public void onAudioStateChanged(AudioState state) {}
Sailesh Nepal400cc482014-06-26 12:04:00 -07001344
1345 /**
Evan Charltonbf11f982014-07-20 22:06:28 -07001346 * Notifies this Connection of an internal state change. This method is called after the
1347 * state is changed.
Ihab Awadf8358972014-05-28 16:46:42 -07001348 *
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001349 * @param state The new state, one of the {@code STATE_*} constants.
Ihab Awadf8358972014-05-28 16:46:42 -07001350 */
Nancy Chen354b2bd2014-09-08 18:27:26 -07001351 public void onStateChanged(int state) {}
Ihab Awadf8358972014-05-28 16:46:42 -07001352
1353 /**
Ihab Awad542e0ea2014-05-16 10:22:16 -07001354 * Notifies this Connection of a request to play a DTMF tone.
1355 *
1356 * @param c A DTMF character.
1357 */
Santos Cordonf2951102014-07-20 19:06:29 -07001358 public void onPlayDtmfTone(char c) {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001359
1360 /**
1361 * Notifies this Connection of a request to stop any currently playing DTMF tones.
1362 */
Santos Cordonf2951102014-07-20 19:06:29 -07001363 public void onStopDtmfTone() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001364
1365 /**
1366 * Notifies this Connection of a request to disconnect.
1367 */
Santos Cordonf2951102014-07-20 19:06:29 -07001368 public void onDisconnect() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001369
1370 /**
Tyler Gunn3b4b1dc2014-11-04 14:53:37 -08001371 * Notifies this Connection of a request to disconnect a participant of the conference managed
1372 * by the connection.
1373 *
1374 * @param endpoint the {@link Uri} of the participant to disconnect.
1375 * @hide
1376 */
1377 public void onDisconnectConferenceParticipant(Uri endpoint) {}
1378
1379 /**
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001380 * Notifies this Connection of a request to separate from its parent conference.
Santos Cordonb6939982014-06-04 20:20:58 -07001381 */
Santos Cordonf2951102014-07-20 19:06:29 -07001382 public void onSeparate() {}
Santos Cordonb6939982014-06-04 20:20:58 -07001383
1384 /**
Ihab Awad542e0ea2014-05-16 10:22:16 -07001385 * Notifies this Connection of a request to abort.
1386 */
Santos Cordonf2951102014-07-20 19:06:29 -07001387 public void onAbort() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001388
1389 /**
1390 * Notifies this Connection of a request to hold.
1391 */
Santos Cordonf2951102014-07-20 19:06:29 -07001392 public void onHold() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001393
1394 /**
1395 * Notifies this Connection of a request to exit a hold state.
1396 */
Santos Cordonf2951102014-07-20 19:06:29 -07001397 public void onUnhold() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001398
1399 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001400 * Notifies this Connection, which is in {@link #STATE_RINGING}, of
Santos Cordond34e5712014-08-05 18:54:03 +00001401 * a request to accept.
Andrew Lee8da4c3c2014-07-16 10:11:42 -07001402 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001403 * @param videoState The video state in which to answer the connection.
Tyler Gunnbe74de02014-08-29 14:51:48 -07001404 * @hide
Ihab Awad542e0ea2014-05-16 10:22:16 -07001405 */
Santos Cordonf2951102014-07-20 19:06:29 -07001406 public void onAnswer(int videoState) {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001407
1408 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001409 * Notifies this Connection, which is in {@link #STATE_RINGING}, of
Tyler Gunnbe74de02014-08-29 14:51:48 -07001410 * a request to accept.
1411 */
1412 public void onAnswer() {
1413 onAnswer(VideoProfile.VideoState.AUDIO_ONLY);
1414 }
1415
1416 /**
1417 * Notifies this Connection, which is in {@link #STATE_RINGING}, of
Santos Cordond34e5712014-08-05 18:54:03 +00001418 * a request to reject.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001419 */
Santos Cordonf2951102014-07-20 19:06:29 -07001420 public void onReject() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001421
Evan Charlton6dea4ac2014-06-03 14:07:13 -07001422 /**
1423 * Notifies this Connection whether the user wishes to proceed with the post-dial DTMF codes.
1424 */
Santos Cordonf2951102014-07-20 19:06:29 -07001425 public void onPostDialContinue(boolean proceed) {}
Evan Charlton6dea4ac2014-06-03 14:07:13 -07001426
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001427 static String toLogSafePhoneNumber(String number) {
1428 // For unknown number, log empty string.
1429 if (number == null) {
1430 return "";
1431 }
1432
1433 if (PII_DEBUG) {
1434 // When PII_DEBUG is true we emit PII.
1435 return number;
1436 }
1437
1438 // Do exactly same thing as Uri#toSafeString() does, which will enable us to compare
1439 // sanitized phone numbers.
1440 StringBuilder builder = new StringBuilder();
1441 for (int i = 0; i < number.length(); i++) {
1442 char c = number.charAt(i);
1443 if (c == '-' || c == '@' || c == '.') {
1444 builder.append(c);
1445 } else {
1446 builder.append('x');
1447 }
1448 }
1449 return builder.toString();
1450 }
1451
Ihab Awad542e0ea2014-05-16 10:22:16 -07001452 private void setState(int state) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001453 checkImmutable();
Ihab Awad6107bab2014-08-18 09:23:25 -07001454 if (mState == STATE_DISCONNECTED && mState != state) {
1455 Log.d(this, "Connection already DISCONNECTED; cannot transition out of this state.");
Evan Charltonbf11f982014-07-20 22:06:28 -07001456 return;
Sailesh Nepal400cc482014-06-26 12:04:00 -07001457 }
Evan Charltonbf11f982014-07-20 22:06:28 -07001458 if (mState != state) {
1459 Log.d(this, "setState: %s", stateToString(state));
1460 mState = state;
Nancy Chen354b2bd2014-09-08 18:27:26 -07001461 onStateChanged(state);
Evan Charltonbf11f982014-07-20 22:06:28 -07001462 for (Listener l : mListeners) {
1463 l.onStateChanged(this, state);
1464 }
Evan Charltonbf11f982014-07-20 22:06:28 -07001465 }
1466 }
1467
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001468 private static class FailureSignalingConnection extends Connection {
Ihab Awad90e34e32014-12-01 16:23:17 -08001469 private boolean mImmutable = false;
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001470 public FailureSignalingConnection(DisconnectCause disconnectCause) {
1471 setDisconnected(disconnectCause);
Ihab Awad90e34e32014-12-01 16:23:17 -08001472 mImmutable = true;
Ihab Awad6107bab2014-08-18 09:23:25 -07001473 }
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001474
1475 public void checkImmutable() {
Ihab Awad90e34e32014-12-01 16:23:17 -08001476 if (mImmutable) {
1477 throw new UnsupportedOperationException("Connection is immutable");
1478 }
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001479 }
Ihab Awad6107bab2014-08-18 09:23:25 -07001480 }
1481
Evan Charltonbf11f982014-07-20 22:06:28 -07001482 /**
Ihab Awad6107bab2014-08-18 09:23:25 -07001483 * Return a {@code Connection} which represents a failed connection attempt. The returned
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001484 * {@code Connection} will have a {@link android.telecom.DisconnectCause} and as specified,
1485 * and a {@link #getState()} of {@link #STATE_DISCONNECTED}.
Ihab Awad6107bab2014-08-18 09:23:25 -07001486 * <p>
1487 * The returned {@code Connection} can be assumed to {@link #destroy()} itself when appropriate,
1488 * so users of this method need not maintain a reference to its return value to destroy it.
Evan Charltonbf11f982014-07-20 22:06:28 -07001489 *
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001490 * @param disconnectCause The disconnect cause, ({@see android.telecomm.DisconnectCause}).
Ihab Awad6107bab2014-08-18 09:23:25 -07001491 * @return A {@code Connection} which indicates failure.
Evan Charltonbf11f982014-07-20 22:06:28 -07001492 */
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001493 public static Connection createFailedConnection(DisconnectCause disconnectCause) {
1494 return new FailureSignalingConnection(disconnectCause);
Evan Charltonbf11f982014-07-20 22:06:28 -07001495 }
1496
Evan Charltonbf11f982014-07-20 22:06:28 -07001497 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001498 * Override to throw an {@link UnsupportedOperationException} if this {@code Connection} is
1499 * not intended to be mutated, e.g., if it is a marker for failure. Only for framework use;
1500 * this should never be un-@hide-den.
1501 *
1502 * @hide
1503 */
1504 public void checkImmutable() {}
1505
1506 /**
Ihab Awad6107bab2014-08-18 09:23:25 -07001507 * Return a {@code Connection} which represents a canceled connection attempt. The returned
1508 * {@code Connection} will have state {@link #STATE_DISCONNECTED}, and cannot be moved out of
1509 * that state. This connection should not be used for anything, and no other
1510 * {@code Connection}s should be attempted.
1511 * <p>
Ihab Awad6107bab2014-08-18 09:23:25 -07001512 * so users of this method need not maintain a reference to its return value to destroy it.
Evan Charltonbf11f982014-07-20 22:06:28 -07001513 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001514 * @return A {@code Connection} which indicates that the underlying connection should
1515 * be canceled.
Evan Charltonbf11f982014-07-20 22:06:28 -07001516 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001517 public static Connection createCanceledConnection() {
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001518 return new FailureSignalingConnection(new DisconnectCause(DisconnectCause.CANCELED));
Ihab Awad542e0ea2014-05-16 10:22:16 -07001519 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001520
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001521 private final void fireOnConferenceableConnectionsChanged() {
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001522 for (Listener l : mListeners) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001523 l.onConferenceablesChanged(this, getConferenceables());
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001524 }
1525 }
1526
Santos Cordon823fd3c2014-08-07 18:35:18 -07001527 private final void fireConferenceChanged() {
1528 for (Listener l : mListeners) {
1529 l.onConferenceChanged(this, mConference);
1530 }
1531 }
1532
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001533 private final void clearConferenceableList() {
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001534 for (Conferenceable c : mConferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001535 if (c instanceof Connection) {
1536 Connection connection = (Connection) c;
1537 connection.removeConnectionListener(mConnectionDeathListener);
1538 } else if (c instanceof Conference) {
1539 Conference conference = (Conference) c;
1540 conference.removeListener(mConferenceDeathListener);
1541 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001542 }
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001543 mConferenceables.clear();
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001544 }
Tyler Gunn3bffcf72014-10-28 13:51:27 -07001545
1546 /**
Tyler Gunnab4650c2014-11-06 20:06:23 -08001547 * Notifies listeners of a change to conference participant(s).
Tyler Gunn3bffcf72014-10-28 13:51:27 -07001548 *
Tyler Gunnab4650c2014-11-06 20:06:23 -08001549 * @param conferenceParticipants The participants.
Tyler Gunn3bffcf72014-10-28 13:51:27 -07001550 * @hide
1551 */
Tyler Gunnab4650c2014-11-06 20:06:23 -08001552 protected final void updateConferenceParticipants(
1553 List<ConferenceParticipant> conferenceParticipants) {
Tyler Gunn3bffcf72014-10-28 13:51:27 -07001554 for (Listener l : mListeners) {
Tyler Gunnab4650c2014-11-06 20:06:23 -08001555 l.onConferenceParticipantsChanged(this, conferenceParticipants);
Tyler Gunn3bffcf72014-10-28 13:51:27 -07001556 }
1557 }
Tyler Gunn8a2b1192015-01-29 11:47:24 -08001558
1559 /**
1560 * Notifies listeners that a conference call has been started.
Jay Shrauner55b97522015-04-09 15:15:43 -07001561 * @hide
Tyler Gunn8a2b1192015-01-29 11:47:24 -08001562 */
1563 protected void notifyConferenceStarted() {
1564 for (Listener l : mListeners) {
1565 l.onConferenceStarted();
1566 }
1567 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001568}