]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/OpenDialog.kt
Restore saving as MHT web archives. https://redmine.stoutner.com/issues/677
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / OpenDialog.kt
1 /*
2  * Copyright © 2019-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.annotation.SuppressLint
23 import android.app.Dialog
24 import android.content.Context
25 import android.content.DialogInterface
26 import android.content.Intent
27 import android.content.res.Configuration
28 import android.os.Bundle
29 import android.text.Editable
30 import android.text.TextWatcher
31 import android.view.View
32 import android.view.WindowManager
33 import android.widget.Button
34 import android.widget.CheckBox
35 import android.widget.EditText
36 import android.widget.TextView
37
38 import androidx.appcompat.app.AlertDialog
39 import androidx.fragment.app.DialogFragment
40 import androidx.preference.PreferenceManager
41
42 import com.stoutner.privacybrowser.R
43 import com.stoutner.privacybrowser.activities.MainWebViewActivity
44
45 // Define the class constants.
46 private const val MHT_EXPLANATION_VISIBILITY = "mht_explanation_visibility"
47
48 class OpenDialog : DialogFragment() {
49     // Declare the class variables.
50     private lateinit var openListener: OpenListener
51
52     // Declare the class views.
53     private lateinit var mhtExplanationTextView: TextView
54
55     // The public interface is used to send information back to the parent activity.
56     interface OpenListener {
57         fun onOpen(dialogFragment: DialogFragment)
58     }
59
60     override fun onAttach(context: Context) {
61         // Run the default commands.
62         super.onAttach(context)
63
64         // Get a handle for the open listener from the launching context.
65         openListener = context as OpenListener
66     }
67
68     // `@SuppressLint("InflateParams")` removes the warning about using null as the parent view group when inflating the alert dialog.
69     @SuppressLint("InflateParams")
70     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
71         // Use an alert dialog builder to create the alert dialog.
72         val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
73
74         // Get the current theme status.
75         val currentThemeStatus = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
76
77         // Set the icon according to the theme.
78         if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
79             dialogBuilder.setIcon(R.drawable.proxy_enabled_day)
80         } else {
81             dialogBuilder.setIcon(R.drawable.proxy_enabled_night)
82         }
83
84         // Set the title.
85         dialogBuilder.setTitle(R.string.open)
86
87         // Set the view.  The parent view is null because it will be assigned by the alert dialog.
88         dialogBuilder.setView(layoutInflater.inflate(R.layout.open_dialog, null))
89
90         // Set the cancel button listener.  Using `null` as the listener closes the dialog without doing anything else.
91         dialogBuilder.setNegativeButton(R.string.cancel, null)
92
93         // Set the open button listener.
94         dialogBuilder.setPositiveButton(R.string.open) { _: DialogInterface?, _: Int ->
95             // Return the dialog fragment to the parent activity.
96             openListener.onOpen(this)
97         }
98
99         // Create an alert dialog from the builder.
100         val alertDialog = dialogBuilder.create()
101
102         // Get a handle for the shared preferences.
103         val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
104
105         // Get the screenshot preference.
106         val allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots_key), false)
107
108         // Disable screenshots if not allowed.
109         if (!allowScreenshots) {
110             alertDialog.window!!.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
111         }
112
113         // The alert dialog must be shown before items in the layout can be modified.
114         alertDialog.show()
115
116         // Get handles for the layout items.
117         val fileNameEditText = alertDialog.findViewById<EditText>(R.id.file_name_edittext)!!
118         val browseButton = alertDialog.findViewById<Button>(R.id.browse_button)!!
119         val mhtCheckBox = alertDialog.findViewById<CheckBox>(R.id.mht_checkbox)!!
120         mhtExplanationTextView = alertDialog.findViewById(R.id.mht_explanation_textview)!!
121         val openButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE)
122
123         // Initially disable the open button.
124         openButton.isEnabled = false
125
126         // Update the status of the open button when the file name changes.
127         fileNameEditText.addTextChangedListener(object : TextWatcher {
128             override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
129                 // Do nothing.
130             }
131
132             override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
133                 // Do nothing.
134             }
135
136             override fun afterTextChanged(editable: Editable) {
137                 // Get the current file name.
138                 val fileNameString = fileNameEditText.text.toString()
139
140                 // Enable the open button if the file name is populated.
141                 openButton.isEnabled = fileNameString.isNotEmpty()
142             }
143         })
144
145         // Handle clicks on the browse button.
146         browseButton.setOnClickListener {
147             // Create the file picker intent.
148             val browseIntent = Intent(Intent.ACTION_OPEN_DOCUMENT)
149
150             // Only display files that can be opened.
151             browseIntent.addCategory(Intent.CATEGORY_OPENABLE)
152
153             // Set the intent MIME type to include all files so that everything is visible.
154             browseIntent.type = "*/*"
155
156             // Start the file picker.  This must be started under `activity` to that the request code is returned correctly.
157             requireActivity().startActivityForResult(browseIntent, MainWebViewActivity.BROWSE_OPEN_REQUEST_CODE)
158         }
159
160         // Handle clicks on the MHT checkbox.
161         mhtCheckBox.setOnClickListener {
162             // Update the visibility of the MHT explanation text view.
163             if (mhtCheckBox.isChecked) {
164                 mhtExplanationTextView.visibility = View.VISIBLE
165             } else {
166                 mhtExplanationTextView.visibility = View.GONE
167             }
168         }
169
170         // Restore the MHT explanation text view visibility if the saved instance state is not null.
171         if (savedInstanceState != null) {
172             // Restore the MHT explanation text view visibility.
173             if (savedInstanceState.getBoolean(MHT_EXPLANATION_VISIBILITY)) {
174                 mhtExplanationTextView.visibility = View.VISIBLE
175             } else {
176                 mhtExplanationTextView.visibility = View.GONE
177             }
178         }
179
180         // Return the alert dialog.
181         return alertDialog
182     }
183
184     override fun onSaveInstanceState(savedInstanceState: Bundle) {
185         // Run the default commands.
186         super.onSaveInstanceState(savedInstanceState)
187
188         // Add the MHT explanation visibility status to the bundle.
189         savedInstanceState.putBoolean(MHT_EXPLANATION_VISIBILITY, mhtExplanationTextView.visibility == View.VISIBLE)
190     }
191 }