Keine Beschreibung

SafeAreaUtils.java 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package com.th3rdwave.safeareacontext;
  2. import android.graphics.Rect;
  3. import android.os.Build;
  4. import android.view.Surface;
  5. import android.view.View;
  6. import android.view.WindowInsets;
  7. import android.view.WindowManager;
  8. import com.facebook.react.bridge.Arguments;
  9. import com.facebook.react.bridge.WritableMap;
  10. import com.facebook.react.common.MapBuilder;
  11. import com.facebook.react.uimanager.PixelUtil;
  12. import java.util.Map;
  13. /* package */ class SafeAreaUtils {
  14. static WritableMap edgeInsetsToJsMap(EdgeInsets insets) {
  15. WritableMap insetsMap = Arguments.createMap();
  16. insetsMap.putDouble("top", PixelUtil.toDIPFromPixel(insets.top));
  17. insetsMap.putDouble("right", PixelUtil.toDIPFromPixel(insets.right));
  18. insetsMap.putDouble("bottom", PixelUtil.toDIPFromPixel(insets.bottom));
  19. insetsMap.putDouble("left", PixelUtil.toDIPFromPixel(insets.left));
  20. return insetsMap;
  21. }
  22. static Map<String, Float> edgeInsetsToJavaMap(EdgeInsets insets) {
  23. return MapBuilder.of(
  24. "top",
  25. PixelUtil.toDIPFromPixel(insets.top),
  26. "right",
  27. PixelUtil.toDIPFromPixel(insets.right),
  28. "bottom",
  29. PixelUtil.toDIPFromPixel(insets.bottom),
  30. "left",
  31. PixelUtil.toDIPFromPixel(insets.left));
  32. }
  33. static EdgeInsets getSafeAreaInsets(WindowManager windowManager, View rootView) {
  34. // Window insets are parts of the window that are covered by system views (status bar,
  35. // navigation bar, notches). There are no apis the get these values for android < M so we
  36. // do a best effort polyfill.
  37. EdgeInsets windowInsets;
  38. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  39. WindowInsets insets = rootView.getRootWindowInsets();
  40. windowInsets = new EdgeInsets(
  41. insets.getSystemWindowInsetTop(),
  42. insets.getSystemWindowInsetRight(),
  43. insets.getSystemWindowInsetBottom(),
  44. insets.getSystemWindowInsetLeft());
  45. } else {
  46. int rotation = windowManager.getDefaultDisplay().getRotation();
  47. int statusBarHeight = 0;
  48. int resourceId = rootView.getResources().getIdentifier("status_bar_height", "dimen", "android");
  49. if (resourceId > 0) {
  50. statusBarHeight = rootView.getResources().getDimensionPixelSize(resourceId);
  51. }
  52. int navbarHeight = 0;
  53. resourceId = rootView.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
  54. if (resourceId > 0) {
  55. navbarHeight = rootView.getResources().getDimensionPixelSize(resourceId);
  56. }
  57. windowInsets = new EdgeInsets(
  58. statusBarHeight,
  59. rotation == Surface.ROTATION_90 ? navbarHeight : 0,
  60. rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180 ? navbarHeight : 0,
  61. rotation == Surface.ROTATION_270 ? navbarHeight : 0);
  62. }
  63. // Calculate the part of the root view that overlaps with window insets.
  64. View contentView = rootView.findViewById(android.R.id.content);
  65. float windowWidth = rootView.getWidth();
  66. float windowHeight = rootView.getHeight();
  67. Rect visibleRect = new Rect();
  68. contentView.getGlobalVisibleRect(visibleRect);
  69. windowInsets.top = Math.max(windowInsets.top - visibleRect.top, 0);
  70. windowInsets.left = Math.max(windowInsets.left - visibleRect.left, 0);
  71. windowInsets.bottom = Math.max(visibleRect.top + contentView.getHeight() + windowInsets.bottom - windowHeight, 0);
  72. windowInsets.right = Math.max(visibleRect.left + contentView.getWidth() + windowInsets.right - windowWidth, 0);
  73. return windowInsets;
  74. }
  75. }