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