blob: 4cc03f3dd70d145bb71152458737cdafd1db5c24 [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;
Eric Erfanianccca3152017-02-22 16:32:36 -080025import android.support.annotation.NonNull;
26import android.support.annotation.Nullable;
27import android.support.annotation.VisibleForTesting;
Eric Erfanian83b20212017-05-31 08:53:10 -070028import android.support.v4.os.UserManagerCompat;
Eric Erfanianccca3152017-02-22 16:32:36 -080029import android.telecom.Call.Details;
30import android.telecom.DisconnectCause;
31import android.telecom.PhoneAccount;
32import android.telecom.PhoneAccountHandle;
33import android.telecom.TelecomManager;
34import android.telecom.VideoProfile;
35import android.telephony.PhoneStateListener;
36import android.telephony.TelephonyManager;
37import android.view.Window;
38import android.view.WindowManager;
Eric Erfanianccca3152017-02-22 16:32:36 -080039import com.android.contacts.common.compat.CallCompat;
40import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler;
41import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler.OnCheckBlockedListener;
Eric Erfanian83b20212017-05-31 08:53:10 -070042import com.android.dialer.blocking.FilteredNumberCompat;
Eric Erfanianccca3152017-02-22 16:32:36 -080043import com.android.dialer.blocking.FilteredNumbersUtil;
44import com.android.dialer.common.LogUtil;
Eric Erfanian2ca43182017-08-31 06:57:16 -070045import com.android.dialer.common.concurrent.DefaultDialerExecutorFactory;
Eric Erfaniand8046e52017-04-06 09:41:50 -070046import com.android.dialer.enrichedcall.EnrichedCallComponent;
Eric Erfanian10b34a52017-05-04 08:23:17 -070047import com.android.dialer.location.GeoUtil;
Eric Erfanian8369df02017-05-03 10:27:13 -070048import com.android.dialer.logging.InteractionEvent;
Eric Erfanianccca3152017-02-22 16:32:36 -080049import com.android.dialer.logging.Logger;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070050import com.android.dialer.postcall.PostCall;
Eric Erfanianccca3152017-02-22 16:32:36 -080051import com.android.dialer.telecom.TelecomUtil;
52import com.android.dialer.util.TouchPointManager;
53import com.android.incallui.InCallOrientationEventListener.ScreenOrientation;
54import com.android.incallui.answerproximitysensor.PseudoScreenState;
55import com.android.incallui.call.CallList;
56import com.android.incallui.call.DialerCall;
Eric Erfanianccca3152017-02-22 16:32:36 -080057import com.android.incallui.call.ExternalCallList;
Eric Erfanianccca3152017-02-22 16:32:36 -080058import com.android.incallui.call.TelecomAdapter;
Eric Erfanian2ca43182017-08-31 06:57:16 -070059import com.android.incallui.disconnectdialog.DisconnectMessage;
Eric Erfanianccca3152017-02-22 16:32:36 -080060import com.android.incallui.latencyreport.LatencyReport;
61import com.android.incallui.legacyblocking.BlockedNumberContentObserver;
62import com.android.incallui.spam.SpamCallListListener;
63import com.android.incallui.util.TelecomCallUtil;
64import com.android.incallui.videosurface.bindings.VideoSurfaceBindings;
65import com.android.incallui.videosurface.protocol.VideoSurfaceTexture;
Eric Erfanian90508232017-03-24 09:31:16 -070066import com.android.incallui.videotech.utils.VideoUtils;
Eric Erfanianccca3152017-02-22 16:32:36 -080067import java.util.Collections;
68import java.util.List;
69import java.util.Objects;
70import java.util.Set;
71import java.util.concurrent.ConcurrentHashMap;
72import java.util.concurrent.CopyOnWriteArrayList;
73import java.util.concurrent.atomic.AtomicBoolean;
74
75/**
76 * Takes updates from the CallList and notifies the InCallActivity (UI) of the changes. Responsible
77 * for starting the activity for a new call and finishing the activity when all calls are
78 * disconnected. Creates and manages the in-call state and provides a listener pattern for the
79 * presenters that want to listen in on the in-call state changes. TODO: This class has become more
80 * of a state machine at this point. Consider renaming.
81 */
Eric Erfaniand5e47f62017-03-15 14:41:07 -070082public class InCallPresenter implements CallList.Listener {
Eric Erfanian2ca43182017-08-31 06:57:16 -070083 private static final String PIXEL2017_SYSTEM_FEATURE =
84 "com.google.android.feature.PIXEL_2017_EXPERIENCE";
Eric Erfanianccca3152017-02-22 16:32:36 -080085 private static final String EXTRA_FIRST_TIME_SHOWN =
86 "com.android.incallui.intent.extra.FIRST_TIME_SHOWN";
87
88 private static final long BLOCK_QUERY_TIMEOUT_MS = 1000;
89
90 private static final Bundle EMPTY_EXTRAS = new Bundle();
91
92 private static InCallPresenter sInCallPresenter;
93
94 /**
95 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is load factor before
96 * resizing, 1 means we only expect a single thread to access the map so make only a single shard
97 */
98 private final Set<InCallStateListener> mListeners =
99 Collections.newSetFromMap(new ConcurrentHashMap<InCallStateListener, Boolean>(8, 0.9f, 1));
100
101 private final List<IncomingCallListener> mIncomingCallListeners = new CopyOnWriteArrayList<>();
102 private final Set<InCallDetailsListener> mDetailsListeners =
103 Collections.newSetFromMap(new ConcurrentHashMap<InCallDetailsListener, Boolean>(8, 0.9f, 1));
104 private final Set<CanAddCallListener> mCanAddCallListeners =
105 Collections.newSetFromMap(new ConcurrentHashMap<CanAddCallListener, Boolean>(8, 0.9f, 1));
106 private final Set<InCallUiListener> mInCallUiListeners =
107 Collections.newSetFromMap(new ConcurrentHashMap<InCallUiListener, Boolean>(8, 0.9f, 1));
108 private final Set<InCallOrientationListener> mOrientationListeners =
109 Collections.newSetFromMap(
110 new ConcurrentHashMap<InCallOrientationListener, Boolean>(8, 0.9f, 1));
111 private final Set<InCallEventListener> mInCallEventListeners =
112 Collections.newSetFromMap(new ConcurrentHashMap<InCallEventListener, Boolean>(8, 0.9f, 1));
113
114 private StatusBarNotifier mStatusBarNotifier;
115 private ExternalCallNotifier mExternalCallNotifier;
116 private ContactInfoCache mContactInfoCache;
117 private Context mContext;
118 private final OnCheckBlockedListener mOnCheckBlockedListener =
119 new OnCheckBlockedListener() {
120 @Override
121 public void onCheckComplete(final Integer id) {
122 if (id != null && id != FilteredNumberAsyncQueryHandler.INVALID_ID) {
123 // Silence the ringer now to prevent ringing and vibration before the call is
124 // terminated when Telecom attempts to add it.
125 TelecomUtil.silenceRinger(mContext);
126 }
127 }
128 };
129 private CallList mCallList;
130 private ExternalCallList mExternalCallList;
131 private InCallActivity mInCallActivity;
132 private ManageConferenceActivity mManageConferenceActivity;
133 private final android.telecom.Call.Callback mCallCallback =
134 new android.telecom.Call.Callback() {
135 @Override
136 public void onPostDialWait(
137 android.telecom.Call telecomCall, String remainingPostDialSequence) {
138 final DialerCall call = mCallList.getDialerCallFromTelecomCall(telecomCall);
139 if (call == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700140 LogUtil.w(
141 "InCallPresenter.onPostDialWait",
142 "DialerCall not found in call list: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800143 return;
144 }
145 onPostDialCharWait(call.getId(), remainingPostDialSequence);
146 }
147
148 @Override
149 public void onDetailsChanged(
150 android.telecom.Call telecomCall, android.telecom.Call.Details details) {
151 final DialerCall call = mCallList.getDialerCallFromTelecomCall(telecomCall);
152 if (call == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700153 LogUtil.w(
154 "InCallPresenter.onDetailsChanged",
155 "DialerCall not found in call list: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800156 return;
157 }
158
159 if (details.hasProperty(Details.PROPERTY_IS_EXTERNAL_CALL)
160 && !mExternalCallList.isCallTracked(telecomCall)) {
161
162 // A regular call became an external call so swap call lists.
Eric Erfanian2ca43182017-08-31 06:57:16 -0700163 LogUtil.i("InCallPresenter.onDetailsChanged", "Call became external: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800164 mCallList.onInternalCallMadeExternal(mContext, telecomCall);
165 mExternalCallList.onCallAdded(telecomCall);
166 return;
167 }
168
169 for (InCallDetailsListener listener : mDetailsListeners) {
170 listener.onDetailsChanged(call, details);
171 }
172 }
173
174 @Override
175 public void onConferenceableCallsChanged(
176 android.telecom.Call telecomCall, List<android.telecom.Call> conferenceableCalls) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700177 LogUtil.i(
178 "InCallPresenter.onConferenceableCallsChanged",
179 "onConferenceableCallsChanged: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800180 onDetailsChanged(telecomCall, telecomCall.getDetails());
181 }
182 };
183 private InCallState mInCallState = InCallState.NO_CALLS;
184 private ProximitySensor mProximitySensor;
185 private final PseudoScreenState mPseudoScreenState = new PseudoScreenState();
186 private boolean mServiceConnected;
Eric Erfanianccca3152017-02-22 16:32:36 -0800187 private InCallCameraManager mInCallCameraManager;
188 private FilteredNumberAsyncQueryHandler mFilteredQueryHandler;
189 private CallList.Listener mSpamCallListListener;
190 /** Whether or not we are currently bound and waiting for Telecom to send us a new call. */
191 private boolean mBoundAndWaitingForOutgoingCall;
192 /** Determines if the InCall UI is in fullscreen mode or not. */
193 private boolean mIsFullScreen = false;
194
Eric Erfanian2ca43182017-08-31 06:57:16 -0700195 private boolean mScreenTimeoutEnabled = true;
196
Eric Erfanianccca3152017-02-22 16:32:36 -0800197 private PhoneStateListener mPhoneStateListener =
198 new PhoneStateListener() {
199 @Override
200 public void onCallStateChanged(int state, String incomingNumber) {
201 if (state == TelephonyManager.CALL_STATE_RINGING) {
202 if (FilteredNumbersUtil.hasRecentEmergencyCall(mContext)) {
203 return;
204 }
205 // Check if the number is blocked, to silence the ringer.
206 String countryIso = GeoUtil.getCurrentCountryIso(mContext);
207 mFilteredQueryHandler.isBlockedNumber(
208 mOnCheckBlockedListener, incomingNumber, countryIso);
209 }
210 }
211 };
212 /**
213 * Is true when the activity has been previously started. Some code needs to know not just if the
214 * activity is currently up, but if it had been previously shown in foreground for this in-call
215 * session (e.g., StatusBarNotifier). This gets reset when the session ends in the tear-down
216 * method.
217 */
218 private boolean mIsActivityPreviouslyStarted = false;
219
220 /** Whether or not InCallService is bound to Telecom. */
221 private boolean mServiceBound = false;
222
223 /**
224 * When configuration changes Android kills the current activity and starts a new one. The flag is
225 * used to check if full clean up is necessary (activity is stopped and new activity won't be
226 * started), or if a new activity will be started right after the current one is destroyed, and
227 * therefore no need in release all resources.
228 */
229 private boolean mIsChangingConfigurations = false;
230
231 private boolean mAwaitingCallListUpdate = false;
232
233 private ExternalCallList.ExternalCallListener mExternalCallListener =
234 new ExternalCallList.ExternalCallListener() {
235
236 @Override
237 public void onExternalCallPulled(android.telecom.Call call) {
238 // Note: keep this code in sync with InCallPresenter#onCallAdded
239 LatencyReport latencyReport = new LatencyReport(call);
240 latencyReport.onCallBlockingDone();
241 // Note: External calls do not require spam checking.
242 mCallList.onCallAdded(mContext, call, latencyReport);
243 call.registerCallback(mCallCallback);
244 }
245
246 @Override
247 public void onExternalCallAdded(android.telecom.Call call) {
248 // No-op
249 }
250
251 @Override
252 public void onExternalCallRemoved(android.telecom.Call call) {
253 // No-op
254 }
255
256 @Override
257 public void onExternalCallUpdated(android.telecom.Call call) {
258 // No-op
259 }
260 };
261
262 private ThemeColorManager mThemeColorManager;
263 private VideoSurfaceTexture mLocalVideoSurfaceTexture;
264 private VideoSurfaceTexture mRemoteVideoSurfaceTexture;
265
Eric Erfanian10b34a52017-05-04 08:23:17 -0700266 /** Inaccessible constructor. Must use getRunningInstance() to get this singleton. */
Eric Erfanianccca3152017-02-22 16:32:36 -0800267 @VisibleForTesting
268 InCallPresenter() {}
269
270 public static synchronized InCallPresenter getInstance() {
271 if (sInCallPresenter == null) {
272 sInCallPresenter = new InCallPresenter();
273 }
274 return sInCallPresenter;
275 }
276
Eric Erfanian10b34a52017-05-04 08:23:17 -0700277 @VisibleForTesting
278 public static synchronized void setInstanceForTesting(InCallPresenter inCallPresenter) {
279 sInCallPresenter = inCallPresenter;
280 }
281
Eric Erfanianccca3152017-02-22 16:32:36 -0800282 /**
283 * Determines whether or not a call has no valid phone accounts that can be used to make the call
284 * with. Emergency calls do not require a phone account.
285 *
286 * @param call to check accounts for.
287 * @return {@code true} if the call has no call capable phone accounts set, {@code false} if the
288 * call contains a phone account that could be used to initiate it with, or is an emergency
289 * call.
290 */
291 public static boolean isCallWithNoValidAccounts(DialerCall call) {
292 if (call != null && !call.isEmergencyCall()) {
293 Bundle extras = call.getIntentExtras();
294
295 if (extras == null) {
296 extras = EMPTY_EXTRAS;
297 }
298
299 final List<PhoneAccountHandle> phoneAccountHandles =
300 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
301
302 if ((call.getAccountHandle() == null
303 && (phoneAccountHandles == null || phoneAccountHandles.isEmpty()))) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700304 LogUtil.i(
305 "InCallPresenter.isCallWithNoValidAccounts", "No valid accounts for call " + call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800306 return true;
307 }
308 }
309 return false;
310 }
311
312 public InCallState getInCallState() {
313 return mInCallState;
314 }
315
316 public CallList getCallList() {
317 return mCallList;
318 }
319
320 public void setUp(
321 @NonNull Context context,
322 CallList callList,
323 ExternalCallList externalCallList,
324 StatusBarNotifier statusBarNotifier,
325 ExternalCallNotifier externalCallNotifier,
326 ContactInfoCache contactInfoCache,
Eric Erfanian83b20212017-05-31 08:53:10 -0700327 ProximitySensor proximitySensor,
328 FilteredNumberAsyncQueryHandler filteredNumberQueryHandler) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700329 Trace.beginSection("InCallPresenter.setUp");
Eric Erfanianccca3152017-02-22 16:32:36 -0800330 if (mServiceConnected) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700331 LogUtil.i("InCallPresenter.setUp", "New service connection replacing existing one.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800332 if (context != mContext || callList != mCallList) {
333 throw new IllegalStateException();
334 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700335 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800336 return;
337 }
338
339 Objects.requireNonNull(context);
340 mContext = context;
341
342 mContactInfoCache = contactInfoCache;
343
344 mStatusBarNotifier = statusBarNotifier;
345 mExternalCallNotifier = externalCallNotifier;
346 addListener(mStatusBarNotifier);
Eric Erfaniand8046e52017-04-06 09:41:50 -0700347 EnrichedCallComponent.get(mContext)
348 .getEnrichedCallManager()
349 .registerStateChangedListener(mStatusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -0800350
351 mProximitySensor = proximitySensor;
352 addListener(mProximitySensor);
353
354 mThemeColorManager =
355 new ThemeColorManager(new InCallUIMaterialColorMapUtils(mContext.getResources()));
356
357 mCallList = callList;
358 mExternalCallList = externalCallList;
359 externalCallList.addExternalCallListener(mExternalCallNotifier);
360 externalCallList.addExternalCallListener(mExternalCallListener);
361
362 // This only gets called by the service so this is okay.
363 mServiceConnected = true;
364
365 // The final thing we do in this set up is add ourselves as a listener to CallList. This
366 // will kick off an update and the whole process can start.
367 mCallList.addListener(this);
368
369 // Create spam call list listener and add it to the list of listeners
Eric Erfanian2ca43182017-08-31 06:57:16 -0700370 mSpamCallListListener = new SpamCallListListener(context, new DefaultDialerExecutorFactory());
Eric Erfanianccca3152017-02-22 16:32:36 -0800371 mCallList.addListener(mSpamCallListListener);
372
373 VideoPauseController.getInstance().setUp(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800374
Eric Erfanian83b20212017-05-31 08:53:10 -0700375 mFilteredQueryHandler = filteredNumberQueryHandler;
Eric Erfanianccca3152017-02-22 16:32:36 -0800376 mContext
377 .getSystemService(TelephonyManager.class)
378 .listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
379
Eric Erfanian2ca43182017-08-31 06:57:16 -0700380 LogUtil.d("InCallPresenter.setUp", "Finished InCallPresenter.setUp");
381 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800382 }
383
384 /**
385 * Called when the telephony service has disconnected from us. This will happen when there are no
386 * more active calls. However, we may still want to continue showing the UI for certain cases like
387 * showing "Call Ended". What we really want is to wait for the activity and the service to both
388 * disconnect before we tear things down. This method sets a serviceConnected boolean and calls a
389 * secondary method that performs the aforementioned logic.
390 */
391 public void tearDown() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700392 LogUtil.d("InCallPresenter.tearDown", "tearDown");
Eric Erfanianccca3152017-02-22 16:32:36 -0800393 mCallList.clearOnDisconnect();
394
395 mServiceConnected = false;
396
397 mContext
398 .getSystemService(TelephonyManager.class)
399 .listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
400
401 attemptCleanup();
402 VideoPauseController.getInstance().tearDown();
Eric Erfanianccca3152017-02-22 16:32:36 -0800403 }
404
405 private void attemptFinishActivity() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700406 mScreenTimeoutEnabled = true;
Eric Erfanianccca3152017-02-22 16:32:36 -0800407 final boolean doFinish = (mInCallActivity != null && isActivityStarted());
Eric Erfanian2ca43182017-08-31 06:57:16 -0700408 LogUtil.i("InCallPresenter.attemptFinishActivity", "Hide in call UI: " + doFinish);
Eric Erfanianccca3152017-02-22 16:32:36 -0800409 if (doFinish) {
410 mInCallActivity.setExcludeFromRecents(true);
411 mInCallActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -0800412 }
413 }
414
415 /**
416 * Called when the UI ends. Attempts to tear down everything if necessary. See {@link #tearDown()}
417 * for more insight on the tear-down process.
418 */
419 public void unsetActivity(InCallActivity inCallActivity) {
420 if (inCallActivity == null) {
421 throw new IllegalArgumentException("unregisterActivity cannot be called with null");
422 }
423 if (mInCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700424 LogUtil.i(
425 "InCallPresenter.unsetActivity", "No InCallActivity currently set, no need to unset.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800426 return;
427 }
428 if (mInCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700429 LogUtil.w(
430 "InCallPresenter.unsetActivity",
Eric Erfanianccca3152017-02-22 16:32:36 -0800431 "Second instance of InCallActivity is trying to unregister when another"
432 + " instance is active. Ignoring.");
433 return;
434 }
435 updateActivity(null);
436 }
437
438 /**
439 * Updates the current instance of {@link InCallActivity} with the provided one. If a {@code null}
440 * activity is provided, it means that the activity was finished and we should attempt to cleanup.
441 */
442 private void updateActivity(InCallActivity inCallActivity) {
443 boolean updateListeners = false;
444 boolean doAttemptCleanup = false;
445
446 if (inCallActivity != null) {
447 if (mInCallActivity == null) {
448 updateListeners = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700449 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized");
Eric Erfanianccca3152017-02-22 16:32:36 -0800450 } else {
451 // since setActivity is called onStart(), it can be called multiple times.
452 // This is fine and ignorable, but we do not want to update the world every time
453 // this happens (like going to/from background) so we do not set updateListeners.
454 }
455
456 mInCallActivity = inCallActivity;
457 mInCallActivity.setExcludeFromRecents(false);
458
459 // By the time the UI finally comes up, the call may already be disconnected.
460 // If that's the case, we may need to show an error dialog.
461 if (mCallList != null && mCallList.getDisconnectedCall() != null) {
462 maybeShowErrorDialogOnDisconnect(mCallList.getDisconnectedCall());
463 }
464
465 // When the UI comes up, we need to first check the in-call state.
466 // If we are showing NO_CALLS, that means that a call probably connected and
467 // then immediately disconnected before the UI was able to come up.
468 // If we dont have any calls, start tearing down the UI instead.
469 // NOTE: This code relies on {@link #mInCallActivity} being set so we run it after
470 // it has been set.
471 if (mInCallState == InCallState.NO_CALLS) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700472 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized, but no calls left. Shut down");
Eric Erfanianccca3152017-02-22 16:32:36 -0800473 attemptFinishActivity();
474 return;
475 }
476 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700477 LogUtil.i("InCallPresenter.updateActivity", "UI Destroyed");
Eric Erfanianccca3152017-02-22 16:32:36 -0800478 updateListeners = true;
479 mInCallActivity = null;
480
481 // We attempt cleanup for the destroy case but only after we recalculate the state
482 // to see if we need to come back up or stay shut down. This is why we do the
483 // cleanup after the call to onCallListChange() instead of directly here.
484 doAttemptCleanup = true;
485 }
486
487 // Messages can come from the telephony layer while the activity is coming up
488 // and while the activity is going down. So in both cases we need to recalculate what
489 // state we should be in after they complete.
490 // Examples: (1) A new incoming call could come in and then get disconnected before
491 // the activity is created.
492 // (2) All calls could disconnect and then get a new incoming call before the
493 // activity is destroyed.
494 //
495 // b/1122139 - We previously had a check for mServiceConnected here as well, but there are
496 // cases where we need to recalculate the current state even if the service in not
497 // connected. In particular the case where startOrFinish() is called while the app is
498 // already finish()ing. In that case, we skip updating the state with the knowledge that
499 // we will check again once the activity has finished. That means we have to recalculate the
500 // state here even if the service is disconnected since we may not have finished a state
501 // transition while finish()ing.
502 if (updateListeners) {
503 onCallListChange(mCallList);
504 }
505
506 if (doAttemptCleanup) {
507 attemptCleanup();
508 }
509 }
510
511 public void setManageConferenceActivity(
512 @Nullable ManageConferenceActivity manageConferenceActivity) {
513 mManageConferenceActivity = manageConferenceActivity;
514 }
515
516 public void onBringToForeground(boolean showDialpad) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700517 LogUtil.i("InCallPresenter.onBringToForeground", "Bringing UI to foreground.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800518 bringToForeground(showDialpad);
519 }
520
521 public void onCallAdded(final android.telecom.Call call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700522 Trace.beginSection("InCallPresenter.onCallAdded");
Eric Erfanianccca3152017-02-22 16:32:36 -0800523 LatencyReport latencyReport = new LatencyReport(call);
524 if (shouldAttemptBlocking(call)) {
525 maybeBlockCall(call, latencyReport);
526 } else {
527 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
528 mExternalCallList.onCallAdded(call);
529 } else {
530 latencyReport.onCallBlockingDone();
531 mCallList.onCallAdded(mContext, call, latencyReport);
532 }
533 }
534
535 // Since a call has been added we are no longer waiting for Telecom to send us a call.
536 setBoundAndWaitingForOutgoingCall(false, null);
537 call.registerCallback(mCallCallback);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700538 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800539 }
540
541 private boolean shouldAttemptBlocking(android.telecom.Call call) {
542 if (call.getState() != android.telecom.Call.STATE_RINGING) {
543 return false;
544 }
Eric Erfanian83b20212017-05-31 08:53:10 -0700545 if (!UserManagerCompat.isUserUnlocked(mContext)) {
546 LogUtil.i(
547 "InCallPresenter.shouldAttemptBlocking",
548 "not attempting to block incoming call because user is locked");
549 return false;
550 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800551 if (TelecomCallUtil.isEmergencyCall(call)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700552 LogUtil.i(
553 "InCallPresenter.shouldAttemptBlocking",
554 "Not attempting to block incoming emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800555 return false;
556 }
557 if (FilteredNumbersUtil.hasRecentEmergencyCall(mContext)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700558 LogUtil.i(
559 "InCallPresenter.shouldAttemptBlocking",
560 "Not attempting to block incoming call due to recent emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800561 return false;
562 }
563 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
564 return false;
565 }
Eric Erfanian83b20212017-05-31 08:53:10 -0700566 if (FilteredNumberCompat.useNewFiltering(mContext)) {
567 LogUtil.i(
568 "InCallPresenter.shouldAttemptBlocking",
569 "not attempting to block incoming call because framework blocking is in use");
570 return false;
571 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800572 return true;
573 }
574
575 /**
576 * Checks whether a call should be blocked, and blocks it if so. Otherwise, it adds the call to
577 * the CallList so it can proceed as normal. There is a timeout, so if the function for checking
578 * whether a function is blocked does not return in a reasonable time, we proceed with adding the
579 * call anyways.
580 */
581 private void maybeBlockCall(final android.telecom.Call call, final LatencyReport latencyReport) {
582 final String countryIso = GeoUtil.getCurrentCountryIso(mContext);
583 final String number = TelecomCallUtil.getNumber(call);
584 final long timeAdded = System.currentTimeMillis();
585
586 // Though AtomicBoolean's can be scary, don't fear, as in this case it is only used on the
587 // main UI thread. It is needed so we can change its value within different scopes, since
588 // that cannot be done with a final boolean.
589 final AtomicBoolean hasTimedOut = new AtomicBoolean(false);
590
591 final Handler handler = new Handler();
592
593 // Proceed if the query is slow; the call may still be blocked after the query returns.
594 final Runnable runnable =
595 new Runnable() {
596 @Override
597 public void run() {
598 hasTimedOut.set(true);
599 latencyReport.onCallBlockingDone();
600 mCallList.onCallAdded(mContext, call, latencyReport);
601 }
602 };
603 handler.postDelayed(runnable, BLOCK_QUERY_TIMEOUT_MS);
604
605 OnCheckBlockedListener onCheckBlockedListener =
606 new OnCheckBlockedListener() {
607 @Override
608 public void onCheckComplete(final Integer id) {
609 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700610 LogUtil.i("InCallPresenter.onCheckComplete", "torn down, not adding call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800611 return;
612 }
613 if (!hasTimedOut.get()) {
614 handler.removeCallbacks(runnable);
615 }
616 if (id == null) {
617 if (!hasTimedOut.get()) {
618 latencyReport.onCallBlockingDone();
619 mCallList.onCallAdded(mContext, call, latencyReport);
620 }
621 } else if (id == FilteredNumberAsyncQueryHandler.INVALID_ID) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700622 LogUtil.d(
623 "InCallPresenter.onCheckComplete", "invalid number, skipping block checking");
Eric Erfanianccca3152017-02-22 16:32:36 -0800624 if (!hasTimedOut.get()) {
625 handler.removeCallbacks(runnable);
626
627 latencyReport.onCallBlockingDone();
628 mCallList.onCallAdded(mContext, call, latencyReport);
629 }
630 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700631 LogUtil.i(
632 "InCallPresenter.onCheckComplete", "Rejecting incoming call from blocked number");
Eric Erfanianccca3152017-02-22 16:32:36 -0800633 call.reject(false, null);
634 Logger.get(mContext).logInteraction(InteractionEvent.Type.CALL_BLOCKED);
635
636 /*
637 * If mContext is null, then the InCallPresenter was torn down before the
638 * block check had a chance to complete. The context is no longer valid, so
639 * don't attempt to remove the call log entry.
640 */
641 if (mContext == null) {
642 return;
643 }
644 // Register observer to update the call log.
645 // BlockedNumberContentObserver will unregister after successful log or timeout.
646 BlockedNumberContentObserver contentObserver =
647 new BlockedNumberContentObserver(mContext, new Handler(), number, timeAdded);
648 contentObserver.register();
649 }
650 }
651 };
652
653 mFilteredQueryHandler.isBlockedNumber(onCheckBlockedListener, number, countryIso);
654 }
655
656 public void onCallRemoved(android.telecom.Call call) {
657 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
658 mExternalCallList.onCallRemoved(call);
659 } else {
660 mCallList.onCallRemoved(mContext, call);
661 call.unregisterCallback(mCallCallback);
662 }
663 }
664
665 public void onCanAddCallChanged(boolean canAddCall) {
666 for (CanAddCallListener listener : mCanAddCallListeners) {
667 listener.onCanAddCallChanged(canAddCall);
668 }
669 }
670
671 @Override
672 public void onWiFiToLteHandover(DialerCall call) {
673 if (mInCallActivity != null) {
674 mInCallActivity.onWiFiToLteHandover(call);
675 }
676 }
677
678 @Override
679 public void onHandoverToWifiFailed(DialerCall call) {
680 if (mInCallActivity != null) {
681 mInCallActivity.onHandoverToWifiFailed(call);
682 }
683 }
684
Eric Erfanianc857f902017-05-15 14:05:33 -0700685 @Override
686 public void onInternationalCallOnWifi(@NonNull DialerCall call) {
687 LogUtil.enterBlock("InCallPresenter.onInternationalCallOnWifi");
688 if (mInCallActivity != null) {
689 mInCallActivity.onInternationalCallOnWifi(call);
690 }
691 }
692
Eric Erfanianccca3152017-02-22 16:32:36 -0800693 /**
694 * Called when there is a change to the call list. Sets the In-Call state for the entire in-call
695 * app based on the information it gets from CallList. Dispatches the in-call state to all
696 * listeners. Can trigger the creation or destruction of the UI based on the states that is
697 * calculates.
698 */
699 @Override
700 public void onCallListChange(CallList callList) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700701 Trace.beginSection("InCallPresenter.onCallListChange");
Eric Erfanianccca3152017-02-22 16:32:36 -0800702 if (mInCallActivity != null && mInCallActivity.isInCallScreenAnimating()) {
703 mAwaitingCallListUpdate = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700704 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800705 return;
706 }
707 if (callList == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700708 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800709 return;
710 }
711
712 mAwaitingCallListUpdate = false;
713
714 InCallState newState = getPotentialStateFromCallList(callList);
715 InCallState oldState = mInCallState;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700716 LogUtil.d(
717 "InCallPresenter.onCallListChange",
718 "onCallListChange oldState= " + oldState + " newState=" + newState);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700719
720 // If the user placed a call and was asked to choose the account, but then pressed "Home", the
721 // incall activity for that call will still exist (even if it's not visible). In the case of
722 // an incoming call in that situation, just disconnect that "waiting for account" call and
723 // dismiss the dialog. The same activity will be reused to handle the new incoming call. See
724 // b/33247755 for more details.
725 DialerCall waitingForAccountCall;
726 if (newState == InCallState.INCOMING
727 && (waitingForAccountCall = callList.getWaitingForAccountCall()) != null) {
728 waitingForAccountCall.disconnect();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700729 // The InCallActivity might be destroyed or not started yet at this point.
730 if (isActivityStarted()) {
731 mInCallActivity.dismissPendingDialogs();
732 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700733 }
734
Eric Erfanianccca3152017-02-22 16:32:36 -0800735 newState = startOrFinishUi(newState);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700736 LogUtil.d(
737 "InCallPresenter.onCallListChange", "onCallListChange newState changed to " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800738
739 // Set the new state before announcing it to the world
Eric Erfanian2ca43182017-08-31 06:57:16 -0700740 LogUtil.i(
741 "InCallPresenter.onCallListChange",
742 "Phone switching state: " + oldState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800743 mInCallState = newState;
744
745 // notify listeners of new state
746 for (InCallStateListener listener : mListeners) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700747 LogUtil.d(
748 "InCallPresenter.onCallListChange",
749 "Notify " + listener + " of state " + mInCallState.toString());
Eric Erfanianccca3152017-02-22 16:32:36 -0800750 listener.onStateChange(oldState, mInCallState, callList);
751 }
752
753 if (isActivityStarted()) {
754 final boolean hasCall =
755 callList.getActiveOrBackgroundCall() != null || callList.getOutgoingCall() != null;
756 mInCallActivity.dismissKeyguard(hasCall);
757 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700758 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800759 }
760
761 /** Called when there is a new incoming call. */
762 @Override
763 public void onIncomingCall(DialerCall call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700764 Trace.beginSection("InCallPresenter.onIncomingCall");
Eric Erfanianccca3152017-02-22 16:32:36 -0800765 InCallState newState = startOrFinishUi(InCallState.INCOMING);
766 InCallState oldState = mInCallState;
767
Eric Erfanian2ca43182017-08-31 06:57:16 -0700768 LogUtil.i(
769 "InCallPresenter.onIncomingCall", "Phone switching state: " + oldState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800770 mInCallState = newState;
771
wangqicf61ca02017-08-31 15:32:55 -0700772 Trace.beginSection("listener.onIncomingCall");
Eric Erfanianccca3152017-02-22 16:32:36 -0800773 for (IncomingCallListener listener : mIncomingCallListeners) {
774 listener.onIncomingCall(oldState, mInCallState, call);
775 }
wangqicf61ca02017-08-31 15:32:55 -0700776 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800777
wangqicf61ca02017-08-31 15:32:55 -0700778 Trace.beginSection("onPrimaryCallStateChanged");
Eric Erfanianccca3152017-02-22 16:32:36 -0800779 if (mInCallActivity != null) {
780 // Re-evaluate which fragment is being shown.
781 mInCallActivity.onPrimaryCallStateChanged();
782 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700783 Trace.endSection();
wangqicf61ca02017-08-31 15:32:55 -0700784 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800785 }
786
787 @Override
788 public void onUpgradeToVideo(DialerCall call) {
Eric Erfanian90508232017-03-24 09:31:16 -0700789 if (VideoUtils.hasReceivedVideoUpgradeRequest(call.getVideoTech().getSessionModificationState())
Eric Erfanianccca3152017-02-22 16:32:36 -0800790 && mInCallState == InCallPresenter.InCallState.INCOMING) {
791 LogUtil.i(
792 "InCallPresenter.onUpgradeToVideo",
793 "rejecting upgrade request due to existing incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700794 call.getVideoTech().declineVideoRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -0800795 }
796
797 if (mInCallActivity != null) {
798 // Re-evaluate which fragment is being shown.
799 mInCallActivity.onPrimaryCallStateChanged();
800 }
801 }
802
803 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700804 public void onSessionModificationStateChange(DialerCall call) {
805 int newState = call.getVideoTech().getSessionModificationState();
Eric Erfanianccca3152017-02-22 16:32:36 -0800806 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "state: %d", newState);
807 if (mProximitySensor == null) {
808 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "proximitySensor is null");
809 return;
810 }
811 mProximitySensor.setIsAttemptingVideoCall(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700812 call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest());
Eric Erfanianccca3152017-02-22 16:32:36 -0800813 if (mInCallActivity != null) {
814 // Re-evaluate which fragment is being shown.
815 mInCallActivity.onPrimaryCallStateChanged();
816 }
817 }
818
819 /**
820 * Called when a call becomes disconnected. Called everytime an existing call changes from being
821 * connected (incoming/outgoing/active) to disconnected.
822 */
823 @Override
824 public void onDisconnect(DialerCall call) {
825 maybeShowErrorDialogOnDisconnect(call);
826
827 // We need to do the run the same code as onCallListChange.
828 onCallListChange(mCallList);
829
830 if (isActivityStarted()) {
831 mInCallActivity.dismissKeyguard(false);
832 }
833
834 if (call.isEmergencyCall()) {
835 FilteredNumbersUtil.recordLastEmergencyCallTime(mContext);
836 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800837
Eric Erfanianfc37b022017-03-21 10:11:17 -0700838 if (!mCallList.hasLiveCall()
839 && !call.getLogState().isIncoming
Eric Erfanian10b34a52017-05-04 08:23:17 -0700840 && !isSecretCode(call.getNumber())
Eric Erfanianfc37b022017-03-21 10:11:17 -0700841 && !CallerInfoUtils.isVoiceMailNumber(mContext, call)) {
Eric Erfanian10b34a52017-05-04 08:23:17 -0700842 PostCall.onCallDisconnected(mContext, call.getNumber(), call.getConnectTimeMillis());
Eric Erfanianccca3152017-02-22 16:32:36 -0800843 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800844 }
845
Eric Erfanian10b34a52017-05-04 08:23:17 -0700846 private boolean isSecretCode(@Nullable String number) {
847 return number != null
848 && (number.length() <= 8 || number.startsWith("*#*#") || number.endsWith("#*#*"));
849 }
850
Eric Erfanianccca3152017-02-22 16:32:36 -0800851 /** Given the call list, return the state in which the in-call screen should be. */
852 public InCallState getPotentialStateFromCallList(CallList callList) {
853
854 InCallState newState = InCallState.NO_CALLS;
855
856 if (callList == null) {
857 return newState;
858 }
859 if (callList.getIncomingCall() != null) {
860 newState = InCallState.INCOMING;
861 } else if (callList.getWaitingForAccountCall() != null) {
862 newState = InCallState.WAITING_FOR_ACCOUNT;
863 } else if (callList.getPendingOutgoingCall() != null) {
864 newState = InCallState.PENDING_OUTGOING;
865 } else if (callList.getOutgoingCall() != null) {
866 newState = InCallState.OUTGOING;
867 } else if (callList.getActiveCall() != null
868 || callList.getBackgroundCall() != null
869 || callList.getDisconnectedCall() != null
870 || callList.getDisconnectingCall() != null) {
871 newState = InCallState.INCALL;
872 }
873
874 if (newState == InCallState.NO_CALLS) {
875 if (mBoundAndWaitingForOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700876 return InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -0800877 }
878 }
879
880 return newState;
881 }
882
883 public boolean isBoundAndWaitingForOutgoingCall() {
884 return mBoundAndWaitingForOutgoingCall;
885 }
886
887 public void setBoundAndWaitingForOutgoingCall(boolean isBound, PhoneAccountHandle handle) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700888 LogUtil.i(
889 "InCallPresenter.setBoundAndWaitingForOutgoingCall",
890 "setBoundAndWaitingForOutgoingCall: " + isBound);
Eric Erfanianccca3152017-02-22 16:32:36 -0800891 mBoundAndWaitingForOutgoingCall = isBound;
892 mThemeColorManager.setPendingPhoneAccountHandle(handle);
893 if (isBound && mInCallState == InCallState.NO_CALLS) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700894 mInCallState = InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -0800895 }
896 }
897
898 public void onShrinkAnimationComplete() {
899 if (mAwaitingCallListUpdate) {
900 onCallListChange(mCallList);
901 }
902 }
903
904 public void addIncomingCallListener(IncomingCallListener listener) {
905 Objects.requireNonNull(listener);
906 mIncomingCallListeners.add(listener);
907 }
908
909 public void removeIncomingCallListener(IncomingCallListener listener) {
910 if (listener != null) {
911 mIncomingCallListeners.remove(listener);
912 }
913 }
914
915 public void addListener(InCallStateListener listener) {
916 Objects.requireNonNull(listener);
917 mListeners.add(listener);
918 }
919
920 public void removeListener(InCallStateListener listener) {
921 if (listener != null) {
922 mListeners.remove(listener);
923 }
924 }
925
926 public void addDetailsListener(InCallDetailsListener listener) {
927 Objects.requireNonNull(listener);
928 mDetailsListeners.add(listener);
929 }
930
931 public void removeDetailsListener(InCallDetailsListener listener) {
932 if (listener != null) {
933 mDetailsListeners.remove(listener);
934 }
935 }
936
937 public void addCanAddCallListener(CanAddCallListener listener) {
938 Objects.requireNonNull(listener);
939 mCanAddCallListeners.add(listener);
940 }
941
942 public void removeCanAddCallListener(CanAddCallListener listener) {
943 if (listener != null) {
944 mCanAddCallListeners.remove(listener);
945 }
946 }
947
948 public void addOrientationListener(InCallOrientationListener listener) {
949 Objects.requireNonNull(listener);
950 mOrientationListeners.add(listener);
951 }
952
953 public void removeOrientationListener(InCallOrientationListener listener) {
954 if (listener != null) {
955 mOrientationListeners.remove(listener);
956 }
957 }
958
959 public void addInCallEventListener(InCallEventListener listener) {
960 Objects.requireNonNull(listener);
961 mInCallEventListeners.add(listener);
962 }
963
964 public void removeInCallEventListener(InCallEventListener listener) {
965 if (listener != null) {
966 mInCallEventListeners.remove(listener);
967 }
968 }
969
970 public ProximitySensor getProximitySensor() {
971 return mProximitySensor;
972 }
973
974 public PseudoScreenState getPseudoScreenState() {
975 return mPseudoScreenState;
976 }
977
978 /** Returns true if the incall app is the foreground application. */
979 public boolean isShowingInCallUi() {
980 if (!isActivityStarted()) {
981 return false;
982 }
983 if (mManageConferenceActivity != null && mManageConferenceActivity.isVisible()) {
984 return true;
985 }
986 return mInCallActivity.isVisible();
987 }
988
989 /**
990 * Returns true if the activity has been created and is running. Returns true as long as activity
991 * is not destroyed or finishing. This ensures that we return true even if the activity is paused
992 * (not in foreground).
993 */
994 public boolean isActivityStarted() {
995 return (mInCallActivity != null
996 && !mInCallActivity.isDestroyed()
997 && !mInCallActivity.isFinishing());
998 }
999
1000 /**
1001 * Determines if the In-Call app is currently changing configuration.
1002 *
1003 * @return {@code true} if the In-Call app is changing configuration.
1004 */
1005 public boolean isChangingConfigurations() {
1006 return mIsChangingConfigurations;
1007 }
1008
1009 /**
1010 * Tracks whether the In-Call app is currently in the process of changing configuration (i.e.
1011 * screen orientation).
1012 */
1013 /*package*/
1014 void updateIsChangingConfigurations() {
1015 mIsChangingConfigurations = false;
1016 if (mInCallActivity != null) {
1017 mIsChangingConfigurations = mInCallActivity.isChangingConfigurations();
1018 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001019 LogUtil.v(
1020 "InCallPresenter.updateIsChangingConfigurations",
1021 "updateIsChangingConfigurations = " + mIsChangingConfigurations);
Eric Erfanianccca3152017-02-22 16:32:36 -08001022 }
1023
1024 /** Called when the activity goes in/out of the foreground. */
1025 public void onUiShowing(boolean showing) {
1026 // We need to update the notification bar when we leave the UI because that
1027 // could trigger it to show again.
1028 if (mStatusBarNotifier != null) {
1029 mStatusBarNotifier.updateNotification(mCallList);
1030 }
1031
1032 if (mProximitySensor != null) {
1033 mProximitySensor.onInCallShowing(showing);
1034 }
1035
1036 Intent broadcastIntent = Bindings.get(mContext).getUiReadyBroadcastIntent(mContext);
1037 if (broadcastIntent != null) {
1038 broadcastIntent.putExtra(EXTRA_FIRST_TIME_SHOWN, !mIsActivityPreviouslyStarted);
1039
1040 if (showing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001041 LogUtil.d("InCallPresenter.onUiShowing", "Sending sticky broadcast: ", broadcastIntent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001042 mContext.sendStickyBroadcast(broadcastIntent);
1043 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001044 LogUtil.d("InCallPresenter.onUiShowing", "Removing sticky broadcast: ", broadcastIntent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001045 mContext.removeStickyBroadcast(broadcastIntent);
1046 }
1047 }
1048
1049 if (showing) {
1050 mIsActivityPreviouslyStarted = true;
1051 } else {
1052 updateIsChangingConfigurations();
1053 }
1054
1055 for (InCallUiListener listener : mInCallUiListeners) {
1056 listener.onUiShowing(showing);
1057 }
1058
1059 if (mInCallActivity != null) {
1060 // Re-evaluate which fragment is being shown.
1061 mInCallActivity.onPrimaryCallStateChanged();
1062 }
1063 }
1064
Eric Erfanian2ca43182017-08-31 06:57:16 -07001065 public void refreshUi() {
1066 if (mInCallActivity != null) {
1067 // Re-evaluate which fragment is being shown.
1068 mInCallActivity.onPrimaryCallStateChanged();
1069 }
1070 }
1071
Eric Erfanianccca3152017-02-22 16:32:36 -08001072 public void addInCallUiListener(InCallUiListener listener) {
1073 mInCallUiListeners.add(listener);
1074 }
1075
1076 public boolean removeInCallUiListener(InCallUiListener listener) {
1077 return mInCallUiListeners.remove(listener);
1078 }
1079
1080 /*package*/
1081 void onActivityStarted() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001082 LogUtil.d("InCallPresenter.onActivityStarted", "onActivityStarted");
Eric Erfanianccca3152017-02-22 16:32:36 -08001083 notifyVideoPauseController(true);
Eric Erfaniand8046e52017-04-06 09:41:50 -07001084 if (mStatusBarNotifier != null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001085 // TODO(maxwelb) - b/36649622: Investigate this redundant call
Eric Erfaniand8046e52017-04-06 09:41:50 -07001086 mStatusBarNotifier.updateNotification(mCallList);
1087 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001088 applyScreenTimeout();
Eric Erfanianccca3152017-02-22 16:32:36 -08001089 }
1090
1091 /*package*/
1092 void onActivityStopped() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001093 LogUtil.d("InCallPresenter.onActivityStopped", "onActivityStopped");
Eric Erfanianccca3152017-02-22 16:32:36 -08001094 notifyVideoPauseController(false);
1095 }
1096
1097 private void notifyVideoPauseController(boolean showing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001098 LogUtil.d(
1099 "InCallPresenter.notifyVideoPauseController",
1100 "mIsChangingConfigurations=" + mIsChangingConfigurations);
Eric Erfanianccca3152017-02-22 16:32:36 -08001101 if (!mIsChangingConfigurations) {
1102 VideoPauseController.getInstance().onUiShowing(showing);
1103 }
1104 }
1105
1106 /** Brings the app into the foreground if possible. */
1107 public void bringToForeground(boolean showDialpad) {
1108 // Before we bring the incall UI to the foreground, we check to see if:
1109 // 1. It is not currently in the foreground
1110 // 2. We are in a state where we want to show the incall ui (i.e. there are calls to
1111 // be displayed)
1112 // If the activity hadn't actually been started previously, yet there are still calls
1113 // present (e.g. a call was accepted by a bluetooth or wired headset), we want to
1114 // bring it up the UI regardless.
1115 if (!isShowingInCallUi() && mInCallState != InCallState.NO_CALLS) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001116 showInCall(showDialpad, false /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001117 }
1118 }
1119
1120 public void onPostDialCharWait(String callId, String chars) {
1121 if (isActivityStarted()) {
1122 mInCallActivity.showPostCharWaitDialog(callId, chars);
1123 }
1124 }
1125
1126 /**
1127 * Handles the green CALL key while in-call.
1128 *
1129 * @return true if we consumed the event.
1130 */
1131 public boolean handleCallKey() {
1132 LogUtil.v("InCallPresenter.handleCallKey", null);
1133
1134 // The green CALL button means either "Answer", "Unhold", or
1135 // "Swap calls", or can be a no-op, depending on the current state
1136 // of the Phone.
1137
1138 /** INCOMING CALL */
1139 final CallList calls = mCallList;
1140 final DialerCall incomingCall = calls.getIncomingCall();
1141 LogUtil.v("InCallPresenter.handleCallKey", "incomingCall: " + incomingCall);
1142
1143 // (1) Attempt to answer a call
1144 if (incomingCall != null) {
1145 incomingCall.answer(VideoProfile.STATE_AUDIO_ONLY);
1146 return true;
1147 }
1148
1149 /** STATE_ACTIVE CALL */
1150 final DialerCall activeCall = calls.getActiveCall();
1151 if (activeCall != null) {
1152 // TODO: This logic is repeated from CallButtonPresenter.java. We should
1153 // consolidate this logic.
1154 final boolean canMerge =
1155 activeCall.can(android.telecom.Call.Details.CAPABILITY_MERGE_CONFERENCE);
1156 final boolean canSwap =
1157 activeCall.can(android.telecom.Call.Details.CAPABILITY_SWAP_CONFERENCE);
1158
Eric Erfanian2ca43182017-08-31 06:57:16 -07001159 LogUtil.v(
1160 "InCallPresenter.handleCallKey",
1161 "activeCall: " + activeCall + ", canMerge: " + canMerge + ", canSwap: " + canSwap);
Eric Erfanianccca3152017-02-22 16:32:36 -08001162
1163 // (2) Attempt actions on conference calls
1164 if (canMerge) {
1165 TelecomAdapter.getInstance().merge(activeCall.getId());
1166 return true;
1167 } else if (canSwap) {
1168 TelecomAdapter.getInstance().swap(activeCall.getId());
1169 return true;
1170 }
1171 }
1172
1173 /** BACKGROUND CALL */
1174 final DialerCall heldCall = calls.getBackgroundCall();
1175 if (heldCall != null) {
1176 // We have a hold call so presumeable it will always support HOLD...but
1177 // there is no harm in double checking.
1178 final boolean canHold = heldCall.can(android.telecom.Call.Details.CAPABILITY_HOLD);
1179
Eric Erfanian2ca43182017-08-31 06:57:16 -07001180 LogUtil.v("InCallPresenter.handleCallKey", "heldCall: " + heldCall + ", canHold: " + canHold);
Eric Erfanianccca3152017-02-22 16:32:36 -08001181
1182 // (4) unhold call
1183 if (heldCall.getState() == DialerCall.State.ONHOLD && canHold) {
1184 heldCall.unhold();
1185 return true;
1186 }
1187 }
1188
1189 // Always consume hard keys
1190 return true;
1191 }
1192
1193 /**
1194 * A dialog could have prevented in-call screen from being previously finished. This function
1195 * checks to see if there should be any UI left and if not attempts to tear down the UI.
1196 */
1197 public void onDismissDialog() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001198 LogUtil.i("InCallPresenter.onDismissDialog", "Dialog dismissed");
Eric Erfanianccca3152017-02-22 16:32:36 -08001199 if (mInCallState == InCallState.NO_CALLS) {
1200 attemptFinishActivity();
1201 attemptCleanup();
1202 }
1203 }
1204
1205 /** Clears the previous fullscreen state. */
1206 public void clearFullscreen() {
1207 mIsFullScreen = false;
1208 }
1209
1210 /**
1211 * Changes the fullscreen mode of the in-call UI.
1212 *
1213 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1214 * otherwise.
1215 */
1216 public void setFullScreen(boolean isFullScreen) {
1217 setFullScreen(isFullScreen, false /* force */);
1218 }
1219
1220 /**
1221 * Changes the fullscreen mode of the in-call UI.
1222 *
1223 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1224 * otherwise.
1225 * @param force {@code true} if fullscreen mode should be set regardless of its current state.
1226 */
1227 public void setFullScreen(boolean isFullScreen, boolean force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001228 LogUtil.i("InCallPresenter.setFullScreen", "setFullScreen = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001229
1230 // As a safeguard, ensure we cannot enter fullscreen if the dialpad is shown.
1231 if (isDialpadVisible()) {
1232 isFullScreen = false;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001233 LogUtil.v(
1234 "InCallPresenter.setFullScreen",
1235 "setFullScreen overridden as dialpad is shown = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001236 }
1237
1238 if (mIsFullScreen == isFullScreen && !force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001239 LogUtil.v("InCallPresenter.setFullScreen", "setFullScreen ignored as already in that state.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001240 return;
1241 }
1242 mIsFullScreen = isFullScreen;
1243 notifyFullscreenModeChange(mIsFullScreen);
1244 }
1245
1246 /**
1247 * @return {@code true} if the in-call ui is currently in fullscreen mode, {@code false}
1248 * otherwise.
1249 */
1250 public boolean isFullscreen() {
1251 return mIsFullScreen;
1252 }
1253
1254 /**
1255 * Called by the {@link VideoCallPresenter} to inform of a change in full screen video status.
1256 *
1257 * @param isFullscreenMode {@code True} if entering full screen mode.
1258 */
1259 public void notifyFullscreenModeChange(boolean isFullscreenMode) {
1260 for (InCallEventListener listener : mInCallEventListeners) {
1261 listener.onFullscreenModeChanged(isFullscreenMode);
1262 }
1263 }
1264
1265 /**
1266 * For some disconnected causes, we show a dialog. This calls into the activity to show the dialog
1267 * if appropriate for the call.
1268 */
1269 private void maybeShowErrorDialogOnDisconnect(DialerCall call) {
1270 // For newly disconnected calls, we may want to show a dialog on specific error conditions
1271 if (isActivityStarted() && call.getState() == DialerCall.State.DISCONNECTED) {
1272 if (call.getAccountHandle() == null && !call.isConferenceCall()) {
1273 setDisconnectCauseForMissingAccounts(call);
1274 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001275 mInCallActivity.maybeShowErrorDialogOnDisconnect(
1276 new DisconnectMessage(mInCallActivity, call));
Eric Erfanianccca3152017-02-22 16:32:36 -08001277 }
1278 }
1279
1280 /**
1281 * When the state of in-call changes, this is the first method to get called. It determines if the
1282 * UI needs to be started or finished depending on the new state and does it.
1283 */
1284 private InCallState startOrFinishUi(InCallState newState) {
wangqicf61ca02017-08-31 15:32:55 -07001285 Trace.beginSection("InCallPresenter.startOrFinishUi");
Eric Erfanian2ca43182017-08-31 06:57:16 -07001286 LogUtil.d(
1287 "InCallPresenter.startOrFinishUi", "startOrFinishUi: " + mInCallState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -08001288
1289 // TODO: Consider a proper state machine implementation
1290
1291 // If the state isn't changing we have already done any starting/stopping of activities in
1292 // a previous pass...so lets cut out early
1293 if (newState == mInCallState) {
wangqicf61ca02017-08-31 15:32:55 -07001294 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001295 return newState;
1296 }
1297
1298 // A new Incoming call means that the user needs to be notified of the the call (since
1299 // it wasn't them who initiated it). We do this through full screen notifications and
1300 // happens indirectly through {@link StatusBarNotifier}.
1301 //
1302 // The process for incoming calls is as follows:
1303 //
1304 // 1) CallList - Announces existence of new INCOMING call
1305 // 2) InCallPresenter - Gets announcement and calculates that the new InCallState
1306 // - should be set to INCOMING.
1307 // 3) InCallPresenter - This method is called to see if we need to start or finish
1308 // the app given the new state.
1309 // 4) StatusBarNotifier - Listens to InCallState changes. InCallPresenter calls
1310 // StatusBarNotifier explicitly to issue a FullScreen Notification
1311 // that will either start the InCallActivity or show the user a
1312 // top-level notification dialog if the user is in an immersive app.
1313 // That notification can also start the InCallActivity.
1314 // 5) InCallActivity - Main activity starts up and at the end of its onCreate will
1315 // call InCallPresenter::setActivity() to let the presenter
1316 // know that start-up is complete.
1317 //
1318 // [ AND NOW YOU'RE IN THE CALL. voila! ]
1319 //
1320 // Our app is started using a fullScreen notification. We need to do this whenever
1321 // we get an incoming call. Depending on the current context of the device, either a
1322 // incoming call HUN or the actual InCallActivity will be shown.
1323 final boolean startIncomingCallSequence = (InCallState.INCOMING == newState);
1324
1325 // A dialog to show on top of the InCallUI to select a PhoneAccount
1326 final boolean showAccountPicker = (InCallState.WAITING_FOR_ACCOUNT == newState);
1327
1328 // A new outgoing call indicates that the user just now dialed a number and when that
1329 // happens we need to display the screen immediately or show an account picker dialog if
1330 // no default is set. However, if the main InCallUI is already visible, we do not want to
1331 // re-initiate the start-up animation, so we do not need to do anything here.
1332 //
1333 // It is also possible to go into an intermediate state where the call has been initiated
1334 // but Telecom has not yet returned with the details of the call (handle, gateway, etc.).
1335 // This pending outgoing state can also launch the call screen.
1336 //
1337 // This is different from the incoming call sequence because we do not need to shock the
1338 // user with a top-level notification. Just show the call UI normally.
1339 boolean callCardFragmentVisible =
1340 mInCallActivity != null && mInCallActivity.getCallCardFragmentVisible();
1341 final boolean mainUiNotVisible = !isShowingInCallUi() || !callCardFragmentVisible;
1342 boolean showCallUi = InCallState.OUTGOING == newState && mainUiNotVisible;
1343
1344 // Direct transition from PENDING_OUTGOING -> INCALL means that there was an error in the
1345 // outgoing call process, so the UI should be brought up to show an error dialog.
1346 showCallUi |=
1347 (InCallState.PENDING_OUTGOING == mInCallState
1348 && InCallState.INCALL == newState
1349 && !isShowingInCallUi());
1350
1351 // Another exception - InCallActivity is in charge of disconnecting a call with no
1352 // valid accounts set. Bring the UI up if this is true for the current pending outgoing
1353 // call so that:
1354 // 1) The call can be disconnected correctly
1355 // 2) The UI comes up and correctly displays the error dialog.
1356 // TODO: Remove these special case conditions by making InCallPresenter a true state
1357 // machine. Telecom should also be the component responsible for disconnecting a call
1358 // with no valid accounts.
1359 showCallUi |=
1360 InCallState.PENDING_OUTGOING == newState
1361 && mainUiNotVisible
1362 && isCallWithNoValidAccounts(mCallList.getPendingOutgoingCall());
1363
1364 // The only time that we have an instance of mInCallActivity and it isn't started is
1365 // when it is being destroyed. In that case, lets avoid bringing up another instance of
1366 // the activity. When it is finally destroyed, we double check if we should bring it back
1367 // up so we aren't going to lose anything by avoiding a second startup here.
1368 boolean activityIsFinishing = mInCallActivity != null && !isActivityStarted();
1369 if (activityIsFinishing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001370 LogUtil.i(
1371 "InCallPresenter.startOrFinishUi",
1372 "Undo the state change: " + newState + " -> " + mInCallState);
wangqicf61ca02017-08-31 15:32:55 -07001373 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001374 return mInCallState;
1375 }
1376
1377 // We're about the bring up the in-call UI for outgoing and incoming call. If we still have
1378 // dialogs up, we need to clear them out before showing in-call screen. This is necessary
1379 // to fix the bug that dialog will show up when data reaches limit even after makeing new
1380 // outgoing call after user ignore it by pressing home button.
1381 if ((newState == InCallState.INCOMING || newState == InCallState.PENDING_OUTGOING)
1382 && !showCallUi
1383 && isActivityStarted()) {
1384 mInCallActivity.dismissPendingDialogs();
1385 }
1386
1387 if (showCallUi || showAccountPicker) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001388 LogUtil.i("InCallPresenter.startOrFinishUi", "Start in call UI");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001389 showInCall(false /* showDialpad */, !showAccountPicker /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001390 } else if (startIncomingCallSequence) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001391 LogUtil.i("InCallPresenter.startOrFinishUi", "Start Full Screen in call UI");
Eric Erfanianccca3152017-02-22 16:32:36 -08001392
Eric Erfaniand8046e52017-04-06 09:41:50 -07001393 mStatusBarNotifier.updateNotification(mCallList);
Eric Erfanianccca3152017-02-22 16:32:36 -08001394 } else if (newState == InCallState.NO_CALLS) {
1395 // The new state is the no calls state. Tear everything down.
1396 attemptFinishActivity();
1397 attemptCleanup();
1398 }
1399
wangqicf61ca02017-08-31 15:32:55 -07001400 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001401 return newState;
1402 }
1403
1404 /**
1405 * Sets the DisconnectCause for a call that was disconnected because it was missing a PhoneAccount
1406 * or PhoneAccounts to select from.
1407 */
1408 private void setDisconnectCauseForMissingAccounts(DialerCall call) {
1409
1410 Bundle extras = call.getIntentExtras();
1411 // Initialize the extras bundle to avoid NPE
1412 if (extras == null) {
1413 extras = new Bundle();
1414 }
1415
1416 final List<PhoneAccountHandle> phoneAccountHandles =
1417 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
1418
1419 if (phoneAccountHandles == null || phoneAccountHandles.isEmpty()) {
1420 String scheme = call.getHandle().getScheme();
1421 final String errorMsg =
1422 PhoneAccount.SCHEME_TEL.equals(scheme)
1423 ? mContext.getString(R.string.callFailed_simError)
1424 : mContext.getString(R.string.incall_error_supp_service_unknown);
1425 DisconnectCause disconnectCause =
1426 new DisconnectCause(DisconnectCause.ERROR, null, errorMsg, errorMsg);
1427 call.setDisconnectCause(disconnectCause);
1428 }
1429 }
1430
Eric Erfanianccca3152017-02-22 16:32:36 -08001431 /**
1432 * @return {@code true} if the InCallPresenter is ready to be torn down, {@code false} otherwise.
1433 * Calling classes should use this as an indication whether to interact with the
1434 * InCallPresenter or not.
1435 */
1436 public boolean isReadyForTearDown() {
1437 return mInCallActivity == null && !mServiceConnected && mInCallState == InCallState.NO_CALLS;
1438 }
1439
1440 /**
1441 * Checks to see if both the UI is gone and the service is disconnected. If so, tear it all down.
1442 */
1443 private void attemptCleanup() {
1444 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001445 LogUtil.i("InCallPresenter.attemptCleanup", "Cleaning up");
Eric Erfanianccca3152017-02-22 16:32:36 -08001446
1447 cleanupSurfaces();
1448
1449 mIsActivityPreviouslyStarted = false;
1450 mIsChangingConfigurations = false;
1451
1452 // blow away stale contact info so that we get fresh data on
1453 // the next set of calls
1454 if (mContactInfoCache != null) {
1455 mContactInfoCache.clearCache();
1456 }
1457 mContactInfoCache = null;
1458
1459 if (mProximitySensor != null) {
1460 removeListener(mProximitySensor);
1461 mProximitySensor.tearDown();
1462 }
1463 mProximitySensor = null;
1464
1465 if (mStatusBarNotifier != null) {
1466 removeListener(mStatusBarNotifier);
Eric Erfaniand8046e52017-04-06 09:41:50 -07001467 EnrichedCallComponent.get(mContext)
1468 .getEnrichedCallManager()
1469 .unregisterStateChangedListener(mStatusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001470 }
Eric Erfaniand8046e52017-04-06 09:41:50 -07001471
Eric Erfanianccca3152017-02-22 16:32:36 -08001472 if (mExternalCallNotifier != null && mExternalCallList != null) {
1473 mExternalCallList.removeExternalCallListener(mExternalCallNotifier);
1474 }
1475 mStatusBarNotifier = null;
1476
1477 if (mCallList != null) {
1478 mCallList.removeListener(this);
1479 mCallList.removeListener(mSpamCallListListener);
1480 }
1481 mCallList = null;
1482
1483 mContext = null;
1484 mInCallActivity = null;
1485 mManageConferenceActivity = null;
1486
1487 mListeners.clear();
1488 mIncomingCallListeners.clear();
1489 mDetailsListeners.clear();
1490 mCanAddCallListeners.clear();
1491 mOrientationListeners.clear();
1492 mInCallEventListeners.clear();
1493 mInCallUiListeners.clear();
1494
Eric Erfanian2ca43182017-08-31 06:57:16 -07001495 LogUtil.d("InCallPresenter.attemptCleanup", "finished");
Eric Erfanianccca3152017-02-22 16:32:36 -08001496 }
1497 }
1498
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001499 public void showInCall(boolean showDialpad, boolean newOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001500 LogUtil.i("InCallPresenter.showInCall", "Showing InCallActivity");
Eric Erfanianccca3152017-02-22 16:32:36 -08001501 mContext.startActivity(
1502 InCallActivity.getIntent(
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001503 mContext, showDialpad, newOutgoingCall, false /* forFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -08001504 }
1505
1506 public void onServiceBind() {
1507 mServiceBound = true;
1508 }
1509
1510 public void onServiceUnbind() {
1511 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(false, null);
1512 mServiceBound = false;
1513 }
1514
1515 public boolean isServiceBound() {
1516 return mServiceBound;
1517 }
1518
1519 public void maybeStartRevealAnimation(Intent intent) {
1520 if (intent == null || mInCallActivity != null) {
1521 return;
1522 }
1523 final Bundle extras = intent.getBundleExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS);
1524 if (extras == null) {
1525 // Incoming call, just show the in-call UI directly.
1526 return;
1527 }
1528
1529 if (extras.containsKey(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS)) {
1530 // Account selection dialog will show up so don't show the animation.
1531 return;
1532 }
1533
1534 final PhoneAccountHandle accountHandle =
1535 intent.getParcelableExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE);
1536 final Point touchPoint = extras.getParcelable(TouchPointManager.TOUCH_POINT);
Eric Erfanianccca3152017-02-22 16:32:36 -08001537
1538 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(true, accountHandle);
1539
1540 final Intent activityIntent =
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001541 InCallActivity.getIntent(mContext, false, true, false /* forFullScreen */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001542 activityIntent.putExtra(TouchPointManager.TOUCH_POINT, touchPoint);
1543 mContext.startActivity(activityIntent);
1544 }
1545
1546 /**
1547 * Retrieves the current in-call camera manager instance, creating if necessary.
1548 *
1549 * @return The {@link InCallCameraManager}.
1550 */
1551 public InCallCameraManager getInCallCameraManager() {
1552 synchronized (this) {
1553 if (mInCallCameraManager == null) {
1554 mInCallCameraManager = new InCallCameraManager(mContext);
1555 }
1556
1557 return mInCallCameraManager;
1558 }
1559 }
1560
1561 /**
1562 * Notifies listeners of changes in orientation and notify calls of rotation angle change.
1563 *
1564 * @param orientation The screen orientation of the device (one of: {@link
1565 * InCallOrientationEventListener#SCREEN_ORIENTATION_0}, {@link
1566 * InCallOrientationEventListener#SCREEN_ORIENTATION_90}, {@link
1567 * InCallOrientationEventListener#SCREEN_ORIENTATION_180}, {@link
1568 * InCallOrientationEventListener#SCREEN_ORIENTATION_270}).
1569 */
1570 public void onDeviceOrientationChange(@ScreenOrientation int orientation) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001571 LogUtil.d(
1572 "InCallPresenter.onDeviceOrientationChange",
1573 "onDeviceOrientationChange: orientation= " + orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001574
1575 if (mCallList != null) {
1576 mCallList.notifyCallsOfDeviceRotation(orientation);
1577 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001578 LogUtil.w("InCallPresenter.onDeviceOrientationChange", "CallList is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001579 }
1580
1581 // Notify listeners of device orientation changed.
1582 for (InCallOrientationListener listener : mOrientationListeners) {
1583 listener.onDeviceOrientationChanged(orientation);
1584 }
1585 }
1586
1587 /**
1588 * Configures the in-call UI activity so it can change orientations or not. Enables the
1589 * orientation event listener if allowOrientationChange is true, disables it if false.
1590 *
1591 * @param allowOrientationChange {@code true} if the in-call UI can change between portrait and
1592 * landscape. {@code false} if the in-call UI should be locked in portrait.
1593 */
1594 public void setInCallAllowsOrientationChange(boolean allowOrientationChange) {
1595 if (mInCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001596 LogUtil.e(
1597 "InCallPresenter.setInCallAllowsOrientationChange",
1598 "InCallActivity is null. Can't set requested orientation.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001599 return;
1600 }
1601 mInCallActivity.setAllowOrientationChange(allowOrientationChange);
1602 }
1603
1604 public void enableScreenTimeout(boolean enable) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001605 LogUtil.v("InCallPresenter.enableScreenTimeout", "enableScreenTimeout: value=" + enable);
1606 mScreenTimeoutEnabled = enable;
1607 applyScreenTimeout();
1608 }
1609
1610 private void applyScreenTimeout() {
Eric Erfanianccca3152017-02-22 16:32:36 -08001611 if (mInCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001612 LogUtil.e("InCallPresenter.applyScreenTimeout", "InCallActivity is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001613 return;
1614 }
1615
1616 final Window window = mInCallActivity.getWindow();
Eric Erfanian2ca43182017-08-31 06:57:16 -07001617 if (mScreenTimeoutEnabled) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001618 window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1619 } else {
1620 window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1621 }
1622 }
1623
1624 /**
1625 * Hides or shows the conference manager fragment.
1626 *
1627 * @param show {@code true} if the conference manager should be shown, {@code false} if it should
1628 * be hidden.
1629 */
1630 public void showConferenceCallManager(boolean show) {
1631 if (mInCallActivity != null) {
1632 mInCallActivity.showConferenceFragment(show);
1633 }
1634 if (!show && mManageConferenceActivity != null) {
1635 mManageConferenceActivity.finish();
1636 }
1637 }
1638
1639 /**
1640 * Determines if the dialpad is visible.
1641 *
1642 * @return {@code true} if the dialpad is visible, {@code false} otherwise.
1643 */
1644 public boolean isDialpadVisible() {
1645 if (mInCallActivity == null) {
1646 return false;
1647 }
1648 return mInCallActivity.isDialpadVisible();
1649 }
1650
1651 public ThemeColorManager getThemeColorManager() {
1652 return mThemeColorManager;
1653 }
1654
1655 /** Called when the foreground call changes. */
1656 public void onForegroundCallChanged(DialerCall newForegroundCall) {
1657 mThemeColorManager.onForegroundCallChanged(mContext, newForegroundCall);
1658 if (mInCallActivity != null) {
1659 mInCallActivity.onForegroundCallChanged(newForegroundCall);
1660 }
1661 }
1662
1663 public InCallActivity getActivity() {
1664 return mInCallActivity;
1665 }
1666
1667 /** Called when the UI begins, and starts the callstate callbacks if necessary. */
1668 public void setActivity(InCallActivity inCallActivity) {
1669 if (inCallActivity == null) {
1670 throw new IllegalArgumentException("registerActivity cannot be called with null");
1671 }
1672 if (mInCallActivity != null && mInCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001673 LogUtil.w(
1674 "InCallPresenter.setActivity", "Setting a second activity before destroying the first.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001675 }
1676 updateActivity(inCallActivity);
1677 }
1678
1679 ExternalCallNotifier getExternalCallNotifier() {
1680 return mExternalCallNotifier;
1681 }
1682
1683 VideoSurfaceTexture getLocalVideoSurfaceTexture() {
1684 if (mLocalVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001685 boolean isPixel2017 = false;
1686 if (mContext != null) {
1687 isPixel2017 = mContext.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
1688 }
1689 mLocalVideoSurfaceTexture = VideoSurfaceBindings.createLocalVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001690 }
1691 return mLocalVideoSurfaceTexture;
1692 }
1693
1694 VideoSurfaceTexture getRemoteVideoSurfaceTexture() {
1695 if (mRemoteVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001696 boolean isPixel2017 = false;
1697 if (mContext != null) {
1698 isPixel2017 = mContext.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
1699 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001700 mRemoteVideoSurfaceTexture =
roldenburg12b50c62017-09-01 14:41:19 -07001701 VideoSurfaceBindings.createRemoteVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001702 }
1703 return mRemoteVideoSurfaceTexture;
1704 }
1705
1706 void cleanupSurfaces() {
1707 if (mRemoteVideoSurfaceTexture != null) {
1708 mRemoteVideoSurfaceTexture.setDoneWithSurface();
1709 mRemoteVideoSurfaceTexture = null;
1710 }
1711 if (mLocalVideoSurfaceTexture != null) {
1712 mLocalVideoSurfaceTexture.setDoneWithSurface();
1713 mLocalVideoSurfaceTexture = null;
1714 }
1715 }
1716
1717 /** All the main states of InCallActivity. */
1718 public enum InCallState {
1719 // InCall Screen is off and there are no calls
1720 NO_CALLS,
1721
1722 // Incoming-call screen is up
1723 INCOMING,
1724
1725 // In-call experience is showing
1726 INCALL,
1727
1728 // Waiting for user input before placing outgoing call
1729 WAITING_FOR_ACCOUNT,
1730
1731 // UI is starting up but no call has been initiated yet.
1732 // The UI is waiting for Telecom to respond.
1733 PENDING_OUTGOING,
1734
1735 // User is dialing out
1736 OUTGOING;
1737
1738 public boolean isIncoming() {
1739 return (this == INCOMING);
1740 }
1741
1742 public boolean isConnectingOrConnected() {
1743 return (this == INCOMING || this == OUTGOING || this == INCALL);
1744 }
1745 }
1746
1747 /** Interface implemented by classes that need to know about the InCall State. */
1748 public interface InCallStateListener {
1749
1750 // TODO: Enhance state to contain the call objects instead of passing CallList
1751 void onStateChange(InCallState oldState, InCallState newState, CallList callList);
1752 }
1753
1754 public interface IncomingCallListener {
1755
1756 void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call);
1757 }
1758
1759 public interface CanAddCallListener {
1760
1761 void onCanAddCallChanged(boolean canAddCall);
1762 }
1763
1764 public interface InCallDetailsListener {
1765
1766 void onDetailsChanged(DialerCall call, android.telecom.Call.Details details);
1767 }
1768
1769 public interface InCallOrientationListener {
1770
1771 void onDeviceOrientationChanged(@ScreenOrientation int orientation);
1772 }
1773
1774 /**
1775 * Interface implemented by classes that need to know about events which occur within the In-Call
1776 * UI. Used as a means of communicating between fragments that make up the UI.
1777 */
1778 public interface InCallEventListener {
1779
1780 void onFullscreenModeChanged(boolean isFullscreenMode);
1781 }
1782
1783 public interface InCallUiListener {
1784
1785 void onUiShowing(boolean showing);
1786 }
1787}