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 | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 20 | import android.os.IBinder; |
| 21 | import android.os.RemoteException; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame^] | 22 | import android.telecomm.CallAudioState; |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 23 | import android.telecomm.CallInfo; |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 24 | import android.telecomm.CallService; |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 25 | import android.telecomm.CallServiceDescriptor; |
Sailesh Nepal | 83cfe7c | 2014-03-11 19:54:22 -0700 | [diff] [blame] | 26 | import android.telecomm.TelecommConstants; |
Sailesh Nepal | a439e1b | 2014-03-11 18:19:58 -0700 | [diff] [blame] | 27 | |
| 28 | import com.android.internal.telecomm.ICallService; |
| 29 | import com.android.internal.telecomm.ICallServiceAdapter; |
| 30 | import com.android.internal.telecomm.ICallServiceProvider; |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Wrapper for {@link ICallService}s, handles binding to {@link ICallService} and keeps track of |
| 34 | * when the object can safely be unbound. Other classes should not use {@link ICallService} directly |
| 35 | * and instead should use this class to invoke methods of {@link ICallService}. |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 36 | */ |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 37 | final class CallServiceWrapper extends ServiceBinder<ICallService> { |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 38 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 39 | /** The descriptor of this call service as supplied by the call-service provider. */ |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 40 | private final CallServiceDescriptor mDescriptor; |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * The adapter used by the underlying call-service implementation to communicate with Telecomm. |
| 44 | */ |
| 45 | private final CallServiceAdapter mAdapter; |
| 46 | |
| 47 | /** The actual service implementation. */ |
| 48 | private ICallService mServiceInterface; |
| 49 | |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 50 | private Binder mBinder = new Binder(); |
| 51 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 52 | /** |
Sailesh Nepal | 18386a8 | 2014-03-19 10:22:40 -0700 | [diff] [blame] | 53 | * Creates a call-service provider for the specified descriptor. |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 54 | * |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 55 | * @param descriptor The call-service descriptor from |
| 56 | * {@link ICallServiceProvider#lookupCallServices}. |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 57 | * @param adapter The call-service adapter. |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 58 | */ |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 59 | CallServiceWrapper(CallServiceDescriptor descriptor, CallServiceAdapter adapter) { |
Sailesh Nepal | a439e1b | 2014-03-11 18:19:58 -0700 | [diff] [blame] | 60 | super(TelecommConstants.ACTION_CALL_SERVICE, descriptor.getServiceComponent()); |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 61 | mDescriptor = descriptor; |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 62 | mAdapter = adapter; |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 65 | CallServiceDescriptor getDescriptor() { |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 66 | return mDescriptor; |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 69 | /** See {@link ICallService#setCallServiceAdapter}. */ |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 70 | void setCallServiceAdapter(ICallServiceAdapter callServiceAdapter) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 71 | if (isServiceValid("setCallServiceAdapter")) { |
| 72 | try { |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 73 | mServiceInterface.setCallServiceAdapter(callServiceAdapter); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 74 | } catch (RemoteException e) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 75 | Log.e(this, e, "Failed to setCallServiceAdapter."); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 76 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Checks whether or not the specified call is compatible with this call-service implementation, |
| 82 | * see {@link ICallService#isCompatibleWith}. Upon failure, the specified error callback is |
| 83 | * invoked. Can be invoked even when the call service is unbound. |
| 84 | * |
| 85 | * @param callInfo The details of the call. |
| 86 | * @param errorCallback The callback to invoke upon failure. |
| 87 | */ |
| 88 | void isCompatibleWith(final CallInfo callInfo, final Runnable errorCallback) { |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 89 | Log.d(this, "isCompatibleWith(%s) via %s.", callInfo, getComponentName()); |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 90 | BindCallback callback = new BindCallback() { |
| 91 | @Override public void onSuccess() { |
| 92 | if (isServiceValid("isCompatibleWith")) { |
| 93 | try { |
Yorke Lee | adee12d | 2014-03-13 12:08:30 -0700 | [diff] [blame] | 94 | mAdapter.addPendingOutgoingCallId(callInfo.getId()); |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 95 | mServiceInterface.isCompatibleWith(callInfo); |
| 96 | } catch (RemoteException e) { |
Yorke Lee | adee12d | 2014-03-13 12:08:30 -0700 | [diff] [blame] | 97 | mAdapter.removePendingOutgoingCallId(callInfo.getId()); |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 98 | Log.e(CallServiceWrapper.this, e, "Failed checking isCompatibleWith."); |
| 99 | } |
| 100 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 101 | } |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 102 | @Override public void onFailure() { |
| 103 | errorCallback.run(); |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | mBinder.bind(callback); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 110 | /** |
| 111 | * Attempts to place the specified call, see {@link ICallService#call}. Upon failure, the |
| 112 | * specified error callback is invoked. Can be invoked even when the call service is unbound. |
| 113 | * |
| 114 | * @param callInfo The details of the call. |
| 115 | * @param errorCallback The callback to invoke upon failure. |
| 116 | */ |
| 117 | void call(final CallInfo callInfo, final Runnable errorCallback) { |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 118 | Log.d(this, "call(%s) via %s.", callInfo, getComponentName()); |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 119 | BindCallback callback = new BindCallback() { |
| 120 | @Override public void onSuccess() { |
| 121 | String callId = callInfo.getId(); |
| 122 | if (isServiceValid("call")) { |
| 123 | try { |
| 124 | mServiceInterface.call(callInfo); |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 125 | } catch (RemoteException e) { |
| 126 | Log.e(CallServiceWrapper.this, e, "Failed to place call %s", callId); |
| 127 | } |
| 128 | } |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 129 | } |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 130 | @Override public void onFailure() { |
| 131 | errorCallback.run(); |
| 132 | } |
| 133 | }; |
| 134 | |
| 135 | mBinder.bind(callback); |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /** See {@link ICallService#abort}. */ |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 139 | void abort(String callId) { |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 140 | mAdapter.removePendingOutgoingCallId(callId); |
| 141 | if (isServiceValid("abort")) { |
| 142 | try { |
| 143 | mServiceInterface.abort(callId); |
| 144 | } catch (RemoteException e) { |
| 145 | Log.e(this, e, "Failed to abort call %s", callId); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 146 | } |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 150 | /** See {@link ICallService#hold}. */ |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame^] | 151 | void hold(String callId) { |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 152 | if (isServiceValid("hold")) { |
| 153 | try { |
| 154 | mServiceInterface.hold(callId); |
| 155 | } catch (RemoteException e) { |
| 156 | Log.e(this, e, "Failed to put on hold for call %s", callId); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /** See {@link ICallService#unhold}. */ |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame^] | 162 | void unhold(String callId) { |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 163 | if (isServiceValid("unhold")) { |
| 164 | try { |
| 165 | mServiceInterface.unhold(callId); |
| 166 | } catch (RemoteException e) { |
| 167 | Log.e(this, e, "Failed to remove from hold for call %s", callId); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame^] | 172 | /** See {@link ICallService#onAudioStateChanged}. */ |
| 173 | void onAudioStateChanged(String activeCallId, CallAudioState audioState) { |
| 174 | if (isServiceValid("onAudioStateChanged")) { |
| 175 | try { |
| 176 | mServiceInterface.onAudioStateChanged(activeCallId, audioState); |
| 177 | } catch (RemoteException e) { |
| 178 | Log.e(this, e, "Failed to update audio state for call %s", activeCallId); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 183 | /** |
| 184 | * Starts retrieval of details for an incoming call. Details are returned through the |
| 185 | * call-service adapter using the specified call ID. Upon failure, the specified error callback |
| 186 | * is invoked. Can be invoked even when the call service is unbound. |
| 187 | * See {@link ICallService#setIncomingCallId}. |
| 188 | * |
| 189 | * @param callId The call ID used for the incoming call. |
| 190 | * @param extras The {@link CallService}-provided extras which need to be sent back. |
| 191 | * @param errorCallback The callback to invoke upon failure. |
| 192 | */ |
| 193 | void setIncomingCallId( |
| 194 | final String callId, |
| 195 | final Bundle extras, |
| 196 | final Runnable errorCallback) { |
| 197 | |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 198 | Log.d(this, "setIncomingCall(%s) via %s.", callId, getComponentName()); |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 199 | BindCallback callback = new BindCallback() { |
| 200 | @Override public void onSuccess() { |
| 201 | if (isServiceValid("setIncomingCallId")) { |
| 202 | mAdapter.addPendingIncomingCallId(callId); |
| 203 | try { |
| 204 | mServiceInterface.setIncomingCallId(callId, extras); |
| 205 | } catch (RemoteException e) { |
| 206 | Log.e(CallServiceWrapper.this, e, |
| 207 | "Failed to setIncomingCallId for call %s", callId); |
| 208 | mAdapter.removePendingIncomingCallId(callId); |
| 209 | } |
| 210 | } |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 211 | } |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 212 | @Override public void onFailure() { |
| 213 | errorCallback.run(); |
| 214 | } |
| 215 | }; |
| 216 | |
| 217 | mBinder.bind(callback); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /** See {@link ICallService#disconnect}. */ |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 221 | void disconnect(String callId) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 222 | if (isServiceValid("disconnect")) { |
| 223 | try { |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 224 | mServiceInterface.disconnect(callId); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 225 | } catch (RemoteException e) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 226 | Log.e(this, e, "Failed to disconnect call %s", callId); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 227 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 228 | } |
| 229 | } |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 230 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 231 | /** See {@link ICallService#answer}. */ |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 232 | void answer(String callId) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 233 | if (isServiceValid("answer")) { |
| 234 | try { |
| 235 | mServiceInterface.answer(callId); |
| 236 | } catch (RemoteException e) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 237 | Log.e(this, e, "Failed to answer call %s", callId); |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 238 | } |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
| 242 | /** See {@link ICallService#reject}. */ |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 243 | void reject(String callId) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 244 | if (isServiceValid("reject")) { |
| 245 | try { |
| 246 | mServiceInterface.reject(callId); |
| 247 | } catch (RemoteException e) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 248 | Log.e(this, e, "Failed to reject call %s"); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 249 | } |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
| 253 | /** |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 254 | * Cancels the incoming call for the specified call ID. |
| 255 | * TODO(santoscordon): This method should be called by IncomingCallsManager when the incoming |
| 256 | * call has failed. |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 257 | * |
| 258 | * @param callId The ID of the call. |
| 259 | */ |
| 260 | void cancelIncomingCall(String callId) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 261 | mAdapter.removePendingIncomingCallId(callId); |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Yorke Lee | adee12d | 2014-03-13 12:08:30 -0700 | [diff] [blame] | 264 | /** |
| 265 | * Cancels the outgoing call for the specified call ID. |
| 266 | * |
| 267 | * @param callId The ID of the call. |
| 268 | */ |
| 269 | void cancelOutgoingCall(String callId) { |
| 270 | mAdapter.removePendingOutgoingCallId(callId); |
| 271 | } |
| 272 | |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 273 | /** {@inheritDoc} */ |
| 274 | @Override protected void setServiceInterface(IBinder binder) { |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 275 | if (binder == null) { |
| 276 | // We have lost our service connection. Notify the world that this call service is done. |
| 277 | // We must notify the adapter before CallsManager. The adapter will force any pending |
| 278 | // outgoing calls to try the next call service. This needs to happen before CallsManager |
| 279 | // tries to clean up any calls still associated with this call service. |
| 280 | mAdapter.handleCallServiceDeath(); |
| 281 | CallsManager.getInstance().handleCallServiceDeath(this); |
| 282 | mServiceInterface = null; |
| 283 | } else { |
| 284 | mServiceInterface = ICallService.Stub.asInterface(binder); |
| 285 | setCallServiceAdapter(mAdapter); |
| 286 | } |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 287 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 288 | } |