blob: bf6a6ef793ffcbca20a756fd33d7164088cbc173 [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;
Grace Jia9a09c672020-08-04 12:52:09 -070034import java.util.List;
Ihab Awad5d0410f2014-07-30 10:07:40 -070035import java.util.Map;
36import java.util.Set;
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);
Tyler Gunnc59fd0c2020-04-17 14:03:35 -0700261 // Track the fact this request was relayed through the remote connection service.
262 newExtras.putParcelable(Connection.EXTRA_REMOTE_PHONE_ACCOUNT_HANDLE,
263 parcel.getPhoneAccount());
Tyler Gunncd6ccfd2016-10-17 15:48:19 -0700264 conference.putExtras(newExtras);
265
Andrew Lee100e2932014-09-08 15:34:24 -0700266 conference.registerCallback(new RemoteConference.Callback() {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700267 @Override
268 public void onDestroyed(RemoteConference c) {
269 mConferenceById.remove(callId);
270 maybeDisconnectAdapter();
271 }
272 });
273
274 mOurConnectionServiceImpl.addRemoteConference(conference);
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 removeCall(String callId, Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700279 if (mConnectionById.containsKey(callId)) {
280 findConnectionForAction(callId, "removeCall")
281 .setDestroyed();
282 } else {
283 findConferenceForAction(callId, "removeCall")
284 .setDestroyed();
285 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700286 }
287
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700288 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700289 public void onPostDialWait(String callId, String remaining, Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700290 findConnectionForAction(callId, "onPostDialWait")
291 .setPostDialWait(remaining);
Santos Cordon52d8a152014-06-17 19:08:45 -0700292 }
293
Santos Cordon52d8a152014-06-17 19:08:45 -0700294 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700295 public void onPostDialChar(String callId, char nextChar, Session.Info sessionInfo) {
Nancy Chen27d1c2d2014-12-15 16:12:50 -0800296 findConnectionForAction(callId, "onPostDialChar")
297 .onPostDialChar(nextChar);
298 }
299
300 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700301 public void queryRemoteConnectionServices(RemoteServiceCallback callback,
Tyler Gunn4c69fb32019-05-17 10:49:16 -0700302 String callingPackage, Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700303 // Not supported from remote connection service.
Santos Cordon52d8a152014-06-17 19:08:45 -0700304 }
Tyler Gunn8d83fa92014-07-01 11:31:21 -0700305
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700306 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700307 public void setVideoProvider(String callId, IVideoProvider videoProvider,
308 Session.Info sessionInfo) {
Tyler Gunnbf9c6fd2016-11-09 10:19:23 -0800309
310 String callingPackage = mOurConnectionServiceImpl.getApplicationContext()
311 .getOpPackageName();
Tyler Gunn159f35c2017-03-02 09:28:37 -0800312 int targetSdkVersion = mOurConnectionServiceImpl.getApplicationInfo().targetSdkVersion;
Sailesh Nepal11aeae52015-01-28 16:54:09 -0800313 RemoteConnection.VideoProvider remoteVideoProvider = null;
314 if (videoProvider != null) {
Tyler Gunnbf9c6fd2016-11-09 10:19:23 -0800315 remoteVideoProvider = new RemoteConnection.VideoProvider(videoProvider,
Tyler Gunn159f35c2017-03-02 09:28:37 -0800316 callingPackage, targetSdkVersion);
Sailesh Nepal11aeae52015-01-28 16:54:09 -0800317 }
Ihab Awada64627c2014-08-20 09:36:40 -0700318 findConnectionForAction(callId, "setVideoProvider")
Sailesh Nepal11aeae52015-01-28 16:54:09 -0800319 .setVideoProvider(remoteVideoProvider);
Tyler Gunnaa07df82014-07-17 07:50:22 -0700320 }
321
322 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700323 public void setVideoState(String callId, int videoState, Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700324 findConnectionForAction(callId, "setVideoState")
325 .setVideoState(videoState);
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700326 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700327
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700328 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700329 public void setIsVoipAudioMode(String callId, boolean isVoip, Session.Info sessionInfo) {
Andrew Lee100e2932014-09-08 15:34:24 -0700330 findConnectionForAction(callId, "setIsVoipAudioMode")
331 .setIsVoipAudioMode(isVoip);
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700332 }
Sailesh Nepal61203862014-07-11 14:50:13 -0700333
334 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700335 public void setStatusHints(String callId, StatusHints statusHints,
336 Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700337 findConnectionForAction(callId, "setStatusHints")
338 .setStatusHints(statusHints);
Sailesh Nepal61203862014-07-11 14:50:13 -0700339 }
340
341 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700342 public void setAddress(String callId, Uri address, int presentation,
343 Session.Info sessionInfo) {
Andrew Lee100e2932014-09-08 15:34:24 -0700344 findConnectionForAction(callId, "setAddress")
345 .setAddress(address, presentation);
Sailesh Nepal61203862014-07-11 14:50:13 -0700346 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700347
348 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700349 public void setCallerDisplayName(String callId, String callerDisplayName,
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700350 int presentation, Session.Info sessionInfo) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700351 findConnectionForAction(callId, "setCallerDisplayName")
352 .setCallerDisplayName(callerDisplayName, presentation);
353 }
354
355 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700356 public IBinder asBinder() {
357 throw new UnsupportedOperationException();
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700358 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700359
360 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700361 public final void setConferenceableConnections(String callId,
362 List<String> conferenceableConnectionIds, Session.Info sessionInfo) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700363 List<RemoteConnection> conferenceable = new ArrayList<>();
364 for (String id : conferenceableConnectionIds) {
365 if (mConnectionById.containsKey(id)) {
366 conferenceable.add(mConnectionById.get(id));
367 }
368 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700369
Ihab Awad50e35062014-09-30 09:17:03 -0700370 if (hasConnection(callId)) {
371 findConnectionForAction(callId, "setConferenceableConnections")
372 .setConferenceableConnections(conferenceable);
373 } else {
374 findConferenceForAction(callId, "setConferenceableConnections")
375 .setConferenceableConnections(conferenceable);
376 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700377 }
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700378
379 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700380 public void addExistingConnection(String callId, ParcelableConnection connection,
381 Session.Info sessionInfo) {
Tyler Gunnbf9c6fd2016-11-09 10:19:23 -0800382 String callingPackage = mOurConnectionServiceImpl.getApplicationContext().
383 getOpPackageName();
Tyler Gunn159f35c2017-03-02 09:28:37 -0800384 int callingTargetSdkVersion = mOurConnectionServiceImpl.getApplicationInfo()
385 .targetSdkVersion;
Tyler Gunncd6ccfd2016-10-17 15:48:19 -0700386 RemoteConnection remoteConnection = new RemoteConnection(callId,
Tyler Gunn159f35c2017-03-02 09:28:37 -0800387 mOutgoingConnectionServiceRpc, connection, callingPackage,
388 callingTargetSdkVersion);
Tyler Gunnc59fd0c2020-04-17 14:03:35 -0700389 // Track that it is via a remote connection.
390 Bundle newExtras = new Bundle();
391 newExtras.putParcelable(Connection.EXTRA_REMOTE_PHONE_ACCOUNT_HANDLE,
392 connection.getPhoneAccount());
393 remoteConnection.putExtras(newExtras);
Tyler Gunncd6ccfd2016-10-17 15:48:19 -0700394 mConnectionById.put(callId, remoteConnection);
395 remoteConnection.registerCallback(new RemoteConnection.Callback() {
396 @Override
397 public void onDestroyed(RemoteConnection connection) {
398 mConnectionById.remove(callId);
399 maybeDisconnectAdapter();
400 }
401 });
402 mOurConnectionServiceImpl.addRemoteExistingConnection(remoteConnection);
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700403 }
Santos Cordon6b7f9552015-05-27 17:21:45 -0700404
405 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700406 public void putExtras(String callId, Bundle extras, Session.Info sessionInfo) {
Tyler Gunndee56a82016-03-23 16:06:34 -0700407 if (hasConnection(callId)) {
408 findConnectionForAction(callId, "putExtras").putExtras(extras);
Santos Cordon6b7f9552015-05-27 17:21:45 -0700409 } else {
Tyler Gunndee56a82016-03-23 16:06:34 -0700410 findConferenceForAction(callId, "putExtras").putExtras(extras);
411 }
412 }
413
414 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700415 public void removeExtras(String callId, List<String> keys, Session.Info sessionInfo) {
Tyler Gunndee56a82016-03-23 16:06:34 -0700416 if (hasConnection(callId)) {
417 findConnectionForAction(callId, "removeExtra").removeExtras(keys);
418 } else {
419 findConferenceForAction(callId, "removeExtra").removeExtras(keys);
Santos Cordon6b7f9552015-05-27 17:21:45 -0700420 }
421 }
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -0800422
423 @Override
Hall Liua98f58b52017-11-07 17:59:28 -0800424 public void setAudioRoute(String callId, int audioRoute, String bluetoothAddress,
425 Session.Info sessionInfo) {
Tyler Gunnf5035432017-01-09 09:43:12 -0800426 if (hasConnection(callId)) {
427 // TODO(3pcalls): handle this for remote connections.
428 // Likely we don't want to do anything since it doesn't make sense for self-managed
429 // connections to go through a connection mgr.
430 }
431 }
432
433 @Override
Brad Ebinger4d75bee2016-10-28 12:29:55 -0700434 public void onConnectionEvent(String callId, String event, Bundle extras,
435 Session.Info sessionInfo) {
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -0800436 if (mConnectionById.containsKey(callId)) {
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700437 findConnectionForAction(callId, "onConnectionEvent").onConnectionEvent(event,
438 extras);
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -0800439 }
440 }
Hall Liu57006aa2017-02-06 10:49:48 -0800441
442 @Override
443 public void onRttInitiationSuccess(String callId, Session.Info sessionInfo)
444 throws RemoteException {
445 if (hasConnection(callId)) {
446 findConnectionForAction(callId, "onRttInitiationSuccess")
447 .onRttInitiationSuccess();
448 } else {
449 Log.w(this, "onRttInitiationSuccess called on a remote conference");
450 }
451 }
452
453 @Override
454 public void onRttInitiationFailure(String callId, int reason, Session.Info sessionInfo)
455 throws RemoteException {
456 if (hasConnection(callId)) {
457 findConnectionForAction(callId, "onRttInitiationFailure")
458 .onRttInitiationFailure(reason);
459 } else {
460 Log.w(this, "onRttInitiationFailure called on a remote conference");
461 }
462 }
463
464 @Override
465 public void onRttSessionRemotelyTerminated(String callId, Session.Info sessionInfo)
466 throws RemoteException {
467 if (hasConnection(callId)) {
468 findConnectionForAction(callId, "onRttSessionRemotelyTerminated")
469 .onRttSessionRemotelyTerminated();
470 } else {
471 Log.w(this, "onRttSessionRemotelyTerminated called on a remote conference");
472 }
473 }
474
475 @Override
476 public void onRemoteRttRequest(String callId, Session.Info sessionInfo)
477 throws RemoteException {
478 if (hasConnection(callId)) {
479 findConnectionForAction(callId, "onRemoteRttRequest")
480 .onRemoteRttRequest();
481 } else {
482 Log.w(this, "onRemoteRttRequest called on a remote conference");
483 }
484 }
Mengjun Leng25707742017-07-04 11:10:37 +0800485
486 @Override
487 public void resetConnectionTime(String callId, Session.Info sessionInfo) {
488 // Do nothing
489 }
Tyler Gunn68a73a42018-10-03 15:38:57 -0700490
491 @Override
492 public void setConferenceState(String callId, boolean isConference,
493 Session.Info sessionInfo) {
494 // Do nothing
495 }
Brad Ebingere0c12f42020-04-08 16:25:12 -0700496
497 @Override
498 public void setCallDirection(String callId, int direction, Session.Info sessionInfo) {
499 // Do nothing
500 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700501 };
502
Ihab Awad5d0410f2014-07-30 10:07:40 -0700503 private final ConnectionServiceAdapterServant mServant =
504 new ConnectionServiceAdapterServant(mServantDelegate);
Santos Cordon52d8a152014-06-17 19:08:45 -0700505
Ihab Awad5d0410f2014-07-30 10:07:40 -0700506 private final DeathRecipient mDeathRecipient = new DeathRecipient() {
507 @Override
508 public void binderDied() {
509 for (RemoteConnection c : mConnectionById.values()) {
510 c.setDestroyed();
511 }
Ihab Awadb8e85c72014-08-23 20:34:57 -0700512 for (RemoteConference c : mConferenceById.values()) {
513 c.setDestroyed();
514 }
Ihab Awad5d0410f2014-07-30 10:07:40 -0700515 mConnectionById.clear();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700516 mConferenceById.clear();
Ihab Awad5d0410f2014-07-30 10:07:40 -0700517 mPendingConnections.clear();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700518 mOutgoingConnectionServiceRpc.asBinder().unlinkToDeath(mDeathRecipient, 0);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700519 }
520 };
521
Ihab Awadb8e85c72014-08-23 20:34:57 -0700522 private final IConnectionService mOutgoingConnectionServiceRpc;
523 private final ConnectionService mOurConnectionServiceImpl;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700524 private final Map<String, RemoteConnection> mConnectionById = new HashMap<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700525 private final Map<String, RemoteConference> mConferenceById = new HashMap<>();
Ihab Awad5d0410f2014-07-30 10:07:40 -0700526 private final Set<RemoteConnection> mPendingConnections = new HashSet<>();
527
Ihab Awadb8e85c72014-08-23 20:34:57 -0700528 RemoteConnectionService(
529 IConnectionService outgoingConnectionServiceRpc,
530 ConnectionService ourConnectionServiceImpl) throws RemoteException {
531 mOutgoingConnectionServiceRpc = outgoingConnectionServiceRpc;
532 mOutgoingConnectionServiceRpc.asBinder().linkToDeath(mDeathRecipient, 0);
533 mOurConnectionServiceImpl = ourConnectionServiceImpl;
Santos Cordon52d8a152014-06-17 19:08:45 -0700534 }
535
536 @Override
537 public String toString() {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700538 return "[RemoteCS - " + mOutgoingConnectionServiceRpc.asBinder().toString() + "]";
Santos Cordon52d8a152014-06-17 19:08:45 -0700539 }
540
Ihab Awadf8b69882014-07-25 15:14:01 -0700541 final RemoteConnection createRemoteConnection(
542 PhoneAccountHandle connectionManagerPhoneAccount,
543 ConnectionRequest request,
544 boolean isIncoming) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700545 final String id = UUID.randomUUID().toString();
Tyler Gunnc59fd0c2020-04-17 14:03:35 -0700546 Bundle extras = new Bundle();
547 if (request.getExtras() != null) {
548 extras.putAll(request.getExtras());
549 }
550 // We will set the package name for the originator of the remote request; this lets the
551 // receiving ConnectionService know that the request originated from a remote connection
552 // service so that it can provide tracking information for Telecom.
553 extras.putString(Connection.EXTRA_REMOTE_CONNECTION_ORIGINATING_PACKAGE_NAME,
554 mOurConnectionServiceImpl.getApplicationContext().getOpPackageName());
555
Hall Liu95d55872017-01-25 17:12:49 -0800556 final ConnectionRequest newRequest = new ConnectionRequest.Builder()
557 .setAccountHandle(request.getAccountHandle())
558 .setAddress(request.getAddress())
Tyler Gunnc59fd0c2020-04-17 14:03:35 -0700559 .setExtras(extras)
Hall Liu95d55872017-01-25 17:12:49 -0800560 .setVideoState(request.getVideoState())
561 .setRttPipeFromInCall(request.getRttPipeFromInCall())
562 .setRttPipeToInCall(request.getRttPipeToInCall())
563 .build();
Ihab Awad5d0410f2014-07-30 10:07:40 -0700564 try {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700565 if (mConnectionById.isEmpty()) {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700566 mOutgoingConnectionServiceRpc.addConnectionServiceAdapter(mServant.getStub(),
567 null /*Session.Info*/);
Ihab Awad8aecfed2014-08-08 17:06:11 -0700568 }
569 RemoteConnection connection =
Ihab Awadb8e85c72014-08-23 20:34:57 -0700570 new RemoteConnection(id, mOutgoingConnectionServiceRpc, newRequest);
Ihab Awad8aecfed2014-08-08 17:06:11 -0700571 mPendingConnections.add(connection);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700572 mConnectionById.put(id, connection);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700573 mOutgoingConnectionServiceRpc.createConnection(
Ihab Awad5d0410f2014-07-30 10:07:40 -0700574 connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700575 id,
Ihab Awad5d0410f2014-07-30 10:07:40 -0700576 newRequest,
Yorke Leec3cf9822014-10-02 09:38:39 -0700577 isIncoming,
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700578 false /* isUnknownCall */,
579 null /*Session.info*/);
Andrew Lee100e2932014-09-08 15:34:24 -0700580 connection.registerCallback(new RemoteConnection.Callback() {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700581 @Override
582 public void onDestroyed(RemoteConnection connection) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700583 mConnectionById.remove(id);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700584 maybeDisconnectAdapter();
Ihab Awad8aecfed2014-08-08 17:06:11 -0700585 }
586 });
Ihab Awad5d0410f2014-07-30 10:07:40 -0700587 return connection;
588 } catch (RemoteException e) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700589 return RemoteConnection.failure(
590 new DisconnectCause(DisconnectCause.ERROR, e.toString()));
Santos Cordon52d8a152014-06-17 19:08:45 -0700591 }
592 }
593
Grace Jia9a09c672020-08-04 12:52:09 -0700594 RemoteConference createRemoteConference(
595 PhoneAccountHandle connectionManagerPhoneAccount,
596 ConnectionRequest request,
597 boolean isIncoming) {
598 final String id = UUID.randomUUID().toString();
599 try {
600 if (mConferenceById.isEmpty()) {
601 mOutgoingConnectionServiceRpc.addConnectionServiceAdapter(mServant.getStub(),
602 null /*Session.Info*/);
603 }
604 RemoteConference conference = new RemoteConference(id, mOutgoingConnectionServiceRpc);
605 mOutgoingConnectionServiceRpc.createConference(connectionManagerPhoneAccount,
606 id,
607 request,
608 isIncoming,
609 false /* isUnknownCall */,
610 null /*Session.info*/);
611 conference.registerCallback(new RemoteConference.Callback() {
612 @Override
613 public void onDestroyed(RemoteConference conference) {
614 mConferenceById.remove(id);
615 maybeDisconnectAdapter();
616 }
617 });
618 conference.putExtras(request.getExtras());
619 return conference;
620 } catch (RemoteException e) {
621 return RemoteConference.failure(
622 new DisconnectCause(DisconnectCause.ERROR, e.toString()));
623 }
624 }
625
Ihab Awad50e35062014-09-30 09:17:03 -0700626 private boolean hasConnection(String callId) {
mike dooley879142b2014-10-08 13:39:28 -0700627 return mConnectionById.containsKey(callId);
Ihab Awad50e35062014-09-30 09:17:03 -0700628 }
629
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700630 private RemoteConnection findConnectionForAction(
631 String callId, String action) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700632 if (mConnectionById.containsKey(callId)) {
633 return mConnectionById.get(callId);
634 }
635 Log.w(this, "%s - Cannot find Connection %s", action, callId);
636 return NULL_CONNECTION;
Santos Cordon52d8a152014-06-17 19:08:45 -0700637 }
Ihab Awadb8e85c72014-08-23 20:34:57 -0700638
639 private RemoteConference findConferenceForAction(
640 String callId, String action) {
641 if (mConferenceById.containsKey(callId)) {
642 return mConferenceById.get(callId);
643 }
644 Log.w(this, "%s - Cannot find Conference %s", action, callId);
645 return NULL_CONFERENCE;
646 }
647
648 private void maybeDisconnectAdapter() {
649 if (mConnectionById.isEmpty() && mConferenceById.isEmpty()) {
650 try {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700651 mOutgoingConnectionServiceRpc.removeConnectionServiceAdapter(mServant.getStub(),
652 null /*Session.info*/);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700653 } catch (RemoteException e) {
654 }
655 }
656 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700657}