2010-12-25 17:30:16 -05:00
|
|
|
package com.fsck.k9.view;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2012-08-14 18:21:22 -04:00
|
|
|
import android.content.pm.PackageManager;
|
2010-12-25 17:30:16 -05:00
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.util.Log;
|
2010-12-25 17:30:34 -05:00
|
|
|
import android.view.KeyEvent;
|
2010-12-25 17:30:16 -05:00
|
|
|
import android.webkit.WebSettings;
|
2010-12-25 17:30:34 -05:00
|
|
|
import android.widget.Toast;
|
2013-03-23 17:53:02 -04:00
|
|
|
|
2010-12-25 17:30:16 -05:00
|
|
|
import com.fsck.k9.K9;
|
2010-12-25 17:30:34 -05:00
|
|
|
import com.fsck.k9.R;
|
2013-03-01 18:48:46 -05:00
|
|
|
import com.fsck.k9.helper.HtmlConverter;
|
2015-01-09 10:16:00 -05:00
|
|
|
import com.fsck.k9.helper.HtmlSanitizer;
|
|
|
|
|
2013-03-01 18:48:46 -05:00
|
|
|
|
2013-03-23 22:41:40 -04:00
|
|
|
public class MessageWebView extends RigidWebView {
|
2012-02-20 22:56:05 -05:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public MessageWebView(Context context) {
|
2010-12-25 17:30:16 -05:00
|
|
|
super(context);
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public MessageWebView(Context context, AttributeSet attrs) {
|
2010-12-25 17:30:16 -05:00
|
|
|
super(context, attrs);
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public MessageWebView(Context context, AttributeSet attrs, int defStyle) {
|
2010-12-25 17:30:16 -05:00
|
|
|
super(context, attrs, defStyle);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configure a web view to load or not load network data. A <b>true</b> setting here means that
|
|
|
|
* network data will be blocked.
|
|
|
|
* @param shouldBlockNetworkData True if network data should be blocked, false to allow network data.
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public void blockNetworkData(final boolean shouldBlockNetworkData) {
|
2014-01-15 15:30:59 -05:00
|
|
|
/*
|
|
|
|
* Block network loads.
|
|
|
|
*
|
|
|
|
* Images with content: URIs will not be blocked, nor
|
|
|
|
* will network images that are already in the WebView cache.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
getSettings().setBlockNetworkLoads(shouldBlockNetworkData);
|
2010-12-25 17:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configure a {@link android.webkit.WebView} to display a Message. This method takes into account a user's
|
|
|
|
* preferences when configuring the view. This message is used to view a message and to display a message being
|
|
|
|
* replied to.
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public void configure() {
|
2010-12-25 17:30:16 -05:00
|
|
|
this.setVerticalScrollBarEnabled(true);
|
|
|
|
this.setVerticalScrollbarOverlay(true);
|
|
|
|
this.setScrollBarStyle(SCROLLBARS_INSIDE_OVERLAY);
|
2012-02-20 22:56:05 -05:00
|
|
|
this.setLongClickable(true);
|
2010-12-25 17:30:16 -05:00
|
|
|
|
2013-02-06 14:08:57 -05:00
|
|
|
if (K9.getK9MessageViewTheme() == K9.Theme.DARK) {
|
2012-04-02 21:41:44 -04:00
|
|
|
// Black theme should get a black webview background
|
|
|
|
// we'll set the background of the messages on load
|
|
|
|
this.setBackgroundColor(0xff000000);
|
|
|
|
}
|
|
|
|
|
2010-12-25 17:30:16 -05:00
|
|
|
final WebSettings webSettings = this.getSettings();
|
|
|
|
|
|
|
|
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
|
2012-08-14 18:21:22 -04:00
|
|
|
|
2010-12-25 17:30:16 -05:00
|
|
|
webSettings.setSupportZoom(true);
|
2012-08-14 18:21:22 -04:00
|
|
|
webSettings.setBuiltInZoomControls(true);
|
2013-02-27 16:04:02 -05:00
|
|
|
webSettings.setUseWideViewPort(true);
|
2013-04-04 17:11:17 -04:00
|
|
|
if (K9.autofitWidth()) {
|
2013-11-07 13:38:35 -05:00
|
|
|
webSettings.setLoadWithOverviewMode(true);
|
2013-04-11 22:26:32 -04:00
|
|
|
}
|
2012-08-14 18:21:22 -04:00
|
|
|
|
|
|
|
disableDisplayZoomControls();
|
|
|
|
|
2010-12-25 17:30:16 -05:00
|
|
|
webSettings.setJavaScriptEnabled(false);
|
|
|
|
webSettings.setLoadsImagesAutomatically(true);
|
|
|
|
webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
|
|
|
|
|
2014-08-16 20:03:22 -04:00
|
|
|
// TODO: Review alternatives. NARROW_COLUMNS is deprecated on KITKAT
|
|
|
|
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
|
2010-12-25 17:30:16 -05:00
|
|
|
|
2014-08-16 18:59:50 -04:00
|
|
|
setOverScrollMode(OVER_SCROLL_NEVER);
|
2012-02-20 22:56:05 -05:00
|
|
|
|
2014-02-12 22:22:45 -05:00
|
|
|
webSettings.setTextZoom(K9.getFontSizes().getMessageViewContentAsPercent());
|
2010-12-25 17:30:16 -05:00
|
|
|
|
2011-09-27 00:03:13 -04:00
|
|
|
// Disable network images by default. This is overridden by preferences.
|
2010-12-25 17:30:16 -05:00
|
|
|
blockNetworkData(true);
|
|
|
|
}
|
2010-12-25 17:30:34 -05:00
|
|
|
|
2012-08-14 18:21:22 -04:00
|
|
|
/**
|
|
|
|
* Disable on-screen zoom controls on devices that support zooming via pinch-to-zoom.
|
|
|
|
*/
|
|
|
|
private void disableDisplayZoomControls() {
|
2014-08-16 18:59:50 -04:00
|
|
|
PackageManager pm = getContext().getPackageManager();
|
|
|
|
boolean supportsMultiTouch =
|
|
|
|
pm.hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH) ||
|
|
|
|
pm.hasSystemFeature(PackageManager.FEATURE_FAKETOUCH_MULTITOUCH_DISTINCT);
|
2012-08-14 18:21:22 -04:00
|
|
|
|
2014-08-16 18:59:50 -04:00
|
|
|
getSettings().setDisplayZoomControls(!supportsMultiTouch);
|
2012-07-07 09:03:23 -04:00
|
|
|
}
|
|
|
|
|
2013-03-01 11:26:52 -05:00
|
|
|
/**
|
|
|
|
* Load a message body into a {@code MessageWebView}
|
|
|
|
*
|
|
|
|
* <p>
|
|
|
|
* Before loading, the text is wrapped in an HTML header and footer
|
|
|
|
* so that it displays properly.
|
|
|
|
* </p>
|
|
|
|
*
|
|
|
|
* @param text
|
|
|
|
* The message body to display. Assumed to be MIME type text/html.
|
|
|
|
*/
|
|
|
|
public void setText(String text) {
|
|
|
|
// Include a meta tag so the WebView will not use a fixed viewport width of 980 px
|
2013-03-01 12:59:59 -05:00
|
|
|
String content = "<html><head><meta name=\"viewport\" content=\"width=device-width\"/>";
|
2013-02-06 14:08:57 -05:00
|
|
|
if (K9.getK9MessageViewTheme() == K9.Theme.DARK) {
|
2013-03-01 12:59:59 -05:00
|
|
|
content += "<style type=\"text/css\">" +
|
2013-04-23 19:05:11 -04:00
|
|
|
"* { background: black ! important; color: #F3F3F3 !important }" +
|
2012-04-02 21:41:44 -04:00
|
|
|
":link, :link * { color: #CCFF33 !important }" +
|
2013-03-01 12:59:59 -05:00
|
|
|
":visited, :visited * { color: #551A8B !important }</style> ";
|
2012-04-02 21:41:44 -04:00
|
|
|
}
|
2013-03-01 18:48:46 -05:00
|
|
|
content += HtmlConverter.cssStylePre();
|
2013-03-01 12:59:59 -05:00
|
|
|
content += "</head><body>" + text + "</body></html>";
|
2015-01-09 10:16:00 -05:00
|
|
|
|
|
|
|
String sanitizedContent = HtmlSanitizer.sanitize(content);
|
|
|
|
loadDataWithBaseURL("http://", sanitizedContent, "text/html", "utf-8", null);
|
2014-01-25 19:32:59 -05:00
|
|
|
resumeTimers();
|
2012-04-02 21:41:44 -04:00
|
|
|
}
|
|
|
|
|
2010-12-31 03:56:05 -05:00
|
|
|
/*
|
2010-12-25 17:30:34 -05:00
|
|
|
* Emulate the shift key being pressed to trigger the text selection mode
|
|
|
|
* of a WebView.
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public void emulateShiftHeld() {
|
|
|
|
try {
|
2010-12-25 17:30:34 -05:00
|
|
|
|
|
|
|
KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
|
|
|
|
KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
|
2013-02-04 05:18:49 -05:00
|
|
|
shiftPressEvent.dispatch(this, null, null);
|
2010-12-25 17:30:34 -05:00
|
|
|
Toast.makeText(getContext() , R.string.select_text_now, Toast.LENGTH_SHORT).show();
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (Exception e) {
|
2010-12-25 17:30:34 -05:00
|
|
|
Log.e(K9.LOG_TAG, "Exception in emulateShiftHeld()", e);
|
|
|
|
}
|
|
|
|
}
|
2011-11-02 19:39:23 -04:00
|
|
|
|
2010-12-25 17:30:16 -05:00
|
|
|
}
|