Browse Source

Added some code to allow inverted and grayscale modes in WebView

master
Anthony Restaino 11 years ago
parent
commit
f5de4626ef
  1. 303
      src/acr/browser/lightning/LightningView.java

303
src/acr/browser/lightning/LightningView.java

@ -10,6 +10,9 @@ import android.app.AlertDialog;
import android.content.*; import android.content.*;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.net.MailTo; import android.net.MailTo;
import android.net.Uri; import android.net.Uri;
import android.net.http.SslError; import android.net.http.SslError;
@ -35,34 +38,26 @@ import java.net.*;
public class LightningView { public class LightningView {
private Title mTitle; private Title mTitle;
private WebView mWebView; private WebView mWebView;
private BrowserController mBrowserController; private BrowserController mBrowserController;
private GestureDetector mGestureDetector; private GestureDetector mGestureDetector;
private Activity mActivity; private Activity mActivity;
private WebSettings mSettings; private WebSettings mSettings;
private static int API = android.os.Build.VERSION.SDK_INT; private static int API = android.os.Build.VERSION.SDK_INT;
private static String mHomepage; private static String mHomepage;
private static String mDefaultUserAgent; private static String mDefaultUserAgent;
private static Bitmap mWebpageBitmap; private static Bitmap mWebpageBitmap;
private static SharedPreferences mPreferences; private static SharedPreferences mPreferences;
private static boolean mWideViewPort; private static boolean mWideViewPort;
private AdBlock mAdBlock; private AdBlock mAdBlock;
private boolean isForegroundTab; private boolean isForegroundTab;
private IntentUtils mIntentUtils; private IntentUtils mIntentUtils;
private Paint mPaint = new Paint();
private static final float[] mNegativeColorArray = { -1.0f, 0, 0, 0, 255, // red
0, -1.0f, 0, 0, 255, // green
0, 0, -1.0f, 0, 255, // blue
0, 0, 0, 1.0f, 0 // alpha
};
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@SuppressLint("NewApi") @SuppressLint("NewApi")
@ -88,6 +83,8 @@ public class LightningView {
mWebView.setAnimationCacheEnabled(false); mWebView.setAnimationCacheEnabled(false);
mWebView.setDrawingCacheEnabled(true); mWebView.setDrawingCacheEnabled(true);
mWebView.setBackgroundColor(activity.getResources().getColor(android.R.color.white)); mWebView.setBackgroundColor(activity.getResources().getColor(android.R.color.white));
mWebView.setBackground(null);
if (API > 15) { if (API > 15) {
mWebView.getRootView().setBackground(null); mWebView.getRootView().setBackground(null);
} else { } else {
@ -137,12 +134,12 @@ public class LightningView {
mSettings = mWebView.getSettings(); mSettings = mWebView.getSettings();
initializeSettings(mWebView.getSettings(), activity); initializeSettings(mWebView.getSettings(), activity);
initializePreferences(activity); initializePreferences(activity);
if (url != null) { if (url != null) {
if (!url.trim().isEmpty()) { if (!url.trim().isEmpty()) {
mWebView.loadUrl(url); mWebView.loadUrl(url);
} else { } else {
//don't load anything, the user is looking for a blank tab // don't load anything, the user is looking for a blank tab
} }
} else { } else {
if (mHomepage.startsWith("about:home")) { if (mHomepage.startsWith("about:home")) {
@ -160,87 +157,87 @@ public class LightningView {
String home; String home;
home = HomepageVariables.HEAD; home = HomepageVariables.HEAD;
switch (mPreferences.getInt(PreferenceConstants.SEARCH, 1)) { switch (mPreferences.getInt(PreferenceConstants.SEARCH, 1)) {
case 0: case 0:
// CUSTOM SEARCH // CUSTOM SEARCH
home = home + "file:///android_asset/lightning.png"; home = home + "file:///android_asset/lightning.png";
home = home + HomepageVariables.MIDDLE; home = home + HomepageVariables.MIDDLE;
home = home home = home
+ mPreferences.getString(PreferenceConstants.SEARCH_URL, + mPreferences.getString(PreferenceConstants.SEARCH_URL,
Constants.GOOGLE_SEARCH); Constants.GOOGLE_SEARCH);
break; break;
case 1: case 1:
// GOOGLE_SEARCH; // GOOGLE_SEARCH;
home = home + "file:///android_asset/google.png"; home = home + "file:///android_asset/google.png";
// + "https://www.google.com/images/srpr/logo11w.png"; // + "https://www.google.com/images/srpr/logo11w.png";
home = home + HomepageVariables.MIDDLE; home = home + HomepageVariables.MIDDLE;
home = home + Constants.GOOGLE_SEARCH; home = home + Constants.GOOGLE_SEARCH;
break; break;
case 2: case 2:
// ANDROID SEARCH; // ANDROID SEARCH;
home = home + "file:///android_asset/lightning.png"; home = home + "file:///android_asset/lightning.png";
home = home + HomepageVariables.MIDDLE; home = home + HomepageVariables.MIDDLE;
home = home + Constants.ANDROID_SEARCH; home = home + Constants.ANDROID_SEARCH;
break; break;
case 3: case 3:
// BING_SEARCH; // BING_SEARCH;
home = home + "file:///android_asset/bing.png"; home = home + "file:///android_asset/bing.png";
// + // +
// "http://upload.wikimedia.org/wikipedia/commons/thumb/b/b1/Bing_logo_%282013%29.svg/500px-Bing_logo_%282013%29.svg.png"; // "http://upload.wikimedia.org/wikipedia/commons/thumb/b/b1/Bing_logo_%282013%29.svg/500px-Bing_logo_%282013%29.svg.png";
home = home + HomepageVariables.MIDDLE; home = home + HomepageVariables.MIDDLE;
home = home + Constants.BING_SEARCH; home = home + Constants.BING_SEARCH;
break; break;
case 4: case 4:
// YAHOO_SEARCH; // YAHOO_SEARCH;
home = home + "file:///android_asset/yahoo.png"; home = home + "file:///android_asset/yahoo.png";
// + // +
// "http://upload.wikimedia.org/wikipedia/commons/thumb/2/24/Yahoo%21_logo.svg/799px-Yahoo%21_logo.svg.png"; // "http://upload.wikimedia.org/wikipedia/commons/thumb/2/24/Yahoo%21_logo.svg/799px-Yahoo%21_logo.svg.png";
home = home + HomepageVariables.MIDDLE; home = home + HomepageVariables.MIDDLE;
home = home + Constants.YAHOO_SEARCH; home = home + Constants.YAHOO_SEARCH;
break; break;
case 5: case 5:
// STARTPAGE_SEARCH; // STARTPAGE_SEARCH;
home = home + "file:///android_asset/startpage.png"; home = home + "file:///android_asset/startpage.png";
// + "https://startpage.com/graphics/startp_logo.gif"; // + "https://startpage.com/graphics/startp_logo.gif";
home = home + HomepageVariables.MIDDLE; home = home + HomepageVariables.MIDDLE;
home = home + Constants.STARTPAGE_SEARCH; home = home + Constants.STARTPAGE_SEARCH;
break; break;
case 6: case 6:
// STARTPAGE_MOBILE // STARTPAGE_MOBILE
home = home + "file:///android_asset/startpage.png"; home = home + "file:///android_asset/startpage.png";
// + "https://startpage.com/graphics/startp_logo.gif"; // + "https://startpage.com/graphics/startp_logo.gif";
home = home + HomepageVariables.MIDDLE; home = home + HomepageVariables.MIDDLE;
home = home + Constants.STARTPAGE_MOBILE_SEARCH; home = home + Constants.STARTPAGE_MOBILE_SEARCH;
case 7: case 7:
// DUCK_SEARCH; // DUCK_SEARCH;
home = home + "file:///android_asset/duckduckgo.png"; home = home + "file:///android_asset/duckduckgo.png";
// + // +
// "https://duckduckgo.com/assets/logo_homepage.normal.v101.png"; // "https://duckduckgo.com/assets/logo_homepage.normal.v101.png";
home = home + HomepageVariables.MIDDLE; home = home + HomepageVariables.MIDDLE;
home = home + Constants.DUCK_SEARCH; home = home + Constants.DUCK_SEARCH;
break; break;
case 8: case 8:
// DUCK_LITE_SEARCH; // DUCK_LITE_SEARCH;
home = home + "file:///android_asset/duckduckgo.png"; home = home + "file:///android_asset/duckduckgo.png";
// + // +
// "https://duckduckgo.com/assets/logo_homepage.normal.v101.png"; // "https://duckduckgo.com/assets/logo_homepage.normal.v101.png";
home = home + HomepageVariables.MIDDLE; home = home + HomepageVariables.MIDDLE;
home = home + Constants.DUCK_LITE_SEARCH; home = home + Constants.DUCK_LITE_SEARCH;
break; break;
case 9: case 9:
// BAIDU_SEARCH; // BAIDU_SEARCH;
home = home + "file:///android_asset/baidu.png"; home = home + "file:///android_asset/baidu.png";
// + "http://www.baidu.com/img/bdlogo.gif"; // + "http://www.baidu.com/img/bdlogo.gif";
home = home + HomepageVariables.MIDDLE; home = home + HomepageVariables.MIDDLE;
home = home + Constants.BAIDU_SEARCH; home = home + Constants.BAIDU_SEARCH;
break; break;
case 10: case 10:
// YANDEX_SEARCH; // YANDEX_SEARCH;
home = home + "file:///android_asset/yandex.png"; home = home + "file:///android_asset/yandex.png";
// + // +
// "http://upload.wikimedia.org/wikipedia/commons/thumb/9/91/Yandex.svg/600px-Yandex.svg.png"; // "http://upload.wikimedia.org/wikipedia/commons/thumb/9/91/Yandex.svg/600px-Yandex.svg.png";
home = home + HomepageVariables.MIDDLE; home = home + HomepageVariables.MIDDLE;
home = home + Constants.YANDEX_SEARCH; home = home + Constants.YANDEX_SEARCH;
break; break;
} }
@ -273,38 +270,38 @@ public class LightningView {
.getBoolean(PreferenceConstants.LOCATION, false)); .getBoolean(PreferenceConstants.LOCATION, false));
if (API < 19) { if (API < 19) {
switch (mPreferences.getInt(PreferenceConstants.ADOBE_FLASH_SUPPORT, 0)) { switch (mPreferences.getInt(PreferenceConstants.ADOBE_FLASH_SUPPORT, 0)) {
case 0: case 0:
mSettings.setPluginState(PluginState.OFF); mSettings.setPluginState(PluginState.OFF);
break; break;
case 1:
mSettings.setPluginState(PluginState.ON_DEMAND);
break;
case 2:
mSettings.setPluginState(PluginState.ON);
break;
default:
break;
}
}
switch (mPreferences.getInt(PreferenceConstants.USER_AGENT, 1)) {
case 1: case 1:
mSettings.setPluginState(PluginState.ON_DEMAND); if (API > 16) {
mSettings.setUserAgentString(WebSettings.getDefaultUserAgent(context));
} else {
mSettings.setUserAgentString(mDefaultUserAgent);
}
break; break;
case 2: case 2:
mSettings.setPluginState(PluginState.ON); mSettings.setUserAgentString(Constants.DESKTOP_USER_AGENT);
break; break;
default: case 3:
mSettings.setUserAgentString(Constants.MOBILE_USER_AGENT);
break;
case 4:
mSettings.setUserAgentString(mPreferences.getString(
PreferenceConstants.USER_AGENT_STRING, mDefaultUserAgent));
break; break;
}
}
switch (mPreferences.getInt(PreferenceConstants.USER_AGENT, 1)) {
case 1:
if (API > 16) {
mSettings.setUserAgentString(WebSettings.getDefaultUserAgent(context));
} else {
mSettings.setUserAgentString(mDefaultUserAgent);
}
break;
case 2:
mSettings.setUserAgentString(Constants.DESKTOP_USER_AGENT);
break;
case 3:
mSettings.setUserAgentString(Constants.MOBILE_USER_AGENT);
break;
case 4:
mSettings.setUserAgentString(mPreferences.getString(
PreferenceConstants.USER_AGENT_STRING, mDefaultUserAgent));
break;
} }
if (mPreferences.getBoolean(PreferenceConstants.SAVE_PASSWORDS, false)) { if (mPreferences.getBoolean(PreferenceConstants.SAVE_PASSWORDS, false)) {
@ -337,27 +334,28 @@ public class LightningView {
mSettings.setLoadWithOverviewMode(mPreferences.getBoolean( mSettings.setLoadWithOverviewMode(mPreferences.getBoolean(
PreferenceConstants.OVERVIEW_MODE, true)); PreferenceConstants.OVERVIEW_MODE, true));
switch (mPreferences.getInt(PreferenceConstants.TEXT_SIZE, 3)) { switch (mPreferences.getInt(PreferenceConstants.TEXT_SIZE, 3)) {
case 1: case 1:
mSettings.setTextZoom(200); mSettings.setTextZoom(200);
break; break;
case 2: case 2:
mSettings.setTextZoom(150); mSettings.setTextZoom(150);
break; break;
case 3: case 3:
mSettings.setTextZoom(100); mSettings.setTextZoom(100);
break; break;
case 4: case 4:
mSettings.setTextZoom(75); mSettings.setTextZoom(75);
break; break;
case 5: case 5:
mSettings.setTextZoom(50); mSettings.setTextZoom(50);
break; break;
} }
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@SuppressLint({ "SetJavaScriptEnabled", "NewApi" }) @SuppressLint({ "SetJavaScriptEnabled", "NewApi" })
public void initializeSettings(WebSettings settings, Context context) { public void initializeSettings(WebSettings settings, Context context) {
this.setNormalRendering();
if (API < 18) { if (API < 18) {
settings.setAppCacheMaxSize(Long.MAX_VALUE); settings.setAppCacheMaxSize(Long.MAX_VALUE);
} }
@ -427,6 +425,33 @@ public class LightningView {
} }
} }
public void setHardwareRendering() {
mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, mPaint);
}
public void setNormalRendering() {
mWebView.setLayerType(View.LAYER_TYPE_NONE, mPaint);
}
public void setColorMode(int mode) {
switch (mode) {
case 0:
mPaint.setColorFilter(null);
break;
case 1:
ColorMatrixColorFilter filterInvert = new ColorMatrixColorFilter(
mNegativeColorArray);
mPaint.setColorFilter(filterInvert);
break;
case 2:
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter filterGray = new ColorMatrixColorFilter(cm);
mPaint.setColorFilter(filterGray);
break;
}
}
public synchronized void pauseTimers() { public synchronized void pauseTimers() {
if (mWebView != null) { if (mWebView != null) {
mWebView.pauseTimers(); mWebView.pauseTimers();

Loading…
Cancel
Save