Added HTML viewer

This commit is contained in:
M66B 2023-11-02 21:25:35 +01:00
parent e273586be1
commit b2e95082c5
12 changed files with 304 additions and 67 deletions

View File

@ -474,6 +474,15 @@
</intent-filter>
</activity>
<activity
android:name=".ActivityHTML"
android:description="@string/app_name"
android:exported="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:launchMode="singleInstance"
android:resizeableActivity="true" />
<activity
android:name=".ActivityDmarc"
android:enabled="false"

View File

@ -481,6 +481,15 @@
</intent-filter>
</activity>
<activity
android:name=".ActivityHTML"
android:description="@string/app_name"
android:exported="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:launchMode="singleInstance"
android:resizeableActivity="true" />
<activity
android:name=".ActivityDmarc"
android:description="@string/app_name"

View File

@ -480,6 +480,15 @@
</intent-filter>
</activity>
<activity
android:name=".ActivityHTML"
android:description="@string/app_name"
android:exported="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:launchMode="singleInstance"
android:resizeableActivity="true" />
<activity
android:name=".ActivityDmarc"
android:enabled="true"

View File

@ -480,6 +480,15 @@
</intent-filter>
</activity>
<activity
android:name=".ActivityHTML"
android:description="@string/app_name"
android:exported="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:launchMode="singleInstance"
android:resizeableActivity="true" />
<activity
android:name=".ActivityDmarc"
android:description="@string/app_name"

View File

@ -476,6 +476,15 @@
</intent-filter>
</activity>
<activity
android:name=".ActivityHTML"
android:description="@string/app_name"
android:exported="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:launchMode="singleInstance"
android:resizeableActivity="true" />
<activity
android:name=".ActivityDmarc"
android:description="@string/app_name"

View File

@ -0,0 +1,190 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2023 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.Group;
import androidx.preference.PreferenceManager;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.w3c.dom.css.CSSStyleSheet;
import java.io.File;
import java.util.List;
public class ActivityHTML extends ActivityBase {
private TextView tvText;
private ContentLoadingProgressBar pbWait;
private Group grpReady;
private boolean sanitize = BuildConfig.DEBUG;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null)
sanitize = savedInstanceState.getBoolean("fair:sanitize");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
View view = LayoutInflater.from(this).inflate(R.layout.activity_text, null);
setContentView(view);
tvText = findViewById(R.id.tvText);
pbWait = findViewById(R.id.pbWait);
grpReady = findViewById(R.id.grpReady);
// Initialize
grpReady.setVisibility(View.GONE);
load();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
load();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean("fair:sanitize", sanitize);
super.onSaveInstanceState(outState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_html, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean debug = prefs.getBoolean("debug", false);
menu.findItem(R.id.menu_sanitize)
.setVisible(BuildConfig.DEBUG || debug)
.setChecked(sanitize);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == android.R.id.home) {
finish();
return true;
} else if (itemId == R.id.menu_sanitize) {
sanitize = !sanitize;
item.setChecked(sanitize);
load();
return true;
}
return super.onOptionsItemSelected(item);
}
private void load() {
Intent intent = getIntent();
long id = intent.getLongExtra("id", -1L);
Log.i("Text id=" + id + " sanitize=" + sanitize);
Bundle args = new Bundle();
args.putLong("id", id);
args.putBoolean("sanitize", sanitize);
new SimpleTask<String>() {
@Override
protected void onPreExecute(Bundle args) {
pbWait.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(Bundle args) {
pbWait.setVisibility(View.GONE);
}
@Override
protected String onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
boolean sanitize = args.getBoolean("sanitize");
DB db = DB.getInstance(context);
EntityMessage message = db.message().getMessage(id);
if (message == null)
return null;
args.putString("subject", message.subject);
File file = message.getFile(context);
if (sanitize) {
Document d = JsoupEx.parse(file);
List<CSSStyleSheet> sheets =
HtmlHelper.parseStyles(d.head().select("style"));
for (Element element : d.select("*")) {
String computed = HtmlHelper.processStyles(context,
element.tagName(),
element.className(),
element.attr("style"),
sheets);
if (!TextUtils.isEmpty(computed))
element.attr("x-computed", computed);
}
d = HtmlHelper.sanitizeView(context, d, false);
d.outputSettings().prettyPrint(true).outline(true).indentAmount(1);
return d.html();
} else
return Helper.readText(file);
}
@Override
protected void onExecuted(Bundle args, String text) {
getSupportActionBar().setSubtitle(args.getString("subject"));
tvText.setText(text);
grpReady.setVisibility(View.VISIBLE);
}
@Override
protected void onException(Bundle args, @NonNull Throwable ex) {
Log.unexpectedError(getSupportFragmentManager(), ex, false);
}
}.execute(this, args, "view:text");
}
}

View File

@ -149,7 +149,6 @@ import com.google.android.material.snackbar.Snackbar;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.w3c.dom.css.CSSStyleSheet;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
@ -6108,7 +6107,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
popupMenu.getMenu().findItem(R.id.menu_show_headers).setEnabled(message.uid != null ||
(message.accountProtocol == EntityAccount.TYPE_POP && message.headers != null));
popupMenu.getMenu().findItem(R.id.menu_share_as_html).setVisible(message.content &&
popupMenu.getMenu().findItem(R.id.menu_show_html).setVisible(message.content &&
(BuildConfig.DEBUG || !BuildConfig.PLAY_STORE_RELEASE));
boolean canRaw = (message.uid != null ||
@ -6227,7 +6226,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
} else if (itemId == R.id.menu_show_headers) {
onMenuShowHeaders(message);
return true;
} else if (itemId == R.id.menu_share_as_html) {
} else if (itemId == R.id.menu_show_html) {
onMenuShareHtml(message);
return true;
} else if (itemId == R.id.menu_raw_save) {
@ -7408,67 +7407,8 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
}
private void onMenuShareHtml(TupleMessageEx message) {
Bundle args = new Bundle();
args.putLong("id", message.id);
new SimpleTask<File>() {
@Override
protected File onExecute(Context context, Bundle args) throws IOException {
Long id = args.getLong("id");
DB db = DB.getInstance(context);
EntityMessage message = db.message().getMessage(id);
if (message == null || !message.content)
return null;
File file = message.getFile(context);
Document d = JsoupEx.parse(file);
if (BuildConfig.DEBUG) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean overview_mode = prefs.getBoolean("overview_mode", false);
HtmlHelper.setViewport(d, overview_mode);
}
d.head().prependElement("meta").attr("charset", "utf-8");
if (message.language != null)
d.body().attr("lang", message.language);
List<CSSStyleSheet> sheets =
HtmlHelper.parseStyles(d.head().select("style"));
for (Element element : d.select("*")) {
String computed = HtmlHelper.processStyles(context,
element.tagName(),
element.className(),
element.attr("style"),
sheets);
if (!TextUtils.isEmpty(computed))
element.attr("x-computed", computed);
}
if (BuildConfig.DEBUG) {
d = HtmlHelper.sanitizeView(context, d, false);
d.outputSettings().prettyPrint(true).outline(true).indentAmount(1);
}
File dir = Helper.ensureExists(new File(context.getFilesDir(), "shared"));
File share = new File(dir, message.id + ".txt");
Helper.writeText(share, d.html());
return share;
}
@Override
protected void onExecuted(Bundle args, File share) {
Helper.share(context, share, "text/plain", share.getName());
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
}.execute(context, owner, args, "message:headers");
context.startActivity(new Intent(context, ActivityHTML.class)
.putExtra("id", message.id));
}
private void onMenuRawSave(TupleMessageEx message) {

View File

@ -0,0 +1,43 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="eu.faircode.email.ActivityHTML">
<ScrollView
android:layout_width="0dp"
android:layout_height="0dp"
android:fillViewport="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/tvText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="monospace"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textColor="?android:attr/textColorPrimary"
android:textIsSelectable="true" />
</ScrollView>
<eu.faircode.email.ContentLoadingProgressBar
android:id="@+id/pbWait"
style="@style/Base.Widget.AppCompat.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.Group
android:id="@+id/grpReady"
android:layout_width="0dp"
android:layout_height="0dp"
app:constraint_referenced_ids="wvAmp" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_sanitize"
android:checkable="true"
android:title="@string/title_sanitize"
app:showAsAction="never" />
</menu>

View File

@ -138,9 +138,9 @@
android:title="@string/title_show_headers" />
<item
android:id="@+id/menu_share_as_html"
android:id="@+id/menu_show_html"
android:icon="@drawable/twotone_source_24"
android:title="@string/title_share_as_html" />
android:title="@string/title_show_html" />
<item
android:id="@+id/menu_raw_save"

View File

@ -1434,7 +1434,7 @@
<string name="title_print_images_remark">Downloading images might take some time</string>
<string name="title_print_block_quotes">Print block quotes lines</string>
<string name="title_show_headers">Show headers</string>
<string name="title_share_as_html">Share as HTML</string>
<string name="title_show_html">Show HTML</string>
<string name="title_raw_save">Save raw message</string>
<string name="title_raw_send">Send as attachment</string>
<string name="title_raw_send_message">Message</string>
@ -2344,6 +2344,7 @@
<string name="title_widget_day_night">Follow dark system theme</string>
<string name="title_compat_dark">Google removed dark mode for Android versions before version 10</string>
<string name="title_sanitize" translatable="false">Sanitize</string>
<!-- Thunderbird -->
<string name="title_keyword_label1">Important</string>

View File

@ -474,6 +474,15 @@
</intent-filter>
</activity>
<activity
android:name=".ActivityHTML"
android:description="@string/app_name"
android:exported="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:launchMode="singleInstance"
android:resizeableActivity="true" />
<activity
android:name=".ActivityDmarc"
android:enabled="false"