Browse Source

Add option to clear Web Storage

master
Anthony Restaino 9 years ago
parent
commit
08eedbe121
  1. 89
      app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java
  2. 62
      app/src/main/java/acr/browser/lightning/fragment/PrivacySettingsFragment.java
  3. 9
      app/src/main/java/acr/browser/lightning/preference/PreferenceManager.java
  4. 59
      app/src/main/java/acr/browser/lightning/utils/WebUtils.java
  5. 3
      app/src/main/res/values/strings.xml
  6. 11
      app/src/main/res/xml/preference_privacy.xml

89
app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java

@ -70,15 +70,11 @@ import android.view.animation.DecelerateInterpolator; @@ -70,15 +70,11 @@ import android.view.animation.DecelerateInterpolator;
import android.view.animation.Transformation;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient.CustomViewCallback;
import android.webkit.WebIconDatabase;
import android.webkit.WebStorage;
import android.webkit.WebView;
import android.webkit.WebView.HitTestResult;
import android.webkit.WebViewDatabase;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
@ -126,6 +122,7 @@ import acr.browser.lightning.receiver.NetworkReceiver; @@ -126,6 +122,7 @@ import acr.browser.lightning.receiver.NetworkReceiver;
import acr.browser.lightning.utils.ProxyUtils;
import acr.browser.lightning.utils.ThemeUtils;
import acr.browser.lightning.utils.Utils;
import acr.browser.lightning.utils.WebUtils;
import acr.browser.lightning.view.AnimatedProgressBar;
import acr.browser.lightning.view.LightningView;
@ -1291,21 +1288,7 @@ public abstract class BrowserActivity extends ThemableActivity implements Browse @@ -1291,21 +1288,7 @@ public abstract class BrowserActivity extends ThemableActivity implements Browse
closeActivity();
} else {
mWebViewList.remove(position);
if (mPreferences.getClearCacheExit() && mCurrentView != null && !isIncognito()) {
mCurrentView.clearCache(true);
Log.d(Constants.TAG, "Cache Cleared");
}
if (mPreferences.getClearHistoryExitEnabled() && !isIncognito()) {
clearHistory();
Log.d(Constants.TAG, "History Cleared");
}
if (mPreferences.getClearCookiesExitEnabled() && !isIncognito()) {
clearCookies();
Log.d(Constants.TAG, "Cookies Cleared");
}
performExitCleanUp();
reference.pauseTimers();
reference.onDestroy();
mCurrentView = null;
@ -1325,31 +1308,41 @@ public abstract class BrowserActivity extends ThemableActivity implements Browse @@ -1325,31 +1308,41 @@ public abstract class BrowserActivity extends ThemableActivity implements Browse
Log.d(Constants.TAG, "deleted tab");
}
@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
showCloseDialog(mDrawerListLeft.getCheckedItemPosition());
}
return true;
}
private void closeBrowser() {
mBrowserFrame.setBackgroundColor(mBackgroundColor);
public void performExitCleanUp() {
if (mPreferences.getClearCacheExit() && mCurrentView != null && !isIncognito()) {
mCurrentView.clearCache(true);
WebUtils.clearCache(mCurrentView.getWebView());
Log.d(Constants.TAG, "Cache Cleared");
}
if (mPreferences.getClearHistoryExitEnabled() && !isIncognito()) {
clearHistory();
WebUtils.clearHistory(this, mSystemBrowser);
Log.d(Constants.TAG, "History Cleared");
}
if (mPreferences.getClearCookiesExitEnabled() && !isIncognito()) {
clearCookies();
WebUtils.clearCookies(this);
Log.d(Constants.TAG, "Cookies Cleared");
}
if (mPreferences.getClearWebStorageExitEnabled() && !isIncognito()) {
WebUtils.clearWebStorage();
Log.d(Constants.TAG, "WebStorage Cleared");
} else if (isIncognito()) {
WebUtils.clearWebStorage(); // We want to make sure incognito mode is secure
}
}
@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
showCloseDialog(mDrawerListLeft.getCheckedItemPosition());
}
return true;
}
private void closeBrowser() {
mBrowserFrame.setBackgroundColor(mBackgroundColor);
performExitCleanUp();
mCurrentView = null;
mWebView = null;
for (int n = 0; n < mWebViewList.size(); n++) {
@ -1362,38 +1355,6 @@ public abstract class BrowserActivity extends ThemableActivity implements Browse @@ -1362,38 +1355,6 @@ public abstract class BrowserActivity extends ThemableActivity implements Browse
finish();
}
private void clearHistory() {
this.deleteDatabase(HistoryDatabase.DATABASE_NAME);
WebViewDatabase m = WebViewDatabase.getInstance(this);
m.clearFormData();
m.clearHttpAuthUsernamePassword();
if (API < 18) {
m.clearUsernamePassword();
WebIconDatabase.getInstance().removeAllIcons();
}
if (mSystemBrowser) {
try {
Browser.clearHistory(getContentResolver());
} catch (NullPointerException ignored) {
}
}
Utils.trimCache(this);
}
private void clearCookies() {
// TODO Break out web storage deletion into its own option/action
// TODO clear web storage for all sites that are visited in Incognito mode
WebStorage storage = WebStorage.getInstance();
storage.deleteAllData();
CookieManager c = CookieManager.getInstance();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
c.removeAllCookies(null);
} else {
CookieSyncManager.createInstance(this);
c.removeAllCookie();
}
}
@Override
public void onBackPressed() {
if (mDrawerLayout.isDrawerOpen(mDrawerLeft)) {

62
app/src/main/java/acr/browser/lightning/fragment/PrivacySettingsFragment.java

@ -12,19 +12,13 @@ import android.os.Message; @@ -12,19 +12,13 @@ import android.os.Message;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.provider.Browser;
import android.support.v7.app.AlertDialog;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebIconDatabase;
import android.webkit.WebStorage;
import android.webkit.WebView;
import android.webkit.WebViewDatabase;
import acr.browser.lightning.R;
import acr.browser.lightning.database.HistoryDatabase;
import acr.browser.lightning.preference.PreferenceManager;
import acr.browser.lightning.utils.Utils;
import acr.browser.lightning.utils.WebUtils;
public class PrivacySettingsFragment extends PreferenceFragment implements Preference.OnPreferenceClickListener, Preference.OnPreferenceChangeListener {
@ -38,12 +32,14 @@ public class PrivacySettingsFragment extends PreferenceFragment implements Prefe @@ -38,12 +32,14 @@ public class PrivacySettingsFragment extends PreferenceFragment implements Prefe
private static final String SETTINGS_CLEARCACHE = "clear_cache";
private static final String SETTINGS_CLEARHISTORY = "clear_history";
private static final String SETTINGS_CLEARCOOKIES = "clear_cookies";
private static final String SETTINGS_CLEARWEBSTORAGE = "clear_webstorage";
private static final String SETTINGS_WEBSTORAGEEXIT = "clear_webstorage_exit";
private static final int API = android.os.Build.VERSION.SDK_INT;
private Activity mActivity;
private PreferenceManager mPreferences;
private CheckBoxPreference cblocation, cb3cookies, cbsavepasswords, cbcacheexit, cbhistoryexit,
cbcookiesexit, cbsynchistory;
cbcookiesexit, cbsynchistory, cbwebstorageexit;
private boolean mSystemBrowser;
private Handler messageHandler;
@ -66,6 +62,8 @@ public class PrivacySettingsFragment extends PreferenceFragment implements Prefe @@ -66,6 +62,8 @@ public class PrivacySettingsFragment extends PreferenceFragment implements Prefe
Preference clearcache = findPreference(SETTINGS_CLEARCACHE);
Preference clearhistory = findPreference(SETTINGS_CLEARHISTORY);
Preference clearcookies = findPreference(SETTINGS_CLEARCOOKIES);
Preference clearwebstorage = findPreference(SETTINGS_CLEARWEBSTORAGE);
cblocation = (CheckBoxPreference) findPreference(SETTINGS_LOCATION);
cb3cookies = (CheckBoxPreference) findPreference(SETTINGS_THIRDPCOOKIES);
cbsavepasswords = (CheckBoxPreference) findPreference(SETTINGS_SAVEPASSWORD);
@ -73,10 +71,13 @@ public class PrivacySettingsFragment extends PreferenceFragment implements Prefe @@ -73,10 +71,13 @@ public class PrivacySettingsFragment extends PreferenceFragment implements Prefe
cbhistoryexit = (CheckBoxPreference) findPreference(SETTINGS_HISTORYEXIT);
cbcookiesexit = (CheckBoxPreference) findPreference(SETTINGS_COOKIEEXIT);
cbsynchistory = (CheckBoxPreference) findPreference(SETTINGS_SYNCHISTORY);
cbwebstorageexit = (CheckBoxPreference) findPreference(SETTINGS_WEBSTORAGEEXIT);
clearcache.setOnPreferenceClickListener(this);
clearhistory.setOnPreferenceClickListener(this);
clearcookies.setOnPreferenceClickListener(this);
clearwebstorage.setOnPreferenceClickListener(this);
cblocation.setOnPreferenceChangeListener(this);
cb3cookies.setOnPreferenceChangeListener(this);
cbsavepasswords.setOnPreferenceChangeListener(this);
@ -84,6 +85,7 @@ public class PrivacySettingsFragment extends PreferenceFragment implements Prefe @@ -84,6 +85,7 @@ public class PrivacySettingsFragment extends PreferenceFragment implements Prefe
cbhistoryexit.setOnPreferenceChangeListener(this);
cbcookiesexit.setOnPreferenceChangeListener(this);
cbsynchistory.setOnPreferenceChangeListener(this);
cbwebstorageexit.setOnPreferenceChangeListener(this);
cblocation.setChecked(mPreferences.getLocationEnabled());
cbsavepasswords.setChecked(mPreferences.getSavePasswordsEnabled());
@ -91,6 +93,7 @@ public class PrivacySettingsFragment extends PreferenceFragment implements Prefe @@ -91,6 +93,7 @@ public class PrivacySettingsFragment extends PreferenceFragment implements Prefe
cbhistoryexit.setChecked(mPreferences.getClearHistoryExitEnabled());
cbcookiesexit.setChecked(mPreferences.getClearCookiesExitEnabled());
cb3cookies.setChecked(mPreferences.getBlockThirdPartyCookiesEnabled());
cbwebstorageexit.setChecked(mPreferences.getClearWebStorageExitEnabled());
cb3cookies.setEnabled(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP);
@ -141,6 +144,9 @@ public class PrivacySettingsFragment extends PreferenceFragment implements Prefe @@ -141,6 +144,9 @@ public class PrivacySettingsFragment extends PreferenceFragment implements Prefe
case SETTINGS_CLEARCOOKIES:
clearCookiesDialog();
return true;
case SETTINGS_CLEARWEBSTORAGE:
clearWebStorage();
return true;
default:
return false;
}
@ -203,43 +209,21 @@ public class PrivacySettingsFragment extends PreferenceFragment implements Prefe @@ -203,43 +209,21 @@ public class PrivacySettingsFragment extends PreferenceFragment implements Prefe
Utils.showSnackbar(mActivity, R.string.message_cache_cleared);
}
@SuppressWarnings("deprecation")
private void clearHistory() {
mActivity.deleteDatabase(HistoryDatabase.DATABASE_NAME);
WebViewDatabase m = WebViewDatabase.getInstance(mActivity);
m.clearFormData();
m.clearHttpAuthUsernamePassword();
if (API < 18) {
m.clearUsernamePassword();
WebIconDatabase.getInstance().removeAllIcons();
}
if (mSystemBrowser) {
try {
Browser.clearHistory(mActivity.getContentResolver());
} catch (Exception ignored) {
}
}
Utils.trimCache(mActivity);
WebUtils.clearHistory(getActivity(), mSystemBrowser);
messageHandler.sendEmptyMessage(1);
}
@SuppressWarnings("deprecation")
private void clearCookies() {
// TODO Break out web storage deletion into its own option/action
// TODO clear web storage for all sites that are visited in Incognito mode
WebStorage storage = WebStorage.getInstance();
storage.deleteAllData();
CookieManager c = CookieManager.getInstance();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
c.removeAllCookies(null);
} else {
CookieSyncManager.createInstance(mActivity);
c.removeAllCookie();
}
WebUtils.clearCookies(getActivity());
messageHandler.sendEmptyMessage(2);
}
private void clearWebStorage() {
WebUtils.clearWebStorage();
Utils.showSnackbar(getActivity(), R.string.message_web_storage_cleared);
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// switch preferences
@ -268,6 +252,10 @@ public class PrivacySettingsFragment extends PreferenceFragment implements Prefe @@ -268,6 +252,10 @@ public class PrivacySettingsFragment extends PreferenceFragment implements Prefe
mPreferences.setClearCookiesExitEnabled((Boolean) newValue);
cbcookiesexit.setChecked((Boolean) newValue);
return true;
case SETTINGS_WEBSTORAGEEXIT:
mPreferences.setClearWebStorageExitEnabled((Boolean) newValue);
cbwebstorageexit.setChecked((Boolean) newValue);
return true;
case SETTINGS_SYNCHISTORY:
mPreferences.setSyncHistoryEnabled((Boolean) newValue);
cbsynchistory.setChecked((Boolean) newValue);

9
app/src/main/java/acr/browser/lightning/preference/PreferenceManager.java

@ -48,6 +48,7 @@ public class PreferenceManager { @@ -48,6 +48,7 @@ public class PreferenceManager {
public static final String THEME = "Theme";
public static final String DEFAULT_BOOKMARKS = "defaultBookmarks";
public static final String TEXT_ENCODING = "textEncoding";
public static final String CLEAR_WEBSTORAGE_EXIT = "clearWebStorageExit";
public static final String USE_PROXY = "useProxy";
public static final String PROXY_CHOICE = "proxyChoice";
@ -101,6 +102,10 @@ public class PreferenceManager { @@ -101,6 +102,10 @@ public class PreferenceManager {
return mPrefs.getBoolean(Name.CLEAR_COOKIES_EXIT, false);
}
public boolean getClearWebStorageExitEnabled() {
return mPrefs.getBoolean(Name.CLEAR_WEBSTORAGE_EXIT, false);
}
public boolean getClearHistoryExitEnabled() {
return mPrefs.getBoolean(Name.CLEAR_HISTORY_EXIT, false);
}
@ -297,6 +302,10 @@ public class PreferenceManager { @@ -297,6 +302,10 @@ public class PreferenceManager {
putBoolean(Name.CLEAR_COOKIES_EXIT, enable);
}
public void setClearWebStorageExitEnabled(boolean enable) {
putBoolean(Name.CLEAR_WEBSTORAGE_EXIT, enable);
}
public void setClearHistoryExitEnabled(boolean enable) {
putBoolean(Name.CLEAR_HISTORY_EXIT, enable);
}

59
app/src/main/java/acr/browser/lightning/utils/WebUtils.java

@ -0,0 +1,59 @@ @@ -0,0 +1,59 @@
package acr.browser.lightning.utils;
import android.content.Context;
import android.os.Build;
import android.provider.Browser;
import android.support.annotation.NonNull;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebIconDatabase;
import android.webkit.WebStorage;
import android.webkit.WebView;
import android.webkit.WebViewDatabase;
import acr.browser.lightning.database.HistoryDatabase;
/**
* Copyright 8/4/2015 Anthony Restaino
*/
public class WebUtils {
public static void clearCookies(@NonNull Context context) {
CookieManager c = CookieManager.getInstance();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
c.removeAllCookies(null);
} else {
CookieSyncManager.createInstance(context);
c.removeAllCookie();
}
}
public static void clearWebStorage() {
WebStorage.getInstance().deleteAllData();
}
public static void clearHistory(@NonNull Context context, boolean systemBrowserPresent) {
context.deleteDatabase(HistoryDatabase.DATABASE_NAME);
WebViewDatabase m = WebViewDatabase.getInstance(context);
m.clearFormData();
m.clearHttpAuthUsernamePassword();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
m.clearUsernamePassword();
WebIconDatabase.getInstance().removeAllIcons();
}
if (systemBrowserPresent) {
try {
Browser.clearHistory(context.getContentResolver());
} catch (NullPointerException ignored) {
// ignored
}
}
Utils.trimCache(context);
}
public static void clearCache(WebView view) {
if (view == null) return;
view.clearCache(true);
}
}

3
app/src/main/res/values/strings.xml

@ -210,4 +210,7 @@ @@ -210,4 +210,7 @@
<string name="action_rename">Rename</string>
<string name="title_rename_folder">Rename Folder</string>
<string name="dialog_folder">What would you like to do with this folder?</string>
<string name="clear_web_storage">Clear Web Storage</string>
<string name="clear_web_storage_exit">Clear web storage on exit</string>
<string name="message_web_storage_cleared">Web Storage Cleared</string>
</resources>

11
app/src/main/res/xml/preference_privacy.xml

@ -13,8 +13,8 @@ @@ -13,8 +13,8 @@
<CheckBoxPreference
android:defaultValue="true"
android:key="password"
android:title="@string/password"
android:summary="@string/recommended" />
android:summary="@string/recommended"
android:title="@string/password" />
<CheckBoxPreference
android:defaultValue="false"
android:key="clear_cache_exit"
@ -27,6 +27,10 @@ @@ -27,6 +27,10 @@
android:defaultValue="false"
android:key="clear_cookies_exit"
android:title="@string/clear_cookies_exit" />
<CheckBoxPreference
android:defaultValue="false"
android:key="clear_webstorage_exit"
android:title="@string/clear_web_storage_exit" />
<CheckBoxPreference
android:defaultValue="false"
android:key="sync_history"
@ -40,5 +44,8 @@ @@ -40,5 +44,8 @@
<Preference
android:key="clear_cookies"
android:title="@string/clear_cookies" />
<Preference
android:key="clear_webstorage"
android:title="@string/clear_web_storage" />
</PreferenceCategory>
</PreferenceScreen>
Loading…
Cancel
Save