]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/UntrustedSslCertificateDialog.kt
Allow View Source to connect to untrusted SSL certificates. https://redmine.stoutner...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / UntrustedSslCertificateDialog.kt
1 /*
2  * Copyright © 2021 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
6  * Privacy Browser 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 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.  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.content.Context
24 import android.content.DialogInterface
25 import android.os.Bundle
26 import android.view.WindowManager
27
28 import androidx.appcompat.app.AlertDialog
29 import androidx.fragment.app.DialogFragment
30 import androidx.preference.PreferenceManager
31
32 import com.stoutner.privacybrowser.R
33
34 class UntrustedSslCertificateDialog : DialogFragment() {
35     // Declare the class variables.
36     private lateinit var untrustedSslCertificateListener: UntrustedSslCertificateListener
37     private var dismissDialog: Boolean = false
38
39     // The public interface is used to send information back to the parent activity.
40     interface UntrustedSslCertificateListener {
41         fun loadAnyway()
42     }
43
44     override fun onAttach(context: Context) {
45         // Run the default commands.
46         super.onAttach(context)
47
48         // Get a handle for the listener form the launching context.
49         untrustedSslCertificateListener = context as UntrustedSslCertificateListener
50     }
51
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 = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
57
58             // Set the icon according to the theme.
59             dialogBuilder.setIconAttribute(R.attr.sslCertificateBlueIcon)
60
61             // Set the title.
62             dialogBuilder.setTitle(R.string.ssl_certificate_error)
63
64             // Set the text.
65             dialogBuilder.setMessage(R.string.untrusted_ssl_certificate)
66
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)
69
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()
74             }
75
76             // Create an alert dialog from the builder.
77             val alertDialog = dialogBuilder.create()
78
79             // Get a handle for the shared preferences.
80             val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
81
82             // Get the screenshot preference.
83             val allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots), false)
84
85             // Disable screenshots if not allowed.
86             if (!allowScreenshots) {
87                 alertDialog.window!!.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
88             }
89
90             // Return the alert dialog.
91             return alertDialog
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)
95
96             // Create an empty alert dialog from the alert dialog builder.
97             val alertDialog = dialogBuilder.create()
98
99             // Set the flag to dismiss the dialog as soon as it is resumed.
100             dismissDialog = true
101
102             // Return the alert dialog.
103             return alertDialog
104         }
105     }
106
107     override fun onResume() {
108         // Run the default commands.
109         super.onResume()
110
111         // Dismiss the alert dialog if the activity was restarted as a new one will automatically be created by GetSourceBackgroundTask.
112         if (dismissDialog) {
113             dialog!!.dismiss()
114         }
115     }
116 }