From a87880f1ebfcf8a74f3cdb9dd0e716f962c10736 Mon Sep 17 00:00:00 2001 From: Jordan Demeulenaere Date: Mon, 24 Jan 2022 15:39:23 +0100 Subject: [PATCH] Fix SysUI dialog width This CL fixes the code that sets the dialog width such that: 1. Dialogs don't match their parent width on phones in landscape. 2. We take the background insets into consideration, so that the actual dialog size is exactly what we want it to be. Before this CL, we wouldn't take the background insets into consideration and therefore a 504dp width dialog was actually 504dp - 2 x 16dp = 472dp. Bug: 203389579 Test: Manual Change-Id: Ic66b043fe57ea7d493831e2b7d0e42a62362fd6f --- packages/SystemUI/res/values-land/dimens.xml | 2 + .../SystemUI/res/values-sw600dp/dimens.xml | 2 +- .../statusbar/phone/SystemUIDialog.java | 60 ++++++++++++------- 3 files changed, 41 insertions(+), 23 deletions(-) diff --git a/packages/SystemUI/res/values-land/dimens.xml b/packages/SystemUI/res/values-land/dimens.xml index fc5edf3ade8fd..9d24e9b97da36 100644 --- a/packages/SystemUI/res/values-land/dimens.xml +++ b/packages/SystemUI/res/values-land/dimens.xml @@ -66,4 +66,6 @@ 8dp 24dp + + 348dp diff --git a/packages/SystemUI/res/values-sw600dp/dimens.xml b/packages/SystemUI/res/values-sw600dp/dimens.xml index 7d033018c27f5..a66ed15c9d841 100644 --- a/packages/SystemUI/res/values-sw600dp/dimens.xml +++ b/packages/SystemUI/res/values-sw600dp/dimens.xml @@ -69,5 +69,5 @@ 0dp - 504dp + 472dp diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/SystemUIDialog.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/SystemUIDialog.java index e3b4caabb1349..d6fc0a426590c 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/SystemUIDialog.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/SystemUIDialog.java @@ -23,6 +23,8 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.res.Configuration; +import android.graphics.Insets; +import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.Handler; import android.os.Looper; @@ -87,11 +89,8 @@ public class SystemUIDialog extends AlertDialog implements ViewRootImpl.ConfigCh this(context, theme, dismissOnDeviceLock, null); } - /** - * @param udfpsDialogManager If set, UDFPS will hide if this dialog is showing. - */ public SystemUIDialog(Context context, int theme, boolean dismissOnDeviceLock, - SystemUIDialogManager dialogManager) { + @Nullable SystemUIDialogManager dialogManager) { super(context, theme); mContext = context; @@ -148,7 +147,7 @@ public class SystemUIDialog extends AlertDialog implements ViewRootImpl.ConfigCh * the device configuration changes, and the result will be used to resize this dialog window. */ protected int getWidth() { - return getDefaultDialogWidth(mContext); + return getDefaultDialogWidth(this); } /** @@ -279,36 +278,53 @@ public class SystemUIDialog extends AlertDialog implements ViewRootImpl.ConfigCh // We need to create the dialog first, otherwise the size will be overridden when it is // created. dialog.create(); - dialog.getWindow().setLayout(getDefaultDialogWidth(dialog.getContext()), - getDefaultDialogHeight()); + dialog.getWindow().setLayout(getDefaultDialogWidth(dialog), getDefaultDialogHeight()); } - private static int getDefaultDialogWidth(Context context) { - boolean isOnTablet = context.getResources().getConfiguration().smallestScreenWidthDp >= 600; - if (!isOnTablet) { - return ViewGroup.LayoutParams.MATCH_PARENT; - } - + private static int getDefaultDialogWidth(Dialog dialog) { + Context context = dialog.getContext(); int flagValue = SystemProperties.getInt(FLAG_TABLET_DIALOG_WIDTH, 0); if (flagValue == -1) { // The width of bottom sheets (624dp). - return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 624, - context.getResources().getDisplayMetrics())); + return calculateDialogWidthWithInsets(dialog, 624); } else if (flagValue == -2) { // The suggested small width for all dialogs (348dp) - return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 348, - context.getResources().getDisplayMetrics())); + return calculateDialogWidthWithInsets(dialog, 348); } else if (flagValue > 0) { // Any given width. - return Math.round( - TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, flagValue, - context.getResources().getDisplayMetrics())); + return calculateDialogWidthWithInsets(dialog, flagValue); } else { - // By default we use the same width as the notification shade in portrait mode (504dp). - return context.getResources().getDimensionPixelSize(R.dimen.large_dialog_width); + // By default we use the same width as the notification shade in portrait mode. + int width = context.getResources().getDimensionPixelSize(R.dimen.large_dialog_width); + if (width > 0) { + // If we are neither WRAP_CONTENT or MATCH_PARENT, add the background insets so that + // the dialog is the desired width. + width += getHorizontalInsets(dialog); + } + return width; } } + /** + * Return the pixel width {@param dialog} should be so that it is {@param widthInDp} wide, + * taking its background insets into consideration. + */ + private static int calculateDialogWidthWithInsets(Dialog dialog, int widthInDp) { + float widthInPixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, widthInDp, + dialog.getContext().getResources().getDisplayMetrics()); + return Math.round(widthInPixels + getHorizontalInsets(dialog)); + } + + private static int getHorizontalInsets(Dialog dialog) { + if (dialog.getWindow().getDecorView() == null) { + return 0; + } + + Drawable background = dialog.getWindow().getDecorView().getBackground(); + Insets insets = background != null ? background.getOpticalInsets() : Insets.NONE; + return insets.left + insets.right; + } + private static int getDefaultDialogHeight() { return ViewGroup.LayoutParams.WRAP_CONTENT; }