2022-02-13 12:03:28 +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-2022 by Marcel Bokhorst (M66B)
|
|
|
|
*/
|
|
|
|
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.net.Uri;
|
2022-07-03 06:56:06 +00:00
|
|
|
import android.os.Build;
|
2022-02-13 12:03:28 +00:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.view.LayoutInflater;
|
2022-02-13 20:51:18 +00:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
2022-02-13 12:03:28 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.webkit.WebSettings;
|
|
|
|
import android.webkit.WebView;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.constraintlayout.widget.Group;
|
|
|
|
import androidx.preference.PreferenceManager;
|
2022-02-13 20:51:18 +00:00
|
|
|
import androidx.webkit.WebSettingsCompat;
|
|
|
|
import androidx.webkit.WebViewFeature;
|
2022-02-13 12:03:28 +00:00
|
|
|
|
2022-02-13 12:49:18 +00:00
|
|
|
import org.jsoup.nodes.Document;
|
|
|
|
import org.jsoup.nodes.Element;
|
|
|
|
|
2022-03-27 11:43:45 +00:00
|
|
|
import java.io.FileNotFoundException;
|
2022-02-13 12:03:28 +00:00
|
|
|
import java.io.InputStream;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
2022-02-13 13:49:50 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
2022-02-13 12:03:28 +00:00
|
|
|
|
|
|
|
public class ActivityAMP extends ActivityBase {
|
|
|
|
private WebView wvAmp;
|
|
|
|
private ContentLoadingProgressBar pbWait;
|
|
|
|
private Group grpReady;
|
|
|
|
|
2022-02-13 20:51:18 +00:00
|
|
|
private boolean force_light = false;
|
|
|
|
|
2022-02-13 13:49:50 +00:00
|
|
|
private static final List<String> ALLOWED_SCRIPT_HOSTS = Collections.unmodifiableList(Arrays.asList(
|
|
|
|
"cdn.ampproject.org"
|
|
|
|
));
|
|
|
|
|
2022-02-13 12:03:28 +00:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
2022-02-13 20:51:18 +00:00
|
|
|
if (savedInstanceState != null)
|
|
|
|
force_light = savedInstanceState.getBoolean("fair:force_light");
|
|
|
|
|
2022-02-13 13:59:37 +00:00
|
|
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
2022-02-13 12:03:28 +00:00
|
|
|
|
|
|
|
View view = LayoutInflater.from(this).inflate(R.layout.activity_amp, null);
|
|
|
|
setContentView(view);
|
|
|
|
|
|
|
|
wvAmp = findViewById(R.id.wvAmp);
|
|
|
|
pbWait = findViewById(R.id.pbWait);
|
|
|
|
grpReady = findViewById(R.id.grpReady);
|
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
2022-02-15 15:29:15 +00:00
|
|
|
boolean overview_mode = prefs.getBoolean("overview_mode", false);
|
2022-02-13 12:03:28 +00:00
|
|
|
boolean safe_browsing = prefs.getBoolean("safe_browsing", false);
|
|
|
|
|
|
|
|
WebSettings settings = wvAmp.getSettings();
|
|
|
|
settings.setUserAgentString(WebViewEx.getUserAgent(this, wvAmp));
|
|
|
|
settings.setUseWideViewPort(true);
|
2022-02-15 15:29:15 +00:00
|
|
|
settings.setLoadWithOverviewMode(overview_mode);
|
2022-02-13 12:03:28 +00:00
|
|
|
|
|
|
|
settings.setBuiltInZoomControls(true);
|
|
|
|
settings.setDisplayZoomControls(false);
|
|
|
|
|
|
|
|
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);
|
|
|
|
|
|
|
|
settings.setAllowFileAccess(false);
|
|
|
|
settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
|
|
|
|
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
|
|
|
|
|
2022-06-15 16:10:01 +00:00
|
|
|
if (WebViewEx.isFeatureSupported(this, WebViewFeature.SAFE_BROWSING_ENABLE))
|
2022-02-13 20:55:55 +00:00
|
|
|
WebSettingsCompat.setSafeBrowsingEnabled(settings, safe_browsing);
|
2022-02-13 12:03:28 +00:00
|
|
|
|
2022-02-13 20:51:18 +00:00
|
|
|
setDarkMode();
|
|
|
|
|
2022-02-13 12:03:28 +00:00
|
|
|
settings.setLoadsImagesAutomatically(true);
|
|
|
|
settings.setBlockNetworkLoads(false);
|
|
|
|
settings.setBlockNetworkImage(false);
|
|
|
|
settings.setJavaScriptEnabled(true);
|
|
|
|
|
|
|
|
// Initialize
|
|
|
|
grpReady.setVisibility(View.GONE);
|
|
|
|
|
|
|
|
load();
|
|
|
|
}
|
|
|
|
|
2022-02-13 20:51:18 +00:00
|
|
|
@Override
|
|
|
|
protected void onSaveInstanceState(Bundle outState) {
|
|
|
|
outState.putBoolean("fair:force_light", force_light);
|
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
MenuInflater inflater = getMenuInflater();
|
|
|
|
inflater.inflate(R.menu.menu_amp, menu);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onPrepareOptionsMenu(Menu menu) {
|
2022-06-11 11:14:28 +00:00
|
|
|
boolean dark = Helper.isDarkTheme(this);
|
2022-06-15 16:10:01 +00:00
|
|
|
boolean canDarken = WebViewEx.isFeatureSupported(this, WebViewFeature.ALGORITHMIC_DARKENING);
|
2022-02-13 20:51:18 +00:00
|
|
|
menu.findItem(R.id.menu_force_light)
|
2022-06-11 11:14:28 +00:00
|
|
|
.setVisible(dark && canDarken)
|
2022-02-13 20:51:18 +00:00
|
|
|
.getIcon().setLevel(force_light ? 1 : 0);
|
|
|
|
return super.onPrepareOptionsMenu(menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
|
|
|
|
int itemId = item.getItemId();
|
|
|
|
if (itemId == R.id.menu_force_light) {
|
|
|
|
onMenuForceLight();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onMenuForceLight() {
|
|
|
|
force_light = !force_light;
|
|
|
|
invalidateOptionsMenu();
|
|
|
|
setDarkMode();
|
|
|
|
}
|
|
|
|
|
2022-02-13 12:03:28 +00:00
|
|
|
@Override
|
|
|
|
protected void onNewIntent(Intent intent) {
|
|
|
|
super.onNewIntent(intent);
|
|
|
|
setIntent(intent);
|
|
|
|
load();
|
|
|
|
}
|
|
|
|
|
2022-02-13 20:51:18 +00:00
|
|
|
private void setDarkMode() {
|
|
|
|
WebSettings settings = wvAmp.getSettings();
|
|
|
|
boolean dark = (Helper.isDarkTheme(this) && !force_light);
|
2022-06-15 16:10:01 +00:00
|
|
|
boolean canDarken = WebViewEx.isFeatureSupported(this, WebViewFeature.ALGORITHMIC_DARKENING);
|
2022-07-03 06:56:06 +00:00
|
|
|
if (canDarken && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
|
2022-06-11 11:14:28 +00:00
|
|
|
WebSettingsCompat.setAlgorithmicDarkeningAllowed(settings, dark);
|
2022-02-13 20:51:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-13 12:03:28 +00:00
|
|
|
private void load() {
|
2022-03-07 16:38:29 +00:00
|
|
|
Intent intent = getIntent();
|
|
|
|
Uri uri = intent.getData();
|
|
|
|
long id = intent.getLongExtra("id", -1L);
|
|
|
|
Log.i("AMP uri=" + uri + " id=" + id);
|
2022-02-13 21:06:09 +00:00
|
|
|
|
2022-02-13 12:03:28 +00:00
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putParcelable("uri", uri);
|
2022-03-07 16:38:29 +00:00
|
|
|
args.putLong("id", id);
|
2022-02-13 12:03:28 +00:00
|
|
|
|
|
|
|
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 {
|
|
|
|
Uri uri = args.getParcelable("uri");
|
2022-03-07 16:38:29 +00:00
|
|
|
long id = args.getLong("id");
|
2022-02-13 12:03:28 +00:00
|
|
|
|
2022-03-18 16:22:25 +00:00
|
|
|
NoStreamException.check(uri, context);
|
2022-02-13 12:03:28 +00:00
|
|
|
|
2022-03-07 16:38:29 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
EntityMessage message = db.message().getMessage(id);
|
|
|
|
|
|
|
|
args.putString("subject",
|
|
|
|
message == null || message.subject == null ? "AMP" : message.subject);
|
|
|
|
|
2022-02-15 17:46:54 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
boolean overview_mode = prefs.getBoolean("overview_mode", false);
|
|
|
|
|
2022-02-13 12:49:18 +00:00
|
|
|
String html;
|
2022-02-13 12:03:28 +00:00
|
|
|
ContentResolver resolver = context.getContentResolver();
|
|
|
|
try (InputStream is = resolver.openInputStream(uri)) {
|
2022-03-27 11:43:45 +00:00
|
|
|
if (is == null)
|
|
|
|
throw new FileNotFoundException(uri.toString());
|
2022-02-13 12:49:18 +00:00
|
|
|
html = Helper.readStream(is);
|
|
|
|
}
|
|
|
|
|
|
|
|
Document d = JsoupEx.parse(html);
|
2022-02-15 17:46:54 +00:00
|
|
|
HtmlHelper.setViewport(d, overview_mode);
|
2022-03-07 16:38:29 +00:00
|
|
|
if (message != null)
|
|
|
|
HtmlHelper.embedInlineImages(context, message.id, d, true);
|
2022-02-13 13:29:00 +00:00
|
|
|
|
2022-02-13 12:49:18 +00:00
|
|
|
for (Element script : d.select("script")) {
|
|
|
|
String src = script.attr("src");
|
|
|
|
Uri u = Uri.parse(src);
|
2022-02-13 13:49:50 +00:00
|
|
|
String host = (u.isHierarchical() ? u.getHost() : null);
|
|
|
|
if (host == null || !ALLOWED_SCRIPT_HOSTS.contains(host.toLowerCase(Locale.ROOT)))
|
2022-02-13 12:49:18 +00:00
|
|
|
script.removeAttr("src");
|
2022-02-13 12:03:28 +00:00
|
|
|
}
|
2022-02-13 12:49:18 +00:00
|
|
|
|
|
|
|
return d.html();
|
2022-02-13 12:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onExecuted(Bundle args, String amp) {
|
2022-03-07 16:38:29 +00:00
|
|
|
getSupportActionBar().setSubtitle(args.getString("subject"));
|
|
|
|
|
2022-02-13 12:03:28 +00:00
|
|
|
wvAmp.loadDataWithBaseURL(null, amp, "text/html", StandardCharsets.UTF_8.name(), null);
|
|
|
|
grpReady.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onException(Bundle args, @NonNull Throwable ex) {
|
2022-03-18 16:22:25 +00:00
|
|
|
if (ex instanceof NoStreamException)
|
|
|
|
((NoStreamException) ex).report(ActivityAMP.this);
|
2022-02-13 12:03:28 +00:00
|
|
|
else
|
|
|
|
Log.unexpectedError(getSupportFragmentManager(), ex, false);
|
|
|
|
}
|
|
|
|
}.execute(this, args, "amp:decode");
|
|
|
|
}
|
|
|
|
}
|