2 * Copyright © 2021-2022 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.WindowManager
28 import androidx.appcompat.app.AlertDialog
29 import androidx.fragment.app.DialogFragment
30 import androidx.preference.PreferenceManager
32 import com.stoutner.privacybrowser.R
34 class UntrustedSslCertificateDialog : DialogFragment() {
35 // Declare the class variables.
36 private lateinit var untrustedSslCertificateListener: UntrustedSslCertificateListener
37 private var dismissDialog: Boolean = false
39 // The public interface is used to send information back to the parent activity.
40 interface UntrustedSslCertificateListener {
44 override fun onAttach(context: Context) {
45 // Run the default commands.
46 super.onAttach(context)
48 // Get a handle for the listener form the launching context.
49 untrustedSslCertificateListener = context as UntrustedSslCertificateListener
52 override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
53 // Check to see if the app has been restarted.
54 if (savedInstanceState == null) { // The app has not been restarted.
55 // Use a builder to create the alert dialog.
56 val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
58 // Set the icon according to the theme.
59 dialogBuilder.setIconAttribute(R.attr.sslCertificateBlueIcon)
62 dialogBuilder.setTitle(R.string.ssl_certificate_error)
65 dialogBuilder.setMessage(R.string.untrusted_ssl_certificate)
67 // Set the cancel button listener. Using `null` as the listener closes the dialog without doing anything else.
68 dialogBuilder.setNegativeButton(R.string.cancel, null)
70 // Set the load anyway button listener.
71 dialogBuilder.setPositiveButton(R.string.load_anyway) { _: DialogInterface, _: Int ->
72 // Instruct the parent activity to load the URL anyway.
73 untrustedSslCertificateListener.loadAnyway()
76 // Create an alert dialog from the builder.
77 val alertDialog = dialogBuilder.create()
79 // Get a handle for the shared preferences.
80 val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
82 // Get the screenshot preference.
83 val allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots_key), false)
85 // Disable screenshots if not allowed.
86 if (!allowScreenshots) {
87 alertDialog.window!!.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
90 // Return the alert dialog.
92 } else { // The app has been restarted. Close the dialog as a new one will automatically be created by GetSourceBackgroundTask.
93 // Use an alert dialog builder to create an empty alert dialog.
94 val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
96 // Create an empty alert dialog from the alert dialog builder.
97 val alertDialog = dialogBuilder.create()
99 // Set the flag to dismiss the dialog as soon as it is resumed.
102 // Return the alert dialog.
107 override fun onResume() {
108 // Run the default commands.
111 // Dismiss the alert dialog if the activity was restarted as a new one will automatically be created by GetSourceBackgroundTask.