blob: 434c709ed78a9bb26fb6d44c9e8e1083655d372d [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
Santos Cordona1610702014-06-04 20:22:56 -070037import java.util.ArrayList;
38import java.util.List;
39import java.util.Set;
40
Santos Cordone3d76ab2014-01-28 17:25:20 -080041/**
42 * Binds to {@link IInCallService} and provides the service to {@link CallsManager} through which it
43 * 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 -070044 * a binding to the {@link IInCallService} (implemented by the in-call app).
Santos Cordone3d76ab2014-01-28 17:25:20 -080045 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070046public final class InCallController extends CallsManagerListenerBase {
Santos Cordone3d76ab2014-01-28 17:25:20 -080047 /**
48 * Used to bind to the in-call app and triggers the start of communication between
Sailesh Nepale59bb192014-04-01 18:33:59 -070049 * this class and in-call app.
Santos Cordone3d76ab2014-01-28 17:25:20 -080050 */
51 private class InCallServiceConnection implements ServiceConnection {
52 /** {@inheritDoc} */
53 @Override public void onServiceConnected(ComponentName name, IBinder service) {
54 onConnected(service);
55 }
56
57 /** {@inheritDoc} */
58 @Override public void onServiceDisconnected(ComponentName name) {
59 onDisconnected();
60 }
61 }
62
Santos Cordone3d76ab2014-01-28 17:25:20 -080063 /**
64 * 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 -080065 * ultimately compiled into the dialer APK, hence the difference in namespaces between this and
66 * {@link #IN_CALL_SERVICE_CLASS_NAME}.
Santos Cordone3d76ab2014-01-28 17:25:20 -080067 * TODO(santoscordon): Change this into config.xml resource entry.
68 */
69 private static final String IN_CALL_PACKAGE_NAME = "com.google.android.dialer";
70
71 /**
72 * Class name of the component within in-call app which implements {@link IInCallService}.
73 */
Sailesh Nepala439e1b2014-03-11 18:19:58 -070074 private static final String IN_CALL_SERVICE_CLASS_NAME =
75 "com.android.incallui.InCallServiceImpl";
Santos Cordone3d76ab2014-01-28 17:25:20 -080076
77 /** Maintains a binding connection to the in-call app. */
78 private final InCallServiceConnection mConnection = new InCallServiceConnection();
79
Santos Cordone3d76ab2014-01-28 17:25:20 -080080 /** The in-call app implementation, see {@link IInCallService}. */
81 private IInCallService mInCallService;
82
Sailesh Nepale59bb192014-04-01 18:33:59 -070083 private final CallIdMapper mCallIdMapper = new CallIdMapper("InCall");
84
Santos Cordone3d76ab2014-01-28 17:25:20 -080085 IInCallService getService() {
86 return mInCallService;
87 }
88
Sailesh Nepal810735e2014-03-18 18:15:46 -070089 @Override
90 public void onCallAdded(Call call) {
91 if (mInCallService == null) {
92 bind();
93 } else {
94 Log.i(this, "Adding call: %s", call);
Sailesh Nepale59bb192014-04-01 18:33:59 -070095 mCallIdMapper.addCall(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -070096 try {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070097 mInCallService.addCall(toInCallCall(call));
Santos Cordonf3671a62014-05-29 21:51:53 -070098 } catch (RemoteException ignored) {
Santos Cordone3d76ab2014-01-28 17:25:20 -080099 }
Santos Cordon049b7b62014-01-30 05:34:26 -0800100 }
101 }
102
Sailesh Nepal810735e2014-03-18 18:15:46 -0700103 @Override
104 public void onCallRemoved(Call call) {
105 if (CallsManager.getInstance().getCalls().isEmpty()) {
106 // TODO(sail): Wait for all messages to be delivered to the service before unbinding.
107 unbind();
Santos Cordon049b7b62014-01-30 05:34:26 -0800108 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700109 mCallIdMapper.removeCall(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800110 }
111
Sailesh Nepal810735e2014-03-18 18:15:46 -0700112 @Override
113 public void onCallStateChanged(Call call, CallState oldState, CallState newState) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700114 updateCall(call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700115 }
116
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700117 @Override
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700118 public void onCallHandoffHandleChanged(Call call, Uri oldHandle, Uri newHandle) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700119 updateCall(call);
120 }
121
122 @Override
123 public void onCallServiceChanged(
124 Call call,
125 CallServiceWrapper oldCallServiceWrapper,
126 CallServiceWrapper newCallService) {
127 updateCall(call);
128 }
129
130 @Override
131 public void onCallHandoffCallServiceDescriptorChanged(
132 Call call,
133 CallServiceDescriptor oldDescriptor,
134 CallServiceDescriptor newDescriptor) {
135 updateCall(call);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700136 }
137
138 @Override
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700139 public void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
140 if (mInCallService != null) {
141 Log.i(this, "Calling onAudioStateChanged, audioState: %s -> %s", oldAudioState,
142 newAudioState);
143 try {
144 mInCallService.onAudioStateChanged(newAudioState);
Santos Cordonf3671a62014-05-29 21:51:53 -0700145 } catch (RemoteException ignored) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700146 }
147 }
148 }
149
Evan Charlton352105c2014-06-03 14:10:54 -0700150 void onPostDialWait(Call call, String remaining) {
151 if (mInCallService != null) {
152 Log.i(this, "Calling onPostDialWait, remaining = %s", remaining);
153 try {
154 mInCallService.setPostDialWait(mCallIdMapper.getCallId(call), remaining);
155 } catch (RemoteException ignored) {
156 }
157 }
158 }
159
Santos Cordona1610702014-06-04 20:22:56 -0700160 @Override
161 public void onIsConferenceCapableChanged(Call call, boolean isConferenceCapable) {
162 updateCall(call);
163 }
164
165 @Override
166 public void onIsConferencedChanged(Call call) {
167 Log.v(this, "onIsConferencedChanged %s", call);
168 updateCall(call);
169 }
170
Santos Cordonf3671a62014-05-29 21:51:53 -0700171 void bringToForeground(boolean showDialpad) {
172 if (mInCallService != null) {
173 try {
174 mInCallService.bringToForeground(showDialpad);
175 } catch (RemoteException ignored) {
176 }
177 } else {
178 Log.w(this, "Asking to bring unbound in-call UI to foreground.");
179 }
180 }
181
Santos Cordone3d76ab2014-01-28 17:25:20 -0800182 /**
183 * Unbinds an existing bound connection to the in-call app.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800184 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700185 private void unbind() {
Santos Cordone3d76ab2014-01-28 17:25:20 -0800186 ThreadUtil.checkOnMainThread();
187 if (mInCallService != null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800188 Log.i(this, "Unbinding from InCallService");
Santos Cordon049b7b62014-01-30 05:34:26 -0800189 TelecommApp.getInstance().unbindService(mConnection);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800190 mInCallService = null;
191 }
192 }
193
194 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800195 * Binds to the in-call app if not already connected by binding directly to the saved
196 * component name of the {@link IInCallService} implementation.
197 */
198 private void bind() {
199 ThreadUtil.checkOnMainThread();
200 if (mInCallService == null) {
201 ComponentName component =
202 new ComponentName(IN_CALL_PACKAGE_NAME, IN_CALL_SERVICE_CLASS_NAME);
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800203 Log.i(this, "Attempting to bind to InCallService: %s", component);
Santos Cordon049b7b62014-01-30 05:34:26 -0800204
205 Intent serviceIntent = new Intent(IInCallService.class.getName());
206 serviceIntent.setComponent(component);
207
208 Context context = TelecommApp.getInstance();
Amith Yamasani60e75842014-05-23 10:09:14 -0700209 if (!context.bindServiceAsUser(serviceIntent, mConnection, Context.BIND_AUTO_CREATE,
210 UserHandle.CURRENT)) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800211 Log.w(this, "Could not connect to the in-call app (%s)", component);
Santos Cordon049b7b62014-01-30 05:34:26 -0800212
213 // TODO(santoscordon): Implement retry or fall-back-to-default logic.
214 }
215 }
216 }
217
218 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800219 * Persists the {@link IInCallService} instance and starts the communication between
Sailesh Nepale59bb192014-04-01 18:33:59 -0700220 * 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 -0800221 * called after a successful binding connection is established.
222 *
223 * @param service The {@link IInCallService} implementation.
224 */
225 private void onConnected(IBinder service) {
226 ThreadUtil.checkOnMainThread();
227 mInCallService = IInCallService.Stub.asInterface(service);
228
229 try {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700230 mInCallService.setInCallAdapter(new InCallAdapter(CallsManager.getInstance(),
231 mCallIdMapper));
Santos Cordone3d76ab2014-01-28 17:25:20 -0800232 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800233 Log.e(this, e, "Failed to set the in-call adapter.");
Santos Cordone3d76ab2014-01-28 17:25:20 -0800234 mInCallService = null;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700235 return;
Santos Cordone3d76ab2014-01-28 17:25:20 -0800236 }
237
Santos Cordon049b7b62014-01-30 05:34:26 -0800238 // Upon successful connection, send the state of the world to the in-call app.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700239 ImmutableCollection<Call> calls = CallsManager.getInstance().getCalls();
240 if (!calls.isEmpty()) {
241 for (Call call : calls) {
242 onCallAdded(call);
243 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700244 onAudioStateChanged(null, CallsManager.getInstance().getAudioState());
Sailesh Nepal810735e2014-03-18 18:15:46 -0700245 } else {
246 unbind();
Santos Cordon049b7b62014-01-30 05:34:26 -0800247 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800248 }
249
250 /**
251 * Cleans up the instance of in-call app after the service has been unbound.
252 */
253 private void onDisconnected() {
254 ThreadUtil.checkOnMainThread();
255 mInCallService = null;
256 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700257
258 private void updateCall(Call call) {
259 if (mInCallService != null) {
260 try {
Santos Cordona1610702014-06-04 20:22:56 -0700261 InCallCall inCallCall = toInCallCall(call);
262 Log.v(this, "updateCall %s ==> %s", call, inCallCall);
263 mInCallService.updateCall(inCallCall);
Santos Cordonf3671a62014-05-29 21:51:53 -0700264 } catch (RemoteException ignored) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700265 }
266 }
267 }
268
269 private InCallCall toInCallCall(Call call) {
270 String callId = mCallIdMapper.getCallId(call);
271 CallServiceDescriptor descriptor =
272 call.getCallService() != null ? call.getCallService().getDescriptor() : null;
273
274 boolean isHandoffCapable = call.getHandoffHandle() != null;
275 int capabilities = CallCapabilities.HOLD | CallCapabilities.MUTE;
276 if (call.getHandoffHandle() != null) {
277 capabilities |= CallCapabilities.CONNECTION_HANDOFF;
278 }
Santos Cordon10838c22014-06-11 17:36:04 -0700279 if (CallsManager.getInstance().isAddCallCapable(call)) {
280 capabilities |= CallCapabilities.ADD_CALL;
281 }
Santos Cordona1610702014-06-04 20:22:56 -0700282 if (call.isConferenceCapable()) {
283 capabilities |= CallCapabilities.MERGE_CALLS;
284 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700285 CallState state = call.getState();
286 if (state == CallState.ABORTED) {
287 state = CallState.DISCONNECTED;
288 }
289 // TODO(sail): Remove this and replace with final reconnecting code.
290 if (state == CallState.DISCONNECTED && call.getHandoffCallServiceDescriptor() != null) {
291 state = CallState.ACTIVE;
292 }
Santos Cordona1610702014-06-04 20:22:56 -0700293
294 String parentCallId = null;
295 Call parentCall = call.getParentCall();
296 if (parentCall != null) {
297 parentCallId = mCallIdMapper.getCallId(parentCall);
298 }
299
300 long connectTimeMillis = call.getConnectTimeMillis();
301 List<Call> childCalls = call.getChildCalls();
302 List<String> childCallIds = new ArrayList<>();
303 if (!childCalls.isEmpty()) {
304 connectTimeMillis = Long.MAX_VALUE;
305 for (Call child : childCalls) {
306 connectTimeMillis = Math.min(child.getConnectTimeMillis(), connectTimeMillis);
307 childCallIds.add(mCallIdMapper.getCallId(child));
308 }
309 }
310
Ihab Awad0fea3f22014-06-03 18:45:05 -0700311 return new InCallCall(callId, state, call.getDisconnectCause(), call.getDisconnectMessage(),
Santos Cordona1610702014-06-04 20:22:56 -0700312 capabilities, connectTimeMillis, call.getHandle(), call.getGatewayInfo(),
313 descriptor, call.getHandoffCallServiceDescriptor(), parentCallId, childCallIds);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700314 }
Santos Cordon10838c22014-06-11 17:36:04 -0700315
Santos Cordone3d76ab2014-01-28 17:25:20 -0800316}