blob: 0bf91189b87d843d2bee05bb56f4dba3cb9a531f [file] [log] [blame]
Ihab Awad542e0ea2014-05-16 10:22:16 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tyler Gunnef9f6f92014-09-12 22:16:17 -070017package android.telecom;
Ihab Awad542e0ea2014-05-16 10:22:16 -070018
Tyler Gunn45382162015-05-06 08:52:27 -070019import com.android.internal.os.SomeArgs;
Tyler Gunnef9f6f92014-09-12 22:16:17 -070020import com.android.internal.telecom.IVideoCallback;
21import com.android.internal.telecom.IVideoProvider;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070022
Ihab Awad542e0ea2014-05-16 10:22:16 -070023import android.net.Uri;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070024import android.os.Handler;
25import android.os.IBinder;
26import android.os.Message;
27import android.os.RemoteException;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070028import android.view.Surface;
Ihab Awad542e0ea2014-05-16 10:22:16 -070029
Santos Cordonb6939982014-06-04 20:20:58 -070030import java.util.ArrayList;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070031import java.util.Collections;
Tyler Gunn75958422015-04-15 14:23:42 -070032import java.util.HashMap;
Santos Cordonb6939982014-06-04 20:20:58 -070033import java.util.List;
Ihab Awad542e0ea2014-05-16 10:22:16 -070034import java.util.Set;
Jay Shrauner229e3822014-08-15 09:23:07 -070035import java.util.concurrent.ConcurrentHashMap;
Ihab Awad542e0ea2014-05-16 10:22:16 -070036
37/**
38 * Represents a connection to a remote endpoint that carries voice traffic.
Ihab Awad6107bab2014-08-18 09:23:25 -070039 * <p>
40 * Implementations create a custom subclass of {@code Connection} and return it to the framework
41 * as the return value of
42 * {@link ConnectionService#onCreateIncomingConnection(PhoneAccountHandle, ConnectionRequest)}
43 * or
44 * {@link ConnectionService#onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
45 * Implementations are then responsible for updating the state of the {@code Connection}, and
46 * must call {@link #destroy()} to signal to the framework that the {@code Connection} is no
47 * longer used and associated resources may be recovered.
Ihab Awad542e0ea2014-05-16 10:22:16 -070048 */
Tyler Gunndf2cbc82015-04-20 09:13:01 -070049public abstract class Connection implements Conferenceable {
Ihab Awad542e0ea2014-05-16 10:22:16 -070050
Ihab Awadb19a0bc2014-08-07 19:46:01 -070051 public static final int STATE_INITIALIZING = 0;
52
53 public static final int STATE_NEW = 1;
54
55 public static final int STATE_RINGING = 2;
56
57 public static final int STATE_DIALING = 3;
58
59 public static final int STATE_ACTIVE = 4;
60
61 public static final int STATE_HOLDING = 5;
62
63 public static final int STATE_DISCONNECTED = 6;
64
Ihab Awad5c9c86e2014-11-12 13:41:16 -080065 /** Connection can currently be put on hold or unheld. */
66 public static final int CAPABILITY_HOLD = 0x00000001;
67
68 /** Connection supports the hold feature. */
69 public static final int CAPABILITY_SUPPORT_HOLD = 0x00000002;
70
71 /**
72 * Connections within a conference can be merged. A {@link ConnectionService} has the option to
73 * add a {@link Conference} before the child {@link Connection}s are merged. This is how
74 * CDMA-based {@link Connection}s are implemented. For these unmerged {@link Conference}s, this
75 * capability allows a merge button to be shown while the conference is in the foreground
76 * of the in-call UI.
77 * <p>
78 * This is only intended for use by a {@link Conference}.
79 */
80 public static final int CAPABILITY_MERGE_CONFERENCE = 0x00000004;
81
82 /**
83 * Connections within a conference can be swapped between foreground and background.
84 * See {@link #CAPABILITY_MERGE_CONFERENCE} for additional information.
85 * <p>
86 * This is only intended for use by a {@link Conference}.
87 */
88 public static final int CAPABILITY_SWAP_CONFERENCE = 0x00000008;
89
90 /**
91 * @hide
92 */
93 public static final int CAPABILITY_UNUSED = 0x00000010;
94
95 /** Connection supports responding via text option. */
96 public static final int CAPABILITY_RESPOND_VIA_TEXT = 0x00000020;
97
98 /** Connection can be muted. */
99 public static final int CAPABILITY_MUTE = 0x00000040;
100
101 /**
102 * Connection supports conference management. This capability only applies to
103 * {@link Conference}s which can have {@link Connection}s as children.
104 */
105 public static final int CAPABILITY_MANAGE_CONFERENCE = 0x00000080;
106
107 /**
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700108 * Local device supports receiving video.
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800109 */
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700110 public static final int CAPABILITY_SUPPORTS_VT_LOCAL_RX = 0x00000100;
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800111
112 /**
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700113 * Local device supports transmitting video.
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800114 */
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700115 public static final int CAPABILITY_SUPPORTS_VT_LOCAL_TX = 0x00000200;
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800116
117 /**
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700118 * Local device supports bidirectional video calling.
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800119 */
Andrew Lee9a8f9ce2015-04-10 18:09:46 -0700120 public static final int CAPABILITY_SUPPORTS_VT_LOCAL_BIDIRECTIONAL =
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700121 CAPABILITY_SUPPORTS_VT_LOCAL_RX | CAPABILITY_SUPPORTS_VT_LOCAL_TX;
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800122
123 /**
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700124 * Remote device supports receiving video.
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800125 */
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700126 public static final int CAPABILITY_SUPPORTS_VT_REMOTE_RX = 0x00000400;
127
128 /**
129 * Remote device supports transmitting video.
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700130 */
131 public static final int CAPABILITY_SUPPORTS_VT_REMOTE_TX = 0x00000800;
132
133 /**
134 * Remote device supports bidirectional video calling.
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700135 */
Andrew Lee9a8f9ce2015-04-10 18:09:46 -0700136 public static final int CAPABILITY_SUPPORTS_VT_REMOTE_BIDIRECTIONAL =
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700137 CAPABILITY_SUPPORTS_VT_REMOTE_RX | CAPABILITY_SUPPORTS_VT_REMOTE_TX;
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800138
139 /**
140 * Connection is able to be separated from its parent {@code Conference}, if any.
141 */
142 public static final int CAPABILITY_SEPARATE_FROM_CONFERENCE = 0x00001000;
143
144 /**
145 * Connection is able to be individually disconnected when in a {@code Conference}.
146 */
147 public static final int CAPABILITY_DISCONNECT_FROM_CONFERENCE = 0x00002000;
148
149 /**
150 * Whether the call is a generic conference, where we do not know the precise state of
151 * participants in the conference (eg. on CDMA).
152 *
153 * @hide
154 */
155 public static final int CAPABILITY_GENERIC_CONFERENCE = 0x00004000;
156
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700157 /**
158 * Connection is using high definition audio.
159 * @hide
160 */
161 public static final int CAPABILITY_HIGH_DEF_AUDIO = 0x00008000;
162
163 /**
164 * Connection is using WIFI.
165 * @hide
166 */
Tyler Gunnd11a3152015-03-18 13:09:14 -0700167 public static final int CAPABILITY_WIFI = 0x00010000;
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700168
Tyler Gunn068085b2015-02-06 13:56:52 -0800169 /**
170 * Indicates that the current device callback number should be shown.
171 *
172 * @hide
173 */
Tyler Gunn96d6c402015-03-18 12:39:23 -0700174 public static final int CAPABILITY_SHOW_CALLBACK_NUMBER = 0x00020000;
Tyler Gunn068085b2015-02-06 13:56:52 -0800175
Tyler Gunn96d6c402015-03-18 12:39:23 -0700176 /**
Dong Zhou89f41eb2015-03-15 11:59:49 -0500177 * Speed up audio setup for MT call.
178 * @hide
Tyler Gunn96d6c402015-03-18 12:39:23 -0700179 */
180 public static final int CAPABILITY_SPEED_UP_MT_AUDIO = 0x00040000;
Tyler Gunn068085b2015-02-06 13:56:52 -0800181
Rekha Kumar07366812015-03-24 16:42:31 -0700182 /**
Tyler Gunnb5e0cfb2015-04-07 16:10:51 -0700183 * Call can be upgraded to a video call.
Rekha Kumar07366812015-03-24 16:42:31 -0700184 */
185 public static final int CAPABILITY_CAN_UPGRADE_TO_VIDEO = 0x00080000;
186
Tyler Gunnb5e0cfb2015-04-07 16:10:51 -0700187 /**
188 * For video calls, indicates whether the outgoing video for the call can be paused using
189 * the {@link android.telecom.VideoProfile.VideoState#PAUSED} VideoState.
Tyler Gunnb5e0cfb2015-04-07 16:10:51 -0700190 */
191 public static final int CAPABILITY_CAN_PAUSE_VIDEO = 0x00100000;
192
Tyler Gunn96d6c402015-03-18 12:39:23 -0700193 //**********************************************************************************************
Tyler Gunnb5e0cfb2015-04-07 16:10:51 -0700194 // Next CAPABILITY value: 0x00200000
Tyler Gunn96d6c402015-03-18 12:39:23 -0700195 //**********************************************************************************************
Tyler Gunn068085b2015-02-06 13:56:52 -0800196
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700197 // Flag controlling whether PII is emitted into the logs
198 private static final boolean PII_DEBUG = Log.isLoggable(android.util.Log.DEBUG);
199
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800200 /**
201 * Whether the given capabilities support the specified capability.
202 *
203 * @param capabilities A capability bit field.
204 * @param capability The capability to check capabilities for.
205 * @return Whether the specified capability is supported.
206 * @hide
207 */
208 public static boolean can(int capabilities, int capability) {
209 return (capabilities & capability) != 0;
210 }
211
212 /**
213 * Whether the capabilities of this {@code Connection} supports the specified capability.
214 *
215 * @param capability The capability to check capabilities for.
216 * @return Whether the specified capability is supported.
217 * @hide
218 */
219 public boolean can(int capability) {
220 return can(mConnectionCapabilities, capability);
221 }
222
223 /**
224 * Removes the specified capability from the set of capabilities of this {@code Connection}.
225 *
226 * @param capability The capability to remove from the set.
227 * @hide
228 */
229 public void removeCapability(int capability) {
230 mConnectionCapabilities &= ~capability;
231 }
232
233 /**
234 * Adds the specified capability to the set of capabilities of this {@code Connection}.
235 *
236 * @param capability The capability to add to the set.
237 * @hide
238 */
239 public void addCapability(int capability) {
240 mConnectionCapabilities |= capability;
241 }
242
243
244 public static String capabilitiesToString(int capabilities) {
245 StringBuilder builder = new StringBuilder();
246 builder.append("[Capabilities:");
247 if (can(capabilities, CAPABILITY_HOLD)) {
248 builder.append(" CAPABILITY_HOLD");
249 }
250 if (can(capabilities, CAPABILITY_SUPPORT_HOLD)) {
251 builder.append(" CAPABILITY_SUPPORT_HOLD");
252 }
253 if (can(capabilities, CAPABILITY_MERGE_CONFERENCE)) {
254 builder.append(" CAPABILITY_MERGE_CONFERENCE");
255 }
256 if (can(capabilities, CAPABILITY_SWAP_CONFERENCE)) {
257 builder.append(" CAPABILITY_SWAP_CONFERENCE");
258 }
259 if (can(capabilities, CAPABILITY_RESPOND_VIA_TEXT)) {
260 builder.append(" CAPABILITY_RESPOND_VIA_TEXT");
261 }
262 if (can(capabilities, CAPABILITY_MUTE)) {
263 builder.append(" CAPABILITY_MUTE");
264 }
265 if (can(capabilities, CAPABILITY_MANAGE_CONFERENCE)) {
266 builder.append(" CAPABILITY_MANAGE_CONFERENCE");
267 }
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700268 if (can(capabilities, CAPABILITY_SUPPORTS_VT_LOCAL_RX)) {
269 builder.append(" CAPABILITY_SUPPORTS_VT_LOCAL_RX");
270 }
271 if (can(capabilities, CAPABILITY_SUPPORTS_VT_LOCAL_TX)) {
272 builder.append(" CAPABILITY_SUPPORTS_VT_LOCAL_TX");
273 }
Andrew Lee9a8f9ce2015-04-10 18:09:46 -0700274 if (can(capabilities, CAPABILITY_SUPPORTS_VT_LOCAL_BIDIRECTIONAL)) {
275 builder.append(" CAPABILITY_SUPPORTS_VT_LOCAL_BIDIRECTIONAL");
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800276 }
Andrew Lee5e9e8bb2015-03-10 13:58:24 -0700277 if (can(capabilities, CAPABILITY_SUPPORTS_VT_REMOTE_RX)) {
278 builder.append(" CAPABILITY_SUPPORTS_VT_REMOTE_RX");
279 }
280 if (can(capabilities, CAPABILITY_SUPPORTS_VT_REMOTE_TX)) {
281 builder.append(" CAPABILITY_SUPPORTS_VT_REMOTE_TX");
282 }
Andrew Lee9a8f9ce2015-04-10 18:09:46 -0700283 if (can(capabilities, CAPABILITY_SUPPORTS_VT_REMOTE_BIDIRECTIONAL)) {
284 builder.append(" CAPABILITY_SUPPORTS_VT_REMOTE_BIDIRECTIONAL");
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800285 }
Andrew Lee80fff3c2014-11-25 17:36:51 -0800286 if (can(capabilities, CAPABILITY_HIGH_DEF_AUDIO)) {
287 builder.append(" CAPABILITY_HIGH_DEF_AUDIO");
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800288 }
Andrew Lee1a8ae3e2015-02-02 13:42:38 -0800289 if (can(capabilities, CAPABILITY_WIFI)) {
290 builder.append(" CAPABILITY_WIFI");
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800291 }
292 if (can(capabilities, CAPABILITY_GENERIC_CONFERENCE)) {
293 builder.append(" CAPABILITY_GENERIC_CONFERENCE");
294 }
Tyler Gunn068085b2015-02-06 13:56:52 -0800295 if (can(capabilities, CAPABILITY_SHOW_CALLBACK_NUMBER)) {
296 builder.append(" CAPABILITY_SHOW_CALLBACK_NUMBER");
297 }
Dong Zhou89f41eb2015-03-15 11:59:49 -0500298 if (can(capabilities, CAPABILITY_SPEED_UP_MT_AUDIO)) {
Tyler Gunnd11a3152015-03-18 13:09:14 -0700299 builder.append(" CAPABILITY_SPEED_UP_MT_AUDIO");
Dong Zhou89f41eb2015-03-15 11:59:49 -0500300 }
Rekha Kumar07366812015-03-24 16:42:31 -0700301 if (can(capabilities, CAPABILITY_CAN_UPGRADE_TO_VIDEO)) {
302 builder.append(" CAPABILITY_CAN_UPGRADE_TO_VIDEO");
303 }
Tyler Gunnb5e0cfb2015-04-07 16:10:51 -0700304 if (can(capabilities, CAPABILITY_CAN_PAUSE_VIDEO)) {
305 builder.append(" CAPABILITY_CAN_PAUSE_VIDEO");
306 }
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800307 builder.append("]");
308 return builder.toString();
309 }
310
Sailesh Nepal091768c2014-06-30 15:15:23 -0700311 /** @hide */
Sailesh Nepal61203862014-07-11 14:50:13 -0700312 public abstract static class Listener {
Ihab Awad542e0ea2014-05-16 10:22:16 -0700313 public void onStateChanged(Connection c, int state) {}
Andrew Lee100e2932014-09-08 15:34:24 -0700314 public void onAddressChanged(Connection c, Uri newAddress, int presentation) {}
Sailesh Nepal61203862014-07-11 14:50:13 -0700315 public void onCallerDisplayNameChanged(
316 Connection c, String callerDisplayName, int presentation) {}
Tyler Gunnaa07df82014-07-17 07:50:22 -0700317 public void onVideoStateChanged(Connection c, int videoState) {}
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700318 public void onDisconnected(Connection c, DisconnectCause disconnectCause) {}
Sailesh Nepal091768c2014-06-30 15:15:23 -0700319 public void onPostDialWait(Connection c, String remaining) {}
Nancy Chen27d1c2d2014-12-15 16:12:50 -0800320 public void onPostDialChar(Connection c, char nextChar) {}
Andrew Lee100e2932014-09-08 15:34:24 -0700321 public void onRingbackRequested(Connection c, boolean ringback) {}
Sailesh Nepal61203862014-07-11 14:50:13 -0700322 public void onDestroyed(Connection c) {}
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800323 public void onConnectionCapabilitiesChanged(Connection c, int capabilities) {}
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700324 public void onVideoProviderChanged(
325 Connection c, VideoProvider videoProvider) {}
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700326 public void onAudioModeIsVoipChanged(Connection c, boolean isVoip) {}
327 public void onStatusHintsChanged(Connection c, StatusHints statusHints) {}
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800328 public void onConferenceablesChanged(
Tyler Gunndf2cbc82015-04-20 09:13:01 -0700329 Connection c, List<Conferenceable> conferenceables) {}
Santos Cordon823fd3c2014-08-07 18:35:18 -0700330 public void onConferenceChanged(Connection c, Conference conference) {}
Tyler Gunn3bffcf72014-10-28 13:51:27 -0700331 /** @hide */
Tyler Gunnab4650c2014-11-06 20:06:23 -0800332 public void onConferenceParticipantsChanged(Connection c,
333 List<ConferenceParticipant> participants) {}
Tyler Gunn8a2b1192015-01-29 11:47:24 -0800334 public void onConferenceStarted() {}
Anthony Lee17455a32015-04-24 15:25:29 -0700335 public void onConferenceMergeFailed(Connection c) {}
Ihab Awad542e0ea2014-05-16 10:22:16 -0700336 }
337
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700338 public static abstract class VideoProvider {
Ihab Awad542e0ea2014-05-16 10:22:16 -0700339
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700340 /**
341 * Video is not being received (no protocol pause was issued).
342 */
343 public static final int SESSION_EVENT_RX_PAUSE = 1;
Evan Charltonbf11f982014-07-20 22:06:28 -0700344
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700345 /**
346 * Video reception has resumed after a SESSION_EVENT_RX_PAUSE.
347 */
348 public static final int SESSION_EVENT_RX_RESUME = 2;
349
350 /**
351 * Video transmission has begun. This occurs after a negotiated start of video transmission
352 * when the underlying protocol has actually begun transmitting video to the remote party.
353 */
354 public static final int SESSION_EVENT_TX_START = 3;
355
356 /**
357 * Video transmission has stopped. This occurs after a negotiated stop of video transmission
358 * when the underlying protocol has actually stopped transmitting video to the remote party.
359 */
360 public static final int SESSION_EVENT_TX_STOP = 4;
361
362 /**
363 * A camera failure has occurred for the selected camera. The In-Call UI can use this as a
364 * cue to inform the user the camera is not available.
365 */
366 public static final int SESSION_EVENT_CAMERA_FAILURE = 5;
367
368 /**
369 * Issued after {@code SESSION_EVENT_CAMERA_FAILURE} when the camera is once again ready for
370 * operation. The In-Call UI can use this as a cue to inform the user that the camera has
371 * become available again.
372 */
373 public static final int SESSION_EVENT_CAMERA_READY = 6;
374
375 /**
376 * Session modify request was successful.
377 */
378 public static final int SESSION_MODIFY_REQUEST_SUCCESS = 1;
379
380 /**
381 * Session modify request failed.
382 */
383 public static final int SESSION_MODIFY_REQUEST_FAIL = 2;
384
385 /**
386 * Session modify request ignored due to invalid parameters.
387 */
388 public static final int SESSION_MODIFY_REQUEST_INVALID = 3;
389
Rekha Kumar07366812015-03-24 16:42:31 -0700390 /**
391 * Session modify request timed out.
392 */
393 public static final int SESSION_MODIFY_REQUEST_TIMED_OUT = 4;
394
395 /**
396 * Session modify request rejected by remote UE.
397 */
398 public static final int SESSION_MODIFY_REQUEST_REJECTED_BY_REMOTE = 5;
399
Tyler Gunn75958422015-04-15 14:23:42 -0700400 private static final int MSG_ADD_VIDEO_CALLBACK = 1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700401 private static final int MSG_SET_CAMERA = 2;
402 private static final int MSG_SET_PREVIEW_SURFACE = 3;
403 private static final int MSG_SET_DISPLAY_SURFACE = 4;
404 private static final int MSG_SET_DEVICE_ORIENTATION = 5;
405 private static final int MSG_SET_ZOOM = 6;
406 private static final int MSG_SEND_SESSION_MODIFY_REQUEST = 7;
407 private static final int MSG_SEND_SESSION_MODIFY_RESPONSE = 8;
408 private static final int MSG_REQUEST_CAMERA_CAPABILITIES = 9;
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800409 private static final int MSG_REQUEST_CONNECTION_DATA_USAGE = 10;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700410 private static final int MSG_SET_PAUSE_IMAGE = 11;
Tyler Gunn75958422015-04-15 14:23:42 -0700411 private static final int MSG_REMOVE_VIDEO_CALLBACK = 12;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700412
413 private final VideoProvider.VideoProviderHandler
414 mMessageHandler = new VideoProvider.VideoProviderHandler();
415 private final VideoProvider.VideoProviderBinder mBinder;
Tyler Gunn75958422015-04-15 14:23:42 -0700416
417 /**
418 * Stores a list of the video callbacks, keyed by IBinder.
419 */
420 private HashMap<IBinder, IVideoCallback> mVideoCallbacks = new HashMap<>();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700421
422 /**
423 * Default handler used to consolidate binder method calls onto a single thread.
424 */
425 private final class VideoProviderHandler extends Handler {
426 @Override
427 public void handleMessage(Message msg) {
428 switch (msg.what) {
Tyler Gunn75958422015-04-15 14:23:42 -0700429 case MSG_ADD_VIDEO_CALLBACK: {
430 IBinder binder = (IBinder) msg.obj;
431 IVideoCallback callback = IVideoCallback.Stub
432 .asInterface((IBinder) msg.obj);
433 if (mVideoCallbacks.containsKey(binder)) {
434 Log.i(this, "addVideoProvider - skipped; already present.");
435 break;
436 }
437 mVideoCallbacks.put(binder, callback);
438 Log.i(this, "addVideoProvider "+ mVideoCallbacks.size());
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700439 break;
Tyler Gunn75958422015-04-15 14:23:42 -0700440 }
441 case MSG_REMOVE_VIDEO_CALLBACK: {
442 IBinder binder = (IBinder) msg.obj;
443 IVideoCallback callback = IVideoCallback.Stub
444 .asInterface((IBinder) msg.obj);
445 if (!mVideoCallbacks.containsKey(binder)) {
446 Log.i(this, "removeVideoProvider - skipped; not present.");
447 break;
448 }
449 mVideoCallbacks.remove(binder);
450 break;
451 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700452 case MSG_SET_CAMERA:
453 onSetCamera((String) msg.obj);
454 break;
455 case MSG_SET_PREVIEW_SURFACE:
456 onSetPreviewSurface((Surface) msg.obj);
457 break;
458 case MSG_SET_DISPLAY_SURFACE:
459 onSetDisplaySurface((Surface) msg.obj);
460 break;
461 case MSG_SET_DEVICE_ORIENTATION:
462 onSetDeviceOrientation(msg.arg1);
463 break;
464 case MSG_SET_ZOOM:
465 onSetZoom((Float) msg.obj);
466 break;
Tyler Gunn45382162015-05-06 08:52:27 -0700467 case MSG_SEND_SESSION_MODIFY_REQUEST: {
468 SomeArgs args = (SomeArgs) msg.obj;
469 try {
470 onSendSessionModifyRequest((VideoProfile) args.arg1,
471 (VideoProfile) args.arg2);
472 } finally {
473 args.recycle();
474 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700475 break;
Tyler Gunn45382162015-05-06 08:52:27 -0700476 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700477 case MSG_SEND_SESSION_MODIFY_RESPONSE:
478 onSendSessionModifyResponse((VideoProfile) msg.obj);
479 break;
480 case MSG_REQUEST_CAMERA_CAPABILITIES:
481 onRequestCameraCapabilities();
482 break;
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800483 case MSG_REQUEST_CONNECTION_DATA_USAGE:
484 onRequestConnectionDataUsage();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700485 break;
486 case MSG_SET_PAUSE_IMAGE:
487 onSetPauseImage((String) msg.obj);
488 break;
489 default:
490 break;
491 }
492 }
493 }
494
495 /**
496 * IVideoProvider stub implementation.
497 */
498 private final class VideoProviderBinder extends IVideoProvider.Stub {
Tyler Gunn75958422015-04-15 14:23:42 -0700499 public void addVideoCallback(IBinder videoCallbackBinder) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700500 mMessageHandler.obtainMessage(
Tyler Gunn75958422015-04-15 14:23:42 -0700501 MSG_ADD_VIDEO_CALLBACK, videoCallbackBinder).sendToTarget();
502 }
503
504 public void removeVideoCallback(IBinder videoCallbackBinder) {
505 mMessageHandler.obtainMessage(
506 MSG_REMOVE_VIDEO_CALLBACK, videoCallbackBinder).sendToTarget();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700507 }
508
509 public void setCamera(String cameraId) {
510 mMessageHandler.obtainMessage(MSG_SET_CAMERA, cameraId).sendToTarget();
511 }
512
513 public void setPreviewSurface(Surface surface) {
514 mMessageHandler.obtainMessage(MSG_SET_PREVIEW_SURFACE, surface).sendToTarget();
515 }
516
517 public void setDisplaySurface(Surface surface) {
518 mMessageHandler.obtainMessage(MSG_SET_DISPLAY_SURFACE, surface).sendToTarget();
519 }
520
521 public void setDeviceOrientation(int rotation) {
Rekha Kumar07366812015-03-24 16:42:31 -0700522 mMessageHandler.obtainMessage(
523 MSG_SET_DEVICE_ORIENTATION, rotation, 0).sendToTarget();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700524 }
525
526 public void setZoom(float value) {
527 mMessageHandler.obtainMessage(MSG_SET_ZOOM, value).sendToTarget();
528 }
529
Tyler Gunn45382162015-05-06 08:52:27 -0700530 public void sendSessionModifyRequest(VideoProfile fromProfile, VideoProfile toProfile) {
531 SomeArgs args = SomeArgs.obtain();
532 args.arg1 = fromProfile;
533 args.arg2 = toProfile;
534 mMessageHandler.obtainMessage(MSG_SEND_SESSION_MODIFY_REQUEST, args).sendToTarget();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700535 }
536
537 public void sendSessionModifyResponse(VideoProfile responseProfile) {
538 mMessageHandler.obtainMessage(
539 MSG_SEND_SESSION_MODIFY_RESPONSE, responseProfile).sendToTarget();
540 }
541
542 public void requestCameraCapabilities() {
543 mMessageHandler.obtainMessage(MSG_REQUEST_CAMERA_CAPABILITIES).sendToTarget();
544 }
545
546 public void requestCallDataUsage() {
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800547 mMessageHandler.obtainMessage(MSG_REQUEST_CONNECTION_DATA_USAGE).sendToTarget();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700548 }
549
550 public void setPauseImage(String uri) {
551 mMessageHandler.obtainMessage(MSG_SET_PAUSE_IMAGE, uri).sendToTarget();
552 }
553 }
554
555 public VideoProvider() {
556 mBinder = new VideoProvider.VideoProviderBinder();
557 }
558
559 /**
560 * Returns binder object which can be used across IPC methods.
561 * @hide
562 */
563 public final IVideoProvider getInterface() {
564 return mBinder;
565 }
566
567 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800568 * Sets the camera to be used for video recording in a video connection.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700569 *
570 * @param cameraId The id of the camera.
571 */
572 public abstract void onSetCamera(String cameraId);
573
574 /**
575 * Sets the surface to be used for displaying a preview of what the user's camera is
576 * currently capturing. When video transmission is enabled, this is the video signal which
577 * is sent to the remote device.
578 *
579 * @param surface The surface.
580 */
581 public abstract void onSetPreviewSurface(Surface surface);
582
583 /**
584 * Sets the surface to be used for displaying the video received from the remote device.
585 *
586 * @param surface The surface.
587 */
588 public abstract void onSetDisplaySurface(Surface surface);
589
590 /**
591 * Sets the device orientation, in degrees. Assumes that a standard portrait orientation of
592 * the device is 0 degrees.
593 *
594 * @param rotation The device orientation, in degrees.
595 */
596 public abstract void onSetDeviceOrientation(int rotation);
597
598 /**
599 * Sets camera zoom ratio.
600 *
601 * @param value The camera zoom ratio.
602 */
603 public abstract void onSetZoom(float value);
604
605 /**
606 * Issues a request to modify the properties of the current session. The request is
607 * sent to the remote device where it it handled by the In-Call UI.
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800608 * Some examples of session modification requests: upgrade connection from audio to video,
609 * downgrade connection from video to audio, pause video.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700610 *
Tyler Gunn45382162015-05-06 08:52:27 -0700611 * @param fromProfile The video properties prior to the request.
612 * @param toProfile The video properties with the requested changes made.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700613 */
Tyler Gunn45382162015-05-06 08:52:27 -0700614 public abstract void onSendSessionModifyRequest(VideoProfile fromProfile,
615 VideoProfile toProfile);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700616
617 /**te
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800618 * Provides a response to a request to change the current connection session video
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700619 * properties.
620 * This is in response to a request the InCall UI has received via the InCall UI.
621 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800622 * @param responseProfile The response connection video properties.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700623 */
624 public abstract void onSendSessionModifyResponse(VideoProfile responseProfile);
625
626 /**
627 * Issues a request to the video provider to retrieve the camera capabilities.
628 * Camera capabilities are reported back to the caller via the In-Call UI.
629 */
630 public abstract void onRequestCameraCapabilities();
631
632 /**
633 * Issues a request to the video telephony framework to retrieve the cumulative data usage
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800634 * for the current connection. Data usage is reported back to the caller via the
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700635 * InCall UI.
636 */
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800637 public abstract void onRequestConnectionDataUsage();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700638
639 /**
640 * Provides the video telephony framework with the URI of an image to be displayed to remote
641 * devices when the video signal is paused.
642 *
643 * @param uri URI of image to display.
644 */
645 public abstract void onSetPauseImage(String uri);
646
647 /**
Tyler Gunn75958422015-04-15 14:23:42 -0700648 * Invokes callback method defined in listening {@link InCallService} implementations.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700649 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800650 * @param videoProfile The requested video connection profile.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700651 */
652 public void receiveSessionModifyRequest(VideoProfile videoProfile) {
Tyler Gunn75958422015-04-15 14:23:42 -0700653 if (mVideoCallbacks != null) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700654 try {
Tyler Gunn75958422015-04-15 14:23:42 -0700655 for (IVideoCallback callback : mVideoCallbacks.values()) {
656 callback.receiveSessionModifyRequest(videoProfile);
657 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700658 } catch (RemoteException ignored) {
659 }
660 }
661 }
662
663 /**
Tyler Gunn75958422015-04-15 14:23:42 -0700664 * Invokes callback method defined in listening {@link InCallService} implementations.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700665 *
666 * @param status Status of the session modify request. Valid values are
667 * {@link VideoProvider#SESSION_MODIFY_REQUEST_SUCCESS},
668 * {@link VideoProvider#SESSION_MODIFY_REQUEST_FAIL},
669 * {@link VideoProvider#SESSION_MODIFY_REQUEST_INVALID}
670 * @param requestedProfile The original request which was sent to the remote device.
671 * @param responseProfile The actual profile changes made by the remote device.
672 */
673 public void receiveSessionModifyResponse(int status,
674 VideoProfile requestedProfile, VideoProfile responseProfile) {
Tyler Gunn75958422015-04-15 14:23:42 -0700675 if (mVideoCallbacks != null) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700676 try {
Tyler Gunn75958422015-04-15 14:23:42 -0700677 for (IVideoCallback callback : mVideoCallbacks.values()) {
678 callback.receiveSessionModifyResponse(status, requestedProfile,
679 responseProfile);
680 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700681 } catch (RemoteException ignored) {
682 }
683 }
684 }
685
686 /**
Tyler Gunn75958422015-04-15 14:23:42 -0700687 * Invokes callback method defined in listening {@link InCallService} implementations.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700688 *
689 * Valid values are: {@link VideoProvider#SESSION_EVENT_RX_PAUSE},
690 * {@link VideoProvider#SESSION_EVENT_RX_RESUME},
691 * {@link VideoProvider#SESSION_EVENT_TX_START},
692 * {@link VideoProvider#SESSION_EVENT_TX_STOP}
693 *
694 * @param event The event.
695 */
696 public void handleCallSessionEvent(int event) {
Tyler Gunn75958422015-04-15 14:23:42 -0700697 if (mVideoCallbacks != null) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700698 try {
Tyler Gunn75958422015-04-15 14:23:42 -0700699 for (IVideoCallback callback : mVideoCallbacks.values()) {
700 callback.handleCallSessionEvent(event);
701 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700702 } catch (RemoteException ignored) {
703 }
704 }
705 }
706
707 /**
Tyler Gunn75958422015-04-15 14:23:42 -0700708 * Invokes callback method defined in listening {@link InCallService} implementations.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700709 *
710 * @param width The updated peer video width.
711 * @param height The updated peer video height.
712 */
713 public void changePeerDimensions(int width, int height) {
Tyler Gunn75958422015-04-15 14:23:42 -0700714 if (mVideoCallbacks != null) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700715 try {
Tyler Gunn75958422015-04-15 14:23:42 -0700716 for (IVideoCallback callback : mVideoCallbacks.values()) {
717 callback.changePeerDimensions(width, height);
718 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700719 } catch (RemoteException ignored) {
720 }
721 }
722 }
723
724 /**
Tyler Gunn75958422015-04-15 14:23:42 -0700725 * Invokes callback method defined in listening {@link InCallService} implementations.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700726 *
727 * @param dataUsage The updated data usage.
728 */
Rekha Kumar07366812015-03-24 16:42:31 -0700729 public void changeCallDataUsage(long dataUsage) {
Tyler Gunn75958422015-04-15 14:23:42 -0700730 if (mVideoCallbacks != null) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700731 try {
Tyler Gunn75958422015-04-15 14:23:42 -0700732 for (IVideoCallback callback : mVideoCallbacks.values()) {
733 callback.changeCallDataUsage(dataUsage);
734 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700735 } catch (RemoteException ignored) {
736 }
737 }
738 }
739
740 /**
Tyler Gunn75958422015-04-15 14:23:42 -0700741 * Invokes callback method defined in listening {@link InCallService} implementations.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700742 *
743 * @param cameraCapabilities The changed camera capabilities.
744 */
745 public void changeCameraCapabilities(CameraCapabilities cameraCapabilities) {
Tyler Gunn75958422015-04-15 14:23:42 -0700746 if (mVideoCallbacks != null) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700747 try {
Tyler Gunn75958422015-04-15 14:23:42 -0700748 for (IVideoCallback callback : mVideoCallbacks.values()) {
749 callback.changeCameraCapabilities(cameraCapabilities);
750 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700751 } catch (RemoteException ignored) {
752 }
753 }
754 }
Rekha Kumar07366812015-03-24 16:42:31 -0700755
756 /**
Tyler Gunn75958422015-04-15 14:23:42 -0700757 * Invokes callback method defined in listening {@link InCallService} implementations.
Rekha Kumar07366812015-03-24 16:42:31 -0700758 *
759 * @param videoQuality The updated video quality.
760 */
761 public void changeVideoQuality(int videoQuality) {
Tyler Gunn75958422015-04-15 14:23:42 -0700762 if (mVideoCallbacks != null) {
Rekha Kumar07366812015-03-24 16:42:31 -0700763 try {
Tyler Gunn75958422015-04-15 14:23:42 -0700764 for (IVideoCallback callback : mVideoCallbacks.values()) {
765 callback.changeVideoQuality(videoQuality);
766 }
Rekha Kumar07366812015-03-24 16:42:31 -0700767 } catch (RemoteException ignored) {
768 }
769 }
770 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700771 }
772
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700773 private final Listener mConnectionDeathListener = new Listener() {
774 @Override
775 public void onDestroyed(Connection c) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800776 if (mConferenceables.remove(c)) {
777 fireOnConferenceableConnectionsChanged();
778 }
779 }
780 };
781
782 private final Conference.Listener mConferenceDeathListener = new Conference.Listener() {
783 @Override
784 public void onDestroyed(Conference c) {
785 if (mConferenceables.remove(c)) {
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700786 fireOnConferenceableConnectionsChanged();
787 }
788 }
789 };
790
Jay Shrauner229e3822014-08-15 09:23:07 -0700791 /**
792 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
793 * load factor before resizing, 1 means we only expect a single thread to
794 * access the map so make only a single shard
795 */
796 private final Set<Listener> mListeners = Collections.newSetFromMap(
797 new ConcurrentHashMap<Listener, Boolean>(8, 0.9f, 1));
Tyler Gunndf2cbc82015-04-20 09:13:01 -0700798 private final List<Conferenceable> mConferenceables = new ArrayList<>();
799 private final List<Conferenceable> mUnmodifiableConferenceables =
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800800 Collections.unmodifiableList(mConferenceables);
Santos Cordonb6939982014-06-04 20:20:58 -0700801
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700802 private int mState = STATE_NEW;
803 private AudioState mAudioState;
Andrew Lee100e2932014-09-08 15:34:24 -0700804 private Uri mAddress;
805 private int mAddressPresentation;
Sailesh Nepal61203862014-07-11 14:50:13 -0700806 private String mCallerDisplayName;
807 private int mCallerDisplayNamePresentation;
Andrew Lee100e2932014-09-08 15:34:24 -0700808 private boolean mRingbackRequested = false;
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800809 private int mConnectionCapabilities;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700810 private VideoProvider mVideoProvider;
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700811 private boolean mAudioModeIsVoip;
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700812 private StatusHints mStatusHints;
Tyler Gunnaa07df82014-07-17 07:50:22 -0700813 private int mVideoState;
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700814 private DisconnectCause mDisconnectCause;
Santos Cordon823fd3c2014-08-07 18:35:18 -0700815 private Conference mConference;
816 private ConnectionService mConnectionService;
Ihab Awad542e0ea2014-05-16 10:22:16 -0700817
818 /**
819 * Create a new Connection.
820 */
Santos Cordonf2951102014-07-20 19:06:29 -0700821 public Connection() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -0700822
823 /**
Andrew Lee100e2932014-09-08 15:34:24 -0700824 * @return The address (e.g., phone number) to which this Connection is currently communicating.
Ihab Awad542e0ea2014-05-16 10:22:16 -0700825 */
Andrew Lee100e2932014-09-08 15:34:24 -0700826 public final Uri getAddress() {
827 return mAddress;
Ihab Awad542e0ea2014-05-16 10:22:16 -0700828 }
829
830 /**
Andrew Lee100e2932014-09-08 15:34:24 -0700831 * @return The presentation requirements for the address.
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700832 * See {@link TelecomManager} for valid values.
Sailesh Nepal61203862014-07-11 14:50:13 -0700833 */
Andrew Lee100e2932014-09-08 15:34:24 -0700834 public final int getAddressPresentation() {
835 return mAddressPresentation;
Sailesh Nepal61203862014-07-11 14:50:13 -0700836 }
837
838 /**
839 * @return The caller display name (CNAP).
840 */
841 public final String getCallerDisplayName() {
842 return mCallerDisplayName;
843 }
844
845 /**
Nancy Chen9d568c02014-09-08 14:17:59 -0700846 * @return The presentation requirements for the handle.
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700847 * See {@link TelecomManager} for valid values.
Sailesh Nepal61203862014-07-11 14:50:13 -0700848 */
849 public final int getCallerDisplayNamePresentation() {
850 return mCallerDisplayNamePresentation;
851 }
852
853 /**
Ihab Awad542e0ea2014-05-16 10:22:16 -0700854 * @return The state of this Connection.
Ihab Awad542e0ea2014-05-16 10:22:16 -0700855 */
856 public final int getState() {
857 return mState;
858 }
859
860 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800861 * Returns the video state of the connection.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700862 * Valid values: {@link VideoProfile.VideoState#AUDIO_ONLY},
863 * {@link VideoProfile.VideoState#BIDIRECTIONAL},
864 * {@link VideoProfile.VideoState#TX_ENABLED},
865 * {@link VideoProfile.VideoState#RX_ENABLED}.
Tyler Gunnaa07df82014-07-17 07:50:22 -0700866 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800867 * @return The video state of the connection.
Tyler Gunn27d1e252014-08-21 16:38:40 -0700868 * @hide
Tyler Gunnaa07df82014-07-17 07:50:22 -0700869 */
870 public final int getVideoState() {
871 return mVideoState;
872 }
873
874 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800875 * @return The audio state of the connection, describing how its audio is currently
Ihab Awad542e0ea2014-05-16 10:22:16 -0700876 * being routed by the system. This is {@code null} if this Connection
877 * does not directly know about its audio state.
878 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700879 public final AudioState getAudioState() {
880 return mAudioState;
Ihab Awad542e0ea2014-05-16 10:22:16 -0700881 }
882
883 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700884 * @return The conference that this connection is a part of. Null if it is not part of any
885 * conference.
886 */
887 public final Conference getConference() {
888 return mConference;
889 }
890
891 /**
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700892 * Returns whether this connection is requesting that the system play a ringback tone
893 * on its behalf.
894 */
Andrew Lee100e2932014-09-08 15:34:24 -0700895 public final boolean isRingbackRequested() {
896 return mRingbackRequested;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700897 }
898
899 /**
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700900 * @return True if the connection's audio mode is VOIP.
901 */
902 public final boolean getAudioModeIsVoip() {
903 return mAudioModeIsVoip;
904 }
905
906 /**
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700907 * @return The status hints for this connection.
908 */
909 public final StatusHints getStatusHints() {
910 return mStatusHints;
911 }
912
913 /**
Ihab Awad542e0ea2014-05-16 10:22:16 -0700914 * Assign a listener to be notified of state changes.
915 *
916 * @param l A listener.
917 * @return This Connection.
918 *
919 * @hide
920 */
921 public final Connection addConnectionListener(Listener l) {
Santos Cordond34e5712014-08-05 18:54:03 +0000922 mListeners.add(l);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700923 return this;
924 }
925
926 /**
927 * Remove a previously assigned listener that was being notified of state changes.
928 *
929 * @param l A Listener.
930 * @return This Connection.
931 *
932 * @hide
933 */
934 public final Connection removeConnectionListener(Listener l) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700935 if (l != null) {
936 mListeners.remove(l);
937 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700938 return this;
939 }
940
941 /**
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700942 * @return The {@link DisconnectCause} for this connection.
Evan Charltonbf11f982014-07-20 22:06:28 -0700943 */
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700944 public final DisconnectCause getDisconnectCause() {
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700945 return mDisconnectCause;
Evan Charltonbf11f982014-07-20 22:06:28 -0700946 }
947
948 /**
Ihab Awad542e0ea2014-05-16 10:22:16 -0700949 * Inform this Connection that the state of its audio output has been changed externally.
950 *
951 * @param state The new audio state.
Sailesh Nepal400cc482014-06-26 12:04:00 -0700952 * @hide
Ihab Awad542e0ea2014-05-16 10:22:16 -0700953 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700954 final void setAudioState(AudioState state) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800955 checkImmutable();
Ihab Awad60ac30b2014-05-20 22:32:12 -0700956 Log.d(this, "setAudioState %s", state);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700957 mAudioState = state;
Nancy Chen354b2bd2014-09-08 18:27:26 -0700958 onAudioStateChanged(state);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700959 }
960
961 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700962 * @param state An integer value of a {@code STATE_*} constant.
Ihab Awad542e0ea2014-05-16 10:22:16 -0700963 * @return A string representation of the value.
964 */
965 public static String stateToString(int state) {
966 switch (state) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700967 case STATE_INITIALIZING:
968 return "STATE_INITIALIZING";
969 case STATE_NEW:
970 return "STATE_NEW";
971 case STATE_RINGING:
972 return "STATE_RINGING";
973 case STATE_DIALING:
974 return "STATE_DIALING";
975 case STATE_ACTIVE:
976 return "STATE_ACTIVE";
977 case STATE_HOLDING:
978 return "STATE_HOLDING";
979 case STATE_DISCONNECTED:
Ihab Awad542e0ea2014-05-16 10:22:16 -0700980 return "DISCONNECTED";
981 default:
Ihab Awad60ac30b2014-05-20 22:32:12 -0700982 Log.wtf(Connection.class, "Unknown state %d", state);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700983 return "UNKNOWN";
984 }
985 }
986
987 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800988 * Returns the connection's capabilities, as a bit mask of the {@code CAPABILITY_*} constants.
Ihab Awad52a28f62014-06-18 10:26:34 -0700989 */
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800990 public final int getConnectionCapabilities() {
991 return mConnectionCapabilities;
Ihab Awad52a28f62014-06-18 10:26:34 -0700992 }
993
994 /**
Andrew Lee100e2932014-09-08 15:34:24 -0700995 * Sets the value of the {@link #getAddress()} property.
Ihab Awad542e0ea2014-05-16 10:22:16 -0700996 *
Andrew Lee100e2932014-09-08 15:34:24 -0700997 * @param address The new address.
998 * @param presentation The presentation requirements for the address.
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700999 * See {@link TelecomManager} for valid values.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001000 */
Andrew Lee100e2932014-09-08 15:34:24 -07001001 public final void setAddress(Uri address, int presentation) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001002 checkImmutable();
Andrew Lee100e2932014-09-08 15:34:24 -07001003 Log.d(this, "setAddress %s", address);
1004 mAddress = address;
1005 mAddressPresentation = presentation;
Santos Cordond34e5712014-08-05 18:54:03 +00001006 for (Listener l : mListeners) {
Andrew Lee100e2932014-09-08 15:34:24 -07001007 l.onAddressChanged(this, address, presentation);
Santos Cordond34e5712014-08-05 18:54:03 +00001008 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001009 }
1010
1011 /**
Sailesh Nepal61203862014-07-11 14:50:13 -07001012 * Sets the caller display name (CNAP).
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001013 *
Sailesh Nepal61203862014-07-11 14:50:13 -07001014 * @param callerDisplayName The new display name.
Nancy Chen9d568c02014-09-08 14:17:59 -07001015 * @param presentation The presentation requirements for the handle.
Tyler Gunnef9f6f92014-09-12 22:16:17 -07001016 * See {@link TelecomManager} for valid values.
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001017 */
Sailesh Nepal61203862014-07-11 14:50:13 -07001018 public final void setCallerDisplayName(String callerDisplayName, int presentation) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001019 checkImmutable();
Sailesh Nepal61203862014-07-11 14:50:13 -07001020 Log.d(this, "setCallerDisplayName %s", callerDisplayName);
Santos Cordond34e5712014-08-05 18:54:03 +00001021 mCallerDisplayName = callerDisplayName;
1022 mCallerDisplayNamePresentation = presentation;
1023 for (Listener l : mListeners) {
1024 l.onCallerDisplayNameChanged(this, callerDisplayName, presentation);
1025 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001026 }
1027
1028 /**
Tyler Gunnaa07df82014-07-17 07:50:22 -07001029 * Set the video state for the connection.
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001030 * Valid values: {@link VideoProfile.VideoState#AUDIO_ONLY},
1031 * {@link VideoProfile.VideoState#BIDIRECTIONAL},
1032 * {@link VideoProfile.VideoState#TX_ENABLED},
1033 * {@link VideoProfile.VideoState#RX_ENABLED}.
Tyler Gunnaa07df82014-07-17 07:50:22 -07001034 *
1035 * @param videoState The new video state.
1036 */
1037 public final void setVideoState(int videoState) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001038 checkImmutable();
Tyler Gunnaa07df82014-07-17 07:50:22 -07001039 Log.d(this, "setVideoState %d", videoState);
Santos Cordond34e5712014-08-05 18:54:03 +00001040 mVideoState = videoState;
1041 for (Listener l : mListeners) {
1042 l.onVideoStateChanged(this, mVideoState);
1043 }
Tyler Gunnaa07df82014-07-17 07:50:22 -07001044 }
1045
1046 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001047 * Sets state to active (e.g., an ongoing connection where two or more parties can actively
Ihab Awad542e0ea2014-05-16 10:22:16 -07001048 * communicate).
1049 */
Sailesh Nepal400cc482014-06-26 12:04:00 -07001050 public final void setActive() {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001051 checkImmutable();
Andrew Lee100e2932014-09-08 15:34:24 -07001052 setRingbackRequested(false);
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001053 setState(STATE_ACTIVE);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001054 }
1055
1056 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001057 * Sets state to ringing (e.g., an inbound ringing connection).
Ihab Awad542e0ea2014-05-16 10:22:16 -07001058 */
Sailesh Nepal400cc482014-06-26 12:04:00 -07001059 public final void setRinging() {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001060 checkImmutable();
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001061 setState(STATE_RINGING);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001062 }
1063
1064 /**
Evan Charltonbf11f982014-07-20 22:06:28 -07001065 * Sets state to initializing (this Connection is not yet ready to be used).
1066 */
1067 public final void setInitializing() {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001068 checkImmutable();
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001069 setState(STATE_INITIALIZING);
Evan Charltonbf11f982014-07-20 22:06:28 -07001070 }
1071
1072 /**
1073 * Sets state to initialized (the Connection has been set up and is now ready to be used).
1074 */
1075 public final void setInitialized() {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001076 checkImmutable();
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001077 setState(STATE_NEW);
Evan Charltonbf11f982014-07-20 22:06:28 -07001078 }
1079
1080 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001081 * Sets state to dialing (e.g., dialing an outbound connection).
Ihab Awad542e0ea2014-05-16 10:22:16 -07001082 */
Sailesh Nepal400cc482014-06-26 12:04:00 -07001083 public final void setDialing() {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001084 checkImmutable();
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001085 setState(STATE_DIALING);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001086 }
1087
1088 /**
1089 * Sets state to be on hold.
1090 */
Sailesh Nepal400cc482014-06-26 12:04:00 -07001091 public final void setOnHold() {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001092 checkImmutable();
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001093 setState(STATE_HOLDING);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001094 }
1095
1096 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001097 * Sets the video connection provider.
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001098 * @param videoProvider The video provider.
Andrew Lee5ffbe8b82014-06-20 16:29:33 -07001099 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001100 public final void setVideoProvider(VideoProvider videoProvider) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001101 checkImmutable();
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001102 mVideoProvider = videoProvider;
Santos Cordond34e5712014-08-05 18:54:03 +00001103 for (Listener l : mListeners) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001104 l.onVideoProviderChanged(this, videoProvider);
Santos Cordond34e5712014-08-05 18:54:03 +00001105 }
Andrew Lee5ffbe8b82014-06-20 16:29:33 -07001106 }
1107
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001108 public final VideoProvider getVideoProvider() {
1109 return mVideoProvider;
Andrew Leea27a1932014-07-09 17:07:13 -07001110 }
1111
Andrew Lee5ffbe8b82014-06-20 16:29:33 -07001112 /**
Sailesh Nepal091768c2014-06-30 15:15:23 -07001113 * Sets state to disconnected.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001114 *
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001115 * @param disconnectCause The reason for the disconnection, as specified by
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001116 * {@link DisconnectCause}.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001117 */
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001118 public final void setDisconnected(DisconnectCause disconnectCause) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001119 checkImmutable();
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001120 mDisconnectCause = disconnectCause;
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001121 setState(STATE_DISCONNECTED);
mike dooleyf34519b2014-09-16 17:33:40 -07001122 Log.d(this, "Disconnected with cause %s", disconnectCause);
Santos Cordond34e5712014-08-05 18:54:03 +00001123 for (Listener l : mListeners) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001124 l.onDisconnected(this, disconnectCause);
Santos Cordond34e5712014-08-05 18:54:03 +00001125 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001126 }
1127
1128 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001129 * Informs listeners that this {@code Connection} is in a post-dial wait state. This is done
1130 * when (a) the {@code Connection} is issuing a DTMF sequence; (b) it has encountered a "wait"
1131 * character; and (c) it wishes to inform the In-Call app that it is waiting for the end-user
1132 * to send an {@link #onPostDialContinue(boolean)} signal.
1133 *
1134 * @param remaining The DTMF character sequence remaining to be emitted once the
1135 * {@link #onPostDialContinue(boolean)} is received, including any "wait" characters
1136 * that remaining sequence may contain.
Sailesh Nepal091768c2014-06-30 15:15:23 -07001137 */
1138 public final void setPostDialWait(String remaining) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001139 checkImmutable();
Santos Cordond34e5712014-08-05 18:54:03 +00001140 for (Listener l : mListeners) {
1141 l.onPostDialWait(this, remaining);
1142 }
Sailesh Nepal091768c2014-06-30 15:15:23 -07001143 }
1144
1145 /**
Nancy Chen27d1c2d2014-12-15 16:12:50 -08001146 * Informs listeners that this {@code Connection} has processed a character in the post-dial
1147 * started state. This is done when (a) the {@code Connection} is issuing a DTMF sequence;
Sailesh Nepal1ed85612015-01-31 15:17:19 -08001148 * and (b) it wishes to signal Telecom to play the corresponding DTMF tone locally.
Nancy Chen27d1c2d2014-12-15 16:12:50 -08001149 *
1150 * @param nextChar The DTMF character that was just processed by the {@code Connection}.
Nancy Chen27d1c2d2014-12-15 16:12:50 -08001151 */
Sailesh Nepal1ed85612015-01-31 15:17:19 -08001152 public final void setNextPostDialChar(char nextChar) {
Nancy Chen27d1c2d2014-12-15 16:12:50 -08001153 checkImmutable();
1154 for (Listener l : mListeners) {
1155 l.onPostDialChar(this, nextChar);
1156 }
1157 }
1158
1159 /**
Ihab Awadf8358972014-05-28 16:46:42 -07001160 * Requests that the framework play a ringback tone. This is to be invoked by implementations
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001161 * that do not play a ringback tone themselves in the connection's audio stream.
Ihab Awadf8358972014-05-28 16:46:42 -07001162 *
1163 * @param ringback Whether the ringback tone is to be played.
1164 */
Andrew Lee100e2932014-09-08 15:34:24 -07001165 public final void setRingbackRequested(boolean ringback) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001166 checkImmutable();
Andrew Lee100e2932014-09-08 15:34:24 -07001167 if (mRingbackRequested != ringback) {
1168 mRingbackRequested = ringback;
Santos Cordond34e5712014-08-05 18:54:03 +00001169 for (Listener l : mListeners) {
Andrew Lee100e2932014-09-08 15:34:24 -07001170 l.onRingbackRequested(this, ringback);
Santos Cordond34e5712014-08-05 18:54:03 +00001171 }
1172 }
Ihab Awadf8358972014-05-28 16:46:42 -07001173 }
1174
1175 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001176 * Sets the connection's capabilities as a bit mask of the {@code CAPABILITY_*} constants.
Sailesh Nepal1a7061b2014-07-09 21:03:20 -07001177 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001178 * @param connectionCapabilities The new connection capabilities.
Santos Cordonb6939982014-06-04 20:20:58 -07001179 */
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001180 public final void setConnectionCapabilities(int connectionCapabilities) {
1181 checkImmutable();
1182 if (mConnectionCapabilities != connectionCapabilities) {
1183 mConnectionCapabilities = connectionCapabilities;
Santos Cordond34e5712014-08-05 18:54:03 +00001184 for (Listener l : mListeners) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001185 l.onConnectionCapabilitiesChanged(this, mConnectionCapabilities);
Santos Cordond34e5712014-08-05 18:54:03 +00001186 }
1187 }
Santos Cordonb6939982014-06-04 20:20:58 -07001188 }
1189
1190 /**
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001191 * Tears down the Connection object.
Santos Cordonb6939982014-06-04 20:20:58 -07001192 */
Evan Charlton36a71342014-07-19 16:31:02 -07001193 public final void destroy() {
Jay Shrauner229e3822014-08-15 09:23:07 -07001194 for (Listener l : mListeners) {
1195 l.onDestroyed(this);
Santos Cordond34e5712014-08-05 18:54:03 +00001196 }
Santos Cordonb6939982014-06-04 20:20:58 -07001197 }
1198
1199 /**
Sailesh Nepal33aaae42014-07-07 22:49:44 -07001200 * Requests that the framework use VOIP audio mode for this connection.
1201 *
1202 * @param isVoip True if the audio mode is VOIP.
1203 */
1204 public final void setAudioModeIsVoip(boolean isVoip) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001205 checkImmutable();
Santos Cordond34e5712014-08-05 18:54:03 +00001206 mAudioModeIsVoip = isVoip;
1207 for (Listener l : mListeners) {
1208 l.onAudioModeIsVoipChanged(this, isVoip);
1209 }
Sailesh Nepal33aaae42014-07-07 22:49:44 -07001210 }
1211
1212 /**
Sailesh Nepale7ef59a2014-07-08 21:48:22 -07001213 * Sets the label and icon status to display in the in-call UI.
1214 *
1215 * @param statusHints The status label and icon to set.
1216 */
1217 public final void setStatusHints(StatusHints statusHints) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001218 checkImmutable();
Santos Cordond34e5712014-08-05 18:54:03 +00001219 mStatusHints = statusHints;
1220 for (Listener l : mListeners) {
1221 l.onStatusHintsChanged(this, statusHints);
1222 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -07001223 }
1224
1225 /**
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001226 * Sets the connections with which this connection can be conferenced.
1227 *
1228 * @param conferenceableConnections The set of connections this connection can conference with.
1229 */
1230 public final void setConferenceableConnections(List<Connection> conferenceableConnections) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001231 checkImmutable();
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001232 clearConferenceableList();
1233 for (Connection c : conferenceableConnections) {
1234 // If statement checks for duplicates in input. It makes it N^2 but we're dealing with a
1235 // small amount of items here.
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001236 if (!mConferenceables.contains(c)) {
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001237 c.addConnectionListener(mConnectionDeathListener);
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001238 mConferenceables.add(c);
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001239 }
1240 }
1241 fireOnConferenceableConnectionsChanged();
1242 }
1243
1244 /**
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001245 * Similar to {@link #setConferenceableConnections(java.util.List)}, sets a list of connections
1246 * or conferences with which this connection can be conferenced.
1247 *
1248 * @param conferenceables The conferenceables.
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001249 */
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001250 public final void setConferenceables(List<Conferenceable> conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001251 clearConferenceableList();
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001252 for (Conferenceable c : conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001253 // If statement checks for duplicates in input. It makes it N^2 but we're dealing with a
1254 // small amount of items here.
1255 if (!mConferenceables.contains(c)) {
1256 if (c instanceof Connection) {
1257 Connection connection = (Connection) c;
1258 connection.addConnectionListener(mConnectionDeathListener);
1259 } else if (c instanceof Conference) {
1260 Conference conference = (Conference) c;
1261 conference.addListener(mConferenceDeathListener);
1262 }
1263 mConferenceables.add(c);
1264 }
1265 }
1266 fireOnConferenceableConnectionsChanged();
1267 }
1268
1269 /**
1270 * Returns the connections or conferences with which this connection can be conferenced.
1271 */
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001272 public final List<Conferenceable> getConferenceables() {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001273 return mUnmodifiableConferenceables;
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001274 }
1275
Evan Charlton8635c572014-09-24 14:04:51 -07001276 /*
Santos Cordon823fd3c2014-08-07 18:35:18 -07001277 * @hide
1278 */
1279 public final void setConnectionService(ConnectionService connectionService) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001280 checkImmutable();
Santos Cordon823fd3c2014-08-07 18:35:18 -07001281 if (mConnectionService != null) {
1282 Log.e(this, new Exception(), "Trying to set ConnectionService on a connection " +
1283 "which is already associated with another ConnectionService.");
1284 } else {
1285 mConnectionService = connectionService;
1286 }
1287 }
1288
1289 /**
1290 * @hide
1291 */
1292 public final void unsetConnectionService(ConnectionService connectionService) {
1293 if (mConnectionService != connectionService) {
1294 Log.e(this, new Exception(), "Trying to remove ConnectionService from a Connection " +
1295 "that does not belong to the ConnectionService.");
1296 } else {
1297 mConnectionService = null;
1298 }
1299 }
1300
1301 /**
Santos Cordonaf1b2962014-10-16 19:23:54 -07001302 * @hide
1303 */
1304 public final ConnectionService getConnectionService() {
1305 return mConnectionService;
1306 }
1307
1308 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001309 * Sets the conference that this connection is a part of. This will fail if the connection is
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001310 * already part of a conference. {@link #resetConference} to un-set the conference first.
Santos Cordon823fd3c2014-08-07 18:35:18 -07001311 *
1312 * @param conference The conference.
1313 * @return {@code true} if the conference was successfully set.
1314 * @hide
1315 */
1316 public final boolean setConference(Conference conference) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001317 checkImmutable();
Santos Cordon823fd3c2014-08-07 18:35:18 -07001318 // We check to see if it is already part of another conference.
Santos Cordon0159ac02014-08-21 14:28:11 -07001319 if (mConference == null) {
Santos Cordon823fd3c2014-08-07 18:35:18 -07001320 mConference = conference;
Santos Cordon0159ac02014-08-21 14:28:11 -07001321 if (mConnectionService != null && mConnectionService.containsConference(conference)) {
1322 fireConferenceChanged();
1323 }
Santos Cordon823fd3c2014-08-07 18:35:18 -07001324 return true;
1325 }
1326 return false;
1327 }
1328
1329 /**
1330 * Resets the conference that this connection is a part of.
1331 * @hide
1332 */
1333 public final void resetConference() {
1334 if (mConference != null) {
Santos Cordon0159ac02014-08-21 14:28:11 -07001335 Log.d(this, "Conference reset");
Santos Cordon823fd3c2014-08-07 18:35:18 -07001336 mConference = null;
1337 fireConferenceChanged();
1338 }
1339 }
1340
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001341 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001342 * Notifies this Connection that the {@link #getAudioState()} property has a new value.
Sailesh Nepal400cc482014-06-26 12:04:00 -07001343 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001344 * @param state The new connection audio state.
Sailesh Nepal400cc482014-06-26 12:04:00 -07001345 */
Nancy Chen354b2bd2014-09-08 18:27:26 -07001346 public void onAudioStateChanged(AudioState state) {}
Sailesh Nepal400cc482014-06-26 12:04:00 -07001347
1348 /**
Evan Charltonbf11f982014-07-20 22:06:28 -07001349 * Notifies this Connection of an internal state change. This method is called after the
1350 * state is changed.
Ihab Awadf8358972014-05-28 16:46:42 -07001351 *
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001352 * @param state The new state, one of the {@code STATE_*} constants.
Ihab Awadf8358972014-05-28 16:46:42 -07001353 */
Nancy Chen354b2bd2014-09-08 18:27:26 -07001354 public void onStateChanged(int state) {}
Ihab Awadf8358972014-05-28 16:46:42 -07001355
1356 /**
Ihab Awad542e0ea2014-05-16 10:22:16 -07001357 * Notifies this Connection of a request to play a DTMF tone.
1358 *
1359 * @param c A DTMF character.
1360 */
Santos Cordonf2951102014-07-20 19:06:29 -07001361 public void onPlayDtmfTone(char c) {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001362
1363 /**
1364 * Notifies this Connection of a request to stop any currently playing DTMF tones.
1365 */
Santos Cordonf2951102014-07-20 19:06:29 -07001366 public void onStopDtmfTone() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001367
1368 /**
1369 * Notifies this Connection of a request to disconnect.
1370 */
Santos Cordonf2951102014-07-20 19:06:29 -07001371 public void onDisconnect() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001372
1373 /**
Tyler Gunn3b4b1dc2014-11-04 14:53:37 -08001374 * Notifies this Connection of a request to disconnect a participant of the conference managed
1375 * by the connection.
1376 *
1377 * @param endpoint the {@link Uri} of the participant to disconnect.
1378 * @hide
1379 */
1380 public void onDisconnectConferenceParticipant(Uri endpoint) {}
1381
1382 /**
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001383 * Notifies this Connection of a request to separate from its parent conference.
Santos Cordonb6939982014-06-04 20:20:58 -07001384 */
Santos Cordonf2951102014-07-20 19:06:29 -07001385 public void onSeparate() {}
Santos Cordonb6939982014-06-04 20:20:58 -07001386
1387 /**
Ihab Awad542e0ea2014-05-16 10:22:16 -07001388 * Notifies this Connection of a request to abort.
1389 */
Santos Cordonf2951102014-07-20 19:06:29 -07001390 public void onAbort() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001391
1392 /**
1393 * Notifies this Connection of a request to hold.
1394 */
Santos Cordonf2951102014-07-20 19:06:29 -07001395 public void onHold() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001396
1397 /**
1398 * Notifies this Connection of a request to exit a hold state.
1399 */
Santos Cordonf2951102014-07-20 19:06:29 -07001400 public void onUnhold() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001401
1402 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001403 * Notifies this Connection, which is in {@link #STATE_RINGING}, of
Santos Cordond34e5712014-08-05 18:54:03 +00001404 * a request to accept.
Andrew Lee8da4c3c2014-07-16 10:11:42 -07001405 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001406 * @param videoState The video state in which to answer the connection.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001407 */
Santos Cordonf2951102014-07-20 19:06:29 -07001408 public void onAnswer(int videoState) {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001409
1410 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001411 * Notifies this Connection, which is in {@link #STATE_RINGING}, of
Tyler Gunnbe74de02014-08-29 14:51:48 -07001412 * a request to accept.
1413 */
1414 public void onAnswer() {
1415 onAnswer(VideoProfile.VideoState.AUDIO_ONLY);
1416 }
1417
1418 /**
1419 * Notifies this Connection, which is in {@link #STATE_RINGING}, of
Santos Cordond34e5712014-08-05 18:54:03 +00001420 * a request to reject.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001421 */
Santos Cordonf2951102014-07-20 19:06:29 -07001422 public void onReject() {}
Ihab Awad542e0ea2014-05-16 10:22:16 -07001423
Evan Charlton6dea4ac2014-06-03 14:07:13 -07001424 /**
1425 * Notifies this Connection whether the user wishes to proceed with the post-dial DTMF codes.
1426 */
Santos Cordonf2951102014-07-20 19:06:29 -07001427 public void onPostDialContinue(boolean proceed) {}
Evan Charlton6dea4ac2014-06-03 14:07:13 -07001428
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001429 static String toLogSafePhoneNumber(String number) {
1430 // For unknown number, log empty string.
1431 if (number == null) {
1432 return "";
1433 }
1434
1435 if (PII_DEBUG) {
1436 // When PII_DEBUG is true we emit PII.
1437 return number;
1438 }
1439
1440 // Do exactly same thing as Uri#toSafeString() does, which will enable us to compare
1441 // sanitized phone numbers.
1442 StringBuilder builder = new StringBuilder();
1443 for (int i = 0; i < number.length(); i++) {
1444 char c = number.charAt(i);
1445 if (c == '-' || c == '@' || c == '.') {
1446 builder.append(c);
1447 } else {
1448 builder.append('x');
1449 }
1450 }
1451 return builder.toString();
1452 }
1453
Ihab Awad542e0ea2014-05-16 10:22:16 -07001454 private void setState(int state) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001455 checkImmutable();
Ihab Awad6107bab2014-08-18 09:23:25 -07001456 if (mState == STATE_DISCONNECTED && mState != state) {
1457 Log.d(this, "Connection already DISCONNECTED; cannot transition out of this state.");
Evan Charltonbf11f982014-07-20 22:06:28 -07001458 return;
Sailesh Nepal400cc482014-06-26 12:04:00 -07001459 }
Evan Charltonbf11f982014-07-20 22:06:28 -07001460 if (mState != state) {
1461 Log.d(this, "setState: %s", stateToString(state));
1462 mState = state;
Nancy Chen354b2bd2014-09-08 18:27:26 -07001463 onStateChanged(state);
Evan Charltonbf11f982014-07-20 22:06:28 -07001464 for (Listener l : mListeners) {
1465 l.onStateChanged(this, state);
1466 }
Evan Charltonbf11f982014-07-20 22:06:28 -07001467 }
1468 }
1469
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001470 private static class FailureSignalingConnection extends Connection {
Ihab Awad90e34e32014-12-01 16:23:17 -08001471 private boolean mImmutable = false;
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001472 public FailureSignalingConnection(DisconnectCause disconnectCause) {
1473 setDisconnected(disconnectCause);
Ihab Awad90e34e32014-12-01 16:23:17 -08001474 mImmutable = true;
Ihab Awad6107bab2014-08-18 09:23:25 -07001475 }
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001476
1477 public void checkImmutable() {
Ihab Awad90e34e32014-12-01 16:23:17 -08001478 if (mImmutable) {
1479 throw new UnsupportedOperationException("Connection is immutable");
1480 }
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001481 }
Ihab Awad6107bab2014-08-18 09:23:25 -07001482 }
1483
Evan Charltonbf11f982014-07-20 22:06:28 -07001484 /**
Ihab Awad6107bab2014-08-18 09:23:25 -07001485 * Return a {@code Connection} which represents a failed connection attempt. The returned
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001486 * {@code Connection} will have a {@link android.telecom.DisconnectCause} and as specified,
1487 * and a {@link #getState()} of {@link #STATE_DISCONNECTED}.
Ihab Awad6107bab2014-08-18 09:23:25 -07001488 * <p>
1489 * The returned {@code Connection} can be assumed to {@link #destroy()} itself when appropriate,
1490 * so users of this method need not maintain a reference to its return value to destroy it.
Evan Charltonbf11f982014-07-20 22:06:28 -07001491 *
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001492 * @param disconnectCause The disconnect cause, ({@see android.telecomm.DisconnectCause}).
Ihab Awad6107bab2014-08-18 09:23:25 -07001493 * @return A {@code Connection} which indicates failure.
Evan Charltonbf11f982014-07-20 22:06:28 -07001494 */
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001495 public static Connection createFailedConnection(DisconnectCause disconnectCause) {
1496 return new FailureSignalingConnection(disconnectCause);
Evan Charltonbf11f982014-07-20 22:06:28 -07001497 }
1498
Evan Charltonbf11f982014-07-20 22:06:28 -07001499 /**
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001500 * Override to throw an {@link UnsupportedOperationException} if this {@code Connection} is
1501 * not intended to be mutated, e.g., if it is a marker for failure. Only for framework use;
1502 * this should never be un-@hide-den.
1503 *
1504 * @hide
1505 */
1506 public void checkImmutable() {}
1507
1508 /**
Ihab Awad6107bab2014-08-18 09:23:25 -07001509 * Return a {@code Connection} which represents a canceled connection attempt. The returned
1510 * {@code Connection} will have state {@link #STATE_DISCONNECTED}, and cannot be moved out of
1511 * that state. This connection should not be used for anything, and no other
1512 * {@code Connection}s should be attempted.
1513 * <p>
Ihab Awad6107bab2014-08-18 09:23:25 -07001514 * so users of this method need not maintain a reference to its return value to destroy it.
Evan Charltonbf11f982014-07-20 22:06:28 -07001515 *
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001516 * @return A {@code Connection} which indicates that the underlying connection should
1517 * be canceled.
Evan Charltonbf11f982014-07-20 22:06:28 -07001518 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001519 public static Connection createCanceledConnection() {
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001520 return new FailureSignalingConnection(new DisconnectCause(DisconnectCause.CANCELED));
Ihab Awad542e0ea2014-05-16 10:22:16 -07001521 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001522
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001523 private final void fireOnConferenceableConnectionsChanged() {
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001524 for (Listener l : mListeners) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001525 l.onConferenceablesChanged(this, getConferenceables());
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001526 }
1527 }
1528
Santos Cordon823fd3c2014-08-07 18:35:18 -07001529 private final void fireConferenceChanged() {
1530 for (Listener l : mListeners) {
1531 l.onConferenceChanged(this, mConference);
1532 }
1533 }
1534
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001535 private final void clearConferenceableList() {
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001536 for (Conferenceable c : mConferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001537 if (c instanceof Connection) {
1538 Connection connection = (Connection) c;
1539 connection.removeConnectionListener(mConnectionDeathListener);
1540 } else if (c instanceof Conference) {
1541 Conference conference = (Conference) c;
1542 conference.removeListener(mConferenceDeathListener);
1543 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001544 }
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001545 mConferenceables.clear();
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001546 }
Tyler Gunn3bffcf72014-10-28 13:51:27 -07001547
1548 /**
Anthony Lee17455a32015-04-24 15:25:29 -07001549 * Notifies listeners that the merge request failed.
1550 *
1551 * @hide
1552 */
1553 protected final void notifyConferenceMergeFailed() {
1554 for (Listener l : mListeners) {
1555 l.onConferenceMergeFailed(this);
1556 }
1557 }
1558
1559 /**
Tyler Gunnab4650c2014-11-06 20:06:23 -08001560 * Notifies listeners of a change to conference participant(s).
Tyler Gunn3bffcf72014-10-28 13:51:27 -07001561 *
Tyler Gunnab4650c2014-11-06 20:06:23 -08001562 * @param conferenceParticipants The participants.
Tyler Gunn3bffcf72014-10-28 13:51:27 -07001563 * @hide
1564 */
Tyler Gunnab4650c2014-11-06 20:06:23 -08001565 protected final void updateConferenceParticipants(
1566 List<ConferenceParticipant> conferenceParticipants) {
Tyler Gunn3bffcf72014-10-28 13:51:27 -07001567 for (Listener l : mListeners) {
Tyler Gunnab4650c2014-11-06 20:06:23 -08001568 l.onConferenceParticipantsChanged(this, conferenceParticipants);
Tyler Gunn3bffcf72014-10-28 13:51:27 -07001569 }
1570 }
Tyler Gunn8a2b1192015-01-29 11:47:24 -08001571
1572 /**
1573 * Notifies listeners that a conference call has been started.
Jay Shrauner55b97522015-04-09 15:15:43 -07001574 * @hide
Tyler Gunn8a2b1192015-01-29 11:47:24 -08001575 */
1576 protected void notifyConferenceStarted() {
1577 for (Listener l : mListeners) {
1578 l.onConferenceStarted();
1579 }
1580 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001581}