diff --git a/layer-core/src/main/java/per/goweii/layer/core/DecorLayer.java b/layer-core/src/main/java/per/goweii/layer/core/DecorLayer.java index 4a2dd5a..c50d90a 100644 --- a/layer-core/src/main/java/per/goweii/layer/core/DecorLayer.java +++ b/layer-core/src/main/java/per/goweii/layer/core/DecorLayer.java @@ -14,7 +14,6 @@ import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; -import androidx.core.view.WindowInsetsControllerCompat; import per.goweii.layer.core.utils.Utils; @@ -25,7 +24,6 @@ public class DecorLayer extends FrameLayer { private final Rect mTempRect = new Rect(); private Runnable mShowRunnable = null; - private WindowInsetsChangedListener mWindowInsetsChangedListener = null; public DecorLayer(@NonNull Context context) { this(Utils.requireActivity(context)); @@ -87,28 +85,16 @@ public LayoutInflater getLayoutInflater() { @Override protected void onAttach() { super.onAttach(); - Rect decorInsets = getDecorInsets(); - fitDecorInsets(decorInsets); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - WindowInsetsControllerCompat windowInsetsController = ViewCompat.getWindowInsetsController(getActivity().getWindow().getDecorView()); - if (windowInsetsController != null) { - if (mWindowInsetsChangedListener == null) { - mWindowInsetsChangedListener = new WindowInsetsChangedListener(); - } - windowInsetsController.addOnControllableInsetsChangedListener(mWindowInsetsChangedListener); - } - } + getDecorInsets(mInsets); + fitDecorInsets(mInsets); } @CallSuper @Override protected void onDetach() { super.onDetach(); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - WindowInsetsControllerCompat windowInsetsController = ViewCompat.getWindowInsetsController(getActivity().getWindow().getDecorView()); - if (windowInsetsController != null && mWindowInsetsChangedListener != null) { - windowInsetsController.removeOnControllableInsetsChangedListener(mWindowInsetsChangedListener); - } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + ViewCompat.setOnApplyWindowInsetsListener(getViewHolder().getChild(), null); } } @@ -118,39 +104,50 @@ protected void onConfigurationChanged(@NonNull Configuration newConfig) { Utils.onViewLayout(getViewHolder().getChild(), new Runnable() { @Override public void run() { - Rect decorInsets = getDecorInsets(); - fitDecorInsets(decorInsets); + if (!mActivity.isDestroyed() && isShown()) { + getDecorInsets(mInsets); + fitDecorInsets(mInsets); + } } }); } - protected void fitDecorInsets(@NonNull Rect insets) { - getViewHolder().getParent().setClipToPadding(false); - getViewHolder().getParent().setClipChildren(false); - Utils.setViewPadding(getViewHolder().getParent(), insets); + @Override + protected void onGlobalLayout() { + super.onGlobalLayout(); + if (!mActivity.isDestroyed() && isShown()) { + getDecorInsets(mInsets); + fitDecorInsets(mInsets); + } } - @NonNull - protected final Rect getDecorInsets() { - mInsets.setEmpty(); + protected final void getDecorInsets(@NonNull Rect insets) { + insets.setEmpty(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - WindowInsetsCompat windowInsets = ViewCompat.getRootWindowInsets(getActivity().getWindow().getDecorView()); - Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars()); - mInsets.set(insets.left, insets.top, insets.right, insets.bottom); + WindowInsetsCompat windowInsets = ViewCompat.getRootWindowInsets(getViewHolder().getDecor()); + if (windowInsets != null) { + Insets realInsets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.ime() | WindowInsetsCompat.Type.displayCutout()); + insets.set(realInsets.left, realInsets.top, realInsets.right, realInsets.bottom); + } } else { Utils.getViewMargin(getViewHolder().getDecorChild(), mTempRect); - mInsets.set(mTempRect); + insets.set(mTempRect); Utils.getViewPadding(getViewHolder().getDecorChild(), mTempRect); - mInsets.left += mTempRect.left; - mInsets.top += mTempRect.top; - mInsets.right += mTempRect.right; - mInsets.bottom += mTempRect.bottom; + insets.left += mTempRect.left; + insets.top += mTempRect.top; + insets.right += mTempRect.right; + insets.bottom += mTempRect.bottom; int statusBarHeightIfVisible = Utils.getStatusBarHeightIfVisible(getActivity()); - if (mInsets.top < statusBarHeightIfVisible) { - mInsets.top = statusBarHeightIfVisible; + if (insets.top < statusBarHeightIfVisible) { + insets.top = statusBarHeightIfVisible; } } - return mInsets; + } + + protected void fitDecorInsets(@NonNull Rect insets) { + getViewHolder().getParent().setClipToPadding(false); + getViewHolder().getParent().setClipChildren(false); + Utils.setViewPadding(getViewHolder().getParent(), insets); } public void showImmediately(boolean withAnim) { @@ -185,16 +182,6 @@ public void dismiss(boolean withAnim) { } } - private class WindowInsetsChangedListener implements WindowInsetsControllerCompat.OnControllableInsetsChangedListener { - @Override - public void onControllableInsetsChanged(@NonNull WindowInsetsControllerCompat controller, int typeMask) { - if (isShown()) { - Rect decorInsets = getDecorInsets(); - fitDecorInsets(decorInsets); - } - } - } - public static class ViewHolder extends FrameLayer.ViewHolder { private FrameLayout mActivityContent; private View mDecorChild; diff --git a/layer-guide/src/main/java/per/goweii/layer/guide/GuideLayer.java b/layer-guide/src/main/java/per/goweii/layer/guide/GuideLayer.java index 92817ec..7bba2ea 100644 --- a/layer-guide/src/main/java/per/goweii/layer/guide/GuideLayer.java +++ b/layer-guide/src/main/java/per/goweii/layer/guide/GuideLayer.java @@ -107,7 +107,9 @@ protected void onAttach() { Utils.onViewLayout(getViewHolder().getChild(), new Runnable() { @Override public void run() { - updateLocation(); + if (isShown()) { + updateLocation(); + } } }); } diff --git a/layer-popup/src/main/java/per/goweii/layer/popup/PopupLayer.java b/layer-popup/src/main/java/per/goweii/layer/popup/PopupLayer.java index f20171b..803822a 100644 --- a/layer-popup/src/main/java/per/goweii/layer/popup/PopupLayer.java +++ b/layer-popup/src/main/java/per/goweii/layer/popup/PopupLayer.java @@ -86,7 +86,9 @@ protected void onAttach() { Utils.getViewSize(getViewHolder().getContainer(), new Runnable() { @Override public void run() { - updateLocation(); + if (isShown()) { + updateLocation(); + } } }); ViewTreeObserver viewTreeObserver = getViewHolder().getParent().getViewTreeObserver(); @@ -100,7 +102,9 @@ public void onScrollChanged() { if (getConfig().mOnViewTreeScrollChangedListener != null) { getConfig().mOnViewTreeScrollChangedListener.onScrollChanged(); } - updateLocation(); + if (isShown()) { + updateLocation(); + } } }; viewTreeObserver.addOnScrollChangedListener(mOnScrollChangedListener); @@ -173,7 +177,9 @@ protected void fitDecorInsets(@NonNull Rect insets) { Utils.onViewLayout(getViewHolder().getDecor(), new Runnable() { @Override public void run() { - updateLocation(); + if (isShown()) { + updateLocation(); + } } }); } @@ -390,7 +396,9 @@ private void initContentLocation(int targetX, int targetY, int targetWidth, int Utils.onViewLayout(getViewHolder().getContentWrapper(), new Runnable() { @Override public void run() { - updateLocation(); + if (isShown()) { + updateLocation(); + } } }); } @@ -478,7 +486,9 @@ private void initBackgroundLocation() { Utils.onViewLayout(getViewHolder().getBackground(), new Runnable() { @Override public void run() { - updateLocation(); + if (isShown()) { + updateLocation(); + } } }); } diff --git a/simple/src/main/AndroidManifest.xml b/simple/src/main/AndroidManifest.xml index f262f1f..de218fb 100644 --- a/simple/src/main/AndroidManifest.xml +++ b/simple/src/main/AndroidManifest.xml @@ -20,6 +20,7 @@ + +