]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/ViewHeadersDetailDialog.kt
First wrong button text in View Headers in night theme. https://redmine.stoutner...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / ViewHeadersDetailDialog.kt
1 /*
2  * Copyright 2023 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 package com.stoutner.privacybrowser.dialogs
21
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
29
30 import androidx.appcompat.app.AlertDialog
31 import androidx.fragment.app.DialogFragment
32 import androidx.preference.PreferenceManager
33
34 import com.stoutner.privacybrowser.R
35
36 // Define the public class constants.
37 const val AVAILABLE_CIPHERS = 0
38 const val SSL_CERTIFICATE = 1
39
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"
44
45 class ViewHeadersDetailDialog : DialogFragment() {
46     companion object {
47         fun displayDialog(dialogType: Int, message: String, appliedCipherString: String = ""): ViewHeadersDetailDialog {
48             // Create an arguments bundle.
49             val argumentsBundle = Bundle()
50
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)
55
56             // Create a new instance of the SSL certificate error dialog.
57             val thisHeadersSslCertificateDialog = ViewHeadersDetailDialog()
58
59             // Add the arguments bundle to the new dialog.
60             thisHeadersSslCertificateDialog.arguments = argumentsBundle
61
62             // Return the new dialog.
63             return thisHeadersSslCertificateDialog
64         }
65     }
66
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)!!
72
73         // Use a builder to create the alert dialog.
74         val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
75
76         // Set the icon according to the theme.
77         dialogBuilder.setIcon(R.drawable.ssl_certificate)
78
79         // Set the title and message according to the type.
80         if (dialogType == AVAILABLE_CIPHERS) {  // A cipher suite dialog is displayed.
81             // Set the title
82             dialogBuilder.setTitle(R.string.available_ciphers)
83
84             // Create a message spannable string builder with the applied cipher bolded.
85             val messageSpannableStringBuilder = SpannableStringBuilder(message)
86
87             // Get the applied cipher index.
88             val appliedCipherIndex = message.indexOf(appliedCipherString)
89
90             // Set the applied cipher to be bold.
91             messageSpannableStringBuilder.setSpan(StyleSpan(Typeface.BOLD), appliedCipherIndex, appliedCipherIndex + appliedCipherString.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
92
93             // Set the message.
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)
99         }
100
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)
103
104         // Create an alert dialog from the alert dialog builder.
105         val alertDialog = dialogBuilder.create()
106
107         // Get a handle for the shared preferences.
108         val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
109
110         // Get the screenshot preference.
111         val allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots_key), false)
112
113         // Disable screenshots if not allowed.
114         if (!allowScreenshots) {
115             alertDialog.window!!.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
116         }
117
118         // Return the alert dialog.
119         return alertDialog
120     }
121 }