blob: 70f0ec2c8588d60c0138eeaf8a73c7056a483c1c [file] [log] [blame]
Santos Cordon63aeb162014-02-10 09:20:40 -08001/*
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
17package com.android.telecomm;
18
Evan Charltona05805b2014-03-05 08:21:46 -080019import android.os.Bundle;
Santos Cordon63aeb162014-02-10 09:20:40 -080020import android.os.IBinder;
21import android.os.RemoteException;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070022import android.telecomm.CallAudioState;
Santos Cordon63aeb162014-02-10 09:20:40 -080023import android.telecomm.CallInfo;
Evan Charltona05805b2014-03-05 08:21:46 -080024import android.telecomm.CallService;
Ben Giladc5b22692014-02-18 20:03:22 -080025import android.telecomm.CallServiceDescriptor;
Sailesh Nepal83cfe7c2014-03-11 19:54:22 -070026import android.telecomm.TelecommConstants;
Sailesh Nepala439e1b2014-03-11 18:19:58 -070027
28import com.android.internal.telecomm.ICallService;
29import com.android.internal.telecomm.ICallServiceAdapter;
30import com.android.internal.telecomm.ICallServiceProvider;
Sailesh Nepal0e5410a2014-04-04 01:20:58 -070031import com.google.common.base.Preconditions;
Santos Cordon63aeb162014-02-10 09:20:40 -080032
33/**
34 * Wrapper for {@link ICallService}s, handles binding to {@link ICallService} and keeps track of
35 * when the object can safely be unbound. Other classes should not use {@link ICallService} directly
36 * and instead should use this class to invoke methods of {@link ICallService}.
Santos Cordon63aeb162014-02-10 09:20:40 -080037 */
Ben Gilad61925612014-03-11 19:06:36 -070038final class CallServiceWrapper extends ServiceBinder<ICallService> {
Santos Cordon63aeb162014-02-10 09:20:40 -080039
Santos Cordonc195e362014-02-11 17:05:31 -080040 /** The descriptor of this call service as supplied by the call-service provider. */
Ben Giladc5b22692014-02-18 20:03:22 -080041 private final CallServiceDescriptor mDescriptor;
Santos Cordonc195e362014-02-11 17:05:31 -080042
43 /**
44 * The adapter used by the underlying call-service implementation to communicate with Telecomm.
45 */
46 private final CallServiceAdapter mAdapter;
47
48 /** The actual service implementation. */
49 private ICallService mServiceInterface;
50
Ben Gilad61925612014-03-11 19:06:36 -070051 private Binder mBinder = new Binder();
Sailesh Nepale59bb192014-04-01 18:33:59 -070052 private final CallIdMapper mCallIdMapper;
Ben Gilad61925612014-03-11 19:06:36 -070053
Santos Cordon63aeb162014-02-10 09:20:40 -080054 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070055 * Creates a call-service for the specified descriptor.
Santos Cordonc195e362014-02-11 17:05:31 -080056 *
Santos Cordon61d0f702014-02-19 02:52:23 -080057 * @param descriptor The call-service descriptor from
58 * {@link ICallServiceProvider#lookupCallServices}.
Sailesh Nepale59bb192014-04-01 18:33:59 -070059 * @param outgoingCallsManager Manages the placing of outgoing calls.
60 * @param incomingCallsManager Manages the incoming call initialization flow.
Santos Cordon63aeb162014-02-10 09:20:40 -080061 */
Sailesh Nepale59bb192014-04-01 18:33:59 -070062 CallServiceWrapper(
63 CallServiceDescriptor descriptor,
64 OutgoingCallsManager outgoingCallsManager,
65 IncomingCallsManager incomingCallsManager) {
Sailesh Nepala439e1b2014-03-11 18:19:58 -070066 super(TelecommConstants.ACTION_CALL_SERVICE, descriptor.getServiceComponent());
Ben Giladc5b22692014-02-18 20:03:22 -080067 mDescriptor = descriptor;
Sailesh Nepale59bb192014-04-01 18:33:59 -070068 mCallIdMapper = new CallIdMapper("CallService");
69 mAdapter = new CallServiceAdapter(outgoingCallsManager, incomingCallsManager,
70 mCallIdMapper);
Santos Cordon63aeb162014-02-10 09:20:40 -080071 }
72
Ben Gilad61925612014-03-11 19:06:36 -070073 CallServiceDescriptor getDescriptor() {
Ben Giladc5b22692014-02-18 20:03:22 -080074 return mDescriptor;
Santos Cordonc195e362014-02-11 17:05:31 -080075 }
76
Santos Cordon63aeb162014-02-10 09:20:40 -080077 /** See {@link ICallService#setCallServiceAdapter}. */
Sailesh Nepale59bb192014-04-01 18:33:59 -070078 private void setCallServiceAdapter(ICallServiceAdapter callServiceAdapter) {
Santos Cordon61d0f702014-02-19 02:52:23 -080079 if (isServiceValid("setCallServiceAdapter")) {
80 try {
Santos Cordon63aeb162014-02-10 09:20:40 -080081 mServiceInterface.setCallServiceAdapter(callServiceAdapter);
Santos Cordon61d0f702014-02-19 02:52:23 -080082 } catch (RemoteException e) {
Santos Cordon63aeb162014-02-10 09:20:40 -080083 }
Santos Cordon63aeb162014-02-10 09:20:40 -080084 }
85 }
86
Ben Gilad61925612014-03-11 19:06:36 -070087 /**
88 * Checks whether or not the specified call is compatible with this call-service implementation,
89 * see {@link ICallService#isCompatibleWith}. Upon failure, the specified error callback is
90 * invoked. Can be invoked even when the call service is unbound.
91 *
Ben Gilad61925612014-03-11 19:06:36 -070092 * @param errorCallback The callback to invoke upon failure.
93 */
Sailesh Nepale59bb192014-04-01 18:33:59 -070094 void isCompatibleWith(final Call call, final Runnable errorCallback) {
95 Log.d(this, "isCompatibleWith(%s) via %s.", call, getComponentName());
Ben Gilad61925612014-03-11 19:06:36 -070096 BindCallback callback = new BindCallback() {
97 @Override public void onSuccess() {
98 if (isServiceValid("isCompatibleWith")) {
99 try {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700100 mAdapter.addPendingCall(call);
101 CallInfo callInfo = call.toCallInfo(mCallIdMapper.getCallId(call));
Ben Gilad61925612014-03-11 19:06:36 -0700102 mServiceInterface.isCompatibleWith(callInfo);
103 } catch (RemoteException e) {
Ben Gilad61925612014-03-11 19:06:36 -0700104 }
105 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800106 }
Ben Gilad61925612014-03-11 19:06:36 -0700107 @Override public void onFailure() {
108 errorCallback.run();
109 }
110 };
111
112 mBinder.bind(callback);
Santos Cordon63aeb162014-02-10 09:20:40 -0800113 }
114
Ben Gilad61925612014-03-11 19:06:36 -0700115 /**
116 * Attempts to place the specified call, see {@link ICallService#call}. Upon failure, the
117 * specified error callback is invoked. Can be invoked even when the call service is unbound.
Ben Gilad61925612014-03-11 19:06:36 -0700118 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700119 void call(Call call) {
120 Log.d(this, "call(%s) via %s.", call, getComponentName());
121 if (isServiceValid("call")) {
122 try {
123 CallInfo callInfo = call.toCallInfo(mCallIdMapper.getCallId(call));
124 mServiceInterface.call(callInfo);
125 } catch (RemoteException e) {
Ben Gilad28e8ad62014-03-06 17:01:54 -0800126 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700127 }
Ben Gilad28e8ad62014-03-06 17:01:54 -0800128 }
129
Ihab Awad74549ec2014-03-10 15:33:25 -0700130 /** @see CallService#abort(String) */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700131 void abort(Call call) {
132 mAdapter.removePendingCall(call);
Ben Gilad28e8ad62014-03-06 17:01:54 -0800133 if (isServiceValid("abort")) {
134 try {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700135 mServiceInterface.abort(mCallIdMapper.getCallId(call));
Ben Gilad28e8ad62014-03-06 17:01:54 -0800136 } catch (RemoteException e) {
Santos Cordon63aeb162014-02-10 09:20:40 -0800137 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800138 }
139 }
140
Ihab Awad74549ec2014-03-10 15:33:25 -0700141 /** @see CallService#hold(String) */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700142 void hold(Call call) {
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700143 if (isServiceValid("hold")) {
144 try {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700145 mServiceInterface.hold(mCallIdMapper.getCallId(call));
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700146 } catch (RemoteException e) {
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700147 }
148 }
149 }
150
Ihab Awad74549ec2014-03-10 15:33:25 -0700151 /** @see CallService#unhold(String) */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700152 void unhold(Call call) {
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700153 if (isServiceValid("unhold")) {
154 try {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700155 mServiceInterface.unhold(mCallIdMapper.getCallId(call));
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700156 } catch (RemoteException e) {
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700157 }
158 }
159 }
160
Ihab Awad74549ec2014-03-10 15:33:25 -0700161 /** @see CallService#onAudioStateChanged(String,CallAudioState) */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700162 void onAudioStateChanged(Call activeCall, CallAudioState audioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700163 if (isServiceValid("onAudioStateChanged")) {
164 try {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700165 mServiceInterface.onAudioStateChanged(mCallIdMapper.getCallId(activeCall),
166 audioState);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700167 } catch (RemoteException e) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700168 }
169 }
170 }
171
Ben Gilad61925612014-03-11 19:06:36 -0700172 /**
173 * Starts retrieval of details for an incoming call. Details are returned through the
174 * call-service adapter using the specified call ID. Upon failure, the specified error callback
175 * is invoked. Can be invoked even when the call service is unbound.
176 * See {@link ICallService#setIncomingCallId}.
177 *
Sailesh Nepale59bb192014-04-01 18:33:59 -0700178 * @param call The call used for the incoming call.
Ben Gilad61925612014-03-11 19:06:36 -0700179 * @param extras The {@link CallService}-provided extras which need to be sent back.
180 * @param errorCallback The callback to invoke upon failure.
181 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700182 void setIncomingCallId(final Call call, final Bundle extras, final Runnable errorCallback) {
183 Log.d(this, "setIncomingCall(%s) via %s.", call, getComponentName());
Ben Gilad61925612014-03-11 19:06:36 -0700184 BindCallback callback = new BindCallback() {
185 @Override public void onSuccess() {
186 if (isServiceValid("setIncomingCallId")) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700187 mAdapter.addPendingCall(call);
Ben Gilad61925612014-03-11 19:06:36 -0700188 try {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700189 mServiceInterface.setIncomingCallId(mCallIdMapper.getCallId(call),
190 extras);
Ben Gilad61925612014-03-11 19:06:36 -0700191 } catch (RemoteException e) {
Ben Gilad61925612014-03-11 19:06:36 -0700192 }
193 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800194 }
Ben Gilad61925612014-03-11 19:06:36 -0700195 @Override public void onFailure() {
196 errorCallback.run();
197 }
198 };
199
200 mBinder.bind(callback);
Santos Cordon63aeb162014-02-10 09:20:40 -0800201 }
202
Ihab Awad74549ec2014-03-10 15:33:25 -0700203 /** @see CallService#disconnect(String) */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700204 void disconnect(Call call) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800205 if (isServiceValid("disconnect")) {
206 try {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700207 mServiceInterface.disconnect(mCallIdMapper.getCallId(call));
Santos Cordon61d0f702014-02-19 02:52:23 -0800208 } catch (RemoteException e) {
Santos Cordon63aeb162014-02-10 09:20:40 -0800209 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800210 }
211 }
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800212
Ihab Awad74549ec2014-03-10 15:33:25 -0700213 /** @see CallService#answer(String) */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700214 void answer(Call call) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800215 if (isServiceValid("answer")) {
216 try {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700217 mServiceInterface.answer(mCallIdMapper.getCallId(call));
Santos Cordon61d0f702014-02-19 02:52:23 -0800218 } catch (RemoteException e) {
Santos Cordon7917d382014-02-14 02:31:18 -0800219 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800220 }
221 }
222
Ihab Awad74549ec2014-03-10 15:33:25 -0700223 /** @see CallService#reject(String) */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700224 void reject(Call call) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800225 if (isServiceValid("reject")) {
226 try {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700227 mServiceInterface.reject(mCallIdMapper.getCallId(call));
Santos Cordon61d0f702014-02-19 02:52:23 -0800228 } catch (RemoteException e) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700229 }
230 }
231 }
232
233 /** @see CallService#playDtmfTone(String,char) */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700234 void playDtmfTone(Call call, char digit) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700235 if (isServiceValid("playDtmfTone")) {
236 try {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700237 mServiceInterface.playDtmfTone(mCallIdMapper.getCallId(call), digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700238 } catch (RemoteException e) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700239 }
240 }
241 }
242
243 /** @see CallService#stopDtmfTone(String) */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700244 void stopDtmfTone(Call call) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700245 if (isServiceValid("stopDtmfTone")) {
246 try {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700247 mServiceInterface.stopDtmfTone(mCallIdMapper.getCallId(call));
Ihab Awad74549ec2014-03-10 15:33:25 -0700248 } catch (RemoteException e) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800249 }
Santos Cordon7917d382014-02-14 02:31:18 -0800250 }
251 }
252
Sailesh Nepale59bb192014-04-01 18:33:59 -0700253 void addCall(Call call) {
254 mCallIdMapper.addCall(call);
Santos Cordon7917d382014-02-14 02:31:18 -0800255 }
256
Sailesh Nepal0e5410a2014-04-04 01:20:58 -0700257 /**
258 * Associates newCall with this call service by replacing callToReplace.
259 */
260 void replaceCall(Call newCall, Call callToReplace) {
261 Preconditions.checkState(callToReplace.getCallService() == this);
262 mCallIdMapper.replaceCall(newCall, callToReplace);
263 }
264
Sailesh Nepale59bb192014-04-01 18:33:59 -0700265 void removeCall(Call call) {
266 mAdapter.removePendingCall(call);
267 mCallIdMapper.removeCall(call);
Yorke Leeadee12d2014-03-13 12:08:30 -0700268 }
269
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800270 /** {@inheritDoc} */
271 @Override protected void setServiceInterface(IBinder binder) {
Santos Cordon4b2c1192014-03-19 18:15:38 -0700272 if (binder == null) {
273 // We have lost our service connection. Notify the world that this call service is done.
274 // We must notify the adapter before CallsManager. The adapter will force any pending
275 // outgoing calls to try the next call service. This needs to happen before CallsManager
276 // tries to clean up any calls still associated with this call service.
277 mAdapter.handleCallServiceDeath();
278 CallsManager.getInstance().handleCallServiceDeath(this);
279 mServiceInterface = null;
280 } else {
281 mServiceInterface = ICallService.Stub.asInterface(binder);
282 setCallServiceAdapter(mAdapter);
283 }
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800284 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800285}