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