@@ -46,6 +46,7 @@ Licensed to the Apache Software Foundation (ASF) under one
46
46
import androidx .core .graphics .Insets ;
47
47
import androidx .core .splashscreen .SplashScreen ;
48
48
import androidx .core .view .ViewCompat ;
49
+ import androidx .core .view .WindowCompat ;
49
50
import androidx .core .view .WindowInsetsCompat ;
50
51
51
52
/**
@@ -104,6 +105,9 @@ public class CordovaActivity extends AppCompatActivity {
104
105
105
106
private SplashScreen splashScreen ;
106
107
108
+ private boolean canEdgeToEdge = false ;
109
+ private boolean isFullScreen = false ;
110
+
107
111
/**
108
112
* Called when the activity is first created.
109
113
*/
@@ -115,6 +119,9 @@ public void onCreate(Bundle savedInstanceState) {
115
119
// need to activate preferences before super.onCreate to avoid "requestFeature() must be called before adding content" exception
116
120
loadConfig ();
117
121
122
+ canEdgeToEdge = preferences .getBoolean ("AndroidEdgeToEdge" , false )
123
+ && Build .VERSION .SDK_INT >= Build .VERSION_CODES .VANILLA_ICE_CREAM ;
124
+
118
125
String logLevel = preferences .getString ("loglevel" , "ERROR" );
119
126
LOG .setLogLevel (logLevel );
120
127
@@ -129,7 +136,10 @@ public void onCreate(Bundle savedInstanceState) {
129
136
LOG .d (TAG , "The SetFullscreen configuration is deprecated in favor of Fullscreen, and will be removed in a future version." );
130
137
preferences .set ("Fullscreen" , true );
131
138
}
132
- if (preferences .getBoolean ("Fullscreen" , false )) {
139
+
140
+ isFullScreen = preferences .getBoolean ("Fullscreen" , false );
141
+
142
+ if (isFullScreen ) {
133
143
// NOTE: use the FullscreenNotImmersive configuration key to set the activity in a REAL full screen
134
144
// (as was the case in previous cordova versions)
135
145
if (!preferences .getBoolean ("FullscreenNotImmersive" , false )) {
@@ -184,6 +194,8 @@ protected void loadConfig() {
184
194
//Suppressing warnings in AndroidStudio
185
195
@ SuppressWarnings ({"deprecation" , "ResourceType" })
186
196
protected void createViews () {
197
+ WindowCompat .setDecorFitsSystemWindows (getWindow (), false );
198
+
187
199
// Root FrameLayout
188
200
FrameLayout rootLayout = new FrameLayout (this );
189
201
rootLayout .setLayoutParams (new FrameLayout .LayoutParams (
@@ -198,47 +210,39 @@ protected void createViews() {
198
210
ViewGroup .LayoutParams .MATCH_PARENT
199
211
));
200
212
201
- // Enable/Disable AndroidEdgeToEdge (Supported in SDK >= 35)
202
- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .VANILLA_ICE_CREAM
203
- && !preferences .getBoolean ("AndroidEdgeToEdge" , false )
204
- ) {
205
- // Create StatusBar view that will overlay the top inset
206
- View statusBarView = new View (this );
207
- statusBarView .setTag ("statusBarView" );
208
-
209
- // Add statusBarView to root layout.
210
- rootLayout .addView (statusBarView );
211
-
212
- // Handle Window Insets
213
- ViewCompat .setOnApplyWindowInsetsListener (rootLayout , (v , insets ) -> {
214
- Insets bars = insets .getInsets (
215
- WindowInsetsCompat .Type .systemBars ()
216
- | WindowInsetsCompat .Type .displayCutout ()
217
- );
218
-
219
- boolean isStatusBarVisible = statusBarView .getVisibility () != View .GONE ;
220
- int top = isStatusBarVisible ? bars .top : 0 ;
221
-
222
- // Update the WebView's Margin LayoutParams
223
- FrameLayout .LayoutParams newParams = (FrameLayout .LayoutParams ) webView .getLayoutParams ();
224
- newParams .setMargins (bars .left , top , bars .right , bars .bottom );
225
- webView .setLayoutParams (newParams );
226
-
227
- // Position the status bar view to overlay the top inset areas
228
- statusBarView .setLayoutParams (new FrameLayout .LayoutParams (
229
- ViewGroup .LayoutParams .MATCH_PARENT ,
230
- top ,
231
- Gravity .TOP
232
- ));
233
-
234
- return WindowInsetsCompat .CONSUMED ;
235
- });
213
+ // Create StatusBar view that will overlay the top inset
214
+ View statusBarView = new View (this );
215
+ statusBarView .setTag ("statusBarView" );
236
216
237
- ViewCompat .requestApplyInsets (rootLayout );
238
- }
217
+ // Handle Window Insets
218
+ ViewCompat .setOnApplyWindowInsetsListener (rootLayout , (v , insets ) -> {
219
+ Insets bars = insets .getInsets (
220
+ WindowInsetsCompat .Type .systemBars () | WindowInsetsCompat .Type .displayCutout ()
221
+ );
222
+
223
+ boolean isStatusBarVisible = statusBarView .getVisibility () != View .GONE ;
224
+ int top = isStatusBarVisible && !canEdgeToEdge && !isFullScreen ? bars .top : 0 ;
225
+ int bottom = !canEdgeToEdge && !isFullScreen ? bars .bottom : 0 ;
226
+
227
+ FrameLayout .LayoutParams webViewParams = (FrameLayout .LayoutParams ) webView .getLayoutParams ();
228
+ webViewParams .setMargins (bars .left , top , bars .right , bottom );
229
+ webView .setLayoutParams (webViewParams );
230
+
231
+ FrameLayout .LayoutParams statusBarParams = new FrameLayout .LayoutParams (
232
+ ViewGroup .LayoutParams .MATCH_PARENT ,
233
+ top ,
234
+ Gravity .TOP
235
+ );
236
+ statusBarView .setLayoutParams (statusBarParams );
237
+
238
+ return WindowInsetsCompat .CONSUMED ;
239
+ });
239
240
240
241
rootLayout .addView (webView );
242
+ rootLayout .addView (statusBarView );
243
+
241
244
setContentView (rootLayout );
245
+ rootLayout .post (() -> ViewCompat .requestApplyInsets (rootLayout ));
242
246
webView .requestFocusFromTouch ();
243
247
}
244
248
0 commit comments