]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/OpenDialog.kt
First wrong button text in View Headers in night theme. https://redmine.stoutner...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / OpenDialog.kt
1 /*
2  * Copyright 2019-2023 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.net.Uri
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.activity.result.contract.ActivityResultContracts
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
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 fileNameEditText: EditText
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 openFile(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     // Define the browse activity result launcher.
68     private val browseActivityResultLauncher = registerForActivityResult(ActivityResultContracts.GetContent()) { fileUri: Uri? ->
69         // Only do something if the user didn't press back from the file picker.
70         if (fileUri != null) {
71             // Get the file name string from the URI.
72             val fileNameString = fileUri.toString()
73
74             // Set the file name text.
75             fileNameEditText.setText(fileNameString)
76
77             // Move the cursor to the end of the file name edit text.
78             fileNameEditText.setSelection(fileNameString.length)
79         }
80     }
81
82     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
83         // Use an alert dialog builder to create the alert dialog.
84         val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
85
86         // Set the icon.
87         dialogBuilder.setIcon(R.drawable.proxy_enabled)
88
89         // Set the title.
90         dialogBuilder.setTitle(R.string.open)
91
92         // Set the view.
93         dialogBuilder.setView(R.layout.open_dialog)
94
95         // Set the cancel button listener.  Using `null` as the listener closes the dialog without doing anything else.
96         dialogBuilder.setNegativeButton(R.string.cancel, null)
97
98         // Set the open button listener.
99         dialogBuilder.setPositiveButton(R.string.open) { _: DialogInterface?, _: Int ->
100             // Return the dialog fragment to the parent activity.
101             openListener.openFile(this)
102         }
103
104         // Create an alert dialog from the builder.
105         val alertDialog = dialogBuilder.create()
106
107         // Get a handle for the shared preferences.
108         val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
109
110         // Get the screenshot preference.
111         val allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots_key), false)
112
113         // Disable screenshots if not allowed.
114         if (!allowScreenshots) {
115             alertDialog.window!!.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
116         }
117
118         // The alert dialog must be shown before items in the layout can be modified.
119         alertDialog.show()
120
121         // Get handles for the layout items.
122         fileNameEditText = alertDialog.findViewById(R.id.file_name_edittext)!!
123         val browseButton = alertDialog.findViewById<Button>(R.id.browse_button)!!
124         val mhtCheckBox = alertDialog.findViewById<CheckBox>(R.id.mht_checkbox)!!
125         mhtExplanationTextView = alertDialog.findViewById(R.id.mht_explanation_textview)!!
126         val openButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE)
127
128         // Initially disable the open button.
129         openButton.isEnabled = false
130
131         // Update the status of the open button when the file name changes.
132         fileNameEditText.addTextChangedListener(object : TextWatcher {
133             override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
134                 // Do nothing.
135             }
136
137             override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
138                 // Do nothing.
139             }
140
141             override fun afterTextChanged(editable: Editable) {
142                 // Get the current file name.
143                 val fileNameString = fileNameEditText.text.toString()
144
145                 // Enable the open button if the file name is populated.
146                 openButton.isEnabled = fileNameString.isNotEmpty()
147             }
148         })
149
150         // Handle clicks on the browse button.
151         browseButton.setOnClickListener {
152             // Launch the file picker.
153             browseActivityResultLauncher.launch("*/*")
154         }
155
156         // Handle clicks on the MHT checkbox.
157         mhtCheckBox.setOnClickListener {
158             // Update the visibility of the MHT explanation text view.
159             if (mhtCheckBox.isChecked) {
160                 mhtExplanationTextView.visibility = View.VISIBLE
161             } else {
162                 mhtExplanationTextView.visibility = View.GONE
163             }
164         }
165
166         // Restore the MHT explanation text view visibility if the saved instance state is not null.
167         if (savedInstanceState != null) {
168             // Restore the MHT explanation text view visibility.
169             if (savedInstanceState.getBoolean(MHT_EXPLANATION_VISIBILITY)) {
170                 mhtExplanationTextView.visibility = View.VISIBLE
171             } else {
172                 mhtExplanationTextView.visibility = View.GONE
173             }
174         }
175
176         // Return the alert dialog.
177         return alertDialog
178     }
179
180     override fun onSaveInstanceState(savedInstanceState: Bundle) {
181         // Run the default commands.
182         super.onSaveInstanceState(savedInstanceState)
183
184         // Add the MHT explanation visibility status to the bundle.
185         savedInstanceState.putBoolean(MHT_EXPLANATION_VISIBILITY, mhtExplanationTextView.visibility == View.VISIBLE)
186     }
187 }