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 | |
| 19 | import android.content.ComponentName; |
| 20 | import android.os.IBinder; |
| 21 | import android.os.RemoteException; |
| 22 | import android.telecomm.CallInfo; |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 23 | import android.telecomm.CallServiceInfo; |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 24 | import android.telecomm.ICallService; |
| 25 | import android.telecomm.ICallServiceAdapter; |
| 26 | import android.util.Log; |
| 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 | private static final String TAG = CallServiceWrapper.class.getSimpleName(); |
| 44 | |
| 45 | /** The descriptor of this call service as supplied by the call-service provider. */ |
| 46 | private final CallServiceInfo mCallServiceInfo; |
| 47 | |
| 48 | /** |
| 49 | * The adapter used by the underlying call-service implementation to communicate with Telecomm. |
| 50 | */ |
| 51 | private final CallServiceAdapter mAdapter; |
| 52 | |
| 53 | /** The actual service implementation. */ |
| 54 | private ICallService mServiceInterface; |
| 55 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 56 | /** |
| 57 | * Creates a call-service provider for the specified component. |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 58 | * |
| 59 | * @param info The call-service descriptor from {@link ICallServiceProvider#lookupCallServices}. |
| 60 | * @param adapter The call-service adapter. |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 61 | */ |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 62 | public CallServiceWrapper(CallServiceInfo info, CallServiceAdapter adapter) { |
| 63 | super(CALL_SERVICE_ACTION, info.getServiceComponent()); |
| 64 | mCallServiceInfo = info; |
| 65 | mAdapter = adapter; |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 68 | public CallServiceInfo getInfo() { |
| 69 | return mCallServiceInfo; |
| 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) { |
| 74 | try { |
| 75 | if (mServiceInterface == null) { |
| 76 | Log.wtf(TAG, "setCallServiceAdapter() invoked while the service is unbound."); |
| 77 | } else { |
| 78 | mServiceInterface.setCallServiceAdapter(callServiceAdapter); |
| 79 | } |
| 80 | } catch (RemoteException e) { |
| 81 | Log.e(TAG, "Failed to setCallServiceAdapter.", e); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /** See {@link ICallService#isCompatibleWith}. */ |
| 86 | public void isCompatibleWith(CallInfo callInfo) { |
| 87 | try { |
| 88 | if (mServiceInterface == null) { |
| 89 | Log.wtf(TAG, "isCompatibleWith() invoked while the service is unbound."); |
| 90 | } else { |
| 91 | mServiceInterface.isCompatibleWith(callInfo); |
| 92 | } |
| 93 | } catch (RemoteException e) { |
| 94 | Log.e(TAG, "Failed checking isCompatibleWith.", e); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** See {@link ICallService#call}. */ |
| 99 | public void call(CallInfo callInfo) { |
| 100 | try { |
| 101 | if (mServiceInterface == null) { |
| 102 | Log.wtf(TAG, "call() invoked while the service is unbound."); |
| 103 | } else { |
| 104 | mServiceInterface.call(callInfo); |
| 105 | } |
| 106 | } catch (RemoteException e) { |
| 107 | Log.e(TAG, "Failed to place call " + callInfo.getId() + ".", e); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** See {@link ICallService#disconnect}. */ |
| 112 | public void disconnect(String callId) { |
| 113 | try { |
| 114 | if (mServiceInterface == null) { |
| 115 | Log.wtf(TAG, "disconnect() invoked while the service is unbound."); |
| 116 | } else { |
| 117 | mServiceInterface.disconnect(callId); |
| 118 | } |
| 119 | } catch (RemoteException e) { |
| 120 | Log.e(TAG, "Failed to disconnect call " + callId + ".", e); |
| 121 | } |
| 122 | } |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 123 | |
| 124 | /** {@inheritDoc} */ |
| 125 | @Override protected void setServiceInterface(IBinder binder) { |
| 126 | mServiceInterface = ICallService.Stub.asInterface(binder); |
| 127 | setCallServiceAdapter(mAdapter); |
| 128 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 129 | } |