1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-11-10 20:10:02 +00:00

Use MathUtils.clamp().

Co-authored-by: Stypox <stypox@pm.me>
This commit is contained in:
Isira Seneviratne 2022-07-18 09:00:11 +05:30
parent ae369ec9ba
commit d62cdc659f
9 changed files with 38 additions and 60 deletions

View File

@ -30,6 +30,7 @@ import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.content.res.AppCompatResources; import androidx.appcompat.content.res.AppCompatResources;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.app.ServiceCompat; import androidx.core.app.ServiceCompat;
import androidx.core.math.MathUtils;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
@ -452,7 +453,7 @@ public class RouterActivity extends AppCompatActivity {
} }
} }
selectedRadioPosition = Math.min(Math.max(-1, selectedRadioPosition), choices.size() - 1); selectedRadioPosition = MathUtils.clamp(selectedRadioPosition, -1, choices.size() - 1);
if (selectedRadioPosition != -1) { if (selectedRadioPosition != -1) {
((RadioButton) radioGroup.getChildAt(selectedRadioPosition)).setChecked(true); ((RadioButton) radioGroup.getChildAt(selectedRadioPosition)).setChecked(true);
} }

View File

@ -62,6 +62,7 @@ import android.view.LayoutInflater;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.math.MathUtils;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
@ -591,7 +592,7 @@ public final class Player implements PlaybackListener, Listener {
final long duration = simpleExoPlayer.getDuration(); final long duration = simpleExoPlayer.getDuration();
// No checks due to https://github.com/TeamNewPipe/NewPipe/pull/7195#issuecomment-962624380 // No checks due to https://github.com/TeamNewPipe/NewPipe/pull/7195#issuecomment-962624380
setRecovery(queuePos, Math.max(0, Math.min(windowPos, duration))); setRecovery(queuePos, MathUtils.clamp(windowPos, 0, duration));
} }
private void setRecovery(final int queuePos, final long windowPos) { private void setRecovery(final int queuePos, final long windowPos) {
@ -1534,14 +1535,8 @@ public final class Player implements PlaybackListener, Listener {
} }
if (!exoPlayerIsNull()) { if (!exoPlayerIsNull()) {
// prevent invalid positions when fast-forwarding/-rewinding // prevent invalid positions when fast-forwarding/-rewinding
long normalizedPositionMillis = positionMillis; simpleExoPlayer.seekTo(MathUtils.clamp(positionMillis, 0,
if (normalizedPositionMillis < 0) { simpleExoPlayer.getDuration()));
normalizedPositionMillis = 0;
} else if (normalizedPositionMillis > simpleExoPlayer.getDuration()) {
normalizedPositionMillis = simpleExoPlayer.getDuration();
}
simpleExoPlayer.seekTo(normalizedPositionMillis);
} }
} }

View File

@ -21,6 +21,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.StringRes; import androidx.annotation.StringRes;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.core.math.MathUtils;
import androidx.fragment.app.DialogFragment; import androidx.fragment.app.DialogFragment;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
@ -532,7 +533,7 @@ public class PlaybackParameterDialog extends DialogFragment {
} }
private void setAndUpdateTempo(final double newTempo) { private void setAndUpdateTempo(final double newTempo) {
this.tempo = calcValidTempo(newTempo); this.tempo = MathUtils.clamp(newTempo, MIN_PITCH_OR_SPEED, MAX_PITCH_OR_SPEED);
binding.tempoSeekbar.setProgress(QUADRATIC_STRATEGY.progressOf(tempo)); binding.tempoSeekbar.setProgress(QUADRATIC_STRATEGY.progressOf(tempo));
setText(binding.tempoCurrentText, PlayerHelper::formatSpeed, tempo); setText(binding.tempoCurrentText, PlayerHelper::formatSpeed, tempo);
@ -551,13 +552,8 @@ public class PlaybackParameterDialog extends DialogFragment {
pitchPercent); pitchPercent);
} }
private double calcValidTempo(final double newTempo) {
return Math.max(MIN_PITCH_OR_SPEED, Math.min(MAX_PITCH_OR_SPEED, newTempo));
}
private double calcValidPitch(final double newPitch) { private double calcValidPitch(final double newPitch) {
final double calcPitch = final double calcPitch = MathUtils.clamp(newPitch, MIN_PITCH_OR_SPEED, MAX_PITCH_OR_SPEED);
Math.max(MIN_PITCH_OR_SPEED, Math.min(MAX_PITCH_OR_SPEED, newPitch));
if (!isCurrentPitchControlModeSemitone()) { if (!isCurrentPitchControlModeSemitone()) {
return calcPitch; return calcPitch;

View File

@ -1,5 +1,7 @@
package org.schabi.newpipe.player.helper; package org.schabi.newpipe.player.helper;
import androidx.core.math.MathUtils;
/** /**
* Converts between percent and 12-tone equal temperament semitones. * Converts between percent and 12-tone equal temperament semitones.
* <br/> * <br/>
@ -33,6 +35,6 @@ public final class PlayerSemitoneHelper {
} }
private static int ensureSemitonesInRange(final int semitones) { private static int ensureSemitonesInRange(final int semitones) {
return Math.max(-SEMITONE_COUNT, Math.min(SEMITONE_COUNT, semitones)); return MathUtils.clamp(semitones, -SEMITONE_COUNT, SEMITONE_COUNT);
} }
} }

View File

@ -1,5 +1,7 @@
package org.schabi.newpipe.player.playqueue; package org.schabi.newpipe.player.playqueue;
import androidx.annotation.NonNull;
import androidx.core.math.MathUtils;
import androidx.recyclerview.widget.ItemTouchHelper; import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
@ -16,18 +18,19 @@ public abstract class PlayQueueItemTouchCallback extends ItemTouchHelper.SimpleC
public abstract void onSwiped(int index); public abstract void onSwiped(int index);
@Override @Override
public int interpolateOutOfBoundsScroll(final RecyclerView recyclerView, final int viewSize, public int interpolateOutOfBoundsScroll(@NonNull final RecyclerView recyclerView,
final int viewSizeOutOfBounds, final int totalSize, final int viewSize, final int viewSizeOutOfBounds,
final long msSinceStartScroll) { final int totalSize, final long msSinceStartScroll) {
final int standardSpeed = super.interpolateOutOfBoundsScroll(recyclerView, viewSize, final int standardSpeed = super.interpolateOutOfBoundsScroll(recyclerView, viewSize,
viewSizeOutOfBounds, totalSize, msSinceStartScroll); viewSizeOutOfBounds, totalSize, msSinceStartScroll);
final int clampedAbsVelocity = Math.max(MINIMUM_INITIAL_DRAG_VELOCITY, final int clampedAbsVelocity = MathUtils.clamp(Math.abs(standardSpeed),
Math.min(Math.abs(standardSpeed), MAXIMUM_INITIAL_DRAG_VELOCITY)); MINIMUM_INITIAL_DRAG_VELOCITY, MAXIMUM_INITIAL_DRAG_VELOCITY);
return clampedAbsVelocity * (int) Math.signum(viewSizeOutOfBounds); return clampedAbsVelocity * (int) Math.signum(viewSizeOutOfBounds);
} }
@Override @Override
public boolean onMove(final RecyclerView recyclerView, final RecyclerView.ViewHolder source, public boolean onMove(@NonNull final RecyclerView recyclerView,
final RecyclerView.ViewHolder source,
final RecyclerView.ViewHolder target) { final RecyclerView.ViewHolder target) {
if (source.getItemViewType() != target.getItemViewType()) { if (source.getItemViewType() != target.getItemViewType()) {
return false; return false;

View File

@ -8,13 +8,13 @@ import android.widget.ImageView;
import androidx.annotation.IntDef; import androidx.annotation.IntDef;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.core.math.MathUtils;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import org.schabi.newpipe.R; import org.schabi.newpipe.R;
import org.schabi.newpipe.util.DeviceUtils; import org.schabi.newpipe.util.DeviceUtils;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.function.IntSupplier; import java.util.function.IntSupplier;
@ -79,19 +79,12 @@ public final class SeekbarPreviewThumbnailHelper {
// Resize original bitmap // Resize original bitmap
try { try {
Objects.requireNonNull(srcBitmap);
final int srcWidth = srcBitmap.getWidth() > 0 ? srcBitmap.getWidth() : 1; final int srcWidth = srcBitmap.getWidth() > 0 ? srcBitmap.getWidth() : 1;
final int newWidth = Math.max(
Math.min(
// Use 1/4 of the width for the preview // Use 1/4 of the width for the preview
Math.round(baseViewWidthSupplier.getAsInt() / 4f), final int newWidth = MathUtils.clamp(Math.round(baseViewWidthSupplier.getAsInt() / 4f),
DeviceUtils.dpToPx(10, context),
// Scaling more than that factor looks really pixelated -> max // Scaling more than that factor looks really pixelated -> max
Math.round(srcWidth * 2.5f) Math.round(srcWidth * 2.5f));
),
// Min width = 10dp
DeviceUtils.dpToPx(10, context)
);
final float scaleFactor = (float) newWidth / srcWidth; final float scaleFactor = (float) newWidth / srcWidth;
final int newHeight = (int) (srcBitmap.getHeight() * scaleFactor); final int newHeight = (int) (srcBitmap.getHeight() * scaleFactor);

View File

@ -27,6 +27,7 @@ import android.widget.LinearLayout;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.core.math.MathUtils;
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout; import com.google.android.exoplayer2.ui.AspectRatioFrameLayout;
import com.google.android.exoplayer2.ui.SubtitleView; import com.google.android.exoplayer2.ui.SubtitleView;
@ -247,17 +248,10 @@ public final class PopupPlayerUi extends VideoPlayerUi {
return; return;
} }
if (popupLayoutParams.x < 0) { popupLayoutParams.x = MathUtils.clamp(popupLayoutParams.x, 0, screenWidth
popupLayoutParams.x = 0; - popupLayoutParams.width);
} else if (popupLayoutParams.x > screenWidth - popupLayoutParams.width) { popupLayoutParams.y = MathUtils.clamp(popupLayoutParams.y, 0, screenHeight
popupLayoutParams.x = screenWidth - popupLayoutParams.width; - popupLayoutParams.height);
}
if (popupLayoutParams.y < 0) {
popupLayoutParams.y = 0;
} else if (popupLayoutParams.y > screenHeight - popupLayoutParams.height) {
popupLayoutParams.y = screenHeight - popupLayoutParams.height;
}
} }
public void updateScreenSize() { public void updateScreenSize() {

View File

@ -43,6 +43,7 @@ import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.view.ContextThemeWrapper; import androidx.appcompat.view.ContextThemeWrapper;
import androidx.appcompat.widget.PopupMenu; import androidx.appcompat.widget.PopupMenu;
import androidx.core.graphics.Insets; import androidx.core.graphics.Insets;
import androidx.core.math.MathUtils;
import androidx.core.view.ViewCompat; import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat; import androidx.core.view.WindowInsetsCompat;
@ -580,16 +581,9 @@ public abstract class VideoPlayerUi extends PlayerUi
currentSeekbarLeft - (binding.seekbarPreviewContainer.getWidth() / 2); currentSeekbarLeft - (binding.seekbarPreviewContainer.getWidth() / 2);
// Fix the position so it's within the boundaries // Fix the position so it's within the boundaries
final int checkedContainerLeft = final int checkedContainerLeft = MathUtils.clamp(uncheckedContainerLeft,
Math.max( 0, binding.playbackWindowRoot.getWidth()
Math.min( - binding.seekbarPreviewContainer.getWidth());
uncheckedContainerLeft,
// Max left
binding.playbackWindowRoot.getWidth()
- binding.seekbarPreviewContainer.getWidth()
),
0 // Min left
);
// See also: https://stackoverflow.com/a/23249734 // See also: https://stackoverflow.com/a/23249734
final LinearLayout.LayoutParams params = final LinearLayout.LayoutParams params =

View File

@ -13,6 +13,7 @@ import android.util.DisplayMetrics;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.PluralsRes; import androidx.annotation.PluralsRes;
import androidx.annotation.StringRes; import androidx.annotation.StringRes;
import androidx.core.math.MathUtils;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import org.ocpsoft.prettytime.PrettyTime; import org.ocpsoft.prettytime.PrettyTime;
@ -247,8 +248,7 @@ public final class Localization {
// is not the responsibility of this method handle long numbers // is not the responsibility of this method handle long numbers
// (it probably will fall in the "other" category, // (it probably will fall in the "other" category,
// or some language have some specific rule... then we have to change it) // or some language have some specific rule... then we have to change it)
final int safeCount = count > Integer.MAX_VALUE ? Integer.MAX_VALUE final int safeCount = (int) MathUtils.clamp(count, Integer.MIN_VALUE, Integer.MAX_VALUE);
: count < Integer.MIN_VALUE ? Integer.MIN_VALUE : (int) count;
return context.getResources().getQuantityString(pluralId, safeCount, formattedCount); return context.getResources().getQuantityString(pluralId, safeCount, formattedCount);
} }