| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.wm.shell.pip; |
| 18 |
|
| 19 |
import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN; |
| 20 |
import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED; |
| 21 |
import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED; |
| 22 |
import static android.util.RotationUtils.deltaRotation; |
| 23 |
import static android.util.RotationUtils.rotateBounds; |
| 24 |
|
| 25 |
import static com.android.wm.shell.ShellTaskOrganizer.TASK_LISTENER_TYPE_PIP; |
| 26 |
import static com.android.wm.shell.ShellTaskOrganizer.taskListenerTypeToString; |
| 27 |
import static com.android.wm.shell.common.split.SplitScreenConstants.SPLIT_POSITION_BOTTOM_OR_RIGHT; |
| 28 |
import static com.android.wm.shell.common.split.SplitScreenConstants.SPLIT_POSITION_TOP_OR_LEFT; |
| 29 |
import static com.android.wm.shell.pip.PipAnimationController.ANIM_TYPE_ALPHA; |
| 30 |
import static com.android.wm.shell.pip.PipAnimationController.ANIM_TYPE_BOUNDS; |
| 31 |
import static com.android.wm.shell.pip.PipAnimationController.FRACTION_START; |
| 32 |
import static com.android.wm.shell.pip.PipAnimationController.TRANSITION_DIRECTION_EXPAND_OR_UNEXPAND; |
| 33 |
import static com.android.wm.shell.pip.PipAnimationController.TRANSITION_DIRECTION_LEAVE_PIP; |
| 34 |
import static com.android.wm.shell.pip.PipAnimationController.TRANSITION_DIRECTION_LEAVE_PIP_TO_SPLIT_SCREEN; |
| 35 |
import static com.android.wm.shell.pip.PipAnimationController.TRANSITION_DIRECTION_NONE; |
| 36 |
import static com.android.wm.shell.pip.PipAnimationController.TRANSITION_DIRECTION_REMOVE_STACK; |
| 37 |
import static com.android.wm.shell.pip.PipAnimationController.TRANSITION_DIRECTION_SAME; |
| 38 |
import static com.android.wm.shell.pip.PipAnimationController.TRANSITION_DIRECTION_SNAP_AFTER_RESIZE; |
| 39 |
import static com.android.wm.shell.pip.PipAnimationController.TRANSITION_DIRECTION_TO_PIP; |
| 40 |
import static com.android.wm.shell.pip.PipAnimationController.TRANSITION_DIRECTION_USER_RESIZE; |
| 41 |
import static com.android.wm.shell.pip.PipAnimationController.isInPipDirection; |
| 42 |
import static com.android.wm.shell.pip.PipAnimationController.isOutPipDirection; |
| 43 |
import static com.android.wm.shell.pip.PipAnimationController.isRemovePipDirection; |
| 44 |
import static com.android.wm.shell.transition.Transitions.ENABLE_SHELL_TRANSITIONS; |
| 45 |
import static com.android.wm.shell.transition.Transitions.TRANSIT_EXIT_PIP; |
| 46 |
import static com.android.wm.shell.transition.Transitions.TRANSIT_EXIT_PIP_TO_SPLIT; |
| 47 |
import static com.android.wm.shell.transition.Transitions.TRANSIT_REMOVE_PIP; |
| 48 |
|
| 49 |
import android.animation.Animator; |
| 50 |
import android.animation.AnimatorListenerAdapter; |
| 51 |
import android.animation.ValueAnimator; |
| 52 |
import android.annotation.NonNull; |
| 53 |
import android.annotation.Nullable; |
| 54 |
import android.app.ActivityManager; |
| 55 |
import android.app.ActivityTaskManager; |
| 56 |
import android.app.PictureInPictureParams; |
| 57 |
import android.app.TaskInfo; |
| 58 |
import android.content.ComponentName; |
| 59 |
import android.content.Context; |
| 60 |
import android.content.pm.ActivityInfo; |
| 61 |
import android.content.res.Configuration; |
| 62 |
import android.graphics.Rect; |
| 63 |
import android.os.RemoteException; |
| 64 |
import android.os.SystemClock; |
| 65 |
import android.util.Log; |
| 66 |
import android.view.Choreographer; |
| 67 |
import android.view.Display; |
| 68 |
import android.view.Surface; |
| 69 |
import android.view.SurfaceControl; |
| 70 |
import android.window.TaskOrganizer; |
| 71 |
import android.window.TaskSnapshot; |
| 72 |
import android.window.WindowContainerToken; |
| 73 |
import android.window.WindowContainerTransaction; |
| 74 |
|
| 75 |
import com.android.internal.annotations.VisibleForTesting; |
| 76 |
import com.android.internal.protolog.common.ProtoLog; |
| 77 |
import com.android.wm.shell.R; |
| 78 |
import com.android.wm.shell.ShellTaskOrganizer; |
| 79 |
import com.android.wm.shell.animation.Interpolators; |
| 80 |
import com.android.wm.shell.common.DisplayController; |
| 81 |
import com.android.wm.shell.common.ScreenshotUtils; |
| 82 |
import com.android.wm.shell.common.ShellExecutor; |
| 83 |
import com.android.wm.shell.common.SyncTransactionQueue; |
| 84 |
import com.android.wm.shell.common.annotations.ShellMainThread; |
| 85 |
import com.android.wm.shell.pip.phone.PipMotionHelper; |
| 86 |
import com.android.wm.shell.protolog.ShellProtoLogGroup; |
| 87 |
import com.android.wm.shell.splitscreen.SplitScreenController; |
| 88 |
import com.android.wm.shell.transition.Transitions; |
| 89 |
|
| 90 |
import java.io.PrintWriter; |
| 91 |
import java.util.Objects; |
| 92 |
import java.util.Optional; |
| 93 |
import java.util.function.Consumer; |
| 94 |
import java.util.function.IntConsumer; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public class PipTaskOrganizer implements ShellTaskOrganizer.TaskListener, |
| 108 |
DisplayController.OnDisplaysChangedListener, ShellTaskOrganizer.FocusListener { |
| 109 |
private static final String TAG = PipTaskOrganizer.class.getSimpleName(); |
| 110 |
private static final boolean DEBUG = false; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
private static final int ONE_SHOT_ALPHA_ANIMATION_TIMEOUT_MS = 1000; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
private static final int CONTENT_OVERLAY_FADE_OUT_DELAY_MS = 500; |
| 124 |
|
| 125 |
private final Context mContext; |
| 126 |
private final SyncTransactionQueue mSyncTransactionQueue; |
| 127 |
private final PipBoundsState mPipBoundsState; |
| 128 |
private final PipBoundsAlgorithm mPipBoundsAlgorithm; |
| 129 |
private final @NonNull PipMenuController mPipMenuController; |
| 130 |
private final PipAnimationController mPipAnimationController; |
| 131 |
protected final PipTransitionController mPipTransitionController; |
| 132 |
protected final PipParamsChangedForwarder mPipParamsChangedForwarder; |
| 133 |
private final PipUiEventLogger mPipUiEventLoggerLogger; |
| 134 |
private final int mEnterAnimationDuration; |
| 135 |
private final int mExitAnimationDuration; |
| 136 |
private final int mCrossFadeAnimationDuration; |
| 137 |
private final PipSurfaceTransactionHelper mSurfaceTransactionHelper; |
| 138 |
private final Optional<SplitScreenController> mSplitScreenOptional; |
| 139 |
protected final ShellTaskOrganizer mTaskOrganizer; |
| 140 |
protected final ShellExecutor mMainExecutor; |
| 141 |
|
| 142 |
|
| 143 |
private final PipAnimationController.PipAnimationCallback mPipAnimationCallback = |
| 144 |
new PipAnimationController.PipAnimationCallback() { |
| 145 |
@Override |
| 146 |
public void onPipAnimationStart(TaskInfo taskInfo, |
| 147 |
PipAnimationController.PipTransitionAnimator animator) { |
| 148 |
final int direction = animator.getTransitionDirection(); |
| 149 |
sendOnPipTransitionStarted(direction); |
| 150 |
} |
| 151 |
|
| 152 |
@Override |
| 153 |
public void onPipAnimationEnd(TaskInfo taskInfo, SurfaceControl.Transaction tx, |
| 154 |
PipAnimationController.PipTransitionAnimator animator) { |
| 155 |
final int direction = animator.getTransitionDirection(); |
| 156 |
final int animationType = animator.getAnimationType(); |
| 157 |
final Rect destinationBounds = animator.getDestinationBounds(); |
| 158 |
if (isInPipDirection(direction) && animator.getContentOverlayLeash() != null) { |
| 159 |
fadeOutAndRemoveOverlay(animator.getContentOverlayLeash(), |
| 160 |
animator::clearContentOverlay, true ); |
| 161 |
} |
| 162 |
if (mWaitForFixedRotation && animationType == ANIM_TYPE_BOUNDS |
| 163 |
&& direction == TRANSITION_DIRECTION_TO_PIP) { |
| 164 |
|
| 165 |
final WindowContainerTransaction wct = new WindowContainerTransaction(); |
| 166 |
wct.scheduleFinishEnterPip(mToken, destinationBounds); |
| 167 |
mTaskOrganizer.applyTransaction(wct); |
| 168 |
|
| 169 |
|
| 170 |
mSurfaceTransactionHelper.round(tx, mLeash, isInPip()); |
| 171 |
mDeferredAnimEndTransaction = tx; |
| 172 |
return; |
| 173 |
} |
| 174 |
final boolean isExitPipDirection = isOutPipDirection(direction) |
| 175 |
|| isRemovePipDirection(direction); |
| 176 |
if (mPipTransitionState.getTransitionState() != PipTransitionState.EXITING_PIP |
| 177 |
|| isExitPipDirection) { |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
finishResizeDelayedIfNeeded(() -> { |
| 184 |
finishResize(tx, destinationBounds, direction, animationType); |
| 185 |
sendOnPipTransitionFinished(direction); |
| 186 |
}); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
@Override |
| 191 |
public void onPipAnimationCancel(TaskInfo taskInfo, |
| 192 |
PipAnimationController.PipTransitionAnimator animator) { |
| 193 |
final int direction = animator.getTransitionDirection(); |
| 194 |
if (isInPipDirection(direction) && animator.getContentOverlayLeash() != null) { |
| 195 |
fadeOutAndRemoveOverlay(animator.getContentOverlayLeash(), |
| 196 |
animator::clearContentOverlay, true ); |
| 197 |
} |
| 198 |
sendOnPipTransitionCancelled(direction); |
| 199 |
} |
| 200 |
}; |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
private void finishResizeDelayedIfNeeded(Runnable finishResizeRunnable) { |
| 220 |
if (!shouldSyncPipTransactionWithMenu()) { |
| 221 |
finishResizeRunnable.run(); |
| 222 |
return; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
Choreographer.getInstance().postCallback(Choreographer.CALLBACK_COMMIT, () -> { |
| 227 |
mMainExecutor.execute(finishResizeRunnable); |
| 228 |
}, null); |
| 229 |
} |
| 230 |
|
| 231 |
private boolean shouldSyncPipTransactionWithMenu() { |
| 232 |
return mPipMenuController.isMenuVisible(); |
| 233 |
} |
| 234 |
|
| 235 |
@VisibleForTesting |
| 236 |
final PipTransitionController.PipTransitionCallback mPipTransitionCallback = |
| 237 |
new PipTransitionController.PipTransitionCallback() { |
| 238 |
@Override |
| 239 |
public void onPipTransitionStarted(int direction, Rect pipBounds) {} |
| 240 |
|
| 241 |
@Override |
| 242 |
public void onPipTransitionFinished(int direction) { |
| 243 |
|
| 244 |
|
| 245 |
if (direction == TRANSITION_DIRECTION_TO_PIP && mDeferredTaskInfo != null) { |
| 246 |
onTaskInfoChanged(mDeferredTaskInfo); |
| 247 |
mDeferredTaskInfo = null; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
@Override |
| 252 |
public void onPipTransitionCanceled(int direction) {} |
| 253 |
}; |
| 254 |
|
| 255 |
private final PipAnimationController.PipTransactionHandler mPipTransactionHandler = |
| 256 |
new PipAnimationController.PipTransactionHandler() { |
| 257 |
@Override |
| 258 |
public boolean handlePipTransaction(SurfaceControl leash, |
| 259 |
SurfaceControl.Transaction tx, Rect destinationBounds) { |
| 260 |
if (shouldSyncPipTransactionWithMenu()) { |
| 261 |
mPipMenuController.movePipMenu(leash, tx, destinationBounds); |
| 262 |
return true; |
| 263 |
} |
| 264 |
return false; |
| 265 |
} |
| 266 |
}; |
| 267 |
|
| 268 |
private ActivityManager.RunningTaskInfo mTaskInfo; |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
private ActivityManager.RunningTaskInfo mDeferredTaskInfo; |
| 273 |
private WindowContainerToken mToken; |
| 274 |
private SurfaceControl mLeash; |
| 275 |
protected PipTransitionState mPipTransitionState; |
| 276 |
private @PipAnimationController.AnimationType int mOneShotAnimationType = ANIM_TYPE_BOUNDS; |
| 277 |
private long mLastOneShotAlphaAnimationTime; |
| 278 |
private PipSurfaceTransactionHelper.SurfaceControlTransactionFactory |
| 279 |
mSurfaceControlTransactionFactory; |
| 280 |
protected PictureInPictureParams mPictureInPictureParams; |
| 281 |
private IntConsumer mOnDisplayIdChangeCallback; |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
private SurfaceControl.Transaction mDeferredAnimEndTransaction; |
| 287 |
|
| 288 |
private boolean mHasFadeOut; |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
private boolean mWaitForFixedRotation; |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
private @Surface.Rotation int mNextRotation; |
| 301 |
|
| 302 |
private @Surface.Rotation int mCurrentRotation; |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
@Nullable |
| 309 |
SurfaceControl mSwipePipToHomeOverlay; |
| 310 |
|
| 311 |
public PipTaskOrganizer(Context context, |
| 312 |
@NonNull SyncTransactionQueue syncTransactionQueue, |
| 313 |
@NonNull PipTransitionState pipTransitionState, |
| 314 |
@NonNull PipBoundsState pipBoundsState, |
| 315 |
@NonNull PipBoundsAlgorithm boundsHandler, |
| 316 |
@NonNull PipMenuController pipMenuController, |
| 317 |
@NonNull PipAnimationController pipAnimationController, |
| 318 |
@NonNull PipSurfaceTransactionHelper surfaceTransactionHelper, |
| 319 |
@NonNull PipTransitionController pipTransitionController, |
| 320 |
@NonNull PipParamsChangedForwarder pipParamsChangedForwarder, |
| 321 |
Optional<SplitScreenController> splitScreenOptional, |
| 322 |
@NonNull DisplayController displayController, |
| 323 |
@NonNull PipUiEventLogger pipUiEventLogger, |
| 324 |
@NonNull ShellTaskOrganizer shellTaskOrganizer, |
| 325 |
@ShellMainThread ShellExecutor mainExecutor) { |
| 326 |
mContext = context; |
| 327 |
mSyncTransactionQueue = syncTransactionQueue; |
| 328 |
mPipTransitionState = pipTransitionState; |
| 329 |
mPipBoundsState = pipBoundsState; |
| 330 |
mPipBoundsAlgorithm = boundsHandler; |
| 331 |
mPipMenuController = pipMenuController; |
| 332 |
mPipTransitionController = pipTransitionController; |
| 333 |
mPipParamsChangedForwarder = pipParamsChangedForwarder; |
| 334 |
mEnterAnimationDuration = context.getResources() |
| 335 |
.getInteger(R.integer.config_pipEnterAnimationDuration); |
| 336 |
mExitAnimationDuration = context.getResources() |
| 337 |
.getInteger(R.integer.config_pipExitAnimationDuration); |
| 338 |
mCrossFadeAnimationDuration = context.getResources() |
| 339 |
.getInteger(R.integer.config_pipCrossfadeAnimationDuration); |
| 340 |
mSurfaceTransactionHelper = surfaceTransactionHelper; |
| 341 |
mPipAnimationController = pipAnimationController; |
| 342 |
mPipUiEventLoggerLogger = pipUiEventLogger; |
| 343 |
mSurfaceControlTransactionFactory = |
| 344 |
new PipSurfaceTransactionHelper.VsyncSurfaceControlTransactionFactory(); |
| 345 |
mSplitScreenOptional = splitScreenOptional; |
| 346 |
mTaskOrganizer = shellTaskOrganizer; |
| 347 |
mMainExecutor = mainExecutor; |
| 348 |
|
| 349 |
|
| 350 |
mMainExecutor.execute(() -> { |
| 351 |
mTaskOrganizer.addListenerForType(this, TASK_LISTENER_TYPE_PIP); |
| 352 |
}); |
| 353 |
mTaskOrganizer.addFocusListener(this); |
| 354 |
mPipTransitionController.setPipOrganizer(this); |
| 355 |
displayController.addDisplayWindowListener(this); |
| 356 |
pipTransitionController.registerPipTransitionCallback(mPipTransitionCallback); |
| 357 |
} |
| 358 |
|
| 359 |
public PipTransitionController getTransitionController() { |
| 360 |
return mPipTransitionController; |
| 361 |
} |
| 362 |
|
| 363 |
public Rect getCurrentOrAnimatingBounds() { |
| 364 |
PipAnimationController.PipTransitionAnimator animator = |
| 365 |
mPipAnimationController.getCurrentAnimator(); |
| 366 |
if (animator != null && animator.isRunning()) { |
| 367 |
return new Rect(animator.getDestinationBounds()); |
| 368 |
} |
| 369 |
return mPipBoundsState.getBounds(); |
| 370 |
} |
| 371 |
|
| 372 |
public boolean isInPip() { |
| 373 |
return mPipTransitionState.isInPip(); |
| 374 |
} |
| 375 |
|
| 376 |
private boolean isLaunchIntoPipTask() { |
| 377 |
return mPictureInPictureParams != null && mPictureInPictureParams.isLaunchIntoPip(); |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
public boolean isEntryScheduled() { |
| 384 |
return mPipTransitionState.getTransitionState() == PipTransitionState.ENTRY_SCHEDULED; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
public void registerOnDisplayIdChangeCallback(IntConsumer onDisplayIdChangeCallback) { |
| 391 |
mOnDisplayIdChangeCallback = onDisplayIdChangeCallback; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
public void setOneShotAnimationType(@PipAnimationController.AnimationType int animationType) { |
| 400 |
mOneShotAnimationType = animationType; |
| 401 |
if (animationType == ANIM_TYPE_ALPHA) { |
| 402 |
mLastOneShotAlphaAnimationTime = SystemClock.uptimeMillis(); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
public Rect startSwipePipToHome(ComponentName componentName, ActivityInfo activityInfo, |
| 411 |
PictureInPictureParams pictureInPictureParams) { |
| 412 |
mPipTransitionState.setInSwipePipToHomeTransition(true); |
| 413 |
sendOnPipTransitionStarted(TRANSITION_DIRECTION_TO_PIP); |
| 414 |
setBoundsStateForEntry(componentName, pictureInPictureParams, activityInfo); |
| 415 |
return mPipBoundsAlgorithm.getEntryDestinationBounds(); |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
public void stopSwipePipToHome(int taskId, ComponentName componentName, Rect destinationBounds, |
| 423 |
SurfaceControl overlay) { |
| 424 |
|
| 425 |
if (!mPipTransitionState.getInSwipePipToHomeTransition()) { |
| 426 |
return; |
| 427 |
} |
| 428 |
mPipBoundsState.setBounds(destinationBounds); |
| 429 |
mSwipePipToHomeOverlay = overlay; |
| 430 |
if (ENABLE_SHELL_TRANSITIONS && overlay != null) { |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
final SurfaceControl.Transaction t = new SurfaceControl.Transaction(); |
| 437 |
mTaskOrganizer.reparentChildSurfaceToTask(taskId, overlay, t); |
| 438 |
t.setLayer(overlay, Integer.MAX_VALUE); |
| 439 |
t.apply(); |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
public ActivityManager.RunningTaskInfo getTaskInfo() { |
| 444 |
return mTaskInfo; |
| 445 |
} |
| 446 |
|
| 447 |
public SurfaceControl getSurfaceControl() { |
| 448 |
return mLeash; |
| 449 |
} |
| 450 |
|
| 451 |
private void setBoundsStateForEntry(ComponentName componentName, |
| 452 |
PictureInPictureParams params, ActivityInfo activityInfo) { |
| 453 |
mPipBoundsState.setBoundsStateForEntry(componentName, activityInfo, params, |
| 454 |
mPipBoundsAlgorithm); |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
public void exitPip(int animationDurationMs, boolean requestEnterSplit) { |
| 470 |
if (!mPipTransitionState.isInPip() |
| 471 |
|| mPipTransitionState.getTransitionState() == PipTransitionState.EXITING_PIP |
| 472 |
|| mToken == null) { |
| 473 |
ProtoLog.wtf(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE, |
| 474 |
"%s: Not allowed to exitPip in current state" |
| 475 |
+ " mState=%d mToken=%s", TAG, mPipTransitionState.getTransitionState(), |
| 476 |
mToken); |
| 477 |
return; |
| 478 |
} |
| 479 |
|
| 480 |
final WindowContainerTransaction wct = new WindowContainerTransaction(); |
| 481 |
if (isLaunchIntoPipTask()) { |
| 482 |
exitLaunchIntoPipTask(wct); |
| 483 |
return; |
| 484 |
} |
| 485 |
|
| 486 |
if (ENABLE_SHELL_TRANSITIONS) { |
| 487 |
if (requestEnterSplit && mSplitScreenOptional.isPresent()) { |
| 488 |
mSplitScreenOptional.get().prepareEnterSplitScreen(wct, mTaskInfo, |
| 489 |
isPipTopLeft() |
| 490 |
? SPLIT_POSITION_TOP_OR_LEFT : SPLIT_POSITION_BOTTOM_OR_RIGHT); |
| 491 |
mPipTransitionController.startExitTransition( |
| 492 |
TRANSIT_EXIT_PIP_TO_SPLIT, wct, null ); |
| 493 |
return; |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
final Rect destinationBounds = getExitDestinationBounds(); |
| 498 |
final int direction = syncWithSplitScreenBounds(destinationBounds, requestEnterSplit) |
| 499 |
? TRANSITION_DIRECTION_LEAVE_PIP_TO_SPLIT_SCREEN |
| 500 |
: TRANSITION_DIRECTION_LEAVE_PIP; |
| 501 |
|
| 502 |
if (Transitions.ENABLE_SHELL_TRANSITIONS && direction == TRANSITION_DIRECTION_LEAVE_PIP) { |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
wct.setWindowingMode(mToken, getOutPipWindowingMode()); |
| 507 |
|
| 508 |
|
| 509 |
wct.setBounds(mToken, null); |
| 510 |
} else { |
| 511 |
final SurfaceControl.Transaction tx = |
| 512 |
mSurfaceControlTransactionFactory.getTransaction(); |
| 513 |
mSurfaceTransactionHelper.scale(tx, mLeash, destinationBounds, |
| 514 |
mPipBoundsState.getBounds()); |
| 515 |
tx.setWindowCrop(mLeash, destinationBounds.width(), destinationBounds.height()); |
| 516 |
|
| 517 |
|
| 518 |
wct.setActivityWindowingMode(mToken, WINDOWING_MODE_FULLSCREEN); |
| 519 |
wct.setBounds(mToken, destinationBounds); |
| 520 |
wct.setBoundsChangeTransaction(mToken, tx); |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
mPipTransitionState.setTransitionState(PipTransitionState.EXITING_PIP); |
| 530 |
|
| 531 |
if (Transitions.ENABLE_SHELL_TRANSITIONS) { |
| 532 |
mPipTransitionController.startExitTransition(TRANSIT_EXIT_PIP, wct, destinationBounds); |
| 533 |
return; |
| 534 |
} |
| 535 |
mSyncTransactionQueue.queue(wct); |
| 536 |
mSyncTransactionQueue.runInSync(t -> { |
| 537 |
|
| 538 |
|
| 539 |
final Rect sourceHintRect = PipBoundsAlgorithm.getValidSourceHintRect( |
| 540 |
mPictureInPictureParams, destinationBounds); |
| 541 |
final PipAnimationController.PipTransitionAnimator<?> animator = |
| 542 |
animateResizePip(mPipBoundsState.getBounds(), destinationBounds, sourceHintRect, |
| 543 |
direction, animationDurationMs, 0 ); |
| 544 |
if (animator != null) { |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
animator.applySurfaceControlTransaction(mLeash, t, FRACTION_START); |
| 552 |
} |
| 553 |
}); |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
public Rect getExitDestinationBounds() { |
| 558 |
return mPipBoundsState.getDisplayBounds(); |
| 559 |
} |
| 560 |
|
| 561 |
private void exitLaunchIntoPipTask(WindowContainerTransaction wct) { |
| 562 |
wct.startTask(mTaskInfo.launchIntoPipHostTaskId, null ); |
| 563 |
mTaskOrganizer.applyTransaction(wct); |
| 564 |
|
| 565 |
|
| 566 |
removePip(); |
| 567 |
} |
| 568 |
|
| 569 |
private void applyWindowingModeChangeOnExit(WindowContainerTransaction wct, int direction) { |
| 570 |
|
| 571 |
wct.setWindowingMode(mToken, getOutPipWindowingMode()); |
| 572 |
|
| 573 |
wct.setActivityWindowingMode(mToken, WINDOWING_MODE_UNDEFINED); |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
public void removePip() { |
| 580 |
if (!mPipTransitionState.isInPip() || mToken == null) { |
| 581 |
ProtoLog.wtf(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE, |
| 582 |
"%s: Not allowed to removePip in current state" |
| 583 |
+ " mState=%d mToken=%s", TAG, mPipTransitionState.getTransitionState(), |
| 584 |
mToken); |
| 585 |
return; |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
ValueAnimator animator = mPipAnimationController |
| 590 |
.getAnimator(mTaskInfo, mLeash, mPipBoundsState.getBounds(), |
| 591 |
1f , 0f ) |
| 592 |
.setTransitionDirection(TRANSITION_DIRECTION_REMOVE_STACK) |
| 593 |
.setPipTransactionHandler(mPipTransactionHandler) |
| 594 |
.setPipAnimationCallback(mPipAnimationCallback); |
| 595 |
animator.setDuration(mExitAnimationDuration); |
| 596 |
animator.setInterpolator(Interpolators.ALPHA_OUT); |
| 597 |
animator.start(); |
| 598 |
mPipTransitionState.setTransitionState(PipTransitionState.EXITING_PIP); |
| 599 |
} |
| 600 |
|
| 601 |
private void removePipImmediately() { |
| 602 |
if (Transitions.ENABLE_SHELL_TRANSITIONS) { |
| 603 |
final WindowContainerTransaction wct = new WindowContainerTransaction(); |
| 604 |
wct.setBounds(mToken, null); |
| 605 |
wct.setWindowingMode(mToken, getOutPipWindowingMode()); |
| 606 |
wct.reorder(mToken, false); |
| 607 |
mPipTransitionController.startExitTransition(TRANSIT_REMOVE_PIP, wct, |
| 608 |
null ); |
| 609 |
return; |
| 610 |
} |
| 611 |
|
| 612 |
try { |
| 613 |
|
| 614 |
final WindowContainerTransaction wct = new WindowContainerTransaction(); |
| 615 |
wct.setBounds(mToken, null); |
| 616 |
mTaskOrganizer.applyTransaction(wct); |
| 617 |
|
| 618 |
ActivityTaskManager.getService().removeRootTasksInWindowingModes( |
| 619 |
new int[]{ WINDOWING_MODE_PINNED }); |
| 620 |
} catch (RemoteException e) { |
| 621 |
ProtoLog.e(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE, |
| 622 |
"%s: Failed to remove PiP, %s", |
| 623 |
TAG, e); |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
@Override |
| 628 |
public void onTaskAppeared(ActivityManager.RunningTaskInfo info, SurfaceControl leash) { |
| 629 |
Objects.requireNonNull(info, "Requires RunningTaskInfo"); |
| 630 |
mTaskInfo = info; |
| 631 |
mToken = mTaskInfo.token; |
| 632 |
mPipTransitionState.setTransitionState(PipTransitionState.TASK_APPEARED); |
| 633 |
mLeash = leash; |
| 634 |
mPictureInPictureParams = mTaskInfo.pictureInPictureParams; |
| 635 |
setBoundsStateForEntry(mTaskInfo.topActivity, mPictureInPictureParams, |
| 636 |
mTaskInfo.topActivityInfo); |
| 637 |
if (mPictureInPictureParams != null) { |
| 638 |
mPipParamsChangedForwarder.notifyActionsChanged(mPictureInPictureParams.getActions(), |
| 639 |
mPictureInPictureParams.getCloseAction()); |
| 640 |
mPipParamsChangedForwarder.notifyTitleChanged( |
| 641 |
mPictureInPictureParams.getTitle()); |
| 642 |
mPipParamsChangedForwarder.notifySubtitleChanged( |
| 643 |
mPictureInPictureParams.getSubtitle()); |
| 644 |
} |
| 645 |
|
| 646 |
mPipUiEventLoggerLogger.setTaskInfo(mTaskInfo); |
| 647 |
mPipUiEventLoggerLogger.log(PipUiEventLogger.PipUiEventEnum.PICTURE_IN_PICTURE_ENTER); |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
if (info.displayId != mPipBoundsState.getDisplayId() |
| 652 |
&& mOnDisplayIdChangeCallback != null) { |
| 653 |
mOnDisplayIdChangeCallback.accept(info.displayId); |
| 654 |
} |
| 655 |
|
| 656 |
if (mPipTransitionState.getInSwipePipToHomeTransition()) { |
| 657 |
if (!mWaitForFixedRotation) { |
| 658 |
onEndOfSwipePipToHomeTransition(); |
| 659 |
} else { |
| 660 |
ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE, |
| 661 |
"%s: Defer onTaskAppeared-SwipePipToHome until end of fixed rotation.", |
| 662 |
TAG); |
| 663 |
} |
| 664 |
return; |
| 665 |
} |
| 666 |
|
| 667 |
if (mOneShotAnimationType == ANIM_TYPE_ALPHA |
| 668 |
&& SystemClock.uptimeMillis() - mLastOneShotAlphaAnimationTime |
| 669 |
> ONE_SHOT_ALPHA_ANIMATION_TIMEOUT_MS) { |
| 670 |
ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE, |
| 671 |
"%s: Alpha animation is expired. Use bounds animation.", TAG); |
| 672 |
mOneShotAnimationType = ANIM_TYPE_BOUNDS; |
| 673 |
} |
| 674 |
|
| 675 |
if (Transitions.ENABLE_SHELL_TRANSITIONS) { |
| 676 |
|
| 677 |
|
| 678 |
return; |
| 679 |
} |
| 680 |
|
| 681 |
if (mWaitForFixedRotation) { |
| 682 |
onTaskAppearedWithFixedRotation(); |
| 683 |
return; |
| 684 |
} |
| 685 |
|
| 686 |
final Rect destinationBounds = mPipBoundsAlgorithm.getEntryDestinationBounds(); |
| 687 |
Objects.requireNonNull(destinationBounds, "Missing destination bounds"); |
| 688 |
final Rect currentBounds = mTaskInfo.configuration.windowConfiguration.getBounds(); |
| 689 |
|
| 690 |
if (mOneShotAnimationType == ANIM_TYPE_BOUNDS) { |
| 691 |
mPipMenuController.attach(mLeash); |
| 692 |
final Rect sourceHintRect = PipBoundsAlgorithm.getValidSourceHintRect( |
| 693 |
info.pictureInPictureParams, currentBounds); |
| 694 |
scheduleAnimateResizePip(currentBounds, destinationBounds, 0 , |
| 695 |
sourceHintRect, TRANSITION_DIRECTION_TO_PIP, mEnterAnimationDuration, |
| 696 |
null ); |
| 697 |
mPipTransitionState.setTransitionState(PipTransitionState.ENTERING_PIP); |
| 698 |
} else if (mOneShotAnimationType == ANIM_TYPE_ALPHA) { |
| 699 |
enterPipWithAlphaAnimation(destinationBounds, mEnterAnimationDuration); |
| 700 |
mOneShotAnimationType = ANIM_TYPE_BOUNDS; |
| 701 |
} else { |
| 702 |
throw new RuntimeException("Unrecognized animation type: " + mOneShotAnimationType); |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
private void onTaskAppearedWithFixedRotation() { |
| 707 |
if (mOneShotAnimationType == ANIM_TYPE_ALPHA) { |
| 708 |
ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE, |
| 709 |
"%s: Defer entering PiP alpha animation, fixed rotation is ongoing", TAG); |
| 710 |
|
| 711 |
final SurfaceControl.Transaction tx = |
| 712 |
mSurfaceControlTransactionFactory.getTransaction(); |
| 713 |
tx.setAlpha(mLeash, 0f); |
| 714 |
tx.show(mLeash); |
| 715 |
tx.apply(); |
| 716 |
mOneShotAnimationType = ANIM_TYPE_BOUNDS; |
| 717 |
return; |
| 718 |
} |
| 719 |
final Rect currentBounds = mTaskInfo.configuration.windowConfiguration.getBounds(); |
| 720 |
final Rect sourceHintRect = PipBoundsAlgorithm.getValidSourceHintRect( |
| 721 |
mPictureInPictureParams, currentBounds); |
| 722 |
final Rect destinationBounds = mPipBoundsAlgorithm.getEntryDestinationBounds(); |
| 723 |
animateResizePip(currentBounds, destinationBounds, sourceHintRect, |
| 724 |
TRANSITION_DIRECTION_TO_PIP, mEnterAnimationDuration, 0 ); |
| 725 |
mPipTransitionState.setTransitionState(PipTransitionState.ENTERING_PIP); |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
public void onDisplayRotationSkipped() { |
| 733 |
if (isEntryScheduled()) { |
| 734 |
|
| 735 |
|
| 736 |
final Rect destinationBounds = mPipBoundsAlgorithm.getEntryDestinationBounds(); |
| 737 |
enterPipWithAlphaAnimation(destinationBounds, mEnterAnimationDuration); |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
@VisibleForTesting |
| 742 |
void enterPipWithAlphaAnimation(Rect destinationBounds, long durationMs) { |
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
final SurfaceControl.Transaction tx = |
| 747 |
mSurfaceControlTransactionFactory.getTransaction(); |
| 748 |
tx.setAlpha(mLeash, 0f); |
| 749 |
tx.apply(); |
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
final SurfaceControl.Transaction boundsChangeTx = |
| 754 |
mSurfaceControlTransactionFactory.getTransaction(); |
| 755 |
mSurfaceTransactionHelper |
| 756 |
.crop(boundsChangeTx, mLeash, destinationBounds) |
| 757 |
.round(boundsChangeTx, mLeash, true ); |
| 758 |
|
| 759 |
mPipTransitionState.setTransitionState(PipTransitionState.ENTRY_SCHEDULED); |
| 760 |
applyEnterPipSyncTransaction(destinationBounds, () -> { |
| 761 |
mPipAnimationController |
| 762 |
.getAnimator(mTaskInfo, mLeash, destinationBounds, 0f, 1f) |
| 763 |
.setTransitionDirection(TRANSITION_DIRECTION_TO_PIP) |
| 764 |
.setPipAnimationCallback(mPipAnimationCallback) |
| 765 |
.setPipTransactionHandler(mPipTransactionHandler) |
| 766 |
.setDuration(durationMs) |
| 767 |
.start(); |
| 768 |
|
| 769 |
|
| 770 |
mPipTransitionState.setTransitionState(PipTransitionState.ENTERING_PIP); |
| 771 |
}, boundsChangeTx); |
| 772 |
} |
| 773 |
|
| 774 |
private void onEndOfSwipePipToHomeTransition() { |
| 775 |
if (Transitions.ENABLE_SHELL_TRANSITIONS) { |
| 776 |
return; |
| 777 |
} |
| 778 |
|
| 779 |
final Rect destinationBounds = mPipBoundsState.getBounds(); |
| 780 |
final SurfaceControl swipeToHomeOverlay = mSwipePipToHomeOverlay; |
| 781 |
final SurfaceControl.Transaction tx = mSurfaceControlTransactionFactory.getTransaction(); |
| 782 |
mSurfaceTransactionHelper |
| 783 |
.resetScale(tx, mLeash, destinationBounds) |
| 784 |
.crop(tx, mLeash, destinationBounds) |
| 785 |
.round(tx, mLeash, isInPip()); |
| 786 |
|
| 787 |
applyEnterPipSyncTransaction(destinationBounds, () -> { |
| 788 |
|
| 789 |
finishResizeForMenu(destinationBounds); |
| 790 |
sendOnPipTransitionFinished(TRANSITION_DIRECTION_TO_PIP); |
| 791 |
|
| 792 |
|
| 793 |
if (swipeToHomeOverlay != null) { |
| 794 |
fadeOutAndRemoveOverlay(swipeToHomeOverlay, |
| 795 |
null , false ); |
| 796 |
} |
| 797 |
}, tx); |
| 798 |
mPipTransitionState.setInSwipePipToHomeTransition(false); |
| 799 |
mSwipePipToHomeOverlay = null; |
| 800 |
} |
| 801 |
|
| 802 |
private void applyEnterPipSyncTransaction(Rect destinationBounds, Runnable runnable, |
| 803 |
@Nullable SurfaceControl.Transaction boundsChangeTransaction) { |
| 804 |
|
| 805 |
|
| 806 |
mPipMenuController.attach(mLeash); |
| 807 |
final WindowContainerTransaction wct = new WindowContainerTransaction(); |
| 808 |
wct.setActivityWindowingMode(mToken, WINDOWING_MODE_UNDEFINED); |
| 809 |
wct.setBounds(mToken, destinationBounds); |
| 810 |
if (boundsChangeTransaction != null) { |
| 811 |
wct.setBoundsChangeTransaction(mToken, boundsChangeTransaction); |
| 812 |
} |
| 813 |
mSyncTransactionQueue.queue(wct); |
| 814 |
if (runnable != null) { |
| 815 |
mSyncTransactionQueue.runInSync(t -> runnable.run()); |
| 816 |
} |
| 817 |
} |
| 818 |
|
| 819 |
private void sendOnPipTransitionStarted( |
| 820 |
@PipAnimationController.TransitionDirection int direction) { |
| 821 |
if (direction == TRANSITION_DIRECTION_TO_PIP) { |
| 822 |
mPipTransitionState.setTransitionState(PipTransitionState.ENTERING_PIP); |
| 823 |
} |
| 824 |
mPipTransitionController.sendOnPipTransitionStarted(direction); |
| 825 |
} |
| 826 |
|
| 827 |
@VisibleForTesting |
| 828 |
void sendOnPipTransitionFinished( |
| 829 |
@PipAnimationController.TransitionDirection int direction) { |
| 830 |
if (direction == TRANSITION_DIRECTION_TO_PIP) { |
| 831 |
mPipTransitionState.setTransitionState(PipTransitionState.ENTERED_PIP); |
| 832 |
} |
| 833 |
mPipTransitionController.sendOnPipTransitionFinished(direction); |
| 834 |
} |
| 835 |
|
| 836 |
private void sendOnPipTransitionCancelled( |
| 837 |
@PipAnimationController.TransitionDirection int direction) { |
| 838 |
mPipTransitionController.sendOnPipTransitionCancelled(direction); |
| 839 |
} |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
@Override |
| 850 |
public void onTaskVanished(ActivityManager.RunningTaskInfo info) { |
| 851 |
if (mPipTransitionState.getTransitionState() == PipTransitionState.UNDEFINED) { |
| 852 |
return; |
| 853 |
} |
| 854 |
if (Transitions.ENABLE_SHELL_TRANSITIONS |
| 855 |
&& mPipTransitionState.getTransitionState() == PipTransitionState.EXITING_PIP) { |
| 856 |
|
| 857 |
return; |
| 858 |
} |
| 859 |
final WindowContainerToken token = info.token; |
| 860 |
Objects.requireNonNull(token, "Requires valid WindowContainerToken"); |
| 861 |
if (token.asBinder() != mToken.asBinder()) { |
| 862 |
ProtoLog.wtf(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE, |
| 863 |
"%s: Unrecognized token: %s", TAG, token); |
| 864 |
return; |
| 865 |
} |
| 866 |
|
| 867 |
cancelCurrentAnimator(); |
| 868 |
onExitPipFinished(info); |
| 869 |
|
| 870 |
if (Transitions.ENABLE_SHELL_TRANSITIONS) { |
| 871 |
mPipTransitionController.forceFinishTransition(); |
| 872 |
} |
| 873 |
} |
| 874 |
|
| 875 |
@Override |
| 876 |
public void onTaskInfoChanged(ActivityManager.RunningTaskInfo info) { |
| 877 |
Objects.requireNonNull(mToken, "onTaskInfoChanged requires valid existing mToken"); |
| 878 |
if (mPipTransitionState.getTransitionState() != PipTransitionState.ENTERED_PIP |
| 879 |
&& mPipTransitionState.getTransitionState() != PipTransitionState.EXITING_PIP) { |
| 880 |
ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE, |
| 881 |
"%s: Defer onTaskInfoChange in current state: %d", TAG, |
| 882 |
mPipTransitionState.getTransitionState()); |
| 883 |
|
| 884 |
|
| 885 |
mDeferredTaskInfo = info; |
| 886 |
return; |
| 887 |
} |
| 888 |
mPipBoundsState.setLastPipComponentName(info.topActivity); |
| 889 |
mPipBoundsState.setOverrideMinSize( |
| 890 |
mPipBoundsAlgorithm.getMinimalSize(info.topActivityInfo)); |
| 891 |
final PictureInPictureParams newParams = info.pictureInPictureParams; |
| 892 |
|
| 893 |
|
| 894 |
if (newParams == null || mPictureInPictureParams == null) { |
| 895 |
return; |
| 896 |
} |
| 897 |
applyNewPictureInPictureParams(newParams); |
| 898 |
mPictureInPictureParams = newParams; |
| 899 |
} |
| 900 |
|
| 901 |
@Override |
| 902 |
public void onFocusTaskChanged(ActivityManager.RunningTaskInfo taskInfo) { |
| 903 |
mPipMenuController.onFocusTaskChanged(taskInfo); |
| 904 |
} |
| 905 |
|
| 906 |
@Override |
| 907 |
public boolean supportCompatUI() { |
| 908 |
|
| 909 |
return false; |
| 910 |
} |
| 911 |
|
| 912 |
@Override |
| 913 |
public void attachChildSurfaceToTask(int taskId, SurfaceControl.Builder b) { |
| 914 |
b.setParent(findTaskSurface(taskId)); |
| 915 |
} |
| 916 |
|
| 917 |
@Override |
| 918 |
public void reparentChildSurfaceToTask(int taskId, SurfaceControl sc, |
| 919 |
SurfaceControl.Transaction t) { |
| 920 |
t.reparent(sc, findTaskSurface(taskId)); |
| 921 |
} |
| 922 |
|
| 923 |
private SurfaceControl findTaskSurface(int taskId) { |
| 924 |
if (mTaskInfo == null || mLeash == null || mTaskInfo.taskId != taskId) { |
| 925 |
throw new IllegalArgumentException("There is no surface for taskId=" + taskId); |
| 926 |
} |
| 927 |
return mLeash; |
| 928 |
} |
| 929 |
|
| 930 |
@Override |
| 931 |
public void onFixedRotationStarted(int displayId, int newRotation) { |
| 932 |
mNextRotation = newRotation; |
| 933 |
mWaitForFixedRotation = true; |
| 934 |
|
| 935 |
if (Transitions.ENABLE_SHELL_TRANSITIONS) { |
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
mPipTransitionController.onFixedRotationStarted(); |
| 942 |
return; |
| 943 |
} |
| 944 |
|
| 945 |
if (mPipTransitionState.isInPip()) { |
| 946 |
|
| 947 |
fadeExistingPip(false ); |
| 948 |
} |
| 949 |
} |
| 950 |
|
| 951 |
@Override |
| 952 |
public void onFixedRotationFinished(int displayId) { |
| 953 |
if (!mWaitForFixedRotation) { |
| 954 |
return; |
| 955 |
} |
| 956 |
if (Transitions.ENABLE_SHELL_TRANSITIONS) { |
| 957 |
clearWaitForFixedRotation(); |
| 958 |
return; |
| 959 |
} |
| 960 |
if (mPipTransitionState.getTransitionState() == PipTransitionState.TASK_APPEARED) { |
| 961 |
if (mPipTransitionState.getInSwipePipToHomeTransition()) { |
| 962 |
onEndOfSwipePipToHomeTransition(); |
| 963 |
} else { |
| 964 |
|
| 965 |
enterPipWithAlphaAnimation(mPipBoundsAlgorithm.getEntryDestinationBounds(), |
| 966 |
mEnterAnimationDuration); |
| 967 |
} |
| 968 |
} else if (mPipTransitionState.getTransitionState() == PipTransitionState.ENTERED_PIP |
| 969 |
&& mHasFadeOut) { |
| 970 |
fadeExistingPip(true ); |
| 971 |
} else if (mPipTransitionState.getTransitionState() == PipTransitionState.ENTERING_PIP |
| 972 |
&& mDeferredAnimEndTransaction != null) { |
| 973 |
final PipAnimationController.PipTransitionAnimator<?> animator = |
| 974 |
mPipAnimationController.getCurrentAnimator(); |
| 975 |
final Rect destinationBounds = animator.getDestinationBounds(); |
| 976 |
mPipBoundsState.setBounds(destinationBounds); |
| 977 |
applyEnterPipSyncTransaction(destinationBounds, () -> { |
| 978 |
finishResizeForMenu(destinationBounds); |
| 979 |
sendOnPipTransitionFinished(TRANSITION_DIRECTION_TO_PIP); |
| 980 |
}, mDeferredAnimEndTransaction); |
| 981 |
} |
| 982 |
clearWaitForFixedRotation(); |
| 983 |
} |
| 984 |
|
| 985 |
|
| 986 |
void onExitPipFinished(TaskInfo info) { |
| 987 |
if (mLeash == null) { |
| 988 |
|
| 989 |
Log.w(TAG, "Warning, onExitPipFinished() called multiple times in the same sessino"); |
| 990 |
return; |
| 991 |
} |
| 992 |
|
| 993 |
clearWaitForFixedRotation(); |
| 994 |
if (mSwipePipToHomeOverlay != null) { |
| 995 |
removeContentOverlay(mSwipePipToHomeOverlay, null ); |
| 996 |
mSwipePipToHomeOverlay = null; |
| 997 |
} |
| 998 |
resetShadowRadius(); |
| 999 |
mPipTransitionState.setInSwipePipToHomeTransition(false); |
| 1000 |
mPictureInPictureParams = null; |
| 1001 |
mPipTransitionState.setTransitionState(PipTransitionState.UNDEFINED); |
| 1002 |
|
| 1003 |
mPipBoundsState.setBounds(new Rect()); |
| 1004 |
mPipUiEventLoggerLogger.setTaskInfo(null); |
| 1005 |
mPipMenuController.detach(); |
| 1006 |
mLeash = null; |
| 1007 |
|
| 1008 |
if (info.displayId != Display.DEFAULT_DISPLAY && mOnDisplayIdChangeCallback != null) { |
| 1009 |
mOnDisplayIdChangeCallback.accept(Display.DEFAULT_DISPLAY); |
| 1010 |
} |
| 1011 |
} |
| 1012 |
|
| 1013 |
private void fadeExistingPip(boolean show) { |
| 1014 |
if (mLeash == null || !mLeash.isValid()) { |
| 1015 |
ProtoLog.w(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE, |
| 1016 |
"%s: Invalid leash on fadeExistingPip: %s", TAG, mLeash); |
| 1017 |
return; |
| 1018 |
} |
| 1019 |
final float alphaStart = show ? 0 : 1; |
| 1020 |
final float alphaEnd = show ? 1 : 0; |
| 1021 |
mPipAnimationController |
| 1022 |
.getAnimator(mTaskInfo, mLeash, mPipBoundsState.getBounds(), alphaStart, alphaEnd) |
| 1023 |
.setTransitionDirection(TRANSITION_DIRECTION_SAME) |
| 1024 |
.setPipTransactionHandler(mPipTransactionHandler) |
| 1025 |
.setDuration(show ? mEnterAnimationDuration : mExitAnimationDuration) |
| 1026 |
.start(); |
| 1027 |
mHasFadeOut = !show; |
| 1028 |
} |
| 1029 |
|
| 1030 |
private void clearWaitForFixedRotation() { |
| 1031 |
mWaitForFixedRotation = false; |
| 1032 |
mDeferredAnimEndTransaction = null; |
| 1033 |
} |
| 1034 |
|
| 1035 |
|
| 1036 |
public void setPipVisibility(boolean visible) { |
| 1037 |
if (!isInPip()) { |
| 1038 |
return; |
| 1039 |
} |
| 1040 |
if (mLeash == null || !mLeash.isValid()) { |
| 1041 |
ProtoLog.w(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE, |
| 1042 |
"%s: Invalid leash on setPipVisibility: %s", TAG, mLeash); |
| 1043 |
return; |
| 1044 |
} |
| 1045 |
final SurfaceControl.Transaction tx = |
| 1046 |
mSurfaceControlTransactionFactory.getTransaction(); |
| 1047 |
mSurfaceTransactionHelper.alpha(tx, mLeash, visible ? 1f : 0f); |
| 1048 |
tx.apply(); |
| 1049 |
} |
| 1050 |
|
| 1051 |
@Override |
| 1052 |
public void onDisplayConfigurationChanged(int displayId, Configuration newConfig) { |
| 1053 |
mCurrentRotation = newConfig.windowConfiguration.getRotation(); |
| 1054 |
} |
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
public void onDensityOrFontScaleChanged(Context context) { |
| 1060 |
mSurfaceTransactionHelper.onDensityOrFontScaleChanged(context); |
| 1061 |
} |
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
@SuppressWarnings("unchecked") |
| 1069 |
public void onMovementBoundsChanged(Rect destinationBoundsOut, boolean fromRotation, |
| 1070 |
boolean fromImeAdjustment, boolean fromShelfAdjustment, |
| 1071 |
WindowContainerTransaction wct) { |
| 1072 |
|
| 1073 |
|
| 1074 |
final boolean waitForFixedRotationOnEnteringPip = mWaitForFixedRotation |
| 1075 |
&& (mPipTransitionState.getTransitionState() != PipTransitionState.ENTERED_PIP); |
| 1076 |
if ((mPipTransitionState.getInSwipePipToHomeTransition() |
| 1077 |
|| waitForFixedRotationOnEnteringPip) && fromRotation) { |
| 1078 |
if (DEBUG) { |
| 1079 |
ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE, |
| 1080 |
"%s: Skip onMovementBoundsChanged on rotation change" |
| 1081 |
+ " InSwipePipToHomeTransition=%b" |
| 1082 |
+ " mWaitForFixedRotation=%b" |
| 1083 |
+ " getTransitionState=%d", TAG, |
| 1084 |
mPipTransitionState.getInSwipePipToHomeTransition(), mWaitForFixedRotation, |
| 1085 |
mPipTransitionState.getTransitionState()); |
| 1086 |
} |
| 1087 |
return; |
| 1088 |
} |
| 1089 |
final PipAnimationController.PipTransitionAnimator animator = |
| 1090 |
mPipAnimationController.getCurrentAnimator(); |
| 1091 |
if (animator == null || !animator.isRunning() |
| 1092 |
|| animator.getTransitionDirection() != TRANSITION_DIRECTION_TO_PIP) { |
| 1093 |
final boolean rotatingPip = mPipTransitionState.isInPip() && fromRotation; |
| 1094 |
if (rotatingPip && Transitions.ENABLE_SHELL_TRANSITIONS) { |
| 1095 |
|
| 1096 |
mPipBoundsState.setBounds(destinationBoundsOut); |
| 1097 |
} else if (rotatingPip && mWaitForFixedRotation && mHasFadeOut) { |
| 1098 |
|
| 1099 |
mPipBoundsState.setBounds(destinationBoundsOut); |
| 1100 |
} else if (rotatingPip) { |
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
mPipBoundsState.setBounds(destinationBoundsOut); |
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
int direction = TRANSITION_DIRECTION_NONE; |
| 1109 |
if (animator != null) { |
| 1110 |
direction = animator.getTransitionDirection(); |
| 1111 |
PipAnimationController.quietCancel(animator); |
| 1112 |
|
| 1113 |
sendOnPipTransitionCancelled(direction); |
| 1114 |
sendOnPipTransitionFinished(direction); |
| 1115 |
} |
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
final SurfaceControl.Transaction tx = createFinishResizeSurfaceTransaction( |
| 1120 |
destinationBoundsOut); |
| 1121 |
prepareFinishResizeTransaction(destinationBoundsOut, direction, tx, wct); |
| 1122 |
} else { |
| 1123 |
|
| 1124 |
|
| 1125 |
if (animator != null && animator.isRunning()) { |
| 1126 |
if (!animator.getDestinationBounds().isEmpty()) { |
| 1127 |
destinationBoundsOut.set(animator.getDestinationBounds()); |
| 1128 |
} |
| 1129 |
} else { |
| 1130 |
if (!mPipBoundsState.getBounds().isEmpty()) { |
| 1131 |
destinationBoundsOut.set(mPipBoundsState.getBounds()); |
| 1132 |
} |
| 1133 |
} |
| 1134 |
} |
| 1135 |
return; |
| 1136 |
} |
| 1137 |
|
| 1138 |
final Rect currentDestinationBounds = animator.getDestinationBounds(); |
| 1139 |
destinationBoundsOut.set(currentDestinationBounds); |
| 1140 |
if (!fromImeAdjustment && !fromShelfAdjustment |
| 1141 |
&& mPipBoundsState.getDisplayBounds().contains(currentDestinationBounds)) { |
| 1142 |
|
| 1143 |
return; |
| 1144 |
} |
| 1145 |
|
| 1146 |
final Rect newDestinationBounds = mPipBoundsAlgorithm.getEntryDestinationBounds(); |
| 1147 |
if (newDestinationBounds.equals(currentDestinationBounds)) return; |
| 1148 |
if (animator.getAnimationType() == ANIM_TYPE_BOUNDS) { |
| 1149 |
if (mWaitForFixedRotation) { |
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
final Rect displayBounds = mPipBoundsState.getDisplayBounds(); |
| 1154 |
final Rect rotatedEndBounds = new Rect(newDestinationBounds); |
| 1155 |
rotateBounds(rotatedEndBounds, displayBounds, mNextRotation, mCurrentRotation); |
| 1156 |
animator.updateEndValue(rotatedEndBounds); |
| 1157 |
} else { |
| 1158 |
animator.updateEndValue(newDestinationBounds); |
| 1159 |
} |
| 1160 |
} |
| 1161 |
animator.setDestinationBounds(newDestinationBounds); |
| 1162 |
destinationBoundsOut.set(newDestinationBounds); |
| 1163 |
} |
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
protected void applyNewPictureInPictureParams(@NonNull PictureInPictureParams params) { |
| 1169 |
if (mDeferredTaskInfo != null || PipUtils.aspectRatioChanged(params.getAspectRatioFloat(), |
| 1170 |
mPictureInPictureParams.getAspectRatioFloat())) { |
| 1171 |
mPipParamsChangedForwarder.notifyAspectRatioChanged(params.getAspectRatioFloat()); |
| 1172 |
} |
| 1173 |
if (mDeferredTaskInfo != null |
| 1174 |
|| PipUtils.remoteActionsChanged(params.getActions(), |
| 1175 |
mPictureInPictureParams.getActions()) |
| 1176 |
|| !PipUtils.remoteActionsMatch(params.getCloseAction(), |
| 1177 |
mPictureInPictureParams.getCloseAction())) { |
| 1178 |
mPipParamsChangedForwarder.notifyActionsChanged(params.getActions(), |
| 1179 |
params.getCloseAction()); |
| 1180 |
} |
| 1181 |
} |
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
public void scheduleAnimateResizePip(Rect toBounds, int duration, |
| 1187 |
Consumer<Rect> updateBoundsCallback) { |
| 1188 |
scheduleAnimateResizePip(toBounds, duration, TRANSITION_DIRECTION_NONE, |
| 1189 |
updateBoundsCallback); |
| 1190 |
} |
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
public void scheduleAnimateResizePip(Rect toBounds, int duration, |
| 1196 |
@PipAnimationController.TransitionDirection int direction, |
| 1197 |
Consumer<Rect> updateBoundsCallback) { |
| 1198 |
if (mWaitForFixedRotation) { |
| 1199 |
ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE, |
| 1200 |
"%s: skip scheduleAnimateResizePip, entering pip deferred", TAG); |
| 1201 |
return; |
| 1202 |
} |
| 1203 |
scheduleAnimateResizePip(mPipBoundsState.getBounds(), toBounds, 0 , |
| 1204 |
null , direction, duration, updateBoundsCallback); |
| 1205 |
} |
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
public void scheduleAnimateResizePip(Rect fromBounds, Rect toBounds, int duration, |
| 1212 |
float startingAngle, Consumer<Rect> updateBoundsCallback) { |
| 1213 |
if (mWaitForFixedRotation) { |
| 1214 |
ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE, |
| 1215 |
"%s: skip scheduleAnimateResizePip, entering pip deferred", TAG); |
| 1216 |
return; |
| 1217 |
} |
| 1218 |
scheduleAnimateResizePip(fromBounds, toBounds, startingAngle, null , |
| 1219 |
TRANSITION_DIRECTION_SNAP_AFTER_RESIZE, duration, updateBoundsCallback); |
| 1220 |
} |
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
private @Nullable PipAnimationController.PipTransitionAnimator<?> scheduleAnimateResizePip( |
| 1227 |
Rect currentBounds, Rect destinationBounds, float startingAngle, Rect sourceHintRect, |
| 1228 |
@PipAnimationController.TransitionDirection int direction, int durationMs, |
| 1229 |
Consumer<Rect> updateBoundsCallback) { |
| 1230 |
if (!mPipTransitionState.isInPip()) { |
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
return null; |
| 1235 |
} |
| 1236 |
|
| 1237 |
final PipAnimationController.PipTransitionAnimator<?> animator = animateResizePip( |
| 1238 |
currentBounds, destinationBounds, sourceHintRect, direction, durationMs, |
| 1239 |
startingAngle); |
| 1240 |
if (updateBoundsCallback != null) { |
| 1241 |
updateBoundsCallback.accept(destinationBounds); |
| 1242 |
} |
| 1243 |
return animator; |
| 1244 |
} |
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
public void scheduleResizePip(Rect toBounds, Consumer<Rect> updateBoundsCallback) { |
| 1251 |
|
| 1252 |
if (mToken == null || mLeash == null) { |
| 1253 |
ProtoLog.w(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE, |
| 1254 |
"%s: Abort animation, invalid leash", TAG); |
| 1255 |
return; |
| 1256 |
} |
| 1257 |
mPipBoundsState.setBounds(toBounds); |
| 1258 |
final SurfaceControl.Transaction tx = mSurfaceControlTransactionFactory.getTransaction(); |
| 1259 |
mSurfaceTransactionHelper |
| 1260 |
.crop(tx, mLeash, toBounds) |
| 1261 |
.round(tx, mLeash, mPipTransitionState.isInPip()); |
| 1262 |
if (shouldSyncPipTransactionWithMenu()) { |
| 1263 |
mPipMenuController.resizePipMenu(mLeash, tx, toBounds); |
| 1264 |
} else { |
| 1265 |
tx.apply(); |
| 1266 |
} |
| 1267 |
if (updateBoundsCallback != null) { |
| 1268 |
updateBoundsCallback.accept(toBounds); |
| 1269 |
} |
| 1270 |
} |
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
public void scheduleUserResizePip(Rect startBounds, Rect toBounds, |
| 1277 |
Consumer<Rect> updateBoundsCallback) { |
| 1278 |
scheduleUserResizePip(startBounds, toBounds, 0 , updateBoundsCallback); |
| 1279 |
} |
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
public void scheduleUserResizePip(Rect startBounds, Rect toBounds, float degrees, |
| 1286 |
Consumer<Rect> updateBoundsCallback) { |
| 1287 |
|
| 1288 |
if (mToken == null || mLeash == null) { |
| 1289 |
ProtoLog.w(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE, |
| 1290 |
"%s: Abort animation, invalid leash", TAG); |
| 1291 |
return; |
| 1292 |
} |
| 1293 |
|
| 1294 |
if (startBounds.isEmpty() || toBounds.isEmpty()) { |
| 1295 |
ProtoLog.w(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE, |
| 1296 |
"%s: Attempted to user resize PIP to or from empty bounds, aborting.", TAG); |
| 1297 |
return; |
| 1298 |
} |
| 1299 |
|
| 1300 |
final SurfaceControl.Transaction tx = mSurfaceControlTransactionFactory.getTransaction(); |
| 1301 |
mSurfaceTransactionHelper |
| 1302 |
.scale(tx, mLeash, startBounds, toBounds, degrees) |
| 1303 |
.round(tx, mLeash, startBounds, toBounds); |
| 1304 |
if (shouldSyncPipTransactionWithMenu()) { |
| 1305 |
mPipMenuController.movePipMenu(mLeash, tx, toBounds); |
| 1306 |
} else { |
| 1307 |
tx.apply(); |
| 1308 |
} |
| 1309 |
if (updateBoundsCallback != null) { |
| 1310 |
updateBoundsCallback.accept(toBounds); |
| 1311 |
} |
| 1312 |
} |
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
public void scheduleFinishResizePip(Rect destinationBounds) { |
| 1319 |
scheduleFinishResizePip(destinationBounds, null ); |
| 1320 |
} |
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
public void scheduleFinishResizePip(Rect destinationBounds, |
| 1326 |
Consumer<Rect> updateBoundsCallback) { |
| 1327 |
scheduleFinishResizePip(destinationBounds, TRANSITION_DIRECTION_NONE, updateBoundsCallback); |
| 1328 |
} |
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
public void scheduleFinishResizePip(Rect destinationBounds, |
| 1339 |
@PipAnimationController.TransitionDirection int direction, |
| 1340 |
Consumer<Rect> updateBoundsCallback) { |
| 1341 |
if (mPipTransitionState.shouldBlockResizeRequest()) { |
| 1342 |
return; |
| 1343 |
} |
| 1344 |
|
| 1345 |
if (mLeash == null || !mLeash.isValid()) { |
| 1346 |
Log.e(TAG, String.format("scheduleFinishResizePip with null leash! mState=%d", |
| 1347 |
mPipTransitionState.getTransitionState())); |
| 1348 |
return; |
| 1349 |
} |
| 1350 |
|
| 1351 |
finishResize(createFinishResizeSurfaceTransaction(destinationBounds), destinationBounds, |
| 1352 |
direction, -1); |
| 1353 |
if (updateBoundsCallback != null) { |
| 1354 |
updateBoundsCallback.accept(destinationBounds); |
| 1355 |
} |
| 1356 |
} |
| 1357 |
|
| 1358 |
private SurfaceControl.Transaction createFinishResizeSurfaceTransaction( |
| 1359 |
Rect destinationBounds) { |
| 1360 |
final SurfaceControl.Transaction tx = mSurfaceControlTransactionFactory.getTransaction(); |
| 1361 |
mSurfaceTransactionHelper |
| 1362 |
.crop(tx, mLeash, destinationBounds) |
| 1363 |
.resetScale(tx, mLeash, destinationBounds) |
| 1364 |
.round(tx, mLeash, mPipTransitionState.isInPip()); |
| 1365 |
return tx; |
| 1366 |
} |
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
public void scheduleOffsetPip(Rect originalBounds, int offset, int duration, |
| 1372 |
Consumer<Rect> updateBoundsCallback) { |
| 1373 |
if (mPipTransitionState.shouldBlockResizeRequest() |
| 1374 |
|| mPipTransitionState.getInSwipePipToHomeTransition()) { |
| 1375 |
return; |
| 1376 |
} |
| 1377 |
if (mWaitForFixedRotation) { |
| 1378 |
ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE, |
| 1379 |
"%s: skip scheduleOffsetPip, entering pip deferred", TAG); |
| 1380 |
return; |
| 1381 |
} |
| 1382 |
offsetPip(originalBounds, 0 , offset, duration); |
| 1383 |
Rect toBounds = new Rect(originalBounds); |
| 1384 |
toBounds.offset(0, offset); |
| 1385 |
if (updateBoundsCallback != null) { |
| 1386 |
updateBoundsCallback.accept(toBounds); |
| 1387 |
} |
| 1388 |
} |
| 1389 |
|
| 1390 |
private void offsetPip(Rect originalBounds, int xOffset, int yOffset, int durationMs) { |
| 1391 |
if (mTaskInfo == null) { |
| 1392 |
ProtoLog.w(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE, "%s: mTaskInfo is not set", |
| 1393 |
TAG); |
| 1394 |
return; |
| 1395 |
} |
| 1396 |
final Rect destinationBounds = new Rect(originalBounds); |
| 1397 |
destinationBounds.offset(xOffset, yOffset); |
| 1398 |
animateResizePip(originalBounds, destinationBounds, null , |
| 1399 |
TRANSITION_DIRECTION_SAME, durationMs, 0); |
| 1400 |
} |
| 1401 |
|
| 1402 |
private void finishResize(SurfaceControl.Transaction tx, Rect destinationBounds, |
| 1403 |
@PipAnimationController.TransitionDirection int direction, |
| 1404 |
@PipAnimationController.AnimationType int type) { |
| 1405 |
final Rect preResizeBounds = new Rect(mPipBoundsState.getBounds()); |
| 1406 |
final boolean isPipTopLeft = isPipTopLeft(); |
| 1407 |
mPipBoundsState.setBounds(destinationBounds); |
| 1408 |
if (direction == TRANSITION_DIRECTION_REMOVE_STACK) { |
| 1409 |
removePipImmediately(); |
| 1410 |
return; |
| 1411 |
} else if (isInPipDirection(direction) && type == ANIM_TYPE_ALPHA) { |
| 1412 |
|
| 1413 |
finishResizeForMenu(destinationBounds); |
| 1414 |
return; |
| 1415 |
} |
| 1416 |
|
| 1417 |
WindowContainerTransaction wct = new WindowContainerTransaction(); |
| 1418 |
prepareFinishResizeTransaction(destinationBounds, direction, tx, wct); |
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
final boolean mayAnimateFinishResize = direction == TRANSITION_DIRECTION_USER_RESIZE |
| 1423 |
|| direction == TRANSITION_DIRECTION_SNAP_AFTER_RESIZE |
| 1424 |
|| direction == TRANSITION_DIRECTION_EXPAND_OR_UNEXPAND; |
| 1425 |
|
| 1426 |
final boolean animateCrossFadeResize = mayAnimateFinishResize |
| 1427 |
&& mPictureInPictureParams != null |
| 1428 |
&& !mPictureInPictureParams.isSeamlessResizeEnabled(); |
| 1429 |
if (animateCrossFadeResize) { |
| 1430 |
|
| 1431 |
|
| 1432 |
preResizeBounds.offsetTo(0, 0); |
| 1433 |
final Rect snapshotDest = new Rect(0, 0, destinationBounds.width(), |
| 1434 |
destinationBounds.height()); |
| 1435 |
|
| 1436 |
|
| 1437 |
final SurfaceControl snapshotSurface = ScreenshotUtils.takeScreenshot( |
| 1438 |
mSurfaceControlTransactionFactory.getTransaction(), mLeash, preResizeBounds, |
| 1439 |
Integer.MAX_VALUE - 2); |
| 1440 |
if (snapshotSurface != null) { |
| 1441 |
mSyncTransactionQueue.queue(wct); |
| 1442 |
mSyncTransactionQueue.runInSync(t -> { |
| 1443 |
|
| 1444 |
mSurfaceTransactionHelper.scale(t, snapshotSurface, preResizeBounds, |
| 1445 |
snapshotDest); |
| 1446 |
|
| 1447 |
|
| 1448 |
fadeOutAndRemoveOverlay(snapshotSurface, |
| 1449 |
null , false ); |
| 1450 |
}); |
| 1451 |
} else { |
| 1452 |
applyFinishBoundsResize(wct, direction, isPipTopLeft); |
| 1453 |
} |
| 1454 |
} else { |
| 1455 |
applyFinishBoundsResize(wct, direction, isPipTopLeft); |
| 1456 |
} |
| 1457 |
|
| 1458 |
finishResizeForMenu(destinationBounds); |
| 1459 |
} |
| 1460 |
|
| 1461 |
|
| 1462 |
public void finishResizeForMenu(Rect destinationBounds) { |
| 1463 |
if (!isInPip()) { |
| 1464 |
return; |
| 1465 |
} |
| 1466 |
mPipMenuController.movePipMenu(null, null, destinationBounds); |
| 1467 |
mPipMenuController.updateMenuBounds(destinationBounds); |
| 1468 |
} |
| 1469 |
|
| 1470 |
private void prepareFinishResizeTransaction(Rect destinationBounds, |
| 1471 |
@PipAnimationController.TransitionDirection int direction, |
| 1472 |
SurfaceControl.Transaction tx, |
| 1473 |
WindowContainerTransaction wct) { |
| 1474 |
final Rect taskBounds; |
| 1475 |
if (isInPipDirection(direction)) { |
| 1476 |
|
| 1477 |
|
| 1478 |
taskBounds = destinationBounds; |
| 1479 |
wct.setActivityWindowingMode(mToken, WINDOWING_MODE_UNDEFINED); |
| 1480 |
} else if (isOutPipDirection(direction)) { |
| 1481 |
|
| 1482 |
|
| 1483 |
taskBounds = null; |
| 1484 |
applyWindowingModeChangeOnExit(wct, direction); |
| 1485 |
} else { |
| 1486 |
|
| 1487 |
taskBounds = destinationBounds; |
| 1488 |
} |
| 1489 |
mSurfaceTransactionHelper.round(tx, mLeash, isInPip()); |
| 1490 |
|
| 1491 |
wct.setBounds(mToken, taskBounds); |
| 1492 |
wct.setBoundsChangeTransaction(mToken, tx); |
| 1493 |
} |
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
public void applyFinishBoundsResize(@NonNull WindowContainerTransaction wct, |
| 1503 |
@PipAnimationController.TransitionDirection int direction, boolean wasPipTopLeft) { |
| 1504 |
if (direction == TRANSITION_DIRECTION_LEAVE_PIP_TO_SPLIT_SCREEN) { |
| 1505 |
mSplitScreenOptional.ifPresent(splitScreenController -> |
| 1506 |
splitScreenController.enterSplitScreen(mTaskInfo.taskId, wasPipTopLeft, wct)); |
| 1507 |
} else { |
| 1508 |
mTaskOrganizer.applyTransaction(wct); |
| 1509 |
} |
| 1510 |
} |
| 1511 |
|
| 1512 |
private boolean isPipTopLeft() { |
| 1513 |
if (!mSplitScreenOptional.isPresent()) { |
| 1514 |
return false; |
| 1515 |
} |
| 1516 |
final Rect topLeft = new Rect(); |
| 1517 |
final Rect bottomRight = new Rect(); |
| 1518 |
mSplitScreenOptional.get().getStageBounds(topLeft, bottomRight); |
| 1519 |
|
| 1520 |
return topLeft.contains(mPipBoundsState.getBounds()); |
| 1521 |
} |
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
public int getOutPipWindowingMode() { |
| 1528 |
|
| 1529 |
return WINDOWING_MODE_UNDEFINED; |
| 1530 |
} |
| 1531 |
|
| 1532 |
private @Nullable PipAnimationController.PipTransitionAnimator<?> animateResizePip( |
| 1533 |
Rect currentBounds, Rect destinationBounds, Rect sourceHintRect, |
| 1534 |
@PipAnimationController.TransitionDirection int direction, int durationMs, |
| 1535 |
float startingAngle) { |
| 1536 |
|
| 1537 |
if (mToken == null || mLeash == null) { |
| 1538 |
ProtoLog.w(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE, |
| 1539 |
"%s: Abort animation, invalid leash", TAG); |
| 1540 |
return null; |
| 1541 |
} |
| 1542 |
if (isInPipDirection(direction) |
| 1543 |
&& !isSourceRectHintValidForEnterPip(sourceHintRect, destinationBounds)) { |
| 1544 |
|
| 1545 |
sourceHintRect = null; |
| 1546 |
} |
| 1547 |
final int rotationDelta = mWaitForFixedRotation |
| 1548 |
? deltaRotation(mCurrentRotation, mNextRotation) |
| 1549 |
: Surface.ROTATION_0; |
| 1550 |
if (rotationDelta != Surface.ROTATION_0) { |
| 1551 |
sourceHintRect = computeRotatedBounds(rotationDelta, direction, destinationBounds, |
| 1552 |
sourceHintRect); |
| 1553 |
} |
| 1554 |
Rect baseBounds = direction == TRANSITION_DIRECTION_SNAP_AFTER_RESIZE |
| 1555 |
? mPipBoundsState.getBounds() : currentBounds; |
| 1556 |
final boolean existingAnimatorRunning = mPipAnimationController.getCurrentAnimator() != null |
| 1557 |
&& mPipAnimationController.getCurrentAnimator().isRunning(); |
| 1558 |
final PipAnimationController.PipTransitionAnimator<?> animator = mPipAnimationController |
| 1559 |
.getAnimator(mTaskInfo, mLeash, baseBounds, currentBounds, destinationBounds, |
| 1560 |
sourceHintRect, direction, startingAngle, rotationDelta); |
| 1561 |
animator.setTransitionDirection(direction) |
| 1562 |
.setPipTransactionHandler(mPipTransactionHandler) |
| 1563 |
.setDuration(durationMs); |
| 1564 |
if (!existingAnimatorRunning) { |
| 1565 |
animator.setPipAnimationCallback(mPipAnimationCallback); |
| 1566 |
} |
| 1567 |
if (isInPipDirection(direction)) { |
| 1568 |
|
| 1569 |
|
| 1570 |
if (sourceHintRect == null) { |
| 1571 |
animator.setColorContentOverlay(mContext); |
| 1572 |
} else { |
| 1573 |
final TaskSnapshot snapshot = PipUtils.getTaskSnapshot( |
| 1574 |
mTaskInfo.launchIntoPipHostTaskId, false ); |
| 1575 |
if (snapshot != null) { |
| 1576 |
|
| 1577 |
|
| 1578 |
animator.setSnapshotContentOverlay(snapshot, sourceHintRect); |
| 1579 |
} |
| 1580 |
} |
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
if (rotationDelta != Surface.ROTATION_0) { |
| 1586 |
animator.setDestinationBounds(mPipBoundsAlgorithm.getEntryDestinationBounds()); |
| 1587 |
} |
| 1588 |
} |
| 1589 |
animator.start(); |
| 1590 |
return animator; |
| 1591 |
} |
| 1592 |
|
| 1593 |
|
| 1594 |
private @Nullable Rect computeRotatedBounds(int rotationDelta, int direction, |
| 1595 |
Rect outDestinationBounds, Rect sourceHintRect) { |
| 1596 |
if (direction == TRANSITION_DIRECTION_TO_PIP) { |
| 1597 |
mPipBoundsState.getDisplayLayout().rotateTo(mContext.getResources(), mNextRotation); |
| 1598 |
final Rect displayBounds = mPipBoundsState.getDisplayBounds(); |
| 1599 |
outDestinationBounds.set(mPipBoundsAlgorithm.getEntryDestinationBounds()); |
| 1600 |
|
| 1601 |
rotateBounds(outDestinationBounds, displayBounds, mNextRotation, mCurrentRotation); |
| 1602 |
|
| 1603 |
|
| 1604 |
if (sourceHintRect != null && mTaskInfo.displayCutoutInsets != null) { |
| 1605 |
if (rotationDelta == Surface.ROTATION_270) { |
| 1606 |
sourceHintRect.offset(mTaskInfo.displayCutoutInsets.left, |
| 1607 |
mTaskInfo.displayCutoutInsets.top); |
| 1608 |
} |
| 1609 |
} |
| 1610 |
} else if (direction == TRANSITION_DIRECTION_LEAVE_PIP) { |
| 1611 |
final Rect rotatedDestinationBounds = new Rect(outDestinationBounds); |
| 1612 |
rotateBounds(rotatedDestinationBounds, mPipBoundsState.getDisplayBounds(), |
| 1613 |
rotationDelta); |
| 1614 |
return PipBoundsAlgorithm.getValidSourceHintRect(mPictureInPictureParams, |
| 1615 |
rotatedDestinationBounds); |
| 1616 |
} |
| 1617 |
return sourceHintRect; |
| 1618 |
} |
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
private boolean isSourceRectHintValidForEnterPip(Rect sourceRectHint, Rect destinationBounds) { |
| 1629 |
return sourceRectHint != null |
| 1630 |
&& sourceRectHint.width() > destinationBounds.width() |
| 1631 |
&& sourceRectHint.height() > destinationBounds.height(); |
| 1632 |
} |
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
private boolean syncWithSplitScreenBounds(Rect destinationBoundsOut, boolean enterSplit) { |
| 1642 |
if (!enterSplit || !mSplitScreenOptional.isPresent()) { |
| 1643 |
return false; |
| 1644 |
} |
| 1645 |
final Rect topLeft = new Rect(); |
| 1646 |
final Rect bottomRight = new Rect(); |
| 1647 |
mSplitScreenOptional.get().getStageBounds(topLeft, bottomRight); |
| 1648 |
final boolean isPipTopLeft = isPipTopLeft(); |
| 1649 |
destinationBoundsOut.set(isPipTopLeft ? topLeft : bottomRight); |
| 1650 |
return true; |
| 1651 |
} |
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
void fadeOutAndRemoveOverlay(SurfaceControl surface, Runnable callback, |
| 1657 |
boolean withStartDelay) { |
| 1658 |
if (surface == null || !surface.isValid()) { |
| 1659 |
return; |
| 1660 |
} |
| 1661 |
|
| 1662 |
final ValueAnimator animator = ValueAnimator.ofFloat(1.0f, 0.0f); |
| 1663 |
animator.setDuration(mCrossFadeAnimationDuration); |
| 1664 |
animator.addUpdateListener(animation -> { |
| 1665 |
if (mPipTransitionState.getTransitionState() == PipTransitionState.UNDEFINED) { |
| 1666 |
|
| 1667 |
|
| 1668 |
ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE, |
| 1669 |
"%s: Task vanished, skip fadeOutAndRemoveOverlay", TAG); |
| 1670 |
PipAnimationController.quietCancel(animation); |
| 1671 |
} else if (surface.isValid()) { |
| 1672 |
final float alpha = (float) animation.getAnimatedValue(); |
| 1673 |
final SurfaceControl.Transaction transaction = |
| 1674 |
mSurfaceControlTransactionFactory.getTransaction(); |
| 1675 |
transaction.setAlpha(surface, alpha); |
| 1676 |
transaction.apply(); |
| 1677 |
} |
| 1678 |
}); |
| 1679 |
animator.addListener(new AnimatorListenerAdapter() { |
| 1680 |
@Override |
| 1681 |
public void onAnimationEnd(Animator animation) { |
| 1682 |
removeContentOverlay(surface, callback); |
| 1683 |
} |
| 1684 |
}); |
| 1685 |
animator.setStartDelay(withStartDelay ? CONTENT_OVERLAY_FADE_OUT_DELAY_MS : 0); |
| 1686 |
animator.start(); |
| 1687 |
} |
| 1688 |
|
| 1689 |
private void removeContentOverlay(SurfaceControl surface, Runnable callback) { |
| 1690 |
if (mPipTransitionState.getTransitionState() == PipTransitionState.UNDEFINED) { |
| 1691 |
|
| 1692 |
return; |
| 1693 |
} |
| 1694 |
if (surface == null || !surface.isValid()) { |
| 1695 |
ProtoLog.w(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE, |
| 1696 |
"%s: trying to remove invalid content overlay (%s)", TAG, surface); |
| 1697 |
return; |
| 1698 |
} |
| 1699 |
final SurfaceControl.Transaction tx = mSurfaceControlTransactionFactory.getTransaction(); |
| 1700 |
tx.remove(surface); |
| 1701 |
tx.apply(); |
| 1702 |
if (callback != null) callback.run(); |
| 1703 |
} |
| 1704 |
|
| 1705 |
private void resetShadowRadius() { |
| 1706 |
if (mPipTransitionState.getTransitionState() == PipTransitionState.UNDEFINED) { |
| 1707 |
|
| 1708 |
return; |
| 1709 |
} |
| 1710 |
final SurfaceControl.Transaction tx = mSurfaceControlTransactionFactory.getTransaction(); |
| 1711 |
tx.setShadowRadius(mLeash, 0f); |
| 1712 |
tx.apply(); |
| 1713 |
} |
| 1714 |
|
| 1715 |
private void cancelCurrentAnimator() { |
| 1716 |
final PipAnimationController.PipTransitionAnimator<?> animator = |
| 1717 |
mPipAnimationController.getCurrentAnimator(); |
| 1718 |
if (animator != null) { |
| 1719 |
if (animator.getContentOverlayLeash() != null) { |
| 1720 |
removeContentOverlay(animator.getContentOverlayLeash(), |
| 1721 |
animator::clearContentOverlay); |
| 1722 |
} |
| 1723 |
PipAnimationController.quietCancel(animator); |
| 1724 |
} |
| 1725 |
} |
| 1726 |
|
| 1727 |
@VisibleForTesting |
| 1728 |
public void setSurfaceControlTransactionFactory( |
| 1729 |
PipSurfaceTransactionHelper.SurfaceControlTransactionFactory factory) { |
| 1730 |
mSurfaceControlTransactionFactory = factory; |
| 1731 |
} |
| 1732 |
|
| 1733 |
|
| 1734 |
|
| 1735 |
|
| 1736 |
@Override |
| 1737 |
public void dump(PrintWriter pw, String prefix) { |
| 1738 |
final String innerPrefix = prefix + " "; |
| 1739 |
pw.println(prefix + TAG); |
| 1740 |
pw.println(innerPrefix + "mTaskInfo=" + mTaskInfo); |
| 1741 |
pw.println(innerPrefix + "mToken=" + mToken |
| 1742 |
+ " binder=" + (mToken != null ? mToken.asBinder() : null)); |
| 1743 |
pw.println(innerPrefix + "mLeash=" + mLeash); |
| 1744 |
pw.println(innerPrefix + "mState=" + mPipTransitionState.getTransitionState()); |
| 1745 |
pw.println(innerPrefix + "mOneShotAnimationType=" + mOneShotAnimationType); |
| 1746 |
pw.println(innerPrefix + "mPictureInPictureParams=" + mPictureInPictureParams); |
| 1747 |
} |
| 1748 |
|
| 1749 |
@Override |
| 1750 |
public String toString() { |
| 1751 |
return TAG + ":" + taskListenerTypeToString(TASK_LISTENER_TYPE_PIP); |
| 1752 |
} |
| 1753 |
} |