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