blob: 74277c4cd1c74cb4e2c51973067ec8b17f36ca7f [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.IBinder;
21import android.os.RemoteException;
22import android.telecomm.CallInfo;
Santos Cordonc195e362014-02-11 17:05:31 -080023import android.telecomm.CallServiceInfo;
Santos Cordon63aeb162014-02-10 09:20:40 -080024import android.telecomm.ICallService;
25import android.telecomm.ICallServiceAdapter;
26import android.util.Log;
27
28/**
29 * Wrapper for {@link ICallService}s, handles binding to {@link ICallService} and keeps track of
30 * when the object can safely be unbound. Other classes should not use {@link ICallService} directly
31 * and instead should use this class to invoke methods of {@link ICallService}.
32 * TODO(santoscordon): Keep track of when the service can be safely unbound.
33 * TODO(santoscordon): Look into combining with android.telecomm.CallService.
34 */
35public class CallServiceWrapper extends ServiceBinder<ICallService> {
Santos Cordon63aeb162014-02-10 09:20:40 -080036
37 /**
38 * The service action used to bind to ICallService implementations.
39 * TODO(santoscordon): Move this to TelecommConstants.
40 */
41 static final String CALL_SERVICE_ACTION = ICallService.class.getName();
42
Santos Cordonc195e362014-02-11 17:05:31 -080043 private static final String TAG = CallServiceWrapper.class.getSimpleName();
44
45 /** The descriptor of this call service as supplied by the call-service provider. */
46 private final CallServiceInfo mCallServiceInfo;
47
48 /**
49 * The adapter used by the underlying call-service implementation to communicate with Telecomm.
50 */
51 private final CallServiceAdapter mAdapter;
52
53 /** The actual service implementation. */
54 private ICallService mServiceInterface;
55
Santos Cordon63aeb162014-02-10 09:20:40 -080056 /**
57 * Creates a call-service provider for the specified component.
Santos Cordonc195e362014-02-11 17:05:31 -080058 *
59 * @param info The call-service descriptor from {@link ICallServiceProvider#lookupCallServices}.
60 * @param adapter The call-service adapter.
Santos Cordon63aeb162014-02-10 09:20:40 -080061 */
Santos Cordonc195e362014-02-11 17:05:31 -080062 public CallServiceWrapper(CallServiceInfo info, CallServiceAdapter adapter) {
63 super(CALL_SERVICE_ACTION, info.getServiceComponent());
64 mCallServiceInfo = info;
65 mAdapter = adapter;
Santos Cordon63aeb162014-02-10 09:20:40 -080066 }
67
Santos Cordonc195e362014-02-11 17:05:31 -080068 public CallServiceInfo getInfo() {
69 return mCallServiceInfo;
70 }
71
Santos Cordon63aeb162014-02-10 09:20:40 -080072 /** See {@link ICallService#setCallServiceAdapter}. */
73 public void setCallServiceAdapter(ICallServiceAdapter callServiceAdapter) {
74 try {
75 if (mServiceInterface == null) {
76 Log.wtf(TAG, "setCallServiceAdapter() invoked while the service is unbound.");
77 } else {
78 mServiceInterface.setCallServiceAdapter(callServiceAdapter);
79 }
80 } catch (RemoteException e) {
81 Log.e(TAG, "Failed to setCallServiceAdapter.", e);
82 }
83 }
84
85 /** See {@link ICallService#isCompatibleWith}. */
86 public void isCompatibleWith(CallInfo callInfo) {
87 try {
88 if (mServiceInterface == null) {
89 Log.wtf(TAG, "isCompatibleWith() invoked while the service is unbound.");
90 } else {
91 mServiceInterface.isCompatibleWith(callInfo);
92 }
93 } catch (RemoteException e) {
94 Log.e(TAG, "Failed checking isCompatibleWith.", e);
95 }
96 }
97
98 /** See {@link ICallService#call}. */
99 public void call(CallInfo callInfo) {
100 try {
101 if (mServiceInterface == null) {
102 Log.wtf(TAG, "call() invoked while the service is unbound.");
103 } else {
104 mServiceInterface.call(callInfo);
105 }
106 } catch (RemoteException e) {
107 Log.e(TAG, "Failed to place call " + callInfo.getId() + ".", e);
108 }
109 }
110
111 /** See {@link ICallService#disconnect}. */
112 public void disconnect(String callId) {
113 try {
114 if (mServiceInterface == null) {
115 Log.wtf(TAG, "disconnect() invoked while the service is unbound.");
116 } else {
117 mServiceInterface.disconnect(callId);
118 }
119 } catch (RemoteException e) {
120 Log.e(TAG, "Failed to disconnect call " + callId + ".", e);
121 }
122 }
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800123
124 /** {@inheritDoc} */
125 @Override protected void setServiceInterface(IBinder binder) {
126 mServiceInterface = ICallService.Stub.asInterface(binder);
127 setCallServiceAdapter(mAdapter);
128 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800129}