summaryrefslogtreecommitdiff
path: root/core/src/main/java/de
diff options
context:
space:
mode:
authorNathan Mascitelli <mascitelli.nathan@gmail.com>2019-02-13 21:06:19 -0500
committerNathan Mascitelli <mascitelli.nathan@gmail.com>2019-02-13 21:06:19 -0500
commite94e4bc3d03d2e6d5122700fd3161bfa63dc4371 (patch)
tree8f1f237a0180e900bfd1d793c57541b517da40f2 /core/src/main/java/de
parentc49e98b5462c9511dffe250a94b17be637884c49 (diff)
downloadAntennaPod-e94e4bc3d03d2e6d5122700fd3161bfa63dc4371.zip
Use a single format for short timecodes
It is unlikely that multiple formats for short timecodes would be used in one document. Therefor we will parse all the short timecodes to see if they are all less then the duration as HH:MM. If they are we will use that, otherwise we will parse them as MM:SS.
Diffstat (limited to 'core/src/main/java/de')
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/util/playback/Timeline.java96
1 files changed, 45 insertions, 51 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/playback/Timeline.java b/core/src/main/java/de/danoeh/antennapod/core/util/playback/Timeline.java
index 34f00fe79..057de06dd 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/util/playback/Timeline.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/util/playback/Timeline.java
@@ -170,70 +170,64 @@ public class Timeline {
Elements elementsWithTimeCodes = document.body().getElementsMatchingOwnText(TIMECODE_REGEX);
Log.d(TAG, "Recognized " + elementsWithTimeCodes.size() + " timecodes");
- // Assuming the timecodes are going to increase through the document loop through the
- // elements backwards so we can determine when/if we need to shift from HH:MM to MM:SS
+ if (elementsWithTimeCodes.size() == 0) {
+ // No elements with timecodes
+ return;
+ }
+
+ int playableDuration = playable == null ? Integer.MAX_VALUE : playable.getDuration();
boolean useHourFormat = true;
- for (int i = elementsWithTimeCodes.size() - 1; i >= 0 ; i--) {
- Element element = elementsWithTimeCodes.get(i);
- Matcher matcherLong = TIMECODE_REGEX.matcher(element.html());
-
- // Get all matches and store in reverse order
- ArrayList<Pair<Boolean, String>> matches = new ArrayList<>();
- while (matcherLong.find()) {
- matches.add(0, new Pair<>(matcherLong.group(1) != null, matcherLong.group(0)));
- }
- // Now loop through the reversed matches and get the replacements. Store them in
- // non-reversed order.
- ArrayList<String> replacements = new ArrayList<>();
- for (Pair<Boolean, String> matchPair : matches) {
- boolean isLongFormat = matchPair.first;
- String group = matchPair.second;
- int time = isLongFormat
- ? Converter.durationStringLongToMs(group)
- : Converter.durationStringShortToMs(group, useHourFormat);
-
- String rep = group;
- if (playable == null) {
- rep = createTimeLink(time, group);
- } else {
- int duration = playable.getDuration();
-
- if (duration > time) {
- rep = createTimeLink(time, group);
- } else if (!isLongFormat && useHourFormat) {
-
- // The duration calculated in hours is too long and the timecode format is
- // short. So try and see if it will work when treated as minutes.
- time = Converter.durationStringShortToMs(group, false);
-
- if (duration > time) {
- // We have found the treating a short timecode as minutes works, do that
- // from now on.
- rep = createTimeLink(time, group);
+ if (playableDuration != Integer.MAX_VALUE) {
+
+ // We need to decide if we are going to treat short timecodes as HH:MM or MM:SS. To do
+ // so we will parse all the short timecodes and see if they fit in the duration. If one
+ // does not we will use MM:SS, otherwise all will be parsed as HH:MM.
+ for (Element element : elementsWithTimeCodes) {
+ Matcher matcherForElement = TIMECODE_REGEX.matcher(element.html());
+ while (matcherForElement.find()) {
+
+ // We only want short timecodes right now.
+ if (matcherForElement.group(1) == null) {
+ int time = Converter.durationStringShortToMs(matcherForElement.group(0), true);
+
+ // If the parsed timecode is greater then the duration then we know we need to
+ // use the minute format so we are done.
+ if (time > playableDuration) {
useHourFormat = false;
+ break;
}
}
}
- replacements.add(0, rep);
+ if (!useHourFormat) {
+ break;
+ }
}
+ }
- // Now that we have all the replacements, replace.
+ for (Element element : elementsWithTimeCodes) {
+
+ Matcher matcherForElement = TIMECODE_REGEX.matcher(element.html());
StringBuffer buffer = new StringBuffer();
- int index = 0;
- matcherLong.reset();
- while (matcherLong.find()) {
- matcherLong.appendReplacement(buffer, replacements.get(index));
- index++;
+
+ while (matcherForElement.find()) {
+ String group = matcherForElement.group(0);
+
+ int time = matcherForElement.group(1) != null
+ ? Converter.durationStringLongToMs(group)
+ : Converter.durationStringShortToMs(group, useHourFormat);
+
+ String replacementText = group;
+ if (time < playableDuration) {
+ replacementText = String.format(Locale.getDefault(), TIMECODE_LINK, time, group);
+ }
+
+ matcherForElement.appendReplacement(buffer, replacementText);
}
- matcherLong.appendTail(buffer);
+ matcherForElement.appendTail(buffer);
element.html(buffer.toString());
}
}
-
- private String createTimeLink(int time, String group) {
- return String.format(Locale.getDefault(), TIMECODE_LINK, time, group);
- }
}