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