暂无描述

SafeAreaView.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package com.th3rdwave.safeareacontext;
  2. import android.content.Context;
  3. import android.os.Build;
  4. import android.view.Surface;
  5. import android.view.View;
  6. import android.view.ViewTreeObserver;
  7. import android.view.WindowInsets;
  8. import android.view.WindowManager;
  9. import com.facebook.infer.annotation.Assertions;
  10. import com.facebook.react.views.view.ReactViewGroup;
  11. import androidx.annotation.Nullable;
  12. public class SafeAreaView extends ReactViewGroup implements ViewTreeObserver.OnGlobalLayoutListener {
  13. public interface OnInsetsChangeListener {
  14. void onInsetsChange(SafeAreaView view, EdgeInsets insets);
  15. }
  16. private @Nullable OnInsetsChangeListener mInsetsChangeListener;
  17. private WindowManager mWindowManager;
  18. private @Nullable EdgeInsets mLastInsets;
  19. public SafeAreaView(Context context) {
  20. super(context);
  21. mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
  22. }
  23. private EdgeInsets getSafeAreaInsets() {
  24. // Window insets are parts of the window that are covered by system views (status bar,
  25. // navigation bar, notches). There are no apis the get these values for android < M so we
  26. // do a best effort polyfill.
  27. EdgeInsets windowInsets;
  28. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  29. WindowInsets insets = getRootWindowInsets();
  30. windowInsets = new EdgeInsets(
  31. insets.getSystemWindowInsetTop(),
  32. insets.getSystemWindowInsetRight(),
  33. insets.getSystemWindowInsetBottom(),
  34. insets.getSystemWindowInsetLeft());
  35. } else {
  36. int rotation = mWindowManager.getDefaultDisplay().getRotation();
  37. int statusBarHeight = 0;
  38. int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
  39. if (resourceId > 0) {
  40. statusBarHeight = getResources().getDimensionPixelSize(resourceId);
  41. }
  42. int navbarHeight = 0;
  43. resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
  44. if (resourceId > 0) {
  45. navbarHeight = getResources().getDimensionPixelSize(resourceId);
  46. }
  47. windowInsets = new EdgeInsets(
  48. statusBarHeight,
  49. rotation == Surface.ROTATION_90 ? navbarHeight : 0,
  50. rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180 ? navbarHeight : 0,
  51. rotation == Surface.ROTATION_270 ? navbarHeight : 0);
  52. }
  53. // Calculate the part of the root view that overlaps with window insets.
  54. View rootView = getRootView();
  55. View contentView = rootView.findViewById(android.R.id.content);
  56. float windowWidth = rootView.getWidth();
  57. float windowHeight = rootView.getHeight();
  58. int[] windowLocation = new int[2];
  59. contentView.getLocationInWindow(windowLocation);
  60. windowInsets.top = Math.max(windowInsets.top - windowLocation[1], 0);
  61. windowInsets.left = Math.max(windowInsets.left - windowLocation[0], 0);
  62. windowInsets.bottom = Math.max(windowLocation[1] + contentView.getHeight() + windowInsets.bottom - windowHeight, 0);
  63. windowInsets.right = Math.max(windowLocation[0] + contentView.getWidth() + windowInsets.right - windowWidth, 0);
  64. return windowInsets;
  65. }
  66. @Override
  67. protected void onAttachedToWindow() {
  68. super.onAttachedToWindow();
  69. getRootView().getViewTreeObserver().addOnGlobalLayoutListener(this);
  70. }
  71. @Override
  72. protected void onDetachedFromWindow() {
  73. super.onDetachedFromWindow();
  74. getRootView().getViewTreeObserver().removeOnGlobalLayoutListener(this);
  75. }
  76. @Override
  77. public void onGlobalLayout() {
  78. EdgeInsets edgeInsets = getSafeAreaInsets();
  79. if (mLastInsets == null || !mLastInsets.equalsToEdgeInsets(edgeInsets)) {
  80. Assertions.assertNotNull(mInsetsChangeListener).onInsetsChange(this, edgeInsets);
  81. mLastInsets = edgeInsets;
  82. }
  83. }
  84. public void setOnInsetsChangeListener(OnInsetsChangeListener listener) {
  85. mInsetsChangeListener = listener;
  86. }
  87. }