blob: 7081dc9c2753f180ed81deeef82d56cd99e97c65 [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;
Sailesh Nepale8ecb982014-07-11 17:19:42 -070031import android.telecomm.CallPropertyPresentation;
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070032import 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;
39import java.util.List;
Santos Cordona1610702014-06-04 20:22:56 -070040
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
Sailesh Nepale8ecb982014-07-11 17:19:42 -070063 private final Call.Listener mCallListener = new Call.ListenerBase() {
64 @Override
65 public void onCallCapabilitiesChanged(Call call) {
66 updateCall(call);
67 }
68
69 @Override
70 public void onCannedSmsResponsesLoaded(Call call) {
71 updateCall(call);
72 }
73
74 @Override
75 public void onCallVideoProviderChanged(Call call) {
76 updateCall(call);
77 }
78
79 @Override
80 public void onStatusHintsChanged(Call call) {
81 updateCall(call);
82 }
83
84 @Override
85 public void onHandleChanged(Call call) {
86 updateCall(call);
87 }
88
89 @Override
90 public void onCallerDisplayNameChanged(Call call) {
91 updateCall(call);
92 }
Andrew Lee4a796602014-07-11 17:23:03 -070093
94 @Override
95 public void onVideoStateChanged(Call call) {
96 updateCall(call);
97 }
Sailesh Nepale8ecb982014-07-11 17:19:42 -070098 };
99
Santos Cordone3d76ab2014-01-28 17:25:20 -0800100 /** Maintains a binding connection to the in-call app. */
101 private final InCallServiceConnection mConnection = new InCallServiceConnection();
102
Santos Cordone3d76ab2014-01-28 17:25:20 -0800103 /** The in-call app implementation, see {@link IInCallService}. */
104 private IInCallService mInCallService;
105
Sailesh Nepale59bb192014-04-01 18:33:59 -0700106 private final CallIdMapper mCallIdMapper = new CallIdMapper("InCall");
107
Santos Cordone3d76ab2014-01-28 17:25:20 -0800108 IInCallService getService() {
109 return mInCallService;
110 }
111
Sailesh Nepal810735e2014-03-18 18:15:46 -0700112 @Override
113 public void onCallAdded(Call call) {
114 if (mInCallService == null) {
115 bind();
116 } else {
117 Log.i(this, "Adding call: %s", call);
Ihab Awadff7493a2014-06-10 13:47:44 -0700118 if (mCallIdMapper.getCallId(call) == null) {
119 mCallIdMapper.addCall(call);
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700120 call.addListener(mCallListener);
Ihab Awadff7493a2014-06-10 13:47:44 -0700121 try {
122 mInCallService.addCall(toInCallCall(call));
123 } catch (RemoteException ignored) {
124 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800125 }
Santos Cordon049b7b62014-01-30 05:34:26 -0800126 }
127 }
128
Sailesh Nepal810735e2014-03-18 18:15:46 -0700129 @Override
130 public void onCallRemoved(Call call) {
131 if (CallsManager.getInstance().getCalls().isEmpty()) {
132 // TODO(sail): Wait for all messages to be delivered to the service before unbinding.
133 unbind();
Santos Cordon049b7b62014-01-30 05:34:26 -0800134 }
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700135 call.removeListener(mCallListener);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700136 mCallIdMapper.removeCall(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800137 }
138
Sailesh Nepal810735e2014-03-18 18:15:46 -0700139 @Override
140 public void onCallStateChanged(Call call, CallState oldState, CallState newState) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700141 updateCall(call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700142 }
143
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700144 @Override
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700145 public void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700146 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700147 ConnectionServiceWrapper oldService,
148 ConnectionServiceWrapper newService) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700149 updateCall(call);
150 }
151
152 @Override
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700153 public void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
154 if (mInCallService != null) {
155 Log.i(this, "Calling onAudioStateChanged, audioState: %s -> %s", oldAudioState,
156 newAudioState);
157 try {
158 mInCallService.onAudioStateChanged(newAudioState);
Santos Cordonf3671a62014-05-29 21:51:53 -0700159 } catch (RemoteException ignored) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700160 }
161 }
162 }
163
Evan Charlton352105c2014-06-03 14:10:54 -0700164 void onPostDialWait(Call call, String remaining) {
165 if (mInCallService != null) {
166 Log.i(this, "Calling onPostDialWait, remaining = %s", remaining);
167 try {
168 mInCallService.setPostDialWait(mCallIdMapper.getCallId(call), remaining);
169 } catch (RemoteException ignored) {
170 }
171 }
172 }
173
Santos Cordona1610702014-06-04 20:22:56 -0700174 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700175 public void onIsConferencedChanged(Call call) {
176 Log.v(this, "onIsConferencedChanged %s", call);
177 updateCall(call);
178 }
179
Santos Cordonf3671a62014-05-29 21:51:53 -0700180 void bringToForeground(boolean showDialpad) {
181 if (mInCallService != null) {
182 try {
183 mInCallService.bringToForeground(showDialpad);
184 } catch (RemoteException ignored) {
185 }
186 } else {
187 Log.w(this, "Asking to bring unbound in-call UI to foreground.");
188 }
189 }
190
Santos Cordone3d76ab2014-01-28 17:25:20 -0800191 /**
192 * Unbinds an existing bound connection to the in-call app.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800193 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700194 private void unbind() {
Santos Cordone3d76ab2014-01-28 17:25:20 -0800195 ThreadUtil.checkOnMainThread();
196 if (mInCallService != null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800197 Log.i(this, "Unbinding from InCallService");
Santos Cordon049b7b62014-01-30 05:34:26 -0800198 TelecommApp.getInstance().unbindService(mConnection);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800199 mInCallService = null;
200 }
201 }
202
203 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800204 * Binds to the in-call app if not already connected by binding directly to the saved
205 * component name of the {@link IInCallService} implementation.
206 */
207 private void bind() {
208 ThreadUtil.checkOnMainThread();
209 if (mInCallService == null) {
Santos Cordonfdfcafa2014-06-26 14:49:05 -0700210 Context context = TelecommApp.getInstance();
211 Resources resources = context.getResources();
212 ComponentName component = new ComponentName(
213 resources.getString(R.string.ui_default_package),
214 resources.getString(R.string.incall_default_class));
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800215 Log.i(this, "Attempting to bind to InCallService: %s", component);
Santos Cordon049b7b62014-01-30 05:34:26 -0800216
217 Intent serviceIntent = new Intent(IInCallService.class.getName());
218 serviceIntent.setComponent(component);
219
Amith Yamasani60e75842014-05-23 10:09:14 -0700220 if (!context.bindServiceAsUser(serviceIntent, mConnection, Context.BIND_AUTO_CREATE,
221 UserHandle.CURRENT)) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800222 Log.w(this, "Could not connect to the in-call app (%s)", component);
Santos Cordon049b7b62014-01-30 05:34:26 -0800223
224 // TODO(santoscordon): Implement retry or fall-back-to-default logic.
225 }
226 }
227 }
228
229 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800230 * Persists the {@link IInCallService} instance and starts the communication between
Sailesh Nepale59bb192014-04-01 18:33:59 -0700231 * 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 -0800232 * called after a successful binding connection is established.
233 *
234 * @param service The {@link IInCallService} implementation.
235 */
236 private void onConnected(IBinder service) {
237 ThreadUtil.checkOnMainThread();
238 mInCallService = IInCallService.Stub.asInterface(service);
239
240 try {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700241 mInCallService.setInCallAdapter(new InCallAdapter(CallsManager.getInstance(),
242 mCallIdMapper));
Santos Cordone3d76ab2014-01-28 17:25:20 -0800243 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800244 Log.e(this, e, "Failed to set the in-call adapter.");
Santos Cordone3d76ab2014-01-28 17:25:20 -0800245 mInCallService = null;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700246 return;
Santos Cordone3d76ab2014-01-28 17:25:20 -0800247 }
248
Santos Cordon049b7b62014-01-30 05:34:26 -0800249 // Upon successful connection, send the state of the world to the in-call app.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700250 ImmutableCollection<Call> calls = CallsManager.getInstance().getCalls();
251 if (!calls.isEmpty()) {
252 for (Call call : calls) {
253 onCallAdded(call);
254 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700255 onAudioStateChanged(null, CallsManager.getInstance().getAudioState());
Sailesh Nepal810735e2014-03-18 18:15:46 -0700256 } else {
257 unbind();
Santos Cordon049b7b62014-01-30 05:34:26 -0800258 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800259 }
260
261 /**
262 * Cleans up the instance of in-call app after the service has been unbound.
263 */
264 private void onDisconnected() {
265 ThreadUtil.checkOnMainThread();
266 mInCallService = null;
267 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700268
269 private void updateCall(Call call) {
270 if (mInCallService != null) {
271 try {
Santos Cordona1610702014-06-04 20:22:56 -0700272 InCallCall inCallCall = toInCallCall(call);
273 Log.v(this, "updateCall %s ==> %s", call, inCallCall);
274 mInCallService.updateCall(inCallCall);
Santos Cordonf3671a62014-05-29 21:51:53 -0700275 } catch (RemoteException ignored) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700276 }
277 }
278 }
279
280 private InCallCall toInCallCall(Call call) {
281 String callId = mCallIdMapper.getCallId(call);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700282
Sailesh Nepale20bc972014-07-09 21:22:36 -0700283 int capabilities = call.getCallCapabilities();
284 if (!CallsManager.getInstance().isAddCallCapable(call)) {
285 capabilities &= ~CallCapabilities.ADD_CALL;
Santos Cordon10838c22014-06-11 17:36:04 -0700286 }
Sailesh Nepale20bc972014-07-09 21:22:36 -0700287 if (call.isEmergencyCall()) {
288 capabilities &= ~CallCapabilities.MUTE;
Santos Cordona1610702014-06-04 20:22:56 -0700289 }
Sailesh Nepale20bc972014-07-09 21:22:36 -0700290
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700291 CallState state = call.getState();
292 if (state == CallState.ABORTED) {
293 state = CallState.DISCONNECTED;
294 }
Santos Cordona1610702014-06-04 20:22:56 -0700295
296 String parentCallId = null;
297 Call parentCall = call.getParentCall();
298 if (parentCall != null) {
299 parentCallId = mCallIdMapper.getCallId(parentCall);
300 }
301
302 long connectTimeMillis = call.getConnectTimeMillis();
303 List<Call> childCalls = call.getChildCalls();
304 List<String> childCallIds = new ArrayList<>();
305 if (!childCalls.isEmpty()) {
306 connectTimeMillis = Long.MAX_VALUE;
307 for (Call child : childCalls) {
308 connectTimeMillis = Math.min(child.getConnectTimeMillis(), connectTimeMillis);
309 childCallIds.add(mCallIdMapper.getCallId(child));
310 }
311 }
312
Ihab Awadff7493a2014-06-10 13:47:44 -0700313 if (call.isRespondViaSmsCapable()) {
314 capabilities |= CallCapabilities.RESPOND_VIA_TEXT;
315 }
316
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700317 Uri handle = call.getHandlePresentation() == CallPropertyPresentation.ALLOWED ?
318 call.getHandle() : null;
319 String callerDisplayName = call.getCallerDisplayNamePresentation() ==
320 CallPropertyPresentation.ALLOWED ? call.getCallerDisplayName() : null;
321
Ihab Awad0fea3f22014-06-03 18:45:05 -0700322 return new InCallCall(callId, state, call.getDisconnectCause(), call.getDisconnectMessage(),
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700323 call.getCannedSmsResponses(), capabilities, connectTimeMillis, handle,
324 call.getHandlePresentation(), callerDisplayName,
325 call.getCallerDisplayNamePresentation(), call.getGatewayInfo(),
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700326 call.getPhoneAccount(), call.getCallVideoProvider(), parentCallId, childCallIds,
Andrew Lee4a796602014-07-11 17:23:03 -0700327 call.getStatusHints() , call.getVideoState());
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700328 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800329}