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