3333import com .google .android .exoplayer2 .ui .AspectRatioFrameLayout ;
3434import com .google .android .exoplayer2 .ui .AspectRatioFrameLayout .ResizeMode ;
3535import com .google .android .exoplayer2 .ui .CaptionStyleCompat ;
36- import com .google .android .exoplayer2 .util .MimeTypes ;
3736
3837import org .schabi .newpipe .R ;
3938import org .schabi .newpipe .extractor .InfoItem ;
40- import org .schabi .newpipe .extractor .MediaFormat ;
4139import org .schabi .newpipe .extractor .stream .StreamInfo ;
4240import org .schabi .newpipe .extractor .stream .StreamInfoItem ;
4341import org .schabi .newpipe .extractor .stream .SubtitlesStream ;
4745import org .schabi .newpipe .player .playqueue .PlayQueueItem ;
4846import org .schabi .newpipe .player .playqueue .SinglePlayQueue ;
4947import org .schabi .newpipe .util .ListHelper ;
48+ import org .schabi .newpipe .util .Localization ;
5049
5150import java .lang .annotation .Retention ;
5251import java .text .DecimalFormat ;
52+ import java .text .DecimalFormatSymbols ;
5353import java .text .NumberFormat ;
5454import java .util .ArrayList ;
5555import java .util .Collections ;
56- import java .util .Formatter ;
5756import java .util .HashSet ;
5857import java .util .List ;
5958import java .util .Locale ;
6261import java .util .concurrent .TimeUnit ;
6362
6463public 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 ,
@@ -89,9 +84,11 @@ public final class PlayerHelper {
8984 private PlayerHelper () {
9085 }
9186
92- ////////////////////////////////////////////////////////////////////////////
93- // Exposed helpers
94- ////////////////////////////////////////////////////////////////////////////
87+ // region Exposed helpers
88+
89+ public static void resetFormat () {
90+ FORMATTERS_PROVIDER .reset ();
91+ }
9592
9693 @ NonNull
9794 public static String getTimeString (final int milliSeconds ) {
@@ -100,35 +97,24 @@ public static String getTimeString(final int milliSeconds) {
10097 final int hours = (milliSeconds % 86400000 ) / 3600000 ;
10198 final int days = (milliSeconds % (86400000 * 7 )) / 86400000 ;
10299
103- STRING_BUILDER .setLength (0 );
104- return (days > 0
105- ? STRING_FORMATTER .format ("%d:%02d:%02d:%02d" , days , hours , minutes , seconds )
106- : hours > 0
107- ? STRING_FORMATTER .format ("%d:%02d:%02d" , hours , minutes , seconds )
108- : STRING_FORMATTER .format ("%02d:%02d" , minutes , seconds )
109- ).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 );
110108 }
111109
112110 @ NonNull
113111 public static String formatSpeed (final double speed ) {
114- return SPEED_FORMATTER .format (speed );
112+ return FORMATTERS_PROVIDER . formatters (). speed () .format (speed );
115113 }
116114
117115 @ NonNull
118116 public static String formatPitch (final double pitch ) {
119- return PITCH_FORMATTER .format (pitch );
120- }
121-
122- @ NonNull
123- public static String subtitleMimeTypesOf (@ NonNull final MediaFormat format ) {
124- switch (format ) {
125- case VTT :
126- return MimeTypes .TEXT_VTT ;
127- case TTML :
128- return MimeTypes .APPLICATION_TTML ;
129- default :
130- throw new IllegalArgumentException ("Unrecognized mime type: " + format .name ());
131- }
117+ return FORMATTERS_PROVIDER .formatters ().pitch ().format (pitch );
132118 }
133119
134120 @ NonNull
@@ -219,9 +205,8 @@ public static PlayQueue autoQueueOf(@NonNull final StreamInfo info,
219205 ? null : getAutoQueuedSinglePlayQueue (autoQueueItems .get (0 ));
220206 }
221207
222- ////////////////////////////////////////////////////////////////////////////
223- // Settings Resolution
224- ////////////////////////////////////////////////////////////////////////////
208+ // endregion
209+ // region Resolution
225210
226211 public static boolean isResumeAfterAudioFocusGain (@ NonNull final Context context ) {
227212 return getPreferences (context )
@@ -405,9 +390,8 @@ public static int getProgressiveLoadIntervalBytes(@NonNull final Context context
405390 return Integer .parseInt (preferredIntervalBytes ) * 1024 ;
406391 }
407392
408- ////////////////////////////////////////////////////////////////////////////
409- // Private helpers
410- ////////////////////////////////////////////////////////////////////////////
393+ // endregion
394+ // region Private helpers
411395
412396 @ NonNull
413397 private static SharedPreferences getPreferences (@ NonNull final Context context ) {
@@ -427,9 +411,8 @@ private static SinglePlayQueue getAutoQueuedSinglePlayQueue(
427411 }
428412
429413
430- ////////////////////////////////////////////////////////////////////////////
431- // Utils used by player
432- ////////////////////////////////////////////////////////////////////////////
414+ // endregion
415+ // region Utils used by player
433416
434417 @ RepeatMode
435418 public static int nextRepeatMode (@ RepeatMode final int repeatMode ) {
@@ -503,4 +486,43 @@ public static int retrieveSeekDurationFromPreferences(final Player player) {
503486 player .getContext ().getString (R .string .seek_duration_key ),
504487 player .getContext ().getString (R .string .seek_duration_default_value ))));
505488 }
489+
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+
527+ // endregion
506528}
0 commit comments