mirror of https://github.com/M66B/FairEmail.git
WebView refactoring
This commit is contained in:
parent
be5393ea50
commit
ac32344ebb
|
@ -79,10 +79,7 @@ import android.view.ViewConfiguration;
|
|||
import android.view.ViewGroup;
|
||||
import android.view.ViewStub;
|
||||
import android.view.animation.AccelerateDecelerateInterpolator;
|
||||
import android.webkit.DownloadListener;
|
||||
import android.webkit.WebSettings;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
import android.widget.Button;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.CompoundButton;
|
||||
|
@ -1291,12 +1288,11 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
|
|||
}
|
||||
}
|
||||
|
||||
int dp60 = Helper.dp2pixels(context, 60);
|
||||
boolean show_full = properties.getValue("full", message.id);
|
||||
boolean show_images = properties.getValue("images", message.id);
|
||||
boolean show_quotes = (properties.getValue("quotes", message.id) || !collapse_quotes);
|
||||
float size = properties.getSize(message.id, show_full ? 0 : textSize);
|
||||
int height = properties.getHeight(message.id, dp60);
|
||||
int height = properties.getHeight(message.id, 0);
|
||||
Pair<Integer, Integer> position = properties.getPosition(message.id);
|
||||
Log.i("Bind size=" + size + " height=" + height);
|
||||
|
||||
|
@ -1306,30 +1302,11 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
|
|||
|
||||
if (show_full) {
|
||||
// Create web view
|
||||
WebView webView;
|
||||
WebViewEx webView;
|
||||
if (wvBody instanceof WebView)
|
||||
webView = (WebView) wvBody;
|
||||
webView = (WebViewEx) wvBody;
|
||||
else {
|
||||
webView = new WebView(context) {
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
if (height > dp60)
|
||||
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST));
|
||||
else
|
||||
super.onMeasure(widthMeasureSpec, heightMeasureSpec); // Unspecified
|
||||
|
||||
int mh = getMeasuredHeight();
|
||||
Log.i("Measured height=" + mh);
|
||||
if (mh == 0)
|
||||
setMeasuredDimension(getMeasuredWidth(), height);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSizeChanged(int w, int h, int ow, int oh) {
|
||||
super.onSizeChanged(w, h, ow, oh);
|
||||
Log.i("Size changed height=" + h);
|
||||
properties.setHeight(message.id, h);
|
||||
}
|
||||
};
|
||||
webView = new WebViewEx(context);
|
||||
|
||||
webView.setId(wvBody.getId());
|
||||
|
||||
|
@ -1340,115 +1317,58 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
|
|||
wvBody.getPaddingLeft(), wvBody.getPaddingTop(),
|
||||
wvBody.getPaddingRight(), wvBody.getPaddingBottom());
|
||||
|
||||
webView.setVerticalScrollBarEnabled(false);
|
||||
webView.setOnTouchListener(ViewHolder.this);
|
||||
|
||||
webView.setWebViewClient(new WebViewClient() {
|
||||
public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
||||
Log.i("Open url=" + url);
|
||||
|
||||
Uri uri = Uri.parse(url);
|
||||
if ("cid".equals(uri.getScheme()) || "data".equals(uri.getScheme()))
|
||||
return false;
|
||||
|
||||
Bundle args = new Bundle();
|
||||
args.putParcelable("uri", uri);
|
||||
args.putString("title", null);
|
||||
|
||||
FragmentDialogLink fragment = new FragmentDialogLink();
|
||||
fragment.setArguments(args);
|
||||
fragment.show(parentFragment.getFragmentManager(), "open:link");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onScaleChanged(WebView view, float oldScale, float newScale) {
|
||||
Log.i("Changed scale=" + newScale);
|
||||
properties.setSize(message.id, newScale);
|
||||
}
|
||||
});
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
|
||||
webView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
|
||||
@Override
|
||||
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
|
||||
properties.setPosition(message.id, new Pair<Integer, Integer>(scrollX, scrollY));
|
||||
}
|
||||
});
|
||||
|
||||
webView.setDownloadListener(new DownloadListener() {
|
||||
public void onDownloadStart(
|
||||
String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
|
||||
Log.i("Download url=" + url + " mime type=" + mimetype);
|
||||
|
||||
Uri uri = Uri.parse(url);
|
||||
if ("cid".equals(uri.getScheme()) || "data".equals(uri.getScheme()))
|
||||
return;
|
||||
|
||||
Helper.view(context, uri, true);
|
||||
}
|
||||
});
|
||||
|
||||
webView.setOnLongClickListener(new View.OnLongClickListener() {
|
||||
@Override
|
||||
public boolean onLongClick(View view) {
|
||||
WebView.HitTestResult result = ((WebView) view).getHitTestResult();
|
||||
if (result.getType() == WebView.HitTestResult.IMAGE_TYPE ||
|
||||
result.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
|
||||
Log.i("Long press url=" + result.getExtra());
|
||||
|
||||
Uri uri = Uri.parse(result.getExtra());
|
||||
if ("cid".equals(uri.getScheme()) || "data".equals(uri.getScheme()))
|
||||
return false;
|
||||
|
||||
Helper.view(context, uri, true);
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
wvBody = webView;
|
||||
}
|
||||
|
||||
WebSettings settings = webView.getSettings();
|
||||
settings.setUseWideViewPort(true);
|
||||
settings.setLoadWithOverviewMode(true);
|
||||
int dp60 = Helper.dp2pixels(context, 60);
|
||||
webView.setMinimumHeight(height == 0 ? dp60 : height);
|
||||
|
||||
settings.setBuiltInZoomControls(true);
|
||||
settings.setDisplayZoomControls(false);
|
||||
webView.init(
|
||||
height, size, position,
|
||||
textSize, monospaced,
|
||||
show_images, inline,
|
||||
new WebViewEx.IWebView() {
|
||||
@Override
|
||||
public void onSizeChanged(int w, int h, int ow, int oh) {
|
||||
properties.setHeight(message.id, h);
|
||||
}
|
||||
|
||||
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);
|
||||
@Override
|
||||
public void onScaleChanged(float newScale) {
|
||||
properties.setSize(message.id, newScale);
|
||||
}
|
||||
|
||||
if (textSize != 0) {
|
||||
int dp = Helper.pixels2dp(context, textSize);
|
||||
settings.setDefaultFontSize(Math.round(dp));
|
||||
settings.setDefaultFixedFontSize(Math.round(dp));
|
||||
}
|
||||
if (monospaced)
|
||||
settings.setStandardFontFamily("monospace");
|
||||
@Override
|
||||
public void onScrollChange(int scrollX, int scrollY) {
|
||||
properties.setPosition(message.id, new Pair<Integer, Integer>(scrollX, scrollY));
|
||||
}
|
||||
|
||||
settings.setAllowFileAccess(false);
|
||||
settings.setLoadsImagesAutomatically(show_images || inline);
|
||||
settings.setBlockNetworkLoads(!show_images);
|
||||
settings.setBlockNetworkImage(!show_images);
|
||||
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
|
||||
@Override
|
||||
public boolean onOpenLink(String url) {
|
||||
Uri uri = Uri.parse(url);
|
||||
if ("cid".equals(uri.getScheme()) || "data".equals(uri.getScheme()))
|
||||
return false;
|
||||
|
||||
webView.setInitialScale(size == 0 ? 1 : Math.round(size * 100));
|
||||
wvBody.setMinimumHeight(height);
|
||||
if (position != null) {
|
||||
wvBody.setScrollX(position.first);
|
||||
wvBody.setScrollY(position.second);
|
||||
}
|
||||
Bundle args = new Bundle();
|
||||
args.putParcelable("uri", uri);
|
||||
args.putString("title", null);
|
||||
|
||||
FragmentDialogLink fragment = new FragmentDialogLink();
|
||||
fragment.setArguments(args);
|
||||
fragment.show(parentFragment.getFragmentManager(), "open:link");
|
||||
|
||||
return true;
|
||||
}
|
||||
});
|
||||
webView.setOnTouchListener(ViewHolder.this);
|
||||
|
||||
tvBody.setVisibility(View.GONE);
|
||||
wvBody.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
tvBody.setMinHeight(height);
|
||||
|
||||
if (size != 0)
|
||||
tvBody.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
|
||||
tvBody.setMinHeight(height);
|
||||
|
||||
tvBody.setTextColor(contrast ? textColorPrimary : colorRead);
|
||||
tvBody.setTypeface(monospaced ? Typeface.MONOSPACE : Typeface.DEFAULT);
|
||||
|
@ -4064,8 +3984,6 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
|
|||
|
||||
@Override
|
||||
public void onViewDetachedFromWindow(@NonNull ViewHolder holder) {
|
||||
if (holder.wvBody instanceof WebView)
|
||||
((WebView) holder.wvBody).loadDataWithBaseURL(null, "", "text/html", "UTF-8", null);
|
||||
holder.cowner.stop();
|
||||
holder.powner.recreate();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,170 @@
|
|||
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-2019 by Marcel Bokhorst (M66B)
|
||||
*/
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.util.Pair;
|
||||
import android.view.View;
|
||||
import android.webkit.DownloadListener;
|
||||
import android.webkit.WebSettings;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
|
||||
public class WebViewEx extends WebView implements DownloadListener, View.OnLongClickListener {
|
||||
private int height;
|
||||
private IWebView intf;
|
||||
|
||||
public WebViewEx(Context context) {
|
||||
super(context);
|
||||
|
||||
setVerticalScrollBarEnabled(false);
|
||||
setHorizontalScrollBarEnabled(false);
|
||||
|
||||
setDownloadListener(this);
|
||||
setOnLongClickListener(this);
|
||||
|
||||
setWebViewClient(new WebViewClient() {
|
||||
public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
||||
Log.i("Open url=" + url);
|
||||
return intf.onOpenLink(url);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onScaleChanged(WebView view, float oldScale, float newScale) {
|
||||
Log.i("Changed scale=" + newScale);
|
||||
intf.onScaleChanged(newScale);
|
||||
}
|
||||
});
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
|
||||
setOnScrollChangeListener(new View.OnScrollChangeListener() {
|
||||
@Override
|
||||
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
|
||||
intf.onScrollChange(scrollX, scrollY);
|
||||
}
|
||||
});
|
||||
|
||||
WebSettings settings = getSettings();
|
||||
settings.setUseWideViewPort(true);
|
||||
settings.setLoadWithOverviewMode(true);
|
||||
|
||||
settings.setBuiltInZoomControls(true);
|
||||
settings.setDisplayZoomControls(false);
|
||||
|
||||
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);
|
||||
|
||||
settings.setAllowFileAccess(false);
|
||||
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
|
||||
}
|
||||
|
||||
void init(
|
||||
int height, float size, Pair<Integer, Integer> position,
|
||||
float textSize, boolean monospaced,
|
||||
boolean show_images, boolean inline,
|
||||
IWebView intf) {
|
||||
Log.i("Init height=" + height + " size=" + size);
|
||||
|
||||
this.height = (height == 0 ? getMinimumHeight() : height);
|
||||
|
||||
setInitialScale(size == 0 ? 1 : Math.round(size * 100));
|
||||
|
||||
if (position != null) {
|
||||
setScrollX(position.first);
|
||||
setScrollY(position.second);
|
||||
}
|
||||
|
||||
WebSettings settings = getSettings();
|
||||
if (textSize != 0) {
|
||||
int dp = Helper.pixels2dp(getContext(), textSize);
|
||||
settings.setDefaultFontSize(Math.round(dp));
|
||||
settings.setDefaultFixedFontSize(Math.round(dp));
|
||||
}
|
||||
if (monospaced)
|
||||
settings.setStandardFontFamily("monospace");
|
||||
|
||||
settings.setLoadsImagesAutomatically(show_images || inline);
|
||||
settings.setBlockNetworkLoads(!show_images);
|
||||
settings.setBlockNetworkImage(!show_images);
|
||||
|
||||
this.intf = intf;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
if (height > getMinimumHeight())
|
||||
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST));
|
||||
else
|
||||
super.onMeasure(widthMeasureSpec, heightMeasureSpec); // Unspecified
|
||||
|
||||
int mh = getMeasuredHeight();
|
||||
Log.i("Measured height=" + mh + " last=" + height);
|
||||
if (mh == 0)
|
||||
setMeasuredDimension(getMeasuredWidth(), height);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSizeChanged(int w, int h, int ow, int oh) {
|
||||
super.onSizeChanged(w, h, ow, oh);
|
||||
Log.i("Size changed height=" + h);
|
||||
this.intf.onSizeChanged(w, h, ow, oh);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDownloadStart(
|
||||
String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
|
||||
Log.i("Download url=" + url + " mime type=" + mimetype);
|
||||
|
||||
Uri uri = Uri.parse(url);
|
||||
if ("cid".equals(uri.getScheme()) || "data".equals(uri.getScheme()))
|
||||
return;
|
||||
|
||||
Helper.view(getContext(), uri, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onLongClick(View view) {
|
||||
WebView.HitTestResult result = ((WebView) view).getHitTestResult();
|
||||
if (result.getType() == WebView.HitTestResult.IMAGE_TYPE ||
|
||||
result.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
|
||||
Log.i("Long press url=" + result.getExtra());
|
||||
|
||||
Uri uri = Uri.parse(result.getExtra());
|
||||
if ("cid".equals(uri.getScheme()) || "data".equals(uri.getScheme()))
|
||||
return false;
|
||||
|
||||
Helper.view(getContext(), uri, true);
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
interface IWebView {
|
||||
void onSizeChanged(int w, int h, int ow, int oh);
|
||||
|
||||
void onScaleChanged(float newScale);
|
||||
|
||||
void onScrollChange(int scrollX, int scrollY);
|
||||
|
||||
boolean onOpenLink(String url);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue