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