From d684ecaffda7fd85eb917a6e9e4275702f8df584 Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Sun, 10 Feb 2019 17:15:37 +0000 Subject: [PATCH] Removing div tag attributes to avoid images not fitting the screen width --- .../readrops/app/database/ItemWithFeed.java | 9 ++++++ .../readrops/app/database/dao/ItemDao.java | 2 +- .../readrops/app/utils/ReadropsWebView.java | 32 ++++++++++++++++--- app/src/main/res/values/html.xml | 12 ++++--- 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/readrops/app/database/ItemWithFeed.java b/app/src/main/java/com/readrops/app/database/ItemWithFeed.java index f7c491ac..c8dd9ab0 100644 --- a/app/src/main/java/com/readrops/app/database/ItemWithFeed.java +++ b/app/src/main/java/com/readrops/app/database/ItemWithFeed.java @@ -23,6 +23,9 @@ public class ItemWithFeed { @ColumnInfo(name = "icon_url") private String feedIconUrl; + @ColumnInfo(name = "siteUrl") + private String websiteUrl; + public Item getItem() { return item; } @@ -63,5 +66,11 @@ public class ItemWithFeed { this.bgColor = bgColor; } + public String getWebsiteUrl() { + return websiteUrl; + } + public void setWebsiteUrl(String websiteUrl) { + this.websiteUrl = websiteUrl; + } } diff --git a/app/src/main/java/com/readrops/app/database/dao/ItemDao.java b/app/src/main/java/com/readrops/app/database/dao/ItemDao.java index cd2be889..54b8fcf9 100644 --- a/app/src/main/java/com/readrops/app/database/dao/ItemDao.java +++ b/app/src/main/java/com/readrops/app/database/dao/ItemDao.java @@ -32,6 +32,6 @@ public interface ItemDao { @Insert void insertAll(List items); - @Query("Select title, Item.description, content, pub_date, image_link, author, text_color, background_color, read_time, name from Item Inner Join Feed on Item.feed_id = Feed.id And Item.id = :id") + @Query("Select title, Item.description, content, pub_date, image_link, author, text_color, background_color, read_time, name, siteUrl from Item Inner Join Feed on Item.feed_id = Feed.id And Item.id = :id") LiveData getItemById(int id); } diff --git a/app/src/main/java/com/readrops/app/utils/ReadropsWebView.java b/app/src/main/java/com/readrops/app/utils/ReadropsWebView.java index 18b6b690..9095f111 100644 --- a/app/src/main/java/com/readrops/app/utils/ReadropsWebView.java +++ b/app/src/main/java/com/readrops/app/utils/ReadropsWebView.java @@ -3,6 +3,7 @@ package com.readrops.app.utils; import android.annotation.SuppressLint; import android.content.Context; import android.util.AttributeSet; +import android.util.Log; import android.webkit.WebSettings; import android.webkit.WebView; @@ -12,7 +13,13 @@ import com.readrops.app.database.entities.Item; import com.readrops.readropslibrary.Utils.LibUtils; import org.jsoup.Jsoup; +import org.jsoup.nodes.Attribute; +import org.jsoup.nodes.Attributes; import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; + +import java.util.Iterator; public class ReadropsWebView extends WebView { @@ -28,7 +35,7 @@ public class ReadropsWebView extends WebView { public void setItem(ItemWithFeed itemWithFeed, int width) { this.itemWithFeed = itemWithFeed; this.width = width; - loadData(getText(), LibUtils.HTML_CONTENT_TYPE, "utf-8"); + loadData(getText(), LibUtils.HTML_CONTENT_TYPE, "UTF-8"); } @SuppressLint("SetJavaScriptEnabled") @@ -36,22 +43,39 @@ public class ReadropsWebView extends WebView { WebSettings settings = getSettings(); settings.setJavaScriptEnabled(true); + settings.setBuiltInZoomControls(true); + settings.setDisplayZoomControls(false); + setBackgroundColor(getResources().getColor(R.color.colorBackground)); - setPadding(0, 0, 0, 0); } private String getText() { if (itemWithFeed.getItem().getText() != null) { - Document document = Jsoup.parse(itemWithFeed.getItem().getText()); + Document document = Jsoup.parse(itemWithFeed.getItem().getText(), itemWithFeed.getWebsiteUrl()); - return getContext().getString(R.string.webview_html_template, String.valueOf(width), + formatDocument(document); + + return getContext().getString(R.string.webview_html_template, Utils.getCssColor(itemWithFeed.getBgColor() != 0 ? itemWithFeed.getBgColor() : (itemWithFeed.getColor() != 0 ? itemWithFeed.getColor() : getResources().getColor(R.color.colorPrimary))), document.body().html()); } else return null; + } + private void formatDocument(Document document) { + Elements elements = document.select("figure,figcaption"); + for (Element element : elements) { + element.unwrap(); + } + + elements.clear(); + elements = document.select("div"); + + for (Element element : elements) { + element.clearAttributes(); + } } diff --git a/app/src/main/res/values/html.xml b/app/src/main/res/values/html.xml index 6246e625..b52f41ab 100644 --- a/app/src/main/res/values/html.xml +++ b/app/src/main/res/values/html.xml @@ -6,9 +6,10 @@ - - %3$s + %2$s ]]> \ No newline at end of file