]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java
Add an `Undo` callback to the `Snackbars` for deleting cookies, DOM storage, and...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / activities / MainWebViewActivity.java
index 107a9cee6a83d538ff49b724f39f51f3da3b139a..f21cae48e51812b8f606a520d45bd2f0049ccb2b 100644 (file)
@@ -948,12 +948,11 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         // Set the display webpage images mode.
         setDisplayWebpageImages();
 
-        // Only reload `mainWebView` if not loading a new intent.
-        if (!loadingNewIntentBoolean) {
+        // Only reload `mainWebView` if not loading a new intent and not waiting for Orbot.
+        if (!loadingNewIntentBoolean && !waitingForOrbot) {
             // Reload the webpage to remove images if `setDisplayWebpageImages` has turned them off.
             mainWebView.reload();
-        } else {
-            // Reset `loadingNewIntentBoolean`.
+        } else if (loadingNewIntentBoolean) {  // Reset `loadingNewIntentBoolean` if this run comes from a new intent.
             loadingNewIntentBoolean = false;
         }
     }
@@ -1259,24 +1258,91 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 return true;
 
             case R.id.clear_cookies:
-                if (Build.VERSION.SDK_INT < 21) {
-                    cookieManager.removeAllCookie();
-                } else {
-                    cookieManager.removeAllCookies(null);
-                }
-                Snackbar.make(findViewById(R.id.main_webview), R.string.cookies_deleted, Snackbar.LENGTH_SHORT).show();
+                Snackbar.make(findViewById(R.id.main_webview), R.string.cookies_deleted, Snackbar.LENGTH_LONG)
+                        .setAction(R.string.undo, new View.OnClickListener() {
+                            @Override
+                            public void onClick(View v) {
+                                // Do nothing because everything will be handled by `onDismissed()` below.
+                            }
+                        })
+                        .addCallback(new Snackbar.Callback() {
+                            @Override
+                            public void onDismissed(Snackbar snackbar, int event) {
+                                switch (event) {
+                                    // The user pushed the `Undo` button.
+                                    case Snackbar.Callback.DISMISS_EVENT_ACTION:
+                                        // Do nothing.
+                                        break;
+
+                                    // The `Snackbar` was dismissed without the `Undo` button being pushed.
+                                    default:
+                                        // `cookieManager.removeAllCookie()` varies by SDK.
+                                        if (Build.VERSION.SDK_INT < 21) {
+                                            cookieManager.removeAllCookie();
+                                        } else {
+                                            // `null` indicates no callback.
+                                            cookieManager.removeAllCookies(null);
+                                        }
+                                }
+                            }
+                        })
+                        .show();
                 return true;
 
             case R.id.clear_dom_storage:
-                WebStorage webStorage = WebStorage.getInstance();
-                webStorage.deleteAllData();
-                Snackbar.make(findViewById(R.id.main_webview), R.string.dom_storage_deleted, Snackbar.LENGTH_SHORT).show();
+                Snackbar.make(findViewById(R.id.main_webview), R.string.dom_storage_deleted, Snackbar.LENGTH_LONG)
+                        .setAction(R.string.undo, new View.OnClickListener() {
+                            @Override
+                            public void onClick(View v) {
+                                // Do nothing because everything will be handled by `onDismissed()` below.
+                            }
+                        })
+                        .addCallback(new Snackbar.Callback() {
+                            @Override
+                            public void onDismissed(Snackbar snackbar, int event) {
+                                switch (event) {
+                                    // The user pushed the `Undo` button.
+                                    case Snackbar.Callback.DISMISS_EVENT_ACTION:
+                                        // Do nothing.
+                                        break;
+
+                                    // The `Snackbar` was dismissed without the `Undo` button being pushed.
+                                    default:
+                                        // Delete the DOM Storage.
+                                        WebStorage webStorage = WebStorage.getInstance();
+                                        webStorage.deleteAllData();
+                                }
+                            }
+                        })
+                        .show();
                 return true;
 
             case R.id.clear_form_data:
-                WebViewDatabase mainWebViewDatabase = WebViewDatabase.getInstance(this);
-                mainWebViewDatabase.clearFormData();
-                Snackbar.make(findViewById(R.id.main_webview), R.string.form_data_deleted, Snackbar.LENGTH_SHORT).show();
+                Snackbar.make(findViewById(R.id.main_webview), R.string.form_data_deleted, Snackbar.LENGTH_LONG)
+                        .setAction(R.string.undo, new View.OnClickListener() {
+                            @Override
+                            public void onClick(View v) {
+                                // Do nothing because everything will be handled by `onDismissed()` below.
+                            }
+                        })
+                        .addCallback(new Snackbar.Callback() {
+                            @Override
+                            public void onDismissed(Snackbar snackbar, int event) {
+                                switch (event) {
+                                    // The user pushed the `Undo` button.
+                                    case Snackbar.Callback.DISMISS_EVENT_ACTION:
+                                        // Do nothing.
+                                        break;
+
+                                    // The `Snackbar` was dismissed without the `Undo` button being pushed.
+                                    default:
+                                        // Delete the form data.
+                                        WebViewDatabase mainWebViewDatabase = WebViewDatabase.getInstance(getApplicationContext());
+                                        mainWebViewDatabase.clearFormData();
+                                }
+                            }
+                        })
+                        .show();
                 return true;
 
             case R.id.font_size_twenty_five_percent:
@@ -1786,6 +1852,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
             DownloadManager.Request downloadRequest = new DownloadManager.Request(Uri.parse(imageUrl));
 
             // Pass cookies to download manager if cookies are enabled.  This is required to download images from websites that require a login.
+            // Code contributed 2017 Hendrik Knackstedt.  Copyright assigned to Soren Stoutner <soren@stoutner.com>.
             if (firstPartyCookiesEnabled) {
                 // Get the cookies for `imageUrl`.
                 String cookies = cookieManager.getCookie(imageUrl);
@@ -1833,6 +1900,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
             DownloadManager.Request downloadRequest = new DownloadManager.Request(Uri.parse(downloadUrl));
 
             // Pass cookies to download manager if cookies are enabled.  This is required to download files from websites that require a login.
+            // Code contributed 2017 Hendrik Knackstedt.  Copyright assigned to Soren Stoutner <soren@stoutner.com>.
             if (firstPartyCookiesEnabled) {
                 // Get the cookies for `downloadUrl`.
                 String cookies = cookieManager.getCookie(downloadUrl);