blob: 941c6eebd81e1642e081232667ca77388fd59200 [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
Santos Cordon63aeb162014-02-10 09:20:40 -080019import android.os.IBinder;
20import android.os.RemoteException;
21import android.telecomm.CallInfo;
Ben Giladc5b22692014-02-18 20:03:22 -080022import android.telecomm.CallServiceDescriptor;
Santos Cordon63aeb162014-02-10 09:20:40 -080023import android.telecomm.ICallService;
24import android.telecomm.ICallServiceAdapter;
25import android.util.Log;
26
27/**
28 * Wrapper for {@link ICallService}s, handles binding to {@link ICallService} and keeps track of
29 * when the object can safely be unbound. Other classes should not use {@link ICallService} directly
30 * and instead should use this class to invoke methods of {@link ICallService}.
31 * TODO(santoscordon): Keep track of when the service can be safely unbound.
32 * TODO(santoscordon): Look into combining with android.telecomm.CallService.
33 */
34public class CallServiceWrapper extends ServiceBinder<ICallService> {
Santos Cordon63aeb162014-02-10 09:20:40 -080035
36 /**
37 * The service action used to bind to ICallService implementations.
38 * TODO(santoscordon): Move this to TelecommConstants.
39 */
40 static final String CALL_SERVICE_ACTION = ICallService.class.getName();
41
Santos Cordonc195e362014-02-11 17:05:31 -080042 private static final String TAG = CallServiceWrapper.class.getSimpleName();
43
44 /** The descriptor of this call service as supplied by the call-service provider. */
Ben Giladc5b22692014-02-18 20:03:22 -080045 private final CallServiceDescriptor mDescriptor;
Santos Cordonc195e362014-02-11 17:05:31 -080046
47 /**
48 * The adapter used by the underlying call-service implementation to communicate with Telecomm.
49 */
50 private final CallServiceAdapter mAdapter;
51
52 /** The actual service implementation. */
53 private ICallService mServiceInterface;
54
Santos Cordon63aeb162014-02-10 09:20:40 -080055 /**
56 * Creates a call-service provider for the specified component.
Santos Cordonc195e362014-02-11 17:05:31 -080057 *
Ben Giladc5b22692014-02-18 20:03:22 -080058 * @param descriptor The call-service descriptor from {@link ICallServiceProvider#lookupCallServices}.
Santos Cordonc195e362014-02-11 17:05:31 -080059 * @param adapter The call-service adapter.
Santos Cordon63aeb162014-02-10 09:20:40 -080060 */
Ben Giladc5b22692014-02-18 20:03:22 -080061 public CallServiceWrapper(CallServiceDescriptor descriptor, CallServiceAdapter adapter) {
62 super(CALL_SERVICE_ACTION, descriptor.getServiceComponent());
63 mDescriptor = descriptor;
Santos Cordonc195e362014-02-11 17:05:31 -080064 mAdapter = adapter;
Santos Cordon63aeb162014-02-10 09:20:40 -080065 }
66
Ben Giladc5b22692014-02-18 20:03:22 -080067 public CallServiceDescriptor getDescriptor() {
68 return mDescriptor;
Santos Cordonc195e362014-02-11 17:05:31 -080069 }
70
Santos Cordon63aeb162014-02-10 09:20:40 -080071 /** See {@link ICallService#setCallServiceAdapter}. */
72 public void setCallServiceAdapter(ICallServiceAdapter callServiceAdapter) {
73 try {
74 if (mServiceInterface == null) {
75 Log.wtf(TAG, "setCallServiceAdapter() invoked while the service is unbound.");
76 } else {
77 mServiceInterface.setCallServiceAdapter(callServiceAdapter);
78 }
79 } catch (RemoteException e) {
80 Log.e(TAG, "Failed to setCallServiceAdapter.", e);
81 }
82 }
83
84 /** See {@link ICallService#isCompatibleWith}. */
85 public void isCompatibleWith(CallInfo callInfo) {
86 try {
87 if (mServiceInterface == null) {
88 Log.wtf(TAG, "isCompatibleWith() invoked while the service is unbound.");
89 } else {
90 mServiceInterface.isCompatibleWith(callInfo);
91 }
92 } catch (RemoteException e) {
93 Log.e(TAG, "Failed checking isCompatibleWith.", e);
94 }
95 }
96
97 /** See {@link ICallService#call}. */
98 public void call(CallInfo callInfo) {
99 try {
100 if (mServiceInterface == null) {
101 Log.wtf(TAG, "call() invoked while the service is unbound.");
102 } else {
103 mServiceInterface.call(callInfo);
104 }
105 } catch (RemoteException e) {
106 Log.e(TAG, "Failed to place call " + callInfo.getId() + ".", e);
107 }
108 }
109
110 /** See {@link ICallService#disconnect}. */
111 public void disconnect(String callId) {
112 try {
113 if (mServiceInterface == null) {
114 Log.wtf(TAG, "disconnect() invoked while the service is unbound.");
115 } else {
116 mServiceInterface.disconnect(callId);
117 }
118 } catch (RemoteException e) {
119 Log.e(TAG, "Failed to disconnect call " + callId + ".", e);
120 }
121 }
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800122
Santos Cordon7917d382014-02-14 02:31:18 -0800123 /** See {@link ICallService#confirmIncomingCall}. */
124 public void confirmIncomingCall(String callId, String callToken) {
125 try {
126 if (mServiceInterface == null) {
127 Log.wtf(TAG, "confirmIncomingCall() invoked while service in unbound.");
128 } else {
129 mAdapter.addUnconfirmedIncomingCallId(callId);
130 mServiceInterface.confirmIncomingCall(callId, callToken);
131 }
132 } catch (RemoteException e) {
133 Log.e(TAG, "Failed to confirmIncomingCall for call " + callId, e);
134 mAdapter.removeUnconfirmedIncomingCallId(callId);
135 }
136 }
137
138 /**
139 * Cancels the an incoming call confirmation for the specified call ID.
140 * TODO(santoscordon): This method should be called by IncomingCallManager when the incoming
141 * call confirmation has failed.
142 *
143 * @param callId The ID of the call.
144 */
145 void cancelIncomingCall(String callId) {
146 mAdapter.removeUnconfirmedIncomingCallId(callId);
147 }
148
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800149 /** {@inheritDoc} */
150 @Override protected void setServiceInterface(IBinder binder) {
151 mServiceInterface = ICallService.Stub.asInterface(binder);
152 setCallServiceAdapter(mAdapter);
153 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800154}