blob: 59ce590858ee82d2d944d47c42f29188a3090d27 [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
Pengquan Meng63d25a52017-11-21 18:01:13 -0800216 public void onConnectionServiceFocusReleased(Session.Info sessionInfo) {}
217
218 @Override
Ihab Awadb8e85c72014-08-23 20:34:57 -0700219 public void addConferenceCall(
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700220 final String callId, ParcelableConference parcel, Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700221 RemoteConference conference = new RemoteConference(callId,
222 mOutgoingConnectionServiceRpc);
223
224 for (String id : parcel.getConnectionIds()) {
225 RemoteConnection c = mConnectionById.get(id);
226 if (c != null) {
227 conference.addConnection(c);
228 }
229 }
Ihab Awadb8e85c72014-08-23 20:34:57 -0700230 if (conference.getConnections().size() == 0) {
231 // A conference was created, but none of its connections are ones that have been
232 // created by, and therefore being tracked by, this remote connection service. It
233 // is of no interest to us.
Tyler Gunncd6ccfd2016-10-17 15:48:19 -0700234 Log.d(this, "addConferenceCall - skipping");
Ihab Awadb8e85c72014-08-23 20:34:57 -0700235 return;
236 }
237
238 conference.setState(parcel.getState());
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800239 conference.setConnectionCapabilities(parcel.getConnectionCapabilities());
Tyler Gunn04ce7572016-08-15 10:56:12 -0700240 conference.setConnectionProperties(parcel.getConnectionProperties());
Tyler Gunncd6ccfd2016-10-17 15:48:19 -0700241 conference.putExtras(parcel.getExtras());
Ihab Awadb8e85c72014-08-23 20:34:57 -0700242 mConferenceById.put(callId, conference);
Tyler Gunncd6ccfd2016-10-17 15:48:19 -0700243
244 // Stash the original connection ID as it exists in the source ConnectionService.
245 // Telecom will use this to avoid adding duplicates later.
246 // See comments on Connection.EXTRA_ORIGINAL_CONNECTION_ID for more information.
247 Bundle newExtras = new Bundle();
248 newExtras.putString(Connection.EXTRA_ORIGINAL_CONNECTION_ID, callId);
249 conference.putExtras(newExtras);
250
Andrew Lee100e2932014-09-08 15:34:24 -0700251 conference.registerCallback(new RemoteConference.Callback() {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700252 @Override
253 public void onDestroyed(RemoteConference c) {
254 mConferenceById.remove(callId);
255 maybeDisconnectAdapter();
256 }
257 });
258
259 mOurConnectionServiceImpl.addRemoteConference(conference);
Santos Cordon52d8a152014-06-17 19:08:45 -0700260 }
261
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700262 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700263 public void removeCall(String callId, Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700264 if (mConnectionById.containsKey(callId)) {
265 findConnectionForAction(callId, "removeCall")
266 .setDestroyed();
267 } else {
268 findConferenceForAction(callId, "removeCall")
269 .setDestroyed();
270 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700271 }
272
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700273 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700274 public void onPostDialWait(String callId, String remaining, Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700275 findConnectionForAction(callId, "onPostDialWait")
276 .setPostDialWait(remaining);
Santos Cordon52d8a152014-06-17 19:08:45 -0700277 }
278
Santos Cordon52d8a152014-06-17 19:08:45 -0700279 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700280 public void onPostDialChar(String callId, char nextChar, Session.Info sessionInfo) {
Nancy Chen27d1c2d2014-12-15 16:12:50 -0800281 findConnectionForAction(callId, "onPostDialChar")
282 .onPostDialChar(nextChar);
283 }
284
285 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700286 public void queryRemoteConnectionServices(RemoteServiceCallback callback,
287 Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700288 // Not supported from remote connection service.
Santos Cordon52d8a152014-06-17 19:08:45 -0700289 }
Tyler Gunn8d83fa92014-07-01 11:31:21 -0700290
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700291 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700292 public void setVideoProvider(String callId, IVideoProvider videoProvider,
293 Session.Info sessionInfo) {
Tyler Gunnbf9c6fd2016-11-09 10:19:23 -0800294
295 String callingPackage = mOurConnectionServiceImpl.getApplicationContext()
296 .getOpPackageName();
Tyler Gunn159f35c2017-03-02 09:28:37 -0800297 int targetSdkVersion = mOurConnectionServiceImpl.getApplicationInfo().targetSdkVersion;
Sailesh Nepal11aeae52015-01-28 16:54:09 -0800298 RemoteConnection.VideoProvider remoteVideoProvider = null;
299 if (videoProvider != null) {
Tyler Gunnbf9c6fd2016-11-09 10:19:23 -0800300 remoteVideoProvider = new RemoteConnection.VideoProvider(videoProvider,
Tyler Gunn159f35c2017-03-02 09:28:37 -0800301 callingPackage, targetSdkVersion);
Sailesh Nepal11aeae52015-01-28 16:54:09 -0800302 }
Ihab Awada64627c2014-08-20 09:36:40 -0700303 findConnectionForAction(callId, "setVideoProvider")
Sailesh Nepal11aeae52015-01-28 16:54:09 -0800304 .setVideoProvider(remoteVideoProvider);
Tyler Gunnaa07df82014-07-17 07:50:22 -0700305 }
306
307 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700308 public void setVideoState(String callId, int videoState, Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700309 findConnectionForAction(callId, "setVideoState")
310 .setVideoState(videoState);
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700311 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700312
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700313 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700314 public void setIsVoipAudioMode(String callId, boolean isVoip, Session.Info sessionInfo) {
Andrew Lee100e2932014-09-08 15:34:24 -0700315 findConnectionForAction(callId, "setIsVoipAudioMode")
316 .setIsVoipAudioMode(isVoip);
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700317 }
Sailesh Nepal61203862014-07-11 14:50:13 -0700318
319 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700320 public void setStatusHints(String callId, StatusHints statusHints,
321 Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700322 findConnectionForAction(callId, "setStatusHints")
323 .setStatusHints(statusHints);
Sailesh Nepal61203862014-07-11 14:50:13 -0700324 }
325
326 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700327 public void setAddress(String callId, Uri address, int presentation,
328 Session.Info sessionInfo) {
Andrew Lee100e2932014-09-08 15:34:24 -0700329 findConnectionForAction(callId, "setAddress")
330 .setAddress(address, presentation);
Sailesh Nepal61203862014-07-11 14:50:13 -0700331 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700332
333 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700334 public void setCallerDisplayName(String callId, String callerDisplayName,
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700335 int presentation, Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700336 findConnectionForAction(callId, "setCallerDisplayName")
337 .setCallerDisplayName(callerDisplayName, presentation);
338 }
339
340 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700341 public IBinder asBinder() {
342 throw new UnsupportedOperationException();
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700343 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700344
345 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700346 public final void setConferenceableConnections(String callId,
347 List<String> conferenceableConnectionIds, Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700348 List<RemoteConnection> conferenceable = new ArrayList<>();
349 for (String id : conferenceableConnectionIds) {
350 if (mConnectionById.containsKey(id)) {
351 conferenceable.add(mConnectionById.get(id));
352 }
353 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700354
Ihab Awad50e35062014-09-30 09:17:03 -0700355 if (hasConnection(callId)) {
356 findConnectionForAction(callId, "setConferenceableConnections")
357 .setConferenceableConnections(conferenceable);
358 } else {
359 findConferenceForAction(callId, "setConferenceableConnections")
360 .setConferenceableConnections(conferenceable);
361 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700362 }
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700363
364 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700365 public void addExistingConnection(String callId, ParcelableConnection connection,
366 Session.Info sessionInfo) {
Tyler Gunnbf9c6fd2016-11-09 10:19:23 -0800367 String callingPackage = mOurConnectionServiceImpl.getApplicationContext().
368 getOpPackageName();
Tyler Gunn159f35c2017-03-02 09:28:37 -0800369 int callingTargetSdkVersion = mOurConnectionServiceImpl.getApplicationInfo()
370 .targetSdkVersion;
Tyler Gunncd6ccfd2016-10-17 15:48:19 -0700371 RemoteConnection remoteConnection = new RemoteConnection(callId,
Tyler Gunn159f35c2017-03-02 09:28:37 -0800372 mOutgoingConnectionServiceRpc, connection, callingPackage,
373 callingTargetSdkVersion);
Tyler Gunncd6ccfd2016-10-17 15:48:19 -0700374 mConnectionById.put(callId, remoteConnection);
375 remoteConnection.registerCallback(new RemoteConnection.Callback() {
376 @Override
377 public void onDestroyed(RemoteConnection connection) {
378 mConnectionById.remove(callId);
379 maybeDisconnectAdapter();
380 }
381 });
382 mOurConnectionServiceImpl.addRemoteExistingConnection(remoteConnection);
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700383 }
Santos Cordon6b7f9552015-05-27 17:21:45 -0700384
385 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700386 public void putExtras(String callId, Bundle extras, Session.Info sessionInfo) {
Tyler Gunndee56a82016-03-23 16:06:34 -0700387 if (hasConnection(callId)) {
388 findConnectionForAction(callId, "putExtras").putExtras(extras);
Santos Cordon6b7f9552015-05-27 17:21:45 -0700389 } else {
Tyler Gunndee56a82016-03-23 16:06:34 -0700390 findConferenceForAction(callId, "putExtras").putExtras(extras);
391 }
392 }
393
394 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700395 public void removeExtras(String callId, List<String> keys, Session.Info sessionInfo) {
Tyler Gunndee56a82016-03-23 16:06:34 -0700396 if (hasConnection(callId)) {
397 findConnectionForAction(callId, "removeExtra").removeExtras(keys);
398 } else {
399 findConferenceForAction(callId, "removeExtra").removeExtras(keys);
Santos Cordon6b7f9552015-05-27 17:21:45 -0700400 }
401 }
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -0800402
403 @Override
Hall Liua98f58b2017-11-07 17:59:28 -0800404 public void setAudioRoute(String callId, int audioRoute, String bluetoothAddress,
405 Session.Info sessionInfo) {
Tyler Gunnf5035432017-01-09 09:43:12 -0800406 if (hasConnection(callId)) {
407 // TODO(3pcalls): handle this for remote connections.
408 // Likely we don't want to do anything since it doesn't make sense for self-managed
409 // connections to go through a connection mgr.
410 }
411 }
412
413 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700414 public void onConnectionEvent(String callId, String event, Bundle extras,
415 Session.Info sessionInfo) {
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -0800416 if (mConnectionById.containsKey(callId)) {
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700417 findConnectionForAction(callId, "onConnectionEvent").onConnectionEvent(event,
418 extras);
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -0800419 }
420 }
Hall Liu57006aa2017-02-06 10:49:48 -0800421
422 @Override
423 public void onRttInitiationSuccess(String callId, Session.Info sessionInfo)
424 throws RemoteException {
425 if (hasConnection(callId)) {
426 findConnectionForAction(callId, "onRttInitiationSuccess")
427 .onRttInitiationSuccess();
428 } else {
429 Log.w(this, "onRttInitiationSuccess called on a remote conference");
430 }
431 }
432
433 @Override
434 public void onRttInitiationFailure(String callId, int reason, Session.Info sessionInfo)
435 throws RemoteException {
436 if (hasConnection(callId)) {
437 findConnectionForAction(callId, "onRttInitiationFailure")
438 .onRttInitiationFailure(reason);
439 } else {
440 Log.w(this, "onRttInitiationFailure called on a remote conference");
441 }
442 }
443
444 @Override
445 public void onRttSessionRemotelyTerminated(String callId, Session.Info sessionInfo)
446 throws RemoteException {
447 if (hasConnection(callId)) {
448 findConnectionForAction(callId, "onRttSessionRemotelyTerminated")
449 .onRttSessionRemotelyTerminated();
450 } else {
451 Log.w(this, "onRttSessionRemotelyTerminated called on a remote conference");
452 }
453 }
454
455 @Override
456 public void onRemoteRttRequest(String callId, Session.Info sessionInfo)
457 throws RemoteException {
458 if (hasConnection(callId)) {
459 findConnectionForAction(callId, "onRemoteRttRequest")
460 .onRemoteRttRequest();
461 } else {
462 Log.w(this, "onRemoteRttRequest called on a remote conference");
463 }
464 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700465 };
466
Ihab Awad5d0410f2014-07-30 10:07:40 -0700467 private final ConnectionServiceAdapterServant mServant =
468 new ConnectionServiceAdapterServant(mServantDelegate);
Santos Cordon52d8a152014-06-17 19:08:45 -0700469
Ihab Awad5d0410f2014-07-30 10:07:40 -0700470 private final DeathRecipient mDeathRecipient = new DeathRecipient() {
471 @Override
472 public void binderDied() {
473 for (RemoteConnection c : mConnectionById.values()) {
474 c.setDestroyed();
475 }
Ihab Awadb8e85c72014-08-23 20:34:57 -0700476 for (RemoteConference c : mConferenceById.values()) {
477 c.setDestroyed();
478 }
Ihab Awad5d0410f2014-07-30 10:07:40 -0700479 mConnectionById.clear();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700480 mConferenceById.clear();
Ihab Awad5d0410f2014-07-30 10:07:40 -0700481 mPendingConnections.clear();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700482 mOutgoingConnectionServiceRpc.asBinder().unlinkToDeath(mDeathRecipient, 0);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700483 }
484 };
485
Ihab Awadb8e85c72014-08-23 20:34:57 -0700486 private final IConnectionService mOutgoingConnectionServiceRpc;
487 private final ConnectionService mOurConnectionServiceImpl;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700488 private final Map<String, RemoteConnection> mConnectionById = new HashMap<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700489 private final Map<String, RemoteConference> mConferenceById = new HashMap<>();
Ihab Awad5d0410f2014-07-30 10:07:40 -0700490 private final Set<RemoteConnection> mPendingConnections = new HashSet<>();
491
Ihab Awadb8e85c72014-08-23 20:34:57 -0700492 RemoteConnectionService(
493 IConnectionService outgoingConnectionServiceRpc,
494 ConnectionService ourConnectionServiceImpl) throws RemoteException {
495 mOutgoingConnectionServiceRpc = outgoingConnectionServiceRpc;
496 mOutgoingConnectionServiceRpc.asBinder().linkToDeath(mDeathRecipient, 0);
497 mOurConnectionServiceImpl = ourConnectionServiceImpl;
Santos Cordon52d8a152014-06-17 19:08:45 -0700498 }
499
500 @Override
501 public String toString() {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700502 return "[RemoteCS - " + mOutgoingConnectionServiceRpc.asBinder().toString() + "]";
Santos Cordon52d8a152014-06-17 19:08:45 -0700503 }
504
Ihab Awadf8b69882014-07-25 15:14:01 -0700505 final RemoteConnection createRemoteConnection(
506 PhoneAccountHandle connectionManagerPhoneAccount,
507 ConnectionRequest request,
508 boolean isIncoming) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700509 final String id = UUID.randomUUID().toString();
Hall Liu95d55872017-01-25 17:12:49 -0800510 final ConnectionRequest newRequest = new ConnectionRequest.Builder()
511 .setAccountHandle(request.getAccountHandle())
512 .setAddress(request.getAddress())
513 .setExtras(request.getExtras())
514 .setVideoState(request.getVideoState())
515 .setRttPipeFromInCall(request.getRttPipeFromInCall())
516 .setRttPipeToInCall(request.getRttPipeToInCall())
517 .build();
Ihab Awad5d0410f2014-07-30 10:07:40 -0700518 try {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700519 if (mConnectionById.isEmpty()) {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700520 mOutgoingConnectionServiceRpc.addConnectionServiceAdapter(mServant.getStub(),
521 null /*Session.Info*/);
Ihab Awad8aecfed2014-08-08 17:06:11 -0700522 }
523 RemoteConnection connection =
Ihab Awadb8e85c72014-08-23 20:34:57 -0700524 new RemoteConnection(id, mOutgoingConnectionServiceRpc, newRequest);
Ihab Awad8aecfed2014-08-08 17:06:11 -0700525 mPendingConnections.add(connection);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700526 mConnectionById.put(id, connection);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700527 mOutgoingConnectionServiceRpc.createConnection(
Ihab Awad5d0410f2014-07-30 10:07:40 -0700528 connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700529 id,
Ihab Awad5d0410f2014-07-30 10:07:40 -0700530 newRequest,
Yorke Leec3cf9822014-10-02 09:38:39 -0700531 isIncoming,
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700532 false /* isUnknownCall */,
533 null /*Session.info*/);
Andrew Lee100e2932014-09-08 15:34:24 -0700534 connection.registerCallback(new RemoteConnection.Callback() {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700535 @Override
536 public void onDestroyed(RemoteConnection connection) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700537 mConnectionById.remove(id);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700538 maybeDisconnectAdapter();
Ihab Awad8aecfed2014-08-08 17:06:11 -0700539 }
540 });
Ihab Awad5d0410f2014-07-30 10:07:40 -0700541 return connection;
542 } catch (RemoteException e) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700543 return RemoteConnection.failure(
544 new DisconnectCause(DisconnectCause.ERROR, e.toString()));
Santos Cordon52d8a152014-06-17 19:08:45 -0700545 }
546 }
547
Ihab Awad50e35062014-09-30 09:17:03 -0700548 private boolean hasConnection(String callId) {
mike dooley879142b2014-10-08 13:39:28 -0700549 return mConnectionById.containsKey(callId);
Ihab Awad50e35062014-09-30 09:17:03 -0700550 }
551
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700552 private RemoteConnection findConnectionForAction(
553 String callId, String action) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700554 if (mConnectionById.containsKey(callId)) {
555 return mConnectionById.get(callId);
556 }
557 Log.w(this, "%s - Cannot find Connection %s", action, callId);
558 return NULL_CONNECTION;
Santos Cordon52d8a152014-06-17 19:08:45 -0700559 }
Ihab Awadb8e85c72014-08-23 20:34:57 -0700560
561 private RemoteConference findConferenceForAction(
562 String callId, String action) {
563 if (mConferenceById.containsKey(callId)) {
564 return mConferenceById.get(callId);
565 }
566 Log.w(this, "%s - Cannot find Conference %s", action, callId);
567 return NULL_CONFERENCE;
568 }
569
570 private void maybeDisconnectAdapter() {
571 if (mConnectionById.isEmpty() && mConferenceById.isEmpty()) {
572 try {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700573 mOutgoingConnectionServiceRpc.removeConnectionServiceAdapter(mServant.getStub(),
574 null /*Session.info*/);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700575 } catch (RemoteException e) {
576 }
577 }
578 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700579}