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; |
| 23 | import android.telecomm.ICallService; |
| 24 | import android.telecomm.ICallServiceAdapter; |
| 25 | import android.util.Log; |
| 26 | |
| 27 | /** |
| 28 | * Wrapper for {@link ICallService}s, handles binding to {@link ICallService} and keeps track of |
| 29 | * when the object can safely be unbound. Other classes should not use {@link ICallService} directly |
| 30 | * and instead should use this class to invoke methods of {@link ICallService}. |
| 31 | * TODO(santoscordon): Keep track of when the service can be safely unbound. |
| 32 | * TODO(santoscordon): Look into combining with android.telecomm.CallService. |
| 33 | */ |
| 34 | public class CallServiceWrapper extends ServiceBinder<ICallService> { |
| 35 | private static final String TAG = CallServiceWrapper.class.getSimpleName(); |
| 36 | |
| 37 | /** The actual service implementation. */ |
| 38 | private ICallService mServiceInterface; |
| 39 | |
| 40 | /** |
| 41 | * The service action used to bind to ICallService implementations. |
| 42 | * TODO(santoscordon): Move this to TelecommConstants. |
| 43 | */ |
| 44 | static final String CALL_SERVICE_ACTION = ICallService.class.getName(); |
| 45 | |
| 46 | /** |
| 47 | * Creates a call-service provider for the specified component. |
| 48 | */ |
| 49 | public CallServiceWrapper(ComponentName componentName) { |
| 50 | super(CALL_SERVICE_ACTION, componentName); |
| 51 | } |
| 52 | |
| 53 | /** {@inheritDoc} */ |
| 54 | @Override public void handleSuccessfulConnection(IBinder binder) { |
| 55 | mServiceInterface = ICallService.Stub.asInterface(binder); |
| 56 | // TODO(santoscordon): Notify CallServiceFinder. |
| 57 | } |
| 58 | |
| 59 | /** {@inheritDoc} */ |
| 60 | @Override public void handleFailedConnection() { |
| 61 | // TODO(santoscordon): Notify CallServiceFinder. |
| 62 | } |
| 63 | |
| 64 | /** {@inheritDoc} */ |
| 65 | @Override public void handleServiceDisconnected() { |
| 66 | mServiceInterface = null; |
| 67 | // TODO(santoscordon): fill in. |
| 68 | } |
| 69 | |
| 70 | /** See {@link ICallService#setCallServiceAdapter}. */ |
| 71 | public void setCallServiceAdapter(ICallServiceAdapter callServiceAdapter) { |
| 72 | try { |
| 73 | if (mServiceInterface == null) { |
| 74 | Log.wtf(TAG, "setCallServiceAdapter() invoked while the service is unbound."); |
| 75 | } else { |
| 76 | mServiceInterface.setCallServiceAdapter(callServiceAdapter); |
| 77 | } |
| 78 | } catch (RemoteException e) { |
| 79 | Log.e(TAG, "Failed to setCallServiceAdapter.", e); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** See {@link ICallService#isCompatibleWith}. */ |
| 84 | public void isCompatibleWith(CallInfo callInfo) { |
| 85 | try { |
| 86 | if (mServiceInterface == null) { |
| 87 | Log.wtf(TAG, "isCompatibleWith() invoked while the service is unbound."); |
| 88 | } else { |
| 89 | mServiceInterface.isCompatibleWith(callInfo); |
| 90 | } |
| 91 | } catch (RemoteException e) { |
| 92 | Log.e(TAG, "Failed checking isCompatibleWith.", e); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** See {@link ICallService#call}. */ |
| 97 | public void call(CallInfo callInfo) { |
| 98 | try { |
| 99 | if (mServiceInterface == null) { |
| 100 | Log.wtf(TAG, "call() invoked while the service is unbound."); |
| 101 | } else { |
| 102 | mServiceInterface.call(callInfo); |
| 103 | } |
| 104 | } catch (RemoteException e) { |
| 105 | Log.e(TAG, "Failed to place call " + callInfo.getId() + ".", e); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** See {@link ICallService#disconnect}. */ |
| 110 | public void disconnect(String callId) { |
| 111 | try { |
| 112 | if (mServiceInterface == null) { |
| 113 | Log.wtf(TAG, "disconnect() invoked while the service is unbound."); |
| 114 | } else { |
| 115 | mServiceInterface.disconnect(callId); |
| 116 | } |
| 117 | } catch (RemoteException e) { |
| 118 | Log.e(TAG, "Failed to disconnect call " + callId + ".", e); |
| 119 | } |
| 120 | } |
| 121 | } |