Skip to content

Commit

Permalink
Apply review
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiGr committed Mar 14, 2021
1 parent 56a9fe6 commit 5c0c6e8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 28 deletions.
74 changes: 49 additions & 25 deletions app/src/main/java/org/schabi/newpipe/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -1193,36 +1193,21 @@ private void initThumbnail(final String url) {
.loadImage(url, ImageDisplayConstants.DISPLAY_THUMBNAIL_OPTIONS, this);
}

/**
* Scale the player audio / end screen thumbnail down if necessary.
* <p>
* This is necessary when the thumbnail's height is larger than the device's height
* and thus is enlarging the player's height
* causing the bottom playback controls to be out of the visible screen.
* </p>
*/
public void updateEndScreenThumbnail() {
if (currentThumbnail == null) {
return;
}

final float endScreenHeight;
final float endScreenHeight = calculateMaxEndScreenThumbnailHeight();

updateScreenSize(); // ensure that screenHeight is initialized and thus not 0

// Show at least stream title and content creator on TVs and tablets (using the land layout)
// 85dp for height of R.id.detail_root and R.id.detail_title_root_layout
// 15/16sp for text size of video title
if (DeviceUtils.isTv(context) && !isFullscreen) {
final int videoInfoHeight =
DeviceUtils.dpToPx(85, context) + DeviceUtils.spToPx(16, context);
endScreenHeight = Math.min(currentThumbnail.getHeight(),
screenHeight - videoInfoHeight);
} else if (DeviceUtils.isTablet(context) && !isFullscreen) {
final int videoInfoHeight =
DeviceUtils.dpToPx(85, context) + DeviceUtils.spToPx(15, context);
endScreenHeight = Math.min(currentThumbnail.getHeight(),
screenHeight - videoInfoHeight);
} else { // fullscreen player: max height is the device height
endScreenHeight = Math.min(currentThumbnail.getHeight(), screenHeight);
}

// scale the player audio / end screen thumbnail
// This is necessary when the thumbnail's height is larger than the device's height
// and thus is enlarging the player's height
// causing the bottom playback controls to be out of the visible screen.
final Bitmap endScreenBitmap = Bitmap.createScaledBitmap(
currentThumbnail,
(int) (currentThumbnail.getWidth()
Expand All @@ -1241,6 +1226,45 @@ public void updateEndScreenThumbnail() {
binding.endScreen.setImageBitmap(endScreenBitmap);
}

/**
* Calculate the maximum allowed height for the {@link R.id.endScreen}
* to prevent it from enlarging the player.
* <p>
* The calculating follows these rules:
* <ul>
* <li>
* Show at least stream title and content creator on TVs and tablets
* when in landscape (always the case for TVs) and not in fullscreen mode.
* This requires to have at least <code>85dp</code> free space for {@link R.id.detail_root}
* and additional space for the stream title text size
* ({@link R.id.detail_title_root_layout}).
* The text size is <code>15sp</code> on tablets and <code>16sp</code> on TVs,
* see {@link R.id.titleTextView}.
* </li>
* <li>
* Otherwise, the max thumbnail height is the screen height.
* </li>
* </ul>
*
* @return the maximum height for the end screen thumbnail
*/
private float calculateMaxEndScreenThumbnailHeight() {
// ensure that screenHeight is initialized and thus not 0
updateScreenSize();

if (DeviceUtils.isTv(context) && !isFullscreen) {
final int videoInfoHeight =
DeviceUtils.dpToPx(85, context) + DeviceUtils.spToPx(16, context);
return Math.min(currentThumbnail.getHeight(), screenHeight - videoInfoHeight);
} else if (DeviceUtils.isTablet(context) && service.isLandscape() && !isFullscreen) {
final int videoInfoHeight =
DeviceUtils.dpToPx(85, context) + DeviceUtils.spToPx(15, context);
return Math.min(currentThumbnail.getHeight(), screenHeight - videoInfoHeight);
} else { // fullscreen player: max height is the device height
return Math.min(currentThumbnail.getHeight(), screenHeight);
}
}

@Override
public void onLoadingStarted(final String imageUri, final View view) {
if (DEBUG) {
Expand All @@ -1261,7 +1285,7 @@ public void onLoadingFailed(final String imageUri, final View view,
@Override
public void onLoadingComplete(final String imageUri, final View view,
final Bitmap loadedImage) {
// scale the notification thumbnail
// scale down the notification thumbnail for performance
final float notificationThumbnailWidth = Math.min(
context.getResources().getDimension(R.dimen.player_notification_thumbnail_width),
loadedImage.getWidth());
Expand Down
12 changes: 9 additions & 3 deletions app/src/main/java/org/schabi/newpipe/util/DeviceUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.util.TypedValue;
import android.view.KeyEvent;

import androidx.annotation.Dimension;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

Expand Down Expand Up @@ -72,11 +73,16 @@ public static boolean isConfirmKey(final int keyCode) {
}
}

public static int dpToPx(final float dp, final Context context) {
return (int) (dp * context.getResources().getDisplayMetrics().density);
public static int dpToPx(@Dimension(unit = Dimension.DP) final int dp,
@NonNull final Context context) {
return (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
dp,
context.getResources().getDisplayMetrics());
}

public static int spToPx(final float sp, final Context context) {
public static int spToPx(@Dimension(unit = Dimension.SP) final int sp,
@NonNull final Context context) {
return (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP,
sp,
Expand Down

0 comments on commit 5c0c6e8

Please sign in to comment.