Merge pull request #3825 from ByteHamster/do-not-linkify-duration

Do not linkify shownotes duration
This commit is contained in:
H. Lehmann 2020-02-04 15:14:49 +01:00 committed by GitHub
commit 9c1232532c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 129 additions and 112 deletions

View File

@ -64,6 +64,7 @@ public class TextOnlyFeedsTest {
openNavDrawer();
onDrawerItem(withText(feed.getTitle())).perform(scrollTo(), click());
onView(withText(feed.getItemAtIndex(0).getTitle())).perform(click());
onView(isRoot()).perform(waitForView(withText(R.string.mark_read_no_media_label), 3000));
onView(withText(R.string.mark_read_no_media_label)).perform(click());
onView(isRoot()).perform(waitForView(allOf(withText(R.string.mark_read_no_media_label), not(isDisplayed())), 3000));
}

View File

@ -26,7 +26,7 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* Test class for timeline
* Test class for timeline.
*/
@SmallTest
public class TimelineTest {
@ -34,7 +34,7 @@ public class TimelineTest {
private Context context;
@Before
public void setUp() throws Exception {
public void setUp() {
context = InstrumentationRegistry.getTargetContext();
}
@ -49,128 +49,151 @@ public class TimelineTest {
}
@Test
public void testProcessShownotesAddTimecodeHHMMSSNoChapters() throws Exception {
public void testProcessShownotesAddTimecodeHHMMSSNoChapters() {
final String timeStr = "10:11:12";
final long time = 3600 * 1000 * 10 + 60 * 1000 * 11 + 12 * 1000;
Playable p = newTestPlayable(null, "<p> Some test text with a timecode " + timeStr + " here.</p>", Integer.MAX_VALUE);
Playable p = newTestPlayable(null, "<p> Some test text with a timecode "
+ timeStr + " here.</p>", Integer.MAX_VALUE);
Timeline t = new Timeline(context, p);
String res = t.processShownotes();
checkLinkCorrect(res, new long[]{time}, new String[]{timeStr});
}
@Test
public void testProcessShownotesAddTimecodeHHMMSSMoreThen24HoursNoChapters() throws Exception {
public void testProcessShownotesAddTimecodeHHMMSSMoreThen24HoursNoChapters() {
final String timeStr = "25:00:00";
final long time = 25 * 60 * 60 * 1000;
Playable p = newTestPlayable(null, "<p> Some test text with a timecode " + timeStr + " here.</p>", Integer.MAX_VALUE);
Playable p = newTestPlayable(null, "<p> Some test text with a timecode "
+ timeStr + " here.</p>", Integer.MAX_VALUE);
Timeline t = new Timeline(context, p);
String res = t.processShownotes();
checkLinkCorrect(res, new long[]{time}, new String[]{timeStr});
}
@Test
public void testProcessShownotesAddTimecodeHHMMNoChapters() throws Exception {
public void testProcessShownotesAddTimecodeHHMMNoChapters() {
final String timeStr = "10:11";
final long time = 3600 * 1000 * 10 + 60 * 1000 * 11;
Playable p = newTestPlayable(null, "<p> Some test text with a timecode " + timeStr + " here.</p>", Integer.MAX_VALUE);
Playable p = newTestPlayable(null, "<p> Some test text with a timecode "
+ timeStr + " here.</p>", Integer.MAX_VALUE);
Timeline t = new Timeline(context, p);
String res = t.processShownotes();
checkLinkCorrect(res, new long[]{time}, new String[]{timeStr});
}
@Test
public void testProcessShownotesAddTimecodeMMSSNoChapters() throws Exception {
public void testProcessShownotesAddTimecodeMMSSNoChapters() {
final String timeStr = "10:11";
final long time = 10 * 60 * 1000 + 11 * 1000;
Playable p = newTestPlayable(null, "<p> Some test text with a timecode " + timeStr + " here.</p>", 11 * 60 * 1000);
Playable p = newTestPlayable(null, "<p> Some test text with a timecode "
+ timeStr + " here.</p>", 11 * 60 * 1000);
Timeline t = new Timeline(context, p);
String res = t.processShownotes();
checkLinkCorrect(res, new long[]{time}, new String[]{timeStr});
}
@Test
public void testProcessShownotesAddTimecodeHMMSSNoChapters() throws Exception {
public void testProcessShownotesAddTimecodeHMMSSNoChapters() {
final String timeStr = "2:11:12";
final long time = 2 * 60 * 60 * 1000 + 11 * 60 * 1000 + 12 * 1000;
Playable p = newTestPlayable(null, "<p> Some test text with a timecode " + timeStr + " here.</p>", Integer.MAX_VALUE);
Playable p = newTestPlayable(null, "<p> Some test text with a timecode "
+ timeStr + " here.</p>", Integer.MAX_VALUE);
Timeline t = new Timeline(context, p);
String res = t.processShownotes();
checkLinkCorrect(res, new long[]{time}, new String[]{timeStr});
}
@Test
public void testProcessShownotesAddTimecodeMSSNoChapters() throws Exception {
public void testProcessShownotesAddTimecodeMSSNoChapters() {
final String timeStr = "1:12";
final long time = 60 * 1000 + 12 * 1000;
Playable p = newTestPlayable(null, "<p> Some test text with a timecode " + timeStr + " here.</p>", 2 * 60 * 1000);
Playable p = newTestPlayable(null, "<p> Some test text with a timecode "
+ timeStr + " here.</p>", 2 * 60 * 1000);
Timeline t = new Timeline(context, p);
String res = t.processShownotes();
checkLinkCorrect(res, new long[]{time}, new String[]{timeStr});
}
@Test
public void testProcessShownotesAddTimecodeMultipleFormatsNoChapters() throws Exception {
final String[] timeStrings = new String[]{ "10:12", "1:10:12" };
Playable p = newTestPlayable(null, "<p> Some test text with a timecode " + timeStrings[0] + " here. Hey look another one " + timeStrings[1] + " here!</p>", 2 * 60 * 60 * 1000);
public void testProcessShownotesAddNoTimecodeDuration() {
final String timeStr = "2:11:12";
final int time = 2 * 60 * 60 * 1000 + 11 * 60 * 1000 + 12 * 1000;
String originalText = "<p> Some test text with a timecode " + timeStr + " here.</p>";
Playable p = newTestPlayable(null, originalText, time);
Timeline t = new Timeline(context, p);
String res = t.processShownotes();
checkLinkCorrect(res, new long[]{ 10 * 60 * 1000 + 12 * 1000, 60 * 60 * 1000 + 10 * 60 * 1000 + 12 * 1000 }, timeStrings);
Document d = Jsoup.parse(res);
assertEquals("Should not parse time codes that equal duration", 0, d.body().getElementsByTag("a").size());
}
@Test
public void testProcessShownotesAddTimecodeMultipleShortFormatNoChapters() throws Exception {
public void testProcessShownotesAddTimecodeMultipleFormatsNoChapters() {
final String[] timeStrings = new String[]{ "10:12", "1:10:12" };
Playable p = newTestPlayable(null, "<p> Some test text with a timecode "
+ timeStrings[0] + " here. Hey look another one " + timeStrings[1] + " here!</p>", 2 * 60 * 60 * 1000);
Timeline t = new Timeline(context, p);
String res = t.processShownotes();
checkLinkCorrect(res, new long[]{ 10 * 60 * 1000 + 12 * 1000,
60 * 60 * 1000 + 10 * 60 * 1000 + 12 * 1000 }, timeStrings);
}
@Test
public void testProcessShownotesAddTimecodeMultipleShortFormatNoChapters() {
// One of these timecodes fits as HH:MM and one does not so both should be parsed as MM:SS.
final String[] timeStrings = new String[]{ "10:12", "2:12" };
Playable p = newTestPlayable(null, "<p> Some test text with a timecode " + timeStrings[0] + " here. Hey look another one " + timeStrings[1] + " here!</p>", 3 * 60 * 60 * 1000);
Playable p = newTestPlayable(null, "<p> Some test text with a timecode "
+ timeStrings[0] + " here. Hey look another one " + timeStrings[1] + " here!</p>", 3 * 60 * 60 * 1000);
Timeline t = new Timeline(context, p);
String res = t.processShownotes();
checkLinkCorrect(res, new long[]{ 10 * 60 * 1000 + 12 * 1000, 2 * 60 * 1000 + 12 * 1000 }, timeStrings);
}
@Test
public void testProcessShownotesAddTimecodeParentheses() throws Exception {
public void testProcessShownotesAddTimecodeParentheses() {
final String timeStr = "10:11";
final long time = 3600 * 1000 * 10 + 60 * 1000 * 11;
Playable p = newTestPlayable(null, "<p> Some test text with a timecode (" + timeStr + ") here.</p>", Integer.MAX_VALUE);
Playable p = newTestPlayable(null, "<p> Some test text with a timecode ("
+ timeStr + ") here.</p>", Integer.MAX_VALUE);
Timeline t = new Timeline(context, p);
String res = t.processShownotes();
checkLinkCorrect(res, new long[]{time}, new String[]{timeStr});
}
@Test
public void testProcessShownotesAddTimecodeBrackets() throws Exception {
public void testProcessShownotesAddTimecodeBrackets() {
final String timeStr = "10:11";
final long time = 3600 * 1000 * 10 + 60 * 1000 * 11;
Playable p = newTestPlayable(null, "<p> Some test text with a timecode [" + timeStr + "] here.</p>", Integer.MAX_VALUE);
Playable p = newTestPlayable(null, "<p> Some test text with a timecode ["
+ timeStr + "] here.</p>", Integer.MAX_VALUE);
Timeline t = new Timeline(context, p);
String res = t.processShownotes();
checkLinkCorrect(res, new long[]{time}, new String[]{timeStr});
}
@Test
public void testProcessShownotesAddTimecodeAngleBrackets() throws Exception {
public void testProcessShownotesAddTimecodeAngleBrackets() {
final String timeStr = "10:11";
final long time = 3600 * 1000 * 10 + 60 * 1000 * 11;
Playable p = newTestPlayable(null, "<p> Some test text with a timecode <" + timeStr + "> here.</p>", Integer.MAX_VALUE);
Playable p = newTestPlayable(null, "<p> Some test text with a timecode <"
+ timeStr + "> here.</p>", Integer.MAX_VALUE);
Timeline t = new Timeline(context, p);
String res = t.processShownotes();
checkLinkCorrect(res, new long[]{time}, new String[]{timeStr});
}
@Test
public void testProcessShownotesAndInvalidTimecode() throws Exception {
public void testProcessShownotesAndInvalidTimecode() {
final String[] timeStrs = new String[] {"2:1", "0:0", "000", "00", "00:000"};
StringBuilder shownotes = new StringBuilder("<p> Some test text with timecodes ");
@ -197,14 +220,15 @@ public class TimelineTest {
assertTrue(href.endsWith(String.valueOf(timecodes[countedLinks])));
assertEquals(timecodeStr[countedLinks], text);
countedLinks++;
assertTrue("Contains too many links: " + countedLinks + " > " + timecodes.length, countedLinks <= timecodes.length);
assertTrue("Contains too many links: " + countedLinks + " > "
+ timecodes.length, countedLinks <= timecodes.length);
}
}
assertEquals(timecodes.length, countedLinks);
}
@Test
public void testIsTimecodeLink() throws Exception {
public void testIsTimecodeLink() {
assertFalse(Timeline.isTimecodeLink(null));
assertFalse(Timeline.isTimecodeLink("http://antennapod/timecode/123123"));
assertFalse(Timeline.isTimecodeLink("antennapod://timecode/"));
@ -215,7 +239,7 @@ public class TimelineTest {
}
@Test
public void testGetTimecodeLinkTime() throws Exception {
public void testGetTimecodeLinkTime() {
assertEquals(-1, Timeline.getTimecodeLinkTime(null));
assertEquals(-1, Timeline.getTimecodeLinkTime("http://timecode/123"));
assertEquals(123, Timeline.getTimecodeLinkTime("antennapod://timecode/123"));

View File

@ -0,0 +1,37 @@
@font-face {
font-family: 'Roboto-Light';
src: url('file:///android_asset/Roboto-Light.ttf');
}
* {
color: %s;
font-family: roboto-Light;
font-size: 13pt;
overflow-wrap: break-word;
}
a {
font-style: normal;
text-decoration: none;
font-weight: normal;
color: #00A8DF;
}
a.timecode {
color: #669900;
}
img, iframe {
display: block;
margin: 10 auto;
max-width: 100%%;
height: auto;
}
body {
margin: %dpx %dpx %dpx %dpx;
}
p#apNoShownotes {
position: fixed;
top: 50%%;
left: 50%%;
transform: translate(-50%%, -50%%);
text-align: center;
-webkit-text-size-adjust: none;
font-size: 80%%;
}

View File

@ -9,11 +9,15 @@ import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import de.danoeh.antennapod.core.feed.FeedItem;
import org.apache.commons.io.IOUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -27,48 +31,20 @@ import de.danoeh.antennapod.core.util.ShownotesProvider;
* shownotes to navigate to another position in the podcast or by highlighting certain parts of the shownotesProvider's
* shownotes.
* <p/>
* A timeline object needs a shownotesProvider from which the chapter information is retrieved and shownotes are generated.
* A timeline object needs a shownotesProvider from which the chapter information
* is retrieved and shownotes are generated.
*/
public class Timeline {
private static final String TAG = "Timeline";
private static final String WEBVIEW_STYLE =
"@font-face {"
+ "font-family: 'Roboto-Light';"
+ "src: url('file:///android_asset/Roboto-Light.ttf');"
+ "}"
+ "* {"
+ "color: %s;"
+ "font-family: roboto-Light;"
+ "font-size: 13pt;"
+ "overflow-wrap: break-word;"
+ "}"
+ "a {"
+ "font-style: normal;"
+ "text-decoration: none;"
+ "font-weight: normal;"
+ "color: #00A8DF;"
+ "}"
+ "a.timecode {"
+ "color: #669900;"
+ "}"
+ "img, iframe {"
+ "display: block;"
+ "margin: 10 auto;"
+ "max-width: %s;"
+ "height: auto;"
+ "}"
+ "body {"
+ "margin: %dpx %dpx %dpx %dpx;"
+ "}";
private ShownotesProvider shownotesProvider;
private static final Pattern TIMECODE_LINK_REGEX = Pattern.compile("antennapod://timecode/((\\d+))");
private static final String TIMECODE_LINK = "<a class=\"timecode\" href=\"antennapod://timecode/%d\">%s</a>";
private static final Pattern TIMECODE_REGEX = Pattern.compile("\\b((\\d+):)?(\\d+):(\\d{2})\\b");
private static final Pattern LINE_BREAK_REGEX = Pattern.compile("<br */?>");
private final ShownotesProvider shownotesProvider;
private final String noShownotesLabel;
private final String colorPrimaryString;
private final String colorSecondaryString;
private final int pageMargin;
private final String webviewStyle;
public Timeline(Context context, ShownotesProvider shownotesProvider) {
if (shownotesProvider == null) {
@ -80,26 +56,21 @@ public class Timeline {
TypedArray res = context.getTheme().obtainStyledAttributes(new int[]{android.R.attr.textColorPrimary});
@ColorInt int col = res.getColor(0, 0);
colorPrimaryString = "rgba(" + Color.red(col) + "," + Color.green(col) + "," +
Color.blue(col) + "," + (Color.alpha(col) / 255.0) + ")";
final String colorPrimary = "rgba(" + Color.red(col) + "," + Color.green(col) + ","
+ Color.blue(col) + "," + (Color.alpha(col) / 255.0) + ")";
res.recycle();
res = context.getTheme().obtainStyledAttributes(new int[]{android.R.attr.textColorSecondary});
col = res.getColor(0, 0);
colorSecondaryString = "rgba(" + Color.red(col) + "," + Color.green(col) + "," +
Color.blue(col) + "," + (Color.alpha(col) / 255.0) + ")";
res.recycle();
pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8,
context.getResources().getDisplayMetrics()
);
final int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8,
context.getResources().getDisplayMetrics());
String styleString = "";
try {
InputStream templateStream = context.getAssets().open("shownotes-style.css");
styleString = IOUtils.toString(templateStream, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
webviewStyle = String.format(Locale.getDefault(), styleString, colorPrimary, margin, margin, margin, margin);
}
private static final Pattern TIMECODE_LINK_REGEX = Pattern.compile("antennapod://timecode/((\\d+))");
private static final String TIMECODE_LINK = "<a class=\"timecode\" href=\"antennapod://timecode/%d\">%s</a>";
private static final Pattern TIMECODE_REGEX = Pattern.compile("\\b((\\d+):)?(\\d+):(\\d{2})\\b");
private static final Pattern LINE_BREAK_REGEX = Pattern.compile("<br */?>");
/**
* Applies an app-specific CSS stylesheet and adds timecode links (optional).
* <p/>
@ -110,10 +81,6 @@ public class Timeline {
*/
@NonNull
public String processShownotes() {
final Playable playable = (shownotesProvider instanceof Playable) ? (Playable) shownotesProvider : null;
// load shownotes
String shownotes;
try {
shownotes = shownotesProvider.loadShownotes().call();
@ -124,21 +91,7 @@ public class Timeline {
if (TextUtils.isEmpty(shownotes)) {
Log.d(TAG, "shownotesProvider contained no shownotes. Returning 'no shownotes' message");
shownotes = "<html>" +
"<head>" +
"<style type='text/css'>" +
"html, body { margin: 0; padding: 0; width: 100%; height: 100%; } " +
"html { display: table; }" +
"body { display: table-cell; vertical-align: middle; text-align:center;" +
"-webkit-text-size-adjust: none; font-size: 87%; color: " + colorSecondaryString + ";} " +
"</style>" +
"</head>" +
"<body>" +
"<p>" + noShownotesLabel + "</p>" +
"</body>" +
"</html>";
Log.d(TAG, "shownotes: " + shownotes);
return shownotes;
shownotes = "<html><head></head><body><p id='apNoShownotes'>" + noShownotesLabel + "</p></body></html>";
}
// replace ASCII line breaks with HTML ones if shownotes don't contain HTML line breaks already
@ -147,14 +100,10 @@ public class Timeline {
}
Document document = Jsoup.parse(shownotes);
// apply style
String styleStr = String.format(Locale.getDefault(), WEBVIEW_STYLE, colorPrimaryString, "100%",
pageMargin, pageMargin, pageMargin, pageMargin);
document.head().appendElement("style").attr("type", "text/css").text(styleStr);
document.head().appendElement("style").attr("type", "text/css").text(webviewStyle);
// apply timecode links
addTimecodes(document, playable);
addTimecodes(document);
return document.toString();
}
@ -184,7 +133,7 @@ public class Timeline {
return -1;
}
private void addTimecodes(Document document, final Playable playable) {
private void addTimecodes(Document document) {
Elements elementsWithTimeCodes = document.body().getElementsMatchingOwnText(TIMECODE_REGEX);
Log.d(TAG, "Recognized " + elementsWithTimeCodes.size() + " timecodes");
@ -193,7 +142,13 @@ public class Timeline {
return;
}
int playableDuration = playable == null ? Integer.MAX_VALUE : playable.getDuration();
int playableDuration = Integer.MAX_VALUE;
if (shownotesProvider instanceof Playable) {
playableDuration = ((Playable) shownotesProvider).getDuration();
} else if (shownotesProvider instanceof FeedItem && ((FeedItem) shownotesProvider).getMedia() != null) {
playableDuration = ((FeedItem) shownotesProvider).getMedia().getDuration();
}
boolean useHourFormat = true;
if (playableDuration != Integer.MAX_VALUE) {