Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 1 | /* |
| 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 Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 14 | * limitations under the License. |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 17 | package android.telecom; |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 18 | |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 19 | import android.net.Uri; |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 20 | import android.os.IBinder; |
Santos Cordon | e8dc4be | 2014-07-21 01:28:28 -0700 | [diff] [blame] | 21 | import android.os.IBinder.DeathRecipient; |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 22 | import android.os.RemoteException; |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 23 | |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 24 | import com.android.internal.telecom.IConnectionService; |
| 25 | import com.android.internal.telecom.IConnectionServiceAdapter; |
| 26 | import com.android.internal.telecom.IVideoProvider; |
| 27 | import com.android.internal.telecom.RemoteServiceCallback; |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 28 | |
Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 29 | import java.util.ArrayList; |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 30 | import java.util.HashMap; |
| 31 | import java.util.HashSet; |
| 32 | import java.util.Map; |
| 33 | import java.util.Set; |
Santos Cordon | 7c7bc7f | 2014-07-28 18:15:48 -0700 | [diff] [blame] | 34 | import java.util.List; |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 35 | import java.util.UUID; |
| 36 | |
| 37 | /** |
| 38 | * Remote connection service which other connection services can use to place calls on their behalf. |
Sailesh Nepal | 091768c | 2014-06-30 15:15:23 -0700 | [diff] [blame] | 39 | * |
| 40 | * @hide |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 41 | */ |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 42 | final class RemoteConnectionService { |
Sailesh Nepal | 4803159 | 2014-07-18 14:21:23 -0700 | [diff] [blame] | 43 | |
Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 44 | 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 Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 49 | |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 50 | private final IConnectionServiceAdapter mServantDelegate = new IConnectionServiceAdapter() { |
Sailesh Nepal | 4803159 | 2014-07-18 14:21:23 -0700 | [diff] [blame] | 51 | @Override |
Ihab Awad | 6107bab | 2014-08-18 09:23:25 -0700 | [diff] [blame] | 52 | public void handleCreateConnectionComplete( |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 53 | String id, |
| 54 | ConnectionRequest request, |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 55 | ParcelableConnection parcel) { |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 56 | RemoteConnection connection = |
| 57 | findConnectionForAction(id, "handleCreateConnectionSuccessful"); |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 58 | if (connection != NULL_CONNECTION && mPendingConnections.contains(connection)) { |
| 59 | mPendingConnections.remove(connection); |
Ihab Awad | 6107bab | 2014-08-18 09:23:25 -0700 | [diff] [blame] | 60 | // Unconditionally initialize the connection ... |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 61 | connection.setCallCapabilities(parcel.getCapabilities()); |
Andrew Lee | 100e293 | 2014-09-08 15:34:24 -0700 | [diff] [blame] | 62 | connection.setAddress( |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 63 | parcel.getHandle(), parcel.getHandlePresentation()); |
| 64 | connection.setCallerDisplayName( |
| 65 | parcel.getCallerDisplayName(), |
| 66 | parcel.getCallerDisplayNamePresentation()); |
Sailesh Nepal | 70638f1 | 2014-09-09 21:49:14 -0700 | [diff] [blame] | 67 | // Set state after handle so that the client can identify the connection. |
| 68 | connection.setState(parcel.getState()); |
Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 69 | List<RemoteConnection> conferenceable = new ArrayList<>(); |
| 70 | for (String confId : parcel.getConferenceableConnectionIds()) { |
| 71 | if (mConnectionById.containsKey(confId)) { |
| 72 | conferenceable.add(mConnectionById.get(confId)); |
| 73 | } |
| 74 | } |
| 75 | connection.setConferenceableConnections(conferenceable); |
Ihab Awad | a64627c | 2014-08-20 09:36:40 -0700 | [diff] [blame] | 76 | connection.setVideoState(parcel.getVideoState()); |
Ihab Awad | 6107bab | 2014-08-18 09:23:25 -0700 | [diff] [blame] | 77 | if (connection.getState() == Connection.STATE_DISCONNECTED) { |
| 78 | // ... then, if it was created in a disconnected state, that indicates |
| 79 | // failure on the providing end, so immediately mark it destroyed |
| 80 | connection.setDestroyed(); |
| 81 | } |
Sailesh Nepal | 4803159 | 2014-07-18 14:21:23 -0700 | [diff] [blame] | 82 | } |
| 83 | } |
Sailesh Nepal | 2ab88cc | 2014-07-18 14:49:18 -0700 | [diff] [blame] | 84 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 85 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 86 | public void setActive(String callId) { |
Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 87 | if (mConnectionById.containsKey(callId)) { |
| 88 | findConnectionForAction(callId, "setActive") |
| 89 | .setState(Connection.STATE_ACTIVE); |
| 90 | } else { |
| 91 | findConferenceForAction(callId, "setActive") |
| 92 | .setState(Connection.STATE_ACTIVE); |
| 93 | } |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 96 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 97 | public void setRinging(String callId) { |
| 98 | findConnectionForAction(callId, "setRinging") |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 99 | .setState(Connection.STATE_RINGING); |
Andrew Lee | 5ffbe8b | 2014-06-20 16:29:33 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 102 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 103 | public void setDialing(String callId) { |
| 104 | findConnectionForAction(callId, "setDialing") |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 105 | .setState(Connection.STATE_DIALING); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 108 | @Override |
Andrew Lee | 7f3d41f | 2014-09-11 17:33:16 -0700 | [diff] [blame^] | 109 | public void setDisconnected(String callId, DisconnectCause disconnectCause) { |
Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 110 | if (mConnectionById.containsKey(callId)) { |
| 111 | findConnectionForAction(callId, "setDisconnected") |
Andrew Lee | 7f3d41f | 2014-09-11 17:33:16 -0700 | [diff] [blame^] | 112 | .setDisconnected(disconnectCause); |
Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 113 | } else { |
| 114 | findConferenceForAction(callId, "setDisconnected") |
Andrew Lee | 7f3d41f | 2014-09-11 17:33:16 -0700 | [diff] [blame^] | 115 | .setDisconnected(disconnectCause); |
Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 116 | } |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 119 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 120 | public void setOnHold(String callId) { |
Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 121 | if (mConnectionById.containsKey(callId)) { |
| 122 | findConnectionForAction(callId, "setOnHold") |
| 123 | .setState(Connection.STATE_HOLDING); |
| 124 | } else { |
| 125 | findConferenceForAction(callId, "setOnHold") |
| 126 | .setState(Connection.STATE_HOLDING); |
| 127 | } |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 130 | @Override |
Andrew Lee | 100e293 | 2014-09-08 15:34:24 -0700 | [diff] [blame] | 131 | public void setRingbackRequested(String callId, boolean ringing) { |
| 132 | findConnectionForAction(callId, "setRingbackRequested") |
| 133 | .setRingbackRequested(ringing); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 136 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 137 | public void setCallCapabilities(String callId, int callCapabilities) { |
Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 138 | if (mConnectionById.containsKey(callId)) { |
| 139 | findConnectionForAction(callId, "setCallCapabilities") |
| 140 | .setCallCapabilities(callCapabilities); |
| 141 | } else { |
| 142 | findConferenceForAction(callId, "setCallCapabilities") |
| 143 | .setCallCapabilities(callCapabilities); |
| 144 | } |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 147 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 148 | public void setIsConferenced(String callId, String conferenceCallId) { |
Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 149 | // Note: callId should not be null; conferenceCallId may be null |
| 150 | RemoteConnection connection = |
| 151 | findConnectionForAction(callId, "setIsConferenced"); |
| 152 | if (connection != NULL_CONNECTION) { |
| 153 | if (conferenceCallId == null) { |
| 154 | // 'connection' is being split from its conference |
| 155 | if (connection.getConference() != null) { |
| 156 | connection.getConference().removeConnection(connection); |
| 157 | } |
| 158 | } else { |
| 159 | RemoteConference conference = |
| 160 | findConferenceForAction(conferenceCallId, "setIsConferenced"); |
| 161 | if (conference != NULL_CONFERENCE) { |
| 162 | conference.addConnection(connection); |
| 163 | } |
| 164 | } |
| 165 | } |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 168 | @Override |
Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 169 | public void addConferenceCall( |
| 170 | final String callId, |
| 171 | ParcelableConference parcel) { |
| 172 | RemoteConference conference = new RemoteConference(callId, |
| 173 | mOutgoingConnectionServiceRpc); |
| 174 | |
| 175 | for (String id : parcel.getConnectionIds()) { |
| 176 | RemoteConnection c = mConnectionById.get(id); |
| 177 | if (c != null) { |
| 178 | conference.addConnection(c); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if (conference.getConnections().size() == 0) { |
| 183 | // A conference was created, but none of its connections are ones that have been |
| 184 | // created by, and therefore being tracked by, this remote connection service. It |
| 185 | // is of no interest to us. |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | conference.setState(parcel.getState()); |
| 190 | conference.setCallCapabilities(parcel.getCapabilities()); |
| 191 | mConferenceById.put(callId, conference); |
Andrew Lee | 100e293 | 2014-09-08 15:34:24 -0700 | [diff] [blame] | 192 | conference.registerCallback(new RemoteConference.Callback() { |
Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 193 | @Override |
| 194 | public void onDestroyed(RemoteConference c) { |
| 195 | mConferenceById.remove(callId); |
| 196 | maybeDisconnectAdapter(); |
| 197 | } |
| 198 | }); |
| 199 | |
| 200 | mOurConnectionServiceImpl.addRemoteConference(conference); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 203 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 204 | public void removeCall(String callId) { |
Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 205 | if (mConnectionById.containsKey(callId)) { |
| 206 | findConnectionForAction(callId, "removeCall") |
| 207 | .setDestroyed(); |
| 208 | } else { |
| 209 | findConferenceForAction(callId, "removeCall") |
| 210 | .setDestroyed(); |
| 211 | } |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 214 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 215 | public void onPostDialWait(String callId, String remaining) { |
| 216 | findConnectionForAction(callId, "onPostDialWait") |
| 217 | .setPostDialWait(remaining); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 220 | @Override |
| 221 | public void queryRemoteConnectionServices(RemoteServiceCallback callback) { |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 222 | // Not supported from remote connection service. |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 223 | } |
Tyler Gunn | 8d83fa9 | 2014-07-01 11:31:21 -0700 | [diff] [blame] | 224 | |
Sailesh Nepal | 33aaae4 | 2014-07-07 22:49:44 -0700 | [diff] [blame] | 225 | @Override |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 226 | public void setVideoProvider(String callId, IVideoProvider videoProvider) { |
Ihab Awad | a64627c | 2014-08-20 09:36:40 -0700 | [diff] [blame] | 227 | findConnectionForAction(callId, "setVideoProvider") |
| 228 | .setVideoProvider(new RemoteConnection.VideoProvider(videoProvider)); |
Tyler Gunn | aa07df8 | 2014-07-17 07:50:22 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 232 | public void setVideoState(String callId, int videoState) { |
| 233 | findConnectionForAction(callId, "setVideoState") |
| 234 | .setVideoState(videoState); |
Sailesh Nepal | 33aaae4 | 2014-07-07 22:49:44 -0700 | [diff] [blame] | 235 | } |
Sailesh Nepal | e7ef59a | 2014-07-08 21:48:22 -0700 | [diff] [blame] | 236 | |
Sailesh Nepal | e7ef59a | 2014-07-08 21:48:22 -0700 | [diff] [blame] | 237 | @Override |
Andrew Lee | 100e293 | 2014-09-08 15:34:24 -0700 | [diff] [blame] | 238 | public void setIsVoipAudioMode(String callId, boolean isVoip) { |
| 239 | findConnectionForAction(callId, "setIsVoipAudioMode") |
| 240 | .setIsVoipAudioMode(isVoip); |
Sailesh Nepal | e7ef59a | 2014-07-08 21:48:22 -0700 | [diff] [blame] | 241 | } |
Sailesh Nepal | 6120386 | 2014-07-11 14:50:13 -0700 | [diff] [blame] | 242 | |
| 243 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 244 | public void setStatusHints(String callId, StatusHints statusHints) { |
| 245 | findConnectionForAction(callId, "setStatusHints") |
| 246 | .setStatusHints(statusHints); |
Sailesh Nepal | 6120386 | 2014-07-11 14:50:13 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | @Override |
Andrew Lee | 100e293 | 2014-09-08 15:34:24 -0700 | [diff] [blame] | 250 | public void setAddress(String callId, Uri address, int presentation) { |
| 251 | findConnectionForAction(callId, "setAddress") |
| 252 | .setAddress(address, presentation); |
Sailesh Nepal | 6120386 | 2014-07-11 14:50:13 -0700 | [diff] [blame] | 253 | } |
Sailesh Nepal | 2ab88cc | 2014-07-18 14:49:18 -0700 | [diff] [blame] | 254 | |
| 255 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 256 | public void setCallerDisplayName(String callId, String callerDisplayName, |
| 257 | int presentation) { |
| 258 | findConnectionForAction(callId, "setCallerDisplayName") |
| 259 | .setCallerDisplayName(callerDisplayName, presentation); |
| 260 | } |
| 261 | |
| 262 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 263 | public IBinder asBinder() { |
| 264 | throw new UnsupportedOperationException(); |
Sailesh Nepal | 2ab88cc | 2014-07-18 14:49:18 -0700 | [diff] [blame] | 265 | } |
Santos Cordon | 7c7bc7f | 2014-07-28 18:15:48 -0700 | [diff] [blame] | 266 | |
| 267 | @Override |
| 268 | public final void setConferenceableConnections( |
| 269 | String callId, List<String> conferenceableConnectionIds) { |
Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 270 | List<RemoteConnection> conferenceable = new ArrayList<>(); |
| 271 | for (String id : conferenceableConnectionIds) { |
| 272 | if (mConnectionById.containsKey(id)) { |
| 273 | conferenceable.add(mConnectionById.get(id)); |
| 274 | } |
| 275 | } |
Santos Cordon | 7c7bc7f | 2014-07-28 18:15:48 -0700 | [diff] [blame] | 276 | |
Santos Cordon | 7c7bc7f | 2014-07-28 18:15:48 -0700 | [diff] [blame] | 277 | findConnectionForAction(callId, "setConferenceableConnections") |
Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 278 | .setConferenceableConnections(conferenceable); |
Santos Cordon | 7c7bc7f | 2014-07-28 18:15:48 -0700 | [diff] [blame] | 279 | } |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 280 | }; |
| 281 | |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 282 | private final ConnectionServiceAdapterServant mServant = |
| 283 | new ConnectionServiceAdapterServant(mServantDelegate); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 284 | |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 285 | private final DeathRecipient mDeathRecipient = new DeathRecipient() { |
| 286 | @Override |
| 287 | public void binderDied() { |
| 288 | for (RemoteConnection c : mConnectionById.values()) { |
| 289 | c.setDestroyed(); |
| 290 | } |
Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 291 | for (RemoteConference c : mConferenceById.values()) { |
| 292 | c.setDestroyed(); |
| 293 | } |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 294 | mConnectionById.clear(); |
Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 295 | mConferenceById.clear(); |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 296 | mPendingConnections.clear(); |
Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 297 | mOutgoingConnectionServiceRpc.asBinder().unlinkToDeath(mDeathRecipient, 0); |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 298 | } |
| 299 | }; |
| 300 | |
Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 301 | private final IConnectionService mOutgoingConnectionServiceRpc; |
| 302 | private final ConnectionService mOurConnectionServiceImpl; |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 303 | private final Map<String, RemoteConnection> mConnectionById = new HashMap<>(); |
Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 304 | private final Map<String, RemoteConference> mConferenceById = new HashMap<>(); |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 305 | private final Set<RemoteConnection> mPendingConnections = new HashSet<>(); |
| 306 | |
Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 307 | RemoteConnectionService( |
| 308 | IConnectionService outgoingConnectionServiceRpc, |
| 309 | ConnectionService ourConnectionServiceImpl) throws RemoteException { |
| 310 | mOutgoingConnectionServiceRpc = outgoingConnectionServiceRpc; |
| 311 | mOutgoingConnectionServiceRpc.asBinder().linkToDeath(mDeathRecipient, 0); |
| 312 | mOurConnectionServiceImpl = ourConnectionServiceImpl; |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | @Override |
| 316 | public String toString() { |
Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 317 | return "[RemoteCS - " + mOutgoingConnectionServiceRpc.asBinder().toString() + "]"; |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Ihab Awad | f8b6988 | 2014-07-25 15:14:01 -0700 | [diff] [blame] | 320 | final RemoteConnection createRemoteConnection( |
| 321 | PhoneAccountHandle connectionManagerPhoneAccount, |
| 322 | ConnectionRequest request, |
| 323 | boolean isIncoming) { |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 324 | final String id = UUID.randomUUID().toString(); |
Ihab Awad | 8aecfed | 2014-08-08 17:06:11 -0700 | [diff] [blame] | 325 | final ConnectionRequest newRequest = new ConnectionRequest( |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 326 | request.getAccountHandle(), |
Nancy Chen | ea38cca | 2014-09-05 16:38:49 -0700 | [diff] [blame] | 327 | request.getAddress(), |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 328 | request.getExtras(), |
| 329 | request.getVideoState()); |
| 330 | try { |
Ihab Awad | 8aecfed | 2014-08-08 17:06:11 -0700 | [diff] [blame] | 331 | if (mConnectionById.isEmpty()) { |
Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 332 | mOutgoingConnectionServiceRpc.addConnectionServiceAdapter(mServant.getStub()); |
Ihab Awad | 8aecfed | 2014-08-08 17:06:11 -0700 | [diff] [blame] | 333 | } |
| 334 | RemoteConnection connection = |
Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 335 | new RemoteConnection(id, mOutgoingConnectionServiceRpc, newRequest); |
Ihab Awad | 8aecfed | 2014-08-08 17:06:11 -0700 | [diff] [blame] | 336 | mPendingConnections.add(connection); |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 337 | mConnectionById.put(id, connection); |
Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 338 | mOutgoingConnectionServiceRpc.createConnection( |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 339 | connectionManagerPhoneAccount, |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 340 | id, |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 341 | newRequest, |
| 342 | isIncoming); |
Andrew Lee | 100e293 | 2014-09-08 15:34:24 -0700 | [diff] [blame] | 343 | connection.registerCallback(new RemoteConnection.Callback() { |
Ihab Awad | 8aecfed | 2014-08-08 17:06:11 -0700 | [diff] [blame] | 344 | @Override |
| 345 | public void onDestroyed(RemoteConnection connection) { |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 346 | mConnectionById.remove(id); |
Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 347 | maybeDisconnectAdapter(); |
Ihab Awad | 8aecfed | 2014-08-08 17:06:11 -0700 | [diff] [blame] | 348 | } |
| 349 | }); |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 350 | return connection; |
| 351 | } catch (RemoteException e) { |
Andrew Lee | 7f3d41f | 2014-09-11 17:33:16 -0700 | [diff] [blame^] | 352 | return RemoteConnection.failure( |
| 353 | new DisconnectCause(DisconnectCause.ERROR, e.toString())); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 357 | private RemoteConnection findConnectionForAction( |
| 358 | String callId, String action) { |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 359 | if (mConnectionById.containsKey(callId)) { |
| 360 | return mConnectionById.get(callId); |
| 361 | } |
| 362 | Log.w(this, "%s - Cannot find Connection %s", action, callId); |
| 363 | return NULL_CONNECTION; |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 364 | } |
Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 365 | |
| 366 | private RemoteConference findConferenceForAction( |
| 367 | String callId, String action) { |
| 368 | if (mConferenceById.containsKey(callId)) { |
| 369 | return mConferenceById.get(callId); |
| 370 | } |
| 371 | Log.w(this, "%s - Cannot find Conference %s", action, callId); |
| 372 | return NULL_CONFERENCE; |
| 373 | } |
| 374 | |
| 375 | private void maybeDisconnectAdapter() { |
| 376 | if (mConnectionById.isEmpty() && mConferenceById.isEmpty()) { |
| 377 | try { |
| 378 | mOutgoingConnectionServiceRpc.removeConnectionServiceAdapter(mServant.getStub()); |
| 379 | } catch (RemoteException e) { |
| 380 | } |
| 381 | } |
| 382 | } |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 383 | } |