@@ -29,9 +29,10 @@ Licensed to the Apache Software Foundation (ASF) under one
29
29
import android .content .DialogInterface ;
30
30
import android .content .Intent ;
31
31
import android .content .res .Configuration ;
32
- import android .graphics .Color ;
33
32
import android .media .AudioManager ;
33
+ import android .os .Build ;
34
34
import android .os .Bundle ;
35
+ import android .view .Gravity ;
35
36
import android .view .Menu ;
36
37
import android .view .MenuItem ;
37
38
import android .view .View ;
@@ -42,7 +43,11 @@ Licensed to the Apache Software Foundation (ASF) under one
42
43
import android .widget .FrameLayout ;
43
44
44
45
import androidx .appcompat .app .AppCompatActivity ;
46
+ import androidx .core .graphics .Insets ;
45
47
import androidx .core .splashscreen .SplashScreen ;
48
+ import androidx .core .view .ViewCompat ;
49
+ import androidx .core .view .WindowCompat ;
50
+ import androidx .core .view .WindowInsetsCompat ;
46
51
47
52
/**
48
53
* This class is the main Android activity that represents the Cordova
@@ -100,6 +105,9 @@ public class CordovaActivity extends AppCompatActivity {
100
105
101
106
private SplashScreen splashScreen ;
102
107
108
+ private boolean canEdgeToEdge = false ;
109
+ private boolean isFullScreen = false ;
110
+
103
111
/**
104
112
* Called when the activity is first created.
105
113
*/
@@ -113,6 +121,9 @@ public void onCreate(Bundle savedInstanceState) {
113
121
// need to activate preferences before super.onCreate to avoid "requestFeature() must be called before adding content" exception
114
122
loadConfig ();
115
123
124
+ canEdgeToEdge = preferences .getBoolean ("AndroidEdgeToEdge" , false )
125
+ && Build .VERSION .SDK_INT >= Build .VERSION_CODES .VANILLA_ICE_CREAM ;
126
+
116
127
String logLevel = preferences .getString ("loglevel" , "ERROR" );
117
128
LOG .setLogLevel (logLevel );
118
129
@@ -127,7 +138,10 @@ public void onCreate(Bundle savedInstanceState) {
127
138
LOG .d (TAG , "The SetFullscreen configuration is deprecated in favor of Fullscreen, and will be removed in a future version." );
128
139
preferences .set ("Fullscreen" , true );
129
140
}
130
- if (preferences .getBoolean ("Fullscreen" , false )) {
141
+
142
+ isFullScreen = preferences .getBoolean ("Fullscreen" , false );
143
+
144
+ if (isFullScreen ) {
131
145
// NOTE: use the FullscreenNotImmersive configuration key to set the activity in a REAL full screen
132
146
// (as was the case in previous cordova versions)
133
147
if (!preferences .getBoolean ("FullscreenNotImmersive" , false )) {
@@ -184,26 +198,56 @@ protected void loadConfig() {
184
198
//Suppressing warnings in AndroidStudio
185
199
@ SuppressWarnings ({"deprecation" , "ResourceType" })
186
200
protected void createViews () {
187
- //Why are we setting a constant as the ID? This should be investigated
188
- appView .getView ().setId (100 );
189
- appView .getView ().setLayoutParams (new FrameLayout .LayoutParams (
201
+ WindowCompat .setDecorFitsSystemWindows (getWindow (), false );
202
+
203
+ // Root FrameLayout
204
+ FrameLayout rootLayout = new FrameLayout (this );
205
+ rootLayout .setLayoutParams (new FrameLayout .LayoutParams (
190
206
ViewGroup .LayoutParams .MATCH_PARENT ,
191
- ViewGroup .LayoutParams .MATCH_PARENT ));
207
+ ViewGroup .LayoutParams .MATCH_PARENT
208
+ ));
192
209
193
- setContentView (appView .getView ());
210
+ // WebView
211
+ View webView = appView .getView ();
212
+ webView .setLayoutParams (new FrameLayout .LayoutParams (
213
+ ViewGroup .LayoutParams .MATCH_PARENT ,
214
+ ViewGroup .LayoutParams .MATCH_PARENT
215
+ ));
216
+
217
+ // Create StatusBar view that will overlay the top inset
218
+ View statusBarView = new View (this );
219
+ statusBarView .setTag ("statusBarView" );
220
+
221
+ // Handle Window Insets
222
+ ViewCompat .setOnApplyWindowInsetsListener (rootLayout , (v , insets ) -> {
223
+ Insets bars = insets .getInsets (
224
+ WindowInsetsCompat .Type .systemBars () | WindowInsetsCompat .Type .displayCutout ()
225
+ );
226
+
227
+ boolean isStatusBarVisible = statusBarView .getVisibility () != View .GONE ;
228
+ int top = isStatusBarVisible && !canEdgeToEdge && !isFullScreen ? bars .top : 0 ;
229
+ int bottom = !canEdgeToEdge && !isFullScreen ? bars .bottom : 0 ;
230
+
231
+ FrameLayout .LayoutParams webViewParams = (FrameLayout .LayoutParams ) webView .getLayoutParams ();
232
+ webViewParams .setMargins (bars .left , top , bars .right , bottom );
233
+ webView .setLayoutParams (webViewParams );
234
+
235
+ FrameLayout .LayoutParams statusBarParams = new FrameLayout .LayoutParams (
236
+ ViewGroup .LayoutParams .MATCH_PARENT ,
237
+ top ,
238
+ Gravity .TOP
239
+ );
240
+ statusBarView .setLayoutParams (statusBarParams );
241
+
242
+ return insets ;
243
+ });
194
244
195
- if (preferences .contains ("BackgroundColor" )) {
196
- try {
197
- int backgroundColor = preferences .getInteger ("BackgroundColor" , Color .BLACK );
198
- // Background of activity:
199
- appView .getView ().setBackgroundColor (backgroundColor );
200
- }
201
- catch (NumberFormatException e ){
202
- e .printStackTrace ();
203
- }
204
- }
245
+ rootLayout .addView (webView );
246
+ rootLayout .addView (statusBarView );
205
247
206
- appView .getView ().requestFocusFromTouch ();
248
+ setContentView (rootLayout );
249
+ rootLayout .post (() -> ViewCompat .requestApplyInsets (rootLayout ));
250
+ webView .requestFocusFromTouch ();
207
251
}
208
252
209
253
/**
0 commit comments