blob: dee5e7eb14859a9dfdfd654f760a0c7673188864 [file] [log] [blame]
Eric Erfanianccca3152017-02-22 16:32:36 -08001/*
2 * Copyright (C) 2016 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
linyuhc3968e62017-11-20 17:40:50 -080019import android.app.ActivityManager;
20import android.app.ActivityManager.AppTask;
linyuh57b093b2017-11-17 14:32:32 -080021import android.app.ActivityManager.TaskDescription;
linyuh7b86f562017-11-16 11:24:09 -080022import android.app.AlertDialog;
linyuhf99f6302017-11-15 11:23:51 -080023import android.app.Dialog;
linyuh57b093b2017-11-17 14:32:32 -080024import android.app.KeyguardManager;
Eric Erfanianccca3152017-02-22 16:32:36 -080025import android.content.Context;
26import android.content.Intent;
linyuhc3968e62017-11-20 17:40:50 -080027import android.content.res.Configuration;
Eric Erfanianccca3152017-02-22 16:32:36 -080028import android.graphics.drawable.GradientDrawable;
29import android.graphics.drawable.GradientDrawable.Orientation;
30import android.os.Bundle;
Eric Erfanian2ca43182017-08-31 06:57:16 -070031import android.os.Trace;
Eric Erfanianccca3152017-02-22 16:32:36 -080032import android.support.annotation.ColorInt;
33import android.support.annotation.FloatRange;
linyuhc3968e62017-11-20 17:40:50 -080034import android.support.annotation.IntDef;
Eric Erfanianc857f902017-05-15 14:05:33 -070035import android.support.annotation.NonNull;
Eric Erfanianccca3152017-02-22 16:32:36 -080036import android.support.annotation.Nullable;
linyuhc3968e62017-11-20 17:40:50 -080037import android.support.annotation.VisibleForTesting;
Eric Erfanianccca3152017-02-22 16:32:36 -080038import android.support.v4.app.FragmentManager;
39import android.support.v4.app.FragmentTransaction;
linyuh57b093b2017-11-17 14:32:32 -080040import android.support.v4.content.res.ResourcesCompat;
Eric Erfanianccca3152017-02-22 16:32:36 -080041import android.support.v4.graphics.ColorUtils;
twyen73a74c32018-03-07 12:12:24 -080042import android.telecom.Call;
linyuhc3968e62017-11-20 17:40:50 -080043import android.telecom.CallAudioState;
44import android.telecom.PhoneAccountHandle;
Eric Erfanian90508232017-03-24 09:31:16 -070045import android.telephony.TelephonyManager;
Eric Erfanianccca3152017-02-22 16:32:36 -080046import android.view.KeyEvent;
47import android.view.MenuItem;
48import android.view.MotionEvent;
49import android.view.View;
linyuh9c327da2017-11-14 12:33:48 -080050import android.view.WindowManager;
linyuhc3968e62017-11-20 17:40:50 -080051import android.view.animation.Animation;
52import android.view.animation.AnimationUtils;
linyuh7b86f562017-11-16 11:24:09 -080053import android.widget.CheckBox;
54import android.widget.Toast;
linyuhf99f6302017-11-15 11:23:51 -080055import com.android.contacts.common.widget.SelectPhoneAccountDialogFragment;
linyuhc3968e62017-11-20 17:40:50 -080056import com.android.dialer.animation.AnimUtils;
57import com.android.dialer.animation.AnimationListenerAdapter;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070058import com.android.dialer.common.Assert;
Eric Erfanianccca3152017-02-22 16:32:36 -080059import com.android.dialer.common.LogUtil;
twyen73a74c32018-03-07 12:12:24 -080060import com.android.dialer.common.concurrent.DialerExecutorComponent;
weijiaxu650e7cc2017-10-31 12:38:54 -070061import com.android.dialer.common.concurrent.ThreadUtil;
Eric Erfanianccca3152017-02-22 16:32:36 -080062import com.android.dialer.compat.ActivityCompat;
linyuhc3968e62017-11-20 17:40:50 -080063import com.android.dialer.compat.CompatUtils;
Eric Erfanian2ca43182017-08-31 06:57:16 -070064import com.android.dialer.configprovider.ConfigProviderBindings;
twyen73a74c32018-03-07 12:12:24 -080065import com.android.dialer.logging.DialerImpression.Type;
Eric Erfanianccca3152017-02-22 16:32:36 -080066import com.android.dialer.logging.Logger;
Eric Erfanian8369df02017-05-03 10:27:13 -070067import com.android.dialer.logging.ScreenEvent;
zachh7a96dc72018-02-20 22:16:03 -080068import com.android.dialer.metrics.Metrics;
69import com.android.dialer.metrics.MetricsComponent;
twyen73a74c32018-03-07 12:12:24 -080070import com.android.dialer.preferredsim.PreferredAccountRecorder;
71import com.android.dialer.preferredsim.PreferredAccountWorker;
72import com.android.dialer.preferredsim.suggestion.SuggestionProvider;
linyuhc3968e62017-11-20 17:40:50 -080073import com.android.dialer.util.ViewUtil;
Eric Erfanianccca3152017-02-22 16:32:36 -080074import com.android.incallui.answer.bindings.AnswerBindings;
75import com.android.incallui.answer.protocol.AnswerScreen;
76import com.android.incallui.answer.protocol.AnswerScreenDelegate;
77import com.android.incallui.answer.protocol.AnswerScreenDelegateFactory;
78import com.android.incallui.answerproximitysensor.PseudoScreenState;
linyuh57b093b2017-11-17 14:32:32 -080079import com.android.incallui.audiomode.AudioModeProvider;
Eric Erfanianccca3152017-02-22 16:32:36 -080080import com.android.incallui.call.CallList;
81import com.android.incallui.call.DialerCall;
82import com.android.incallui.call.DialerCall.State;
linyuh57b093b2017-11-17 14:32:32 -080083import com.android.incallui.call.TelecomAdapter;
Eric Erfanian2ca43182017-08-31 06:57:16 -070084import com.android.incallui.callpending.CallPendingActivity;
85import com.android.incallui.disconnectdialog.DisconnectMessage;
Eric Erfanianccca3152017-02-22 16:32:36 -080086import com.android.incallui.incall.bindings.InCallBindings;
87import com.android.incallui.incall.protocol.InCallButtonUiDelegate;
88import com.android.incallui.incall.protocol.InCallButtonUiDelegateFactory;
89import com.android.incallui.incall.protocol.InCallScreen;
90import com.android.incallui.incall.protocol.InCallScreenDelegate;
91import com.android.incallui.incall.protocol.InCallScreenDelegateFactory;
linyuh7b86f562017-11-16 11:24:09 -080092import com.android.incallui.incalluilock.InCallUiLock;
wangqi219b8702018-02-13 09:34:41 -080093import com.android.incallui.rtt.bindings.RttBindings;
94import com.android.incallui.rtt.protocol.RttCallScreen;
95import com.android.incallui.rtt.protocol.RttCallScreenDelegate;
96import com.android.incallui.rtt.protocol.RttCallScreenDelegateFactory;
linyuhf99f6302017-11-15 11:23:51 -080097import com.android.incallui.telecomeventui.InternationalCallOnWifiDialogFragment;
Eric Erfanianccca3152017-02-22 16:32:36 -080098import com.android.incallui.video.bindings.VideoBindings;
99import com.android.incallui.video.protocol.VideoCallScreen;
100import com.android.incallui.video.protocol.VideoCallScreenDelegate;
101import com.android.incallui.video.protocol.VideoCallScreenDelegateFactory;
linyuhc3968e62017-11-20 17:40:50 -0800102import com.google.common.base.Optional;
103import java.lang.annotation.Retention;
104import java.lang.annotation.RetentionPolicy;
105import java.util.ArrayList;
106import java.util.List;
Eric Erfanianccca3152017-02-22 16:32:36 -0800107
108/** Version of {@link InCallActivity} that shows the new UI */
109public class InCallActivity extends TransactionSafeFragmentActivity
110 implements AnswerScreenDelegateFactory,
111 InCallScreenDelegateFactory,
112 InCallButtonUiDelegateFactory,
113 VideoCallScreenDelegateFactory,
wangqi219b8702018-02-13 09:34:41 -0800114 RttCallScreenDelegateFactory,
Eric Erfanianccca3152017-02-22 16:32:36 -0800115 PseudoScreenState.StateChangedListener {
116
linyuhc3968e62017-11-20 17:40:50 -0800117 @Retention(RetentionPolicy.SOURCE)
118 @IntDef({
119 DIALPAD_REQUEST_NONE,
120 DIALPAD_REQUEST_SHOW,
121 DIALPAD_REQUEST_HIDE,
122 })
123 @interface DialpadRequestType {}
Eric Erfanian2ca43182017-08-31 06:57:16 -0700124
linyuhc3968e62017-11-20 17:40:50 -0800125 private static final int DIALPAD_REQUEST_NONE = 1;
126 private static final int DIALPAD_REQUEST_SHOW = 2;
127 private static final int DIALPAD_REQUEST_HIDE = 3;
linyuh57b093b2017-11-17 14:32:32 -0800128
linyuhc3968e62017-11-20 17:40:50 -0800129 private static Optional<Integer> audioRouteForTesting = Optional.absent();
linyuh57b093b2017-11-17 14:32:32 -0800130
linyuhc3968e62017-11-20 17:40:50 -0800131 private final InternationalCallOnWifiCallback internationalCallOnWifiCallback =
132 new InternationalCallOnWifiCallback();
twyen73a74c32018-03-07 12:12:24 -0800133
134 private SelectPhoneAccountListener selectPhoneAccountListener;
Eric Erfanianccca3152017-02-22 16:32:36 -0800135
linyuhc3968e62017-11-20 17:40:50 -0800136 private Animation dialpadSlideInAnimation;
137 private Animation dialpadSlideOutAnimation;
138 private Dialog errorDialog;
139 private GradientDrawable backgroundDrawable;
linyuh69a25062017-11-15 16:18:51 -0800140 private InCallOrientationEventListener inCallOrientationEventListener;
linyuhc3968e62017-11-20 17:40:50 -0800141 private View pseudoBlackScreenOverlay;
142 private SelectPhoneAccountDialogFragment selectPhoneAccountDialogFragment;
143 private String dtmfTextToPrepopulate;
144 private String showPostCharWaitDialogCallId;
145 private String showPostCharWaitDialogChars;
146 private boolean allowOrientationChange;
147 private boolean animateDialpadOnShow;
Eric Erfanianccca3152017-02-22 16:32:36 -0800148 private boolean didShowAnswerScreen;
149 private boolean didShowInCallScreen;
150 private boolean didShowVideoCallScreen;
wangqi219b8702018-02-13 09:34:41 -0800151 private boolean didShowRttCallScreen;
linyuh9c327da2017-11-14 12:33:48 -0800152 private boolean dismissKeyguard;
Eric Erfanianccca3152017-02-22 16:32:36 -0800153 private boolean isInShowMainInCallFragment;
linyuhc3968e62017-11-20 17:40:50 -0800154 private boolean isRecreating; // whether the activity is going to be recreated
155 private boolean isVisible;
Eric Erfanianccca3152017-02-22 16:32:36 -0800156 private boolean needDismissPendingDialogs;
linyuhc3968e62017-11-20 17:40:50 -0800157 private boolean showPostCharWaitDialogOnResume;
158 private boolean touchDownWhenPseudoScreenOff;
159 private int[] backgroundDrawableColors;
160 @DialpadRequestType private int showDialpadRequest = DIALPAD_REQUEST_NONE;
Eric Erfanianccca3152017-02-22 16:32:36 -0800161
162 public static Intent getIntent(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700163 Context context, boolean showDialpad, boolean newOutgoingCall, boolean isForFullScreen) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800164 Intent intent = new Intent(Intent.ACTION_MAIN, null);
165 intent.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION | Intent.FLAG_ACTIVITY_NEW_TASK);
166 intent.setClass(context, InCallActivity.class);
linyuhc3968e62017-11-20 17:40:50 -0800167 if (showDialpad) {
168 intent.putExtra(IntentExtraNames.SHOW_DIALPAD, true);
169 }
170 intent.putExtra(IntentExtraNames.NEW_OUTGOING_CALL, newOutgoingCall);
171 intent.putExtra(IntentExtraNames.FOR_FULL_SCREEN, isForFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -0800172 return intent;
173 }
174
175 @Override
176 protected void onResumeFragments() {
177 super.onResumeFragments();
178 if (needDismissPendingDialogs) {
179 dismissPendingDialogs();
180 }
181 }
182
183 @Override
linyuhc3968e62017-11-20 17:40:50 -0800184 protected void onCreate(Bundle bundle) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700185 Trace.beginSection("InCallActivity.onCreate");
linyuhc3968e62017-11-20 17:40:50 -0800186 super.onCreate(bundle);
Eric Erfanianccca3152017-02-22 16:32:36 -0800187
twyen73a74c32018-03-07 12:12:24 -0800188 selectPhoneAccountListener = new SelectPhoneAccountListener(getApplicationContext());
189
linyuhc3968e62017-11-20 17:40:50 -0800190 if (bundle != null) {
191 didShowAnswerScreen = bundle.getBoolean(KeysForSavedInstance.DID_SHOW_ANSWER_SCREEN);
192 didShowInCallScreen = bundle.getBoolean(KeysForSavedInstance.DID_SHOW_IN_CALL_SCREEN);
193 didShowVideoCallScreen = bundle.getBoolean(KeysForSavedInstance.DID_SHOW_VIDEO_CALL_SCREEN);
wangqi153af2f2018-02-15 16:21:49 -0800194 didShowRttCallScreen = bundle.getBoolean(KeysForSavedInstance.DID_SHOW_RTT_CALL_SCREEN);
Eric Erfanianccca3152017-02-22 16:32:36 -0800195 }
196
linyuhc3968e62017-11-20 17:40:50 -0800197 setWindowFlags();
198 setContentView(R.layout.incall_screen);
199 internalResolveIntent(getIntent());
200
201 boolean isLandscape =
202 getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
203 boolean isRtl = ViewUtil.isRtl();
204 if (isLandscape) {
205 dialpadSlideInAnimation =
206 AnimationUtils.loadAnimation(
207 this, isRtl ? R.anim.dialpad_slide_in_left : R.anim.dialpad_slide_in_right);
208 dialpadSlideOutAnimation =
209 AnimationUtils.loadAnimation(
210 this, isRtl ? R.anim.dialpad_slide_out_left : R.anim.dialpad_slide_out_right);
211 } else {
212 dialpadSlideInAnimation = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_in_bottom);
213 dialpadSlideOutAnimation =
214 AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_out_bottom);
215 }
216 dialpadSlideInAnimation.setInterpolator(AnimUtils.EASE_IN);
217 dialpadSlideOutAnimation.setInterpolator(AnimUtils.EASE_OUT);
218 dialpadSlideOutAnimation.setAnimationListener(
219 new AnimationListenerAdapter() {
220 @Override
221 public void onAnimationEnd(Animation animation) {
222 hideDialpadFragment();
223 }
224 });
225
226 if (bundle != null && showDialpadRequest == DIALPAD_REQUEST_NONE) {
227 // If the dialpad was shown before, set related variables so that it can be shown and
228 // populated with the previous DTMF text during onResume().
229 if (bundle.containsKey(IntentExtraNames.SHOW_DIALPAD)) {
230 boolean showDialpad = bundle.getBoolean(IntentExtraNames.SHOW_DIALPAD);
231 showDialpadRequest = showDialpad ? DIALPAD_REQUEST_SHOW : DIALPAD_REQUEST_HIDE;
232 animateDialpadOnShow = false;
233 }
234 dtmfTextToPrepopulate = bundle.getString(KeysForSavedInstance.DIALPAD_TEXT);
235
236 SelectPhoneAccountDialogFragment selectPhoneAccountDialogFragment =
237 (SelectPhoneAccountDialogFragment)
238 getFragmentManager().findFragmentByTag(Tags.SELECT_ACCOUNT_FRAGMENT);
239 if (selectPhoneAccountDialogFragment != null) {
240 selectPhoneAccountDialogFragment.setListener(selectPhoneAccountListener);
241 }
242 }
243
244 InternationalCallOnWifiDialogFragment existingInternationalCallOnWifiDialogFragment =
245 (InternationalCallOnWifiDialogFragment)
246 getSupportFragmentManager().findFragmentByTag(Tags.INTERNATIONAL_CALL_ON_WIFI);
247 if (existingInternationalCallOnWifiDialogFragment != null) {
248 existingInternationalCallOnWifiDialogFragment.setCallback(internationalCallOnWifiCallback);
249 }
250
linyuh69a25062017-11-15 16:18:51 -0800251 inCallOrientationEventListener = new InCallOrientationEventListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800252
253 getWindow()
254 .getDecorView()
255 .setSystemUiVisibility(
256 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
257
258 pseudoBlackScreenOverlay = findViewById(R.id.psuedo_black_screen_overlay);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700259 sendBroadcast(CallPendingActivity.getFinishBroadcast());
260 Trace.endSection();
zachh7a96dc72018-02-20 22:16:03 -0800261 MetricsComponent.get(this)
262 .metrics()
263 .stopTimer(Metrics.ON_CALL_ADDED_TO_ON_INCALL_UI_SHOWN_INCOMING);
264 MetricsComponent.get(this)
265 .metrics()
266 .stopTimer(Metrics.ON_CALL_ADDED_TO_ON_INCALL_UI_SHOWN_OUTGOING);
Eric Erfanianccca3152017-02-22 16:32:36 -0800267 }
268
linyuhc3968e62017-11-20 17:40:50 -0800269 private void setWindowFlags() {
270 // Allow the activity to be shown when the screen is locked and filter out touch events that are
271 // "too fat".
272 int flags =
273 WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
274 | WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES;
275
linyuhf79d1cb2017-12-15 17:49:56 -0800276 // When the audio stream is not via Bluetooth, turn on the screen once the activity is shown.
277 // When the audio stream is via Bluetooth, turn on the screen only for an incoming call.
linyuhc3968e62017-11-20 17:40:50 -0800278 final int audioRoute = getAudioRoute();
linyuhf79d1cb2017-12-15 17:49:56 -0800279 if (audioRoute != CallAudioState.ROUTE_BLUETOOTH
280 || CallList.getInstance().getIncomingCall() != null) {
linyuhc3968e62017-11-20 17:40:50 -0800281 flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
282 }
283
284 getWindow().addFlags(flags);
285 }
286
287 private static int getAudioRoute() {
288 if (audioRouteForTesting.isPresent()) {
289 return audioRouteForTesting.get();
290 }
291
292 return AudioModeProvider.getInstance().getAudioState().getRoute();
293 }
294
295 @VisibleForTesting(otherwise = VisibleForTesting.NONE)
296 public static void setAudioRouteForTesting(int audioRoute) {
297 audioRouteForTesting = Optional.of(audioRoute);
298 }
299
300 private void internalResolveIntent(Intent intent) {
301 if (!intent.getAction().equals(Intent.ACTION_MAIN)) {
302 return;
303 }
304
305 if (intent.hasExtra(IntentExtraNames.SHOW_DIALPAD)) {
306 // IntentExtraNames.SHOW_DIALPAD can be used to specify whether the DTMF dialpad should be
307 // initially visible. If the extra is absent, leave the dialpad in its previous state.
308 boolean showDialpad = intent.getBooleanExtra(IntentExtraNames.SHOW_DIALPAD, false);
309 relaunchedFromDialer(showDialpad);
310 }
311
312 DialerCall outgoingCall = CallList.getInstance().getOutgoingCall();
313 if (outgoingCall == null) {
314 outgoingCall = CallList.getInstance().getPendingOutgoingCall();
315 }
316 if (intent.getBooleanExtra(IntentExtraNames.NEW_OUTGOING_CALL, false)) {
317 intent.removeExtra(IntentExtraNames.NEW_OUTGOING_CALL);
318
319 // InCallActivity is responsible for disconnecting a new outgoing call if there is no way of
320 // making it (i.e. no valid call capable accounts).
321 // If the version is not MSIM compatible, ignore this code.
322 if (CompatUtils.isMSIMCompatible()
323 && InCallPresenter.isCallWithNoValidAccounts(outgoingCall)) {
324 LogUtil.i(
325 "InCallActivity.internalResolveIntent", "Call with no valid accounts, disconnecting");
326 outgoingCall.disconnect();
327 }
328
329 dismissKeyguard(true);
330 }
331
332 if (showPhoneAccountSelectionDialog()) {
333 hideMainInCallFragment();
334 }
335 }
336
337 /**
338 * When relaunching from the dialer app, {@code showDialpad} indicates whether the dialpad should
339 * be shown on launch.
340 *
341 * @param showDialpad {@code true} to indicate the dialpad should be shown on launch, and {@code
342 * false} to indicate no change should be made to the dialpad visibility.
343 */
344 private void relaunchedFromDialer(boolean showDialpad) {
345 showDialpadRequest = showDialpad ? DIALPAD_REQUEST_SHOW : DIALPAD_REQUEST_NONE;
346 animateDialpadOnShow = true;
347
348 if (showDialpadRequest == DIALPAD_REQUEST_SHOW) {
349 // If there's only one line in use, AND it's on hold, then we're sure the user
350 // wants to use the dialpad toward the exact line, so un-hold the holding line.
351 DialerCall call = CallList.getInstance().getActiveOrBackgroundCall();
352 if (call != null && call.getState() == State.ONHOLD) {
353 call.unhold();
354 }
355 }
356 }
357
358 /**
359 * Show a phone account selection dialog if there is a call waiting for phone account selection.
360 *
361 * @return true if the dialog was shown.
362 */
363 private boolean showPhoneAccountSelectionDialog() {
364 DialerCall waitingForAccountCall = CallList.getInstance().getWaitingForAccountCall();
365 if (waitingForAccountCall == null) {
366 return false;
367 }
368
twyen73a74c32018-03-07 12:12:24 -0800369 DialerExecutorComponent.get(this)
370 .dialerExecutorFactory()
371 .createNonUiTaskBuilder(new PreferredAccountWorker(waitingForAccountCall.getNumber()))
372 .onSuccess(
373 (result -> {
374 if (result.getPhoneAccountHandle().isPresent()) {
375 Logger.get(this).logImpression(Type.DUAL_SIM_SELECTION_PREFERRED_USED);
376 selectPhoneAccountListener.onPhoneAccountSelected(
377 result.getPhoneAccountHandle().get(), false, waitingForAccountCall.getId());
378 return;
379 }
380 if (result.getSuggestion().isPresent()) {
381 LogUtil.i(
382 "CallingAccountSelector.processPreferredAccount",
383 "SIM suggested: " + result.getSuggestion().get().reason);
384 if (result.getSuggestion().get().shouldAutoSelect) {
385 Logger.get(this).logImpression(Type.DUAL_SIM_SELECTION_SUGGESTION_AUTO_SELECTED);
386 LogUtil.i(
387 "CallingAccountSelector.processPreferredAccount", "Auto selected suggestion");
388 selectPhoneAccountListener.onPhoneAccountSelected(
389 result.getSuggestion().get().phoneAccountHandle,
390 false,
391 waitingForAccountCall.getId());
392 return;
393 }
394 }
395 Bundle extras = waitingForAccountCall.getIntentExtras();
396 List<PhoneAccountHandle> phoneAccountHandles =
397 extras == null
398 ? new ArrayList<>()
399 : extras.getParcelableArrayList(Call.AVAILABLE_PHONE_ACCOUNTS);
linyuhc3968e62017-11-20 17:40:50 -0800400
twyen73a74c32018-03-07 12:12:24 -0800401 waitingForAccountCall.setPreferredAccountRecorder(
402 new PreferredAccountRecorder(
403 waitingForAccountCall.getNumber(),
404 result.getSuggestion().orNull(),
405 result.getDataId().orNull()));
406 selectPhoneAccountDialogFragment =
407 SelectPhoneAccountDialogFragment.newInstance(
408 R.string.select_phone_account_for_calls,
409 result.getDataId().isPresent() /* canSetDefault */,
410 R.string.select_phone_account_for_calls_remember /* setDefaultResId */,
411 phoneAccountHandles,
412 selectPhoneAccountListener,
413 waitingForAccountCall.getId(),
414 SuggestionProvider.buildHint(
415 this, phoneAccountHandles, result.getSuggestion().orNull() /* hints */));
416 selectPhoneAccountDialogFragment.show(
417 getFragmentManager(), Tags.SELECT_ACCOUNT_FRAGMENT);
418 }))
419 .build()
420 .executeParallel(this);
421
linyuhc3968e62017-11-20 17:40:50 -0800422 return true;
423 }
424
Eric Erfanianccca3152017-02-22 16:32:36 -0800425 @Override
426 protected void onSaveInstanceState(Bundle out) {
linyuh57b093b2017-11-17 14:32:32 -0800427 LogUtil.enterBlock("InCallActivity.onSaveInstanceState");
428
429 // TODO: DialpadFragment should handle this as part of its own state
linyuhc3968e62017-11-20 17:40:50 -0800430 out.putBoolean(IntentExtraNames.SHOW_DIALPAD, isDialpadVisible());
linyuh57b093b2017-11-17 14:32:32 -0800431 DialpadFragment dialpadFragment = getDialpadFragment();
432 if (dialpadFragment != null) {
linyuhc3968e62017-11-20 17:40:50 -0800433 out.putString(KeysForSavedInstance.DIALPAD_TEXT, dialpadFragment.getDtmfText());
linyuh57b093b2017-11-17 14:32:32 -0800434 }
435
linyuhc3968e62017-11-20 17:40:50 -0800436 out.putBoolean(KeysForSavedInstance.DID_SHOW_ANSWER_SCREEN, didShowAnswerScreen);
437 out.putBoolean(KeysForSavedInstance.DID_SHOW_IN_CALL_SCREEN, didShowInCallScreen);
438 out.putBoolean(KeysForSavedInstance.DID_SHOW_VIDEO_CALL_SCREEN, didShowVideoCallScreen);
wangqi153af2f2018-02-15 16:21:49 -0800439 out.putBoolean(KeysForSavedInstance.DID_SHOW_RTT_CALL_SCREEN, didShowRttCallScreen);
linyuh57b093b2017-11-17 14:32:32 -0800440
Eric Erfanianccca3152017-02-22 16:32:36 -0800441 super.onSaveInstanceState(out);
442 isVisible = false;
443 }
444
445 @Override
446 protected void onStart() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700447 Trace.beginSection("InCallActivity.onStart");
Eric Erfanianccca3152017-02-22 16:32:36 -0800448 super.onStart();
linyuh57b093b2017-11-17 14:32:32 -0800449
Eric Erfanianccca3152017-02-22 16:32:36 -0800450 isVisible = true;
451 showMainInCallFragment();
linyuh57b093b2017-11-17 14:32:32 -0800452
453 InCallPresenter.getInstance().setActivity(this);
454 enableInCallOrientationEventListener(
455 getRequestedOrientation()
456 == InCallOrientationEventListener.ACTIVITY_PREFERENCE_ALLOW_ROTATION);
457 InCallPresenter.getInstance().onActivityStarted();
458
yueg10f6e822018-01-17 15:32:18 -0800459 if (!isRecreating) {
460 InCallPresenter.getInstance().onUiShowing(true);
461 }
462
Eric Erfanianccca3152017-02-22 16:32:36 -0800463 if (ActivityCompat.isInMultiWindowMode(this)
464 && !getResources().getBoolean(R.bool.incall_dialpad_allowed)) {
465 // Hide the dialpad because there may not be enough room
466 showDialpadFragment(false, false);
467 }
linyuh57b093b2017-11-17 14:32:32 -0800468
Eric Erfanian2ca43182017-08-31 06:57:16 -0700469 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800470 }
471
472 @Override
473 protected void onResume() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700474 Trace.beginSection("InCallActivity.onResume");
Eric Erfanianccca3152017-02-22 16:32:36 -0800475 super.onResume();
linyuhc3968e62017-11-20 17:40:50 -0800476
477 if (!InCallPresenter.getInstance().isReadyForTearDown()) {
478 updateTaskDescription();
yueg10f6e822018-01-17 15:32:18 -0800479 InCallPresenter.getInstance().updateNotification();
linyuhc3968e62017-11-20 17:40:50 -0800480 }
481
482 // If there is a pending request to show or hide the dialpad, handle that now.
483 if (showDialpadRequest != DIALPAD_REQUEST_NONE) {
484 if (showDialpadRequest == DIALPAD_REQUEST_SHOW) {
485 // Exit fullscreen so that the user has access to the dialpad hide/show button.
486 // This is important when showing the dialpad from within dialer.
487 InCallPresenter.getInstance().setFullScreen(false /* isFullScreen */, true /* force */);
488
489 showDialpadFragment(true /* show */, animateDialpadOnShow /* animate */);
490 animateDialpadOnShow = false;
491
492 DialpadFragment dialpadFragment = getDialpadFragment();
493 if (dialpadFragment != null) {
494 dialpadFragment.setDtmfText(dtmfTextToPrepopulate);
495 dtmfTextToPrepopulate = null;
496 }
497 } else {
498 LogUtil.i("InCallActivity.onResume", "Force-hide the dialpad");
499 if (getDialpadFragment() != null) {
500 showDialpadFragment(false /* show */, false /* animate */);
501 }
502 }
503 showDialpadRequest = DIALPAD_REQUEST_NONE;
504 }
505 updateNavigationBar(isDialpadVisible());
506
507 if (showPostCharWaitDialogOnResume) {
508 showDialogForPostCharWait(showPostCharWaitDialogCallId, showPostCharWaitDialogChars);
509 }
510
511 CallList.getInstance()
512 .onInCallUiShown(getIntent().getBooleanExtra(IntentExtraNames.FOR_FULL_SCREEN, false));
513
Eric Erfanianccca3152017-02-22 16:32:36 -0800514 PseudoScreenState pseudoScreenState = InCallPresenter.getInstance().getPseudoScreenState();
515 pseudoScreenState.addListener(this);
516 onPseudoScreenStateChanged(pseudoScreenState.isOn());
Eric Erfanian2ca43182017-08-31 06:57:16 -0700517 Trace.endSection();
weijiaxu650e7cc2017-10-31 12:38:54 -0700518 // add 1 sec delay to get memory snapshot so that dialer wont react slowly on resume.
519 ThreadUtil.postDelayedOnUiThread(
weijiaxuc950a9b2017-11-06 16:39:04 -0800520 () ->
zachh7a96dc72018-02-20 22:16:03 -0800521 MetricsComponent.get(this)
522 .metrics()
523 .recordMemory(Metrics.INCALL_ACTIVITY_ON_RESUME_MEMORY_EVENT_NAME),
weijiaxu650e7cc2017-10-31 12:38:54 -0700524 1000);
Eric Erfanianccca3152017-02-22 16:32:36 -0800525 }
526
Eric Erfanianccca3152017-02-22 16:32:36 -0800527 @Override
528 protected void onPause() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700529 Trace.beginSection("InCallActivity.onPause");
Eric Erfanianccca3152017-02-22 16:32:36 -0800530 super.onPause();
linyuh57b093b2017-11-17 14:32:32 -0800531
532 DialpadFragment dialpadFragment = getDialpadFragment();
533 if (dialpadFragment != null) {
534 dialpadFragment.onDialerKeyUp(null);
535 }
536
yueg10f6e822018-01-17 15:32:18 -0800537 InCallPresenter.getInstance().updateNotification();
linyuh57b093b2017-11-17 14:32:32 -0800538
Eric Erfanianccca3152017-02-22 16:32:36 -0800539 InCallPresenter.getInstance().getPseudoScreenState().removeListener(this);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700540 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800541 }
542
543 @Override
544 protected void onStop() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700545 Trace.beginSection("InCallActivity.onStop");
wangqi4d705e52017-09-28 12:23:35 -0700546 isVisible = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800547 super.onStop();
linyuh57b093b2017-11-17 14:32:32 -0800548
549 // Disconnects the call waiting for a phone account when the activity is hidden (e.g., after the
550 // user presses the home button).
551 // Without this the pending call will get stuck on phone account selection and new calls can't
552 // be created.
553 // Skip this when the screen is locked since the activity may complete its current life cycle
554 // and restart.
linyuhc3968e62017-11-20 17:40:50 -0800555 if (!isRecreating && !getSystemService(KeyguardManager.class).isKeyguardLocked()) {
linyuh57b093b2017-11-17 14:32:32 -0800556 DialerCall waitingForAccountCall = CallList.getInstance().getWaitingForAccountCall();
557 if (waitingForAccountCall != null) {
558 waitingForAccountCall.disconnect();
559 }
560 }
561
562 enableInCallOrientationEventListener(false);
563 InCallPresenter.getInstance().updateIsChangingConfigurations();
564 InCallPresenter.getInstance().onActivityStopped();
linyuhc3968e62017-11-20 17:40:50 -0800565 if (!isRecreating) {
yueg10f6e822018-01-17 15:32:18 -0800566 InCallPresenter.getInstance().onUiShowing(false);
linyuh57b093b2017-11-17 14:32:32 -0800567 if (errorDialog != null) {
568 errorDialog.dismiss();
569 }
570 }
571
yueg10f6e822018-01-17 15:32:18 -0800572 if (isFinishing()) {
573 InCallPresenter.getInstance().unsetActivity(this);
574 }
575
Eric Erfanian2ca43182017-08-31 06:57:16 -0700576 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800577 }
578
579 @Override
580 protected void onDestroy() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700581 Trace.beginSection("InCallActivity.onDestroy");
Eric Erfanianccca3152017-02-22 16:32:36 -0800582 super.onDestroy();
linyuh57b093b2017-11-17 14:32:32 -0800583
584 InCallPresenter.getInstance().unsetActivity(this);
585 InCallPresenter.getInstance().updateIsChangingConfigurations();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700586 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800587 }
588
589 @Override
590 public void finish() {
591 if (shouldCloseActivityOnFinish()) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700592 // When user select incall ui from recents after the call is disconnected, it tries to launch
593 // a new InCallActivity but InCallPresenter is already teared down at this point, which causes
594 // crash.
595 // By calling finishAndRemoveTask() instead of finish() the task associated with
596 // InCallActivity is cleared completely. So system won't try to create a new InCallActivity in
597 // this case.
598 //
599 // Calling finish won't clear the task and normally when an activity finishes it shouldn't
600 // clear the task since there could be parent activity in the same task that's still alive.
601 // But InCallActivity is special since it's singleInstance which means it's root activity and
602 // only instance of activity in the task. So it should be safe to also remove task when
603 // finishing.
604 // It's also necessary in the sense of it's excluded from recents. So whenever the activity
605 // finishes, the task should also be removed since it doesn't make sense to go back to it in
606 // anyway anymore.
607 super.finishAndRemoveTask();
Eric Erfanianccca3152017-02-22 16:32:36 -0800608 }
609 }
610
611 private boolean shouldCloseActivityOnFinish() {
linyuhc3968e62017-11-20 17:40:50 -0800612 if (!isVisible) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800613 LogUtil.i(
614 "InCallActivity.shouldCloseActivityOnFinish",
615 "allowing activity to be closed because it's not visible");
616 return true;
617 }
618
twyen8efb4952017-10-06 16:35:54 -0700619 if (InCallPresenter.getInstance().isInCallUiLocked()) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800620 LogUtil.i(
621 "InCallActivity.shouldCloseActivityOnFinish",
twyen8efb4952017-10-06 16:35:54 -0700622 "in call ui is locked, not closing activity");
Eric Erfanianccca3152017-02-22 16:32:36 -0800623 return false;
624 }
625
626 LogUtil.i(
627 "InCallActivity.shouldCloseActivityOnFinish",
twyen8efb4952017-10-06 16:35:54 -0700628 "activity is visible and has no locks, allowing activity to close");
Eric Erfanianccca3152017-02-22 16:32:36 -0800629 return true;
630 }
631
632 @Override
633 protected void onNewIntent(Intent intent) {
linyuhc3968e62017-11-20 17:40:50 -0800634 LogUtil.enterBlock("InCallActivity.onNewIntent");
Eric Erfanianccca3152017-02-22 16:32:36 -0800635
636 // If the screen is off, we need to make sure it gets turned on for incoming calls.
637 // This normally works just fine thanks to FLAG_TURN_SCREEN_ON but that only works
638 // when the activity is first created. Therefore, to ensure the screen is turned on
639 // for the call waiting case, we recreate() the current activity. There should be no jank from
640 // this since the screen is already off and will remain so until our new activity is up.
linyuhc3968e62017-11-20 17:40:50 -0800641 if (!isVisible) {
642 onNewIntent(intent, true /* isRecreating */);
Eric Erfanianccca3152017-02-22 16:32:36 -0800643 LogUtil.i("InCallActivity.onNewIntent", "Restarting InCallActivity to force screen on.");
644 recreate();
Eric Erfanian10b34a52017-05-04 08:23:17 -0700645 } else {
linyuhc3968e62017-11-20 17:40:50 -0800646 onNewIntent(intent, false /* isRecreating */);
647 }
648 }
649
yuega3305352018-01-09 11:02:47 -0800650 @VisibleForTesting
651 void onNewIntent(Intent intent, boolean isRecreating) {
linyuhc3968e62017-11-20 17:40:50 -0800652 this.isRecreating = isRecreating;
653
654 // We're being re-launched with a new Intent. Since it's possible for a single InCallActivity
655 // instance to persist indefinitely (even if we finish() ourselves), this sequence can
656 // happen any time the InCallActivity needs to be displayed.
657
658 // Stash away the new intent so that we can get it in the future by calling getIntent().
659 // Otherwise getIntent() will return the original Intent from when we first got created.
660 setIntent(intent);
661
662 // Activities are always paused before receiving a new intent, so we can count on our onResume()
663 // method being called next.
664
665 // Just like in onCreate(), handle the intent.
666 // Skip if InCallActivity is going to be recreated since this will be called in onCreate().
667 if (!isRecreating) {
668 internalResolveIntent(intent);
Eric Erfanianccca3152017-02-22 16:32:36 -0800669 }
670 }
671
672 @Override
673 public void onBackPressed() {
linyuh57b093b2017-11-17 14:32:32 -0800674 LogUtil.enterBlock("InCallActivity.onBackPressed");
675
linyuhc3968e62017-11-20 17:40:50 -0800676 if (!isVisible) {
linyuh57b093b2017-11-17 14:32:32 -0800677 return;
Eric Erfanianccca3152017-02-22 16:32:36 -0800678 }
linyuh57b093b2017-11-17 14:32:32 -0800679
680 if (!getCallCardFragmentVisible()) {
681 return;
682 }
683
684 DialpadFragment dialpadFragment = getDialpadFragment();
685 if (dialpadFragment != null && dialpadFragment.isVisible()) {
686 showDialpadFragment(false /* show */, true /* animate */);
687 return;
688 }
689
690 if (CallList.getInstance().getIncomingCall() != null) {
691 LogUtil.i(
692 "InCallActivity.onBackPressed",
693 "Ignore the press of the back key when an incoming call is ringing");
694 return;
695 }
696
697 // Nothing special to do. Fall back to the default behavior.
698 super.onBackPressed();
Eric Erfanianccca3152017-02-22 16:32:36 -0800699 }
700
701 @Override
702 public boolean onOptionsItemSelected(MenuItem item) {
703 LogUtil.i("InCallActivity.onOptionsItemSelected", "item: " + item);
704 if (item.getItemId() == android.R.id.home) {
705 onBackPressed();
706 return true;
707 }
708 return super.onOptionsItemSelected(item);
709 }
710
711 @Override
712 public boolean onKeyUp(int keyCode, KeyEvent event) {
linyuh57b093b2017-11-17 14:32:32 -0800713 DialpadFragment dialpadFragment = getDialpadFragment();
714 if (dialpadFragment != null
715 && dialpadFragment.isVisible()
716 && dialpadFragment.onDialerKeyUp(event)) {
717 return true;
718 }
719
720 if (keyCode == KeyEvent.KEYCODE_CALL) {
721 // Always consume KEYCODE_CALL to ensure the PhoneWindow won't do anything with it.
722 return true;
723 }
724
725 return super.onKeyUp(keyCode, event);
Eric Erfanianccca3152017-02-22 16:32:36 -0800726 }
727
728 @Override
729 public boolean onKeyDown(int keyCode, KeyEvent event) {
linyuh57b093b2017-11-17 14:32:32 -0800730 switch (keyCode) {
731 case KeyEvent.KEYCODE_CALL:
732 if (!InCallPresenter.getInstance().handleCallKey()) {
733 LogUtil.e(
734 "InCallActivity.onKeyDown",
735 "InCallPresenter should always handle KEYCODE_CALL in onKeyDown");
736 }
737 // Always consume KEYCODE_CALL to ensure the PhoneWindow won't do anything with it.
738 return true;
739
740 // Note that KEYCODE_ENDCALL isn't handled here as the standard system-wide handling of it
741 // is exactly what's needed, namely
742 // (1) "hang up" if there's an active call, or
743 // (2) "don't answer" if there's an incoming call.
744 // (See PhoneWindowManager for implementation details.)
745
746 case KeyEvent.KEYCODE_CAMERA:
747 // Consume KEYCODE_CAMERA since it's easy to accidentally press the camera button.
748 return true;
749
750 case KeyEvent.KEYCODE_VOLUME_UP:
751 case KeyEvent.KEYCODE_VOLUME_DOWN:
752 case KeyEvent.KEYCODE_VOLUME_MUTE:
753 // Ringer silencing handled by PhoneWindowManager.
754 break;
755
756 case KeyEvent.KEYCODE_MUTE:
757 TelecomAdapter.getInstance()
758 .mute(!AudioModeProvider.getInstance().getAudioState().isMuted());
759 return true;
760
761 case KeyEvent.KEYCODE_SLASH:
762 // When verbose logging is enabled, dump the view for debugging/testing purposes.
763 if (LogUtil.isVerboseEnabled()) {
764 View decorView = getWindow().getDecorView();
765 LogUtil.v("InCallActivity.onKeyDown", "View dump:\n%s", decorView);
766 return true;
767 }
768 break;
769
770 case KeyEvent.KEYCODE_EQUALS:
771 break;
772
773 default: // fall out
774 }
775
776 // Pass other key events to DialpadFragment's "onDialerKeyDown" method in case the user types
777 // in DTMF (Dual-tone multi-frequency signaling) code.
778 DialpadFragment dialpadFragment = getDialpadFragment();
779 if (dialpadFragment != null
780 && dialpadFragment.isVisible()
781 && dialpadFragment.onDialerKeyDown(event)) {
782 return true;
783 }
784
785 return super.onKeyDown(keyCode, event);
Eric Erfanianccca3152017-02-22 16:32:36 -0800786 }
787
788 public boolean isInCallScreenAnimating() {
789 return false;
790 }
791
792 public void showConferenceFragment(boolean show) {
793 if (show) {
794 startActivity(new Intent(this, ManageConferenceActivity.class));
795 }
796 }
797
linyuhc3968e62017-11-20 17:40:50 -0800798 public void showDialpadFragment(boolean show, boolean animate) {
799 if (show == isDialpadVisible()) {
800 return;
Eric Erfanianccca3152017-02-22 16:32:36 -0800801 }
linyuhc3968e62017-11-20 17:40:50 -0800802
803 FragmentManager dialpadFragmentManager = getDialpadFragmentManager();
804 if (dialpadFragmentManager == null) {
805 LogUtil.i("InCallActivity.showDialpadFragment", "Unable to obtain a FragmentManager");
806 return;
807 }
808
809 if (!animate) {
810 if (show) {
811 showDialpadFragment();
812 } else {
813 hideDialpadFragment();
814 }
815 } else {
816 if (show) {
817 showDialpadFragment();
818 getDialpadFragment().animateShowDialpad();
819 }
820 getDialpadFragment()
821 .getView()
822 .startAnimation(show ? dialpadSlideInAnimation : dialpadSlideOutAnimation);
823 }
824
825 ProximitySensor sensor = InCallPresenter.getInstance().getProximitySensor();
826 if (sensor != null) {
827 sensor.onDialpadVisible(show);
828 }
829 showDialpadRequest = DIALPAD_REQUEST_NONE;
830
831 // Note: onInCallScreenDialpadVisibilityChange is called here to ensure that the dialpad FAB
832 // repositions itself.
833 getInCallScreen().onInCallScreenDialpadVisibilityChange(show);
834 }
835
836 private void showDialpadFragment() {
837 FragmentManager dialpadFragmentManager = getDialpadFragmentManager();
838 if (dialpadFragmentManager == null) {
839 return;
840 }
841
842 FragmentTransaction transaction = dialpadFragmentManager.beginTransaction();
843 DialpadFragment dialpadFragment = getDialpadFragment();
844 if (dialpadFragment == null) {
845 transaction.add(getDialpadContainerId(), new DialpadFragment(), Tags.DIALPAD_FRAGMENT);
846 } else {
847 transaction.show(dialpadFragment);
calderwoodrad5883872017-12-12 15:29:12 -0800848 dialpadFragment.setUserVisibleHint(true);
linyuhc3968e62017-11-20 17:40:50 -0800849 }
850 transaction.commitAllowingStateLoss();
851 dialpadFragmentManager.executePendingTransactions();
852
853 Logger.get(this).logScreenView(ScreenEvent.Type.INCALL_DIALPAD, this);
854 updateNavigationBar(true /* isDialpadVisible */);
855 }
856
857 private void hideDialpadFragment() {
858 FragmentManager dialpadFragmentManager = getDialpadFragmentManager();
859 if (dialpadFragmentManager == null) {
860 return;
861 }
862
calderwoodrad5883872017-12-12 15:29:12 -0800863 DialpadFragment dialpadFragment = getDialpadFragment();
linyuhc3968e62017-11-20 17:40:50 -0800864 if (dialpadFragment != null) {
865 FragmentTransaction transaction = dialpadFragmentManager.beginTransaction();
866 transaction.hide(dialpadFragment);
867 transaction.commitAllowingStateLoss();
868 dialpadFragmentManager.executePendingTransactions();
calderwoodrad5883872017-12-12 15:29:12 -0800869 dialpadFragment.setUserVisibleHint(false);
linyuhc3968e62017-11-20 17:40:50 -0800870 }
871 updateNavigationBar(false /* isDialpadVisible */);
Eric Erfanianccca3152017-02-22 16:32:36 -0800872 }
873
874 public boolean isDialpadVisible() {
linyuh69a25062017-11-15 16:18:51 -0800875 DialpadFragment dialpadFragment = getDialpadFragment();
calderwoodraa584bcd2018-01-24 12:19:56 -0800876 return dialpadFragment != null
877 && dialpadFragment.isAdded()
878 && !dialpadFragment.isHidden()
879 && dialpadFragment.getView() != null
880 && dialpadFragment.getUserVisibleHint();
linyuh69a25062017-11-15 16:18:51 -0800881 }
882
linyuhc3968e62017-11-20 17:40:50 -0800883 /** Returns the {@link DialpadFragment} that's shown by this activity, or {@code null} */
linyuh69a25062017-11-15 16:18:51 -0800884 @Nullable
linyuhc3968e62017-11-20 17:40:50 -0800885 private DialpadFragment getDialpadFragment() {
linyuh69a25062017-11-15 16:18:51 -0800886 FragmentManager fragmentManager = getDialpadFragmentManager();
887 if (fragmentManager == null) {
888 return null;
889 }
linyuhc3968e62017-11-20 17:40:50 -0800890 return (DialpadFragment) fragmentManager.findFragmentByTag(Tags.DIALPAD_FRAGMENT);
Eric Erfanianccca3152017-02-22 16:32:36 -0800891 }
892
893 public void onForegroundCallChanged(DialerCall newForegroundCall) {
linyuh57b093b2017-11-17 14:32:32 -0800894 updateTaskDescription();
895
896 if (newForegroundCall == null || !didShowAnswerScreen) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800897 LogUtil.v("InCallActivity.onForegroundCallChanged", "resetting background color");
linyuh57b093b2017-11-17 14:32:32 -0800898 updateWindowBackgroundColor(0 /* progress */);
Eric Erfanianccca3152017-02-22 16:32:36 -0800899 }
900 }
901
linyuhc3968e62017-11-20 17:40:50 -0800902 private void updateTaskDescription() {
linyuh57b093b2017-11-17 14:32:32 -0800903 int color =
904 getResources().getBoolean(R.bool.is_layout_landscape)
905 ? ResourcesCompat.getColor(
906 getResources(), R.color.statusbar_background_color, getTheme())
907 : InCallPresenter.getInstance().getThemeColorManager().getSecondaryColor();
908 setTaskDescription(
909 new TaskDescription(
910 getResources().getString(R.string.notification_ongoing_call), null /* icon */, color));
911 }
912
Eric Erfanianccca3152017-02-22 16:32:36 -0800913 public void updateWindowBackgroundColor(@FloatRange(from = -1f, to = 1.0f) float progress) {
914 ThemeColorManager themeColorManager = InCallPresenter.getInstance().getThemeColorManager();
915 @ColorInt int top;
916 @ColorInt int middle;
917 @ColorInt int bottom;
918 @ColorInt int gray = 0x66000000;
919
920 if (ActivityCompat.isInMultiWindowMode(this)) {
921 top = themeColorManager.getBackgroundColorSolid();
922 middle = themeColorManager.getBackgroundColorSolid();
923 bottom = themeColorManager.getBackgroundColorSolid();
924 } else {
925 top = themeColorManager.getBackgroundColorTop();
926 middle = themeColorManager.getBackgroundColorMiddle();
927 bottom = themeColorManager.getBackgroundColorBottom();
928 }
929
930 if (progress < 0) {
931 float correctedProgress = Math.abs(progress);
932 top = ColorUtils.blendARGB(top, gray, correctedProgress);
933 middle = ColorUtils.blendARGB(middle, gray, correctedProgress);
934 bottom = ColorUtils.blendARGB(bottom, gray, correctedProgress);
935 }
936
937 boolean backgroundDirty = false;
938 if (backgroundDrawable == null) {
939 backgroundDrawableColors = new int[] {top, middle, bottom};
940 backgroundDrawable = new GradientDrawable(Orientation.TOP_BOTTOM, backgroundDrawableColors);
941 backgroundDirty = true;
942 } else {
943 if (backgroundDrawableColors[0] != top) {
944 backgroundDrawableColors[0] = top;
945 backgroundDirty = true;
946 }
947 if (backgroundDrawableColors[1] != middle) {
948 backgroundDrawableColors[1] = middle;
949 backgroundDirty = true;
950 }
951 if (backgroundDrawableColors[2] != bottom) {
952 backgroundDrawableColors[2] = bottom;
953 backgroundDirty = true;
954 }
955 if (backgroundDirty) {
956 backgroundDrawable.setColors(backgroundDrawableColors);
957 }
958 }
959
960 if (backgroundDirty) {
961 getWindow().setBackgroundDrawable(backgroundDrawable);
962 }
963 }
964
965 public boolean isVisible() {
966 return isVisible;
967 }
968
969 public boolean getCallCardFragmentVisible() {
970 return didShowInCallScreen || didShowVideoCallScreen;
971 }
972
973 public void dismissKeyguard(boolean dismiss) {
linyuh9c327da2017-11-14 12:33:48 -0800974 if (dismissKeyguard == dismiss) {
975 return;
976 }
977
978 dismissKeyguard = dismiss;
979 if (dismiss) {
980 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
981 } else {
982 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
983 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800984 }
985
linyuhc3968e62017-11-20 17:40:50 -0800986 public void showDialogForPostCharWait(String callId, String chars) {
987 if (isVisible) {
988 PostCharDialogFragment fragment = new PostCharDialogFragment(callId, chars);
989 fragment.show(getSupportFragmentManager(), Tags.POST_CHAR_DIALOG_FRAGMENT);
990
991 showPostCharWaitDialogOnResume = false;
992 showPostCharWaitDialogCallId = null;
993 showPostCharWaitDialogChars = null;
994 } else {
995 showPostCharWaitDialogOnResume = true;
996 showPostCharWaitDialogCallId = callId;
997 showPostCharWaitDialogChars = chars;
998 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800999 }
1000
linyuh7b86f562017-11-16 11:24:09 -08001001 public void showDialogOrToastForDisconnectedCall(DisconnectMessage disconnectMessage) {
1002 LogUtil.i(
1003 "InCallActivity.showDialogOrToastForDisconnectedCall",
1004 "disconnect cause: %s",
1005 disconnectMessage);
1006
1007 if (disconnectMessage.dialog == null || isFinishing()) {
1008 return;
1009 }
1010
1011 dismissPendingDialogs();
1012
1013 // Show a toast if the app is in background when a dialog can't be visible.
1014 if (!isVisible()) {
1015 Toast.makeText(getApplicationContext(), disconnectMessage.toastMessage, Toast.LENGTH_LONG)
1016 .show();
1017 return;
1018 }
1019
1020 // Show the dialog.
linyuhc3968e62017-11-20 17:40:50 -08001021 errorDialog = disconnectMessage.dialog;
linyuh7b86f562017-11-16 11:24:09 -08001022 InCallUiLock lock = InCallPresenter.getInstance().acquireInCallUiLock("showErrorDialog");
1023 disconnectMessage.dialog.setOnDismissListener(
1024 dialogInterface -> {
1025 lock.release();
1026 onDialogDismissed();
1027 });
1028 disconnectMessage.dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
1029 disconnectMessage.dialog.show();
1030 }
1031
1032 private void onDialogDismissed() {
linyuhc3968e62017-11-20 17:40:50 -08001033 errorDialog = null;
linyuh7b86f562017-11-16 11:24:09 -08001034 CallList.getInstance().onErrorDialogDismissed();
Eric Erfanianccca3152017-02-22 16:32:36 -08001035 }
1036
1037 public void dismissPendingDialogs() {
linyuhc3968e62017-11-20 17:40:50 -08001038 LogUtil.enterBlock("InCallActivity.dismissPendingDialogs");
linyuhf99f6302017-11-15 11:23:51 -08001039
1040 if (!isVisible) {
1041 // Defer the dismissing action as the activity is not visible and onSaveInstanceState may have
1042 // been called.
Eric Erfanianccca3152017-02-22 16:32:36 -08001043 LogUtil.i(
1044 "InCallActivity.dismissPendingDialogs", "defer actions since activity is not visible");
1045 needDismissPendingDialogs = true;
linyuhf99f6302017-11-15 11:23:51 -08001046 return;
Eric Erfanianccca3152017-02-22 16:32:36 -08001047 }
linyuhf99f6302017-11-15 11:23:51 -08001048
1049 // Dismiss the error dialog
linyuhf99f6302017-11-15 11:23:51 -08001050 if (errorDialog != null) {
1051 errorDialog.dismiss();
linyuhc3968e62017-11-20 17:40:50 -08001052 errorDialog = null;
linyuhf99f6302017-11-15 11:23:51 -08001053 }
1054
1055 // Dismiss the phone account selection dialog
linyuhf99f6302017-11-15 11:23:51 -08001056 if (selectPhoneAccountDialogFragment != null) {
1057 selectPhoneAccountDialogFragment.dismiss();
linyuhc3968e62017-11-20 17:40:50 -08001058 selectPhoneAccountDialogFragment = null;
linyuhf99f6302017-11-15 11:23:51 -08001059 }
1060
1061 // Dismiss the dialog for international call on WiFi
1062 InternationalCallOnWifiDialogFragment internationalCallOnWifiFragment =
1063 (InternationalCallOnWifiDialogFragment)
linyuhc3968e62017-11-20 17:40:50 -08001064 getSupportFragmentManager().findFragmentByTag(Tags.INTERNATIONAL_CALL_ON_WIFI);
linyuhf99f6302017-11-15 11:23:51 -08001065 if (internationalCallOnWifiFragment != null) {
1066 internationalCallOnWifiFragment.dismiss();
1067 }
1068
1069 // Dismiss the answer screen
1070 AnswerScreen answerScreen = getAnswerScreen();
1071 if (answerScreen != null) {
1072 answerScreen.dismissPendingDialogs();
1073 }
1074
1075 needDismissPendingDialogs = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001076 }
1077
linyuhc3968e62017-11-20 17:40:50 -08001078 private void enableInCallOrientationEventListener(boolean enable) {
linyuh69a25062017-11-15 16:18:51 -08001079 if (enable) {
1080 inCallOrientationEventListener.enable(true /* notifyDeviceOrientationChange */);
1081 } else {
1082 inCallOrientationEventListener.disable();
1083 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001084 }
1085
1086 public void setExcludeFromRecents(boolean exclude) {
linyuhc3968e62017-11-20 17:40:50 -08001087 int taskId = getTaskId();
1088
1089 List<AppTask> tasks = getSystemService(ActivityManager.class).getAppTasks();
1090 for (AppTask task : tasks) {
1091 try {
1092 if (task.getTaskInfo().id == taskId) {
1093 task.setExcludeFromRecents(exclude);
1094 }
1095 } catch (RuntimeException e) {
1096 LogUtil.e("InCallActivity.setExcludeFromRecents", "RuntimeException:\n%s", e);
1097 }
1098 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001099 }
1100
Eric Erfanianccca3152017-02-22 16:32:36 -08001101 @Nullable
1102 public FragmentManager getDialpadFragmentManager() {
1103 InCallScreen inCallScreen = getInCallScreen();
1104 if (inCallScreen != null) {
1105 return inCallScreen.getInCallScreenFragment().getChildFragmentManager();
1106 }
1107 return null;
1108 }
1109
1110 public int getDialpadContainerId() {
1111 return getInCallScreen().getAnswerAndDialpadContainerResourceId();
1112 }
1113
1114 @Override
1115 public AnswerScreenDelegate newAnswerScreenDelegate(AnswerScreen answerScreen) {
1116 DialerCall call = CallList.getInstance().getCallById(answerScreen.getCallId());
1117 if (call == null) {
1118 // This is a work around for a bug where we attempt to create a new delegate after the call
1119 // has already been removed. An example of when this can happen is:
1120 // 1. incoming video call in landscape mode
1121 // 2. remote party hangs up
1122 // 3. activity switches from landscape to portrait
1123 // At step #3 the answer fragment will try to create a new answer delegate but the call won't
1124 // exist. In this case we'll simply return a stub delegate that does nothing. This is ok
1125 // because this new state is transient and the activity will be destroyed soon.
1126 LogUtil.i("InCallActivity.onPrimaryCallStateChanged", "call doesn't exist, using stub");
1127 return new AnswerScreenPresenterStub();
1128 } else {
1129 return new AnswerScreenPresenter(
1130 this, answerScreen, CallList.getInstance().getCallById(answerScreen.getCallId()));
1131 }
1132 }
1133
1134 @Override
1135 public InCallScreenDelegate newInCallScreenDelegate() {
1136 return new CallCardPresenter(this);
1137 }
1138
1139 @Override
1140 public InCallButtonUiDelegate newInCallButtonUiDelegate() {
1141 return new CallButtonPresenter(this);
1142 }
1143
1144 @Override
Eric Erfanian90508232017-03-24 09:31:16 -07001145 public VideoCallScreenDelegate newVideoCallScreenDelegate(VideoCallScreen videoCallScreen) {
1146 DialerCall dialerCall = CallList.getInstance().getCallById(videoCallScreen.getCallId());
1147 if (dialerCall != null && dialerCall.getVideoTech().shouldUseSurfaceView()) {
1148 return dialerCall.getVideoTech().createVideoCallScreenDelegate(this, videoCallScreen);
1149 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001150 return new VideoCallPresenter();
1151 }
1152
1153 public void onPrimaryCallStateChanged() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001154 Trace.beginSection("InCallActivity.onPrimaryCallStateChanged");
Eric Erfanianccca3152017-02-22 16:32:36 -08001155 showMainInCallFragment();
Eric Erfanian2ca43182017-08-31 06:57:16 -07001156 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001157 }
1158
linyuh7b86f562017-11-16 11:24:09 -08001159 public void showToastForWiFiToLteHandover(DialerCall call) {
1160 if (call.hasShownWiFiToLteHandoverToast()) {
1161 return;
1162 }
1163
1164 Toast.makeText(this, R.string.video_call_wifi_to_lte_handover_toast, Toast.LENGTH_LONG).show();
1165 call.setHasShownWiFiToLteHandoverToast();
Eric Erfanianccca3152017-02-22 16:32:36 -08001166 }
1167
linyuh7b86f562017-11-16 11:24:09 -08001168 public void showDialogOrToastForWifiHandoverFailure(DialerCall call) {
1169 if (call.showWifiHandoverAlertAsToast()) {
1170 Toast.makeText(this, R.string.video_call_lte_to_wifi_failed_message, Toast.LENGTH_SHORT)
1171 .show();
1172 return;
1173 }
1174
1175 dismissPendingDialogs();
1176
1177 AlertDialog.Builder builder =
1178 new AlertDialog.Builder(this).setTitle(R.string.video_call_lte_to_wifi_failed_title);
1179
1180 // This allows us to use the theme of the dialog instead of the activity
1181 View dialogCheckBoxView =
1182 View.inflate(builder.getContext(), R.layout.video_call_lte_to_wifi_failed, null /* root */);
1183 CheckBox wifiHandoverFailureCheckbox =
1184 (CheckBox) dialogCheckBoxView.findViewById(R.id.video_call_lte_to_wifi_failed_checkbox);
1185 wifiHandoverFailureCheckbox.setChecked(false);
1186
1187 InCallUiLock lock = InCallPresenter.getInstance().acquireInCallUiLock("WifiFailedDialog");
linyuhc3968e62017-11-20 17:40:50 -08001188 errorDialog =
linyuh7b86f562017-11-16 11:24:09 -08001189 builder
1190 .setView(dialogCheckBoxView)
1191 .setMessage(R.string.video_call_lte_to_wifi_failed_message)
1192 .setOnCancelListener(dialogInterface -> onDialogDismissed())
1193 .setPositiveButton(
1194 android.R.string.ok,
1195 (dialogInterface, id) -> {
1196 call.setDoNotShowDialogForHandoffToWifiFailure(
1197 wifiHandoverFailureCheckbox.isChecked());
1198 dialogInterface.cancel();
1199 onDialogDismissed();
1200 })
1201 .setOnDismissListener(dialogInterface -> lock.release())
1202 .create();
linyuh7b86f562017-11-16 11:24:09 -08001203 errorDialog.show();
Eric Erfanianccca3152017-02-22 16:32:36 -08001204 }
1205
linyuh7b86f562017-11-16 11:24:09 -08001206 public void showDialogForInternationalCallOnWifi(@NonNull DialerCall call) {
1207 if (!InternationalCallOnWifiDialogFragment.shouldShow(this)) {
1208 LogUtil.i(
1209 "InCallActivity.showDialogForInternationalCallOnWifi",
1210 "InternationalCallOnWifiDialogFragment.shouldShow returned false");
1211 return;
1212 }
1213
1214 InternationalCallOnWifiDialogFragment fragment =
1215 InternationalCallOnWifiDialogFragment.newInstance(
linyuhc3968e62017-11-20 17:40:50 -08001216 call.getId(), internationalCallOnWifiCallback);
1217 fragment.show(getSupportFragmentManager(), Tags.INTERNATIONAL_CALL_ON_WIFI);
Eric Erfanianc857f902017-05-15 14:05:33 -07001218 }
1219
Eric Erfanian938468d2017-10-24 14:05:52 -07001220 @Override
1221 public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
1222 super.onMultiWindowModeChanged(isInMultiWindowMode);
linyuh57b093b2017-11-17 14:32:32 -08001223 updateNavigationBar(isDialpadVisible());
1224 }
1225
linyuhc3968e62017-11-20 17:40:50 -08001226 private void updateNavigationBar(boolean isDialpadVisible) {
linyuh57b093b2017-11-17 14:32:32 -08001227 if (ActivityCompat.isInMultiWindowMode(this)) {
1228 return;
1229 }
1230
1231 View navigationBarBackground = getWindow().findViewById(R.id.navigation_bar_background);
1232 if (navigationBarBackground != null) {
1233 navigationBarBackground.setVisibility(isDialpadVisible ? View.VISIBLE : View.GONE);
Eric Erfanian938468d2017-10-24 14:05:52 -07001234 }
1235 }
1236
Eric Erfanianccca3152017-02-22 16:32:36 -08001237 public void setAllowOrientationChange(boolean allowOrientationChange) {
wangqi9982f0d2017-10-11 17:46:07 -07001238 if (this.allowOrientationChange == allowOrientationChange) {
1239 return;
1240 }
1241 this.allowOrientationChange = allowOrientationChange;
Eric Erfanianccca3152017-02-22 16:32:36 -08001242 if (!allowOrientationChange) {
1243 setRequestedOrientation(InCallOrientationEventListener.ACTIVITY_PREFERENCE_DISALLOW_ROTATION);
1244 } else {
1245 setRequestedOrientation(InCallOrientationEventListener.ACTIVITY_PREFERENCE_ALLOW_ROTATION);
1246 }
1247 enableInCallOrientationEventListener(allowOrientationChange);
1248 }
1249
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001250 public void hideMainInCallFragment() {
linyuhc3968e62017-11-20 17:40:50 -08001251 LogUtil.enterBlock("InCallActivity.hideMainInCallFragment");
1252 if (getCallCardFragmentVisible()) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001253 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
1254 hideInCallScreenFragment(transaction);
1255 hideVideoCallScreenFragment(transaction);
1256 transaction.commitAllowingStateLoss();
1257 getSupportFragmentManager().executePendingTransactions();
1258 }
1259 }
1260
1261 private void showMainInCallFragment() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001262 Trace.beginSection("InCallActivity.showMainInCallFragment");
Eric Erfanianccca3152017-02-22 16:32:36 -08001263 // If the activity's onStart method hasn't been called yet then defer doing any work.
1264 if (!isVisible) {
1265 LogUtil.i("InCallActivity.showMainInCallFragment", "not visible yet/anymore");
Eric Erfanian2ca43182017-08-31 06:57:16 -07001266 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001267 return;
1268 }
1269
1270 // Don't let this be reentrant.
1271 if (isInShowMainInCallFragment) {
1272 LogUtil.i("InCallActivity.showMainInCallFragment", "already in method, bailing");
Eric Erfanian2ca43182017-08-31 06:57:16 -07001273 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001274 return;
1275 }
1276
1277 isInShowMainInCallFragment = true;
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001278 ShouldShowUiResult shouldShowAnswerUi = getShouldShowAnswerUi();
1279 ShouldShowUiResult shouldShowVideoUi = getShouldShowVideoUi();
wangqi219b8702018-02-13 09:34:41 -08001280 ShouldShowUiResult shouldShowRttUi = getShouldShowRttUi();
Eric Erfanianccca3152017-02-22 16:32:36 -08001281 LogUtil.i(
1282 "InCallActivity.showMainInCallFragment",
wangqi219b8702018-02-13 09:34:41 -08001283 "shouldShowAnswerUi: %b, shouldShowRttUi: %b, shouldShowVideoUi: %b "
1284 + "didShowAnswerScreen: %b, didShowInCallScreen: %b, didShowRttCallScreen: %b, "
1285 + "didShowVideoCallScreen: %b",
Eric Erfanianccca3152017-02-22 16:32:36 -08001286 shouldShowAnswerUi.shouldShow,
wangqi219b8702018-02-13 09:34:41 -08001287 shouldShowRttUi.shouldShow,
Eric Erfanian10b34a52017-05-04 08:23:17 -07001288 shouldShowVideoUi.shouldShow,
Eric Erfanianccca3152017-02-22 16:32:36 -08001289 didShowAnswerScreen,
1290 didShowInCallScreen,
wangqi219b8702018-02-13 09:34:41 -08001291 didShowRttCallScreen,
Eric Erfanianccca3152017-02-22 16:32:36 -08001292 didShowVideoCallScreen);
1293 // Only video call ui allows orientation change.
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001294 setAllowOrientationChange(shouldShowVideoUi.shouldShow);
Eric Erfanianccca3152017-02-22 16:32:36 -08001295
1296 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
wangqi219b8702018-02-13 09:34:41 -08001297 boolean didChange;
Eric Erfanianccca3152017-02-22 16:32:36 -08001298 if (shouldShowAnswerUi.shouldShow) {
wangqi219b8702018-02-13 09:34:41 -08001299 didChange = hideInCallScreenFragment(transaction);
1300 didChange |= hideVideoCallScreenFragment(transaction);
1301 didChange |= hideRttCallScreenFragment(transaction);
1302 didChange |= showAnswerScreenFragment(transaction, shouldShowAnswerUi.call);
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001303 } else if (shouldShowVideoUi.shouldShow) {
wangqi219b8702018-02-13 09:34:41 -08001304 didChange = hideInCallScreenFragment(transaction);
1305 didChange |= showVideoCallScreenFragment(transaction, shouldShowVideoUi.call);
1306 didChange |= hideRttCallScreenFragment(transaction);
1307 didChange |= hideAnswerScreenFragment(transaction);
1308 } else if (shouldShowRttUi.shouldShow) {
1309 didChange = hideInCallScreenFragment(transaction);
1310 didChange |= hideVideoCallScreenFragment(transaction);
1311 didChange |= hideAnswerScreenFragment(transaction);
1312 didChange |= showRttCallScreenFragment(transaction, shouldShowRttUi.call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001313 } else {
wangqi219b8702018-02-13 09:34:41 -08001314 didChange = showInCallScreenFragment(transaction);
1315 didChange |= hideVideoCallScreenFragment(transaction);
1316 didChange |= hideRttCallScreenFragment(transaction);
1317 didChange |= hideAnswerScreenFragment(transaction);
Eric Erfanianccca3152017-02-22 16:32:36 -08001318 }
1319
wangqi219b8702018-02-13 09:34:41 -08001320 if (didChange) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001321 Trace.beginSection("InCallActivity.commitTransaction");
Eric Erfanianccca3152017-02-22 16:32:36 -08001322 transaction.commitNow();
Eric Erfanian2ca43182017-08-31 06:57:16 -07001323 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001324 Logger.get(this).logScreenView(ScreenEvent.Type.INCALL, this);
1325 }
1326 isInShowMainInCallFragment = false;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001327 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001328 }
1329
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001330 private ShouldShowUiResult getShouldShowAnswerUi() {
Eric Erfanianccca3152017-02-22 16:32:36 -08001331 DialerCall call = CallList.getInstance().getIncomingCall();
1332 if (call != null) {
1333 LogUtil.i("InCallActivity.getShouldShowAnswerUi", "found incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001334 return new ShouldShowUiResult(true, call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001335 }
1336
1337 call = CallList.getInstance().getVideoUpgradeRequestCall();
1338 if (call != null) {
1339 LogUtil.i("InCallActivity.getShouldShowAnswerUi", "found video upgrade request");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001340 return new ShouldShowUiResult(true, call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001341 }
1342
1343 // Check if we're showing the answer screen and the call is disconnected. If this condition is
1344 // true then we won't switch from the answer UI to the in call UI. This prevents flicker when
1345 // the user rejects an incoming call.
1346 call = CallList.getInstance().getFirstCall();
1347 if (call == null) {
1348 call = CallList.getInstance().getBackgroundCall();
1349 }
1350 if (didShowAnswerScreen && (call == null || call.getState() == State.DISCONNECTED)) {
1351 LogUtil.i("InCallActivity.getShouldShowAnswerUi", "found disconnecting incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001352 return new ShouldShowUiResult(true, call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001353 }
1354
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001355 return new ShouldShowUiResult(false, null);
Eric Erfanianccca3152017-02-22 16:32:36 -08001356 }
1357
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001358 private static ShouldShowUiResult getShouldShowVideoUi() {
Eric Erfanianccca3152017-02-22 16:32:36 -08001359 DialerCall call = CallList.getInstance().getFirstCall();
1360 if (call == null) {
1361 LogUtil.i("InCallActivity.getShouldShowVideoUi", "null call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001362 return new ShouldShowUiResult(false, null);
Eric Erfanianccca3152017-02-22 16:32:36 -08001363 }
1364
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001365 if (call.isVideoCall()) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001366 LogUtil.i("InCallActivity.getShouldShowVideoUi", "found video call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001367 return new ShouldShowUiResult(true, call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001368 }
1369
linyuh8fbecce2017-12-18 13:53:09 -08001370 if (call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest()) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001371 LogUtil.i("InCallActivity.getShouldShowVideoUi", "upgrading to video");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001372 return new ShouldShowUiResult(true, call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001373 }
1374
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001375 return new ShouldShowUiResult(false, null);
Eric Erfanianccca3152017-02-22 16:32:36 -08001376 }
1377
wangqi219b8702018-02-13 09:34:41 -08001378 private static ShouldShowUiResult getShouldShowRttUi() {
1379 DialerCall call = CallList.getInstance().getFirstCall();
1380 if (call == null) {
1381 LogUtil.i("InCallActivity.getShouldShowRttUi", "null call");
1382 return new ShouldShowUiResult(false, null);
1383 }
1384
1385 if (call.isRttCall()) {
1386 LogUtil.i("InCallActivity.getShouldShowRttUi", "found rtt call");
1387 return new ShouldShowUiResult(true, call);
1388 }
1389
1390 if (call.hasSentRttUpgradeRequest()) {
1391 LogUtil.i("InCallActivity.getShouldShowRttUi", "upgrading to rtt");
1392 return new ShouldShowUiResult(true, call);
1393 }
1394
1395 return new ShouldShowUiResult(false, null);
1396 }
1397
Eric Erfanianccca3152017-02-22 16:32:36 -08001398 private boolean showAnswerScreenFragment(FragmentTransaction transaction, DialerCall call) {
1399 // When rejecting a call the active call can become null in which case we should continue
1400 // showing the answer screen.
1401 if (didShowAnswerScreen && call == null) {
1402 return false;
1403 }
1404
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001405 Assert.checkArgument(call != null, "didShowAnswerScreen was false but call was still null");
1406
1407 boolean isVideoUpgradeRequest = call.hasReceivedVideoUpgradeRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -08001408
1409 // Check if we're already showing an answer screen for this call.
1410 if (didShowAnswerScreen) {
1411 AnswerScreen answerScreen = getAnswerScreen();
1412 if (answerScreen.getCallId().equals(call.getId())
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001413 && answerScreen.isVideoCall() == call.isVideoCall()
Eric Erfanian2ca43182017-08-31 06:57:16 -07001414 && answerScreen.isVideoUpgradeRequest() == isVideoUpgradeRequest
1415 && !answerScreen.isActionTimeout()) {
1416 LogUtil.d(
1417 "InCallActivity.showAnswerScreenFragment",
1418 "answer fragment exists for same call and has NOT been accepted/rejected/timed out");
Eric Erfanianccca3152017-02-22 16:32:36 -08001419 return false;
1420 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001421 if (answerScreen.isActionTimeout()) {
1422 LogUtil.i(
1423 "InCallActivity.showAnswerScreenFragment",
1424 "answer fragment exists but has been accepted/rejected and timed out");
1425 } else {
1426 LogUtil.i(
1427 "InCallActivity.showAnswerScreenFragment",
1428 "answer fragment exists but arguments do not match");
1429 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001430 hideAnswerScreenFragment(transaction);
1431 }
1432
1433 // Show a new answer screen.
1434 AnswerScreen answerScreen =
Eric Erfanianfc37b022017-03-21 10:11:17 -07001435 AnswerBindings.createAnswerScreen(
1436 call.getId(),
wangqi219b8702018-02-13 09:34:41 -08001437 call.isRttCall(),
Eric Erfanianfc37b022017-03-21 10:11:17 -07001438 call.isVideoCall(),
1439 isVideoUpgradeRequest,
Eric Erfanian90508232017-03-24 09:31:16 -07001440 call.getVideoTech().isSelfManagedCamera(),
1441 shouldAllowAnswerAndRelease(call),
1442 CallList.getInstance().getBackgroundCall() != null);
linyuhc3968e62017-11-20 17:40:50 -08001443 transaction.add(R.id.main, answerScreen.getAnswerScreenFragment(), Tags.ANSWER_SCREEN);
Eric Erfanianccca3152017-02-22 16:32:36 -08001444
1445 Logger.get(this).logScreenView(ScreenEvent.Type.INCOMING_CALL, this);
1446 didShowAnswerScreen = true;
1447 return true;
1448 }
1449
Eric Erfanian90508232017-03-24 09:31:16 -07001450 private boolean shouldAllowAnswerAndRelease(DialerCall call) {
1451 if (CallList.getInstance().getActiveCall() == null) {
1452 LogUtil.i("InCallActivity.shouldAllowAnswerAndRelease", "no active call");
1453 return false;
1454 }
1455 if (getSystemService(TelephonyManager.class).getPhoneType()
1456 == TelephonyManager.PHONE_TYPE_CDMA) {
1457 LogUtil.i("InCallActivity.shouldAllowAnswerAndRelease", "PHONE_TYPE_CDMA not supported");
1458 return false;
1459 }
1460 if (call.isVideoCall() || call.hasReceivedVideoUpgradeRequest()) {
1461 LogUtil.i("InCallActivity.shouldAllowAnswerAndRelease", "video call");
1462 return false;
1463 }
linyuhc3968e62017-11-20 17:40:50 -08001464 if (!ConfigProviderBindings.get(this)
1465 .getBoolean(ConfigNames.ANSWER_AND_RELEASE_ENABLED, true)) {
Eric Erfanian90508232017-03-24 09:31:16 -07001466 LogUtil.i("InCallActivity.shouldAllowAnswerAndRelease", "disabled by config");
1467 return false;
1468 }
1469
1470 return true;
1471 }
1472
Eric Erfanianccca3152017-02-22 16:32:36 -08001473 private boolean hideAnswerScreenFragment(FragmentTransaction transaction) {
1474 if (!didShowAnswerScreen) {
1475 return false;
1476 }
1477 AnswerScreen answerScreen = getAnswerScreen();
1478 if (answerScreen != null) {
1479 transaction.remove(answerScreen.getAnswerScreenFragment());
1480 }
1481
1482 didShowAnswerScreen = false;
1483 return true;
1484 }
1485
1486 private boolean showInCallScreenFragment(FragmentTransaction transaction) {
1487 if (didShowInCallScreen) {
1488 return false;
1489 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001490 InCallScreen inCallScreen = InCallBindings.createInCallScreen();
linyuhc3968e62017-11-20 17:40:50 -08001491 transaction.add(R.id.main, inCallScreen.getInCallScreenFragment(), Tags.IN_CALL_SCREEN);
Eric Erfanianccca3152017-02-22 16:32:36 -08001492 Logger.get(this).logScreenView(ScreenEvent.Type.INCALL, this);
1493 didShowInCallScreen = true;
1494 return true;
1495 }
1496
1497 private boolean hideInCallScreenFragment(FragmentTransaction transaction) {
1498 if (!didShowInCallScreen) {
1499 return false;
1500 }
1501 InCallScreen inCallScreen = getInCallScreen();
1502 if (inCallScreen != null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001503 transaction.remove(inCallScreen.getInCallScreenFragment());
Eric Erfanianccca3152017-02-22 16:32:36 -08001504 }
1505 didShowInCallScreen = false;
1506 return true;
1507 }
1508
wangqi219b8702018-02-13 09:34:41 -08001509 private boolean showRttCallScreenFragment(FragmentTransaction transaction, DialerCall call) {
1510 if (didShowRttCallScreen) {
1511 // This shouldn't happen since only one RTT call is allow at same time.
1512 if (!getRttCallScreen().getCallId().equals(call.getId())) {
1513 LogUtil.e("InCallActivity.showRttCallScreenFragment", "RTT call id doesn't match");
1514 }
1515 return false;
1516 }
1517 RttCallScreen rttCallScreen = RttBindings.createRttCallScreen(call.getId());
1518 transaction.add(R.id.main, rttCallScreen.getRttCallScreenFragment(), Tags.RTT_CALL_SCREEN);
1519 Logger.get(this).logScreenView(ScreenEvent.Type.INCALL, this);
1520 didShowRttCallScreen = true;
1521 return true;
1522 }
1523
1524 private boolean hideRttCallScreenFragment(FragmentTransaction transaction) {
1525 if (!didShowRttCallScreen) {
1526 return false;
1527 }
1528 RttCallScreen rttCallScreen = getRttCallScreen();
1529 if (rttCallScreen != null) {
1530 transaction.remove(rttCallScreen.getRttCallScreenFragment());
1531 }
1532 didShowRttCallScreen = false;
1533 return true;
1534 }
1535
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001536 private boolean showVideoCallScreenFragment(FragmentTransaction transaction, DialerCall call) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001537 if (didShowVideoCallScreen) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001538 VideoCallScreen videoCallScreen = getVideoCallScreen();
1539 if (videoCallScreen.getCallId().equals(call.getId())) {
1540 return false;
1541 }
1542 LogUtil.i(
1543 "InCallActivity.showVideoCallScreenFragment",
1544 "video call fragment exists but arguments do not match");
1545 hideVideoCallScreenFragment(transaction);
Eric Erfanianccca3152017-02-22 16:32:36 -08001546 }
1547
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001548 LogUtil.i("InCallActivity.showVideoCallScreenFragment", "call: %s", call);
1549
Eric Erfanian90508232017-03-24 09:31:16 -07001550 VideoCallScreen videoCallScreen =
1551 VideoBindings.createVideoCallScreen(
1552 call.getId(), call.getVideoTech().shouldUseSurfaceView());
linyuhc3968e62017-11-20 17:40:50 -08001553 transaction.add(
1554 R.id.main, videoCallScreen.getVideoCallScreenFragment(), Tags.VIDEO_CALL_SCREEN);
Eric Erfanianccca3152017-02-22 16:32:36 -08001555
1556 Logger.get(this).logScreenView(ScreenEvent.Type.INCALL, this);
1557 didShowVideoCallScreen = true;
1558 return true;
1559 }
1560
1561 private boolean hideVideoCallScreenFragment(FragmentTransaction transaction) {
1562 if (!didShowVideoCallScreen) {
1563 return false;
1564 }
1565 VideoCallScreen videoCallScreen = getVideoCallScreen();
1566 if (videoCallScreen != null) {
1567 transaction.remove(videoCallScreen.getVideoCallScreenFragment());
1568 }
1569 didShowVideoCallScreen = false;
1570 return true;
1571 }
1572
linyuhc3968e62017-11-20 17:40:50 -08001573 private AnswerScreen getAnswerScreen() {
1574 return (AnswerScreen) getSupportFragmentManager().findFragmentByTag(Tags.ANSWER_SCREEN);
Eric Erfanianccca3152017-02-22 16:32:36 -08001575 }
1576
linyuhc3968e62017-11-20 17:40:50 -08001577 private InCallScreen getInCallScreen() {
1578 return (InCallScreen) getSupportFragmentManager().findFragmentByTag(Tags.IN_CALL_SCREEN);
Eric Erfanianccca3152017-02-22 16:32:36 -08001579 }
1580
linyuhc3968e62017-11-20 17:40:50 -08001581 private VideoCallScreen getVideoCallScreen() {
1582 return (VideoCallScreen) getSupportFragmentManager().findFragmentByTag(Tags.VIDEO_CALL_SCREEN);
Eric Erfanianccca3152017-02-22 16:32:36 -08001583 }
1584
wangqi219b8702018-02-13 09:34:41 -08001585 private RttCallScreen getRttCallScreen() {
1586 return (RttCallScreen) getSupportFragmentManager().findFragmentByTag(Tags.RTT_CALL_SCREEN);
1587 }
1588
Eric Erfanianccca3152017-02-22 16:32:36 -08001589 @Override
1590 public void onPseudoScreenStateChanged(boolean isOn) {
1591 LogUtil.i("InCallActivity.onPseudoScreenStateChanged", "isOn: " + isOn);
1592 pseudoBlackScreenOverlay.setVisibility(isOn ? View.GONE : View.VISIBLE);
1593 }
1594
1595 /**
1596 * For some touch related issue, turning off the screen can be faked by drawing a black view over
1597 * the activity. All touch events started when the screen is "off" is rejected.
1598 *
1599 * @see PseudoScreenState
1600 */
1601 @Override
1602 public boolean dispatchTouchEvent(MotionEvent event) {
1603 // Reject any gesture that started when the screen is in the fake off state.
1604 if (touchDownWhenPseudoScreenOff) {
1605 if (event.getAction() == MotionEvent.ACTION_UP) {
1606 touchDownWhenPseudoScreenOff = false;
1607 }
1608 return true;
1609 }
1610 // Reject all touch event when the screen is in the fake off state.
1611 if (!InCallPresenter.getInstance().getPseudoScreenState().isOn()) {
1612 if (event.getAction() == MotionEvent.ACTION_DOWN) {
1613 touchDownWhenPseudoScreenOff = true;
1614 LogUtil.i("InCallActivity.dispatchTouchEvent", "touchDownWhenPseudoScreenOff");
1615 }
1616 return true;
1617 }
1618 return super.dispatchTouchEvent(event);
1619 }
1620
wangqi219b8702018-02-13 09:34:41 -08001621 @Override
1622 public RttCallScreenDelegate newRttCallScreenDelegate(RttCallScreen videoCallScreen) {
1623 return new RttCallPresenter();
1624 }
1625
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001626 private static class ShouldShowUiResult {
Eric Erfanianccca3152017-02-22 16:32:36 -08001627 public final boolean shouldShow;
1628 public final DialerCall call;
1629
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001630 ShouldShowUiResult(boolean shouldShow, DialerCall call) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001631 this.shouldShow = shouldShow;
1632 this.call = call;
1633 }
1634 }
linyuhc3968e62017-11-20 17:40:50 -08001635
1636 private static final class IntentExtraNames {
1637 static final String FOR_FULL_SCREEN = "InCallActivity.for_full_screen_intent";
1638 static final String NEW_OUTGOING_CALL = "InCallActivity.new_outgoing_call";
1639 static final String SHOW_DIALPAD = "InCallActivity.show_dialpad";
1640 }
1641
1642 private static final class KeysForSavedInstance {
1643 static final String DIALPAD_TEXT = "InCallActivity.dialpad_text";
1644 static final String DID_SHOW_ANSWER_SCREEN = "did_show_answer_screen";
1645 static final String DID_SHOW_IN_CALL_SCREEN = "did_show_in_call_screen";
1646 static final String DID_SHOW_VIDEO_CALL_SCREEN = "did_show_video_call_screen";
wangqi153af2f2018-02-15 16:21:49 -08001647 static final String DID_SHOW_RTT_CALL_SCREEN = "did_show_rtt_call_screen";
linyuhc3968e62017-11-20 17:40:50 -08001648 }
1649
1650 /** Request codes for pending intents. */
1651 public static final class PendingIntentRequestCodes {
1652 static final int NON_FULL_SCREEN = 0;
1653 static final int FULL_SCREEN = 1;
1654 static final int BUBBLE = 2;
1655 }
1656
1657 private static final class Tags {
1658 static final String ANSWER_SCREEN = "tag_answer_screen";
1659 static final String DIALPAD_FRAGMENT = "tag_dialpad_fragment";
1660 static final String IN_CALL_SCREEN = "tag_in_call_screen";
1661 static final String INTERNATIONAL_CALL_ON_WIFI = "tag_international_call_on_wifi";
1662 static final String SELECT_ACCOUNT_FRAGMENT = "tag_select_account_fragment";
1663 static final String VIDEO_CALL_SCREEN = "tag_video_call_screen";
wangqi219b8702018-02-13 09:34:41 -08001664 static final String RTT_CALL_SCREEN = "tag_rtt_call_screen";
linyuhc3968e62017-11-20 17:40:50 -08001665 static final String POST_CHAR_DIALOG_FRAGMENT = "tag_post_char_dialog_fragment";
1666 }
1667
1668 private static final class ConfigNames {
1669 static final String ANSWER_AND_RELEASE_ENABLED = "answer_and_release_enabled";
1670 }
1671
1672 private static final class InternationalCallOnWifiCallback
1673 implements InternationalCallOnWifiDialogFragment.Callback {
1674 private static final String TAG = InternationalCallOnWifiCallback.class.getCanonicalName();
1675
1676 @Override
1677 public void continueCall(@NonNull String callId) {
1678 LogUtil.i(TAG, "Continuing call with ID: %s", callId);
1679 }
1680
1681 @Override
1682 public void cancelCall(@NonNull String callId) {
1683 DialerCall call = CallList.getInstance().getCallById(callId);
1684 if (call == null) {
1685 LogUtil.i(TAG, "Call destroyed before the dialog is closed");
1686 return;
1687 }
1688
1689 LogUtil.i(TAG, "Disconnecting international call on WiFi");
1690 call.disconnect();
1691 }
1692 }
1693
1694 private static final class SelectPhoneAccountListener
1695 extends SelectPhoneAccountDialogFragment.SelectPhoneAccountListener {
1696 private static final String TAG = SelectPhoneAccountListener.class.getCanonicalName();
1697
twyen73a74c32018-03-07 12:12:24 -08001698 private final Context appContext;
1699
1700 SelectPhoneAccountListener(Context appContext) {
1701 this.appContext = appContext;
1702 }
1703
linyuhc3968e62017-11-20 17:40:50 -08001704 @Override
1705 public void onPhoneAccountSelected(
1706 PhoneAccountHandle selectedAccountHandle, boolean setDefault, String callId) {
1707 DialerCall call = CallList.getInstance().getCallById(callId);
1708 LogUtil.i(TAG, "Phone account select with call:\n%s", call);
1709
1710 if (call != null) {
twyen73a74c32018-03-07 12:12:24 -08001711 call.phoneAccountSelected(selectedAccountHandle, false);
1712 if (call.getPreferredAccountRecorder() != null) {
1713 call.getPreferredAccountRecorder().record(appContext, selectedAccountHandle, setDefault);
1714 }
linyuhc3968e62017-11-20 17:40:50 -08001715 }
1716 }
1717
1718 @Override
1719 public void onDialogDismissed(String callId) {
1720 DialerCall call = CallList.getInstance().getCallById(callId);
1721 LogUtil.i(TAG, "Disconnecting call:\n%s" + call);
1722
1723 if (call != null) {
1724 call.disconnect();
1725 }
1726 }
1727 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001728}