blob: 871bf756a08f8d7d0af8ead01d2d01a6b72cf3e9 [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
Nancy Chena65d41f2014-06-24 12:06:03 -0700165 @Override
166 public void onCallVideoProviderChanged(Call call) {
167 updateCall(call);
168 }
169
Santos Cordonf3671a62014-05-29 21:51:53 -0700170 void bringToForeground(boolean showDialpad) {
171 if (mInCallService != null) {
172 try {
173 mInCallService.bringToForeground(showDialpad);
174 } catch (RemoteException ignored) {
175 }
176 } else {
177 Log.w(this, "Asking to bring unbound in-call UI to foreground.");
178 }
179 }
180
Santos Cordone3d76ab2014-01-28 17:25:20 -0800181 /**
182 * Unbinds an existing bound connection to the in-call app.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800183 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700184 private void unbind() {
Santos Cordone3d76ab2014-01-28 17:25:20 -0800185 ThreadUtil.checkOnMainThread();
186 if (mInCallService != null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800187 Log.i(this, "Unbinding from InCallService");
Santos Cordon049b7b62014-01-30 05:34:26 -0800188 TelecommApp.getInstance().unbindService(mConnection);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800189 mInCallService = null;
190 }
191 }
192
193 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800194 * Binds to the in-call app if not already connected by binding directly to the saved
195 * component name of the {@link IInCallService} implementation.
196 */
197 private void bind() {
198 ThreadUtil.checkOnMainThread();
199 if (mInCallService == null) {
Santos Cordonfdfcafa2014-06-26 14:49:05 -0700200 Context context = TelecommApp.getInstance();
201 Resources resources = context.getResources();
202 ComponentName component = new ComponentName(
203 resources.getString(R.string.ui_default_package),
204 resources.getString(R.string.incall_default_class));
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800205 Log.i(this, "Attempting to bind to InCallService: %s", component);
Santos Cordon049b7b62014-01-30 05:34:26 -0800206
207 Intent serviceIntent = new Intent(IInCallService.class.getName());
208 serviceIntent.setComponent(component);
209
Amith Yamasani60e75842014-05-23 10:09:14 -0700210 if (!context.bindServiceAsUser(serviceIntent, mConnection, Context.BIND_AUTO_CREATE,
211 UserHandle.CURRENT)) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800212 Log.w(this, "Could not connect to the in-call app (%s)", component);
Santos Cordon049b7b62014-01-30 05:34:26 -0800213
214 // TODO(santoscordon): Implement retry or fall-back-to-default logic.
215 }
216 }
217 }
218
219 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800220 * Persists the {@link IInCallService} instance and starts the communication between
Sailesh Nepale59bb192014-04-01 18:33:59 -0700221 * 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 -0800222 * called after a successful binding connection is established.
223 *
224 * @param service The {@link IInCallService} implementation.
225 */
226 private void onConnected(IBinder service) {
227 ThreadUtil.checkOnMainThread();
228 mInCallService = IInCallService.Stub.asInterface(service);
229
230 try {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700231 mInCallService.setInCallAdapter(new InCallAdapter(CallsManager.getInstance(),
232 mCallIdMapper));
Santos Cordone3d76ab2014-01-28 17:25:20 -0800233 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800234 Log.e(this, e, "Failed to set the in-call adapter.");
Santos Cordone3d76ab2014-01-28 17:25:20 -0800235 mInCallService = null;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700236 return;
Santos Cordone3d76ab2014-01-28 17:25:20 -0800237 }
238
Santos Cordon049b7b62014-01-30 05:34:26 -0800239 // Upon successful connection, send the state of the world to the in-call app.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700240 ImmutableCollection<Call> calls = CallsManager.getInstance().getCalls();
241 if (!calls.isEmpty()) {
242 for (Call call : calls) {
243 onCallAdded(call);
244 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700245 onAudioStateChanged(null, CallsManager.getInstance().getAudioState());
Sailesh Nepal810735e2014-03-18 18:15:46 -0700246 } else {
247 unbind();
Santos Cordon049b7b62014-01-30 05:34:26 -0800248 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800249 }
250
251 /**
252 * Cleans up the instance of in-call app after the service has been unbound.
253 */
254 private void onDisconnected() {
255 ThreadUtil.checkOnMainThread();
256 mInCallService = null;
257 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700258
259 private void updateCall(Call call) {
260 if (mInCallService != null) {
261 try {
Santos Cordona1610702014-06-04 20:22:56 -0700262 InCallCall inCallCall = toInCallCall(call);
263 Log.v(this, "updateCall %s ==> %s", call, inCallCall);
264 mInCallService.updateCall(inCallCall);
Santos Cordonf3671a62014-05-29 21:51:53 -0700265 } catch (RemoteException ignored) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700266 }
267 }
268 }
269
270 private InCallCall toInCallCall(Call call) {
271 String callId = mCallIdMapper.getCallId(call);
272 CallServiceDescriptor descriptor =
273 call.getCallService() != null ? call.getCallService().getDescriptor() : null;
274
275 boolean isHandoffCapable = call.getHandoffHandle() != null;
276 int capabilities = CallCapabilities.HOLD | CallCapabilities.MUTE;
277 if (call.getHandoffHandle() != null) {
278 capabilities |= CallCapabilities.CONNECTION_HANDOFF;
279 }
Santos Cordon10838c22014-06-11 17:36:04 -0700280 if (CallsManager.getInstance().isAddCallCapable(call)) {
281 capabilities |= CallCapabilities.ADD_CALL;
282 }
Santos Cordona1610702014-06-04 20:22:56 -0700283 if (call.isConferenceCapable()) {
284 capabilities |= CallCapabilities.MERGE_CALLS;
285 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700286 CallState state = call.getState();
287 if (state == CallState.ABORTED) {
288 state = CallState.DISCONNECTED;
289 }
290 // TODO(sail): Remove this and replace with final reconnecting code.
291 if (state == CallState.DISCONNECTED && call.getHandoffCallServiceDescriptor() != null) {
292 state = CallState.ACTIVE;
293 }
Santos Cordona1610702014-06-04 20:22:56 -0700294
295 String parentCallId = null;
296 Call parentCall = call.getParentCall();
297 if (parentCall != null) {
298 parentCallId = mCallIdMapper.getCallId(parentCall);
299 }
300
301 long connectTimeMillis = call.getConnectTimeMillis();
302 List<Call> childCalls = call.getChildCalls();
303 List<String> childCallIds = new ArrayList<>();
304 if (!childCalls.isEmpty()) {
305 connectTimeMillis = Long.MAX_VALUE;
306 for (Call child : childCalls) {
307 connectTimeMillis = Math.min(child.getConnectTimeMillis(), connectTimeMillis);
308 childCallIds.add(mCallIdMapper.getCallId(child));
309 }
310 }
311
Ihab Awadff7493a2014-06-10 13:47:44 -0700312 if (call.isRespondViaSmsCapable()) {
313 capabilities |= CallCapabilities.RESPOND_VIA_TEXT;
314 }
315
Ihab Awad0fea3f22014-06-03 18:45:05 -0700316 return new InCallCall(callId, state, call.getDisconnectCause(), call.getDisconnectMessage(),
Ihab Awadff7493a2014-06-10 13:47:44 -0700317 call.getCannedSmsResponses(), capabilities, connectTimeMillis, call.getHandle(),
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700318 call.getGatewayInfo(), call.getSubscription(), descriptor,
Nancy Chena65d41f2014-06-24 12:06:03 -0700319 call.getHandoffCallServiceDescriptor(), call.getCallVideoProvider(),
320 parentCallId, childCallIds);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700321 }
Santos Cordon10838c22014-06-11 17:36:04 -0700322
Santos Cordone3d76ab2014-01-28 17:25:20 -0800323}