From 6a0542ca1fe23537eeeab361c702a0b60a2579db Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Mon, 3 Feb 2020 23:17:04 +0100 Subject: Moved css to readable file instead of variable --- .../antennapod/core/util/playback/Timeline.java | 97 +++++----------------- 1 file changed, 23 insertions(+), 74 deletions(-) (limited to 'core/src/main/java/de/danoeh') 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 917f8e8e2..0fd658853 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 @@ -10,11 +10,14 @@ 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; @@ -34,43 +37,14 @@ import de.danoeh.antennapod.core.util.ShownotesProvider; 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 = "%s"; + private static final Pattern TIMECODE_REGEX = Pattern.compile("\\b((\\d+):)?(\\d+):(\\d{2})\\b"); + private static final Pattern LINE_BREAK_REGEX = Pattern.compile("
"); + 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) { @@ -82,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 = "%s"; - private static final Pattern TIMECODE_REGEX = Pattern.compile("\\b((\\d+):)?(\\d+):(\\d{2})\\b"); - private static final Pattern LINE_BREAK_REGEX = Pattern.compile("
"); - - /** * Applies an app-specific CSS stylesheet and adds timecode links (optional). *

@@ -112,8 +81,6 @@ public class Timeline { */ @NonNull public String processShownotes() { - // 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 = "" + - "" + - "" + - "" + - "" + - "

" + noShownotesLabel + "

" + - "" + - ""; - Log.d(TAG, "shownotes: " + shownotes); - return shownotes; + shownotes = "

" + noShownotesLabel + "

"; } // replace ASCII line breaks with HTML ones if shownotes don't contain HTML line breaks already @@ -147,11 +100,7 @@ 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); -- cgit v1.2.3