blob: 21581f5805a794914a25a0e3445b8d59ca648496 [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
Tyler Gunn61b92102014-08-19 07:42:20 -070019import android.Manifest;
Sailesh Nepal9d58de52014-07-18 14:53:19 -070020import android.app.PendingIntent;
Santos Cordone3d76ab2014-01-28 17:25:20 -080021import android.content.ComponentName;
22import android.content.Context;
23import android.content.Intent;
24import android.content.ServiceConnection;
Tyler Gunn61b92102014-08-19 07:42:20 -070025import android.content.pm.PackageManager;
26import android.content.pm.ResolveInfo;
27import android.content.pm.ServiceInfo;
Santos Cordonfdfcafa2014-06-26 14:49:05 -070028import android.content.res.Resources;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070029import android.net.Uri;
Santos Cordone3d76ab2014-01-28 17:25:20 -080030import android.os.IBinder;
31import android.os.RemoteException;
Amith Yamasani60e75842014-05-23 10:09:14 -070032import android.os.UserHandle;
Ihab Awad6fb37c82014-08-07 19:48:57 -070033import android.telecomm.AudioState;
Andrew Leebfffd2d2014-08-27 16:28:31 -070034import android.telecomm.CallProperties;
Tyler Gunn61b92102014-08-19 07:42:20 -070035import android.telecomm.CallState;
36import android.telecomm.InCallService;
37import android.telecomm.ParcelableCall;
Ihab Awad6fb37c82014-08-07 19:48:57 -070038import android.telecomm.PhoneCapabilities;
39import android.telecomm.PropertyPresentation;
Tyler Gunn61b92102014-08-19 07:42:20 -070040import android.util.ArrayMap;
Sailesh Nepala439e1b2014-03-11 18:19:58 -070041
42import com.android.internal.telecomm.IInCallService;
Sailesh Nepal810735e2014-03-18 18:15:46 -070043import com.google.common.collect.ImmutableCollection;
Santos Cordone3d76ab2014-01-28 17:25:20 -080044
Santos Cordona1610702014-06-04 20:22:56 -070045import java.util.ArrayList;
Tyler Gunn61b92102014-08-19 07:42:20 -070046import java.util.Iterator;
Santos Cordona1610702014-06-04 20:22:56 -070047import java.util.List;
Tyler Gunn61b92102014-08-19 07:42:20 -070048import java.util.Map;
49import java.util.concurrent.ConcurrentHashMap;
Santos Cordona1610702014-06-04 20:22:56 -070050
Santos Cordone3d76ab2014-01-28 17:25:20 -080051/**
52 * Binds to {@link IInCallService} and provides the service to {@link CallsManager} through which it
53 * 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 -070054 * a binding to the {@link IInCallService} (implemented by the in-call app).
Santos Cordone3d76ab2014-01-28 17:25:20 -080055 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070056public final class InCallController extends CallsManagerListenerBase {
Santos Cordone3d76ab2014-01-28 17:25:20 -080057 /**
58 * Used to bind to the in-call app and triggers the start of communication between
Sailesh Nepale59bb192014-04-01 18:33:59 -070059 * this class and in-call app.
Santos Cordone3d76ab2014-01-28 17:25:20 -080060 */
61 private class InCallServiceConnection implements ServiceConnection {
62 /** {@inheritDoc} */
63 @Override public void onServiceConnected(ComponentName name, IBinder service) {
Tyler Gunn61b92102014-08-19 07:42:20 -070064 onConnected(name, service);
Santos Cordone3d76ab2014-01-28 17:25:20 -080065 }
66
67 /** {@inheritDoc} */
68 @Override public void onServiceDisconnected(ComponentName name) {
Tyler Gunn61b92102014-08-19 07:42:20 -070069 onDisconnected(name);
Santos Cordone3d76ab2014-01-28 17:25:20 -080070 }
71 }
72
Sailesh Nepale8ecb982014-07-11 17:19:42 -070073 private final Call.Listener mCallListener = new Call.ListenerBase() {
74 @Override
75 public void onCallCapabilitiesChanged(Call call) {
76 updateCall(call);
77 }
78
79 @Override
80 public void onCannedSmsResponsesLoaded(Call call) {
81 updateCall(call);
82 }
83
84 @Override
Andrew Lee3bcf9352014-07-23 12:36:05 -070085 public void onVideoCallProviderChanged(Call call) {
Sailesh Nepale8ecb982014-07-11 17:19:42 -070086 updateCall(call);
87 }
88
89 @Override
90 public void onStatusHintsChanged(Call call) {
91 updateCall(call);
92 }
93
94 @Override
95 public void onHandleChanged(Call call) {
96 updateCall(call);
97 }
98
99 @Override
100 public void onCallerDisplayNameChanged(Call call) {
101 updateCall(call);
102 }
Andrew Lee4a796602014-07-11 17:23:03 -0700103
104 @Override
105 public void onVideoStateChanged(Call call) {
106 updateCall(call);
107 }
Sailesh Nepal9d58de52014-07-18 14:53:19 -0700108
109 @Override
110 public void onStartActivityFromInCall(Call call, PendingIntent intent) {
Tyler Gunn61b92102014-08-19 07:42:20 -0700111 if (!mInCallServices.isEmpty()) {
Sailesh Nepal9d58de52014-07-18 14:53:19 -0700112 Log.i(this, "Calling startActivity, intent: %s", intent);
Tyler Gunn61b92102014-08-19 07:42:20 -0700113 for (IInCallService inCallService : mInCallServices.values()) {
114 try {
115 inCallService.startActivity(mCallIdMapper.getCallId(call), intent);
116 } catch (RemoteException ignored) {
117 }
Sailesh Nepal9d58de52014-07-18 14:53:19 -0700118 }
119 }
120 }
Ihab Awad69eb0f52014-07-18 11:20:37 -0700121
122 @Override
Ihab Awadb78b2762014-07-25 15:16:23 -0700123 public void onTargetPhoneAccountChanged(Call call) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700124 updateCall(call);
125 }
Santos Cordon12d61822014-07-29 16:02:20 -0700126
127 @Override
128 public void onConferenceableCallsChanged(Call call) {
129 updateCall(call);
130 }
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700131 };
132
Tyler Gunn61b92102014-08-19 07:42:20 -0700133 /**
134 * Maintains a binding connection to the in-call app(s).
135 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
136 * load factor before resizing, 1 means we only expect a single thread to
137 * access the map so make only a single shard
138 */
139 private final Map<ComponentName, InCallServiceConnection> mServiceConnections =
140 new ConcurrentHashMap<ComponentName, InCallServiceConnection>(8, 0.9f, 1);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800141
Tyler Gunn61b92102014-08-19 07:42:20 -0700142 /** The in-call app implementations, see {@link IInCallService}. */
143 private final Map<ComponentName, IInCallService> mInCallServices = new ArrayMap<>();
Santos Cordone3d76ab2014-01-28 17:25:20 -0800144
Sailesh Nepale59bb192014-04-01 18:33:59 -0700145 private final CallIdMapper mCallIdMapper = new CallIdMapper("InCall");
146
Tyler Gunn12bccad2014-08-21 13:52:03 -0700147 /** The {@link ComponentName} of the default InCall UI. */
Tyler Gunn61b92102014-08-19 07:42:20 -0700148 private ComponentName mInCallComponentName;
149
150 public InCallController() {
151 Context context = TelecommApp.getInstance();
152 Resources resources = context.getResources();
153
154 mInCallComponentName = new ComponentName(
155 resources.getString(R.string.ui_default_package),
156 resources.getString(R.string.incall_default_class));
Santos Cordone3d76ab2014-01-28 17:25:20 -0800157 }
158
Sailesh Nepal810735e2014-03-18 18:15:46 -0700159 @Override
160 public void onCallAdded(Call call) {
Tyler Gunn61b92102014-08-19 07:42:20 -0700161 if (mInCallServices.isEmpty()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700162 bind();
163 } else {
164 Log.i(this, "Adding call: %s", call);
Tyler Gunn61b92102014-08-19 07:42:20 -0700165 // Track the call if we don't already know about it.
166 addCall(call);
167
Tyler Gunn12bccad2014-08-21 13:52:03 -0700168 for (Map.Entry<ComponentName, IInCallService> entry : mInCallServices.entrySet()) {
169 ComponentName componentName = entry.getKey();
170 IInCallService inCallService = entry.getValue();
171
172 ParcelableCall parcelableCall = toParcelableCall(call,
173 componentName.equals(mInCallComponentName) /* includeVideoProvider */);
Ihab Awadff7493a2014-06-10 13:47:44 -0700174 try {
Tyler Gunn61b92102014-08-19 07:42:20 -0700175 inCallService.addCall(parcelableCall);
Ihab Awadff7493a2014-06-10 13:47:44 -0700176 } catch (RemoteException ignored) {
177 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800178 }
Santos Cordon049b7b62014-01-30 05:34:26 -0800179 }
180 }
181
Sailesh Nepal810735e2014-03-18 18:15:46 -0700182 @Override
183 public void onCallRemoved(Call call) {
184 if (CallsManager.getInstance().getCalls().isEmpty()) {
Santos Cordondf399862014-08-06 04:39:15 -0700185 // TODO: Wait for all messages to be delivered to the service before unbinding.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700186 unbind();
Santos Cordon049b7b62014-01-30 05:34:26 -0800187 }
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700188 call.removeListener(mCallListener);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700189 mCallIdMapper.removeCall(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800190 }
191
Sailesh Nepal810735e2014-03-18 18:15:46 -0700192 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700193 public void onCallStateChanged(Call call, int oldState, int newState) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700194 updateCall(call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700195 }
196
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700197 @Override
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700198 public void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700199 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700200 ConnectionServiceWrapper oldService,
201 ConnectionServiceWrapper newService) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700202 updateCall(call);
203 }
204
205 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700206 public void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) {
Tyler Gunn61b92102014-08-19 07:42:20 -0700207 if (!mInCallServices.isEmpty()) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700208 Log.i(this, "Calling onAudioStateChanged, audioState: %s -> %s", oldAudioState,
209 newAudioState);
Tyler Gunn61b92102014-08-19 07:42:20 -0700210 for (IInCallService inCallService : mInCallServices.values()) {
211 try {
212 inCallService.onAudioStateChanged(newAudioState);
213 } catch (RemoteException ignored) {
214 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700215 }
216 }
217 }
218
Evan Charlton352105c2014-06-03 14:10:54 -0700219 void onPostDialWait(Call call, String remaining) {
Tyler Gunn61b92102014-08-19 07:42:20 -0700220 if (!mInCallServices.isEmpty()) {
Evan Charlton352105c2014-06-03 14:10:54 -0700221 Log.i(this, "Calling onPostDialWait, remaining = %s", remaining);
Tyler Gunn61b92102014-08-19 07:42:20 -0700222 for (IInCallService inCallService : mInCallServices.values()) {
223 try {
224 inCallService.setPostDialWait(mCallIdMapper.getCallId(call), remaining);
225 } catch (RemoteException ignored) {
226 }
Evan Charlton352105c2014-06-03 14:10:54 -0700227 }
228 }
229 }
230
Santos Cordona1610702014-06-04 20:22:56 -0700231 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700232 public void onIsConferencedChanged(Call call) {
Santos Cordon0fbe6322014-08-14 04:04:25 -0700233 Log.d(this, "onIsConferencedChanged %s", call);
Santos Cordona1610702014-06-04 20:22:56 -0700234 updateCall(call);
235 }
236
Santos Cordonf3671a62014-05-29 21:51:53 -0700237 void bringToForeground(boolean showDialpad) {
Tyler Gunn61b92102014-08-19 07:42:20 -0700238 if (!mInCallServices.isEmpty()) {
239 for (IInCallService inCallService : mInCallServices.values()) {
240 try {
241 inCallService.bringToForeground(showDialpad);
242 } catch (RemoteException ignored) {
243 }
Santos Cordonf3671a62014-05-29 21:51:53 -0700244 }
245 } else {
246 Log.w(this, "Asking to bring unbound in-call UI to foreground.");
247 }
248 }
249
Santos Cordone3d76ab2014-01-28 17:25:20 -0800250 /**
251 * Unbinds an existing bound connection to the in-call app.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800252 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700253 private void unbind() {
Santos Cordone3d76ab2014-01-28 17:25:20 -0800254 ThreadUtil.checkOnMainThread();
Tyler Gunn61b92102014-08-19 07:42:20 -0700255 if (!mInCallServices.isEmpty()) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800256 Log.i(this, "Unbinding from InCallService");
Tyler Gunn61b92102014-08-19 07:42:20 -0700257 for (InCallServiceConnection connection : mServiceConnections.values()) {
258 TelecommApp.getInstance().unbindService(connection);
259 }
260 mInCallServices.clear();
Santos Cordone3d76ab2014-01-28 17:25:20 -0800261 }
262 }
263
264 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800265 * Binds to the in-call app if not already connected by binding directly to the saved
266 * component name of the {@link IInCallService} implementation.
267 */
268 private void bind() {
269 ThreadUtil.checkOnMainThread();
Tyler Gunn61b92102014-08-19 07:42:20 -0700270 if (mInCallServices.isEmpty()) {
271 mServiceConnections.clear();
Santos Cordonfdfcafa2014-06-26 14:49:05 -0700272 Context context = TelecommApp.getInstance();
Tyler Gunn61b92102014-08-19 07:42:20 -0700273 PackageManager packageManager = TelecommApp.getInstance().getPackageManager();
274 Intent intent = new Intent(InCallService.SERVICE_INTERFACE);
Santos Cordon049b7b62014-01-30 05:34:26 -0800275
Tyler Gunn61b92102014-08-19 07:42:20 -0700276 for (ResolveInfo entry : packageManager.queryIntentServices(intent, 0)) {
277 ServiceInfo serviceInfo = entry.serviceInfo;
278 if (serviceInfo != null) {
279 boolean hasServiceBindPermission = serviceInfo.permission != null &&
280 serviceInfo.permission.equals(
281 Manifest.permission.BIND_INCALL_SERVICE);
282 boolean hasControlInCallPermission = packageManager.checkPermission(
283 Manifest.permission.CONTROL_INCALL_EXPERIENCE,
284 serviceInfo.packageName) == PackageManager.PERMISSION_GRANTED;
Santos Cordon049b7b62014-01-30 05:34:26 -0800285
Tyler Gunn61b92102014-08-19 07:42:20 -0700286 if (!hasServiceBindPermission) {
287 Log.w(this, "InCallService does not have BIND_INCALL_SERVICE permission: " +
288 serviceInfo.packageName);
289 continue;
290 }
Santos Cordon049b7b62014-01-30 05:34:26 -0800291
Tyler Gunn61b92102014-08-19 07:42:20 -0700292 if (!hasControlInCallPermission) {
293 Log.w(this,
294 "InCall UI does not have CONTROL_INCALL_EXPERIENCE permission: " +
295 serviceInfo.packageName);
296 continue;
297 }
298
299 Log.i(this, "Attempting to bind to InCall " + serviceInfo.packageName);
300 InCallServiceConnection inCallServiceConnection = new InCallServiceConnection();
301 ComponentName componentName = new ComponentName(serviceInfo.packageName,
302 serviceInfo.name);
303 intent.setComponent(componentName);
304
305 if (context.bindServiceAsUser(intent, inCallServiceConnection,
306 Context.BIND_AUTO_CREATE, UserHandle.CURRENT)) {
307 mServiceConnections.put(componentName, inCallServiceConnection);
308 }
309 }
Santos Cordon049b7b62014-01-30 05:34:26 -0800310 }
311 }
312 }
313
314 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800315 * Persists the {@link IInCallService} instance and starts the communication between
Sailesh Nepale59bb192014-04-01 18:33:59 -0700316 * 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 -0800317 * called after a successful binding connection is established.
318 *
Tyler Gunn61b92102014-08-19 07:42:20 -0700319 * @param componentName The service {@link ComponentName}.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800320 * @param service The {@link IInCallService} implementation.
321 */
Tyler Gunn61b92102014-08-19 07:42:20 -0700322 private void onConnected(ComponentName componentName, IBinder service) {
Santos Cordone3d76ab2014-01-28 17:25:20 -0800323 ThreadUtil.checkOnMainThread();
Tyler Gunn61b92102014-08-19 07:42:20 -0700324
325 IInCallService inCallService = IInCallService.Stub.asInterface(service);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800326
327 try {
Tyler Gunn61b92102014-08-19 07:42:20 -0700328 inCallService.setInCallAdapter(new InCallAdapter(CallsManager.getInstance(),
Sailesh Nepale59bb192014-04-01 18:33:59 -0700329 mCallIdMapper));
Tyler Gunn61b92102014-08-19 07:42:20 -0700330 mInCallServices.put(componentName, inCallService);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800331 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800332 Log.e(this, e, "Failed to set the in-call adapter.");
Sailesh Nepal810735e2014-03-18 18:15:46 -0700333 return;
Santos Cordone3d76ab2014-01-28 17:25:20 -0800334 }
335
Tyler Gunn61b92102014-08-19 07:42:20 -0700336 // Upon successful connection, send the state of the world to the service.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700337 ImmutableCollection<Call> calls = CallsManager.getInstance().getCalls();
338 if (!calls.isEmpty()) {
339 for (Call call : calls) {
Tyler Gunn61b92102014-08-19 07:42:20 -0700340 try {
341 // Track the call if we don't already know about it.
342 addCall(call);
343
Tyler Gunn12bccad2014-08-21 13:52:03 -0700344 inCallService.addCall(toParcelableCall(call,
345 componentName.equals(mInCallComponentName) /* includeVideoProvider */));
Tyler Gunn61b92102014-08-19 07:42:20 -0700346 } catch (RemoteException ignored) {
347 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700348 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700349 onAudioStateChanged(null, CallsManager.getInstance().getAudioState());
Sailesh Nepal810735e2014-03-18 18:15:46 -0700350 } else {
351 unbind();
Santos Cordon049b7b62014-01-30 05:34:26 -0800352 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800353 }
354
355 /**
Tyler Gunn61b92102014-08-19 07:42:20 -0700356 * Cleans up an instance of in-call app after the service has been unbound.
357 *
358 * @param disconnectedComponent The {@link ComponentName} of the service which disconnected.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800359 */
Tyler Gunn61b92102014-08-19 07:42:20 -0700360 private void onDisconnected(ComponentName disconnectedComponent) {
Santos Cordone3d76ab2014-01-28 17:25:20 -0800361 ThreadUtil.checkOnMainThread();
Tyler Gunn61b92102014-08-19 07:42:20 -0700362 if (mInCallServices.containsKey(disconnectedComponent)) {
363 mInCallServices.remove(disconnectedComponent);
364 }
365
366 // If the default in-call UI has disconnected, disconnect all calls and un-bind all other
367 // InCallService implementations.
368 if (disconnectedComponent.equals(mInCallComponentName)) {
369 Log.i(this, "In-call UI %s disconnected.", disconnectedComponent);
370 CallsManager.getInstance().disconnectAllCalls();
371
372 // Iterate through the in-call services, removing them as they are un-bound.
373 Iterator<Map.Entry<ComponentName, IInCallService>> it =
374 mInCallServices.entrySet().iterator();
375 while (it.hasNext()) {
376 Map.Entry<ComponentName, IInCallService> entry = it.next();
377 ComponentName componentName = entry.getKey();
378
379 InCallServiceConnection connection = mServiceConnections.remove(componentName);
380 it.remove();
381 if (connection == null) {
382 continue;
383 }
384
385 Log.i(this, "Unbinding other InCallService %s", componentName);
386 TelecommApp.getInstance().unbindService(connection);
387 }
388 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800389 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700390
Tyler Gunn12bccad2014-08-21 13:52:03 -0700391 /**
392 * Informs all {@link InCallService} instances of the updated call information. Changes to the
393 * video provider are only communicated to the default in-call UI.
394 *
395 * @param call The {@link Call}.
396 */
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700397 private void updateCall(Call call) {
Tyler Gunn61b92102014-08-19 07:42:20 -0700398 if (!mInCallServices.isEmpty()) {
Tyler Gunn12bccad2014-08-21 13:52:03 -0700399 for (Map.Entry<ComponentName, IInCallService> entry : mInCallServices.entrySet()) {
400 ComponentName componentName = entry.getKey();
401 IInCallService inCallService = entry.getValue();
402 ParcelableCall parcelableCall = toParcelableCall(call,
403 componentName.equals(mInCallComponentName) /* includeVideoProvider */);
404
405 Log.v(this, "updateCall %s ==> %s", call, parcelableCall);
Tyler Gunn61b92102014-08-19 07:42:20 -0700406 try {
407 inCallService.updateCall(parcelableCall);
408 } catch (RemoteException ignored) {
409 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700410 }
411 }
412 }
413
Tyler Gunn12bccad2014-08-21 13:52:03 -0700414 /**
415 * Parcels all information for a {@link Call} into a new {@link ParcelableCall} instance.
416 *
417 * @param call The {@link Call} to parcel.
418 * @param includeVideoProvider When {@code true}, the {@link IVideoProvider} is included in the
419 * parcelled call. When {@code false}, the {@link IVideoProvider} is not included.
420 * @return The {@link ParcelableCall} containing all call information from the {@link Call}.
421 */
422 private ParcelableCall toParcelableCall(Call call, boolean includeVideoProvider) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700423 String callId = mCallIdMapper.getCallId(call);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700424
Sailesh Nepale20bc972014-07-09 21:22:36 -0700425 int capabilities = call.getCallCapabilities();
Santos Cordon4b034a62014-07-18 13:42:05 -0700426 if (CallsManager.getInstance().isAddCallCapable(call)) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700427 capabilities |= PhoneCapabilities.ADD_CALL;
Santos Cordon10838c22014-06-11 17:36:04 -0700428 }
Tyler Gunn1711aec2014-08-26 14:48:20 -0700429
430 // Disable mute for emergency calls.
431 if (call.isEmergencyCall()) {
432 capabilities &= ~PhoneCapabilities.MUTE;
Santos Cordona1610702014-06-04 20:22:56 -0700433 }
Sailesh Nepale20bc972014-07-09 21:22:36 -0700434
Andrew Leebfffd2d2014-08-27 16:28:31 -0700435 int properties = call.isConference() ? CallProperties.CONFERENCE : 0;
436
Ihab Awad6fb37c82014-08-07 19:48:57 -0700437 int state = call.getState();
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700438 if (state == CallState.ABORTED) {
439 state = CallState.DISCONNECTED;
440 }
Santos Cordona1610702014-06-04 20:22:56 -0700441
442 String parentCallId = null;
443 Call parentCall = call.getParentCall();
444 if (parentCall != null) {
445 parentCallId = mCallIdMapper.getCallId(parentCall);
446 }
447
448 long connectTimeMillis = call.getConnectTimeMillis();
449 List<Call> childCalls = call.getChildCalls();
450 List<String> childCallIds = new ArrayList<>();
451 if (!childCalls.isEmpty()) {
452 connectTimeMillis = Long.MAX_VALUE;
453 for (Call child : childCalls) {
454 connectTimeMillis = Math.min(child.getConnectTimeMillis(), connectTimeMillis);
455 childCallIds.add(mCallIdMapper.getCallId(child));
456 }
457 }
458
Ihab Awadff7493a2014-06-10 13:47:44 -0700459 if (call.isRespondViaSmsCapable()) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700460 capabilities |= PhoneCapabilities.RESPOND_VIA_TEXT;
Ihab Awadff7493a2014-06-10 13:47:44 -0700461 }
462
Ihab Awad6fb37c82014-08-07 19:48:57 -0700463 Uri handle = call.getHandlePresentation() == PropertyPresentation.ALLOWED ?
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700464 call.getHandle() : null;
465 String callerDisplayName = call.getCallerDisplayNamePresentation() ==
Ihab Awad6fb37c82014-08-07 19:48:57 -0700466 PropertyPresentation.ALLOWED ? call.getCallerDisplayName() : null;
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700467
Santos Cordon12d61822014-07-29 16:02:20 -0700468 List<Call> conferenceableCalls = call.getConferenceableCalls();
469 List<String> conferenceableCallIds = new ArrayList<String>(conferenceableCalls.size());
470 for (Call otherCall : conferenceableCalls) {
471 String otherId = mCallIdMapper.getCallId(otherCall);
472 if (otherId != null) {
473 conferenceableCallIds.add(otherId);
474 }
475 }
476
Santos Cordon2583b672014-07-19 13:07:33 -0700477 return new ParcelableCall(
478 callId,
479 state,
480 call.getDisconnectCause(),
481 call.getDisconnectMessage(),
482 call.getCannedSmsResponses(),
483 capabilities,
Andrew Leebfffd2d2014-08-27 16:28:31 -0700484 properties,
Santos Cordon2583b672014-07-19 13:07:33 -0700485 connectTimeMillis,
486 handle,
487 call.getHandlePresentation(),
488 callerDisplayName,
489 call.getCallerDisplayNamePresentation(),
490 call.getGatewayInfo(),
Ihab Awadb78b2762014-07-25 15:16:23 -0700491 call.getTargetPhoneAccount(),
Tyler Gunn12bccad2014-08-21 13:52:03 -0700492 includeVideoProvider ? call.getVideoProvider() : null,
Santos Cordon2583b672014-07-19 13:07:33 -0700493 parentCallId,
494 childCallIds,
495 call.getStatusHints(),
Santos Cordon12d61822014-07-29 16:02:20 -0700496 call.getVideoState(),
Nancy Chena9d91da2014-08-12 14:31:06 -0700497 conferenceableCallIds,
498 call.getExtras());
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700499 }
Tyler Gunn61b92102014-08-19 07:42:20 -0700500
501 /**
502 * Adds the call to the list of calls tracked by the {@link InCallController}.
503 * @param call The call to add.
504 */
505 private void addCall(Call call) {
506 if (mCallIdMapper.getCallId(call) == null) {
507 mCallIdMapper.addCall(call);
508 call.addListener(mCallListener);
509 }
510 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800511}