No Description

SafeAreaUtils.java 3.5KB

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