Skip to content

Commit d97a4c2

Browse files
committed
Improve edge-to-edge functionality
Handle inconsistencies for Samsung One UI.
1 parent 4739bc7 commit d97a4c2

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

dynamic-utils/src/main/java/com/pranavpandey/android/dynamic/util/DynamicWindowUtils.java

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2022 Pranav Pandey
2+
* Copyright 2017-2024 Pranav Pandey
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,7 +24,6 @@
2424
import android.content.res.Resources;
2525
import android.graphics.Insets;
2626
import android.graphics.Point;
27-
import android.hardware.display.DisplayManager;
2827
import android.os.Build;
2928
import android.util.DisplayMetrics;
3029
import android.view.Display;
@@ -36,6 +35,7 @@
3635

3736
import androidx.annotation.NonNull;
3837
import androidx.annotation.Nullable;
38+
import androidx.annotation.RequiresApi;
3939
import androidx.core.content.ContextCompat;
4040
import androidx.core.view.WindowCompat;
4141

@@ -44,13 +44,20 @@
4444
*
4545
* @see WindowManager
4646
*/
47+
@TargetApi(Build.VERSION_CODES.R)
4748
public class DynamicWindowUtils {
4849

4950
/**
5051
* Maximum height for the gesture navigation in DP.
5152
*/
5253
public static final int GESTURE_NAVIGATION_BAR_HEIGHT = 24;
5354

55+
/**
56+
* Inset type to calculate the app usable screen size.
57+
*/
58+
@RequiresApi(api = Build.VERSION_CODES.R)
59+
public static int APP_USABLE_SCREEN_SIZE_INSETS = 0;
60+
5461
/**
5562
* Returns the correct display according to the different API levels.
5663
*
@@ -61,28 +68,13 @@ public class DynamicWindowUtils {
6168
* @see Context#getDisplay()
6269
* @see WindowManager#getDefaultDisplay()
6370
*/
64-
@SuppressWarnings("deprecation")
6571
@TargetApi(Build.VERSION_CODES.R)
6672
public static @Nullable Display getDisplay(@Nullable Context context) {
6773
if (context == null) {
6874
return null;
6975
}
7076

71-
if (DynamicSdkUtils.is30()) {
72-
try {
73-
return context.getDisplay();
74-
} catch (Exception ignored) {
75-
DisplayManager displayManager = ContextCompat.getSystemService(
76-
context, DisplayManager.class);
77-
return displayManager != null ? displayManager.getDisplay(
78-
Display.DEFAULT_DISPLAY) : null;
79-
}
80-
}
81-
82-
WindowManager windowManager = context instanceof Activity
83-
? ((Activity) context).getWindowManager()
84-
: ContextCompat.getSystemService(context, WindowManager.class);
85-
return windowManager != null ? windowManager.getDefaultDisplay() : null;
77+
return ContextCompat.getDisplayOrDefault(context);
8678
}
8779

8880
/**
@@ -160,16 +152,13 @@ public static float getDisplayDensity(@Nullable Context context) {
160152
public static @NonNull Point getAppUsableScreenSize(@Nullable Context context) {
161153
Point size = new Point();
162154

163-
/*
164-
* This approach is not efficient on API 30 (Samsung OneUI) when some specific mode
165-
* is enabled like Game mode, so we are using it on API 31 and above.
166-
*/
167-
if (DynamicSdkUtils.is31()) {
155+
if (DynamicSdkUtils.is30()) {
168156
WindowMetrics windowMetrics = getCurrentWindowMetrics(context);
169157

170158
if (windowMetrics != null) {
171159
Insets insets = windowMetrics.getWindowInsets().getInsetsIgnoringVisibility(
172-
WindowInsets.Type.navigationBars());
160+
APP_USABLE_SCREEN_SIZE_INSETS != 0 ? APP_USABLE_SCREEN_SIZE_INSETS
161+
: WindowInsets.Type.navigationBars());
173162
size.x = windowMetrics.getBounds().width() - insets.left - insets.right;
174163
size.y = windowMetrics.getBounds().height() - insets.top - insets.bottom;
175164
}

0 commit comments

Comments
 (0)