From 58adbf64c37e94d0c4f56dd38c2c668d69d62eb7 Mon Sep 17 00:00:00 2001 From: M66B Date: Fri, 3 Aug 2018 10:32:17 +0000 Subject: [PATCH] Added inline viewing of images and links --- app/src/main/AndroidManifest.xml | 8 ++ .../java/eu/faircode/email/ActivityView.java | 1 + .../eu/faircode/email/FragmentMessage.java | 39 +++++++++- .../eu/faircode/email/FragmentWebView.java | 75 +++++++++++++++++++ app/src/main/res/layout/fragment_webview.xml | 25 +++++++ 5 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/eu/faircode/email/FragmentWebView.java create mode 100644 app/src/main/res/layout/fragment_webview.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 77047117..91b6b0c5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -17,6 +17,14 @@ android:supportsRtl="true" android:theme="@style/AppThemeLight"> + + + + diff --git a/app/src/main/java/eu/faircode/email/ActivityView.java b/app/src/main/java/eu/faircode/email/ActivityView.java index 38bc9137..fce8ef82 100644 --- a/app/src/main/java/eu/faircode/email/ActivityView.java +++ b/app/src/main/java/eu/faircode/email/ActivityView.java @@ -77,6 +77,7 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + setContentView(R.layout.activity_view); getSupportActionBar().setDisplayHomeAsUpEnabled(true); diff --git a/app/src/main/java/eu/faircode/email/FragmentMessage.java b/app/src/main/java/eu/faircode/email/FragmentMessage.java index 49ce6951..215b8995 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessage.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessage.java @@ -34,12 +34,16 @@ import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.text.Html; +import android.text.Layout; +import android.text.Spannable; import android.text.method.LinkMovementMethod; +import android.text.style.URLSpan; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; +import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.ProgressBar; @@ -98,7 +102,40 @@ public class FragmentMessage extends Fragment { grpReady = view.findViewById(R.id.grpReady); setHasOptionsMenu(true); - tvBody.setMovementMethod(LinkMovementMethod.getInstance()); + tvBody.setMovementMethod(new LinkMovementMethod() { + + public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) { + if (event.getAction() != MotionEvent.ACTION_UP) + return super.onTouchEvent(widget, buffer, event); + + int x = (int) event.getX(); + int y = (int) event.getY(); + + x -= widget.getTotalPaddingLeft(); + y -= widget.getTotalPaddingTop(); + + x += widget.getScrollX(); + y += widget.getScrollY(); + + Layout layout = widget.getLayout(); + int line = layout.getLineForVertical(y); + int off = layout.getOffsetForHorizontal(line, x); + + URLSpan[] link = buffer.getSpans(off, off, URLSpan.class); + if (link.length != 0) { + Bundle args = new Bundle(); + args.putString("link", link[0].getURL()); + + FragmentWebView fragment = new FragmentWebView(); + fragment.setArguments(args); + + FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); + fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("link"); + fragmentTransaction.commit(); + } + return true; + } + }); // Wire controls diff --git a/app/src/main/java/eu/faircode/email/FragmentWebView.java b/app/src/main/java/eu/faircode/email/FragmentWebView.java new file mode 100644 index 00000000..9d8dc403 --- /dev/null +++ b/app/src/main/java/eu/faircode/email/FragmentWebView.java @@ -0,0 +1,75 @@ +package eu.faircode.email; + +/* + This file is part of Safe email. + + Safe email 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. + + NetGuard 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 NetGuard. If not, see . + + Copyright 2018 by Marcel Bokhorst (M66B) +*/ + +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.v4.app.Fragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.webkit.WebChromeClient; +import android.webkit.WebSettings; +import android.webkit.WebView; +import android.webkit.WebViewClient; +import android.widget.ProgressBar; + +// https://developer.android.com/reference/android/webkit/WebView + +public class FragmentWebView extends Fragment { + + @Override + @Nullable + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.fragment_webview, container, false); + + final ProgressBar progressBar = view.findViewById(R.id.progressbar); + WebView webview = view.findViewById(R.id.webview); + + progressBar.setProgress(0); + progressBar.setVisibility(View.VISIBLE); + + WebSettings settings = webview.getSettings(); + settings.setJavaScriptEnabled(true); + settings.setLoadWithOverviewMode(true); + settings.setUseWideViewPort(true); + //settings.setBuiltInZoomControls(true); + + webview.setWebViewClient(new WebViewClient() { + public boolean shouldOverrideUrlLoading(WebView view, String url) { + view.loadUrl(url); + return false; + } + }); + + webview.setWebChromeClient(new WebChromeClient() { + public void onProgressChanged(WebView view, int progress) { + progressBar.setProgress(progress); + if (progress == 100) + progressBar.setVisibility(View.GONE); + } + }); + + webview.loadUrl(getArguments().getString("link")); + + return view; + } +} diff --git a/app/src/main/res/layout/fragment_webview.xml b/app/src/main/res/layout/fragment_webview.xml new file mode 100644 index 00000000..25a1cc48 --- /dev/null +++ b/app/src/main/res/layout/fragment_webview.xml @@ -0,0 +1,25 @@ + + + + + + + \ No newline at end of file