Merge "Shell command to toggle sandboxing" into sc-dev

This commit is contained in:
Naomi Musgrave
2021-04-13 09:11:26 +00:00
committed by Android (Google) Code Review
5 changed files with 111 additions and 0 deletions

View File

@@ -7430,6 +7430,10 @@ final class ActivityRecord extends WindowToken implements WindowManagerService.A
getPid(), info.applicationInfo.uid) == PERMISSION_GRANTED) {
return false;
}
// Do not sandbox to activity window bounds if the feature is disabled.
if (mDisplayContent != null && !mDisplayContent.sandboxDisplayApis()) {
return false;
}
// Max bounds should be sandboxed where an activity is letterboxed (activity bounds will be
// smaller than task bounds).
if (!matchParentBounds()) {

View File

@@ -348,6 +348,14 @@ class DisplayContent extends RootDisplayArea implements WindowManagerPolicy.Disp
int mBaseDisplayWidth = 0;
int mBaseDisplayHeight = 0;
boolean mIsSizeForced = false;
/**
* Overridden display size and metrics to activity window bounds. Set via
* "adb shell wm set-sandbox-display-apis". Default to true, since only disable for debugging.
* @see WindowManagerService#setSandboxDisplayApis(int, boolean)
*/
private boolean mSandboxDisplayApis = true;
/**
* Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
* but can be set from Settings or via shell command "adb shell wm density".
@@ -5736,6 +5744,21 @@ class DisplayContent extends RootDisplayArea implements WindowManagerPolicy.Disp
return true;
}
/**
* Sets if Display APIs should be sandboxed to the activity window bounds.
*/
void setSandboxDisplayApis(boolean sandboxDisplayApis) {
mSandboxDisplayApis = sandboxDisplayApis;
}
/**
* Returns {@code true} is Display APIs should be sandboxed to the activity window bounds,
* {@code false} otherwise. Default to true, unless set for debugging purposes.
*/
boolean sandboxDisplayApis() {
return mSandboxDisplayApis;
}
/** The entry for proceeding to handle {@link #mFixedRotationLaunchingApp}. */
class FixedRotationTransitionListener extends WindowManagerInternal.AppTransitionListener {

View File

@@ -5571,6 +5571,25 @@ public class WindowManagerService extends IWindowManager.Stub
}
}
void setSandboxDisplayApis(int displayId, boolean sandboxDisplayApis) {
if (mContext.checkCallingOrSelfPermission(WRITE_SECURE_SETTINGS)
!= PackageManager.PERMISSION_GRANTED) {
throw new SecurityException("Must hold permission " + WRITE_SECURE_SETTINGS);
}
final long ident = Binder.clearCallingIdentity();
try {
synchronized (mGlobalLock) {
final DisplayContent displayContent = mRoot.getDisplayContent(displayId);
if (displayContent != null) {
displayContent.setSandboxDisplayApis(sandboxDisplayApis);
}
}
} finally {
Binder.restoreCallingIdentity(ident);
}
}
/** The global settings only apply to default display. */
private boolean applyForcedPropertiesForDefaultDisplay() {
boolean changed = false;

View File

@@ -143,6 +143,8 @@ public class WindowManagerShellCommand extends ShellCommand {
return runSetLetterboxBackgroundWallpaperDarkScrimAlpha(pw);
case "get-letterbox-background-wallpaper-dark-scrim-alpha":
return runGetLetterboxBackgroundWallpaperDarkScrimAlpha(pw);
case "set-sandbox-display-apis":
return runSandboxDisplayApis(pw);
case "reset":
return runReset(pw);
case "disable-blur":
@@ -353,6 +355,37 @@ public class WindowManagerShellCommand extends ShellCommand {
return 0;
}
/**
* Override display size and metrics to reflect the DisplayArea of the calling activity.
*/
private int runSandboxDisplayApis(PrintWriter pw) throws RemoteException {
int displayId = Display.DEFAULT_DISPLAY;
String arg = getNextArgRequired();
if ("-d".equals(arg)) {
displayId = Integer.parseInt(getNextArgRequired());
arg = getNextArgRequired();
}
final boolean sandboxDisplayApis;
switch (arg) {
case "true":
case "1":
sandboxDisplayApis = true;
break;
case "false":
case "0":
sandboxDisplayApis = false;
break;
default:
getErrPrintWriter().println("Error: expecting true, 1, false, 0, but we "
+ "get " + arg);
return -1;
}
mInternal.setSandboxDisplayApis(displayId, sandboxDisplayApis);
return 0;
}
private int runDismissKeyguard(PrintWriter pw) throws RemoteException {
mInterface.dismissKeyguard(null /* callback */, null /* message */);
return 0;
@@ -855,6 +888,9 @@ public class WindowManagerShellCommand extends ShellCommand {
// set-letterbox-background-wallpaper-dark-scrim-alpha
mInternal.resetLetterboxBackgroundWallpaperDarkScrimAlpha();
// set-sandbox-display-apis
mInternal.setSandboxDisplayApis(displayId, /* sandboxDisplayApis= */ true);
pw.println("Reset all settings for displayId=" + displayId);
return 0;
}
@@ -918,6 +954,10 @@ public class WindowManagerShellCommand extends ShellCommand {
pw.println(" letterbox background. If alpha < 0 or >= 1 both it and");
pw.println(" R.dimen.config_letterboxBackgroundWallaperDarkScrimAlpha are ignored and ");
pw.println(" 0.0 (transparent) is used instead.");
pw.println(" set-sandbox-display-apis [true|1|false|0]");
pw.println(" Sets override of Display APIs getRealSize / getRealMetrics to reflect ");
pw.println(" DisplayArea of the activity, or the window bounds if in letterbox or");
pw.println(" Size Compat Mode.");
pw.println(" reset [-d DISPLAY_ID]");
pw.println(" Reset all override settings.");
if (!IS_USER) {

View File

@@ -1359,6 +1359,31 @@ public class SizeCompatTests extends WindowTestsBase {
assertActivityMaxBoundsSandboxed();
}
@Test
public void testSandboxDisplayApis_letterboxAppNotSandboxed() {
// Set up a display in landscape with an unresizable app.
setUpDisplaySizeWithApp(2500, 1000);
mActivity.mDisplayContent.setSandboxDisplayApis(false /* sandboxDisplayApis */);
prepareUnresizable(mActivity, 1.5f, SCREEN_ORIENTATION_LANDSCAPE);
assertFitted();
// Activity max bounds not be sandboxed since sandboxing is disabled.
assertThat(mActivity.getMaxBounds()).isEqualTo(mActivity.mDisplayContent.getBounds());
}
@Test
public void testSandboxDisplayApis_letterboxAppSandboxed() {
// Set up a display in landscape with an unresizable app.
setUpDisplaySizeWithApp(2500, 1000);
mActivity.mDisplayContent.setSandboxDisplayApis(true /* sandboxDisplayApis */);
prepareUnresizable(mActivity, 1.5f, SCREEN_ORIENTATION_LANDSCAPE);
assertFitted();
// Activity max bounds should be sandboxed since sandboxing is enabled.
assertActivityMaxBoundsSandboxed();
}
@Test
public void testTaskDisplayAreaNotFillDisplay() {
setUpDisplaySizeWithApp(1400, 2800);