blob: 5c36ca46917d890ec2c189fda117b49a877f7ed9 [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
Sailesh Nepal9d58de52014-07-18 14:53:19 -070019import android.app.PendingIntent;
Santos Cordone3d76ab2014-01-28 17:25:20 -080020import android.content.ComponentName;
21import android.content.Context;
22import android.content.Intent;
23import android.content.ServiceConnection;
Santos Cordonfdfcafa2014-06-26 14:49:05 -070024
25import android.content.res.Resources;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070026import android.net.Uri;
Santos Cordone3d76ab2014-01-28 17:25:20 -080027import android.os.IBinder;
28import android.os.RemoteException;
Amith Yamasani60e75842014-05-23 10:09:14 -070029import android.os.UserHandle;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070030import android.telecomm.CallAudioState;
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070031import android.telecomm.CallCapabilities;
Sailesh Nepale8ecb982014-07-11 17:19:42 -070032import android.telecomm.CallPropertyPresentation;
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070033import android.telecomm.CallState;
Santos Cordon2583b672014-07-19 13:07:33 -070034import android.telecomm.ParcelableCall;
Sailesh Nepala439e1b2014-03-11 18:19:58 -070035
36import com.android.internal.telecomm.IInCallService;
Sailesh Nepal810735e2014-03-18 18:15:46 -070037import com.google.common.collect.ImmutableCollection;
Santos Cordone3d76ab2014-01-28 17:25:20 -080038
Santos Cordona1610702014-06-04 20:22:56 -070039import java.util.ArrayList;
40import 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
Sailesh Nepale8ecb982014-07-11 17:19:42 -070064 private final Call.Listener mCallListener = new Call.ListenerBase() {
65 @Override
66 public void onCallCapabilitiesChanged(Call call) {
67 updateCall(call);
68 }
69
70 @Override
71 public void onCannedSmsResponsesLoaded(Call call) {
72 updateCall(call);
73 }
74
75 @Override
Andrew Lee3bcf9352014-07-23 12:36:05 -070076 public void onVideoCallProviderChanged(Call call) {
Sailesh Nepale8ecb982014-07-11 17:19:42 -070077 updateCall(call);
78 }
79
80 @Override
81 public void onStatusHintsChanged(Call call) {
82 updateCall(call);
83 }
84
85 @Override
86 public void onHandleChanged(Call call) {
87 updateCall(call);
88 }
89
90 @Override
91 public void onCallerDisplayNameChanged(Call call) {
92 updateCall(call);
93 }
Andrew Lee4a796602014-07-11 17:23:03 -070094
95 @Override
96 public void onVideoStateChanged(Call call) {
97 updateCall(call);
98 }
Sailesh Nepal9d58de52014-07-18 14:53:19 -070099
100 @Override
101 public void onStartActivityFromInCall(Call call, PendingIntent intent) {
102 if (mInCallService != null) {
103 Log.i(this, "Calling startActivity, intent: %s", intent);
104 try {
105 mInCallService.startActivity(mCallIdMapper.getCallId(call), intent);
106 } catch (RemoteException ignored) {
107 }
108 }
109 }
Ihab Awad69eb0f52014-07-18 11:20:37 -0700110
111 @Override
Ihab Awadb78b2762014-07-25 15:16:23 -0700112 public void onTargetPhoneAccountChanged(Call call) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700113 updateCall(call);
114 }
Santos Cordon12d61822014-07-29 16:02:20 -0700115
116 @Override
117 public void onConferenceableCallsChanged(Call call) {
118 updateCall(call);
119 }
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700120 };
121
Santos Cordone3d76ab2014-01-28 17:25:20 -0800122 /** Maintains a binding connection to the in-call app. */
123 private final InCallServiceConnection mConnection = new InCallServiceConnection();
124
Santos Cordone3d76ab2014-01-28 17:25:20 -0800125 /** The in-call app implementation, see {@link IInCallService}. */
126 private IInCallService mInCallService;
127
Sailesh Nepale59bb192014-04-01 18:33:59 -0700128 private final CallIdMapper mCallIdMapper = new CallIdMapper("InCall");
129
Santos Cordone3d76ab2014-01-28 17:25:20 -0800130 IInCallService getService() {
131 return mInCallService;
132 }
133
Sailesh Nepal810735e2014-03-18 18:15:46 -0700134 @Override
135 public void onCallAdded(Call call) {
136 if (mInCallService == null) {
137 bind();
138 } else {
139 Log.i(this, "Adding call: %s", call);
Ihab Awadff7493a2014-06-10 13:47:44 -0700140 if (mCallIdMapper.getCallId(call) == null) {
141 mCallIdMapper.addCall(call);
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700142 call.addListener(mCallListener);
Ihab Awadff7493a2014-06-10 13:47:44 -0700143 try {
Santos Cordon2583b672014-07-19 13:07:33 -0700144 mInCallService.addCall(toParcelableCall(call));
Ihab Awadff7493a2014-06-10 13:47:44 -0700145 } catch (RemoteException ignored) {
146 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800147 }
Santos Cordon049b7b62014-01-30 05:34:26 -0800148 }
149 }
150
Sailesh Nepal810735e2014-03-18 18:15:46 -0700151 @Override
152 public void onCallRemoved(Call call) {
153 if (CallsManager.getInstance().getCalls().isEmpty()) {
Santos Cordondf399862014-08-06 04:39:15 -0700154 // TODO: Wait for all messages to be delivered to the service before unbinding.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700155 unbind();
Santos Cordon049b7b62014-01-30 05:34:26 -0800156 }
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700157 call.removeListener(mCallListener);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700158 mCallIdMapper.removeCall(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800159 }
160
Sailesh Nepal810735e2014-03-18 18:15:46 -0700161 @Override
162 public void onCallStateChanged(Call call, CallState oldState, CallState newState) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700163 updateCall(call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700164 }
165
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700166 @Override
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700167 public void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700168 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700169 ConnectionServiceWrapper oldService,
170 ConnectionServiceWrapper newService) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700171 updateCall(call);
172 }
173
174 @Override
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700175 public void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
176 if (mInCallService != null) {
177 Log.i(this, "Calling onAudioStateChanged, audioState: %s -> %s", oldAudioState,
178 newAudioState);
179 try {
180 mInCallService.onAudioStateChanged(newAudioState);
Santos Cordonf3671a62014-05-29 21:51:53 -0700181 } catch (RemoteException ignored) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700182 }
183 }
184 }
185
Evan Charlton352105c2014-06-03 14:10:54 -0700186 void onPostDialWait(Call call, String remaining) {
187 if (mInCallService != null) {
188 Log.i(this, "Calling onPostDialWait, remaining = %s", remaining);
189 try {
190 mInCallService.setPostDialWait(mCallIdMapper.getCallId(call), remaining);
191 } catch (RemoteException ignored) {
192 }
193 }
194 }
195
Santos Cordona1610702014-06-04 20:22:56 -0700196 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700197 public void onIsConferencedChanged(Call call) {
198 Log.v(this, "onIsConferencedChanged %s", call);
199 updateCall(call);
200 }
201
Santos Cordonf3671a62014-05-29 21:51:53 -0700202 void bringToForeground(boolean showDialpad) {
203 if (mInCallService != null) {
204 try {
205 mInCallService.bringToForeground(showDialpad);
206 } catch (RemoteException ignored) {
207 }
208 } else {
209 Log.w(this, "Asking to bring unbound in-call UI to foreground.");
210 }
211 }
212
Santos Cordone3d76ab2014-01-28 17:25:20 -0800213 /**
214 * Unbinds an existing bound connection to the in-call app.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800215 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700216 private void unbind() {
Santos Cordone3d76ab2014-01-28 17:25:20 -0800217 ThreadUtil.checkOnMainThread();
218 if (mInCallService != null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800219 Log.i(this, "Unbinding from InCallService");
Santos Cordon049b7b62014-01-30 05:34:26 -0800220 TelecommApp.getInstance().unbindService(mConnection);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800221 mInCallService = null;
222 }
223 }
224
225 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800226 * Binds to the in-call app if not already connected by binding directly to the saved
227 * component name of the {@link IInCallService} implementation.
228 */
229 private void bind() {
230 ThreadUtil.checkOnMainThread();
231 if (mInCallService == null) {
Santos Cordonfdfcafa2014-06-26 14:49:05 -0700232 Context context = TelecommApp.getInstance();
233 Resources resources = context.getResources();
234 ComponentName component = new ComponentName(
235 resources.getString(R.string.ui_default_package),
236 resources.getString(R.string.incall_default_class));
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800237 Log.i(this, "Attempting to bind to InCallService: %s", component);
Santos Cordon049b7b62014-01-30 05:34:26 -0800238
239 Intent serviceIntent = new Intent(IInCallService.class.getName());
240 serviceIntent.setComponent(component);
241
Amith Yamasani60e75842014-05-23 10:09:14 -0700242 if (!context.bindServiceAsUser(serviceIntent, mConnection, Context.BIND_AUTO_CREATE,
243 UserHandle.CURRENT)) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800244 Log.w(this, "Could not connect to the in-call app (%s)", component);
Santos Cordon049b7b62014-01-30 05:34:26 -0800245
Santos Cordondf399862014-08-06 04:39:15 -0700246 // TODO: Implement retry or fall-back-to-default logic.
Santos Cordon049b7b62014-01-30 05:34:26 -0800247 }
248 }
249 }
250
251 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800252 * Persists the {@link IInCallService} instance and starts the communication between
Sailesh Nepale59bb192014-04-01 18:33:59 -0700253 * 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 -0800254 * called after a successful binding connection is established.
255 *
256 * @param service The {@link IInCallService} implementation.
257 */
258 private void onConnected(IBinder service) {
259 ThreadUtil.checkOnMainThread();
260 mInCallService = IInCallService.Stub.asInterface(service);
261
262 try {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700263 mInCallService.setInCallAdapter(new InCallAdapter(CallsManager.getInstance(),
264 mCallIdMapper));
Santos Cordone3d76ab2014-01-28 17:25:20 -0800265 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800266 Log.e(this, e, "Failed to set the in-call adapter.");
Santos Cordone3d76ab2014-01-28 17:25:20 -0800267 mInCallService = null;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700268 return;
Santos Cordone3d76ab2014-01-28 17:25:20 -0800269 }
270
Santos Cordon049b7b62014-01-30 05:34:26 -0800271 // Upon successful connection, send the state of the world to the in-call app.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700272 ImmutableCollection<Call> calls = CallsManager.getInstance().getCalls();
273 if (!calls.isEmpty()) {
274 for (Call call : calls) {
275 onCallAdded(call);
276 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700277 onAudioStateChanged(null, CallsManager.getInstance().getAudioState());
Sailesh Nepal810735e2014-03-18 18:15:46 -0700278 } else {
279 unbind();
Santos Cordon049b7b62014-01-30 05:34:26 -0800280 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800281 }
282
283 /**
284 * Cleans up the instance of in-call app after the service has been unbound.
285 */
286 private void onDisconnected() {
287 ThreadUtil.checkOnMainThread();
288 mInCallService = null;
289 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700290
291 private void updateCall(Call call) {
292 if (mInCallService != null) {
293 try {
Santos Cordon2583b672014-07-19 13:07:33 -0700294 ParcelableCall parcelableCall = toParcelableCall(call);
295 Log.v(this, "updateCall %s ==> %s", call, parcelableCall);
296 mInCallService.updateCall(parcelableCall);
Santos Cordonf3671a62014-05-29 21:51:53 -0700297 } catch (RemoteException ignored) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700298 }
299 }
300 }
301
Santos Cordon2583b672014-07-19 13:07:33 -0700302 private ParcelableCall toParcelableCall(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700303 String callId = mCallIdMapper.getCallId(call);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700304
Sailesh Nepale20bc972014-07-09 21:22:36 -0700305 int capabilities = call.getCallCapabilities();
Santos Cordon4b034a62014-07-18 13:42:05 -0700306 if (CallsManager.getInstance().isAddCallCapable(call)) {
307 capabilities |= CallCapabilities.ADD_CALL;
Santos Cordon10838c22014-06-11 17:36:04 -0700308 }
Santos Cordon4b034a62014-07-18 13:42:05 -0700309 if (!call.isEmergencyCall()) {
310 capabilities |= CallCapabilities.MUTE;
Santos Cordona1610702014-06-04 20:22:56 -0700311 }
Sailesh Nepale20bc972014-07-09 21:22:36 -0700312
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700313 CallState state = call.getState();
314 if (state == CallState.ABORTED) {
315 state = CallState.DISCONNECTED;
316 }
Santos Cordona1610702014-06-04 20:22:56 -0700317
318 String parentCallId = null;
319 Call parentCall = call.getParentCall();
320 if (parentCall != null) {
321 parentCallId = mCallIdMapper.getCallId(parentCall);
322 }
323
324 long connectTimeMillis = call.getConnectTimeMillis();
325 List<Call> childCalls = call.getChildCalls();
326 List<String> childCallIds = new ArrayList<>();
327 if (!childCalls.isEmpty()) {
328 connectTimeMillis = Long.MAX_VALUE;
329 for (Call child : childCalls) {
330 connectTimeMillis = Math.min(child.getConnectTimeMillis(), connectTimeMillis);
331 childCallIds.add(mCallIdMapper.getCallId(child));
332 }
333 }
334
Ihab Awadff7493a2014-06-10 13:47:44 -0700335 if (call.isRespondViaSmsCapable()) {
336 capabilities |= CallCapabilities.RESPOND_VIA_TEXT;
337 }
338
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700339 Uri handle = call.getHandlePresentation() == CallPropertyPresentation.ALLOWED ?
340 call.getHandle() : null;
341 String callerDisplayName = call.getCallerDisplayNamePresentation() ==
342 CallPropertyPresentation.ALLOWED ? call.getCallerDisplayName() : null;
343
Santos Cordon12d61822014-07-29 16:02:20 -0700344 List<Call> conferenceableCalls = call.getConferenceableCalls();
345 List<String> conferenceableCallIds = new ArrayList<String>(conferenceableCalls.size());
346 for (Call otherCall : conferenceableCalls) {
347 String otherId = mCallIdMapper.getCallId(otherCall);
348 if (otherId != null) {
349 conferenceableCallIds.add(otherId);
350 }
351 }
352
Santos Cordon2583b672014-07-19 13:07:33 -0700353 return new ParcelableCall(
354 callId,
355 state,
356 call.getDisconnectCause(),
357 call.getDisconnectMessage(),
358 call.getCannedSmsResponses(),
359 capabilities,
360 connectTimeMillis,
361 handle,
362 call.getHandlePresentation(),
363 callerDisplayName,
364 call.getCallerDisplayNamePresentation(),
365 call.getGatewayInfo(),
Ihab Awadb78b2762014-07-25 15:16:23 -0700366 call.getTargetPhoneAccount(),
Andrew Lee3bcf9352014-07-23 12:36:05 -0700367 call.getVideoCallProvider(),
Santos Cordon2583b672014-07-19 13:07:33 -0700368 parentCallId,
369 childCallIds,
370 call.getStatusHints(),
Santos Cordon12d61822014-07-29 16:02:20 -0700371 call.getVideoState(),
Nancy Chena9d91da2014-08-12 14:31:06 -0700372 conferenceableCallIds,
373 call.getExtras());
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700374 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800375}