blob: 2cc431436acb23e835a91de590de0ef5df4f63af [file] [log] [blame]
Santos Cordon52d8a152014-06-17 19:08:45 -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
Ihab Awadb19a0bc2014-08-07 19:46:01 -070014 * limitations under the License.
Santos Cordon52d8a152014-06-17 19:08:45 -070015 */
16
Tyler Gunnef9f6f92014-09-12 22:16:17 -070017package android.telecom;
Santos Cordon52d8a152014-06-17 19:08:45 -070018
Santos Cordon52d8a152014-06-17 19:08:45 -070019import android.net.Uri;
Santos Cordon6b7f9552015-05-27 17:21:45 -070020import android.os.Bundle;
Ihab Awad5d0410f2014-07-30 10:07:40 -070021import android.os.IBinder;
Santos Cordone8dc4be2014-07-21 01:28:28 -070022import android.os.IBinder.DeathRecipient;
Santos Cordon52d8a152014-06-17 19:08:45 -070023import android.os.RemoteException;
Brad Ebinger4d75bee2016-10-28 12:29:55 -070024import android.telecom.Logging.Session;
Santos Cordon52d8a152014-06-17 19:08:45 -070025
Tyler Gunnef9f6f92014-09-12 22:16:17 -070026import com.android.internal.telecom.IConnectionService;
27import com.android.internal.telecom.IConnectionServiceAdapter;
28import com.android.internal.telecom.IVideoProvider;
29import com.android.internal.telecom.RemoteServiceCallback;
Santos Cordon52d8a152014-06-17 19:08:45 -070030
Ihab Awadb8e85c72014-08-23 20:34:57 -070031import java.util.ArrayList;
Ihab Awad5d0410f2014-07-30 10:07:40 -070032import java.util.HashMap;
33import java.util.HashSet;
34import java.util.Map;
35import java.util.Set;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -070036import java.util.List;
Santos Cordon52d8a152014-06-17 19:08:45 -070037import java.util.UUID;
38
39/**
40 * Remote connection service which other connection services can use to place calls on their behalf.
Sailesh Nepal091768c2014-06-30 15:15:23 -070041 *
42 * @hide
Santos Cordon52d8a152014-06-17 19:08:45 -070043 */
Ihab Awad5d0410f2014-07-30 10:07:40 -070044final class RemoteConnectionService {
Sailesh Nepal48031592014-07-18 14:21:23 -070045
Tyler Gunn4a57b9b2014-10-30 14:27:48 -070046 // Note: Casting null to avoid ambiguous constructor reference.
Ihab Awadb8e85c72014-08-23 20:34:57 -070047 private static final RemoteConnection NULL_CONNECTION =
Tyler Gunn4a57b9b2014-10-30 14:27:48 -070048 new RemoteConnection("NULL", null, (ConnectionRequest) null);
Ihab Awadb8e85c72014-08-23 20:34:57 -070049
50 private static final RemoteConference NULL_CONFERENCE =
51 new RemoteConference("NULL", null);
Santos Cordon52d8a152014-06-17 19:08:45 -070052
Ihab Awad5d0410f2014-07-30 10:07:40 -070053 private final IConnectionServiceAdapter mServantDelegate = new IConnectionServiceAdapter() {
Sailesh Nepal48031592014-07-18 14:21:23 -070054 @Override
Ihab Awad6107bab2014-08-18 09:23:25 -070055 public void handleCreateConnectionComplete(
Ihab Awadb19a0bc2014-08-07 19:46:01 -070056 String id,
57 ConnectionRequest request,
Brad Ebinger4d75bee2016-10-28 12:29:55 -070058 ParcelableConnection parcel,
59 Session.Info info) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -070060 RemoteConnection connection =
61 findConnectionForAction(id, "handleCreateConnectionSuccessful");
Ihab Awad5d0410f2014-07-30 10:07:40 -070062 if (connection != NULL_CONNECTION && mPendingConnections.contains(connection)) {
63 mPendingConnections.remove(connection);
Ihab Awad6107bab2014-08-18 09:23:25 -070064 // Unconditionally initialize the connection ...
Ihab Awad5c9c86e2014-11-12 13:41:16 -080065 connection.setConnectionCapabilities(parcel.getConnectionCapabilities());
Tyler Gunn720c6642016-03-22 09:02:47 -070066 connection.setConnectionProperties(parcel.getConnectionProperties());
Sailesh Nepal2d3ced72015-01-31 20:17:35 -080067 if (parcel.getHandle() != null
68 || parcel.getState() != Connection.STATE_DISCONNECTED) {
69 connection.setAddress(parcel.getHandle(), parcel.getHandlePresentation());
70 }
71 if (parcel.getCallerDisplayName() != null
72 || parcel.getState() != Connection.STATE_DISCONNECTED) {
73 connection.setCallerDisplayName(
74 parcel.getCallerDisplayName(),
75 parcel.getCallerDisplayNamePresentation());
76 }
Sailesh Nepal70638f12014-09-09 21:49:14 -070077 // Set state after handle so that the client can identify the connection.
Sailesh Nepalc2a978d2014-09-20 18:23:05 -070078 if (parcel.getState() == Connection.STATE_DISCONNECTED) {
79 connection.setDisconnected(parcel.getDisconnectCause());
80 } else {
81 connection.setState(parcel.getState());
82 }
Ihab Awadb8e85c72014-08-23 20:34:57 -070083 List<RemoteConnection> conferenceable = new ArrayList<>();
84 for (String confId : parcel.getConferenceableConnectionIds()) {
85 if (mConnectionById.containsKey(confId)) {
86 conferenceable.add(mConnectionById.get(confId));
87 }
88 }
89 connection.setConferenceableConnections(conferenceable);
Ihab Awada64627c2014-08-20 09:36:40 -070090 connection.setVideoState(parcel.getVideoState());
Ihab Awad6107bab2014-08-18 09:23:25 -070091 if (connection.getState() == Connection.STATE_DISCONNECTED) {
92 // ... then, if it was created in a disconnected state, that indicates
93 // failure on the providing end, so immediately mark it destroyed
94 connection.setDestroyed();
95 }
Sailesh Nepal48031592014-07-18 14:21:23 -070096 }
97 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -070098
Sailesh Nepal2a46b902014-07-04 17:21:07 -070099 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700100 public void setActive(String callId, Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700101 if (mConnectionById.containsKey(callId)) {
102 findConnectionForAction(callId, "setActive")
103 .setState(Connection.STATE_ACTIVE);
104 } else {
105 findConferenceForAction(callId, "setActive")
106 .setState(Connection.STATE_ACTIVE);
107 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700108 }
109
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700110 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700111 public void setRinging(String callId, Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700112 findConnectionForAction(callId, "setRinging")
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700113 .setState(Connection.STATE_RINGING);
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700114 }
115
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700116 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700117 public void setDialing(String callId, Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700118 findConnectionForAction(callId, "setDialing")
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700119 .setState(Connection.STATE_DIALING);
Santos Cordon52d8a152014-06-17 19:08:45 -0700120 }
121
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700122 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700123 public void setPulling(String callId, Session.Info sessionInfo) {
Tyler Gunnc96b5e02016-07-07 22:53:57 -0700124 findConnectionForAction(callId, "setPulling")
125 .setState(Connection.STATE_PULLING_CALL);
126 }
127
128 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700129 public void setDisconnected(String callId, DisconnectCause disconnectCause,
130 Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700131 if (mConnectionById.containsKey(callId)) {
132 findConnectionForAction(callId, "setDisconnected")
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700133 .setDisconnected(disconnectCause);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700134 } else {
135 findConferenceForAction(callId, "setDisconnected")
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700136 .setDisconnected(disconnectCause);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700137 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700138 }
139
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700140 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700141 public void setOnHold(String callId, Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700142 if (mConnectionById.containsKey(callId)) {
143 findConnectionForAction(callId, "setOnHold")
144 .setState(Connection.STATE_HOLDING);
145 } else {
146 findConferenceForAction(callId, "setOnHold")
147 .setState(Connection.STATE_HOLDING);
148 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700149 }
150
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700151 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700152 public void setRingbackRequested(String callId, boolean ringing, Session.Info sessionInfo) {
Andrew Lee100e2932014-09-08 15:34:24 -0700153 findConnectionForAction(callId, "setRingbackRequested")
154 .setRingbackRequested(ringing);
Santos Cordon52d8a152014-06-17 19:08:45 -0700155 }
156
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700157 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700158 public void setConnectionCapabilities(String callId, int connectionCapabilities,
159 Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700160 if (mConnectionById.containsKey(callId)) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800161 findConnectionForAction(callId, "setConnectionCapabilities")
162 .setConnectionCapabilities(connectionCapabilities);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700163 } else {
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800164 findConferenceForAction(callId, "setConnectionCapabilities")
165 .setConnectionCapabilities(connectionCapabilities);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700166 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700167 }
168
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700169 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700170 public void setConnectionProperties(String callId, int connectionProperties,
171 Session.Info sessionInfo) {
Tyler Gunn720c6642016-03-22 09:02:47 -0700172 if (mConnectionById.containsKey(callId)) {
173 findConnectionForAction(callId, "setConnectionProperties")
174 .setConnectionProperties(connectionProperties);
175 } else {
176 findConferenceForAction(callId, "setConnectionProperties")
177 .setConnectionProperties(connectionProperties);
178 }
179 }
180
181 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700182 public void setIsConferenced(String callId, String conferenceCallId,
183 Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700184 // Note: callId should not be null; conferenceCallId may be null
185 RemoteConnection connection =
186 findConnectionForAction(callId, "setIsConferenced");
187 if (connection != NULL_CONNECTION) {
188 if (conferenceCallId == null) {
189 // 'connection' is being split from its conference
190 if (connection.getConference() != null) {
191 connection.getConference().removeConnection(connection);
192 }
193 } else {
194 RemoteConference conference =
195 findConferenceForAction(conferenceCallId, "setIsConferenced");
196 if (conference != NULL_CONFERENCE) {
197 conference.addConnection(connection);
198 }
199 }
200 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700201 }
202
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700203 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700204 public void setConferenceMergeFailed(String callId, Session.Info sessionInfo) {
Anthony Lee17455a32015-04-24 15:25:29 -0700205 // Nothing to do here.
206 // The event has already been handled and there is no state to update
207 // in the underlying connection or conference objects
208 }
209
210 @Override
Srikanth Chintalafcb15012017-05-04 20:58:34 +0530211 public void onPhoneAccountChanged(String callId, PhoneAccountHandle pHandle,
212 Session.Info sessionInfo) {
213 }
214
215 @Override
Ihab Awadb8e85c72014-08-23 20:34:57 -0700216 public void addConferenceCall(
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700217 final String callId, ParcelableConference parcel, Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700218 RemoteConference conference = new RemoteConference(callId,
219 mOutgoingConnectionServiceRpc);
220
221 for (String id : parcel.getConnectionIds()) {
222 RemoteConnection c = mConnectionById.get(id);
223 if (c != null) {
224 conference.addConnection(c);
225 }
226 }
Ihab Awadb8e85c72014-08-23 20:34:57 -0700227 if (conference.getConnections().size() == 0) {
228 // A conference was created, but none of its connections are ones that have been
229 // created by, and therefore being tracked by, this remote connection service. It
230 // is of no interest to us.
Tyler Gunncd6ccfd2016-10-17 15:48:19 -0700231 Log.d(this, "addConferenceCall - skipping");
Ihab Awadb8e85c72014-08-23 20:34:57 -0700232 return;
233 }
234
235 conference.setState(parcel.getState());
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800236 conference.setConnectionCapabilities(parcel.getConnectionCapabilities());
Tyler Gunn04ce7572016-08-15 10:56:12 -0700237 conference.setConnectionProperties(parcel.getConnectionProperties());
Tyler Gunncd6ccfd2016-10-17 15:48:19 -0700238 conference.putExtras(parcel.getExtras());
Ihab Awadb8e85c72014-08-23 20:34:57 -0700239 mConferenceById.put(callId, conference);
Tyler Gunncd6ccfd2016-10-17 15:48:19 -0700240
241 // Stash the original connection ID as it exists in the source ConnectionService.
242 // Telecom will use this to avoid adding duplicates later.
243 // See comments on Connection.EXTRA_ORIGINAL_CONNECTION_ID for more information.
244 Bundle newExtras = new Bundle();
245 newExtras.putString(Connection.EXTRA_ORIGINAL_CONNECTION_ID, callId);
246 conference.putExtras(newExtras);
247
Andrew Lee100e2932014-09-08 15:34:24 -0700248 conference.registerCallback(new RemoteConference.Callback() {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700249 @Override
250 public void onDestroyed(RemoteConference c) {
251 mConferenceById.remove(callId);
252 maybeDisconnectAdapter();
253 }
254 });
255
256 mOurConnectionServiceImpl.addRemoteConference(conference);
Santos Cordon52d8a152014-06-17 19:08:45 -0700257 }
258
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700259 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700260 public void removeCall(String callId, Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700261 if (mConnectionById.containsKey(callId)) {
262 findConnectionForAction(callId, "removeCall")
263 .setDestroyed();
264 } else {
265 findConferenceForAction(callId, "removeCall")
266 .setDestroyed();
267 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700268 }
269
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700270 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700271 public void onPostDialWait(String callId, String remaining, Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700272 findConnectionForAction(callId, "onPostDialWait")
273 .setPostDialWait(remaining);
Santos Cordon52d8a152014-06-17 19:08:45 -0700274 }
275
Santos Cordon52d8a152014-06-17 19:08:45 -0700276 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700277 public void onPostDialChar(String callId, char nextChar, Session.Info sessionInfo) {
Nancy Chen27d1c2d2014-12-15 16:12:50 -0800278 findConnectionForAction(callId, "onPostDialChar")
279 .onPostDialChar(nextChar);
280 }
281
282 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700283 public void queryRemoteConnectionServices(RemoteServiceCallback callback,
284 Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700285 // Not supported from remote connection service.
Santos Cordon52d8a152014-06-17 19:08:45 -0700286 }
Tyler Gunn8d83fa92014-07-01 11:31:21 -0700287
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700288 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700289 public void setVideoProvider(String callId, IVideoProvider videoProvider,
290 Session.Info sessionInfo) {
Tyler Gunnbf9c6fd2016-11-09 10:19:23 -0800291
292 String callingPackage = mOurConnectionServiceImpl.getApplicationContext()
293 .getOpPackageName();
Tyler Gunn159f35c2017-03-02 09:28:37 -0800294 int targetSdkVersion = mOurConnectionServiceImpl.getApplicationInfo().targetSdkVersion;
Sailesh Nepal11aeae52015-01-28 16:54:09 -0800295 RemoteConnection.VideoProvider remoteVideoProvider = null;
296 if (videoProvider != null) {
Tyler Gunnbf9c6fd2016-11-09 10:19:23 -0800297 remoteVideoProvider = new RemoteConnection.VideoProvider(videoProvider,
Tyler Gunn159f35c2017-03-02 09:28:37 -0800298 callingPackage, targetSdkVersion);
Sailesh Nepal11aeae52015-01-28 16:54:09 -0800299 }
Ihab Awada64627c2014-08-20 09:36:40 -0700300 findConnectionForAction(callId, "setVideoProvider")
Sailesh Nepal11aeae52015-01-28 16:54:09 -0800301 .setVideoProvider(remoteVideoProvider);
Tyler Gunnaa07df82014-07-17 07:50:22 -0700302 }
303
304 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700305 public void setVideoState(String callId, int videoState, Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700306 findConnectionForAction(callId, "setVideoState")
307 .setVideoState(videoState);
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700308 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700309
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700310 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700311 public void setIsVoipAudioMode(String callId, boolean isVoip, Session.Info sessionInfo) {
Andrew Lee100e2932014-09-08 15:34:24 -0700312 findConnectionForAction(callId, "setIsVoipAudioMode")
313 .setIsVoipAudioMode(isVoip);
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700314 }
Sailesh Nepal61203862014-07-11 14:50:13 -0700315
316 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700317 public void setStatusHints(String callId, StatusHints statusHints,
318 Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700319 findConnectionForAction(callId, "setStatusHints")
320 .setStatusHints(statusHints);
Sailesh Nepal61203862014-07-11 14:50:13 -0700321 }
322
323 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700324 public void setAddress(String callId, Uri address, int presentation,
325 Session.Info sessionInfo) {
Andrew Lee100e2932014-09-08 15:34:24 -0700326 findConnectionForAction(callId, "setAddress")
327 .setAddress(address, presentation);
Sailesh Nepal61203862014-07-11 14:50:13 -0700328 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700329
330 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700331 public void setCallerDisplayName(String callId, String callerDisplayName,
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700332 int presentation, Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700333 findConnectionForAction(callId, "setCallerDisplayName")
334 .setCallerDisplayName(callerDisplayName, presentation);
335 }
336
337 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700338 public IBinder asBinder() {
339 throw new UnsupportedOperationException();
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700340 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700341
342 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700343 public final void setConferenceableConnections(String callId,
344 List<String> conferenceableConnectionIds, Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700345 List<RemoteConnection> conferenceable = new ArrayList<>();
346 for (String id : conferenceableConnectionIds) {
347 if (mConnectionById.containsKey(id)) {
348 conferenceable.add(mConnectionById.get(id));
349 }
350 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700351
Ihab Awad50e35062014-09-30 09:17:03 -0700352 if (hasConnection(callId)) {
353 findConnectionForAction(callId, "setConferenceableConnections")
354 .setConferenceableConnections(conferenceable);
355 } else {
356 findConferenceForAction(callId, "setConferenceableConnections")
357 .setConferenceableConnections(conferenceable);
358 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700359 }
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700360
361 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700362 public void addExistingConnection(String callId, ParcelableConnection connection,
363 Session.Info sessionInfo) {
Tyler Gunnbf9c6fd2016-11-09 10:19:23 -0800364 String callingPackage = mOurConnectionServiceImpl.getApplicationContext().
365 getOpPackageName();
Tyler Gunn159f35c2017-03-02 09:28:37 -0800366 int callingTargetSdkVersion = mOurConnectionServiceImpl.getApplicationInfo()
367 .targetSdkVersion;
Tyler Gunncd6ccfd2016-10-17 15:48:19 -0700368 RemoteConnection remoteConnection = new RemoteConnection(callId,
Tyler Gunn159f35c2017-03-02 09:28:37 -0800369 mOutgoingConnectionServiceRpc, connection, callingPackage,
370 callingTargetSdkVersion);
Tyler Gunncd6ccfd2016-10-17 15:48:19 -0700371 mConnectionById.put(callId, remoteConnection);
372 remoteConnection.registerCallback(new RemoteConnection.Callback() {
373 @Override
374 public void onDestroyed(RemoteConnection connection) {
375 mConnectionById.remove(callId);
376 maybeDisconnectAdapter();
377 }
378 });
379 mOurConnectionServiceImpl.addRemoteExistingConnection(remoteConnection);
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700380 }
Santos Cordon6b7f9552015-05-27 17:21:45 -0700381
382 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700383 public void putExtras(String callId, Bundle extras, Session.Info sessionInfo) {
Tyler Gunndee56a82016-03-23 16:06:34 -0700384 if (hasConnection(callId)) {
385 findConnectionForAction(callId, "putExtras").putExtras(extras);
Santos Cordon6b7f9552015-05-27 17:21:45 -0700386 } else {
Tyler Gunndee56a82016-03-23 16:06:34 -0700387 findConferenceForAction(callId, "putExtras").putExtras(extras);
388 }
389 }
390
391 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700392 public void removeExtras(String callId, List<String> keys, Session.Info sessionInfo) {
Tyler Gunndee56a82016-03-23 16:06:34 -0700393 if (hasConnection(callId)) {
394 findConnectionForAction(callId, "removeExtra").removeExtras(keys);
395 } else {
396 findConferenceForAction(callId, "removeExtra").removeExtras(keys);
Santos Cordon6b7f9552015-05-27 17:21:45 -0700397 }
398 }
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -0800399
400 @Override
Tyler Gunnf5035432017-01-09 09:43:12 -0800401 public void setAudioRoute(String callId, int audioRoute, Session.Info sessionInfo) {
402 if (hasConnection(callId)) {
403 // TODO(3pcalls): handle this for remote connections.
404 // Likely we don't want to do anything since it doesn't make sense for self-managed
405 // connections to go through a connection mgr.
406 }
407 }
408
409 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700410 public void onConnectionEvent(String callId, String event, Bundle extras,
411 Session.Info sessionInfo) {
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -0800412 if (mConnectionById.containsKey(callId)) {
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700413 findConnectionForAction(callId, "onConnectionEvent").onConnectionEvent(event,
414 extras);
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -0800415 }
416 }
Hall Liu57006aa2017-02-06 10:49:48 -0800417
418 @Override
419 public void onRttInitiationSuccess(String callId, Session.Info sessionInfo)
420 throws RemoteException {
421 if (hasConnection(callId)) {
422 findConnectionForAction(callId, "onRttInitiationSuccess")
423 .onRttInitiationSuccess();
424 } else {
425 Log.w(this, "onRttInitiationSuccess called on a remote conference");
426 }
427 }
428
429 @Override
430 public void onRttInitiationFailure(String callId, int reason, Session.Info sessionInfo)
431 throws RemoteException {
432 if (hasConnection(callId)) {
433 findConnectionForAction(callId, "onRttInitiationFailure")
434 .onRttInitiationFailure(reason);
435 } else {
436 Log.w(this, "onRttInitiationFailure called on a remote conference");
437 }
438 }
439
440 @Override
441 public void onRttSessionRemotelyTerminated(String callId, Session.Info sessionInfo)
442 throws RemoteException {
443 if (hasConnection(callId)) {
444 findConnectionForAction(callId, "onRttSessionRemotelyTerminated")
445 .onRttSessionRemotelyTerminated();
446 } else {
447 Log.w(this, "onRttSessionRemotelyTerminated called on a remote conference");
448 }
449 }
450
451 @Override
452 public void onRemoteRttRequest(String callId, Session.Info sessionInfo)
453 throws RemoteException {
454 if (hasConnection(callId)) {
455 findConnectionForAction(callId, "onRemoteRttRequest")
456 .onRemoteRttRequest();
457 } else {
458 Log.w(this, "onRemoteRttRequest called on a remote conference");
459 }
460 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700461 };
462
Ihab Awad5d0410f2014-07-30 10:07:40 -0700463 private final ConnectionServiceAdapterServant mServant =
464 new ConnectionServiceAdapterServant(mServantDelegate);
Santos Cordon52d8a152014-06-17 19:08:45 -0700465
Ihab Awad5d0410f2014-07-30 10:07:40 -0700466 private final DeathRecipient mDeathRecipient = new DeathRecipient() {
467 @Override
468 public void binderDied() {
469 for (RemoteConnection c : mConnectionById.values()) {
470 c.setDestroyed();
471 }
Ihab Awadb8e85c72014-08-23 20:34:57 -0700472 for (RemoteConference c : mConferenceById.values()) {
473 c.setDestroyed();
474 }
Ihab Awad5d0410f2014-07-30 10:07:40 -0700475 mConnectionById.clear();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700476 mConferenceById.clear();
Ihab Awad5d0410f2014-07-30 10:07:40 -0700477 mPendingConnections.clear();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700478 mOutgoingConnectionServiceRpc.asBinder().unlinkToDeath(mDeathRecipient, 0);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700479 }
480 };
481
Ihab Awadb8e85c72014-08-23 20:34:57 -0700482 private final IConnectionService mOutgoingConnectionServiceRpc;
483 private final ConnectionService mOurConnectionServiceImpl;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700484 private final Map<String, RemoteConnection> mConnectionById = new HashMap<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700485 private final Map<String, RemoteConference> mConferenceById = new HashMap<>();
Ihab Awad5d0410f2014-07-30 10:07:40 -0700486 private final Set<RemoteConnection> mPendingConnections = new HashSet<>();
487
Ihab Awadb8e85c72014-08-23 20:34:57 -0700488 RemoteConnectionService(
489 IConnectionService outgoingConnectionServiceRpc,
490 ConnectionService ourConnectionServiceImpl) throws RemoteException {
491 mOutgoingConnectionServiceRpc = outgoingConnectionServiceRpc;
492 mOutgoingConnectionServiceRpc.asBinder().linkToDeath(mDeathRecipient, 0);
493 mOurConnectionServiceImpl = ourConnectionServiceImpl;
Santos Cordon52d8a152014-06-17 19:08:45 -0700494 }
495
496 @Override
497 public String toString() {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700498 return "[RemoteCS - " + mOutgoingConnectionServiceRpc.asBinder().toString() + "]";
Santos Cordon52d8a152014-06-17 19:08:45 -0700499 }
500
Ihab Awadf8b69882014-07-25 15:14:01 -0700501 final RemoteConnection createRemoteConnection(
502 PhoneAccountHandle connectionManagerPhoneAccount,
503 ConnectionRequest request,
504 boolean isIncoming) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700505 final String id = UUID.randomUUID().toString();
Hall Liu95d55872017-01-25 17:12:49 -0800506 final ConnectionRequest newRequest = new ConnectionRequest.Builder()
507 .setAccountHandle(request.getAccountHandle())
508 .setAddress(request.getAddress())
509 .setExtras(request.getExtras())
510 .setVideoState(request.getVideoState())
511 .setRttPipeFromInCall(request.getRttPipeFromInCall())
512 .setRttPipeToInCall(request.getRttPipeToInCall())
513 .build();
Ihab Awad5d0410f2014-07-30 10:07:40 -0700514 try {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700515 if (mConnectionById.isEmpty()) {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700516 mOutgoingConnectionServiceRpc.addConnectionServiceAdapter(mServant.getStub(),
517 null /*Session.Info*/);
Ihab Awad8aecfed2014-08-08 17:06:11 -0700518 }
519 RemoteConnection connection =
Ihab Awadb8e85c72014-08-23 20:34:57 -0700520 new RemoteConnection(id, mOutgoingConnectionServiceRpc, newRequest);
Ihab Awad8aecfed2014-08-08 17:06:11 -0700521 mPendingConnections.add(connection);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700522 mConnectionById.put(id, connection);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700523 mOutgoingConnectionServiceRpc.createConnection(
Ihab Awad5d0410f2014-07-30 10:07:40 -0700524 connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700525 id,
Ihab Awad5d0410f2014-07-30 10:07:40 -0700526 newRequest,
Yorke Leec3cf9822014-10-02 09:38:39 -0700527 isIncoming,
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700528 false /* isUnknownCall */,
529 null /*Session.info*/);
Andrew Lee100e2932014-09-08 15:34:24 -0700530 connection.registerCallback(new RemoteConnection.Callback() {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700531 @Override
532 public void onDestroyed(RemoteConnection connection) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700533 mConnectionById.remove(id);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700534 maybeDisconnectAdapter();
Ihab Awad8aecfed2014-08-08 17:06:11 -0700535 }
536 });
Ihab Awad5d0410f2014-07-30 10:07:40 -0700537 return connection;
538 } catch (RemoteException e) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700539 return RemoteConnection.failure(
540 new DisconnectCause(DisconnectCause.ERROR, e.toString()));
Santos Cordon52d8a152014-06-17 19:08:45 -0700541 }
542 }
543
Ihab Awad50e35062014-09-30 09:17:03 -0700544 private boolean hasConnection(String callId) {
mike dooley879142b2014-10-08 13:39:28 -0700545 return mConnectionById.containsKey(callId);
Ihab Awad50e35062014-09-30 09:17:03 -0700546 }
547
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700548 private RemoteConnection findConnectionForAction(
549 String callId, String action) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700550 if (mConnectionById.containsKey(callId)) {
551 return mConnectionById.get(callId);
552 }
553 Log.w(this, "%s - Cannot find Connection %s", action, callId);
554 return NULL_CONNECTION;
Santos Cordon52d8a152014-06-17 19:08:45 -0700555 }
Ihab Awadb8e85c72014-08-23 20:34:57 -0700556
557 private RemoteConference findConferenceForAction(
558 String callId, String action) {
559 if (mConferenceById.containsKey(callId)) {
560 return mConferenceById.get(callId);
561 }
562 Log.w(this, "%s - Cannot find Conference %s", action, callId);
563 return NULL_CONFERENCE;
564 }
565
566 private void maybeDisconnectAdapter() {
567 if (mConnectionById.isEmpty() && mConferenceById.isEmpty()) {
568 try {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700569 mOutgoingConnectionServiceRpc.removeConnectionServiceAdapter(mServant.getStub(),
570 null /*Session.info*/);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700571 } catch (RemoteException e) {
572 }
573 }
574 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700575}