X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fdialogs%2FSaveDialog.kt;h=faae1819df68bf38582d8fe1888ac64beacd5d1c;hb=3b5b81db53b0ee6f448ac3144a176c6d9042f4c8;hp=1aa31dd00c7ce94fc117dde1fcb5f64d1b24ae32;hpb=aba828cabddd0a277271c90816b50292bb64baf1;p=PrivacyBrowserAndroid.git diff --git a/app/src/main/java/com/stoutner/privacybrowser/dialogs/SaveDialog.kt b/app/src/main/java/com/stoutner/privacybrowser/dialogs/SaveDialog.kt index 1aa31dd0..faae1819 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/dialogs/SaveDialog.kt +++ b/app/src/main/java/com/stoutner/privacybrowser/dialogs/SaveDialog.kt @@ -1,5 +1,5 @@ /* - * Copyright © 2019-2022 Soren Stoutner . + * Copyright 2019-2023 Soren Stoutner . * * This file is part of Privacy Browser Android . * @@ -22,7 +22,6 @@ package com.stoutner.privacybrowser.dialogs 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 @@ -36,9 +35,14 @@ import androidx.fragment.app.DialogFragment import androidx.preference.PreferenceManager import com.stoutner.privacybrowser.R -import com.stoutner.privacybrowser.asynctasks.GetUrlSize +import com.stoutner.privacybrowser.helpers.UrlHelper -// Define the class constants. +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext + +// Define the private class constants. private const val URL_STRING = "url_string" private const val FILE_SIZE_STRING = "file_size_string" private const val FILE_NAME_STRING = "file_name_string" @@ -46,36 +50,15 @@ private const val USER_AGENT_STRING = "user_agent_string" private const val COOKIES_ENABLED = "cookies_enabled" class SaveDialog : DialogFragment() { - // 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) - } - - override fun onAttach(context: Context) { - // Run the default commands. - super.onAttach(context) - - // Get a handle for the save webpage listener from the launching context. - saveListener = context as SaveListener - } - 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) @@ -90,11 +73,27 @@ class SaveDialog : DialogFragment() { } } + // Declare the class variables. + private lateinit var saveListener: SaveListener + + // The public interface is used to send information back to the parent activity. + interface SaveListener { + fun saveUrl(originalUrlString: String, fileNameString: String, dialogFragment: DialogFragment) + } + + override fun onAttach(context: Context) { + // Run the default commands. + super.onAttach(context) + + // Get a handle for the save webpage listener from the launching context. + saveListener = context as SaveListener + } + 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) @@ -104,8 +103,8 @@ class SaveDialog : DialogFragment() { // 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) @@ -116,7 +115,7 @@ class SaveDialog : DialogFragment() { // Set the save button listener. dialogBuilder.setPositiveButton(R.string.save) { _: DialogInterface, _: Int -> // Return the dialog fragment to the parent activity. - saveListener.onSaveUrl(originalUrlString, fileNameString, this) + saveListener.saveUrl(originalUrlString, fileNameString, this) } // Create an alert dialog from the builder. @@ -170,26 +169,32 @@ class SaveDialog : DialogFragment() { } 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 + + // 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 +}