blob: 2dc5170ebffeefac6951790f2ecb855d34d1d2d1 [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 /**
69 * Initializes the underlying call-service implementation upon successful binding.
70 *
71 * {@inheritDoc}
72 */
Santos Cordon63aeb162014-02-10 09:20:40 -080073 @Override public void handleSuccessfulConnection(IBinder binder) {
74 mServiceInterface = ICallService.Stub.asInterface(binder);
Santos Cordonc195e362014-02-11 17:05:31 -080075 setCallServiceAdapter(mAdapter);
Santos Cordon63aeb162014-02-10 09:20:40 -080076 }
77
78 /** {@inheritDoc} */
79 @Override public void handleFailedConnection() {
Santos Cordonc195e362014-02-11 17:05:31 -080080 // TODO(santoscordon): fill in
Santos Cordon63aeb162014-02-10 09:20:40 -080081 }
82
83 /** {@inheritDoc} */
84 @Override public void handleServiceDisconnected() {
85 mServiceInterface = null;
86 // TODO(santoscordon): fill in.
87 }
88
Santos Cordonc195e362014-02-11 17:05:31 -080089 public CallServiceInfo getInfo() {
90 return mCallServiceInfo;
91 }
92
Santos Cordon63aeb162014-02-10 09:20:40 -080093 /** See {@link ICallService#setCallServiceAdapter}. */
94 public void setCallServiceAdapter(ICallServiceAdapter callServiceAdapter) {
95 try {
96 if (mServiceInterface == null) {
97 Log.wtf(TAG, "setCallServiceAdapter() invoked while the service is unbound.");
98 } else {
99 mServiceInterface.setCallServiceAdapter(callServiceAdapter);
100 }
101 } catch (RemoteException e) {
102 Log.e(TAG, "Failed to setCallServiceAdapter.", e);
103 }
104 }
105
106 /** See {@link ICallService#isCompatibleWith}. */
107 public void isCompatibleWith(CallInfo callInfo) {
108 try {
109 if (mServiceInterface == null) {
110 Log.wtf(TAG, "isCompatibleWith() invoked while the service is unbound.");
111 } else {
112 mServiceInterface.isCompatibleWith(callInfo);
113 }
114 } catch (RemoteException e) {
115 Log.e(TAG, "Failed checking isCompatibleWith.", e);
116 }
117 }
118
119 /** See {@link ICallService#call}. */
120 public void call(CallInfo callInfo) {
121 try {
122 if (mServiceInterface == null) {
123 Log.wtf(TAG, "call() invoked while the service is unbound.");
124 } else {
125 mServiceInterface.call(callInfo);
126 }
127 } catch (RemoteException e) {
128 Log.e(TAG, "Failed to place call " + callInfo.getId() + ".", e);
129 }
130 }
131
132 /** See {@link ICallService#disconnect}. */
133 public void disconnect(String callId) {
134 try {
135 if (mServiceInterface == null) {
136 Log.wtf(TAG, "disconnect() invoked while the service is unbound.");
137 } else {
138 mServiceInterface.disconnect(callId);
139 }
140 } catch (RemoteException e) {
141 Log.e(TAG, "Failed to disconnect call " + callId + ".", e);
142 }
143 }
144}