blob: 5d2162a7d99ea41cdae2e84024d7a6de39d37054 [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
27/**
28 * Wrapper for {@link ICallService}s, handles binding to {@link ICallService} and keeps track of
29 * when the object can safely be unbound. Other classes should not use {@link ICallService} directly
30 * and instead should use this class to invoke methods of {@link ICallService}.
31 * TODO(santoscordon): Keep track of when the service can be safely unbound.
32 * TODO(santoscordon): Look into combining with android.telecomm.CallService.
33 */
34public class CallServiceWrapper extends ServiceBinder<ICallService> {
Santos Cordon63aeb162014-02-10 09:20:40 -080035
36 /**
37 * The service action used to bind to ICallService implementations.
38 * TODO(santoscordon): Move this to TelecommConstants.
39 */
40 static final String CALL_SERVICE_ACTION = ICallService.class.getName();
41
Santos Cordonc195e362014-02-11 17:05:31 -080042 private static final String TAG = CallServiceWrapper.class.getSimpleName();
43
44 /** The descriptor of this call service as supplied by the call-service provider. */
Ben Giladc5b22692014-02-18 20:03:22 -080045 private final CallServiceDescriptor mDescriptor;
Santos Cordonc195e362014-02-11 17:05:31 -080046
47 /**
48 * The adapter used by the underlying call-service implementation to communicate with Telecomm.
49 */
50 private final CallServiceAdapter mAdapter;
51
52 /** The actual service implementation. */
53 private ICallService mServiceInterface;
54
Santos Cordon63aeb162014-02-10 09:20:40 -080055 /**
56 * Creates a call-service provider for the specified component.
Santos Cordonc195e362014-02-11 17:05:31 -080057 *
Santos Cordon61d0f702014-02-19 02:52:23 -080058 * @param descriptor The call-service descriptor from
59 * {@link ICallServiceProvider#lookupCallServices}.
Santos Cordonc195e362014-02-11 17:05:31 -080060 * @param adapter The call-service adapter.
Santos Cordon63aeb162014-02-10 09:20:40 -080061 */
Ben Giladc5b22692014-02-18 20:03:22 -080062 public CallServiceWrapper(CallServiceDescriptor descriptor, CallServiceAdapter adapter) {
63 super(CALL_SERVICE_ACTION, descriptor.getServiceComponent());
64 mDescriptor = descriptor;
Santos Cordonc195e362014-02-11 17:05:31 -080065 mAdapter = adapter;
Santos Cordon63aeb162014-02-10 09:20:40 -080066 }
67
Ben Giladc5b22692014-02-18 20:03:22 -080068 public CallServiceDescriptor getDescriptor() {
69 return mDescriptor;
Santos Cordonc195e362014-02-11 17:05:31 -080070 }
71
Santos Cordon63aeb162014-02-10 09:20:40 -080072 /** See {@link ICallService#setCallServiceAdapter}. */
73 public void setCallServiceAdapter(ICallServiceAdapter callServiceAdapter) {
Santos Cordon61d0f702014-02-19 02:52:23 -080074 if (isServiceValid("setCallServiceAdapter")) {
75 try {
Santos Cordon63aeb162014-02-10 09:20:40 -080076 mServiceInterface.setCallServiceAdapter(callServiceAdapter);
Santos Cordon61d0f702014-02-19 02:52:23 -080077 } catch (RemoteException e) {
78 Log.e(TAG, "Failed to setCallServiceAdapter.", e);
Santos Cordon63aeb162014-02-10 09:20:40 -080079 }
Santos Cordon63aeb162014-02-10 09:20:40 -080080 }
81 }
82
83 /** See {@link ICallService#isCompatibleWith}. */
84 public void isCompatibleWith(CallInfo callInfo) {
Santos Cordon61d0f702014-02-19 02:52:23 -080085 if (isServiceValid("isCompatibleWith")) {
86 try {
Santos Cordon63aeb162014-02-10 09:20:40 -080087 mServiceInterface.isCompatibleWith(callInfo);
Santos Cordon61d0f702014-02-19 02:52:23 -080088 } catch (RemoteException e) {
89 Log.e(TAG, "Failed checking isCompatibleWith.", e);
Santos Cordon63aeb162014-02-10 09:20:40 -080090 }
Santos Cordon63aeb162014-02-10 09:20:40 -080091 }
92 }
93
94 /** See {@link ICallService#call}. */
95 public void call(CallInfo callInfo) {
Santos Cordon61d0f702014-02-19 02:52:23 -080096 if (isServiceValid("call")) {
97 try {
Santos Cordon63aeb162014-02-10 09:20:40 -080098 mServiceInterface.call(callInfo);
Santos Cordon61d0f702014-02-19 02:52:23 -080099 } catch (RemoteException e) {
100 Log.e(TAG, "Failed to place call " + callInfo.getId() + ".", e);
Santos Cordon63aeb162014-02-10 09:20:40 -0800101 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800102 }
103 }
104
105 /** See {@link ICallService#setIncomingCallId}. */
106 public void setIncomingCallId(String callId) {
107 if (isServiceValid("setIncomingCallId")) {
108 mAdapter.addPendingIncomingCallId(callId);
109 try {
110 mServiceInterface.setIncomingCallId(callId);
111 } catch (RemoteException e) {
112 Log.e(TAG, "Failed to setIncomingCallId for call " + callId, e);
113 mAdapter.removePendingIncomingCallId(callId);
114 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800115 }
116 }
117
118 /** See {@link ICallService#disconnect}. */
119 public void disconnect(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800120 if (isServiceValid("disconnect")) {
121 try {
Santos Cordon63aeb162014-02-10 09:20:40 -0800122 mServiceInterface.disconnect(callId);
Santos Cordon61d0f702014-02-19 02:52:23 -0800123 } catch (RemoteException e) {
124 Log.e(TAG, "Failed to disconnect call " + callId + ".", e);
Santos Cordon63aeb162014-02-10 09:20:40 -0800125 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800126 }
127 }
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800128
Santos Cordon61d0f702014-02-19 02:52:23 -0800129 /** See {@link ICallService#answer}. */
130 public void answer(String callId) {
131 if (isServiceValid("answer")) {
132 try {
133 mServiceInterface.answer(callId);
134 } catch (RemoteException e) {
135 Log.e(TAG, "Failed to answer call " + callId, e);
Santos Cordon7917d382014-02-14 02:31:18 -0800136 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800137 }
138 }
139
140 /** See {@link ICallService#reject}. */
141 public void reject(String callId) {
142 if (isServiceValid("reject")) {
143 try {
144 mServiceInterface.reject(callId);
145 } catch (RemoteException e) {
146 Log.e(TAG, "Failed to reject call " + callId, e);
147 }
Santos Cordon7917d382014-02-14 02:31:18 -0800148 }
149 }
150
151 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800152 * Cancels the incoming call for the specified call ID.
153 * TODO(santoscordon): This method should be called by IncomingCallsManager when the incoming
154 * call has failed.
Santos Cordon7917d382014-02-14 02:31:18 -0800155 *
156 * @param callId The ID of the call.
157 */
158 void cancelIncomingCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800159 mAdapter.removePendingIncomingCallId(callId);
Santos Cordon7917d382014-02-14 02:31:18 -0800160 }
161
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800162 /** {@inheritDoc} */
163 @Override protected void setServiceInterface(IBinder binder) {
164 mServiceInterface = ICallService.Stub.asInterface(binder);
165 setCallServiceAdapter(mAdapter);
166 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800167
168 private boolean isServiceValid(String actionName) {
169 if (mServiceInterface != null) {
170 return true;
171 }
172
173 Log.wtf(TAG, actionName + " invoked while service is unbound");
174 return false;
175 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800176}