From 98a82a9c4c67a85f53f019bfa793988bc5fa0336 Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Wed, 27 Jan 2016 16:44:43 -0700 Subject: [PATCH] Create Cookie menu options. --- .../com/stoutner/privacybrowser/Webview.java | 67 +++++++++++++++++-- app/src/main/res/menu/menu_webview.xml | 13 ++++ app/src/main/res/values/strings.xml | 2 + 3 files changed, 76 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/stoutner/privacybrowser/Webview.java b/app/src/main/java/com/stoutner/privacybrowser/Webview.java index 154353f7..6295ce52 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/Webview.java +++ b/app/src/main/java/com/stoutner/privacybrowser/Webview.java @@ -41,6 +41,7 @@ import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.inputmethod.InputMethodManager; +import android.webkit.CookieManager; import android.webkit.DownloadListener; import android.webkit.WebChromeClient; import android.webkit.WebView; @@ -75,6 +76,11 @@ public class Webview extends AppCompatActivity implements CreateHomeScreenShortc private boolean enableSaveFormData; */ + // cookieManager is used in onCreate and onOptionsItemSelected. + private CookieManager cookieManager; + //enableCookies is used in onCreate, onCreateOptionsMenu, and onOptionsItemSelected. + private boolean enableCookies; + // actionBar is used in onCreate and onOptionsItemSelected. private ActionBar actionBar; @@ -275,6 +281,11 @@ public class Webview extends AppCompatActivity implements CreateHomeScreenShortc mainWebView.getSettings().setSaveFormData(enableSaveFormData); */ + // Set Cookies initial status. + cookieManager = CookieManager.getInstance(); + enableCookies = true; + cookieManager.setAcceptCookie(enableCookies); + // Get the intent information that started the app. final Intent intent = getIntent(); @@ -297,22 +308,43 @@ public class Webview extends AppCompatActivity implements CreateHomeScreenShortc public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_webview, menu); - MenuItem toggleJavaScriptMenuItem = menu.findItem(R.id.toggleJavaScript); - MenuItem toggleDomStorageMenuItem = menu.findItem(R.id.toggleDomStorage); + + // Get MenuItems for checkable menu items. + MenuItem toggleJavaScript = menu.findItem(R.id.toggleJavaScript); + MenuItem toggleDomStorage = menu.findItem(R.id.toggleDomStorage); /* toggleSaveFormData does nothing until database storage is implemented. - MenuItem toggleSaveFormDataMenuItem = menu.findItem(R.id.toggleSaveFormData); + MenuItem toggleSaveFormData = menu.findItem(R.id.toggleSaveFormData); */ + MenuItem toggleCookies = menu.findItem(R.id.toggleCookies); + MenuItem clearCookies = menu.findItem(R.id.clearCookies); // Set the initial status of the menu item checkboxes. - toggleJavaScriptMenuItem.setChecked(enableJavaScript); - toggleDomStorageMenuItem.setChecked(enableDomStorage); + toggleJavaScript.setChecked(enableJavaScript); + toggleDomStorage.setChecked(enableDomStorage); /* toggleSaveFormData does nothing until database storage is implemented. - toggleSaveFormDataMenuItem.setChecked(enableSaveFormData); + toggleSaveFormData.setChecked(enableSaveFormData); */ + toggleCookies.setChecked(enableCookies); + + // Disable Clear Cookies if there are none. + clearCookies.setEnabled(cookieManager.hasCookies()); return true; } + @Override + public boolean onPrepareOptionsMenu(Menu menu) { + // Enable Clear Cookies if there are any. + MenuItem clearCookies = menu.findItem(R.id.clearCookies); + clearCookies.setEnabled(cookieManager.hasCookies()); + + // Run all the other default commands. + super.onPrepareOptionsMenu(menu); + + // Return true displays the menu. + return true; + } + @Override // @TargetApi(11) turns off the errors regarding copy and paste, which are removed from view in menu_webview.xml for lower version of Android. @TargetApi(11) @@ -368,6 +400,29 @@ public class Webview extends AppCompatActivity implements CreateHomeScreenShortc return true; */ + case R.id.toggleCookies: + if (enableCookies) { + enableCookies = false; + menuItem.setChecked(false); + cookieManager.setAcceptCookie(false); + mainWebView.reload(); + } else { + enableCookies = true; + menuItem.setChecked(true); + cookieManager.setAcceptCookie(true); + mainWebView.reload(); + } + return true; + + case R.id.clearCookies: + if (Build.VERSION.SDK_INT < 21) { + cookieManager.removeAllCookie(); + } else { + cookieManager.removeAllCookies(null); + } + Toast.makeText(getApplicationContext(), "Cookies deleted", Toast.LENGTH_SHORT).show(); + return true; + case R.id.home: mainWebView.loadUrl(homepage); return true; diff --git a/app/src/main/res/menu/menu_webview.xml b/app/src/main/res/menu/menu_webview.xml index d6010645..18130e28 100644 --- a/app/src/main/res/menu/menu_webview.xml +++ b/app/src/main/res/menu/menu_webview.xml @@ -46,6 +46,19 @@ app:showAsAction="never" /> --> + + + + JavaScript DOM Storage Save Form Data + Cookies + Clear Cookies Home Refresh Back -- 2.45.2