From 3f59ddfd461f2a9034319fed79180c618a924856 Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 13 Feb 2022 13:03:28 +0100 Subject: [PATCH] Added AMP viewer --- app/src/amazon/AndroidManifest.xml | 18 ++ app/src/fdroid/AndroidManifest.xml | 18 ++ app/src/github/AndroidManifest.xml | 18 ++ app/src/main/AndroidManifest.xml | 18 ++ .../java/eu/faircode/email/ActivityAMP.java | 155 ++++++++++++++++++ .../eu/faircode/email/AdapterAttachment.java | 64 +------- .../email/FragmentDialogOpenFull.java | 3 - app/src/main/res/layout/activity_amp.xml | 29 ++++ app/src/main/res/values/strings.xml | 1 - app/src/play/AndroidManifest.xml | 18 ++ 10 files changed, 276 insertions(+), 66 deletions(-) create mode 100644 app/src/main/java/eu/faircode/email/ActivityAMP.java create mode 100644 app/src/main/res/layout/activity_amp.xml diff --git a/app/src/amazon/AndroidManifest.xml b/app/src/amazon/AndroidManifest.xml index 25f966c814..6f39320274 100644 --- a/app/src/amazon/AndroidManifest.xml +++ b/app/src/amazon/AndroidManifest.xml @@ -352,6 +352,24 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + . + + Copyright 2018-2022 by Marcel Bokhorst (M66B) +*/ + +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; +import android.view.LayoutInflater; +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; + +import com.google.android.material.snackbar.Snackbar; + +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; + +public class ActivityAMP extends ActivityBase { + private WebView wvAmp; + private ContentLoadingProgressBar pbWait; + private Group grpReady; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + getSupportActionBar().setIcon(R.drawable.twotone_bolt_24); + 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); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) + settings.setSafeBrowsingEnabled(safe_browsing); + + settings.setLoadsImagesAutomatically(true); + settings.setBlockNetworkLoads(false); + settings.setBlockNetworkImage(false); + settings.setJavaScriptEnabled(true); + + // Initialize + grpReady.setVisibility(View.GONE); + + load(); + } + + @Override + protected void onNewIntent(Intent intent) { + super.onNewIntent(intent); + setIntent(intent); + load(); + } + + private void load() { + Uri uri = getIntent().getData(); + Log.i("AMP uri=" + uri); + + Bundle args = new Bundle(); + args.putParcelable("uri", uri); + + new SimpleTask() { + @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)); + } + + ContentResolver resolver = context.getContentResolver(); + try (InputStream is = resolver.openInputStream(uri)) { + return Helper.readStream(is); + } + } + + @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"); + } +} diff --git a/app/src/main/java/eu/faircode/email/AdapterAttachment.java b/app/src/main/java/eu/faircode/email/AdapterAttachment.java index 779ba01df5..ad286b5889 100644 --- a/app/src/main/java/eu/faircode/email/AdapterAttachment.java +++ b/app/src/main/java/eu/faircode/email/AdapterAttachment.java @@ -20,7 +20,6 @@ package eu.faircode.email; */ import android.content.Context; -import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Bitmap; @@ -35,7 +34,6 @@ import android.widget.ProgressBar; import android.widget.TextView; import androidx.annotation.NonNull; -import androidx.appcompat.app.AlertDialog; import androidx.core.content.FileProvider; import androidx.fragment.app.Fragment; import androidx.lifecycle.Lifecycle; @@ -295,66 +293,8 @@ public class AdapterAttachment extends RecyclerView.Adapter() { - @Override - protected String onExecute(Context context, Bundle args) throws Throwable { - long id = args.getLong("id"); - - DB db = DB.getInstance(context); - EntityAttachment attachment = db.attachment().getAttachment(id); - if (attachment == null) - return null; - - File file = attachment.getFile(context); - return Helper.readText(file); - } - - @Override - protected void onExecuted(Bundle args, String html) { - if (html == null) - return; - - Bundle hargs = new Bundle(); - hargs.putString("html", html); - hargs.putBoolean("overview_mode", true); - hargs.putBoolean("safe_browsing", true); - hargs.putBoolean("force_light", true); - hargs.putBoolean("javascript", true); - - FragmentDialogOpenFull dialog = new FragmentDialogOpenFull(); - dialog.setArguments(hargs); - dialog.show(parentFragment.getParentFragmentManager(), "amp"); - } - - @Override - protected void onException(Bundle args, Throwable ex) { - Log.unexpectedError(parentFragment.getParentFragmentManager(), ex); - } - }.execute(context, owner, args, "attachment:amp"); + String title = (attachment.name == null ? attachment.cid : attachment.name); + Helper.share(context, attachment.getFile(context), attachment.getMimeType(), title); } private void onDownload(EntityAttachment attachment) { diff --git a/app/src/main/java/eu/faircode/email/FragmentDialogOpenFull.java b/app/src/main/java/eu/faircode/email/FragmentDialogOpenFull.java index 6ea1a2dacd..9edeb2defb 100644 --- a/app/src/main/java/eu/faircode/email/FragmentDialogOpenFull.java +++ b/app/src/main/java/eu/faircode/email/FragmentDialogOpenFull.java @@ -62,7 +62,6 @@ public class FragmentDialogOpenFull extends FragmentDialogBase { boolean overview_mode = args.getBoolean("overview_mode"); boolean safe_browsing = args.getBoolean("safe_browsing"); boolean force_light = args.getBoolean("force_light"); - boolean javascript = args.getBoolean("javascript"); final Context context = getContext(); @@ -90,8 +89,6 @@ public class FragmentDialogOpenFull extends FragmentDialogBase { if (WebViewEx.isFeatureSupported(WebViewFeature.FORCE_DARK)) WebSettingsCompat.setForceDark(settings, dark ? FORCE_DARK_ON : FORCE_DARK_OFF); - settings.setJavaScriptEnabled(javascript); - settings.setLoadsImagesAutomatically(true); settings.setBlockNetworkLoads(false); settings.setBlockNetworkImage(false); diff --git a/app/src/main/res/layout/activity_amp.xml b/app/src/main/res/layout/activity_amp.xml new file mode 100644 index 0000000000..3b587cf336 --- /dev/null +++ b/app/src/main/res/layout/activity_amp.xml @@ -0,0 +1,29 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a5085eee88..f2d0ac6756 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1211,7 +1211,6 @@ Always show images on showing original messages Showing images can leak privacy sensitive information Images recognized as tracking images will not be shown - Show AMP variant of the message? Delete local messages? Messages will remain on the remote server. Help improve FairEmail Send error reports? diff --git a/app/src/play/AndroidManifest.xml b/app/src/play/AndroidManifest.xml index 582c727997..dc46ebc464 100644 --- a/app/src/play/AndroidManifest.xml +++ b/app/src/play/AndroidManifest.xml @@ -352,6 +352,24 @@ + + + + + + + + + + + + + +