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