|
@@ -2,52 +2,40 @@ package com.th3rdwave.safeareacontext;
|
2
|
2
|
|
3
|
3
|
import android.graphics.Rect;
|
4
|
4
|
import android.os.Build;
|
5
|
|
-import android.view.Surface;
|
6
|
5
|
import android.view.View;
|
7
|
6
|
import android.view.ViewGroup;
|
8
|
7
|
import android.view.WindowInsets;
|
9
|
|
-import android.view.WindowManager;
|
10
|
8
|
|
11
|
9
|
import androidx.annotation.Nullable;
|
12
|
10
|
|
13
|
11
|
/* package */ class SafeAreaUtils {
|
14
|
|
- static @Nullable EdgeInsets getSafeAreaInsets(
|
15
|
|
- WindowManager windowManager,
|
16
|
|
- View rootView,
|
17
|
|
- View view
|
18
|
|
- ) {
|
19
|
|
- // Window insets are parts of the window that are covered by system views (status bar,
|
20
|
|
- // navigation bar, notches). There are no apis the get these values for android < M so we
|
21
|
|
- // do a best effort polyfill.
|
22
|
|
- EdgeInsets windowInsets;
|
|
12
|
+
|
|
13
|
+ private static @Nullable EdgeInsets getRootWindowInsetsCompat(View rootView) {
|
23
|
14
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
24
|
15
|
WindowInsets insets = rootView.getRootWindowInsets();
|
25
|
16
|
if (insets == null) {
|
26
|
17
|
return null;
|
27
|
18
|
}
|
28
|
|
- windowInsets = new EdgeInsets(
|
|
19
|
+ return new EdgeInsets(
|
29
|
20
|
insets.getSystemWindowInsetTop(),
|
30
|
21
|
insets.getSystemWindowInsetRight(),
|
31
|
22
|
insets.getSystemWindowInsetBottom(),
|
32
|
23
|
insets.getSystemWindowInsetLeft());
|
33
|
24
|
} else {
|
34
|
|
- int rotation = windowManager.getDefaultDisplay().getRotation();
|
35
|
|
- int statusBarHeight = 0;
|
36
|
|
- int resourceId = rootView.getResources().getIdentifier("status_bar_height", "dimen", "android");
|
37
|
|
- if (resourceId > 0) {
|
38
|
|
- statusBarHeight = rootView.getResources().getDimensionPixelSize(resourceId);
|
39
|
|
- }
|
40
|
|
- int navbarHeight = 0;
|
41
|
|
- resourceId = rootView.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
|
42
|
|
- if (resourceId > 0) {
|
43
|
|
- navbarHeight = rootView.getResources().getDimensionPixelSize(resourceId);
|
44
|
|
- }
|
|
25
|
+ Rect visibleRect = new Rect();
|
|
26
|
+ rootView.getWindowVisibleDisplayFrame(visibleRect);
|
|
27
|
+ return new EdgeInsets(
|
|
28
|
+ visibleRect.top,
|
|
29
|
+ rootView.getWidth() - visibleRect.right,
|
|
30
|
+ rootView.getHeight() - visibleRect.bottom,
|
|
31
|
+ visibleRect.left);
|
|
32
|
+ }
|
|
33
|
+ }
|
45
|
34
|
|
46
|
|
- windowInsets = new EdgeInsets(
|
47
|
|
- statusBarHeight,
|
48
|
|
- rotation == Surface.ROTATION_90 ? navbarHeight : 0,
|
49
|
|
- rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180 ? navbarHeight : 0,
|
50
|
|
- rotation == Surface.ROTATION_270 ? navbarHeight : 0);
|
|
35
|
+ static @Nullable EdgeInsets getSafeAreaInsets(View rootView, View view) {
|
|
36
|
+ EdgeInsets windowInsets = getRootWindowInsetsCompat(rootView);
|
|
37
|
+ if (windowInsets == null) {
|
|
38
|
+ return null;
|
51
|
39
|
}
|
52
|
40
|
|
53
|
41
|
// Calculate the part of the view that overlaps with window insets.
|