Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | package com.android.incallui; |
| 18 | |
| 19 | import android.app.ActivityManager; |
| 20 | import android.app.ActivityManager.AppTask; |
| 21 | import android.app.ActivityManager.TaskDescription; |
| 22 | import android.app.AlertDialog; |
| 23 | import android.app.Dialog; |
| 24 | import android.app.DialogFragment; |
| 25 | import android.content.Context; |
| 26 | import android.content.DialogInterface; |
| 27 | import android.content.DialogInterface.OnCancelListener; |
| 28 | import android.content.DialogInterface.OnDismissListener; |
| 29 | import android.content.Intent; |
| 30 | import android.content.res.Configuration; |
| 31 | import android.content.res.Resources; |
| 32 | import android.os.Bundle; |
| 33 | import android.support.annotation.IntDef; |
| 34 | import android.support.annotation.NonNull; |
| 35 | import android.support.annotation.Nullable; |
| 36 | import android.support.v4.app.Fragment; |
| 37 | import android.support.v4.app.FragmentManager; |
| 38 | import android.support.v4.app.FragmentTransaction; |
| 39 | import android.support.v4.content.res.ResourcesCompat; |
| 40 | import android.telecom.DisconnectCause; |
| 41 | import android.telecom.PhoneAccountHandle; |
| 42 | import android.text.TextUtils; |
| 43 | import android.util.Pair; |
| 44 | import android.view.KeyEvent; |
| 45 | import android.view.View; |
| 46 | import android.view.Window; |
| 47 | import android.view.WindowManager; |
| 48 | import android.view.animation.Animation; |
| 49 | import android.view.animation.AnimationUtils; |
| 50 | import android.widget.CheckBox; |
| 51 | import android.widget.Toast; |
| 52 | import com.android.contacts.common.widget.SelectPhoneAccountDialogFragment; |
| 53 | import com.android.contacts.common.widget.SelectPhoneAccountDialogFragment.SelectPhoneAccountListener; |
| 54 | import com.android.dialer.animation.AnimUtils; |
| 55 | import com.android.dialer.animation.AnimationListenerAdapter; |
| 56 | import com.android.dialer.common.LogUtil; |
| 57 | import com.android.dialer.compat.CompatUtils; |
| 58 | import com.android.dialer.logging.Logger; |
| 59 | import com.android.dialer.logging.nano.ScreenEvent; |
| 60 | import com.android.dialer.util.ViewUtil; |
| 61 | import com.android.incallui.call.CallList; |
| 62 | import com.android.incallui.call.DialerCall; |
| 63 | import com.android.incallui.call.DialerCall.State; |
| 64 | import com.android.incallui.call.TelecomAdapter; |
| 65 | import com.android.incallui.wifi.EnableWifiCallingPrompt; |
| 66 | import java.lang.annotation.Retention; |
| 67 | import java.lang.annotation.RetentionPolicy; |
| 68 | import java.util.ArrayList; |
| 69 | import java.util.List; |
| 70 | |
| 71 | /** Shared functionality between the new and old in call activity. */ |
| 72 | public class InCallActivityCommon { |
| 73 | |
| 74 | private static final String INTENT_EXTRA_SHOW_DIALPAD = "InCallActivity.show_dialpad"; |
| 75 | private static final String INTENT_EXTRA_NEW_OUTGOING_CALL = "InCallActivity.new_outgoing_call"; |
| 76 | private static final String INTENT_EXTRA_FOR_FULL_SCREEN = |
| 77 | "InCallActivity.for_full_screen_intent"; |
| 78 | |
| 79 | private static final String DIALPAD_TEXT_KEY = "InCallActivity.dialpad_text"; |
| 80 | |
| 81 | private static final String TAG_SELECT_ACCOUNT_FRAGMENT = "tag_select_account_fragment"; |
| 82 | private static final String TAG_DIALPAD_FRAGMENT = "tag_dialpad_fragment"; |
| 83 | |
| 84 | @Retention(RetentionPolicy.SOURCE) |
| 85 | @IntDef({ |
| 86 | DIALPAD_REQUEST_NONE, |
| 87 | DIALPAD_REQUEST_SHOW, |
| 88 | DIALPAD_REQUEST_HIDE, |
| 89 | }) |
| 90 | @interface DialpadRequestType {} |
| 91 | |
| 92 | private static final int DIALPAD_REQUEST_NONE = 1; |
| 93 | private static final int DIALPAD_REQUEST_SHOW = 2; |
| 94 | private static final int DIALPAD_REQUEST_HIDE = 3; |
| 95 | |
| 96 | private final InCallActivity inCallActivity; |
| 97 | private boolean dismissKeyguard; |
| 98 | private boolean showPostCharWaitDialogOnResume; |
| 99 | private String showPostCharWaitDialogCallId; |
| 100 | private String showPostCharWaitDialogChars; |
| 101 | private Dialog dialog; |
| 102 | private InCallOrientationEventListener inCallOrientationEventListener; |
| 103 | private Animation dialpadSlideInAnimation; |
| 104 | private Animation dialpadSlideOutAnimation; |
| 105 | private boolean animateDialpadOnShow; |
| 106 | private String dtmfTextToPreopulate; |
| 107 | @DialpadRequestType private int showDialpadRequest = DIALPAD_REQUEST_NONE; |
| 108 | |
| 109 | private SelectPhoneAccountListener selectAccountListener = |
| 110 | new SelectPhoneAccountListener() { |
| 111 | @Override |
| 112 | public void onPhoneAccountSelected( |
| 113 | PhoneAccountHandle selectedAccountHandle, boolean setDefault, String callId) { |
| 114 | DialerCall call = CallList.getInstance().getCallById(callId); |
| 115 | LogUtil.i( |
| 116 | "InCallActivityCommon.SelectPhoneAccountListener.onPhoneAccountSelected", |
| 117 | "call: " + call); |
| 118 | if (call != null) { |
| 119 | call.phoneAccountSelected(selectedAccountHandle, setDefault); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void onDialogDismissed(String callId) { |
| 125 | DialerCall call = CallList.getInstance().getCallById(callId); |
| 126 | LogUtil.i( |
| 127 | "InCallActivityCommon.SelectPhoneAccountListener.onDialogDismissed", |
| 128 | "disconnecting call: " + call); |
| 129 | if (call != null) { |
| 130 | call.disconnect(); |
| 131 | } |
| 132 | } |
| 133 | }; |
| 134 | |
| 135 | public static void setIntentExtras( |
| 136 | Intent intent, boolean showDialpad, boolean newOutgoingCall, boolean isForFullScreen) { |
| 137 | if (showDialpad) { |
| 138 | intent.putExtra(INTENT_EXTRA_SHOW_DIALPAD, true); |
| 139 | } |
| 140 | intent.putExtra(INTENT_EXTRA_NEW_OUTGOING_CALL, newOutgoingCall); |
| 141 | intent.putExtra(INTENT_EXTRA_FOR_FULL_SCREEN, isForFullScreen); |
| 142 | } |
| 143 | |
| 144 | public InCallActivityCommon(InCallActivity inCallActivity) { |
| 145 | this.inCallActivity = inCallActivity; |
| 146 | } |
| 147 | |
| 148 | public void onCreate(Bundle icicle) { |
| 149 | // set this flag so this activity will stay in front of the keyguard |
| 150 | // Have the WindowManager filter out touch events that are "too fat". |
| 151 | int flags = |
| 152 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
| 153 | | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
| 154 | | WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES; |
| 155 | |
| 156 | inCallActivity.getWindow().addFlags(flags); |
| 157 | |
| 158 | inCallActivity.setContentView(R.layout.incall_screen); |
| 159 | |
| 160 | internalResolveIntent(inCallActivity.getIntent()); |
| 161 | |
| 162 | boolean isLandscape = |
| 163 | inCallActivity.getResources().getConfiguration().orientation |
| 164 | == Configuration.ORIENTATION_LANDSCAPE; |
| 165 | boolean isRtl = ViewUtil.isRtl(); |
| 166 | |
| 167 | if (isLandscape) { |
| 168 | dialpadSlideInAnimation = |
| 169 | AnimationUtils.loadAnimation( |
| 170 | inCallActivity, isRtl ? R.anim.dialpad_slide_in_left : R.anim.dialpad_slide_in_right); |
| 171 | dialpadSlideOutAnimation = |
| 172 | AnimationUtils.loadAnimation( |
| 173 | inCallActivity, |
| 174 | isRtl ? R.anim.dialpad_slide_out_left : R.anim.dialpad_slide_out_right); |
| 175 | } else { |
| 176 | dialpadSlideInAnimation = |
| 177 | AnimationUtils.loadAnimation(inCallActivity, R.anim.dialpad_slide_in_bottom); |
| 178 | dialpadSlideOutAnimation = |
| 179 | AnimationUtils.loadAnimation(inCallActivity, R.anim.dialpad_slide_out_bottom); |
| 180 | } |
| 181 | |
| 182 | dialpadSlideInAnimation.setInterpolator(AnimUtils.EASE_IN); |
| 183 | dialpadSlideOutAnimation.setInterpolator(AnimUtils.EASE_OUT); |
| 184 | |
| 185 | dialpadSlideOutAnimation.setAnimationListener( |
| 186 | new AnimationListenerAdapter() { |
| 187 | @Override |
| 188 | public void onAnimationEnd(Animation animation) { |
| 189 | performHideDialpadFragment(); |
| 190 | } |
| 191 | }); |
| 192 | |
| 193 | if (icicle != null) { |
| 194 | // If the dialpad was shown before, set variables indicating it should be shown and |
| 195 | // populated with the previous DTMF text. The dialpad is actually shown and populated |
| 196 | // in onResume() to ensure the hosting fragment has been inflated and is ready to receive it. |
| 197 | if (icicle.containsKey(INTENT_EXTRA_SHOW_DIALPAD)) { |
| 198 | boolean showDialpad = icicle.getBoolean(INTENT_EXTRA_SHOW_DIALPAD); |
| 199 | showDialpadRequest = showDialpad ? DIALPAD_REQUEST_SHOW : DIALPAD_REQUEST_HIDE; |
| 200 | animateDialpadOnShow = false; |
| 201 | } |
| 202 | dtmfTextToPreopulate = icicle.getString(DIALPAD_TEXT_KEY); |
| 203 | |
| 204 | SelectPhoneAccountDialogFragment dialogFragment = |
| 205 | (SelectPhoneAccountDialogFragment) |
| 206 | inCallActivity.getFragmentManager().findFragmentByTag(TAG_SELECT_ACCOUNT_FRAGMENT); |
| 207 | if (dialogFragment != null) { |
| 208 | dialogFragment.setListener(selectAccountListener); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | inCallOrientationEventListener = new InCallOrientationEventListener(inCallActivity); |
| 213 | } |
| 214 | |
| 215 | public void onSaveInstanceState(Bundle out) { |
| 216 | // TODO: The dialpad fragment should handle this as part of its own state |
| 217 | out.putBoolean(INTENT_EXTRA_SHOW_DIALPAD, isDialpadVisible()); |
| 218 | DialpadFragment dialpadFragment = getDialpadFragment(); |
| 219 | if (dialpadFragment != null) { |
| 220 | out.putString(DIALPAD_TEXT_KEY, dialpadFragment.getDtmfText()); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | public void onStart() { |
| 225 | // setting activity should be last thing in setup process |
| 226 | InCallPresenter.getInstance().setActivity(inCallActivity); |
| 227 | enableInCallOrientationEventListener( |
| 228 | inCallActivity.getRequestedOrientation() |
| 229 | == InCallOrientationEventListener.ACTIVITY_PREFERENCE_ALLOW_ROTATION); |
| 230 | |
| 231 | InCallPresenter.getInstance().onActivityStarted(); |
| 232 | } |
| 233 | |
| 234 | public void onResume() { |
| 235 | if (InCallPresenter.getInstance().isReadyForTearDown()) { |
| 236 | LogUtil.i( |
| 237 | "InCallActivityCommon.onResume", |
| 238 | "InCallPresenter is ready for tear down, not sending updates"); |
| 239 | } else { |
| 240 | updateTaskDescription(); |
| 241 | InCallPresenter.getInstance().onUiShowing(true); |
| 242 | } |
| 243 | |
| 244 | // If there is a pending request to show or hide the dialpad, handle that now. |
| 245 | if (showDialpadRequest != DIALPAD_REQUEST_NONE) { |
| 246 | if (showDialpadRequest == DIALPAD_REQUEST_SHOW) { |
| 247 | // Exit fullscreen so that the user has access to the dialpad hide/show button and |
| 248 | // can hide the dialpad. Important when showing the dialpad from within dialer. |
| 249 | InCallPresenter.getInstance().setFullScreen(false, true /* force */); |
| 250 | |
| 251 | inCallActivity.showDialpadFragment(true /* show */, animateDialpadOnShow /* animate */); |
| 252 | animateDialpadOnShow = false; |
| 253 | |
| 254 | DialpadFragment dialpadFragment = getDialpadFragment(); |
| 255 | if (dialpadFragment != null) { |
| 256 | dialpadFragment.setDtmfText(dtmfTextToPreopulate); |
| 257 | dtmfTextToPreopulate = null; |
| 258 | } |
| 259 | } else { |
| 260 | LogUtil.i("InCallActivityCommon.onResume", "force hide dialpad"); |
| 261 | if (getDialpadFragment() != null) { |
| 262 | inCallActivity.showDialpadFragment(false /* show */, false /* animate */); |
| 263 | } |
| 264 | } |
| 265 | showDialpadRequest = DIALPAD_REQUEST_NONE; |
| 266 | } |
| 267 | |
| 268 | if (showPostCharWaitDialogOnResume) { |
| 269 | showPostCharWaitDialog(showPostCharWaitDialogCallId, showPostCharWaitDialogChars); |
| 270 | } |
| 271 | |
| 272 | CallList.getInstance() |
| 273 | .onInCallUiShown( |
| 274 | inCallActivity.getIntent().getBooleanExtra(INTENT_EXTRA_FOR_FULL_SCREEN, false)); |
| 275 | } |
| 276 | |
| 277 | // onPause is guaranteed to be called when the InCallActivity goes |
| 278 | // in the background. |
| 279 | public void onPause() { |
| 280 | DialpadFragment dialpadFragment = getDialpadFragment(); |
| 281 | if (dialpadFragment != null) { |
| 282 | dialpadFragment.onDialerKeyUp(null); |
| 283 | } |
| 284 | |
| 285 | InCallPresenter.getInstance().onUiShowing(false); |
| 286 | if (inCallActivity.isFinishing()) { |
| 287 | InCallPresenter.getInstance().unsetActivity(inCallActivity); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | public void onStop() { |
| 292 | enableInCallOrientationEventListener(false); |
| 293 | InCallPresenter.getInstance().updateIsChangingConfigurations(); |
| 294 | InCallPresenter.getInstance().onActivityStopped(); |
| 295 | } |
| 296 | |
| 297 | public void onDestroy() { |
| 298 | InCallPresenter.getInstance().unsetActivity(inCallActivity); |
| 299 | InCallPresenter.getInstance().updateIsChangingConfigurations(); |
| 300 | } |
| 301 | |
| 302 | public void onNewIntent(Intent intent) { |
| 303 | LogUtil.i("InCallActivityCommon.onNewIntent", ""); |
| 304 | |
| 305 | // We're being re-launched with a new Intent. Since it's possible for a |
| 306 | // single InCallActivity instance to persist indefinitely (even if we |
| 307 | // finish() ourselves), this sequence can potentially happen any time |
| 308 | // the InCallActivity needs to be displayed. |
| 309 | |
| 310 | // Stash away the new intent so that we can get it in the future |
| 311 | // by calling getIntent(). (Otherwise getIntent() will return the |
| 312 | // original Intent from when we first got created!) |
| 313 | inCallActivity.setIntent(intent); |
| 314 | |
| 315 | // Activities are always paused before receiving a new intent, so |
| 316 | // we can count on our onResume() method being called next. |
| 317 | |
| 318 | // Just like in onCreate(), handle the intent. |
| 319 | internalResolveIntent(intent); |
| 320 | } |
| 321 | |
| 322 | public boolean onBackPressed(boolean isInCallScreenVisible) { |
| 323 | LogUtil.i("InCallActivityCommon.onBackPressed", ""); |
| 324 | |
| 325 | // BACK is also used to exit out of any "special modes" of the |
| 326 | // in-call UI: |
| 327 | if (!inCallActivity.isVisible()) { |
| 328 | return true; |
| 329 | } |
| 330 | |
| 331 | if (!isInCallScreenVisible) { |
| 332 | return true; |
| 333 | } |
| 334 | |
| 335 | DialpadFragment dialpadFragment = getDialpadFragment(); |
| 336 | if (dialpadFragment != null && dialpadFragment.isVisible()) { |
| 337 | inCallActivity.showDialpadFragment(false /* show */, true /* animate */); |
| 338 | return true; |
| 339 | } |
| 340 | |
| 341 | // Always disable the Back key while an incoming call is ringing |
| 342 | DialerCall call = CallList.getInstance().getIncomingCall(); |
| 343 | if (call != null) { |
| 344 | LogUtil.i("InCallActivityCommon.onBackPressed", "consume Back press for an incoming call"); |
| 345 | return true; |
| 346 | } |
| 347 | |
| 348 | // Nothing special to do. Fall back to the default behavior. |
| 349 | return false; |
| 350 | } |
| 351 | |
| 352 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 353 | DialpadFragment dialpadFragment = getDialpadFragment(); |
| 354 | // push input to the dialer. |
| 355 | if (dialpadFragment != null |
| 356 | && (dialpadFragment.isVisible()) |
| 357 | && (dialpadFragment.onDialerKeyUp(event))) { |
| 358 | return true; |
| 359 | } else if (keyCode == KeyEvent.KEYCODE_CALL) { |
| 360 | // Always consume CALL to be sure the PhoneWindow won't do anything with it |
| 361 | return true; |
| 362 | } |
| 363 | return false; |
| 364 | } |
| 365 | |
| 366 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 367 | switch (keyCode) { |
| 368 | case KeyEvent.KEYCODE_CALL: |
| 369 | boolean handled = InCallPresenter.getInstance().handleCallKey(); |
| 370 | if (!handled) { |
| 371 | LogUtil.e( |
| 372 | "InCallActivityCommon.onKeyDown", |
| 373 | "InCallPresenter should always handle KEYCODE_CALL in onKeyDown"); |
| 374 | } |
| 375 | // Always consume CALL to be sure the PhoneWindow won't do anything with it |
| 376 | return true; |
| 377 | |
| 378 | // Note there's no KeyEvent.KEYCODE_ENDCALL case here. |
| 379 | // The standard system-wide handling of the ENDCALL key |
| 380 | // (see PhoneWindowManager's handling of KEYCODE_ENDCALL) |
| 381 | // already implements exactly what the UI spec wants, |
| 382 | // namely (1) "hang up" if there's a current active call, |
| 383 | // or (2) "don't answer" if there's a current ringing call. |
| 384 | |
| 385 | case KeyEvent.KEYCODE_CAMERA: |
| 386 | // Disable the CAMERA button while in-call since it's too |
| 387 | // easy to press accidentally. |
| 388 | return true; |
| 389 | |
| 390 | case KeyEvent.KEYCODE_VOLUME_UP: |
| 391 | case KeyEvent.KEYCODE_VOLUME_DOWN: |
| 392 | case KeyEvent.KEYCODE_VOLUME_MUTE: |
| 393 | // Ringer silencing handled by PhoneWindowManager. |
| 394 | break; |
| 395 | |
| 396 | case KeyEvent.KEYCODE_MUTE: |
| 397 | TelecomAdapter.getInstance() |
| 398 | .mute(!AudioModeProvider.getInstance().getAudioState().isMuted()); |
| 399 | return true; |
| 400 | |
| 401 | // Various testing/debugging features, enabled ONLY when VERBOSE == true. |
| 402 | case KeyEvent.KEYCODE_SLASH: |
| 403 | if (LogUtil.isVerboseEnabled()) { |
| 404 | LogUtil.v( |
| 405 | "InCallActivityCommon.onKeyDown", |
| 406 | "----------- InCallActivity View dump --------------"); |
| 407 | // Dump starting from the top-level view of the entire activity: |
| 408 | Window w = inCallActivity.getWindow(); |
| 409 | View decorView = w.getDecorView(); |
| 410 | LogUtil.v("InCallActivityCommon.onKeyDown", "View dump:" + decorView); |
| 411 | return true; |
| 412 | } |
| 413 | break; |
| 414 | case KeyEvent.KEYCODE_EQUALS: |
| 415 | break; |
| 416 | } |
| 417 | |
| 418 | return event.getRepeatCount() == 0 && handleDialerKeyDown(keyCode, event); |
| 419 | } |
| 420 | |
| 421 | private boolean handleDialerKeyDown(int keyCode, KeyEvent event) { |
| 422 | LogUtil.v("InCallActivityCommon.handleDialerKeyDown", "keyCode %d, event: %s", keyCode, event); |
| 423 | |
| 424 | // As soon as the user starts typing valid dialable keys on the |
| 425 | // keyboard (presumably to type DTMF tones) we start passing the |
| 426 | // key events to the DTMFDialer's onDialerKeyDown. |
| 427 | DialpadFragment dialpadFragment = getDialpadFragment(); |
| 428 | if (dialpadFragment != null && dialpadFragment.isVisible()) { |
| 429 | return dialpadFragment.onDialerKeyDown(event); |
| 430 | } |
| 431 | |
| 432 | return false; |
| 433 | } |
| 434 | |
| 435 | public void dismissKeyguard(boolean dismiss) { |
| 436 | if (dismissKeyguard == dismiss) { |
| 437 | return; |
| 438 | } |
| 439 | dismissKeyguard = dismiss; |
| 440 | if (dismiss) { |
| 441 | inCallActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); |
| 442 | } else { |
| 443 | inCallActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | public void showPostCharWaitDialog(String callId, String chars) { |
| 448 | if (inCallActivity.isVisible()) { |
| 449 | PostCharDialogFragment fragment = new PostCharDialogFragment(callId, chars); |
| 450 | fragment.show(inCallActivity.getSupportFragmentManager(), "postCharWait"); |
| 451 | |
| 452 | showPostCharWaitDialogOnResume = false; |
| 453 | showPostCharWaitDialogCallId = null; |
| 454 | showPostCharWaitDialogChars = null; |
| 455 | } else { |
| 456 | showPostCharWaitDialogOnResume = true; |
| 457 | showPostCharWaitDialogCallId = callId; |
| 458 | showPostCharWaitDialogChars = chars; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | public void maybeShowErrorDialogOnDisconnect(DisconnectCause cause) { |
| 463 | LogUtil.i( |
| 464 | "InCallActivityCommon.maybeShowErrorDialogOnDisconnect", "disconnect cause: %s", cause); |
| 465 | |
| 466 | if (!inCallActivity.isFinishing()) { |
| 467 | if (EnableWifiCallingPrompt.shouldShowPrompt(cause)) { |
| 468 | Pair<Dialog, CharSequence> pair = |
| 469 | EnableWifiCallingPrompt.createDialog(inCallActivity, cause); |
| 470 | showErrorDialog(pair.first, pair.second); |
| 471 | } else if (shouldShowDisconnectErrorDialog(cause)) { |
| 472 | Pair<Dialog, CharSequence> pair = getDisconnectErrorDialog(inCallActivity, cause); |
| 473 | showErrorDialog(pair.first, pair.second); |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * When relaunching from the dialer app, {@code showDialpad} indicates whether the dialpad should |
| 480 | * be shown on launch. |
| 481 | * |
| 482 | * @param showDialpad {@code true} to indicate the dialpad should be shown on launch, and {@code |
| 483 | * false} to indicate no change should be made to the dialpad visibility. |
| 484 | */ |
| 485 | private void relaunchedFromDialer(boolean showDialpad) { |
| 486 | showDialpadRequest = showDialpad ? DIALPAD_REQUEST_SHOW : DIALPAD_REQUEST_NONE; |
| 487 | animateDialpadOnShow = true; |
| 488 | |
| 489 | if (showDialpadRequest == DIALPAD_REQUEST_SHOW) { |
| 490 | // If there's only one line in use, AND it's on hold, then we're sure the user |
| 491 | // wants to use the dialpad toward the exact line, so un-hold the holding line. |
| 492 | DialerCall call = CallList.getInstance().getActiveOrBackgroundCall(); |
| 493 | if (call != null && call.getState() == State.ONHOLD) { |
| 494 | call.unhold(); |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | public void dismissPendingDialogs() { |
| 500 | if (dialog != null) { |
| 501 | dialog.dismiss(); |
| 502 | dialog = null; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | private static boolean shouldShowDisconnectErrorDialog(@NonNull DisconnectCause cause) { |
| 507 | return !TextUtils.isEmpty(cause.getDescription()) |
| 508 | && (cause.getCode() == DisconnectCause.ERROR |
| 509 | || cause.getCode() == DisconnectCause.RESTRICTED); |
| 510 | } |
| 511 | |
| 512 | private static Pair<Dialog, CharSequence> getDisconnectErrorDialog( |
| 513 | @NonNull Context context, @NonNull DisconnectCause cause) { |
| 514 | CharSequence message = cause.getDescription(); |
| 515 | Dialog dialog = |
| 516 | new AlertDialog.Builder(context) |
| 517 | .setMessage(message) |
| 518 | .setPositiveButton(android.R.string.ok, null) |
| 519 | .create(); |
| 520 | return new Pair<>(dialog, message); |
| 521 | } |
| 522 | |
| 523 | private void showErrorDialog(Dialog dialog, CharSequence message) { |
| 524 | LogUtil.i("InCallActivityCommon.showErrorDialog", "message: %s", message); |
| 525 | inCallActivity.dismissPendingDialogs(); |
| 526 | |
| 527 | // Show toast if apps is in background when dialog won't be visible. |
| 528 | if (!inCallActivity.isVisible()) { |
| 529 | Toast.makeText(inCallActivity.getApplicationContext(), message, Toast.LENGTH_LONG).show(); |
| 530 | return; |
| 531 | } |
| 532 | |
| 533 | this.dialog = dialog; |
| 534 | dialog.setOnDismissListener( |
| 535 | new OnDismissListener() { |
| 536 | @Override |
| 537 | public void onDismiss(DialogInterface dialog) { |
| 538 | LogUtil.i("InCallActivityCommon.showErrorDialog", "dialog dismissed"); |
| 539 | onDialogDismissed(); |
| 540 | } |
| 541 | }); |
| 542 | dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); |
| 543 | dialog.show(); |
| 544 | } |
| 545 | |
| 546 | private void onDialogDismissed() { |
| 547 | dialog = null; |
| 548 | CallList.getInstance().onErrorDialogDismissed(); |
| 549 | InCallPresenter.getInstance().onDismissDialog(); |
| 550 | } |
| 551 | |
| 552 | public void enableInCallOrientationEventListener(boolean enable) { |
| 553 | if (enable) { |
| 554 | inCallOrientationEventListener.enable(true); |
| 555 | } else { |
| 556 | inCallOrientationEventListener.disable(); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | public void setExcludeFromRecents(boolean exclude) { |
| 561 | List<AppTask> tasks = inCallActivity.getSystemService(ActivityManager.class).getAppTasks(); |
| 562 | int taskId = inCallActivity.getTaskId(); |
| 563 | for (int i = 0; i < tasks.size(); i++) { |
| 564 | ActivityManager.AppTask task = tasks.get(i); |
| 565 | try { |
| 566 | if (task.getTaskInfo().id == taskId) { |
| 567 | task.setExcludeFromRecents(exclude); |
| 568 | } |
| 569 | } catch (RuntimeException e) { |
| 570 | LogUtil.e( |
| 571 | "InCallActivityCommon.setExcludeFromRecents", |
| 572 | "RuntimeException when excluding task from recents.", |
| 573 | e); |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | public void showWifiToLteHandoverToast(DialerCall call) { |
| 579 | if (call.hasShownWiFiToLteHandoverToast()) { |
| 580 | return; |
| 581 | } |
| 582 | Toast.makeText( |
| 583 | inCallActivity, R.string.video_call_wifi_to_lte_handover_toast, Toast.LENGTH_LONG) |
| 584 | .show(); |
| 585 | call.setHasShownWiFiToLteHandoverToast(); |
| 586 | } |
| 587 | |
| 588 | public void showWifiFailedDialog(final DialerCall call) { |
| 589 | if (call.showWifiHandoverAlertAsToast()) { |
| 590 | LogUtil.i("InCallActivityCommon.showWifiFailedDialog", "as toast"); |
| 591 | Toast.makeText( |
| 592 | inCallActivity, R.string.video_call_lte_to_wifi_failed_message, Toast.LENGTH_SHORT) |
| 593 | .show(); |
| 594 | return; |
| 595 | } |
| 596 | |
| 597 | dismissPendingDialogs(); |
| 598 | |
| 599 | AlertDialog.Builder builder = |
| 600 | new AlertDialog.Builder(inCallActivity) |
| 601 | .setTitle(R.string.video_call_lte_to_wifi_failed_title); |
| 602 | |
| 603 | // This allows us to use the theme of the dialog instead of the activity |
| 604 | View dialogCheckBoxView = |
| 605 | View.inflate(builder.getContext(), R.layout.video_call_lte_to_wifi_failed, null); |
| 606 | final CheckBox wifiHandoverFailureCheckbox = |
| 607 | (CheckBox) dialogCheckBoxView.findViewById(R.id.video_call_lte_to_wifi_failed_checkbox); |
| 608 | wifiHandoverFailureCheckbox.setChecked(false); |
| 609 | |
| 610 | dialog = |
| 611 | builder |
| 612 | .setView(dialogCheckBoxView) |
| 613 | .setMessage(R.string.video_call_lte_to_wifi_failed_message) |
| 614 | .setOnCancelListener( |
| 615 | new OnCancelListener() { |
| 616 | @Override |
| 617 | public void onCancel(DialogInterface dialog) { |
| 618 | onDialogDismissed(); |
| 619 | } |
| 620 | }) |
| 621 | .setPositiveButton( |
| 622 | android.R.string.ok, |
| 623 | new DialogInterface.OnClickListener() { |
| 624 | @Override |
| 625 | public void onClick(DialogInterface dialog, int id) { |
| 626 | call.setDoNotShowDialogForHandoffToWifiFailure( |
| 627 | wifiHandoverFailureCheckbox.isChecked()); |
| 628 | dialog.cancel(); |
| 629 | onDialogDismissed(); |
| 630 | } |
| 631 | }) |
| 632 | .create(); |
| 633 | |
| 634 | LogUtil.i("InCallActivityCommon.showWifiFailedDialog", "as dialog"); |
| 635 | dialog.show(); |
| 636 | } |
| 637 | |
| 638 | public boolean showDialpadFragment(boolean show, boolean animate) { |
| 639 | // If the dialpad is already visible, don't animate in. If it's gone, don't animate out. |
| 640 | boolean isDialpadVisible = isDialpadVisible(); |
| 641 | LogUtil.i( |
| 642 | "InCallActivityCommon.showDialpadFragment", |
| 643 | "show: %b, animate: %b, " + "isDialpadVisible: %b", |
| 644 | show, |
| 645 | animate, |
| 646 | isDialpadVisible); |
| 647 | if (show == isDialpadVisible) { |
| 648 | return false; |
| 649 | } |
| 650 | |
| 651 | FragmentManager dialpadFragmentManager = inCallActivity.getDialpadFragmentManager(); |
| 652 | if (dialpadFragmentManager == null) { |
| 653 | LogUtil.i( |
| 654 | "InCallActivityCommon.showDialpadFragment", "unable to show or hide dialpad fragment"); |
| 655 | return false; |
| 656 | } |
| 657 | |
| 658 | // We don't do a FragmentTransaction on the hide case because it will be dealt with when |
| 659 | // the listener is fired after an animation finishes. |
| 660 | if (!animate) { |
| 661 | if (show) { |
| 662 | performShowDialpadFragment(dialpadFragmentManager); |
| 663 | } else { |
| 664 | performHideDialpadFragment(); |
| 665 | } |
| 666 | } else { |
| 667 | if (show) { |
| 668 | performShowDialpadFragment(dialpadFragmentManager); |
| 669 | getDialpadFragment().animateShowDialpad(); |
| 670 | } |
| 671 | getDialpadFragment() |
| 672 | .getView() |
| 673 | .startAnimation(show ? dialpadSlideInAnimation : dialpadSlideOutAnimation); |
| 674 | } |
| 675 | |
| 676 | ProximitySensor sensor = InCallPresenter.getInstance().getProximitySensor(); |
| 677 | if (sensor != null) { |
| 678 | sensor.onDialpadVisible(show); |
| 679 | } |
| 680 | showDialpadRequest = DIALPAD_REQUEST_NONE; |
| 681 | return true; |
| 682 | } |
| 683 | |
| 684 | private void performShowDialpadFragment(@NonNull FragmentManager dialpadFragmentManager) { |
| 685 | FragmentTransaction transaction = dialpadFragmentManager.beginTransaction(); |
| 686 | DialpadFragment dialpadFragment = getDialpadFragment(); |
| 687 | if (dialpadFragment == null) { |
| 688 | transaction.add( |
| 689 | inCallActivity.getDialpadContainerId(), new DialpadFragment(), TAG_DIALPAD_FRAGMENT); |
| 690 | } else { |
| 691 | transaction.show(dialpadFragment); |
| 692 | } |
| 693 | |
| 694 | transaction.commitAllowingStateLoss(); |
| 695 | dialpadFragmentManager.executePendingTransactions(); |
| 696 | |
| 697 | Logger.get(inCallActivity).logScreenView(ScreenEvent.Type.INCALL_DIALPAD, inCallActivity); |
| 698 | } |
| 699 | |
| 700 | private void performHideDialpadFragment() { |
| 701 | FragmentManager fragmentManager = inCallActivity.getDialpadFragmentManager(); |
| 702 | if (fragmentManager == null) { |
| 703 | LogUtil.e( |
| 704 | "InCallActivityCommon.performHideDialpadFragment", "child fragment manager is null"); |
| 705 | return; |
| 706 | } |
| 707 | |
| 708 | Fragment fragment = fragmentManager.findFragmentByTag(TAG_DIALPAD_FRAGMENT); |
| 709 | if (fragment != null) { |
| 710 | FragmentTransaction transaction = fragmentManager.beginTransaction(); |
| 711 | transaction.hide(fragment); |
| 712 | transaction.commitAllowingStateLoss(); |
| 713 | fragmentManager.executePendingTransactions(); |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | public boolean isDialpadVisible() { |
| 718 | DialpadFragment dialpadFragment = getDialpadFragment(); |
| 719 | return dialpadFragment != null && dialpadFragment.isVisible(); |
| 720 | } |
| 721 | |
| 722 | /** Returns the {@link DialpadFragment} that's shown by this activity, or {@code null} */ |
| 723 | @Nullable |
| 724 | private DialpadFragment getDialpadFragment() { |
| 725 | FragmentManager fragmentManager = inCallActivity.getDialpadFragmentManager(); |
| 726 | if (fragmentManager == null) { |
| 727 | return null; |
| 728 | } |
| 729 | return (DialpadFragment) fragmentManager.findFragmentByTag(TAG_DIALPAD_FRAGMENT); |
| 730 | } |
| 731 | |
| 732 | public void updateTaskDescription() { |
| 733 | Resources resources = inCallActivity.getResources(); |
| 734 | int color; |
| 735 | if (resources.getBoolean(R.bool.is_layout_landscape)) { |
| 736 | color = |
| 737 | ResourcesCompat.getColor( |
| 738 | resources, R.color.statusbar_background_color, inCallActivity.getTheme()); |
| 739 | } else { |
| 740 | color = InCallPresenter.getInstance().getThemeColorManager().getSecondaryColor(); |
| 741 | } |
| 742 | |
| 743 | TaskDescription td = |
| 744 | new TaskDescription(resources.getString(R.string.notification_ongoing_call), null, color); |
| 745 | inCallActivity.setTaskDescription(td); |
| 746 | } |
| 747 | |
| 748 | public boolean hasPendingDialogs() { |
| 749 | return dialog != null; |
| 750 | } |
| 751 | |
| 752 | private void internalResolveIntent(Intent intent) { |
| 753 | if (!intent.getAction().equals(Intent.ACTION_MAIN)) { |
| 754 | return; |
| 755 | } |
| 756 | |
| 757 | if (intent.hasExtra(INTENT_EXTRA_SHOW_DIALPAD)) { |
| 758 | // SHOW_DIALPAD_EXTRA can be used here to specify whether the DTMF |
| 759 | // dialpad should be initially visible. If the extra isn't |
| 760 | // present at all, we just leave the dialpad in its previous state. |
| 761 | boolean showDialpad = intent.getBooleanExtra(INTENT_EXTRA_SHOW_DIALPAD, false); |
| 762 | LogUtil.i("InCallActivityCommon.internalResolveIntent", "SHOW_DIALPAD_EXTRA: " + showDialpad); |
| 763 | |
| 764 | relaunchedFromDialer(showDialpad); |
| 765 | } |
| 766 | |
| 767 | DialerCall outgoingCall = CallList.getInstance().getOutgoingCall(); |
| 768 | if (outgoingCall == null) { |
| 769 | outgoingCall = CallList.getInstance().getPendingOutgoingCall(); |
| 770 | } |
| 771 | |
| 772 | boolean isNewOutgoingCall = false; |
| 773 | if (intent.getBooleanExtra(INTENT_EXTRA_NEW_OUTGOING_CALL, false)) { |
| 774 | isNewOutgoingCall = true; |
| 775 | intent.removeExtra(INTENT_EXTRA_NEW_OUTGOING_CALL); |
| 776 | |
| 777 | // InCallActivity is responsible for disconnecting a new outgoing call if there |
| 778 | // is no way of making it (i.e. no valid call capable accounts). |
| 779 | // If the version is not MSIM compatible, then ignore this code. |
| 780 | if (CompatUtils.isMSIMCompatible() |
| 781 | && InCallPresenter.isCallWithNoValidAccounts(outgoingCall)) { |
| 782 | LogUtil.i( |
| 783 | "InCallActivityCommon.internalResolveIntent", |
| 784 | "call with no valid accounts, disconnecting"); |
| 785 | outgoingCall.disconnect(); |
| 786 | } |
| 787 | |
| 788 | dismissKeyguard(true); |
| 789 | } |
| 790 | |
| 791 | boolean didShowAccountSelectionDialog = maybeShowAccountSelectionDialog(); |
| 792 | inCallActivity.onResolveIntent(outgoingCall, isNewOutgoingCall, didShowAccountSelectionDialog); |
| 793 | } |
| 794 | |
| 795 | private boolean maybeShowAccountSelectionDialog() { |
| 796 | DialerCall call = CallList.getInstance().getWaitingForAccountCall(); |
| 797 | if (call == null) { |
| 798 | return false; |
| 799 | } |
| 800 | |
| 801 | Bundle extras = call.getIntentExtras(); |
| 802 | List<PhoneAccountHandle> phoneAccountHandles; |
| 803 | if (extras != null) { |
| 804 | phoneAccountHandles = |
| 805 | extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS); |
| 806 | } else { |
| 807 | phoneAccountHandles = new ArrayList<>(); |
| 808 | } |
| 809 | |
| 810 | DialogFragment dialogFragment = |
| 811 | SelectPhoneAccountDialogFragment.newInstance( |
| 812 | R.string.select_phone_account_for_calls, |
| 813 | true, |
| 814 | phoneAccountHandles, |
| 815 | selectAccountListener, |
| 816 | call.getId()); |
| 817 | dialogFragment.show(inCallActivity.getFragmentManager(), TAG_SELECT_ACCOUNT_FRAGMENT); |
| 818 | return true; |
| 819 | } |
| 820 | } |