14
14
import androidx .core .graphics .Insets ;
15
15
import androidx .core .view .ViewCompat ;
16
16
import androidx .core .view .WindowInsetsCompat ;
17
- import androidx .core .view .WindowInsetsControllerCompat ;
18
17
19
18
import per .goweii .layer .core .utils .Utils ;
20
19
@@ -25,7 +24,6 @@ public class DecorLayer extends FrameLayer {
25
24
private final Rect mTempRect = new Rect ();
26
25
27
26
private Runnable mShowRunnable = null ;
28
- private WindowInsetsChangedListener mWindowInsetsChangedListener = null ;
29
27
30
28
public DecorLayer (@ NonNull Context context ) {
31
29
this (Utils .requireActivity (context ));
@@ -87,28 +85,16 @@ public LayoutInflater getLayoutInflater() {
87
85
@ Override
88
86
protected void onAttach () {
89
87
super .onAttach ();
90
- Rect decorInsets = getDecorInsets ();
91
- fitDecorInsets (decorInsets );
92
- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .R ) {
93
- WindowInsetsControllerCompat windowInsetsController = ViewCompat .getWindowInsetsController (getActivity ().getWindow ().getDecorView ());
94
- if (windowInsetsController != null ) {
95
- if (mWindowInsetsChangedListener == null ) {
96
- mWindowInsetsChangedListener = new WindowInsetsChangedListener ();
97
- }
98
- windowInsetsController .addOnControllableInsetsChangedListener (mWindowInsetsChangedListener );
99
- }
100
- }
88
+ getDecorInsets (mInsets );
89
+ fitDecorInsets (mInsets );
101
90
}
102
91
103
92
@ CallSuper
104
93
@ Override
105
94
protected void onDetach () {
106
95
super .onDetach ();
107
- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .R ) {
108
- WindowInsetsControllerCompat windowInsetsController = ViewCompat .getWindowInsetsController (getActivity ().getWindow ().getDecorView ());
109
- if (windowInsetsController != null && mWindowInsetsChangedListener != null ) {
110
- windowInsetsController .removeOnControllableInsetsChangedListener (mWindowInsetsChangedListener );
111
- }
96
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
97
+ ViewCompat .setOnApplyWindowInsetsListener (getViewHolder ().getChild (), null );
112
98
}
113
99
}
114
100
@@ -118,39 +104,50 @@ protected void onConfigurationChanged(@NonNull Configuration newConfig) {
118
104
Utils .onViewLayout (getViewHolder ().getChild (), new Runnable () {
119
105
@ Override
120
106
public void run () {
121
- Rect decorInsets = getDecorInsets ();
122
- fitDecorInsets (decorInsets );
107
+ if (!mActivity .isDestroyed () && isShown ()) {
108
+ getDecorInsets (mInsets );
109
+ fitDecorInsets (mInsets );
110
+ }
123
111
}
124
112
});
125
113
}
126
114
127
- protected void fitDecorInsets (@ NonNull Rect insets ) {
128
- getViewHolder ().getParent ().setClipToPadding (false );
129
- getViewHolder ().getParent ().setClipChildren (false );
130
- Utils .setViewPadding (getViewHolder ().getParent (), insets );
115
+ @ Override
116
+ protected void onGlobalLayout () {
117
+ super .onGlobalLayout ();
118
+ if (!mActivity .isDestroyed () && isShown ()) {
119
+ getDecorInsets (mInsets );
120
+ fitDecorInsets (mInsets );
121
+ }
131
122
}
132
123
133
- @ NonNull
134
- protected final Rect getDecorInsets () {
135
- mInsets .setEmpty ();
124
+ protected final void getDecorInsets (@ NonNull Rect insets ) {
125
+ insets .setEmpty ();
136
126
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
137
- WindowInsetsCompat windowInsets = ViewCompat .getRootWindowInsets (getActivity ().getWindow ().getDecorView ());
138
- Insets insets = windowInsets .getInsets (WindowInsetsCompat .Type .systemBars ());
139
- mInsets .set (insets .left , insets .top , insets .right , insets .bottom );
127
+ WindowInsetsCompat windowInsets = ViewCompat .getRootWindowInsets (getViewHolder ().getDecor ());
128
+ if (windowInsets != null ) {
129
+ Insets realInsets = windowInsets .getInsets (WindowInsetsCompat .Type .systemBars () | WindowInsetsCompat .Type .ime () | WindowInsetsCompat .Type .displayCutout ());
130
+ insets .set (realInsets .left , realInsets .top , realInsets .right , realInsets .bottom );
131
+ }
140
132
} else {
141
133
Utils .getViewMargin (getViewHolder ().getDecorChild (), mTempRect );
142
- mInsets .set (mTempRect );
134
+ insets .set (mTempRect );
143
135
Utils .getViewPadding (getViewHolder ().getDecorChild (), mTempRect );
144
- mInsets .left += mTempRect .left ;
145
- mInsets .top += mTempRect .top ;
146
- mInsets .right += mTempRect .right ;
147
- mInsets .bottom += mTempRect .bottom ;
136
+ insets .left += mTempRect .left ;
137
+ insets .top += mTempRect .top ;
138
+ insets .right += mTempRect .right ;
139
+ insets .bottom += mTempRect .bottom ;
148
140
int statusBarHeightIfVisible = Utils .getStatusBarHeightIfVisible (getActivity ());
149
- if (mInsets .top < statusBarHeightIfVisible ) {
150
- mInsets .top = statusBarHeightIfVisible ;
141
+ if (insets .top < statusBarHeightIfVisible ) {
142
+ insets .top = statusBarHeightIfVisible ;
151
143
}
152
144
}
153
- return mInsets ;
145
+ }
146
+
147
+ protected void fitDecorInsets (@ NonNull Rect insets ) {
148
+ getViewHolder ().getParent ().setClipToPadding (false );
149
+ getViewHolder ().getParent ().setClipChildren (false );
150
+ Utils .setViewPadding (getViewHolder ().getParent (), insets );
154
151
}
155
152
156
153
public void showImmediately (boolean withAnim ) {
@@ -185,16 +182,6 @@ public void dismiss(boolean withAnim) {
185
182
}
186
183
}
187
184
188
- private class WindowInsetsChangedListener implements WindowInsetsControllerCompat .OnControllableInsetsChangedListener {
189
- @ Override
190
- public void onControllableInsetsChanged (@ NonNull WindowInsetsControllerCompat controller , int typeMask ) {
191
- if (isShown ()) {
192
- Rect decorInsets = getDecorInsets ();
193
- fitDecorInsets (decorInsets );
194
- }
195
- }
196
- }
197
-
198
185
public static class ViewHolder extends FrameLayer .ViewHolder {
199
186
private FrameLayout mActivityContent ;
200
187
private View mDecorChild ;
0 commit comments