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.telecom; 18 19 import android.net.Uri; 20 import android.os.Handler; 21 import android.os.Message; 22 import android.os.RemoteException; 23 24 import com.android.internal.os.SomeArgs; 25 import com.android.internal.telecom.IConnectionServiceAdapter; 26 import com.android.internal.telecom.IVideoProvider; 27 import com.android.internal.telecom.RemoteServiceCallback; 28 29 import java.util.List; 30 31 /** 32 * A component that provides an RPC servant implementation of {@link IConnectionServiceAdapter}, 33 * posting incoming messages on the main thread on a client-supplied delegate object. 34 * 35 * TODO: Generate this and similar classes using a compiler starting from AIDL interfaces. 36 * 37 * @hide 38 */ 39 final class ConnectionServiceAdapterServant { 40 private static final int MSG_HANDLE_CREATE_CONNECTION_COMPLETE = 1; 41 private static final int MSG_SET_ACTIVE = 2; 42 private static final int MSG_SET_RINGING = 3; 43 private static final int MSG_SET_DIALING = 4; 44 private static final int MSG_SET_DISCONNECTED = 5; 45 private static final int MSG_SET_ON_HOLD = 6; 46 private static final int MSG_SET_RINGBACK_REQUESTED = 7; 47 private static final int MSG_SET_CALL_CAPABILITIES = 8; 48 private static final int MSG_SET_IS_CONFERENCED = 9; 49 private static final int MSG_ADD_CONFERENCE_CALL = 10; 50 private static final int MSG_REMOVE_CALL = 11; 51 private static final int MSG_ON_POST_DIAL_WAIT = 12; 52 private static final int MSG_QUERY_REMOTE_CALL_SERVICES = 13; 53 private static final int MSG_SET_VIDEO_STATE = 14; 54 private static final int MSG_SET_VIDEO_CALL_PROVIDER = 15; 55 private static final int MSG_SET_IS_VOIP_AUDIO_MODE = 16; 56 private static final int MSG_SET_STATUS_HINTS = 17; 57 private static final int MSG_SET_ADDRESS = 18; 58 private static final int MSG_SET_CALLER_DISPLAY_NAME = 19; 59 private static final int MSG_SET_CONFERENCEABLE_CONNECTIONS = 20; 60 61 private final IConnectionServiceAdapter mDelegate; 62 63 private final Handler mHandler = new Handler() { 64 @Override 65 public void handleMessage(Message msg) { 66 try { 67 internalHandleMessage(msg); 68 } catch (RemoteException e) { 69 } 70 } 71 72 // Internal method defined to centralize handling of RemoteException 73 private void internalHandleMessage(Message msg) throws RemoteException { 74 switch (msg.what) { 75 case MSG_HANDLE_CREATE_CONNECTION_COMPLETE: { 76 SomeArgs args = (SomeArgs) msg.obj; 77 try { 78 mDelegate.handleCreateConnectionComplete( 79 (String) args.arg1, 80 (ConnectionRequest) args.arg2, 81 (ParcelableConnection) args.arg3); 82 } finally { 83 args.recycle(); 84 } 85 break; 86 } 87 case MSG_SET_ACTIVE: 88 mDelegate.setActive((String) msg.obj); 89 break; 90 case MSG_SET_RINGING: 91 mDelegate.setRinging((String) msg.obj); 92 break; 93 case MSG_SET_DIALING: 94 mDelegate.setDialing((String) msg.obj); 95 break; 96 case MSG_SET_DISCONNECTED: { 97 SomeArgs args = (SomeArgs) msg.obj; 98 try { 99 mDelegate.setDisconnected((String) args.arg1, (DisconnectCause) args.arg2); 100 } finally { 101 args.recycle(); 102 } 103 break; 104 } 105 case MSG_SET_ON_HOLD: 106 mDelegate.setOnHold((String) msg.obj); 107 break; 108 case MSG_SET_RINGBACK_REQUESTED: 109 mDelegate.setRingbackRequested((String) msg.obj, msg.arg1 == 1); 110 break; 111 case MSG_SET_CALL_CAPABILITIES: 112 mDelegate.setCallCapabilities((String) msg.obj, msg.arg1); 113 break; 114 case MSG_SET_IS_CONFERENCED: { 115 SomeArgs args = (SomeArgs) msg.obj; 116 try { 117 mDelegate.setIsConferenced((String) args.arg1, (String) args.arg2); 118 } finally { 119 args.recycle(); 120 } 121 break; 122 } 123 case MSG_ADD_CONFERENCE_CALL: { 124 SomeArgs args = (SomeArgs) msg.obj; 125 try { 126 mDelegate.addConferenceCall( 127 (String) args.arg1, (ParcelableConference) args.arg2); 128 } finally { 129 args.recycle(); 130 } 131 break; 132 } 133 case MSG_REMOVE_CALL: 134 mDelegate.removeCall((String) msg.obj); 135 break; 136 case MSG_ON_POST_DIAL_WAIT: { 137 SomeArgs args = (SomeArgs) msg.obj; 138 try { 139 mDelegate.onPostDialWait((String) args.arg1, (String) args.arg2); 140 } finally { 141 args.recycle(); 142 } 143 break; 144 } 145 case MSG_QUERY_REMOTE_CALL_SERVICES: 146 mDelegate.queryRemoteConnectionServices((RemoteServiceCallback) msg.obj); 147 break; 148 case MSG_SET_VIDEO_STATE: 149 mDelegate.setVideoState((String) msg.obj, msg.arg1); 150 break; 151 case MSG_SET_VIDEO_CALL_PROVIDER: { 152 SomeArgs args = (SomeArgs) msg.obj; 153 try { 154 mDelegate.setVideoProvider((String) args.arg1, 155 (IVideoProvider) args.arg2); 156 } finally { 157 args.recycle(); 158 } 159 break; 160 } 161 case MSG_SET_IS_VOIP_AUDIO_MODE: 162 mDelegate.setIsVoipAudioMode((String) msg.obj, msg.arg1 == 1); 163 break; 164 case MSG_SET_STATUS_HINTS: { 165 SomeArgs args = (SomeArgs) msg.obj; 166 try { 167 mDelegate.setStatusHints((String) args.arg1, (StatusHints) args.arg2); 168 } finally { 169 args.recycle(); 170 } 171 break; 172 } 173 case MSG_SET_ADDRESS: { 174 SomeArgs args = (SomeArgs) msg.obj; 175 try { 176 mDelegate.setAddress((String) args.arg1, (Uri) args.arg2, args.argi1); 177 } finally { 178 args.recycle(); 179 } 180 break; 181 } 182 case MSG_SET_CALLER_DISPLAY_NAME: { 183 SomeArgs args = (SomeArgs) msg.obj; 184 try { 185 mDelegate.setCallerDisplayName( 186 (String) args.arg1, (String) args.arg2, args.argi1); 187 } finally { 188 args.recycle(); 189 } 190 break; 191 } 192 case MSG_SET_CONFERENCEABLE_CONNECTIONS: { 193 SomeArgs args = (SomeArgs) msg.obj; 194 try { 195 mDelegate.setConferenceableConnections( 196 (String) args.arg1, (List<String>) args.arg2); 197 } finally { 198 args.recycle(); 199 } 200 break; 201 } 202 } 203 } 204 }; 205 206 private final IConnectionServiceAdapter mStub = new IConnectionServiceAdapter.Stub() { 207 @Override 208 public void handleCreateConnectionComplete( 209 String id, 210 ConnectionRequest request, 211 ParcelableConnection connection) { 212 SomeArgs args = SomeArgs.obtain(); 213 args.arg1 = id; 214 args.arg2 = request; 215 args.arg3 = connection; 216 mHandler.obtainMessage(MSG_HANDLE_CREATE_CONNECTION_COMPLETE, args).sendToTarget(); 217 } 218 219 @Override 220 public void setActive(String connectionId) { 221 mHandler.obtainMessage(MSG_SET_ACTIVE, connectionId).sendToTarget(); 222 } 223 224 @Override 225 public void setRinging(String connectionId) { 226 mHandler.obtainMessage(MSG_SET_RINGING, connectionId).sendToTarget(); 227 } 228 229 @Override 230 public void setDialing(String connectionId) { 231 mHandler.obtainMessage(MSG_SET_DIALING, connectionId).sendToTarget(); 232 } 233 234 @Override 235 public void setDisconnected( 236 String connectionId, DisconnectCause disconnectCause) { 237 SomeArgs args = SomeArgs.obtain(); 238 args.arg1 = connectionId; 239 args.arg2 = disconnectCause; 240 mHandler.obtainMessage(MSG_SET_DISCONNECTED, args).sendToTarget(); 241 } 242 243 @Override 244 public void setOnHold(String connectionId) { 245 mHandler.obtainMessage(MSG_SET_ON_HOLD, connectionId).sendToTarget(); 246 } 247 248 @Override 249 public void setRingbackRequested(String connectionId, boolean ringback) { 250 mHandler.obtainMessage(MSG_SET_RINGBACK_REQUESTED, ringback ? 1 : 0, 0, connectionId) 251 .sendToTarget(); 252 } 253 254 @Override 255 public void setCallCapabilities(String connectionId, int callCapabilities) { 256 mHandler.obtainMessage(MSG_SET_CALL_CAPABILITIES, callCapabilities, 0, connectionId) 257 .sendToTarget(); 258 } 259 260 @Override 261 public void setIsConferenced(String callId, String conferenceCallId) { 262 SomeArgs args = SomeArgs.obtain(); 263 args.arg1 = callId; 264 args.arg2 = conferenceCallId; 265 mHandler.obtainMessage(MSG_SET_IS_CONFERENCED, args).sendToTarget(); 266 } 267 268 @Override 269 public void addConferenceCall(String callId, ParcelableConference parcelableConference) { 270 SomeArgs args = SomeArgs.obtain(); 271 args.arg1 = callId; 272 args.arg2 = parcelableConference; 273 mHandler.obtainMessage(MSG_ADD_CONFERENCE_CALL, args).sendToTarget(); 274 } 275 276 @Override 277 public void removeCall(String connectionId) { 278 mHandler.obtainMessage(MSG_REMOVE_CALL, connectionId).sendToTarget(); 279 } 280 281 @Override 282 public void onPostDialWait(String connectionId, String remainingDigits) { 283 SomeArgs args = SomeArgs.obtain(); 284 args.arg1 = connectionId; 285 args.arg2 = remainingDigits; 286 mHandler.obtainMessage(MSG_ON_POST_DIAL_WAIT, args).sendToTarget(); 287 } 288 289 @Override 290 public void queryRemoteConnectionServices(RemoteServiceCallback callback) { 291 mHandler.obtainMessage(MSG_QUERY_REMOTE_CALL_SERVICES, callback).sendToTarget(); 292 } 293 294 @Override 295 public void setVideoState(String connectionId, int videoState) { 296 mHandler.obtainMessage(MSG_SET_VIDEO_STATE, videoState, 0, connectionId).sendToTarget(); 297 } 298 299 @Override 300 public void setVideoProvider(String connectionId, IVideoProvider videoProvider) { 301 SomeArgs args = SomeArgs.obtain(); 302 args.arg1 = connectionId; 303 args.arg2 = videoProvider; 304 mHandler.obtainMessage(MSG_SET_VIDEO_CALL_PROVIDER, args).sendToTarget(); 305 } 306 307 @Override 308 public final void setIsVoipAudioMode(String connectionId, boolean isVoip) { 309 mHandler.obtainMessage(MSG_SET_IS_VOIP_AUDIO_MODE, isVoip ? 1 : 0, 0, 310 connectionId).sendToTarget(); 311 } 312 313 @Override 314 public final void setStatusHints(String connectionId, StatusHints statusHints) { 315 SomeArgs args = SomeArgs.obtain(); 316 args.arg1 = connectionId; 317 args.arg2 = statusHints; 318 mHandler.obtainMessage(MSG_SET_STATUS_HINTS, args).sendToTarget(); 319 } 320 321 @Override 322 public final void setAddress(String connectionId, Uri address, int presentation) { 323 SomeArgs args = SomeArgs.obtain(); 324 args.arg1 = connectionId; 325 args.arg2 = address; 326 args.argi1 = presentation; 327 mHandler.obtainMessage(MSG_SET_ADDRESS, args).sendToTarget(); 328 } 329 330 @Override 331 public final void setCallerDisplayName( 332 String connectionId, String callerDisplayName, int presentation) { 333 SomeArgs args = SomeArgs.obtain(); 334 args.arg1 = connectionId; 335 args.arg2 = callerDisplayName; 336 args.argi1 = presentation; 337 mHandler.obtainMessage(MSG_SET_CALLER_DISPLAY_NAME, args).sendToTarget(); 338 } 339 340 @Override 341 public final void setConferenceableConnections( 342 String connectionId, List<String> conferenceableConnectionIds) { 343 SomeArgs args = SomeArgs.obtain(); 344 args.arg1 = connectionId; 345 args.arg2 = conferenceableConnectionIds; 346 mHandler.obtainMessage(MSG_SET_CONFERENCEABLE_CONNECTIONS, args).sendToTarget(); 347 } 348 }; 349 350 public ConnectionServiceAdapterServant(IConnectionServiceAdapter delegate) { 351 mDelegate = delegate; 352 } 353 354 public IConnectionServiceAdapter getStub() { 355 return mStub; 356 } 357 } 358