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; |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 25 | import android.telecomm.ICallService; |
| 26 | import android.telecomm.ICallServiceAdapter; |
| 27 | import android.util.Log; |
| 28 | |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 29 | import com.android.telecomm.ServiceBinder.BindCallback; |
| 30 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 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 | */ |
| 38 | public class CallServiceWrapper extends ServiceBinder<ICallService> { |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * The service action used to bind to ICallService implementations. |
| 42 | * TODO(santoscordon): Move this to TelecommConstants. |
| 43 | */ |
| 44 | static final String CALL_SERVICE_ACTION = ICallService.class.getName(); |
| 45 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 46 | private static final String TAG = CallServiceWrapper.class.getSimpleName(); |
| 47 | |
| 48 | /** 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] | 49 | private final CallServiceDescriptor mDescriptor; |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * The adapter used by the underlying call-service implementation to communicate with Telecomm. |
| 53 | */ |
| 54 | private final CallServiceAdapter mAdapter; |
| 55 | |
| 56 | /** The actual service implementation. */ |
| 57 | private ICallService mServiceInterface; |
| 58 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 59 | /** |
| 60 | * Creates a call-service provider for the specified component. |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 61 | * |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 62 | * @param descriptor The call-service descriptor from |
| 63 | * {@link ICallServiceProvider#lookupCallServices}. |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 64 | * @param adapter The call-service adapter. |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 65 | */ |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 66 | public CallServiceWrapper(CallServiceDescriptor descriptor, CallServiceAdapter adapter) { |
| 67 | super(CALL_SERVICE_ACTION, descriptor.getServiceComponent()); |
| 68 | mDescriptor = descriptor; |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 69 | mAdapter = adapter; |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 72 | public CallServiceDescriptor getDescriptor() { |
| 73 | return mDescriptor; |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 76 | /** See {@link ICallService#setCallServiceAdapter}. */ |
| 77 | public void setCallServiceAdapter(ICallServiceAdapter callServiceAdapter) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 78 | if (isServiceValid("setCallServiceAdapter")) { |
| 79 | try { |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 80 | mServiceInterface.setCallServiceAdapter(callServiceAdapter); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 81 | } catch (RemoteException e) { |
| 82 | Log.e(TAG, "Failed to setCallServiceAdapter.", e); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 83 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | /** See {@link ICallService#isCompatibleWith}. */ |
| 88 | public void isCompatibleWith(CallInfo callInfo) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 89 | if (isServiceValid("isCompatibleWith")) { |
| 90 | try { |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 91 | mServiceInterface.isCompatibleWith(callInfo); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 92 | } catch (RemoteException e) { |
| 93 | Log.e(TAG, "Failed checking isCompatibleWith.", e); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 94 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | /** See {@link ICallService#call}. */ |
| 99 | public void call(CallInfo callInfo) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 100 | if (isServiceValid("call")) { |
| 101 | try { |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 102 | mServiceInterface.call(callInfo); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 103 | } catch (RemoteException e) { |
| 104 | Log.e(TAG, "Failed to place call " + callInfo.getId() + ".", e); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 105 | } |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | /** See {@link ICallService#setIncomingCallId}. */ |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame^] | 110 | public void setIncomingCallId(String callId, Bundle extras) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 111 | if (isServiceValid("setIncomingCallId")) { |
| 112 | mAdapter.addPendingIncomingCallId(callId); |
| 113 | try { |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame^] | 114 | mServiceInterface.setIncomingCallId(callId, extras); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 115 | } catch (RemoteException e) { |
| 116 | Log.e(TAG, "Failed to setIncomingCallId for call " + callId, e); |
| 117 | mAdapter.removePendingIncomingCallId(callId); |
| 118 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
| 122 | /** See {@link ICallService#disconnect}. */ |
| 123 | public void disconnect(String callId) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 124 | if (isServiceValid("disconnect")) { |
| 125 | try { |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 126 | mServiceInterface.disconnect(callId); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 127 | } catch (RemoteException e) { |
| 128 | Log.e(TAG, "Failed to disconnect call " + callId + ".", e); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 129 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 130 | } |
| 131 | } |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 132 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 133 | /** See {@link ICallService#answer}. */ |
| 134 | public void answer(String callId) { |
| 135 | if (isServiceValid("answer")) { |
| 136 | try { |
| 137 | mServiceInterface.answer(callId); |
| 138 | } catch (RemoteException e) { |
| 139 | Log.e(TAG, "Failed to answer call " + callId, e); |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 140 | } |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | /** See {@link ICallService#reject}. */ |
| 145 | public void reject(String callId) { |
| 146 | if (isServiceValid("reject")) { |
| 147 | try { |
| 148 | mServiceInterface.reject(callId); |
| 149 | } catch (RemoteException e) { |
| 150 | Log.e(TAG, "Failed to reject call " + callId, e); |
| 151 | } |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | /** |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 156 | * Starts retrieval of details for an incoming call. Details are returned through the |
| 157 | * call-service adapter using the specified call ID. Upon failure, the specified error callback |
| 158 | * is invoked. Can be invoked even when the call service is unbound. |
| 159 | * |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame^] | 160 | * @param callId The call ID used for the incoming call. |
| 161 | * @param extras The {@link CallService}-provided extras which need to be sent back. |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 162 | * @param errorCallback The callback invoked upon failure. |
| 163 | */ |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame^] | 164 | void retrieveIncomingCall(final String callId, final Bundle extras, |
| 165 | final Runnable errorCallback) { |
| 166 | |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 167 | BindCallback callback = new BindCallback() { |
| 168 | @Override public void onSuccess() { |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame^] | 169 | setIncomingCallId(callId, extras); |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 170 | } |
| 171 | @Override public void onFailure() { |
| 172 | errorCallback.run(); |
| 173 | } |
| 174 | }; |
| 175 | |
| 176 | bind(callback); |
| 177 | } |
| 178 | |
| 179 | /** |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 180 | * Cancels the incoming call for the specified call ID. |
| 181 | * TODO(santoscordon): This method should be called by IncomingCallsManager when the incoming |
| 182 | * call has failed. |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 183 | * |
| 184 | * @param callId The ID of the call. |
| 185 | */ |
| 186 | void cancelIncomingCall(String callId) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 187 | mAdapter.removePendingIncomingCallId(callId); |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 190 | /** {@inheritDoc} */ |
| 191 | @Override protected void setServiceInterface(IBinder binder) { |
| 192 | mServiceInterface = ICallService.Stub.asInterface(binder); |
| 193 | setCallServiceAdapter(mAdapter); |
| 194 | } |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 195 | |
| 196 | private boolean isServiceValid(String actionName) { |
| 197 | if (mServiceInterface != null) { |
| 198 | return true; |
| 199 | } |
| 200 | |
| 201 | Log.wtf(TAG, actionName + " invoked while service is unbound"); |
| 202 | return false; |
| 203 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 204 | } |