blob: e9c2467ef94f3894cf8b64b643e76cb7965c6cd6 [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
19import android.app.ActivityManager;
20import android.app.ActivityManager.AppTask;
21import android.app.ActivityManager.TaskDescription;
22import android.app.AlertDialog;
23import android.app.Dialog;
Eric Erfanian2ca43182017-08-31 06:57:16 -070024import android.app.KeyguardManager;
Eric Erfanianccca3152017-02-22 16:32:36 -080025import android.content.DialogInterface;
26import android.content.DialogInterface.OnCancelListener;
27import android.content.DialogInterface.OnDismissListener;
28import android.content.Intent;
29import android.content.res.Configuration;
30import android.content.res.Resources;
31import android.os.Bundle;
wangqi9982f0d2017-10-11 17:46:07 -070032import android.os.Trace;
Eric Erfanianccca3152017-02-22 16:32:36 -080033import android.support.annotation.IntDef;
34import android.support.annotation.NonNull;
35import android.support.annotation.Nullable;
linyuh9a915fc2017-11-09 16:27:13 -080036import android.support.annotation.VisibleForTesting;
Eric Erfanianccca3152017-02-22 16:32:36 -080037import android.support.v4.app.Fragment;
38import android.support.v4.app.FragmentManager;
39import android.support.v4.app.FragmentTransaction;
40import android.support.v4.content.res.ResourcesCompat;
linyuh9a915fc2017-11-09 16:27:13 -080041import android.telecom.CallAudioState;
Eric Erfanianccca3152017-02-22 16:32:36 -080042import android.telecom.PhoneAccountHandle;
Eric Erfanianccca3152017-02-22 16:32:36 -080043import android.view.KeyEvent;
44import android.view.View;
45import android.view.Window;
46import android.view.WindowManager;
47import android.view.animation.Animation;
48import android.view.animation.AnimationUtils;
49import android.widget.CheckBox;
50import android.widget.Toast;
51import com.android.contacts.common.widget.SelectPhoneAccountDialogFragment;
52import com.android.contacts.common.widget.SelectPhoneAccountDialogFragment.SelectPhoneAccountListener;
53import com.android.dialer.animation.AnimUtils;
54import com.android.dialer.animation.AnimationListenerAdapter;
55import com.android.dialer.common.LogUtil;
wangqifad3d872017-10-25 13:15:23 -070056import com.android.dialer.compat.ActivityCompat;
Eric Erfanianccca3152017-02-22 16:32:36 -080057import com.android.dialer.compat.CompatUtils;
58import com.android.dialer.logging.Logger;
Eric Erfanian8369df02017-05-03 10:27:13 -070059import com.android.dialer.logging.ScreenEvent;
Eric Erfanianccca3152017-02-22 16:32:36 -080060import com.android.dialer.util.ViewUtil;
Eric Erfanian8369df02017-05-03 10:27:13 -070061import com.android.incallui.audiomode.AudioModeProvider;
Eric Erfanianccca3152017-02-22 16:32:36 -080062import com.android.incallui.call.CallList;
63import com.android.incallui.call.DialerCall;
64import com.android.incallui.call.DialerCall.State;
65import com.android.incallui.call.TelecomAdapter;
Eric Erfanian2ca43182017-08-31 06:57:16 -070066import com.android.incallui.disconnectdialog.DisconnectMessage;
twyen8efb4952017-10-06 16:35:54 -070067import com.android.incallui.incalluilock.InCallUiLock;
Eric Erfanianc857f902017-05-15 14:05:33 -070068import com.android.incallui.telecomeventui.InternationalCallOnWifiDialogFragment;
69import com.android.incallui.telecomeventui.InternationalCallOnWifiDialogFragment.Callback;
linyuh9a915fc2017-11-09 16:27:13 -080070import com.google.common.base.Optional;
Eric Erfanianccca3152017-02-22 16:32:36 -080071import java.lang.annotation.Retention;
72import java.lang.annotation.RetentionPolicy;
73import java.util.ArrayList;
74import java.util.List;
75
76/** Shared functionality between the new and old in call activity. */
77public class InCallActivityCommon {
78
79 private static final String INTENT_EXTRA_SHOW_DIALPAD = "InCallActivity.show_dialpad";
80 private static final String INTENT_EXTRA_NEW_OUTGOING_CALL = "InCallActivity.new_outgoing_call";
81 private static final String INTENT_EXTRA_FOR_FULL_SCREEN =
82 "InCallActivity.for_full_screen_intent";
83
84 private static final String DIALPAD_TEXT_KEY = "InCallActivity.dialpad_text";
85
86 private static final String TAG_SELECT_ACCOUNT_FRAGMENT = "tag_select_account_fragment";
87 private static final String TAG_DIALPAD_FRAGMENT = "tag_dialpad_fragment";
Eric Erfanianc857f902017-05-15 14:05:33 -070088 private static final String TAG_INTERNATIONAL_CALL_ON_WIFI = "tag_international_call_on_wifi";
Eric Erfanianccca3152017-02-22 16:32:36 -080089
90 @Retention(RetentionPolicy.SOURCE)
91 @IntDef({
92 DIALPAD_REQUEST_NONE,
93 DIALPAD_REQUEST_SHOW,
94 DIALPAD_REQUEST_HIDE,
95 })
96 @interface DialpadRequestType {}
97
98 private static final int DIALPAD_REQUEST_NONE = 1;
99 private static final int DIALPAD_REQUEST_SHOW = 2;
100 private static final int DIALPAD_REQUEST_HIDE = 3;
101
linyuh9a915fc2017-11-09 16:27:13 -0800102 private static Optional<Integer> audioRouteForTesting = Optional.absent();
103
Eric Erfanianccca3152017-02-22 16:32:36 -0800104 private final InCallActivity inCallActivity;
Eric Erfanianccca3152017-02-22 16:32:36 -0800105 private boolean showPostCharWaitDialogOnResume;
106 private String showPostCharWaitDialogCallId;
107 private String showPostCharWaitDialogChars;
linyuhf99f6302017-11-15 11:23:51 -0800108 private Dialog errorDialog;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700109 private SelectPhoneAccountDialogFragment selectPhoneAccountDialogFragment;
Eric Erfanianccca3152017-02-22 16:32:36 -0800110 private InCallOrientationEventListener inCallOrientationEventListener;
111 private Animation dialpadSlideInAnimation;
112 private Animation dialpadSlideOutAnimation;
113 private boolean animateDialpadOnShow;
114 private String dtmfTextToPreopulate;
115 @DialpadRequestType private int showDialpadRequest = DIALPAD_REQUEST_NONE;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700116 // If activity is going to be recreated. This is usually happening in {@link onNewIntent}.
117 private boolean isRecreating;
Eric Erfanianccca3152017-02-22 16:32:36 -0800118
Eric Erfanianc857f902017-05-15 14:05:33 -0700119 private final SelectPhoneAccountListener selectAccountListener =
Eric Erfanianccca3152017-02-22 16:32:36 -0800120 new SelectPhoneAccountListener() {
121 @Override
122 public void onPhoneAccountSelected(
123 PhoneAccountHandle selectedAccountHandle, boolean setDefault, String callId) {
124 DialerCall call = CallList.getInstance().getCallById(callId);
125 LogUtil.i(
126 "InCallActivityCommon.SelectPhoneAccountListener.onPhoneAccountSelected",
127 "call: " + call);
128 if (call != null) {
129 call.phoneAccountSelected(selectedAccountHandle, setDefault);
130 }
131 }
132
133 @Override
134 public void onDialogDismissed(String callId) {
135 DialerCall call = CallList.getInstance().getCallById(callId);
136 LogUtil.i(
137 "InCallActivityCommon.SelectPhoneAccountListener.onDialogDismissed",
138 "disconnecting call: " + call);
139 if (call != null) {
140 call.disconnect();
141 }
142 }
143 };
144
Eric Erfanianc857f902017-05-15 14:05:33 -0700145 private InternationalCallOnWifiDialogFragment.Callback internationalCallOnWifiCallback =
146 new Callback() {
147 @Override
148 public void continueCall(@NonNull String callId) {
149 LogUtil.i("InCallActivityCommon.continueCall", "continuing call with id: %s", callId);
150 }
151
152 @Override
153 public void cancelCall(@NonNull String callId) {
154 DialerCall call = CallList.getInstance().getCallById(callId);
155 if (call == null) {
156 LogUtil.i("InCallActivityCommon.cancelCall", "call destroyed before dialog closed");
157 return;
158 }
159 LogUtil.i("InCallActivityCommon.cancelCall", "disconnecting international call on wifi");
160 call.disconnect();
161 }
162 };
163
Eric Erfanianccca3152017-02-22 16:32:36 -0800164 public static void setIntentExtras(
165 Intent intent, boolean showDialpad, boolean newOutgoingCall, boolean isForFullScreen) {
166 if (showDialpad) {
167 intent.putExtra(INTENT_EXTRA_SHOW_DIALPAD, true);
168 }
169 intent.putExtra(INTENT_EXTRA_NEW_OUTGOING_CALL, newOutgoingCall);
170 intent.putExtra(INTENT_EXTRA_FOR_FULL_SCREEN, isForFullScreen);
171 }
172
173 public InCallActivityCommon(InCallActivity inCallActivity) {
174 this.inCallActivity = inCallActivity;
175 }
176
177 public void onCreate(Bundle icicle) {
linyuh9a915fc2017-11-09 16:27:13 -0800178 setWindowFlags();
Eric Erfanianccca3152017-02-22 16:32:36 -0800179
180 inCallActivity.setContentView(R.layout.incall_screen);
181
182 internalResolveIntent(inCallActivity.getIntent());
183
184 boolean isLandscape =
185 inCallActivity.getResources().getConfiguration().orientation
186 == Configuration.ORIENTATION_LANDSCAPE;
187 boolean isRtl = ViewUtil.isRtl();
188
189 if (isLandscape) {
190 dialpadSlideInAnimation =
191 AnimationUtils.loadAnimation(
192 inCallActivity, isRtl ? R.anim.dialpad_slide_in_left : R.anim.dialpad_slide_in_right);
193 dialpadSlideOutAnimation =
194 AnimationUtils.loadAnimation(
195 inCallActivity,
196 isRtl ? R.anim.dialpad_slide_out_left : R.anim.dialpad_slide_out_right);
197 } else {
198 dialpadSlideInAnimation =
199 AnimationUtils.loadAnimation(inCallActivity, R.anim.dialpad_slide_in_bottom);
200 dialpadSlideOutAnimation =
201 AnimationUtils.loadAnimation(inCallActivity, R.anim.dialpad_slide_out_bottom);
202 }
203
204 dialpadSlideInAnimation.setInterpolator(AnimUtils.EASE_IN);
205 dialpadSlideOutAnimation.setInterpolator(AnimUtils.EASE_OUT);
206
207 dialpadSlideOutAnimation.setAnimationListener(
208 new AnimationListenerAdapter() {
209 @Override
210 public void onAnimationEnd(Animation animation) {
211 performHideDialpadFragment();
212 }
213 });
214
Eric Erfanian2ca43182017-08-31 06:57:16 -0700215 // Don't override the value if show dialpad request is true in intent extras.
216 if (icicle != null && showDialpadRequest == DIALPAD_REQUEST_NONE) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800217 // If the dialpad was shown before, set variables indicating it should be shown and
218 // populated with the previous DTMF text. The dialpad is actually shown and populated
219 // in onResume() to ensure the hosting fragment has been inflated and is ready to receive it.
220 if (icicle.containsKey(INTENT_EXTRA_SHOW_DIALPAD)) {
221 boolean showDialpad = icicle.getBoolean(INTENT_EXTRA_SHOW_DIALPAD);
222 showDialpadRequest = showDialpad ? DIALPAD_REQUEST_SHOW : DIALPAD_REQUEST_HIDE;
223 animateDialpadOnShow = false;
224 }
225 dtmfTextToPreopulate = icicle.getString(DIALPAD_TEXT_KEY);
226
227 SelectPhoneAccountDialogFragment dialogFragment =
228 (SelectPhoneAccountDialogFragment)
229 inCallActivity.getFragmentManager().findFragmentByTag(TAG_SELECT_ACCOUNT_FRAGMENT);
230 if (dialogFragment != null) {
231 dialogFragment.setListener(selectAccountListener);
232 }
233 }
234
Eric Erfanianc857f902017-05-15 14:05:33 -0700235 InternationalCallOnWifiDialogFragment existingInternationalFragment =
236 (InternationalCallOnWifiDialogFragment)
237 inCallActivity
238 .getSupportFragmentManager()
239 .findFragmentByTag(TAG_INTERNATIONAL_CALL_ON_WIFI);
240 if (existingInternationalFragment != null) {
241 LogUtil.i(
242 "InCallActivityCommon.onCreate", "international fragment exists attaching callback");
243 existingInternationalFragment.setCallback(internationalCallOnWifiCallback);
244 }
245
Eric Erfanianccca3152017-02-22 16:32:36 -0800246 inCallOrientationEventListener = new InCallOrientationEventListener(inCallActivity);
247 }
248
249 public void onSaveInstanceState(Bundle out) {
250 // TODO: The dialpad fragment should handle this as part of its own state
251 out.putBoolean(INTENT_EXTRA_SHOW_DIALPAD, isDialpadVisible());
252 DialpadFragment dialpadFragment = getDialpadFragment();
253 if (dialpadFragment != null) {
254 out.putString(DIALPAD_TEXT_KEY, dialpadFragment.getDtmfText());
255 }
256 }
257
258 public void onStart() {
wangqi9982f0d2017-10-11 17:46:07 -0700259 Trace.beginSection("InCallActivityCommon.onStart");
Eric Erfanianccca3152017-02-22 16:32:36 -0800260 // setting activity should be last thing in setup process
261 InCallPresenter.getInstance().setActivity(inCallActivity);
262 enableInCallOrientationEventListener(
263 inCallActivity.getRequestedOrientation()
264 == InCallOrientationEventListener.ACTIVITY_PREFERENCE_ALLOW_ROTATION);
265
266 InCallPresenter.getInstance().onActivityStarted();
yueg7b28abc2017-09-21 16:08:30 -0700267 if (!isRecreating) {
268 InCallPresenter.getInstance().onUiShowing(true);
269 }
wangqi9982f0d2017-10-11 17:46:07 -0700270 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800271 }
272
273 public void onResume() {
wangqi9982f0d2017-10-11 17:46:07 -0700274 Trace.beginSection("InCallActivityCommon.onResume");
Eric Erfanianccca3152017-02-22 16:32:36 -0800275 if (InCallPresenter.getInstance().isReadyForTearDown()) {
276 LogUtil.i(
277 "InCallActivityCommon.onResume",
278 "InCallPresenter is ready for tear down, not sending updates");
279 } else {
280 updateTaskDescription();
Eric Erfanianccca3152017-02-22 16:32:36 -0800281 }
282
283 // If there is a pending request to show or hide the dialpad, handle that now.
284 if (showDialpadRequest != DIALPAD_REQUEST_NONE) {
285 if (showDialpadRequest == DIALPAD_REQUEST_SHOW) {
286 // Exit fullscreen so that the user has access to the dialpad hide/show button and
287 // can hide the dialpad. Important when showing the dialpad from within dialer.
288 InCallPresenter.getInstance().setFullScreen(false, true /* force */);
289
290 inCallActivity.showDialpadFragment(true /* show */, animateDialpadOnShow /* animate */);
291 animateDialpadOnShow = false;
292
293 DialpadFragment dialpadFragment = getDialpadFragment();
294 if (dialpadFragment != null) {
295 dialpadFragment.setDtmfText(dtmfTextToPreopulate);
296 dtmfTextToPreopulate = null;
297 }
298 } else {
299 LogUtil.i("InCallActivityCommon.onResume", "force hide dialpad");
300 if (getDialpadFragment() != null) {
301 inCallActivity.showDialpadFragment(false /* show */, false /* animate */);
302 }
303 }
304 showDialpadRequest = DIALPAD_REQUEST_NONE;
305 }
Eric Erfanian938468d2017-10-24 14:05:52 -0700306 updateNavigationBar(isDialpadVisible());
Eric Erfanianccca3152017-02-22 16:32:36 -0800307
308 if (showPostCharWaitDialogOnResume) {
309 showPostCharWaitDialog(showPostCharWaitDialogCallId, showPostCharWaitDialogChars);
310 }
311
312 CallList.getInstance()
313 .onInCallUiShown(
314 inCallActivity.getIntent().getBooleanExtra(INTENT_EXTRA_FOR_FULL_SCREEN, false));
wangqi9982f0d2017-10-11 17:46:07 -0700315 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800316 }
317
318 // onPause is guaranteed to be called when the InCallActivity goes
319 // in the background.
320 public void onPause() {
321 DialpadFragment dialpadFragment = getDialpadFragment();
322 if (dialpadFragment != null) {
323 dialpadFragment.onDialerKeyUp(null);
324 }
roldenburg006ac372017-11-15 10:59:49 -0800325
326 if (inCallActivity.isFinishing()) {
327 InCallPresenter.getInstance().unsetActivity(inCallActivity);
328 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800329 }
330
331 public void onStop() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700332 // Disconnects call waiting for account when activity is hidden e.g. user press home button.
333 // This is necessary otherwise the pending call will stuck on account choose and no new call
Eric Erfanian938468d2017-10-24 14:05:52 -0700334 // will be able to create. See a bug for more details.
Eric Erfanian2ca43182017-08-31 06:57:16 -0700335 // Skip this on locked screen since the activity may go over life cycle and start again.
336 if (!isRecreating
337 && !inCallActivity.getSystemService(KeyguardManager.class).isKeyguardLocked()) {
338 DialerCall waitingForAccountCall = CallList.getInstance().getWaitingForAccountCall();
339 if (waitingForAccountCall != null) {
340 waitingForAccountCall.disconnect();
341 }
342 }
343
Eric Erfanianccca3152017-02-22 16:32:36 -0800344 enableInCallOrientationEventListener(false);
345 InCallPresenter.getInstance().updateIsChangingConfigurations();
346 InCallPresenter.getInstance().onActivityStopped();
yueg7b28abc2017-09-21 16:08:30 -0700347 if (!isRecreating) {
348 InCallPresenter.getInstance().onUiShowing(false);
linyuhf99f6302017-11-15 11:23:51 -0800349 if (errorDialog != null) {
350 errorDialog.dismiss();
twyen8efb4952017-10-06 16:35:54 -0700351 }
yueg7b28abc2017-09-21 16:08:30 -0700352 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800353 }
354
355 public void onDestroy() {
356 InCallPresenter.getInstance().unsetActivity(inCallActivity);
357 InCallPresenter.getInstance().updateIsChangingConfigurations();
358 }
359
Eric Erfanian10b34a52017-05-04 08:23:17 -0700360 void onNewIntent(Intent intent, boolean isRecreating) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800361 LogUtil.i("InCallActivityCommon.onNewIntent", "");
Eric Erfanian2ca43182017-08-31 06:57:16 -0700362 this.isRecreating = isRecreating;
Eric Erfanianccca3152017-02-22 16:32:36 -0800363
364 // We're being re-launched with a new Intent. Since it's possible for a
365 // single InCallActivity instance to persist indefinitely (even if we
366 // finish() ourselves), this sequence can potentially happen any time
367 // the InCallActivity needs to be displayed.
368
369 // Stash away the new intent so that we can get it in the future
370 // by calling getIntent(). (Otherwise getIntent() will return the
371 // original Intent from when we first got created!)
372 inCallActivity.setIntent(intent);
373
374 // Activities are always paused before receiving a new intent, so
375 // we can count on our onResume() method being called next.
376
377 // Just like in onCreate(), handle the intent.
Eric Erfanian10b34a52017-05-04 08:23:17 -0700378 // Skip if InCallActivity is going to recreate since this will be called in onCreate().
379 if (!isRecreating) {
380 internalResolveIntent(intent);
381 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800382 }
383
384 public boolean onBackPressed(boolean isInCallScreenVisible) {
385 LogUtil.i("InCallActivityCommon.onBackPressed", "");
386
387 // BACK is also used to exit out of any "special modes" of the
388 // in-call UI:
389 if (!inCallActivity.isVisible()) {
390 return true;
391 }
392
393 if (!isInCallScreenVisible) {
394 return true;
395 }
396
397 DialpadFragment dialpadFragment = getDialpadFragment();
398 if (dialpadFragment != null && dialpadFragment.isVisible()) {
399 inCallActivity.showDialpadFragment(false /* show */, true /* animate */);
400 return true;
401 }
402
403 // Always disable the Back key while an incoming call is ringing
404 DialerCall call = CallList.getInstance().getIncomingCall();
405 if (call != null) {
406 LogUtil.i("InCallActivityCommon.onBackPressed", "consume Back press for an incoming call");
407 return true;
408 }
409
410 // Nothing special to do. Fall back to the default behavior.
411 return false;
412 }
413
414 public boolean onKeyUp(int keyCode, KeyEvent event) {
415 DialpadFragment dialpadFragment = getDialpadFragment();
416 // push input to the dialer.
417 if (dialpadFragment != null
418 && (dialpadFragment.isVisible())
419 && (dialpadFragment.onDialerKeyUp(event))) {
420 return true;
421 } else if (keyCode == KeyEvent.KEYCODE_CALL) {
422 // Always consume CALL to be sure the PhoneWindow won't do anything with it
423 return true;
424 }
425 return false;
426 }
427
428 public boolean onKeyDown(int keyCode, KeyEvent event) {
429 switch (keyCode) {
430 case KeyEvent.KEYCODE_CALL:
431 boolean handled = InCallPresenter.getInstance().handleCallKey();
432 if (!handled) {
433 LogUtil.e(
434 "InCallActivityCommon.onKeyDown",
435 "InCallPresenter should always handle KEYCODE_CALL in onKeyDown");
436 }
437 // Always consume CALL to be sure the PhoneWindow won't do anything with it
438 return true;
439
440 // Note there's no KeyEvent.KEYCODE_ENDCALL case here.
441 // The standard system-wide handling of the ENDCALL key
442 // (see PhoneWindowManager's handling of KEYCODE_ENDCALL)
443 // already implements exactly what the UI spec wants,
444 // namely (1) "hang up" if there's a current active call,
445 // or (2) "don't answer" if there's a current ringing call.
446
447 case KeyEvent.KEYCODE_CAMERA:
448 // Disable the CAMERA button while in-call since it's too
449 // easy to press accidentally.
450 return true;
451
452 case KeyEvent.KEYCODE_VOLUME_UP:
453 case KeyEvent.KEYCODE_VOLUME_DOWN:
454 case KeyEvent.KEYCODE_VOLUME_MUTE:
455 // Ringer silencing handled by PhoneWindowManager.
456 break;
457
458 case KeyEvent.KEYCODE_MUTE:
459 TelecomAdapter.getInstance()
460 .mute(!AudioModeProvider.getInstance().getAudioState().isMuted());
461 return true;
462
463 // Various testing/debugging features, enabled ONLY when VERBOSE == true.
464 case KeyEvent.KEYCODE_SLASH:
465 if (LogUtil.isVerboseEnabled()) {
466 LogUtil.v(
467 "InCallActivityCommon.onKeyDown",
468 "----------- InCallActivity View dump --------------");
469 // Dump starting from the top-level view of the entire activity:
470 Window w = inCallActivity.getWindow();
471 View decorView = w.getDecorView();
472 LogUtil.v("InCallActivityCommon.onKeyDown", "View dump:" + decorView);
473 return true;
474 }
475 break;
476 case KeyEvent.KEYCODE_EQUALS:
477 break;
Eric Erfanian10b34a52017-05-04 08:23:17 -0700478 default: // fall out
Eric Erfanianccca3152017-02-22 16:32:36 -0800479 }
480
481 return event.getRepeatCount() == 0 && handleDialerKeyDown(keyCode, event);
482 }
483
linyuh9a915fc2017-11-09 16:27:13 -0800484 private void setWindowFlags() {
485 // Allow the activity to be shown when the screen is locked and filter out touch events that are
486 // "too fat".
487 int flags =
488 WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
489 | WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES;
490
491 // When the audio stream is not directed through Bluetooth, turn the screen on once the
492 // activity is shown.
493 final int audioRoute = getAudioRoute();
494 if (audioRoute != CallAudioState.ROUTE_BLUETOOTH) {
495 flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
496 }
497
498 inCallActivity.getWindow().addFlags(flags);
499 }
500
501 private static int getAudioRoute() {
502 if (audioRouteForTesting.isPresent()) {
503 return audioRouteForTesting.get();
504 }
505
506 return AudioModeProvider.getInstance().getAudioState().getRoute();
507 }
508
509 @VisibleForTesting(otherwise = VisibleForTesting.NONE)
510 public static void setAudioRouteForTesting(int audioRoute) {
511 audioRouteForTesting = Optional.of(audioRoute);
512 }
513
Eric Erfanianccca3152017-02-22 16:32:36 -0800514 private boolean handleDialerKeyDown(int keyCode, KeyEvent event) {
515 LogUtil.v("InCallActivityCommon.handleDialerKeyDown", "keyCode %d, event: %s", keyCode, event);
516
517 // As soon as the user starts typing valid dialable keys on the
518 // keyboard (presumably to type DTMF tones) we start passing the
519 // key events to the DTMFDialer's onDialerKeyDown.
520 DialpadFragment dialpadFragment = getDialpadFragment();
521 if (dialpadFragment != null && dialpadFragment.isVisible()) {
522 return dialpadFragment.onDialerKeyDown(event);
523 }
524
525 return false;
526 }
527
Eric Erfanianccca3152017-02-22 16:32:36 -0800528 public void showPostCharWaitDialog(String callId, String chars) {
529 if (inCallActivity.isVisible()) {
530 PostCharDialogFragment fragment = new PostCharDialogFragment(callId, chars);
531 fragment.show(inCallActivity.getSupportFragmentManager(), "postCharWait");
532
533 showPostCharWaitDialogOnResume = false;
534 showPostCharWaitDialogCallId = null;
535 showPostCharWaitDialogChars = null;
536 } else {
537 showPostCharWaitDialogOnResume = true;
538 showPostCharWaitDialogCallId = callId;
539 showPostCharWaitDialogChars = chars;
540 }
541 }
542
Eric Erfanian2ca43182017-08-31 06:57:16 -0700543 public void maybeShowErrorDialogOnDisconnect(DisconnectMessage disconnectMessage) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800544 LogUtil.i(
Eric Erfanian2ca43182017-08-31 06:57:16 -0700545 "InCallActivityCommon.maybeShowErrorDialogOnDisconnect",
546 "disconnect cause: %s",
547 disconnectMessage);
Eric Erfanianccca3152017-02-22 16:32:36 -0800548
549 if (!inCallActivity.isFinishing()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700550 if (disconnectMessage.dialog != null) {
551 showErrorDialog(disconnectMessage.dialog, disconnectMessage.toastMessage);
Eric Erfanianccca3152017-02-22 16:32:36 -0800552 }
553 }
554 }
555
556 /**
557 * When relaunching from the dialer app, {@code showDialpad} indicates whether the dialpad should
558 * be shown on launch.
559 *
560 * @param showDialpad {@code true} to indicate the dialpad should be shown on launch, and {@code
561 * false} to indicate no change should be made to the dialpad visibility.
562 */
563 private void relaunchedFromDialer(boolean showDialpad) {
564 showDialpadRequest = showDialpad ? DIALPAD_REQUEST_SHOW : DIALPAD_REQUEST_NONE;
565 animateDialpadOnShow = true;
566
567 if (showDialpadRequest == DIALPAD_REQUEST_SHOW) {
568 // If there's only one line in use, AND it's on hold, then we're sure the user
569 // wants to use the dialpad toward the exact line, so un-hold the holding line.
570 DialerCall call = CallList.getInstance().getActiveOrBackgroundCall();
571 if (call != null && call.getState() == State.ONHOLD) {
572 call.unhold();
573 }
574 }
575 }
576
Eric Erfanianccca3152017-02-22 16:32:36 -0800577 private void showErrorDialog(Dialog dialog, CharSequence message) {
578 LogUtil.i("InCallActivityCommon.showErrorDialog", "message: %s", message);
579 inCallActivity.dismissPendingDialogs();
580
581 // Show toast if apps is in background when dialog won't be visible.
582 if (!inCallActivity.isVisible()) {
583 Toast.makeText(inCallActivity.getApplicationContext(), message, Toast.LENGTH_LONG).show();
584 return;
585 }
586
linyuhf99f6302017-11-15 11:23:51 -0800587 this.errorDialog = dialog;
twyen8efb4952017-10-06 16:35:54 -0700588 InCallUiLock lock = InCallPresenter.getInstance().acquireInCallUiLock("showErrorDialog");
Eric Erfanianccca3152017-02-22 16:32:36 -0800589 dialog.setOnDismissListener(
590 new OnDismissListener() {
591 @Override
592 public void onDismiss(DialogInterface dialog) {
593 LogUtil.i("InCallActivityCommon.showErrorDialog", "dialog dismissed");
twyen8efb4952017-10-06 16:35:54 -0700594 lock.release();
Eric Erfanianccca3152017-02-22 16:32:36 -0800595 onDialogDismissed();
596 }
597 });
598 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
599 dialog.show();
600 }
601
602 private void onDialogDismissed() {
linyuhf99f6302017-11-15 11:23:51 -0800603 errorDialog = null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800604 CallList.getInstance().onErrorDialogDismissed();
Eric Erfanianccca3152017-02-22 16:32:36 -0800605 }
606
607 public void enableInCallOrientationEventListener(boolean enable) {
608 if (enable) {
609 inCallOrientationEventListener.enable(true);
610 } else {
611 inCallOrientationEventListener.disable();
612 }
613 }
614
615 public void setExcludeFromRecents(boolean exclude) {
616 List<AppTask> tasks = inCallActivity.getSystemService(ActivityManager.class).getAppTasks();
617 int taskId = inCallActivity.getTaskId();
618 for (int i = 0; i < tasks.size(); i++) {
619 ActivityManager.AppTask task = tasks.get(i);
620 try {
621 if (task.getTaskInfo().id == taskId) {
622 task.setExcludeFromRecents(exclude);
623 }
624 } catch (RuntimeException e) {
625 LogUtil.e(
626 "InCallActivityCommon.setExcludeFromRecents",
627 "RuntimeException when excluding task from recents.",
628 e);
629 }
630 }
631 }
632
Eric Erfanianc857f902017-05-15 14:05:33 -0700633 void showInternationalCallOnWifiDialog(@NonNull DialerCall call) {
634 LogUtil.enterBlock("InCallActivityCommon.showInternationalCallOnWifiDialog");
635 if (!InternationalCallOnWifiDialogFragment.shouldShow(inCallActivity)) {
636 LogUtil.i(
637 "InCallActivityCommon.showInternationalCallOnWifiDialog",
638 "InternationalCallOnWifiDialogFragment.shouldShow returned false");
639 return;
640 }
641
642 InternationalCallOnWifiDialogFragment fragment =
643 InternationalCallOnWifiDialogFragment.newInstance(
644 call.getId(), internationalCallOnWifiCallback);
645 fragment.show(inCallActivity.getSupportFragmentManager(), TAG_INTERNATIONAL_CALL_ON_WIFI);
646 }
647
Eric Erfanianccca3152017-02-22 16:32:36 -0800648 public void showWifiToLteHandoverToast(DialerCall call) {
649 if (call.hasShownWiFiToLteHandoverToast()) {
650 return;
651 }
652 Toast.makeText(
653 inCallActivity, R.string.video_call_wifi_to_lte_handover_toast, Toast.LENGTH_LONG)
654 .show();
655 call.setHasShownWiFiToLteHandoverToast();
656 }
657
658 public void showWifiFailedDialog(final DialerCall call) {
659 if (call.showWifiHandoverAlertAsToast()) {
660 LogUtil.i("InCallActivityCommon.showWifiFailedDialog", "as toast");
661 Toast.makeText(
662 inCallActivity, R.string.video_call_lte_to_wifi_failed_message, Toast.LENGTH_SHORT)
663 .show();
664 return;
665 }
666
linyuhf99f6302017-11-15 11:23:51 -0800667 inCallActivity.dismissPendingDialogs();
Eric Erfanianccca3152017-02-22 16:32:36 -0800668
669 AlertDialog.Builder builder =
670 new AlertDialog.Builder(inCallActivity)
671 .setTitle(R.string.video_call_lte_to_wifi_failed_title);
672
673 // This allows us to use the theme of the dialog instead of the activity
674 View dialogCheckBoxView =
675 View.inflate(builder.getContext(), R.layout.video_call_lte_to_wifi_failed, null);
676 final CheckBox wifiHandoverFailureCheckbox =
677 (CheckBox) dialogCheckBoxView.findViewById(R.id.video_call_lte_to_wifi_failed_checkbox);
678 wifiHandoverFailureCheckbox.setChecked(false);
679
twyen8efb4952017-10-06 16:35:54 -0700680 InCallUiLock lock = InCallPresenter.getInstance().acquireInCallUiLock("WifiFailedDialog");
linyuhf99f6302017-11-15 11:23:51 -0800681 errorDialog =
Eric Erfanianccca3152017-02-22 16:32:36 -0800682 builder
683 .setView(dialogCheckBoxView)
684 .setMessage(R.string.video_call_lte_to_wifi_failed_message)
685 .setOnCancelListener(
686 new OnCancelListener() {
687 @Override
688 public void onCancel(DialogInterface dialog) {
689 onDialogDismissed();
690 }
691 })
692 .setPositiveButton(
693 android.R.string.ok,
694 new DialogInterface.OnClickListener() {
695 @Override
696 public void onClick(DialogInterface dialog, int id) {
697 call.setDoNotShowDialogForHandoffToWifiFailure(
698 wifiHandoverFailureCheckbox.isChecked());
699 dialog.cancel();
700 onDialogDismissed();
701 }
702 })
twyen8efb4952017-10-06 16:35:54 -0700703 .setOnDismissListener((dialog) -> lock.release())
Eric Erfanianccca3152017-02-22 16:32:36 -0800704 .create();
705
706 LogUtil.i("InCallActivityCommon.showWifiFailedDialog", "as dialog");
linyuhf99f6302017-11-15 11:23:51 -0800707 errorDialog.show();
Eric Erfanianccca3152017-02-22 16:32:36 -0800708 }
709
Eric Erfanian938468d2017-10-24 14:05:52 -0700710 void updateNavigationBar(boolean isDialpadVisible) {
wangqifad3d872017-10-25 13:15:23 -0700711 if (!ActivityCompat.isInMultiWindowMode(inCallActivity)) {
Eric Erfanian938468d2017-10-24 14:05:52 -0700712 View navigationBarBackground =
713 inCallActivity.getWindow().findViewById(R.id.navigation_bar_background);
714 if (navigationBarBackground != null) {
715 navigationBarBackground.setVisibility(isDialpadVisible ? View.VISIBLE : View.GONE);
716 }
717 }
718 }
719
Eric Erfanianccca3152017-02-22 16:32:36 -0800720 public boolean showDialpadFragment(boolean show, boolean animate) {
721 // If the dialpad is already visible, don't animate in. If it's gone, don't animate out.
722 boolean isDialpadVisible = isDialpadVisible();
723 LogUtil.i(
724 "InCallActivityCommon.showDialpadFragment",
725 "show: %b, animate: %b, " + "isDialpadVisible: %b",
726 show,
727 animate,
728 isDialpadVisible);
729 if (show == isDialpadVisible) {
730 return false;
731 }
732
733 FragmentManager dialpadFragmentManager = inCallActivity.getDialpadFragmentManager();
734 if (dialpadFragmentManager == null) {
735 LogUtil.i(
736 "InCallActivityCommon.showDialpadFragment", "unable to show or hide dialpad fragment");
737 return false;
738 }
739
740 // We don't do a FragmentTransaction on the hide case because it will be dealt with when
741 // the listener is fired after an animation finishes.
742 if (!animate) {
743 if (show) {
744 performShowDialpadFragment(dialpadFragmentManager);
745 } else {
746 performHideDialpadFragment();
747 }
748 } else {
749 if (show) {
750 performShowDialpadFragment(dialpadFragmentManager);
751 getDialpadFragment().animateShowDialpad();
752 }
753 getDialpadFragment()
754 .getView()
755 .startAnimation(show ? dialpadSlideInAnimation : dialpadSlideOutAnimation);
756 }
757
758 ProximitySensor sensor = InCallPresenter.getInstance().getProximitySensor();
759 if (sensor != null) {
760 sensor.onDialpadVisible(show);
761 }
762 showDialpadRequest = DIALPAD_REQUEST_NONE;
763 return true;
764 }
765
766 private void performShowDialpadFragment(@NonNull FragmentManager dialpadFragmentManager) {
767 FragmentTransaction transaction = dialpadFragmentManager.beginTransaction();
768 DialpadFragment dialpadFragment = getDialpadFragment();
769 if (dialpadFragment == null) {
770 transaction.add(
771 inCallActivity.getDialpadContainerId(), new DialpadFragment(), TAG_DIALPAD_FRAGMENT);
772 } else {
773 transaction.show(dialpadFragment);
774 }
775
776 transaction.commitAllowingStateLoss();
777 dialpadFragmentManager.executePendingTransactions();
778
779 Logger.get(inCallActivity).logScreenView(ScreenEvent.Type.INCALL_DIALPAD, inCallActivity);
Eric Erfanian938468d2017-10-24 14:05:52 -0700780 updateNavigationBar(true /* isDialpadVisible */);
Eric Erfanianccca3152017-02-22 16:32:36 -0800781 }
782
783 private void performHideDialpadFragment() {
784 FragmentManager fragmentManager = inCallActivity.getDialpadFragmentManager();
785 if (fragmentManager == null) {
786 LogUtil.e(
787 "InCallActivityCommon.performHideDialpadFragment", "child fragment manager is null");
788 return;
789 }
790
791 Fragment fragment = fragmentManager.findFragmentByTag(TAG_DIALPAD_FRAGMENT);
792 if (fragment != null) {
793 FragmentTransaction transaction = fragmentManager.beginTransaction();
794 transaction.hide(fragment);
795 transaction.commitAllowingStateLoss();
796 fragmentManager.executePendingTransactions();
797 }
Eric Erfanian938468d2017-10-24 14:05:52 -0700798 updateNavigationBar(false /* isDialpadVisible */);
Eric Erfanianccca3152017-02-22 16:32:36 -0800799 }
800
801 public boolean isDialpadVisible() {
802 DialpadFragment dialpadFragment = getDialpadFragment();
803 return dialpadFragment != null && dialpadFragment.isVisible();
804 }
805
806 /** Returns the {@link DialpadFragment} that's shown by this activity, or {@code null} */
807 @Nullable
808 private DialpadFragment getDialpadFragment() {
809 FragmentManager fragmentManager = inCallActivity.getDialpadFragmentManager();
810 if (fragmentManager == null) {
811 return null;
812 }
813 return (DialpadFragment) fragmentManager.findFragmentByTag(TAG_DIALPAD_FRAGMENT);
814 }
815
816 public void updateTaskDescription() {
817 Resources resources = inCallActivity.getResources();
818 int color;
819 if (resources.getBoolean(R.bool.is_layout_landscape)) {
820 color =
821 ResourcesCompat.getColor(
822 resources, R.color.statusbar_background_color, inCallActivity.getTheme());
823 } else {
824 color = InCallPresenter.getInstance().getThemeColorManager().getSecondaryColor();
825 }
826
827 TaskDescription td =
828 new TaskDescription(resources.getString(R.string.notification_ongoing_call), null, color);
829 inCallActivity.setTaskDescription(td);
830 }
831
Eric Erfanianccca3152017-02-22 16:32:36 -0800832 private void internalResolveIntent(Intent intent) {
833 if (!intent.getAction().equals(Intent.ACTION_MAIN)) {
834 return;
835 }
836
837 if (intent.hasExtra(INTENT_EXTRA_SHOW_DIALPAD)) {
838 // SHOW_DIALPAD_EXTRA can be used here to specify whether the DTMF
839 // dialpad should be initially visible. If the extra isn't
840 // present at all, we just leave the dialpad in its previous state.
841 boolean showDialpad = intent.getBooleanExtra(INTENT_EXTRA_SHOW_DIALPAD, false);
842 LogUtil.i("InCallActivityCommon.internalResolveIntent", "SHOW_DIALPAD_EXTRA: " + showDialpad);
843
844 relaunchedFromDialer(showDialpad);
845 }
846
847 DialerCall outgoingCall = CallList.getInstance().getOutgoingCall();
848 if (outgoingCall == null) {
849 outgoingCall = CallList.getInstance().getPendingOutgoingCall();
850 }
851
Eric Erfanianccca3152017-02-22 16:32:36 -0800852 if (intent.getBooleanExtra(INTENT_EXTRA_NEW_OUTGOING_CALL, false)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800853 intent.removeExtra(INTENT_EXTRA_NEW_OUTGOING_CALL);
854
855 // InCallActivity is responsible for disconnecting a new outgoing call if there
856 // is no way of making it (i.e. no valid call capable accounts).
857 // If the version is not MSIM compatible, then ignore this code.
858 if (CompatUtils.isMSIMCompatible()
859 && InCallPresenter.isCallWithNoValidAccounts(outgoingCall)) {
860 LogUtil.i(
861 "InCallActivityCommon.internalResolveIntent",
862 "call with no valid accounts, disconnecting");
863 outgoingCall.disconnect();
864 }
865
linyuh9c327da2017-11-14 12:33:48 -0800866 inCallActivity.dismissKeyguard(true);
Eric Erfanianccca3152017-02-22 16:32:36 -0800867 }
868
869 boolean didShowAccountSelectionDialog = maybeShowAccountSelectionDialog();
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700870 if (didShowAccountSelectionDialog) {
871 inCallActivity.hideMainInCallFragment();
872 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800873 }
874
875 private boolean maybeShowAccountSelectionDialog() {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700876 DialerCall waitingForAccountCall = CallList.getInstance().getWaitingForAccountCall();
877 if (waitingForAccountCall == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800878 return false;
879 }
880
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700881 Bundle extras = waitingForAccountCall.getIntentExtras();
Eric Erfanianccca3152017-02-22 16:32:36 -0800882 List<PhoneAccountHandle> phoneAccountHandles;
883 if (extras != null) {
884 phoneAccountHandles =
885 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
886 } else {
887 phoneAccountHandles = new ArrayList<>();
888 }
889
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700890 selectPhoneAccountDialogFragment =
Eric Erfanianccca3152017-02-22 16:32:36 -0800891 SelectPhoneAccountDialogFragment.newInstance(
892 R.string.select_phone_account_for_calls,
893 true,
894 phoneAccountHandles,
895 selectAccountListener,
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700896 waitingForAccountCall.getId());
897 selectPhoneAccountDialogFragment.show(
898 inCallActivity.getFragmentManager(), TAG_SELECT_ACCOUNT_FRAGMENT);
Eric Erfanianccca3152017-02-22 16:32:36 -0800899 return true;
900 }
linyuhf99f6302017-11-15 11:23:51 -0800901
902 /** @deprecated Only for temporary use during the deprecation of {@link InCallActivityCommon} */
903 @Deprecated
904 @Nullable
905 Dialog getErrorDialog() {
906 return errorDialog;
907 }
908
909 /** @deprecated Only for temporary use during the deprecation of {@link InCallActivityCommon} */
910 @Deprecated
911 void setErrorDialog(@Nullable Dialog errorDialog) {
912 this.errorDialog = errorDialog;
913 }
914
915 /** @deprecated Only for temporary use during the deprecation of {@link InCallActivityCommon} */
916 @Deprecated
917 @Nullable
918 SelectPhoneAccountDialogFragment getSelectPhoneAccountDialogFragment() {
919 return selectPhoneAccountDialogFragment;
920 }
921
922 /** @deprecated Only for temporary use during the deprecation of {@link InCallActivityCommon} */
923 @Deprecated
924 void setSelectPhoneAccountDialogFragment(
925 @Nullable SelectPhoneAccountDialogFragment selectPhoneAccountDialogFragment) {
926 this.selectPhoneAccountDialogFragment = selectPhoneAccountDialogFragment;
927 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800928}