blob: 70fe88be8182e96640101c1b65ef3dc92c83e4a8 [file] [log] [blame]
Eric Erfanianccca3152017-02-22 16:32:36 -08001/*
2 * Copyright (C) 2013 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.incallui;
18
19import android.content.Context;
20import android.content.Intent;
21import android.graphics.Point;
22import android.os.Bundle;
23import android.os.Handler;
Eric Erfanian2ca43182017-08-31 06:57:16 -070024import android.os.Trace;
twyen8efb4952017-10-06 16:35:54 -070025import android.support.annotation.MainThread;
Eric Erfanianccca3152017-02-22 16:32:36 -080026import android.support.annotation.NonNull;
27import android.support.annotation.Nullable;
28import android.support.annotation.VisibleForTesting;
Eric Erfanian83b20212017-05-31 08:53:10 -070029import android.support.v4.os.UserManagerCompat;
Eric Erfanianccca3152017-02-22 16:32:36 -080030import android.telecom.Call.Details;
31import android.telecom.DisconnectCause;
32import android.telecom.PhoneAccount;
33import android.telecom.PhoneAccountHandle;
34import android.telecom.TelecomManager;
35import android.telecom.VideoProfile;
36import android.telephony.PhoneStateListener;
37import android.telephony.TelephonyManager;
twyen8efb4952017-10-06 16:35:54 -070038import android.util.ArraySet;
Eric Erfanianccca3152017-02-22 16:32:36 -080039import android.view.Window;
40import android.view.WindowManager;
Eric Erfanianccca3152017-02-22 16:32:36 -080041import com.android.contacts.common.compat.CallCompat;
42import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler;
43import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler.OnCheckBlockedListener;
Eric Erfanian83b20212017-05-31 08:53:10 -070044import com.android.dialer.blocking.FilteredNumberCompat;
Eric Erfanianccca3152017-02-22 16:32:36 -080045import com.android.dialer.blocking.FilteredNumbersUtil;
twyen8efb4952017-10-06 16:35:54 -070046import com.android.dialer.common.Assert;
Eric Erfanianccca3152017-02-22 16:32:36 -080047import com.android.dialer.common.LogUtil;
Eric Erfanian2ca43182017-08-31 06:57:16 -070048import com.android.dialer.common.concurrent.DefaultDialerExecutorFactory;
Eric Erfaniand8046e52017-04-06 09:41:50 -070049import com.android.dialer.enrichedcall.EnrichedCallComponent;
Eric Erfanian10b34a52017-05-04 08:23:17 -070050import com.android.dialer.location.GeoUtil;
Eric Erfanian8369df02017-05-03 10:27:13 -070051import com.android.dialer.logging.InteractionEvent;
Eric Erfanianccca3152017-02-22 16:32:36 -080052import com.android.dialer.logging.Logger;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070053import com.android.dialer.postcall.PostCall;
Eric Erfanianccca3152017-02-22 16:32:36 -080054import com.android.dialer.telecom.TelecomUtil;
55import com.android.dialer.util.TouchPointManager;
56import com.android.incallui.InCallOrientationEventListener.ScreenOrientation;
57import com.android.incallui.answerproximitysensor.PseudoScreenState;
58import com.android.incallui.call.CallList;
59import com.android.incallui.call.DialerCall;
Eric Erfanianccca3152017-02-22 16:32:36 -080060import com.android.incallui.call.ExternalCallList;
Eric Erfanianccca3152017-02-22 16:32:36 -080061import com.android.incallui.call.TelecomAdapter;
Eric Erfanian2ca43182017-08-31 06:57:16 -070062import com.android.incallui.disconnectdialog.DisconnectMessage;
twyen8efb4952017-10-06 16:35:54 -070063import com.android.incallui.incalluilock.InCallUiLock;
Eric Erfanianccca3152017-02-22 16:32:36 -080064import com.android.incallui.latencyreport.LatencyReport;
65import com.android.incallui.legacyblocking.BlockedNumberContentObserver;
66import com.android.incallui.spam.SpamCallListListener;
67import com.android.incallui.util.TelecomCallUtil;
68import com.android.incallui.videosurface.bindings.VideoSurfaceBindings;
69import com.android.incallui.videosurface.protocol.VideoSurfaceTexture;
Eric Erfanian90508232017-03-24 09:31:16 -070070import com.android.incallui.videotech.utils.VideoUtils;
Eric Erfanianccca3152017-02-22 16:32:36 -080071import java.util.Collections;
72import java.util.List;
73import java.util.Objects;
74import java.util.Set;
75import java.util.concurrent.ConcurrentHashMap;
76import java.util.concurrent.CopyOnWriteArrayList;
77import java.util.concurrent.atomic.AtomicBoolean;
78
79/**
80 * Takes updates from the CallList and notifies the InCallActivity (UI) of the changes. Responsible
81 * for starting the activity for a new call and finishing the activity when all calls are
82 * disconnected. Creates and manages the in-call state and provides a listener pattern for the
83 * presenters that want to listen in on the in-call state changes. TODO: This class has become more
84 * of a state machine at this point. Consider renaming.
85 */
Eric Erfaniand5e47f62017-03-15 14:41:07 -070086public class InCallPresenter implements CallList.Listener {
Eric Erfanian2ca43182017-08-31 06:57:16 -070087 private static final String PIXEL2017_SYSTEM_FEATURE =
88 "com.google.android.feature.PIXEL_2017_EXPERIENCE";
Eric Erfanianccca3152017-02-22 16:32:36 -080089 private static final String EXTRA_FIRST_TIME_SHOWN =
90 "com.android.incallui.intent.extra.FIRST_TIME_SHOWN";
91
92 private static final long BLOCK_QUERY_TIMEOUT_MS = 1000;
93
94 private static final Bundle EMPTY_EXTRAS = new Bundle();
95
96 private static InCallPresenter sInCallPresenter;
97
98 /**
99 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is load factor before
100 * resizing, 1 means we only expect a single thread to access the map so make only a single shard
101 */
102 private final Set<InCallStateListener> mListeners =
103 Collections.newSetFromMap(new ConcurrentHashMap<InCallStateListener, Boolean>(8, 0.9f, 1));
104
105 private final List<IncomingCallListener> mIncomingCallListeners = new CopyOnWriteArrayList<>();
106 private final Set<InCallDetailsListener> mDetailsListeners =
107 Collections.newSetFromMap(new ConcurrentHashMap<InCallDetailsListener, Boolean>(8, 0.9f, 1));
108 private final Set<CanAddCallListener> mCanAddCallListeners =
109 Collections.newSetFromMap(new ConcurrentHashMap<CanAddCallListener, Boolean>(8, 0.9f, 1));
110 private final Set<InCallUiListener> mInCallUiListeners =
111 Collections.newSetFromMap(new ConcurrentHashMap<InCallUiListener, Boolean>(8, 0.9f, 1));
112 private final Set<InCallOrientationListener> mOrientationListeners =
113 Collections.newSetFromMap(
114 new ConcurrentHashMap<InCallOrientationListener, Boolean>(8, 0.9f, 1));
115 private final Set<InCallEventListener> mInCallEventListeners =
116 Collections.newSetFromMap(new ConcurrentHashMap<InCallEventListener, Boolean>(8, 0.9f, 1));
117
118 private StatusBarNotifier mStatusBarNotifier;
119 private ExternalCallNotifier mExternalCallNotifier;
120 private ContactInfoCache mContactInfoCache;
121 private Context mContext;
122 private final OnCheckBlockedListener mOnCheckBlockedListener =
123 new OnCheckBlockedListener() {
124 @Override
125 public void onCheckComplete(final Integer id) {
126 if (id != null && id != FilteredNumberAsyncQueryHandler.INVALID_ID) {
127 // Silence the ringer now to prevent ringing and vibration before the call is
128 // terminated when Telecom attempts to add it.
129 TelecomUtil.silenceRinger(mContext);
130 }
131 }
132 };
133 private CallList mCallList;
134 private ExternalCallList mExternalCallList;
135 private InCallActivity mInCallActivity;
136 private ManageConferenceActivity mManageConferenceActivity;
137 private final android.telecom.Call.Callback mCallCallback =
138 new android.telecom.Call.Callback() {
139 @Override
140 public void onPostDialWait(
141 android.telecom.Call telecomCall, String remainingPostDialSequence) {
142 final DialerCall call = mCallList.getDialerCallFromTelecomCall(telecomCall);
143 if (call == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700144 LogUtil.w(
145 "InCallPresenter.onPostDialWait",
146 "DialerCall not found in call list: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800147 return;
148 }
149 onPostDialCharWait(call.getId(), remainingPostDialSequence);
150 }
151
152 @Override
153 public void onDetailsChanged(
154 android.telecom.Call telecomCall, android.telecom.Call.Details details) {
155 final DialerCall call = mCallList.getDialerCallFromTelecomCall(telecomCall);
156 if (call == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700157 LogUtil.w(
158 "InCallPresenter.onDetailsChanged",
159 "DialerCall not found in call list: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800160 return;
161 }
162
163 if (details.hasProperty(Details.PROPERTY_IS_EXTERNAL_CALL)
164 && !mExternalCallList.isCallTracked(telecomCall)) {
165
166 // A regular call became an external call so swap call lists.
Eric Erfanian2ca43182017-08-31 06:57:16 -0700167 LogUtil.i("InCallPresenter.onDetailsChanged", "Call became external: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800168 mCallList.onInternalCallMadeExternal(mContext, telecomCall);
169 mExternalCallList.onCallAdded(telecomCall);
170 return;
171 }
172
173 for (InCallDetailsListener listener : mDetailsListeners) {
174 listener.onDetailsChanged(call, details);
175 }
176 }
177
178 @Override
179 public void onConferenceableCallsChanged(
180 android.telecom.Call telecomCall, List<android.telecom.Call> conferenceableCalls) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700181 LogUtil.i(
182 "InCallPresenter.onConferenceableCallsChanged",
183 "onConferenceableCallsChanged: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800184 onDetailsChanged(telecomCall, telecomCall.getDetails());
185 }
186 };
187 private InCallState mInCallState = InCallState.NO_CALLS;
188 private ProximitySensor mProximitySensor;
189 private final PseudoScreenState mPseudoScreenState = new PseudoScreenState();
190 private boolean mServiceConnected;
Eric Erfanianccca3152017-02-22 16:32:36 -0800191 private InCallCameraManager mInCallCameraManager;
192 private FilteredNumberAsyncQueryHandler mFilteredQueryHandler;
193 private CallList.Listener mSpamCallListListener;
194 /** Whether or not we are currently bound and waiting for Telecom to send us a new call. */
195 private boolean mBoundAndWaitingForOutgoingCall;
196 /** Determines if the InCall UI is in fullscreen mode or not. */
197 private boolean mIsFullScreen = false;
198
Eric Erfanian2ca43182017-08-31 06:57:16 -0700199 private boolean mScreenTimeoutEnabled = true;
200
Eric Erfanianccca3152017-02-22 16:32:36 -0800201 private PhoneStateListener mPhoneStateListener =
202 new PhoneStateListener() {
203 @Override
204 public void onCallStateChanged(int state, String incomingNumber) {
205 if (state == TelephonyManager.CALL_STATE_RINGING) {
206 if (FilteredNumbersUtil.hasRecentEmergencyCall(mContext)) {
207 return;
208 }
209 // Check if the number is blocked, to silence the ringer.
210 String countryIso = GeoUtil.getCurrentCountryIso(mContext);
211 mFilteredQueryHandler.isBlockedNumber(
212 mOnCheckBlockedListener, incomingNumber, countryIso);
213 }
214 }
215 };
216 /**
217 * Is true when the activity has been previously started. Some code needs to know not just if the
218 * activity is currently up, but if it had been previously shown in foreground for this in-call
219 * session (e.g., StatusBarNotifier). This gets reset when the session ends in the tear-down
220 * method.
221 */
222 private boolean mIsActivityPreviouslyStarted = false;
223
224 /** Whether or not InCallService is bound to Telecom. */
225 private boolean mServiceBound = false;
226
227 /**
228 * When configuration changes Android kills the current activity and starts a new one. The flag is
229 * used to check if full clean up is necessary (activity is stopped and new activity won't be
230 * started), or if a new activity will be started right after the current one is destroyed, and
231 * therefore no need in release all resources.
232 */
233 private boolean mIsChangingConfigurations = false;
234
235 private boolean mAwaitingCallListUpdate = false;
236
237 private ExternalCallList.ExternalCallListener mExternalCallListener =
238 new ExternalCallList.ExternalCallListener() {
239
240 @Override
241 public void onExternalCallPulled(android.telecom.Call call) {
242 // Note: keep this code in sync with InCallPresenter#onCallAdded
243 LatencyReport latencyReport = new LatencyReport(call);
244 latencyReport.onCallBlockingDone();
245 // Note: External calls do not require spam checking.
246 mCallList.onCallAdded(mContext, call, latencyReport);
247 call.registerCallback(mCallCallback);
248 }
249
250 @Override
251 public void onExternalCallAdded(android.telecom.Call call) {
252 // No-op
253 }
254
255 @Override
256 public void onExternalCallRemoved(android.telecom.Call call) {
257 // No-op
258 }
259
260 @Override
261 public void onExternalCallUpdated(android.telecom.Call call) {
262 // No-op
263 }
264 };
265
266 private ThemeColorManager mThemeColorManager;
267 private VideoSurfaceTexture mLocalVideoSurfaceTexture;
268 private VideoSurfaceTexture mRemoteVideoSurfaceTexture;
269
Eric Erfanian10b34a52017-05-04 08:23:17 -0700270 /** Inaccessible constructor. Must use getRunningInstance() to get this singleton. */
Eric Erfanianccca3152017-02-22 16:32:36 -0800271 @VisibleForTesting
272 InCallPresenter() {}
273
274 public static synchronized InCallPresenter getInstance() {
275 if (sInCallPresenter == null) {
276 sInCallPresenter = new InCallPresenter();
277 }
278 return sInCallPresenter;
279 }
280
Eric Erfanian10b34a52017-05-04 08:23:17 -0700281 @VisibleForTesting
282 public static synchronized void setInstanceForTesting(InCallPresenter inCallPresenter) {
283 sInCallPresenter = inCallPresenter;
284 }
285
Eric Erfanianccca3152017-02-22 16:32:36 -0800286 /**
287 * Determines whether or not a call has no valid phone accounts that can be used to make the call
288 * with. Emergency calls do not require a phone account.
289 *
290 * @param call to check accounts for.
291 * @return {@code true} if the call has no call capable phone accounts set, {@code false} if the
292 * call contains a phone account that could be used to initiate it with, or is an emergency
293 * call.
294 */
295 public static boolean isCallWithNoValidAccounts(DialerCall call) {
296 if (call != null && !call.isEmergencyCall()) {
297 Bundle extras = call.getIntentExtras();
298
299 if (extras == null) {
300 extras = EMPTY_EXTRAS;
301 }
302
303 final List<PhoneAccountHandle> phoneAccountHandles =
304 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
305
306 if ((call.getAccountHandle() == null
307 && (phoneAccountHandles == null || phoneAccountHandles.isEmpty()))) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700308 LogUtil.i(
309 "InCallPresenter.isCallWithNoValidAccounts", "No valid accounts for call " + call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800310 return true;
311 }
312 }
313 return false;
314 }
315
316 public InCallState getInCallState() {
317 return mInCallState;
318 }
319
320 public CallList getCallList() {
321 return mCallList;
322 }
323
324 public void setUp(
325 @NonNull Context context,
326 CallList callList,
327 ExternalCallList externalCallList,
328 StatusBarNotifier statusBarNotifier,
329 ExternalCallNotifier externalCallNotifier,
330 ContactInfoCache contactInfoCache,
Eric Erfanian83b20212017-05-31 08:53:10 -0700331 ProximitySensor proximitySensor,
332 FilteredNumberAsyncQueryHandler filteredNumberQueryHandler) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700333 Trace.beginSection("InCallPresenter.setUp");
Eric Erfanianccca3152017-02-22 16:32:36 -0800334 if (mServiceConnected) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700335 LogUtil.i("InCallPresenter.setUp", "New service connection replacing existing one.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800336 if (context != mContext || callList != mCallList) {
337 throw new IllegalStateException();
338 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700339 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800340 return;
341 }
342
343 Objects.requireNonNull(context);
344 mContext = context;
345
346 mContactInfoCache = contactInfoCache;
347
348 mStatusBarNotifier = statusBarNotifier;
349 mExternalCallNotifier = externalCallNotifier;
350 addListener(mStatusBarNotifier);
Eric Erfaniand8046e52017-04-06 09:41:50 -0700351 EnrichedCallComponent.get(mContext)
352 .getEnrichedCallManager()
353 .registerStateChangedListener(mStatusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -0800354
355 mProximitySensor = proximitySensor;
356 addListener(mProximitySensor);
357
358 mThemeColorManager =
359 new ThemeColorManager(new InCallUIMaterialColorMapUtils(mContext.getResources()));
360
361 mCallList = callList;
362 mExternalCallList = externalCallList;
363 externalCallList.addExternalCallListener(mExternalCallNotifier);
364 externalCallList.addExternalCallListener(mExternalCallListener);
365
366 // This only gets called by the service so this is okay.
367 mServiceConnected = true;
368
369 // The final thing we do in this set up is add ourselves as a listener to CallList. This
370 // will kick off an update and the whole process can start.
371 mCallList.addListener(this);
372
373 // Create spam call list listener and add it to the list of listeners
Eric Erfanian2ca43182017-08-31 06:57:16 -0700374 mSpamCallListListener = new SpamCallListListener(context, new DefaultDialerExecutorFactory());
Eric Erfanianccca3152017-02-22 16:32:36 -0800375 mCallList.addListener(mSpamCallListListener);
376
377 VideoPauseController.getInstance().setUp(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800378
Eric Erfanian83b20212017-05-31 08:53:10 -0700379 mFilteredQueryHandler = filteredNumberQueryHandler;
Eric Erfanianccca3152017-02-22 16:32:36 -0800380 mContext
381 .getSystemService(TelephonyManager.class)
382 .listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
383
Eric Erfanian2ca43182017-08-31 06:57:16 -0700384 LogUtil.d("InCallPresenter.setUp", "Finished InCallPresenter.setUp");
385 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800386 }
387
388 /**
389 * Called when the telephony service has disconnected from us. This will happen when there are no
390 * more active calls. However, we may still want to continue showing the UI for certain cases like
391 * showing "Call Ended". What we really want is to wait for the activity and the service to both
392 * disconnect before we tear things down. This method sets a serviceConnected boolean and calls a
393 * secondary method that performs the aforementioned logic.
394 */
395 public void tearDown() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700396 LogUtil.d("InCallPresenter.tearDown", "tearDown");
Eric Erfanianccca3152017-02-22 16:32:36 -0800397 mCallList.clearOnDisconnect();
398
399 mServiceConnected = false;
400
401 mContext
402 .getSystemService(TelephonyManager.class)
403 .listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
404
405 attemptCleanup();
406 VideoPauseController.getInstance().tearDown();
Eric Erfanianccca3152017-02-22 16:32:36 -0800407 }
408
409 private void attemptFinishActivity() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700410 mScreenTimeoutEnabled = true;
Eric Erfanianccca3152017-02-22 16:32:36 -0800411 final boolean doFinish = (mInCallActivity != null && isActivityStarted());
Eric Erfanian2ca43182017-08-31 06:57:16 -0700412 LogUtil.i("InCallPresenter.attemptFinishActivity", "Hide in call UI: " + doFinish);
Eric Erfanianccca3152017-02-22 16:32:36 -0800413 if (doFinish) {
414 mInCallActivity.setExcludeFromRecents(true);
415 mInCallActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -0800416 }
417 }
418
419 /**
420 * Called when the UI ends. Attempts to tear down everything if necessary. See {@link #tearDown()}
421 * for more insight on the tear-down process.
422 */
423 public void unsetActivity(InCallActivity inCallActivity) {
424 if (inCallActivity == null) {
425 throw new IllegalArgumentException("unregisterActivity cannot be called with null");
426 }
427 if (mInCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700428 LogUtil.i(
429 "InCallPresenter.unsetActivity", "No InCallActivity currently set, no need to unset.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800430 return;
431 }
432 if (mInCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700433 LogUtil.w(
434 "InCallPresenter.unsetActivity",
Eric Erfanianccca3152017-02-22 16:32:36 -0800435 "Second instance of InCallActivity is trying to unregister when another"
436 + " instance is active. Ignoring.");
437 return;
438 }
439 updateActivity(null);
440 }
441
442 /**
443 * Updates the current instance of {@link InCallActivity} with the provided one. If a {@code null}
444 * activity is provided, it means that the activity was finished and we should attempt to cleanup.
445 */
446 private void updateActivity(InCallActivity inCallActivity) {
wangqi9982f0d2017-10-11 17:46:07 -0700447 Trace.beginSection("InCallPresenter.updateActivity");
Eric Erfanianccca3152017-02-22 16:32:36 -0800448 boolean updateListeners = false;
449 boolean doAttemptCleanup = false;
450
451 if (inCallActivity != null) {
452 if (mInCallActivity == null) {
453 updateListeners = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700454 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized");
Eric Erfanianccca3152017-02-22 16:32:36 -0800455 } else {
456 // since setActivity is called onStart(), it can be called multiple times.
457 // This is fine and ignorable, but we do not want to update the world every time
458 // this happens (like going to/from background) so we do not set updateListeners.
459 }
460
461 mInCallActivity = inCallActivity;
462 mInCallActivity.setExcludeFromRecents(false);
463
464 // By the time the UI finally comes up, the call may already be disconnected.
465 // If that's the case, we may need to show an error dialog.
466 if (mCallList != null && mCallList.getDisconnectedCall() != null) {
467 maybeShowErrorDialogOnDisconnect(mCallList.getDisconnectedCall());
468 }
469
470 // When the UI comes up, we need to first check the in-call state.
471 // If we are showing NO_CALLS, that means that a call probably connected and
472 // then immediately disconnected before the UI was able to come up.
473 // If we dont have any calls, start tearing down the UI instead.
474 // NOTE: This code relies on {@link #mInCallActivity} being set so we run it after
475 // it has been set.
476 if (mInCallState == InCallState.NO_CALLS) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700477 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized, but no calls left. Shut down");
Eric Erfanianccca3152017-02-22 16:32:36 -0800478 attemptFinishActivity();
wangqi9982f0d2017-10-11 17:46:07 -0700479 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800480 return;
481 }
482 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700483 LogUtil.i("InCallPresenter.updateActivity", "UI Destroyed");
Eric Erfanianccca3152017-02-22 16:32:36 -0800484 updateListeners = true;
485 mInCallActivity = null;
486
487 // We attempt cleanup for the destroy case but only after we recalculate the state
488 // to see if we need to come back up or stay shut down. This is why we do the
489 // cleanup after the call to onCallListChange() instead of directly here.
490 doAttemptCleanup = true;
491 }
492
493 // Messages can come from the telephony layer while the activity is coming up
494 // and while the activity is going down. So in both cases we need to recalculate what
495 // state we should be in after they complete.
496 // Examples: (1) A new incoming call could come in and then get disconnected before
497 // the activity is created.
498 // (2) All calls could disconnect and then get a new incoming call before the
499 // activity is destroyed.
500 //
501 // b/1122139 - We previously had a check for mServiceConnected here as well, but there are
502 // cases where we need to recalculate the current state even if the service in not
503 // connected. In particular the case where startOrFinish() is called while the app is
504 // already finish()ing. In that case, we skip updating the state with the knowledge that
505 // we will check again once the activity has finished. That means we have to recalculate the
506 // state here even if the service is disconnected since we may not have finished a state
507 // transition while finish()ing.
508 if (updateListeners) {
509 onCallListChange(mCallList);
510 }
511
512 if (doAttemptCleanup) {
513 attemptCleanup();
514 }
wangqi9982f0d2017-10-11 17:46:07 -0700515 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800516 }
517
518 public void setManageConferenceActivity(
519 @Nullable ManageConferenceActivity manageConferenceActivity) {
520 mManageConferenceActivity = manageConferenceActivity;
521 }
522
523 public void onBringToForeground(boolean showDialpad) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700524 LogUtil.i("InCallPresenter.onBringToForeground", "Bringing UI to foreground.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800525 bringToForeground(showDialpad);
526 }
527
528 public void onCallAdded(final android.telecom.Call call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700529 Trace.beginSection("InCallPresenter.onCallAdded");
Eric Erfanianccca3152017-02-22 16:32:36 -0800530 LatencyReport latencyReport = new LatencyReport(call);
531 if (shouldAttemptBlocking(call)) {
532 maybeBlockCall(call, latencyReport);
533 } else {
534 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
535 mExternalCallList.onCallAdded(call);
536 } else {
537 latencyReport.onCallBlockingDone();
538 mCallList.onCallAdded(mContext, call, latencyReport);
539 }
540 }
541
542 // Since a call has been added we are no longer waiting for Telecom to send us a call.
543 setBoundAndWaitingForOutgoingCall(false, null);
544 call.registerCallback(mCallCallback);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700545 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800546 }
547
548 private boolean shouldAttemptBlocking(android.telecom.Call call) {
549 if (call.getState() != android.telecom.Call.STATE_RINGING) {
550 return false;
551 }
Eric Erfanian83b20212017-05-31 08:53:10 -0700552 if (!UserManagerCompat.isUserUnlocked(mContext)) {
553 LogUtil.i(
554 "InCallPresenter.shouldAttemptBlocking",
555 "not attempting to block incoming call because user is locked");
556 return false;
557 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800558 if (TelecomCallUtil.isEmergencyCall(call)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700559 LogUtil.i(
560 "InCallPresenter.shouldAttemptBlocking",
561 "Not attempting to block incoming emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800562 return false;
563 }
564 if (FilteredNumbersUtil.hasRecentEmergencyCall(mContext)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700565 LogUtil.i(
566 "InCallPresenter.shouldAttemptBlocking",
567 "Not attempting to block incoming call due to recent emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800568 return false;
569 }
570 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
571 return false;
572 }
Eric Erfanian83b20212017-05-31 08:53:10 -0700573 if (FilteredNumberCompat.useNewFiltering(mContext)) {
574 LogUtil.i(
575 "InCallPresenter.shouldAttemptBlocking",
576 "not attempting to block incoming call because framework blocking is in use");
577 return false;
578 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800579 return true;
580 }
581
582 /**
583 * Checks whether a call should be blocked, and blocks it if so. Otherwise, it adds the call to
584 * the CallList so it can proceed as normal. There is a timeout, so if the function for checking
585 * whether a function is blocked does not return in a reasonable time, we proceed with adding the
586 * call anyways.
587 */
588 private void maybeBlockCall(final android.telecom.Call call, final LatencyReport latencyReport) {
589 final String countryIso = GeoUtil.getCurrentCountryIso(mContext);
590 final String number = TelecomCallUtil.getNumber(call);
591 final long timeAdded = System.currentTimeMillis();
592
593 // Though AtomicBoolean's can be scary, don't fear, as in this case it is only used on the
594 // main UI thread. It is needed so we can change its value within different scopes, since
595 // that cannot be done with a final boolean.
596 final AtomicBoolean hasTimedOut = new AtomicBoolean(false);
597
598 final Handler handler = new Handler();
599
600 // Proceed if the query is slow; the call may still be blocked after the query returns.
601 final Runnable runnable =
602 new Runnable() {
603 @Override
604 public void run() {
605 hasTimedOut.set(true);
606 latencyReport.onCallBlockingDone();
607 mCallList.onCallAdded(mContext, call, latencyReport);
608 }
609 };
610 handler.postDelayed(runnable, BLOCK_QUERY_TIMEOUT_MS);
611
612 OnCheckBlockedListener onCheckBlockedListener =
613 new OnCheckBlockedListener() {
614 @Override
615 public void onCheckComplete(final Integer id) {
616 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700617 LogUtil.i("InCallPresenter.onCheckComplete", "torn down, not adding call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800618 return;
619 }
620 if (!hasTimedOut.get()) {
621 handler.removeCallbacks(runnable);
622 }
623 if (id == null) {
624 if (!hasTimedOut.get()) {
625 latencyReport.onCallBlockingDone();
626 mCallList.onCallAdded(mContext, call, latencyReport);
627 }
628 } else if (id == FilteredNumberAsyncQueryHandler.INVALID_ID) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700629 LogUtil.d(
630 "InCallPresenter.onCheckComplete", "invalid number, skipping block checking");
Eric Erfanianccca3152017-02-22 16:32:36 -0800631 if (!hasTimedOut.get()) {
632 handler.removeCallbacks(runnable);
633
634 latencyReport.onCallBlockingDone();
635 mCallList.onCallAdded(mContext, call, latencyReport);
636 }
637 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700638 LogUtil.i(
639 "InCallPresenter.onCheckComplete", "Rejecting incoming call from blocked number");
Eric Erfanianccca3152017-02-22 16:32:36 -0800640 call.reject(false, null);
641 Logger.get(mContext).logInteraction(InteractionEvent.Type.CALL_BLOCKED);
642
643 /*
644 * If mContext is null, then the InCallPresenter was torn down before the
645 * block check had a chance to complete. The context is no longer valid, so
646 * don't attempt to remove the call log entry.
647 */
648 if (mContext == null) {
649 return;
650 }
651 // Register observer to update the call log.
652 // BlockedNumberContentObserver will unregister after successful log or timeout.
653 BlockedNumberContentObserver contentObserver =
654 new BlockedNumberContentObserver(mContext, new Handler(), number, timeAdded);
655 contentObserver.register();
656 }
657 }
658 };
659
660 mFilteredQueryHandler.isBlockedNumber(onCheckBlockedListener, number, countryIso);
661 }
662
663 public void onCallRemoved(android.telecom.Call call) {
664 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
665 mExternalCallList.onCallRemoved(call);
666 } else {
667 mCallList.onCallRemoved(mContext, call);
668 call.unregisterCallback(mCallCallback);
669 }
670 }
671
672 public void onCanAddCallChanged(boolean canAddCall) {
673 for (CanAddCallListener listener : mCanAddCallListeners) {
674 listener.onCanAddCallChanged(canAddCall);
675 }
676 }
677
678 @Override
679 public void onWiFiToLteHandover(DialerCall call) {
680 if (mInCallActivity != null) {
681 mInCallActivity.onWiFiToLteHandover(call);
682 }
683 }
684
685 @Override
686 public void onHandoverToWifiFailed(DialerCall call) {
687 if (mInCallActivity != null) {
688 mInCallActivity.onHandoverToWifiFailed(call);
689 }
690 }
691
Eric Erfanianc857f902017-05-15 14:05:33 -0700692 @Override
693 public void onInternationalCallOnWifi(@NonNull DialerCall call) {
694 LogUtil.enterBlock("InCallPresenter.onInternationalCallOnWifi");
695 if (mInCallActivity != null) {
696 mInCallActivity.onInternationalCallOnWifi(call);
697 }
698 }
699
Eric Erfanianccca3152017-02-22 16:32:36 -0800700 /**
701 * Called when there is a change to the call list. Sets the In-Call state for the entire in-call
702 * app based on the information it gets from CallList. Dispatches the in-call state to all
703 * listeners. Can trigger the creation or destruction of the UI based on the states that is
704 * calculates.
705 */
706 @Override
707 public void onCallListChange(CallList callList) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700708 Trace.beginSection("InCallPresenter.onCallListChange");
Eric Erfanianccca3152017-02-22 16:32:36 -0800709 if (mInCallActivity != null && mInCallActivity.isInCallScreenAnimating()) {
710 mAwaitingCallListUpdate = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700711 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800712 return;
713 }
714 if (callList == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700715 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800716 return;
717 }
718
719 mAwaitingCallListUpdate = false;
720
721 InCallState newState = getPotentialStateFromCallList(callList);
722 InCallState oldState = mInCallState;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700723 LogUtil.d(
724 "InCallPresenter.onCallListChange",
725 "onCallListChange oldState= " + oldState + " newState=" + newState);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700726
727 // If the user placed a call and was asked to choose the account, but then pressed "Home", the
728 // incall activity for that call will still exist (even if it's not visible). In the case of
729 // an incoming call in that situation, just disconnect that "waiting for account" call and
730 // dismiss the dialog. The same activity will be reused to handle the new incoming call. See
731 // b/33247755 for more details.
732 DialerCall waitingForAccountCall;
733 if (newState == InCallState.INCOMING
734 && (waitingForAccountCall = callList.getWaitingForAccountCall()) != null) {
735 waitingForAccountCall.disconnect();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700736 // The InCallActivity might be destroyed or not started yet at this point.
737 if (isActivityStarted()) {
738 mInCallActivity.dismissPendingDialogs();
739 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700740 }
741
Eric Erfanianccca3152017-02-22 16:32:36 -0800742 newState = startOrFinishUi(newState);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700743 LogUtil.d(
744 "InCallPresenter.onCallListChange", "onCallListChange newState changed to " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800745
746 // Set the new state before announcing it to the world
Eric Erfanian2ca43182017-08-31 06:57:16 -0700747 LogUtil.i(
748 "InCallPresenter.onCallListChange",
749 "Phone switching state: " + oldState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800750 mInCallState = newState;
751
752 // notify listeners of new state
753 for (InCallStateListener listener : mListeners) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700754 LogUtil.d(
755 "InCallPresenter.onCallListChange",
756 "Notify " + listener + " of state " + mInCallState.toString());
Eric Erfanianccca3152017-02-22 16:32:36 -0800757 listener.onStateChange(oldState, mInCallState, callList);
758 }
759
760 if (isActivityStarted()) {
761 final boolean hasCall =
762 callList.getActiveOrBackgroundCall() != null || callList.getOutgoingCall() != null;
763 mInCallActivity.dismissKeyguard(hasCall);
764 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700765 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800766 }
767
768 /** Called when there is a new incoming call. */
769 @Override
770 public void onIncomingCall(DialerCall call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700771 Trace.beginSection("InCallPresenter.onIncomingCall");
Eric Erfanianccca3152017-02-22 16:32:36 -0800772 InCallState newState = startOrFinishUi(InCallState.INCOMING);
773 InCallState oldState = mInCallState;
774
Eric Erfanian2ca43182017-08-31 06:57:16 -0700775 LogUtil.i(
776 "InCallPresenter.onIncomingCall", "Phone switching state: " + oldState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800777 mInCallState = newState;
778
wangqicf61ca02017-08-31 15:32:55 -0700779 Trace.beginSection("listener.onIncomingCall");
Eric Erfanianccca3152017-02-22 16:32:36 -0800780 for (IncomingCallListener listener : mIncomingCallListeners) {
781 listener.onIncomingCall(oldState, mInCallState, call);
782 }
wangqicf61ca02017-08-31 15:32:55 -0700783 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800784
wangqicf61ca02017-08-31 15:32:55 -0700785 Trace.beginSection("onPrimaryCallStateChanged");
Eric Erfanianccca3152017-02-22 16:32:36 -0800786 if (mInCallActivity != null) {
787 // Re-evaluate which fragment is being shown.
788 mInCallActivity.onPrimaryCallStateChanged();
789 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700790 Trace.endSection();
wangqicf61ca02017-08-31 15:32:55 -0700791 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800792 }
793
794 @Override
795 public void onUpgradeToVideo(DialerCall call) {
Eric Erfanian90508232017-03-24 09:31:16 -0700796 if (VideoUtils.hasReceivedVideoUpgradeRequest(call.getVideoTech().getSessionModificationState())
Eric Erfanianccca3152017-02-22 16:32:36 -0800797 && mInCallState == InCallPresenter.InCallState.INCOMING) {
798 LogUtil.i(
799 "InCallPresenter.onUpgradeToVideo",
800 "rejecting upgrade request due to existing incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700801 call.getVideoTech().declineVideoRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -0800802 }
803
804 if (mInCallActivity != null) {
805 // Re-evaluate which fragment is being shown.
806 mInCallActivity.onPrimaryCallStateChanged();
807 }
808 }
809
810 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700811 public void onSessionModificationStateChange(DialerCall call) {
812 int newState = call.getVideoTech().getSessionModificationState();
Eric Erfanianccca3152017-02-22 16:32:36 -0800813 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "state: %d", newState);
814 if (mProximitySensor == null) {
815 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "proximitySensor is null");
816 return;
817 }
818 mProximitySensor.setIsAttemptingVideoCall(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700819 call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest());
Eric Erfanianccca3152017-02-22 16:32:36 -0800820 if (mInCallActivity != null) {
821 // Re-evaluate which fragment is being shown.
822 mInCallActivity.onPrimaryCallStateChanged();
823 }
824 }
825
826 /**
827 * Called when a call becomes disconnected. Called everytime an existing call changes from being
828 * connected (incoming/outgoing/active) to disconnected.
829 */
830 @Override
831 public void onDisconnect(DialerCall call) {
832 maybeShowErrorDialogOnDisconnect(call);
833
834 // We need to do the run the same code as onCallListChange.
835 onCallListChange(mCallList);
836
837 if (isActivityStarted()) {
838 mInCallActivity.dismissKeyguard(false);
839 }
840
841 if (call.isEmergencyCall()) {
842 FilteredNumbersUtil.recordLastEmergencyCallTime(mContext);
843 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800844
Eric Erfanianfc37b022017-03-21 10:11:17 -0700845 if (!mCallList.hasLiveCall()
846 && !call.getLogState().isIncoming
Eric Erfanian10b34a52017-05-04 08:23:17 -0700847 && !isSecretCode(call.getNumber())
wangqi9982f0d2017-10-11 17:46:07 -0700848 && !call.isVoiceMailNumber()) {
Eric Erfanian10b34a52017-05-04 08:23:17 -0700849 PostCall.onCallDisconnected(mContext, call.getNumber(), call.getConnectTimeMillis());
Eric Erfanianccca3152017-02-22 16:32:36 -0800850 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800851 }
852
Eric Erfanian10b34a52017-05-04 08:23:17 -0700853 private boolean isSecretCode(@Nullable String number) {
854 return number != null
855 && (number.length() <= 8 || number.startsWith("*#*#") || number.endsWith("#*#*"));
856 }
857
Eric Erfanianccca3152017-02-22 16:32:36 -0800858 /** Given the call list, return the state in which the in-call screen should be. */
859 public InCallState getPotentialStateFromCallList(CallList callList) {
860
861 InCallState newState = InCallState.NO_CALLS;
862
863 if (callList == null) {
864 return newState;
865 }
866 if (callList.getIncomingCall() != null) {
867 newState = InCallState.INCOMING;
868 } else if (callList.getWaitingForAccountCall() != null) {
869 newState = InCallState.WAITING_FOR_ACCOUNT;
870 } else if (callList.getPendingOutgoingCall() != null) {
871 newState = InCallState.PENDING_OUTGOING;
872 } else if (callList.getOutgoingCall() != null) {
873 newState = InCallState.OUTGOING;
874 } else if (callList.getActiveCall() != null
875 || callList.getBackgroundCall() != null
876 || callList.getDisconnectedCall() != null
877 || callList.getDisconnectingCall() != null) {
878 newState = InCallState.INCALL;
879 }
880
881 if (newState == InCallState.NO_CALLS) {
882 if (mBoundAndWaitingForOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700883 return InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -0800884 }
885 }
886
887 return newState;
888 }
889
890 public boolean isBoundAndWaitingForOutgoingCall() {
891 return mBoundAndWaitingForOutgoingCall;
892 }
893
894 public void setBoundAndWaitingForOutgoingCall(boolean isBound, PhoneAccountHandle handle) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700895 LogUtil.i(
896 "InCallPresenter.setBoundAndWaitingForOutgoingCall",
897 "setBoundAndWaitingForOutgoingCall: " + isBound);
Eric Erfanianccca3152017-02-22 16:32:36 -0800898 mBoundAndWaitingForOutgoingCall = isBound;
899 mThemeColorManager.setPendingPhoneAccountHandle(handle);
900 if (isBound && mInCallState == InCallState.NO_CALLS) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700901 mInCallState = InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -0800902 }
903 }
904
905 public void onShrinkAnimationComplete() {
906 if (mAwaitingCallListUpdate) {
907 onCallListChange(mCallList);
908 }
909 }
910
911 public void addIncomingCallListener(IncomingCallListener listener) {
912 Objects.requireNonNull(listener);
913 mIncomingCallListeners.add(listener);
914 }
915
916 public void removeIncomingCallListener(IncomingCallListener listener) {
917 if (listener != null) {
918 mIncomingCallListeners.remove(listener);
919 }
920 }
921
922 public void addListener(InCallStateListener listener) {
923 Objects.requireNonNull(listener);
924 mListeners.add(listener);
925 }
926
927 public void removeListener(InCallStateListener listener) {
928 if (listener != null) {
929 mListeners.remove(listener);
930 }
931 }
932
933 public void addDetailsListener(InCallDetailsListener listener) {
934 Objects.requireNonNull(listener);
935 mDetailsListeners.add(listener);
936 }
937
938 public void removeDetailsListener(InCallDetailsListener listener) {
939 if (listener != null) {
940 mDetailsListeners.remove(listener);
941 }
942 }
943
944 public void addCanAddCallListener(CanAddCallListener listener) {
945 Objects.requireNonNull(listener);
946 mCanAddCallListeners.add(listener);
947 }
948
949 public void removeCanAddCallListener(CanAddCallListener listener) {
950 if (listener != null) {
951 mCanAddCallListeners.remove(listener);
952 }
953 }
954
955 public void addOrientationListener(InCallOrientationListener listener) {
956 Objects.requireNonNull(listener);
957 mOrientationListeners.add(listener);
958 }
959
960 public void removeOrientationListener(InCallOrientationListener listener) {
961 if (listener != null) {
962 mOrientationListeners.remove(listener);
963 }
964 }
965
966 public void addInCallEventListener(InCallEventListener listener) {
967 Objects.requireNonNull(listener);
968 mInCallEventListeners.add(listener);
969 }
970
971 public void removeInCallEventListener(InCallEventListener listener) {
972 if (listener != null) {
973 mInCallEventListeners.remove(listener);
974 }
975 }
976
977 public ProximitySensor getProximitySensor() {
978 return mProximitySensor;
979 }
980
981 public PseudoScreenState getPseudoScreenState() {
982 return mPseudoScreenState;
983 }
984
985 /** Returns true if the incall app is the foreground application. */
986 public boolean isShowingInCallUi() {
987 if (!isActivityStarted()) {
988 return false;
989 }
990 if (mManageConferenceActivity != null && mManageConferenceActivity.isVisible()) {
991 return true;
992 }
993 return mInCallActivity.isVisible();
994 }
995
996 /**
997 * Returns true if the activity has been created and is running. Returns true as long as activity
998 * is not destroyed or finishing. This ensures that we return true even if the activity is paused
999 * (not in foreground).
1000 */
1001 public boolean isActivityStarted() {
1002 return (mInCallActivity != null
1003 && !mInCallActivity.isDestroyed()
1004 && !mInCallActivity.isFinishing());
1005 }
1006
1007 /**
1008 * Determines if the In-Call app is currently changing configuration.
1009 *
1010 * @return {@code true} if the In-Call app is changing configuration.
1011 */
1012 public boolean isChangingConfigurations() {
1013 return mIsChangingConfigurations;
1014 }
1015
1016 /**
1017 * Tracks whether the In-Call app is currently in the process of changing configuration (i.e.
1018 * screen orientation).
1019 */
1020 /*package*/
1021 void updateIsChangingConfigurations() {
1022 mIsChangingConfigurations = false;
1023 if (mInCallActivity != null) {
1024 mIsChangingConfigurations = mInCallActivity.isChangingConfigurations();
1025 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001026 LogUtil.v(
1027 "InCallPresenter.updateIsChangingConfigurations",
1028 "updateIsChangingConfigurations = " + mIsChangingConfigurations);
Eric Erfanianccca3152017-02-22 16:32:36 -08001029 }
1030
1031 /** Called when the activity goes in/out of the foreground. */
1032 public void onUiShowing(boolean showing) {
1033 // We need to update the notification bar when we leave the UI because that
1034 // could trigger it to show again.
1035 if (mStatusBarNotifier != null) {
1036 mStatusBarNotifier.updateNotification(mCallList);
1037 }
1038
1039 if (mProximitySensor != null) {
1040 mProximitySensor.onInCallShowing(showing);
1041 }
1042
1043 Intent broadcastIntent = Bindings.get(mContext).getUiReadyBroadcastIntent(mContext);
1044 if (broadcastIntent != null) {
1045 broadcastIntent.putExtra(EXTRA_FIRST_TIME_SHOWN, !mIsActivityPreviouslyStarted);
1046
1047 if (showing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001048 LogUtil.d("InCallPresenter.onUiShowing", "Sending sticky broadcast: ", broadcastIntent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001049 mContext.sendStickyBroadcast(broadcastIntent);
1050 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001051 LogUtil.d("InCallPresenter.onUiShowing", "Removing sticky broadcast: ", broadcastIntent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001052 mContext.removeStickyBroadcast(broadcastIntent);
1053 }
1054 }
1055
1056 if (showing) {
1057 mIsActivityPreviouslyStarted = true;
1058 } else {
1059 updateIsChangingConfigurations();
1060 }
1061
1062 for (InCallUiListener listener : mInCallUiListeners) {
1063 listener.onUiShowing(showing);
1064 }
1065
1066 if (mInCallActivity != null) {
1067 // Re-evaluate which fragment is being shown.
1068 mInCallActivity.onPrimaryCallStateChanged();
1069 }
1070 }
1071
Eric Erfanian2ca43182017-08-31 06:57:16 -07001072 public void refreshUi() {
1073 if (mInCallActivity != null) {
1074 // Re-evaluate which fragment is being shown.
1075 mInCallActivity.onPrimaryCallStateChanged();
1076 }
1077 }
1078
Eric Erfanianccca3152017-02-22 16:32:36 -08001079 public void addInCallUiListener(InCallUiListener listener) {
1080 mInCallUiListeners.add(listener);
1081 }
1082
1083 public boolean removeInCallUiListener(InCallUiListener listener) {
1084 return mInCallUiListeners.remove(listener);
1085 }
1086
1087 /*package*/
1088 void onActivityStarted() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001089 LogUtil.d("InCallPresenter.onActivityStarted", "onActivityStarted");
Eric Erfanianccca3152017-02-22 16:32:36 -08001090 notifyVideoPauseController(true);
Eric Erfaniand8046e52017-04-06 09:41:50 -07001091 if (mStatusBarNotifier != null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001092 // TODO(maxwelb) - b/36649622: Investigate this redundant call
Eric Erfaniand8046e52017-04-06 09:41:50 -07001093 mStatusBarNotifier.updateNotification(mCallList);
1094 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001095 applyScreenTimeout();
Eric Erfanianccca3152017-02-22 16:32:36 -08001096 }
1097
1098 /*package*/
1099 void onActivityStopped() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001100 LogUtil.d("InCallPresenter.onActivityStopped", "onActivityStopped");
Eric Erfanianccca3152017-02-22 16:32:36 -08001101 notifyVideoPauseController(false);
1102 }
1103
1104 private void notifyVideoPauseController(boolean showing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001105 LogUtil.d(
1106 "InCallPresenter.notifyVideoPauseController",
1107 "mIsChangingConfigurations=" + mIsChangingConfigurations);
Eric Erfanianccca3152017-02-22 16:32:36 -08001108 if (!mIsChangingConfigurations) {
1109 VideoPauseController.getInstance().onUiShowing(showing);
1110 }
1111 }
1112
1113 /** Brings the app into the foreground if possible. */
1114 public void bringToForeground(boolean showDialpad) {
1115 // Before we bring the incall UI to the foreground, we check to see if:
1116 // 1. It is not currently in the foreground
1117 // 2. We are in a state where we want to show the incall ui (i.e. there are calls to
1118 // be displayed)
1119 // If the activity hadn't actually been started previously, yet there are still calls
1120 // present (e.g. a call was accepted by a bluetooth or wired headset), we want to
1121 // bring it up the UI regardless.
1122 if (!isShowingInCallUi() && mInCallState != InCallState.NO_CALLS) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001123 showInCall(showDialpad, false /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001124 }
1125 }
1126
1127 public void onPostDialCharWait(String callId, String chars) {
1128 if (isActivityStarted()) {
1129 mInCallActivity.showPostCharWaitDialog(callId, chars);
1130 }
1131 }
1132
1133 /**
1134 * Handles the green CALL key while in-call.
1135 *
1136 * @return true if we consumed the event.
1137 */
1138 public boolean handleCallKey() {
1139 LogUtil.v("InCallPresenter.handleCallKey", null);
1140
1141 // The green CALL button means either "Answer", "Unhold", or
1142 // "Swap calls", or can be a no-op, depending on the current state
1143 // of the Phone.
1144
1145 /** INCOMING CALL */
1146 final CallList calls = mCallList;
1147 final DialerCall incomingCall = calls.getIncomingCall();
1148 LogUtil.v("InCallPresenter.handleCallKey", "incomingCall: " + incomingCall);
1149
1150 // (1) Attempt to answer a call
1151 if (incomingCall != null) {
1152 incomingCall.answer(VideoProfile.STATE_AUDIO_ONLY);
1153 return true;
1154 }
1155
1156 /** STATE_ACTIVE CALL */
1157 final DialerCall activeCall = calls.getActiveCall();
1158 if (activeCall != null) {
1159 // TODO: This logic is repeated from CallButtonPresenter.java. We should
1160 // consolidate this logic.
1161 final boolean canMerge =
1162 activeCall.can(android.telecom.Call.Details.CAPABILITY_MERGE_CONFERENCE);
1163 final boolean canSwap =
1164 activeCall.can(android.telecom.Call.Details.CAPABILITY_SWAP_CONFERENCE);
1165
Eric Erfanian2ca43182017-08-31 06:57:16 -07001166 LogUtil.v(
1167 "InCallPresenter.handleCallKey",
1168 "activeCall: " + activeCall + ", canMerge: " + canMerge + ", canSwap: " + canSwap);
Eric Erfanianccca3152017-02-22 16:32:36 -08001169
1170 // (2) Attempt actions on conference calls
1171 if (canMerge) {
1172 TelecomAdapter.getInstance().merge(activeCall.getId());
1173 return true;
1174 } else if (canSwap) {
1175 TelecomAdapter.getInstance().swap(activeCall.getId());
1176 return true;
1177 }
1178 }
1179
1180 /** BACKGROUND CALL */
1181 final DialerCall heldCall = calls.getBackgroundCall();
1182 if (heldCall != null) {
1183 // We have a hold call so presumeable it will always support HOLD...but
1184 // there is no harm in double checking.
1185 final boolean canHold = heldCall.can(android.telecom.Call.Details.CAPABILITY_HOLD);
1186
Eric Erfanian2ca43182017-08-31 06:57:16 -07001187 LogUtil.v("InCallPresenter.handleCallKey", "heldCall: " + heldCall + ", canHold: " + canHold);
Eric Erfanianccca3152017-02-22 16:32:36 -08001188
1189 // (4) unhold call
1190 if (heldCall.getState() == DialerCall.State.ONHOLD && canHold) {
1191 heldCall.unhold();
1192 return true;
1193 }
1194 }
1195
1196 // Always consume hard keys
1197 return true;
1198 }
1199
Eric Erfanianccca3152017-02-22 16:32:36 -08001200 /** Clears the previous fullscreen state. */
1201 public void clearFullscreen() {
1202 mIsFullScreen = false;
1203 }
1204
1205 /**
1206 * Changes the fullscreen mode of the in-call UI.
1207 *
1208 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1209 * otherwise.
1210 */
1211 public void setFullScreen(boolean isFullScreen) {
1212 setFullScreen(isFullScreen, false /* force */);
1213 }
1214
1215 /**
1216 * Changes the fullscreen mode of the in-call UI.
1217 *
1218 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1219 * otherwise.
1220 * @param force {@code true} if fullscreen mode should be set regardless of its current state.
1221 */
1222 public void setFullScreen(boolean isFullScreen, boolean force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001223 LogUtil.i("InCallPresenter.setFullScreen", "setFullScreen = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001224
1225 // As a safeguard, ensure we cannot enter fullscreen if the dialpad is shown.
1226 if (isDialpadVisible()) {
1227 isFullScreen = false;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001228 LogUtil.v(
1229 "InCallPresenter.setFullScreen",
1230 "setFullScreen overridden as dialpad is shown = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001231 }
1232
1233 if (mIsFullScreen == isFullScreen && !force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001234 LogUtil.v("InCallPresenter.setFullScreen", "setFullScreen ignored as already in that state.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001235 return;
1236 }
1237 mIsFullScreen = isFullScreen;
1238 notifyFullscreenModeChange(mIsFullScreen);
1239 }
1240
1241 /**
1242 * @return {@code true} if the in-call ui is currently in fullscreen mode, {@code false}
1243 * otherwise.
1244 */
1245 public boolean isFullscreen() {
1246 return mIsFullScreen;
1247 }
1248
1249 /**
1250 * Called by the {@link VideoCallPresenter} to inform of a change in full screen video status.
1251 *
1252 * @param isFullscreenMode {@code True} if entering full screen mode.
1253 */
1254 public void notifyFullscreenModeChange(boolean isFullscreenMode) {
1255 for (InCallEventListener listener : mInCallEventListeners) {
1256 listener.onFullscreenModeChanged(isFullscreenMode);
1257 }
1258 }
1259
1260 /**
1261 * For some disconnected causes, we show a dialog. This calls into the activity to show the dialog
1262 * if appropriate for the call.
1263 */
1264 private void maybeShowErrorDialogOnDisconnect(DialerCall call) {
1265 // For newly disconnected calls, we may want to show a dialog on specific error conditions
1266 if (isActivityStarted() && call.getState() == DialerCall.State.DISCONNECTED) {
1267 if (call.getAccountHandle() == null && !call.isConferenceCall()) {
1268 setDisconnectCauseForMissingAccounts(call);
1269 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001270 mInCallActivity.maybeShowErrorDialogOnDisconnect(
1271 new DisconnectMessage(mInCallActivity, call));
Eric Erfanianccca3152017-02-22 16:32:36 -08001272 }
1273 }
1274
1275 /**
1276 * When the state of in-call changes, this is the first method to get called. It determines if the
1277 * UI needs to be started or finished depending on the new state and does it.
1278 */
1279 private InCallState startOrFinishUi(InCallState newState) {
wangqicf61ca02017-08-31 15:32:55 -07001280 Trace.beginSection("InCallPresenter.startOrFinishUi");
Eric Erfanian2ca43182017-08-31 06:57:16 -07001281 LogUtil.d(
1282 "InCallPresenter.startOrFinishUi", "startOrFinishUi: " + mInCallState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -08001283
1284 // TODO: Consider a proper state machine implementation
1285
1286 // If the state isn't changing we have already done any starting/stopping of activities in
1287 // a previous pass...so lets cut out early
1288 if (newState == mInCallState) {
wangqicf61ca02017-08-31 15:32:55 -07001289 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001290 return newState;
1291 }
1292
1293 // A new Incoming call means that the user needs to be notified of the the call (since
1294 // it wasn't them who initiated it). We do this through full screen notifications and
1295 // happens indirectly through {@link StatusBarNotifier}.
1296 //
1297 // The process for incoming calls is as follows:
1298 //
1299 // 1) CallList - Announces existence of new INCOMING call
1300 // 2) InCallPresenter - Gets announcement and calculates that the new InCallState
1301 // - should be set to INCOMING.
1302 // 3) InCallPresenter - This method is called to see if we need to start or finish
1303 // the app given the new state.
1304 // 4) StatusBarNotifier - Listens to InCallState changes. InCallPresenter calls
1305 // StatusBarNotifier explicitly to issue a FullScreen Notification
1306 // that will either start the InCallActivity or show the user a
1307 // top-level notification dialog if the user is in an immersive app.
1308 // That notification can also start the InCallActivity.
1309 // 5) InCallActivity - Main activity starts up and at the end of its onCreate will
1310 // call InCallPresenter::setActivity() to let the presenter
1311 // know that start-up is complete.
1312 //
1313 // [ AND NOW YOU'RE IN THE CALL. voila! ]
1314 //
1315 // Our app is started using a fullScreen notification. We need to do this whenever
1316 // we get an incoming call. Depending on the current context of the device, either a
1317 // incoming call HUN or the actual InCallActivity will be shown.
1318 final boolean startIncomingCallSequence = (InCallState.INCOMING == newState);
1319
1320 // A dialog to show on top of the InCallUI to select a PhoneAccount
1321 final boolean showAccountPicker = (InCallState.WAITING_FOR_ACCOUNT == newState);
1322
1323 // A new outgoing call indicates that the user just now dialed a number and when that
1324 // happens we need to display the screen immediately or show an account picker dialog if
1325 // no default is set. However, if the main InCallUI is already visible, we do not want to
1326 // re-initiate the start-up animation, so we do not need to do anything here.
1327 //
1328 // It is also possible to go into an intermediate state where the call has been initiated
1329 // but Telecom has not yet returned with the details of the call (handle, gateway, etc.).
1330 // This pending outgoing state can also launch the call screen.
1331 //
1332 // This is different from the incoming call sequence because we do not need to shock the
1333 // user with a top-level notification. Just show the call UI normally.
1334 boolean callCardFragmentVisible =
1335 mInCallActivity != null && mInCallActivity.getCallCardFragmentVisible();
1336 final boolean mainUiNotVisible = !isShowingInCallUi() || !callCardFragmentVisible;
1337 boolean showCallUi = InCallState.OUTGOING == newState && mainUiNotVisible;
1338
1339 // Direct transition from PENDING_OUTGOING -> INCALL means that there was an error in the
1340 // outgoing call process, so the UI should be brought up to show an error dialog.
1341 showCallUi |=
1342 (InCallState.PENDING_OUTGOING == mInCallState
1343 && InCallState.INCALL == newState
1344 && !isShowingInCallUi());
1345
1346 // Another exception - InCallActivity is in charge of disconnecting a call with no
1347 // valid accounts set. Bring the UI up if this is true for the current pending outgoing
1348 // call so that:
1349 // 1) The call can be disconnected correctly
1350 // 2) The UI comes up and correctly displays the error dialog.
1351 // TODO: Remove these special case conditions by making InCallPresenter a true state
1352 // machine. Telecom should also be the component responsible for disconnecting a call
1353 // with no valid accounts.
1354 showCallUi |=
1355 InCallState.PENDING_OUTGOING == newState
1356 && mainUiNotVisible
1357 && isCallWithNoValidAccounts(mCallList.getPendingOutgoingCall());
1358
1359 // The only time that we have an instance of mInCallActivity and it isn't started is
1360 // when it is being destroyed. In that case, lets avoid bringing up another instance of
1361 // the activity. When it is finally destroyed, we double check if we should bring it back
1362 // up so we aren't going to lose anything by avoiding a second startup here.
1363 boolean activityIsFinishing = mInCallActivity != null && !isActivityStarted();
1364 if (activityIsFinishing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001365 LogUtil.i(
1366 "InCallPresenter.startOrFinishUi",
1367 "Undo the state change: " + newState + " -> " + mInCallState);
wangqicf61ca02017-08-31 15:32:55 -07001368 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001369 return mInCallState;
1370 }
1371
1372 // We're about the bring up the in-call UI for outgoing and incoming call. If we still have
1373 // dialogs up, we need to clear them out before showing in-call screen. This is necessary
1374 // to fix the bug that dialog will show up when data reaches limit even after makeing new
1375 // outgoing call after user ignore it by pressing home button.
1376 if ((newState == InCallState.INCOMING || newState == InCallState.PENDING_OUTGOING)
1377 && !showCallUi
1378 && isActivityStarted()) {
1379 mInCallActivity.dismissPendingDialogs();
1380 }
1381
1382 if (showCallUi || showAccountPicker) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001383 LogUtil.i("InCallPresenter.startOrFinishUi", "Start in call UI");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001384 showInCall(false /* showDialpad */, !showAccountPicker /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001385 } else if (startIncomingCallSequence) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001386 LogUtil.i("InCallPresenter.startOrFinishUi", "Start Full Screen in call UI");
Eric Erfanianccca3152017-02-22 16:32:36 -08001387
Eric Erfaniand8046e52017-04-06 09:41:50 -07001388 mStatusBarNotifier.updateNotification(mCallList);
Eric Erfanianccca3152017-02-22 16:32:36 -08001389 } else if (newState == InCallState.NO_CALLS) {
1390 // The new state is the no calls state. Tear everything down.
1391 attemptFinishActivity();
1392 attemptCleanup();
1393 }
1394
wangqicf61ca02017-08-31 15:32:55 -07001395 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001396 return newState;
1397 }
1398
1399 /**
1400 * Sets the DisconnectCause for a call that was disconnected because it was missing a PhoneAccount
1401 * or PhoneAccounts to select from.
1402 */
1403 private void setDisconnectCauseForMissingAccounts(DialerCall call) {
1404
1405 Bundle extras = call.getIntentExtras();
1406 // Initialize the extras bundle to avoid NPE
1407 if (extras == null) {
1408 extras = new Bundle();
1409 }
1410
1411 final List<PhoneAccountHandle> phoneAccountHandles =
1412 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
1413
1414 if (phoneAccountHandles == null || phoneAccountHandles.isEmpty()) {
1415 String scheme = call.getHandle().getScheme();
1416 final String errorMsg =
1417 PhoneAccount.SCHEME_TEL.equals(scheme)
1418 ? mContext.getString(R.string.callFailed_simError)
1419 : mContext.getString(R.string.incall_error_supp_service_unknown);
1420 DisconnectCause disconnectCause =
1421 new DisconnectCause(DisconnectCause.ERROR, null, errorMsg, errorMsg);
1422 call.setDisconnectCause(disconnectCause);
1423 }
1424 }
1425
Eric Erfanianccca3152017-02-22 16:32:36 -08001426 /**
1427 * @return {@code true} if the InCallPresenter is ready to be torn down, {@code false} otherwise.
1428 * Calling classes should use this as an indication whether to interact with the
1429 * InCallPresenter or not.
1430 */
1431 public boolean isReadyForTearDown() {
1432 return mInCallActivity == null && !mServiceConnected && mInCallState == InCallState.NO_CALLS;
1433 }
1434
1435 /**
1436 * Checks to see if both the UI is gone and the service is disconnected. If so, tear it all down.
1437 */
1438 private void attemptCleanup() {
1439 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001440 LogUtil.i("InCallPresenter.attemptCleanup", "Cleaning up");
Eric Erfanianccca3152017-02-22 16:32:36 -08001441
1442 cleanupSurfaces();
1443
1444 mIsActivityPreviouslyStarted = false;
1445 mIsChangingConfigurations = false;
1446
1447 // blow away stale contact info so that we get fresh data on
1448 // the next set of calls
1449 if (mContactInfoCache != null) {
1450 mContactInfoCache.clearCache();
1451 }
1452 mContactInfoCache = null;
1453
1454 if (mProximitySensor != null) {
1455 removeListener(mProximitySensor);
1456 mProximitySensor.tearDown();
1457 }
1458 mProximitySensor = null;
1459
1460 if (mStatusBarNotifier != null) {
1461 removeListener(mStatusBarNotifier);
Eric Erfaniand8046e52017-04-06 09:41:50 -07001462 EnrichedCallComponent.get(mContext)
1463 .getEnrichedCallManager()
1464 .unregisterStateChangedListener(mStatusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001465 }
Eric Erfaniand8046e52017-04-06 09:41:50 -07001466
Eric Erfanianccca3152017-02-22 16:32:36 -08001467 if (mExternalCallNotifier != null && mExternalCallList != null) {
1468 mExternalCallList.removeExternalCallListener(mExternalCallNotifier);
1469 }
1470 mStatusBarNotifier = null;
1471
1472 if (mCallList != null) {
1473 mCallList.removeListener(this);
1474 mCallList.removeListener(mSpamCallListListener);
1475 }
1476 mCallList = null;
1477
1478 mContext = null;
1479 mInCallActivity = null;
1480 mManageConferenceActivity = null;
1481
1482 mListeners.clear();
1483 mIncomingCallListeners.clear();
1484 mDetailsListeners.clear();
1485 mCanAddCallListeners.clear();
1486 mOrientationListeners.clear();
1487 mInCallEventListeners.clear();
1488 mInCallUiListeners.clear();
twyen8efb4952017-10-06 16:35:54 -07001489 if (!mInCallUiLocks.isEmpty()) {
1490 LogUtil.e("InCallPresenter.attemptCleanup", "held in call locks: " + mInCallUiLocks);
1491 mInCallUiLocks.clear();
1492 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001493 LogUtil.d("InCallPresenter.attemptCleanup", "finished");
Eric Erfanianccca3152017-02-22 16:32:36 -08001494 }
1495 }
1496
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001497 public void showInCall(boolean showDialpad, boolean newOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001498 LogUtil.i("InCallPresenter.showInCall", "Showing InCallActivity");
Eric Erfanianccca3152017-02-22 16:32:36 -08001499 mContext.startActivity(
1500 InCallActivity.getIntent(
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001501 mContext, showDialpad, newOutgoingCall, false /* forFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -08001502 }
1503
1504 public void onServiceBind() {
1505 mServiceBound = true;
1506 }
1507
1508 public void onServiceUnbind() {
1509 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(false, null);
1510 mServiceBound = false;
1511 }
1512
1513 public boolean isServiceBound() {
1514 return mServiceBound;
1515 }
1516
1517 public void maybeStartRevealAnimation(Intent intent) {
1518 if (intent == null || mInCallActivity != null) {
1519 return;
1520 }
1521 final Bundle extras = intent.getBundleExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS);
1522 if (extras == null) {
1523 // Incoming call, just show the in-call UI directly.
1524 return;
1525 }
1526
1527 if (extras.containsKey(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS)) {
1528 // Account selection dialog will show up so don't show the animation.
1529 return;
1530 }
1531
1532 final PhoneAccountHandle accountHandle =
1533 intent.getParcelableExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE);
1534 final Point touchPoint = extras.getParcelable(TouchPointManager.TOUCH_POINT);
Eric Erfanianccca3152017-02-22 16:32:36 -08001535
1536 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(true, accountHandle);
1537
1538 final Intent activityIntent =
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001539 InCallActivity.getIntent(mContext, false, true, false /* forFullScreen */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001540 activityIntent.putExtra(TouchPointManager.TOUCH_POINT, touchPoint);
1541 mContext.startActivity(activityIntent);
1542 }
1543
1544 /**
1545 * Retrieves the current in-call camera manager instance, creating if necessary.
1546 *
1547 * @return The {@link InCallCameraManager}.
1548 */
1549 public InCallCameraManager getInCallCameraManager() {
1550 synchronized (this) {
1551 if (mInCallCameraManager == null) {
1552 mInCallCameraManager = new InCallCameraManager(mContext);
1553 }
1554
1555 return mInCallCameraManager;
1556 }
1557 }
1558
1559 /**
1560 * Notifies listeners of changes in orientation and notify calls of rotation angle change.
1561 *
1562 * @param orientation The screen orientation of the device (one of: {@link
1563 * InCallOrientationEventListener#SCREEN_ORIENTATION_0}, {@link
1564 * InCallOrientationEventListener#SCREEN_ORIENTATION_90}, {@link
1565 * InCallOrientationEventListener#SCREEN_ORIENTATION_180}, {@link
1566 * InCallOrientationEventListener#SCREEN_ORIENTATION_270}).
1567 */
1568 public void onDeviceOrientationChange(@ScreenOrientation int orientation) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001569 LogUtil.d(
1570 "InCallPresenter.onDeviceOrientationChange",
1571 "onDeviceOrientationChange: orientation= " + orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001572
1573 if (mCallList != null) {
1574 mCallList.notifyCallsOfDeviceRotation(orientation);
1575 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001576 LogUtil.w("InCallPresenter.onDeviceOrientationChange", "CallList is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001577 }
1578
1579 // Notify listeners of device orientation changed.
1580 for (InCallOrientationListener listener : mOrientationListeners) {
1581 listener.onDeviceOrientationChanged(orientation);
1582 }
1583 }
1584
1585 /**
1586 * Configures the in-call UI activity so it can change orientations or not. Enables the
1587 * orientation event listener if allowOrientationChange is true, disables it if false.
1588 *
1589 * @param allowOrientationChange {@code true} if the in-call UI can change between portrait and
1590 * landscape. {@code false} if the in-call UI should be locked in portrait.
1591 */
1592 public void setInCallAllowsOrientationChange(boolean allowOrientationChange) {
1593 if (mInCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001594 LogUtil.e(
1595 "InCallPresenter.setInCallAllowsOrientationChange",
1596 "InCallActivity is null. Can't set requested orientation.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001597 return;
1598 }
1599 mInCallActivity.setAllowOrientationChange(allowOrientationChange);
1600 }
1601
1602 public void enableScreenTimeout(boolean enable) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001603 LogUtil.v("InCallPresenter.enableScreenTimeout", "enableScreenTimeout: value=" + enable);
1604 mScreenTimeoutEnabled = enable;
1605 applyScreenTimeout();
1606 }
1607
1608 private void applyScreenTimeout() {
Eric Erfanianccca3152017-02-22 16:32:36 -08001609 if (mInCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001610 LogUtil.e("InCallPresenter.applyScreenTimeout", "InCallActivity is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001611 return;
1612 }
1613
1614 final Window window = mInCallActivity.getWindow();
Eric Erfanian2ca43182017-08-31 06:57:16 -07001615 if (mScreenTimeoutEnabled) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001616 window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1617 } else {
1618 window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1619 }
1620 }
1621
1622 /**
1623 * Hides or shows the conference manager fragment.
1624 *
1625 * @param show {@code true} if the conference manager should be shown, {@code false} if it should
1626 * be hidden.
1627 */
1628 public void showConferenceCallManager(boolean show) {
1629 if (mInCallActivity != null) {
1630 mInCallActivity.showConferenceFragment(show);
1631 }
1632 if (!show && mManageConferenceActivity != null) {
1633 mManageConferenceActivity.finish();
1634 }
1635 }
1636
1637 /**
1638 * Determines if the dialpad is visible.
1639 *
1640 * @return {@code true} if the dialpad is visible, {@code false} otherwise.
1641 */
1642 public boolean isDialpadVisible() {
1643 if (mInCallActivity == null) {
1644 return false;
1645 }
1646 return mInCallActivity.isDialpadVisible();
1647 }
1648
1649 public ThemeColorManager getThemeColorManager() {
1650 return mThemeColorManager;
1651 }
1652
1653 /** Called when the foreground call changes. */
1654 public void onForegroundCallChanged(DialerCall newForegroundCall) {
1655 mThemeColorManager.onForegroundCallChanged(mContext, newForegroundCall);
1656 if (mInCallActivity != null) {
1657 mInCallActivity.onForegroundCallChanged(newForegroundCall);
1658 }
1659 }
1660
1661 public InCallActivity getActivity() {
1662 return mInCallActivity;
1663 }
1664
1665 /** Called when the UI begins, and starts the callstate callbacks if necessary. */
1666 public void setActivity(InCallActivity inCallActivity) {
1667 if (inCallActivity == null) {
1668 throw new IllegalArgumentException("registerActivity cannot be called with null");
1669 }
1670 if (mInCallActivity != null && mInCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001671 LogUtil.w(
1672 "InCallPresenter.setActivity", "Setting a second activity before destroying the first.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001673 }
1674 updateActivity(inCallActivity);
1675 }
1676
1677 ExternalCallNotifier getExternalCallNotifier() {
1678 return mExternalCallNotifier;
1679 }
1680
1681 VideoSurfaceTexture getLocalVideoSurfaceTexture() {
1682 if (mLocalVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001683 boolean isPixel2017 = false;
1684 if (mContext != null) {
1685 isPixel2017 = mContext.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
1686 }
1687 mLocalVideoSurfaceTexture = VideoSurfaceBindings.createLocalVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001688 }
1689 return mLocalVideoSurfaceTexture;
1690 }
1691
1692 VideoSurfaceTexture getRemoteVideoSurfaceTexture() {
1693 if (mRemoteVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001694 boolean isPixel2017 = false;
1695 if (mContext != null) {
1696 isPixel2017 = mContext.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
1697 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001698 mRemoteVideoSurfaceTexture =
roldenburg12b50c62017-09-01 14:41:19 -07001699 VideoSurfaceBindings.createRemoteVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001700 }
1701 return mRemoteVideoSurfaceTexture;
1702 }
1703
1704 void cleanupSurfaces() {
1705 if (mRemoteVideoSurfaceTexture != null) {
1706 mRemoteVideoSurfaceTexture.setDoneWithSurface();
1707 mRemoteVideoSurfaceTexture = null;
1708 }
1709 if (mLocalVideoSurfaceTexture != null) {
1710 mLocalVideoSurfaceTexture.setDoneWithSurface();
1711 mLocalVideoSurfaceTexture = null;
1712 }
1713 }
1714
1715 /** All the main states of InCallActivity. */
1716 public enum InCallState {
1717 // InCall Screen is off and there are no calls
1718 NO_CALLS,
1719
1720 // Incoming-call screen is up
1721 INCOMING,
1722
1723 // In-call experience is showing
1724 INCALL,
1725
1726 // Waiting for user input before placing outgoing call
1727 WAITING_FOR_ACCOUNT,
1728
1729 // UI is starting up but no call has been initiated yet.
1730 // The UI is waiting for Telecom to respond.
1731 PENDING_OUTGOING,
1732
1733 // User is dialing out
1734 OUTGOING;
1735
1736 public boolean isIncoming() {
1737 return (this == INCOMING);
1738 }
1739
1740 public boolean isConnectingOrConnected() {
1741 return (this == INCOMING || this == OUTGOING || this == INCALL);
1742 }
1743 }
1744
1745 /** Interface implemented by classes that need to know about the InCall State. */
1746 public interface InCallStateListener {
1747
1748 // TODO: Enhance state to contain the call objects instead of passing CallList
1749 void onStateChange(InCallState oldState, InCallState newState, CallList callList);
1750 }
1751
1752 public interface IncomingCallListener {
1753
1754 void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call);
1755 }
1756
1757 public interface CanAddCallListener {
1758
1759 void onCanAddCallChanged(boolean canAddCall);
1760 }
1761
1762 public interface InCallDetailsListener {
1763
1764 void onDetailsChanged(DialerCall call, android.telecom.Call.Details details);
1765 }
1766
1767 public interface InCallOrientationListener {
1768
1769 void onDeviceOrientationChanged(@ScreenOrientation int orientation);
1770 }
1771
1772 /**
1773 * Interface implemented by classes that need to know about events which occur within the In-Call
1774 * UI. Used as a means of communicating between fragments that make up the UI.
1775 */
1776 public interface InCallEventListener {
1777
1778 void onFullscreenModeChanged(boolean isFullscreenMode);
1779 }
1780
1781 public interface InCallUiListener {
1782
1783 void onUiShowing(boolean showing);
1784 }
twyen8efb4952017-10-06 16:35:54 -07001785
1786 private class InCallUiLockImpl implements InCallUiLock {
1787 private final String tag;
1788
1789 private InCallUiLockImpl(String tag) {
1790 this.tag = tag;
1791 }
1792
1793 @MainThread
1794 @Override
1795 public void release() {
1796 Assert.isMainThread();
1797 releaseInCallUiLock(InCallUiLockImpl.this);
1798 }
1799
1800 @Override
1801 public String toString() {
1802 return "InCallUiLock[" + tag + "]";
1803 }
1804 }
1805
1806 @MainThread
1807 public InCallUiLock acquireInCallUiLock(String tag) {
1808 Assert.isMainThread();
1809 InCallUiLock lock = new InCallUiLockImpl(tag);
1810 mInCallUiLocks.add(lock);
1811 return lock;
1812 }
1813
1814 @MainThread
1815 private void releaseInCallUiLock(InCallUiLock lock) {
1816 Assert.isMainThread();
1817 LogUtil.i("InCallPresenter.releaseInCallUiLock", "releasing %s", lock);
1818 mInCallUiLocks.remove(lock);
1819 if (mInCallUiLocks.isEmpty()) {
1820 LogUtil.i("InCallPresenter.releaseInCallUiLock", "all locks released");
1821 if (mInCallState == InCallState.NO_CALLS) {
1822 LogUtil.i("InCallPresenter.releaseInCallUiLock", "no more calls, finishing UI");
1823 attemptFinishActivity();
1824 attemptCleanup();
1825 }
1826 }
1827 }
1828
1829 @MainThread
1830 public boolean isInCallUiLocked() {
1831 Assert.isMainThread();
1832 if (mInCallUiLocks.isEmpty()) {
1833 return false;
1834 }
1835 for (InCallUiLock lock : mInCallUiLocks) {
1836 LogUtil.i("InCallPresenter.isInCallUiLocked", "still locked by %s", lock);
1837 }
1838 return true;
1839 }
1840
1841 private final Set<InCallUiLock> mInCallUiLocks = new ArraySet<>();
Eric Erfanianccca3152017-02-22 16:32:36 -08001842}