Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Android navigation mode detection #6053

Merged
merged 5 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions android/app/src/main/java/me/rainbow/MainApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import me.rainbow.NativeModules.RNStartTime.RNStartTimePackage
import me.rainbow.NativeModules.RNTextAnimatorPackage.RNTextAnimatorPackage
import me.rainbow.NativeModules.RNZoomableButton.RNZoomableButtonPackage
import me.rainbow.NativeModules.SystemNavigationBar.SystemNavigationBarPackage
import me.rainbow.NativeModules.NavbarHeight.NavbarHeightPackage

class MainApplication : Application(), ReactApplication {
override val reactNativeHost: ReactNativeHost = object : DefaultReactNativeHost(this) {
Expand All @@ -41,6 +42,7 @@ class MainApplication : Application(), ReactApplication {
packages.add(KeychainPackage(KeychainModuleBuilder().withoutWarmUp()))
packages.add(RNStartTimePackage(START_MARK))
packages.add(RNHapticsPackage())
packages.add(NavbarHeightPackage())
return packages
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package me.rainbow.NativeModules.NavbarHeight;

import androidx.annotation.NonNull;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.module.annotations.ReactModule;
import android.graphics.Point;
import android.view.WindowManager;
import android.view.Display;
import java.lang.IllegalAccessException;
import java.lang.reflect.InvocationTargetException;
import java.lang.NoSuchMethodException;
import android.view.WindowInsets;
import android.os.Build;
import android.content.Context;

@ReactModule(name = NavbarHeightModule.NAME)
public class NavbarHeightModule extends ReactContextBaseJavaModule {
public static final String NAME = "NavbarHeight";

public NavbarHeightModule(ReactApplicationContext reactContext) {
super(reactContext);
}

@Override
@NonNull
public String getName() {
return NAME;
}

// Example method
// See https://reactnative.dev/docs/native-modules-android
@ReactMethod
public double getNavigationBarHeightSync() {
Context context = getReactApplicationContext();
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
if (Build.VERSION.SDK_INT >= 30) {
return windowManager
.getCurrentWindowMetrics()
.getWindowInsets()
.getInsets(WindowInsets.Type.navigationBars())
.bottom;
} else {
Point appUsableSize = getAppUsableScreenSize(context);
Point realScreenSize = getRealScreenSize(context);

// navigation bar on the side
if (appUsableSize.x < realScreenSize.x) {
return appUsableSize.y;
}

// navigation bar at the bottom
if (appUsableSize.y < realScreenSize.y) {
return realScreenSize.y - appUsableSize.y;
}

// navigation bar is not present
return 0;
}
}
public Point getAppUsableScreenSize(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
return size;
}
public Point getRealScreenSize(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();

if (Build.VERSION.SDK_INT >= 17) {
display.getRealSize(size);
} else if (Build.VERSION.SDK_INT >= 14) {
try {
size.x = (Integer) Display.class.getMethod("getRawWidth").invoke(display);
size.y = (Integer) Display.class.getMethod("getRawHeight").invoke(display);
} catch (IllegalAccessException e) {} catch (InvocationTargetException e) {} catch (NoSuchMethodException e) {}
}

return size;
}
@ReactMethod(isBlockingSynchronousMethod = true)
public double getNavigationBarHeight() {
return getNavigationBarHeightSync();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package me.rainbow.NativeModules.NavbarHeight;

import androidx.annotation.NonNull;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class NavbarHeightPackage implements ReactPackage {
@NonNull
@Override
public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new NavbarHeightModule(reactContext));
return modules;
}

@NonNull
@Override
public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}
Loading
Loading