15
15
import android .view .ViewGroup .MarginLayoutParams ;
16
16
import android .view .Window ;
17
17
import android .view .WindowManager ;
18
+ import android .view .WindowInsets ;
19
+ import android .view .WindowInsetsController ;
18
20
import android .widget .LinearLayout ;
19
21
import android .webkit .ConsoleMessage ;
20
22
import android .webkit .JavascriptInterface ;
@@ -34,66 +36,15 @@ public class WebViewJNI {
34
36
35
37
private Activity activity ;
36
38
private static WebViewInfo [] infos ;
39
+ private boolean immersiveMode = false ;
40
+ private boolean displayCutout = false ;
37
41
38
42
public native void onPageLoading (String url , int webview_id , int request_id );
39
43
public native void onPageFinished (String url , int webview_id , int request_id );
40
44
public native void onReceivedError (String url , int webview_id , int request_id , String errorMessage );
41
45
public native void onEvalFinished (String result , int webview_id , int request_id );
42
46
public native void onEvalFailed (String errorMessage , int webview_id , int request_id );
43
47
44
- private static class CustomWebView extends WebView {
45
- private WebViewInfo info ;
46
-
47
- public CustomWebView (Activity context , WebViewInfo info ) {
48
- super (context );
49
- this .info = info ;
50
- }
51
-
52
- // For Android 2.3 able to show Keyboard on input / textarea focus
53
- @ Override
54
- public boolean onTouchEvent (MotionEvent event ) {
55
- switch (event .getAction ()) {
56
- case MotionEvent .ACTION_DOWN :
57
- case MotionEvent .ACTION_UP :
58
- if (!this .hasFocus ()) {
59
- this .requestFocus ();
60
-
61
- }
62
-
63
- if ( Build .VERSION .SDK_INT >= Build .VERSION_CODES .HONEYCOMB ) { // Api level 11
64
- setSystemUiVisibility (
65
- View .SYSTEM_UI_FLAG_LAYOUT_STABLE
66
- //| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
67
- | View .SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
68
- //| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
69
- | View .SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
70
- | View .SYSTEM_UI_FLAG_IMMERSIVE
71
- );
72
- invalidate ();
73
- }
74
- break ;
75
- }
76
- return super .onTouchEvent (event );
77
- }
78
-
79
- @ Override
80
- protected void onLayout (boolean changed , int left , int top , int right , int bottom ) {
81
- super .onLayout (changed , left , top , right , bottom );
82
-
83
- if ( Build .VERSION .SDK_INT >= Build .VERSION_CODES .HONEYCOMB ) { // Api level 11
84
- setSystemUiVisibility (
85
- View .SYSTEM_UI_FLAG_LAYOUT_STABLE
86
- | View .SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
87
- | View .SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
88
- | View .SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
89
- | View .SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
90
- | View .SYSTEM_UI_FLAG_IMMERSIVE
91
- );
92
- invalidate ();
93
- }
94
- }
95
- }
96
-
97
48
private static class CustomWebViewClient extends WebViewClient {
98
49
public Activity activity ;
99
50
public int webviewID ;
@@ -238,7 +189,7 @@ public boolean onConsoleMessage(ConsoleMessage msg) {
238
189
239
190
private class WebViewInfo
240
191
{
241
- CustomWebView webview ;
192
+ WebView webview ;
242
193
CustomWebViewClient webviewClient ;
243
194
CustomWebChromeClient webviewChromeClient ;
244
195
LinearLayout layout ;
@@ -247,29 +198,20 @@ private class WebViewInfo
247
198
int webviewID ;
248
199
};
249
200
250
- public WebViewJNI (Activity activity , int maxnumviews ) {
201
+ public WebViewJNI (Activity activity , int maxnumviews , boolean immersiveMode , boolean displayCutout ) {
251
202
this .activity = activity ;
252
203
this .infos = new WebViewInfo [maxnumviews ];
204
+ this .immersiveMode = immersiveMode ;
205
+ this .displayCutout = displayCutout ;
253
206
}
254
207
255
208
private WebViewInfo createView (Activity activity , int webview_id )
256
209
{
257
210
WebViewInfo info = new WebViewInfo ();
258
211
info .webviewID = webview_id ;
259
- info .webview = new CustomWebView (activity , info );
212
+ info .webview = new WebView (activity );
260
213
info .webview .setVisibility (View .GONE );
261
214
262
- if ( Build .VERSION .SDK_INT >= Build .VERSION_CODES .HONEYCOMB ) { // Api level 11
263
- info .webview .setSystemUiVisibility (
264
- View .SYSTEM_UI_FLAG_LAYOUT_STABLE
265
- //| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
266
- | View .SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
267
- //| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
268
- | View .SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
269
- | View .SYSTEM_UI_FLAG_IMMERSIVE
270
- );
271
- }
272
-
273
215
MarginLayoutParams params = new MarginLayoutParams (LinearLayout .LayoutParams .MATCH_PARENT , LinearLayout .LayoutParams .MATCH_PARENT );
274
216
params .setMargins (0 , 0 , 0 , 0 );
275
217
@@ -308,7 +250,28 @@ private WebViewInfo createView(Activity activity, int webview_id)
308
250
info .windowParams .y = WindowManager .LayoutParams .MATCH_PARENT ;
309
251
info .windowParams .width = WindowManager .LayoutParams .MATCH_PARENT ;
310
252
info .windowParams .height = WindowManager .LayoutParams .MATCH_PARENT ;
311
- info .windowParams .flags = WindowManager .LayoutParams .FLAG_NOT_TOUCH_MODAL ;
253
+ info .windowParams .flags = WindowManager .LayoutParams .FLAG_NOT_FOCUSABLE ; // Fix navigation bar visible briefly when hiding/showing
254
+ info .windowParams .softInputMode = WindowManager .LayoutParams .SOFT_INPUT_ADJUST_NOTHING ;
255
+ if (Build .VERSION .SDK_INT < 30 ) {
256
+ if (immersiveMode ) {
257
+ info .windowParams .systemUiVisibility = View .SYSTEM_UI_FLAG_LAYOUT_STABLE
258
+ | View .SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
259
+ | View .SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
260
+ | View .SYSTEM_UI_FLAG_HIDE_NAVIGATION
261
+ | View .SYSTEM_UI_FLAG_FULLSCREEN
262
+ | View .SYSTEM_UI_FLAG_IMMERSIVE_STICKY ;
263
+ } else {
264
+ info .windowParams .systemUiVisibility = View .SYSTEM_UI_FLAG_LAYOUT_STABLE
265
+ // | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
266
+ | View .SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
267
+ // | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
268
+ | View .SYSTEM_UI_FLAG_FULLSCREEN
269
+ | View .SYSTEM_UI_FLAG_IMMERSIVE_STICKY ;
270
+ }
271
+ }
272
+ if (displayCutout && Build .VERSION .SDK_INT >= 28 ) {
273
+ info .windowParams .layoutInDisplayCutoutMode = WindowManager .LayoutParams .LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES ;
274
+ }
312
275
313
276
info .layout .setLayoutParams (info .windowParams );
314
277
return info ;
@@ -323,6 +286,20 @@ private void setVisibleInternal(WebViewInfo info, int visible)
323
286
info .first = 0 ;
324
287
WindowManager wm = activity .getWindowManager ();
325
288
wm .addView (info .layout , info .windowParams );
289
+
290
+ // Fix navigation bar visible briefly when hiding/showing
291
+ info .windowParams .flags = WindowManager .LayoutParams .FLAG_NOT_TOUCH_MODAL ;
292
+ wm .updateViewLayout (info .layout , info .windowParams );
293
+
294
+ if (Build .VERSION .SDK_INT >= 30 ) {
295
+ WindowInsetsController windowInsetsController = info .layout .getWindowInsetsController ();
296
+ windowInsetsController .setSystemBarsBehavior (WindowInsetsController .BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE );
297
+ if (immersiveMode ) {
298
+ windowInsetsController .hide (WindowInsets .Type .systemBars ());
299
+ } else {
300
+ windowInsetsController .hide (WindowInsets .Type .statusBars ());
301
+ }
302
+ }
326
303
}
327
304
}
328
305
@@ -333,7 +310,7 @@ private void setPositionInternal(WebViewInfo info, int x, int y, int width, int
333
310
info .windowParams .width = width >= 0 ? width : WindowManager .LayoutParams .MATCH_PARENT ;
334
311
info .windowParams .height = height >= 0 ? height : WindowManager .LayoutParams .MATCH_PARENT ;
335
312
336
- if (info .webview .getVisibility () == View .VISIBLE ) {
313
+ if (info .webview .getVisibility () == View .VISIBLE ) {
337
314
WindowManager wm = activity .getWindowManager ();
338
315
wm .updateViewLayout (info .layout , info .windowParams );
339
316
}
0 commit comments