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; |
Sailesh Nepal | 9359ef7 | 2014-07-05 12:53:16 -0700 | [diff] [blame] | 20 | import android.os.Handler; |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 21 | import android.os.IBinder; |
| 22 | import android.os.RemoteException; |
Sailesh Nepal | 9359ef7 | 2014-07-05 12:53:16 -0700 | [diff] [blame] | 23 | import android.telecomm.CallServiceDescriptor; |
Sailesh Nepal | a439e1b | 2014-03-11 18:19:58 -0700 | [diff] [blame] | 24 | import android.telecomm.TelecommConstants; |
| 25 | |
| 26 | import com.android.internal.telecomm.ICallServiceLookupResponse; |
| 27 | import com.android.internal.telecomm.ICallServiceProvider; |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 28 | |
Sailesh Nepal | 9359ef7 | 2014-07-05 12:53:16 -0700 | [diff] [blame] | 29 | import java.util.HashSet; |
| 30 | import java.util.List; |
| 31 | import java.util.Set; |
| 32 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 33 | /** |
| 34 | * Wrapper for {@link ICallServiceProvider}s, handles binding to {@link ICallServiceProvider} and |
| 35 | * keeps track of when the object can safely be unbound. Other classes should not use |
| 36 | * {@link ICallServiceProvider} directly and instead should use this class to invoke methods of |
| 37 | * {@link ICallServiceProvider}. |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 38 | */ |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 39 | final class CallServiceProviderWrapper extends ServiceBinder<ICallServiceProvider> { |
Sailesh Nepal | 9359ef7 | 2014-07-05 12:53:16 -0700 | [diff] [blame] | 40 | interface LookupResponse { |
| 41 | void setCallServiceDescriptors(List<CallServiceDescriptor> descriptors); |
| 42 | } |
| 43 | |
| 44 | private class LookupResponseWrapper extends ICallServiceLookupResponse.Stub { |
| 45 | private final LookupResponse mResponse; |
| 46 | |
| 47 | LookupResponseWrapper(LookupResponse response) { |
| 48 | mResponse = response; |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public void setCallServiceDescriptors(final List<CallServiceDescriptor> descriptors) { |
| 53 | mHandler.post(new Runnable() { |
| 54 | @Override |
| 55 | public void run() { |
| 56 | if (mLookupResponses.remove(mResponse)) { |
| 57 | mResponse.setCallServiceDescriptors(descriptors); |
| 58 | } |
| 59 | } |
| 60 | }); |
| 61 | } |
| 62 | }; |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 63 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 64 | /** The actual service implementation. */ |
| 65 | private ICallServiceProvider mServiceInterface; |
Sailesh Nepal | 9359ef7 | 2014-07-05 12:53:16 -0700 | [diff] [blame] | 66 | private final Binder mBinder = new Binder(); |
| 67 | private Set<LookupResponse> mLookupResponses = new HashSet<LookupResponse>(); |
| 68 | private final Handler mHandler = new Handler(); |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 69 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 70 | /** |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 71 | * Creates a call-service provider for the specified component. |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 72 | * |
| 73 | * @param componentName The component name of the service to bind to. |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 74 | */ |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 75 | CallServiceProviderWrapper(ComponentName componentName) { |
Sailesh Nepal | a439e1b | 2014-03-11 18:19:58 -0700 | [diff] [blame] | 76 | super(TelecommConstants.ACTION_CALL_SERVICE_PROVIDER, componentName); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 79 | /** |
Sailesh Nepal | 9359ef7 | 2014-07-05 12:53:16 -0700 | [diff] [blame] | 80 | * Initiates a call-service lookup cycle, see {@link ICallServiceProvider#lookupCallServices}. |
| 81 | * Can be invoked even when the call service is unbound. |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 82 | * |
| 83 | * @param response The response object via which to return the relevant call-service |
| 84 | * implementations, if any. |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 85 | */ |
Sailesh Nepal | 9359ef7 | 2014-07-05 12:53:16 -0700 | [diff] [blame] | 86 | void lookupCallServices(final LookupResponse response) { |
| 87 | mLookupResponses.add(response); |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 88 | BindCallback callback = new BindCallback() { |
Sailesh Nepal | 9359ef7 | 2014-07-05 12:53:16 -0700 | [diff] [blame] | 89 | @Override |
| 90 | public void onSuccess() { |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 91 | if (isServiceValid("lookupCallServices")) { |
| 92 | try { |
Sailesh Nepal | 9359ef7 | 2014-07-05 12:53:16 -0700 | [diff] [blame] | 93 | mServiceInterface.lookupCallServices(new LookupResponseWrapper(response)); |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 94 | } catch (RemoteException e) { |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 97 | } |
Sailesh Nepal | 9359ef7 | 2014-07-05 12:53:16 -0700 | [diff] [blame] | 98 | |
| 99 | @Override |
| 100 | public void onFailure() { |
| 101 | if (mLookupResponses.remove(response)) { |
| 102 | response.setCallServiceDescriptors(null); |
| 103 | } |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 104 | } |
| 105 | }; |
| 106 | |
| 107 | mBinder.bind(callback); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 108 | } |
Ben Gilad | bb167cd | 2014-02-25 16:24:05 -0800 | [diff] [blame] | 109 | |
| 110 | /** {@inheritDoc} */ |
| 111 | @Override protected void setServiceInterface(IBinder binder) { |
Sailesh Nepal | 9359ef7 | 2014-07-05 12:53:16 -0700 | [diff] [blame] | 112 | if (binder == null) { |
| 113 | mServiceInterface = null; |
| 114 | |
| 115 | Set<LookupResponse> responses = mLookupResponses; |
| 116 | mLookupResponses = new HashSet<LookupResponse>(); |
| 117 | for (LookupResponse response : responses) { |
| 118 | response.setCallServiceDescriptors(null); |
| 119 | } |
| 120 | } else { |
| 121 | mServiceInterface = ICallServiceProvider.Stub.asInterface(binder); |
| 122 | } |
Ben Gilad | bb167cd | 2014-02-25 16:24:05 -0800 | [diff] [blame] | 123 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 124 | } |