blob: 94be7c8ebb6dce96eb5e4e2e9d55b92efab56a01 [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;
Santos Cordon63aeb162014-02-10 09:20:40 -080025import android.telecomm.ICallService;
26import android.telecomm.ICallServiceAdapter;
Santos Cordon63aeb162014-02-10 09:20:40 -080027
28/**
29 * Wrapper for {@link ICallService}s, handles binding to {@link ICallService} and keeps track of
30 * when the object can safely be unbound. Other classes should not use {@link ICallService} directly
31 * and instead should use this class to invoke methods of {@link ICallService}.
32 * TODO(santoscordon): Keep track of when the service can be safely unbound.
33 * TODO(santoscordon): Look into combining with android.telecomm.CallService.
34 */
35public class CallServiceWrapper extends ServiceBinder<ICallService> {
Santos Cordon63aeb162014-02-10 09:20:40 -080036
37 /**
38 * The service action used to bind to ICallService implementations.
39 * TODO(santoscordon): Move this to TelecommConstants.
40 */
41 static final String CALL_SERVICE_ACTION = ICallService.class.getName();
42
Santos Cordonc195e362014-02-11 17:05:31 -080043 /** The descriptor of this call service as supplied by the call-service provider. */
Ben Giladc5b22692014-02-18 20:03:22 -080044 private final CallServiceDescriptor mDescriptor;
Santos Cordonc195e362014-02-11 17:05:31 -080045
46 /**
47 * The adapter used by the underlying call-service implementation to communicate with Telecomm.
48 */
49 private final CallServiceAdapter mAdapter;
50
51 /** The actual service implementation. */
52 private ICallService mServiceInterface;
53
Santos Cordon63aeb162014-02-10 09:20:40 -080054 /**
55 * Creates a call-service provider for the specified component.
Santos Cordonc195e362014-02-11 17:05:31 -080056 *
Santos Cordon61d0f702014-02-19 02:52:23 -080057 * @param descriptor The call-service descriptor from
58 * {@link ICallServiceProvider#lookupCallServices}.
Santos Cordonc195e362014-02-11 17:05:31 -080059 * @param adapter The call-service adapter.
Santos Cordon63aeb162014-02-10 09:20:40 -080060 */
Ben Giladc5b22692014-02-18 20:03:22 -080061 public CallServiceWrapper(CallServiceDescriptor descriptor, CallServiceAdapter adapter) {
62 super(CALL_SERVICE_ACTION, descriptor.getServiceComponent());
63 mDescriptor = descriptor;
Santos Cordonc195e362014-02-11 17:05:31 -080064 mAdapter = adapter;
Santos Cordon63aeb162014-02-10 09:20:40 -080065 }
66
Ben Giladc5b22692014-02-18 20:03:22 -080067 public CallServiceDescriptor getDescriptor() {
68 return mDescriptor;
Santos Cordonc195e362014-02-11 17:05:31 -080069 }
70
Santos Cordon63aeb162014-02-10 09:20:40 -080071 /** See {@link ICallService#setCallServiceAdapter}. */
72 public void setCallServiceAdapter(ICallServiceAdapter callServiceAdapter) {
Santos Cordon61d0f702014-02-19 02:52:23 -080073 if (isServiceValid("setCallServiceAdapter")) {
74 try {
Santos Cordon63aeb162014-02-10 09:20:40 -080075 mServiceInterface.setCallServiceAdapter(callServiceAdapter);
Santos Cordon61d0f702014-02-19 02:52:23 -080076 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -080077 Log.e(this, e, "Failed to setCallServiceAdapter.");
Santos Cordon63aeb162014-02-10 09:20:40 -080078 }
Santos Cordon63aeb162014-02-10 09:20:40 -080079 }
80 }
81
82 /** See {@link ICallService#isCompatibleWith}. */
83 public void isCompatibleWith(CallInfo callInfo) {
Santos Cordon61d0f702014-02-19 02:52:23 -080084 if (isServiceValid("isCompatibleWith")) {
85 try {
Santos Cordon63aeb162014-02-10 09:20:40 -080086 mServiceInterface.isCompatibleWith(callInfo);
Santos Cordon61d0f702014-02-19 02:52:23 -080087 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -080088 Log.e(this, e, "Failed checking isCompatibleWith.");
Santos Cordon63aeb162014-02-10 09:20:40 -080089 }
Santos Cordon63aeb162014-02-10 09:20:40 -080090 }
91 }
92
93 /** See {@link ICallService#call}. */
94 public void call(CallInfo callInfo) {
Santos Cordon61d0f702014-02-19 02:52:23 -080095 if (isServiceValid("call")) {
96 try {
Santos Cordon63aeb162014-02-10 09:20:40 -080097 mServiceInterface.call(callInfo);
Santos Cordon61d0f702014-02-19 02:52:23 -080098 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -080099 Log.e(this, e, "Failed to place call " + callInfo.getId() + ".");
Santos Cordon63aeb162014-02-10 09:20:40 -0800100 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800101 }
102 }
103
104 /** See {@link ICallService#setIncomingCallId}. */
Evan Charltona05805b2014-03-05 08:21:46 -0800105 public void setIncomingCallId(String callId, Bundle extras) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800106 if (isServiceValid("setIncomingCallId")) {
107 mAdapter.addPendingIncomingCallId(callId);
108 try {
Evan Charltona05805b2014-03-05 08:21:46 -0800109 mServiceInterface.setIncomingCallId(callId, extras);
Santos Cordon61d0f702014-02-19 02:52:23 -0800110 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800111 Log.e(this, e, "Failed to setIncomingCallId for call %s", callId);
Santos Cordon61d0f702014-02-19 02:52:23 -0800112 mAdapter.removePendingIncomingCallId(callId);
113 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800114 }
115 }
116
117 /** See {@link ICallService#disconnect}. */
118 public void disconnect(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800119 if (isServiceValid("disconnect")) {
120 try {
Santos Cordon63aeb162014-02-10 09:20:40 -0800121 mServiceInterface.disconnect(callId);
Santos Cordon61d0f702014-02-19 02:52:23 -0800122 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800123 Log.e(this, e, "Failed to disconnect call %s", callId);
Santos Cordon63aeb162014-02-10 09:20:40 -0800124 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800125 }
126 }
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800127
Santos Cordon61d0f702014-02-19 02:52:23 -0800128 /** See {@link ICallService#answer}. */
129 public void answer(String callId) {
130 if (isServiceValid("answer")) {
131 try {
132 mServiceInterface.answer(callId);
133 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800134 Log.e(this, e, "Failed to answer call %s", callId);
Santos Cordon7917d382014-02-14 02:31:18 -0800135 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800136 }
137 }
138
139 /** See {@link ICallService#reject}. */
140 public void reject(String callId) {
141 if (isServiceValid("reject")) {
142 try {
143 mServiceInterface.reject(callId);
144 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800145 Log.e(this, e, "Failed to reject call %s");
Santos Cordon61d0f702014-02-19 02:52:23 -0800146 }
Santos Cordon7917d382014-02-14 02:31:18 -0800147 }
148 }
149
150 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800151 * Starts retrieval of details for an incoming call. Details are returned through the
152 * call-service adapter using the specified call ID. Upon failure, the specified error callback
153 * is invoked. Can be invoked even when the call service is unbound.
154 *
Evan Charltona05805b2014-03-05 08:21:46 -0800155 * @param callId The call ID used for the incoming call.
156 * @param extras The {@link CallService}-provided extras which need to be sent back.
Santos Cordon493e8f22014-02-19 03:15:12 -0800157 * @param errorCallback The callback invoked upon failure.
158 */
Evan Charltona05805b2014-03-05 08:21:46 -0800159 void retrieveIncomingCall(final String callId, final Bundle extras,
160 final Runnable errorCallback) {
161
Santos Cordon493e8f22014-02-19 03:15:12 -0800162 BindCallback callback = new BindCallback() {
163 @Override public void onSuccess() {
Evan Charltona05805b2014-03-05 08:21:46 -0800164 setIncomingCallId(callId, extras);
Santos Cordon493e8f22014-02-19 03:15:12 -0800165 }
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
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800196 Log.wtf(this, "%s invoked while service is unbound", actionName);
Santos Cordon61d0f702014-02-19 02:52:23 -0800197 return false;
198 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800199}