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.Binder; |
| 21 | import android.os.IBinder; |
| 22 | import android.os.RemoteException; |
| 23 | import android.telecomm.ICallServiceLookupResponse; |
| 24 | import android.telecomm.ICallServiceProvider; |
| 25 | import android.util.Log; |
| 26 | |
| 27 | /** |
| 28 | * Wrapper for {@link ICallServiceProvider}s, handles binding to {@link ICallServiceProvider} and |
| 29 | * keeps track of when the object can safely be unbound. Other classes should not use |
| 30 | * {@link ICallServiceProvider} directly and instead should use this class to invoke methods of |
| 31 | * {@link ICallServiceProvider}. |
| 32 | * TODO(santoscordon): Keep track of when the service can be safely unbound. |
| 33 | * TODO(santoscordon): Look into combining with android.telecomm.CallServiceProvider. |
| 34 | */ |
| 35 | public class CallServiceProviderWrapper extends ServiceBinder<ICallServiceProvider> { |
| 36 | /** |
| 37 | * The service action used to bind to ICallServiceProvider implementations. |
| 38 | * TODO(santoscordon): Move this to TelecommConstants. |
| 39 | */ |
| 40 | static final String CALL_SERVICE_PROVIDER_ACTION = ICallServiceProvider.class.getName(); |
| 41 | |
| 42 | private static final String TAG = CallServiceProviderWrapper.class.getSimpleName(); |
| 43 | |
| 44 | /** The actual service implementation. */ |
| 45 | private ICallServiceProvider mServiceInterface; |
| 46 | |
| 47 | /** |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame^] | 48 | * The class to notify when binding succeeds or fails. |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 49 | */ |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame^] | 50 | private final CallServiceRepository mRepository; |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Creates a call-service provider for the specified component. |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame^] | 54 | * |
| 55 | * @param componentName The component name of the service to bind to. |
| 56 | * @param repository The call-service repository. |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 57 | */ |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame^] | 58 | public CallServiceProviderWrapper(ComponentName componentName, CallServiceRepository repository) { |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 59 | super(CALL_SERVICE_PROVIDER_ACTION, componentName); |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame^] | 60 | mRepository = repository; |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** {@inheritDoc} */ |
| 64 | @Override public void handleSuccessfulConnection(IBinder binder) { |
| 65 | mServiceInterface = ICallServiceProvider.Stub.asInterface(binder); |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame^] | 66 | mRepository.processProvider(getComponentName(), this); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** {@inheritDoc} */ |
| 70 | @Override public void handleFailedConnection() { |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame^] | 71 | mRepository.abortProvider(getComponentName()); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** {@inheritDoc} */ |
| 75 | @Override public void handleServiceDisconnected() { |
| 76 | mServiceInterface = null; |
| 77 | // TODO(santoscordon): fill in. |
| 78 | } |
| 79 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame^] | 80 | /** |
| 81 | * See {@link ICallServiceProvider#lookupCallServices}. |
| 82 | */ |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 83 | public void lookupCallServices(ICallServiceLookupResponse response) { |
| 84 | try { |
| 85 | if (mServiceInterface == null) { |
| 86 | Log.wtf(TAG, "lookupCallServices() invoked while the service is unbound."); |
| 87 | } else { |
| 88 | mServiceInterface.lookupCallServices(response); |
| 89 | } |
| 90 | } catch (RemoteException e) { |
| 91 | Log.e(TAG, "Failed to lookupCallServices.", e); |
| 92 | } |
| 93 | } |
| 94 | } |