]> 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 d4b134747642db01fd4958838186fb8ea1aab366..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,16 +60,19 @@ 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(), onOptionsItemSelected(), and loadUrlFromTextBox().
     public static WebView mainWebView;
 
+    // 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/";
+    private String homepage;
     // javaScriptEnabled is used in onCreate(), onCreateOptionsMenu(), onOptionsItemSelected(), and loadUrlFromTextBox().
     private boolean javaScriptEnabled;
     // domStorageEnabled is used in onCreate(), onCreateOptionsMenu(), and onOptionsItemSelected().
@@ -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;
-        final ActionBar actionBar = getSupportActionBar();
+        // 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);
 
-        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.
-            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.
@@ -160,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);
@@ -178,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));
                 }
             }
@@ -188,8 +197,8 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh
             // Enter full screen video
             @Override
             public void onShowCustomView(View view, CustomViewCallback callback) {
-                if (actionBar != null) {
-                    actionBar.hide();
+                if (appBar != null) {
+                    appBar.hide();
                 }
 
                 fullScreenVideoFrameLayout.addView(view);
@@ -220,8 +229,8 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh
 
             // Exit full screen video
             public void onHideCustomView() {
-                if (actionBar != null) {
-                    actionBar.show();
+                if (appBar != null) {
+                    appBar.show();
                 }
 
                 mainWebView.setVisibility(View.VISIBLE);
@@ -260,12 +269,18 @@ 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.
-        javaScriptEnabled = false;
+        javaScriptEnabled = savedPreferences.getBoolean("javascript_enabled", false);
         mainWebView.getSettings().setJavaScriptEnabled(javaScriptEnabled);
 
-        // Set DOM Storage initial status.
-        domStorageEnabled = false;
+        // 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.
@@ -274,11 +289,14 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh
         mainWebView.getSettings().setSaveFormData(saveFormDataEnabled);
         */
 
-        // Set Cookies initial status.
-        cookiesEnabled = false;
+        // Set cookies initial status.
+        cookiesEnabled = savedPreferences.getBoolean("cookies_enabled", false);
         cookieManager = CookieManager.getInstance();
         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();
 
@@ -299,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) {
@@ -317,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);
@@ -327,9 +348,13 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh
 
         // Set the initial icon for toggleJavaScript
         if (javaScriptEnabled) {
-            toggleJavaScript.setIcon(R.drawable.javascript_on);
+            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.
@@ -372,23 +397,40 @@ 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 (javaScriptEnabled) {
                     javaScriptEnabled = false;
-                    menuItem.setIcon(R.drawable.javascript_off);
                     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 {
                     javaScriptEnabled = true;
-                    menuItem.setIcon(R.drawable.javascript_on);
+                    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;
 
@@ -398,11 +440,29 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh
                     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 {
                     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;
 
@@ -428,11 +488,29 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh
                     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 {
                     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;
 
@@ -451,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;
@@ -467,21 +563,7 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh
                 mainWebView.goForward();
                 return true;
 
-            case R.id.copyURL:
-                clipboard.setPrimaryClip(ClipData.newPlainText("URL", urlTextBox.getText()));
-                return true;
-
-            case R.id.pasteURL:
-                ClipData.Item clipboardData = clipboard.getPrimaryClip().getItemAt(0);
-                urlTextBox.setText(clipboardData.coerceToText(this));
-                    try {
-                        loadUrlFromTextBox();
-                    } catch (UnsupportedEncodingException e) {
-                        e.printStackTrace();
-                    }
-                return true;
-
-            case R.id.shareURL:
+            case R.id.share:
                 Intent shareIntent = new Intent();
                 shareIntent.setAction(Intent.ACTION_SEND);
                 shareIntent.putExtra(Intent.EXTRA_TEXT, urlTextBox.getText().toString());
@@ -489,22 +571,10 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh
                 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.
-                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: