/*
- * Copyright © 2019-2022 Soren Stoutner <soren@stoutner.com>.
+ * Copyright 2019-2022 Soren Stoutner <soren@stoutner.com>.
*
* This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
*
import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
-import android.os.AsyncTask
import android.os.Bundle
import android.text.Editable
import android.text.InputType
import androidx.preference.PreferenceManager
import com.stoutner.privacybrowser.R
-import com.stoutner.privacybrowser.asynctasks.GetUrlSize
+import com.stoutner.privacybrowser.helpers.UrlHelper
+
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
// Define the class constants.
private const val URL_STRING = "url_string"
// Declare the class variables.
private lateinit var saveListener: SaveListener
- // Define the class variables.
- private var getUrlSize: AsyncTask<*, *, *>? = null
-
// The public interface is used to send information back to the parent activity.
interface SaveListener {
fun onSaveUrl(originalUrlString: String, fileNameString: String, dialogFragment: DialogFragment)
companion object {
// `@JvmStatic` will no longer be required once all the code has transitioned to Kotlin.
@JvmStatic
- fun saveUrl(urlString: String, fileSizeString: String, fileNameString: String, userAgentString: String, cookiesEnabled: Boolean): SaveDialog {
+ fun saveUrl(urlString: String, fileNameString: String, fileSizeString: String, userAgentString: String, cookiesEnabled: Boolean): SaveDialog {
// Create an arguments bundle.
val argumentsBundle = Bundle()
// Store the arguments in the bundle.
argumentsBundle.putString(URL_STRING, urlString)
- argumentsBundle.putString(FILE_SIZE_STRING, fileSizeString)
argumentsBundle.putString(FILE_NAME_STRING, fileNameString)
+ argumentsBundle.putString(FILE_SIZE_STRING, fileSizeString)
argumentsBundle.putString(USER_AGENT_STRING, userAgentString)
argumentsBundle.putBoolean(COOKIES_ENABLED, cookiesEnabled)
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
// Get the arguments from the bundle.
val originalUrlString = requireArguments().getString(URL_STRING)!!
+ var fileNameString = requireArguments().getString(FILE_NAME_STRING)!!
val fileSizeString = requireArguments().getString(FILE_SIZE_STRING)!!
- val fileNameString = requireArguments().getString(FILE_NAME_STRING)!!
val userAgentString = requireArguments().getString(USER_AGENT_STRING)!!
val cookiesEnabled = requireArguments().getBoolean(COOKIES_ENABLED)
// Set the title.
dialogBuilder.setTitle(R.string.save_url)
- // Set the icon according to the theme.
- dialogBuilder.setIconAttribute(R.attr.copyBlueIcon)
+ // Set the icon.
+ dialogBuilder.setIcon(R.drawable.download)
// Set the view.
dialogBuilder.setView(R.layout.save_dialog)
val alertDialog = dialogBuilder.create()
// Get a handle for the shared preferences.
- val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
+ val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
// Get the screenshot preference.
val allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots_key), false)
}
override fun afterTextChanged(editable: Editable) {
- // Cancel the get URL size AsyncTask if it is running.
- if (getUrlSize != null) {
- getUrlSize!!.cancel(true)
- }
-
// Get the current URL to save.
val urlToSave = urlEditText.text.toString()
- // Wipe the file size text view.
- fileSizeTextView.text = ""
-
- // Get the file size for the current URL.
- getUrlSize = GetUrlSize(context, alertDialog, userAgentString, cookiesEnabled).execute(urlToSave)
-
// Enable the save button if the URL is populated.
saveButton.isEnabled = urlToSave.isNotEmpty()
+
+ CoroutineScope(Dispatchers.Main).launch {
+ // Create a URL size string.
+ var fileNameAndSize: Pair<String, String>
+
+ // Get the URL size on the IO thread.
+ withContext(Dispatchers.IO) {
+ // Get the updated file name and size.
+ fileNameAndSize = UrlHelper.getNameAndSize(requireContext(), urlToSave, userAgentString, cookiesEnabled)
+
+ // Save the updated file name.
+ fileNameString = fileNameAndSize.first
+ }
+
+ // Display the updated URL.
+ fileSizeTextView.text = fileNameAndSize.second
+ }
}
})
// Return the alert dialog.
return alertDialog
}
-}
\ No newline at end of file
+}