reduce complexity of method "getHtmlPage"

This commit is contained in:
David Luhmer 2019-04-09 23:57:08 -03:00
parent d93c5cf66d
commit cb67e17b53

View file

@ -88,105 +88,39 @@ public class RssItemToHtmlTask extends AsyncTask<Void, Void, String> {
String favIconUrl = null;
Feed feed = rssItem.getFeed();
//int[] colors = ColorHelper.getColorsFromAttributes(context,
// R.attr.dividerLineColor,
// R.attr.rssItemListBackground);
//int feedColor = colors[0];
if (feed != null) {
feedTitle = Html.escapeHtml(feed.getFeedTitle());
favIconUrl = feed.getFaviconUrl();
//if(feed.getAvgColour() != null) {
// feedColor = Integer.parseInt(feed.getAvgColour());
//}
}
if (favIconUrl != null) {
DiskCache diskCache = ImageLoader.getInstance().getDiskCache();
File file = diskCache.get(favIconUrl);
if(file != null) {
favIconUrl = "file://" + file.getAbsolutePath();
}
favIconUrl = getCachedFavIcon(favIconUrl);
} else {
favIconUrl = "file:///android_res/drawable/default_feed_icon_light.png";
}
String body_id = null;
THEME selectedTheme = ThemeChooser.getSelectedTheme();
switch (selectedTheme) {
case LIGHT:
body_id = "lightTheme";
break;
case DARK:
body_id = "darkTheme";
break;
case OLED:
body_id = "darkThemeOLED";
break;
}
String body_id = getSelectedTheme();
Log.v(TAG, "Selected Theme: " + body_id);
String rtlClass = isRightToLeft ? "rtl" : "";
//String borderSide = isRightToLeft ? "right" : "left";
StringBuilder builder = new StringBuilder();
builder.append("<html><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=0\" />");
builder.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"web.css\" />");
// font size scaling
double scalingFactor = Float.parseFloat(mPrefs.getString(SettingsActivity.SP_FONT_SIZE, "1.0"));
DecimalFormat fontFormat = new DecimalFormat("#.#");
builder.append("<style type=\"text/css\">");
builder.append(String.format(
":root { \n" +
"--fontsize-body: %sem; \n" +
"--fontsize-header: %sem; \n" +
"--fontsize-subscript: %sem; \n" +
"}",
fontFormat.format(scalingFactor*BODY_FONT_SIZE),
fontFormat.format(scalingFactor*HEADING_FONT_SIZE),
fontFormat.format(scalingFactor*SUBSCRIPT_FONT_SIZE)
));
builder.append(getFontSizeScalingCss(mPrefs));
builder.append("</style>");
builder.append(String.format("</head><body class=\"%s\" class=\"%s\">", body_id, rtlClass));
if (showHeader) {
builder.append("<div id=\"top_section\">");
builder.append(String.format("<div id=\"header\" class=\"%s\">", body_id));
String title = Html.escapeHtml(rssItem.getTitle());
String linkToFeed = Html.escapeHtml(rssItem.getLink());
builder.append(String.format("<a href=\"%s\">%s</a>", linkToFeed, title));
builder.append("</div>");
String authorOfArticle = Html.escapeHtml(rssItem.getAuthor());
if (authorOfArticle != null)
if (!authorOfArticle.trim().equals(""))
feedTitle += " - " + authorOfArticle.trim();
builder.append("<div id=\"header_small_text\">");
builder.append("<div id=\"subscription\">");
builder.append(String.format("<img id=\"imgFavicon\" src=\"%s\" />", favIconUrl));
builder.append(feedTitle.trim());
builder.append("</div>");
Date date = rssItem.getPubDate();
if (date != null) {
String dateString = (String) DateUtils.getRelativeTimeSpanString(date.getTime());
builder.append("<div id=\"datetime\">");
builder.append(dateString);
builder.append("</div>");
}
builder.append("</div>");
builder.append("</div>");
builder.append(
buildHeader(rssItem, body_id, feedTitle, favIconUrl)
);
}
String description = rssItem.getBody();
@ -207,6 +141,84 @@ public class RssItemToHtmlTask extends AsyncTask<Void, Void, String> {
return builder.toString().replaceAll("\"//", "\"https://");
}
private static String getSelectedTheme() {
THEME selectedTheme = ThemeChooser.getSelectedTheme();
switch (selectedTheme) {
case LIGHT:
return "lightTheme";
case DARK:
return "darkTheme";
case OLED:
return "darkThemeOLED";
default:
return null;
}
}
private static String buildHeader(RssItem rssItem, String body_id, String feedTitle, String favIconUrl) {
StringBuilder builder = new StringBuilder();
builder.append("<div id=\"top_section\">");
builder.append(String.format("<div id=\"header\" class=\"%s\">", body_id));
String title = Html.escapeHtml(rssItem.getTitle());
String linkToFeed = Html.escapeHtml(rssItem.getLink());
builder.append(String.format("<a href=\"%s\">%s</a>", linkToFeed, title));
builder.append("</div>");
String authorOfArticle = Html.escapeHtml(rssItem.getAuthor());
if (authorOfArticle != null) {
if (!authorOfArticle.trim().equals("")) {
feedTitle += " - " + authorOfArticle.trim();
}
}
builder.append("<div id=\"header_small_text\">");
builder.append("<div id=\"subscription\">");
builder.append(String.format("<img id=\"imgFavicon\" src=\"%s\" />", favIconUrl));
builder.append(feedTitle.trim());
builder.append("</div>");
Date date = rssItem.getPubDate();
if (date != null) {
String dateString = (String) DateUtils.getRelativeTimeSpanString(date.getTime());
builder.append("<div id=\"datetime\">");
builder.append(dateString);
builder.append("</div>");
}
builder.append("</div>");
builder.append("</div>");
return builder.toString();
}
private static String getCachedFavIcon(String favIconUrl) {
DiskCache diskCache = ImageLoader.getInstance().getDiskCache();
File file = diskCache.get(favIconUrl);
if(file != null) {
return "file://" + file.getAbsolutePath();
} else {
return favIconUrl; // Return favicon url if not cached
}
}
private static String getFontSizeScalingCss(SharedPreferences mPrefs) {
// font size scaling
double scalingFactor = Float.parseFloat(mPrefs.getString(SettingsActivity.SP_FONT_SIZE, "1.0"));
DecimalFormat fontFormat = new DecimalFormat("#.#");
return String.format(
":root { \n" +
"--fontsize-body: %sem; \n" +
"--fontsize-header: %sem; \n" +
"--fontsize-subscript: %sem; \n" +
"}",
fontFormat.format(scalingFactor*BODY_FONT_SIZE),
fontFormat.format(scalingFactor*HEADING_FONT_SIZE),
fontFormat.format(scalingFactor*SUBSCRIPT_FONT_SIZE)
);
}
private static String getDescriptionWithCachedImages(String text) {
List<String> links = ImageHandler.getImageLinksFromText(text);
DiskCache diskCache = ImageLoader.getInstance().getDiskCache();