Create a dark theme for `SettingsActivity`.
[PrivacyBrowser.git] / app / src / main / java / com / stoutner / privacybrowser / fragments / DomainSettingsFragment.java
index aecf8e813d178466d25719821347677e3a9eb991..a231391c40ab0cffdf7bc8a268519b6a87da25a7 100644 (file)
@@ -1,5 +1,7 @@
 /*
- * Copyright 2017 Soren Stoutner <soren@stoutner.com>.
+ * Copyright © 2017 Soren Stoutner <soren@stoutner.com>.
+ *
+ * Huawei spinner code fix contributed 2017 Thomas Jensen <lianergoist@vongriffen.dk>.  Copyright assigned to Soren Stoutner <soren@stoutner.com>.
  *
  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
  *
@@ -22,6 +24,7 @@ package com.stoutner.privacybrowser.fragments;
 import android.annotation.SuppressLint;
 import android.content.Context;
 import android.database.Cursor;
+import android.os.Build;
 import android.os.Bundle;
 // We have to use `android.support.v4.app.Fragment` until minimum API >= 23.  Otherwise we cannot call `getContext()`.
 import android.support.v4.app.Fragment;
@@ -34,11 +37,13 @@ import android.widget.ArrayAdapter;
 import android.widget.CompoundButton;
 import android.widget.EditText;
 import android.widget.ImageView;
+import android.widget.LinearLayout;
 import android.widget.Spinner;
 import android.widget.Switch;
 import android.widget.TextView;
 
 import com.stoutner.privacybrowser.R;
+import com.stoutner.privacybrowser.activities.MainWebViewActivity;
 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
 
 public class DomainSettingsFragment extends Fragment {
@@ -72,9 +77,10 @@ public class DomainSettingsFragment extends Fragment {
         final ImageView javaScriptImageView = (ImageView) domainSettingsView.findViewById(R.id.domain_settings_javascript_imageview);
         Switch firstPartyCookiesEnabledSwitch = (Switch) domainSettingsView.findViewById(R.id.domain_settings_first_party_cookies_switch);
         final ImageView firstPartyCookiesImageView = (ImageView) domainSettingsView.findViewById(R.id.domain_settings_first_party_cookies_imageview);
-        Switch thirdPartyCookiesEnabledSwitch = (Switch) domainSettingsView.findViewById(R.id.domain_settings_third_party_cookies_switch);
+        LinearLayout thirdPartyCookiesLinearLayout = (LinearLayout) domainSettingsView.findViewById(R.id.domain_settings_third_party_cookies_linearlayout);
+        final Switch thirdPartyCookiesEnabledSwitch = (Switch) domainSettingsView.findViewById(R.id.domain_settings_third_party_cookies_switch);
         final ImageView thirdPartyCookiesImageView = (ImageView) domainSettingsView.findViewById(R.id.domain_settings_third_party_cookies_imageview);
-        Switch domStorageEnabledSwitch = (Switch) domainSettingsView.findViewById(R.id.domain_settings_dom_storage_switch);
+        final Switch domStorageEnabledSwitch = (Switch) domainSettingsView.findViewById(R.id.domain_settings_dom_storage_switch);
         final ImageView domStorageImageView = (ImageView) domainSettingsView.findViewById(R.id.domain_settings_dom_storage_imageview);
         Switch formDataEnabledSwitch = (Switch) domainSettingsView.findViewById(R.id.domain_settings_form_data_switch);
         final ImageView formDataImageView = (ImageView) domainSettingsView.findViewById(R.id.domain_settings_form_data_imageview);
@@ -82,6 +88,8 @@ public class DomainSettingsFragment extends Fragment {
         final TextView userAgentTextView = (TextView) domainSettingsView.findViewById(R.id.domain_settings_user_agent_textview);
         final EditText customUserAgentEditText = (EditText) domainSettingsView.findViewById(R.id.domain_settings_custom_user_agent_edittext);
         Spinner fontSizeSpinner = (Spinner) domainSettingsView.findViewById(R.id.domain_settings_font_size_spinner);
+        final ImageView displayWebpageImagesImageView = (ImageView) domainSettingsView.findViewById(R.id.domain_settings_display_webpage_images_imageview);
+        Spinner displayWebpageImagesSpinner = (Spinner) domainSettingsView.findViewById(R.id.domain_settings_display_webpage_images_spinner);
 
         // Initialize the database handler.  `this` specifies the context.  The two `nulls` do not specify the database name or a `CursorFactory`.
         // The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
@@ -100,20 +108,31 @@ public class DomainSettingsFragment extends Fragment {
         int formDataEnabledInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FORM_DATA));
         final String currentUserAgentString = domainCursor.getString(domainCursor.getColumnIndex(DomainsDatabaseHelper.USER_AGENT));
         int fontSizeInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.FONT_SIZE));
+        int displayImagesInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.DISPLAY_IMAGES));
 
         // Create `ArrayAdapters` for the `Spinners`and their `entry values`.
         ArrayAdapter<CharSequence> userAgentArrayAdapter = ArrayAdapter.createFromResource(context, R.array.user_agent_entries, android.R.layout.simple_spinner_item);
         final ArrayAdapter<CharSequence> userAgentEntryValuesArrayAdapter = ArrayAdapter.createFromResource(context, R.array.user_agent_entry_values, android.R.layout.simple_spinner_item);
         ArrayAdapter<CharSequence> fontSizeArrayAdapter = ArrayAdapter.createFromResource(context, R.array.default_font_size_entries, android.R.layout.simple_spinner_item);
         ArrayAdapter<CharSequence> fontSizeEntryValuesArrayAdapter = ArrayAdapter.createFromResource(context, R.array.default_font_size_entry_values, android.R.layout.simple_spinner_item);
-
-        // Set the drop down style for the `ArrayAdapters`.
-        userAgentArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
-        fontSizeArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
+        final ArrayAdapter<CharSequence> displayImagesArrayAdapter = ArrayAdapter.createFromResource(context, R.array.display_website_images_array, android.R.layout.simple_spinner_item);
+
+        // Some phones running Huawei's customized Android 7.0 have layout problems displaying a spinner with the default `simple_spinner_dropdown_item`.  The Huawei P9 Lite is known to be affected.
+        if (Build.BRAND.equals("HUAWEI") && (Build.VERSION.SDK_INT == 24)) {  // The device is manufactured by Huawei and is running Android 7.0.
+            // Use a customized `simple_spinner_dropdown_item`.  Huawei spinner code fix contributed 2017 Thomas Jensen <lianergoist@vongriffen.dk>.  Copyright assigned to Soren Stoutner <soren@stoutner.com>.
+            userAgentArrayAdapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item_huawei_fix);
+            fontSizeArrayAdapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item_huawei_fix);
+            displayImagesArrayAdapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item_huawei_fix);
+        } else {  // Use the standard `android.R.layout.simple_spinner_dropdown_item` on all other devices.
+            userAgentArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
+            fontSizeArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
+            displayImagesArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
+        }
 
         // Set the `ArrayAdapters` for the `Spinners`.
         userAgentSpinner.setAdapter(userAgentArrayAdapter);
         fontSizeSpinner.setAdapter(fontSizeArrayAdapter);
+        displayWebpageImagesSpinner.setAdapter(displayImagesArrayAdapter);
 
         // Set the domain name from the the database cursor.
         domainNameEditText.setText(domainNameString);
@@ -127,40 +146,74 @@ public class DomainSettingsFragment extends Fragment {
             javaScriptImageView.setImageDrawable(getResources().getDrawable(R.drawable.privacy_mode));
         }
 
-        // Set the first-party cookies status.
+        // Set the first-party cookies status.  Once minimum API >= 21 we can use a selector as the tint mode instead of specifying different icons.
         if (firstPartyCookiesEnabledInt == 1) {  // First-party cookies are enabled.
             firstPartyCookiesEnabledSwitch.setChecked(true);
-            firstPartyCookiesImageView.setEnabled(true);
+            firstPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_enabled));
         } else {  // First-party cookies are disabled.
             firstPartyCookiesEnabledSwitch.setChecked(false);
-            firstPartyCookiesImageView.setEnabled(false);
+            firstPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_disabled_light));
         }
 
-        // Set the third-party cookies status.
-        if (thirdPartyCookiesEnabledInt == 1) {  // Third-party cookies are enabled.
-            thirdPartyCookiesEnabledSwitch.setChecked(true);
-            thirdPartyCookiesImageView.setEnabled(true);
-        } else {  // Third-party cookies are disabled.
-            thirdPartyCookiesEnabledSwitch.setChecked(false);
-            thirdPartyCookiesImageView.setEnabled(false);
+        // Only display third-party cookies if SDK_INT >= 21.
+        if (Build.VERSION.SDK_INT >= 21) {  // Third-party cookies can be configured for API >= 21.
+            // Only enable third-party-cookies if first-party cookies are enabled.
+            if (firstPartyCookiesEnabledInt == 1) {  // First-party cookies are enabled.
+                // Set the third-party cookies status.  Once minimum API >= 21 we can use a selector as the tint mode instead of specifying different icons.
+                if (thirdPartyCookiesEnabledInt == 1) {  // Both first-party and third-party cookies are enabled.
+                    thirdPartyCookiesEnabledSwitch.setChecked(true);
+                    thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_warning));
+                } else {  // First party cookies are enabled but third-party cookies are disabled.
+                    thirdPartyCookiesEnabledSwitch.setChecked(false);
+                    thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_disabled_light));
+                }
+            } else {  // First-party cookies are disabled.
+                // Set the status of third-party cookies, but disable it.
+                if (thirdPartyCookiesEnabledInt == 1) {  // Third-party cookies are enabled but first-party cookies are disabled.
+                    thirdPartyCookiesEnabledSwitch.setChecked(true);
+                    thirdPartyCookiesEnabledSwitch.setEnabled(false);
+                    thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_ghosted_light));
+                } else {  // Both first party and third-party cookies are disabled.
+                    thirdPartyCookiesEnabledSwitch.setChecked(false);
+                    thirdPartyCookiesEnabledSwitch.setEnabled(false);
+                    thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_ghosted_light));
+                }
+            }
+        } else {  // Third-party cookies cannot be configured for API <= 21.
+            // Hide the `LinearLayout` for third-party cookies.
+            thirdPartyCookiesLinearLayout.setVisibility(View.GONE);
         }
 
-        // Set the DOM storage status.
-        if (domStorageEnabledInt == 1) {  // DOM storage is enabled.
-            domStorageEnabledSwitch.setChecked(true);
-            domStorageImageView.setEnabled(true);
-        } else {  // Dom storage is disabled.
-            domStorageEnabledSwitch.setChecked(false);
-            domStorageImageView.setEnabled(false);
+        // Only enable DOM storage if JavaScript is enabled.
+        if (javaScriptEnabledInt == 1) {  // JavaScript is enabled.
+            // Set the DOM storage status.  Once minimum API >= 21 we can use a selector as the tint mode instead of specifying different icons.
+            if (domStorageEnabledInt == 1) {  // Both JavaScript and DOM storage are enabled.
+                domStorageEnabledSwitch.setChecked(true);
+                domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_enabled));
+            } else {  // JavaScript is enabled but DOM storage is disabled.
+                domStorageEnabledSwitch.setChecked(false);
+                domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_disabled_light));
+            }
+        } else {  // JavaScript is disabled.
+            // Set the checked status of DOM storage.
+            if (domStorageEnabledInt == 1) {  // DOM storage is enabled but JavaScript is disabled.
+                domStorageEnabledSwitch.setChecked(true);
+            } else {  // Both JavaScript and DOM storage are disabled.
+                domStorageEnabledSwitch.setChecked(false);
+            }
+
+            // Disable `domStorageEnabledSwitch` and set the icon to be ghosted.
+            domStorageEnabledSwitch.setEnabled(false);
+            domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_ghosted_light));
         }
 
-        // Set the form data status.
+        // Set the form data status.  Once minimum API >= 21 we can use a selector as the tint mode instead of specifying different icons.
         if (formDataEnabledInt == 1) {  // Form data is enabled.
             formDataEnabledSwitch.setChecked(true);
-            formDataImageView.setEnabled(true);
+            formDataImageView.setImageDrawable(getResources().getDrawable(R.drawable.form_data_enabled));
         } else {  // Form data is disabled.
             formDataEnabledSwitch.setChecked(false);
-            formDataImageView.setEnabled(false);
+            formDataImageView.setImageDrawable(getResources().getDrawable(R.drawable.form_data_disabled_light));
         }
 
         // We need to inflated a `WebView` to get the default user agent.
@@ -209,15 +262,55 @@ public class DomainSettingsFragment extends Fragment {
         int fontSizeArrayPosition = fontSizeEntryValuesArrayAdapter.getPosition(String.valueOf(fontSizeInt));
         fontSizeSpinner.setSelection(fontSizeArrayPosition);
 
+        // Set the selected display website images mode.
+        displayWebpageImagesSpinner.setSelection(displayImagesInt);
+
+        // Set the display website images icon.
+        switch (displayImagesInt) {
+            case DomainsDatabaseHelper.DISPLAY_WEBPAGE_IMAGES_SYSTEM_DEFAULT:
+                if (MainWebViewActivity.displayWebpageImagesBoolean) {
+                    displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_enabled_light));
+                } else {
+                    displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_disabled_light));
+                }
+                break;
+
+            case DomainsDatabaseHelper.DISPLAY_WEBPAGE_IMAGES_ENABLED:
+                displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_enabled_light));
+                break;
+
+            case DomainsDatabaseHelper.DISPLAY_WEBPAGE_IMAGES_DISABLED:
+                displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_disabled_light));
+                break;
+        }
+
+
         // Set the `javaScriptEnabledSwitch` `OnCheckedChangeListener()`.
         javaScriptEnabledSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
             @Override
             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
-                // Update the icon.
-                if (isChecked) {
+                if (isChecked) {  // JavaScript is enabled.
+                    // Update the JavaScript icon.
                     javaScriptImageView.setImageDrawable(getResources().getDrawable(R.drawable.javascript_enabled));
-                } else {
+
+                    // Enable the DOM storage `Switch`.
+                    domStorageEnabledSwitch.setEnabled(true);
+
+                    // Update the DOM storage icon.
+                    if (domStorageEnabledSwitch.isChecked()) {  // DOM storage is enabled.
+                        domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_enabled));
+                    } else {  // DOM storage is disabled.
+                        domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_disabled_light));
+                    }
+                } else {  // JavaScript is disabled.
+                    // Update the JavaScript icon.
                     javaScriptImageView.setImageDrawable(getResources().getDrawable(R.drawable.privacy_mode));
+
+                    // Disable the DOM storage `Switch`.
+                    domStorageEnabledSwitch.setEnabled(false);
+
+                    // Set the DOM storage icon to be ghosted.
+                    domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_ghosted_light));
                 }
             }
         });
@@ -226,8 +319,29 @@ public class DomainSettingsFragment extends Fragment {
         firstPartyCookiesEnabledSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
             @Override
             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
-                // Update the icon.
-                firstPartyCookiesImageView.setEnabled(isChecked);
+                if (isChecked) {  // First-party cookies are enabled.
+                    // Update the first-party cookies icon.
+                    firstPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_enabled));
+
+                    // Enable the third-party cookies `Switch`.
+                    thirdPartyCookiesEnabledSwitch.setEnabled(true);
+
+                    // Update the third-party cookies icon.
+                    if (thirdPartyCookiesEnabledSwitch.isChecked()) {  // Third-party cookies are enabled.
+                        thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_warning));
+                    } else {  // Third-party cookies are disabled.
+                        thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_disabled_light));
+                    }
+                } else {  // First-party cookies are disabled.
+                    // Update the first-party cookies icon.
+                    firstPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_disabled_light));
+
+                    // Disable the third-party cookies `Switch`.
+                    thirdPartyCookiesEnabledSwitch.setEnabled(false);
+
+                    // Set the third-party cookies icon to be ghosted.
+                    thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_ghosted_light));
+                }
             }
         });
 
@@ -236,7 +350,11 @@ public class DomainSettingsFragment extends Fragment {
             @Override
             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                 // Update the icon.
-                thirdPartyCookiesImageView.setEnabled(isChecked);
+                if (isChecked) {
+                    thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_warning));
+                } else {
+                    thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_disabled_light));
+                }
             }
         });
 
@@ -245,7 +363,11 @@ public class DomainSettingsFragment extends Fragment {
             @Override
             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                 // Update the icon.
-                domStorageImageView.setEnabled(isChecked);
+                if (isChecked) {
+                    domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_enabled));
+                } else {
+                    domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_disabled_light));
+                }
             }
         });
 
@@ -254,7 +376,11 @@ public class DomainSettingsFragment extends Fragment {
             @Override
             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                 // Update the icon.
-                formDataImageView.setEnabled(isChecked);
+                if (isChecked) {
+                    formDataImageView.setImageDrawable(getResources().getDrawable(R.drawable.form_data_enabled));
+                } else {
+                    formDataImageView.setImageDrawable(getResources().getDrawable(R.drawable.form_data_disabled_light));
+                }
             }
         });
 
@@ -302,6 +428,36 @@ public class DomainSettingsFragment extends Fragment {
             }
         });
 
+        // Set the `displayImagesSwitch` `onItemClickListener()`.
+        displayWebpageImagesSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
+            @Override
+            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
+                // Update the icon.
+                switch (position) {
+                    case DomainsDatabaseHelper.DISPLAY_WEBPAGE_IMAGES_SYSTEM_DEFAULT:
+                        if (MainWebViewActivity.displayWebpageImagesBoolean) {
+                            displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_enabled_light));
+                        } else {
+                            displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_disabled_light));
+                        }
+                        break;
+
+                    case DomainsDatabaseHelper.DISPLAY_WEBPAGE_IMAGES_ENABLED:
+                        displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_enabled_light));
+                        break;
+
+                    case DomainsDatabaseHelper.DISPLAY_WEBPAGE_IMAGES_DISABLED:
+                        displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_disabled_light));
+                        break;
+                }
+            }
+
+            @Override
+            public void onNothingSelected(AdapterView<?> parent) {
+                // Do nothing.
+            }
+        });
+
         return domainSettingsView;
     }
 }