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.Bundle; 21 import android.os.Handler; 22 import android.os.Message; 23 import android.os.RemoteException; 24 25 import com.android.internal.os.SomeArgs; 26 import com.android.internal.telecom.IConnectionServiceAdapter; 27 import com.android.internal.telecom.IVideoProvider; 28 import com.android.internal.telecom.RemoteServiceCallback; 29 30 import java.util.List; 31 32 /** 33 * A component that provides an RPC servant implementation of {@link IConnectionServiceAdapter}, 34 * posting incoming messages on the main thread on a client-supplied delegate object. 35 * 36 * TODO: Generate this and similar classes using a compiler starting from AIDL interfaces. 37 * 38 * @hide 39 */ 40 final class ConnectionServiceAdapterServant { 41 private static final int MSG_HANDLE_CREATE_CONNECTION_COMPLETE = 1; 42 private static final int MSG_SET_ACTIVE = 2; 43 private static final int MSG_SET_RINGING = 3; 44 private static final int MSG_SET_DIALING = 4; 45 private static final int MSG_SET_DISCONNECTED = 5; 46 private static final int MSG_SET_ON_HOLD = 6; 47 private static final int MSG_SET_RINGBACK_REQUESTED = 7; 48 private static final int MSG_SET_CONNECTION_CAPABILITIES = 8; 49 private static final int MSG_SET_IS_CONFERENCED = 9; 50 private static final int MSG_ADD_CONFERENCE_CALL = 10; 51 private static final int MSG_REMOVE_CALL = 11; 52 private static final int MSG_ON_POST_DIAL_WAIT = 12; 53 private static final int MSG_QUERY_REMOTE_CALL_SERVICES = 13; 54 private static final int MSG_SET_VIDEO_STATE = 14; 55 private static final int MSG_SET_VIDEO_CALL_PROVIDER = 15; 56 private static final int MSG_SET_IS_VOIP_AUDIO_MODE = 16; 57 private static final int MSG_SET_STATUS_HINTS = 17; 58 private static final int MSG_SET_ADDRESS = 18; 59 private static final int MSG_SET_CALLER_DISPLAY_NAME = 19; 60 private static final int MSG_SET_CONFERENCEABLE_CONNECTIONS = 20; 61 private static final int MSG_ADD_EXISTING_CONNECTION = 21; 62 private static final int MSG_ON_POST_DIAL_CHAR = 22; 63 private static final int MSG_SET_CONFERENCE_MERGE_FAILED = 23; 64 private static final int MSG_PUT_EXTRAS = 24; 65 private static final int MSG_REMOVE_EXTRAS = 25; 66 private static final int MSG_ON_CONNECTION_EVENT = 26; 67 private static final int MSG_SET_CONNECTION_PROPERTIES = 27; 68 private static final int MSG_SET_PULLING = 28; 69 70 private final IConnectionServiceAdapter mDelegate; 71 72 private final Handler mHandler = new Handler() { 73 @Override 74 public void handleMessage(Message msg) { 75 try { 76 internalHandleMessage(msg); 77 } catch (RemoteException e) { 78 } 79 } 80 81 // Internal method defined to centralize handling of RemoteException 82 private void internalHandleMessage(Message msg) throws RemoteException { 83 switch (msg.what) { 84 case MSG_HANDLE_CREATE_CONNECTION_COMPLETE: { 85 SomeArgs args = (SomeArgs) msg.obj; 86 try { 87 mDelegate.handleCreateConnectionComplete( 88 (String) args.arg1, 89 (ConnectionRequest) args.arg2, 90 (ParcelableConnection) args.arg3); 91 } finally { 92 args.recycle(); 93 } 94 break; 95 } 96 case MSG_SET_ACTIVE: 97 mDelegate.setActive((String) msg.obj); 98 break; 99 case MSG_SET_RINGING: 100 mDelegate.setRinging((String) msg.obj); 101 break; 102 case MSG_SET_DIALING: 103 mDelegate.setDialing((String) msg.obj); 104 break; 105 case MSG_SET_PULLING: 106 mDelegate.setPulling((String) msg.obj); 107 break; 108 case MSG_SET_DISCONNECTED: { 109 SomeArgs args = (SomeArgs) msg.obj; 110 try { 111 mDelegate.setDisconnected((String) args.arg1, (DisconnectCause) args.arg2); 112 } finally { 113 args.recycle(); 114 } 115 break; 116 } 117 case MSG_SET_ON_HOLD: 118 mDelegate.setOnHold((String) msg.obj); 119 break; 120 case MSG_SET_RINGBACK_REQUESTED: 121 mDelegate.setRingbackRequested((String) msg.obj, msg.arg1 == 1); 122 break; 123 case MSG_SET_CONNECTION_CAPABILITIES: 124 mDelegate.setConnectionCapabilities((String) msg.obj, msg.arg1); 125 break; 126 case MSG_SET_CONNECTION_PROPERTIES: 127 mDelegate.setConnectionProperties((String) msg.obj, msg.arg1); 128 break; 129 case MSG_SET_IS_CONFERENCED: { 130 SomeArgs args = (SomeArgs) msg.obj; 131 try { 132 mDelegate.setIsConferenced((String) args.arg1, (String) args.arg2); 133 } finally { 134 args.recycle(); 135 } 136 break; 137 } 138 case MSG_ADD_CONFERENCE_CALL: { 139 SomeArgs args = (SomeArgs) msg.obj; 140 try { 141 mDelegate.addConferenceCall( 142 (String) args.arg1, (ParcelableConference) args.arg2); 143 } finally { 144 args.recycle(); 145 } 146 break; 147 } 148 case MSG_REMOVE_CALL: 149 mDelegate.removeCall((String) msg.obj); 150 break; 151 case MSG_ON_POST_DIAL_WAIT: { 152 SomeArgs args = (SomeArgs) msg.obj; 153 try { 154 mDelegate.onPostDialWait((String) args.arg1, (String) args.arg2); 155 } finally { 156 args.recycle(); 157 } 158 break; 159 } 160 case MSG_ON_POST_DIAL_CHAR: { 161 SomeArgs args = (SomeArgs) msg.obj; 162 try { 163 mDelegate.onPostDialChar((String) args.arg1, (char) args.argi1); 164 } finally { 165 args.recycle(); 166 } 167 break; 168 } 169 case MSG_QUERY_REMOTE_CALL_SERVICES: 170 mDelegate.queryRemoteConnectionServices((RemoteServiceCallback) msg.obj); 171 break; 172 case MSG_SET_VIDEO_STATE: 173 mDelegate.setVideoState((String) msg.obj, msg.arg1); 174 break; 175 case MSG_SET_VIDEO_CALL_PROVIDER: { 176 SomeArgs args = (SomeArgs) msg.obj; 177 try { 178 mDelegate.setVideoProvider((String) args.arg1, 179 (IVideoProvider) args.arg2); 180 } finally { 181 args.recycle(); 182 } 183 break; 184 } 185 case MSG_SET_IS_VOIP_AUDIO_MODE: 186 mDelegate.setIsVoipAudioMode((String) msg.obj, msg.arg1 == 1); 187 break; 188 case MSG_SET_STATUS_HINTS: { 189 SomeArgs args = (SomeArgs) msg.obj; 190 try { 191 mDelegate.setStatusHints((String) args.arg1, (StatusHints) args.arg2); 192 } finally { 193 args.recycle(); 194 } 195 break; 196 } 197 case MSG_SET_ADDRESS: { 198 SomeArgs args = (SomeArgs) msg.obj; 199 try { 200 mDelegate.setAddress((String) args.arg1, (Uri) args.arg2, args.argi1); 201 } finally { 202 args.recycle(); 203 } 204 break; 205 } 206 case MSG_SET_CALLER_DISPLAY_NAME: { 207 SomeArgs args = (SomeArgs) msg.obj; 208 try { 209 mDelegate.setCallerDisplayName( 210 (String) args.arg1, (String) args.arg2, args.argi1); 211 } finally { 212 args.recycle(); 213 } 214 break; 215 } 216 case MSG_SET_CONFERENCEABLE_CONNECTIONS: { 217 SomeArgs args = (SomeArgs) msg.obj; 218 try { 219 mDelegate.setConferenceableConnections( 220 (String) args.arg1, (List<String>) args.arg2); 221 } finally { 222 args.recycle(); 223 } 224 break; 225 } 226 case MSG_ADD_EXISTING_CONNECTION: { 227 SomeArgs args = (SomeArgs) msg.obj; 228 try { 229 mDelegate.addExistingConnection( 230 (String) args.arg1, (ParcelableConnection) args.arg2); 231 } finally { 232 args.recycle(); 233 } 234 break; 235 } 236 case MSG_SET_CONFERENCE_MERGE_FAILED: { 237 SomeArgs args = (SomeArgs) msg.obj; 238 try { 239 mDelegate.setConferenceMergeFailed((String) args.arg1); 240 } finally { 241 args.recycle(); 242 } 243 break; 244 } 245 case MSG_PUT_EXTRAS: { 246 SomeArgs args = (SomeArgs) msg.obj; 247 try { 248 mDelegate.putExtras((String) args.arg1, (Bundle) args.arg2); 249 } finally { 250 args.recycle(); 251 } 252 break; 253 } 254 case MSG_REMOVE_EXTRAS: { 255 SomeArgs args = (SomeArgs) msg.obj; 256 try { 257 mDelegate.removeExtras((String) args.arg1, (List<String>) args.arg2); 258 } finally { 259 args.recycle(); 260 } 261 break; 262 } 263 case MSG_ON_CONNECTION_EVENT: { 264 SomeArgs args = (SomeArgs) msg.obj; 265 try { 266 mDelegate.onConnectionEvent((String) args.arg1, (String) args.arg2, 267 (Bundle) args.arg3); 268 } finally { 269 args.recycle(); 270 } 271 break; 272 } 273 } 274 } 275 }; 276 277 private final IConnectionServiceAdapter mStub = new IConnectionServiceAdapter.Stub() { 278 @Override 279 public void handleCreateConnectionComplete( 280 String id, 281 ConnectionRequest request, 282 ParcelableConnection connection) { 283 SomeArgs args = SomeArgs.obtain(); 284 args.arg1 = id; 285 args.arg2 = request; 286 args.arg3 = connection; 287 mHandler.obtainMessage(MSG_HANDLE_CREATE_CONNECTION_COMPLETE, args).sendToTarget(); 288 } 289 290 @Override 291 public void setActive(String connectionId) { 292 mHandler.obtainMessage(MSG_SET_ACTIVE, connectionId).sendToTarget(); 293 } 294 295 @Override 296 public void setRinging(String connectionId) { 297 mHandler.obtainMessage(MSG_SET_RINGING, connectionId).sendToTarget(); 298 } 299 300 @Override 301 public void setDialing(String connectionId) { 302 mHandler.obtainMessage(MSG_SET_DIALING, connectionId).sendToTarget(); 303 } 304 305 @Override 306 public void setPulling(String connectionId) { 307 mHandler.obtainMessage(MSG_SET_PULLING, connectionId).sendToTarget(); 308 } 309 310 @Override 311 public void setDisconnected( 312 String connectionId, DisconnectCause disconnectCause) { 313 SomeArgs args = SomeArgs.obtain(); 314 args.arg1 = connectionId; 315 args.arg2 = disconnectCause; 316 mHandler.obtainMessage(MSG_SET_DISCONNECTED, args).sendToTarget(); 317 } 318 319 @Override 320 public void setOnHold(String connectionId) { 321 mHandler.obtainMessage(MSG_SET_ON_HOLD, connectionId).sendToTarget(); 322 } 323 324 @Override 325 public void setRingbackRequested(String connectionId, boolean ringback) { 326 mHandler.obtainMessage(MSG_SET_RINGBACK_REQUESTED, ringback ? 1 : 0, 0, connectionId) 327 .sendToTarget(); 328 } 329 330 @Override 331 public void setConnectionCapabilities(String connectionId, int connectionCapabilities) { 332 mHandler.obtainMessage( 333 MSG_SET_CONNECTION_CAPABILITIES, connectionCapabilities, 0, connectionId) 334 .sendToTarget(); 335 } 336 337 @Override 338 public void setConnectionProperties(String connectionId, int connectionProperties) { 339 mHandler.obtainMessage( 340 MSG_SET_CONNECTION_PROPERTIES, connectionProperties, 0, connectionId) 341 .sendToTarget(); 342 } 343 344 @Override 345 public void setConferenceMergeFailed(String callId) { 346 SomeArgs args = SomeArgs.obtain(); 347 args.arg1 = callId; 348 mHandler.obtainMessage(MSG_SET_CONFERENCE_MERGE_FAILED, args).sendToTarget(); 349 } 350 351 @Override 352 public void setIsConferenced(String callId, String conferenceCallId) { 353 SomeArgs args = SomeArgs.obtain(); 354 args.arg1 = callId; 355 args.arg2 = conferenceCallId; 356 mHandler.obtainMessage(MSG_SET_IS_CONFERENCED, args).sendToTarget(); 357 } 358 359 @Override 360 public void addConferenceCall(String callId, ParcelableConference parcelableConference) { 361 SomeArgs args = SomeArgs.obtain(); 362 args.arg1 = callId; 363 args.arg2 = parcelableConference; 364 mHandler.obtainMessage(MSG_ADD_CONFERENCE_CALL, args).sendToTarget(); 365 } 366 367 @Override 368 public void removeCall(String connectionId) { 369 mHandler.obtainMessage(MSG_REMOVE_CALL, connectionId).sendToTarget(); 370 } 371 372 @Override 373 public void onPostDialWait(String connectionId, String remainingDigits) { 374 SomeArgs args = SomeArgs.obtain(); 375 args.arg1 = connectionId; 376 args.arg2 = remainingDigits; 377 mHandler.obtainMessage(MSG_ON_POST_DIAL_WAIT, args).sendToTarget(); 378 } 379 380 @Override 381 public void onPostDialChar(String connectionId, char nextChar) { 382 SomeArgs args = SomeArgs.obtain(); 383 args.arg1 = connectionId; 384 args.argi1 = nextChar; 385 mHandler.obtainMessage(MSG_ON_POST_DIAL_CHAR, args).sendToTarget(); 386 } 387 388 @Override 389 public void queryRemoteConnectionServices(RemoteServiceCallback callback) { 390 mHandler.obtainMessage(MSG_QUERY_REMOTE_CALL_SERVICES, callback).sendToTarget(); 391 } 392 393 @Override 394 public void setVideoState(String connectionId, int videoState) { 395 mHandler.obtainMessage(MSG_SET_VIDEO_STATE, videoState, 0, connectionId).sendToTarget(); 396 } 397 398 @Override 399 public void setVideoProvider(String connectionId, IVideoProvider videoProvider) { 400 SomeArgs args = SomeArgs.obtain(); 401 args.arg1 = connectionId; 402 args.arg2 = videoProvider; 403 mHandler.obtainMessage(MSG_SET_VIDEO_CALL_PROVIDER, args).sendToTarget(); 404 } 405 406 @Override 407 public final void setIsVoipAudioMode(String connectionId, boolean isVoip) { 408 mHandler.obtainMessage(MSG_SET_IS_VOIP_AUDIO_MODE, isVoip ? 1 : 0, 0, 409 connectionId).sendToTarget(); 410 } 411 412 @Override 413 public final void setStatusHints(String connectionId, StatusHints statusHints) { 414 SomeArgs args = SomeArgs.obtain(); 415 args.arg1 = connectionId; 416 args.arg2 = statusHints; 417 mHandler.obtainMessage(MSG_SET_STATUS_HINTS, args).sendToTarget(); 418 } 419 420 @Override 421 public final void setAddress(String connectionId, Uri address, int presentation) { 422 SomeArgs args = SomeArgs.obtain(); 423 args.arg1 = connectionId; 424 args.arg2 = address; 425 args.argi1 = presentation; 426 mHandler.obtainMessage(MSG_SET_ADDRESS, args).sendToTarget(); 427 } 428 429 @Override 430 public final void setCallerDisplayName( 431 String connectionId, String callerDisplayName, int presentation) { 432 SomeArgs args = SomeArgs.obtain(); 433 args.arg1 = connectionId; 434 args.arg2 = callerDisplayName; 435 args.argi1 = presentation; 436 mHandler.obtainMessage(MSG_SET_CALLER_DISPLAY_NAME, args).sendToTarget(); 437 } 438 439 @Override 440 public final void setConferenceableConnections( 441 String connectionId, List<String> conferenceableConnectionIds) { 442 SomeArgs args = SomeArgs.obtain(); 443 args.arg1 = connectionId; 444 args.arg2 = conferenceableConnectionIds; 445 mHandler.obtainMessage(MSG_SET_CONFERENCEABLE_CONNECTIONS, args).sendToTarget(); 446 } 447 448 @Override 449 public final void addExistingConnection( 450 String connectionId, ParcelableConnection connection) { 451 SomeArgs args = SomeArgs.obtain(); 452 args.arg1 = connectionId; 453 args.arg2 = connection; 454 mHandler.obtainMessage(MSG_ADD_EXISTING_CONNECTION, args).sendToTarget(); 455 } 456 457 @Override 458 public final void putExtras(String connectionId, Bundle extras) { 459 SomeArgs args = SomeArgs.obtain(); 460 args.arg1 = connectionId; 461 args.arg2 = extras; 462 mHandler.obtainMessage(MSG_PUT_EXTRAS, args).sendToTarget(); 463 } 464 465 @Override 466 public final void removeExtras(String connectionId, List<String> keys) { 467 SomeArgs args = SomeArgs.obtain(); 468 args.arg1 = connectionId; 469 args.arg2 = keys; 470 mHandler.obtainMessage(MSG_REMOVE_EXTRAS, args).sendToTarget(); 471 } 472 473 @Override 474 public final void onConnectionEvent(String connectionId, String event, Bundle extras) { 475 SomeArgs args = SomeArgs.obtain(); 476 args.arg1 = connectionId; 477 args.arg2 = event; 478 args.arg3 = extras; 479 mHandler.obtainMessage(MSG_ON_CONNECTION_EVENT, args).sendToTarget(); 480 } 481 }; 482 483 public ConnectionServiceAdapterServant(IConnectionServiceAdapter delegate) { 484 mDelegate = delegate; 485 } 486 487 public IConnectionServiceAdapter getStub() { 488 return mStub; 489 } 490 } 491