blob: 5585fc78d6f20d20a6c1d25a74f1c7ef5e001e72 [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;
22import android.telecomm.CallInfo;
Evan Charltona05805b2014-03-05 08:21:46 -080023import android.telecomm.CallService;
Ben Giladc5b22692014-02-18 20:03:22 -080024import android.telecomm.CallServiceDescriptor;
Sailesh Nepal83cfe7c2014-03-11 19:54:22 -070025import android.telecomm.TelecommConstants;
Sailesh Nepala439e1b2014-03-11 18:19:58 -070026
27import com.android.internal.telecomm.ICallService;
28import com.android.internal.telecomm.ICallServiceAdapter;
29import com.android.internal.telecomm.ICallServiceProvider;
Santos Cordon63aeb162014-02-10 09:20:40 -080030
31/**
32 * Wrapper for {@link ICallService}s, handles binding to {@link ICallService} and keeps track of
33 * when the object can safely be unbound. Other classes should not use {@link ICallService} directly
34 * and instead should use this class to invoke methods of {@link ICallService}.
35 * TODO(santoscordon): Keep track of when the service can be safely unbound.
36 * TODO(santoscordon): Look into combining with android.telecomm.CallService.
37 */
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();
52
Santos Cordon63aeb162014-02-10 09:20:40 -080053 /**
54 * Creates a call-service provider for the specified component.
Santos Cordonc195e362014-02-11 17:05:31 -080055 *
Santos Cordon61d0f702014-02-19 02:52:23 -080056 * @param descriptor The call-service descriptor from
57 * {@link ICallServiceProvider#lookupCallServices}.
Santos Cordonc195e362014-02-11 17:05:31 -080058 * @param adapter The call-service adapter.
Santos Cordon63aeb162014-02-10 09:20:40 -080059 */
Ben Gilad61925612014-03-11 19:06:36 -070060 CallServiceWrapper(CallServiceDescriptor descriptor, CallServiceAdapter adapter) {
Sailesh Nepala439e1b2014-03-11 18:19:58 -070061 super(TelecommConstants.ACTION_CALL_SERVICE, descriptor.getServiceComponent());
Ben Giladc5b22692014-02-18 20:03:22 -080062 mDescriptor = descriptor;
Santos Cordonc195e362014-02-11 17:05:31 -080063 mAdapter = adapter;
Santos Cordon63aeb162014-02-10 09:20:40 -080064 }
65
Ben Gilad61925612014-03-11 19:06:36 -070066 CallServiceDescriptor getDescriptor() {
Ben Giladc5b22692014-02-18 20:03:22 -080067 return mDescriptor;
Santos Cordonc195e362014-02-11 17:05:31 -080068 }
69
Santos Cordon63aeb162014-02-10 09:20:40 -080070 /** See {@link ICallService#setCallServiceAdapter}. */
Ben Gilad61925612014-03-11 19:06:36 -070071 void setCallServiceAdapter(ICallServiceAdapter callServiceAdapter) {
Santos Cordon61d0f702014-02-19 02:52:23 -080072 if (isServiceValid("setCallServiceAdapter")) {
73 try {
Santos Cordon63aeb162014-02-10 09:20:40 -080074 mServiceInterface.setCallServiceAdapter(callServiceAdapter);
Santos Cordon61d0f702014-02-19 02:52:23 -080075 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -080076 Log.e(this, e, "Failed to setCallServiceAdapter.");
Santos Cordon63aeb162014-02-10 09:20:40 -080077 }
Santos Cordon63aeb162014-02-10 09:20:40 -080078 }
79 }
80
Ben Gilad61925612014-03-11 19:06:36 -070081 /**
82 * Checks whether or not the specified call is compatible with this call-service implementation,
83 * see {@link ICallService#isCompatibleWith}. Upon failure, the specified error callback is
84 * invoked. Can be invoked even when the call service is unbound.
85 *
86 * @param callInfo The details of the call.
87 * @param errorCallback The callback to invoke upon failure.
88 */
89 void isCompatibleWith(final CallInfo callInfo, final Runnable errorCallback) {
90 BindCallback callback = new BindCallback() {
91 @Override public void onSuccess() {
92 if (isServiceValid("isCompatibleWith")) {
93 try {
Yorke Leeadee12d2014-03-13 12:08:30 -070094 mAdapter.addPendingOutgoingCallId(callInfo.getId());
Ben Gilad61925612014-03-11 19:06:36 -070095 mServiceInterface.isCompatibleWith(callInfo);
96 } catch (RemoteException e) {
Yorke Leeadee12d2014-03-13 12:08:30 -070097 mAdapter.removePendingOutgoingCallId(callInfo.getId());
Ben Gilad61925612014-03-11 19:06:36 -070098 Log.e(CallServiceWrapper.this, e, "Failed checking isCompatibleWith.");
99 }
100 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800101 }
Ben Gilad61925612014-03-11 19:06:36 -0700102 @Override public void onFailure() {
103 errorCallback.run();
104 }
105 };
106
107 mBinder.bind(callback);
Santos Cordon63aeb162014-02-10 09:20:40 -0800108 }
109
Ben Gilad61925612014-03-11 19:06:36 -0700110 /**
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) {
118 BindCallback callback = new BindCallback() {
119 @Override public void onSuccess() {
120 String callId = callInfo.getId();
121 if (isServiceValid("call")) {
122 try {
123 mServiceInterface.call(callInfo);
Ben Gilad61925612014-03-11 19:06:36 -0700124 } catch (RemoteException e) {
125 Log.e(CallServiceWrapper.this, e, "Failed to place call %s", callId);
126 }
127 }
Ben Gilad28e8ad62014-03-06 17:01:54 -0800128 }
Ben Gilad61925612014-03-11 19:06:36 -0700129 @Override public void onFailure() {
130 errorCallback.run();
131 }
132 };
133
134 mBinder.bind(callback);
Ben Gilad28e8ad62014-03-06 17:01:54 -0800135 }
136
137 /** See {@link ICallService#abort}. */
Ben Gilad61925612014-03-11 19:06:36 -0700138 void abort(String callId) {
Ben Gilad28e8ad62014-03-06 17:01:54 -0800139 mAdapter.removePendingOutgoingCallId(callId);
140 if (isServiceValid("abort")) {
141 try {
142 mServiceInterface.abort(callId);
143 } catch (RemoteException e) {
144 Log.e(this, e, "Failed to abort call %s", callId);
Santos Cordon63aeb162014-02-10 09:20:40 -0800145 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800146 }
147 }
148
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700149 /** See {@link ICallService#hold}. */
150 public void hold(String callId) {
151 if (isServiceValid("hold")) {
152 try {
153 mServiceInterface.hold(callId);
154 } catch (RemoteException e) {
155 Log.e(this, e, "Failed to put on hold for call %s", callId);
156 }
157 }
158 }
159
160 /** See {@link ICallService#unhold}. */
161 public void unhold(String callId) {
162 if (isServiceValid("unhold")) {
163 try {
164 mServiceInterface.unhold(callId);
165 } catch (RemoteException e) {
166 Log.e(this, e, "Failed to remove from hold for call %s", callId);
167 }
168 }
169 }
170
Ben Gilad61925612014-03-11 19:06:36 -0700171 /**
172 * Starts retrieval of details for an incoming call. Details are returned through the
173 * call-service adapter using the specified call ID. Upon failure, the specified error callback
174 * is invoked. Can be invoked even when the call service is unbound.
175 * See {@link ICallService#setIncomingCallId}.
176 *
177 * @param callId The call ID used for the incoming call.
178 * @param extras The {@link CallService}-provided extras which need to be sent back.
179 * @param errorCallback The callback to invoke upon failure.
180 */
181 void setIncomingCallId(
182 final String callId,
183 final Bundle extras,
184 final Runnable errorCallback) {
185
186 BindCallback callback = new BindCallback() {
187 @Override public void onSuccess() {
188 if (isServiceValid("setIncomingCallId")) {
189 mAdapter.addPendingIncomingCallId(callId);
190 try {
191 mServiceInterface.setIncomingCallId(callId, extras);
192 } catch (RemoteException e) {
193 Log.e(CallServiceWrapper.this, e,
194 "Failed to setIncomingCallId for call %s", callId);
195 mAdapter.removePendingIncomingCallId(callId);
196 }
197 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800198 }
Ben Gilad61925612014-03-11 19:06:36 -0700199 @Override public void onFailure() {
200 errorCallback.run();
201 }
202 };
203
204 mBinder.bind(callback);
Santos Cordon63aeb162014-02-10 09:20:40 -0800205 }
206
207 /** See {@link ICallService#disconnect}. */
Ben Gilad61925612014-03-11 19:06:36 -0700208 void disconnect(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800209 if (isServiceValid("disconnect")) {
210 try {
Santos Cordon63aeb162014-02-10 09:20:40 -0800211 mServiceInterface.disconnect(callId);
Santos Cordon61d0f702014-02-19 02:52:23 -0800212 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800213 Log.e(this, e, "Failed to disconnect call %s", callId);
Santos Cordon63aeb162014-02-10 09:20:40 -0800214 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800215 }
216 }
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800217
Santos Cordon61d0f702014-02-19 02:52:23 -0800218 /** See {@link ICallService#answer}. */
Ben Gilad61925612014-03-11 19:06:36 -0700219 void answer(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800220 if (isServiceValid("answer")) {
221 try {
222 mServiceInterface.answer(callId);
223 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800224 Log.e(this, e, "Failed to answer call %s", callId);
Santos Cordon7917d382014-02-14 02:31:18 -0800225 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800226 }
227 }
228
229 /** See {@link ICallService#reject}. */
Ben Gilad61925612014-03-11 19:06:36 -0700230 void reject(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800231 if (isServiceValid("reject")) {
232 try {
233 mServiceInterface.reject(callId);
234 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800235 Log.e(this, e, "Failed to reject call %s");
Santos Cordon61d0f702014-02-19 02:52:23 -0800236 }
Santos Cordon7917d382014-02-14 02:31:18 -0800237 }
238 }
239
240 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800241 * Cancels the incoming call for the specified call ID.
242 * TODO(santoscordon): This method should be called by IncomingCallsManager when the incoming
243 * call has failed.
Santos Cordon7917d382014-02-14 02:31:18 -0800244 *
245 * @param callId The ID of the call.
246 */
247 void cancelIncomingCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800248 mAdapter.removePendingIncomingCallId(callId);
Santos Cordon7917d382014-02-14 02:31:18 -0800249 }
250
Yorke Leeadee12d2014-03-13 12:08:30 -0700251 /**
252 * Cancels the outgoing call for the specified call ID.
253 *
254 * @param callId The ID of the call.
255 */
256 void cancelOutgoingCall(String callId) {
257 mAdapter.removePendingOutgoingCallId(callId);
258 }
259
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800260 /** {@inheritDoc} */
261 @Override protected void setServiceInterface(IBinder binder) {
262 mServiceInterface = ICallService.Stub.asInterface(binder);
263 setCallServiceAdapter(mAdapter);
264 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800265}