blob: bb9d649ba0852f445df0c838441b0a96f6aba839 [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
Santos Cordon63aeb162014-02-10 09:20:40 -080019import android.os.IBinder;
20import android.os.RemoteException;
21import android.telecomm.CallInfo;
Ben Giladc5b22692014-02-18 20:03:22 -080022import android.telecomm.CallServiceDescriptor;
Santos Cordon63aeb162014-02-10 09:20:40 -080023import android.telecomm.ICallService;
24import android.telecomm.ICallServiceAdapter;
25import android.util.Log;
26
Santos Cordon493e8f22014-02-19 03:15:12 -080027import com.android.telecomm.ServiceBinder.BindCallback;
28
Santos Cordon63aeb162014-02-10 09:20:40 -080029/**
30 * Wrapper for {@link ICallService}s, handles binding to {@link ICallService} and keeps track of
31 * when the object can safely be unbound. Other classes should not use {@link ICallService} directly
32 * and instead should use this class to invoke methods of {@link ICallService}.
33 * TODO(santoscordon): Keep track of when the service can be safely unbound.
34 * TODO(santoscordon): Look into combining with android.telecomm.CallService.
35 */
36public class CallServiceWrapper extends ServiceBinder<ICallService> {
Santos Cordon63aeb162014-02-10 09:20:40 -080037
38 /**
39 * The service action used to bind to ICallService implementations.
40 * TODO(santoscordon): Move this to TelecommConstants.
41 */
42 static final String CALL_SERVICE_ACTION = ICallService.class.getName();
43
Santos Cordonc195e362014-02-11 17:05:31 -080044 private static final String TAG = CallServiceWrapper.class.getSimpleName();
45
46 /** The descriptor of this call service as supplied by the call-service provider. */
Ben Giladc5b22692014-02-18 20:03:22 -080047 private final CallServiceDescriptor mDescriptor;
Santos Cordonc195e362014-02-11 17:05:31 -080048
49 /**
50 * The adapter used by the underlying call-service implementation to communicate with Telecomm.
51 */
52 private final CallServiceAdapter mAdapter;
53
54 /** The actual service implementation. */
55 private ICallService mServiceInterface;
56
Santos Cordon63aeb162014-02-10 09:20:40 -080057 /**
58 * Creates a call-service provider for the specified component.
Santos Cordonc195e362014-02-11 17:05:31 -080059 *
Santos Cordon61d0f702014-02-19 02:52:23 -080060 * @param descriptor The call-service descriptor from
61 * {@link ICallServiceProvider#lookupCallServices}.
Santos Cordonc195e362014-02-11 17:05:31 -080062 * @param adapter The call-service adapter.
Santos Cordon63aeb162014-02-10 09:20:40 -080063 */
Ben Giladc5b22692014-02-18 20:03:22 -080064 public CallServiceWrapper(CallServiceDescriptor descriptor, CallServiceAdapter adapter) {
65 super(CALL_SERVICE_ACTION, descriptor.getServiceComponent());
66 mDescriptor = descriptor;
Santos Cordonc195e362014-02-11 17:05:31 -080067 mAdapter = adapter;
Santos Cordon63aeb162014-02-10 09:20:40 -080068 }
69
Ben Giladc5b22692014-02-18 20:03:22 -080070 public CallServiceDescriptor getDescriptor() {
71 return mDescriptor;
Santos Cordonc195e362014-02-11 17:05:31 -080072 }
73
Santos Cordon63aeb162014-02-10 09:20:40 -080074 /** See {@link ICallService#setCallServiceAdapter}. */
75 public void setCallServiceAdapter(ICallServiceAdapter callServiceAdapter) {
Santos Cordon61d0f702014-02-19 02:52:23 -080076 if (isServiceValid("setCallServiceAdapter")) {
77 try {
Santos Cordon63aeb162014-02-10 09:20:40 -080078 mServiceInterface.setCallServiceAdapter(callServiceAdapter);
Santos Cordon61d0f702014-02-19 02:52:23 -080079 } catch (RemoteException e) {
80 Log.e(TAG, "Failed to setCallServiceAdapter.", e);
Santos Cordon63aeb162014-02-10 09:20:40 -080081 }
Santos Cordon63aeb162014-02-10 09:20:40 -080082 }
83 }
84
85 /** See {@link ICallService#isCompatibleWith}. */
86 public void isCompatibleWith(CallInfo callInfo) {
Santos Cordon61d0f702014-02-19 02:52:23 -080087 if (isServiceValid("isCompatibleWith")) {
88 try {
Santos Cordon63aeb162014-02-10 09:20:40 -080089 mServiceInterface.isCompatibleWith(callInfo);
Santos Cordon61d0f702014-02-19 02:52:23 -080090 } catch (RemoteException e) {
91 Log.e(TAG, "Failed checking isCompatibleWith.", e);
Santos Cordon63aeb162014-02-10 09:20:40 -080092 }
Santos Cordon63aeb162014-02-10 09:20:40 -080093 }
94 }
95
96 /** See {@link ICallService#call}. */
97 public void call(CallInfo callInfo) {
Santos Cordon61d0f702014-02-19 02:52:23 -080098 if (isServiceValid("call")) {
99 try {
Santos Cordon63aeb162014-02-10 09:20:40 -0800100 mServiceInterface.call(callInfo);
Santos Cordon61d0f702014-02-19 02:52:23 -0800101 } catch (RemoteException e) {
102 Log.e(TAG, "Failed to place call " + callInfo.getId() + ".", e);
Santos Cordon63aeb162014-02-10 09:20:40 -0800103 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800104 }
105 }
106
107 /** See {@link ICallService#setIncomingCallId}. */
108 public void setIncomingCallId(String callId) {
109 if (isServiceValid("setIncomingCallId")) {
110 mAdapter.addPendingIncomingCallId(callId);
111 try {
112 mServiceInterface.setIncomingCallId(callId);
113 } catch (RemoteException e) {
114 Log.e(TAG, "Failed to setIncomingCallId for call " + callId, e);
115 mAdapter.removePendingIncomingCallId(callId);
116 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800117 }
118 }
119
120 /** See {@link ICallService#disconnect}. */
121 public void disconnect(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800122 if (isServiceValid("disconnect")) {
123 try {
Santos Cordon63aeb162014-02-10 09:20:40 -0800124 mServiceInterface.disconnect(callId);
Santos Cordon61d0f702014-02-19 02:52:23 -0800125 } catch (RemoteException e) {
126 Log.e(TAG, "Failed to disconnect call " + callId + ".", e);
Santos Cordon63aeb162014-02-10 09:20:40 -0800127 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800128 }
129 }
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800130
Santos Cordon61d0f702014-02-19 02:52:23 -0800131 /** See {@link ICallService#answer}. */
132 public void answer(String callId) {
133 if (isServiceValid("answer")) {
134 try {
135 mServiceInterface.answer(callId);
136 } catch (RemoteException e) {
137 Log.e(TAG, "Failed to answer call " + callId, e);
Santos Cordon7917d382014-02-14 02:31:18 -0800138 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800139 }
140 }
141
142 /** See {@link ICallService#reject}. */
143 public void reject(String callId) {
144 if (isServiceValid("reject")) {
145 try {
146 mServiceInterface.reject(callId);
147 } catch (RemoteException e) {
148 Log.e(TAG, "Failed to reject call " + callId, e);
149 }
Santos Cordon7917d382014-02-14 02:31:18 -0800150 }
151 }
152
153 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800154 * Starts retrieval of details for an incoming call. Details are returned through the
155 * call-service adapter using the specified call ID. Upon failure, the specified error callback
156 * is invoked. Can be invoked even when the call service is unbound.
157 *
158 * @param callID The call ID used for the incoming call.
159 * @param errorCallback The callback invoked upon failure.
160 */
161 void retrieveIncomingCall(final String callId, final Runnable errorCallback) {
162 BindCallback callback = new BindCallback() {
163 @Override public void onSuccess() {
164 setIncomingCallId(callId);
165 }
166 @Override public void onFailure() {
167 errorCallback.run();
168 }
169 };
170
171 bind(callback);
172 }
173
174 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800175 * Cancels the incoming call for the specified call ID.
176 * TODO(santoscordon): This method should be called by IncomingCallsManager when the incoming
177 * call has failed.
Santos Cordon7917d382014-02-14 02:31:18 -0800178 *
179 * @param callId The ID of the call.
180 */
181 void cancelIncomingCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800182 mAdapter.removePendingIncomingCallId(callId);
Santos Cordon7917d382014-02-14 02:31:18 -0800183 }
184
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800185 /** {@inheritDoc} */
186 @Override protected void setServiceInterface(IBinder binder) {
187 mServiceInterface = ICallService.Stub.asInterface(binder);
188 setCallServiceAdapter(mAdapter);
189 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800190
191 private boolean isServiceValid(String actionName) {
192 if (mServiceInterface != null) {
193 return true;
194 }
195
196 Log.wtf(TAG, actionName + " invoked while service is unbound");
197 return false;
198 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800199}