mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2025-08-22 01:58:16 +00:00
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)
This commit is contained in:
parent
ebd5e1a318
commit
893a1cb699
@ -33,11 +33,9 @@ import com.google.android.exoplayer2.trackselection.ExoTrackSelection;
|
||||
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout;
|
||||
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout.ResizeMode;
|
||||
import com.google.android.exoplayer2.ui.CaptionStyleCompat;
|
||||
import com.google.android.exoplayer2.util.MimeTypes;
|
||||
|
||||
import org.schabi.newpipe.R;
|
||||
import org.schabi.newpipe.extractor.InfoItem;
|
||||
import org.schabi.newpipe.extractor.MediaFormat;
|
||||
import org.schabi.newpipe.extractor.stream.StreamInfo;
|
||||
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
|
||||
import org.schabi.newpipe.extractor.stream.SubtitlesStream;
|
||||
@ -47,13 +45,14 @@ import org.schabi.newpipe.player.playqueue.PlayQueue;
|
||||
import org.schabi.newpipe.player.playqueue.PlayQueueItem;
|
||||
import org.schabi.newpipe.player.playqueue.SinglePlayQueue;
|
||||
import org.schabi.newpipe.util.ListHelper;
|
||||
import org.schabi.newpipe.util.Localization;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.DecimalFormatSymbols;
|
||||
import java.text.NumberFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Formatter;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
@ -62,11 +61,7 @@ import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public final class PlayerHelper {
|
||||
private static final StringBuilder STRING_BUILDER = new StringBuilder();
|
||||
private static final Formatter STRING_FORMATTER =
|
||||
new Formatter(STRING_BUILDER, Locale.getDefault());
|
||||
private static final NumberFormat SPEED_FORMATTER = new DecimalFormat("0.##x");
|
||||
private static final NumberFormat PITCH_FORMATTER = new DecimalFormat("##%");
|
||||
private static final FormattersProvider FORMATTERS_PROVIDER = new FormattersProvider();
|
||||
|
||||
@Retention(SOURCE)
|
||||
@IntDef({AUTOPLAY_TYPE_ALWAYS, AUTOPLAY_TYPE_WIFI,
|
||||
@ -91,6 +86,10 @@ public final class PlayerHelper {
|
||||
|
||||
// region Exposed helpers
|
||||
|
||||
public static void resetFormat() {
|
||||
FORMATTERS_PROVIDER.reset();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static String getTimeString(final int milliSeconds) {
|
||||
final int seconds = (milliSeconds % 60000) / 1000;
|
||||
@ -98,23 +97,24 @@ public final class PlayerHelper {
|
||||
final int hours = (milliSeconds % 86400000) / 3600000;
|
||||
final int days = (milliSeconds % (86400000 * 7)) / 86400000;
|
||||
|
||||
STRING_BUILDER.setLength(0);
|
||||
return (days > 0
|
||||
? STRING_FORMATTER.format("%d:%02d:%02d:%02d", days, hours, minutes, seconds)
|
||||
: hours > 0
|
||||
? STRING_FORMATTER.format("%d:%02d:%02d", hours, minutes, seconds)
|
||||
: STRING_FORMATTER.format("%02d:%02d", minutes, seconds)
|
||||
).toString();
|
||||
final Formatters formatters = FORMATTERS_PROVIDER.formatters();
|
||||
if (days > 0) {
|
||||
return formatters.stringFormat("%d:%02d:%02d:%02d", days, hours, minutes, seconds);
|
||||
}
|
||||
|
||||
return hours > 0
|
||||
? formatters.stringFormat("%d:%02d:%02d", hours, minutes, seconds)
|
||||
: formatters.stringFormat("%02d:%02d", minutes, seconds);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static String formatSpeed(final double speed) {
|
||||
return SPEED_FORMATTER.format(speed);
|
||||
return FORMATTERS_PROVIDER.formatters().speed().format(speed);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static String formatPitch(final double pitch) {
|
||||
return PITCH_FORMATTER.format(pitch);
|
||||
return FORMATTERS_PROVIDER.formatters().pitch().format(pitch);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@ -487,5 +487,42 @@ public final class PlayerHelper {
|
||||
player.getContext().getString(R.string.seek_duration_default_value))));
|
||||
}
|
||||
|
||||
// endregion
|
||||
// region Format
|
||||
|
||||
static class FormattersProvider {
|
||||
private Formatters formatters;
|
||||
|
||||
public Formatters formatters() {
|
||||
if (formatters == null) {
|
||||
formatters = Formatters.create();
|
||||
}
|
||||
return formatters;
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
formatters = null;
|
||||
}
|
||||
}
|
||||
|
||||
record Formatters(
|
||||
Locale locale,
|
||||
NumberFormat speed,
|
||||
NumberFormat pitch) {
|
||||
|
||||
static Formatters create() {
|
||||
final Locale locale = Localization.getAppLocale();
|
||||
final DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(locale);
|
||||
return new Formatters(
|
||||
locale,
|
||||
new DecimalFormat("0.##x", dfs),
|
||||
new DecimalFormat("##%", dfs));
|
||||
}
|
||||
|
||||
String stringFormat(final String format, final Object... args) {
|
||||
return String.format(locale, format, args);
|
||||
}
|
||||
}
|
||||
|
||||
// endregion
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ import androidx.preference.Preference;
|
||||
import org.schabi.newpipe.DownloaderImpl;
|
||||
import org.schabi.newpipe.R;
|
||||
import org.schabi.newpipe.extractor.NewPipe;
|
||||
import org.schabi.newpipe.player.helper.PlayerHelper;
|
||||
import org.schabi.newpipe.util.Localization;
|
||||
import org.schabi.newpipe.util.image.ImageStrategy;
|
||||
import org.schabi.newpipe.util.image.PicassoHelper;
|
||||
@ -53,6 +54,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
|
||||
final Intent intent = new Intent(Settings.ACTION_APP_LOCALE_SETTINGS)
|
||||
.setData(Uri.fromParts("package", requireContext().getPackageName(), null));
|
||||
startActivity(intent);
|
||||
PlayerHelper.resetFormat();
|
||||
return true;
|
||||
});
|
||||
newAppLanguagePref.setVisible(true);
|
||||
@ -64,6 +66,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
|
||||
final String systemLang = getString(R.string.default_localization_key);
|
||||
final String tag = systemLang.equals(language) ? null : language;
|
||||
AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags(tag));
|
||||
PlayerHelper.resetFormat();
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user