Skip to content

Commit 893a1cb

Browse files
litetexStypox
authored andcommitted
Encapsulate Formatters in PlayerHelper
and reset them when the language is changed/changing. This way they will be re-initialized on the next call. Also Remove a bunch of outdated/non-thread safe code (STRING_FORMATTER)
1 parent ebd5e1a commit 893a1cb

File tree

2 files changed

+57
-17
lines changed

2 files changed

+57
-17
lines changed

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

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,9 @@
3333
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout;
3434
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout.ResizeMode;
3535
import com.google.android.exoplayer2.ui.CaptionStyleCompat;
36-
import com.google.android.exoplayer2.util.MimeTypes;
3736

3837
import org.schabi.newpipe.R;
3938
import org.schabi.newpipe.extractor.InfoItem;
40-
import org.schabi.newpipe.extractor.MediaFormat;
4139
import org.schabi.newpipe.extractor.stream.StreamInfo;
4240
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
4341
import org.schabi.newpipe.extractor.stream.SubtitlesStream;
@@ -47,13 +45,14 @@
4745
import org.schabi.newpipe.player.playqueue.PlayQueueItem;
4846
import org.schabi.newpipe.player.playqueue.SinglePlayQueue;
4947
import org.schabi.newpipe.util.ListHelper;
48+
import org.schabi.newpipe.util.Localization;
5049

5150
import java.lang.annotation.Retention;
5251
import java.text.DecimalFormat;
52+
import java.text.DecimalFormatSymbols;
5353
import java.text.NumberFormat;
5454
import java.util.ArrayList;
5555
import java.util.Collections;
56-
import java.util.Formatter;
5756
import java.util.HashSet;
5857
import java.util.List;
5958
import java.util.Locale;
@@ -62,11 +61,7 @@
6261
import java.util.concurrent.TimeUnit;
6362

6463
public final class PlayerHelper {
65-
private static final StringBuilder STRING_BUILDER = new StringBuilder();
66-
private static final Formatter STRING_FORMATTER =
67-
new Formatter(STRING_BUILDER, Locale.getDefault());
68-
private static final NumberFormat SPEED_FORMATTER = new DecimalFormat("0.##x");
69-
private static final NumberFormat PITCH_FORMATTER = new DecimalFormat("##%");
64+
private static final FormattersProvider FORMATTERS_PROVIDER = new FormattersProvider();
7065

7166
@Retention(SOURCE)
7267
@IntDef({AUTOPLAY_TYPE_ALWAYS, AUTOPLAY_TYPE_WIFI,
@@ -91,30 +86,35 @@ private PlayerHelper() {
9186

9287
// region Exposed helpers
9388

89+
public static void resetFormat() {
90+
FORMATTERS_PROVIDER.reset();
91+
}
92+
9493
@NonNull
9594
public static String getTimeString(final int milliSeconds) {
9695
final int seconds = (milliSeconds % 60000) / 1000;
9796
final int minutes = (milliSeconds % 3600000) / 60000;
9897
final int hours = (milliSeconds % 86400000) / 3600000;
9998
final int days = (milliSeconds % (86400000 * 7)) / 86400000;
10099

101-
STRING_BUILDER.setLength(0);
102-
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)
107-
).toString();
100+
final Formatters formatters = FORMATTERS_PROVIDER.formatters();
101+
if (days > 0) {
102+
return formatters.stringFormat("%d:%02d:%02d:%02d", days, hours, minutes, seconds);
103+
}
104+
105+
return hours > 0
106+
? formatters.stringFormat("%d:%02d:%02d", hours, minutes, seconds)
107+
: formatters.stringFormat("%02d:%02d", minutes, seconds);
108108
}
109109

110110
@NonNull
111111
public static String formatSpeed(final double speed) {
112-
return SPEED_FORMATTER.format(speed);
112+
return FORMATTERS_PROVIDER.formatters().speed().format(speed);
113113
}
114114

115115
@NonNull
116116
public static String formatPitch(final double pitch) {
117-
return PITCH_FORMATTER.format(pitch);
117+
return FORMATTERS_PROVIDER.formatters().pitch().format(pitch);
118118
}
119119

120120
@NonNull
@@ -487,5 +487,42 @@ public static int retrieveSeekDurationFromPreferences(final Player player) {
487487
player.getContext().getString(R.string.seek_duration_default_value))));
488488
}
489489

490+
// endregion
491+
// region Format
492+
493+
static class FormattersProvider {
494+
private Formatters formatters;
495+
496+
public Formatters formatters() {
497+
if (formatters == null) {
498+
formatters = Formatters.create();
499+
}
500+
return formatters;
501+
}
502+
503+
public void reset() {
504+
formatters = null;
505+
}
506+
}
507+
508+
record Formatters(
509+
Locale locale,
510+
NumberFormat speed,
511+
NumberFormat pitch) {
512+
513+
static Formatters create() {
514+
final Locale locale = Localization.getAppLocale();
515+
final DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(locale);
516+
return new Formatters(
517+
locale,
518+
new DecimalFormat("0.##x", dfs),
519+
new DecimalFormat("##%", dfs));
520+
}
521+
522+
String stringFormat(final String format, final Object... args) {
523+
return String.format(locale, format, args);
524+
}
525+
}
526+
490527
// endregion
491528
}

app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.schabi.newpipe.DownloaderImpl;
1717
import org.schabi.newpipe.R;
1818
import org.schabi.newpipe.extractor.NewPipe;
19+
import org.schabi.newpipe.player.helper.PlayerHelper;
1920
import org.schabi.newpipe.util.Localization;
2021
import org.schabi.newpipe.util.image.ImageStrategy;
2122
import org.schabi.newpipe.util.image.PicassoHelper;
@@ -53,6 +54,7 @@ private void setupAppLanguagePreferences() {
5354
final Intent intent = new Intent(Settings.ACTION_APP_LOCALE_SETTINGS)
5455
.setData(Uri.fromParts("package", requireContext().getPackageName(), null));
5556
startActivity(intent);
57+
PlayerHelper.resetFormat();
5658
return true;
5759
});
5860
newAppLanguagePref.setVisible(true);
@@ -64,6 +66,7 @@ private void setupAppLanguagePreferences() {
6466
final String systemLang = getString(R.string.default_localization_key);
6567
final String tag = systemLang.equals(language) ? null : language;
6668
AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags(tag));
69+
PlayerHelper.resetFormat();
6770
return true;
6871
});
6972
}

0 commit comments

Comments
 (0)