Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.telecomm; |
| 18 | |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 19 | import android.os.Bundle; |
Santos Cordon | 3d3b405 | 2014-05-05 12:05:36 -0700 | [diff] [blame^] | 20 | |
| 21 | import android.os.Handler; |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 22 | import android.os.IBinder; |
Santos Cordon | 3d3b405 | 2014-05-05 12:05:36 -0700 | [diff] [blame^] | 23 | import android.os.Message; |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 24 | import android.os.RemoteException; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 25 | import android.telecomm.CallAudioState; |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 26 | import android.telecomm.CallInfo; |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 27 | import android.telecomm.CallService; |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 28 | import android.telecomm.CallServiceDescriptor; |
Sailesh Nepal | 83cfe7c | 2014-03-11 19:54:22 -0700 | [diff] [blame] | 29 | import android.telecomm.TelecommConstants; |
Sailesh Nepal | a439e1b | 2014-03-11 18:19:58 -0700 | [diff] [blame] | 30 | |
Santos Cordon | 3d3b405 | 2014-05-05 12:05:36 -0700 | [diff] [blame^] | 31 | import com.android.internal.os.SomeArgs; |
Sailesh Nepal | a439e1b | 2014-03-11 18:19:58 -0700 | [diff] [blame] | 32 | import com.android.internal.telecomm.ICallService; |
| 33 | import com.android.internal.telecomm.ICallServiceAdapter; |
| 34 | import com.android.internal.telecomm.ICallServiceProvider; |
Sailesh Nepal | 0e5410a | 2014-04-04 01:20:58 -0700 | [diff] [blame] | 35 | import com.google.common.base.Preconditions; |
Santos Cordon | 3d3b405 | 2014-05-05 12:05:36 -0700 | [diff] [blame^] | 36 | import com.google.common.collect.ImmutableList; |
| 37 | import com.google.common.collect.Sets; |
| 38 | |
| 39 | import java.util.Set; |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Wrapper for {@link ICallService}s, handles binding to {@link ICallService} and keeps track of |
| 43 | * when the object can safely be unbound. Other classes should not use {@link ICallService} directly |
| 44 | * and instead should use this class to invoke methods of {@link ICallService}. |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 45 | */ |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 46 | final class CallServiceWrapper extends ServiceBinder<ICallService> { |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 47 | |
Santos Cordon | 3d3b405 | 2014-05-05 12:05:36 -0700 | [diff] [blame^] | 48 | private final class Adapter extends ICallServiceAdapter.Stub { |
| 49 | private static final int MSG_SET_IS_COMPATIBLE_WITH = 0; |
| 50 | private static final int MSG_NOTIFY_INCOMING_CALL = 1; |
| 51 | private static final int MSG_HANDLE_SUCCESSFUL_OUTGOING_CALL = 2; |
| 52 | private static final int MSG_HANDLE_FAILED_OUTGOING_CALL = 3; |
| 53 | private static final int MSG_SET_ACTIVE = 4; |
| 54 | private static final int MSG_SET_RINGING = 5; |
| 55 | private static final int MSG_SET_DIALING = 6; |
| 56 | private static final int MSG_SET_DISCONNECTED = 7; |
| 57 | private static final int MSG_SET_ON_HOLD = 8; |
| 58 | |
| 59 | private final Handler mHandler = new Handler() { |
| 60 | @Override |
| 61 | public void handleMessage(Message msg) { |
| 62 | Call call; |
| 63 | switch (msg.what) { |
| 64 | case MSG_SET_IS_COMPATIBLE_WITH: |
| 65 | call = mCallIdMapper.getCall(msg.obj); |
| 66 | if (call != null && !call.isIncoming()) { |
| 67 | mOutgoingCallsManager.setIsCompatibleWith(call, |
| 68 | msg.arg1 == 1 ? true : false); |
| 69 | } else { |
| 70 | Log.w(this, "setIsCompatibleWith, unknown call: %s, id: %s", call, |
| 71 | msg.obj); |
| 72 | } |
| 73 | break; |
| 74 | case MSG_NOTIFY_INCOMING_CALL: |
| 75 | CallInfo clientCallInfo = (CallInfo) msg.obj; |
| 76 | call = mCallIdMapper.getCall(clientCallInfo.getId()); |
| 77 | if (call != null && mPendingCalls.remove(call) && call.isIncoming()) { |
| 78 | CallInfo callInfo = new CallInfo(null, clientCallInfo.getState(), |
| 79 | clientCallInfo.getHandle()); |
| 80 | mIncomingCallsManager.handleSuccessfulIncomingCall(call, callInfo); |
| 81 | } else { |
| 82 | Log.w(this, "notifyIncomingCall, unknown incoming call: %s, id: %s", |
| 83 | call, |
| 84 | clientCallInfo.getId()); |
| 85 | } |
| 86 | break; |
| 87 | case MSG_HANDLE_SUCCESSFUL_OUTGOING_CALL: |
| 88 | call = mCallIdMapper.getCall(msg.obj); |
| 89 | if (call != null && mPendingCalls.remove(call) && !call.isIncoming()) { |
| 90 | mOutgoingCallsManager.handleSuccessfulCallAttempt(call); |
| 91 | } else { |
| 92 | // TODO(gilad): Figure out how to wire up the callService.abort() call. |
| 93 | Log.w(this, |
| 94 | "handleSuccessfulOutgoingCall, unknown call: %s, id: %s", |
| 95 | call, msg.obj); |
| 96 | } |
| 97 | break; |
| 98 | case MSG_HANDLE_FAILED_OUTGOING_CALL: { |
| 99 | SomeArgs args = (SomeArgs) msg.obj; |
| 100 | try { |
| 101 | call = mCallIdMapper.getCall(args.arg1); |
| 102 | String reason = (String) args.arg2; |
| 103 | if (call != null && mPendingCalls.remove(call) && !call.isIncoming()) { |
| 104 | mOutgoingCallsManager.handleFailedCallAttempt(call, reason); |
| 105 | } else { |
| 106 | Log.w(this, |
| 107 | "handleFailedOutgoingCall, unknown call: %s, id: %s", |
| 108 | call, args.arg1); |
| 109 | } |
| 110 | } finally { |
| 111 | args.recycle(); |
| 112 | } |
| 113 | break; |
| 114 | } |
| 115 | case MSG_SET_ACTIVE: |
| 116 | call = mCallIdMapper.getCall(msg.obj); |
| 117 | if (call != null) { |
| 118 | mCallsManager.markCallAsActive(call); |
| 119 | } else { |
| 120 | Log.w(this, "setActive, unknown call id: %s", msg.obj); |
| 121 | } |
| 122 | break; |
| 123 | case MSG_SET_RINGING: |
| 124 | call = mCallIdMapper.getCall(msg.obj); |
| 125 | if (call != null) { |
| 126 | mCallsManager.markCallAsRinging(call); |
| 127 | } else { |
| 128 | Log.w(this, "setRinging, unknown call id: %s", msg.obj); |
| 129 | } |
| 130 | break; |
| 131 | case MSG_SET_DIALING: |
| 132 | call = mCallIdMapper.getCall(msg.obj); |
| 133 | if (call != null) { |
| 134 | mCallsManager.markCallAsDialing(call); |
| 135 | } else { |
| 136 | Log.w(this, "setDialing, unknown call id: %s", msg.obj); |
| 137 | } |
| 138 | break; |
| 139 | case MSG_SET_DISCONNECTED: { |
| 140 | SomeArgs args = (SomeArgs) msg.obj; |
| 141 | try { |
| 142 | call = mCallIdMapper.getCall(args.arg1); |
| 143 | String disconnectMessage = (String) args.arg2; |
| 144 | int disconnectCause = args.argi1; |
| 145 | if (call != null) { |
| 146 | mCallsManager.markCallAsDisconnected(call, disconnectCause, |
| 147 | disconnectMessage); |
| 148 | } else { |
| 149 | Log.w(this, "setDisconnected, unknown call id: %s", args.arg1); |
| 150 | } |
| 151 | } finally { |
| 152 | args.recycle(); |
| 153 | } |
| 154 | break; |
| 155 | } |
| 156 | case MSG_SET_ON_HOLD: |
| 157 | call = mCallIdMapper.getCall(msg.obj); |
| 158 | if (call != null) { |
| 159 | mCallsManager.markCallAsOnHold(call); |
| 160 | } else { |
| 161 | Log.w(this, "setOnHold, unknown call id: %s", msg.obj); |
| 162 | } |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | /** {@inheritDoc} */ |
| 169 | @Override |
| 170 | public void setIsCompatibleWith(String callId, boolean isCompatible) { |
| 171 | Log.v(this, "setIsCompatibleWith id: %s, isCompatible: %b", callId, isCompatible); |
| 172 | mCallIdMapper.checkValidCallId(callId); |
| 173 | mHandler.obtainMessage(MSG_SET_IS_COMPATIBLE_WITH, isCompatible ? 1 : 0, 0, callId). |
| 174 | sendToTarget(); |
| 175 | } |
| 176 | |
| 177 | /** {@inheritDoc} */ |
| 178 | @Override |
| 179 | public void notifyIncomingCall(CallInfo callInfo) { |
| 180 | mCallIdMapper.checkValidCallId(callInfo.getId()); |
| 181 | mHandler.obtainMessage(MSG_NOTIFY_INCOMING_CALL, callInfo).sendToTarget(); |
| 182 | } |
| 183 | |
| 184 | /** {@inheritDoc} */ |
| 185 | @Override |
| 186 | public void handleSuccessfulOutgoingCall(String callId) { |
| 187 | mCallIdMapper.checkValidCallId(callId); |
| 188 | mHandler.obtainMessage(MSG_HANDLE_SUCCESSFUL_OUTGOING_CALL, callId).sendToTarget(); |
| 189 | } |
| 190 | |
| 191 | /** {@inheritDoc} */ |
| 192 | @Override |
| 193 | public void handleFailedOutgoingCall(String callId, String reason) { |
| 194 | mCallIdMapper.checkValidCallId(callId); |
| 195 | SomeArgs args = SomeArgs.obtain(); |
| 196 | args.arg1 = callId; |
| 197 | args.arg2 = reason; |
| 198 | mHandler.obtainMessage(MSG_HANDLE_FAILED_OUTGOING_CALL, args).sendToTarget(); |
| 199 | } |
| 200 | |
| 201 | /** {@inheritDoc} */ |
| 202 | @Override |
| 203 | public void setActive(String callId) { |
| 204 | mCallIdMapper.checkValidCallId(callId); |
| 205 | mHandler.obtainMessage(MSG_SET_ACTIVE, callId).sendToTarget(); |
| 206 | } |
| 207 | |
| 208 | /** {@inheritDoc} */ |
| 209 | @Override |
| 210 | public void setRinging(String callId) { |
| 211 | mCallIdMapper.checkValidCallId(callId); |
| 212 | mHandler.obtainMessage(MSG_SET_RINGING, callId).sendToTarget(); |
| 213 | } |
| 214 | |
| 215 | /** {@inheritDoc} */ |
| 216 | @Override |
| 217 | public void setDialing(String callId) { |
| 218 | mCallIdMapper.checkValidCallId(callId); |
| 219 | mHandler.obtainMessage(MSG_SET_DIALING, callId).sendToTarget(); |
| 220 | } |
| 221 | |
| 222 | /** {@inheritDoc} */ |
| 223 | @Override |
| 224 | public void setDisconnected( |
| 225 | String callId, int disconnectCause, String disconnectMessage) { |
| 226 | mCallIdMapper.checkValidCallId(callId); |
| 227 | SomeArgs args = SomeArgs.obtain(); |
| 228 | args.arg1 = callId; |
| 229 | args.arg2 = disconnectMessage; |
| 230 | args.argi1 = disconnectCause; |
| 231 | mHandler.obtainMessage(MSG_SET_DISCONNECTED, args).sendToTarget(); |
| 232 | } |
| 233 | |
| 234 | /** {@inheritDoc} */ |
| 235 | @Override |
| 236 | public void setOnHold(String callId) { |
| 237 | mCallIdMapper.checkValidCallId(callId); |
| 238 | mHandler.obtainMessage(MSG_SET_ON_HOLD, callId).sendToTarget(); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | private final Adapter mAdapter = new Adapter(); |
| 243 | private final CallsManager mCallsManager = CallsManager.getInstance(); |
| 244 | private final Set<Call> mPendingCalls = Sets.newHashSet(); |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 245 | private final CallServiceDescriptor mDescriptor; |
Santos Cordon | 3d3b405 | 2014-05-05 12:05:36 -0700 | [diff] [blame^] | 246 | private final CallIdMapper mCallIdMapper = new CallIdMapper("CallService"); |
| 247 | private final OutgoingCallsManager mOutgoingCallsManager; |
| 248 | private final IncomingCallsManager mIncomingCallsManager; |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 249 | |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 250 | private Binder mBinder = new Binder(); |
Santos Cordon | 3d3b405 | 2014-05-05 12:05:36 -0700 | [diff] [blame^] | 251 | private ICallService mServiceInterface; |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 252 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 253 | /** |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 254 | * Creates a call-service for the specified descriptor. |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 255 | * |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 256 | * @param descriptor The call-service descriptor from |
Santos Cordon | 3d3b405 | 2014-05-05 12:05:36 -0700 | [diff] [blame^] | 257 | * {@link ICallServiceProvider#lookupCallServices}. |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 258 | * @param outgoingCallsManager Manages the placing of outgoing calls. |
| 259 | * @param incomingCallsManager Manages the incoming call initialization flow. |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 260 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 261 | CallServiceWrapper( |
| 262 | CallServiceDescriptor descriptor, |
| 263 | OutgoingCallsManager outgoingCallsManager, |
| 264 | IncomingCallsManager incomingCallsManager) { |
Sailesh Nepal | a439e1b | 2014-03-11 18:19:58 -0700 | [diff] [blame] | 265 | super(TelecommConstants.ACTION_CALL_SERVICE, descriptor.getServiceComponent()); |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 266 | mDescriptor = descriptor; |
Santos Cordon | 3d3b405 | 2014-05-05 12:05:36 -0700 | [diff] [blame^] | 267 | mIncomingCallsManager = incomingCallsManager; |
| 268 | mOutgoingCallsManager = outgoingCallsManager; |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 269 | } |
| 270 | |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 271 | CallServiceDescriptor getDescriptor() { |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 272 | return mDescriptor; |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 275 | /** See {@link ICallService#setCallServiceAdapter}. */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 276 | private void setCallServiceAdapter(ICallServiceAdapter callServiceAdapter) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 277 | if (isServiceValid("setCallServiceAdapter")) { |
| 278 | try { |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 279 | mServiceInterface.setCallServiceAdapter(callServiceAdapter); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 280 | } catch (RemoteException e) { |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 281 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 285 | /** |
| 286 | * Checks whether or not the specified call is compatible with this call-service implementation, |
Santos Cordon | 3d3b405 | 2014-05-05 12:05:36 -0700 | [diff] [blame^] | 287 | * see {@link ICallService#isCompatibleWith}. Upon failure, the specified error callback is |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 288 | * invoked. Can be invoked even when the call service is unbound. |
| 289 | * |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 290 | * @param errorCallback The callback to invoke upon failure. |
| 291 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 292 | void isCompatibleWith(final Call call, final Runnable errorCallback) { |
| 293 | Log.d(this, "isCompatibleWith(%s) via %s.", call, getComponentName()); |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 294 | BindCallback callback = new BindCallback() { |
Santos Cordon | 3d3b405 | 2014-05-05 12:05:36 -0700 | [diff] [blame^] | 295 | @Override |
| 296 | public void onSuccess() { |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 297 | if (isServiceValid("isCompatibleWith")) { |
| 298 | try { |
Santos Cordon | 3d3b405 | 2014-05-05 12:05:36 -0700 | [diff] [blame^] | 299 | mPendingCalls.add(call); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 300 | CallInfo callInfo = call.toCallInfo(mCallIdMapper.getCallId(call)); |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 301 | mServiceInterface.isCompatibleWith(callInfo); |
| 302 | } catch (RemoteException e) { |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 303 | } |
| 304 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 305 | } |
Santos Cordon | 3d3b405 | 2014-05-05 12:05:36 -0700 | [diff] [blame^] | 306 | |
| 307 | @Override |
| 308 | public void onFailure() { |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 309 | errorCallback.run(); |
| 310 | } |
| 311 | }; |
| 312 | |
| 313 | mBinder.bind(callback); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 316 | /** |
Santos Cordon | 3d3b405 | 2014-05-05 12:05:36 -0700 | [diff] [blame^] | 317 | * Attempts to place the specified call, see {@link ICallService#call}. Upon failure, the |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 318 | * specified error callback is invoked. Can be invoked even when the call service is unbound. |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 319 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 320 | void call(Call call) { |
| 321 | Log.d(this, "call(%s) via %s.", call, getComponentName()); |
| 322 | if (isServiceValid("call")) { |
| 323 | try { |
| 324 | CallInfo callInfo = call.toCallInfo(mCallIdMapper.getCallId(call)); |
| 325 | mServiceInterface.call(callInfo); |
| 326 | } catch (RemoteException e) { |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 327 | } |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 328 | } |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 331 | /** @see CallService#abort(String) */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 332 | void abort(Call call) { |
Santos Cordon | 3d3b405 | 2014-05-05 12:05:36 -0700 | [diff] [blame^] | 333 | mPendingCalls.remove(call); |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 334 | if (isServiceValid("abort")) { |
| 335 | try { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 336 | mServiceInterface.abort(mCallIdMapper.getCallId(call)); |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 337 | } catch (RemoteException e) { |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 338 | } |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 342 | /** @see CallService#hold(String) */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 343 | void hold(Call call) { |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 344 | if (isServiceValid("hold")) { |
| 345 | try { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 346 | mServiceInterface.hold(mCallIdMapper.getCallId(call)); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 347 | } catch (RemoteException e) { |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 352 | /** @see CallService#unhold(String) */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 353 | void unhold(Call call) { |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 354 | if (isServiceValid("unhold")) { |
| 355 | try { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 356 | mServiceInterface.unhold(mCallIdMapper.getCallId(call)); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 357 | } catch (RemoteException e) { |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 362 | /** @see CallService#onAudioStateChanged(String,CallAudioState) */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 363 | void onAudioStateChanged(Call activeCall, CallAudioState audioState) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 364 | if (isServiceValid("onAudioStateChanged")) { |
| 365 | try { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 366 | mServiceInterface.onAudioStateChanged(mCallIdMapper.getCallId(activeCall), |
| 367 | audioState); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 368 | } catch (RemoteException e) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 373 | /** |
| 374 | * Starts retrieval of details for an incoming call. Details are returned through the |
| 375 | * call-service adapter using the specified call ID. Upon failure, the specified error callback |
Santos Cordon | 3d3b405 | 2014-05-05 12:05:36 -0700 | [diff] [blame^] | 376 | * is invoked. Can be invoked even when the call service is unbound. See |
| 377 | * {@link ICallService#setIncomingCallId}. |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 378 | * |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 379 | * @param call The call used for the incoming call. |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 380 | * @param extras The {@link CallService}-provided extras which need to be sent back. |
| 381 | * @param errorCallback The callback to invoke upon failure. |
| 382 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 383 | void setIncomingCallId(final Call call, final Bundle extras, final Runnable errorCallback) { |
| 384 | Log.d(this, "setIncomingCall(%s) via %s.", call, getComponentName()); |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 385 | BindCallback callback = new BindCallback() { |
Santos Cordon | 3d3b405 | 2014-05-05 12:05:36 -0700 | [diff] [blame^] | 386 | @Override |
| 387 | public void onSuccess() { |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 388 | if (isServiceValid("setIncomingCallId")) { |
Santos Cordon | 3d3b405 | 2014-05-05 12:05:36 -0700 | [diff] [blame^] | 389 | mPendingCalls.add(call); |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 390 | try { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 391 | mServiceInterface.setIncomingCallId(mCallIdMapper.getCallId(call), |
| 392 | extras); |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 393 | } catch (RemoteException e) { |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 394 | } |
| 395 | } |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 396 | } |
Santos Cordon | 3d3b405 | 2014-05-05 12:05:36 -0700 | [diff] [blame^] | 397 | |
| 398 | @Override |
| 399 | public void onFailure() { |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 400 | errorCallback.run(); |
| 401 | } |
| 402 | }; |
| 403 | |
| 404 | mBinder.bind(callback); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 405 | } |
| 406 | |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 407 | /** @see CallService#disconnect(String) */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 408 | void disconnect(Call call) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 409 | if (isServiceValid("disconnect")) { |
| 410 | try { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 411 | mServiceInterface.disconnect(mCallIdMapper.getCallId(call)); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 412 | } catch (RemoteException e) { |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 413 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 414 | } |
| 415 | } |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 416 | |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 417 | /** @see CallService#answer(String) */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 418 | void answer(Call call) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 419 | if (isServiceValid("answer")) { |
| 420 | try { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 421 | mServiceInterface.answer(mCallIdMapper.getCallId(call)); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 422 | } catch (RemoteException e) { |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 423 | } |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 427 | /** @see CallService#reject(String) */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 428 | void reject(Call call) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 429 | if (isServiceValid("reject")) { |
| 430 | try { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 431 | mServiceInterface.reject(mCallIdMapper.getCallId(call)); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 432 | } catch (RemoteException e) { |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | /** @see CallService#playDtmfTone(String,char) */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 438 | void playDtmfTone(Call call, char digit) { |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 439 | if (isServiceValid("playDtmfTone")) { |
| 440 | try { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 441 | mServiceInterface.playDtmfTone(mCallIdMapper.getCallId(call), digit); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 442 | } catch (RemoteException e) { |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | /** @see CallService#stopDtmfTone(String) */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 448 | void stopDtmfTone(Call call) { |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 449 | if (isServiceValid("stopDtmfTone")) { |
| 450 | try { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 451 | mServiceInterface.stopDtmfTone(mCallIdMapper.getCallId(call)); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 452 | } catch (RemoteException e) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 453 | } |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 457 | void addCall(Call call) { |
| 458 | mCallIdMapper.addCall(call); |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 459 | } |
| 460 | |
Sailesh Nepal | 0e5410a | 2014-04-04 01:20:58 -0700 | [diff] [blame] | 461 | /** |
| 462 | * Associates newCall with this call service by replacing callToReplace. |
| 463 | */ |
| 464 | void replaceCall(Call newCall, Call callToReplace) { |
| 465 | Preconditions.checkState(callToReplace.getCallService() == this); |
| 466 | mCallIdMapper.replaceCall(newCall, callToReplace); |
| 467 | } |
| 468 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 469 | void removeCall(Call call) { |
Santos Cordon | 3d3b405 | 2014-05-05 12:05:36 -0700 | [diff] [blame^] | 470 | mPendingCalls.remove(call); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 471 | mCallIdMapper.removeCall(call); |
Yorke Lee | adee12d | 2014-03-13 12:08:30 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 474 | /** {@inheritDoc} */ |
Santos Cordon | 3d3b405 | 2014-05-05 12:05:36 -0700 | [diff] [blame^] | 475 | @Override |
| 476 | protected void setServiceInterface(IBinder binder) { |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 477 | if (binder == null) { |
| 478 | // We have lost our service connection. Notify the world that this call service is done. |
| 479 | // We must notify the adapter before CallsManager. The adapter will force any pending |
| 480 | // outgoing calls to try the next call service. This needs to happen before CallsManager |
| 481 | // tries to clean up any calls still associated with this call service. |
Santos Cordon | 3d3b405 | 2014-05-05 12:05:36 -0700 | [diff] [blame^] | 482 | handleCallServiceDeath(); |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 483 | CallsManager.getInstance().handleCallServiceDeath(this); |
| 484 | mServiceInterface = null; |
| 485 | } else { |
| 486 | mServiceInterface = ICallService.Stub.asInterface(binder); |
| 487 | setCallServiceAdapter(mAdapter); |
| 488 | } |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 489 | } |
Santos Cordon | 3d3b405 | 2014-05-05 12:05:36 -0700 | [diff] [blame^] | 490 | |
| 491 | /** |
| 492 | * Called when the associated call service dies. |
| 493 | */ |
| 494 | private void handleCallServiceDeath() { |
| 495 | if (!mPendingCalls.isEmpty()) { |
| 496 | // Iterate through a copy because the code inside the loop will modify the original |
| 497 | // list. |
| 498 | for (Call call : ImmutableList.copyOf(mPendingCalls)) { |
| 499 | if (call.isIncoming()) { |
| 500 | mIncomingCallsManager.handleFailedIncomingCall(call); |
| 501 | } else { |
| 502 | mOutgoingCallsManager.handleFailedCallAttempt(call, |
| 503 | "Call service disconnected."); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | if (!mPendingCalls.isEmpty()) { |
| 508 | Log.wtf(this, "Pending calls did not get cleared."); |
| 509 | mPendingCalls.clear(); |
| 510 | } |
| 511 | } |
| 512 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 513 | } |