blob: e5b8256c1331e115d0287e4df519e9d50a2a9028 [file] [log] [blame]
Santos Cordone3d76ab2014-01-28 17:25:20 -08001/*
2 * Copyright (C) 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.content.Context;
21import android.content.Intent;
22import android.content.ServiceConnection;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070023import android.net.Uri;
Santos Cordone3d76ab2014-01-28 17:25:20 -080024import android.os.IBinder;
25import android.os.RemoteException;
Amith Yamasani60e75842014-05-23 10:09:14 -070026import android.os.UserHandle;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070027import android.telecomm.CallAudioState;
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070028import android.telecomm.CallCapabilities;
29import android.telecomm.CallServiceDescriptor;
30import android.telecomm.CallState;
31import android.telecomm.InCallCall;
Sailesh Nepal810735e2014-03-18 18:15:46 -070032import android.telecomm.CallState;
Sailesh Nepala439e1b2014-03-11 18:19:58 -070033
34import com.android.internal.telecomm.IInCallService;
Sailesh Nepal810735e2014-03-18 18:15:46 -070035import com.google.common.collect.ImmutableCollection;
Santos Cordone3d76ab2014-01-28 17:25:20 -080036
37/**
38 * Binds to {@link IInCallService} and provides the service to {@link CallsManager} through which it
39 * can send updates to the in-call app. This class is created and owned by CallsManager and retains
Sailesh Nepale59bb192014-04-01 18:33:59 -070040 * a binding to the {@link IInCallService} (implemented by the in-call app).
Santos Cordone3d76ab2014-01-28 17:25:20 -080041 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070042public final class InCallController extends CallsManagerListenerBase {
Santos Cordone3d76ab2014-01-28 17:25:20 -080043 /**
44 * Used to bind to the in-call app and triggers the start of communication between
Sailesh Nepale59bb192014-04-01 18:33:59 -070045 * this class and in-call app.
Santos Cordone3d76ab2014-01-28 17:25:20 -080046 */
47 private class InCallServiceConnection implements ServiceConnection {
48 /** {@inheritDoc} */
49 @Override public void onServiceConnected(ComponentName name, IBinder service) {
50 onConnected(service);
51 }
52
53 /** {@inheritDoc} */
54 @Override public void onServiceDisconnected(ComponentName name) {
55 onDisconnected();
56 }
57 }
58
Santos Cordone3d76ab2014-01-28 17:25:20 -080059 /**
60 * Package name of the in-call app. Although in-call code in kept in its own namespace, it is
Ben Gilad13329fd2014-02-11 17:20:29 -080061 * ultimately compiled into the dialer APK, hence the difference in namespaces between this and
62 * {@link #IN_CALL_SERVICE_CLASS_NAME}.
Santos Cordone3d76ab2014-01-28 17:25:20 -080063 * TODO(santoscordon): Change this into config.xml resource entry.
64 */
65 private static final String IN_CALL_PACKAGE_NAME = "com.google.android.dialer";
66
67 /**
68 * Class name of the component within in-call app which implements {@link IInCallService}.
69 */
Sailesh Nepala439e1b2014-03-11 18:19:58 -070070 private static final String IN_CALL_SERVICE_CLASS_NAME =
71 "com.android.incallui.InCallServiceImpl";
Santos Cordone3d76ab2014-01-28 17:25:20 -080072
73 /** Maintains a binding connection to the in-call app. */
74 private final InCallServiceConnection mConnection = new InCallServiceConnection();
75
Santos Cordone3d76ab2014-01-28 17:25:20 -080076 /** The in-call app implementation, see {@link IInCallService}. */
77 private IInCallService mInCallService;
78
Sailesh Nepale59bb192014-04-01 18:33:59 -070079 private final CallIdMapper mCallIdMapper = new CallIdMapper("InCall");
80
Santos Cordone3d76ab2014-01-28 17:25:20 -080081 IInCallService getService() {
82 return mInCallService;
83 }
84
Sailesh Nepal810735e2014-03-18 18:15:46 -070085 @Override
86 public void onCallAdded(Call call) {
87 if (mInCallService == null) {
88 bind();
89 } else {
90 Log.i(this, "Adding call: %s", call);
Sailesh Nepale59bb192014-04-01 18:33:59 -070091 mCallIdMapper.addCall(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -070092 try {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070093 mInCallService.addCall(toInCallCall(call));
Sailesh Nepal810735e2014-03-18 18:15:46 -070094 } catch (RemoteException e) {
Santos Cordone3d76ab2014-01-28 17:25:20 -080095 }
Santos Cordon049b7b62014-01-30 05:34:26 -080096 }
97 }
98
Sailesh Nepal810735e2014-03-18 18:15:46 -070099 @Override
100 public void onCallRemoved(Call call) {
101 if (CallsManager.getInstance().getCalls().isEmpty()) {
102 // TODO(sail): Wait for all messages to be delivered to the service before unbinding.
103 unbind();
Santos Cordon049b7b62014-01-30 05:34:26 -0800104 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700105 mCallIdMapper.removeCall(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800106 }
107
Sailesh Nepal810735e2014-03-18 18:15:46 -0700108 @Override
109 public void onCallStateChanged(Call call, CallState oldState, CallState newState) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700110 updateCall(call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700111 }
112
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700113 @Override
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700114 public void onCallHandoffHandleChanged(Call call, Uri oldHandle, Uri newHandle) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700115 updateCall(call);
116 }
117
118 @Override
119 public void onCallServiceChanged(
120 Call call,
121 CallServiceWrapper oldCallServiceWrapper,
122 CallServiceWrapper newCallService) {
123 updateCall(call);
124 }
125
126 @Override
127 public void onCallHandoffCallServiceDescriptorChanged(
128 Call call,
129 CallServiceDescriptor oldDescriptor,
130 CallServiceDescriptor newDescriptor) {
131 updateCall(call);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700132 }
133
134 @Override
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700135 public void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
136 if (mInCallService != null) {
137 Log.i(this, "Calling onAudioStateChanged, audioState: %s -> %s", oldAudioState,
138 newAudioState);
139 try {
140 mInCallService.onAudioStateChanged(newAudioState);
141 } catch (RemoteException e) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700142 }
143 }
144 }
145
Santos Cordone3d76ab2014-01-28 17:25:20 -0800146 /**
147 * Unbinds an existing bound connection to the in-call app.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800148 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700149 private void unbind() {
Santos Cordone3d76ab2014-01-28 17:25:20 -0800150 ThreadUtil.checkOnMainThread();
151 if (mInCallService != null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800152 Log.i(this, "Unbinding from InCallService");
Santos Cordon049b7b62014-01-30 05:34:26 -0800153 TelecommApp.getInstance().unbindService(mConnection);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800154 mInCallService = null;
155 }
156 }
157
158 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800159 * Binds to the in-call app if not already connected by binding directly to the saved
160 * component name of the {@link IInCallService} implementation.
161 */
162 private void bind() {
163 ThreadUtil.checkOnMainThread();
164 if (mInCallService == null) {
165 ComponentName component =
166 new ComponentName(IN_CALL_PACKAGE_NAME, IN_CALL_SERVICE_CLASS_NAME);
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800167 Log.i(this, "Attempting to bind to InCallService: %s", component);
Santos Cordon049b7b62014-01-30 05:34:26 -0800168
169 Intent serviceIntent = new Intent(IInCallService.class.getName());
170 serviceIntent.setComponent(component);
171
172 Context context = TelecommApp.getInstance();
Amith Yamasani60e75842014-05-23 10:09:14 -0700173 if (!context.bindServiceAsUser(serviceIntent, mConnection, Context.BIND_AUTO_CREATE,
174 UserHandle.CURRENT)) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800175 Log.w(this, "Could not connect to the in-call app (%s)", component);
Santos Cordon049b7b62014-01-30 05:34:26 -0800176
177 // TODO(santoscordon): Implement retry or fall-back-to-default logic.
178 }
179 }
180 }
181
182 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800183 * Persists the {@link IInCallService} instance and starts the communication between
Sailesh Nepale59bb192014-04-01 18:33:59 -0700184 * this class and in-call app by sending the first update to in-call app. This method is
Santos Cordone3d76ab2014-01-28 17:25:20 -0800185 * called after a successful binding connection is established.
186 *
187 * @param service The {@link IInCallService} implementation.
188 */
189 private void onConnected(IBinder service) {
190 ThreadUtil.checkOnMainThread();
191 mInCallService = IInCallService.Stub.asInterface(service);
192
193 try {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700194 mInCallService.setInCallAdapter(new InCallAdapter(CallsManager.getInstance(),
195 mCallIdMapper));
Santos Cordone3d76ab2014-01-28 17:25:20 -0800196 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800197 Log.e(this, e, "Failed to set the in-call adapter.");
Santos Cordone3d76ab2014-01-28 17:25:20 -0800198 mInCallService = null;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700199 return;
Santos Cordone3d76ab2014-01-28 17:25:20 -0800200 }
201
Santos Cordon049b7b62014-01-30 05:34:26 -0800202 // Upon successful connection, send the state of the world to the in-call app.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700203 ImmutableCollection<Call> calls = CallsManager.getInstance().getCalls();
204 if (!calls.isEmpty()) {
205 for (Call call : calls) {
206 onCallAdded(call);
207 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700208 onAudioStateChanged(null, CallsManager.getInstance().getAudioState());
Sailesh Nepal810735e2014-03-18 18:15:46 -0700209 } else {
210 unbind();
Santos Cordon049b7b62014-01-30 05:34:26 -0800211 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800212 }
213
214 /**
215 * Cleans up the instance of in-call app after the service has been unbound.
216 */
217 private void onDisconnected() {
218 ThreadUtil.checkOnMainThread();
219 mInCallService = null;
220 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700221
222 private void updateCall(Call call) {
223 if (mInCallService != null) {
224 try {
225 mInCallService.updateCall(toInCallCall(call));
226 } catch (RemoteException e) {
227 }
228 }
229 }
230
231 private InCallCall toInCallCall(Call call) {
232 String callId = mCallIdMapper.getCallId(call);
233 CallServiceDescriptor descriptor =
234 call.getCallService() != null ? call.getCallService().getDescriptor() : null;
235
236 boolean isHandoffCapable = call.getHandoffHandle() != null;
237 int capabilities = CallCapabilities.HOLD | CallCapabilities.MUTE;
238 if (call.getHandoffHandle() != null) {
239 capabilities |= CallCapabilities.CONNECTION_HANDOFF;
240 }
241 CallState state = call.getState();
242 if (state == CallState.ABORTED) {
243 state = CallState.DISCONNECTED;
244 }
245 // TODO(sail): Remove this and replace with final reconnecting code.
246 if (state == CallState.DISCONNECTED && call.getHandoffCallServiceDescriptor() != null) {
247 state = CallState.ACTIVE;
248 }
249 return new InCallCall(callId, state, call.getDisconnectCause(), capabilities,
250 call.getConnectTimeMillis(), call.getHandle(), call.getGatewayInfo(), descriptor,
251 call.getHandoffCallServiceDescriptor());
252 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800253}