blob: 1a3b3e1f89c286d3c6ea6f5e580ee477d238a7e5 [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;
Santos Cordon63aeb162014-02-10 09:20:40 -080020import android.os.IBinder;
21import android.os.RemoteException;
Sailesh Nepala439e1b2014-03-11 18:19:58 -070022import android.telecomm.TelecommConstants;
23
24import com.android.internal.telecomm.ICallServiceLookupResponse;
25import com.android.internal.telecomm.ICallServiceProvider;
Santos Cordon63aeb162014-02-10 09:20:40 -080026
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.
Santos Cordon63aeb162014-02-10 09:20:40 -080033 */
Ben Gilad61925612014-03-11 19:06:36 -070034final class CallServiceProviderWrapper extends ServiceBinder<ICallServiceProvider> {
35 /**
36 * The service action used to bind to ICallServiceProvider implementations.
37 * TODO(santoscordon): Move this to TelecommConstants.
38 */
39 static final String CALL_SERVICE_PROVIDER_ACTION = ICallServiceProvider.class.getName();
40
Santos Cordon63aeb162014-02-10 09:20:40 -080041 /** The actual service implementation. */
42 private ICallServiceProvider mServiceInterface;
43
Ben Gilad61925612014-03-11 19:06:36 -070044 private Binder mBinder = new Binder();
45
Santos Cordon63aeb162014-02-10 09:20:40 -080046 /**
Santos Cordon63aeb162014-02-10 09:20:40 -080047 * Creates a call-service provider for the specified component.
Santos Cordonc195e362014-02-11 17:05:31 -080048 *
49 * @param componentName The component name of the service to bind to.
Santos Cordon63aeb162014-02-10 09:20:40 -080050 */
Ben Gilad61925612014-03-11 19:06:36 -070051 CallServiceProviderWrapper(ComponentName componentName) {
Sailesh Nepala439e1b2014-03-11 18:19:58 -070052 super(TelecommConstants.ACTION_CALL_SERVICE_PROVIDER, componentName);
Santos Cordon63aeb162014-02-10 09:20:40 -080053 }
54
Santos Cordonc195e362014-02-11 17:05:31 -080055 /**
Ben Gilad61925612014-03-11 19:06:36 -070056 * initiates a call-service lookup cycle, see {@link ICallServiceProvider#lookupCallServices}.
57 * Upon failure, the specified error callback is invoked. Can be invoked even when the call
58 * service is unbound.
59 *
60 * @param response The response object via which to return the relevant call-service
61 * implementations, if any.
62 * @param errorCallback The callback to invoke upon failure.
Santos Cordonc195e362014-02-11 17:05:31 -080063 */
Ben Gilad61925612014-03-11 19:06:36 -070064 void lookupCallServices(
65 final ICallServiceLookupResponse response,
66 final Runnable errorCallback) {
67
68 BindCallback callback = new BindCallback() {
69 @Override public void onSuccess() {
70 if (isServiceValid("lookupCallServices")) {
71 try {
72 mServiceInterface.lookupCallServices(response);
73 } catch (RemoteException e) {
74 Log.e(CallServiceProviderWrapper.this, e, "Failed to lookupCallServices.");
75 }
76 }
Santos Cordon63aeb162014-02-10 09:20:40 -080077 }
Ben Gilad61925612014-03-11 19:06:36 -070078 @Override public void onFailure() {
79 errorCallback.run();
80 }
81 };
82
83 mBinder.bind(callback);
Santos Cordon63aeb162014-02-10 09:20:40 -080084 }
Ben Giladbb167cd2014-02-25 16:24:05 -080085
86 /** {@inheritDoc} */
87 @Override protected void setServiceInterface(IBinder binder) {
88 mServiceInterface = ICallServiceProvider.Stub.asInterface(binder);
89 }
Santos Cordon63aeb162014-02-10 09:20:40 -080090}