Skip to content

Commit 11151b6

Browse files
committed
Ensure that the formatters in PlayerHelper always use the app language
1 parent dbc5380 commit 11151b6

5 files changed

Lines changed: 72 additions & 49 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ private void onPlayModeChanged(final int repeatMode, final boolean shuffled) {
592592
private void onPlaybackParameterChanged(@Nullable final PlaybackParameters parameters) {
593593
if (parameters != null && menu != null && player != null) {
594594
final MenuItem item = menu.findItem(R.id.action_playback_speed);
595-
item.setTitle(formatSpeed(parameters.speed));
595+
item.setTitle(formatSpeed(getApplicationContext(), parameters.speed));
596596
}
597597
}
598598

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
import java.util.Map;
3737
import java.util.Objects;
38+
import java.util.function.BiFunction;
3839
import java.util.function.Consumer;
3940
import java.util.function.DoubleConsumer;
4041
import java.util.function.DoubleFunction;
@@ -283,8 +284,16 @@ private void initUI() {
283284

284285
private void setText(
285286
final TextView textView,
286-
final DoubleFunction<String> formatter,
287+
final BiFunction<Context, Double, String> formatter,
287288
final double value
289+
) {
290+
Objects.requireNonNull(textView).setText(formatter.apply(requireContext(), value));
291+
}
292+
293+
private void setText(
294+
final TextView textView,
295+
final DoubleFunction<String> formatter,
296+
final double value
288297
) {
289298
Objects.requireNonNull(textView).setText(formatter.apply(value));
290299
}
@@ -392,7 +401,7 @@ private void setupStepTextView(
392401
final double stepSizeValue,
393402
final TextView textView
394403
) {
395-
setText(textView, PlaybackParameterDialog::getPercentString, stepSizeValue);
404+
setText(textView, this::getPercentString, stepSizeValue);
396405
textView.setOnClickListener(view -> {
397406
PreferenceManager.getDefaultSharedPreferences(requireContext())
398407
.edit()
@@ -576,18 +585,18 @@ private void updateCallback() {
576585
}
577586

578587
@NonNull
579-
private static String getStepUpPercentString(final double percent) {
588+
private String getStepUpPercentString(final double percent) {
580589
return '+' + getPercentString(percent);
581590
}
582591

583592
@NonNull
584-
private static String getStepDownPercentString(final double percent) {
593+
private String getStepDownPercentString(final double percent) {
585594
return '-' + getPercentString(percent);
586595
}
587596

588597
@NonNull
589-
private static String getPercentString(final double percent) {
590-
return PlayerHelper.formatPitch(percent);
598+
private String getPercentString(final double percent) {
599+
return PlayerHelper.formatPitch(requireContext(), percent);
591600
}
592601

593602
public interface Callback {

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

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@
4545
import org.schabi.newpipe.player.playqueue.PlayQueueItem;
4646
import org.schabi.newpipe.player.playqueue.SinglePlayQueue;
4747
import org.schabi.newpipe.util.ListHelper;
48+
import org.schabi.newpipe.util.Localization;
4849

4950
import java.lang.annotation.Retention;
5051
import java.text.DecimalFormat;
52+
import java.text.DecimalFormatSymbols;
5153
import java.text.NumberFormat;
5254
import java.util.ArrayList;
5355
import java.util.Collections;
@@ -60,11 +62,14 @@
6062
import java.util.concurrent.TimeUnit;
6163

6264
public final class PlayerHelper {
63-
private static final StringBuilder STRING_BUILDER = new StringBuilder();
64-
private static final Formatter STRING_FORMATTER =
65-
new Formatter(STRING_BUILDER, Locale.getDefault());
66-
private static final NumberFormat SPEED_FORMATTER = new DecimalFormat("0.##x");
67-
private static final NumberFormat PITCH_FORMATTER = new DecimalFormat("##%");
65+
private static PlayerHelperFormatters formatters;
66+
67+
private static PlayerHelperFormatters formatters(final Context context) {
68+
if (formatters == null) {
69+
formatters = PlayerHelperFormatters.create(context);
70+
}
71+
return formatters;
72+
}
6873

6974
@Retention(SOURCE)
7075
@IntDef({AUTOPLAY_TYPE_ALWAYS, AUTOPLAY_TYPE_WIFI,
@@ -87,37 +92,32 @@ public final class PlayerHelper {
8792
private PlayerHelper() {
8893
}
8994

90-
////////////////////////////////////////////////////////////////////////////
91-
// Exposed helpers
92-
////////////////////////////////////////////////////////////////////////////
95+
// region Exposed helpers
9396

9497
@NonNull
95-
public static String getTimeString(final int milliSeconds) {
98+
public static String getTimeString(@NonNull final Context context, final int milliSeconds) {
9699
final int seconds = (milliSeconds % 60000) / 1000;
97100
final int minutes = (milliSeconds % 3600000) / 60000;
98101
final int hours = (milliSeconds % 86400000) / 3600000;
99102
final int days = (milliSeconds % (86400000 * 7)) / 86400000;
100103

101-
STRING_BUILDER.setLength(0);
104+
final Formatter stringFormatter = formatters(context).string();
102105
return (days > 0
103-
? STRING_FORMATTER.format("%d:%02d:%02d:%02d", days, hours, minutes, seconds)
104-
: hours > 0
105-
? STRING_FORMATTER.format("%d:%02d:%02d", hours, minutes, seconds)
106-
: STRING_FORMATTER.format("%02d:%02d", minutes, seconds)
106+
? stringFormatter.format("%d:%02d:%02d:%02d", days, hours, minutes, seconds)
107+
: hours > 0
108+
? stringFormatter.format("%d:%02d:%02d", hours, minutes, seconds)
109+
: stringFormatter.format("%02d:%02d", minutes, seconds)
107110
).toString();
108111
}
109112

110113
@NonNull
111-
public static String formatSpeed(final double speed) {
112-
return SPEED_FORMATTER.format(speed);
113-
}
114-
115-
@NonNull
116-
public static String formatPitch(final double pitch) {
117-
return PITCH_FORMATTER.format(pitch);
114+
public static String formatSpeed(@NonNull final Context context, final double speed) {
115+
return formatters(context).speed().format(speed);
118116
}
119117

120118
@NonNull
119+
public static String formatPitch(@NonNull final Context context, final double pitch) {
120+
return formatters(context).pitch().format(pitch);
121121
}
122122

123123
@NonNull
@@ -208,9 +208,8 @@ public static PlayQueue autoQueueOf(@NonNull final StreamInfo info,
208208
? null : getAutoQueuedSinglePlayQueue(autoQueueItems.get(0));
209209
}
210210

211-
////////////////////////////////////////////////////////////////////////////
212-
// Settings Resolution
213-
////////////////////////////////////////////////////////////////////////////
211+
// endregion
212+
// region Resolution
214213

215214
public static boolean isResumeAfterAudioFocusGain(@NonNull final Context context) {
216215
return getPreferences(context)
@@ -394,9 +393,8 @@ public static int getProgressiveLoadIntervalBytes(@NonNull final Context context
394393
return Integer.parseInt(preferredIntervalBytes) * 1024;
395394
}
396395

397-
////////////////////////////////////////////////////////////////////////////
398-
// Private helpers
399-
////////////////////////////////////////////////////////////////////////////
396+
// endregion
397+
// region Private helpers
400398

401399
@NonNull
402400
private static SharedPreferences getPreferences(@NonNull final Context context) {
@@ -415,10 +413,8 @@ private static SinglePlayQueue getAutoQueuedSinglePlayQueue(
415413
return singlePlayQueue;
416414
}
417415

418-
419-
////////////////////////////////////////////////////////////////////////////
420-
// Utils used by player
421-
////////////////////////////////////////////////////////////////////////////
416+
// endregion
417+
// region Utils used by player
422418

423419
@RepeatMode
424420
public static int nextRepeatMode(@RepeatMode final int repeatMode) {
@@ -492,4 +488,22 @@ public static int retrieveSeekDurationFromPreferences(final Player player) {
492488
player.getContext().getString(R.string.seek_duration_key),
493489
player.getContext().getString(R.string.seek_duration_default_value))));
494490
}
491+
492+
// endregion
493+
494+
record PlayerHelperFormatters(
495+
Formatter string,
496+
NumberFormat speed,
497+
NumberFormat pitch) {
498+
499+
static PlayerHelperFormatters create(final Context context) {
500+
final Locale locale = Localization.getAppLocale(context);
501+
502+
final DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(locale);
503+
return new PlayerHelperFormatters(
504+
new Formatter(locale),
505+
new DecimalFormat("0.##x", dfs),
506+
new DecimalFormat("##%", dfs));
507+
}
508+
}
495509
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -829,8 +829,8 @@ private void updateQueueTime(final int currentTime) {
829829

830830
binding.itemsListHeaderDuration.setText(
831831
String.format("%s/%s",
832-
getTimeString(currentTime + before),
833-
getTimeString(before + after)
832+
getTimeString(context, currentTime + before),
833+
getTimeString(context, before + after)
834834
));
835835
}
836836

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ private void updatePlayBackElementsCurrentDuration(final int currentProgress) {
546546
if (player.getCurrentState() != STATE_PAUSED_SEEK) {
547547
binding.playbackSeekBar.setProgress(currentProgress);
548548
}
549-
binding.playbackCurrentTime.setText(getTimeString(currentProgress));
549+
binding.playbackCurrentTime.setText(getTimeString(context, currentProgress));
550550
}
551551

552552
/**
@@ -555,7 +555,7 @@ private void updatePlayBackElementsCurrentDuration(final int currentProgress) {
555555
* @param duration the video duration, in milliseconds
556556
*/
557557
private void setVideoDurationToControls(final int duration) {
558-
binding.playbackEndTime.setText(getTimeString(duration));
558+
binding.playbackEndTime.setText(getTimeString(context, duration));
559559

560560
binding.playbackSeekBar.setMax(duration);
561561
// This is important for Android TVs otherwise it would apply the default from
@@ -576,7 +576,7 @@ public void onProgressChanged(final SeekBar seekBar, final int progress,
576576
+ "seekBar = [" + seekBar + "], progress = [" + progress + "]");
577577
}
578578

579-
binding.currentDisplaySeek.setText(getTimeString(progress));
579+
binding.currentDisplaySeek.setText(getTimeString(context, progress));
580580

581581
// Seekbar Preview Thumbnail
582582
SeekbarPreviewThumbnailHelper
@@ -652,7 +652,7 @@ public void onStopTrackingTouch(final SeekBar seekBar) {
652652
player.getExoPlayer().play();
653653
}
654654

655-
binding.playbackCurrentTime.setText(getTimeString(seekBar.getProgress()));
655+
binding.playbackCurrentTime.setText(getTimeString(context, seekBar.getProgress()));
656656
animate(binding.currentDisplaySeek, false, 200, AnimationType.SCALE_AND_ALPHA);
657657
animate(binding.currentSeekbarPreviewThumbnail, false, 200, AnimationType.SCALE_AND_ALPHA);
658658

@@ -794,7 +794,7 @@ private void updatePlayPauseButton(final PlayButtonAction action) {
794794
public void onPrepared() {
795795
super.onPrepared();
796796
setVideoDurationToControls((int) player.getExoPlayer().getDuration());
797-
binding.playbackSpeed.setText(formatSpeed(player.getPlaybackSpeed()));
797+
binding.playbackSpeed.setText(formatSpeed(context, player.getPlaybackSpeed()));
798798
}
799799

800800
@Override
@@ -994,7 +994,7 @@ private void setShuffleButton(final boolean shuffled) {
994994
@Override
995995
public void onPlaybackParametersChanged(@NonNull final PlaybackParameters playbackParameters) {
996996
super.onPlaybackParametersChanged(playbackParameters);
997-
binding.playbackSpeed.setText(formatSpeed(playbackParameters.speed));
997+
binding.playbackSpeed.setText(formatSpeed(context, playbackParameters.speed));
998998
}
999999

10001000
@Override
@@ -1147,9 +1147,9 @@ private void buildPlaybackSpeedMenu() {
11471147

11481148
for (int i = 0; i < PLAYBACK_SPEEDS.length; i++) {
11491149
playbackSpeedPopupMenu.getMenu().add(POPUP_MENU_ID_PLAYBACK_SPEED, i, Menu.NONE,
1150-
formatSpeed(PLAYBACK_SPEEDS[i]));
1150+
formatSpeed(context, PLAYBACK_SPEEDS[i]));
11511151
}
1152-
binding.playbackSpeed.setText(formatSpeed(player.getPlaybackSpeed()));
1152+
binding.playbackSpeed.setText(formatSpeed(context, player.getPlaybackSpeed()));
11531153
playbackSpeedPopupMenu.setOnMenuItemClickListener(this);
11541154
playbackSpeedPopupMenu.setOnDismissListener(this);
11551155
}
@@ -1274,7 +1274,7 @@ public boolean onMenuItemClick(@NonNull final MenuItem menuItem) {
12741274
final float speed = PLAYBACK_SPEEDS[speedIndex];
12751275

12761276
player.setPlaybackSpeed(speed);
1277-
binding.playbackSpeed.setText(formatSpeed(speed));
1277+
binding.playbackSpeed.setText(formatSpeed(context, speed));
12781278
}
12791279

12801280
return false;

0 commit comments

Comments
 (0)