1
+ package me .rainbow .NativeModules .NavbarHeight ;
2
+
3
+ import androidx .annotation .NonNull ;
4
+
5
+ import com .facebook .react .bridge .Promise ;
6
+ import com .facebook .react .bridge .ReactApplicationContext ;
7
+ import com .facebook .react .bridge .ReactContextBaseJavaModule ;
8
+ import com .facebook .react .bridge .ReactMethod ;
9
+ import com .facebook .react .module .annotations .ReactModule ;
10
+ import android .graphics .Point ;
11
+ import android .view .WindowManager ;
12
+ import android .view .Display ;
13
+ import java .lang .IllegalAccessException ;
14
+ import java .lang .reflect .InvocationTargetException ;
15
+ import java .lang .NoSuchMethodException ;
16
+ import android .view .WindowInsets ;
17
+ import android .os .Build ;
18
+ import android .content .Context ;
19
+
20
+ @ ReactModule (name = NavbarHeightModule .NAME )
21
+ public class NavbarHeightModule extends ReactContextBaseJavaModule {
22
+ public static final String NAME = "NavbarHeight" ;
23
+
24
+ public NavbarHeightModule (ReactApplicationContext reactContext ) {
25
+ super (reactContext );
26
+ }
27
+
28
+ @ Override
29
+ @ NonNull
30
+ public String getName () {
31
+ return NAME ;
32
+ }
33
+
34
+ // Example method
35
+ // See https://reactnative.dev/docs/native-modules-android
36
+ @ ReactMethod
37
+ public double getNavigationBarHeightSync () {
38
+ Context context = getReactApplicationContext ();
39
+ WindowManager windowManager = (WindowManager ) context .getSystemService (Context .WINDOW_SERVICE );
40
+ if (Build .VERSION .SDK_INT >= 30 ) {
41
+ return windowManager
42
+ .getCurrentWindowMetrics ()
43
+ .getWindowInsets ()
44
+ .getInsets (WindowInsets .Type .navigationBars ())
45
+ .bottom ;
46
+ } else {
47
+ Point appUsableSize = getAppUsableScreenSize (context );
48
+ Point realScreenSize = getRealScreenSize (context );
49
+
50
+ // navigation bar on the side
51
+ if (appUsableSize .x < realScreenSize .x ) {
52
+ return appUsableSize .y ;
53
+ }
54
+
55
+ // navigation bar at the bottom
56
+ if (appUsableSize .y < realScreenSize .y ) {
57
+ return realScreenSize .y - appUsableSize .y ;
58
+ }
59
+
60
+ // navigation bar is not present
61
+ return 0 ;
62
+ }
63
+ }
64
+ public Point getAppUsableScreenSize (Context context ) {
65
+ WindowManager windowManager = (WindowManager ) context .getSystemService (Context .WINDOW_SERVICE );
66
+ Display display = windowManager .getDefaultDisplay ();
67
+ Point size = new Point ();
68
+ display .getSize (size );
69
+ return size ;
70
+ }
71
+ public Point getRealScreenSize (Context context ) {
72
+ WindowManager windowManager = (WindowManager ) context .getSystemService (Context .WINDOW_SERVICE );
73
+ Display display = windowManager .getDefaultDisplay ();
74
+ Point size = new Point ();
75
+
76
+ if (Build .VERSION .SDK_INT >= 17 ) {
77
+ display .getRealSize (size );
78
+ } else if (Build .VERSION .SDK_INT >= 14 ) {
79
+ try {
80
+ size .x = (Integer ) Display .class .getMethod ("getRawWidth" ).invoke (display );
81
+ size .y = (Integer ) Display .class .getMethod ("getRawHeight" ).invoke (display );
82
+ } catch (IllegalAccessException e ) {} catch (InvocationTargetException e ) {} catch (NoSuchMethodException e ) {}
83
+ }
84
+
85
+ return size ;
86
+ }
87
+ @ ReactMethod (isBlockingSynchronousMethod = true )
88
+ public double getNavigationBarHeight () {
89
+ return getNavigationBarHeightSync ();
90
+ }
91
+ }
0 commit comments