blob: 21a7706e5f55c1bca3c1fbc7def1b51894136471 [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;
Santos Cordon52d8a152014-06-17 19:08:45 -070024
Tyler Gunnef9f6f92014-09-12 22:16:17 -070025import com.android.internal.telecom.IConnectionService;
26import com.android.internal.telecom.IConnectionServiceAdapter;
27import com.android.internal.telecom.IVideoProvider;
28import com.android.internal.telecom.RemoteServiceCallback;
Santos Cordon52d8a152014-06-17 19:08:45 -070029
Ihab Awadb8e85c72014-08-23 20:34:57 -070030import java.util.ArrayList;
Ihab Awad5d0410f2014-07-30 10:07:40 -070031import java.util.HashMap;
32import java.util.HashSet;
33import java.util.Map;
34import java.util.Set;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -070035import java.util.List;
Santos Cordon52d8a152014-06-17 19:08:45 -070036import java.util.UUID;
37
38/**
39 * Remote connection service which other connection services can use to place calls on their behalf.
Sailesh Nepal091768c2014-06-30 15:15:23 -070040 *
41 * @hide
Santos Cordon52d8a152014-06-17 19:08:45 -070042 */
Ihab Awad5d0410f2014-07-30 10:07:40 -070043final class RemoteConnectionService {
Sailesh Nepal48031592014-07-18 14:21:23 -070044
Tyler Gunn4a57b9b2014-10-30 14:27:48 -070045 // Note: Casting null to avoid ambiguous constructor reference.
Ihab Awadb8e85c72014-08-23 20:34:57 -070046 private static final RemoteConnection NULL_CONNECTION =
Tyler Gunn4a57b9b2014-10-30 14:27:48 -070047 new RemoteConnection("NULL", null, (ConnectionRequest) null);
Ihab Awadb8e85c72014-08-23 20:34:57 -070048
49 private static final RemoteConference NULL_CONFERENCE =
50 new RemoteConference("NULL", null);
Santos Cordon52d8a152014-06-17 19:08:45 -070051
Ihab Awad5d0410f2014-07-30 10:07:40 -070052 private final IConnectionServiceAdapter mServantDelegate = new IConnectionServiceAdapter() {
Sailesh Nepal48031592014-07-18 14:21:23 -070053 @Override
Ihab Awad6107bab2014-08-18 09:23:25 -070054 public void handleCreateConnectionComplete(
Ihab Awadb19a0bc2014-08-07 19:46:01 -070055 String id,
56 ConnectionRequest request,
Ihab Awad5d0410f2014-07-30 10:07:40 -070057 ParcelableConnection parcel) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -070058 RemoteConnection connection =
59 findConnectionForAction(id, "handleCreateConnectionSuccessful");
Ihab Awad5d0410f2014-07-30 10:07:40 -070060 if (connection != NULL_CONNECTION && mPendingConnections.contains(connection)) {
61 mPendingConnections.remove(connection);
Ihab Awad6107bab2014-08-18 09:23:25 -070062 // Unconditionally initialize the connection ...
Ihab Awad5c9c86e2014-11-12 13:41:16 -080063 connection.setConnectionCapabilities(parcel.getConnectionCapabilities());
Tyler Gunn720c6642016-03-22 09:02:47 -070064 connection.setConnectionProperties(parcel.getConnectionProperties());
Sailesh Nepal2d3ced72015-01-31 20:17:35 -080065 if (parcel.getHandle() != null
66 || parcel.getState() != Connection.STATE_DISCONNECTED) {
67 connection.setAddress(parcel.getHandle(), parcel.getHandlePresentation());
68 }
69 if (parcel.getCallerDisplayName() != null
70 || parcel.getState() != Connection.STATE_DISCONNECTED) {
71 connection.setCallerDisplayName(
72 parcel.getCallerDisplayName(),
73 parcel.getCallerDisplayNamePresentation());
74 }
Sailesh Nepal70638f12014-09-09 21:49:14 -070075 // Set state after handle so that the client can identify the connection.
Sailesh Nepalc2a978d2014-09-20 18:23:05 -070076 if (parcel.getState() == Connection.STATE_DISCONNECTED) {
77 connection.setDisconnected(parcel.getDisconnectCause());
78 } else {
79 connection.setState(parcel.getState());
80 }
Ihab Awadb8e85c72014-08-23 20:34:57 -070081 List<RemoteConnection> conferenceable = new ArrayList<>();
82 for (String confId : parcel.getConferenceableConnectionIds()) {
83 if (mConnectionById.containsKey(confId)) {
84 conferenceable.add(mConnectionById.get(confId));
85 }
86 }
87 connection.setConferenceableConnections(conferenceable);
Ihab Awada64627c2014-08-20 09:36:40 -070088 connection.setVideoState(parcel.getVideoState());
Ihab Awad6107bab2014-08-18 09:23:25 -070089 if (connection.getState() == Connection.STATE_DISCONNECTED) {
90 // ... then, if it was created in a disconnected state, that indicates
91 // failure on the providing end, so immediately mark it destroyed
92 connection.setDestroyed();
93 }
Sailesh Nepal48031592014-07-18 14:21:23 -070094 }
95 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -070096
Sailesh Nepal2a46b902014-07-04 17:21:07 -070097 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -070098 public void setActive(String callId) {
Ihab Awadb8e85c72014-08-23 20:34:57 -070099 if (mConnectionById.containsKey(callId)) {
100 findConnectionForAction(callId, "setActive")
101 .setState(Connection.STATE_ACTIVE);
102 } else {
103 findConferenceForAction(callId, "setActive")
104 .setState(Connection.STATE_ACTIVE);
105 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700106 }
107
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700108 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700109 public void setRinging(String callId) {
110 findConnectionForAction(callId, "setRinging")
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700111 .setState(Connection.STATE_RINGING);
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700112 }
113
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700114 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700115 public void setDialing(String callId) {
116 findConnectionForAction(callId, "setDialing")
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700117 .setState(Connection.STATE_DIALING);
Santos Cordon52d8a152014-06-17 19:08:45 -0700118 }
119
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700120 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700121 public void setDisconnected(String callId, DisconnectCause disconnectCause) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700122 if (mConnectionById.containsKey(callId)) {
123 findConnectionForAction(callId, "setDisconnected")
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700124 .setDisconnected(disconnectCause);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700125 } else {
126 findConferenceForAction(callId, "setDisconnected")
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700127 .setDisconnected(disconnectCause);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700128 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700129 }
130
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700131 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700132 public void setOnHold(String callId) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700133 if (mConnectionById.containsKey(callId)) {
134 findConnectionForAction(callId, "setOnHold")
135 .setState(Connection.STATE_HOLDING);
136 } else {
137 findConferenceForAction(callId, "setOnHold")
138 .setState(Connection.STATE_HOLDING);
139 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700140 }
141
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700142 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700143 public void setRingbackRequested(String callId, boolean ringing) {
144 findConnectionForAction(callId, "setRingbackRequested")
145 .setRingbackRequested(ringing);
Santos Cordon52d8a152014-06-17 19:08:45 -0700146 }
147
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700148 @Override
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800149 public void setConnectionCapabilities(String callId, int connectionCapabilities) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700150 if (mConnectionById.containsKey(callId)) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800151 findConnectionForAction(callId, "setConnectionCapabilities")
152 .setConnectionCapabilities(connectionCapabilities);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700153 } else {
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800154 findConferenceForAction(callId, "setConnectionCapabilities")
155 .setConnectionCapabilities(connectionCapabilities);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700156 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700157 }
158
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700159 @Override
Tyler Gunn720c6642016-03-22 09:02:47 -0700160 public void setConnectionProperties(String callId, int connectionProperties) {
161 if (mConnectionById.containsKey(callId)) {
162 findConnectionForAction(callId, "setConnectionProperties")
163 .setConnectionProperties(connectionProperties);
164 } else {
165 findConferenceForAction(callId, "setConnectionProperties")
166 .setConnectionProperties(connectionProperties);
167 }
168 }
169
170 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700171 public void setIsConferenced(String callId, String conferenceCallId) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700172 // Note: callId should not be null; conferenceCallId may be null
173 RemoteConnection connection =
174 findConnectionForAction(callId, "setIsConferenced");
175 if (connection != NULL_CONNECTION) {
176 if (conferenceCallId == null) {
177 // 'connection' is being split from its conference
178 if (connection.getConference() != null) {
179 connection.getConference().removeConnection(connection);
180 }
181 } else {
182 RemoteConference conference =
183 findConferenceForAction(conferenceCallId, "setIsConferenced");
184 if (conference != NULL_CONFERENCE) {
185 conference.addConnection(connection);
186 }
187 }
188 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700189 }
190
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700191 @Override
Anthony Lee17455a32015-04-24 15:25:29 -0700192 public void setConferenceMergeFailed(String callId) {
193 // Nothing to do here.
194 // The event has already been handled and there is no state to update
195 // in the underlying connection or conference objects
196 }
197
198 @Override
Ihab Awadb8e85c72014-08-23 20:34:57 -0700199 public void addConferenceCall(
200 final String callId,
201 ParcelableConference parcel) {
202 RemoteConference conference = new RemoteConference(callId,
203 mOutgoingConnectionServiceRpc);
204
205 for (String id : parcel.getConnectionIds()) {
206 RemoteConnection c = mConnectionById.get(id);
207 if (c != null) {
208 conference.addConnection(c);
209 }
210 }
211
212 if (conference.getConnections().size() == 0) {
213 // A conference was created, but none of its connections are ones that have been
214 // created by, and therefore being tracked by, this remote connection service. It
215 // is of no interest to us.
216 return;
217 }
218
219 conference.setState(parcel.getState());
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800220 conference.setConnectionCapabilities(parcel.getConnectionCapabilities());
Ihab Awadb8e85c72014-08-23 20:34:57 -0700221 mConferenceById.put(callId, conference);
Andrew Lee100e2932014-09-08 15:34:24 -0700222 conference.registerCallback(new RemoteConference.Callback() {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700223 @Override
224 public void onDestroyed(RemoteConference c) {
225 mConferenceById.remove(callId);
226 maybeDisconnectAdapter();
227 }
228 });
229
230 mOurConnectionServiceImpl.addRemoteConference(conference);
Santos Cordon52d8a152014-06-17 19:08:45 -0700231 }
232
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700233 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700234 public void removeCall(String callId) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700235 if (mConnectionById.containsKey(callId)) {
236 findConnectionForAction(callId, "removeCall")
237 .setDestroyed();
238 } else {
239 findConferenceForAction(callId, "removeCall")
240 .setDestroyed();
241 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700242 }
243
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700244 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700245 public void onPostDialWait(String callId, String remaining) {
246 findConnectionForAction(callId, "onPostDialWait")
247 .setPostDialWait(remaining);
Santos Cordon52d8a152014-06-17 19:08:45 -0700248 }
249
Santos Cordon52d8a152014-06-17 19:08:45 -0700250 @Override
Nancy Chen27d1c2d2014-12-15 16:12:50 -0800251 public void onPostDialChar(String callId, char nextChar) {
252 findConnectionForAction(callId, "onPostDialChar")
253 .onPostDialChar(nextChar);
254 }
255
256 @Override
Santos Cordon52d8a152014-06-17 19:08:45 -0700257 public void queryRemoteConnectionServices(RemoteServiceCallback callback) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700258 // Not supported from remote connection service.
Santos Cordon52d8a152014-06-17 19:08:45 -0700259 }
Tyler Gunn8d83fa92014-07-01 11:31:21 -0700260
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700261 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700262 public void setVideoProvider(String callId, IVideoProvider videoProvider) {
Sailesh Nepal11aeae52015-01-28 16:54:09 -0800263 RemoteConnection.VideoProvider remoteVideoProvider = null;
264 if (videoProvider != null) {
265 remoteVideoProvider = new RemoteConnection.VideoProvider(videoProvider);
266 }
Ihab Awada64627c2014-08-20 09:36:40 -0700267 findConnectionForAction(callId, "setVideoProvider")
Sailesh Nepal11aeae52015-01-28 16:54:09 -0800268 .setVideoProvider(remoteVideoProvider);
Tyler Gunnaa07df82014-07-17 07:50:22 -0700269 }
270
271 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700272 public void setVideoState(String callId, int videoState) {
273 findConnectionForAction(callId, "setVideoState")
274 .setVideoState(videoState);
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700275 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700276
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700277 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700278 public void setIsVoipAudioMode(String callId, boolean isVoip) {
279 findConnectionForAction(callId, "setIsVoipAudioMode")
280 .setIsVoipAudioMode(isVoip);
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700281 }
Sailesh Nepal61203862014-07-11 14:50:13 -0700282
283 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700284 public void setStatusHints(String callId, StatusHints statusHints) {
285 findConnectionForAction(callId, "setStatusHints")
286 .setStatusHints(statusHints);
Sailesh Nepal61203862014-07-11 14:50:13 -0700287 }
288
289 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700290 public void setAddress(String callId, Uri address, int presentation) {
291 findConnectionForAction(callId, "setAddress")
292 .setAddress(address, presentation);
Sailesh Nepal61203862014-07-11 14:50:13 -0700293 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700294
295 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700296 public void setCallerDisplayName(String callId, String callerDisplayName,
297 int presentation) {
298 findConnectionForAction(callId, "setCallerDisplayName")
299 .setCallerDisplayName(callerDisplayName, presentation);
300 }
301
302 @Override
Ihab Awad5d0410f2014-07-30 10:07:40 -0700303 public IBinder asBinder() {
304 throw new UnsupportedOperationException();
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700305 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700306
307 @Override
308 public final void setConferenceableConnections(
309 String callId, List<String> conferenceableConnectionIds) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700310 List<RemoteConnection> conferenceable = new ArrayList<>();
311 for (String id : conferenceableConnectionIds) {
312 if (mConnectionById.containsKey(id)) {
313 conferenceable.add(mConnectionById.get(id));
314 }
315 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700316
Ihab Awad50e35062014-09-30 09:17:03 -0700317 if (hasConnection(callId)) {
318 findConnectionForAction(callId, "setConferenceableConnections")
319 .setConferenceableConnections(conferenceable);
320 } else {
321 findConferenceForAction(callId, "setConferenceableConnections")
322 .setConferenceableConnections(conferenceable);
323 }
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700324 }
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700325
326 @Override
327 public void addExistingConnection(String callId, ParcelableConnection connection) {
328 // TODO: add contents of this method
329 RemoteConnection remoteConnction = new RemoteConnection(callId,
330 mOutgoingConnectionServiceRpc, connection);
331
332 mOurConnectionServiceImpl.addRemoteExistingConnection(remoteConnction);
333 }
Santos Cordon6b7f9552015-05-27 17:21:45 -0700334
335 @Override
Tyler Gunndee56a82016-03-23 16:06:34 -0700336 public void putExtras(String callId, Bundle extras) {
337 if (hasConnection(callId)) {
338 findConnectionForAction(callId, "putExtras").putExtras(extras);
Santos Cordon6b7f9552015-05-27 17:21:45 -0700339 } else {
Tyler Gunndee56a82016-03-23 16:06:34 -0700340 findConferenceForAction(callId, "putExtras").putExtras(extras);
341 }
342 }
343
344 @Override
345 public void removeExtras(String callId, List<String> keys) {
346 if (hasConnection(callId)) {
347 findConnectionForAction(callId, "removeExtra").removeExtras(keys);
348 } else {
349 findConferenceForAction(callId, "removeExtra").removeExtras(keys);
Santos Cordon6b7f9552015-05-27 17:21:45 -0700350 }
351 }
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -0800352
353 @Override
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700354 public void onConnectionEvent(String callId, String event, Bundle extras) {
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -0800355 if (mConnectionById.containsKey(callId)) {
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700356 findConnectionForAction(callId, "onConnectionEvent").onConnectionEvent(event,
357 extras);
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -0800358 }
359 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700360 };
361
Ihab Awad5d0410f2014-07-30 10:07:40 -0700362 private final ConnectionServiceAdapterServant mServant =
363 new ConnectionServiceAdapterServant(mServantDelegate);
Santos Cordon52d8a152014-06-17 19:08:45 -0700364
Ihab Awad5d0410f2014-07-30 10:07:40 -0700365 private final DeathRecipient mDeathRecipient = new DeathRecipient() {
366 @Override
367 public void binderDied() {
368 for (RemoteConnection c : mConnectionById.values()) {
369 c.setDestroyed();
370 }
Ihab Awadb8e85c72014-08-23 20:34:57 -0700371 for (RemoteConference c : mConferenceById.values()) {
372 c.setDestroyed();
373 }
Ihab Awad5d0410f2014-07-30 10:07:40 -0700374 mConnectionById.clear();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700375 mConferenceById.clear();
Ihab Awad5d0410f2014-07-30 10:07:40 -0700376 mPendingConnections.clear();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700377 mOutgoingConnectionServiceRpc.asBinder().unlinkToDeath(mDeathRecipient, 0);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700378 }
379 };
380
Ihab Awadb8e85c72014-08-23 20:34:57 -0700381 private final IConnectionService mOutgoingConnectionServiceRpc;
382 private final ConnectionService mOurConnectionServiceImpl;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700383 private final Map<String, RemoteConnection> mConnectionById = new HashMap<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700384 private final Map<String, RemoteConference> mConferenceById = new HashMap<>();
Ihab Awad5d0410f2014-07-30 10:07:40 -0700385 private final Set<RemoteConnection> mPendingConnections = new HashSet<>();
386
Ihab Awadb8e85c72014-08-23 20:34:57 -0700387 RemoteConnectionService(
388 IConnectionService outgoingConnectionServiceRpc,
389 ConnectionService ourConnectionServiceImpl) throws RemoteException {
390 mOutgoingConnectionServiceRpc = outgoingConnectionServiceRpc;
391 mOutgoingConnectionServiceRpc.asBinder().linkToDeath(mDeathRecipient, 0);
392 mOurConnectionServiceImpl = ourConnectionServiceImpl;
Santos Cordon52d8a152014-06-17 19:08:45 -0700393 }
394
395 @Override
396 public String toString() {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700397 return "[RemoteCS - " + mOutgoingConnectionServiceRpc.asBinder().toString() + "]";
Santos Cordon52d8a152014-06-17 19:08:45 -0700398 }
399
Ihab Awadf8b69882014-07-25 15:14:01 -0700400 final RemoteConnection createRemoteConnection(
401 PhoneAccountHandle connectionManagerPhoneAccount,
402 ConnectionRequest request,
403 boolean isIncoming) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700404 final String id = UUID.randomUUID().toString();
Ihab Awad8aecfed2014-08-08 17:06:11 -0700405 final ConnectionRequest newRequest = new ConnectionRequest(
Ihab Awad5d0410f2014-07-30 10:07:40 -0700406 request.getAccountHandle(),
Nancy Chenea38cca2014-09-05 16:38:49 -0700407 request.getAddress(),
Ihab Awad5d0410f2014-07-30 10:07:40 -0700408 request.getExtras(),
409 request.getVideoState());
410 try {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700411 if (mConnectionById.isEmpty()) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700412 mOutgoingConnectionServiceRpc.addConnectionServiceAdapter(mServant.getStub());
Ihab Awad8aecfed2014-08-08 17:06:11 -0700413 }
414 RemoteConnection connection =
Ihab Awadb8e85c72014-08-23 20:34:57 -0700415 new RemoteConnection(id, mOutgoingConnectionServiceRpc, newRequest);
Ihab Awad8aecfed2014-08-08 17:06:11 -0700416 mPendingConnections.add(connection);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700417 mConnectionById.put(id, connection);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700418 mOutgoingConnectionServiceRpc.createConnection(
Ihab Awad5d0410f2014-07-30 10:07:40 -0700419 connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700420 id,
Ihab Awad5d0410f2014-07-30 10:07:40 -0700421 newRequest,
Yorke Leec3cf9822014-10-02 09:38:39 -0700422 isIncoming,
423 false /* isUnknownCall */);
Andrew Lee100e2932014-09-08 15:34:24 -0700424 connection.registerCallback(new RemoteConnection.Callback() {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700425 @Override
426 public void onDestroyed(RemoteConnection connection) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700427 mConnectionById.remove(id);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700428 maybeDisconnectAdapter();
Ihab Awad8aecfed2014-08-08 17:06:11 -0700429 }
430 });
Ihab Awad5d0410f2014-07-30 10:07:40 -0700431 return connection;
432 } catch (RemoteException e) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700433 return RemoteConnection.failure(
434 new DisconnectCause(DisconnectCause.ERROR, e.toString()));
Santos Cordon52d8a152014-06-17 19:08:45 -0700435 }
436 }
437
Ihab Awad50e35062014-09-30 09:17:03 -0700438 private boolean hasConnection(String callId) {
mike dooley879142b2014-10-08 13:39:28 -0700439 return mConnectionById.containsKey(callId);
Ihab Awad50e35062014-09-30 09:17:03 -0700440 }
441
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700442 private RemoteConnection findConnectionForAction(
443 String callId, String action) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700444 if (mConnectionById.containsKey(callId)) {
445 return mConnectionById.get(callId);
446 }
447 Log.w(this, "%s - Cannot find Connection %s", action, callId);
448 return NULL_CONNECTION;
Santos Cordon52d8a152014-06-17 19:08:45 -0700449 }
Ihab Awadb8e85c72014-08-23 20:34:57 -0700450
451 private RemoteConference findConferenceForAction(
452 String callId, String action) {
453 if (mConferenceById.containsKey(callId)) {
454 return mConferenceById.get(callId);
455 }
456 Log.w(this, "%s - Cannot find Conference %s", action, callId);
457 return NULL_CONFERENCE;
458 }
459
460 private void maybeDisconnectAdapter() {
461 if (mConnectionById.isEmpty() && mConferenceById.isEmpty()) {
462 try {
463 mOutgoingConnectionServiceRpc.removeConnectionServiceAdapter(mServant.getStub());
464 } catch (RemoteException e) {
465 }
466 }
467 }
Santos Cordon52d8a152014-06-17 19:08:45 -0700468}