From 9aa6cd5a64762356eb4a72e867e0e01a859d5ae5 Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 5 Nov 2023 14:54:48 +0100 Subject: [PATCH] Added check HTML to message source view --- .../java/eu/faircode/email/ActivityCode.java | 53 +++++++++++++++++++ app/src/main/res/menu/menu_code.xml | 5 ++ 2 files changed, 58 insertions(+) diff --git a/app/src/main/java/eu/faircode/email/ActivityCode.java b/app/src/main/java/eu/faircode/email/ActivityCode.java index b2ebb32bd4..f7781306c2 100644 --- a/app/src/main/java/eu/faircode/email/ActivityCode.java +++ b/app/src/main/java/eu/faircode/email/ActivityCode.java @@ -40,14 +40,19 @@ import android.widget.Toast; import androidx.activity.OnBackPressedCallback; import androidx.annotation.NonNull; +import androidx.appcompat.app.AlertDialog; import androidx.appcompat.widget.SearchView; import androidx.constraintlayout.widget.Group; import androidx.preference.PreferenceManager; import androidx.webkit.WebSettingsCompat; import androidx.webkit.WebViewFeature; +import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; +import org.jsoup.parser.ParseError; +import org.jsoup.parser.ParseErrorList; +import org.jsoup.parser.Parser; import org.w3c.dom.css.CSSStyleSheet; import java.io.File; @@ -252,6 +257,9 @@ public class ActivityCode extends ActivityBase { item.setChecked(links); load(); return true; + } else if (itemId == R.id.menu_check_html) { + checkHtml(); + return true; } else if (itemId == R.id.menu_save) { selectFile(); return true; @@ -393,6 +401,51 @@ public class ActivityCode extends ActivityBase { }.execute(this, args, "code:view"); } + private void checkHtml() { + Intent intent = getIntent(); + long id = intent.getLongExtra("id", -1L); + Bundle args = new Bundle(); + args.putLong("id", id); + + new SimpleTask() { + @Override + protected ParseErrorList onExecute(Context context, Bundle args) throws Throwable { + long id = args.getLong("id"); + + File file = EntityMessage.getFile(context, id); + Parser parser = Parser.htmlParser().setTrackErrors(20); + Jsoup.parse(file, StandardCharsets.UTF_8.name(), "", parser); + return parser.getErrors(); + } + + @Override + protected void onExecuted(Bundle args, ParseErrorList errors) { + SpannableStringBuilderEx ssb = new SpannableStringBuilderEx(); + ssb.append("Errors: ") + .append(Integer.toString(errors.size())) + .append("\n\n"); + for (ParseError error : errors) + ssb.append("At ") + .append(error.getCursorPos()) + .append(' ') + .append(error.getErrorMessage()) + .append("\n\n"); + + new AlertDialog.Builder(ActivityCode.this) + .setIcon(R.drawable.twotone_bug_report_24) + .setTitle(R.string.title_check_html) + .setMessage(ssb) + .setPositiveButton(android.R.string.ok, null) + .show(); + } + + @Override + protected void onException(Bundle args, Throwable ex) { + Log.unexpectedError(getSupportFragmentManager(), ex); + } + }.execute(this, args, "code:check"); + } + private void selectFile() { long id = getIntent().getLongExtra("id", -1L); diff --git a/app/src/main/res/menu/menu_code.xml b/app/src/main/res/menu/menu_code.xml index 8826cd7ba9..481506434c 100644 --- a/app/src/main/res/menu/menu_code.xml +++ b/app/src/main/res/menu/menu_code.xml @@ -32,6 +32,11 @@ android:title="@string/title_legend_enable_links" app:showAsAction="never" /> + +