blob: d334f6250c97a707dfd8439f26c71399a24a56c2 [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 Nepala439e1b2014-03-11 18:19:58 -070025
26import com.android.internal.telecomm.ICallService;
27import com.android.internal.telecomm.ICallServiceAdapter;
28import com.android.internal.telecomm.ICallServiceProvider;
Santos Cordon63aeb162014-02-10 09:20:40 -080029
30/**
31 * Wrapper for {@link ICallService}s, handles binding to {@link ICallService} and keeps track of
32 * when the object can safely be unbound. Other classes should not use {@link ICallService} directly
33 * and instead should use this class to invoke methods of {@link ICallService}.
34 * TODO(santoscordon): Keep track of when the service can be safely unbound.
35 * TODO(santoscordon): Look into combining with android.telecomm.CallService.
36 */
37public class CallServiceWrapper extends ServiceBinder<ICallService> {
Santos Cordon63aeb162014-02-10 09:20:40 -080038
Santos Cordonc195e362014-02-11 17:05:31 -080039 /** The descriptor of this call service as supplied by the call-service provider. */
Ben Giladc5b22692014-02-18 20:03:22 -080040 private final CallServiceDescriptor mDescriptor;
Santos Cordonc195e362014-02-11 17:05:31 -080041
42 /**
43 * The adapter used by the underlying call-service implementation to communicate with Telecomm.
44 */
45 private final CallServiceAdapter mAdapter;
46
47 /** The actual service implementation. */
48 private ICallService mServiceInterface;
49
Santos Cordon63aeb162014-02-10 09:20:40 -080050 /**
51 * Creates a call-service provider for the specified component.
Santos Cordonc195e362014-02-11 17:05:31 -080052 *
Santos Cordon61d0f702014-02-19 02:52:23 -080053 * @param descriptor The call-service descriptor from
54 * {@link ICallServiceProvider#lookupCallServices}.
Santos Cordonc195e362014-02-11 17:05:31 -080055 * @param adapter The call-service adapter.
Santos Cordon63aeb162014-02-10 09:20:40 -080056 */
Ben Giladc5b22692014-02-18 20:03:22 -080057 public CallServiceWrapper(CallServiceDescriptor descriptor, CallServiceAdapter adapter) {
Sailesh Nepala439e1b2014-03-11 18:19:58 -070058 super(TelecommConstants.ACTION_CALL_SERVICE, descriptor.getServiceComponent());
Ben Giladc5b22692014-02-18 20:03:22 -080059 mDescriptor = descriptor;
Santos Cordonc195e362014-02-11 17:05:31 -080060 mAdapter = adapter;
Santos Cordon63aeb162014-02-10 09:20:40 -080061 }
62
Ben Giladc5b22692014-02-18 20:03:22 -080063 public CallServiceDescriptor getDescriptor() {
64 return mDescriptor;
Santos Cordonc195e362014-02-11 17:05:31 -080065 }
66
Santos Cordon63aeb162014-02-10 09:20:40 -080067 /** See {@link ICallService#setCallServiceAdapter}. */
68 public void setCallServiceAdapter(ICallServiceAdapter callServiceAdapter) {
Santos Cordon61d0f702014-02-19 02:52:23 -080069 if (isServiceValid("setCallServiceAdapter")) {
70 try {
Santos Cordon63aeb162014-02-10 09:20:40 -080071 mServiceInterface.setCallServiceAdapter(callServiceAdapter);
Santos Cordon61d0f702014-02-19 02:52:23 -080072 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -080073 Log.e(this, e, "Failed to setCallServiceAdapter.");
Santos Cordon63aeb162014-02-10 09:20:40 -080074 }
Santos Cordon63aeb162014-02-10 09:20:40 -080075 }
76 }
77
78 /** See {@link ICallService#isCompatibleWith}. */
79 public void isCompatibleWith(CallInfo callInfo) {
Santos Cordon61d0f702014-02-19 02:52:23 -080080 if (isServiceValid("isCompatibleWith")) {
81 try {
Santos Cordon63aeb162014-02-10 09:20:40 -080082 mServiceInterface.isCompatibleWith(callInfo);
Santos Cordon61d0f702014-02-19 02:52:23 -080083 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -080084 Log.e(this, e, "Failed checking isCompatibleWith.");
Santos Cordon63aeb162014-02-10 09:20:40 -080085 }
Santos Cordon63aeb162014-02-10 09:20:40 -080086 }
87 }
88
89 /** See {@link ICallService#call}. */
90 public void call(CallInfo callInfo) {
Ben Gilad28e8ad62014-03-06 17:01:54 -080091 String callId = callInfo.getId();
Santos Cordon61d0f702014-02-19 02:52:23 -080092 if (isServiceValid("call")) {
93 try {
Santos Cordon63aeb162014-02-10 09:20:40 -080094 mServiceInterface.call(callInfo);
Ben Gilad28e8ad62014-03-06 17:01:54 -080095 mAdapter.addPendingOutgoingCallId(callId);
Santos Cordon61d0f702014-02-19 02:52:23 -080096 } catch (RemoteException e) {
Ben Gilad28e8ad62014-03-06 17:01:54 -080097 Log.e(this, e, "Failed to place call " + callId + ".");
98 }
99 }
100 }
101
102 /** See {@link ICallService#abort}. */
103 public void abort(String callId) {
104 mAdapter.removePendingOutgoingCallId(callId);
105 if (isServiceValid("abort")) {
106 try {
107 mServiceInterface.abort(callId);
108 } catch (RemoteException e) {
109 Log.e(this, e, "Failed to abort call %s", callId);
Santos Cordon63aeb162014-02-10 09:20:40 -0800110 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800111 }
112 }
113
114 /** See {@link ICallService#setIncomingCallId}. */
Evan Charltona05805b2014-03-05 08:21:46 -0800115 public void setIncomingCallId(String callId, Bundle extras) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800116 if (isServiceValid("setIncomingCallId")) {
117 mAdapter.addPendingIncomingCallId(callId);
118 try {
Evan Charltona05805b2014-03-05 08:21:46 -0800119 mServiceInterface.setIncomingCallId(callId, extras);
Santos Cordon61d0f702014-02-19 02:52:23 -0800120 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800121 Log.e(this, e, "Failed to setIncomingCallId for call %s", callId);
Santos Cordon61d0f702014-02-19 02:52:23 -0800122 mAdapter.removePendingIncomingCallId(callId);
123 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800124 }
125 }
126
127 /** See {@link ICallService#disconnect}. */
128 public void disconnect(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800129 if (isServiceValid("disconnect")) {
130 try {
Santos Cordon63aeb162014-02-10 09:20:40 -0800131 mServiceInterface.disconnect(callId);
Santos Cordon61d0f702014-02-19 02:52:23 -0800132 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800133 Log.e(this, e, "Failed to disconnect call %s", callId);
Santos Cordon63aeb162014-02-10 09:20:40 -0800134 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800135 }
136 }
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800137
Santos Cordon61d0f702014-02-19 02:52:23 -0800138 /** See {@link ICallService#answer}. */
139 public void answer(String callId) {
140 if (isServiceValid("answer")) {
141 try {
142 mServiceInterface.answer(callId);
143 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800144 Log.e(this, e, "Failed to answer call %s", callId);
Santos Cordon7917d382014-02-14 02:31:18 -0800145 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800146 }
147 }
148
149 /** See {@link ICallService#reject}. */
150 public void reject(String callId) {
151 if (isServiceValid("reject")) {
152 try {
153 mServiceInterface.reject(callId);
154 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800155 Log.e(this, e, "Failed to reject call %s");
Santos Cordon61d0f702014-02-19 02:52:23 -0800156 }
Santos Cordon7917d382014-02-14 02:31:18 -0800157 }
158 }
159
160 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800161 * Starts retrieval of details for an incoming call. Details are returned through the
162 * call-service adapter using the specified call ID. Upon failure, the specified error callback
163 * is invoked. Can be invoked even when the call service is unbound.
164 *
Evan Charltona05805b2014-03-05 08:21:46 -0800165 * @param callId The call ID used for the incoming call.
166 * @param extras The {@link CallService}-provided extras which need to be sent back.
Santos Cordon493e8f22014-02-19 03:15:12 -0800167 * @param errorCallback The callback invoked upon failure.
168 */
Evan Charltona05805b2014-03-05 08:21:46 -0800169 void retrieveIncomingCall(final String callId, final Bundle extras,
170 final Runnable errorCallback) {
171
Santos Cordon493e8f22014-02-19 03:15:12 -0800172 BindCallback callback = new BindCallback() {
173 @Override public void onSuccess() {
Evan Charltona05805b2014-03-05 08:21:46 -0800174 setIncomingCallId(callId, extras);
Santos Cordon493e8f22014-02-19 03:15:12 -0800175 }
176 @Override public void onFailure() {
177 errorCallback.run();
178 }
179 };
180
181 bind(callback);
182 }
183
184 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800185 * Cancels the incoming call for the specified call ID.
186 * TODO(santoscordon): This method should be called by IncomingCallsManager when the incoming
187 * call has failed.
Santos Cordon7917d382014-02-14 02:31:18 -0800188 *
189 * @param callId The ID of the call.
190 */
191 void cancelIncomingCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800192 mAdapter.removePendingIncomingCallId(callId);
Santos Cordon7917d382014-02-14 02:31:18 -0800193 }
194
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800195 /** {@inheritDoc} */
196 @Override protected void setServiceInterface(IBinder binder) {
197 mServiceInterface = ICallService.Stub.asInterface(binder);
198 setCallServiceAdapter(mAdapter);
199 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800200
201 private boolean isServiceValid(String actionName) {
202 if (mServiceInterface != null) {
203 return true;
204 }
205
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800206 Log.wtf(this, "%s invoked while service is unbound", actionName);
Santos Cordon61d0f702014-02-19 02:52:23 -0800207 return false;
208 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800209}