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