blob: 744544eb01f173805a6004812485713e3d1b62a5 [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 }
Tyler Gunn31f0e0b2018-02-13 08:39:45 -080096 connection.setStatusHints(parcel.getStatusHints());
97 connection.setIsVoipAudioMode(parcel.getIsVoipAudioMode());
98 connection.setRingbackRequested(parcel.isRingbackRequested());
99 connection.putExtras(parcel.getExtras());
Sailesh Nepal48031592014-07-18 14:21:23 -0700100 }
101 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700102
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700103 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700104 public void setActive(String callId, Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700105 if (mConnectionById.containsKey(callId)) {
106 findConnectionForAction(callId, "setActive")
107 .setState(Connection.STATE_ACTIVE);
108 } else {
109 findConferenceForAction(callId, "setActive")
110 .setState(Connection.STATE_ACTIVE);
111 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700112 }
113
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700114 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700115 public void setRinging(String callId, Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700116 findConnectionForAction(callId, "setRinging")
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700117 .setState(Connection.STATE_RINGING);
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700118 }
119
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700120 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700121 public void setDialing(String callId, Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700122 findConnectionForAction(callId, "setDialing")
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700123 .setState(Connection.STATE_DIALING);
Santos Cordon52d8a152014-06-17 19:08:45 -0700124 }
125
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700126 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700127 public void setPulling(String callId, Session.Info sessionInfo) {
Tyler Gunnc96b5e02016-07-07 22:53:57 -0700128 findConnectionForAction(callId, "setPulling")
129 .setState(Connection.STATE_PULLING_CALL);
130 }
131
132 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700133 public void setDisconnected(String callId, DisconnectCause disconnectCause,
134 Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700135 if (mConnectionById.containsKey(callId)) {
136 findConnectionForAction(callId, "setDisconnected")
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700137 .setDisconnected(disconnectCause);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700138 } else {
139 findConferenceForAction(callId, "setDisconnected")
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700140 .setDisconnected(disconnectCause);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700141 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700142 }
143
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700144 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700145 public void setOnHold(String callId, Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700146 if (mConnectionById.containsKey(callId)) {
147 findConnectionForAction(callId, "setOnHold")
148 .setState(Connection.STATE_HOLDING);
149 } else {
150 findConferenceForAction(callId, "setOnHold")
151 .setState(Connection.STATE_HOLDING);
152 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700153 }
154
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700155 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700156 public void setRingbackRequested(String callId, boolean ringing, Session.Info sessionInfo) {
Andrew Lee100e2932014-09-08 15:34:24 -0700157 findConnectionForAction(callId, "setRingbackRequested")
158 .setRingbackRequested(ringing);
Santos Cordon52d8a152014-06-17 19:08:45 -0700159 }
160
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700161 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700162 public void setConnectionCapabilities(String callId, int connectionCapabilities,
163 Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700164 if (mConnectionById.containsKey(callId)) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800165 findConnectionForAction(callId, "setConnectionCapabilities")
166 .setConnectionCapabilities(connectionCapabilities);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700167 } else {
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800168 findConferenceForAction(callId, "setConnectionCapabilities")
169 .setConnectionCapabilities(connectionCapabilities);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700170 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700171 }
172
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700173 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700174 public void setConnectionProperties(String callId, int connectionProperties,
175 Session.Info sessionInfo) {
Tyler Gunn720c6642016-03-22 09:02:47 -0700176 if (mConnectionById.containsKey(callId)) {
177 findConnectionForAction(callId, "setConnectionProperties")
178 .setConnectionProperties(connectionProperties);
179 } else {
180 findConferenceForAction(callId, "setConnectionProperties")
181 .setConnectionProperties(connectionProperties);
182 }
183 }
184
185 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700186 public void setIsConferenced(String callId, String conferenceCallId,
187 Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700188 // Note: callId should not be null; conferenceCallId may be null
189 RemoteConnection connection =
190 findConnectionForAction(callId, "setIsConferenced");
191 if (connection != NULL_CONNECTION) {
192 if (conferenceCallId == null) {
193 // 'connection' is being split from its conference
194 if (connection.getConference() != null) {
195 connection.getConference().removeConnection(connection);
196 }
197 } else {
198 RemoteConference conference =
199 findConferenceForAction(conferenceCallId, "setIsConferenced");
200 if (conference != NULL_CONFERENCE) {
201 conference.addConnection(connection);
202 }
203 }
204 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700205 }
206
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700207 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700208 public void setConferenceMergeFailed(String callId, Session.Info sessionInfo) {
Anthony Lee17455a32015-04-24 15:25:29 -0700209 // Nothing to do here.
210 // The event has already been handled and there is no state to update
211 // in the underlying connection or conference objects
212 }
213
214 @Override
Srikanth Chintalafcb15012017-05-04 20:58:34 +0530215 public void onPhoneAccountChanged(String callId, PhoneAccountHandle pHandle,
216 Session.Info sessionInfo) {
217 }
218
219 @Override
Pengquan Meng63d25a52017-11-21 18:01:13 -0800220 public void onConnectionServiceFocusReleased(Session.Info sessionInfo) {}
221
222 @Override
Ihab Awadb8e85c72014-08-23 20:34:57 -0700223 public void addConferenceCall(
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700224 final String callId, ParcelableConference parcel, Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700225 RemoteConference conference = new RemoteConference(callId,
226 mOutgoingConnectionServiceRpc);
227
228 for (String id : parcel.getConnectionIds()) {
229 RemoteConnection c = mConnectionById.get(id);
230 if (c != null) {
231 conference.addConnection(c);
232 }
233 }
Ihab Awadb8e85c72014-08-23 20:34:57 -0700234 if (conference.getConnections().size() == 0) {
235 // A conference was created, but none of its connections are ones that have been
236 // created by, and therefore being tracked by, this remote connection service. It
237 // is of no interest to us.
Tyler Gunncd6ccfd2016-10-17 15:48:19 -0700238 Log.d(this, "addConferenceCall - skipping");
Ihab Awadb8e85c72014-08-23 20:34:57 -0700239 return;
240 }
241
242 conference.setState(parcel.getState());
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800243 conference.setConnectionCapabilities(parcel.getConnectionCapabilities());
Tyler Gunn04ce7572016-08-15 10:56:12 -0700244 conference.setConnectionProperties(parcel.getConnectionProperties());
Tyler Gunncd6ccfd2016-10-17 15:48:19 -0700245 conference.putExtras(parcel.getExtras());
Ihab Awadb8e85c72014-08-23 20:34:57 -0700246 mConferenceById.put(callId, conference);
Tyler Gunncd6ccfd2016-10-17 15:48:19 -0700247
248 // Stash the original connection ID as it exists in the source ConnectionService.
249 // Telecom will use this to avoid adding duplicates later.
250 // See comments on Connection.EXTRA_ORIGINAL_CONNECTION_ID for more information.
251 Bundle newExtras = new Bundle();
252 newExtras.putString(Connection.EXTRA_ORIGINAL_CONNECTION_ID, callId);
253 conference.putExtras(newExtras);
254
Andrew Lee100e2932014-09-08 15:34:24 -0700255 conference.registerCallback(new RemoteConference.Callback() {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700256 @Override
257 public void onDestroyed(RemoteConference c) {
258 mConferenceById.remove(callId);
259 maybeDisconnectAdapter();
260 }
261 });
262
263 mOurConnectionServiceImpl.addRemoteConference(conference);
Santos Cordon52d8a152014-06-17 19:08:45 -0700264 }
265
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700266 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700267 public void removeCall(String callId, Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700268 if (mConnectionById.containsKey(callId)) {
269 findConnectionForAction(callId, "removeCall")
270 .setDestroyed();
271 } else {
272 findConferenceForAction(callId, "removeCall")
273 .setDestroyed();
274 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700275 }
276
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700277 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700278 public void onPostDialWait(String callId, String remaining, Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700279 findConnectionForAction(callId, "onPostDialWait")
280 .setPostDialWait(remaining);
Santos Cordon52d8a152014-06-17 19:08:45 -0700281 }
282
Santos Cordon52d8a152014-06-17 19:08:45 -0700283 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700284 public void onPostDialChar(String callId, char nextChar, Session.Info sessionInfo) {
Nancy Chen27d1c2d2014-12-15 16:12:50 -0800285 findConnectionForAction(callId, "onPostDialChar")
286 .onPostDialChar(nextChar);
287 }
288
289 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700290 public void queryRemoteConnectionServices(RemoteServiceCallback callback,
291 Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700292 // Not supported from remote connection service.
Santos Cordon52d8a152014-06-17 19:08:45 -0700293 }
Tyler Gunn8d83fa92014-07-01 11:31:21 -0700294
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700295 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700296 public void setVideoProvider(String callId, IVideoProvider videoProvider,
297 Session.Info sessionInfo) {
Tyler Gunnbf9c6fd2016-11-09 10:19:23 -0800298
299 String callingPackage = mOurConnectionServiceImpl.getApplicationContext()
300 .getOpPackageName();
Tyler Gunn159f35c2017-03-02 09:28:37 -0800301 int targetSdkVersion = mOurConnectionServiceImpl.getApplicationInfo().targetSdkVersion;
Sailesh Nepal11aeae52015-01-28 16:54:09 -0800302 RemoteConnection.VideoProvider remoteVideoProvider = null;
303 if (videoProvider != null) {
Tyler Gunnbf9c6fd2016-11-09 10:19:23 -0800304 remoteVideoProvider = new RemoteConnection.VideoProvider(videoProvider,
Tyler Gunn159f35c2017-03-02 09:28:37 -0800305 callingPackage, targetSdkVersion);
Sailesh Nepal11aeae52015-01-28 16:54:09 -0800306 }
Ihab Awada64627c2014-08-20 09:36:40 -0700307 findConnectionForAction(callId, "setVideoProvider")
Sailesh Nepal11aeae52015-01-28 16:54:09 -0800308 .setVideoProvider(remoteVideoProvider);
Tyler Gunnaa07df82014-07-17 07:50:22 -0700309 }
310
311 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700312 public void setVideoState(String callId, int videoState, Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700313 findConnectionForAction(callId, "setVideoState")
314 .setVideoState(videoState);
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700315 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700316
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700317 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700318 public void setIsVoipAudioMode(String callId, boolean isVoip, Session.Info sessionInfo) {
Andrew Lee100e2932014-09-08 15:34:24 -0700319 findConnectionForAction(callId, "setIsVoipAudioMode")
320 .setIsVoipAudioMode(isVoip);
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700321 }
Sailesh Nepal61203862014-07-11 14:50:13 -0700322
323 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700324 public void setStatusHints(String callId, StatusHints statusHints,
325 Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700326 findConnectionForAction(callId, "setStatusHints")
327 .setStatusHints(statusHints);
Sailesh Nepal61203862014-07-11 14:50:13 -0700328 }
329
330 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700331 public void setAddress(String callId, Uri address, int presentation,
332 Session.Info sessionInfo) {
Andrew Lee100e2932014-09-08 15:34:24 -0700333 findConnectionForAction(callId, "setAddress")
334 .setAddress(address, presentation);
Sailesh Nepal61203862014-07-11 14:50:13 -0700335 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700336
337 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700338 public void setCallerDisplayName(String callId, String callerDisplayName,
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700339 int presentation, Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700340 findConnectionForAction(callId, "setCallerDisplayName")
341 .setCallerDisplayName(callerDisplayName, presentation);
342 }
343
344 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700345 public IBinder asBinder() {
346 throw new UnsupportedOperationException();
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700347 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700348
349 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700350 public final void setConferenceableConnections(String callId,
351 List<String> conferenceableConnectionIds, Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700352 List<RemoteConnection> conferenceable = new ArrayList<>();
353 for (String id : conferenceableConnectionIds) {
354 if (mConnectionById.containsKey(id)) {
355 conferenceable.add(mConnectionById.get(id));
356 }
357 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700358
Ihab Awad50e35062014-09-30 09:17:03 -0700359 if (hasConnection(callId)) {
360 findConnectionForAction(callId, "setConferenceableConnections")
361 .setConferenceableConnections(conferenceable);
362 } else {
363 findConferenceForAction(callId, "setConferenceableConnections")
364 .setConferenceableConnections(conferenceable);
365 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700366 }
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700367
368 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700369 public void addExistingConnection(String callId, ParcelableConnection connection,
370 Session.Info sessionInfo) {
Tyler Gunnbf9c6fd2016-11-09 10:19:23 -0800371 String callingPackage = mOurConnectionServiceImpl.getApplicationContext().
372 getOpPackageName();
Tyler Gunn159f35c2017-03-02 09:28:37 -0800373 int callingTargetSdkVersion = mOurConnectionServiceImpl.getApplicationInfo()
374 .targetSdkVersion;
Tyler Gunncd6ccfd2016-10-17 15:48:19 -0700375 RemoteConnection remoteConnection = new RemoteConnection(callId,
Tyler Gunn159f35c2017-03-02 09:28:37 -0800376 mOutgoingConnectionServiceRpc, connection, callingPackage,
377 callingTargetSdkVersion);
Tyler Gunncd6ccfd2016-10-17 15:48:19 -0700378 mConnectionById.put(callId, remoteConnection);
379 remoteConnection.registerCallback(new RemoteConnection.Callback() {
380 @Override
381 public void onDestroyed(RemoteConnection connection) {
382 mConnectionById.remove(callId);
383 maybeDisconnectAdapter();
384 }
385 });
386 mOurConnectionServiceImpl.addRemoteExistingConnection(remoteConnection);
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700387 }
Santos Cordon6b7f9552015-05-27 17:21:45 -0700388
389 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700390 public void putExtras(String callId, Bundle extras, Session.Info sessionInfo) {
Tyler Gunndee56a82016-03-23 16:06:34 -0700391 if (hasConnection(callId)) {
392 findConnectionForAction(callId, "putExtras").putExtras(extras);
Santos Cordon6b7f9552015-05-27 17:21:45 -0700393 } else {
Tyler Gunndee56a82016-03-23 16:06:34 -0700394 findConferenceForAction(callId, "putExtras").putExtras(extras);
395 }
396 }
397
398 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700399 public void removeExtras(String callId, List<String> keys, Session.Info sessionInfo) {
Tyler Gunndee56a82016-03-23 16:06:34 -0700400 if (hasConnection(callId)) {
401 findConnectionForAction(callId, "removeExtra").removeExtras(keys);
402 } else {
403 findConferenceForAction(callId, "removeExtra").removeExtras(keys);
Santos Cordon6b7f9552015-05-27 17:21:45 -0700404 }
405 }
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -0800406
407 @Override
Hall Liua98f58b2017-11-07 17:59:28 -0800408 public void setAudioRoute(String callId, int audioRoute, String bluetoothAddress,
409 Session.Info sessionInfo) {
Tyler Gunnf5035432017-01-09 09:43:12 -0800410 if (hasConnection(callId)) {
411 // TODO(3pcalls): handle this for remote connections.
412 // Likely we don't want to do anything since it doesn't make sense for self-managed
413 // connections to go through a connection mgr.
414 }
415 }
416
417 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700418 public void onConnectionEvent(String callId, String event, Bundle extras,
419 Session.Info sessionInfo) {
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -0800420 if (mConnectionById.containsKey(callId)) {
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700421 findConnectionForAction(callId, "onConnectionEvent").onConnectionEvent(event,
422 extras);
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -0800423 }
424 }
Hall Liu57006aa2017-02-06 10:49:48 -0800425
426 @Override
427 public void onRttInitiationSuccess(String callId, Session.Info sessionInfo)
428 throws RemoteException {
429 if (hasConnection(callId)) {
430 findConnectionForAction(callId, "onRttInitiationSuccess")
431 .onRttInitiationSuccess();
432 } else {
433 Log.w(this, "onRttInitiationSuccess called on a remote conference");
434 }
435 }
436
437 @Override
438 public void onRttInitiationFailure(String callId, int reason, Session.Info sessionInfo)
439 throws RemoteException {
440 if (hasConnection(callId)) {
441 findConnectionForAction(callId, "onRttInitiationFailure")
442 .onRttInitiationFailure(reason);
443 } else {
444 Log.w(this, "onRttInitiationFailure called on a remote conference");
445 }
446 }
447
448 @Override
449 public void onRttSessionRemotelyTerminated(String callId, Session.Info sessionInfo)
450 throws RemoteException {
451 if (hasConnection(callId)) {
452 findConnectionForAction(callId, "onRttSessionRemotelyTerminated")
453 .onRttSessionRemotelyTerminated();
454 } else {
455 Log.w(this, "onRttSessionRemotelyTerminated called on a remote conference");
456 }
457 }
458
459 @Override
460 public void onRemoteRttRequest(String callId, Session.Info sessionInfo)
461 throws RemoteException {
462 if (hasConnection(callId)) {
463 findConnectionForAction(callId, "onRemoteRttRequest")
464 .onRemoteRttRequest();
465 } else {
466 Log.w(this, "onRemoteRttRequest called on a remote conference");
467 }
468 }
Mengjun Leng25707742017-07-04 11:10:37 +0800469
470 @Override
471 public void resetConnectionTime(String callId, Session.Info sessionInfo) {
472 // Do nothing
473 }
Tyler Gunn68a73a42018-10-03 15:38:57 -0700474
475 @Override
476 public void setConferenceState(String callId, boolean isConference,
477 Session.Info sessionInfo) {
478 // Do nothing
479 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700480 };
481
Ihab Awad5d0410f2014-07-30 10:07:40 -0700482 private final ConnectionServiceAdapterServant mServant =
483 new ConnectionServiceAdapterServant(mServantDelegate);
Santos Cordon52d8a152014-06-17 19:08:45 -0700484
Ihab Awad5d0410f2014-07-30 10:07:40 -0700485 private final DeathRecipient mDeathRecipient = new DeathRecipient() {
486 @Override
487 public void binderDied() {
488 for (RemoteConnection c : mConnectionById.values()) {
489 c.setDestroyed();
490 }
Ihab Awadb8e85c72014-08-23 20:34:57 -0700491 for (RemoteConference c : mConferenceById.values()) {
492 c.setDestroyed();
493 }
Ihab Awad5d0410f2014-07-30 10:07:40 -0700494 mConnectionById.clear();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700495 mConferenceById.clear();
Ihab Awad5d0410f2014-07-30 10:07:40 -0700496 mPendingConnections.clear();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700497 mOutgoingConnectionServiceRpc.asBinder().unlinkToDeath(mDeathRecipient, 0);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700498 }
499 };
500
Ihab Awadb8e85c72014-08-23 20:34:57 -0700501 private final IConnectionService mOutgoingConnectionServiceRpc;
502 private final ConnectionService mOurConnectionServiceImpl;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700503 private final Map<String, RemoteConnection> mConnectionById = new HashMap<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700504 private final Map<String, RemoteConference> mConferenceById = new HashMap<>();
Ihab Awad5d0410f2014-07-30 10:07:40 -0700505 private final Set<RemoteConnection> mPendingConnections = new HashSet<>();
506
Ihab Awadb8e85c72014-08-23 20:34:57 -0700507 RemoteConnectionService(
508 IConnectionService outgoingConnectionServiceRpc,
509 ConnectionService ourConnectionServiceImpl) throws RemoteException {
510 mOutgoingConnectionServiceRpc = outgoingConnectionServiceRpc;
511 mOutgoingConnectionServiceRpc.asBinder().linkToDeath(mDeathRecipient, 0);
512 mOurConnectionServiceImpl = ourConnectionServiceImpl;
Santos Cordon52d8a152014-06-17 19:08:45 -0700513 }
514
515 @Override
516 public String toString() {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700517 return "[RemoteCS - " + mOutgoingConnectionServiceRpc.asBinder().toString() + "]";
Santos Cordon52d8a152014-06-17 19:08:45 -0700518 }
519
Ihab Awadf8b69882014-07-25 15:14:01 -0700520 final RemoteConnection createRemoteConnection(
521 PhoneAccountHandle connectionManagerPhoneAccount,
522 ConnectionRequest request,
523 boolean isIncoming) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700524 final String id = UUID.randomUUID().toString();
Hall Liu95d55872017-01-25 17:12:49 -0800525 final ConnectionRequest newRequest = new ConnectionRequest.Builder()
526 .setAccountHandle(request.getAccountHandle())
527 .setAddress(request.getAddress())
528 .setExtras(request.getExtras())
529 .setVideoState(request.getVideoState())
530 .setRttPipeFromInCall(request.getRttPipeFromInCall())
531 .setRttPipeToInCall(request.getRttPipeToInCall())
532 .build();
Ihab Awad5d0410f2014-07-30 10:07:40 -0700533 try {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700534 if (mConnectionById.isEmpty()) {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700535 mOutgoingConnectionServiceRpc.addConnectionServiceAdapter(mServant.getStub(),
536 null /*Session.Info*/);
Ihab Awad8aecfed2014-08-08 17:06:11 -0700537 }
538 RemoteConnection connection =
Ihab Awadb8e85c72014-08-23 20:34:57 -0700539 new RemoteConnection(id, mOutgoingConnectionServiceRpc, newRequest);
Ihab Awad8aecfed2014-08-08 17:06:11 -0700540 mPendingConnections.add(connection);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700541 mConnectionById.put(id, connection);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700542 mOutgoingConnectionServiceRpc.createConnection(
Ihab Awad5d0410f2014-07-30 10:07:40 -0700543 connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700544 id,
Ihab Awad5d0410f2014-07-30 10:07:40 -0700545 newRequest,
Yorke Leec3cf9822014-10-02 09:38:39 -0700546 isIncoming,
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700547 false /* isUnknownCall */,
548 null /*Session.info*/);
Andrew Lee100e2932014-09-08 15:34:24 -0700549 connection.registerCallback(new RemoteConnection.Callback() {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700550 @Override
551 public void onDestroyed(RemoteConnection connection) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700552 mConnectionById.remove(id);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700553 maybeDisconnectAdapter();
Ihab Awad8aecfed2014-08-08 17:06:11 -0700554 }
555 });
Ihab Awad5d0410f2014-07-30 10:07:40 -0700556 return connection;
557 } catch (RemoteException e) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700558 return RemoteConnection.failure(
559 new DisconnectCause(DisconnectCause.ERROR, e.toString()));
Santos Cordon52d8a152014-06-17 19:08:45 -0700560 }
561 }
562
Ihab Awad50e35062014-09-30 09:17:03 -0700563 private boolean hasConnection(String callId) {
mike dooley879142b2014-10-08 13:39:28 -0700564 return mConnectionById.containsKey(callId);
Ihab Awad50e35062014-09-30 09:17:03 -0700565 }
566
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700567 private RemoteConnection findConnectionForAction(
568 String callId, String action) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700569 if (mConnectionById.containsKey(callId)) {
570 return mConnectionById.get(callId);
571 }
572 Log.w(this, "%s - Cannot find Connection %s", action, callId);
573 return NULL_CONNECTION;
Santos Cordon52d8a152014-06-17 19:08:45 -0700574 }
Ihab Awadb8e85c72014-08-23 20:34:57 -0700575
576 private RemoteConference findConferenceForAction(
577 String callId, String action) {
578 if (mConferenceById.containsKey(callId)) {
579 return mConferenceById.get(callId);
580 }
581 Log.w(this, "%s - Cannot find Conference %s", action, callId);
582 return NULL_CONFERENCE;
583 }
584
585 private void maybeDisconnectAdapter() {
586 if (mConnectionById.isEmpty() && mConferenceById.isEmpty()) {
587 try {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700588 mOutgoingConnectionServiceRpc.removeConnectionServiceAdapter(mServant.getStub(),
589 null /*Session.info*/);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700590 } catch (RemoteException e) {
591 }
592 }
593 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700594}