blob: 3824c6f52d1a71ca09185b24180c3cfd0f3dc1fa [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 }
93 };
94
Santos Cordone3d76ab2014-01-28 17:25:20 -080095 /** Maintains a binding connection to the in-call app. */
96 private final InCallServiceConnection mConnection = new InCallServiceConnection();
97
Santos Cordone3d76ab2014-01-28 17:25:20 -080098 /** The in-call app implementation, see {@link IInCallService}. */
99 private IInCallService mInCallService;
100
Sailesh Nepale59bb192014-04-01 18:33:59 -0700101 private final CallIdMapper mCallIdMapper = new CallIdMapper("InCall");
102
Santos Cordone3d76ab2014-01-28 17:25:20 -0800103 IInCallService getService() {
104 return mInCallService;
105 }
106
Sailesh Nepal810735e2014-03-18 18:15:46 -0700107 @Override
108 public void onCallAdded(Call call) {
109 if (mInCallService == null) {
110 bind();
111 } else {
112 Log.i(this, "Adding call: %s", call);
Ihab Awadff7493a2014-06-10 13:47:44 -0700113 if (mCallIdMapper.getCallId(call) == null) {
114 mCallIdMapper.addCall(call);
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700115 call.addListener(mCallListener);
Ihab Awadff7493a2014-06-10 13:47:44 -0700116 try {
117 mInCallService.addCall(toInCallCall(call));
118 } catch (RemoteException ignored) {
119 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800120 }
Santos Cordon049b7b62014-01-30 05:34:26 -0800121 }
122 }
123
Sailesh Nepal810735e2014-03-18 18:15:46 -0700124 @Override
125 public void onCallRemoved(Call call) {
126 if (CallsManager.getInstance().getCalls().isEmpty()) {
127 // TODO(sail): Wait for all messages to be delivered to the service before unbinding.
128 unbind();
Santos Cordon049b7b62014-01-30 05:34:26 -0800129 }
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700130 call.removeListener(mCallListener);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700131 mCallIdMapper.removeCall(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800132 }
133
Sailesh Nepal810735e2014-03-18 18:15:46 -0700134 @Override
135 public void onCallStateChanged(Call call, CallState oldState, CallState newState) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700136 updateCall(call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700137 }
138
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700139 @Override
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700140 public void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700141 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700142 ConnectionServiceWrapper oldService,
143 ConnectionServiceWrapper newService) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700144 updateCall(call);
145 }
146
147 @Override
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700148 public void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
149 if (mInCallService != null) {
150 Log.i(this, "Calling onAudioStateChanged, audioState: %s -> %s", oldAudioState,
151 newAudioState);
152 try {
153 mInCallService.onAudioStateChanged(newAudioState);
Santos Cordonf3671a62014-05-29 21:51:53 -0700154 } catch (RemoteException ignored) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700155 }
156 }
157 }
158
Evan Charlton352105c2014-06-03 14:10:54 -0700159 void onPostDialWait(Call call, String remaining) {
160 if (mInCallService != null) {
161 Log.i(this, "Calling onPostDialWait, remaining = %s", remaining);
162 try {
163 mInCallService.setPostDialWait(mCallIdMapper.getCallId(call), remaining);
164 } catch (RemoteException ignored) {
165 }
166 }
167 }
168
Santos Cordona1610702014-06-04 20:22:56 -0700169 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700170 public void onIsConferencedChanged(Call call) {
171 Log.v(this, "onIsConferencedChanged %s", call);
172 updateCall(call);
173 }
174
Santos Cordonf3671a62014-05-29 21:51:53 -0700175 void bringToForeground(boolean showDialpad) {
176 if (mInCallService != null) {
177 try {
178 mInCallService.bringToForeground(showDialpad);
179 } catch (RemoteException ignored) {
180 }
181 } else {
182 Log.w(this, "Asking to bring unbound in-call UI to foreground.");
183 }
184 }
185
Santos Cordone3d76ab2014-01-28 17:25:20 -0800186 /**
187 * Unbinds an existing bound connection to the in-call app.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800188 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700189 private void unbind() {
Santos Cordone3d76ab2014-01-28 17:25:20 -0800190 ThreadUtil.checkOnMainThread();
191 if (mInCallService != null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800192 Log.i(this, "Unbinding from InCallService");
Santos Cordon049b7b62014-01-30 05:34:26 -0800193 TelecommApp.getInstance().unbindService(mConnection);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800194 mInCallService = null;
195 }
196 }
197
198 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800199 * Binds to the in-call app if not already connected by binding directly to the saved
200 * component name of the {@link IInCallService} implementation.
201 */
202 private void bind() {
203 ThreadUtil.checkOnMainThread();
204 if (mInCallService == null) {
Santos Cordonfdfcafa2014-06-26 14:49:05 -0700205 Context context = TelecommApp.getInstance();
206 Resources resources = context.getResources();
207 ComponentName component = new ComponentName(
208 resources.getString(R.string.ui_default_package),
209 resources.getString(R.string.incall_default_class));
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800210 Log.i(this, "Attempting to bind to InCallService: %s", component);
Santos Cordon049b7b62014-01-30 05:34:26 -0800211
212 Intent serviceIntent = new Intent(IInCallService.class.getName());
213 serviceIntent.setComponent(component);
214
Amith Yamasani60e75842014-05-23 10:09:14 -0700215 if (!context.bindServiceAsUser(serviceIntent, mConnection, Context.BIND_AUTO_CREATE,
216 UserHandle.CURRENT)) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800217 Log.w(this, "Could not connect to the in-call app (%s)", component);
Santos Cordon049b7b62014-01-30 05:34:26 -0800218
219 // TODO(santoscordon): Implement retry or fall-back-to-default logic.
220 }
221 }
222 }
223
224 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800225 * Persists the {@link IInCallService} instance and starts the communication between
Sailesh Nepale59bb192014-04-01 18:33:59 -0700226 * 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 -0800227 * called after a successful binding connection is established.
228 *
229 * @param service The {@link IInCallService} implementation.
230 */
231 private void onConnected(IBinder service) {
232 ThreadUtil.checkOnMainThread();
233 mInCallService = IInCallService.Stub.asInterface(service);
234
235 try {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700236 mInCallService.setInCallAdapter(new InCallAdapter(CallsManager.getInstance(),
237 mCallIdMapper));
Santos Cordone3d76ab2014-01-28 17:25:20 -0800238 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800239 Log.e(this, e, "Failed to set the in-call adapter.");
Santos Cordone3d76ab2014-01-28 17:25:20 -0800240 mInCallService = null;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700241 return;
Santos Cordone3d76ab2014-01-28 17:25:20 -0800242 }
243
Santos Cordon049b7b62014-01-30 05:34:26 -0800244 // Upon successful connection, send the state of the world to the in-call app.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700245 ImmutableCollection<Call> calls = CallsManager.getInstance().getCalls();
246 if (!calls.isEmpty()) {
247 for (Call call : calls) {
248 onCallAdded(call);
249 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700250 onAudioStateChanged(null, CallsManager.getInstance().getAudioState());
Sailesh Nepal810735e2014-03-18 18:15:46 -0700251 } else {
252 unbind();
Santos Cordon049b7b62014-01-30 05:34:26 -0800253 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800254 }
255
256 /**
257 * Cleans up the instance of in-call app after the service has been unbound.
258 */
259 private void onDisconnected() {
260 ThreadUtil.checkOnMainThread();
261 mInCallService = null;
262 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700263
264 private void updateCall(Call call) {
265 if (mInCallService != null) {
266 try {
Santos Cordona1610702014-06-04 20:22:56 -0700267 InCallCall inCallCall = toInCallCall(call);
268 Log.v(this, "updateCall %s ==> %s", call, inCallCall);
269 mInCallService.updateCall(inCallCall);
Santos Cordonf3671a62014-05-29 21:51:53 -0700270 } catch (RemoteException ignored) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700271 }
272 }
273 }
274
275 private InCallCall toInCallCall(Call call) {
276 String callId = mCallIdMapper.getCallId(call);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700277
Sailesh Nepale20bc972014-07-09 21:22:36 -0700278 int capabilities = call.getCallCapabilities();
279 if (!CallsManager.getInstance().isAddCallCapable(call)) {
280 capabilities &= ~CallCapabilities.ADD_CALL;
Santos Cordon10838c22014-06-11 17:36:04 -0700281 }
Sailesh Nepale20bc972014-07-09 21:22:36 -0700282 if (call.isEmergencyCall()) {
283 capabilities &= ~CallCapabilities.MUTE;
Santos Cordona1610702014-06-04 20:22:56 -0700284 }
Sailesh Nepale20bc972014-07-09 21:22:36 -0700285
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700286 CallState state = call.getState();
287 if (state == CallState.ABORTED) {
288 state = CallState.DISCONNECTED;
289 }
Santos Cordona1610702014-06-04 20:22:56 -0700290
291 String parentCallId = null;
292 Call parentCall = call.getParentCall();
293 if (parentCall != null) {
294 parentCallId = mCallIdMapper.getCallId(parentCall);
295 }
296
297 long connectTimeMillis = call.getConnectTimeMillis();
298 List<Call> childCalls = call.getChildCalls();
299 List<String> childCallIds = new ArrayList<>();
300 if (!childCalls.isEmpty()) {
301 connectTimeMillis = Long.MAX_VALUE;
302 for (Call child : childCalls) {
303 connectTimeMillis = Math.min(child.getConnectTimeMillis(), connectTimeMillis);
304 childCallIds.add(mCallIdMapper.getCallId(child));
305 }
306 }
307
Ihab Awadff7493a2014-06-10 13:47:44 -0700308 if (call.isRespondViaSmsCapable()) {
309 capabilities |= CallCapabilities.RESPOND_VIA_TEXT;
310 }
311
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700312 Uri handle = call.getHandlePresentation() == CallPropertyPresentation.ALLOWED ?
313 call.getHandle() : null;
314 String callerDisplayName = call.getCallerDisplayNamePresentation() ==
315 CallPropertyPresentation.ALLOWED ? call.getCallerDisplayName() : null;
316
Ihab Awad0fea3f22014-06-03 18:45:05 -0700317 return new InCallCall(callId, state, call.getDisconnectCause(), call.getDisconnectMessage(),
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700318 call.getCannedSmsResponses(), capabilities, connectTimeMillis, handle,
319 call.getHandlePresentation(), callerDisplayName,
320 call.getCallerDisplayNamePresentation(), call.getGatewayInfo(),
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700321 call.getPhoneAccount(), call.getCallVideoProvider(), parentCallId, childCallIds,
322 call.getStatusHints());
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700323 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800324}