]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/dialogs/ViewRequestDialog.kt
First wrong button text in View Headers in night theme. https://redmine.stoutner...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / ViewRequestDialog.kt
index 54e997af4507e7885e1f9a64c72a8635be4c87b7..1c8648ea142e64d7bc5a5cdabc0b75e379c6c769 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2018-2022 Soren Stoutner <soren@stoutner.com>.
+ * Copyright 2018-2023 Soren Stoutner <soren@stoutner.com>.
  *
  * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
  *
@@ -22,18 +22,49 @@ package com.stoutner.privacybrowser.dialogs
 import android.app.Dialog
 import android.content.Context
 import android.content.DialogInterface
-import android.content.res.Configuration
 import android.os.Bundle
 import android.view.View
 import android.view.WindowManager
 import android.widget.TextView
 
 import androidx.appcompat.app.AlertDialog
+import androidx.core.content.ContextCompat.getColor
 import androidx.fragment.app.DialogFragment
 import androidx.preference.PreferenceManager
 
 import com.stoutner.privacybrowser.R
-import com.stoutner.privacybrowser.helpers.BlocklistHelper
+import com.stoutner.privacybrowser.helpers.DOMAIN_ALLOWLIST
+import com.stoutner.privacybrowser.helpers.DOMAIN_BLOCKLIST
+import com.stoutner.privacybrowser.helpers.DOMAIN_FINAL_ALLOWLIST
+import com.stoutner.privacybrowser.helpers.DOMAIN_FINAL_BLOCKLIST
+import com.stoutner.privacybrowser.helpers.DOMAIN_INITIAL_ALLOWLIST
+import com.stoutner.privacybrowser.helpers.DOMAIN_INITIAL_BLOCKLIST
+import com.stoutner.privacybrowser.helpers.DOMAIN_REGULAR_EXPRESSION_BLOCKLIST
+import com.stoutner.privacybrowser.helpers.INITIAL_BLOCKLIST
+import com.stoutner.privacybrowser.helpers.REQUEST_ALLOWED
+import com.stoutner.privacybrowser.helpers.REQUEST_BLOCKED
+import com.stoutner.privacybrowser.helpers.REQUEST_BLOCKLIST
+import com.stoutner.privacybrowser.helpers.REQUEST_BLOCKLIST_ENTRIES
+import com.stoutner.privacybrowser.helpers.REQUEST_BLOCKLIST_ORIGINAL_ENTRY
+import com.stoutner.privacybrowser.helpers.REQUEST_DEFAULT
+import com.stoutner.privacybrowser.helpers.REQUEST_DISPOSITION
+import com.stoutner.privacybrowser.helpers.FINAL_ALLOWLIST
+import com.stoutner.privacybrowser.helpers.FINAL_BLOCKLIST
+import com.stoutner.privacybrowser.helpers.MAIN_ALLOWLIST
+import com.stoutner.privacybrowser.helpers.MAIN_BLOCKLIST
+import com.stoutner.privacybrowser.helpers.REGULAR_EXPRESSION_BLOCKLIST
+import com.stoutner.privacybrowser.helpers.REQUEST_SUBLIST
+import com.stoutner.privacybrowser.helpers.REQUEST_THIRD_PARTY
+import com.stoutner.privacybrowser.helpers.REQUEST_URL
+import com.stoutner.privacybrowser.helpers.THIRD_PARTY_BLOCKLIST
+import com.stoutner.privacybrowser.helpers.THIRD_PARTY_DOMAIN_BLOCKLIST
+import com.stoutner.privacybrowser.helpers.THIRD_PARTY_DOMAIN_INITIAL_ALLOWLIST
+import com.stoutner.privacybrowser.helpers.THIRD_PARTY_DOMAIN_INITIAL_BLOCKLIST
+import com.stoutner.privacybrowser.helpers.THIRD_PARTY_DOMAIN_REGULAR_EXPRESSION_BLOCKLIST
+import com.stoutner.privacybrowser.helpers.THIRD_PARTY_DOMAIN_ALLOWLIST
+import com.stoutner.privacybrowser.helpers.THIRD_PARTY_INITIAL_BLOCKLIST
+import com.stoutner.privacybrowser.helpers.THIRD_PARTY_ALLOWLIST
+import com.stoutner.privacybrowser.helpers.THIRD_PARTY_REGULAR_EXPRESSION_BLOCKLIST
 
 // Define the class constants.
 private const val ID = "id"
@@ -41,29 +72,7 @@ private const val IS_LAST_REQUEST = "is_last_request"
 private const val REQUEST_DETAILS = "request_details"
 
 class ViewRequestDialog : DialogFragment() {
-    // Define the class variables.
-    private lateinit var viewRequestListener: ViewRequestListener
-
-    // The public interface is used to send information back to the parent activity.
-    interface ViewRequestListener {
-        // Show the previous request.
-        fun onPrevious(currentId: Int)
-
-        // Show the next request.
-        fun onNext(currentId: Int)
-    }
-
-    override fun onAttach(context: Context) {
-        // Run the default commands.
-        super.onAttach(context)
-
-        // Get a handle for the listener from the launching context.
-        viewRequestListener = context as ViewRequestListener
-    }
-
     companion object {
-        // `@JvmStatic` will no longer be required once all the code has transitioned to Kotlin.
-        @JvmStatic
         fun request(id: Int, isLastRequest: Boolean, requestDetails: Array<String>): ViewRequestDialog {
             // Create a bundle.
             val bundle = Bundle()
@@ -84,6 +93,26 @@ class ViewRequestDialog : DialogFragment() {
         }
     }
 
+    // Define the class variables.
+    private lateinit var viewRequestListener: ViewRequestListener
+
+    // The public interface is used to send information back to the parent activity.
+    interface ViewRequestListener {
+        // Show the previous request.
+        fun onPrevious(currentId: Int)
+
+        // Show the next request.
+        fun onNext(currentId: Int)
+    }
+
+    override fun onAttach(context: Context) {
+        // Run the default commands.
+        super.onAttach(context)
+
+        // Get a handle for the listener from the launching context.
+        viewRequestListener = context as ViewRequestListener
+    }
+
     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
         // Get the arguments from the bundle.
         val id = requireArguments().getInt(ID)
@@ -93,9 +122,6 @@ class ViewRequestDialog : DialogFragment() {
         // Use an alert dialog builder to create the alert dialog.
         val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
 
-        // Get the current theme status.
-        val currentThemeStatus = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
-
         // Set the icon.
         dialogBuilder.setIcon(R.drawable.block_ads_enabled)
 
@@ -141,14 +167,14 @@ class ViewRequestDialog : DialogFragment() {
         // Get handles for the dialog views.
         val requestDisposition = alertDialog.findViewById<TextView>(R.id.request_disposition)!!
         val requestUrl = alertDialog.findViewById<TextView>(R.id.request_url)!!
-        val requestBlockListLabel = alertDialog.findViewById<TextView>(R.id.request_blocklist_label)!!
-        val requestBlockList = alertDialog.findViewById<TextView>(R.id.request_blocklist)!!
+        val requestFilterListLabel = alertDialog.findViewById<TextView>(R.id.request_filterlist_label)!!
+        val requestFilterList = alertDialog.findViewById<TextView>(R.id.request_filterlist)!!
         val requestSubListLabel = alertDialog.findViewById<TextView>(R.id.request_sublist_label)!!
         val requestSubList = alertDialog.findViewById<TextView>(R.id.request_sublist)!!
-        val requestBlockListEntriesLabel = alertDialog.findViewById<TextView>(R.id.request_blocklist_entries_label)!!
-        val requestBlockListEntries = alertDialog.findViewById<TextView>(R.id.request_blocklist_entries)!!
-        val requestBlockListOriginalEntryLabel = alertDialog.findViewById<TextView>(R.id.request_blocklist_original_entry_label)!!
-        val requestBlockListOriginalEntry = alertDialog.findViewById<TextView>(R.id.request_blocklist_original_entry)!!
+        val requestFilterListEntriesLabel = alertDialog.findViewById<TextView>(R.id.request_filterlist_entries_label)!!
+        val requestFilterListEntries = alertDialog.findViewById<TextView>(R.id.request_filterlist_entries)!!
+        val requestFilterListOriginalEntryLabel = alertDialog.findViewById<TextView>(R.id.request_filterlist_original_entry_label)!!
+        val requestFilterListOriginalEntry = alertDialog.findViewById<TextView>(R.id.request_filterlist_original_entry)!!
         val previousButton = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE)
         val nextButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE)
 
@@ -159,104 +185,86 @@ class ViewRequestDialog : DialogFragment() {
         nextButton.isEnabled = !isLastRequest
 
         // Set the request action text.
-        when (requestDetails[BlocklistHelper.REQUEST_DISPOSITION]) {
-            BlocklistHelper.REQUEST_DEFAULT -> {
+        when (requestDetails[REQUEST_DISPOSITION]) {
+            REQUEST_DEFAULT -> {
                 // Set the text.
                 requestDisposition.setText(R.string.default_allowed)
 
-                // Set the background color.  The deprecated `getColor()` must be used until the minimum API >= 23.
-                @Suppress("DEPRECATION")
-                requestDisposition.setBackgroundColor(resources.getColor(R.color.transparent))
+                // Set the background color to be transparent.
+                requestDisposition.setBackgroundColor(getColor(requireContext(), R.color.transparent))
             }
 
-            BlocklistHelper.REQUEST_ALLOWED -> {
+            REQUEST_ALLOWED -> {
                 // Set the text.
                 requestDisposition.setText(R.string.allowed)
 
-                // Set the background color according to the theme.  The deprecated `getColor()` must be used until the minimum API >= 23.
-                if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
-                    @Suppress("DEPRECATION")
-                    requestDisposition.setBackgroundColor(resources.getColor(R.color.blue_100))
-                } else {
-                    @Suppress("DEPRECATION")
-                    requestDisposition.setBackgroundColor(resources.getColor(R.color.blue_700_50))
-                }
+                // Set the background color to be blue.
+                requestDisposition.setBackgroundColor(getColor(requireContext(), R.color.requests_blue_background))
             }
 
-            BlocklistHelper.REQUEST_THIRD_PARTY -> {
+            REQUEST_THIRD_PARTY -> {
                 // Set the text.
                 requestDisposition.setText(R.string.third_party_blocked)
 
-                // Set the background color according to the theme.  The deprecated `getColor()` must be used until the minimum API >= 23.
-                if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
-                    @Suppress("DEPRECATION")
-                    requestDisposition.setBackgroundColor(resources.getColor(R.color.yellow_100))
-                } else {
-                    @Suppress("DEPRECATION")
-                    requestDisposition.setBackgroundColor(resources.getColor(R.color.yellow_700_50))
-                }
+                // Set the background color to be yellow.
+                requestDisposition.setBackgroundColor(getColor(requireContext(), R.color.yellow_background))
             }
-            BlocklistHelper.REQUEST_BLOCKED -> {
+
+            REQUEST_BLOCKED -> {
                 // Set the text.
                 requestDisposition.setText(R.string.blocked)
 
-                // Set the background color according to the theme.  The deprecated `getColor()` must be used until the minimum API >= 23.
-                if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
-                    @Suppress("DEPRECATION")
-                    requestDisposition.setBackgroundColor(resources.getColor(R.color.red_100))
-                } else {
-                    @Suppress("DEPRECATION")
-                    requestDisposition.setBackgroundColor(resources.getColor(R.color.red_700_40))
-                }
+                // Set the background color to be red.
+                requestDisposition.setBackgroundColor(getColor(requireContext(), R.color.red_background))
             }
         }
 
         // Display the request URL.
-        requestUrl.text = requestDetails[BlocklistHelper.REQUEST_URL]
+        requestUrl.text = requestDetails[REQUEST_URL]
 
         // Modify the dialog based on the request action.
         if (requestDetails.size == 2) {  // A default request.
             // Hide the unused views.
-            requestBlockListLabel.visibility = View.GONE
-            requestBlockList.visibility = View.GONE
+            requestFilterListLabel.visibility = View.GONE
+            requestFilterList.visibility = View.GONE
             requestSubListLabel.visibility = View.GONE
             requestSubList.visibility = View.GONE
-            requestBlockListEntriesLabel.visibility = View.GONE
-            requestBlockListEntries.visibility = View.GONE
-            requestBlockListOriginalEntryLabel.visibility = View.GONE
-            requestBlockListOriginalEntry.visibility = View.GONE
+            requestFilterListEntriesLabel.visibility = View.GONE
+            requestFilterListEntries.visibility = View.GONE
+            requestFilterListOriginalEntryLabel.visibility = View.GONE
+            requestFilterListOriginalEntry.visibility = View.GONE
         } else {  // A blocked or allowed request.
             // Set the text on the text views.
-            requestBlockList.text = requestDetails[BlocklistHelper.REQUEST_BLOCKLIST]
-            requestBlockListEntries.text = requestDetails[BlocklistHelper.REQUEST_BLOCKLIST_ENTRIES]
-            requestBlockListOriginalEntry.text = requestDetails[BlocklistHelper.REQUEST_BLOCKLIST_ORIGINAL_ENTRY]
-            when (requestDetails[BlocklistHelper.REQUEST_SUBLIST]) {
-                BlocklistHelper.MAIN_WHITELIST -> requestSubList.setText(R.string.main_whitelist)
-                BlocklistHelper.FINAL_WHITELIST -> requestSubList.setText(R.string.final_whitelist)
-                BlocklistHelper.DOMAIN_WHITELIST -> requestSubList.setText(R.string.domain_whitelist)
-                BlocklistHelper.DOMAIN_INITIAL_WHITELIST -> requestSubList.setText(R.string.domain_initial_whitelist)
-                BlocklistHelper.DOMAIN_FINAL_WHITELIST -> requestSubList.setText(R.string.domain_final_whitelist)
-                BlocklistHelper.THIRD_PARTY_WHITELIST -> requestSubList.setText(R.string.third_party_whitelist)
-                BlocklistHelper.THIRD_PARTY_DOMAIN_WHITELIST -> requestSubList.setText(R.string.third_party_domain_whitelist)
-                BlocklistHelper.THIRD_PARTY_DOMAIN_INITIAL_WHITELIST -> requestSubList.setText(R.string.third_party_domain_initial_whitelist)
-                BlocklistHelper.MAIN_BLACKLIST -> requestSubList.setText(R.string.main_blacklist)
-                BlocklistHelper.INITIAL_BLACKLIST -> requestSubList.setText(R.string.initial_blacklist)
-                BlocklistHelper.FINAL_BLACKLIST -> requestSubList.setText(R.string.final_blacklist)
-                BlocklistHelper.DOMAIN_BLACKLIST -> requestSubList.setText(R.string.domain_blacklist)
-                BlocklistHelper.DOMAIN_INITIAL_BLACKLIST -> requestSubList.setText(R.string.domain_initial_blacklist)
-                BlocklistHelper.DOMAIN_FINAL_BLACKLIST -> requestSubList.setText(R.string.domain_final_blacklist)
-                BlocklistHelper.DOMAIN_REGULAR_EXPRESSION_BLACKLIST -> requestSubList.setText(R.string.domain_regular_expression_blacklist)
-                BlocklistHelper.THIRD_PARTY_BLACKLIST -> requestSubList.setText(R.string.third_party_blacklist)
-                BlocklistHelper.THIRD_PARTY_INITIAL_BLACKLIST -> requestSubList.setText(R.string.third_party_initial_blacklist)
-                BlocklistHelper.THIRD_PARTY_DOMAIN_BLACKLIST -> requestSubList.setText(R.string.third_party_domain_blacklist)
-                BlocklistHelper.THIRD_PARTY_DOMAIN_INITIAL_BLACKLIST -> requestSubList.setText(R.string.third_party_domain_initial_blacklist)
-                BlocklistHelper.THIRD_PARTY_REGULAR_EXPRESSION_BLACKLIST -> requestSubList.setText(R.string.third_party_regular_expression_blacklist)
-                BlocklistHelper.THIRD_PARTY_DOMAIN_REGULAR_EXPRESSION_BLACKLIST -> requestSubList.setText(R.string.third_party_domain_regular_expression_blacklist)
-                BlocklistHelper.REGULAR_EXPRESSION_BLACKLIST -> requestSubList.setText(R.string.regular_expression_blacklist)
+            requestFilterList.text = requestDetails[REQUEST_BLOCKLIST]
+            requestFilterListEntries.text = requestDetails[REQUEST_BLOCKLIST_ENTRIES]
+            requestFilterListOriginalEntry.text = requestDetails[REQUEST_BLOCKLIST_ORIGINAL_ENTRY]
+            when (requestDetails[REQUEST_SUBLIST]) {
+                MAIN_ALLOWLIST -> requestSubList.setText(R.string.main_allowlist)
+                FINAL_ALLOWLIST -> requestSubList.setText(R.string.final_allowlist)
+                DOMAIN_ALLOWLIST -> requestSubList.setText(R.string.domain_allowlist)
+                DOMAIN_INITIAL_ALLOWLIST -> requestSubList.setText(R.string.domain_initial_allowlist)
+                DOMAIN_FINAL_ALLOWLIST -> requestSubList.setText(R.string.domain_final_allowlist)
+                THIRD_PARTY_ALLOWLIST -> requestSubList.setText(R.string.third_party_allowlist)
+                THIRD_PARTY_DOMAIN_ALLOWLIST -> requestSubList.setText(R.string.third_party_domain_allowlist)
+                THIRD_PARTY_DOMAIN_INITIAL_ALLOWLIST -> requestSubList.setText(R.string.third_party_domain_initial_allowlist)
+                MAIN_BLOCKLIST -> requestSubList.setText(R.string.main_blocklist)
+                INITIAL_BLOCKLIST -> requestSubList.setText(R.string.initial_blocklist)
+                FINAL_BLOCKLIST -> requestSubList.setText(R.string.final_blocklist)
+                DOMAIN_BLOCKLIST -> requestSubList.setText(R.string.domain_blocklist)
+                DOMAIN_INITIAL_BLOCKLIST -> requestSubList.setText(R.string.domain_initial_blocklist)
+                DOMAIN_FINAL_BLOCKLIST -> requestSubList.setText(R.string.domain_final_blocklist)
+                DOMAIN_REGULAR_EXPRESSION_BLOCKLIST -> requestSubList.setText(R.string.domain_regular_expression_blocklist)
+                THIRD_PARTY_BLOCKLIST -> requestSubList.setText(R.string.third_party_blocklist)
+                THIRD_PARTY_INITIAL_BLOCKLIST -> requestSubList.setText(R.string.third_party_initial_blocklist)
+                THIRD_PARTY_DOMAIN_BLOCKLIST -> requestSubList.setText(R.string.third_party_domain_blocklist)
+                THIRD_PARTY_DOMAIN_INITIAL_BLOCKLIST -> requestSubList.setText(R.string.third_party_domain_initial_blocklist)
+                THIRD_PARTY_REGULAR_EXPRESSION_BLOCKLIST -> requestSubList.setText(R.string.third_party_regular_expression_blocklist)
+                THIRD_PARTY_DOMAIN_REGULAR_EXPRESSION_BLOCKLIST -> requestSubList.setText(R.string.third_party_domain_regular_expression_blocklist)
+                REGULAR_EXPRESSION_BLOCKLIST -> requestSubList.setText(R.string.regular_expression_blocklist)
             }
         }
 
         // Return the alert dialog.
         return alertDialog
     }
-}
\ No newline at end of file
+}