blob: de1dc1723fe492368a419eafa75eceff666cba45 [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;
Ihab Awad5d0410f2014-07-30 10:07:40 -070020import android.os.IBinder;
Santos Cordone8dc4be2014-07-21 01:28:28 -070021import android.os.IBinder.DeathRecipient;
Santos Cordon52d8a152014-06-17 19:08:45 -070022import android.os.RemoteException;
Santos Cordon52d8a152014-06-17 19:08:45 -070023
Tyler Gunnef9f6f92014-09-12 22:16:17 -070024import com.android.internal.telecom.IConnectionService;
25import com.android.internal.telecom.IConnectionServiceAdapter;
26import com.android.internal.telecom.IVideoProvider;
27import com.android.internal.telecom.RemoteServiceCallback;
Santos Cordon52d8a152014-06-17 19:08:45 -070028
Ihab Awadb8e85c72014-08-23 20:34:57 -070029import java.util.ArrayList;
Ihab Awad5d0410f2014-07-30 10:07:40 -070030import java.util.HashMap;
31import java.util.HashSet;
32import java.util.Map;
33import java.util.Set;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -070034import java.util.List;
Santos Cordon52d8a152014-06-17 19:08:45 -070035import java.util.UUID;
36
37/**
38 * Remote connection service which other connection services can use to place calls on their behalf.
Sailesh Nepal091768c2014-06-30 15:15:23 -070039 *
40 * @hide
Santos Cordon52d8a152014-06-17 19:08:45 -070041 */
Ihab Awad5d0410f2014-07-30 10:07:40 -070042final class RemoteConnectionService {
Sailesh Nepal48031592014-07-18 14:21:23 -070043
Ihab Awadb8e85c72014-08-23 20:34:57 -070044 private static final RemoteConnection NULL_CONNECTION =
45 new RemoteConnection("NULL", null, null);
46
47 private static final RemoteConference NULL_CONFERENCE =
48 new RemoteConference("NULL", null);
Santos Cordon52d8a152014-06-17 19:08:45 -070049
Ihab Awad5d0410f2014-07-30 10:07:40 -070050 private final IConnectionServiceAdapter mServantDelegate = new IConnectionServiceAdapter() {
Sailesh Nepal48031592014-07-18 14:21:23 -070051 @Override
Ihab Awad6107bab2014-08-18 09:23:25 -070052 public void handleCreateConnectionComplete(
Ihab Awadb19a0bc2014-08-07 19:46:01 -070053 String id,
54 ConnectionRequest request,
Ihab Awad5d0410f2014-07-30 10:07:40 -070055 ParcelableConnection parcel) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -070056 RemoteConnection connection =
57 findConnectionForAction(id, "handleCreateConnectionSuccessful");
Ihab Awad5d0410f2014-07-30 10:07:40 -070058 if (connection != NULL_CONNECTION && mPendingConnections.contains(connection)) {
59 mPendingConnections.remove(connection);
Ihab Awad6107bab2014-08-18 09:23:25 -070060 // Unconditionally initialize the connection ...
Ihab Awad5d0410f2014-07-30 10:07:40 -070061 connection.setCallCapabilities(parcel.getCapabilities());
Andrew Lee100e2932014-09-08 15:34:24 -070062 connection.setAddress(
Ihab Awad5d0410f2014-07-30 10:07:40 -070063 parcel.getHandle(), parcel.getHandlePresentation());
64 connection.setCallerDisplayName(
65 parcel.getCallerDisplayName(),
66 parcel.getCallerDisplayNamePresentation());
Sailesh Nepal70638f12014-09-09 21:49:14 -070067 // Set state after handle so that the client can identify the connection.
Sailesh Nepalc2a978d2014-09-20 18:23:05 -070068 if (parcel.getState() == Connection.STATE_DISCONNECTED) {
69 connection.setDisconnected(parcel.getDisconnectCause());
70 } else {
71 connection.setState(parcel.getState());
72 }
Ihab Awadb8e85c72014-08-23 20:34:57 -070073 List<RemoteConnection> conferenceable = new ArrayList<>();
74 for (String confId : parcel.getConferenceableConnectionIds()) {
75 if (mConnectionById.containsKey(confId)) {
76 conferenceable.add(mConnectionById.get(confId));
77 }
78 }
79 connection.setConferenceableConnections(conferenceable);
Ihab Awada64627c2014-08-20 09:36:40 -070080 connection.setVideoState(parcel.getVideoState());
Ihab Awad6107bab2014-08-18 09:23:25 -070081 if (connection.getState() == Connection.STATE_DISCONNECTED) {
82 // ... then, if it was created in a disconnected state, that indicates
83 // failure on the providing end, so immediately mark it destroyed
84 connection.setDestroyed();
85 }
Sailesh Nepal48031592014-07-18 14:21:23 -070086 }
87 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -070088
Sailesh Nepal2a46b902014-07-04 17:21:07 -070089 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -070090 public void setActive(String callId) {
Ihab Awadb8e85c72014-08-23 20:34:57 -070091 if (mConnectionById.containsKey(callId)) {
92 findConnectionForAction(callId, "setActive")
93 .setState(Connection.STATE_ACTIVE);
94 } else {
95 findConferenceForAction(callId, "setActive")
96 .setState(Connection.STATE_ACTIVE);
97 }
Santos Cordon52d8a152014-06-17 19:08:45 -070098 }
99
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700100 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700101 public void setRinging(String callId) {
102 findConnectionForAction(callId, "setRinging")
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700103 .setState(Connection.STATE_RINGING);
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700104 }
105
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700106 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700107 public void setDialing(String callId) {
108 findConnectionForAction(callId, "setDialing")
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700109 .setState(Connection.STATE_DIALING);
Santos Cordon52d8a152014-06-17 19:08:45 -0700110 }
111
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700112 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700113 public void setDisconnected(String callId, DisconnectCause disconnectCause) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700114 if (mConnectionById.containsKey(callId)) {
115 findConnectionForAction(callId, "setDisconnected")
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700116 .setDisconnected(disconnectCause);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700117 } else {
118 findConferenceForAction(callId, "setDisconnected")
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700119 .setDisconnected(disconnectCause);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700120 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700121 }
122
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700123 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700124 public void setOnHold(String callId) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700125 if (mConnectionById.containsKey(callId)) {
126 findConnectionForAction(callId, "setOnHold")
127 .setState(Connection.STATE_HOLDING);
128 } else {
129 findConferenceForAction(callId, "setOnHold")
130 .setState(Connection.STATE_HOLDING);
131 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700132 }
133
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700134 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700135 public void setRingbackRequested(String callId, boolean ringing) {
136 findConnectionForAction(callId, "setRingbackRequested")
137 .setRingbackRequested(ringing);
Santos Cordon52d8a152014-06-17 19:08:45 -0700138 }
139
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700140 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700141 public void setCallCapabilities(String callId, int callCapabilities) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700142 if (mConnectionById.containsKey(callId)) {
143 findConnectionForAction(callId, "setCallCapabilities")
144 .setCallCapabilities(callCapabilities);
145 } else {
146 findConferenceForAction(callId, "setCallCapabilities")
147 .setCallCapabilities(callCapabilities);
148 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700149 }
150
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700151 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700152 public void setIsConferenced(String callId, String conferenceCallId) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700153 // Note: callId should not be null; conferenceCallId may be null
154 RemoteConnection connection =
155 findConnectionForAction(callId, "setIsConferenced");
156 if (connection != NULL_CONNECTION) {
157 if (conferenceCallId == null) {
158 // 'connection' is being split from its conference
159 if (connection.getConference() != null) {
160 connection.getConference().removeConnection(connection);
161 }
162 } else {
163 RemoteConference conference =
164 findConferenceForAction(conferenceCallId, "setIsConferenced");
165 if (conference != NULL_CONFERENCE) {
166 conference.addConnection(connection);
167 }
168 }
169 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700170 }
171
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700172 @Override
Ihab Awadb8e85c72014-08-23 20:34:57 -0700173 public void addConferenceCall(
174 final String callId,
175 ParcelableConference parcel) {
176 RemoteConference conference = new RemoteConference(callId,
177 mOutgoingConnectionServiceRpc);
178
179 for (String id : parcel.getConnectionIds()) {
180 RemoteConnection c = mConnectionById.get(id);
181 if (c != null) {
182 conference.addConnection(c);
183 }
184 }
185
186 if (conference.getConnections().size() == 0) {
187 // A conference was created, but none of its connections are ones that have been
188 // created by, and therefore being tracked by, this remote connection service. It
189 // is of no interest to us.
190 return;
191 }
192
193 conference.setState(parcel.getState());
194 conference.setCallCapabilities(parcel.getCapabilities());
195 mConferenceById.put(callId, conference);
Andrew Lee100e2932014-09-08 15:34:24 -0700196 conference.registerCallback(new RemoteConference.Callback() {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700197 @Override
198 public void onDestroyed(RemoteConference c) {
199 mConferenceById.remove(callId);
200 maybeDisconnectAdapter();
201 }
202 });
203
204 mOurConnectionServiceImpl.addRemoteConference(conference);
Santos Cordon52d8a152014-06-17 19:08:45 -0700205 }
206
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700207 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700208 public void removeCall(String callId) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700209 if (mConnectionById.containsKey(callId)) {
210 findConnectionForAction(callId, "removeCall")
211 .setDestroyed();
212 } else {
213 findConferenceForAction(callId, "removeCall")
214 .setDestroyed();
215 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700216 }
217
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700218 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700219 public void onPostDialWait(String callId, String remaining) {
220 findConnectionForAction(callId, "onPostDialWait")
221 .setPostDialWait(remaining);
Santos Cordon52d8a152014-06-17 19:08:45 -0700222 }
223
Santos Cordon52d8a152014-06-17 19:08:45 -0700224 @Override
225 public void queryRemoteConnectionServices(RemoteServiceCallback callback) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700226 // Not supported from remote connection service.
Santos Cordon52d8a152014-06-17 19:08:45 -0700227 }
Tyler Gunn8d83fa92014-07-01 11:31:21 -0700228
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700229 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700230 public void setVideoProvider(String callId, IVideoProvider videoProvider) {
Ihab Awada64627c2014-08-20 09:36:40 -0700231 findConnectionForAction(callId, "setVideoProvider")
232 .setVideoProvider(new RemoteConnection.VideoProvider(videoProvider));
Tyler Gunnaa07df82014-07-17 07:50:22 -0700233 }
234
235 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700236 public void setVideoState(String callId, int videoState) {
237 findConnectionForAction(callId, "setVideoState")
238 .setVideoState(videoState);
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700239 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700240
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700241 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700242 public void setIsVoipAudioMode(String callId, boolean isVoip) {
243 findConnectionForAction(callId, "setIsVoipAudioMode")
244 .setIsVoipAudioMode(isVoip);
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700245 }
Sailesh Nepal61203862014-07-11 14:50:13 -0700246
247 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700248 public void setStatusHints(String callId, StatusHints statusHints) {
249 findConnectionForAction(callId, "setStatusHints")
250 .setStatusHints(statusHints);
Sailesh Nepal61203862014-07-11 14:50:13 -0700251 }
252
253 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700254 public void setAddress(String callId, Uri address, int presentation) {
255 findConnectionForAction(callId, "setAddress")
256 .setAddress(address, presentation);
Sailesh Nepal61203862014-07-11 14:50:13 -0700257 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700258
259 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700260 public void setCallerDisplayName(String callId, String callerDisplayName,
261 int presentation) {
262 findConnectionForAction(callId, "setCallerDisplayName")
263 .setCallerDisplayName(callerDisplayName, presentation);
264 }
265
266 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700267 public IBinder asBinder() {
268 throw new UnsupportedOperationException();
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700269 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700270
271 @Override
272 public final void setConferenceableConnections(
273 String callId, List<String> conferenceableConnectionIds) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700274 List<RemoteConnection> conferenceable = new ArrayList<>();
275 for (String id : conferenceableConnectionIds) {
276 if (mConnectionById.containsKey(id)) {
277 conferenceable.add(mConnectionById.get(id));
278 }
279 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700280
Ihab Awad50e35062014-09-30 09:17:03 -0700281 if (hasConnection(callId)) {
282 findConnectionForAction(callId, "setConferenceableConnections")
283 .setConferenceableConnections(conferenceable);
284 } else {
285 findConferenceForAction(callId, "setConferenceableConnections")
286 .setConferenceableConnections(conferenceable);
287 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700288 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700289 };
290
Ihab Awad5d0410f2014-07-30 10:07:40 -0700291 private final ConnectionServiceAdapterServant mServant =
292 new ConnectionServiceAdapterServant(mServantDelegate);
Santos Cordon52d8a152014-06-17 19:08:45 -0700293
Ihab Awad5d0410f2014-07-30 10:07:40 -0700294 private final DeathRecipient mDeathRecipient = new DeathRecipient() {
295 @Override
296 public void binderDied() {
297 for (RemoteConnection c : mConnectionById.values()) {
298 c.setDestroyed();
299 }
Ihab Awadb8e85c72014-08-23 20:34:57 -0700300 for (RemoteConference c : mConferenceById.values()) {
301 c.setDestroyed();
302 }
Ihab Awad5d0410f2014-07-30 10:07:40 -0700303 mConnectionById.clear();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700304 mConferenceById.clear();
Ihab Awad5d0410f2014-07-30 10:07:40 -0700305 mPendingConnections.clear();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700306 mOutgoingConnectionServiceRpc.asBinder().unlinkToDeath(mDeathRecipient, 0);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700307 }
308 };
309
Ihab Awadb8e85c72014-08-23 20:34:57 -0700310 private final IConnectionService mOutgoingConnectionServiceRpc;
311 private final ConnectionService mOurConnectionServiceImpl;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700312 private final Map<String, RemoteConnection> mConnectionById = new HashMap<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700313 private final Map<String, RemoteConference> mConferenceById = new HashMap<>();
Ihab Awad5d0410f2014-07-30 10:07:40 -0700314 private final Set<RemoteConnection> mPendingConnections = new HashSet<>();
315
Ihab Awadb8e85c72014-08-23 20:34:57 -0700316 RemoteConnectionService(
317 IConnectionService outgoingConnectionServiceRpc,
318 ConnectionService ourConnectionServiceImpl) throws RemoteException {
319 mOutgoingConnectionServiceRpc = outgoingConnectionServiceRpc;
320 mOutgoingConnectionServiceRpc.asBinder().linkToDeath(mDeathRecipient, 0);
321 mOurConnectionServiceImpl = ourConnectionServiceImpl;
Santos Cordon52d8a152014-06-17 19:08:45 -0700322 }
323
324 @Override
325 public String toString() {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700326 return "[RemoteCS - " + mOutgoingConnectionServiceRpc.asBinder().toString() + "]";
Santos Cordon52d8a152014-06-17 19:08:45 -0700327 }
328
Ihab Awadf8b69882014-07-25 15:14:01 -0700329 final RemoteConnection createRemoteConnection(
330 PhoneAccountHandle connectionManagerPhoneAccount,
331 ConnectionRequest request,
332 boolean isIncoming) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700333 final String id = UUID.randomUUID().toString();
Ihab Awad8aecfed2014-08-08 17:06:11 -0700334 final ConnectionRequest newRequest = new ConnectionRequest(
Ihab Awad5d0410f2014-07-30 10:07:40 -0700335 request.getAccountHandle(),
Nancy Chenea38cca2014-09-05 16:38:49 -0700336 request.getAddress(),
Ihab Awad5d0410f2014-07-30 10:07:40 -0700337 request.getExtras(),
338 request.getVideoState());
339 try {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700340 if (mConnectionById.isEmpty()) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700341 mOutgoingConnectionServiceRpc.addConnectionServiceAdapter(mServant.getStub());
Ihab Awad8aecfed2014-08-08 17:06:11 -0700342 }
343 RemoteConnection connection =
Ihab Awadb8e85c72014-08-23 20:34:57 -0700344 new RemoteConnection(id, mOutgoingConnectionServiceRpc, newRequest);
Ihab Awad8aecfed2014-08-08 17:06:11 -0700345 mPendingConnections.add(connection);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700346 mConnectionById.put(id, connection);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700347 mOutgoingConnectionServiceRpc.createConnection(
Ihab Awad5d0410f2014-07-30 10:07:40 -0700348 connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700349 id,
Ihab Awad5d0410f2014-07-30 10:07:40 -0700350 newRequest,
Yorke Leec3cf9822014-10-02 09:38:39 -0700351 isIncoming,
352 false /* isUnknownCall */);
Andrew Lee100e2932014-09-08 15:34:24 -0700353 connection.registerCallback(new RemoteConnection.Callback() {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700354 @Override
355 public void onDestroyed(RemoteConnection connection) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700356 mConnectionById.remove(id);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700357 maybeDisconnectAdapter();
Ihab Awad8aecfed2014-08-08 17:06:11 -0700358 }
359 });
Ihab Awad5d0410f2014-07-30 10:07:40 -0700360 return connection;
361 } catch (RemoteException e) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700362 return RemoteConnection.failure(
363 new DisconnectCause(DisconnectCause.ERROR, e.toString()));
Santos Cordon52d8a152014-06-17 19:08:45 -0700364 }
365 }
366
Ihab Awad50e35062014-09-30 09:17:03 -0700367 private boolean hasConnection(String callId) {
368 return mConferenceById.containsKey(callId);
369 }
370
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700371 private RemoteConnection findConnectionForAction(
372 String callId, String action) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700373 if (mConnectionById.containsKey(callId)) {
374 return mConnectionById.get(callId);
375 }
376 Log.w(this, "%s - Cannot find Connection %s", action, callId);
377 return NULL_CONNECTION;
Santos Cordon52d8a152014-06-17 19:08:45 -0700378 }
Ihab Awadb8e85c72014-08-23 20:34:57 -0700379
380 private RemoteConference findConferenceForAction(
381 String callId, String action) {
382 if (mConferenceById.containsKey(callId)) {
383 return mConferenceById.get(callId);
384 }
385 Log.w(this, "%s - Cannot find Conference %s", action, callId);
386 return NULL_CONFERENCE;
387 }
388
389 private void maybeDisconnectAdapter() {
390 if (mConnectionById.isEmpty() && mConferenceById.isEmpty()) {
391 try {
392 mOutgoingConnectionServiceRpc.removeConnectionServiceAdapter(mServant.getStub());
393 } catch (RemoteException e) {
394 }
395 }
396 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700397}