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