Skip to content

Commit d62cdc6

Browse files
Use MathUtils.clamp().
Co-authored-by: Stypox <stypox@pm.me>
1 parent ae369ec commit d62cdc6

9 files changed

Lines changed: 38 additions & 60 deletions

File tree

app/src/main/java/org/schabi/newpipe/RouterActivity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import androidx.appcompat.content.res.AppCompatResources;
3131
import androidx.core.app.NotificationCompat;
3232
import androidx.core.app.ServiceCompat;
33+
import androidx.core.math.MathUtils;
3334
import androidx.fragment.app.FragmentManager;
3435
import androidx.preference.PreferenceManager;
3536

@@ -452,7 +453,7 @@ private void showDialog(final List<AdapterChoiceItem> choices) {
452453
}
453454
}
454455

455-
selectedRadioPosition = Math.min(Math.max(-1, selectedRadioPosition), choices.size() - 1);
456+
selectedRadioPosition = MathUtils.clamp(selectedRadioPosition, -1, choices.size() - 1);
456457
if (selectedRadioPosition != -1) {
457458
((RadioButton) radioGroup.getChildAt(selectedRadioPosition)).setChecked(true);
458459
}

app/src/main/java/org/schabi/newpipe/player/Player.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262

6363
import androidx.annotation.NonNull;
6464
import androidx.annotation.Nullable;
65+
import androidx.core.math.MathUtils;
6566
import androidx.preference.PreferenceManager;
6667

6768
import com.google.android.exoplayer2.C;
@@ -591,7 +592,7 @@ public void setRecovery() {
591592
final long duration = simpleExoPlayer.getDuration();
592593

593594
// No checks due to https://github.com/TeamNewPipe/NewPipe/pull/7195#issuecomment-962624380
594-
setRecovery(queuePos, Math.max(0, Math.min(windowPos, duration)));
595+
setRecovery(queuePos, MathUtils.clamp(windowPos, 0, duration));
595596
}
596597

597598
private void setRecovery(final int queuePos, final long windowPos) {
@@ -1534,14 +1535,8 @@ public void seekTo(final long positionMillis) {
15341535
}
15351536
if (!exoPlayerIsNull()) {
15361537
// prevent invalid positions when fast-forwarding/-rewinding
1537-
long normalizedPositionMillis = positionMillis;
1538-
if (normalizedPositionMillis < 0) {
1539-
normalizedPositionMillis = 0;
1540-
} else if (normalizedPositionMillis > simpleExoPlayer.getDuration()) {
1541-
normalizedPositionMillis = simpleExoPlayer.getDuration();
1542-
}
1543-
1544-
simpleExoPlayer.seekTo(normalizedPositionMillis);
1538+
simpleExoPlayer.seekTo(MathUtils.clamp(positionMillis, 0,
1539+
simpleExoPlayer.getDuration()));
15451540
}
15461541
}
15471542

app/src/main/java/org/schabi/newpipe/player/helper/PlaybackParameterDialog.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import androidx.annotation.Nullable;
2222
import androidx.annotation.StringRes;
2323
import androidx.appcompat.app.AlertDialog;
24+
import androidx.core.math.MathUtils;
2425
import androidx.fragment.app.DialogFragment;
2526
import androidx.preference.PreferenceManager;
2627

@@ -532,7 +533,7 @@ private void setSliders(final double newValue) {
532533
}
533534

534535
private void setAndUpdateTempo(final double newTempo) {
535-
this.tempo = calcValidTempo(newTempo);
536+
this.tempo = MathUtils.clamp(newTempo, MIN_PITCH_OR_SPEED, MAX_PITCH_OR_SPEED);
536537

537538
binding.tempoSeekbar.setProgress(QUADRATIC_STRATEGY.progressOf(tempo));
538539
setText(binding.tempoCurrentText, PlayerHelper::formatSpeed, tempo);
@@ -551,13 +552,8 @@ private void setAndUpdatePitch(final double newPitch) {
551552
pitchPercent);
552553
}
553554

554-
private double calcValidTempo(final double newTempo) {
555-
return Math.max(MIN_PITCH_OR_SPEED, Math.min(MAX_PITCH_OR_SPEED, newTempo));
556-
}
557-
558555
private double calcValidPitch(final double newPitch) {
559-
final double calcPitch =
560-
Math.max(MIN_PITCH_OR_SPEED, Math.min(MAX_PITCH_OR_SPEED, newPitch));
556+
final double calcPitch = MathUtils.clamp(newPitch, MIN_PITCH_OR_SPEED, MAX_PITCH_OR_SPEED);
561557

562558
if (!isCurrentPitchControlModeSemitone()) {
563559
return calcPitch;

app/src/main/java/org/schabi/newpipe/player/helper/PlayerSemitoneHelper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.schabi.newpipe.player.helper;
22

3+
import androidx.core.math.MathUtils;
4+
35
/**
46
* Converts between percent and 12-tone equal temperament semitones.
57
* <br/>
@@ -33,6 +35,6 @@ public static int percentToSemitones(final double percent) {
3335
}
3436

3537
private static int ensureSemitonesInRange(final int semitones) {
36-
return Math.max(-SEMITONE_COUNT, Math.min(SEMITONE_COUNT, semitones));
38+
return MathUtils.clamp(semitones, -SEMITONE_COUNT, SEMITONE_COUNT);
3739
}
3840
}

app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueueItemTouchCallback.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.schabi.newpipe.player.playqueue;
22

3+
import androidx.annotation.NonNull;
4+
import androidx.core.math.MathUtils;
35
import androidx.recyclerview.widget.ItemTouchHelper;
46
import androidx.recyclerview.widget.RecyclerView;
57

@@ -16,18 +18,19 @@ public PlayQueueItemTouchCallback() {
1618
public abstract void onSwiped(int index);
1719

1820
@Override
19-
public int interpolateOutOfBoundsScroll(final RecyclerView recyclerView, final int viewSize,
20-
final int viewSizeOutOfBounds, final int totalSize,
21-
final long msSinceStartScroll) {
21+
public int interpolateOutOfBoundsScroll(@NonNull final RecyclerView recyclerView,
22+
final int viewSize, final int viewSizeOutOfBounds,
23+
final int totalSize, final long msSinceStartScroll) {
2224
final int standardSpeed = super.interpolateOutOfBoundsScroll(recyclerView, viewSize,
2325
viewSizeOutOfBounds, totalSize, msSinceStartScroll);
24-
final int clampedAbsVelocity = Math.max(MINIMUM_INITIAL_DRAG_VELOCITY,
25-
Math.min(Math.abs(standardSpeed), MAXIMUM_INITIAL_DRAG_VELOCITY));
26+
final int clampedAbsVelocity = MathUtils.clamp(Math.abs(standardSpeed),
27+
MINIMUM_INITIAL_DRAG_VELOCITY, MAXIMUM_INITIAL_DRAG_VELOCITY);
2628
return clampedAbsVelocity * (int) Math.signum(viewSizeOutOfBounds);
2729
}
2830

2931
@Override
30-
public boolean onMove(final RecyclerView recyclerView, final RecyclerView.ViewHolder source,
32+
public boolean onMove(@NonNull final RecyclerView recyclerView,
33+
final RecyclerView.ViewHolder source,
3134
final RecyclerView.ViewHolder target) {
3235
if (source.getItemViewType() != target.getItemViewType()) {
3336
return false;

app/src/main/java/org/schabi/newpipe/player/seekbarpreview/SeekbarPreviewThumbnailHelper.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
import androidx.annotation.IntDef;
1010
import androidx.annotation.NonNull;
11+
import androidx.core.math.MathUtils;
1112
import androidx.preference.PreferenceManager;
1213

1314
import org.schabi.newpipe.R;
1415
import org.schabi.newpipe.util.DeviceUtils;
1516

1617
import java.lang.annotation.Retention;
17-
import java.util.Objects;
1818
import java.util.Optional;
1919
import java.util.function.IntSupplier;
2020

@@ -79,19 +79,12 @@ public static void tryResizeAndSetSeekbarPreviewThumbnail(
7979

8080
// Resize original bitmap
8181
try {
82-
Objects.requireNonNull(srcBitmap);
83-
8482
final int srcWidth = srcBitmap.getWidth() > 0 ? srcBitmap.getWidth() : 1;
85-
final int newWidth = Math.max(
86-
Math.min(
87-
// Use 1/4 of the width for the preview
88-
Math.round(baseViewWidthSupplier.getAsInt() / 4f),
89-
// Scaling more than that factor looks really pixelated -> max
90-
Math.round(srcWidth * 2.5f)
91-
),
92-
// Min width = 10dp
93-
DeviceUtils.dpToPx(10, context)
94-
);
83+
// Use 1/4 of the width for the preview
84+
final int newWidth = MathUtils.clamp(Math.round(baseViewWidthSupplier.getAsInt() / 4f),
85+
DeviceUtils.dpToPx(10, context),
86+
// Scaling more than that factor looks really pixelated -> max
87+
Math.round(srcWidth * 2.5f));
9588

9689
final float scaleFactor = (float) newWidth / srcWidth;
9790
final int newHeight = (int) (srcBitmap.getHeight() * scaleFactor);

app/src/main/java/org/schabi/newpipe/player/ui/PopupPlayerUi.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import androidx.annotation.NonNull;
2929
import androidx.core.content.ContextCompat;
30+
import androidx.core.math.MathUtils;
3031

3132
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout;
3233
import com.google.android.exoplayer2.ui.SubtitleView;
@@ -247,17 +248,10 @@ public void checkPopupPositionBounds() {
247248
return;
248249
}
249250

250-
if (popupLayoutParams.x < 0) {
251-
popupLayoutParams.x = 0;
252-
} else if (popupLayoutParams.x > screenWidth - popupLayoutParams.width) {
253-
popupLayoutParams.x = screenWidth - popupLayoutParams.width;
254-
}
255-
256-
if (popupLayoutParams.y < 0) {
257-
popupLayoutParams.y = 0;
258-
} else if (popupLayoutParams.y > screenHeight - popupLayoutParams.height) {
259-
popupLayoutParams.y = screenHeight - popupLayoutParams.height;
260-
}
251+
popupLayoutParams.x = MathUtils.clamp(popupLayoutParams.x, 0, screenWidth
252+
- popupLayoutParams.width);
253+
popupLayoutParams.y = MathUtils.clamp(popupLayoutParams.y, 0, screenHeight
254+
- popupLayoutParams.height);
261255
}
262256

263257
public void updateScreenSize() {

app/src/main/java/org/schabi/newpipe/player/ui/VideoPlayerUi.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import androidx.appcompat.view.ContextThemeWrapper;
4444
import androidx.appcompat.widget.PopupMenu;
4545
import androidx.core.graphics.Insets;
46+
import androidx.core.math.MathUtils;
4647
import androidx.core.view.ViewCompat;
4748
import androidx.core.view.WindowInsetsCompat;
4849

@@ -580,16 +581,9 @@ private void adjustSeekbarPreviewContainer() {
580581
currentSeekbarLeft - (binding.seekbarPreviewContainer.getWidth() / 2);
581582

582583
// Fix the position so it's within the boundaries
583-
final int checkedContainerLeft =
584-
Math.max(
585-
Math.min(
586-
uncheckedContainerLeft,
587-
// Max left
588-
binding.playbackWindowRoot.getWidth()
589-
- binding.seekbarPreviewContainer.getWidth()
590-
),
591-
0 // Min left
592-
);
584+
final int checkedContainerLeft = MathUtils.clamp(uncheckedContainerLeft,
585+
0, binding.playbackWindowRoot.getWidth()
586+
- binding.seekbarPreviewContainer.getWidth());
593587

594588
// See also: https://stackoverflow.com/a/23249734
595589
final LinearLayout.LayoutParams params =

app/src/main/java/org/schabi/newpipe/util/Localization.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import androidx.annotation.NonNull;
1414
import androidx.annotation.PluralsRes;
1515
import androidx.annotation.StringRes;
16+
import androidx.core.math.MathUtils;
1617
import androidx.preference.PreferenceManager;
1718

1819
import org.ocpsoft.prettytime.PrettyTime;
@@ -247,8 +248,7 @@ private static String getQuantity(final Context context, @PluralsRes final int p
247248
// is not the responsibility of this method handle long numbers
248249
// (it probably will fall in the "other" category,
249250
// or some language have some specific rule... then we have to change it)
250-
final int safeCount = count > Integer.MAX_VALUE ? Integer.MAX_VALUE
251-
: count < Integer.MIN_VALUE ? Integer.MIN_VALUE : (int) count;
251+
final int safeCount = (int) MathUtils.clamp(count, Integer.MIN_VALUE, Integer.MAX_VALUE);
252252
return context.getResources().getQuantityString(pluralId, safeCount, formattedCount);
253253
}
254254

0 commit comments

Comments
 (0)