mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2024-12-23 00:20:32 +00:00
Merge pull request #8651 from Isira-Seneviratne/Use_limiting_methods
Use range-limiting methods.
This commit is contained in:
commit
cacce6d2d0
@ -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;
|
||||
|
||||
@ -451,7 +452,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);
|
||||
}
|
||||
|
@ -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()));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -233,22 +233,14 @@ class PopupPlayerGestureListener(
|
||||
|
||||
isMoving = true
|
||||
|
||||
val diffX: Float = (movingEvent.rawX - initialEvent.rawX)
|
||||
var posX: Float = (initialPopupX + diffX)
|
||||
val diffY: Float = (movingEvent.rawY - initialEvent.rawY)
|
||||
var posY: Float = (initialPopupY + diffY)
|
||||
|
||||
if (posX > playerUi.screenWidth - playerUi.popupLayoutParams.width) {
|
||||
posX = (playerUi.screenWidth - playerUi.popupLayoutParams.width).toFloat()
|
||||
} else if (posX < 0) {
|
||||
posX = 0f
|
||||
}
|
||||
|
||||
if (posY > playerUi.screenHeight - playerUi.popupLayoutParams.height) {
|
||||
posY = (playerUi.screenHeight - playerUi.popupLayoutParams.height).toFloat()
|
||||
} else if (posY < 0) {
|
||||
posY = 0f
|
||||
}
|
||||
val diffX = (movingEvent.rawX - initialEvent.rawX)
|
||||
val posX = (initialPopupX + diffX).coerceIn(
|
||||
0f, (playerUi.screenWidth - playerUi.popupLayoutParams.width).toFloat()
|
||||
)
|
||||
val diffY = (movingEvent.rawY - initialEvent.rawY)
|
||||
val posY = (initialPopupY + diffY).coerceIn(
|
||||
0f, (playerUi.screenHeight - playerUi.popupLayoutParams.height).toFloat()
|
||||
)
|
||||
|
||||
playerUi.popupLayoutParams.x = posX.toInt()
|
||||
playerUi.popupLayoutParams.y = posY.toInt()
|
||||
|
@ -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;
|
||||
|
||||
@ -527,7 +528,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);
|
||||
@ -546,13 +547,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;
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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,21 @@ 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,
|
||||
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;
|
||||
|
@ -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,14 @@ 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)
|
||||
);
|
||||
final int newWidth = MathUtils.clamp(
|
||||
// Use 1/4 of the width for the preview
|
||||
Math.round(baseViewWidthSupplier.getAsInt() / 4f),
|
||||
// But have a min width of 10dp
|
||||
DeviceUtils.dpToPx(10, context),
|
||||
// And 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);
|
||||
|
@ -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() {
|
||||
|
@ -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 =
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user