blob: c5310b969fd57fe159d4404632d7ce16834c90a6 [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;
yueg77cb8e52017-10-27 16:42:51 -070031import android.telecom.CallAudioState;
Eric Erfanianccca3152017-02-22 16:32:36 -080032import android.telecom.DisconnectCause;
33import android.telecom.PhoneAccount;
34import android.telecom.PhoneAccountHandle;
35import android.telecom.TelecomManager;
36import android.telecom.VideoProfile;
37import android.telephony.PhoneStateListener;
38import android.telephony.TelephonyManager;
twyen8efb4952017-10-06 16:35:54 -070039import android.util.ArraySet;
Eric Erfanianccca3152017-02-22 16:32:36 -080040import android.view.Window;
41import android.view.WindowManager;
Eric Erfanianccca3152017-02-22 16:32:36 -080042import com.android.contacts.common.compat.CallCompat;
43import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler;
44import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler.OnCheckBlockedListener;
Eric Erfanian83b20212017-05-31 08:53:10 -070045import com.android.dialer.blocking.FilteredNumberCompat;
Eric Erfanianccca3152017-02-22 16:32:36 -080046import com.android.dialer.blocking.FilteredNumbersUtil;
twyen8efb4952017-10-06 16:35:54 -070047import com.android.dialer.common.Assert;
Eric Erfanianccca3152017-02-22 16:32:36 -080048import com.android.dialer.common.LogUtil;
zachh6a4cebd2017-10-24 17:10:06 -070049import com.android.dialer.common.concurrent.DialerExecutorComponent;
Eric Erfaniand8046e52017-04-06 09:41:50 -070050import com.android.dialer.enrichedcall.EnrichedCallComponent;
Eric Erfanian10b34a52017-05-04 08:23:17 -070051import com.android.dialer.location.GeoUtil;
Eric Erfanian8369df02017-05-03 10:27:13 -070052import com.android.dialer.logging.InteractionEvent;
Eric Erfanianccca3152017-02-22 16:32:36 -080053import com.android.dialer.logging.Logger;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070054import com.android.dialer.postcall.PostCall;
Eric Erfanianccca3152017-02-22 16:32:36 -080055import com.android.dialer.telecom.TelecomUtil;
56import com.android.dialer.util.TouchPointManager;
57import com.android.incallui.InCallOrientationEventListener.ScreenOrientation;
58import com.android.incallui.answerproximitysensor.PseudoScreenState;
yueg77cb8e52017-10-27 16:42:51 -070059import com.android.incallui.audiomode.AudioModeProvider;
Eric Erfanianccca3152017-02-22 16:32:36 -080060import com.android.incallui.call.CallList;
61import com.android.incallui.call.DialerCall;
Eric Erfanianccca3152017-02-22 16:32:36 -080062import com.android.incallui.call.ExternalCallList;
Eric Erfanianccca3152017-02-22 16:32:36 -080063import com.android.incallui.call.TelecomAdapter;
Eric Erfanian2ca43182017-08-31 06:57:16 -070064import com.android.incallui.disconnectdialog.DisconnectMessage;
twyen8efb4952017-10-06 16:35:54 -070065import com.android.incallui.incalluilock.InCallUiLock;
Eric Erfanianccca3152017-02-22 16:32:36 -080066import com.android.incallui.latencyreport.LatencyReport;
67import com.android.incallui.legacyblocking.BlockedNumberContentObserver;
68import com.android.incallui.spam.SpamCallListListener;
69import com.android.incallui.util.TelecomCallUtil;
70import com.android.incallui.videosurface.bindings.VideoSurfaceBindings;
71import com.android.incallui.videosurface.protocol.VideoSurfaceTexture;
Eric Erfanian90508232017-03-24 09:31:16 -070072import com.android.incallui.videotech.utils.VideoUtils;
Eric Erfanianccca3152017-02-22 16:32:36 -080073import java.util.Collections;
74import java.util.List;
75import java.util.Objects;
76import java.util.Set;
77import java.util.concurrent.ConcurrentHashMap;
78import java.util.concurrent.CopyOnWriteArrayList;
79import java.util.concurrent.atomic.AtomicBoolean;
80
81/**
82 * Takes updates from the CallList and notifies the InCallActivity (UI) of the changes. Responsible
83 * for starting the activity for a new call and finishing the activity when all calls are
84 * disconnected. Creates and manages the in-call state and provides a listener pattern for the
85 * presenters that want to listen in on the in-call state changes. TODO: This class has become more
86 * of a state machine at this point. Consider renaming.
87 */
yueg77cb8e52017-10-27 16:42:51 -070088public class InCallPresenter implements CallList.Listener, AudioModeProvider.AudioModeListener {
Eric Erfanian2ca43182017-08-31 06:57:16 -070089 private static final String PIXEL2017_SYSTEM_FEATURE =
90 "com.google.android.feature.PIXEL_2017_EXPERIENCE";
Eric Erfanianccca3152017-02-22 16:32:36 -080091
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 };
yueg092b21c2017-11-15 16:20:07 -0800216
Eric Erfanianccca3152017-02-22 16:32:36 -0800217 /** Whether or not InCallService is bound to Telecom. */
218 private boolean mServiceBound = false;
219
220 /**
221 * When configuration changes Android kills the current activity and starts a new one. The flag is
222 * used to check if full clean up is necessary (activity is stopped and new activity won't be
223 * started), or if a new activity will be started right after the current one is destroyed, and
224 * therefore no need in release all resources.
225 */
226 private boolean mIsChangingConfigurations = false;
227
228 private boolean mAwaitingCallListUpdate = false;
229
230 private ExternalCallList.ExternalCallListener mExternalCallListener =
231 new ExternalCallList.ExternalCallListener() {
232
233 @Override
234 public void onExternalCallPulled(android.telecom.Call call) {
235 // Note: keep this code in sync with InCallPresenter#onCallAdded
236 LatencyReport latencyReport = new LatencyReport(call);
237 latencyReport.onCallBlockingDone();
238 // Note: External calls do not require spam checking.
239 mCallList.onCallAdded(mContext, call, latencyReport);
240 call.registerCallback(mCallCallback);
241 }
242
243 @Override
244 public void onExternalCallAdded(android.telecom.Call call) {
245 // No-op
246 }
247
248 @Override
249 public void onExternalCallRemoved(android.telecom.Call call) {
250 // No-op
251 }
252
253 @Override
254 public void onExternalCallUpdated(android.telecom.Call call) {
255 // No-op
256 }
257 };
258
259 private ThemeColorManager mThemeColorManager;
260 private VideoSurfaceTexture mLocalVideoSurfaceTexture;
261 private VideoSurfaceTexture mRemoteVideoSurfaceTexture;
262
Eric Erfanian10b34a52017-05-04 08:23:17 -0700263 /** Inaccessible constructor. Must use getRunningInstance() to get this singleton. */
Eric Erfanianccca3152017-02-22 16:32:36 -0800264 @VisibleForTesting
265 InCallPresenter() {}
266
267 public static synchronized InCallPresenter getInstance() {
268 if (sInCallPresenter == null) {
wangqic8cf79e2017-10-17 09:21:00 -0700269 Trace.beginSection("InCallPresenter.Constructor");
Eric Erfanianccca3152017-02-22 16:32:36 -0800270 sInCallPresenter = new InCallPresenter();
wangqic8cf79e2017-10-17 09:21:00 -0700271 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800272 }
273 return sInCallPresenter;
274 }
275
Eric Erfanian10b34a52017-05-04 08:23:17 -0700276 @VisibleForTesting
277 public static synchronized void setInstanceForTesting(InCallPresenter inCallPresenter) {
278 sInCallPresenter = inCallPresenter;
279 }
280
Eric Erfanianccca3152017-02-22 16:32:36 -0800281 /**
282 * Determines whether or not a call has no valid phone accounts that can be used to make the call
283 * with. Emergency calls do not require a phone account.
284 *
285 * @param call to check accounts for.
286 * @return {@code true} if the call has no call capable phone accounts set, {@code false} if the
287 * call contains a phone account that could be used to initiate it with, or is an emergency
288 * call.
289 */
290 public static boolean isCallWithNoValidAccounts(DialerCall call) {
291 if (call != null && !call.isEmergencyCall()) {
292 Bundle extras = call.getIntentExtras();
293
294 if (extras == null) {
295 extras = EMPTY_EXTRAS;
296 }
297
298 final List<PhoneAccountHandle> phoneAccountHandles =
299 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
300
301 if ((call.getAccountHandle() == null
302 && (phoneAccountHandles == null || phoneAccountHandles.isEmpty()))) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700303 LogUtil.i(
304 "InCallPresenter.isCallWithNoValidAccounts", "No valid accounts for call " + call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800305 return true;
306 }
307 }
308 return false;
309 }
310
311 public InCallState getInCallState() {
312 return mInCallState;
313 }
314
315 public CallList getCallList() {
316 return mCallList;
317 }
318
319 public void setUp(
320 @NonNull Context context,
321 CallList callList,
322 ExternalCallList externalCallList,
323 StatusBarNotifier statusBarNotifier,
324 ExternalCallNotifier externalCallNotifier,
325 ContactInfoCache contactInfoCache,
Eric Erfanian83b20212017-05-31 08:53:10 -0700326 ProximitySensor proximitySensor,
327 FilteredNumberAsyncQueryHandler filteredNumberQueryHandler) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700328 Trace.beginSection("InCallPresenter.setUp");
Eric Erfanianccca3152017-02-22 16:32:36 -0800329 if (mServiceConnected) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700330 LogUtil.i("InCallPresenter.setUp", "New service connection replacing existing one.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800331 if (context != mContext || callList != mCallList) {
332 throw new IllegalStateException();
333 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700334 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800335 return;
336 }
337
338 Objects.requireNonNull(context);
339 mContext = context;
340
341 mContactInfoCache = contactInfoCache;
342
343 mStatusBarNotifier = statusBarNotifier;
344 mExternalCallNotifier = externalCallNotifier;
345 addListener(mStatusBarNotifier);
Eric Erfaniand8046e52017-04-06 09:41:50 -0700346 EnrichedCallComponent.get(mContext)
347 .getEnrichedCallManager()
348 .registerStateChangedListener(mStatusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -0800349
350 mProximitySensor = proximitySensor;
351 addListener(mProximitySensor);
352
wangqi8d662ca2017-10-26 11:27:19 -0700353 if (mThemeColorManager == null) {
354 mThemeColorManager =
355 new ThemeColorManager(new InCallUIMaterialColorMapUtils(mContext.getResources()));
356 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800357
358 mCallList = callList;
359 mExternalCallList = externalCallList;
360 externalCallList.addExternalCallListener(mExternalCallNotifier);
361 externalCallList.addExternalCallListener(mExternalCallListener);
362
363 // This only gets called by the service so this is okay.
364 mServiceConnected = true;
365
366 // The final thing we do in this set up is add ourselves as a listener to CallList. This
367 // will kick off an update and the whole process can start.
368 mCallList.addListener(this);
369
370 // Create spam call list listener and add it to the list of listeners
zachh6a4cebd2017-10-24 17:10:06 -0700371 mSpamCallListListener =
372 new SpamCallListListener(
373 context, DialerExecutorComponent.get(context).dialerExecutorFactory());
Eric Erfanianccca3152017-02-22 16:32:36 -0800374 mCallList.addListener(mSpamCallListListener);
375
376 VideoPauseController.getInstance().setUp(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800377
Eric Erfanian83b20212017-05-31 08:53:10 -0700378 mFilteredQueryHandler = filteredNumberQueryHandler;
Eric Erfanianccca3152017-02-22 16:32:36 -0800379 mContext
380 .getSystemService(TelephonyManager.class)
381 .listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
382
yueg77cb8e52017-10-27 16:42:51 -0700383 AudioModeProvider.getInstance().addListener(this);
384
Eric Erfanian2ca43182017-08-31 06:57:16 -0700385 LogUtil.d("InCallPresenter.setUp", "Finished InCallPresenter.setUp");
386 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800387 }
388
389 /**
390 * Called when the telephony service has disconnected from us. This will happen when there are no
391 * more active calls. However, we may still want to continue showing the UI for certain cases like
392 * showing "Call Ended". What we really want is to wait for the activity and the service to both
393 * disconnect before we tear things down. This method sets a serviceConnected boolean and calls a
394 * secondary method that performs the aforementioned logic.
395 */
396 public void tearDown() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700397 LogUtil.d("InCallPresenter.tearDown", "tearDown");
Eric Erfanianccca3152017-02-22 16:32:36 -0800398 mCallList.clearOnDisconnect();
399
400 mServiceConnected = false;
401
402 mContext
403 .getSystemService(TelephonyManager.class)
404 .listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
405
406 attemptCleanup();
407 VideoPauseController.getInstance().tearDown();
yueg77cb8e52017-10-27 16:42:51 -0700408 AudioModeProvider.getInstance().removeListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800409 }
410
411 private void attemptFinishActivity() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700412 mScreenTimeoutEnabled = true;
Eric Erfanianccca3152017-02-22 16:32:36 -0800413 final boolean doFinish = (mInCallActivity != null && isActivityStarted());
Eric Erfanian2ca43182017-08-31 06:57:16 -0700414 LogUtil.i("InCallPresenter.attemptFinishActivity", "Hide in call UI: " + doFinish);
Eric Erfanianccca3152017-02-22 16:32:36 -0800415 if (doFinish) {
416 mInCallActivity.setExcludeFromRecents(true);
417 mInCallActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -0800418 }
419 }
420
421 /**
422 * Called when the UI ends. Attempts to tear down everything if necessary. See {@link #tearDown()}
423 * for more insight on the tear-down process.
424 */
425 public void unsetActivity(InCallActivity inCallActivity) {
426 if (inCallActivity == null) {
427 throw new IllegalArgumentException("unregisterActivity cannot be called with null");
428 }
429 if (mInCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700430 LogUtil.i(
431 "InCallPresenter.unsetActivity", "No InCallActivity currently set, no need to unset.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800432 return;
433 }
434 if (mInCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700435 LogUtil.w(
436 "InCallPresenter.unsetActivity",
Eric Erfanianccca3152017-02-22 16:32:36 -0800437 "Second instance of InCallActivity is trying to unregister when another"
438 + " instance is active. Ignoring.");
439 return;
440 }
441 updateActivity(null);
442 }
443
444 /**
445 * Updates the current instance of {@link InCallActivity} with the provided one. If a {@code null}
446 * activity is provided, it means that the activity was finished and we should attempt to cleanup.
447 */
448 private void updateActivity(InCallActivity inCallActivity) {
wangqi9982f0d2017-10-11 17:46:07 -0700449 Trace.beginSection("InCallPresenter.updateActivity");
Eric Erfanianccca3152017-02-22 16:32:36 -0800450 boolean updateListeners = false;
451 boolean doAttemptCleanup = false;
452
453 if (inCallActivity != null) {
454 if (mInCallActivity == null) {
Eric Erfanian938468d2017-10-24 14:05:52 -0700455 mContext = inCallActivity.getApplicationContext();
Eric Erfanianccca3152017-02-22 16:32:36 -0800456 updateListeners = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700457 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized");
Eric Erfanianccca3152017-02-22 16:32:36 -0800458 } else {
459 // since setActivity is called onStart(), it can be called multiple times.
460 // This is fine and ignorable, but we do not want to update the world every time
461 // this happens (like going to/from background) so we do not set updateListeners.
462 }
463
464 mInCallActivity = inCallActivity;
465 mInCallActivity.setExcludeFromRecents(false);
466
467 // By the time the UI finally comes up, the call may already be disconnected.
468 // If that's the case, we may need to show an error dialog.
469 if (mCallList != null && mCallList.getDisconnectedCall() != null) {
linyuh7b86f562017-11-16 11:24:09 -0800470 showDialogOrToastForDisconnectedCall(mCallList.getDisconnectedCall());
Eric Erfanianccca3152017-02-22 16:32:36 -0800471 }
472
473 // When the UI comes up, we need to first check the in-call state.
474 // If we are showing NO_CALLS, that means that a call probably connected and
475 // then immediately disconnected before the UI was able to come up.
476 // If we dont have any calls, start tearing down the UI instead.
477 // NOTE: This code relies on {@link #mInCallActivity} being set so we run it after
478 // it has been set.
479 if (mInCallState == InCallState.NO_CALLS) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700480 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized, but no calls left. Shut down");
Eric Erfanianccca3152017-02-22 16:32:36 -0800481 attemptFinishActivity();
wangqi9982f0d2017-10-11 17:46:07 -0700482 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800483 return;
484 }
485 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700486 LogUtil.i("InCallPresenter.updateActivity", "UI Destroyed");
Eric Erfanianccca3152017-02-22 16:32:36 -0800487 updateListeners = true;
488 mInCallActivity = null;
489
490 // We attempt cleanup for the destroy case but only after we recalculate the state
491 // to see if we need to come back up or stay shut down. This is why we do the
492 // cleanup after the call to onCallListChange() instead of directly here.
493 doAttemptCleanup = true;
494 }
495
496 // Messages can come from the telephony layer while the activity is coming up
497 // and while the activity is going down. So in both cases we need to recalculate what
498 // state we should be in after they complete.
499 // Examples: (1) A new incoming call could come in and then get disconnected before
500 // the activity is created.
501 // (2) All calls could disconnect and then get a new incoming call before the
502 // activity is destroyed.
503 //
Eric Erfanian938468d2017-10-24 14:05:52 -0700504 // a bug - We previously had a check for mServiceConnected here as well, but there are
Eric Erfanianccca3152017-02-22 16:32:36 -0800505 // cases where we need to recalculate the current state even if the service in not
506 // connected. In particular the case where startOrFinish() is called while the app is
507 // already finish()ing. In that case, we skip updating the state with the knowledge that
508 // we will check again once the activity has finished. That means we have to recalculate the
509 // state here even if the service is disconnected since we may not have finished a state
510 // transition while finish()ing.
511 if (updateListeners) {
512 onCallListChange(mCallList);
513 }
514
515 if (doAttemptCleanup) {
516 attemptCleanup();
517 }
wangqi9982f0d2017-10-11 17:46:07 -0700518 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800519 }
520
521 public void setManageConferenceActivity(
522 @Nullable ManageConferenceActivity manageConferenceActivity) {
523 mManageConferenceActivity = manageConferenceActivity;
524 }
525
526 public void onBringToForeground(boolean showDialpad) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700527 LogUtil.i("InCallPresenter.onBringToForeground", "Bringing UI to foreground.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800528 bringToForeground(showDialpad);
529 }
530
531 public void onCallAdded(final android.telecom.Call call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700532 Trace.beginSection("InCallPresenter.onCallAdded");
Eric Erfanianccca3152017-02-22 16:32:36 -0800533 LatencyReport latencyReport = new LatencyReport(call);
534 if (shouldAttemptBlocking(call)) {
535 maybeBlockCall(call, latencyReport);
536 } else {
537 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
538 mExternalCallList.onCallAdded(call);
539 } else {
540 latencyReport.onCallBlockingDone();
541 mCallList.onCallAdded(mContext, call, latencyReport);
542 }
543 }
544
545 // Since a call has been added we are no longer waiting for Telecom to send us a call.
546 setBoundAndWaitingForOutgoingCall(false, null);
547 call.registerCallback(mCallCallback);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700548 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800549 }
550
551 private boolean shouldAttemptBlocking(android.telecom.Call call) {
552 if (call.getState() != android.telecom.Call.STATE_RINGING) {
553 return false;
554 }
Eric Erfanian83b20212017-05-31 08:53:10 -0700555 if (!UserManagerCompat.isUserUnlocked(mContext)) {
556 LogUtil.i(
557 "InCallPresenter.shouldAttemptBlocking",
558 "not attempting to block incoming call because user is locked");
559 return false;
560 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800561 if (TelecomCallUtil.isEmergencyCall(call)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700562 LogUtil.i(
563 "InCallPresenter.shouldAttemptBlocking",
564 "Not attempting to block incoming emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800565 return false;
566 }
567 if (FilteredNumbersUtil.hasRecentEmergencyCall(mContext)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700568 LogUtil.i(
569 "InCallPresenter.shouldAttemptBlocking",
570 "Not attempting to block incoming call due to recent emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800571 return false;
572 }
573 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
574 return false;
575 }
Eric Erfanian83b20212017-05-31 08:53:10 -0700576 if (FilteredNumberCompat.useNewFiltering(mContext)) {
577 LogUtil.i(
578 "InCallPresenter.shouldAttemptBlocking",
579 "not attempting to block incoming call because framework blocking is in use");
580 return false;
581 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800582 return true;
583 }
584
585 /**
586 * Checks whether a call should be blocked, and blocks it if so. Otherwise, it adds the call to
587 * the CallList so it can proceed as normal. There is a timeout, so if the function for checking
588 * whether a function is blocked does not return in a reasonable time, we proceed with adding the
589 * call anyways.
590 */
591 private void maybeBlockCall(final android.telecom.Call call, final LatencyReport latencyReport) {
592 final String countryIso = GeoUtil.getCurrentCountryIso(mContext);
593 final String number = TelecomCallUtil.getNumber(call);
594 final long timeAdded = System.currentTimeMillis();
595
596 // Though AtomicBoolean's can be scary, don't fear, as in this case it is only used on the
597 // main UI thread. It is needed so we can change its value within different scopes, since
598 // that cannot be done with a final boolean.
599 final AtomicBoolean hasTimedOut = new AtomicBoolean(false);
600
601 final Handler handler = new Handler();
602
603 // Proceed if the query is slow; the call may still be blocked after the query returns.
604 final Runnable runnable =
605 new Runnable() {
606 @Override
607 public void run() {
608 hasTimedOut.set(true);
609 latencyReport.onCallBlockingDone();
610 mCallList.onCallAdded(mContext, call, latencyReport);
611 }
612 };
613 handler.postDelayed(runnable, BLOCK_QUERY_TIMEOUT_MS);
614
615 OnCheckBlockedListener onCheckBlockedListener =
616 new OnCheckBlockedListener() {
617 @Override
618 public void onCheckComplete(final Integer id) {
619 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700620 LogUtil.i("InCallPresenter.onCheckComplete", "torn down, not adding call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800621 return;
622 }
623 if (!hasTimedOut.get()) {
624 handler.removeCallbacks(runnable);
625 }
626 if (id == null) {
627 if (!hasTimedOut.get()) {
628 latencyReport.onCallBlockingDone();
629 mCallList.onCallAdded(mContext, call, latencyReport);
630 }
631 } else if (id == FilteredNumberAsyncQueryHandler.INVALID_ID) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700632 LogUtil.d(
633 "InCallPresenter.onCheckComplete", "invalid number, skipping block checking");
Eric Erfanianccca3152017-02-22 16:32:36 -0800634 if (!hasTimedOut.get()) {
635 handler.removeCallbacks(runnable);
636
637 latencyReport.onCallBlockingDone();
638 mCallList.onCallAdded(mContext, call, latencyReport);
639 }
640 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700641 LogUtil.i(
642 "InCallPresenter.onCheckComplete", "Rejecting incoming call from blocked number");
Eric Erfanianccca3152017-02-22 16:32:36 -0800643 call.reject(false, null);
644 Logger.get(mContext).logInteraction(InteractionEvent.Type.CALL_BLOCKED);
645
646 /*
647 * If mContext is null, then the InCallPresenter was torn down before the
648 * block check had a chance to complete. The context is no longer valid, so
649 * don't attempt to remove the call log entry.
650 */
651 if (mContext == null) {
652 return;
653 }
654 // Register observer to update the call log.
655 // BlockedNumberContentObserver will unregister after successful log or timeout.
656 BlockedNumberContentObserver contentObserver =
657 new BlockedNumberContentObserver(mContext, new Handler(), number, timeAdded);
658 contentObserver.register();
659 }
660 }
661 };
662
663 mFilteredQueryHandler.isBlockedNumber(onCheckBlockedListener, number, countryIso);
664 }
665
666 public void onCallRemoved(android.telecom.Call call) {
667 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
668 mExternalCallList.onCallRemoved(call);
669 } else {
670 mCallList.onCallRemoved(mContext, call);
671 call.unregisterCallback(mCallCallback);
672 }
673 }
674
675 public void onCanAddCallChanged(boolean canAddCall) {
676 for (CanAddCallListener listener : mCanAddCallListeners) {
677 listener.onCanAddCallChanged(canAddCall);
678 }
679 }
680
681 @Override
682 public void onWiFiToLteHandover(DialerCall call) {
683 if (mInCallActivity != null) {
linyuh7b86f562017-11-16 11:24:09 -0800684 mInCallActivity.showToastForWiFiToLteHandover(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800685 }
686 }
687
688 @Override
689 public void onHandoverToWifiFailed(DialerCall call) {
690 if (mInCallActivity != null) {
linyuh7b86f562017-11-16 11:24:09 -0800691 mInCallActivity.showDialogOrToastForWifiHandoverFailure(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800692 }
693 }
694
Eric Erfanianc857f902017-05-15 14:05:33 -0700695 @Override
696 public void onInternationalCallOnWifi(@NonNull DialerCall call) {
697 LogUtil.enterBlock("InCallPresenter.onInternationalCallOnWifi");
698 if (mInCallActivity != null) {
linyuh7b86f562017-11-16 11:24:09 -0800699 mInCallActivity.showDialogForInternationalCallOnWifi(call);
Eric Erfanianc857f902017-05-15 14:05:33 -0700700 }
701 }
702
Eric Erfanianccca3152017-02-22 16:32:36 -0800703 /**
704 * Called when there is a change to the call list. Sets the In-Call state for the entire in-call
705 * app based on the information it gets from CallList. Dispatches the in-call state to all
706 * listeners. Can trigger the creation or destruction of the UI based on the states that is
707 * calculates.
708 */
709 @Override
710 public void onCallListChange(CallList callList) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700711 Trace.beginSection("InCallPresenter.onCallListChange");
Eric Erfanianccca3152017-02-22 16:32:36 -0800712 if (mInCallActivity != null && mInCallActivity.isInCallScreenAnimating()) {
713 mAwaitingCallListUpdate = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700714 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800715 return;
716 }
717 if (callList == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700718 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800719 return;
720 }
721
722 mAwaitingCallListUpdate = false;
723
724 InCallState newState = getPotentialStateFromCallList(callList);
725 InCallState oldState = mInCallState;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700726 LogUtil.d(
727 "InCallPresenter.onCallListChange",
728 "onCallListChange oldState= " + oldState + " newState=" + newState);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700729
730 // If the user placed a call and was asked to choose the account, but then pressed "Home", the
731 // incall activity for that call will still exist (even if it's not visible). In the case of
732 // an incoming call in that situation, just disconnect that "waiting for account" call and
733 // dismiss the dialog. The same activity will be reused to handle the new incoming call. See
Eric Erfanian938468d2017-10-24 14:05:52 -0700734 // a bug for more details.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700735 DialerCall waitingForAccountCall;
736 if (newState == InCallState.INCOMING
737 && (waitingForAccountCall = callList.getWaitingForAccountCall()) != null) {
738 waitingForAccountCall.disconnect();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700739 // The InCallActivity might be destroyed or not started yet at this point.
740 if (isActivityStarted()) {
741 mInCallActivity.dismissPendingDialogs();
742 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700743 }
744
Eric Erfanianccca3152017-02-22 16:32:36 -0800745 newState = startOrFinishUi(newState);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700746 LogUtil.d(
747 "InCallPresenter.onCallListChange", "onCallListChange newState changed to " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800748
749 // Set the new state before announcing it to the world
Eric Erfanian2ca43182017-08-31 06:57:16 -0700750 LogUtil.i(
751 "InCallPresenter.onCallListChange",
752 "Phone switching state: " + oldState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800753 mInCallState = newState;
754
755 // notify listeners of new state
756 for (InCallStateListener listener : mListeners) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700757 LogUtil.d(
758 "InCallPresenter.onCallListChange",
759 "Notify " + listener + " of state " + mInCallState.toString());
Eric Erfanianccca3152017-02-22 16:32:36 -0800760 listener.onStateChange(oldState, mInCallState, callList);
761 }
762
763 if (isActivityStarted()) {
764 final boolean hasCall =
765 callList.getActiveOrBackgroundCall() != null || callList.getOutgoingCall() != null;
766 mInCallActivity.dismissKeyguard(hasCall);
767 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700768 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800769 }
770
771 /** Called when there is a new incoming call. */
772 @Override
773 public void onIncomingCall(DialerCall call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700774 Trace.beginSection("InCallPresenter.onIncomingCall");
Eric Erfanianccca3152017-02-22 16:32:36 -0800775 InCallState newState = startOrFinishUi(InCallState.INCOMING);
776 InCallState oldState = mInCallState;
777
Eric Erfanian2ca43182017-08-31 06:57:16 -0700778 LogUtil.i(
779 "InCallPresenter.onIncomingCall", "Phone switching state: " + oldState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800780 mInCallState = newState;
781
wangqicf61ca02017-08-31 15:32:55 -0700782 Trace.beginSection("listener.onIncomingCall");
Eric Erfanianccca3152017-02-22 16:32:36 -0800783 for (IncomingCallListener listener : mIncomingCallListeners) {
784 listener.onIncomingCall(oldState, mInCallState, call);
785 }
wangqicf61ca02017-08-31 15:32:55 -0700786 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800787
wangqicf61ca02017-08-31 15:32:55 -0700788 Trace.beginSection("onPrimaryCallStateChanged");
Eric Erfanianccca3152017-02-22 16:32:36 -0800789 if (mInCallActivity != null) {
790 // Re-evaluate which fragment is being shown.
791 mInCallActivity.onPrimaryCallStateChanged();
792 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700793 Trace.endSection();
wangqicf61ca02017-08-31 15:32:55 -0700794 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800795 }
796
797 @Override
798 public void onUpgradeToVideo(DialerCall call) {
Eric Erfanian90508232017-03-24 09:31:16 -0700799 if (VideoUtils.hasReceivedVideoUpgradeRequest(call.getVideoTech().getSessionModificationState())
Eric Erfanianccca3152017-02-22 16:32:36 -0800800 && mInCallState == InCallPresenter.InCallState.INCOMING) {
801 LogUtil.i(
802 "InCallPresenter.onUpgradeToVideo",
803 "rejecting upgrade request due to existing incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700804 call.getVideoTech().declineVideoRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -0800805 }
806
807 if (mInCallActivity != null) {
808 // Re-evaluate which fragment is being shown.
809 mInCallActivity.onPrimaryCallStateChanged();
810 }
811 }
812
813 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700814 public void onSessionModificationStateChange(DialerCall call) {
815 int newState = call.getVideoTech().getSessionModificationState();
Eric Erfanianccca3152017-02-22 16:32:36 -0800816 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "state: %d", newState);
817 if (mProximitySensor == null) {
818 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "proximitySensor is null");
819 return;
820 }
821 mProximitySensor.setIsAttemptingVideoCall(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700822 call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest());
Eric Erfanianccca3152017-02-22 16:32:36 -0800823 if (mInCallActivity != null) {
824 // Re-evaluate which fragment is being shown.
825 mInCallActivity.onPrimaryCallStateChanged();
826 }
827 }
828
829 /**
830 * Called when a call becomes disconnected. Called everytime an existing call changes from being
831 * connected (incoming/outgoing/active) to disconnected.
832 */
833 @Override
834 public void onDisconnect(DialerCall call) {
linyuh7b86f562017-11-16 11:24:09 -0800835 showDialogOrToastForDisconnectedCall(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800836
837 // We need to do the run the same code as onCallListChange.
838 onCallListChange(mCallList);
839
840 if (isActivityStarted()) {
841 mInCallActivity.dismissKeyguard(false);
842 }
843
844 if (call.isEmergencyCall()) {
845 FilteredNumbersUtil.recordLastEmergencyCallTime(mContext);
846 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800847
Eric Erfanianfc37b022017-03-21 10:11:17 -0700848 if (!mCallList.hasLiveCall()
849 && !call.getLogState().isIncoming
Eric Erfanian10b34a52017-05-04 08:23:17 -0700850 && !isSecretCode(call.getNumber())
wangqi9982f0d2017-10-11 17:46:07 -0700851 && !call.isVoiceMailNumber()) {
Eric Erfanian10b34a52017-05-04 08:23:17 -0700852 PostCall.onCallDisconnected(mContext, call.getNumber(), call.getConnectTimeMillis());
Eric Erfanianccca3152017-02-22 16:32:36 -0800853 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800854 }
855
Eric Erfanian10b34a52017-05-04 08:23:17 -0700856 private boolean isSecretCode(@Nullable String number) {
857 return number != null
858 && (number.length() <= 8 || number.startsWith("*#*#") || number.endsWith("#*#*"));
859 }
860
Eric Erfanianccca3152017-02-22 16:32:36 -0800861 /** Given the call list, return the state in which the in-call screen should be. */
862 public InCallState getPotentialStateFromCallList(CallList callList) {
863
864 InCallState newState = InCallState.NO_CALLS;
865
866 if (callList == null) {
867 return newState;
868 }
869 if (callList.getIncomingCall() != null) {
870 newState = InCallState.INCOMING;
871 } else if (callList.getWaitingForAccountCall() != null) {
872 newState = InCallState.WAITING_FOR_ACCOUNT;
873 } else if (callList.getPendingOutgoingCall() != null) {
874 newState = InCallState.PENDING_OUTGOING;
875 } else if (callList.getOutgoingCall() != null) {
876 newState = InCallState.OUTGOING;
877 } else if (callList.getActiveCall() != null
878 || callList.getBackgroundCall() != null
879 || callList.getDisconnectedCall() != null
880 || callList.getDisconnectingCall() != null) {
881 newState = InCallState.INCALL;
882 }
883
884 if (newState == InCallState.NO_CALLS) {
885 if (mBoundAndWaitingForOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700886 return InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -0800887 }
888 }
889
890 return newState;
891 }
892
893 public boolean isBoundAndWaitingForOutgoingCall() {
894 return mBoundAndWaitingForOutgoingCall;
895 }
896
897 public void setBoundAndWaitingForOutgoingCall(boolean isBound, PhoneAccountHandle handle) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700898 LogUtil.i(
899 "InCallPresenter.setBoundAndWaitingForOutgoingCall",
900 "setBoundAndWaitingForOutgoingCall: " + isBound);
Eric Erfanianccca3152017-02-22 16:32:36 -0800901 mBoundAndWaitingForOutgoingCall = isBound;
902 mThemeColorManager.setPendingPhoneAccountHandle(handle);
903 if (isBound && mInCallState == InCallState.NO_CALLS) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700904 mInCallState = InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -0800905 }
906 }
907
908 public void onShrinkAnimationComplete() {
909 if (mAwaitingCallListUpdate) {
910 onCallListChange(mCallList);
911 }
912 }
913
914 public void addIncomingCallListener(IncomingCallListener listener) {
915 Objects.requireNonNull(listener);
916 mIncomingCallListeners.add(listener);
917 }
918
919 public void removeIncomingCallListener(IncomingCallListener listener) {
920 if (listener != null) {
921 mIncomingCallListeners.remove(listener);
922 }
923 }
924
925 public void addListener(InCallStateListener listener) {
926 Objects.requireNonNull(listener);
927 mListeners.add(listener);
928 }
929
930 public void removeListener(InCallStateListener listener) {
931 if (listener != null) {
932 mListeners.remove(listener);
933 }
934 }
935
936 public void addDetailsListener(InCallDetailsListener listener) {
937 Objects.requireNonNull(listener);
938 mDetailsListeners.add(listener);
939 }
940
941 public void removeDetailsListener(InCallDetailsListener listener) {
942 if (listener != null) {
943 mDetailsListeners.remove(listener);
944 }
945 }
946
947 public void addCanAddCallListener(CanAddCallListener listener) {
948 Objects.requireNonNull(listener);
949 mCanAddCallListeners.add(listener);
950 }
951
952 public void removeCanAddCallListener(CanAddCallListener listener) {
953 if (listener != null) {
954 mCanAddCallListeners.remove(listener);
955 }
956 }
957
958 public void addOrientationListener(InCallOrientationListener listener) {
959 Objects.requireNonNull(listener);
960 mOrientationListeners.add(listener);
961 }
962
963 public void removeOrientationListener(InCallOrientationListener listener) {
964 if (listener != null) {
965 mOrientationListeners.remove(listener);
966 }
967 }
968
969 public void addInCallEventListener(InCallEventListener listener) {
970 Objects.requireNonNull(listener);
971 mInCallEventListeners.add(listener);
972 }
973
974 public void removeInCallEventListener(InCallEventListener listener) {
975 if (listener != null) {
976 mInCallEventListeners.remove(listener);
977 }
978 }
979
980 public ProximitySensor getProximitySensor() {
981 return mProximitySensor;
982 }
983
984 public PseudoScreenState getPseudoScreenState() {
985 return mPseudoScreenState;
986 }
987
988 /** Returns true if the incall app is the foreground application. */
989 public boolean isShowingInCallUi() {
990 if (!isActivityStarted()) {
991 return false;
992 }
993 if (mManageConferenceActivity != null && mManageConferenceActivity.isVisible()) {
994 return true;
995 }
996 return mInCallActivity.isVisible();
997 }
998
999 /**
1000 * Returns true if the activity has been created and is running. Returns true as long as activity
1001 * is not destroyed or finishing. This ensures that we return true even if the activity is paused
1002 * (not in foreground).
1003 */
1004 public boolean isActivityStarted() {
1005 return (mInCallActivity != null
1006 && !mInCallActivity.isDestroyed()
1007 && !mInCallActivity.isFinishing());
1008 }
1009
1010 /**
1011 * Determines if the In-Call app is currently changing configuration.
1012 *
1013 * @return {@code true} if the In-Call app is changing configuration.
1014 */
1015 public boolean isChangingConfigurations() {
1016 return mIsChangingConfigurations;
1017 }
1018
1019 /**
1020 * Tracks whether the In-Call app is currently in the process of changing configuration (i.e.
1021 * screen orientation).
1022 */
1023 /*package*/
1024 void updateIsChangingConfigurations() {
1025 mIsChangingConfigurations = false;
1026 if (mInCallActivity != null) {
1027 mIsChangingConfigurations = mInCallActivity.isChangingConfigurations();
1028 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001029 LogUtil.v(
1030 "InCallPresenter.updateIsChangingConfigurations",
1031 "updateIsChangingConfigurations = " + mIsChangingConfigurations);
Eric Erfanianccca3152017-02-22 16:32:36 -08001032 }
1033
1034 /** Called when the activity goes in/out of the foreground. */
1035 public void onUiShowing(boolean showing) {
1036 // We need to update the notification bar when we leave the UI because that
1037 // could trigger it to show again.
1038 if (mStatusBarNotifier != null) {
wangqic8cf79e2017-10-17 09:21:00 -07001039 mStatusBarNotifier.updateNotification();
Eric Erfanianccca3152017-02-22 16:32:36 -08001040 }
1041
1042 if (mProximitySensor != null) {
1043 mProximitySensor.onInCallShowing(showing);
1044 }
1045
yueg092b21c2017-11-15 16:20:07 -08001046 if (!showing) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001047 updateIsChangingConfigurations();
1048 }
1049
1050 for (InCallUiListener listener : mInCallUiListeners) {
1051 listener.onUiShowing(showing);
1052 }
1053
1054 if (mInCallActivity != null) {
1055 // Re-evaluate which fragment is being shown.
1056 mInCallActivity.onPrimaryCallStateChanged();
1057 }
1058 }
1059
Eric Erfanian2ca43182017-08-31 06:57:16 -07001060 public void refreshUi() {
1061 if (mInCallActivity != null) {
1062 // Re-evaluate which fragment is being shown.
1063 mInCallActivity.onPrimaryCallStateChanged();
1064 }
1065 }
1066
Eric Erfanianccca3152017-02-22 16:32:36 -08001067 public void addInCallUiListener(InCallUiListener listener) {
1068 mInCallUiListeners.add(listener);
1069 }
1070
1071 public boolean removeInCallUiListener(InCallUiListener listener) {
1072 return mInCallUiListeners.remove(listener);
1073 }
1074
1075 /*package*/
1076 void onActivityStarted() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001077 LogUtil.d("InCallPresenter.onActivityStarted", "onActivityStarted");
Eric Erfanianccca3152017-02-22 16:32:36 -08001078 notifyVideoPauseController(true);
Eric Erfanian2ca43182017-08-31 06:57:16 -07001079 applyScreenTimeout();
Eric Erfanianccca3152017-02-22 16:32:36 -08001080 }
1081
1082 /*package*/
1083 void onActivityStopped() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001084 LogUtil.d("InCallPresenter.onActivityStopped", "onActivityStopped");
Eric Erfanianccca3152017-02-22 16:32:36 -08001085 notifyVideoPauseController(false);
1086 }
1087
1088 private void notifyVideoPauseController(boolean showing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001089 LogUtil.d(
1090 "InCallPresenter.notifyVideoPauseController",
1091 "mIsChangingConfigurations=" + mIsChangingConfigurations);
Eric Erfanianccca3152017-02-22 16:32:36 -08001092 if (!mIsChangingConfigurations) {
1093 VideoPauseController.getInstance().onUiShowing(showing);
1094 }
1095 }
1096
1097 /** Brings the app into the foreground if possible. */
1098 public void bringToForeground(boolean showDialpad) {
1099 // Before we bring the incall UI to the foreground, we check to see if:
1100 // 1. It is not currently in the foreground
1101 // 2. We are in a state where we want to show the incall ui (i.e. there are calls to
1102 // be displayed)
1103 // If the activity hadn't actually been started previously, yet there are still calls
1104 // present (e.g. a call was accepted by a bluetooth or wired headset), we want to
1105 // bring it up the UI regardless.
1106 if (!isShowingInCallUi() && mInCallState != InCallState.NO_CALLS) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001107 showInCall(showDialpad, false /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001108 }
1109 }
1110
1111 public void onPostDialCharWait(String callId, String chars) {
1112 if (isActivityStarted()) {
linyuhc3968e62017-11-20 17:40:50 -08001113 mInCallActivity.showDialogForPostCharWait(callId, chars);
Eric Erfanianccca3152017-02-22 16:32:36 -08001114 }
1115 }
1116
1117 /**
1118 * Handles the green CALL key while in-call.
1119 *
1120 * @return true if we consumed the event.
1121 */
1122 public boolean handleCallKey() {
1123 LogUtil.v("InCallPresenter.handleCallKey", null);
1124
1125 // The green CALL button means either "Answer", "Unhold", or
1126 // "Swap calls", or can be a no-op, depending on the current state
1127 // of the Phone.
1128
1129 /** INCOMING CALL */
1130 final CallList calls = mCallList;
1131 final DialerCall incomingCall = calls.getIncomingCall();
1132 LogUtil.v("InCallPresenter.handleCallKey", "incomingCall: " + incomingCall);
1133
1134 // (1) Attempt to answer a call
1135 if (incomingCall != null) {
1136 incomingCall.answer(VideoProfile.STATE_AUDIO_ONLY);
1137 return true;
1138 }
1139
1140 /** STATE_ACTIVE CALL */
1141 final DialerCall activeCall = calls.getActiveCall();
1142 if (activeCall != null) {
1143 // TODO: This logic is repeated from CallButtonPresenter.java. We should
1144 // consolidate this logic.
1145 final boolean canMerge =
1146 activeCall.can(android.telecom.Call.Details.CAPABILITY_MERGE_CONFERENCE);
1147 final boolean canSwap =
1148 activeCall.can(android.telecom.Call.Details.CAPABILITY_SWAP_CONFERENCE);
1149
Eric Erfanian2ca43182017-08-31 06:57:16 -07001150 LogUtil.v(
1151 "InCallPresenter.handleCallKey",
1152 "activeCall: " + activeCall + ", canMerge: " + canMerge + ", canSwap: " + canSwap);
Eric Erfanianccca3152017-02-22 16:32:36 -08001153
1154 // (2) Attempt actions on conference calls
1155 if (canMerge) {
1156 TelecomAdapter.getInstance().merge(activeCall.getId());
1157 return true;
1158 } else if (canSwap) {
1159 TelecomAdapter.getInstance().swap(activeCall.getId());
1160 return true;
1161 }
1162 }
1163
1164 /** BACKGROUND CALL */
1165 final DialerCall heldCall = calls.getBackgroundCall();
1166 if (heldCall != null) {
1167 // We have a hold call so presumeable it will always support HOLD...but
1168 // there is no harm in double checking.
1169 final boolean canHold = heldCall.can(android.telecom.Call.Details.CAPABILITY_HOLD);
1170
Eric Erfanian2ca43182017-08-31 06:57:16 -07001171 LogUtil.v("InCallPresenter.handleCallKey", "heldCall: " + heldCall + ", canHold: " + canHold);
Eric Erfanianccca3152017-02-22 16:32:36 -08001172
1173 // (4) unhold call
1174 if (heldCall.getState() == DialerCall.State.ONHOLD && canHold) {
1175 heldCall.unhold();
1176 return true;
1177 }
1178 }
1179
1180 // Always consume hard keys
1181 return true;
1182 }
1183
Eric Erfanianccca3152017-02-22 16:32:36 -08001184 /** Clears the previous fullscreen state. */
1185 public void clearFullscreen() {
1186 mIsFullScreen = false;
1187 }
1188
1189 /**
1190 * Changes the fullscreen mode of the in-call UI.
1191 *
1192 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1193 * otherwise.
1194 */
1195 public void setFullScreen(boolean isFullScreen) {
1196 setFullScreen(isFullScreen, false /* force */);
1197 }
1198
1199 /**
1200 * Changes the fullscreen mode of the in-call UI.
1201 *
1202 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1203 * otherwise.
1204 * @param force {@code true} if fullscreen mode should be set regardless of its current state.
1205 */
1206 public void setFullScreen(boolean isFullScreen, boolean force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001207 LogUtil.i("InCallPresenter.setFullScreen", "setFullScreen = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001208
1209 // As a safeguard, ensure we cannot enter fullscreen if the dialpad is shown.
1210 if (isDialpadVisible()) {
1211 isFullScreen = false;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001212 LogUtil.v(
1213 "InCallPresenter.setFullScreen",
1214 "setFullScreen overridden as dialpad is shown = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001215 }
1216
1217 if (mIsFullScreen == isFullScreen && !force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001218 LogUtil.v("InCallPresenter.setFullScreen", "setFullScreen ignored as already in that state.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001219 return;
1220 }
1221 mIsFullScreen = isFullScreen;
1222 notifyFullscreenModeChange(mIsFullScreen);
1223 }
1224
1225 /**
1226 * @return {@code true} if the in-call ui is currently in fullscreen mode, {@code false}
1227 * otherwise.
1228 */
1229 public boolean isFullscreen() {
1230 return mIsFullScreen;
1231 }
1232
1233 /**
1234 * Called by the {@link VideoCallPresenter} to inform of a change in full screen video status.
1235 *
1236 * @param isFullscreenMode {@code True} if entering full screen mode.
1237 */
1238 public void notifyFullscreenModeChange(boolean isFullscreenMode) {
1239 for (InCallEventListener listener : mInCallEventListeners) {
1240 listener.onFullscreenModeChanged(isFullscreenMode);
1241 }
1242 }
1243
linyuh7b86f562017-11-16 11:24:09 -08001244 /** Instruct the in-call activity to show an error dialog or toast for a disconnected call. */
1245 private void showDialogOrToastForDisconnectedCall(DialerCall call) {
1246 if (!isActivityStarted() || call.getState() != DialerCall.State.DISCONNECTED) {
1247 return;
Eric Erfanianccca3152017-02-22 16:32:36 -08001248 }
linyuh7b86f562017-11-16 11:24:09 -08001249
1250 // For newly disconnected calls, we may want to show a dialog on specific error conditions
1251 if (call.getAccountHandle() == null && !call.isConferenceCall()) {
1252 setDisconnectCauseForMissingAccounts(call);
1253 }
1254
1255 mInCallActivity.showDialogOrToastForDisconnectedCall(
1256 new DisconnectMessage(mInCallActivity, call));
Eric Erfanianccca3152017-02-22 16:32:36 -08001257 }
1258
1259 /**
1260 * When the state of in-call changes, this is the first method to get called. It determines if the
1261 * UI needs to be started or finished depending on the new state and does it.
1262 */
1263 private InCallState startOrFinishUi(InCallState newState) {
wangqicf61ca02017-08-31 15:32:55 -07001264 Trace.beginSection("InCallPresenter.startOrFinishUi");
Eric Erfanian2ca43182017-08-31 06:57:16 -07001265 LogUtil.d(
1266 "InCallPresenter.startOrFinishUi", "startOrFinishUi: " + mInCallState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -08001267
1268 // TODO: Consider a proper state machine implementation
1269
1270 // If the state isn't changing we have already done any starting/stopping of activities in
1271 // a previous pass...so lets cut out early
1272 if (newState == mInCallState) {
wangqicf61ca02017-08-31 15:32:55 -07001273 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001274 return newState;
1275 }
1276
1277 // A new Incoming call means that the user needs to be notified of the the call (since
1278 // it wasn't them who initiated it). We do this through full screen notifications and
1279 // happens indirectly through {@link StatusBarNotifier}.
1280 //
1281 // The process for incoming calls is as follows:
1282 //
1283 // 1) CallList - Announces existence of new INCOMING call
1284 // 2) InCallPresenter - Gets announcement and calculates that the new InCallState
1285 // - should be set to INCOMING.
1286 // 3) InCallPresenter - This method is called to see if we need to start or finish
1287 // the app given the new state.
1288 // 4) StatusBarNotifier - Listens to InCallState changes. InCallPresenter calls
1289 // StatusBarNotifier explicitly to issue a FullScreen Notification
1290 // that will either start the InCallActivity or show the user a
1291 // top-level notification dialog if the user is in an immersive app.
1292 // That notification can also start the InCallActivity.
1293 // 5) InCallActivity - Main activity starts up and at the end of its onCreate will
1294 // call InCallPresenter::setActivity() to let the presenter
1295 // know that start-up is complete.
1296 //
1297 // [ AND NOW YOU'RE IN THE CALL. voila! ]
1298 //
1299 // Our app is started using a fullScreen notification. We need to do this whenever
1300 // we get an incoming call. Depending on the current context of the device, either a
1301 // incoming call HUN or the actual InCallActivity will be shown.
1302 final boolean startIncomingCallSequence = (InCallState.INCOMING == newState);
1303
1304 // A dialog to show on top of the InCallUI to select a PhoneAccount
1305 final boolean showAccountPicker = (InCallState.WAITING_FOR_ACCOUNT == newState);
1306
1307 // A new outgoing call indicates that the user just now dialed a number and when that
1308 // happens we need to display the screen immediately or show an account picker dialog if
1309 // no default is set. However, if the main InCallUI is already visible, we do not want to
1310 // re-initiate the start-up animation, so we do not need to do anything here.
1311 //
1312 // It is also possible to go into an intermediate state where the call has been initiated
1313 // but Telecom has not yet returned with the details of the call (handle, gateway, etc.).
1314 // This pending outgoing state can also launch the call screen.
1315 //
1316 // This is different from the incoming call sequence because we do not need to shock the
1317 // user with a top-level notification. Just show the call UI normally.
1318 boolean callCardFragmentVisible =
1319 mInCallActivity != null && mInCallActivity.getCallCardFragmentVisible();
1320 final boolean mainUiNotVisible = !isShowingInCallUi() || !callCardFragmentVisible;
1321 boolean showCallUi = InCallState.OUTGOING == newState && mainUiNotVisible;
1322
1323 // Direct transition from PENDING_OUTGOING -> INCALL means that there was an error in the
1324 // outgoing call process, so the UI should be brought up to show an error dialog.
1325 showCallUi |=
1326 (InCallState.PENDING_OUTGOING == mInCallState
1327 && InCallState.INCALL == newState
1328 && !isShowingInCallUi());
1329
1330 // Another exception - InCallActivity is in charge of disconnecting a call with no
1331 // valid accounts set. Bring the UI up if this is true for the current pending outgoing
1332 // call so that:
1333 // 1) The call can be disconnected correctly
1334 // 2) The UI comes up and correctly displays the error dialog.
1335 // TODO: Remove these special case conditions by making InCallPresenter a true state
1336 // machine. Telecom should also be the component responsible for disconnecting a call
1337 // with no valid accounts.
1338 showCallUi |=
1339 InCallState.PENDING_OUTGOING == newState
1340 && mainUiNotVisible
1341 && isCallWithNoValidAccounts(mCallList.getPendingOutgoingCall());
1342
1343 // The only time that we have an instance of mInCallActivity and it isn't started is
1344 // when it is being destroyed. In that case, lets avoid bringing up another instance of
1345 // the activity. When it is finally destroyed, we double check if we should bring it back
1346 // up so we aren't going to lose anything by avoiding a second startup here.
1347 boolean activityIsFinishing = mInCallActivity != null && !isActivityStarted();
1348 if (activityIsFinishing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001349 LogUtil.i(
1350 "InCallPresenter.startOrFinishUi",
1351 "Undo the state change: " + newState + " -> " + mInCallState);
wangqicf61ca02017-08-31 15:32:55 -07001352 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001353 return mInCallState;
1354 }
1355
1356 // We're about the bring up the in-call UI for outgoing and incoming call. If we still have
1357 // dialogs up, we need to clear them out before showing in-call screen. This is necessary
1358 // to fix the bug that dialog will show up when data reaches limit even after makeing new
1359 // outgoing call after user ignore it by pressing home button.
1360 if ((newState == InCallState.INCOMING || newState == InCallState.PENDING_OUTGOING)
1361 && !showCallUi
1362 && isActivityStarted()) {
1363 mInCallActivity.dismissPendingDialogs();
1364 }
1365
1366 if (showCallUi || showAccountPicker) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001367 LogUtil.i("InCallPresenter.startOrFinishUi", "Start in call UI");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001368 showInCall(false /* showDialpad */, !showAccountPicker /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001369 } else if (startIncomingCallSequence) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001370 LogUtil.i("InCallPresenter.startOrFinishUi", "Start Full Screen in call UI");
Eric Erfanianccca3152017-02-22 16:32:36 -08001371
wangqic8cf79e2017-10-17 09:21:00 -07001372 mStatusBarNotifier.updateNotification();
Eric Erfanianccca3152017-02-22 16:32:36 -08001373 } else if (newState == InCallState.NO_CALLS) {
1374 // The new state is the no calls state. Tear everything down.
1375 attemptFinishActivity();
1376 attemptCleanup();
1377 }
1378
wangqicf61ca02017-08-31 15:32:55 -07001379 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001380 return newState;
1381 }
1382
1383 /**
1384 * Sets the DisconnectCause for a call that was disconnected because it was missing a PhoneAccount
1385 * or PhoneAccounts to select from.
1386 */
1387 private void setDisconnectCauseForMissingAccounts(DialerCall call) {
1388
1389 Bundle extras = call.getIntentExtras();
1390 // Initialize the extras bundle to avoid NPE
1391 if (extras == null) {
1392 extras = new Bundle();
1393 }
1394
1395 final List<PhoneAccountHandle> phoneAccountHandles =
1396 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
1397
1398 if (phoneAccountHandles == null || phoneAccountHandles.isEmpty()) {
1399 String scheme = call.getHandle().getScheme();
1400 final String errorMsg =
1401 PhoneAccount.SCHEME_TEL.equals(scheme)
1402 ? mContext.getString(R.string.callFailed_simError)
1403 : mContext.getString(R.string.incall_error_supp_service_unknown);
1404 DisconnectCause disconnectCause =
1405 new DisconnectCause(DisconnectCause.ERROR, null, errorMsg, errorMsg);
1406 call.setDisconnectCause(disconnectCause);
1407 }
1408 }
1409
Eric Erfanianccca3152017-02-22 16:32:36 -08001410 /**
1411 * @return {@code true} if the InCallPresenter is ready to be torn down, {@code false} otherwise.
1412 * Calling classes should use this as an indication whether to interact with the
1413 * InCallPresenter or not.
1414 */
1415 public boolean isReadyForTearDown() {
1416 return mInCallActivity == null && !mServiceConnected && mInCallState == InCallState.NO_CALLS;
1417 }
1418
1419 /**
1420 * Checks to see if both the UI is gone and the service is disconnected. If so, tear it all down.
1421 */
1422 private void attemptCleanup() {
1423 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001424 LogUtil.i("InCallPresenter.attemptCleanup", "Cleaning up");
Eric Erfanianccca3152017-02-22 16:32:36 -08001425
1426 cleanupSurfaces();
1427
Eric Erfanianccca3152017-02-22 16:32:36 -08001428 mIsChangingConfigurations = false;
1429
1430 // blow away stale contact info so that we get fresh data on
1431 // the next set of calls
1432 if (mContactInfoCache != null) {
1433 mContactInfoCache.clearCache();
1434 }
1435 mContactInfoCache = null;
1436
1437 if (mProximitySensor != null) {
1438 removeListener(mProximitySensor);
1439 mProximitySensor.tearDown();
1440 }
1441 mProximitySensor = null;
1442
1443 if (mStatusBarNotifier != null) {
1444 removeListener(mStatusBarNotifier);
Eric Erfaniand8046e52017-04-06 09:41:50 -07001445 EnrichedCallComponent.get(mContext)
1446 .getEnrichedCallManager()
1447 .unregisterStateChangedListener(mStatusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001448 }
Eric Erfaniand8046e52017-04-06 09:41:50 -07001449
Eric Erfanianccca3152017-02-22 16:32:36 -08001450 if (mExternalCallNotifier != null && mExternalCallList != null) {
1451 mExternalCallList.removeExternalCallListener(mExternalCallNotifier);
1452 }
1453 mStatusBarNotifier = null;
1454
1455 if (mCallList != null) {
1456 mCallList.removeListener(this);
1457 mCallList.removeListener(mSpamCallListListener);
1458 }
1459 mCallList = null;
1460
1461 mContext = null;
1462 mInCallActivity = null;
1463 mManageConferenceActivity = null;
1464
1465 mListeners.clear();
1466 mIncomingCallListeners.clear();
1467 mDetailsListeners.clear();
1468 mCanAddCallListeners.clear();
1469 mOrientationListeners.clear();
1470 mInCallEventListeners.clear();
1471 mInCallUiListeners.clear();
twyen8efb4952017-10-06 16:35:54 -07001472 if (!mInCallUiLocks.isEmpty()) {
1473 LogUtil.e("InCallPresenter.attemptCleanup", "held in call locks: " + mInCallUiLocks);
1474 mInCallUiLocks.clear();
1475 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001476 LogUtil.d("InCallPresenter.attemptCleanup", "finished");
Eric Erfanianccca3152017-02-22 16:32:36 -08001477 }
1478 }
1479
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001480 public void showInCall(boolean showDialpad, boolean newOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001481 LogUtil.i("InCallPresenter.showInCall", "Showing InCallActivity");
Eric Erfanianccca3152017-02-22 16:32:36 -08001482 mContext.startActivity(
1483 InCallActivity.getIntent(
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001484 mContext, showDialpad, newOutgoingCall, false /* forFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -08001485 }
1486
1487 public void onServiceBind() {
1488 mServiceBound = true;
1489 }
1490
1491 public void onServiceUnbind() {
1492 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(false, null);
1493 mServiceBound = false;
1494 }
1495
1496 public boolean isServiceBound() {
1497 return mServiceBound;
1498 }
1499
1500 public void maybeStartRevealAnimation(Intent intent) {
1501 if (intent == null || mInCallActivity != null) {
1502 return;
1503 }
1504 final Bundle extras = intent.getBundleExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS);
1505 if (extras == null) {
1506 // Incoming call, just show the in-call UI directly.
1507 return;
1508 }
1509
1510 if (extras.containsKey(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS)) {
1511 // Account selection dialog will show up so don't show the animation.
1512 return;
1513 }
1514
1515 final PhoneAccountHandle accountHandle =
1516 intent.getParcelableExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE);
1517 final Point touchPoint = extras.getParcelable(TouchPointManager.TOUCH_POINT);
Eric Erfanianccca3152017-02-22 16:32:36 -08001518
1519 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(true, accountHandle);
1520
1521 final Intent activityIntent =
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001522 InCallActivity.getIntent(mContext, false, true, false /* forFullScreen */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001523 activityIntent.putExtra(TouchPointManager.TOUCH_POINT, touchPoint);
1524 mContext.startActivity(activityIntent);
1525 }
1526
1527 /**
1528 * Retrieves the current in-call camera manager instance, creating if necessary.
1529 *
1530 * @return The {@link InCallCameraManager}.
1531 */
1532 public InCallCameraManager getInCallCameraManager() {
1533 synchronized (this) {
1534 if (mInCallCameraManager == null) {
1535 mInCallCameraManager = new InCallCameraManager(mContext);
1536 }
1537
1538 return mInCallCameraManager;
1539 }
1540 }
1541
1542 /**
1543 * Notifies listeners of changes in orientation and notify calls of rotation angle change.
1544 *
1545 * @param orientation The screen orientation of the device (one of: {@link
1546 * InCallOrientationEventListener#SCREEN_ORIENTATION_0}, {@link
1547 * InCallOrientationEventListener#SCREEN_ORIENTATION_90}, {@link
1548 * InCallOrientationEventListener#SCREEN_ORIENTATION_180}, {@link
1549 * InCallOrientationEventListener#SCREEN_ORIENTATION_270}).
1550 */
1551 public void onDeviceOrientationChange(@ScreenOrientation int orientation) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001552 LogUtil.d(
1553 "InCallPresenter.onDeviceOrientationChange",
1554 "onDeviceOrientationChange: orientation= " + orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001555
1556 if (mCallList != null) {
1557 mCallList.notifyCallsOfDeviceRotation(orientation);
1558 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001559 LogUtil.w("InCallPresenter.onDeviceOrientationChange", "CallList is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001560 }
1561
1562 // Notify listeners of device orientation changed.
1563 for (InCallOrientationListener listener : mOrientationListeners) {
1564 listener.onDeviceOrientationChanged(orientation);
1565 }
1566 }
1567
1568 /**
1569 * Configures the in-call UI activity so it can change orientations or not. Enables the
1570 * orientation event listener if allowOrientationChange is true, disables it if false.
1571 *
1572 * @param allowOrientationChange {@code true} if the in-call UI can change between portrait and
1573 * landscape. {@code false} if the in-call UI should be locked in portrait.
1574 */
1575 public void setInCallAllowsOrientationChange(boolean allowOrientationChange) {
1576 if (mInCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001577 LogUtil.e(
1578 "InCallPresenter.setInCallAllowsOrientationChange",
1579 "InCallActivity is null. Can't set requested orientation.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001580 return;
1581 }
1582 mInCallActivity.setAllowOrientationChange(allowOrientationChange);
1583 }
1584
1585 public void enableScreenTimeout(boolean enable) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001586 LogUtil.v("InCallPresenter.enableScreenTimeout", "enableScreenTimeout: value=" + enable);
1587 mScreenTimeoutEnabled = enable;
1588 applyScreenTimeout();
1589 }
1590
1591 private void applyScreenTimeout() {
Eric Erfanianccca3152017-02-22 16:32:36 -08001592 if (mInCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001593 LogUtil.e("InCallPresenter.applyScreenTimeout", "InCallActivity is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001594 return;
1595 }
1596
1597 final Window window = mInCallActivity.getWindow();
Eric Erfanian2ca43182017-08-31 06:57:16 -07001598 if (mScreenTimeoutEnabled) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001599 window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1600 } else {
1601 window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1602 }
1603 }
1604
1605 /**
1606 * Hides or shows the conference manager fragment.
1607 *
1608 * @param show {@code true} if the conference manager should be shown, {@code false} if it should
1609 * be hidden.
1610 */
1611 public void showConferenceCallManager(boolean show) {
1612 if (mInCallActivity != null) {
1613 mInCallActivity.showConferenceFragment(show);
1614 }
1615 if (!show && mManageConferenceActivity != null) {
1616 mManageConferenceActivity.finish();
1617 }
1618 }
1619
1620 /**
1621 * Determines if the dialpad is visible.
1622 *
1623 * @return {@code true} if the dialpad is visible, {@code false} otherwise.
1624 */
1625 public boolean isDialpadVisible() {
1626 if (mInCallActivity == null) {
1627 return false;
1628 }
1629 return mInCallActivity.isDialpadVisible();
1630 }
1631
1632 public ThemeColorManager getThemeColorManager() {
1633 return mThemeColorManager;
1634 }
1635
wangqi8d662ca2017-10-26 11:27:19 -07001636 @VisibleForTesting
1637 public void setThemeColorManager(ThemeColorManager themeColorManager) {
1638 mThemeColorManager = themeColorManager;
1639 }
1640
Eric Erfanianccca3152017-02-22 16:32:36 -08001641 /** Called when the foreground call changes. */
1642 public void onForegroundCallChanged(DialerCall newForegroundCall) {
1643 mThemeColorManager.onForegroundCallChanged(mContext, newForegroundCall);
1644 if (mInCallActivity != null) {
1645 mInCallActivity.onForegroundCallChanged(newForegroundCall);
1646 }
1647 }
1648
1649 public InCallActivity getActivity() {
1650 return mInCallActivity;
1651 }
1652
1653 /** Called when the UI begins, and starts the callstate callbacks if necessary. */
1654 public void setActivity(InCallActivity inCallActivity) {
1655 if (inCallActivity == null) {
1656 throw new IllegalArgumentException("registerActivity cannot be called with null");
1657 }
1658 if (mInCallActivity != null && mInCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001659 LogUtil.w(
1660 "InCallPresenter.setActivity", "Setting a second activity before destroying the first.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001661 }
1662 updateActivity(inCallActivity);
1663 }
1664
1665 ExternalCallNotifier getExternalCallNotifier() {
1666 return mExternalCallNotifier;
1667 }
1668
1669 VideoSurfaceTexture getLocalVideoSurfaceTexture() {
1670 if (mLocalVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001671 boolean isPixel2017 = false;
1672 if (mContext != null) {
1673 isPixel2017 = mContext.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
1674 }
1675 mLocalVideoSurfaceTexture = VideoSurfaceBindings.createLocalVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001676 }
1677 return mLocalVideoSurfaceTexture;
1678 }
1679
1680 VideoSurfaceTexture getRemoteVideoSurfaceTexture() {
1681 if (mRemoteVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001682 boolean isPixel2017 = false;
1683 if (mContext != null) {
1684 isPixel2017 = mContext.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
1685 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001686 mRemoteVideoSurfaceTexture =
roldenburg12b50c62017-09-01 14:41:19 -07001687 VideoSurfaceBindings.createRemoteVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001688 }
1689 return mRemoteVideoSurfaceTexture;
1690 }
1691
1692 void cleanupSurfaces() {
1693 if (mRemoteVideoSurfaceTexture != null) {
1694 mRemoteVideoSurfaceTexture.setDoneWithSurface();
1695 mRemoteVideoSurfaceTexture = null;
1696 }
1697 if (mLocalVideoSurfaceTexture != null) {
1698 mLocalVideoSurfaceTexture.setDoneWithSurface();
1699 mLocalVideoSurfaceTexture = null;
1700 }
1701 }
1702
yueg77cb8e52017-10-27 16:42:51 -07001703 @Override
1704 public void onAudioStateChanged(CallAudioState audioState) {
1705 if (mStatusBarNotifier != null) {
1706 mStatusBarNotifier.updateNotification();
1707 }
1708 }
1709
Eric Erfanianccca3152017-02-22 16:32:36 -08001710 /** All the main states of InCallActivity. */
1711 public enum InCallState {
1712 // InCall Screen is off and there are no calls
1713 NO_CALLS,
1714
1715 // Incoming-call screen is up
1716 INCOMING,
1717
1718 // In-call experience is showing
1719 INCALL,
1720
1721 // Waiting for user input before placing outgoing call
1722 WAITING_FOR_ACCOUNT,
1723
1724 // UI is starting up but no call has been initiated yet.
1725 // The UI is waiting for Telecom to respond.
1726 PENDING_OUTGOING,
1727
1728 // User is dialing out
1729 OUTGOING;
1730
1731 public boolean isIncoming() {
1732 return (this == INCOMING);
1733 }
1734
1735 public boolean isConnectingOrConnected() {
1736 return (this == INCOMING || this == OUTGOING || this == INCALL);
1737 }
1738 }
1739
1740 /** Interface implemented by classes that need to know about the InCall State. */
1741 public interface InCallStateListener {
1742
1743 // TODO: Enhance state to contain the call objects instead of passing CallList
1744 void onStateChange(InCallState oldState, InCallState newState, CallList callList);
1745 }
1746
1747 public interface IncomingCallListener {
1748
1749 void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call);
1750 }
1751
1752 public interface CanAddCallListener {
1753
1754 void onCanAddCallChanged(boolean canAddCall);
1755 }
1756
1757 public interface InCallDetailsListener {
1758
1759 void onDetailsChanged(DialerCall call, android.telecom.Call.Details details);
1760 }
1761
1762 public interface InCallOrientationListener {
1763
1764 void onDeviceOrientationChanged(@ScreenOrientation int orientation);
1765 }
1766
1767 /**
1768 * Interface implemented by classes that need to know about events which occur within the In-Call
1769 * UI. Used as a means of communicating between fragments that make up the UI.
1770 */
1771 public interface InCallEventListener {
1772
1773 void onFullscreenModeChanged(boolean isFullscreenMode);
1774 }
1775
1776 public interface InCallUiListener {
1777
1778 void onUiShowing(boolean showing);
1779 }
twyen8efb4952017-10-06 16:35:54 -07001780
1781 private class InCallUiLockImpl implements InCallUiLock {
1782 private final String tag;
1783
1784 private InCallUiLockImpl(String tag) {
1785 this.tag = tag;
1786 }
1787
1788 @MainThread
1789 @Override
1790 public void release() {
1791 Assert.isMainThread();
1792 releaseInCallUiLock(InCallUiLockImpl.this);
1793 }
1794
1795 @Override
1796 public String toString() {
1797 return "InCallUiLock[" + tag + "]";
1798 }
1799 }
1800
1801 @MainThread
1802 public InCallUiLock acquireInCallUiLock(String tag) {
1803 Assert.isMainThread();
1804 InCallUiLock lock = new InCallUiLockImpl(tag);
1805 mInCallUiLocks.add(lock);
1806 return lock;
1807 }
1808
1809 @MainThread
1810 private void releaseInCallUiLock(InCallUiLock lock) {
1811 Assert.isMainThread();
1812 LogUtil.i("InCallPresenter.releaseInCallUiLock", "releasing %s", lock);
1813 mInCallUiLocks.remove(lock);
1814 if (mInCallUiLocks.isEmpty()) {
1815 LogUtil.i("InCallPresenter.releaseInCallUiLock", "all locks released");
1816 if (mInCallState == InCallState.NO_CALLS) {
1817 LogUtil.i("InCallPresenter.releaseInCallUiLock", "no more calls, finishing UI");
1818 attemptFinishActivity();
1819 attemptCleanup();
1820 }
1821 }
1822 }
1823
1824 @MainThread
1825 public boolean isInCallUiLocked() {
1826 Assert.isMainThread();
1827 if (mInCallUiLocks.isEmpty()) {
1828 return false;
1829 }
1830 for (InCallUiLock lock : mInCallUiLocks) {
1831 LogUtil.i("InCallPresenter.isInCallUiLocked", "still locked by %s", lock);
1832 }
1833 return true;
1834 }
1835
1836 private final Set<InCallUiLock> mInCallUiLocks = new ArraySet<>();
Eric Erfanianccca3152017-02-22 16:32:36 -08001837}