FairEmail/app/src/main/java/eu/faircode/email/ActivityCode.java

237 lines
8.1 KiB
Java
Raw Normal View History

2023-11-02 20:25:35 +00:00
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;
2023-11-03 08:40:36 +00:00
import android.text.Html;
2023-11-02 20:25:35 +00:00
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
2023-11-03 08:40:36 +00:00
import android.webkit.WebSettings;
import android.webkit.WebView;
2023-11-02 20:25:35 +00:00
2023-11-02 21:26:39 +00:00
import androidx.activity.OnBackPressedCallback;
2023-11-02 20:25:35 +00:00
import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.Group;
import androidx.preference.PreferenceManager;
2023-11-03 10:55:54 +00:00
import androidx.webkit.WebSettingsCompat;
import androidx.webkit.WebViewFeature;
2023-11-02 20:25:35 +00:00
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.w3c.dom.css.CSSStyleSheet;
import java.io.File;
2023-11-03 08:40:36 +00:00
import java.nio.charset.StandardCharsets;
2023-11-02 20:25:35 +00:00
import java.util.List;
2023-11-03 08:40:36 +00:00
public class ActivityCode extends ActivityBase {
private WebView wvCode;
2023-11-02 20:25:35 +00:00
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);
2023-11-02 21:26:39 +00:00
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
finishAndRemoveTask();
}
});
2023-11-03 08:40:36 +00:00
View view = LayoutInflater.from(this).inflate(R.layout.activity_code, null);
2023-11-02 20:25:35 +00:00
setContentView(view);
2023-11-03 08:40:36 +00:00
wvCode = findViewById(R.id.wvCode);
2023-11-02 20:25:35 +00:00
pbWait = findViewById(R.id.pbWait);
grpReady = findViewById(R.id.grpReady);
2023-11-03 08:40:36 +00:00
WebSettings settings = wvCode.getSettings();
2023-11-03 10:55:54 +00:00
settings.setBuiltInZoomControls(true);
settings.setDisplayZoomControls(false);
settings.setAllowFileAccess(false);
settings.setCacheMode(WebSettings.LOAD_CACHE_ONLY);
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_NEVER_ALLOW);
settings.setLoadsImagesAutomatically(false);
settings.setBlockNetworkLoads(true);
settings.setBlockNetworkImage(true);
2023-11-03 08:40:36 +00:00
settings.setJavaScriptEnabled(true);
2023-11-02 20:25:35 +00:00
// 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();
2023-11-03 08:40:36 +00:00
inflater.inflate(R.menu.menu_code, menu);
2023-11-02 20:25:35 +00:00
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)
2023-11-03 10:18:34 +00:00
.setChecked(sanitize)
.setIcon(sanitize
? R.drawable.twotone_fullscreen_24
: R.drawable.twotone_fullscreen_exit_24)
.setTitle(getString(sanitize
? R.string.title_legend_show_full
: R.string.title_legend_show_reformatted));
2023-11-02 20:25:35 +00:00
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == android.R.id.home) {
2023-11-02 21:26:39 +00:00
finishAndRemoveTask();
2023-11-02 20:25:35 +00:00
return true;
} else if (itemId == R.id.menu_sanitize) {
sanitize = !sanitize;
2023-11-03 10:18:34 +00:00
invalidateOptionsMenu();
2023-11-02 20:25:35 +00:00
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);
2023-11-03 08:40:36 +00:00
Document d = JsoupEx.parse(file);
2023-11-02 20:25:35 +00:00
2023-11-03 08:40:36 +00:00
if (sanitize) {
2023-11-02 20:25:35 +00:00
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);
2023-11-03 08:40:36 +00:00
}
2023-11-02 20:25:35 +00:00
2023-11-03 08:40:36 +00:00
return d.html();
2023-11-02 20:25:35 +00:00
}
@Override
2023-11-03 08:40:36 +00:00
protected void onExecuted(Bundle args, String code) {
2023-11-02 20:25:35 +00:00
getSupportActionBar().setSubtitle(args.getString("subject"));
2023-11-03 08:40:36 +00:00
String html = "<!DOCTYPE html>" +
"<html>" +
"<head>" +
"<link href=\"file:///android_asset/prism.css\" rel=\"stylesheet\" />" +
"<style>" +
" code[class=\"language-html\"] { font-size: smaller !important; }" +
"</style>" +
"</head>" +
"<body>" +
"<script src=\"file:///android_asset/prism.js\"></script>" +
"<code class=\"language-html\">" + Html.escapeHtml(code) + "</code>" +
"</body>" +
"</html>";
wvCode.loadDataWithBaseURL("file:///android_asset/", html, "text/html", StandardCharsets.UTF_8.name(), null);
2023-11-02 20:25:35 +00:00
grpReady.setVisibility(View.VISIBLE);
}
@Override
protected void onException(Bundle args, @NonNull Throwable ex) {
Log.unexpectedError(getSupportFragmentManager(), ex, false);
}
}.execute(this, args, "view:text");
}
}