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.
2424import android .content .res .Resources ;
2525import android .graphics .Insets ;
2626import android .graphics .Point ;
27- import android .hardware .display .DisplayManager ;
2827import android .os .Build ;
2928import android .util .DisplayMetrics ;
3029import android .view .Display ;
3635
3736import androidx .annotation .NonNull ;
3837import androidx .annotation .Nullable ;
38+ import androidx .annotation .RequiresApi ;
3939import androidx .core .content .ContextCompat ;
4040import androidx .core .view .WindowCompat ;
4141
4444 *
4545 * @see WindowManager
4646 */
47+ @ TargetApi (Build .VERSION_CODES .R )
4748public 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