From 8030312924400d73084acb6bc0a7c1874c0e8e56 Mon Sep 17 00:00:00 2001 From: Avently <7953703+avently@users.noreply.github.com> Date: Mon, 17 Aug 2020 17:42:32 +0300 Subject: [PATCH 1/3] Prevent jumping of the player on devices with cutout --- .../newpipe/fragments/detail/VideoDetailFragment.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java index 50ad1ae57..9e8a650a5 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java @@ -1982,6 +1982,11 @@ public class VideoDetailFragment return; } + // Prevent jumping of the player on devices with cutout + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + activity.getWindow().getAttributes().layoutInDisplayCutoutMode = + WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT; + } activity.getWindow().getDecorView().setSystemUiVisibility(0); activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); } @@ -1995,6 +2000,11 @@ public class VideoDetailFragment return; } + // Prevent jumping of the player on devices with cutout + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + activity.getWindow().getAttributes().layoutInDisplayCutoutMode = + WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER; + } final int visibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION From e833d415e322eb83c65d2f8e9085f35d2ace2dc0 Mon Sep 17 00:00:00 2001 From: Avently <7953703+avently@users.noreply.github.com> Date: Mon, 17 Aug 2020 20:18:16 +0300 Subject: [PATCH 2/3] Fixed wrong padding on devices with cutout on vertical videos --- .../newpipe/player/VideoPlayerImpl.java | 42 ++++++++++++++----- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/VideoPlayerImpl.java b/app/src/main/java/org/schabi/newpipe/player/VideoPlayerImpl.java index 0e06c1aaf..c21dca16f 100644 --- a/app/src/main/java/org/schabi/newpipe/player/VideoPlayerImpl.java +++ b/app/src/main/java/org/schabi/newpipe/player/VideoPlayerImpl.java @@ -915,8 +915,8 @@ public class VideoPlayerImpl extends VideoPlayer // show kodi button if it supports the current service and it is enabled in settings final boolean showKodiButton = playQueue != null && playQueue.getItem() != null && KoreUtil.isServiceSupportedByKore(playQueue.getItem().getServiceId()) - && PreferenceManager.getDefaultSharedPreferences(context) - .getBoolean(context.getString(R.string.show_play_with_kodi_key), false); + && PreferenceManager.getDefaultSharedPreferences(context) + .getBoolean(context.getString(R.string.show_play_with_kodi_key), false); playWithKodi.setVisibility(videoPlayerSelected() && kodiEnabled && showKodiButton ? View.VISIBLE : View.GONE); } @@ -1499,9 +1499,10 @@ public class VideoPlayerImpl extends VideoPlayer // It doesn't include NavigationBar, notches, etc. display.getSize(size); + final boolean isLandscape = service.isLandscape(); final int width = isFullscreen - ? (service.isLandscape() - ? size.x : size.y) : ViewGroup.LayoutParams.MATCH_PARENT; + ? (isLandscape ? size.x : size.y) + : ViewGroup.LayoutParams.MATCH_PARENT; final int gravity = isFullscreen ? (display.getRotation() == Surface.ROTATION_90 ? Gravity.START : Gravity.END) @@ -1532,14 +1533,15 @@ public class VideoPlayerImpl extends VideoPlayer // And the situations when we need to set custom height is // in fullscreen mode in tablet in non-multiWindow mode or with vertical video. // Other than that MATCH_PARENT is good - final boolean navBarAtTheBottom = DeviceUtils.isTablet(service) || !service.isLandscape(); + final boolean navBarAtTheBottom = DeviceUtils.isTablet(service) || !isLandscape; controlsRoot.getLayoutParams().height = isFullscreen && !isInMultiWindow() && navBarAtTheBottom ? size.y : ViewGroup.LayoutParams.MATCH_PARENT; controlsRoot.requestLayout(); - final int topPadding = isFullscreen && !isInMultiWindow() ? getStatusBarHeight() : 0; - getRootView().findViewById(R.id.playbackWindowRoot).setPadding(0, topPadding, 0, 0); - getRootView().findViewById(R.id.playbackWindowRoot).requestLayout(); + int topPadding = isFullscreen && !isInMultiWindow() ? getStatusBarHeight() : 0; + topPadding = !isLandscape && hasCutout(topPadding) ? 0 : topPadding; + getRootView().findViewById(R.id.playbackWindowRoot).setTranslationY(topPadding); + getBottomControlsRoot().setTranslationY(-topPadding); } /** @@ -1548,8 +1550,12 @@ public class VideoPlayerImpl extends VideoPlayer */ private int getStatusBarHeight() { int statusBarHeight = 0; - final int resourceId = service.getResources().getIdentifier( - "status_bar_height_landscape", "dimen", "android"); + final int resourceId = service.isLandscape() + ? service.getResources().getIdentifier( + "status_bar_height_landscape", "dimen", "android") + : service.getResources().getIdentifier( + "status_bar_height", "dimen", "android"); + if (resourceId > 0) { statusBarHeight = service.getResources().getDimensionPixelSize(resourceId); } @@ -1563,6 +1569,20 @@ public class VideoPlayerImpl extends VideoPlayer return statusBarHeight; } + /* + * Compares current status bar height with default status bar height in Android and decides, + * does the device has cutout or not + * */ + private boolean hasCutout(final float statusBarHeight) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + final DisplayMetrics metrics = getRootView().getResources().getDisplayMetrics(); + final float defaultStatusBarHeight = TypedValue.applyDimension( + TypedValue.COMPLEX_UNIT_DIP, 25, metrics); + return statusBarHeight > defaultStatusBarHeight; + } + return false; + } + protected void setMuteButton(final ImageButton button, final boolean isMuted) { button.setImageDrawable(AppCompatResources.getDrawable(service, isMuted ? R.drawable.ic_volume_off_white_24dp : R.drawable.ic_volume_up_white_24dp)); @@ -2081,7 +2101,7 @@ public class VideoPlayerImpl extends VideoPlayer * This will be called when a user goes to another app/activity, turns off a screen. * We don't want to interrupt playback and don't want to see notification so * next lines of code will enable audio-only playback only if needed - * */ + */ private void onFragmentStopped() { if (videoPlayerSelected() && (isPlaying() || isLoading())) { if (backgroundPlaybackEnabled()) { From b8a35e9e4aae29800739a1d2dd470e97e5327df3 Mon Sep 17 00:00:00 2001 From: Avently <7953703+avently@users.noreply.github.com> Date: Thu, 3 Sep 2020 15:44:36 +0300 Subject: [PATCH 3/3] Moved device-specific code into DeviceUtils --- .../schabi/newpipe/player/VideoPlayerImpl.java | 17 ++--------------- .../org/schabi/newpipe/util/DeviceUtils.java | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/VideoPlayerImpl.java b/app/src/main/java/org/schabi/newpipe/player/VideoPlayerImpl.java index c21dca16f..fce70138a 100644 --- a/app/src/main/java/org/schabi/newpipe/player/VideoPlayerImpl.java +++ b/app/src/main/java/org/schabi/newpipe/player/VideoPlayerImpl.java @@ -1538,8 +1538,9 @@ public class VideoPlayerImpl extends VideoPlayer && navBarAtTheBottom ? size.y : ViewGroup.LayoutParams.MATCH_PARENT; controlsRoot.requestLayout(); + final DisplayMetrics metrics = getRootView().getResources().getDisplayMetrics(); int topPadding = isFullscreen && !isInMultiWindow() ? getStatusBarHeight() : 0; - topPadding = !isLandscape && hasCutout(topPadding) ? 0 : topPadding; + topPadding = !isLandscape && DeviceUtils.hasCutout(topPadding, metrics) ? 0 : topPadding; getRootView().findViewById(R.id.playbackWindowRoot).setTranslationY(topPadding); getBottomControlsRoot().setTranslationY(-topPadding); } @@ -1569,20 +1570,6 @@ public class VideoPlayerImpl extends VideoPlayer return statusBarHeight; } - /* - * Compares current status bar height with default status bar height in Android and decides, - * does the device has cutout or not - * */ - private boolean hasCutout(final float statusBarHeight) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { - final DisplayMetrics metrics = getRootView().getResources().getDisplayMetrics(); - final float defaultStatusBarHeight = TypedValue.applyDimension( - TypedValue.COMPLEX_UNIT_DIP, 25, metrics); - return statusBarHeight > defaultStatusBarHeight; - } - return false; - } - protected void setMuteButton(final ImageButton button, final boolean isMuted) { button.setImageDrawable(AppCompatResources.getDrawable(service, isMuted ? R.drawable.ic_volume_off_white_24dp : R.drawable.ic_volume_up_white_24dp)); diff --git a/app/src/main/java/org/schabi/newpipe/util/DeviceUtils.java b/app/src/main/java/org/schabi/newpipe/util/DeviceUtils.java index d852c2296..7592d2f35 100644 --- a/app/src/main/java/org/schabi/newpipe/util/DeviceUtils.java +++ b/app/src/main/java/org/schabi/newpipe/util/DeviceUtils.java @@ -6,6 +6,8 @@ import android.content.pm.PackageManager; import android.content.res.Configuration; import android.os.BatteryManager; import android.os.Build; +import android.util.DisplayMetrics; +import android.util.TypedValue; import android.view.KeyEvent; import androidx.annotation.NonNull; @@ -72,4 +74,17 @@ public final class DeviceUtils { return false; } } + + /* + * Compares current status bar height with default status bar height in Android and decides, + * does the device has cutout or not + * */ + public static boolean hasCutout(final float statusBarHeight, final DisplayMetrics metrics) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + final float defaultStatusBarHeight = TypedValue.applyDimension( + TypedValue.COMPLEX_UNIT_DIP, 25, metrics); + return statusBarHeight > defaultStatusBarHeight; + } + return false; + } }