2 * Copyright 2018-2023 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
6 * Privacy Browser Android is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser Android is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser Android. If not, see <http://www.gnu.org/licenses/>.
20 package com.stoutner.privacybrowser.dialogs
22 import android.app.Dialog
23 import android.content.Context
24 import android.content.DialogInterface
25 import android.os.Bundle
26 import android.view.View
27 import android.view.WindowManager
28 import android.widget.TextView
30 import androidx.appcompat.app.AlertDialog
31 import androidx.core.content.ContextCompat.getColor
32 import androidx.fragment.app.DialogFragment
33 import androidx.preference.PreferenceManager
35 import com.stoutner.privacybrowser.R
36 import com.stoutner.privacybrowser.helpers.BlocklistHelper
38 // Define the class constants.
39 private const val ID = "id"
40 private const val IS_LAST_REQUEST = "is_last_request"
41 private const val REQUEST_DETAILS = "request_details"
43 class ViewRequestDialog : DialogFragment() {
45 fun request(id: Int, isLastRequest: Boolean, requestDetails: Array<String>): ViewRequestDialog {
49 // Store the request details.
51 bundle.putBoolean(IS_LAST_REQUEST, isLastRequest)
52 bundle.putStringArray(REQUEST_DETAILS, requestDetails)
54 // Create a new instance of the view request dialog.
55 val viewRequestDialog = ViewRequestDialog()
57 // Add the arguments to the new dialog.
58 viewRequestDialog.arguments = bundle
60 // Return the new dialog.
61 return viewRequestDialog
65 // Define the class variables.
66 private lateinit var viewRequestListener: ViewRequestListener
68 // The public interface is used to send information back to the parent activity.
69 interface ViewRequestListener {
70 // Show the previous request.
71 fun onPrevious(currentId: Int)
73 // Show the next request.
74 fun onNext(currentId: Int)
77 override fun onAttach(context: Context) {
78 // Run the default commands.
79 super.onAttach(context)
81 // Get a handle for the listener from the launching context.
82 viewRequestListener = context as ViewRequestListener
85 override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
86 // Get the arguments from the bundle.
87 val id = requireArguments().getInt(ID)
88 val isLastRequest = requireArguments().getBoolean(IS_LAST_REQUEST)
89 val requestDetails = requireArguments().getStringArray(REQUEST_DETAILS)!!
91 // Use an alert dialog builder to create the alert dialog.
92 val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
95 dialogBuilder.setIcon(R.drawable.block_ads_enabled)
98 dialogBuilder.setTitle(resources.getString(R.string.request_details) + " - " + id)
101 dialogBuilder.setView(R.layout.view_request_dialog)
103 // Set the close button. Using `null` as the listener closes the dialog without doing anything else.
104 dialogBuilder.setNeutralButton(R.string.close, null)
106 // Set the previous button.
107 dialogBuilder.setNegativeButton(R.string.previous) { _: DialogInterface?, _: Int ->
108 // Load the previous request.
109 viewRequestListener.onPrevious(id)
112 // Set the next button.
113 dialogBuilder.setPositiveButton(R.string.next) { _: DialogInterface?, _: Int ->
114 // Load the next request.
115 viewRequestListener.onNext(id)
118 // Create an alert dialog from the alert dialog builder.
119 val alertDialog = dialogBuilder.create()
121 // Get a handle for the shared preferences.
122 val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
124 // Get the screenshot preference.
125 val allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots_key), false)
127 // Disable screenshots if not allowed.
128 if (!allowScreenshots) {
129 // Disable screenshots.
130 alertDialog.window!!.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
133 //The alert dialog must be shown before the contents can be modified.
136 // Get handles for the dialog views.
137 val requestDisposition = alertDialog.findViewById<TextView>(R.id.request_disposition)!!
138 val requestUrl = alertDialog.findViewById<TextView>(R.id.request_url)!!
139 val requestBlockListLabel = alertDialog.findViewById<TextView>(R.id.request_blocklist_label)!!
140 val requestBlockList = alertDialog.findViewById<TextView>(R.id.request_blocklist)!!
141 val requestSubListLabel = alertDialog.findViewById<TextView>(R.id.request_sublist_label)!!
142 val requestSubList = alertDialog.findViewById<TextView>(R.id.request_sublist)!!
143 val requestBlockListEntriesLabel = alertDialog.findViewById<TextView>(R.id.request_blocklist_entries_label)!!
144 val requestBlockListEntries = alertDialog.findViewById<TextView>(R.id.request_blocklist_entries)!!
145 val requestBlockListOriginalEntryLabel = alertDialog.findViewById<TextView>(R.id.request_blocklist_original_entry_label)!!
146 val requestBlockListOriginalEntry = alertDialog.findViewById<TextView>(R.id.request_blocklist_original_entry)!!
147 val previousButton = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE)
148 val nextButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE)
150 // Disable the previous button if the first resource request is displayed.
151 previousButton.isEnabled = (id != 1)
153 // Disable the next button if the last resource request is displayed.
154 nextButton.isEnabled = !isLastRequest
156 // Set the request action text.
157 when (requestDetails[BlocklistHelper.REQUEST_DISPOSITION]) {
158 BlocklistHelper.REQUEST_DEFAULT -> {
160 requestDisposition.setText(R.string.default_allowed)
162 // Set the background color to be transparent.
163 requestDisposition.setBackgroundColor(getColor(requireContext(), R.color.transparent))
166 BlocklistHelper.REQUEST_ALLOWED -> {
168 requestDisposition.setText(R.string.allowed)
170 // Set the background color to be blue.
171 requestDisposition.setBackgroundColor(getColor(requireContext(), R.color.blue_background))
174 BlocklistHelper.REQUEST_THIRD_PARTY -> {
176 requestDisposition.setText(R.string.third_party_blocked)
178 // Set the background color to be yellow.
179 requestDisposition.setBackgroundColor(getColor(requireContext(), R.color.yellow_background))
181 BlocklistHelper.REQUEST_BLOCKED -> {
183 requestDisposition.setText(R.string.blocked)
185 // Set the background color to be red.
186 requestDisposition.setBackgroundColor(getColor(requireContext(), R.color.red_background))
190 // Display the request URL.
191 requestUrl.text = requestDetails[BlocklistHelper.REQUEST_URL]
193 // Modify the dialog based on the request action.
194 if (requestDetails.size == 2) { // A default request.
195 // Hide the unused views.
196 requestBlockListLabel.visibility = View.GONE
197 requestBlockList.visibility = View.GONE
198 requestSubListLabel.visibility = View.GONE
199 requestSubList.visibility = View.GONE
200 requestBlockListEntriesLabel.visibility = View.GONE
201 requestBlockListEntries.visibility = View.GONE
202 requestBlockListOriginalEntryLabel.visibility = View.GONE
203 requestBlockListOriginalEntry.visibility = View.GONE
204 } else { // A blocked or allowed request.
205 // Set the text on the text views.
206 requestBlockList.text = requestDetails[BlocklistHelper.REQUEST_BLOCKLIST]
207 requestBlockListEntries.text = requestDetails[BlocklistHelper.REQUEST_BLOCKLIST_ENTRIES]
208 requestBlockListOriginalEntry.text = requestDetails[BlocklistHelper.REQUEST_BLOCKLIST_ORIGINAL_ENTRY]
209 when (requestDetails[BlocklistHelper.REQUEST_SUBLIST]) {
210 BlocklistHelper.MAIN_WHITELIST -> requestSubList.setText(R.string.main_whitelist)
211 BlocklistHelper.FINAL_WHITELIST -> requestSubList.setText(R.string.final_whitelist)
212 BlocklistHelper.DOMAIN_WHITELIST -> requestSubList.setText(R.string.domain_whitelist)
213 BlocklistHelper.DOMAIN_INITIAL_WHITELIST -> requestSubList.setText(R.string.domain_initial_whitelist)
214 BlocklistHelper.DOMAIN_FINAL_WHITELIST -> requestSubList.setText(R.string.domain_final_whitelist)
215 BlocklistHelper.THIRD_PARTY_WHITELIST -> requestSubList.setText(R.string.third_party_whitelist)
216 BlocklistHelper.THIRD_PARTY_DOMAIN_WHITELIST -> requestSubList.setText(R.string.third_party_domain_whitelist)
217 BlocklistHelper.THIRD_PARTY_DOMAIN_INITIAL_WHITELIST -> requestSubList.setText(R.string.third_party_domain_initial_whitelist)
218 BlocklistHelper.MAIN_BLACKLIST -> requestSubList.setText(R.string.main_blacklist)
219 BlocklistHelper.INITIAL_BLACKLIST -> requestSubList.setText(R.string.initial_blacklist)
220 BlocklistHelper.FINAL_BLACKLIST -> requestSubList.setText(R.string.final_blacklist)
221 BlocklistHelper.DOMAIN_BLACKLIST -> requestSubList.setText(R.string.domain_blacklist)
222 BlocklistHelper.DOMAIN_INITIAL_BLACKLIST -> requestSubList.setText(R.string.domain_initial_blacklist)
223 BlocklistHelper.DOMAIN_FINAL_BLACKLIST -> requestSubList.setText(R.string.domain_final_blacklist)
224 BlocklistHelper.DOMAIN_REGULAR_EXPRESSION_BLACKLIST -> requestSubList.setText(R.string.domain_regular_expression_blacklist)
225 BlocklistHelper.THIRD_PARTY_BLACKLIST -> requestSubList.setText(R.string.third_party_blacklist)
226 BlocklistHelper.THIRD_PARTY_INITIAL_BLACKLIST -> requestSubList.setText(R.string.third_party_initial_blacklist)
227 BlocklistHelper.THIRD_PARTY_DOMAIN_BLACKLIST -> requestSubList.setText(R.string.third_party_domain_blacklist)
228 BlocklistHelper.THIRD_PARTY_DOMAIN_INITIAL_BLACKLIST -> requestSubList.setText(R.string.third_party_domain_initial_blacklist)
229 BlocklistHelper.THIRD_PARTY_REGULAR_EXPRESSION_BLACKLIST -> requestSubList.setText(R.string.third_party_regular_expression_blacklist)
230 BlocklistHelper.THIRD_PARTY_DOMAIN_REGULAR_EXPRESSION_BLACKLIST -> requestSubList.setText(R.string.third_party_domain_regular_expression_blacklist)
231 BlocklistHelper.REGULAR_EXPRESSION_BLACKLIST -> requestSubList.setText(R.string.regular_expression_blacklist)
235 // Return the alert dialog.