1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-23 08:30:44 +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.core.app.NotificationCompat;
import androidx.core.app.ServiceCompat;
import androidx.core.math.MathUtils;
import androidx.fragment.app.FragmentManager;
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) {
((RadioButton) radioGroup.getChildAt(selectedRadioPosition)).setChecked(true);
}

View File

@ -62,6 +62,7 @@ import android.view.LayoutInflater;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.math.MathUtils;
import androidx.preference.PreferenceManager;
import com.google.android.exoplayer2.C;
@ -591,7 +592,7 @@ public final class Player implements PlaybackListener, Listener {
final long duration = simpleExoPlayer.getDuration();
// 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) {
@ -1534,14 +1535,8 @@ public final class Player implements PlaybackListener, Listener {
}
if (!exoPlayerIsNull()) {
// prevent invalid positions when fast-forwarding/-rewinding
long normalizedPositionMillis = positionMillis;
if (normalizedPositionMillis < 0) {
normalizedPositionMillis = 0;
} else if (normalizedPositionMillis > simpleExoPlayer.getDuration()) {
normalizedPositionMillis = simpleExoPlayer.getDuration();
}
simpleExoPlayer.seekTo(normalizedPositionMillis);
simpleExoPlayer.seekTo(MathUtils.clamp(positionMillis, 0,
simpleExoPlayer.getDuration()));
}
}

View File

@ -21,6 +21,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AlertDialog;
import androidx.core.math.MathUtils;
import androidx.fragment.app.DialogFragment;
import androidx.preference.PreferenceManager;
@ -532,7 +533,7 @@ public class PlaybackParameterDialog extends DialogFragment {
}
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));
setText(binding.tempoCurrentText, PlayerHelper::formatSpeed, tempo);
@ -551,13 +552,8 @@ public class PlaybackParameterDialog extends DialogFragment {
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) {
final double calcPitch =
Math.max(MIN_PITCH_OR_SPEED, Math.min(MAX_PITCH_OR_SPEED, newPitch));
final double calcPitch = MathUtils.clamp(newPitch, MIN_PITCH_OR_SPEED, MAX_PITCH_OR_SPEED);
if (!isCurrentPitchControlModeSemitone()) {
return calcPitch;

View File

@ -1,5 +1,7 @@
package org.schabi.newpipe.player.helper;
import androidx.core.math.MathUtils;
/**
* Converts between percent and 12-tone equal temperament semitones.
* <br/>
@ -33,6 +35,6 @@ public final class PlayerSemitoneHelper {
}
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;
import androidx.annotation.NonNull;
import androidx.core.math.MathUtils;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;
@ -16,18 +18,19 @@ public abstract class PlayQueueItemTouchCallback extends ItemTouchHelper.SimpleC
public abstract void onSwiped(int index);
@Override
public int interpolateOutOfBoundsScroll(final RecyclerView recyclerView, final int viewSize,
final int viewSizeOutOfBounds, final int totalSize,
final long msSinceStartScroll) {
public int interpolateOutOfBoundsScroll(@NonNull final RecyclerView recyclerView,
final int viewSize, final int viewSizeOutOfBounds,
final int totalSize, final long msSinceStartScroll) {
final int standardSpeed = super.interpolateOutOfBoundsScroll(recyclerView, viewSize,
viewSizeOutOfBounds, totalSize, msSinceStartScroll);
final int clampedAbsVelocity = Math.max(MINIMUM_INITIAL_DRAG_VELOCITY,
Math.min(Math.abs(standardSpeed), MAXIMUM_INITIAL_DRAG_VELOCITY));
final int clampedAbsVelocity = MathUtils.clamp(Math.abs(standardSpeed),
MINIMUM_INITIAL_DRAG_VELOCITY, MAXIMUM_INITIAL_DRAG_VELOCITY);
return clampedAbsVelocity * (int) Math.signum(viewSizeOutOfBounds);
}
@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) {
if (source.getItemViewType() != target.getItemViewType()) {
return false;

View File

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

View File

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

View File

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

View File

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