blob: 5c9905ed8fbe124fb78ec8588c0eed0d28f42466 [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 */
38public 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
Santos Cordon63aeb162014-02-10 09:20:40 -080051 /**
52 * Creates a call-service provider for the specified component.
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 Giladc5b22692014-02-18 20:03:22 -080058 public 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 Giladc5b22692014-02-18 20:03:22 -080064 public CallServiceDescriptor getDescriptor() {
65 return mDescriptor;
Santos Cordonc195e362014-02-11 17:05:31 -080066 }
67
Santos Cordon63aeb162014-02-10 09:20:40 -080068 /** See {@link ICallService#setCallServiceAdapter}. */
69 public 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
79 /** See {@link ICallService#isCompatibleWith}. */
80 public void isCompatibleWith(CallInfo callInfo) {
Santos Cordon61d0f702014-02-19 02:52:23 -080081 if (isServiceValid("isCompatibleWith")) {
82 try {
Santos Cordon63aeb162014-02-10 09:20:40 -080083 mServiceInterface.isCompatibleWith(callInfo);
Santos Cordon61d0f702014-02-19 02:52:23 -080084 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -080085 Log.e(this, e, "Failed checking isCompatibleWith.");
Santos Cordon63aeb162014-02-10 09:20:40 -080086 }
Santos Cordon63aeb162014-02-10 09:20:40 -080087 }
88 }
89
90 /** See {@link ICallService#call}. */
91 public void call(CallInfo callInfo) {
Ben Gilad28e8ad62014-03-06 17:01:54 -080092 String callId = callInfo.getId();
Santos Cordon61d0f702014-02-19 02:52:23 -080093 if (isServiceValid("call")) {
94 try {
Santos Cordon63aeb162014-02-10 09:20:40 -080095 mServiceInterface.call(callInfo);
Ben Gilad28e8ad62014-03-06 17:01:54 -080096 mAdapter.addPendingOutgoingCallId(callId);
Santos Cordon61d0f702014-02-19 02:52:23 -080097 } catch (RemoteException e) {
Ben Gilad28e8ad62014-03-06 17:01:54 -080098 Log.e(this, e, "Failed to place call " + callId + ".");
99 }
100 }
101 }
102
103 /** See {@link ICallService#abort}. */
104 public void abort(String callId) {
105 mAdapter.removePendingOutgoingCallId(callId);
106 if (isServiceValid("abort")) {
107 try {
108 mServiceInterface.abort(callId);
109 } catch (RemoteException e) {
110 Log.e(this, e, "Failed to abort call %s", callId);
Santos Cordon63aeb162014-02-10 09:20:40 -0800111 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800112 }
113 }
114
115 /** See {@link ICallService#setIncomingCallId}. */
Evan Charltona05805b2014-03-05 08:21:46 -0800116 public void setIncomingCallId(String callId, Bundle extras) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800117 if (isServiceValid("setIncomingCallId")) {
118 mAdapter.addPendingIncomingCallId(callId);
119 try {
Evan Charltona05805b2014-03-05 08:21:46 -0800120 mServiceInterface.setIncomingCallId(callId, extras);
Santos Cordon61d0f702014-02-19 02:52:23 -0800121 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800122 Log.e(this, e, "Failed to setIncomingCallId for call %s", callId);
Santos Cordon61d0f702014-02-19 02:52:23 -0800123 mAdapter.removePendingIncomingCallId(callId);
124 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800125 }
126 }
127
128 /** See {@link ICallService#disconnect}. */
129 public void disconnect(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800130 if (isServiceValid("disconnect")) {
131 try {
Santos Cordon63aeb162014-02-10 09:20:40 -0800132 mServiceInterface.disconnect(callId);
Santos Cordon61d0f702014-02-19 02:52:23 -0800133 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800134 Log.e(this, e, "Failed to disconnect call %s", callId);
Santos Cordon63aeb162014-02-10 09:20:40 -0800135 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800136 }
137 }
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800138
Santos Cordon61d0f702014-02-19 02:52:23 -0800139 /** See {@link ICallService#answer}. */
140 public void answer(String callId) {
141 if (isServiceValid("answer")) {
142 try {
143 mServiceInterface.answer(callId);
144 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800145 Log.e(this, e, "Failed to answer call %s", callId);
Santos Cordon7917d382014-02-14 02:31:18 -0800146 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800147 }
148 }
149
150 /** See {@link ICallService#reject}. */
151 public void reject(String callId) {
152 if (isServiceValid("reject")) {
153 try {
154 mServiceInterface.reject(callId);
155 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800156 Log.e(this, e, "Failed to reject call %s");
Santos Cordon61d0f702014-02-19 02:52:23 -0800157 }
Santos Cordon7917d382014-02-14 02:31:18 -0800158 }
159 }
160
161 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800162 * Starts retrieval of details for an incoming call. Details are returned through the
163 * call-service adapter using the specified call ID. Upon failure, the specified error callback
164 * is invoked. Can be invoked even when the call service is unbound.
165 *
Evan Charltona05805b2014-03-05 08:21:46 -0800166 * @param callId The call ID used for the incoming call.
167 * @param extras The {@link CallService}-provided extras which need to be sent back.
Santos Cordon493e8f22014-02-19 03:15:12 -0800168 * @param errorCallback The callback invoked upon failure.
169 */
Evan Charltona05805b2014-03-05 08:21:46 -0800170 void retrieveIncomingCall(final String callId, final Bundle extras,
171 final Runnable errorCallback) {
172
Santos Cordon493e8f22014-02-19 03:15:12 -0800173 BindCallback callback = new BindCallback() {
174 @Override public void onSuccess() {
Evan Charltona05805b2014-03-05 08:21:46 -0800175 setIncomingCallId(callId, extras);
Santos Cordon493e8f22014-02-19 03:15:12 -0800176 }
177 @Override public void onFailure() {
178 errorCallback.run();
179 }
180 };
181
182 bind(callback);
183 }
184
185 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800186 * Cancels the incoming call for the specified call ID.
187 * TODO(santoscordon): This method should be called by IncomingCallsManager when the incoming
188 * call has failed.
Santos Cordon7917d382014-02-14 02:31:18 -0800189 *
190 * @param callId The ID of the call.
191 */
192 void cancelIncomingCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800193 mAdapter.removePendingIncomingCallId(callId);
Santos Cordon7917d382014-02-14 02:31:18 -0800194 }
195
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800196 /** {@inheritDoc} */
197 @Override protected void setServiceInterface(IBinder binder) {
198 mServiceInterface = ICallService.Stub.asInterface(binder);
199 setCallServiceAdapter(mAdapter);
200 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800201
202 private boolean isServiceValid(String actionName) {
203 if (mServiceInterface != null) {
204 return true;
205 }
206
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800207 Log.wtf(this, "%s invoked while service is unbound", actionName);
Santos Cordon61d0f702014-02-19 02:52:23 -0800208 return false;
209 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800210}