blob: 044d370db9228421469599688684bf74295c943b [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;
20import android.os.Binder;
21import android.os.IBinder;
22import android.os.RemoteException;
23import android.telecomm.ICallServiceLookupResponse;
24import android.telecomm.ICallServiceProvider;
25import 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 */
35public 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 /**
48 * The class to notify when the binding succeeds or fails.
49 */
50 private final CallServiceFinder mFinder;
51
52 /**
53 * Creates a call-service provider for the specified component.
54 */
55 public CallServiceProviderWrapper(ComponentName componentName, CallServiceFinder finder) {
56 super(CALL_SERVICE_PROVIDER_ACTION, componentName);
57 mFinder = finder;
58 }
59
60 /** {@inheritDoc} */
61 @Override public void handleSuccessfulConnection(IBinder binder) {
62 mServiceInterface = ICallServiceProvider.Stub.asInterface(binder);
63 mFinder.registerProvider(getComponentName(), this);
64 }
65
66 /** {@inheritDoc} */
67 @Override public void handleFailedConnection() {
68 mFinder.unregisterProvider(getComponentName());
69 // TODO(santoscordon): Notify CallServiceFinder.
70 }
71
72 /** {@inheritDoc} */
73 @Override public void handleServiceDisconnected() {
74 mServiceInterface = null;
75 // TODO(santoscordon): fill in.
76 }
77
78 /** See {@link ICallServiceProvider#lookupCallServices}. */
79 public void lookupCallServices(ICallServiceLookupResponse response) {
80 try {
81 if (mServiceInterface == null) {
82 Log.wtf(TAG, "lookupCallServices() invoked while the service is unbound.");
83 } else {
84 mServiceInterface.lookupCallServices(response);
85 }
86 } catch (RemoteException e) {
87 Log.e(TAG, "Failed to lookupCallServices.", e);
88 }
89 }
90}