diff options
author | Anderson Mesquita <andersonvom@gmail.com> | 2019-05-04 13:09:51 -0400 |
---|---|---|
committer | Anderson Mesquita <andersonvom@gmail.com> | 2019-05-05 00:04:06 -0400 |
commit | 9b41139709656d0e0a650acd0a17a67eac987ab7 (patch) | |
tree | 90ae33afa98904dea948c452bcce5bc4e0edaabb /core/src/main | |
parent | a4c852dac050e426587501b7b792cedb90571d4e (diff) | |
download | AntennaPod-9b41139709656d0e0a650acd0a17a67eac987ab7.zip |
Extract DurationParser
This makes it easier to test the logic of parsing duration strings.
References: #3024
Diffstat (limited to 'core/src/main')
-rw-r--r-- | core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSITunes.java | 21 | ||||
-rw-r--r-- | core/src/main/java/de/danoeh/antennapod/core/syndication/parsers/DurationParser.java | 35 |
2 files changed, 40 insertions, 16 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSITunes.java b/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSITunes.java index 62bc55e2e..1e069a1f0 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSITunes.java +++ b/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSITunes.java @@ -5,10 +5,9 @@ import android.util.Log; import org.xml.sax.Attributes; -import java.util.concurrent.TimeUnit; - import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.syndication.handler.HandlerState; +import de.danoeh.antennapod.core.syndication.parsers.DurationParser; public class NSITunes extends Namespace { @@ -72,22 +71,12 @@ public class NSITunes extends Namespace { if (TextUtils.isEmpty(durationStr)) { return; } - String[] parts = durationStr.trim().split(":"); + try { - int durationMs = 0; - if (parts.length == 2) { - durationMs += TimeUnit.MINUTES.toMillis(Long.parseLong(parts[0])) + - TimeUnit.SECONDS.toMillis((long) Float.parseFloat(parts[1])); - } else if (parts.length >= 3) { - durationMs += TimeUnit.HOURS.toMillis(Long.parseLong(parts[0])) + - TimeUnit.MINUTES.toMillis(Long.parseLong(parts[1])) + - TimeUnit.SECONDS.toMillis((long) Float.parseFloat(parts[2])); - } else { - return; - } - state.getTempObjects().put(DURATION, durationMs); + long durationMs = DurationParser.inMillis(durationStr); + state.getTempObjects().put(DURATION, (int) durationMs); } catch (NumberFormatException e) { - Log.e(NSTAG, "Duration \"" + durationStr + "\" could not be parsed"); + Log.e(NSTAG, String.format("Duration '%s' could not be parsed", durationStr)); } } diff --git a/core/src/main/java/de/danoeh/antennapod/core/syndication/parsers/DurationParser.java b/core/src/main/java/de/danoeh/antennapod/core/syndication/parsers/DurationParser.java new file mode 100644 index 000000000..e14866b4e --- /dev/null +++ b/core/src/main/java/de/danoeh/antennapod/core/syndication/parsers/DurationParser.java @@ -0,0 +1,35 @@ +package de.danoeh.antennapod.core.syndication.parsers; + +import static java.util.concurrent.TimeUnit.HOURS; +import static java.util.concurrent.TimeUnit.MINUTES; +import static java.util.concurrent.TimeUnit.SECONDS; + +public class DurationParser { + public static long inMillis(String durationStr) throws NumberFormatException { + String[] parts = durationStr.trim().split(":"); + + if (parts.length == 2) { + return toMillis("0", parts[0], parts[1]); + } else if (parts.length == 3) { + return toMillis(parts[0], parts[1], parts[2]); + } else { + throw new NumberFormatException(); + } + } + + private static long toMillis(String hours, String minutes, String seconds) { + return HOURS.toMillis(Long.parseLong(hours)) + + MINUTES.toMillis(Long.parseLong(minutes)) + + toMillis(seconds); + } + + private static long toMillis(String seconds) { + if (seconds.contains(".")) { + float value = Float.parseFloat(seconds); + float millis = value % 1; + return SECONDS.toMillis((long) value) + (long) (millis * 1000); + } else { + return SECONDS.toMillis(Long.parseLong(seconds)); + } + } +} |