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 |
| 14 | R* limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.telecomm; |
| 18 | |
Sailesh Nepal | 2ab88cc | 2014-07-18 14:49:18 -0700 | [diff] [blame] | 19 | import android.app.PendingIntent; |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 20 | import android.net.Uri; |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 21 | import android.os.IBinder; |
Santos Cordon | e8dc4be | 2014-07-21 01:28:28 -0700 | [diff] [blame] | 22 | import android.os.IBinder.DeathRecipient; |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 23 | import android.os.RemoteException; |
| 24 | import android.telephony.DisconnectCause; |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 25 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 26 | import com.android.internal.telecomm.IConnectionService; |
| 27 | import com.android.internal.telecomm.IConnectionServiceAdapter; |
Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 28 | import com.android.internal.telecomm.IVideoCallProvider; |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 29 | import com.android.internal.telecomm.RemoteServiceCallback; |
| 30 | |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 31 | import java.util.HashMap; |
| 32 | import java.util.HashSet; |
| 33 | import java.util.Map; |
| 34 | import java.util.Set; |
Santos Cordon | 7c7bc7f | 2014-07-28 18:15:48 -0700 | [diff] [blame] | 35 | import java.util.Collections; |
| 36 | import java.util.List; |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 37 | import java.util.UUID; |
| 38 | |
| 39 | /** |
| 40 | * 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] | 41 | * |
| 42 | * @hide |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 43 | */ |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 44 | final class RemoteConnectionService { |
Sailesh Nepal | 4803159 | 2014-07-18 14:21:23 -0700 | [diff] [blame] | 45 | |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 46 | private static final RemoteConnection NULL_CONNECTION = new RemoteConnection(null, null); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 47 | |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 48 | private final IConnectionServiceAdapter mServantDelegate = new IConnectionServiceAdapter() { |
Sailesh Nepal | 4803159 | 2014-07-18 14:21:23 -0700 | [diff] [blame] | 49 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 50 | public void handleCreateConnectionSuccessful(ConnectionRequest request, |
| 51 | ParcelableConnection parcel) { |
| 52 | RemoteConnection connection = findConnectionForAction( |
| 53 | request.getCallId(), "handleCreateConnectionSuccessful"); |
| 54 | if (connection != NULL_CONNECTION && mPendingConnections.contains(connection)) { |
| 55 | mPendingConnections.remove(connection); |
| 56 | connection.setState(parcel.getState()); |
| 57 | connection.setCallCapabilities(parcel.getCapabilities()); |
| 58 | connection.setHandle( |
| 59 | parcel.getHandle(), parcel.getHandlePresentation()); |
| 60 | connection.setCallerDisplayName( |
| 61 | parcel.getCallerDisplayName(), |
| 62 | parcel.getCallerDisplayNamePresentation()); |
| 63 | // TODO: Do we need to support video providers for remote connections? |
Sailesh Nepal | 4803159 | 2014-07-18 14:21:23 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
Sailesh Nepal | 2ab88cc | 2014-07-18 14:49:18 -0700 | [diff] [blame] | 66 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 67 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 68 | public void handleCreateConnectionFailed(ConnectionRequest request, int errorCode, |
| 69 | String errorMessage) { |
| 70 | // TODO: How do we propagate the failure codes? |
| 71 | findConnectionForAction( |
| 72 | request.getCallId(), "handleCreateConnectionFailed") |
| 73 | .setDestroyed(); |
Sailesh Nepal | 506e386 | 2014-06-25 13:35:14 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 76 | @Override |
Sailesh Nepal | c5b0157 | 2014-07-14 16:29:44 -0700 | [diff] [blame] | 77 | public void handleCreateConnectionCancelled(ConnectionRequest request) { |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 78 | findConnectionForAction( |
| 79 | request.getCallId(), "handleCreateConnectionCancelled") |
| 80 | .setDestroyed(); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 83 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 84 | public void setActive(String callId) { |
| 85 | findConnectionForAction(callId, "setActive") |
| 86 | .setState(Connection.State.ACTIVE); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 89 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 90 | public void setRinging(String callId) { |
| 91 | findConnectionForAction(callId, "setRinging") |
| 92 | .setState(Connection.State.RINGING); |
Andrew Lee | 5ffbe8b | 2014-06-20 16:29:33 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 95 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 96 | public void setDialing(String callId) { |
| 97 | findConnectionForAction(callId, "setDialing") |
| 98 | .setState(Connection.State.DIALING); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 101 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 102 | public void setDisconnected(String callId, int disconnectCause, |
| 103 | String disconnectMessage) { |
| 104 | findConnectionForAction(callId, "setDisconnected") |
| 105 | .setDisconnected(disconnectCause, disconnectMessage); |
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 |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 109 | public void setOnHold(String callId) { |
| 110 | findConnectionForAction(callId, "setOnHold") |
| 111 | .setState(Connection.State.HOLDING); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 114 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 115 | public void setRequestingRingback(String callId, boolean ringing) { |
| 116 | findConnectionForAction(callId, "setRequestingRingback") |
| 117 | .setRequestingRingback(ringing); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 120 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 121 | public void setCallCapabilities(String callId, int callCapabilities) { |
| 122 | findConnectionForAction("callId", "setCallCapabilities") |
| 123 | .setCallCapabilities(callCapabilities); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 126 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 127 | public void setIsConferenced(String callId, String conferenceCallId) { |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 128 | // not supported for remote connections. |
| 129 | } |
| 130 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 131 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 132 | public void addConferenceCall(String callId) { |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 133 | // not supported for remote connections. |
| 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 removeCall(String callId) { |
| 138 | findConnectionForAction(callId, "removeCall") |
| 139 | .setDestroyed(); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 142 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 143 | public void onPostDialWait(String callId, String remaining) { |
| 144 | findConnectionForAction(callId, "onPostDialWait") |
| 145 | .setPostDialWait(remaining); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 148 | @Override |
| 149 | public void queryRemoteConnectionServices(RemoteServiceCallback callback) { |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 150 | // Not supported from remote connection service. |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 151 | } |
Tyler Gunn | 8d83fa9 | 2014-07-01 11:31:21 -0700 | [diff] [blame] | 152 | |
Sailesh Nepal | 33aaae4 | 2014-07-07 22:49:44 -0700 | [diff] [blame] | 153 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 154 | public void setVideoCallProvider(String callId, |
| 155 | IVideoCallProvider videoCallProvider) { |
Sailesh Nepal | 4803159 | 2014-07-18 14:21:23 -0700 | [diff] [blame] | 156 | // not supported for remote connections. |
Tyler Gunn | aa07df8 | 2014-07-17 07:50:22 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 160 | public void setVideoState(String callId, int videoState) { |
| 161 | findConnectionForAction(callId, "setVideoState") |
| 162 | .setVideoState(videoState); |
Sailesh Nepal | 33aaae4 | 2014-07-07 22:49:44 -0700 | [diff] [blame] | 163 | } |
Sailesh Nepal | e7ef59a | 2014-07-08 21:48:22 -0700 | [diff] [blame] | 164 | |
Sailesh Nepal | e7ef59a | 2014-07-08 21:48:22 -0700 | [diff] [blame] | 165 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 166 | public void setAudioModeIsVoip(String callId, boolean isVoip) { |
| 167 | findConnectionForAction(callId, "setAudioModeIsVoip") |
| 168 | .setAudioModeIsVoip(isVoip); |
Sailesh Nepal | e7ef59a | 2014-07-08 21:48:22 -0700 | [diff] [blame] | 169 | } |
Sailesh Nepal | 6120386 | 2014-07-11 14:50:13 -0700 | [diff] [blame] | 170 | |
| 171 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 172 | public void setStatusHints(String callId, StatusHints statusHints) { |
| 173 | findConnectionForAction(callId, "setStatusHints") |
| 174 | .setStatusHints(statusHints); |
Sailesh Nepal | 6120386 | 2014-07-11 14:50:13 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 178 | public void setHandle(String callId, Uri handle, int presentation) { |
| 179 | findConnectionForAction(callId, "setHandle") |
| 180 | .setHandle(handle, presentation); |
Sailesh Nepal | 6120386 | 2014-07-11 14:50:13 -0700 | [diff] [blame] | 181 | } |
Sailesh Nepal | 2ab88cc | 2014-07-18 14:49:18 -0700 | [diff] [blame] | 182 | |
| 183 | @Override |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 184 | public void setCallerDisplayName(String callId, String callerDisplayName, |
| 185 | int presentation) { |
| 186 | findConnectionForAction(callId, "setCallerDisplayName") |
| 187 | .setCallerDisplayName(callerDisplayName, presentation); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public void startActivityFromInCall(String callId, PendingIntent intent) { |
| 192 | findConnectionForAction(callId, "startActivityFromInCall") |
| 193 | .startActivityFromInCall(intent); |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public IBinder asBinder() { |
| 198 | throw new UnsupportedOperationException(); |
Sailesh Nepal | 2ab88cc | 2014-07-18 14:49:18 -0700 | [diff] [blame] | 199 | } |
Santos Cordon | 7c7bc7f | 2014-07-28 18:15:48 -0700 | [diff] [blame] | 200 | |
| 201 | @Override |
| 202 | public final void setConferenceableConnections( |
| 203 | String callId, List<String> conferenceableConnectionIds) { |
| 204 | |
| 205 | // TODO: When we support more than 1 remote connection, this should |
| 206 | // loop through the incoming list of connection IDs and acquire the list |
| 207 | // of remote connections which correspond to the IDs. That list should |
| 208 | // be set onto the remote connections. |
| 209 | findConnectionForAction(callId, "setConferenceableConnections") |
| 210 | .setConferenceableConnections(Collections.<RemoteConnection>emptyList()); |
| 211 | } |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 212 | }; |
| 213 | |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 214 | private final ConnectionServiceAdapterServant mServant = |
| 215 | new ConnectionServiceAdapterServant(mServantDelegate); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 216 | |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 217 | private final DeathRecipient mDeathRecipient = new DeathRecipient() { |
| 218 | @Override |
| 219 | public void binderDied() { |
| 220 | for (RemoteConnection c : mConnectionById.values()) { |
| 221 | c.setDestroyed(); |
| 222 | } |
| 223 | mConnectionById.clear(); |
| 224 | mPendingConnections.clear(); |
| 225 | mConnectionService.asBinder().unlinkToDeath(mDeathRecipient, 0); |
| 226 | } |
| 227 | }; |
| 228 | |
| 229 | private final IConnectionService mConnectionService; |
| 230 | private final Map<String, RemoteConnection> mConnectionById = new HashMap<>(); |
| 231 | private final Set<RemoteConnection> mPendingConnections = new HashSet<>(); |
| 232 | |
| 233 | RemoteConnectionService(IConnectionService connectionService) throws RemoteException { |
| 234 | mConnectionService = connectionService; |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 235 | mConnectionService.asBinder().linkToDeath(mDeathRecipient, 0); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public String toString() { |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 240 | return "[RemoteCS - " + mConnectionService.asBinder().toString() + "]"; |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Ihab Awad | f8b6988 | 2014-07-25 15:14:01 -0700 | [diff] [blame] | 243 | final RemoteConnection createRemoteConnection( |
| 244 | PhoneAccountHandle connectionManagerPhoneAccount, |
| 245 | ConnectionRequest request, |
| 246 | boolean isIncoming) { |
Ihab Awad | 8aecfed | 2014-08-08 17:06:11 -0700 | [diff] [blame^] | 247 | final ConnectionRequest newRequest = new ConnectionRequest( |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 248 | request.getAccountHandle(), |
| 249 | UUID.randomUUID().toString(), |
| 250 | request.getHandle(), |
| 251 | request.getHandlePresentation(), |
| 252 | request.getExtras(), |
| 253 | request.getVideoState()); |
| 254 | try { |
Ihab Awad | 8aecfed | 2014-08-08 17:06:11 -0700 | [diff] [blame^] | 255 | if (mConnectionById.isEmpty()) { |
| 256 | mConnectionService.addConnectionServiceAdapter(mServant.getStub()); |
| 257 | } |
| 258 | RemoteConnection connection = |
| 259 | new RemoteConnection(mConnectionService, newRequest); |
| 260 | mPendingConnections.add(connection); |
| 261 | mConnectionById.put(newRequest.getCallId(), connection); |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 262 | mConnectionService.createConnection( |
| 263 | connectionManagerPhoneAccount, |
| 264 | newRequest, |
| 265 | isIncoming); |
Ihab Awad | 8aecfed | 2014-08-08 17:06:11 -0700 | [diff] [blame^] | 266 | connection.addListener(new RemoteConnection.Listener() { |
| 267 | @Override |
| 268 | public void onDestroyed(RemoteConnection connection) { |
| 269 | mConnectionById.remove(newRequest.getCallId()); |
| 270 | if (mConnectionById.isEmpty()) { |
| 271 | try { |
| 272 | mConnectionService.removeConnectionServiceAdapter(mServant.getStub()); |
| 273 | } catch (RemoteException e) { |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | }); |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 278 | return connection; |
| 279 | } catch (RemoteException e) { |
| 280 | return RemoteConnection.failure(DisconnectCause.ERROR_UNSPECIFIED, e.toString()); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
Ihab Awad | 5d0410f | 2014-07-30 10:07:40 -0700 | [diff] [blame] | 284 | private RemoteConnection findConnectionForAction(String callId, String action) { |
| 285 | if (mConnectionById.containsKey(callId)) { |
| 286 | return mConnectionById.get(callId); |
| 287 | } |
| 288 | Log.w(this, "%s - Cannot find Connection %s", action, callId); |
| 289 | return NULL_CONNECTION; |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 290 | } |
| 291 | } |