Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 63 additions & 41 deletions app/src/main/java/org/schabi/newpipe/player/helper/PlayerHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@
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;
Expand All @@ -47,13 +45,14 @@
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;
Expand All @@ -62,11 +61,7 @@
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,
Expand All @@ -89,9 +84,11 @@ public final class PlayerHelper {
private PlayerHelper() {
}

////////////////////////////////////////////////////////////////////////////
// Exposed helpers
////////////////////////////////////////////////////////////////////////////
// region Exposed helpers

public static void resetFormat() {
FORMATTERS_PROVIDER.reset();
}

@NonNull
public static String getTimeString(final int milliSeconds) {
Expand All @@ -100,35 +97,24 @@ public static String getTimeString(final int milliSeconds) {
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);
}

@NonNull
public static String subtitleMimeTypesOf(@NonNull final MediaFormat format) {
switch (format) {
case VTT:
return MimeTypes.TEXT_VTT;
case TTML:
return MimeTypes.APPLICATION_TTML;
default:
throw new IllegalArgumentException("Unrecognized mime type: " + format.name());
}
return FORMATTERS_PROVIDER.formatters().pitch().format(pitch);
}

@NonNull
Expand Down Expand Up @@ -219,9 +205,8 @@ public static PlayQueue autoQueueOf(@NonNull final StreamInfo info,
? null : getAutoQueuedSinglePlayQueue(autoQueueItems.get(0));
}

////////////////////////////////////////////////////////////////////////////
// Settings Resolution
////////////////////////////////////////////////////////////////////////////
// endregion
// region Resolution

public static boolean isResumeAfterAudioFocusGain(@NonNull final Context context) {
return getPreferences(context)
Expand Down Expand Up @@ -405,9 +390,8 @@ public static int getProgressiveLoadIntervalBytes(@NonNull final Context context
return Integer.parseInt(preferredIntervalBytes) * 1024;
}

////////////////////////////////////////////////////////////////////////////
// Private helpers
////////////////////////////////////////////////////////////////////////////
// endregion
// region Private helpers

@NonNull
private static SharedPreferences getPreferences(@NonNull final Context context) {
Expand All @@ -427,9 +411,8 @@ private static SinglePlayQueue getAutoQueuedSinglePlayQueue(
}


////////////////////////////////////////////////////////////////////////////
// Utils used by player
////////////////////////////////////////////////////////////////////////////
// endregion
// region Utils used by player

@RepeatMode
public static int nextRepeatMode(@RepeatMode final int repeatMode) {
Expand Down Expand Up @@ -503,4 +486,43 @@ public static int retrieveSeekDurationFromPreferences(final Player player) {
player.getContext().getString(R.string.seek_duration_key),
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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;
Expand Down Expand Up @@ -107,5 +108,6 @@ public void onDestroy() {
NewPipe.setupLocalization(
Localization.getPreferredLocalization(context),
Localization.getPreferredContentCountry(context));
PlayerHelper.resetFormat();
}
}
7 changes: 3 additions & 4 deletions app/src/main/java/org/schabi/newpipe/util/Localization.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,13 @@ public static String localizeNumber(final long number) {
}

public static String localizeNumber(final double number) {
final NumberFormat nf = NumberFormat.getInstance(getAppLocale());
return nf.format(number);
return NumberFormat.getInstance(getAppLocale()).format(number);
}

public static String formatDate(@NonNull final OffsetDateTime offsetDateTime) {
return DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
.withLocale(getAppLocale()).format(offsetDateTime
.atZoneSameInstant(ZoneId.systemDefault()));
.withLocale(getAppLocale())
.format(offsetDateTime.atZoneSameInstant(ZoneId.systemDefault()));
}

@SuppressLint("StringFormatInvalid")
Expand Down