Use WindowMetrics API in VideoDetailFragment and PopupPlayerUi.

This commit is contained in:
Isira Seneviratne 2022-07-18 08:53:29 +05:30
parent 81c4b822e0
commit 4d7a6fb6de
3 changed files with 36 additions and 12 deletions

View File

@ -21,7 +21,6 @@ import android.content.SharedPreferences;
import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo;
import android.database.ContentObserver; import android.database.ContentObserver;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
@ -1051,15 +1050,13 @@ public final class VideoDetailFragment
// call `post()` to be sure `viewPager.getHitRect()` // call `post()` to be sure `viewPager.getHitRect()`
// is up to date and not being currently recomputed // is up to date and not being currently recomputed
binding.tabLayout.post(() -> { binding.tabLayout.post(() -> {
if (getContext() != null) { final var activity = getActivity();
if (activity != null) {
final Rect pagerHitRect = new Rect(); final Rect pagerHitRect = new Rect();
binding.viewPager.getHitRect(pagerHitRect); binding.viewPager.getHitRect(pagerHitRect);
final Point displaySize = new Point(); final int height = DeviceUtils.getWindowHeight(activity.getWindowManager());
Objects.requireNonNull(ContextCompat.getSystemService(getContext(), final int viewPagerVisibleHeight = height - pagerHitRect.top;
WindowManager.class)).getDefaultDisplay().getSize(displaySize);
final int viewPagerVisibleHeight = displaySize.y - pagerHitRect.top;
// see TabLayout.DEFAULT_HEIGHT, which is equal to 48dp // see TabLayout.DEFAULT_HEIGHT, which is equal to 48dp
final float tabLayoutHeight = TypedValue.applyDimension( final float tabLayoutHeight = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 48, getResources().getDisplayMetrics()); TypedValue.COMPLEX_UNIT_DIP, 48, getResources().getDisplayMetrics());

View File

@ -21,6 +21,7 @@ import android.view.LayoutInflater;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.WindowInsets;
import android.view.WindowManager; import android.view.WindowManager;
import android.view.animation.AnticipateInterpolator; import android.view.animation.AnticipateInterpolator;
import android.widget.LinearLayout; import android.widget.LinearLayout;
@ -255,11 +256,20 @@ public final class PopupPlayerUi extends VideoPlayerUi {
} }
public void updateScreenSize() { public void updateScreenSize() {
final DisplayMetrics metrics = new DisplayMetrics(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
windowManager.getDefaultDisplay().getMetrics(metrics); final var windowMetrics = windowManager.getCurrentWindowMetrics();
final var bounds = windowMetrics.getBounds();
screenWidth = metrics.widthPixels; final var windowInsets = windowMetrics.getWindowInsets();
screenHeight = metrics.heightPixels; final var insets = windowInsets.getInsetsIgnoringVisibility(
WindowInsets.Type.navigationBars() | WindowInsets.Type.displayCutout());
screenWidth = bounds.width() - (insets.left + insets.right);
screenHeight = bounds.height() - (insets.top + insets.bottom);
} else {
final DisplayMetrics metrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.widthPixels;
screenHeight = metrics.heightPixels;
}
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "updateScreenSize() called: screenWidth = [" Log.d(TAG, "updateScreenSize() called: screenWidth = ["
+ screenWidth + "], screenHeight = [" + screenHeight + "]"); + screenWidth + "], screenHeight = [" + screenHeight + "]");

View File

@ -4,11 +4,14 @@ import android.app.UiModeManager;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.graphics.Point;
import android.os.BatteryManager; import android.os.BatteryManager;
import android.os.Build; import android.os.Build;
import android.provider.Settings; import android.provider.Settings;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.WindowInsets;
import android.view.WindowManager;
import androidx.annotation.Dimension; import androidx.annotation.Dimension;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -151,4 +154,18 @@ public final class DeviceUtils {
Settings.Global.ANIMATOR_DURATION_SCALE, Settings.Global.ANIMATOR_DURATION_SCALE,
1F) != 0F; 1F) != 0F;
} }
public static int getWindowHeight(@NonNull final WindowManager windowManager) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
final var windowMetrics = windowManager.getCurrentWindowMetrics();
final var windowInsets = windowMetrics.getWindowInsets();
final var insets = windowInsets.getInsetsIgnoringVisibility(
WindowInsets.Type.navigationBars() | WindowInsets.Type.displayCutout());
return windowMetrics.getBounds().height() - (insets.top + insets.bottom);
} else {
final Point point = new Point();
windowManager.getDefaultDisplay().getSize(point);
return point.y;
}
}
} }