]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/MainWebView.java
Remove copy and paste menu items (they are now handled by SupportActionBar) and creat...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / MainWebView.java
index 3582df9f9f9aa6364b7590eb9e12c8fe48f93a47..daaa58a809d384b4fb01ae7414d6d38bc271a376 100644 (file)
@@ -27,14 +27,17 @@ import android.content.ClipData;
 import android.content.ClipboardManager;
 import android.content.Context;
 import android.content.Intent;
+import android.content.SharedPreferences;
 import android.graphics.Bitmap;
 import android.net.Uri;
 import android.os.Build;
 import android.os.Bundle;
+import android.preference.PreferenceManager;
 import android.support.v4.app.DialogFragment;
 import android.support.v7.app.ActionBar;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.app.AppCompatDialogFragment;
+import android.support.v7.widget.Toolbar;
 import android.util.Patterns;
 import android.view.KeyEvent;
 import android.view.Menu;
@@ -57,33 +60,36 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLEncoder;
 
+// We need to use AppCompatActivity from android.support.v7.app.AppCompatActivity to have access to the SupportActionBar until the minimum API is >= 21.
 public class MainWebView extends AppCompatActivity implements CreateHomeScreenShortcut.CreateHomeScreenSchortcutListener {
     // favoriteIcon is public static so it can be accessed from CreateHomeScreenShortcut.
     public static Bitmap favoriteIcon;
-    // mainWebView is public static so it can be accessed from AboutDialog.  It is also used in onCreate and onOptionsItemSelected.
+    // mainWebView is public static so it can be accessed from AboutDialog.  It is also used in onCreate(), onOptionsItemSelected(), and loadUrlFromTextBox().
     public static WebView mainWebView;
 
-    // formattedUrlString is used in onCreate, onOptionsItemSelected, onCreateHomeScreenShortcutCreate, and loadUrlFromTextBox.
+    // mainMenu is used in onCreateOptionsMenu() and onOptionsItemSelected().
+    private Menu mainMenu;
+    // formattedUrlString is used in onCreate(), onOptionsItemSelected(), onCreateHomeScreenShortcutCreate(), and loadUrlFromTextBox().
     private String formattedUrlString;
-    // homepage is used in onCreate and onOptionsItemSelected.
-    private String homepage = "https://www.duckduckgo.com/";
-    // enableJavaScript is used in onCreate, onCreateOptionsMenu, and onOptionsItemSelected.
-    private boolean enableJavaScript;
-    // enableDomStorage is used in onCreate, onCreateOptionsMenu, and onOptionsItemSelected.
-    private boolean enableDomStorage;
-
-    /*  enableSaveFormData does nothing until database storage is implemented.
-    // enableSaveFormData is used in onCreate, onCreateOptionsMenu, and onOptionsItemSelected.
-    private boolean enableSaveFormData;
+    // homepage is used in onCreate() and onOptionsItemSelected().
+    private String homepage;
+    // javaScriptEnabled is used in onCreate(), onCreateOptionsMenu(), onOptionsItemSelected(), and loadUrlFromTextBox().
+    private boolean javaScriptEnabled;
+    // domStorageEnabled is used in onCreate(), onCreateOptionsMenu(), and onOptionsItemSelected().
+    private boolean domStorageEnabled;
+
+    /* saveFormDataEnabled does nothing until database storage is implemented.
+    // saveFormDataEnabled is used in onCreate(), onCreateOptionsMenu(), and onOptionsItemSelected().
+    private boolean saveFormDataEnabled;
     */
 
-    // cookieManager is used in onCreate and onOptionsItemSelected.
+    // cookieManager is used in onCreate() and onOptionsItemSelected().
     private CookieManager cookieManager;
-    // enableCookies is used in onCreate, onCreateOptionsMenu, and onOptionsItemSelected.
-    private boolean enableCookies;
+    // cookiesEnabled is used in onCreate(), onCreateOptionsMenu(), and onOptionsItemSelected().
+    private boolean cookiesEnabled;
 
-    // actionBar is used in onCreate and onOptionsItemSelected.
-    private ActionBar actionBar;
+    // urlTextBox is used in onCreate(), onOptionsItemSelected(), and loadUrlFromTextBox().
+    private EditText urlTextBox;
 
     @Override
     // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled.
@@ -91,23 +97,26 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_webview);
+        Toolbar toolbar = (Toolbar) findViewById(R.id.appBar);
+        setSupportActionBar(toolbar);
 
         final FrameLayout fullScreenVideoFrameLayout = (FrameLayout) findViewById(R.id.fullScreenVideoFrameLayout);
         final Activity mainWebViewActivity = this;
+        // We need to use the SupportActionBar from android.support.v7.app.ActionBar until the minimum API is >= 21.
+        final ActionBar appBar = getSupportActionBar();
 
         mainWebView = (WebView) findViewById(R.id.mainWebView);
-        actionBar = getSupportActionBar();
 
-        if (actionBar != null) {
-            // Remove the title from the action bar.
-            actionBar.setDisplayShowTitleEnabled(false);
+        if (appBar != null) {
+            // Remove the title from the app bar.
+            appBar.setDisplayShowTitleEnabled(false);
 
-            // Add the custom app_bar layout, which shows the favoriteIcon, urlTextBar, and progressBar.
-            actionBar.setCustomView(R.layout.app_bar);
-            actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
+            // Add the custom url_bar layout, which shows the favoriteIcon, urlTextBar, and progressBar.
+            appBar.setCustomView(R.layout.url_bar);
+            appBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
 
             // Set the "go" button on the keyboard to load the URL in urlTextBox.
-            EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox);
+            urlTextBox = (EditText) appBar.getCustomView().findViewById(R.id.urlTextBox);
             urlTextBox.setOnKeyListener(new View.OnKeyListener() {
                 public boolean onKey(View v, int keyCode, KeyEvent event) {
                     // If the event is a key-down event on the "enter" button, load the URL.
@@ -145,21 +154,14 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh
             // Update the URL in urlTextBox when the page starts to load.
             @Override
             public void onPageStarted(WebView view, String url, Bitmap favicon) {
-                if (actionBar != null) {
-                    EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox);
-                    urlTextBox.setText(url);
-                }
+                urlTextBox.setText(url);
             }
 
             // Update formattedUrlString and urlTextBox.  It is necessary to do this after the page finishes loading because the final URL can change during load.
             @Override
             public void onPageFinished(WebView view, String url) {
                 formattedUrlString = url;
-
-                if (actionBar != null) {
-                    EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox);
-                    urlTextBox.setText(formattedUrlString);
-                }
+                urlTextBox.setText(formattedUrlString);
             }
         });
 
@@ -167,9 +169,9 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh
             // Update the progress bar when a page is loading.
             @Override
             public void onProgressChanged(WebView view, int progress) {
-                // Make sure that actionBar is not null.
-                if (actionBar != null) {
-                    ProgressBar progressBar = (ProgressBar) actionBar.getCustomView().findViewById(R.id.progressBar);
+                // Make sure that appBar is not null.
+                if (appBar != null) {
+                    ProgressBar progressBar = (ProgressBar) appBar.getCustomView().findViewById(R.id.progressBar);
                     progressBar.setProgress(progress);
                     if (progress < 100) {
                         progressBar.setVisibility(View.VISIBLE);
@@ -185,9 +187,9 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh
                 // Save a copy of the favorite icon for use if a shortcut is added to the home screen.
                 favoriteIcon = icon;
 
-                // Place the favorite icon in the actionBar if it is not null.
-                if (actionBar != null) {
-                    ImageView imageViewFavoriteIcon = (ImageView) actionBar.getCustomView().findViewById(R.id.favoriteIcon);
+                // Place the favorite icon in the appBar if it is not null.
+                if (appBar != null) {
+                    ImageView imageViewFavoriteIcon = (ImageView) appBar.getCustomView().findViewById(R.id.favoriteIcon);
                     imageViewFavoriteIcon.setImageBitmap(Bitmap.createScaledBitmap(icon, 64, 64, true));
                 }
             }
@@ -195,8 +197,8 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh
             // Enter full screen video
             @Override
             public void onShowCustomView(View view, CustomViewCallback callback) {
-                if (getSupportActionBar() != null) {
-                    getSupportActionBar().hide();
+                if (appBar != null) {
+                    appBar.hide();
                 }
 
                 fullScreenVideoFrameLayout.addView(view);
@@ -227,8 +229,8 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh
 
             // Exit full screen video
             public void onHideCustomView() {
-                if (getSupportActionBar() != null) {
-                    getSupportActionBar().show();
+                if (appBar != null) {
+                    appBar.show();
                 }
 
                 mainWebView.setVisibility(View.VISIBLE);
@@ -267,24 +269,33 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh
             mainWebView.getSettings().setDisplayZoomControls(false);
         }
 
+        // Initialize the default preference values the first time the program is run.
+        PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
+
+        // Get the shared preference values.
+        SharedPreferences savedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
+
         // Set JavaScript initial status.
-        enableJavaScript = false;
-        mainWebView.getSettings().setJavaScriptEnabled(enableJavaScript);
+        javaScriptEnabled = savedPreferences.getBoolean("javascript_enabled", false);
+        mainWebView.getSettings().setJavaScriptEnabled(javaScriptEnabled);
 
-        // Set DOM Storage initial status.
-        enableDomStorage = false;
-        mainWebView.getSettings().setDomStorageEnabled(enableDomStorage);
+        // Set DOM storage initial status.
+        domStorageEnabled = savedPreferences.getBoolean("dom_storage_enabled", false);
+        mainWebView.getSettings().setDomStorageEnabled(domStorageEnabled);
 
         /* Save Form Data does nothing until database storage is implemented.
         // Set Save Form Data initial status.
-        enableSaveFormData = true;
-        mainWebView.getSettings().setSaveFormData(enableSaveFormData);
+        saveFormDataEnabled = true;
+        mainWebView.getSettings().setSaveFormData(saveFormDataEnabled);
         */
 
-        // Set Cookies initial status.
-        enableCookies = false;
+        // Set cookies initial status.
+        cookiesEnabled = savedPreferences.getBoolean("cookies_enabled", false);
         cookieManager = CookieManager.getInstance();
-        cookieManager.setAcceptCookie(enableCookies);
+        cookieManager.setAcceptCookie(cookiesEnabled);
+
+        // Set hompage initial status.
+        homepage = savedPreferences.getString("homepage", "https://www.duckduckgo.com");
 
         // Get the intent information that started the app.
         final Intent intent = getIntent();
@@ -306,7 +317,7 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh
 
     @Override
     protected void onNewIntent(Intent intent) {
-        // Sets the new intent as the activity intent, so that any future getIntent() picks up this one.
+        // Sets the new intent as the activity intent, so that any future getIntent()s pick up this one instead of creating a new activity.
         setIntent(intent);
 
         if (intent.getData() != null) {
@@ -324,6 +335,9 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh
         // Inflate the menu; this adds items to the action bar if it is present.
         getMenuInflater().inflate(R.menu.menu_webview, menu);
 
+        // Set mainMenu so it can be used by onOptionsItemSelected.
+        mainMenu = menu;
+
         // Get MenuItems for checkable menu items.
         MenuItem toggleJavaScript = menu.findItem(R.id.toggleJavaScript);
         MenuItem toggleDomStorage = menu.findItem(R.id.toggleDomStorage);
@@ -333,18 +347,22 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh
         MenuItem toggleCookies = menu.findItem(R.id.toggleCookies);
 
         // Set the initial icon for toggleJavaScript
-        if (enableJavaScript) {
-            toggleJavaScript.setIcon(R.drawable.javascript_on);
+        if (javaScriptEnabled) {
+            toggleJavaScript.setIcon(R.drawable.javascript_enabled);
         } else {
-            toggleJavaScript.setIcon(R.drawable.javascript_off);
+            if (domStorageEnabled || cookiesEnabled) {
+                toggleJavaScript.setIcon(R.drawable.warning);
+            } else {
+                toggleJavaScript.setIcon(R.drawable.privacy_mode);
+            }
         }
 
         // Set the initial status of the menu item checkboxes.
-        toggleDomStorage.setChecked(enableDomStorage);
+        toggleDomStorage.setChecked(domStorageEnabled);
         /* toggleSaveFormData does nothing until database storage is implemented.
-        toggleSaveFormData.setChecked(enableSaveFormData);
+        toggleSaveFormData.setChecked(saveFormDataEnabled);
         */
-        toggleCookies.setChecked(enableCookies);
+        toggleCookies.setChecked(cookiesEnabled);
 
         return true;
     }
@@ -379,49 +397,84 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh
     @SuppressWarnings("deprecation")
     public boolean onOptionsItemSelected(MenuItem menuItem) {
         int menuItemId = menuItem.getItemId();
-        ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
 
-        // Sets the commands that relate to the menu entries.
+        // Some options need to update the drawable for toggleJavaScript.
+        MenuItem toggleJavaScript = mainMenu.findItem(R.id.toggleJavaScript);
+
+        // Set the commands that relate to the menu entries.
         switch (menuItemId) {
             case R.id.toggleJavaScript:
-                if (enableJavaScript) {
-                    enableJavaScript = false;
-                    menuItem.setIcon(R.drawable.javascript_off);
+                if (javaScriptEnabled) {
+                    javaScriptEnabled = false;
                     mainWebView.getSettings().setJavaScriptEnabled(false);
                     mainWebView.reload();
-                    Toast.makeText(getApplicationContext(), "JavaScript Disabled", Toast.LENGTH_SHORT).show();
+
+                    // Update the toggleJavaScript icon and display a toast message.
+                    if (domStorageEnabled || cookiesEnabled) {
+                        menuItem.setIcon(R.drawable.warning);
+                        if (domStorageEnabled && cookiesEnabled) {
+                            Toast.makeText(getApplicationContext(), "JavaScript disabled, DOM Storage and Cookies still enabled", Toast.LENGTH_SHORT).show();
+                        } else {
+                            if (domStorageEnabled) {
+                                Toast.makeText(getApplicationContext(), "JavaScript disabled, DOM Storage still enabled", Toast.LENGTH_SHORT).show();
+                            } else {
+                                Toast.makeText(getApplicationContext(), "JavaScript disabled, Cookies still enabled", Toast.LENGTH_SHORT).show();
+                            }
+                        }
+                    } else {
+                        menuItem.setIcon(R.drawable.privacy_mode);
+                        Toast.makeText(getApplicationContext(), "Privacy Mode", Toast.LENGTH_SHORT).show();
+                    }
                 } else {
-                    enableJavaScript = true;
-                    menuItem.setIcon(R.drawable.javascript_on);
+                    javaScriptEnabled = true;
+                    menuItem.setIcon(R.drawable.javascript_enabled);
                     mainWebView.getSettings().setJavaScriptEnabled(true);
                     mainWebView.reload();
-                    Toast.makeText(getApplicationContext(), "JavaScript Enabled", Toast.LENGTH_SHORT).show();
+                    Toast.makeText(getApplicationContext(), "JavaScript enabled", Toast.LENGTH_SHORT).show();
                 }
                 return true;
 
             case R.id.toggleDomStorage:
-                if (enableDomStorage) {
-                    enableDomStorage = false;
+                if (domStorageEnabled) {
+                    domStorageEnabled = false;
                     menuItem.setChecked(false);
                     mainWebView.getSettings().setDomStorageEnabled(false);
                     mainWebView.reload();
+
+                    // Update the toggleJavaScript icon and display a toast message if appropriate.
+                    if (!javaScriptEnabled && !cookiesEnabled) {
+                        toggleJavaScript.setIcon(R.drawable.privacy_mode);
+                        Toast.makeText(getApplicationContext(), "Privacy Mode", Toast.LENGTH_SHORT).show();
+                    } else {
+                        if (cookiesEnabled) {
+                            toggleJavaScript.setIcon(R.drawable.warning);
+                            Toast.makeText(getApplicationContext(), "Cookies still enabled", Toast.LENGTH_SHORT).show();
+                        } // Else Do nothing because JavaScript is enabled.
+                    }
                 } else {
-                    enableDomStorage = true;
+                    domStorageEnabled = true;
                     menuItem.setChecked(true);
                     mainWebView.getSettings().setDomStorageEnabled(true);
                     mainWebView.reload();
+
+                    // Update the toggleJavaScript icon if appropriate.
+                    if (!javaScriptEnabled) {
+                        toggleJavaScript.setIcon(R.drawable.warning);
+                    } // Else Do nothing because JavaScript is enabled.
+
+                    Toast.makeText(getApplicationContext(), "DOM Storage enabled", Toast.LENGTH_SHORT).show();
                 }
                 return true;
 
             /* toggleSaveFormData does nothing until database storage is implemented.
             case R.id.toggleSaveFormData:
-                if (enableSaveFormData) {
-                    enableSaveFormData = false;
+                if (saveFormDataEnabled) {
+                    saveFormDataEnabled = false;
                     menuItem.setChecked(false);
                     mainWebView.getSettings().setSaveFormData(false);
                     mainWebView.reload();
                 } else {
-                    enableSaveFormData = true;
+                    saveFormDataEnabled = true;
                     menuItem.setChecked(true);
                     mainWebView.getSettings().setSaveFormData(true);
                     mainWebView.reload();
@@ -430,16 +483,34 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh
             */
 
             case R.id.toggleCookies:
-                if (enableCookies) {
-                    enableCookies = false;
+                if (cookiesEnabled) {
+                    cookiesEnabled = false;
                     menuItem.setChecked(false);
                     cookieManager.setAcceptCookie(false);
                     mainWebView.reload();
+
+                    // Update the toggleJavaScript icon and display a toast message if appropriate.
+                    if (!javaScriptEnabled && !domStorageEnabled) {
+                        toggleJavaScript.setIcon(R.drawable.privacy_mode);
+                        Toast.makeText(getApplicationContext(), "Privacy Mode", Toast.LENGTH_SHORT).show();
+                    } else {
+                        if (domStorageEnabled) {
+                            toggleJavaScript.setIcon(R.drawable.warning);
+                            Toast.makeText(getApplicationContext(), "DOM Storage still enabled", Toast.LENGTH_SHORT).show();
+                        } // Else Do nothing because JavaScript is enabled.
+                    }
                 } else {
-                    enableCookies = true;
+                    cookiesEnabled = true;
                     menuItem.setChecked(true);
                     cookieManager.setAcceptCookie(true);
                     mainWebView.reload();
+
+                    // Update the toggleJavaScript icon if appropriate.
+                    if (!javaScriptEnabled) {
+                        toggleJavaScript.setIcon(R.drawable.warning);
+                    } // Else Do nothing because JavaScript is enabled.
+
+                    Toast.makeText(getApplicationContext(), "Cookies enabled", Toast.LENGTH_SHORT).show();
                 }
                 return true;
 
@@ -458,6 +529,24 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh
                 Toast.makeText(getApplicationContext(), "Cookies deleted", Toast.LENGTH_SHORT).show();
                 return true;
 
+            case R.id.addToHomescreen:
+                // Show the CreateHomeScreenShortcut AlertDialog and name this instance createShortcut.
+                AppCompatDialogFragment shortcutDialog = new CreateHomeScreenShortcut();
+                shortcutDialog.show(getSupportFragmentManager(), "createShortcut");
+
+                //Everything else will be handled by CreateHomeScreenShortcut and the associated listeners below.
+                return true;
+
+            case R.id.downloads:
+                // Launch the system Download Manager.
+                Intent downloadManangerIntent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
+
+                // Launch as a new task so that Download Manager and Privacy Browser show as separate windows in the recent tasks list.
+                downloadManangerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+
+                startActivity(downloadManangerIntent);
+                return true;
+
             case R.id.home:
                 mainWebView.loadUrl(homepage);
                 return true;
@@ -474,56 +563,18 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh
                 mainWebView.goForward();
                 return true;
 
-            case R.id.copyURL:
-                // Make sure that actionBar is not null.
-                if (actionBar != null) {
-                    EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox);
-                    clipboard.setPrimaryClip(ClipData.newPlainText("URL", urlTextBox.getText()));
-                }
-                return true;
-
-            case R.id.pasteURL:
-                // Make sure that actionBar is not null.
-                if (actionBar != null) {
-                    ClipData.Item clipboardData = clipboard.getPrimaryClip().getItemAt(0);
-                    EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox);
-                    urlTextBox.setText(clipboardData.coerceToText(this));
-                    try {
-                        loadUrlFromTextBox();
-                    } catch (UnsupportedEncodingException e) {
-                        e.printStackTrace();
-                    }
-                }
-                return true;
-
-            case R.id.shareURL:
-                // Make sure that actionBar is not null.
-                if (actionBar != null) {
-                    EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox);
-                    Intent shareIntent = new Intent();
-                    shareIntent.setAction(Intent.ACTION_SEND);
-                    shareIntent.putExtra(Intent.EXTRA_TEXT, urlTextBox.getText().toString());
-                    shareIntent.setType("text/plain");
-                    startActivity(Intent.createChooser(shareIntent, "Share URL"));
-                }
-                return true;
-
-            case R.id.addToHomescreen:
-                // Show the CreateHomeScreenShortcut AlertDialog and name this instance createShortcut.
-                AppCompatDialogFragment shortcutDialog = new CreateHomeScreenShortcut();
-                shortcutDialog.show(getSupportFragmentManager(), "createShortcut");
-
-                //Everything else will be handled by CreateHomeScreenShortcut and the associated listeners below.
+            case R.id.share:
+                Intent shareIntent = new Intent();
+                shareIntent.setAction(Intent.ACTION_SEND);
+                shareIntent.putExtra(Intent.EXTRA_TEXT, urlTextBox.getText().toString());
+                shareIntent.setType("text/plain");
+                startActivity(Intent.createChooser(shareIntent, "Share URL"));
                 return true;
 
-            case R.id.downloads:
-                // Launch the system Download Manager.
-                Intent downloadManangerIntent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
-
-                // Launch as a new task so that Download Manager and Privacy Browser show as separate windows in the recent tasks list.
-                downloadManangerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-
-                startActivity(downloadManangerIntent);
+            case R.id.settings:
+                // Start the Settings activity.
+                Intent intent = new Intent(this, Settings.class);
+                startActivity(intent);
                 return true;
 
             case R.id.about:
@@ -593,53 +644,50 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh
     }
 
     public void loadUrlFromTextBox() throws UnsupportedEncodingException {
-        // Make sure that actionBar is not null.
-        ActionBar actionBar = getSupportActionBar();
-        if (actionBar != null) {
-            final WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
-            EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox);
-
-            // Get the text from urlTextInput and convert it to a string.
-            String unformattedUrlString = urlTextBox.getText().toString();
-            URL unformattedUrl = null;
-            Uri.Builder formattedUri = new Uri.Builder();
-
-            // Check to see if unformattedUrlString is a valid URL.  Otherwise, convert it into a Duck Duck Go search.
-            if (Patterns.WEB_URL.matcher(unformattedUrlString).matches()) {
-
-                // Add http:// at the beginning if it is missing.  Otherwise the app will segfault.
-                if (!unformattedUrlString.startsWith("http")) {
-                    unformattedUrlString = "http://" + unformattedUrlString;
-                }
+        // Get the text from urlTextBox and convert it to a string.
+        String unformattedUrlString = urlTextBox.getText().toString();
+        URL unformattedUrl = null;
+        Uri.Builder formattedUri = new Uri.Builder();
+
+        // Check to see if unformattedUrlString is a valid URL.  Otherwise, convert it into a Duck Duck Go search.
+        if (Patterns.WEB_URL.matcher(unformattedUrlString).matches()) {
+            // Add http:// at the beginning if it is missing.  Otherwise the app will segfault.
+            if (!unformattedUrlString.startsWith("http")) {
+                unformattedUrlString = "http://" + unformattedUrlString;
+            }
 
-                // Convert unformattedUrlString to a URL, then to a URI, and then back to a string, which sanitizes the input and adds in any missing components.
-                try {
-                    unformattedUrl = new URL(unformattedUrlString);
-                } catch (MalformedURLException e) {
-                    e.printStackTrace();
-                }
+            // Convert unformattedUrlString to a URL, then to a URI, and then back to a string, which sanitizes the input and adds in any missing components.
+            try {
+                unformattedUrl = new URL(unformattedUrlString);
+            } catch (MalformedURLException e) {
+                e.printStackTrace();
+            }
 
-                // The ternary operator (? :) makes sure that a null pointer exception is not thrown, which would happen if .get was called on a null value.
-                final String scheme = unformattedUrl != null ? unformattedUrl.getProtocol() : null;
-                final String authority = unformattedUrl != null ? unformattedUrl.getAuthority() : null;
-                final String path = unformattedUrl != null ? unformattedUrl.getPath() : null;
-                final String query = unformattedUrl != null ? unformattedUrl.getQuery() : null;
-                final String fragment = unformattedUrl != null ? unformattedUrl.getRef() : null;
+            // The ternary operator (? :) makes sure that a null pointer exception is not thrown, which would happen if .get was called on a null value.
+            final String scheme = unformattedUrl != null ? unformattedUrl.getProtocol() : null;
+            final String authority = unformattedUrl != null ? unformattedUrl.getAuthority() : null;
+            final String path = unformattedUrl != null ? unformattedUrl.getPath() : null;
+            final String query = unformattedUrl != null ? unformattedUrl.getQuery() : null;
+            final String fragment = unformattedUrl != null ? unformattedUrl.getRef() : null;
 
-                formattedUri.scheme(scheme).authority(authority).path(path).query(query).fragment(fragment);
-                formattedUrlString = formattedUri.build().toString();
+            formattedUri.scheme(scheme).authority(authority).path(path).query(query).fragment(fragment);
+            formattedUrlString = formattedUri.build().toString();
+        } else {
+            // Sanitize the search input and convert it to a DuckDuckGo search.
+            final String encodedUrlString = URLEncoder.encode(unformattedUrlString, "UTF-8");
 
-            } else {
-                // Sanitize the search input and convert it to a DuckDuckGo search.
-                final String encodedUrlString = URLEncoder.encode(unformattedUrlString, "UTF-8");
+            // Use the correct search URL based on javaScriptEnabled.
+            if (javaScriptEnabled) {
                 formattedUrlString = "https://duckduckgo.com/?q=" + encodedUrlString;
+            } else {
+                formattedUrlString = "https://duckduckgo.com/html/?q=" + encodedUrlString;
             }
+        }
 
-            mainWebView.loadUrl(formattedUrlString);
+        mainWebView.loadUrl(formattedUrlString);
 
-            // Hides the keyboard so we can see the webpage.
-            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
-            inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0);
-        }
+        // Hides the keyboard so we can see the webpage.
+        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
+        inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0);
     }
-}
\ No newline at end of file
+}