2 * Copyright 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.graphics.Typeface
24 import android.os.Bundle
25 import android.text.SpannableStringBuilder
26 import android.text.Spanned
27 import android.text.style.StyleSpan
28 import android.view.WindowManager
30 import androidx.appcompat.app.AlertDialog
31 import androidx.fragment.app.DialogFragment
32 import androidx.preference.PreferenceManager
34 import com.stoutner.privacybrowser.R
36 // Define the public class constants.
37 const val AVAILABLE_CIPHERS = 0
38 const val SSL_CERTIFICATE = 1
40 // Define the private class constants.
41 private const val DIALOG_TYPE = "A"
42 private const val MESSAGE = "B"
43 private const val APPLIED_CIPHER_STRING = "C"
45 class ViewHeadersDetailDialog : DialogFragment() {
47 fun displayDialog(dialogType: Int, message: String, appliedCipherString: String = ""): ViewHeadersDetailDialog {
48 // Create an arguments bundle.
49 val argumentsBundle = Bundle()
51 // Store the SSL error message components in the bundle.
52 argumentsBundle.putInt(DIALOG_TYPE, dialogType)
53 argumentsBundle.putString(MESSAGE, message)
54 argumentsBundle.putString(APPLIED_CIPHER_STRING, appliedCipherString)
56 // Create a new instance of the SSL certificate error dialog.
57 val thisHeadersSslCertificateDialog = ViewHeadersDetailDialog()
59 // Add the arguments bundle to the new dialog.
60 thisHeadersSslCertificateDialog.arguments = argumentsBundle
62 // Return the new dialog.
63 return thisHeadersSslCertificateDialog
67 override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
68 // Get the arguments from the bundle.
69 val dialogType = requireArguments().getInt(DIALOG_TYPE)
70 val message = requireArguments().getString(MESSAGE)!!
71 val appliedCipherString = requireArguments().getString(APPLIED_CIPHER_STRING)!!
73 // Use a builder to create the alert dialog.
74 val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
76 // Set the icon according to the theme.
77 dialogBuilder.setIcon(R.drawable.ssl_certificate)
79 // Set the title and message according to the type.
80 if (dialogType == AVAILABLE_CIPHERS) { // A cipher suite dialog is displayed.
82 dialogBuilder.setTitle(R.string.available_ciphers)
84 // Create a message spannable string builder with the applied cipher bolded.
85 val messageSpannableStringBuilder = SpannableStringBuilder(message)
87 // Get the applied cipher index.
88 val appliedCipherIndex = message.indexOf(appliedCipherString)
90 // Set the applied cipher to be bold.
91 messageSpannableStringBuilder.setSpan(StyleSpan(Typeface.BOLD), appliedCipherIndex, appliedCipherIndex + appliedCipherString.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
94 dialogBuilder.setMessage(messageSpannableStringBuilder)
95 } else { // An SSL certificate dialog is displayed.
96 // Set the title and message.
97 dialogBuilder.setTitle(R.string.ssl_certificate)
98 dialogBuilder.setMessage(message)
101 // Set the close button listener. Using `null` as the listener closes the dialog without doing anything else.
102 dialogBuilder.setNegativeButton(R.string.close, null)
104 // Create an alert dialog from the alert dialog builder.
105 val alertDialog = dialogBuilder.create()
107 // Get a handle for the shared preferences.
108 val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
110 // Get the screenshot preference.
111 val allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots_key), false)
113 // Disable screenshots if not allowed.
114 if (!allowScreenshots) {
115 alertDialog.window!!.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
118 // Return the alert dialog.