Merge pull request #8656 from Isira-Seneviratne/Use_WindowMetrics

Use WindowMetrics API.
This commit is contained in:
Stypox 2022-08-04 10:12:32 +02:00 committed by GitHub
commit ffc1d9a212
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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.database.ContentObserver;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.net.Uri;
@ -1051,15 +1050,13 @@ public final class VideoDetailFragment
// call `post()` to be sure `viewPager.getHitRect()`
// is up to date and not being currently recomputed
binding.tabLayout.post(() -> {
if (getContext() != null) {
final var activity = getActivity();
if (activity != null) {
final Rect pagerHitRect = new Rect();
binding.viewPager.getHitRect(pagerHitRect);
final Point displaySize = new Point();
Objects.requireNonNull(ContextCompat.getSystemService(getContext(),
WindowManager.class)).getDefaultDisplay().getSize(displaySize);
final int viewPagerVisibleHeight = displaySize.y - pagerHitRect.top;
final int height = DeviceUtils.getWindowHeight(activity.getWindowManager());
final int viewPagerVisibleHeight = height - pagerHitRect.top;
// see TabLayout.DEFAULT_HEIGHT, which is equal to 48dp
final float tabLayoutHeight = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 48, getResources().getDisplayMetrics());

View File

@ -21,6 +21,7 @@ import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowInsets;
import android.view.WindowManager;
import android.view.animation.AnticipateInterpolator;
import android.widget.LinearLayout;
@ -255,11 +256,20 @@ public final class PopupPlayerUi extends VideoPlayerUi {
}
public void updateScreenSize() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
final var windowMetrics = windowManager.getCurrentWindowMetrics();
final var bounds = windowMetrics.getBounds();
final var windowInsets = windowMetrics.getWindowInsets();
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) {
Log.d(TAG, "updateScreenSize() called: screenWidth = ["
+ screenWidth + "], screenHeight = [" + screenHeight + "]");

View File

@ -4,11 +4,14 @@ import android.app.UiModeManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Point;
import android.os.BatteryManager;
import android.os.Build;
import android.provider.Settings;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.WindowInsets;
import android.view.WindowManager;
import androidx.annotation.Dimension;
import androidx.annotation.NonNull;
@ -151,4 +154,18 @@ public final class DeviceUtils {
Settings.Global.ANIMATOR_DURATION_SCALE,
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;
}
}
}