Skip to content

Commit 265bc25

Browse files
authored
Fix Android navigation mode detection (#6053)
* fix android navigation bar height on specific devices * rm unused import * fix call to native module on ios
1 parent 5516162 commit 265bc25

File tree

9 files changed

+385
-278
lines changed

9 files changed

+385
-278
lines changed

android/app/src/main/java/me/rainbow/MainApplication.kt

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import me.rainbow.NativeModules.RNStartTime.RNStartTimePackage
2121
import me.rainbow.NativeModules.RNTextAnimatorPackage.RNTextAnimatorPackage
2222
import me.rainbow.NativeModules.RNZoomableButton.RNZoomableButtonPackage
2323
import me.rainbow.NativeModules.SystemNavigationBar.SystemNavigationBarPackage
24+
import me.rainbow.NativeModules.NavbarHeight.NavbarHeightPackage
2425

2526
class MainApplication : Application(), ReactApplication {
2627
override val reactNativeHost: ReactNativeHost = object : DefaultReactNativeHost(this) {
@@ -41,6 +42,7 @@ class MainApplication : Application(), ReactApplication {
4142
packages.add(KeychainPackage(KeychainModuleBuilder().withoutWarmUp()))
4243
packages.add(RNStartTimePackage(START_MARK))
4344
packages.add(RNHapticsPackage())
45+
packages.add(NavbarHeightPackage())
4446
return packages
4547
}
4648

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package me.rainbow.NativeModules.NavbarHeight;
2+
3+
import androidx.annotation.NonNull;
4+
5+
import com.facebook.react.ReactPackage;
6+
import com.facebook.react.bridge.NativeModule;
7+
import com.facebook.react.bridge.ReactApplicationContext;
8+
import com.facebook.react.uimanager.ViewManager;
9+
10+
import java.util.ArrayList;
11+
import java.util.Collections;
12+
import java.util.List;
13+
14+
public class NavbarHeightPackage implements ReactPackage {
15+
@NonNull
16+
@Override
17+
public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext reactContext) {
18+
List<NativeModule> modules = new ArrayList<>();
19+
modules.add(new NavbarHeightModule(reactContext));
20+
return modules;
21+
}
22+
23+
@NonNull
24+
@Override
25+
public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
26+
return Collections.emptyList();
27+
}
28+
}

0 commit comments

Comments
 (0)