]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java
Add controls for form data. Fixes https://redmine.stoutner.com/issues/29
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / MainWebViewActivity.java
index 993c363b338ee4b5986b3c7f607f66ad911ca72b..66fe512e549bf60eeaa4b89efb761aed46c369dd 100644 (file)
@@ -53,6 +53,7 @@ import android.webkit.WebChromeClient;
 import android.webkit.WebStorage;
 import android.webkit.WebView;
 import android.webkit.WebViewClient;
+import android.webkit.WebViewDatabase;
 import android.widget.EditText;
 import android.widget.FrameLayout;
 import android.widget.ImageView;
@@ -82,6 +83,8 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
     public static boolean thirdPartyCookiesEnabled;
     // domStorageEnabled is public static so it can be accessed from SettingsFragment.  It is also used in onCreate(), onCreateOptionsMenu(), and onOptionsItemSelected().
     public static boolean domStorageEnabled;
+    // saveFormDataEnabled is public static so it can be accessed from SettingsFragment.  It is also used in onCreate(), onCreateOptionsMenu(), and onOptionsItemSelected().
+    public static boolean saveFormDataEnabled;
     // javaScriptDisabledSearchURL is public static so it can be accessed from SettingsFragment.  It is also used in onCreate() and loadURLFromTextBox().
     public static String javaScriptDisabledSearchURL;
     // javaScriptEnabledSearchURL is public static so it can be accessed from SettingsFragment.  It is also used in onCreate() and loadURLFromTextBox().
@@ -247,19 +250,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                  * SYSTEM_UI_FLAG_FULLSCREEN hides the status bar across the top of the screen.
                  * SYSTEM_UI_FLAG_IMMERSIVE_STICKY makes the navigation and status bars ghosted overlays and automatically rehides them.
                  */
-
-                // Set the one flag supported by API >= 14.
-                view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
-
-                // Set the two flags that are supported by API >= 16.
-                if (Build.VERSION.SDK_INT >= 16) {
-                    view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN);
-                }
-
-                // Set all three flags that are supported by API >= 19.
-                if (Build.VERSION.SDK_INT >= 19) {
-                    view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
-                }
+                view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
             }
 
             // Exit full screen video
@@ -333,6 +324,10 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         domStorageEnabled = savedPreferences.getBoolean("dom_storage_enabled", false);
         mainWebView.getSettings().setDomStorageEnabled(domStorageEnabled);
 
+        // Set the saved form data initial status.  The default is false.
+        saveFormDataEnabled = savedPreferences.getBoolean("save_form_data_enabled", false);
+        mainWebView.getSettings().setSaveFormData(saveFormDataEnabled);
+
         // Set the user agent initial status.
         String userAgentString = savedPreferences.getString("user_agent", "Default user agent");
         switch (userAgentString) {
@@ -439,6 +434,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         MenuItem toggleFirstPartyCookies = menu.findItem(R.id.toggleFirstPartyCookies);
         MenuItem toggleThirdPartyCookies = menu.findItem(R.id.toggleThirdPartyCookies);
         MenuItem toggleDomStorage = menu.findItem(R.id.toggleDomStorage);
+        MenuItem toggleSaveFormData = menu.findItem(R.id.toggleSaveFormData);
 
         // Set the initial status of the privacy icon.
         updatePrivacyIcon();
@@ -447,6 +443,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         toggleFirstPartyCookies.setChecked(firstPartyCookiesEnabled);
         toggleThirdPartyCookies.setChecked(thirdPartyCookiesEnabled);
         toggleDomStorage.setChecked(domStorageEnabled);
+        toggleSaveFormData.setChecked(saveFormDataEnabled);
 
         return true;
     }
@@ -461,13 +458,18 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
             toggleThirdPartyCookies.setEnabled(false);
         }
 
+        // Enable DOM Storage if JavaScript is enabled.
+        MenuItem toggleDomStorage = menu.findItem(R.id.toggleDomStorage);
+        toggleDomStorage.setEnabled(javaScriptEnabled);
+
         // Enable Clear Cookies if there are any.
         MenuItem clearCookies = menu.findItem(R.id.clearCookies);
         clearCookies.setEnabled(cookieManager.hasCookies());
 
-        // Enable DOM Storage if JavaScript is enabled.
-        MenuItem toggleDomStorage = menu.findItem(R.id.toggleDomStorage);
-        toggleDomStorage.setEnabled(javaScriptEnabled);
+        // Enable Clear Form Data is there is any.
+        MenuItem clearFormData = menu.findItem(R.id.clearFormData);
+        WebViewDatabase mainWebViewDatabase = WebViewDatabase.getInstance(this);
+        clearFormData.setEnabled(mainWebViewDatabase.hasFormData());
 
         // Run all the other default commands.
         super.onPrepareOptionsMenu(menu);
@@ -558,6 +560,20 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 mainWebView.reload();
                 return true;
 
+            case R.id.toggleSaveFormData:
+                // Switch the status of saveFormDataEnabled.
+                saveFormDataEnabled = !saveFormDataEnabled;
+
+                // Update the menu checkbox.
+                menuItem.setChecked(saveFormDataEnabled);
+
+                // Apply the new form data status.
+                mainWebView.getSettings().setSaveFormData(saveFormDataEnabled);
+
+                // Reload the WebView.
+                mainWebView.reload();
+                return true;
+
             case R.id.clearCookies:
                 if (Build.VERSION.SDK_INT < 21) {
                     cookieManager.removeAllCookie();
@@ -573,6 +589,12 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 Snackbar.make(findViewById(R.id.mainWebView), R.string.dom_storage_deleted, Snackbar.LENGTH_SHORT).show();
                 return true;
 
+            case R.id.clearFormData:
+                WebViewDatabase mainWebViewDatabase = WebViewDatabase.getInstance(this);
+                mainWebViewDatabase.clearFormData();
+                mainWebView.reload();
+                return true;
+
             case R.id.share:
                 Intent shareIntent = new Intent();
                 shareIntent.setAction(Intent.ACTION_SEND);
@@ -768,8 +790,9 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
     }
 
     private void loadUrlFromTextBox() throws UnsupportedEncodingException {
-        // Get the text from urlTextBox and convert it to a string.
-        String unformattedUrlString = urlTextBox.getText().toString();
+        // Get the text from urlTextBox and convert it to a string.  trim() removes white spaces from the beginning and end of the string.
+        String unformattedUrlString = urlTextBox.getText().toString().trim();
+
         URL unformattedUrl = null;
         Uri.Builder formattedUri = new Uri.Builder();