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