4545import org .schabi .newpipe .player .playqueue .PlayQueueItem ;
4646import org .schabi .newpipe .player .playqueue .SinglePlayQueue ;
4747import org .schabi .newpipe .util .ListHelper ;
48+ import org .schabi .newpipe .util .Localization ;
4849
4950import java .lang .annotation .Retention ;
5051import java .text .DecimalFormat ;
52+ import java .text .DecimalFormatSymbols ;
5153import java .text .NumberFormat ;
5254import java .util .ArrayList ;
5355import java .util .Collections ;
6062import java .util .concurrent .TimeUnit ;
6163
6264public 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}
0 commit comments