]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/SaveDialog.kt
Fix a crash when opening a drawer while restarting. 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.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.WindowManager
30 import android.widget.Button
31 import android.widget.EditText
32
33 import androidx.appcompat.app.AlertDialog
34 import androidx.fragment.app.DialogFragment
35 import androidx.preference.PreferenceManager
36
37 import com.stoutner.privacybrowser.R
38
39 // Define 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         // Define 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 new dialog.
79             saveDialog.arguments = argumentsBundle
80
81             // Return the new dialog.
82             return saveDialog
83         }
84     }
85
86     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
87         // Get the arguments from the bundle.
88         val saveType = requireArguments().getInt(SAVE_TYPE)
89
90         // Use an alert dialog builder to create the alert dialog.
91         val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
92
93         // Set the title and the icon according to the save type.
94         when (saveType) {
95             SAVE_LOGCAT -> {
96                 // Set the title.
97                 dialogBuilder.setTitle(R.string.save_logcat)
98
99                 // Set the icon according to the theme.
100                 dialogBuilder.setIconAttribute(R.attr.saveBlueIcon)
101             }
102
103             SAVE_ABOUT_VERSION_TEXT -> {
104                 // Set the title.
105                 dialogBuilder.setTitle(R.string.save_text)
106
107                 // Set the icon according to the theme.
108                 dialogBuilder.setIconAttribute(R.attr.saveTextBlueIcon)
109             }
110
111             SAVE_ABOUT_VERSION_IMAGE -> {
112                 // Set the title.
113                 dialogBuilder.setTitle(R.string.save_image)
114
115                 // Set the icon according to the theme.
116                 dialogBuilder.setIconAttribute(R.attr.imagesBlueIcon)
117             }
118         }
119
120         // Set the view.
121         dialogBuilder.setView(R.layout.save_dialog)
122
123         // Set the cancel button listener.  Using `null` as the listener closes the dialog without doing anything else.
124         dialogBuilder.setNegativeButton(R.string.cancel, null)
125
126         // Set the save button listener.
127         dialogBuilder.setPositiveButton(R.string.save) { _: DialogInterface?, _: Int ->
128             // Return the dialog fragment to the parent activity.
129             saveListener.onSave(saveType, this)
130         }
131
132         // Create an alert dialog from the builder.
133         val alertDialog = dialogBuilder.create()
134
135         // Get a handle for the shared preferences.
136         val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
137
138         // Get the screenshot preference.
139         val allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots_key), false)
140
141         // Disable screenshots if not allowed.
142         if (!allowScreenshots) {
143             alertDialog.window!!.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
144         }
145
146         // The alert dialog must be shown before items in the layout can be modified.
147         alertDialog.show()
148
149         // Get handles for the layout items.
150         val fileNameEditText = alertDialog.findViewById<EditText>(R.id.file_name_edittext)!!
151         val browseButton = alertDialog.findViewById<Button>(R.id.browse_button)!!
152         val saveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE)
153
154         // Initially disable the save button.
155         saveButton.isEnabled = false
156
157         // Update the status of the save button when the file name changes.
158         fileNameEditText.addTextChangedListener(object : TextWatcher {
159             override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
160                 // Do nothing.
161             }
162
163             override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
164                 // Do nothing.
165             }
166
167             override fun afterTextChanged(s: Editable) {
168                 // Get the current file name.
169                 val fileNameString = fileNameEditText.text.toString()
170
171                 // Enable the save button if the file name is populated.
172                 saveButton.isEnabled = fileNameString.isNotEmpty()
173             }
174         })
175
176         // Set the file name according to the type.
177         when (saveType) {
178             SAVE_LOGCAT -> fileName = getString(R.string.privacy_browser_logcat_txt)
179             SAVE_ABOUT_VERSION_TEXT -> fileName = getString(R.string.privacy_browser_version_txt)
180             SAVE_ABOUT_VERSION_IMAGE -> fileName = getString(R.string.privacy_browser_version_png)
181         }
182
183         // Handle clicks on the browse button.
184         browseButton.setOnClickListener {
185             // Create the file picker intent.
186             val browseIntent = Intent(Intent.ACTION_CREATE_DOCUMENT)
187
188             // Set the intent MIME type to include all files so that everything is visible.
189             browseIntent.type = "*/*"
190
191             // Set the initial file name.
192             browseIntent.putExtra(Intent.EXTRA_TITLE, fileName)
193
194             // Request a file that can be opened.
195             browseIntent.addCategory(Intent.CATEGORY_OPENABLE)
196
197             // 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.
198             requireActivity().startActivityForResult(browseIntent, 0)
199         }
200
201         // Return the alert dialog.
202         return alertDialog
203     }
204 }