Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.telecomm; |
| 18 | |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 19 | import android.os.Bundle; |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 20 | import android.os.IBinder; |
| 21 | import android.os.RemoteException; |
| 22 | import android.telecomm.CallInfo; |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 23 | import android.telecomm.CallService; |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 24 | import android.telecomm.CallServiceDescriptor; |
Sailesh Nepal | 83cfe7c | 2014-03-11 19:54:22 -0700 | [diff] [blame] | 25 | import android.telecomm.TelecommConstants; |
Sailesh Nepal | a439e1b | 2014-03-11 18:19:58 -0700 | [diff] [blame] | 26 | |
| 27 | import com.android.internal.telecomm.ICallService; |
| 28 | import com.android.internal.telecomm.ICallServiceAdapter; |
| 29 | import com.android.internal.telecomm.ICallServiceProvider; |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 30 | |
| 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 | */ |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 38 | final class CallServiceWrapper extends ServiceBinder<ICallService> { |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 39 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 40 | /** The descriptor of this call service as supplied by the call-service provider. */ |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 41 | private final CallServiceDescriptor mDescriptor; |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 42 | |
| 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 | |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 51 | private Binder mBinder = new Binder(); |
| 52 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 53 | /** |
| 54 | * Creates a call-service provider for the specified component. |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 55 | * |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 56 | * @param descriptor The call-service descriptor from |
| 57 | * {@link ICallServiceProvider#lookupCallServices}. |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 58 | * @param adapter The call-service adapter. |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 59 | */ |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 60 | CallServiceWrapper(CallServiceDescriptor descriptor, CallServiceAdapter adapter) { |
Sailesh Nepal | a439e1b | 2014-03-11 18:19:58 -0700 | [diff] [blame] | 61 | super(TelecommConstants.ACTION_CALL_SERVICE, descriptor.getServiceComponent()); |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 62 | mDescriptor = descriptor; |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 63 | mAdapter = adapter; |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 66 | CallServiceDescriptor getDescriptor() { |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 67 | return mDescriptor; |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 70 | /** See {@link ICallService#setCallServiceAdapter}. */ |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 71 | void setCallServiceAdapter(ICallServiceAdapter callServiceAdapter) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 72 | if (isServiceValid("setCallServiceAdapter")) { |
| 73 | try { |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 74 | mServiceInterface.setCallServiceAdapter(callServiceAdapter); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 75 | } catch (RemoteException e) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 76 | Log.e(this, e, "Failed to setCallServiceAdapter."); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 77 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 81 | /** |
| 82 | * Checks whether or not the specified call is compatible with this call-service implementation, |
| 83 | * see {@link ICallService#isCompatibleWith}. Upon failure, the specified error callback is |
| 84 | * invoked. Can be invoked even when the call service is unbound. |
| 85 | * |
| 86 | * @param callInfo The details of the call. |
| 87 | * @param errorCallback The callback to invoke upon failure. |
| 88 | */ |
| 89 | void isCompatibleWith(final CallInfo callInfo, final Runnable errorCallback) { |
| 90 | BindCallback callback = new BindCallback() { |
| 91 | @Override public void onSuccess() { |
| 92 | if (isServiceValid("isCompatibleWith")) { |
| 93 | try { |
Yorke Lee | adee12d | 2014-03-13 12:08:30 -0700 | [diff] [blame^] | 94 | mAdapter.addPendingOutgoingCallId(callInfo.getId()); |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 95 | mServiceInterface.isCompatibleWith(callInfo); |
| 96 | } catch (RemoteException e) { |
Yorke Lee | adee12d | 2014-03-13 12:08:30 -0700 | [diff] [blame^] | 97 | mAdapter.removePendingOutgoingCallId(callInfo.getId()); |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 98 | Log.e(CallServiceWrapper.this, e, "Failed checking isCompatibleWith."); |
| 99 | } |
| 100 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 101 | } |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 102 | @Override public void onFailure() { |
| 103 | errorCallback.run(); |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | mBinder.bind(callback); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 110 | /** |
| 111 | * Attempts to place the specified call, see {@link ICallService#call}. Upon failure, the |
| 112 | * specified error callback is invoked. Can be invoked even when the call service is unbound. |
| 113 | * |
| 114 | * @param callInfo The details of the call. |
| 115 | * @param errorCallback The callback to invoke upon failure. |
| 116 | */ |
| 117 | void call(final CallInfo callInfo, final Runnable errorCallback) { |
| 118 | BindCallback callback = new BindCallback() { |
| 119 | @Override public void onSuccess() { |
| 120 | String callId = callInfo.getId(); |
| 121 | if (isServiceValid("call")) { |
| 122 | try { |
| 123 | mServiceInterface.call(callInfo); |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 124 | } catch (RemoteException e) { |
| 125 | Log.e(CallServiceWrapper.this, e, "Failed to place call %s", callId); |
| 126 | } |
| 127 | } |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 128 | } |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 129 | @Override public void onFailure() { |
| 130 | errorCallback.run(); |
| 131 | } |
| 132 | }; |
| 133 | |
| 134 | mBinder.bind(callback); |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /** See {@link ICallService#abort}. */ |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 138 | void abort(String callId) { |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 139 | mAdapter.removePendingOutgoingCallId(callId); |
| 140 | if (isServiceValid("abort")) { |
| 141 | try { |
| 142 | mServiceInterface.abort(callId); |
| 143 | } catch (RemoteException e) { |
| 144 | Log.e(this, e, "Failed to abort call %s", callId); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 145 | } |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 149 | /** |
| 150 | * Starts retrieval of details for an incoming call. Details are returned through the |
| 151 | * call-service adapter using the specified call ID. Upon failure, the specified error callback |
| 152 | * is invoked. Can be invoked even when the call service is unbound. |
| 153 | * See {@link ICallService#setIncomingCallId}. |
| 154 | * |
| 155 | * @param callId The call ID used for the incoming call. |
| 156 | * @param extras The {@link CallService}-provided extras which need to be sent back. |
| 157 | * @param errorCallback The callback to invoke upon failure. |
| 158 | */ |
| 159 | void setIncomingCallId( |
| 160 | final String callId, |
| 161 | final Bundle extras, |
| 162 | final Runnable errorCallback) { |
| 163 | |
| 164 | BindCallback callback = new BindCallback() { |
| 165 | @Override public void onSuccess() { |
| 166 | if (isServiceValid("setIncomingCallId")) { |
| 167 | mAdapter.addPendingIncomingCallId(callId); |
| 168 | try { |
| 169 | mServiceInterface.setIncomingCallId(callId, extras); |
| 170 | } catch (RemoteException e) { |
| 171 | Log.e(CallServiceWrapper.this, e, |
| 172 | "Failed to setIncomingCallId for call %s", callId); |
| 173 | mAdapter.removePendingIncomingCallId(callId); |
| 174 | } |
| 175 | } |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 176 | } |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 177 | @Override public void onFailure() { |
| 178 | errorCallback.run(); |
| 179 | } |
| 180 | }; |
| 181 | |
| 182 | mBinder.bind(callback); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /** See {@link ICallService#disconnect}. */ |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 186 | void disconnect(String callId) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 187 | if (isServiceValid("disconnect")) { |
| 188 | try { |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 189 | mServiceInterface.disconnect(callId); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 190 | } catch (RemoteException e) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 191 | Log.e(this, e, "Failed to disconnect call %s", callId); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 192 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 193 | } |
| 194 | } |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 195 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 196 | /** See {@link ICallService#answer}. */ |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 197 | void answer(String callId) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 198 | if (isServiceValid("answer")) { |
| 199 | try { |
| 200 | mServiceInterface.answer(callId); |
| 201 | } catch (RemoteException e) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 202 | Log.e(this, e, "Failed to answer call %s", callId); |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 203 | } |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| 207 | /** See {@link ICallService#reject}. */ |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 208 | void reject(String callId) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 209 | if (isServiceValid("reject")) { |
| 210 | try { |
| 211 | mServiceInterface.reject(callId); |
| 212 | } catch (RemoteException e) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 213 | Log.e(this, e, "Failed to reject call %s"); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 214 | } |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
| 218 | /** |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 219 | * Cancels the incoming call for the specified call ID. |
| 220 | * TODO(santoscordon): This method should be called by IncomingCallsManager when the incoming |
| 221 | * call has failed. |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 222 | * |
| 223 | * @param callId The ID of the call. |
| 224 | */ |
| 225 | void cancelIncomingCall(String callId) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 226 | mAdapter.removePendingIncomingCallId(callId); |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Yorke Lee | adee12d | 2014-03-13 12:08:30 -0700 | [diff] [blame^] | 229 | /** |
| 230 | * Cancels the outgoing call for the specified call ID. |
| 231 | * |
| 232 | * @param callId The ID of the call. |
| 233 | */ |
| 234 | void cancelOutgoingCall(String callId) { |
| 235 | mAdapter.removePendingOutgoingCallId(callId); |
| 236 | } |
| 237 | |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 238 | /** {@inheritDoc} */ |
| 239 | @Override protected void setServiceInterface(IBinder binder) { |
| 240 | mServiceInterface = ICallService.Stub.asInterface(binder); |
| 241 | setCallServiceAdapter(mAdapter); |
| 242 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 243 | } |