Merge "Improve first opaque activity candidate detection" into udc-dev am: 3d8f16685e am: 9f6a2c6deb

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/22885818

Change-Id: I5a524ea82c9510fdbae73a3ff6400e23f2136d59
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Massimo Carli
2023-05-05 00:29:44 +00:00
committed by Automerger Merge Worker
3 changed files with 138 additions and 64 deletions

View File

@@ -1640,7 +1640,7 @@ final class ActivityRecord extends WindowToken implements WindowManagerService.A
if (isState(RESUMED)) { if (isState(RESUMED)) {
newParent.setResumedActivity(this, "onParentChanged"); newParent.setResumedActivity(this, "onParentChanged");
} }
mLetterboxUiController.onActivityParentChanged(newParent); mLetterboxUiController.updateInheritedLetterbox();
} }
if (rootTask != null && rootTask.topRunningActivity() == this) { if (rootTask != null && rootTask.topRunningActivity() == this) {

View File

@@ -113,6 +113,8 @@ import com.android.internal.statusbar.LetterboxDetails;
import com.android.server.wm.LetterboxConfiguration.LetterboxBackgroundType; import com.android.server.wm.LetterboxConfiguration.LetterboxBackgroundType;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.function.BooleanSupplier; import java.util.function.BooleanSupplier;
import java.util.function.Consumer; import java.util.function.Consumer;
@@ -127,8 +129,7 @@ import java.util.function.Predicate;
final class LetterboxUiController { final class LetterboxUiController {
private static final Predicate<ActivityRecord> FIRST_OPAQUE_NOT_FINISHING_ACTIVITY_PREDICATE = private static final Predicate<ActivityRecord> FIRST_OPAQUE_NOT_FINISHING_ACTIVITY_PREDICATE =
activityRecord -> activityRecord.fillsParent() && !activityRecord.isFinishing() activityRecord -> activityRecord.fillsParent() && !activityRecord.isFinishing();
&& activityRecord.nowVisible;
private static final String TAG = TAG_WITH_CLASS_NAME ? "LetterboxUiController" : TAG_ATM; private static final String TAG = TAG_WITH_CLASS_NAME ? "LetterboxUiController" : TAG_ATM;
@@ -180,6 +181,10 @@ final class LetterboxUiController {
// Corresponds to OVERRIDE_ENABLE_COMPAT_FAKE_FOCUS // Corresponds to OVERRIDE_ENABLE_COMPAT_FAKE_FOCUS
private final boolean mIsOverrideEnableCompatFakeFocusEnabled; private final boolean mIsOverrideEnableCompatFakeFocusEnabled;
// The list of observers for the destroy event of candidate opaque activities
// when dealing with translucent activities.
private final List<LetterboxUiController> mDestroyListeners = new ArrayList<>();
@Nullable @Nullable
private final Boolean mBooleanPropertyAllowOrientationOverride; private final Boolean mBooleanPropertyAllowOrientationOverride;
@Nullable @Nullable
@@ -193,6 +198,10 @@ final class LetterboxUiController {
@Nullable @Nullable
private WindowContainerListener mLetterboxConfigListener; private WindowContainerListener mLetterboxConfigListener;
@Nullable
@VisibleForTesting
ActivityRecord mFirstOpaqueActivityBeneath;
private boolean mShowWallpaperForLetterboxBackground; private boolean mShowWallpaperForLetterboxBackground;
// In case of transparent activities we might need to access the aspectRatio of the // In case of transparent activities we might need to access the aspectRatio of the
@@ -353,6 +362,10 @@ final class LetterboxUiController {
mLetterbox.destroy(); mLetterbox.destroy();
mLetterbox = null; mLetterbox = null;
} }
for (int i = mDestroyListeners.size() - 1; i >= 0; i--) {
mDestroyListeners.get(i).updateInheritedLetterbox();
}
mDestroyListeners.clear();
if (mLetterboxConfigListener != null) { if (mLetterboxConfigListener != null) {
mLetterboxConfigListener.onRemoved(); mLetterboxConfigListener.onRemoved();
mLetterboxConfigListener = null; mLetterboxConfigListener = null;
@@ -1571,7 +1584,11 @@ final class LetterboxUiController {
* first opaque activity beneath. * first opaque activity beneath.
* @param parent The parent container. * @param parent The parent container.
*/ */
void onActivityParentChanged(WindowContainer<?> parent) { void updateInheritedLetterbox() {
final WindowContainer<?> parent = mActivityRecord.getParent();
if (parent == null) {
return;
}
if (!mLetterboxConfiguration.isTranslucentLetterboxingEnabled()) { if (!mLetterboxConfiguration.isTranslucentLetterboxingEnabled()) {
return; return;
} }
@@ -1581,22 +1598,24 @@ final class LetterboxUiController {
} }
// In case mActivityRecord.hasCompatDisplayInsetsWithoutOverride() we don't apply the // In case mActivityRecord.hasCompatDisplayInsetsWithoutOverride() we don't apply the
// opaque activity constraints because we're expecting the activity is already letterboxed. // opaque activity constraints because we're expecting the activity is already letterboxed.
mFirstOpaqueActivityBeneath = mActivityRecord.getTask().getActivity(
FIRST_OPAQUE_NOT_FINISHING_ACTIVITY_PREDICATE /* callback */,
mActivityRecord /* boundary */, false /* includeBoundary */,
true /* traverseTopToBottom */);
if (mFirstOpaqueActivityBeneath == null || mFirstOpaqueActivityBeneath.isEmbedded()) {
// We skip letterboxing if the translucent activity doesn't have any opaque
// activities beneath or the activity below is embedded which never has letterbox.
mActivityRecord.recomputeConfiguration();
return;
}
if (mActivityRecord.getTask() == null || mActivityRecord.fillsParent() if (mActivityRecord.getTask() == null || mActivityRecord.fillsParent()
|| mActivityRecord.hasCompatDisplayInsetsWithoutInheritance()) { || mActivityRecord.hasCompatDisplayInsetsWithoutInheritance()) {
return; return;
} }
final ActivityRecord firstOpaqueActivityBeneath = mActivityRecord.getTask().getActivity( mFirstOpaqueActivityBeneath.mLetterboxUiController.mDestroyListeners.add(this);
FIRST_OPAQUE_NOT_FINISHING_ACTIVITY_PREDICATE /* callback */, inheritConfiguration(mFirstOpaqueActivityBeneath);
mActivityRecord /* boundary */, false /* includeBoundary */,
true /* traverseTopToBottom */);
if (firstOpaqueActivityBeneath == null || firstOpaqueActivityBeneath.isEmbedded()) {
// We skip letterboxing if the translucent activity doesn't have any opaque
// activities beneath or the activity below is embedded which never has letterbox.
return;
}
inheritConfiguration(firstOpaqueActivityBeneath);
mLetterboxConfigListener = WindowContainer.overrideConfigurationPropagation( mLetterboxConfigListener = WindowContainer.overrideConfigurationPropagation(
mActivityRecord, firstOpaqueActivityBeneath, mActivityRecord, mFirstOpaqueActivityBeneath,
(opaqueConfig, transparentOverrideConfig) -> { (opaqueConfig, transparentOverrideConfig) -> {
resetTranslucentOverrideConfig(transparentOverrideConfig); resetTranslucentOverrideConfig(transparentOverrideConfig);
final Rect parentBounds = parent.getWindowConfiguration().getBounds(); final Rect parentBounds = parent.getWindowConfiguration().getBounds();
@@ -1610,7 +1629,7 @@ final class LetterboxUiController {
// We need to initialize appBounds to avoid NPE. The actual value will // We need to initialize appBounds to avoid NPE. The actual value will
// be set ahead when resolving the Configuration for the activity. // be set ahead when resolving the Configuration for the activity.
transparentOverrideConfig.windowConfiguration.setAppBounds(new Rect()); transparentOverrideConfig.windowConfiguration.setAppBounds(new Rect());
inheritConfiguration(firstOpaqueActivityBeneath); inheritConfiguration(mFirstOpaqueActivityBeneath);
return transparentOverrideConfig; return transparentOverrideConfig;
}); });
} }
@@ -1684,10 +1703,7 @@ final class LetterboxUiController {
if (!hasInheritedLetterboxBehavior() || mActivityRecord.getTask() == null) { if (!hasInheritedLetterboxBehavior() || mActivityRecord.getTask() == null) {
return Optional.empty(); return Optional.empty();
} }
return Optional.ofNullable(mActivityRecord.getTask().getActivity( return Optional.ofNullable(mFirstOpaqueActivityBeneath);
FIRST_OPAQUE_NOT_FINISHING_ACTIVITY_PREDICATE /* callback */,
mActivityRecord /* boundary */, false /* includeBoundary */,
true /* traverseTopToBottom */));
} }
/** Resets the screen size related fields so they can be resolved by requested bounds later. */ /** Resets the screen size related fields so they can be resolved by requested bounds later. */
@@ -1718,6 +1734,10 @@ final class LetterboxUiController {
} }
private void clearInheritedConfig() { private void clearInheritedConfig() {
if (mFirstOpaqueActivityBeneath != null) {
mFirstOpaqueActivityBeneath.mLetterboxUiController.mDestroyListeners.remove(this);
}
mFirstOpaqueActivityBeneath = null;
mLetterboxConfigListener = null; mLetterboxConfigListener = null;
mInheritedMinAspectRatio = UNDEFINED_ASPECT_RATIO; mInheritedMinAspectRatio = UNDEFINED_ASPECT_RATIO;
mInheritedMaxAspectRatio = UNDEFINED_ASPECT_RATIO; mInheritedMaxAspectRatio = UNDEFINED_ASPECT_RATIO;

View File

@@ -178,44 +178,6 @@ public class SizeCompatTests extends WindowTestsBase {
setUpApp(builder.build()); setUpApp(builder.build());
} }
@Test
public void testActivityInHistoryAndNotVisibleIsNotUsedAsOpaqueForTranslucentActivities() {
mWm.mLetterboxConfiguration.setTranslucentLetterboxingOverrideEnabled(true);
setUpDisplaySizeWithApp(2000, 1000);
prepareUnresizable(mActivity, SCREEN_ORIENTATION_PORTRAIT);
mActivity.mDisplayContent.setIgnoreOrientationRequest(true /* ignoreOrientationRequest */);
mActivity.nowVisible = false;
// Translucent Activity
final ActivityRecord translucentActivity = new ActivityBuilder(mAtm)
.setLaunchedFromUid(mActivity.getUid())
.setScreenOrientation(SCREEN_ORIENTATION_PORTRAIT)
.build();
doReturn(false).when(translucentActivity).fillsParent();
mTask.addChild(translucentActivity);
assertFalse(translucentActivity.mLetterboxUiController.hasInheritedLetterboxBehavior());
}
@Test
public void testActivityInHistoryAndVisibleIsUsedAsOpaqueForTranslucentActivities() {
mWm.mLetterboxConfiguration.setTranslucentLetterboxingOverrideEnabled(true);
setUpDisplaySizeWithApp(2000, 1000);
prepareUnresizable(mActivity, SCREEN_ORIENTATION_PORTRAIT);
mActivity.mDisplayContent.setIgnoreOrientationRequest(true /* ignoreOrientationRequest */);
mActivity.nowVisible = true;
// Translucent Activity
final ActivityRecord translucentActivity = new ActivityBuilder(mAtm)
.setLaunchedFromUid(mActivity.getUid())
.setScreenOrientation(SCREEN_ORIENTATION_PORTRAIT)
.build();
doReturn(false).when(translucentActivity).fillsParent();
mTask.addChild(translucentActivity);
assertTrue(translucentActivity.mLetterboxUiController.hasInheritedLetterboxBehavior());
}
@Test @Test
public void testCleanLetterboxConfigListenerWhenTranslucentIsDestroyed() { public void testCleanLetterboxConfigListenerWhenTranslucentIsDestroyed() {
mWm.mLetterboxConfiguration.setTranslucentLetterboxingOverrideEnabled(true); mWm.mLetterboxConfiguration.setTranslucentLetterboxingOverrideEnabled(true);
@@ -240,7 +202,6 @@ public class SizeCompatTests extends WindowTestsBase {
public void testHorizontalReachabilityEnabledForTranslucentActivities() { public void testHorizontalReachabilityEnabledForTranslucentActivities() {
setUpDisplaySizeWithApp(2500, 1000); setUpDisplaySizeWithApp(2500, 1000);
mActivity.mDisplayContent.setIgnoreOrientationRequest(true /* ignoreOrientationRequest */); mActivity.mDisplayContent.setIgnoreOrientationRequest(true /* ignoreOrientationRequest */);
mActivity.nowVisible = true;
final LetterboxConfiguration config = mWm.mLetterboxConfiguration; final LetterboxConfiguration config = mWm.mLetterboxConfiguration;
config.setTranslucentLetterboxingOverrideEnabled(true); config.setTranslucentLetterboxingOverrideEnabled(true);
config.setLetterboxHorizontalPositionMultiplier(0.5f); config.setLetterboxHorizontalPositionMultiplier(0.5f);
@@ -316,7 +277,6 @@ public class SizeCompatTests extends WindowTestsBase {
public void testVerticalReachabilityEnabledForTranslucentActivities() { public void testVerticalReachabilityEnabledForTranslucentActivities() {
setUpDisplaySizeWithApp(1000, 2500); setUpDisplaySizeWithApp(1000, 2500);
mActivity.mDisplayContent.setIgnoreOrientationRequest(true /* ignoreOrientationRequest */); mActivity.mDisplayContent.setIgnoreOrientationRequest(true /* ignoreOrientationRequest */);
mActivity.nowVisible = true;
final LetterboxConfiguration config = mWm.mLetterboxConfiguration; final LetterboxConfiguration config = mWm.mLetterboxConfiguration;
config.setTranslucentLetterboxingOverrideEnabled(true); config.setTranslucentLetterboxingOverrideEnabled(true);
config.setLetterboxVerticalPositionMultiplier(0.5f); config.setLetterboxVerticalPositionMultiplier(0.5f);
@@ -388,6 +348,104 @@ public class SizeCompatTests extends WindowTestsBase {
checkIsCentered.run(); checkIsCentered.run();
} }
@Test
public void testApplyStrategyAgainWhenOpaqueIsDestroyed() {
mWm.mLetterboxConfiguration.setTranslucentLetterboxingOverrideEnabled(true);
setUpDisplaySizeWithApp(2000, 1000);
prepareUnresizable(mActivity, SCREEN_ORIENTATION_PORTRAIT);
mActivity.mDisplayContent.setIgnoreOrientationRequest(true /* ignoreOrientationRequest */);
// Launch another opaque activity
final ActivityRecord opaqueActivity = new ActivityBuilder(mAtm)
.setLaunchedFromUid(mActivity.getUid())
.setScreenOrientation(SCREEN_ORIENTATION_PORTRAIT)
.build();
mTask.addChild(opaqueActivity);
// Transparent activity strategy not applied
assertFalse(opaqueActivity.mLetterboxUiController.hasInheritedLetterboxBehavior());
// Launch translucent Activity
final ActivityRecord translucentActivity = new ActivityBuilder(mAtm)
.setLaunchedFromUid(mActivity.getUid())
.setScreenOrientation(SCREEN_ORIENTATION_PORTRAIT)
.build();
doReturn(false).when(translucentActivity).fillsParent();
mTask.addChild(translucentActivity);
// Transparent strategy applied
assertTrue(translucentActivity.mLetterboxUiController.hasInheritedLetterboxBehavior());
spyOn(translucentActivity.mLetterboxUiController);
clearInvocations(translucentActivity.mLetterboxUiController);
// We destroy the first opaque activity
opaqueActivity.setState(DESTROYED, "testing");
opaqueActivity.removeImmediately();
// Check that updateInheritedLetterbox() is invoked again
verify(translucentActivity.mLetterboxUiController).updateInheritedLetterbox();
}
@Test
public void testResetOpaqueReferenceWhenOpaqueIsDestroyed() {
mWm.mLetterboxConfiguration.setTranslucentLetterboxingOverrideEnabled(true);
setUpDisplaySizeWithApp(2000, 1000);
prepareUnresizable(mActivity, SCREEN_ORIENTATION_PORTRAIT);
mActivity.mDisplayContent.setIgnoreOrientationRequest(true /* ignoreOrientationRequest */);
// Launch translucent Activity
final ActivityRecord translucentActivity = new ActivityBuilder(mAtm)
.setLaunchedFromUid(mActivity.getUid())
.setScreenOrientation(SCREEN_ORIENTATION_PORTRAIT)
.build();
doReturn(false).when(translucentActivity).fillsParent();
mTask.addChild(translucentActivity);
// Transparent strategy applied
assertTrue(translucentActivity.mLetterboxUiController.hasInheritedLetterboxBehavior());
assertNotNull(translucentActivity.mLetterboxUiController.mFirstOpaqueActivityBeneath);
spyOn(translucentActivity.mLetterboxUiController);
clearInvocations(translucentActivity.mLetterboxUiController);
// We destroy the first opaque activity
mActivity.setState(DESTROYED, "testing");
mActivity.removeImmediately();
// Check that updateInheritedLetterbox() is invoked again
verify(translucentActivity.mLetterboxUiController).updateInheritedLetterbox();
assertNull(translucentActivity.mLetterboxUiController.mFirstOpaqueActivityBeneath);
}
@Test
public void testNotApplyStrategyAgainWhenOpaqueIsNotDestroyed() {
mWm.mLetterboxConfiguration.setTranslucentLetterboxingOverrideEnabled(true);
setUpDisplaySizeWithApp(2000, 1000);
prepareUnresizable(mActivity, SCREEN_ORIENTATION_PORTRAIT);
mActivity.mDisplayContent.setIgnoreOrientationRequest(true /* ignoreOrientationRequest */);
// Launch another opaque activity
final ActivityRecord opaqueActivity = new ActivityBuilder(mAtm)
.setLaunchedFromUid(mActivity.getUid())
.setScreenOrientation(SCREEN_ORIENTATION_PORTRAIT)
.build();
mTask.addChild(opaqueActivity);
// Transparent activity strategy not applied
assertFalse(opaqueActivity.mLetterboxUiController.hasInheritedLetterboxBehavior());
// Launch translucent Activity
final ActivityRecord translucentActivity = new ActivityBuilder(mAtm)
.setLaunchedFromUid(mActivity.getUid())
.setScreenOrientation(SCREEN_ORIENTATION_PORTRAIT)
.build();
doReturn(false).when(translucentActivity).fillsParent();
mTask.addChild(translucentActivity);
// Transparent strategy applied
assertTrue(translucentActivity.mLetterboxUiController.hasInheritedLetterboxBehavior());
spyOn(translucentActivity.mLetterboxUiController);
clearInvocations(translucentActivity.mLetterboxUiController);
// Check that updateInheritedLetterbox() is invoked again
verify(translucentActivity.mLetterboxUiController, never()).updateInheritedLetterbox();
}
@Test @Test
public void testApplyStrategyToTranslucentActivities() { public void testApplyStrategyToTranslucentActivities() {
mWm.mLetterboxConfiguration.setTranslucentLetterboxingOverrideEnabled(true); mWm.mLetterboxConfiguration.setTranslucentLetterboxingOverrideEnabled(true);
@@ -395,7 +453,6 @@ public class SizeCompatTests extends WindowTestsBase {
prepareUnresizable(mActivity, 1.5f /* maxAspect */, SCREEN_ORIENTATION_PORTRAIT); prepareUnresizable(mActivity, 1.5f /* maxAspect */, SCREEN_ORIENTATION_PORTRAIT);
mActivity.info.setMinAspectRatio(1.2f); mActivity.info.setMinAspectRatio(1.2f);
mActivity.mDisplayContent.setIgnoreOrientationRequest(true /* ignoreOrientationRequest */); mActivity.mDisplayContent.setIgnoreOrientationRequest(true /* ignoreOrientationRequest */);
mActivity.nowVisible = true;
// Translucent Activity // Translucent Activity
final ActivityRecord translucentActivity = new ActivityBuilder(mAtm) final ActivityRecord translucentActivity = new ActivityBuilder(mAtm)
.setLaunchedFromUid(mActivity.getUid()) .setLaunchedFromUid(mActivity.getUid())
@@ -456,7 +513,6 @@ public class SizeCompatTests extends WindowTestsBase {
prepareUnresizable(mActivity, 1.5f /* maxAspect */, SCREEN_ORIENTATION_PORTRAIT); prepareUnresizable(mActivity, 1.5f /* maxAspect */, SCREEN_ORIENTATION_PORTRAIT);
mActivity.info.setMinAspectRatio(1.2f); mActivity.info.setMinAspectRatio(1.2f);
mActivity.mDisplayContent.setIgnoreOrientationRequest(true /* ignoreOrientationRequest */); mActivity.mDisplayContent.setIgnoreOrientationRequest(true /* ignoreOrientationRequest */);
mActivity.nowVisible = true;
// Translucent Activity // Translucent Activity
final ActivityRecord translucentActivity = new ActivityBuilder(mAtm) final ActivityRecord translucentActivity = new ActivityBuilder(mAtm)
.setLaunchedFromUid(mActivity.getUid()) .setLaunchedFromUid(mActivity.getUid())
@@ -550,7 +606,6 @@ public class SizeCompatTests extends WindowTestsBase {
true /* ignoreOrientationRequest */); true /* ignoreOrientationRequest */);
mActivity.mWmService.mLetterboxConfiguration.setLetterboxHorizontalPositionMultiplier( mActivity.mWmService.mLetterboxConfiguration.setLetterboxHorizontalPositionMultiplier(
1.0f /*letterboxVerticalPositionMultiplier*/); 1.0f /*letterboxVerticalPositionMultiplier*/);
mActivity.nowVisible = true;
prepareUnresizable(mActivity, SCREEN_ORIENTATION_PORTRAIT); prepareUnresizable(mActivity, SCREEN_ORIENTATION_PORTRAIT);
// We launch a transparent activity // We launch a transparent activity
final ActivityRecord translucentActivity = new ActivityBuilder(mAtm) final ActivityRecord translucentActivity = new ActivityBuilder(mAtm)
@@ -583,7 +638,6 @@ public class SizeCompatTests extends WindowTestsBase {
mWm.mLetterboxConfiguration.setTranslucentLetterboxingOverrideEnabled(true); mWm.mLetterboxConfiguration.setTranslucentLetterboxingOverrideEnabled(true);
setUpDisplaySizeWithApp(2800, 1400); setUpDisplaySizeWithApp(2800, 1400);
mActivity.mDisplayContent.setIgnoreOrientationRequest(true /* ignoreOrientationRequest */); mActivity.mDisplayContent.setIgnoreOrientationRequest(true /* ignoreOrientationRequest */);
mActivity.nowVisible = true;
prepareUnresizable(mActivity, -1f /* maxAspect */, SCREEN_ORIENTATION_PORTRAIT); prepareUnresizable(mActivity, -1f /* maxAspect */, SCREEN_ORIENTATION_PORTRAIT);
// Rotate to put activity in size compat mode. // Rotate to put activity in size compat mode.
rotateDisplay(mActivity.mDisplayContent, ROTATION_90); rotateDisplay(mActivity.mDisplayContent, ROTATION_90);