Debian Codegraph @

libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipBoundsState.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2020 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.wm.shell.pip;
18
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.app.ActivityTaskManager;
23 import android.app.PictureInPictureParams;
24 import android.app.PictureInPictureUiState;
25 import android.content.ComponentName;
26 import android.content.Context;
27 import android.content.pm.ActivityInfo;
28 import android.graphics.Point;
29 import android.graphics.Rect;
30 import android.os.RemoteException;
31 import android.util.ArraySet;
32 import android.util.Size;
33 import android.view.Display;
34
35 import com.android.internal.annotations.VisibleForTesting;
36 import com.android.internal.protolog.common.ProtoLog;
37 import com.android.internal.util.function.TriConsumer;
38 import com.android.wm.shell.R;
39 import com.android.wm.shell.common.DisplayLayout;
40 import com.android.wm.shell.protolog.ShellProtoLogGroup;
41
42 import java.io.PrintWriter;
43 import java.lang.annotation.Retention;
44 import java.lang.annotation.RetentionPolicy;
45 import java.util.ArrayList;
46 import java.util.List;
47 import java.util.Objects;
48 import java.util.Set;
49 import java.util.function.Consumer;
50
51 /**
52 * Singleton source of truth for the current state of PIP bounds.
53 */
54 public class PipBoundsState {
55 public static final int STASH_TYPE_NONE = 0;
56 public static final int STASH_TYPE_LEFT = 1;
57 public static final int STASH_TYPE_RIGHT = 2;
58 public static final int STASH_TYPE_BOTTOM = 3;
59 public static final int STASH_TYPE_TOP = 4;
60
61 @IntDef(prefix = { "STASH_TYPE_" }, value = {
62 STASH_TYPE_NONE,
63 STASH_TYPE_LEFT,
64 STASH_TYPE_RIGHT,
65 STASH_TYPE_BOTTOM,
66 STASH_TYPE_TOP
67 })
68 @Retention(RetentionPolicy.SOURCE)
69 public @interface StashType {}
70
71 private static final String TAG = PipBoundsState.class.getSimpleName();
72
73 private final @NonNull Rect mBounds = new Rect();
74 private final @NonNull Rect mMovementBounds = new Rect();
75 private final @NonNull Rect mNormalBounds = new Rect();
76 private final @NonNull Rect mExpandedBounds = new Rect();
77 private final @NonNull Rect mNormalMovementBounds = new Rect();
78 private final @NonNull Rect mExpandedMovementBounds = new Rect();
79 private final Point mMaxSize = new Point();
80 private final Point mMinSize = new Point();
81 private final @NonNull Context mContext;
82 private float mAspectRatio;
83 private int mStashedState = STASH_TYPE_NONE;
84 private int mStashOffset;
85 private @Nullable PipReentryState mPipReentryState;
86 private @Nullable ComponentName mLastPipComponentName;
87 private int mDisplayId = Display.DEFAULT_DISPLAY;
88 private final @NonNull DisplayLayout mDisplayLayout = new DisplayLayout();
89 /** The current minimum edge size of PIP. */
90 private int mMinEdgeSize;
91 /** The preferred minimum (and default) size specified by apps. */
92 private @Nullable Size mOverrideMinSize;
93 private final @NonNull MotionBoundsState mMotionBoundsState = new MotionBoundsState();
94 private boolean mIsImeShowing;
95 private int mImeHeight;
96 private boolean mIsShelfShowing;
97 private int mShelfHeight;
98 /** Whether the user has resized the PIP manually. */
99 private boolean mHasUserResizedPip;
100 /** Whether the user has moved the PIP manually. */
101 private boolean mHasUserMovedPip;
102 /**
103 * Areas defined by currently visible apps that they prefer to keep clear from overlays such as
104 * the PiP. Restricted areas may only move the PiP a limited amount from its anchor position.
105 * The system will try to respect these areas, but when not possible will ignore them.
106 *
107 * @see android.view.View#setPreferKeepClearRects
108 */
109 private final Set<Rect> mRestrictedKeepClearAreas = new ArraySet<>();
110 /**
111 * Areas defined by currently visible apps holding
112 * {@link android.Manifest.permission#SET_UNRESTRICTED_KEEP_CLEAR_AREAS} that they prefer to
113 * keep clear from overlays such as the PiP.
114 * Unrestricted areas can move the PiP farther than restricted areas, and the system will try
115 * harder to respect these areas.
116 *
117 * @see android.view.View#setPreferKeepClearRects
118 */
119 private final Set<Rect> mUnrestrictedKeepClearAreas = new ArraySet<>();
120
121 private @Nullable Runnable mOnMinimalSizeChangeCallback;
122 private @Nullable TriConsumer<Boolean, Integer, Boolean> mOnShelfVisibilityChangeCallback;
123 private List<Consumer<Rect>> mOnPipExclusionBoundsChangeCallbacks = new ArrayList<>();
124
125 public PipBoundsState(@NonNull Context context) {
126 mContext = context;
127 reloadResources();
128 }
129
130 /** Reloads the resources. */
131 public void onConfigurationChanged() {
132 reloadResources();
133 }
134
135 private void reloadResources() {
136 mStashOffset = mContext.getResources().getDimensionPixelSize(R.dimen.pip_stash_offset);
137 }
138
139 /** Set the current PIP bounds. */
140 public void setBounds(@NonNull Rect bounds) {
141 mBounds.set(bounds);
142 for (Consumer<Rect> callback : mOnPipExclusionBoundsChangeCallbacks) {
143 callback.accept(bounds);
144 }
145 }
146
147 /** Get the current PIP bounds. */
148 @NonNull
149 public Rect getBounds() {
150 return new Rect(mBounds);
151 }
152
153 /** Returns the current movement bounds. */
154 @NonNull
155 public Rect getMovementBounds() {
156 return mMovementBounds;
157 }
158
159 /** Set the current normal PIP bounds. */
160 public void setNormalBounds(@NonNull Rect bounds) {
161 mNormalBounds.set(bounds);
162 }
163
164 /** Get the current normal PIP bounds. */
165 @NonNull
166 public Rect getNormalBounds() {
167 return mNormalBounds;
168 }
169
170 /** Set the expanded bounds of PIP. */
171 public void setExpandedBounds(@NonNull Rect bounds) {
172 mExpandedBounds.set(bounds);
173 }
174
175 /** Get the PIP expanded bounds. */
176 @NonNull
177 public Rect getExpandedBounds() {
178 return mExpandedBounds;
179 }
180
181 /** Set the normal movement bounds. */
182 public void setNormalMovementBounds(@NonNull Rect bounds) {
183 mNormalMovementBounds.set(bounds);
184 }
185
186 /** Returns the normal movement bounds. */
187 @NonNull
188 public Rect getNormalMovementBounds() {
189 return mNormalMovementBounds;
190 }
191
192 /** Set the expanded movement bounds. */
193 public void setExpandedMovementBounds(@NonNull Rect bounds) {
194 mExpandedMovementBounds.set(bounds);
195 }
196
197 /** Sets the max possible size for resize. */
198 public void setMaxSize(int width, int height) {
199 mMaxSize.set(width, height);
200 }
201
202 /** Sets the min possible size for resize. */
203 public void setMinSize(int width, int height) {
204 mMinSize.set(width, height);
205 }
206
207 public Point getMaxSize() {
208 return mMaxSize;
209 }
210
211 public Point getMinSize() {
212 return mMinSize;
213 }
214
215 /** Returns the expanded movement bounds. */
216 @NonNull
217 public Rect getExpandedMovementBounds() {
218 return mExpandedMovementBounds;
219 }
220
221 /** Dictate where PiP currently should be stashed, if at all. */
222 public void setStashed(@StashType int stashedState) {
223 if (mStashedState == stashedState) {
224 return;
225 }
226
227 mStashedState = stashedState;
228 try {
229 ActivityTaskManager.getService().onPictureInPictureStateChanged(
230 new PictureInPictureUiState(stashedState != STASH_TYPE_NONE /* isStashed */)
231 );
232 } catch (RemoteException e) {
233 ProtoLog.e(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
234 "%s: Unable to set alert PiP state change.", TAG);
235 }
236 }
237
238 /**
239 * Return where the PiP is stashed, if at all.
240 * @return {@code STASH_NONE}, {@code STASH_LEFT} or {@code STASH_RIGHT}.
241 */
242 public @StashType int getStashedState() {
243 return mStashedState;
244 }
245
246 /** Whether PiP is stashed or not. */
247 public boolean isStashed() {
248 return mStashedState != STASH_TYPE_NONE;
249 }
250
251 /** Returns the offset from the edge of the screen for PiP stash. */
252 public int getStashOffset() {
253 return mStashOffset;
254 }
255
256 /** Set the PIP aspect ratio. */
257 public void setAspectRatio(float aspectRatio) {
258 mAspectRatio = aspectRatio;
259 }
260
261 /** Get the PIP aspect ratio. */
262 public float getAspectRatio() {
263 return mAspectRatio;
264 }
265
266 /** Save the reentry state to restore to when re-entering PIP mode. */
267 public void saveReentryState(Size size, float fraction) {
268 mPipReentryState = new PipReentryState(size, fraction);
269 }
270
271 /** Returns the saved reentry state. */
272 @Nullable
273 public PipReentryState getReentryState() {
274 return mPipReentryState;
275 }
276
277 /** Set the last {@link ComponentName} to enter PIP mode. */
278 public void setLastPipComponentName(@Nullable ComponentName lastPipComponentName) {
279 final boolean changed = !Objects.equals(mLastPipComponentName, lastPipComponentName);
280 mLastPipComponentName = lastPipComponentName;
281 if (changed) {
282 clearReentryState();
283 setHasUserResizedPip(false);
284 setHasUserMovedPip(false);
285 }
286 }
287
288 /** Get the last PIP component name, if any. */
289 @Nullable
290 public ComponentName getLastPipComponentName() {
291 return mLastPipComponentName;
292 }
293
294 /** Get the current display id. */
295 public int getDisplayId() {
296 return mDisplayId;
297 }
298
299 /** Set the current display id for the associated display layout. */
300 public void setDisplayId(int displayId) {
301 mDisplayId = displayId;
302 }
303
304 /** Returns the display's bounds. */
305 @NonNull
306 public Rect getDisplayBounds() {
307 return new Rect(0, 0, mDisplayLayout.width(), mDisplayLayout.height());
308 }
309
310 /** Update the display layout. */
311 public void setDisplayLayout(@NonNull DisplayLayout displayLayout) {
312 mDisplayLayout.set(displayLayout);
313 }
314
315 /** Get the display layout. */
316 @NonNull
317 public DisplayLayout getDisplayLayout() {
318 return mDisplayLayout;
319 }
320
321 @VisibleForTesting
322 void clearReentryState() {
323 mPipReentryState = null;
324 }
325
326 /** Set the PIP minimum edge size. */
327 public void setMinEdgeSize(int minEdgeSize) {
328 mMinEdgeSize = minEdgeSize;
329 }
330
331 /** Returns the PIP's current minimum edge size. */
332 public int getMinEdgeSize() {
333 return mMinEdgeSize;
334 }
335
336 /** Sets the preferred size of PIP as specified by the activity in PIP mode. */
337 public void setOverrideMinSize(@Nullable Size overrideMinSize) {
338 final boolean changed = !Objects.equals(overrideMinSize, mOverrideMinSize);
339 mOverrideMinSize = overrideMinSize;
340 if (changed && mOnMinimalSizeChangeCallback != null) {
341 mOnMinimalSizeChangeCallback.run();
342 }
343 }
344
345 /** Returns the preferred minimal size specified by the activity in PIP. */
346 @Nullable
347 public Size getOverrideMinSize() {
348 return mOverrideMinSize;
349 }
350
351 /** Returns the minimum edge size of the override minimum size, or 0 if not set. */
352 public int getOverrideMinEdgeSize() {
353 if (mOverrideMinSize == null) return 0;
354 return Math.min(mOverrideMinSize.getWidth(), mOverrideMinSize.getHeight());
355 }
356
357 /** Get the state of the bounds in motion. */
358 @NonNull
359 public MotionBoundsState getMotionBoundsState() {
360 return mMotionBoundsState;
361 }
362
363 /** Set whether the IME is currently showing and its height. */
364 public void setImeVisibility(boolean imeShowing, int imeHeight) {
365 mIsImeShowing = imeShowing;
366 mImeHeight = imeHeight;
367 }
368
369 /** Returns whether the IME is currently showing. */
370 public boolean isImeShowing() {
371 return mIsImeShowing;
372 }
373
374 /** Returns the IME height. */
375 public int getImeHeight() {
376 return mImeHeight;
377 }
378
379 /** Set whether the shelf is showing and its height. */
380 public void setShelfVisibility(boolean showing, int height) {
381 setShelfVisibility(showing, height, true);
382 }
383
384 /** Set whether the shelf is showing and its height. */
385 public void setShelfVisibility(boolean showing, int height, boolean updateMovementBounds) {
386 final boolean shelfShowing = showing && height > 0;
387 if (shelfShowing == mIsShelfShowing && height == mShelfHeight) {
388 return;
389 }
390
391 mIsShelfShowing = showing;
392 mShelfHeight = height;
393 if (mOnShelfVisibilityChangeCallback != null) {
394 mOnShelfVisibilityChangeCallback.accept(mIsShelfShowing, mShelfHeight,
395 updateMovementBounds);
396 }
397 }
398
399 /** Set the keep clear areas onscreen. The PiP should ideally not cover them. */
400 public void setKeepClearAreas(@NonNull Set<Rect> restrictedAreas,
401 @NonNull Set<Rect> unrestrictedAreas) {
402 mRestrictedKeepClearAreas.clear();
403 mRestrictedKeepClearAreas.addAll(restrictedAreas);
404 mUnrestrictedKeepClearAreas.clear();
405 mUnrestrictedKeepClearAreas.addAll(unrestrictedAreas);
406 }
407
408 @NonNull
409 public Set<Rect> getRestrictedKeepClearAreas() {
410 return mRestrictedKeepClearAreas;
411 }
412
413 @NonNull
414 public Set<Rect> getUnrestrictedKeepClearAreas() {
415 return mUnrestrictedKeepClearAreas;
416 }
417
418 /**
419 * Initialize states when first entering PiP.
420 */
421 public void setBoundsStateForEntry(ComponentName componentName, ActivityInfo activityInfo,
422 PictureInPictureParams params, PipBoundsAlgorithm pipBoundsAlgorithm) {
423 setLastPipComponentName(componentName);
424 setAspectRatio(pipBoundsAlgorithm.getAspectRatioOrDefault(params));
425 setOverrideMinSize(pipBoundsAlgorithm.getMinimalSize(activityInfo));
426 }
427
428 /** Returns whether the shelf is currently showing. */
429 public boolean isShelfShowing() {
430 return mIsShelfShowing;
431 }
432
433 /** Returns the shelf height. */
434 public int getShelfHeight() {
435 return mShelfHeight;
436 }
437
438 /** Returns whether the user has resized the PIP. */
439 public boolean hasUserResizedPip() {
440 return mHasUserResizedPip;
441 }
442
443 /** Set whether the user has resized the PIP. */
444 public void setHasUserResizedPip(boolean hasUserResizedPip) {
445 mHasUserResizedPip = hasUserResizedPip;
446 }
447
448 /** Returns whether the user has moved the PIP. */
449 public boolean hasUserMovedPip() {
450 return mHasUserMovedPip;
451 }
452
453 /** Set whether the user has moved the PIP. */
454 public void setHasUserMovedPip(boolean hasUserMovedPip) {
455 mHasUserMovedPip = hasUserMovedPip;
456 }
457
458 /**
459 * Registers a callback when the minimal size of PIP that is set by the app changes.
460 */
461 public void setOnMinimalSizeChangeCallback(@Nullable Runnable onMinimalSizeChangeCallback) {
462 mOnMinimalSizeChangeCallback = onMinimalSizeChangeCallback;
463 }
464
465 /** Set a callback to be notified when the shelf visibility changes. */
466 public void setOnShelfVisibilityChangeCallback(
467 @Nullable TriConsumer<Boolean, Integer, Boolean> onShelfVisibilityChangeCallback) {
468 mOnShelfVisibilityChangeCallback = onShelfVisibilityChangeCallback;
469 }
470
471 /**
472 * Add a callback to watch out for PiP bounds. This is mostly used by SystemUI's
473 * Back-gesture handler, to avoid conflicting with PiP when it's stashed.
474 */
475 public void addPipExclusionBoundsChangeCallback(
476 @Nullable Consumer<Rect> onPipExclusionBoundsChangeCallback) {
477 mOnPipExclusionBoundsChangeCallbacks.add(onPipExclusionBoundsChangeCallback);
478 for (Consumer<Rect> callback : mOnPipExclusionBoundsChangeCallbacks) {
479 callback.accept(getBounds());
480 }
481 }
482
483 /**
484 * Remove a callback that was previously added.
485 */
486 public void removePipExclusionBoundsChangeCallback(
487 @Nullable Consumer<Rect> onPipExclusionBoundsChangeCallback) {
488 mOnPipExclusionBoundsChangeCallbacks.remove(onPipExclusionBoundsChangeCallback);
489 }
490
491 /** Source of truth for the current bounds of PIP that may be in motion. */
492 public static class MotionBoundsState {
493 /** The bounds used when PIP is in motion (e.g. during a drag or animation) */
494 private final @NonNull Rect mBoundsInMotion = new Rect();
495 /** The destination bounds to which PIP is animating. */
496 private final @NonNull Rect mAnimatingToBounds = new Rect();
497
498 /** Whether PIP is being dragged or animated (e.g. resizing, in fling, etc). */
499 public boolean isInMotion() {
500 return !mBoundsInMotion.isEmpty();
501 }
502
503 /** Set the temporary bounds used to represent the drag or animation bounds of PIP. */
504 public void setBoundsInMotion(@NonNull Rect bounds) {
505 mBoundsInMotion.set(bounds);
506 }
507
508 /** Set the bounds to which PIP is animating. */
509 public void setAnimatingToBounds(@NonNull Rect bounds) {
510 mAnimatingToBounds.set(bounds);
511 }
512
513 /** Called when all ongoing motion operations have ended. */
514 public void onAllAnimationsEnded() {
515 mBoundsInMotion.setEmpty();
516 }
517
518 /** Called when an ongoing physics animation has ended. */
519 public void onPhysicsAnimationEnded() {
520 mAnimatingToBounds.setEmpty();
521 }
522
523 /** Returns the motion bounds. */
524 @NonNull
525 public Rect getBoundsInMotion() {
526 return mBoundsInMotion;
527 }
528
529 /** Returns the destination bounds to which PIP is currently animating. */
530 @NonNull
531 public Rect getAnimatingToBounds() {
532 return mAnimatingToBounds;
533 }
534
535 void dump(PrintWriter pw, String prefix) {
536 final String innerPrefix = prefix + " ";
537 pw.println(prefix + MotionBoundsState.class.getSimpleName());
538 pw.println(innerPrefix + "mBoundsInMotion=" + mBoundsInMotion);
539 pw.println(innerPrefix + "mAnimatingToBounds=" + mAnimatingToBounds);
540 }
541 }
542
543 static final class PipReentryState {
544 private static final String TAG = PipReentryState.class.getSimpleName();
545
546 private final @Nullable Size mSize;
547 private final float mSnapFraction;
548
549 PipReentryState(@Nullable Size size, float snapFraction) {
550 mSize = size;
551 mSnapFraction = snapFraction;
552 }
553
554 @Nullable
555 Size getSize() {
556 return mSize;
557 }
558
559 float getSnapFraction() {
560 return mSnapFraction;
561 }
562
563 void dump(PrintWriter pw, String prefix) {
564 final String innerPrefix = prefix + " ";
565 pw.println(prefix + TAG);
566 pw.println(innerPrefix + "mSize=" + mSize);
567 pw.println(innerPrefix + "mSnapFraction=" + mSnapFraction);
568 }
569 }
570
571 /** Dumps internal state. */
572 public void dump(PrintWriter pw, String prefix) {
573 final String innerPrefix = prefix + " ";
574 pw.println(prefix + TAG);
575 pw.println(innerPrefix + "mBounds=" + mBounds);
576 pw.println(innerPrefix + "mNormalBounds=" + mNormalBounds);
577 pw.println(innerPrefix + "mExpandedBounds=" + mExpandedBounds);
578 pw.println(innerPrefix + "mMovementBounds=" + mMovementBounds);
579 pw.println(innerPrefix + "mNormalMovementBounds=" + mNormalMovementBounds);
580 pw.println(innerPrefix + "mExpandedMovementBounds=" + mExpandedMovementBounds);
581 pw.println(innerPrefix + "mLastPipComponentName=" + mLastPipComponentName);
582 pw.println(innerPrefix + "mAspectRatio=" + mAspectRatio);
583 pw.println(innerPrefix + "mDisplayId=" + mDisplayId);
584 pw.println(innerPrefix + "mDisplayLayout=" + mDisplayLayout);
585 pw.println(innerPrefix + "mStashedState=" + mStashedState);
586 pw.println(innerPrefix + "mStashOffset=" + mStashOffset);
587 pw.println(innerPrefix + "mMinEdgeSize=" + mMinEdgeSize);
588 pw.println(innerPrefix + "mOverrideMinSize=" + mOverrideMinSize);
589 pw.println(innerPrefix + "mIsImeShowing=" + mIsImeShowing);
590 pw.println(innerPrefix + "mImeHeight=" + mImeHeight);
591 pw.println(innerPrefix + "mIsShelfShowing=" + mIsShelfShowing);
592 pw.println(innerPrefix + "mShelfHeight=" + mShelfHeight);
593 pw.println(innerPrefix + "mHasUserMovedPip=" + mHasUserMovedPip);
594 pw.println(innerPrefix + "mHasUserResizedPip=" + mHasUserResizedPip);
595 if (mPipReentryState == null) {
596 pw.println(innerPrefix + "mPipReentryState=null");
597 } else {
598 mPipReentryState.dump(pw, innerPrefix);
599 }
600 mMotionBoundsState.dump(pw, innerPrefix);
601 }
602 }