blob: 426a019e06770a0a832de47c24ca80dd5055562b [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}.
Santos Cordon63aeb162014-02-10 09:20:40 -080035 */
Ben Gilad61925612014-03-11 19:06:36 -070036final class CallServiceWrapper extends ServiceBinder<ICallService> {
Santos Cordon63aeb162014-02-10 09:20:40 -080037
Santos Cordonc195e362014-02-11 17:05:31 -080038 /** The descriptor of this call service as supplied by the call-service provider. */
Ben Giladc5b22692014-02-18 20:03:22 -080039 private final CallServiceDescriptor mDescriptor;
Santos Cordonc195e362014-02-11 17:05:31 -080040
41 /**
42 * The adapter used by the underlying call-service implementation to communicate with Telecomm.
43 */
44 private final CallServiceAdapter mAdapter;
45
46 /** The actual service implementation. */
47 private ICallService mServiceInterface;
48
Ben Gilad61925612014-03-11 19:06:36 -070049 private Binder mBinder = new Binder();
50
Santos Cordon63aeb162014-02-10 09:20:40 -080051 /**
Sailesh Nepal18386a82014-03-19 10:22:40 -070052 * Creates a call-service provider for the specified descriptor.
Santos Cordonc195e362014-02-11 17:05:31 -080053 *
Santos Cordon61d0f702014-02-19 02:52:23 -080054 * @param descriptor The call-service descriptor from
55 * {@link ICallServiceProvider#lookupCallServices}.
Santos Cordonc195e362014-02-11 17:05:31 -080056 * @param adapter The call-service adapter.
Santos Cordon63aeb162014-02-10 09:20:40 -080057 */
Ben Gilad61925612014-03-11 19:06:36 -070058 CallServiceWrapper(CallServiceDescriptor descriptor, CallServiceAdapter adapter) {
Sailesh Nepala439e1b2014-03-11 18:19:58 -070059 super(TelecommConstants.ACTION_CALL_SERVICE, descriptor.getServiceComponent());
Ben Giladc5b22692014-02-18 20:03:22 -080060 mDescriptor = descriptor;
Santos Cordonc195e362014-02-11 17:05:31 -080061 mAdapter = adapter;
Santos Cordon63aeb162014-02-10 09:20:40 -080062 }
63
Ben Gilad61925612014-03-11 19:06:36 -070064 CallServiceDescriptor getDescriptor() {
Ben Giladc5b22692014-02-18 20:03:22 -080065 return mDescriptor;
Santos Cordonc195e362014-02-11 17:05:31 -080066 }
67
Santos Cordon63aeb162014-02-10 09:20:40 -080068 /** See {@link ICallService#setCallServiceAdapter}. */
Ben Gilad61925612014-03-11 19:06:36 -070069 void setCallServiceAdapter(ICallServiceAdapter callServiceAdapter) {
Santos Cordon61d0f702014-02-19 02:52:23 -080070 if (isServiceValid("setCallServiceAdapter")) {
71 try {
Santos Cordon63aeb162014-02-10 09:20:40 -080072 mServiceInterface.setCallServiceAdapter(callServiceAdapter);
Santos Cordon61d0f702014-02-19 02:52:23 -080073 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -080074 Log.e(this, e, "Failed to setCallServiceAdapter.");
Santos Cordon63aeb162014-02-10 09:20:40 -080075 }
Santos Cordon63aeb162014-02-10 09:20:40 -080076 }
77 }
78
Ben Gilad61925612014-03-11 19:06:36 -070079 /**
80 * Checks whether or not the specified call is compatible with this call-service implementation,
81 * see {@link ICallService#isCompatibleWith}. Upon failure, the specified error callback is
82 * invoked. Can be invoked even when the call service is unbound.
83 *
84 * @param callInfo The details of the call.
85 * @param errorCallback The callback to invoke upon failure.
86 */
87 void isCompatibleWith(final CallInfo callInfo, final Runnable errorCallback) {
88 BindCallback callback = new BindCallback() {
89 @Override public void onSuccess() {
90 if (isServiceValid("isCompatibleWith")) {
91 try {
Yorke Leeadee12d2014-03-13 12:08:30 -070092 mAdapter.addPendingOutgoingCallId(callInfo.getId());
Ben Gilad61925612014-03-11 19:06:36 -070093 mServiceInterface.isCompatibleWith(callInfo);
94 } catch (RemoteException e) {
Yorke Leeadee12d2014-03-13 12:08:30 -070095 mAdapter.removePendingOutgoingCallId(callInfo.getId());
Ben Gilad61925612014-03-11 19:06:36 -070096 Log.e(CallServiceWrapper.this, e, "Failed checking isCompatibleWith.");
97 }
98 }
Santos Cordon63aeb162014-02-10 09:20:40 -080099 }
Ben Gilad61925612014-03-11 19:06:36 -0700100 @Override public void onFailure() {
101 errorCallback.run();
102 }
103 };
104
105 mBinder.bind(callback);
Santos Cordon63aeb162014-02-10 09:20:40 -0800106 }
107
Ben Gilad61925612014-03-11 19:06:36 -0700108 /**
109 * Attempts to place the specified call, see {@link ICallService#call}. Upon failure, the
110 * specified error callback is invoked. Can be invoked even when the call service is unbound.
111 *
112 * @param callInfo The details of the call.
113 * @param errorCallback The callback to invoke upon failure.
114 */
115 void call(final CallInfo callInfo, final Runnable errorCallback) {
116 BindCallback callback = new BindCallback() {
117 @Override public void onSuccess() {
118 String callId = callInfo.getId();
119 if (isServiceValid("call")) {
120 try {
121 mServiceInterface.call(callInfo);
Ben Gilad61925612014-03-11 19:06:36 -0700122 } catch (RemoteException e) {
123 Log.e(CallServiceWrapper.this, e, "Failed to place call %s", callId);
124 }
125 }
Ben Gilad28e8ad62014-03-06 17:01:54 -0800126 }
Ben Gilad61925612014-03-11 19:06:36 -0700127 @Override public void onFailure() {
128 errorCallback.run();
129 }
130 };
131
132 mBinder.bind(callback);
Ben Gilad28e8ad62014-03-06 17:01:54 -0800133 }
134
135 /** See {@link ICallService#abort}. */
Ben Gilad61925612014-03-11 19:06:36 -0700136 void abort(String callId) {
Ben Gilad28e8ad62014-03-06 17:01:54 -0800137 mAdapter.removePendingOutgoingCallId(callId);
138 if (isServiceValid("abort")) {
139 try {
140 mServiceInterface.abort(callId);
141 } catch (RemoteException e) {
142 Log.e(this, e, "Failed to abort call %s", callId);
Santos Cordon63aeb162014-02-10 09:20:40 -0800143 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800144 }
145 }
146
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700147 /** See {@link ICallService#hold}. */
148 public void hold(String callId) {
149 if (isServiceValid("hold")) {
150 try {
151 mServiceInterface.hold(callId);
152 } catch (RemoteException e) {
153 Log.e(this, e, "Failed to put on hold for call %s", callId);
154 }
155 }
156 }
157
158 /** See {@link ICallService#unhold}. */
159 public void unhold(String callId) {
160 if (isServiceValid("unhold")) {
161 try {
162 mServiceInterface.unhold(callId);
163 } catch (RemoteException e) {
164 Log.e(this, e, "Failed to remove from hold for call %s", callId);
165 }
166 }
167 }
168
Ben Gilad61925612014-03-11 19:06:36 -0700169 /**
170 * Starts retrieval of details for an incoming call. Details are returned through the
171 * call-service adapter using the specified call ID. Upon failure, the specified error callback
172 * is invoked. Can be invoked even when the call service is unbound.
173 * See {@link ICallService#setIncomingCallId}.
174 *
175 * @param callId The call ID used for the incoming call.
176 * @param extras The {@link CallService}-provided extras which need to be sent back.
177 * @param errorCallback The callback to invoke upon failure.
178 */
179 void setIncomingCallId(
180 final String callId,
181 final Bundle extras,
182 final Runnable errorCallback) {
183
184 BindCallback callback = new BindCallback() {
185 @Override public void onSuccess() {
186 if (isServiceValid("setIncomingCallId")) {
187 mAdapter.addPendingIncomingCallId(callId);
188 try {
189 mServiceInterface.setIncomingCallId(callId, extras);
190 } catch (RemoteException e) {
191 Log.e(CallServiceWrapper.this, e,
192 "Failed to setIncomingCallId for call %s", callId);
193 mAdapter.removePendingIncomingCallId(callId);
194 }
195 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800196 }
Ben Gilad61925612014-03-11 19:06:36 -0700197 @Override public void onFailure() {
198 errorCallback.run();
199 }
200 };
201
202 mBinder.bind(callback);
Santos Cordon63aeb162014-02-10 09:20:40 -0800203 }
204
205 /** See {@link ICallService#disconnect}. */
Ben Gilad61925612014-03-11 19:06:36 -0700206 void disconnect(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800207 if (isServiceValid("disconnect")) {
208 try {
Santos Cordon63aeb162014-02-10 09:20:40 -0800209 mServiceInterface.disconnect(callId);
Santos Cordon61d0f702014-02-19 02:52:23 -0800210 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800211 Log.e(this, e, "Failed to disconnect call %s", callId);
Santos Cordon63aeb162014-02-10 09:20:40 -0800212 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800213 }
214 }
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800215
Santos Cordon61d0f702014-02-19 02:52:23 -0800216 /** See {@link ICallService#answer}. */
Ben Gilad61925612014-03-11 19:06:36 -0700217 void answer(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800218 if (isServiceValid("answer")) {
219 try {
220 mServiceInterface.answer(callId);
221 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800222 Log.e(this, e, "Failed to answer call %s", callId);
Santos Cordon7917d382014-02-14 02:31:18 -0800223 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800224 }
225 }
226
227 /** See {@link ICallService#reject}. */
Ben Gilad61925612014-03-11 19:06:36 -0700228 void reject(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800229 if (isServiceValid("reject")) {
230 try {
231 mServiceInterface.reject(callId);
232 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800233 Log.e(this, e, "Failed to reject call %s");
Santos Cordon61d0f702014-02-19 02:52:23 -0800234 }
Santos Cordon7917d382014-02-14 02:31:18 -0800235 }
236 }
237
238 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800239 * Cancels the incoming call for the specified call ID.
240 * TODO(santoscordon): This method should be called by IncomingCallsManager when the incoming
241 * call has failed.
Santos Cordon7917d382014-02-14 02:31:18 -0800242 *
243 * @param callId The ID of the call.
244 */
245 void cancelIncomingCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800246 mAdapter.removePendingIncomingCallId(callId);
Santos Cordon7917d382014-02-14 02:31:18 -0800247 }
248
Yorke Leeadee12d2014-03-13 12:08:30 -0700249 /**
250 * Cancels the outgoing call for the specified call ID.
251 *
252 * @param callId The ID of the call.
253 */
254 void cancelOutgoingCall(String callId) {
255 mAdapter.removePendingOutgoingCallId(callId);
256 }
257
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800258 /** {@inheritDoc} */
259 @Override protected void setServiceInterface(IBinder binder) {
260 mServiceInterface = ICallService.Stub.asInterface(binder);
261 setCallServiceAdapter(mAdapter);
262 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800263}