From 655cf0f174ed78e4163bc160e85cb19289a00a72 Mon Sep 17 00:00:00 2001 From: Dmitry Strekha Date: Tue, 17 Dec 2024 15:02:40 +0300 Subject: [PATCH] [Android] Fix for wrong ime insets on some devices (#9786) * [Android] Possible fix for wrong ime insets on some devices * [Android] Allow changing default insets type mask with a Builder * [Android] Change insets type to exclude ime insets Signed-off-by: Dzmitry Strekha --- .../maplayer/MapButtonsController.java | 16 +++- .../routing/RoutingPlanController.java | 7 +- .../organicmaps/util/WindowInsetUtils.java | 82 ++++++++++++++++++- .../widget/placepage/PlacePageButtons.java | 7 +- 4 files changed, 105 insertions(+), 7 deletions(-) diff --git a/android/app/src/main/java/app/organicmaps/maplayer/MapButtonsController.java b/android/app/src/main/java/app/organicmaps/maplayer/MapButtonsController.java index e3ae526f3b..8f53e4387d 100644 --- a/android/app/src/main/java/app/organicmaps/maplayer/MapButtonsController.java +++ b/android/app/src/main/java/app/organicmaps/maplayer/MapButtonsController.java @@ -13,6 +13,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.OptIn; import androidx.core.view.ViewCompat; +import androidx.core.view.WindowInsetsCompat; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentActivity; import androidx.lifecycle.Observer; @@ -31,7 +32,6 @@ import app.organicmaps.util.Config; import app.organicmaps.util.ThemeUtils; import app.organicmaps.util.UiUtils; import app.organicmaps.util.WindowInsetUtils; -import app.organicmaps.util.WindowInsetUtils.PaddingInsetsListener; import app.organicmaps.widget.menu.MyPositionButton; import app.organicmaps.widget.placepage.PlacePageViewModel; import com.google.android.material.badge.BadgeDrawable; @@ -157,8 +157,6 @@ public class MapButtonsController extends Fragment mButtonsMap.put(MapButtons.menu, menuButton); if (helpButton != null) mButtonsMap.put(MapButtons.help, helpButton); - - ViewCompat.setOnApplyWindowInsetsListener(mFrame, PaddingInsetsListener.allSides()); return mFrame; } @@ -362,6 +360,18 @@ public class MapButtonsController extends Fragment mSearchWheel.onResume(); updateMenuBadge(); updateLayerButton(); + final WindowInsetUtils.PaddingInsetsListener insetsListener = new WindowInsetUtils.PaddingInsetsListener.Builder() + .setInsetsTypeMask(WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.displayCutout()) + .setAllSides() + .build(); + ViewCompat.setOnApplyWindowInsetsListener(mFrame, insetsListener); + } + + @Override + public void onPause() + { + ViewCompat.setOnApplyWindowInsetsListener(mFrame, null); + super.onPause(); } @Override diff --git a/android/app/src/main/java/app/organicmaps/routing/RoutingPlanController.java b/android/app/src/main/java/app/organicmaps/routing/RoutingPlanController.java index deadbcab41..691681690e 100644 --- a/android/app/src/main/java/app/organicmaps/routing/RoutingPlanController.java +++ b/android/app/src/main/java/app/organicmaps/routing/RoutingPlanController.java @@ -12,6 +12,7 @@ import androidx.annotation.IdRes; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.view.ViewCompat; +import androidx.core.view.WindowInsetsCompat; import app.organicmaps.Framework; import app.organicmaps.MwmApplication; import app.organicmaps.R; @@ -108,7 +109,11 @@ public class RoutingPlanController extends ToolbarController .getResources().getInteger(R.integer.anim_default); final View menuFrame = activity.findViewById(R.id.menu_frame); - ViewCompat.setOnApplyWindowInsetsListener(menuFrame, PaddingInsetsListener.excludeTop()); + final PaddingInsetsListener insetsListener = new PaddingInsetsListener.Builder() + .setInsetsTypeMask(WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.displayCutout()) + .setExcludeTop() + .build(); + ViewCompat.setOnApplyWindowInsetsListener(menuFrame, insetsListener); } @NonNull diff --git a/android/app/src/main/java/app/organicmaps/util/WindowInsetUtils.java b/android/app/src/main/java/app/organicmaps/util/WindowInsetUtils.java index 9bde7da119..f366562223 100644 --- a/android/app/src/main/java/app/organicmaps/util/WindowInsetUtils.java +++ b/android/app/src/main/java/app/organicmaps/util/WindowInsetUtils.java @@ -138,19 +138,29 @@ public final class WindowInsetUtils public static final class PaddingInsetsListener implements OnApplyWindowInsetsListener { + private final int insetsTypeMask; private final boolean top; private final boolean bottom; private final boolean left; private final boolean right; - public PaddingInsetsListener(boolean top, boolean bottom, boolean left, boolean right) + public PaddingInsetsListener(int insetsTypeMask, boolean top, boolean bottom, boolean left, boolean right) { + this.insetsTypeMask = insetsTypeMask; this.top = top; this.bottom = bottom; this.left = left; this.right = right; } + /** + * Creates PaddingInsetsListener with default insetsTypeMask equals TYPE_SAFE_DRAWING + */ + public PaddingInsetsListener(boolean top, boolean bottom, boolean left, boolean right) + { + this(TYPE_SAFE_DRAWING, top, bottom, left, right); + } + public static PaddingInsetsListener allSides() { return new PaddingInsetsListener(true, true, true, true); @@ -170,7 +180,7 @@ public final class WindowInsetUtils @Override public WindowInsetsCompat onApplyWindowInsets(@NonNull View v, @NonNull WindowInsetsCompat windowInsets) { - final Insets insets = windowInsets.getInsets(TYPE_SAFE_DRAWING); + final Insets insets = windowInsets.getInsets(insetsTypeMask); v.setPadding( left ? insets.left : v.getPaddingLeft(), top ? insets.top : v.getPaddingTop(), @@ -178,5 +188,73 @@ public final class WindowInsetUtils bottom ? insets.bottom : v.getPaddingBottom()); return windowInsets; } + + public static class Builder { + + private int mInsetsTypeMask = TYPE_SAFE_DRAWING; + private boolean mTop; + private boolean mBottom; + private boolean mLeft; + private boolean mRight; + + public Builder setInsetsTypeMask(int insetsTypeMask) + { + mInsetsTypeMask = insetsTypeMask; + return this; + } + + public Builder setAllSides() { + mTop = true; + mBottom = true; + mLeft = true; + mRight = true; + return this; + } + + public Builder setExcludeTop() { + mTop = false; + mBottom = true; + mLeft = true; + mRight = true; + return this; + } + + public Builder setExcludeBottom() { + mTop = true; + mBottom = false; + mLeft = true; + mRight = true; + return this; + } + + public Builder setTop(boolean top) + { + mTop = top; + return this; + } + + public Builder setBottom(boolean bottom) + { + mBottom = bottom; + return this; + } + + public Builder setLeft(boolean left) + { + mLeft = left; + return this; + } + + public Builder setRight(boolean right) + { + mRight = right; + return this; + } + + public PaddingInsetsListener build() + { + return new PaddingInsetsListener(mInsetsTypeMask, mTop, mBottom, mLeft, mRight); + } + } } } diff --git a/android/app/src/main/java/app/organicmaps/widget/placepage/PlacePageButtons.java b/android/app/src/main/java/app/organicmaps/widget/placepage/PlacePageButtons.java index d4fd663135..5fd7ec6b26 100644 --- a/android/app/src/main/java/app/organicmaps/widget/placepage/PlacePageButtons.java +++ b/android/app/src/main/java/app/organicmaps/widget/placepage/PlacePageButtons.java @@ -11,6 +11,7 @@ import androidx.annotation.AttrRes; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.view.ViewCompat; +import androidx.core.view.WindowInsetsCompat; import androidx.fragment.app.Fragment; import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProvider; @@ -44,7 +45,11 @@ public final class PlacePageButtons extends Fragment implements Observer