blob: 22b8187b3f17a68ff78d50ee7c55ca27e343165b [file] [log] [blame]
Santos Cordon63aeb162014-02-10 09:20:40 -08001/*
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
17package com.android.telecomm;
18
19import android.content.ComponentName;
Sailesh Nepal9359ef72014-07-05 12:53:16 -070020import android.os.Handler;
Santos Cordon63aeb162014-02-10 09:20:40 -080021import android.os.IBinder;
22import android.os.RemoteException;
Sailesh Nepal9359ef72014-07-05 12:53:16 -070023import android.telecomm.CallServiceDescriptor;
Sailesh Nepala439e1b2014-03-11 18:19:58 -070024import android.telecomm.TelecommConstants;
25
26import com.android.internal.telecomm.ICallServiceLookupResponse;
27import com.android.internal.telecomm.ICallServiceProvider;
Santos Cordon63aeb162014-02-10 09:20:40 -080028
Sailesh Nepal9359ef72014-07-05 12:53:16 -070029import java.util.HashSet;
30import java.util.List;
31import java.util.Set;
32
Santos Cordon63aeb162014-02-10 09:20:40 -080033/**
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 Cordon63aeb162014-02-10 09:20:40 -080038 */
Ben Gilad61925612014-03-11 19:06:36 -070039final class CallServiceProviderWrapper extends ServiceBinder<ICallServiceProvider> {
Sailesh Nepal9359ef72014-07-05 12:53:16 -070040 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 Gilad61925612014-03-11 19:06:36 -070063
Santos Cordon63aeb162014-02-10 09:20:40 -080064 /** The actual service implementation. */
65 private ICallServiceProvider mServiceInterface;
Sailesh Nepal9359ef72014-07-05 12:53:16 -070066 private final Binder mBinder = new Binder();
67 private Set<LookupResponse> mLookupResponses = new HashSet<LookupResponse>();
68 private final Handler mHandler = new Handler();
Ben Gilad61925612014-03-11 19:06:36 -070069
Santos Cordon63aeb162014-02-10 09:20:40 -080070 /**
Santos Cordon63aeb162014-02-10 09:20:40 -080071 * Creates a call-service provider for the specified component.
Santos Cordonc195e362014-02-11 17:05:31 -080072 *
73 * @param componentName The component name of the service to bind to.
Santos Cordon63aeb162014-02-10 09:20:40 -080074 */
Ben Gilad61925612014-03-11 19:06:36 -070075 CallServiceProviderWrapper(ComponentName componentName) {
Sailesh Nepala439e1b2014-03-11 18:19:58 -070076 super(TelecommConstants.ACTION_CALL_SERVICE_PROVIDER, componentName);
Santos Cordon63aeb162014-02-10 09:20:40 -080077 }
78
Santos Cordonc195e362014-02-11 17:05:31 -080079 /**
Sailesh Nepal9359ef72014-07-05 12:53:16 -070080 * Initiates a call-service lookup cycle, see {@link ICallServiceProvider#lookupCallServices}.
81 * Can be invoked even when the call service is unbound.
Ben Gilad61925612014-03-11 19:06:36 -070082 *
83 * @param response The response object via which to return the relevant call-service
84 * implementations, if any.
Santos Cordonc195e362014-02-11 17:05:31 -080085 */
Sailesh Nepal9359ef72014-07-05 12:53:16 -070086 void lookupCallServices(final LookupResponse response) {
87 mLookupResponses.add(response);
Ben Gilad61925612014-03-11 19:06:36 -070088 BindCallback callback = new BindCallback() {
Sailesh Nepal9359ef72014-07-05 12:53:16 -070089 @Override
90 public void onSuccess() {
Ben Gilad61925612014-03-11 19:06:36 -070091 if (isServiceValid("lookupCallServices")) {
92 try {
Sailesh Nepal9359ef72014-07-05 12:53:16 -070093 mServiceInterface.lookupCallServices(new LookupResponseWrapper(response));
Ben Gilad61925612014-03-11 19:06:36 -070094 } catch (RemoteException e) {
Ben Gilad61925612014-03-11 19:06:36 -070095 }
96 }
Santos Cordon63aeb162014-02-10 09:20:40 -080097 }
Sailesh Nepal9359ef72014-07-05 12:53:16 -070098
99 @Override
100 public void onFailure() {
101 if (mLookupResponses.remove(response)) {
102 response.setCallServiceDescriptors(null);
103 }
Ben Gilad61925612014-03-11 19:06:36 -0700104 }
105 };
106
107 mBinder.bind(callback);
Santos Cordon63aeb162014-02-10 09:20:40 -0800108 }
Ben Giladbb167cd2014-02-25 16:24:05 -0800109
110 /** {@inheritDoc} */
111 @Override protected void setServiceInterface(IBinder binder) {
Sailesh Nepal9359ef72014-07-05 12:53:16 -0700112 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 Giladbb167cd2014-02-25 16:24:05 -0800123 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800124}