blob: e971c81a7d554c85057cad0512ea1f76342b0f1b [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;
Santos Cordonfdfcafa2014-06-26 14:49:05 -070023
24import android.content.res.Resources;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070025import android.net.Uri;
Santos Cordone3d76ab2014-01-28 17:25:20 -080026import android.os.IBinder;
27import android.os.RemoteException;
Amith Yamasani60e75842014-05-23 10:09:14 -070028import android.os.UserHandle;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070029import android.telecomm.CallAudioState;
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070030import android.telecomm.CallCapabilities;
31import android.telecomm.CallServiceDescriptor;
32import android.telecomm.CallState;
33import android.telecomm.InCallCall;
Sailesh Nepala439e1b2014-03-11 18:19:58 -070034
35import com.android.internal.telecomm.IInCallService;
Sailesh Nepal810735e2014-03-18 18:15:46 -070036import com.google.common.collect.ImmutableCollection;
Santos Cordone3d76ab2014-01-28 17:25:20 -080037
Santos Cordona1610702014-06-04 20:22:56 -070038import java.util.ArrayList;
Ihab Awadff7493a2014-06-10 13:47:44 -070039import java.util.Collections;
Santos Cordona1610702014-06-04 20:22:56 -070040import java.util.List;
Santos Cordona1610702014-06-04 20:22:56 -070041
Santos Cordone3d76ab2014-01-28 17:25:20 -080042/**
43 * Binds to {@link IInCallService} and provides the service to {@link CallsManager} through which it
44 * 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 -070045 * a binding to the {@link IInCallService} (implemented by the in-call app).
Santos Cordone3d76ab2014-01-28 17:25:20 -080046 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070047public final class InCallController extends CallsManagerListenerBase {
Santos Cordone3d76ab2014-01-28 17:25:20 -080048 /**
49 * Used to bind to the in-call app and triggers the start of communication between
Sailesh Nepale59bb192014-04-01 18:33:59 -070050 * this class and in-call app.
Santos Cordone3d76ab2014-01-28 17:25:20 -080051 */
52 private class InCallServiceConnection implements ServiceConnection {
53 /** {@inheritDoc} */
54 @Override public void onServiceConnected(ComponentName name, IBinder service) {
55 onConnected(service);
56 }
57
58 /** {@inheritDoc} */
59 @Override public void onServiceDisconnected(ComponentName name) {
60 onDisconnected();
61 }
62 }
63
Santos Cordone3d76ab2014-01-28 17:25:20 -080064 /** Maintains a binding connection to the in-call app. */
65 private final InCallServiceConnection mConnection = new InCallServiceConnection();
66
Santos Cordone3d76ab2014-01-28 17:25:20 -080067 /** The in-call app implementation, see {@link IInCallService}. */
68 private IInCallService mInCallService;
69
Sailesh Nepale59bb192014-04-01 18:33:59 -070070 private final CallIdMapper mCallIdMapper = new CallIdMapper("InCall");
71
Santos Cordone3d76ab2014-01-28 17:25:20 -080072 IInCallService getService() {
73 return mInCallService;
74 }
75
Sailesh Nepal810735e2014-03-18 18:15:46 -070076 @Override
77 public void onCallAdded(Call call) {
78 if (mInCallService == null) {
79 bind();
80 } else {
81 Log.i(this, "Adding call: %s", call);
Ihab Awadff7493a2014-06-10 13:47:44 -070082 if (mCallIdMapper.getCallId(call) == null) {
83 mCallIdMapper.addCall(call);
84 try {
85 mInCallService.addCall(toInCallCall(call));
86 } catch (RemoteException ignored) {
87 }
Santos Cordone3d76ab2014-01-28 17:25:20 -080088 }
Santos Cordon049b7b62014-01-30 05:34:26 -080089 }
90 }
91
Sailesh Nepal810735e2014-03-18 18:15:46 -070092 @Override
93 public void onCallRemoved(Call call) {
94 if (CallsManager.getInstance().getCalls().isEmpty()) {
95 // TODO(sail): Wait for all messages to be delivered to the service before unbinding.
96 unbind();
Santos Cordon049b7b62014-01-30 05:34:26 -080097 }
Sailesh Nepale59bb192014-04-01 18:33:59 -070098 mCallIdMapper.removeCall(call);
Santos Cordon049b7b62014-01-30 05:34:26 -080099 }
100
Sailesh Nepal810735e2014-03-18 18:15:46 -0700101 @Override
102 public void onCallStateChanged(Call call, CallState oldState, CallState newState) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700103 updateCall(call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700104 }
105
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700106 @Override
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700107 public void onCallHandoffHandleChanged(Call call, Uri oldHandle, Uri newHandle) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700108 updateCall(call);
109 }
110
111 @Override
112 public void onCallServiceChanged(
113 Call call,
114 CallServiceWrapper oldCallServiceWrapper,
115 CallServiceWrapper newCallService) {
116 updateCall(call);
117 }
118
119 @Override
120 public void onCallHandoffCallServiceDescriptorChanged(
121 Call call,
122 CallServiceDescriptor oldDescriptor,
123 CallServiceDescriptor newDescriptor) {
124 updateCall(call);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700125 }
126
127 @Override
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700128 public void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
129 if (mInCallService != null) {
130 Log.i(this, "Calling onAudioStateChanged, audioState: %s -> %s", oldAudioState,
131 newAudioState);
132 try {
133 mInCallService.onAudioStateChanged(newAudioState);
Santos Cordonf3671a62014-05-29 21:51:53 -0700134 } catch (RemoteException ignored) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700135 }
136 }
137 }
138
Evan Charlton352105c2014-06-03 14:10:54 -0700139 void onPostDialWait(Call call, String remaining) {
140 if (mInCallService != null) {
141 Log.i(this, "Calling onPostDialWait, remaining = %s", remaining);
142 try {
143 mInCallService.setPostDialWait(mCallIdMapper.getCallId(call), remaining);
144 } catch (RemoteException ignored) {
145 }
146 }
147 }
148
Santos Cordona1610702014-06-04 20:22:56 -0700149 @Override
150 public void onIsConferenceCapableChanged(Call call, boolean isConferenceCapable) {
151 updateCall(call);
152 }
153
154 @Override
155 public void onIsConferencedChanged(Call call) {
156 Log.v(this, "onIsConferencedChanged %s", call);
157 updateCall(call);
158 }
159
Ihab Awadff7493a2014-06-10 13:47:44 -0700160 @Override
161 public void onCannedSmsResponsesLoaded(Call call) {
162 updateCall(call);
163 }
164
Santos Cordonf3671a62014-05-29 21:51:53 -0700165 void bringToForeground(boolean showDialpad) {
166 if (mInCallService != null) {
167 try {
168 mInCallService.bringToForeground(showDialpad);
169 } catch (RemoteException ignored) {
170 }
171 } else {
172 Log.w(this, "Asking to bring unbound in-call UI to foreground.");
173 }
174 }
175
Santos Cordone3d76ab2014-01-28 17:25:20 -0800176 /**
177 * Unbinds an existing bound connection to the in-call app.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800178 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700179 private void unbind() {
Santos Cordone3d76ab2014-01-28 17:25:20 -0800180 ThreadUtil.checkOnMainThread();
181 if (mInCallService != null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800182 Log.i(this, "Unbinding from InCallService");
Santos Cordon049b7b62014-01-30 05:34:26 -0800183 TelecommApp.getInstance().unbindService(mConnection);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800184 mInCallService = null;
185 }
186 }
187
188 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800189 * Binds to the in-call app if not already connected by binding directly to the saved
190 * component name of the {@link IInCallService} implementation.
191 */
192 private void bind() {
193 ThreadUtil.checkOnMainThread();
194 if (mInCallService == null) {
Santos Cordonfdfcafa2014-06-26 14:49:05 -0700195 Context context = TelecommApp.getInstance();
196 Resources resources = context.getResources();
197 ComponentName component = new ComponentName(
198 resources.getString(R.string.ui_default_package),
199 resources.getString(R.string.incall_default_class));
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800200 Log.i(this, "Attempting to bind to InCallService: %s", component);
Santos Cordon049b7b62014-01-30 05:34:26 -0800201
202 Intent serviceIntent = new Intent(IInCallService.class.getName());
203 serviceIntent.setComponent(component);
204
Amith Yamasani60e75842014-05-23 10:09:14 -0700205 if (!context.bindServiceAsUser(serviceIntent, mConnection, Context.BIND_AUTO_CREATE,
206 UserHandle.CURRENT)) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800207 Log.w(this, "Could not connect to the in-call app (%s)", component);
Santos Cordon049b7b62014-01-30 05:34:26 -0800208
209 // TODO(santoscordon): Implement retry or fall-back-to-default logic.
210 }
211 }
212 }
213
214 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800215 * Persists the {@link IInCallService} instance and starts the communication between
Sailesh Nepale59bb192014-04-01 18:33:59 -0700216 * 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 -0800217 * called after a successful binding connection is established.
218 *
219 * @param service The {@link IInCallService} implementation.
220 */
221 private void onConnected(IBinder service) {
222 ThreadUtil.checkOnMainThread();
223 mInCallService = IInCallService.Stub.asInterface(service);
224
225 try {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700226 mInCallService.setInCallAdapter(new InCallAdapter(CallsManager.getInstance(),
227 mCallIdMapper));
Santos Cordone3d76ab2014-01-28 17:25:20 -0800228 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800229 Log.e(this, e, "Failed to set the in-call adapter.");
Santos Cordone3d76ab2014-01-28 17:25:20 -0800230 mInCallService = null;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700231 return;
Santos Cordone3d76ab2014-01-28 17:25:20 -0800232 }
233
Santos Cordon049b7b62014-01-30 05:34:26 -0800234 // Upon successful connection, send the state of the world to the in-call app.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700235 ImmutableCollection<Call> calls = CallsManager.getInstance().getCalls();
236 if (!calls.isEmpty()) {
237 for (Call call : calls) {
238 onCallAdded(call);
239 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700240 onAudioStateChanged(null, CallsManager.getInstance().getAudioState());
Sailesh Nepal810735e2014-03-18 18:15:46 -0700241 } else {
242 unbind();
Santos Cordon049b7b62014-01-30 05:34:26 -0800243 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800244 }
245
246 /**
247 * Cleans up the instance of in-call app after the service has been unbound.
248 */
249 private void onDisconnected() {
250 ThreadUtil.checkOnMainThread();
251 mInCallService = null;
252 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700253
254 private void updateCall(Call call) {
255 if (mInCallService != null) {
256 try {
Santos Cordona1610702014-06-04 20:22:56 -0700257 InCallCall inCallCall = toInCallCall(call);
258 Log.v(this, "updateCall %s ==> %s", call, inCallCall);
259 mInCallService.updateCall(inCallCall);
Santos Cordonf3671a62014-05-29 21:51:53 -0700260 } catch (RemoteException ignored) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700261 }
262 }
263 }
264
265 private InCallCall toInCallCall(Call call) {
266 String callId = mCallIdMapper.getCallId(call);
267 CallServiceDescriptor descriptor =
268 call.getCallService() != null ? call.getCallService().getDescriptor() : null;
269
270 boolean isHandoffCapable = call.getHandoffHandle() != null;
271 int capabilities = CallCapabilities.HOLD | CallCapabilities.MUTE;
272 if (call.getHandoffHandle() != null) {
273 capabilities |= CallCapabilities.CONNECTION_HANDOFF;
274 }
Santos Cordon10838c22014-06-11 17:36:04 -0700275 if (CallsManager.getInstance().isAddCallCapable(call)) {
276 capabilities |= CallCapabilities.ADD_CALL;
277 }
Santos Cordona1610702014-06-04 20:22:56 -0700278 if (call.isConferenceCapable()) {
279 capabilities |= CallCapabilities.MERGE_CALLS;
280 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700281 CallState state = call.getState();
282 if (state == CallState.ABORTED) {
283 state = CallState.DISCONNECTED;
284 }
285 // TODO(sail): Remove this and replace with final reconnecting code.
286 if (state == CallState.DISCONNECTED && call.getHandoffCallServiceDescriptor() != null) {
287 state = CallState.ACTIVE;
288 }
Santos Cordona1610702014-06-04 20:22:56 -0700289
290 String parentCallId = null;
291 Call parentCall = call.getParentCall();
292 if (parentCall != null) {
293 parentCallId = mCallIdMapper.getCallId(parentCall);
294 }
295
296 long connectTimeMillis = call.getConnectTimeMillis();
297 List<Call> childCalls = call.getChildCalls();
298 List<String> childCallIds = new ArrayList<>();
299 if (!childCalls.isEmpty()) {
300 connectTimeMillis = Long.MAX_VALUE;
301 for (Call child : childCalls) {
302 connectTimeMillis = Math.min(child.getConnectTimeMillis(), connectTimeMillis);
303 childCallIds.add(mCallIdMapper.getCallId(child));
304 }
305 }
306
Ihab Awadff7493a2014-06-10 13:47:44 -0700307 if (call.isRespondViaSmsCapable()) {
308 capabilities |= CallCapabilities.RESPOND_VIA_TEXT;
309 }
310
Ihab Awad0fea3f22014-06-03 18:45:05 -0700311 return new InCallCall(callId, state, call.getDisconnectCause(), call.getDisconnectMessage(),
Ihab Awadff7493a2014-06-10 13:47:44 -0700312 call.getCannedSmsResponses(), capabilities, connectTimeMillis, call.getHandle(),
313 call.getGatewayInfo(), descriptor, call.getHandoffCallServiceDescriptor(),
314 parentCallId, childCallIds);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700315 }
Santos Cordon10838c22014-06-11 17:36:04 -0700316
Santos Cordone3d76ab2014-01-28 17:25:20 -0800317}