Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent jumping of the player and wrong padding on devices with cutout #4154

Merged
merged 3 commits into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1982,6 +1982,11 @@ private void showSystemUi() {
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);
}
Expand All @@ -1995,6 +2000,11 @@ private void hideSystemUi() {
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;
}
Comment on lines +2003 to +2007

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to the following and it fixes landscape too.

        // Prevent jumping of the player on devices with cutout
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            if (isLandscape()) {
                activity.getWindow().getAttributes().layoutInDisplayCutoutMode =
                        WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
            } else {
                activity.getWindow().getAttributes().layoutInDisplayCutoutMode =
                        WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER;
            }
        }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do this but in this case a title and a description will be hidden under possible cutout from top right corner (in landscape orientation it will be in top left corner). I'm not sure about how much devices have such cutout but it's not an ideal choice anyway.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's actually a separate issue that's caused by the status bar height (and navbar height?) not being accounted for while viewing a video in landscape. So I'm currently trying to work out the best way to center the controls.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know that someone cares about it. Not only landscape mode has the problem with the centering but vertical videos too.

final int visibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
Expand Down
29 changes: 18 additions & 11 deletions app/src/main/java/org/schabi/newpipe/player/VideoPlayerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -915,8 +915,8 @@ private void showHideKodiButton() {
// 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);
}
Expand Down Expand Up @@ -1499,9 +1499,10 @@ public void setControlsSize() {
// 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)
Expand Down Expand Up @@ -1532,14 +1533,16 @@ public void setControlsSize() {
// 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();
final DisplayMetrics metrics = getRootView().getResources().getDisplayMetrics();
int topPadding = isFullscreen && !isInMultiWindow() ? getStatusBarHeight() : 0;
topPadding = !isLandscape && DeviceUtils.hasCutout(topPadding, metrics) ? 0 : topPadding;
getRootView().findViewById(R.id.playbackWindowRoot).setTranslationY(topPadding);
getBottomControlsRoot().setTranslationY(-topPadding);
}

/**
Expand All @@ -1548,8 +1551,12 @@ public void setControlsSize() {
*/
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);
}
Expand Down Expand Up @@ -2081,7 +2088,7 @@ void stopActivityBinding() {
* 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()) {
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/org/schabi/newpipe/util/DeviceUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
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;
Expand Down Expand Up @@ -72,4 +74,17 @@ public static boolean isConfirmKey(final int keyCode) {
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;
}
}