2 * Copyright 2019-2022 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
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.
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.
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/>.
20 package com.stoutner.privacybrowser.dialogs
22 import android.app.Dialog
23 import android.content.Context
24 import android.content.DialogInterface
25 import android.os.Bundle
26 import android.text.Editable
27 import android.text.InputType
28 import android.text.TextWatcher
29 import android.view.WindowManager
30 import android.widget.EditText
31 import android.widget.TextView
33 import androidx.appcompat.app.AlertDialog
34 import androidx.fragment.app.DialogFragment
35 import androidx.preference.PreferenceManager
37 import com.stoutner.privacybrowser.R
38 import com.stoutner.privacybrowser.helpers.UrlHelper
40 import kotlinx.coroutines.CoroutineScope
41 import kotlinx.coroutines.Dispatchers
42 import kotlinx.coroutines.launch
43 import kotlinx.coroutines.withContext
45 // Define the class constants.
46 private const val URL_STRING = "url_string"
47 private const val FILE_SIZE_STRING = "file_size_string"
48 private const val FILE_NAME_STRING = "file_name_string"
49 private const val USER_AGENT_STRING = "user_agent_string"
50 private const val COOKIES_ENABLED = "cookies_enabled"
52 class SaveDialog : DialogFragment() {
53 // Declare the class variables.
54 private lateinit var saveListener: SaveListener
56 // The public interface is used to send information back to the parent activity.
57 interface SaveListener {
58 fun onSaveUrl(originalUrlString: String, fileNameString: String, dialogFragment: DialogFragment)
61 override fun onAttach(context: Context) {
62 // Run the default commands.
63 super.onAttach(context)
65 // Get a handle for the save webpage listener from the launching context.
66 saveListener = context as SaveListener
70 // `@JvmStatic` will no longer be required once all the code has transitioned to Kotlin.
72 fun saveUrl(urlString: String, fileNameString: String, fileSizeString: String, userAgentString: String, cookiesEnabled: Boolean): SaveDialog {
73 // Create an arguments bundle.
74 val argumentsBundle = Bundle()
76 // Store the arguments in the bundle.
77 argumentsBundle.putString(URL_STRING, urlString)
78 argumentsBundle.putString(FILE_NAME_STRING, fileNameString)
79 argumentsBundle.putString(FILE_SIZE_STRING, fileSizeString)
80 argumentsBundle.putString(USER_AGENT_STRING, userAgentString)
81 argumentsBundle.putBoolean(COOKIES_ENABLED, cookiesEnabled)
83 // Create a new instance of the save webpage dialog.
84 val saveDialog = SaveDialog()
86 // Add the arguments bundle to the new dialog.
87 saveDialog.arguments = argumentsBundle
89 // Return the new dialog.
94 override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
95 // Get the arguments from the bundle.
96 val originalUrlString = requireArguments().getString(URL_STRING)!!
97 var fileNameString = requireArguments().getString(FILE_NAME_STRING)!!
98 val fileSizeString = requireArguments().getString(FILE_SIZE_STRING)!!
99 val userAgentString = requireArguments().getString(USER_AGENT_STRING)!!
100 val cookiesEnabled = requireArguments().getBoolean(COOKIES_ENABLED)
102 // Use an alert dialog builder to create the alert dialog.
103 val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
106 dialogBuilder.setTitle(R.string.save_url)
109 dialogBuilder.setIcon(R.drawable.download)
112 dialogBuilder.setView(R.layout.save_dialog)
114 // Set the cancel button listener. Using `null` as the listener closes the dialog without doing anything else.
115 dialogBuilder.setNegativeButton(R.string.cancel, null)
117 // Set the save button listener.
118 dialogBuilder.setPositiveButton(R.string.save) { _: DialogInterface, _: Int ->
119 // Return the dialog fragment to the parent activity.
120 saveListener.onSaveUrl(originalUrlString, fileNameString, this)
123 // Create an alert dialog from the builder.
124 val alertDialog = dialogBuilder.create()
126 // Get a handle for the shared preferences.
127 val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
129 // Get the screenshot preference.
130 val allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots_key), false)
132 // Disable screenshots if not allowed.
133 if (!allowScreenshots) {
134 alertDialog.window!!.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
137 // The alert dialog must be shown before items in the layout can be modified.
140 // Get handles for the layout items.
141 val urlEditText = alertDialog.findViewById<EditText>(R.id.url_edittext)!!
142 val fileSizeTextView = alertDialog.findViewById<TextView>(R.id.file_size_textview)!!
143 val saveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE)
145 // Set the file size text view.
146 fileSizeTextView.text = fileSizeString
148 // Populate the URL edit text according to the type. This must be done before the text change listener is created below so that the file size isn't requested again.
149 if (originalUrlString.startsWith("data:")) { // The URL contains the entire data of an image.
150 // Get a substring of the data URL with the first 100 characters. Otherwise, the user interface will freeze while trying to layout the edit text.
151 val urlSubstring = originalUrlString.substring(0, 100) + "…"
153 // Populate the URL edit text with the truncated URL.
154 urlEditText.setText(urlSubstring)
156 // Disable the editing of the URL edit text.
157 urlEditText.inputType = InputType.TYPE_NULL
158 } else { // The URL contains a reference to the location of the data.
159 // Populate the URL edit text with the full URL.
160 urlEditText.setText(originalUrlString)
163 // Update the file size when the URL changes.
164 urlEditText.addTextChangedListener(object : TextWatcher {
165 override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
169 override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
173 override fun afterTextChanged(editable: Editable) {
174 // Get the current URL to save.
175 val urlToSave = urlEditText.text.toString()
177 // Enable the save button if the URL is populated.
178 saveButton.isEnabled = urlToSave.isNotEmpty()
180 CoroutineScope(Dispatchers.Main).launch {
181 // Create a URL size string.
182 var fileNameAndSize: Pair<String, String>
184 // Get the URL size on the IO thread.
185 withContext(Dispatchers.IO) {
186 // Get the updated file name and size.
187 fileNameAndSize = UrlHelper.getNameAndSize(requireContext(), urlToSave, userAgentString, cookiesEnabled)
189 // Save the updated file name.
190 fileNameString = fileNameAndSize.first
193 // Display the updated URL.
194 fileSizeTextView.text = fileNameAndSize.second
199 // Return the alert dialog.