]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/commitdiff
Create Cookie menu options.
authorSoren Stoutner <soren@stoutner.com>
Wed, 27 Jan 2016 23:44:43 +0000 (16:44 -0700)
committerSoren Stoutner <soren@stoutner.com>
Wed, 27 Jan 2016 23:44:43 +0000 (16:44 -0700)
app/src/main/java/com/stoutner/privacybrowser/Webview.java
app/src/main/res/menu/menu_webview.xml
app/src/main/res/values/strings.xml

index 154353f7ff7b120c5c46538481894e4ddcae8004..6295ce52a79683d86c8d372f2fab0332232f9528 100644 (file)
@@ -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;
index d6010645c4676c34359e394a50cf1aba468b8d25..18130e28e52088cea8eb2704216bc9acc4979862 100644 (file)
         app:showAsAction="never" />
         -->
 
+    <item
+        android:id="@+id/toggleCookies"
+        android:title="@string/cookies"
+        android:orderInCategory="4"
+        android:checkable="true"
+        app:showAsAction="never" />
+
+    <item
+        android:id="@+id/clearCookies"
+        android:title="@string/clearCookies"
+        android:orderInCategory="5"
+        app:showAsAction="never" />
+
     <item
         android:id="@+id/home"
         android:title="@string/home"
index aa9d518732da210b0ca04f80383844fe4b29c67c..348063830ce463100ddb095726f76f6f0030505b 100644 (file)
@@ -27,6 +27,8 @@
     <string name="javaScript">JavaScript</string>
     <string name="domStorage">DOM Storage</string>
     <string name="saveFormData">Save Form Data</string>
+    <string name="cookies">Cookies</string>
+    <string name="clearCookies">Clear Cookies</string>
     <string name="home">Home</string>
     <string name="refresh">Refresh</string>
     <string name="back">Back</string>