blob: 76640e036eeb7b25670a5a83790c073d79718825 [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
Ravi Paluri80aa2142019-12-02 11:57:37 +0530104 public void handleCreateConferenceComplete(
105 String id,
106 ConnectionRequest request,
107 ParcelableConference parcel,
108 Session.Info info) {
109 }
110
111 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700112 public void setActive(String callId, Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700113 if (mConnectionById.containsKey(callId)) {
114 findConnectionForAction(callId, "setActive")
115 .setState(Connection.STATE_ACTIVE);
116 } else {
117 findConferenceForAction(callId, "setActive")
118 .setState(Connection.STATE_ACTIVE);
119 }
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 setRinging(String callId, Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700124 findConnectionForAction(callId, "setRinging")
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700125 .setState(Connection.STATE_RINGING);
Andrew Lee5ffbe8b82014-06-20 16:29:33 -0700126 }
127
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700128 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700129 public void setDialing(String callId, Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700130 findConnectionForAction(callId, "setDialing")
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700131 .setState(Connection.STATE_DIALING);
Santos Cordon52d8a152014-06-17 19:08:45 -0700132 }
133
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700134 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700135 public void setPulling(String callId, Session.Info sessionInfo) {
Tyler Gunnc96b5e02016-07-07 22:53:57 -0700136 findConnectionForAction(callId, "setPulling")
137 .setState(Connection.STATE_PULLING_CALL);
138 }
139
140 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700141 public void setDisconnected(String callId, DisconnectCause disconnectCause,
142 Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700143 if (mConnectionById.containsKey(callId)) {
144 findConnectionForAction(callId, "setDisconnected")
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700145 .setDisconnected(disconnectCause);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700146 } else {
147 findConferenceForAction(callId, "setDisconnected")
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700148 .setDisconnected(disconnectCause);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700149 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700150 }
151
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700152 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700153 public void setOnHold(String callId, Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700154 if (mConnectionById.containsKey(callId)) {
155 findConnectionForAction(callId, "setOnHold")
156 .setState(Connection.STATE_HOLDING);
157 } else {
158 findConferenceForAction(callId, "setOnHold")
159 .setState(Connection.STATE_HOLDING);
160 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700161 }
162
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700163 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700164 public void setRingbackRequested(String callId, boolean ringing, Session.Info sessionInfo) {
Andrew Lee100e2932014-09-08 15:34:24 -0700165 findConnectionForAction(callId, "setRingbackRequested")
166 .setRingbackRequested(ringing);
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 setConnectionCapabilities(String callId, int connectionCapabilities,
171 Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700172 if (mConnectionById.containsKey(callId)) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800173 findConnectionForAction(callId, "setConnectionCapabilities")
174 .setConnectionCapabilities(connectionCapabilities);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700175 } else {
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800176 findConferenceForAction(callId, "setConnectionCapabilities")
177 .setConnectionCapabilities(connectionCapabilities);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700178 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700179 }
180
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700181 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700182 public void setConnectionProperties(String callId, int connectionProperties,
183 Session.Info sessionInfo) {
Tyler Gunn720c6642016-03-22 09:02:47 -0700184 if (mConnectionById.containsKey(callId)) {
185 findConnectionForAction(callId, "setConnectionProperties")
186 .setConnectionProperties(connectionProperties);
187 } else {
188 findConferenceForAction(callId, "setConnectionProperties")
189 .setConnectionProperties(connectionProperties);
190 }
191 }
192
193 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700194 public void setIsConferenced(String callId, String conferenceCallId,
195 Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700196 // Note: callId should not be null; conferenceCallId may be null
197 RemoteConnection connection =
198 findConnectionForAction(callId, "setIsConferenced");
199 if (connection != NULL_CONNECTION) {
200 if (conferenceCallId == null) {
201 // 'connection' is being split from its conference
202 if (connection.getConference() != null) {
203 connection.getConference().removeConnection(connection);
204 }
205 } else {
206 RemoteConference conference =
207 findConferenceForAction(conferenceCallId, "setIsConferenced");
208 if (conference != NULL_CONFERENCE) {
209 conference.addConnection(connection);
210 }
211 }
212 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700213 }
214
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700215 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700216 public void setConferenceMergeFailed(String callId, Session.Info sessionInfo) {
Anthony Lee17455a32015-04-24 15:25:29 -0700217 // Nothing to do here.
218 // The event has already been handled and there is no state to update
219 // in the underlying connection or conference objects
220 }
221
222 @Override
Srikanth Chintalafcb15012017-05-04 20:58:34 +0530223 public void onPhoneAccountChanged(String callId, PhoneAccountHandle pHandle,
224 Session.Info sessionInfo) {
225 }
226
227 @Override
Pengquan Meng63d25a52017-11-21 18:01:13 -0800228 public void onConnectionServiceFocusReleased(Session.Info sessionInfo) {}
229
230 @Override
Ihab Awadb8e85c72014-08-23 20:34:57 -0700231 public void addConferenceCall(
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700232 final String callId, ParcelableConference parcel, Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700233 RemoteConference conference = new RemoteConference(callId,
234 mOutgoingConnectionServiceRpc);
235
236 for (String id : parcel.getConnectionIds()) {
237 RemoteConnection c = mConnectionById.get(id);
238 if (c != null) {
239 conference.addConnection(c);
240 }
241 }
Ihab Awadb8e85c72014-08-23 20:34:57 -0700242 if (conference.getConnections().size() == 0) {
243 // A conference was created, but none of its connections are ones that have been
244 // created by, and therefore being tracked by, this remote connection service. It
245 // is of no interest to us.
Tyler Gunncd6ccfd2016-10-17 15:48:19 -0700246 Log.d(this, "addConferenceCall - skipping");
Ihab Awadb8e85c72014-08-23 20:34:57 -0700247 return;
248 }
249
250 conference.setState(parcel.getState());
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800251 conference.setConnectionCapabilities(parcel.getConnectionCapabilities());
Tyler Gunn04ce7572016-08-15 10:56:12 -0700252 conference.setConnectionProperties(parcel.getConnectionProperties());
Tyler Gunncd6ccfd2016-10-17 15:48:19 -0700253 conference.putExtras(parcel.getExtras());
Ihab Awadb8e85c72014-08-23 20:34:57 -0700254 mConferenceById.put(callId, conference);
Tyler Gunncd6ccfd2016-10-17 15:48:19 -0700255
256 // Stash the original connection ID as it exists in the source ConnectionService.
257 // Telecom will use this to avoid adding duplicates later.
258 // See comments on Connection.EXTRA_ORIGINAL_CONNECTION_ID for more information.
259 Bundle newExtras = new Bundle();
260 newExtras.putString(Connection.EXTRA_ORIGINAL_CONNECTION_ID, callId);
261 conference.putExtras(newExtras);
262
Andrew Lee100e2932014-09-08 15:34:24 -0700263 conference.registerCallback(new RemoteConference.Callback() {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700264 @Override
265 public void onDestroyed(RemoteConference c) {
266 mConferenceById.remove(callId);
267 maybeDisconnectAdapter();
268 }
269 });
270
271 mOurConnectionServiceImpl.addRemoteConference(conference);
Santos Cordon52d8a152014-06-17 19:08:45 -0700272 }
273
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700274 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700275 public void removeCall(String callId, Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700276 if (mConnectionById.containsKey(callId)) {
277 findConnectionForAction(callId, "removeCall")
278 .setDestroyed();
279 } else {
280 findConferenceForAction(callId, "removeCall")
281 .setDestroyed();
282 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700283 }
284
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700285 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700286 public void onPostDialWait(String callId, String remaining, Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700287 findConnectionForAction(callId, "onPostDialWait")
288 .setPostDialWait(remaining);
Santos Cordon52d8a152014-06-17 19:08:45 -0700289 }
290
Santos Cordon52d8a152014-06-17 19:08:45 -0700291 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700292 public void onPostDialChar(String callId, char nextChar, Session.Info sessionInfo) {
Nancy Chen27d1c2d2014-12-15 16:12:50 -0800293 findConnectionForAction(callId, "onPostDialChar")
294 .onPostDialChar(nextChar);
295 }
296
297 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700298 public void queryRemoteConnectionServices(RemoteServiceCallback callback,
Tyler Gunn4c69fb32019-05-17 10:49:16 -0700299 String callingPackage, Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700300 // Not supported from remote connection service.
Santos Cordon52d8a152014-06-17 19:08:45 -0700301 }
Tyler Gunn8d83fa92014-07-01 11:31:21 -0700302
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700303 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700304 public void setVideoProvider(String callId, IVideoProvider videoProvider,
305 Session.Info sessionInfo) {
Tyler Gunnbf9c6fd2016-11-09 10:19:23 -0800306
307 String callingPackage = mOurConnectionServiceImpl.getApplicationContext()
308 .getOpPackageName();
Tyler Gunn159f35c2017-03-02 09:28:37 -0800309 int targetSdkVersion = mOurConnectionServiceImpl.getApplicationInfo().targetSdkVersion;
Sailesh Nepal11aeae52015-01-28 16:54:09 -0800310 RemoteConnection.VideoProvider remoteVideoProvider = null;
311 if (videoProvider != null) {
Tyler Gunnbf9c6fd2016-11-09 10:19:23 -0800312 remoteVideoProvider = new RemoteConnection.VideoProvider(videoProvider,
Tyler Gunn159f35c2017-03-02 09:28:37 -0800313 callingPackage, targetSdkVersion);
Sailesh Nepal11aeae52015-01-28 16:54:09 -0800314 }
Ihab Awada64627c2014-08-20 09:36:40 -0700315 findConnectionForAction(callId, "setVideoProvider")
Sailesh Nepal11aeae52015-01-28 16:54:09 -0800316 .setVideoProvider(remoteVideoProvider);
Tyler Gunnaa07df82014-07-17 07:50:22 -0700317 }
318
319 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700320 public void setVideoState(String callId, int videoState, Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700321 findConnectionForAction(callId, "setVideoState")
322 .setVideoState(videoState);
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700323 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700324
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700325 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700326 public void setIsVoipAudioMode(String callId, boolean isVoip, Session.Info sessionInfo) {
Andrew Lee100e2932014-09-08 15:34:24 -0700327 findConnectionForAction(callId, "setIsVoipAudioMode")
328 .setIsVoipAudioMode(isVoip);
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700329 }
Sailesh Nepal61203862014-07-11 14:50:13 -0700330
331 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700332 public void setStatusHints(String callId, StatusHints statusHints,
333 Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700334 findConnectionForAction(callId, "setStatusHints")
335 .setStatusHints(statusHints);
Sailesh Nepal61203862014-07-11 14:50:13 -0700336 }
337
338 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700339 public void setAddress(String callId, Uri address, int presentation,
340 Session.Info sessionInfo) {
Andrew Lee100e2932014-09-08 15:34:24 -0700341 findConnectionForAction(callId, "setAddress")
342 .setAddress(address, presentation);
Sailesh Nepal61203862014-07-11 14:50:13 -0700343 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700344
345 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700346 public void setCallerDisplayName(String callId, String callerDisplayName,
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700347 int presentation, Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700348 findConnectionForAction(callId, "setCallerDisplayName")
349 .setCallerDisplayName(callerDisplayName, presentation);
350 }
351
352 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700353 public IBinder asBinder() {
354 throw new UnsupportedOperationException();
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700355 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700356
357 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700358 public final void setConferenceableConnections(String callId,
359 List<String> conferenceableConnectionIds, Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700360 List<RemoteConnection> conferenceable = new ArrayList<>();
361 for (String id : conferenceableConnectionIds) {
362 if (mConnectionById.containsKey(id)) {
363 conferenceable.add(mConnectionById.get(id));
364 }
365 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700366
Ihab Awad50e35062014-09-30 09:17:03 -0700367 if (hasConnection(callId)) {
368 findConnectionForAction(callId, "setConferenceableConnections")
369 .setConferenceableConnections(conferenceable);
370 } else {
371 findConferenceForAction(callId, "setConferenceableConnections")
372 .setConferenceableConnections(conferenceable);
373 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700374 }
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700375
376 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700377 public void addExistingConnection(String callId, ParcelableConnection connection,
378 Session.Info sessionInfo) {
Tyler Gunnbf9c6fd2016-11-09 10:19:23 -0800379 String callingPackage = mOurConnectionServiceImpl.getApplicationContext().
380 getOpPackageName();
Tyler Gunn159f35c2017-03-02 09:28:37 -0800381 int callingTargetSdkVersion = mOurConnectionServiceImpl.getApplicationInfo()
382 .targetSdkVersion;
Tyler Gunncd6ccfd2016-10-17 15:48:19 -0700383 RemoteConnection remoteConnection = new RemoteConnection(callId,
Tyler Gunn159f35c2017-03-02 09:28:37 -0800384 mOutgoingConnectionServiceRpc, connection, callingPackage,
385 callingTargetSdkVersion);
Tyler Gunncd6ccfd2016-10-17 15:48:19 -0700386 mConnectionById.put(callId, remoteConnection);
387 remoteConnection.registerCallback(new RemoteConnection.Callback() {
388 @Override
389 public void onDestroyed(RemoteConnection connection) {
390 mConnectionById.remove(callId);
391 maybeDisconnectAdapter();
392 }
393 });
394 mOurConnectionServiceImpl.addRemoteExistingConnection(remoteConnection);
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700395 }
Santos Cordon6b7f9552015-05-27 17:21:45 -0700396
397 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700398 public void putExtras(String callId, Bundle extras, Session.Info sessionInfo) {
Tyler Gunndee56a82016-03-23 16:06:34 -0700399 if (hasConnection(callId)) {
400 findConnectionForAction(callId, "putExtras").putExtras(extras);
Santos Cordon6b7f9552015-05-27 17:21:45 -0700401 } else {
Tyler Gunndee56a82016-03-23 16:06:34 -0700402 findConferenceForAction(callId, "putExtras").putExtras(extras);
403 }
404 }
405
406 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700407 public void removeExtras(String callId, List<String> keys, Session.Info sessionInfo) {
Tyler Gunndee56a82016-03-23 16:06:34 -0700408 if (hasConnection(callId)) {
409 findConnectionForAction(callId, "removeExtra").removeExtras(keys);
410 } else {
411 findConferenceForAction(callId, "removeExtra").removeExtras(keys);
Santos Cordon6b7f9552015-05-27 17:21:45 -0700412 }
413 }
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -0800414
415 @Override
Hall Liua98f58b52017-11-07 17:59:28 -0800416 public void setAudioRoute(String callId, int audioRoute, String bluetoothAddress,
417 Session.Info sessionInfo) {
Tyler Gunnf5035432017-01-09 09:43:12 -0800418 if (hasConnection(callId)) {
419 // TODO(3pcalls): handle this for remote connections.
420 // Likely we don't want to do anything since it doesn't make sense for self-managed
421 // connections to go through a connection mgr.
422 }
423 }
424
425 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700426 public void onConnectionEvent(String callId, String event, Bundle extras,
427 Session.Info sessionInfo) {
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -0800428 if (mConnectionById.containsKey(callId)) {
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700429 findConnectionForAction(callId, "onConnectionEvent").onConnectionEvent(event,
430 extras);
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -0800431 }
432 }
Hall Liu57006aa2017-02-06 10:49:48 -0800433
434 @Override
435 public void onRttInitiationSuccess(String callId, Session.Info sessionInfo)
436 throws RemoteException {
437 if (hasConnection(callId)) {
438 findConnectionForAction(callId, "onRttInitiationSuccess")
439 .onRttInitiationSuccess();
440 } else {
441 Log.w(this, "onRttInitiationSuccess called on a remote conference");
442 }
443 }
444
445 @Override
446 public void onRttInitiationFailure(String callId, int reason, Session.Info sessionInfo)
447 throws RemoteException {
448 if (hasConnection(callId)) {
449 findConnectionForAction(callId, "onRttInitiationFailure")
450 .onRttInitiationFailure(reason);
451 } else {
452 Log.w(this, "onRttInitiationFailure called on a remote conference");
453 }
454 }
455
456 @Override
457 public void onRttSessionRemotelyTerminated(String callId, Session.Info sessionInfo)
458 throws RemoteException {
459 if (hasConnection(callId)) {
460 findConnectionForAction(callId, "onRttSessionRemotelyTerminated")
461 .onRttSessionRemotelyTerminated();
462 } else {
463 Log.w(this, "onRttSessionRemotelyTerminated called on a remote conference");
464 }
465 }
466
467 @Override
468 public void onRemoteRttRequest(String callId, Session.Info sessionInfo)
469 throws RemoteException {
470 if (hasConnection(callId)) {
471 findConnectionForAction(callId, "onRemoteRttRequest")
472 .onRemoteRttRequest();
473 } else {
474 Log.w(this, "onRemoteRttRequest called on a remote conference");
475 }
476 }
Mengjun Leng25707742017-07-04 11:10:37 +0800477
478 @Override
479 public void resetConnectionTime(String callId, Session.Info sessionInfo) {
480 // Do nothing
481 }
Tyler Gunn68a73a42018-10-03 15:38:57 -0700482
483 @Override
484 public void setConferenceState(String callId, boolean isConference,
485 Session.Info sessionInfo) {
486 // Do nothing
487 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700488 };
489
Ihab Awad5d0410f2014-07-30 10:07:40 -0700490 private final ConnectionServiceAdapterServant mServant =
491 new ConnectionServiceAdapterServant(mServantDelegate);
Santos Cordon52d8a152014-06-17 19:08:45 -0700492
Ihab Awad5d0410f2014-07-30 10:07:40 -0700493 private final DeathRecipient mDeathRecipient = new DeathRecipient() {
494 @Override
495 public void binderDied() {
496 for (RemoteConnection c : mConnectionById.values()) {
497 c.setDestroyed();
498 }
Ihab Awadb8e85c72014-08-23 20:34:57 -0700499 for (RemoteConference c : mConferenceById.values()) {
500 c.setDestroyed();
501 }
Ihab Awad5d0410f2014-07-30 10:07:40 -0700502 mConnectionById.clear();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700503 mConferenceById.clear();
Ihab Awad5d0410f2014-07-30 10:07:40 -0700504 mPendingConnections.clear();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700505 mOutgoingConnectionServiceRpc.asBinder().unlinkToDeath(mDeathRecipient, 0);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700506 }
507 };
508
Ihab Awadb8e85c72014-08-23 20:34:57 -0700509 private final IConnectionService mOutgoingConnectionServiceRpc;
510 private final ConnectionService mOurConnectionServiceImpl;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700511 private final Map<String, RemoteConnection> mConnectionById = new HashMap<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700512 private final Map<String, RemoteConference> mConferenceById = new HashMap<>();
Ihab Awad5d0410f2014-07-30 10:07:40 -0700513 private final Set<RemoteConnection> mPendingConnections = new HashSet<>();
514
Ihab Awadb8e85c72014-08-23 20:34:57 -0700515 RemoteConnectionService(
516 IConnectionService outgoingConnectionServiceRpc,
517 ConnectionService ourConnectionServiceImpl) throws RemoteException {
518 mOutgoingConnectionServiceRpc = outgoingConnectionServiceRpc;
519 mOutgoingConnectionServiceRpc.asBinder().linkToDeath(mDeathRecipient, 0);
520 mOurConnectionServiceImpl = ourConnectionServiceImpl;
Santos Cordon52d8a152014-06-17 19:08:45 -0700521 }
522
523 @Override
524 public String toString() {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700525 return "[RemoteCS - " + mOutgoingConnectionServiceRpc.asBinder().toString() + "]";
Santos Cordon52d8a152014-06-17 19:08:45 -0700526 }
527
Ihab Awadf8b69882014-07-25 15:14:01 -0700528 final RemoteConnection createRemoteConnection(
529 PhoneAccountHandle connectionManagerPhoneAccount,
530 ConnectionRequest request,
531 boolean isIncoming) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700532 final String id = UUID.randomUUID().toString();
Hall Liu95d55872017-01-25 17:12:49 -0800533 final ConnectionRequest newRequest = new ConnectionRequest.Builder()
534 .setAccountHandle(request.getAccountHandle())
535 .setAddress(request.getAddress())
536 .setExtras(request.getExtras())
537 .setVideoState(request.getVideoState())
538 .setRttPipeFromInCall(request.getRttPipeFromInCall())
539 .setRttPipeToInCall(request.getRttPipeToInCall())
540 .build();
Ihab Awad5d0410f2014-07-30 10:07:40 -0700541 try {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700542 if (mConnectionById.isEmpty()) {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700543 mOutgoingConnectionServiceRpc.addConnectionServiceAdapter(mServant.getStub(),
544 null /*Session.Info*/);
Ihab Awad8aecfed2014-08-08 17:06:11 -0700545 }
546 RemoteConnection connection =
Ihab Awadb8e85c72014-08-23 20:34:57 -0700547 new RemoteConnection(id, mOutgoingConnectionServiceRpc, newRequest);
Ihab Awad8aecfed2014-08-08 17:06:11 -0700548 mPendingConnections.add(connection);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700549 mConnectionById.put(id, connection);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700550 mOutgoingConnectionServiceRpc.createConnection(
Ihab Awad5d0410f2014-07-30 10:07:40 -0700551 connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700552 id,
Ihab Awad5d0410f2014-07-30 10:07:40 -0700553 newRequest,
Yorke Leec3cf9822014-10-02 09:38:39 -0700554 isIncoming,
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700555 false /* isUnknownCall */,
556 null /*Session.info*/);
Andrew Lee100e2932014-09-08 15:34:24 -0700557 connection.registerCallback(new RemoteConnection.Callback() {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700558 @Override
559 public void onDestroyed(RemoteConnection connection) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700560 mConnectionById.remove(id);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700561 maybeDisconnectAdapter();
Ihab Awad8aecfed2014-08-08 17:06:11 -0700562 }
563 });
Ihab Awad5d0410f2014-07-30 10:07:40 -0700564 return connection;
565 } catch (RemoteException e) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700566 return RemoteConnection.failure(
567 new DisconnectCause(DisconnectCause.ERROR, e.toString()));
Santos Cordon52d8a152014-06-17 19:08:45 -0700568 }
569 }
570
Ihab Awad50e35062014-09-30 09:17:03 -0700571 private boolean hasConnection(String callId) {
mike dooley879142b2014-10-08 13:39:28 -0700572 return mConnectionById.containsKey(callId);
Ihab Awad50e35062014-09-30 09:17:03 -0700573 }
574
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700575 private RemoteConnection findConnectionForAction(
576 String callId, String action) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700577 if (mConnectionById.containsKey(callId)) {
578 return mConnectionById.get(callId);
579 }
580 Log.w(this, "%s - Cannot find Connection %s", action, callId);
581 return NULL_CONNECTION;
Santos Cordon52d8a152014-06-17 19:08:45 -0700582 }
Ihab Awadb8e85c72014-08-23 20:34:57 -0700583
584 private RemoteConference findConferenceForAction(
585 String callId, String action) {
586 if (mConferenceById.containsKey(callId)) {
587 return mConferenceById.get(callId);
588 }
589 Log.w(this, "%s - Cannot find Conference %s", action, callId);
590 return NULL_CONFERENCE;
591 }
592
593 private void maybeDisconnectAdapter() {
594 if (mConnectionById.isEmpty() && mConferenceById.isEmpty()) {
595 try {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700596 mOutgoingConnectionServiceRpc.removeConnectionServiceAdapter(mServant.getStub(),
597 null /*Session.info*/);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700598 } catch (RemoteException e) {
599 }
600 }
601 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700602}