blob: 0a5666690ead6577d40fd6af0e4cefe127b2ebbe [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;
linyuhc3968e62017-11-20 17:40:50 -080042import android.telecom.CallAudioState;
43import android.telecom.PhoneAccountHandle;
Eric Erfanian90508232017-03-24 09:31:16 -070044import android.telephony.TelephonyManager;
Eric Erfanianccca3152017-02-22 16:32:36 -080045import android.view.KeyEvent;
46import android.view.MenuItem;
47import android.view.MotionEvent;
48import android.view.View;
linyuh9c327da2017-11-14 12:33:48 -080049import android.view.WindowManager;
linyuhc3968e62017-11-20 17:40:50 -080050import android.view.animation.Animation;
51import android.view.animation.AnimationUtils;
linyuh7b86f562017-11-16 11:24:09 -080052import android.widget.CheckBox;
53import android.widget.Toast;
linyuhf99f6302017-11-15 11:23:51 -080054import com.android.contacts.common.widget.SelectPhoneAccountDialogFragment;
linyuhc3968e62017-11-20 17:40:50 -080055import com.android.dialer.animation.AnimUtils;
56import com.android.dialer.animation.AnimationListenerAdapter;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070057import com.android.dialer.common.Assert;
Eric Erfanianccca3152017-02-22 16:32:36 -080058import com.android.dialer.common.LogUtil;
weijiaxu650e7cc2017-10-31 12:38:54 -070059import com.android.dialer.common.concurrent.ThreadUtil;
Eric Erfanianccca3152017-02-22 16:32:36 -080060import com.android.dialer.compat.ActivityCompat;
linyuhc3968e62017-11-20 17:40:50 -080061import com.android.dialer.compat.CompatUtils;
Eric Erfanian2ca43182017-08-31 06:57:16 -070062import com.android.dialer.configprovider.ConfigProviderBindings;
63import com.android.dialer.logging.DialerImpression;
Eric Erfanianccca3152017-02-22 16:32:36 -080064import com.android.dialer.logging.Logger;
weijiaxu94df7202017-10-25 18:21:41 -070065import com.android.dialer.logging.LoggingBindings;
Eric Erfanian8369df02017-05-03 10:27:13 -070066import com.android.dialer.logging.ScreenEvent;
linyuhc3968e62017-11-20 17:40:50 -080067import com.android.dialer.util.ViewUtil;
Eric Erfanianccca3152017-02-22 16:32:36 -080068import com.android.incallui.answer.bindings.AnswerBindings;
69import com.android.incallui.answer.protocol.AnswerScreen;
70import com.android.incallui.answer.protocol.AnswerScreenDelegate;
71import com.android.incallui.answer.protocol.AnswerScreenDelegateFactory;
72import com.android.incallui.answerproximitysensor.PseudoScreenState;
linyuh57b093b2017-11-17 14:32:32 -080073import com.android.incallui.audiomode.AudioModeProvider;
Eric Erfanianccca3152017-02-22 16:32:36 -080074import com.android.incallui.call.CallList;
75import com.android.incallui.call.DialerCall;
76import com.android.incallui.call.DialerCall.State;
linyuh57b093b2017-11-17 14:32:32 -080077import com.android.incallui.call.TelecomAdapter;
Eric Erfanian2ca43182017-08-31 06:57:16 -070078import com.android.incallui.callpending.CallPendingActivity;
79import com.android.incallui.disconnectdialog.DisconnectMessage;
Eric Erfanianccca3152017-02-22 16:32:36 -080080import com.android.incallui.incall.bindings.InCallBindings;
81import com.android.incallui.incall.protocol.InCallButtonUiDelegate;
82import com.android.incallui.incall.protocol.InCallButtonUiDelegateFactory;
83import com.android.incallui.incall.protocol.InCallScreen;
84import com.android.incallui.incall.protocol.InCallScreenDelegate;
85import com.android.incallui.incall.protocol.InCallScreenDelegateFactory;
linyuh7b86f562017-11-16 11:24:09 -080086import com.android.incallui.incalluilock.InCallUiLock;
linyuhf99f6302017-11-15 11:23:51 -080087import com.android.incallui.telecomeventui.InternationalCallOnWifiDialogFragment;
Eric Erfanianccca3152017-02-22 16:32:36 -080088import com.android.incallui.video.bindings.VideoBindings;
89import com.android.incallui.video.protocol.VideoCallScreen;
90import com.android.incallui.video.protocol.VideoCallScreenDelegate;
91import com.android.incallui.video.protocol.VideoCallScreenDelegateFactory;
linyuhc3968e62017-11-20 17:40:50 -080092import com.google.common.base.Optional;
93import java.lang.annotation.Retention;
94import java.lang.annotation.RetentionPolicy;
95import java.util.ArrayList;
96import java.util.List;
Eric Erfanianccca3152017-02-22 16:32:36 -080097
98/** Version of {@link InCallActivity} that shows the new UI */
99public class InCallActivity extends TransactionSafeFragmentActivity
100 implements AnswerScreenDelegateFactory,
101 InCallScreenDelegateFactory,
102 InCallButtonUiDelegateFactory,
103 VideoCallScreenDelegateFactory,
104 PseudoScreenState.StateChangedListener {
105
linyuhc3968e62017-11-20 17:40:50 -0800106 @Retention(RetentionPolicy.SOURCE)
107 @IntDef({
108 DIALPAD_REQUEST_NONE,
109 DIALPAD_REQUEST_SHOW,
110 DIALPAD_REQUEST_HIDE,
111 })
112 @interface DialpadRequestType {}
Eric Erfanian2ca43182017-08-31 06:57:16 -0700113
linyuhc3968e62017-11-20 17:40:50 -0800114 private static final int DIALPAD_REQUEST_NONE = 1;
115 private static final int DIALPAD_REQUEST_SHOW = 2;
116 private static final int DIALPAD_REQUEST_HIDE = 3;
linyuh57b093b2017-11-17 14:32:32 -0800117
linyuhc3968e62017-11-20 17:40:50 -0800118 private static Optional<Integer> audioRouteForTesting = Optional.absent();
linyuh57b093b2017-11-17 14:32:32 -0800119
linyuhc3968e62017-11-20 17:40:50 -0800120 private final InternationalCallOnWifiCallback internationalCallOnWifiCallback =
121 new InternationalCallOnWifiCallback();
122 private final SelectPhoneAccountListener selectPhoneAccountListener =
123 new SelectPhoneAccountListener();
Eric Erfanianccca3152017-02-22 16:32:36 -0800124
linyuhc3968e62017-11-20 17:40:50 -0800125 private Animation dialpadSlideInAnimation;
126 private Animation dialpadSlideOutAnimation;
127 private Dialog errorDialog;
128 private GradientDrawable backgroundDrawable;
linyuh69a25062017-11-15 16:18:51 -0800129 private InCallOrientationEventListener inCallOrientationEventListener;
linyuhc3968e62017-11-20 17:40:50 -0800130 private View pseudoBlackScreenOverlay;
131 private SelectPhoneAccountDialogFragment selectPhoneAccountDialogFragment;
132 private String dtmfTextToPrepopulate;
133 private String showPostCharWaitDialogCallId;
134 private String showPostCharWaitDialogChars;
135 private boolean allowOrientationChange;
136 private boolean animateDialpadOnShow;
Eric Erfanianccca3152017-02-22 16:32:36 -0800137 private boolean didShowAnswerScreen;
138 private boolean didShowInCallScreen;
139 private boolean didShowVideoCallScreen;
linyuh9c327da2017-11-14 12:33:48 -0800140 private boolean dismissKeyguard;
Eric Erfanianccca3152017-02-22 16:32:36 -0800141 private boolean isInShowMainInCallFragment;
linyuhc3968e62017-11-20 17:40:50 -0800142 private boolean isRecreating; // whether the activity is going to be recreated
143 private boolean isVisible;
Eric Erfanianccca3152017-02-22 16:32:36 -0800144 private boolean needDismissPendingDialogs;
linyuhc3968e62017-11-20 17:40:50 -0800145 private boolean showPostCharWaitDialogOnResume;
146 private boolean touchDownWhenPseudoScreenOff;
147 private int[] backgroundDrawableColors;
148 @DialpadRequestType private int showDialpadRequest = DIALPAD_REQUEST_NONE;
Eric Erfanianccca3152017-02-22 16:32:36 -0800149
150 public static Intent getIntent(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700151 Context context, boolean showDialpad, boolean newOutgoingCall, boolean isForFullScreen) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800152 Intent intent = new Intent(Intent.ACTION_MAIN, null);
153 intent.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION | Intent.FLAG_ACTIVITY_NEW_TASK);
154 intent.setClass(context, InCallActivity.class);
linyuhc3968e62017-11-20 17:40:50 -0800155 if (showDialpad) {
156 intent.putExtra(IntentExtraNames.SHOW_DIALPAD, true);
157 }
158 intent.putExtra(IntentExtraNames.NEW_OUTGOING_CALL, newOutgoingCall);
159 intent.putExtra(IntentExtraNames.FOR_FULL_SCREEN, isForFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -0800160 return intent;
161 }
162
163 @Override
164 protected void onResumeFragments() {
165 super.onResumeFragments();
166 if (needDismissPendingDialogs) {
167 dismissPendingDialogs();
168 }
169 }
170
171 @Override
linyuhc3968e62017-11-20 17:40:50 -0800172 protected void onCreate(Bundle bundle) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700173 Trace.beginSection("InCallActivity.onCreate");
linyuhc3968e62017-11-20 17:40:50 -0800174 super.onCreate(bundle);
Eric Erfanianccca3152017-02-22 16:32:36 -0800175
Eric Erfanian2ca43182017-08-31 06:57:16 -0700176 if (getIntent().getBooleanExtra(ReturnToCallController.RETURN_TO_CALL_EXTRA_KEY, false)) {
177 Logger.get(this).logImpression(DialerImpression.Type.BUBBLE_PRIMARY_BUTTON_RETURN_TO_CALL);
178 getIntent().removeExtra(ReturnToCallController.RETURN_TO_CALL_EXTRA_KEY);
179 }
180
linyuhc3968e62017-11-20 17:40:50 -0800181 if (bundle != null) {
182 didShowAnswerScreen = bundle.getBoolean(KeysForSavedInstance.DID_SHOW_ANSWER_SCREEN);
183 didShowInCallScreen = bundle.getBoolean(KeysForSavedInstance.DID_SHOW_IN_CALL_SCREEN);
184 didShowVideoCallScreen = bundle.getBoolean(KeysForSavedInstance.DID_SHOW_VIDEO_CALL_SCREEN);
Eric Erfanianccca3152017-02-22 16:32:36 -0800185 }
186
linyuhc3968e62017-11-20 17:40:50 -0800187 setWindowFlags();
188 setContentView(R.layout.incall_screen);
189 internalResolveIntent(getIntent());
190
191 boolean isLandscape =
192 getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
193 boolean isRtl = ViewUtil.isRtl();
194 if (isLandscape) {
195 dialpadSlideInAnimation =
196 AnimationUtils.loadAnimation(
197 this, isRtl ? R.anim.dialpad_slide_in_left : R.anim.dialpad_slide_in_right);
198 dialpadSlideOutAnimation =
199 AnimationUtils.loadAnimation(
200 this, isRtl ? R.anim.dialpad_slide_out_left : R.anim.dialpad_slide_out_right);
201 } else {
202 dialpadSlideInAnimation = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_in_bottom);
203 dialpadSlideOutAnimation =
204 AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_out_bottom);
205 }
206 dialpadSlideInAnimation.setInterpolator(AnimUtils.EASE_IN);
207 dialpadSlideOutAnimation.setInterpolator(AnimUtils.EASE_OUT);
208 dialpadSlideOutAnimation.setAnimationListener(
209 new AnimationListenerAdapter() {
210 @Override
211 public void onAnimationEnd(Animation animation) {
212 hideDialpadFragment();
213 }
214 });
215
216 if (bundle != null && showDialpadRequest == DIALPAD_REQUEST_NONE) {
217 // If the dialpad was shown before, set related variables so that it can be shown and
218 // populated with the previous DTMF text during onResume().
219 if (bundle.containsKey(IntentExtraNames.SHOW_DIALPAD)) {
220 boolean showDialpad = bundle.getBoolean(IntentExtraNames.SHOW_DIALPAD);
221 showDialpadRequest = showDialpad ? DIALPAD_REQUEST_SHOW : DIALPAD_REQUEST_HIDE;
222 animateDialpadOnShow = false;
223 }
224 dtmfTextToPrepopulate = bundle.getString(KeysForSavedInstance.DIALPAD_TEXT);
225
226 SelectPhoneAccountDialogFragment selectPhoneAccountDialogFragment =
227 (SelectPhoneAccountDialogFragment)
228 getFragmentManager().findFragmentByTag(Tags.SELECT_ACCOUNT_FRAGMENT);
229 if (selectPhoneAccountDialogFragment != null) {
230 selectPhoneAccountDialogFragment.setListener(selectPhoneAccountListener);
231 }
232 }
233
234 InternationalCallOnWifiDialogFragment existingInternationalCallOnWifiDialogFragment =
235 (InternationalCallOnWifiDialogFragment)
236 getSupportFragmentManager().findFragmentByTag(Tags.INTERNATIONAL_CALL_ON_WIFI);
237 if (existingInternationalCallOnWifiDialogFragment != null) {
238 existingInternationalCallOnWifiDialogFragment.setCallback(internationalCallOnWifiCallback);
239 }
240
linyuh69a25062017-11-15 16:18:51 -0800241 inCallOrientationEventListener = new InCallOrientationEventListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800242
243 getWindow()
244 .getDecorView()
245 .setSystemUiVisibility(
246 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
247
248 pseudoBlackScreenOverlay = findViewById(R.id.psuedo_black_screen_overlay);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700249 sendBroadcast(CallPendingActivity.getFinishBroadcast());
250 Trace.endSection();
weijiaxuc950a9b2017-11-06 16:39:04 -0800251 Logger.get(this)
252 .logStopLatencyTimer(LoggingBindings.ON_CALL_ADDED_TO_ON_INCALL_UI_SHOWN_INCOMING);
253 Logger.get(this)
254 .logStopLatencyTimer(LoggingBindings.ON_CALL_ADDED_TO_ON_INCALL_UI_SHOWN_OUTGOING);
Eric Erfanianccca3152017-02-22 16:32:36 -0800255 }
256
linyuhc3968e62017-11-20 17:40:50 -0800257 private void setWindowFlags() {
258 // Allow the activity to be shown when the screen is locked and filter out touch events that are
259 // "too fat".
260 int flags =
261 WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
262 | WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES;
263
linyuhf79d1cb2017-12-15 17:49:56 -0800264 // When the audio stream is not via Bluetooth, turn on the screen once the activity is shown.
265 // When the audio stream is via Bluetooth, turn on the screen only for an incoming call.
linyuhc3968e62017-11-20 17:40:50 -0800266 final int audioRoute = getAudioRoute();
linyuhf79d1cb2017-12-15 17:49:56 -0800267 if (audioRoute != CallAudioState.ROUTE_BLUETOOTH
268 || CallList.getInstance().getIncomingCall() != null) {
linyuhc3968e62017-11-20 17:40:50 -0800269 flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
270 }
271
272 getWindow().addFlags(flags);
273 }
274
275 private static int getAudioRoute() {
276 if (audioRouteForTesting.isPresent()) {
277 return audioRouteForTesting.get();
278 }
279
280 return AudioModeProvider.getInstance().getAudioState().getRoute();
281 }
282
283 @VisibleForTesting(otherwise = VisibleForTesting.NONE)
284 public static void setAudioRouteForTesting(int audioRoute) {
285 audioRouteForTesting = Optional.of(audioRoute);
286 }
287
288 private void internalResolveIntent(Intent intent) {
289 if (!intent.getAction().equals(Intent.ACTION_MAIN)) {
290 return;
291 }
292
293 if (intent.hasExtra(IntentExtraNames.SHOW_DIALPAD)) {
294 // IntentExtraNames.SHOW_DIALPAD can be used to specify whether the DTMF dialpad should be
295 // initially visible. If the extra is absent, leave the dialpad in its previous state.
296 boolean showDialpad = intent.getBooleanExtra(IntentExtraNames.SHOW_DIALPAD, false);
297 relaunchedFromDialer(showDialpad);
298 }
299
300 DialerCall outgoingCall = CallList.getInstance().getOutgoingCall();
301 if (outgoingCall == null) {
302 outgoingCall = CallList.getInstance().getPendingOutgoingCall();
303 }
304 if (intent.getBooleanExtra(IntentExtraNames.NEW_OUTGOING_CALL, false)) {
305 intent.removeExtra(IntentExtraNames.NEW_OUTGOING_CALL);
306
307 // InCallActivity is responsible for disconnecting a new outgoing call if there is no way of
308 // making it (i.e. no valid call capable accounts).
309 // If the version is not MSIM compatible, ignore this code.
310 if (CompatUtils.isMSIMCompatible()
311 && InCallPresenter.isCallWithNoValidAccounts(outgoingCall)) {
312 LogUtil.i(
313 "InCallActivity.internalResolveIntent", "Call with no valid accounts, disconnecting");
314 outgoingCall.disconnect();
315 }
316
317 dismissKeyguard(true);
318 }
319
320 if (showPhoneAccountSelectionDialog()) {
321 hideMainInCallFragment();
322 }
323 }
324
325 /**
326 * When relaunching from the dialer app, {@code showDialpad} indicates whether the dialpad should
327 * be shown on launch.
328 *
329 * @param showDialpad {@code true} to indicate the dialpad should be shown on launch, and {@code
330 * false} to indicate no change should be made to the dialpad visibility.
331 */
332 private void relaunchedFromDialer(boolean showDialpad) {
333 showDialpadRequest = showDialpad ? DIALPAD_REQUEST_SHOW : DIALPAD_REQUEST_NONE;
334 animateDialpadOnShow = true;
335
336 if (showDialpadRequest == DIALPAD_REQUEST_SHOW) {
337 // If there's only one line in use, AND it's on hold, then we're sure the user
338 // wants to use the dialpad toward the exact line, so un-hold the holding line.
339 DialerCall call = CallList.getInstance().getActiveOrBackgroundCall();
340 if (call != null && call.getState() == State.ONHOLD) {
341 call.unhold();
342 }
343 }
344 }
345
346 /**
347 * Show a phone account selection dialog if there is a call waiting for phone account selection.
348 *
349 * @return true if the dialog was shown.
350 */
351 private boolean showPhoneAccountSelectionDialog() {
352 DialerCall waitingForAccountCall = CallList.getInstance().getWaitingForAccountCall();
353 if (waitingForAccountCall == null) {
354 return false;
355 }
356
357 Bundle extras = waitingForAccountCall.getIntentExtras();
358 List<PhoneAccountHandle> phoneAccountHandles =
359 extras == null
360 ? new ArrayList<>()
361 : extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
362
363 selectPhoneAccountDialogFragment =
364 SelectPhoneAccountDialogFragment.newInstance(
365 R.string.select_phone_account_for_calls,
366 true /* canSetDefault */,
367 0 /* setDefaultResId */,
368 phoneAccountHandles,
369 selectPhoneAccountListener,
370 waitingForAccountCall.getId(),
371 null /* hints */);
372 selectPhoneAccountDialogFragment.show(getFragmentManager(), Tags.SELECT_ACCOUNT_FRAGMENT);
373 return true;
374 }
375
Eric Erfanianccca3152017-02-22 16:32:36 -0800376 @Override
377 protected void onSaveInstanceState(Bundle out) {
linyuh57b093b2017-11-17 14:32:32 -0800378 LogUtil.enterBlock("InCallActivity.onSaveInstanceState");
379
380 // TODO: DialpadFragment should handle this as part of its own state
linyuhc3968e62017-11-20 17:40:50 -0800381 out.putBoolean(IntentExtraNames.SHOW_DIALPAD, isDialpadVisible());
linyuh57b093b2017-11-17 14:32:32 -0800382 DialpadFragment dialpadFragment = getDialpadFragment();
383 if (dialpadFragment != null) {
linyuhc3968e62017-11-20 17:40:50 -0800384 out.putString(KeysForSavedInstance.DIALPAD_TEXT, dialpadFragment.getDtmfText());
linyuh57b093b2017-11-17 14:32:32 -0800385 }
386
linyuhc3968e62017-11-20 17:40:50 -0800387 out.putBoolean(KeysForSavedInstance.DID_SHOW_ANSWER_SCREEN, didShowAnswerScreen);
388 out.putBoolean(KeysForSavedInstance.DID_SHOW_IN_CALL_SCREEN, didShowInCallScreen);
389 out.putBoolean(KeysForSavedInstance.DID_SHOW_VIDEO_CALL_SCREEN, didShowVideoCallScreen);
linyuh57b093b2017-11-17 14:32:32 -0800390
Eric Erfanianccca3152017-02-22 16:32:36 -0800391 super.onSaveInstanceState(out);
392 isVisible = false;
393 }
394
395 @Override
396 protected void onStart() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700397 Trace.beginSection("InCallActivity.onStart");
Eric Erfanianccca3152017-02-22 16:32:36 -0800398 super.onStart();
linyuh57b093b2017-11-17 14:32:32 -0800399
Eric Erfanianccca3152017-02-22 16:32:36 -0800400 isVisible = true;
401 showMainInCallFragment();
linyuh57b093b2017-11-17 14:32:32 -0800402
403 InCallPresenter.getInstance().setActivity(this);
404 enableInCallOrientationEventListener(
405 getRequestedOrientation()
406 == InCallOrientationEventListener.ACTIVITY_PREFERENCE_ALLOW_ROTATION);
407 InCallPresenter.getInstance().onActivityStarted();
408
Eric Erfanianccca3152017-02-22 16:32:36 -0800409 if (ActivityCompat.isInMultiWindowMode(this)
410 && !getResources().getBoolean(R.bool.incall_dialpad_allowed)) {
411 // Hide the dialpad because there may not be enough room
412 showDialpadFragment(false, false);
413 }
linyuh57b093b2017-11-17 14:32:32 -0800414
Eric Erfanian2ca43182017-08-31 06:57:16 -0700415 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800416 }
417
418 @Override
419 protected void onResume() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700420 Trace.beginSection("InCallActivity.onResume");
Eric Erfanianccca3152017-02-22 16:32:36 -0800421 super.onResume();
linyuhc3968e62017-11-20 17:40:50 -0800422
423 if (!InCallPresenter.getInstance().isReadyForTearDown()) {
424 updateTaskDescription();
425 InCallPresenter.getInstance().onUiShowing(true);
426 }
427
428 // If there is a pending request to show or hide the dialpad, handle that now.
429 if (showDialpadRequest != DIALPAD_REQUEST_NONE) {
430 if (showDialpadRequest == DIALPAD_REQUEST_SHOW) {
431 // Exit fullscreen so that the user has access to the dialpad hide/show button.
432 // This is important when showing the dialpad from within dialer.
433 InCallPresenter.getInstance().setFullScreen(false /* isFullScreen */, true /* force */);
434
435 showDialpadFragment(true /* show */, animateDialpadOnShow /* animate */);
436 animateDialpadOnShow = false;
437
438 DialpadFragment dialpadFragment = getDialpadFragment();
439 if (dialpadFragment != null) {
440 dialpadFragment.setDtmfText(dtmfTextToPrepopulate);
441 dtmfTextToPrepopulate = null;
442 }
443 } else {
444 LogUtil.i("InCallActivity.onResume", "Force-hide the dialpad");
445 if (getDialpadFragment() != null) {
446 showDialpadFragment(false /* show */, false /* animate */);
447 }
448 }
449 showDialpadRequest = DIALPAD_REQUEST_NONE;
450 }
451 updateNavigationBar(isDialpadVisible());
452
453 if (showPostCharWaitDialogOnResume) {
454 showDialogForPostCharWait(showPostCharWaitDialogCallId, showPostCharWaitDialogChars);
455 }
456
457 CallList.getInstance()
458 .onInCallUiShown(getIntent().getBooleanExtra(IntentExtraNames.FOR_FULL_SCREEN, false));
459
Eric Erfanianccca3152017-02-22 16:32:36 -0800460 PseudoScreenState pseudoScreenState = InCallPresenter.getInstance().getPseudoScreenState();
461 pseudoScreenState.addListener(this);
462 onPseudoScreenStateChanged(pseudoScreenState.isOn());
Eric Erfanian2ca43182017-08-31 06:57:16 -0700463 Trace.endSection();
weijiaxu650e7cc2017-10-31 12:38:54 -0700464 // add 1 sec delay to get memory snapshot so that dialer wont react slowly on resume.
465 ThreadUtil.postDelayedOnUiThread(
weijiaxuc950a9b2017-11-06 16:39:04 -0800466 () ->
467 Logger.get(this)
468 .logRecordMemory(LoggingBindings.INCALL_ACTIVITY_ON_RESUME_MEMORY_EVENT_NAME),
weijiaxu650e7cc2017-10-31 12:38:54 -0700469 1000);
Eric Erfanianccca3152017-02-22 16:32:36 -0800470 }
471
Eric Erfanianccca3152017-02-22 16:32:36 -0800472 @Override
473 protected void onPause() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700474 Trace.beginSection("InCallActivity.onPause");
Eric Erfanianccca3152017-02-22 16:32:36 -0800475 super.onPause();
linyuh57b093b2017-11-17 14:32:32 -0800476
477 DialpadFragment dialpadFragment = getDialpadFragment();
478 if (dialpadFragment != null) {
479 dialpadFragment.onDialerKeyUp(null);
480 }
481
yuega3305352018-01-09 11:02:47 -0800482 if (!isRecreating) {
483 InCallPresenter.getInstance().onUiShowing(false);
484 }
linyuh57b093b2017-11-17 14:32:32 -0800485 if (isFinishing()) {
486 InCallPresenter.getInstance().unsetActivity(this);
487 }
488
Eric Erfanianccca3152017-02-22 16:32:36 -0800489 InCallPresenter.getInstance().getPseudoScreenState().removeListener(this);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700490 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800491 }
492
493 @Override
494 protected void onStop() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700495 Trace.beginSection("InCallActivity.onStop");
wangqi4d705e52017-09-28 12:23:35 -0700496 isVisible = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800497 super.onStop();
linyuh57b093b2017-11-17 14:32:32 -0800498
499 // Disconnects the call waiting for a phone account when the activity is hidden (e.g., after the
500 // user presses the home button).
501 // Without this the pending call will get stuck on phone account selection and new calls can't
502 // be created.
503 // Skip this when the screen is locked since the activity may complete its current life cycle
504 // and restart.
linyuhc3968e62017-11-20 17:40:50 -0800505 if (!isRecreating && !getSystemService(KeyguardManager.class).isKeyguardLocked()) {
linyuh57b093b2017-11-17 14:32:32 -0800506 DialerCall waitingForAccountCall = CallList.getInstance().getWaitingForAccountCall();
507 if (waitingForAccountCall != null) {
508 waitingForAccountCall.disconnect();
509 }
510 }
511
512 enableInCallOrientationEventListener(false);
513 InCallPresenter.getInstance().updateIsChangingConfigurations();
514 InCallPresenter.getInstance().onActivityStopped();
linyuhc3968e62017-11-20 17:40:50 -0800515 if (!isRecreating) {
linyuh57b093b2017-11-17 14:32:32 -0800516 if (errorDialog != null) {
517 errorDialog.dismiss();
518 }
519 }
520
Eric Erfanian2ca43182017-08-31 06:57:16 -0700521 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800522 }
523
524 @Override
525 protected void onDestroy() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700526 Trace.beginSection("InCallActivity.onDestroy");
Eric Erfanianccca3152017-02-22 16:32:36 -0800527 super.onDestroy();
linyuh57b093b2017-11-17 14:32:32 -0800528
529 InCallPresenter.getInstance().unsetActivity(this);
530 InCallPresenter.getInstance().updateIsChangingConfigurations();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700531 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800532 }
533
534 @Override
535 public void finish() {
536 if (shouldCloseActivityOnFinish()) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700537 // When user select incall ui from recents after the call is disconnected, it tries to launch
538 // a new InCallActivity but InCallPresenter is already teared down at this point, which causes
539 // crash.
540 // By calling finishAndRemoveTask() instead of finish() the task associated with
541 // InCallActivity is cleared completely. So system won't try to create a new InCallActivity in
542 // this case.
543 //
544 // Calling finish won't clear the task and normally when an activity finishes it shouldn't
545 // clear the task since there could be parent activity in the same task that's still alive.
546 // But InCallActivity is special since it's singleInstance which means it's root activity and
547 // only instance of activity in the task. So it should be safe to also remove task when
548 // finishing.
549 // It's also necessary in the sense of it's excluded from recents. So whenever the activity
550 // finishes, the task should also be removed since it doesn't make sense to go back to it in
551 // anyway anymore.
552 super.finishAndRemoveTask();
Eric Erfanianccca3152017-02-22 16:32:36 -0800553 }
554 }
555
556 private boolean shouldCloseActivityOnFinish() {
linyuhc3968e62017-11-20 17:40:50 -0800557 if (!isVisible) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800558 LogUtil.i(
559 "InCallActivity.shouldCloseActivityOnFinish",
560 "allowing activity to be closed because it's not visible");
561 return true;
562 }
563
twyen8efb4952017-10-06 16:35:54 -0700564 if (InCallPresenter.getInstance().isInCallUiLocked()) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800565 LogUtil.i(
566 "InCallActivity.shouldCloseActivityOnFinish",
twyen8efb4952017-10-06 16:35:54 -0700567 "in call ui is locked, not closing activity");
Eric Erfanianccca3152017-02-22 16:32:36 -0800568 return false;
569 }
570
571 LogUtil.i(
572 "InCallActivity.shouldCloseActivityOnFinish",
twyen8efb4952017-10-06 16:35:54 -0700573 "activity is visible and has no locks, allowing activity to close");
Eric Erfanianccca3152017-02-22 16:32:36 -0800574 return true;
575 }
576
577 @Override
578 protected void onNewIntent(Intent intent) {
linyuhc3968e62017-11-20 17:40:50 -0800579 LogUtil.enterBlock("InCallActivity.onNewIntent");
Eric Erfanianccca3152017-02-22 16:32:36 -0800580
581 // If the screen is off, we need to make sure it gets turned on for incoming calls.
582 // This normally works just fine thanks to FLAG_TURN_SCREEN_ON but that only works
583 // when the activity is first created. Therefore, to ensure the screen is turned on
584 // for the call waiting case, we recreate() the current activity. There should be no jank from
585 // this since the screen is already off and will remain so until our new activity is up.
linyuhc3968e62017-11-20 17:40:50 -0800586 if (!isVisible) {
587 onNewIntent(intent, true /* isRecreating */);
Eric Erfanianccca3152017-02-22 16:32:36 -0800588 LogUtil.i("InCallActivity.onNewIntent", "Restarting InCallActivity to force screen on.");
589 recreate();
Eric Erfanian10b34a52017-05-04 08:23:17 -0700590 } else {
linyuhc3968e62017-11-20 17:40:50 -0800591 onNewIntent(intent, false /* isRecreating */);
592 }
593 }
594
yuega3305352018-01-09 11:02:47 -0800595 @VisibleForTesting
596 void onNewIntent(Intent intent, boolean isRecreating) {
linyuhc3968e62017-11-20 17:40:50 -0800597 this.isRecreating = isRecreating;
598
599 // We're being re-launched with a new Intent. Since it's possible for a single InCallActivity
600 // instance to persist indefinitely (even if we finish() ourselves), this sequence can
601 // happen any time the InCallActivity needs to be displayed.
602
603 // Stash away the new intent so that we can get it in the future by calling getIntent().
604 // Otherwise getIntent() will return the original Intent from when we first got created.
605 setIntent(intent);
606
607 // Activities are always paused before receiving a new intent, so we can count on our onResume()
608 // method being called next.
609
610 // Just like in onCreate(), handle the intent.
611 // Skip if InCallActivity is going to be recreated since this will be called in onCreate().
612 if (!isRecreating) {
613 internalResolveIntent(intent);
Eric Erfanianccca3152017-02-22 16:32:36 -0800614 }
615 }
616
617 @Override
618 public void onBackPressed() {
linyuh57b093b2017-11-17 14:32:32 -0800619 LogUtil.enterBlock("InCallActivity.onBackPressed");
620
linyuhc3968e62017-11-20 17:40:50 -0800621 if (!isVisible) {
linyuh57b093b2017-11-17 14:32:32 -0800622 return;
Eric Erfanianccca3152017-02-22 16:32:36 -0800623 }
linyuh57b093b2017-11-17 14:32:32 -0800624
625 if (!getCallCardFragmentVisible()) {
626 return;
627 }
628
629 DialpadFragment dialpadFragment = getDialpadFragment();
630 if (dialpadFragment != null && dialpadFragment.isVisible()) {
631 showDialpadFragment(false /* show */, true /* animate */);
632 return;
633 }
634
635 if (CallList.getInstance().getIncomingCall() != null) {
636 LogUtil.i(
637 "InCallActivity.onBackPressed",
638 "Ignore the press of the back key when an incoming call is ringing");
639 return;
640 }
641
642 // Nothing special to do. Fall back to the default behavior.
643 super.onBackPressed();
Eric Erfanianccca3152017-02-22 16:32:36 -0800644 }
645
646 @Override
647 public boolean onOptionsItemSelected(MenuItem item) {
648 LogUtil.i("InCallActivity.onOptionsItemSelected", "item: " + item);
649 if (item.getItemId() == android.R.id.home) {
650 onBackPressed();
651 return true;
652 }
653 return super.onOptionsItemSelected(item);
654 }
655
656 @Override
657 public boolean onKeyUp(int keyCode, KeyEvent event) {
linyuh57b093b2017-11-17 14:32:32 -0800658 DialpadFragment dialpadFragment = getDialpadFragment();
659 if (dialpadFragment != null
660 && dialpadFragment.isVisible()
661 && dialpadFragment.onDialerKeyUp(event)) {
662 return true;
663 }
664
665 if (keyCode == KeyEvent.KEYCODE_CALL) {
666 // Always consume KEYCODE_CALL to ensure the PhoneWindow won't do anything with it.
667 return true;
668 }
669
670 return super.onKeyUp(keyCode, event);
Eric Erfanianccca3152017-02-22 16:32:36 -0800671 }
672
673 @Override
674 public boolean onKeyDown(int keyCode, KeyEvent event) {
linyuh57b093b2017-11-17 14:32:32 -0800675 switch (keyCode) {
676 case KeyEvent.KEYCODE_CALL:
677 if (!InCallPresenter.getInstance().handleCallKey()) {
678 LogUtil.e(
679 "InCallActivity.onKeyDown",
680 "InCallPresenter should always handle KEYCODE_CALL in onKeyDown");
681 }
682 // Always consume KEYCODE_CALL to ensure the PhoneWindow won't do anything with it.
683 return true;
684
685 // Note that KEYCODE_ENDCALL isn't handled here as the standard system-wide handling of it
686 // is exactly what's needed, namely
687 // (1) "hang up" if there's an active call, or
688 // (2) "don't answer" if there's an incoming call.
689 // (See PhoneWindowManager for implementation details.)
690
691 case KeyEvent.KEYCODE_CAMERA:
692 // Consume KEYCODE_CAMERA since it's easy to accidentally press the camera button.
693 return true;
694
695 case KeyEvent.KEYCODE_VOLUME_UP:
696 case KeyEvent.KEYCODE_VOLUME_DOWN:
697 case KeyEvent.KEYCODE_VOLUME_MUTE:
698 // Ringer silencing handled by PhoneWindowManager.
699 break;
700
701 case KeyEvent.KEYCODE_MUTE:
702 TelecomAdapter.getInstance()
703 .mute(!AudioModeProvider.getInstance().getAudioState().isMuted());
704 return true;
705
706 case KeyEvent.KEYCODE_SLASH:
707 // When verbose logging is enabled, dump the view for debugging/testing purposes.
708 if (LogUtil.isVerboseEnabled()) {
709 View decorView = getWindow().getDecorView();
710 LogUtil.v("InCallActivity.onKeyDown", "View dump:\n%s", decorView);
711 return true;
712 }
713 break;
714
715 case KeyEvent.KEYCODE_EQUALS:
716 break;
717
718 default: // fall out
719 }
720
721 // Pass other key events to DialpadFragment's "onDialerKeyDown" method in case the user types
722 // in DTMF (Dual-tone multi-frequency signaling) code.
723 DialpadFragment dialpadFragment = getDialpadFragment();
724 if (dialpadFragment != null
725 && dialpadFragment.isVisible()
726 && dialpadFragment.onDialerKeyDown(event)) {
727 return true;
728 }
729
730 return super.onKeyDown(keyCode, event);
Eric Erfanianccca3152017-02-22 16:32:36 -0800731 }
732
733 public boolean isInCallScreenAnimating() {
734 return false;
735 }
736
737 public void showConferenceFragment(boolean show) {
738 if (show) {
739 startActivity(new Intent(this, ManageConferenceActivity.class));
740 }
741 }
742
linyuhc3968e62017-11-20 17:40:50 -0800743 public void showDialpadFragment(boolean show, boolean animate) {
744 if (show == isDialpadVisible()) {
745 return;
Eric Erfanianccca3152017-02-22 16:32:36 -0800746 }
linyuhc3968e62017-11-20 17:40:50 -0800747
748 FragmentManager dialpadFragmentManager = getDialpadFragmentManager();
749 if (dialpadFragmentManager == null) {
750 LogUtil.i("InCallActivity.showDialpadFragment", "Unable to obtain a FragmentManager");
751 return;
752 }
753
754 if (!animate) {
755 if (show) {
756 showDialpadFragment();
757 } else {
758 hideDialpadFragment();
759 }
760 } else {
761 if (show) {
762 showDialpadFragment();
763 getDialpadFragment().animateShowDialpad();
764 }
765 getDialpadFragment()
766 .getView()
767 .startAnimation(show ? dialpadSlideInAnimation : dialpadSlideOutAnimation);
768 }
769
770 ProximitySensor sensor = InCallPresenter.getInstance().getProximitySensor();
771 if (sensor != null) {
772 sensor.onDialpadVisible(show);
773 }
774 showDialpadRequest = DIALPAD_REQUEST_NONE;
775
776 // Note: onInCallScreenDialpadVisibilityChange is called here to ensure that the dialpad FAB
777 // repositions itself.
778 getInCallScreen().onInCallScreenDialpadVisibilityChange(show);
779 }
780
781 private void showDialpadFragment() {
782 FragmentManager dialpadFragmentManager = getDialpadFragmentManager();
783 if (dialpadFragmentManager == null) {
784 return;
785 }
786
787 FragmentTransaction transaction = dialpadFragmentManager.beginTransaction();
788 DialpadFragment dialpadFragment = getDialpadFragment();
789 if (dialpadFragment == null) {
790 transaction.add(getDialpadContainerId(), new DialpadFragment(), Tags.DIALPAD_FRAGMENT);
791 } else {
792 transaction.show(dialpadFragment);
calderwoodrad5883872017-12-12 15:29:12 -0800793 dialpadFragment.setUserVisibleHint(true);
linyuhc3968e62017-11-20 17:40:50 -0800794 }
795 transaction.commitAllowingStateLoss();
796 dialpadFragmentManager.executePendingTransactions();
797
798 Logger.get(this).logScreenView(ScreenEvent.Type.INCALL_DIALPAD, this);
799 updateNavigationBar(true /* isDialpadVisible */);
800 }
801
802 private void hideDialpadFragment() {
803 FragmentManager dialpadFragmentManager = getDialpadFragmentManager();
804 if (dialpadFragmentManager == null) {
805 return;
806 }
807
calderwoodrad5883872017-12-12 15:29:12 -0800808 DialpadFragment dialpadFragment = getDialpadFragment();
linyuhc3968e62017-11-20 17:40:50 -0800809 if (dialpadFragment != null) {
810 FragmentTransaction transaction = dialpadFragmentManager.beginTransaction();
811 transaction.hide(dialpadFragment);
812 transaction.commitAllowingStateLoss();
813 dialpadFragmentManager.executePendingTransactions();
calderwoodrad5883872017-12-12 15:29:12 -0800814 dialpadFragment.setUserVisibleHint(false);
linyuhc3968e62017-11-20 17:40:50 -0800815 }
816 updateNavigationBar(false /* isDialpadVisible */);
Eric Erfanianccca3152017-02-22 16:32:36 -0800817 }
818
819 public boolean isDialpadVisible() {
linyuh69a25062017-11-15 16:18:51 -0800820 DialpadFragment dialpadFragment = getDialpadFragment();
calderwoodrad5883872017-12-12 15:29:12 -0800821 return dialpadFragment != null && dialpadFragment.getUserVisibleHint();
linyuh69a25062017-11-15 16:18:51 -0800822 }
823
linyuhc3968e62017-11-20 17:40:50 -0800824 /** Returns the {@link DialpadFragment} that's shown by this activity, or {@code null} */
linyuh69a25062017-11-15 16:18:51 -0800825 @Nullable
linyuhc3968e62017-11-20 17:40:50 -0800826 private DialpadFragment getDialpadFragment() {
linyuh69a25062017-11-15 16:18:51 -0800827 FragmentManager fragmentManager = getDialpadFragmentManager();
828 if (fragmentManager == null) {
829 return null;
830 }
linyuhc3968e62017-11-20 17:40:50 -0800831 return (DialpadFragment) fragmentManager.findFragmentByTag(Tags.DIALPAD_FRAGMENT);
Eric Erfanianccca3152017-02-22 16:32:36 -0800832 }
833
834 public void onForegroundCallChanged(DialerCall newForegroundCall) {
linyuh57b093b2017-11-17 14:32:32 -0800835 updateTaskDescription();
836
837 if (newForegroundCall == null || !didShowAnswerScreen) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800838 LogUtil.v("InCallActivity.onForegroundCallChanged", "resetting background color");
linyuh57b093b2017-11-17 14:32:32 -0800839 updateWindowBackgroundColor(0 /* progress */);
Eric Erfanianccca3152017-02-22 16:32:36 -0800840 }
841 }
842
linyuhc3968e62017-11-20 17:40:50 -0800843 private void updateTaskDescription() {
linyuh57b093b2017-11-17 14:32:32 -0800844 int color =
845 getResources().getBoolean(R.bool.is_layout_landscape)
846 ? ResourcesCompat.getColor(
847 getResources(), R.color.statusbar_background_color, getTheme())
848 : InCallPresenter.getInstance().getThemeColorManager().getSecondaryColor();
849 setTaskDescription(
850 new TaskDescription(
851 getResources().getString(R.string.notification_ongoing_call), null /* icon */, color));
852 }
853
Eric Erfanianccca3152017-02-22 16:32:36 -0800854 public void updateWindowBackgroundColor(@FloatRange(from = -1f, to = 1.0f) float progress) {
855 ThemeColorManager themeColorManager = InCallPresenter.getInstance().getThemeColorManager();
856 @ColorInt int top;
857 @ColorInt int middle;
858 @ColorInt int bottom;
859 @ColorInt int gray = 0x66000000;
860
861 if (ActivityCompat.isInMultiWindowMode(this)) {
862 top = themeColorManager.getBackgroundColorSolid();
863 middle = themeColorManager.getBackgroundColorSolid();
864 bottom = themeColorManager.getBackgroundColorSolid();
865 } else {
866 top = themeColorManager.getBackgroundColorTop();
867 middle = themeColorManager.getBackgroundColorMiddle();
868 bottom = themeColorManager.getBackgroundColorBottom();
869 }
870
871 if (progress < 0) {
872 float correctedProgress = Math.abs(progress);
873 top = ColorUtils.blendARGB(top, gray, correctedProgress);
874 middle = ColorUtils.blendARGB(middle, gray, correctedProgress);
875 bottom = ColorUtils.blendARGB(bottom, gray, correctedProgress);
876 }
877
878 boolean backgroundDirty = false;
879 if (backgroundDrawable == null) {
880 backgroundDrawableColors = new int[] {top, middle, bottom};
881 backgroundDrawable = new GradientDrawable(Orientation.TOP_BOTTOM, backgroundDrawableColors);
882 backgroundDirty = true;
883 } else {
884 if (backgroundDrawableColors[0] != top) {
885 backgroundDrawableColors[0] = top;
886 backgroundDirty = true;
887 }
888 if (backgroundDrawableColors[1] != middle) {
889 backgroundDrawableColors[1] = middle;
890 backgroundDirty = true;
891 }
892 if (backgroundDrawableColors[2] != bottom) {
893 backgroundDrawableColors[2] = bottom;
894 backgroundDirty = true;
895 }
896 if (backgroundDirty) {
897 backgroundDrawable.setColors(backgroundDrawableColors);
898 }
899 }
900
901 if (backgroundDirty) {
902 getWindow().setBackgroundDrawable(backgroundDrawable);
903 }
904 }
905
906 public boolean isVisible() {
907 return isVisible;
908 }
909
910 public boolean getCallCardFragmentVisible() {
911 return didShowInCallScreen || didShowVideoCallScreen;
912 }
913
914 public void dismissKeyguard(boolean dismiss) {
linyuh9c327da2017-11-14 12:33:48 -0800915 if (dismissKeyguard == dismiss) {
916 return;
917 }
918
919 dismissKeyguard = dismiss;
920 if (dismiss) {
921 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
922 } else {
923 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
924 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800925 }
926
linyuhc3968e62017-11-20 17:40:50 -0800927 public void showDialogForPostCharWait(String callId, String chars) {
928 if (isVisible) {
929 PostCharDialogFragment fragment = new PostCharDialogFragment(callId, chars);
930 fragment.show(getSupportFragmentManager(), Tags.POST_CHAR_DIALOG_FRAGMENT);
931
932 showPostCharWaitDialogOnResume = false;
933 showPostCharWaitDialogCallId = null;
934 showPostCharWaitDialogChars = null;
935 } else {
936 showPostCharWaitDialogOnResume = true;
937 showPostCharWaitDialogCallId = callId;
938 showPostCharWaitDialogChars = chars;
939 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800940 }
941
linyuh7b86f562017-11-16 11:24:09 -0800942 public void showDialogOrToastForDisconnectedCall(DisconnectMessage disconnectMessage) {
943 LogUtil.i(
944 "InCallActivity.showDialogOrToastForDisconnectedCall",
945 "disconnect cause: %s",
946 disconnectMessage);
947
948 if (disconnectMessage.dialog == null || isFinishing()) {
949 return;
950 }
951
952 dismissPendingDialogs();
953
954 // Show a toast if the app is in background when a dialog can't be visible.
955 if (!isVisible()) {
956 Toast.makeText(getApplicationContext(), disconnectMessage.toastMessage, Toast.LENGTH_LONG)
957 .show();
958 return;
959 }
960
961 // Show the dialog.
linyuhc3968e62017-11-20 17:40:50 -0800962 errorDialog = disconnectMessage.dialog;
linyuh7b86f562017-11-16 11:24:09 -0800963 InCallUiLock lock = InCallPresenter.getInstance().acquireInCallUiLock("showErrorDialog");
964 disconnectMessage.dialog.setOnDismissListener(
965 dialogInterface -> {
966 lock.release();
967 onDialogDismissed();
968 });
969 disconnectMessage.dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
970 disconnectMessage.dialog.show();
971 }
972
973 private void onDialogDismissed() {
linyuhc3968e62017-11-20 17:40:50 -0800974 errorDialog = null;
linyuh7b86f562017-11-16 11:24:09 -0800975 CallList.getInstance().onErrorDialogDismissed();
Eric Erfanianccca3152017-02-22 16:32:36 -0800976 }
977
978 public void dismissPendingDialogs() {
linyuhc3968e62017-11-20 17:40:50 -0800979 LogUtil.enterBlock("InCallActivity.dismissPendingDialogs");
linyuhf99f6302017-11-15 11:23:51 -0800980
981 if (!isVisible) {
982 // Defer the dismissing action as the activity is not visible and onSaveInstanceState may have
983 // been called.
Eric Erfanianccca3152017-02-22 16:32:36 -0800984 LogUtil.i(
985 "InCallActivity.dismissPendingDialogs", "defer actions since activity is not visible");
986 needDismissPendingDialogs = true;
linyuhf99f6302017-11-15 11:23:51 -0800987 return;
Eric Erfanianccca3152017-02-22 16:32:36 -0800988 }
linyuhf99f6302017-11-15 11:23:51 -0800989
990 // Dismiss the error dialog
linyuhf99f6302017-11-15 11:23:51 -0800991 if (errorDialog != null) {
992 errorDialog.dismiss();
linyuhc3968e62017-11-20 17:40:50 -0800993 errorDialog = null;
linyuhf99f6302017-11-15 11:23:51 -0800994 }
995
996 // Dismiss the phone account selection dialog
linyuhf99f6302017-11-15 11:23:51 -0800997 if (selectPhoneAccountDialogFragment != null) {
998 selectPhoneAccountDialogFragment.dismiss();
linyuhc3968e62017-11-20 17:40:50 -0800999 selectPhoneAccountDialogFragment = null;
linyuhf99f6302017-11-15 11:23:51 -08001000 }
1001
1002 // Dismiss the dialog for international call on WiFi
1003 InternationalCallOnWifiDialogFragment internationalCallOnWifiFragment =
1004 (InternationalCallOnWifiDialogFragment)
linyuhc3968e62017-11-20 17:40:50 -08001005 getSupportFragmentManager().findFragmentByTag(Tags.INTERNATIONAL_CALL_ON_WIFI);
linyuhf99f6302017-11-15 11:23:51 -08001006 if (internationalCallOnWifiFragment != null) {
1007 internationalCallOnWifiFragment.dismiss();
1008 }
1009
1010 // Dismiss the answer screen
1011 AnswerScreen answerScreen = getAnswerScreen();
1012 if (answerScreen != null) {
1013 answerScreen.dismissPendingDialogs();
1014 }
1015
1016 needDismissPendingDialogs = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001017 }
1018
linyuhc3968e62017-11-20 17:40:50 -08001019 private void enableInCallOrientationEventListener(boolean enable) {
linyuh69a25062017-11-15 16:18:51 -08001020 if (enable) {
1021 inCallOrientationEventListener.enable(true /* notifyDeviceOrientationChange */);
1022 } else {
1023 inCallOrientationEventListener.disable();
1024 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001025 }
1026
1027 public void setExcludeFromRecents(boolean exclude) {
linyuhc3968e62017-11-20 17:40:50 -08001028 int taskId = getTaskId();
1029
1030 List<AppTask> tasks = getSystemService(ActivityManager.class).getAppTasks();
1031 for (AppTask task : tasks) {
1032 try {
1033 if (task.getTaskInfo().id == taskId) {
1034 task.setExcludeFromRecents(exclude);
1035 }
1036 } catch (RuntimeException e) {
1037 LogUtil.e("InCallActivity.setExcludeFromRecents", "RuntimeException:\n%s", e);
1038 }
1039 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001040 }
1041
Eric Erfanianccca3152017-02-22 16:32:36 -08001042 @Nullable
1043 public FragmentManager getDialpadFragmentManager() {
1044 InCallScreen inCallScreen = getInCallScreen();
1045 if (inCallScreen != null) {
1046 return inCallScreen.getInCallScreenFragment().getChildFragmentManager();
1047 }
1048 return null;
1049 }
1050
1051 public int getDialpadContainerId() {
1052 return getInCallScreen().getAnswerAndDialpadContainerResourceId();
1053 }
1054
1055 @Override
1056 public AnswerScreenDelegate newAnswerScreenDelegate(AnswerScreen answerScreen) {
1057 DialerCall call = CallList.getInstance().getCallById(answerScreen.getCallId());
1058 if (call == null) {
1059 // This is a work around for a bug where we attempt to create a new delegate after the call
1060 // has already been removed. An example of when this can happen is:
1061 // 1. incoming video call in landscape mode
1062 // 2. remote party hangs up
1063 // 3. activity switches from landscape to portrait
1064 // At step #3 the answer fragment will try to create a new answer delegate but the call won't
1065 // exist. In this case we'll simply return a stub delegate that does nothing. This is ok
1066 // because this new state is transient and the activity will be destroyed soon.
1067 LogUtil.i("InCallActivity.onPrimaryCallStateChanged", "call doesn't exist, using stub");
1068 return new AnswerScreenPresenterStub();
1069 } else {
1070 return new AnswerScreenPresenter(
1071 this, answerScreen, CallList.getInstance().getCallById(answerScreen.getCallId()));
1072 }
1073 }
1074
1075 @Override
1076 public InCallScreenDelegate newInCallScreenDelegate() {
1077 return new CallCardPresenter(this);
1078 }
1079
1080 @Override
1081 public InCallButtonUiDelegate newInCallButtonUiDelegate() {
1082 return new CallButtonPresenter(this);
1083 }
1084
1085 @Override
Eric Erfanian90508232017-03-24 09:31:16 -07001086 public VideoCallScreenDelegate newVideoCallScreenDelegate(VideoCallScreen videoCallScreen) {
1087 DialerCall dialerCall = CallList.getInstance().getCallById(videoCallScreen.getCallId());
1088 if (dialerCall != null && dialerCall.getVideoTech().shouldUseSurfaceView()) {
1089 return dialerCall.getVideoTech().createVideoCallScreenDelegate(this, videoCallScreen);
1090 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001091 return new VideoCallPresenter();
1092 }
1093
1094 public void onPrimaryCallStateChanged() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001095 Trace.beginSection("InCallActivity.onPrimaryCallStateChanged");
Eric Erfanianccca3152017-02-22 16:32:36 -08001096 showMainInCallFragment();
Eric Erfanian2ca43182017-08-31 06:57:16 -07001097 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001098 }
1099
linyuh7b86f562017-11-16 11:24:09 -08001100 public void showToastForWiFiToLteHandover(DialerCall call) {
1101 if (call.hasShownWiFiToLteHandoverToast()) {
1102 return;
1103 }
1104
1105 Toast.makeText(this, R.string.video_call_wifi_to_lte_handover_toast, Toast.LENGTH_LONG).show();
1106 call.setHasShownWiFiToLteHandoverToast();
Eric Erfanianccca3152017-02-22 16:32:36 -08001107 }
1108
linyuh7b86f562017-11-16 11:24:09 -08001109 public void showDialogOrToastForWifiHandoverFailure(DialerCall call) {
1110 if (call.showWifiHandoverAlertAsToast()) {
1111 Toast.makeText(this, R.string.video_call_lte_to_wifi_failed_message, Toast.LENGTH_SHORT)
1112 .show();
1113 return;
1114 }
1115
1116 dismissPendingDialogs();
1117
1118 AlertDialog.Builder builder =
1119 new AlertDialog.Builder(this).setTitle(R.string.video_call_lte_to_wifi_failed_title);
1120
1121 // This allows us to use the theme of the dialog instead of the activity
1122 View dialogCheckBoxView =
1123 View.inflate(builder.getContext(), R.layout.video_call_lte_to_wifi_failed, null /* root */);
1124 CheckBox wifiHandoverFailureCheckbox =
1125 (CheckBox) dialogCheckBoxView.findViewById(R.id.video_call_lte_to_wifi_failed_checkbox);
1126 wifiHandoverFailureCheckbox.setChecked(false);
1127
1128 InCallUiLock lock = InCallPresenter.getInstance().acquireInCallUiLock("WifiFailedDialog");
linyuhc3968e62017-11-20 17:40:50 -08001129 errorDialog =
linyuh7b86f562017-11-16 11:24:09 -08001130 builder
1131 .setView(dialogCheckBoxView)
1132 .setMessage(R.string.video_call_lte_to_wifi_failed_message)
1133 .setOnCancelListener(dialogInterface -> onDialogDismissed())
1134 .setPositiveButton(
1135 android.R.string.ok,
1136 (dialogInterface, id) -> {
1137 call.setDoNotShowDialogForHandoffToWifiFailure(
1138 wifiHandoverFailureCheckbox.isChecked());
1139 dialogInterface.cancel();
1140 onDialogDismissed();
1141 })
1142 .setOnDismissListener(dialogInterface -> lock.release())
1143 .create();
linyuh7b86f562017-11-16 11:24:09 -08001144 errorDialog.show();
Eric Erfanianccca3152017-02-22 16:32:36 -08001145 }
1146
linyuh7b86f562017-11-16 11:24:09 -08001147 public void showDialogForInternationalCallOnWifi(@NonNull DialerCall call) {
1148 if (!InternationalCallOnWifiDialogFragment.shouldShow(this)) {
1149 LogUtil.i(
1150 "InCallActivity.showDialogForInternationalCallOnWifi",
1151 "InternationalCallOnWifiDialogFragment.shouldShow returned false");
1152 return;
1153 }
1154
1155 InternationalCallOnWifiDialogFragment fragment =
1156 InternationalCallOnWifiDialogFragment.newInstance(
linyuhc3968e62017-11-20 17:40:50 -08001157 call.getId(), internationalCallOnWifiCallback);
1158 fragment.show(getSupportFragmentManager(), Tags.INTERNATIONAL_CALL_ON_WIFI);
Eric Erfanianc857f902017-05-15 14:05:33 -07001159 }
1160
Eric Erfanian938468d2017-10-24 14:05:52 -07001161 @Override
1162 public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
1163 super.onMultiWindowModeChanged(isInMultiWindowMode);
linyuh57b093b2017-11-17 14:32:32 -08001164 updateNavigationBar(isDialpadVisible());
1165 }
1166
linyuhc3968e62017-11-20 17:40:50 -08001167 private void updateNavigationBar(boolean isDialpadVisible) {
linyuh57b093b2017-11-17 14:32:32 -08001168 if (ActivityCompat.isInMultiWindowMode(this)) {
1169 return;
1170 }
1171
1172 View navigationBarBackground = getWindow().findViewById(R.id.navigation_bar_background);
1173 if (navigationBarBackground != null) {
1174 navigationBarBackground.setVisibility(isDialpadVisible ? View.VISIBLE : View.GONE);
Eric Erfanian938468d2017-10-24 14:05:52 -07001175 }
1176 }
1177
Eric Erfanianccca3152017-02-22 16:32:36 -08001178 public void setAllowOrientationChange(boolean allowOrientationChange) {
wangqi9982f0d2017-10-11 17:46:07 -07001179 if (this.allowOrientationChange == allowOrientationChange) {
1180 return;
1181 }
1182 this.allowOrientationChange = allowOrientationChange;
Eric Erfanianccca3152017-02-22 16:32:36 -08001183 if (!allowOrientationChange) {
1184 setRequestedOrientation(InCallOrientationEventListener.ACTIVITY_PREFERENCE_DISALLOW_ROTATION);
1185 } else {
1186 setRequestedOrientation(InCallOrientationEventListener.ACTIVITY_PREFERENCE_ALLOW_ROTATION);
1187 }
1188 enableInCallOrientationEventListener(allowOrientationChange);
1189 }
1190
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001191 public void hideMainInCallFragment() {
linyuhc3968e62017-11-20 17:40:50 -08001192 LogUtil.enterBlock("InCallActivity.hideMainInCallFragment");
1193 if (getCallCardFragmentVisible()) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001194 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
1195 hideInCallScreenFragment(transaction);
1196 hideVideoCallScreenFragment(transaction);
1197 transaction.commitAllowingStateLoss();
1198 getSupportFragmentManager().executePendingTransactions();
1199 }
1200 }
1201
1202 private void showMainInCallFragment() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001203 Trace.beginSection("InCallActivity.showMainInCallFragment");
Eric Erfanianccca3152017-02-22 16:32:36 -08001204 // If the activity's onStart method hasn't been called yet then defer doing any work.
1205 if (!isVisible) {
1206 LogUtil.i("InCallActivity.showMainInCallFragment", "not visible yet/anymore");
Eric Erfanian2ca43182017-08-31 06:57:16 -07001207 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001208 return;
1209 }
1210
1211 // Don't let this be reentrant.
1212 if (isInShowMainInCallFragment) {
1213 LogUtil.i("InCallActivity.showMainInCallFragment", "already in method, bailing");
Eric Erfanian2ca43182017-08-31 06:57:16 -07001214 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001215 return;
1216 }
1217
1218 isInShowMainInCallFragment = true;
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001219 ShouldShowUiResult shouldShowAnswerUi = getShouldShowAnswerUi();
1220 ShouldShowUiResult shouldShowVideoUi = getShouldShowVideoUi();
Eric Erfanianccca3152017-02-22 16:32:36 -08001221 LogUtil.i(
1222 "InCallActivity.showMainInCallFragment",
1223 "shouldShowAnswerUi: %b, shouldShowVideoUi: %b, "
1224 + "didShowAnswerScreen: %b, didShowInCallScreen: %b, didShowVideoCallScreen: %b",
1225 shouldShowAnswerUi.shouldShow,
Eric Erfanian10b34a52017-05-04 08:23:17 -07001226 shouldShowVideoUi.shouldShow,
Eric Erfanianccca3152017-02-22 16:32:36 -08001227 didShowAnswerScreen,
1228 didShowInCallScreen,
1229 didShowVideoCallScreen);
1230 // Only video call ui allows orientation change.
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001231 setAllowOrientationChange(shouldShowVideoUi.shouldShow);
Eric Erfanianccca3152017-02-22 16:32:36 -08001232
1233 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
1234 boolean didChangeInCall;
1235 boolean didChangeVideo;
1236 boolean didChangeAnswer;
1237 if (shouldShowAnswerUi.shouldShow) {
1238 didChangeInCall = hideInCallScreenFragment(transaction);
1239 didChangeVideo = hideVideoCallScreenFragment(transaction);
1240 didChangeAnswer = showAnswerScreenFragment(transaction, shouldShowAnswerUi.call);
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001241 } else if (shouldShowVideoUi.shouldShow) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001242 didChangeInCall = hideInCallScreenFragment(transaction);
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001243 didChangeVideo = showVideoCallScreenFragment(transaction, shouldShowVideoUi.call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001244 didChangeAnswer = hideAnswerScreenFragment(transaction);
1245 } else {
1246 didChangeInCall = showInCallScreenFragment(transaction);
1247 didChangeVideo = hideVideoCallScreenFragment(transaction);
1248 didChangeAnswer = hideAnswerScreenFragment(transaction);
1249 }
1250
1251 if (didChangeInCall || didChangeVideo || didChangeAnswer) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001252 Trace.beginSection("InCallActivity.commitTransaction");
Eric Erfanianccca3152017-02-22 16:32:36 -08001253 transaction.commitNow();
Eric Erfanian2ca43182017-08-31 06:57:16 -07001254 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001255 Logger.get(this).logScreenView(ScreenEvent.Type.INCALL, this);
1256 }
1257 isInShowMainInCallFragment = false;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001258 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001259 }
1260
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001261 private ShouldShowUiResult getShouldShowAnswerUi() {
Eric Erfanianccca3152017-02-22 16:32:36 -08001262 DialerCall call = CallList.getInstance().getIncomingCall();
1263 if (call != null) {
1264 LogUtil.i("InCallActivity.getShouldShowAnswerUi", "found incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001265 return new ShouldShowUiResult(true, call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001266 }
1267
1268 call = CallList.getInstance().getVideoUpgradeRequestCall();
1269 if (call != null) {
1270 LogUtil.i("InCallActivity.getShouldShowAnswerUi", "found video upgrade request");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001271 return new ShouldShowUiResult(true, call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001272 }
1273
1274 // Check if we're showing the answer screen and the call is disconnected. If this condition is
1275 // true then we won't switch from the answer UI to the in call UI. This prevents flicker when
1276 // the user rejects an incoming call.
1277 call = CallList.getInstance().getFirstCall();
1278 if (call == null) {
1279 call = CallList.getInstance().getBackgroundCall();
1280 }
1281 if (didShowAnswerScreen && (call == null || call.getState() == State.DISCONNECTED)) {
1282 LogUtil.i("InCallActivity.getShouldShowAnswerUi", "found disconnecting incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001283 return new ShouldShowUiResult(true, call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001284 }
1285
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001286 return new ShouldShowUiResult(false, null);
Eric Erfanianccca3152017-02-22 16:32:36 -08001287 }
1288
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001289 private static ShouldShowUiResult getShouldShowVideoUi() {
Eric Erfanianccca3152017-02-22 16:32:36 -08001290 DialerCall call = CallList.getInstance().getFirstCall();
1291 if (call == null) {
1292 LogUtil.i("InCallActivity.getShouldShowVideoUi", "null call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001293 return new ShouldShowUiResult(false, null);
Eric Erfanianccca3152017-02-22 16:32:36 -08001294 }
1295
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001296 if (call.isVideoCall()) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001297 LogUtil.i("InCallActivity.getShouldShowVideoUi", "found video call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001298 return new ShouldShowUiResult(true, call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001299 }
1300
linyuh8fbecce2017-12-18 13:53:09 -08001301 if (call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest()) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001302 LogUtil.i("InCallActivity.getShouldShowVideoUi", "upgrading to video");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001303 return new ShouldShowUiResult(true, call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001304 }
1305
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001306 return new ShouldShowUiResult(false, null);
Eric Erfanianccca3152017-02-22 16:32:36 -08001307 }
1308
1309 private boolean showAnswerScreenFragment(FragmentTransaction transaction, DialerCall call) {
1310 // When rejecting a call the active call can become null in which case we should continue
1311 // showing the answer screen.
1312 if (didShowAnswerScreen && call == null) {
1313 return false;
1314 }
1315
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001316 Assert.checkArgument(call != null, "didShowAnswerScreen was false but call was still null");
1317
1318 boolean isVideoUpgradeRequest = call.hasReceivedVideoUpgradeRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -08001319
1320 // Check if we're already showing an answer screen for this call.
1321 if (didShowAnswerScreen) {
1322 AnswerScreen answerScreen = getAnswerScreen();
1323 if (answerScreen.getCallId().equals(call.getId())
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001324 && answerScreen.isVideoCall() == call.isVideoCall()
Eric Erfanian2ca43182017-08-31 06:57:16 -07001325 && answerScreen.isVideoUpgradeRequest() == isVideoUpgradeRequest
1326 && !answerScreen.isActionTimeout()) {
1327 LogUtil.d(
1328 "InCallActivity.showAnswerScreenFragment",
1329 "answer fragment exists for same call and has NOT been accepted/rejected/timed out");
Eric Erfanianccca3152017-02-22 16:32:36 -08001330 return false;
1331 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001332 if (answerScreen.isActionTimeout()) {
1333 LogUtil.i(
1334 "InCallActivity.showAnswerScreenFragment",
1335 "answer fragment exists but has been accepted/rejected and timed out");
1336 } else {
1337 LogUtil.i(
1338 "InCallActivity.showAnswerScreenFragment",
1339 "answer fragment exists but arguments do not match");
1340 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001341 hideAnswerScreenFragment(transaction);
1342 }
1343
1344 // Show a new answer screen.
1345 AnswerScreen answerScreen =
Eric Erfanianfc37b022017-03-21 10:11:17 -07001346 AnswerBindings.createAnswerScreen(
1347 call.getId(),
1348 call.isVideoCall(),
1349 isVideoUpgradeRequest,
Eric Erfanian90508232017-03-24 09:31:16 -07001350 call.getVideoTech().isSelfManagedCamera(),
1351 shouldAllowAnswerAndRelease(call),
1352 CallList.getInstance().getBackgroundCall() != null);
linyuhc3968e62017-11-20 17:40:50 -08001353 transaction.add(R.id.main, answerScreen.getAnswerScreenFragment(), Tags.ANSWER_SCREEN);
Eric Erfanianccca3152017-02-22 16:32:36 -08001354
1355 Logger.get(this).logScreenView(ScreenEvent.Type.INCOMING_CALL, this);
1356 didShowAnswerScreen = true;
1357 return true;
1358 }
1359
Eric Erfanian90508232017-03-24 09:31:16 -07001360 private boolean shouldAllowAnswerAndRelease(DialerCall call) {
1361 if (CallList.getInstance().getActiveCall() == null) {
1362 LogUtil.i("InCallActivity.shouldAllowAnswerAndRelease", "no active call");
1363 return false;
1364 }
1365 if (getSystemService(TelephonyManager.class).getPhoneType()
1366 == TelephonyManager.PHONE_TYPE_CDMA) {
1367 LogUtil.i("InCallActivity.shouldAllowAnswerAndRelease", "PHONE_TYPE_CDMA not supported");
1368 return false;
1369 }
1370 if (call.isVideoCall() || call.hasReceivedVideoUpgradeRequest()) {
1371 LogUtil.i("InCallActivity.shouldAllowAnswerAndRelease", "video call");
1372 return false;
1373 }
linyuhc3968e62017-11-20 17:40:50 -08001374 if (!ConfigProviderBindings.get(this)
1375 .getBoolean(ConfigNames.ANSWER_AND_RELEASE_ENABLED, true)) {
Eric Erfanian90508232017-03-24 09:31:16 -07001376 LogUtil.i("InCallActivity.shouldAllowAnswerAndRelease", "disabled by config");
1377 return false;
1378 }
1379
1380 return true;
1381 }
1382
Eric Erfanianccca3152017-02-22 16:32:36 -08001383 private boolean hideAnswerScreenFragment(FragmentTransaction transaction) {
1384 if (!didShowAnswerScreen) {
1385 return false;
1386 }
1387 AnswerScreen answerScreen = getAnswerScreen();
1388 if (answerScreen != null) {
1389 transaction.remove(answerScreen.getAnswerScreenFragment());
1390 }
1391
1392 didShowAnswerScreen = false;
1393 return true;
1394 }
1395
1396 private boolean showInCallScreenFragment(FragmentTransaction transaction) {
1397 if (didShowInCallScreen) {
1398 return false;
1399 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001400 InCallScreen inCallScreen = InCallBindings.createInCallScreen();
linyuhc3968e62017-11-20 17:40:50 -08001401 transaction.add(R.id.main, inCallScreen.getInCallScreenFragment(), Tags.IN_CALL_SCREEN);
Eric Erfanianccca3152017-02-22 16:32:36 -08001402 Logger.get(this).logScreenView(ScreenEvent.Type.INCALL, this);
1403 didShowInCallScreen = true;
1404 return true;
1405 }
1406
1407 private boolean hideInCallScreenFragment(FragmentTransaction transaction) {
1408 if (!didShowInCallScreen) {
1409 return false;
1410 }
1411 InCallScreen inCallScreen = getInCallScreen();
1412 if (inCallScreen != null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001413 transaction.remove(inCallScreen.getInCallScreenFragment());
Eric Erfanianccca3152017-02-22 16:32:36 -08001414 }
1415 didShowInCallScreen = false;
1416 return true;
1417 }
1418
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001419 private boolean showVideoCallScreenFragment(FragmentTransaction transaction, DialerCall call) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001420 if (didShowVideoCallScreen) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001421 VideoCallScreen videoCallScreen = getVideoCallScreen();
1422 if (videoCallScreen.getCallId().equals(call.getId())) {
1423 return false;
1424 }
1425 LogUtil.i(
1426 "InCallActivity.showVideoCallScreenFragment",
1427 "video call fragment exists but arguments do not match");
1428 hideVideoCallScreenFragment(transaction);
Eric Erfanianccca3152017-02-22 16:32:36 -08001429 }
1430
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001431 LogUtil.i("InCallActivity.showVideoCallScreenFragment", "call: %s", call);
1432
Eric Erfanian90508232017-03-24 09:31:16 -07001433 VideoCallScreen videoCallScreen =
1434 VideoBindings.createVideoCallScreen(
1435 call.getId(), call.getVideoTech().shouldUseSurfaceView());
linyuhc3968e62017-11-20 17:40:50 -08001436 transaction.add(
1437 R.id.main, videoCallScreen.getVideoCallScreenFragment(), Tags.VIDEO_CALL_SCREEN);
Eric Erfanianccca3152017-02-22 16:32:36 -08001438
1439 Logger.get(this).logScreenView(ScreenEvent.Type.INCALL, this);
1440 didShowVideoCallScreen = true;
1441 return true;
1442 }
1443
1444 private boolean hideVideoCallScreenFragment(FragmentTransaction transaction) {
1445 if (!didShowVideoCallScreen) {
1446 return false;
1447 }
1448 VideoCallScreen videoCallScreen = getVideoCallScreen();
1449 if (videoCallScreen != null) {
1450 transaction.remove(videoCallScreen.getVideoCallScreenFragment());
1451 }
1452 didShowVideoCallScreen = false;
1453 return true;
1454 }
1455
linyuhc3968e62017-11-20 17:40:50 -08001456 private AnswerScreen getAnswerScreen() {
1457 return (AnswerScreen) getSupportFragmentManager().findFragmentByTag(Tags.ANSWER_SCREEN);
Eric Erfanianccca3152017-02-22 16:32:36 -08001458 }
1459
linyuhc3968e62017-11-20 17:40:50 -08001460 private InCallScreen getInCallScreen() {
1461 return (InCallScreen) getSupportFragmentManager().findFragmentByTag(Tags.IN_CALL_SCREEN);
Eric Erfanianccca3152017-02-22 16:32:36 -08001462 }
1463
linyuhc3968e62017-11-20 17:40:50 -08001464 private VideoCallScreen getVideoCallScreen() {
1465 return (VideoCallScreen) getSupportFragmentManager().findFragmentByTag(Tags.VIDEO_CALL_SCREEN);
Eric Erfanianccca3152017-02-22 16:32:36 -08001466 }
1467
1468 @Override
1469 public void onPseudoScreenStateChanged(boolean isOn) {
1470 LogUtil.i("InCallActivity.onPseudoScreenStateChanged", "isOn: " + isOn);
1471 pseudoBlackScreenOverlay.setVisibility(isOn ? View.GONE : View.VISIBLE);
1472 }
1473
1474 /**
1475 * For some touch related issue, turning off the screen can be faked by drawing a black view over
1476 * the activity. All touch events started when the screen is "off" is rejected.
1477 *
1478 * @see PseudoScreenState
1479 */
1480 @Override
1481 public boolean dispatchTouchEvent(MotionEvent event) {
1482 // Reject any gesture that started when the screen is in the fake off state.
1483 if (touchDownWhenPseudoScreenOff) {
1484 if (event.getAction() == MotionEvent.ACTION_UP) {
1485 touchDownWhenPseudoScreenOff = false;
1486 }
1487 return true;
1488 }
1489 // Reject all touch event when the screen is in the fake off state.
1490 if (!InCallPresenter.getInstance().getPseudoScreenState().isOn()) {
1491 if (event.getAction() == MotionEvent.ACTION_DOWN) {
1492 touchDownWhenPseudoScreenOff = true;
1493 LogUtil.i("InCallActivity.dispatchTouchEvent", "touchDownWhenPseudoScreenOff");
1494 }
1495 return true;
1496 }
1497 return super.dispatchTouchEvent(event);
1498 }
1499
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001500 private static class ShouldShowUiResult {
Eric Erfanianccca3152017-02-22 16:32:36 -08001501 public final boolean shouldShow;
1502 public final DialerCall call;
1503
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001504 ShouldShowUiResult(boolean shouldShow, DialerCall call) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001505 this.shouldShow = shouldShow;
1506 this.call = call;
1507 }
1508 }
linyuhc3968e62017-11-20 17:40:50 -08001509
1510 private static final class IntentExtraNames {
1511 static final String FOR_FULL_SCREEN = "InCallActivity.for_full_screen_intent";
1512 static final String NEW_OUTGOING_CALL = "InCallActivity.new_outgoing_call";
1513 static final String SHOW_DIALPAD = "InCallActivity.show_dialpad";
1514 }
1515
1516 private static final class KeysForSavedInstance {
1517 static final String DIALPAD_TEXT = "InCallActivity.dialpad_text";
1518 static final String DID_SHOW_ANSWER_SCREEN = "did_show_answer_screen";
1519 static final String DID_SHOW_IN_CALL_SCREEN = "did_show_in_call_screen";
1520 static final String DID_SHOW_VIDEO_CALL_SCREEN = "did_show_video_call_screen";
1521 }
1522
1523 /** Request codes for pending intents. */
1524 public static final class PendingIntentRequestCodes {
1525 static final int NON_FULL_SCREEN = 0;
1526 static final int FULL_SCREEN = 1;
1527 static final int BUBBLE = 2;
1528 }
1529
1530 private static final class Tags {
1531 static final String ANSWER_SCREEN = "tag_answer_screen";
1532 static final String DIALPAD_FRAGMENT = "tag_dialpad_fragment";
1533 static final String IN_CALL_SCREEN = "tag_in_call_screen";
1534 static final String INTERNATIONAL_CALL_ON_WIFI = "tag_international_call_on_wifi";
1535 static final String SELECT_ACCOUNT_FRAGMENT = "tag_select_account_fragment";
1536 static final String VIDEO_CALL_SCREEN = "tag_video_call_screen";
1537 static final String POST_CHAR_DIALOG_FRAGMENT = "tag_post_char_dialog_fragment";
1538 }
1539
1540 private static final class ConfigNames {
1541 static final String ANSWER_AND_RELEASE_ENABLED = "answer_and_release_enabled";
1542 }
1543
1544 private static final class InternationalCallOnWifiCallback
1545 implements InternationalCallOnWifiDialogFragment.Callback {
1546 private static final String TAG = InternationalCallOnWifiCallback.class.getCanonicalName();
1547
1548 @Override
1549 public void continueCall(@NonNull String callId) {
1550 LogUtil.i(TAG, "Continuing call with ID: %s", callId);
1551 }
1552
1553 @Override
1554 public void cancelCall(@NonNull String callId) {
1555 DialerCall call = CallList.getInstance().getCallById(callId);
1556 if (call == null) {
1557 LogUtil.i(TAG, "Call destroyed before the dialog is closed");
1558 return;
1559 }
1560
1561 LogUtil.i(TAG, "Disconnecting international call on WiFi");
1562 call.disconnect();
1563 }
1564 }
1565
1566 private static final class SelectPhoneAccountListener
1567 extends SelectPhoneAccountDialogFragment.SelectPhoneAccountListener {
1568 private static final String TAG = SelectPhoneAccountListener.class.getCanonicalName();
1569
1570 @Override
1571 public void onPhoneAccountSelected(
1572 PhoneAccountHandle selectedAccountHandle, boolean setDefault, String callId) {
1573 DialerCall call = CallList.getInstance().getCallById(callId);
1574 LogUtil.i(TAG, "Phone account select with call:\n%s", call);
1575
1576 if (call != null) {
1577 call.phoneAccountSelected(selectedAccountHandle, setDefault);
1578 }
1579 }
1580
1581 @Override
1582 public void onDialogDismissed(String callId) {
1583 DialerCall call = CallList.getInstance().getCallById(callId);
1584 LogUtil.i(TAG, "Disconnecting call:\n%s" + call);
1585
1586 if (call != null) {
1587 call.disconnect();
1588 }
1589 }
1590 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001591}