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

249 lines
8.4 KiB
Java
Raw Normal View History

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)
*/
2022-02-13 20:51:18 +00:00
import static androidx.webkit.WebSettingsCompat.FORCE_DARK_OFF;
import static androidx.webkit.WebSettingsCompat.FORCE_DARK_ON;
2022-02-13 12:03:28 +00:00
import android.Manifest;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
2022-02-13 21:06:09 +00:00
import android.text.TextUtils;
2022-02-13 12:03:28 +00:00
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
import com.google.android.material.snackbar.Snackbar;
2022-02-13 12:49:18 +00:00
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
2022-02-13 12:03:28 +00:00
import java.io.FileNotFoundException;
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
getSupportActionBar().setSubtitle("AMP");
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);
boolean safe_browsing = prefs.getBoolean("safe_browsing", false);
WebSettings settings = wvAmp.getSettings();
settings.setUserAgentString(WebViewEx.getUserAgent(this, wvAmp));
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true);
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-02-13 20:55:55 +00:00
if (WebViewEx.isFeatureSupported(WebViewFeature.SAFE_BROWSING_ENABLE))
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) {
boolean available =
(WebViewEx.isFeatureSupported(WebViewFeature.FORCE_DARK) &&
Helper.isDarkTheme(this));
menu.findItem(R.id.menu_force_light)
.setVisible(available)
.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);
if (WebViewEx.isFeatureSupported(WebViewFeature.FORCE_DARK))
WebSettingsCompat.setForceDark(settings, dark ? FORCE_DARK_ON : FORCE_DARK_OFF);
}
2022-02-13 12:03:28 +00:00
private void load() {
Uri uri = getIntent().getData();
Log.i("AMP uri=" + uri);
2022-02-13 21:06:09 +00:00
String subject = getIntent().getStringExtra("subject");
if (TextUtils.isEmpty(subject))
subject = "AMP";
getSupportActionBar().setSubtitle(subject);
2022-02-13 12:03:28 +00:00
Bundle args = new Bundle();
args.putParcelable("uri", uri);
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");
if (uri == null)
throw new FileNotFoundException();
if (!"content".equals(uri.getScheme()) &&
!Helper.hasPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE)) {
Log.w("AMP uri=" + uri);
throw new IllegalArgumentException(context.getString(R.string.title_no_stream));
}
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-02-13 12:49:18 +00:00
html = Helper.readStream(is);
}
Document d = JsoupEx.parse(html);
2022-02-13 13:29:00 +00:00
HtmlHelper.setViewport(d, false);
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) {
wvAmp.loadDataWithBaseURL(null, amp, "text/html", StandardCharsets.UTF_8.name(), null);
grpReady.setVisibility(View.VISIBLE);
}
@Override
protected void onException(Bundle args, @NonNull Throwable ex) {
if (ex instanceof IllegalArgumentException)
Snackbar.make(findViewById(android.R.id.content), ex.getMessage(), Snackbar.LENGTH_LONG)
.setGestureInsetBottomIgnored(true).show();
else
Log.unexpectedError(getSupportFragmentManager(), ex, false);
}
}.execute(this, args, "amp:decode");
}
}