]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/SaveDialog.kt
Redesign file access to work with the scoped storage. https://redmine.stoutner.com...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / SaveDialog.kt
1 /*
2  * Copyright © 2016-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.WindowManager
32 import android.widget.Button
33 import android.widget.EditText
34 import androidx.appcompat.app.AlertDialog
35 import androidx.fragment.app.DialogFragment
36 import androidx.preference.PreferenceManager
37 import com.stoutner.privacybrowser.R
38
39 // Declare the class constants.
40 private const val SAVE_TYPE = "save_type"
41
42 class SaveDialog : DialogFragment() {
43     // Declare the class variables.
44     private lateinit var saveListener: SaveListener
45     private lateinit var fileName: String
46
47     // The public interface is used to send information back to the parent activity.
48     interface SaveListener {
49         fun onSave(saveType: Int, dialogFragment: DialogFragment)
50     }
51
52     override fun onAttach(context: Context) {
53         // Run the default commands.
54         super.onAttach(context)
55
56         // Get a handle for the save listener from the launching context.
57         saveListener = context as SaveListener
58     }
59
60     companion object {
61         // Declare the companion object constants.  These can be moved to class constants once all of the code has transitioned to Kotlin.
62         const val SAVE_LOGCAT = 0
63         const val SAVE_ABOUT_VERSION_TEXT = 1
64         const val SAVE_ABOUT_VERSION_IMAGE = 2
65
66         // `@JvmStatic` will no longer be required once all the code has transitioned to Kotlin.
67         @JvmStatic
68         fun save(saveType: Int): SaveDialog {
69             // Create an arguments bundle.
70             val argumentsBundle = Bundle()
71
72             // Store the arguments in the bundle.
73             argumentsBundle.putInt(SAVE_TYPE, saveType)
74
75             // Create a new instance of the save dialog.
76             val saveDialog = SaveDialog()
77
78             // Add the arguments bundle to the dialog.
79             saveDialog.arguments = argumentsBundle
80
81             // Return the new dialog.
82             return saveDialog
83         }
84     }
85
86     // `@SuppressLint("InflateParams")` removes the warning about using null as the parent view group when inflating the alert dialog.
87     @SuppressLint("InflateParams")
88     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
89         // Get the arguments from the bundle.
90         val saveType = requireArguments().getInt(SAVE_TYPE)
91
92         // Use an alert dialog builder to create the alert dialog.
93         val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
94
95         // Get the current theme status.
96         val currentThemeStatus = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
97
98         // Set the title and the icon according to the save type.
99         when (saveType) {
100             SAVE_LOGCAT -> {
101                 // Set the title.
102                 dialogBuilder.setTitle(R.string.save_logcat)
103
104                 // Set the icon according to the theme.
105                 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
106                     dialogBuilder.setIcon(R.drawable.save_dialog_day)
107                 } else {
108                     dialogBuilder.setIcon(R.drawable.save_dialog_night)
109                 }
110             }
111
112             SAVE_ABOUT_VERSION_TEXT -> {
113                 // Set the title.
114                 dialogBuilder.setTitle(R.string.save_text)
115
116                 // Set the icon according to the theme.
117                 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
118                     dialogBuilder.setIcon(R.drawable.save_text_blue_day)
119                 } else {
120                     dialogBuilder.setIcon(R.drawable.save_text_blue_night)
121                 }
122             }
123
124             SAVE_ABOUT_VERSION_IMAGE -> {
125                 // Set the title.
126                 dialogBuilder.setTitle(R.string.save_image)
127
128                 // Set the icon according to the theme.
129                 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
130                     dialogBuilder.setIcon(R.drawable.images_enabled_day)
131                 } else {
132                     dialogBuilder.setIcon(R.drawable.images_enabled_night)
133                 }
134             }
135         }
136
137         // Set the view.  The parent view is null because it will be assigned by the alert dialog.
138         dialogBuilder.setView(requireActivity().layoutInflater.inflate(R.layout.save_dialog, null))
139
140         // Set the cancel button listener.  Using `null` as the listener closes the dialog without doing anything else.
141         dialogBuilder.setNegativeButton(R.string.cancel, null)
142
143         // Set the save button listener.
144         dialogBuilder.setPositiveButton(R.string.save) { _: DialogInterface?, _: Int ->
145             // Return the dialog fragment to the parent activity.
146             saveListener.onSave(saveType, this)
147         }
148
149         // Create an alert dialog from the builder.
150         val alertDialog = dialogBuilder.create()
151
152         // Get a handle for the shared preferences.
153         val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
154
155         // Get the screenshot preference.
156         val allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots_key), false)
157
158         // Disable screenshots if not allowed.
159         if (!allowScreenshots) {
160             alertDialog.window!!.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
161         }
162
163         // The alert dialog must be shown before items in the layout can be modified.
164         alertDialog.show()
165
166         // Get handles for the layout items.
167         val fileNameEditText = alertDialog.findViewById<EditText>(R.id.file_name_edittext)!!
168         val browseButton = alertDialog.findViewById<Button>(R.id.browse_button)!!
169         val saveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE)
170
171         // Initially disable the save button.
172         saveButton.isEnabled = false
173
174         // Update the status of the save button when the file name changes.
175         fileNameEditText.addTextChangedListener(object : TextWatcher {
176             override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
177                 // Do nothing.
178             }
179
180             override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
181                 // Do nothing.
182             }
183
184             override fun afterTextChanged(s: Editable) {
185                 // Get the current file name.
186                 val fileNameString = fileNameEditText.text.toString()
187
188                 // Enable the save button if the file name is populated.
189                 saveButton.isEnabled = fileNameString.isNotEmpty()
190             }
191         })
192
193         // Set the file name according to the type.
194         when (saveType) {
195             SAVE_LOGCAT -> fileName = getString(R.string.privacy_browser_logcat_txt)
196             SAVE_ABOUT_VERSION_TEXT -> fileName = getString(R.string.privacy_browser_version_txt)
197             SAVE_ABOUT_VERSION_IMAGE -> fileName = getString(R.string.privacy_browser_version_png)
198         }
199
200         // Handle clicks on the browse button.
201         browseButton.setOnClickListener {
202             // Create the file picker intent.
203             val browseIntent = Intent(Intent.ACTION_CREATE_DOCUMENT)
204
205             // Set the intent MIME type to include all files so that everything is visible.
206             browseIntent.type = "*/*"
207
208             // Set the initial file name.
209             browseIntent.putExtra(Intent.EXTRA_TITLE, fileName)
210
211             // Request a file that can be opened.
212             browseIntent.addCategory(Intent.CATEGORY_OPENABLE)
213
214             // Launch the file picker.  There is only one `startActivityForResult()`, so the request code is simply set to 0, but it must be run under `activity` so the response is processed correctly.
215             requireActivity().startActivityForResult(browseIntent, 0)
216         }
217
218         // Return the alert dialog.
219         return alertDialog
220     }
221 }