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