]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/dialogs/SaveDialog.kt
Convert five AsyncTasks to Kotlin. https://redmine.stoutner.com/issues/931
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / SaveDialog.kt
index f681e8df1b66e401b4faccaeb12554464d6b5b54..63a6eaa301c0d8f76a41e0ab3b589b2c97d0719e 100644 (file)
@@ -1,20 +1,20 @@
 /*
- * Copyright © 2019-2021 Soren Stoutner <soren@stoutner.com>.
+ * Copyright 2019-2022 Soren Stoutner <soren@stoutner.com>.
  *
- * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
+ * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
  *
- * Privacy Browser is free software: you can redistribute it and/or modify
+ * Privacy Browser Android is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
- * Privacy Browser is distributed in the hope that it will be useful,
+ * Privacy Browser Android is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with Privacy Browser.  If not, see <http://www.gnu.org/licenses/>.
+ * along with Privacy Browser Android.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 package com.stoutner.privacybrowser.dialogs
@@ -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,7 +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.GetUrlSizeHelper
+
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
+
+import java.net.URL
 
 // Define the class constants.
 private const val URL_STRING = "url_string"
@@ -49,9 +55,6 @@ 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)
@@ -104,8 +107,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)
@@ -123,7 +126,7 @@ class SaveDialog : DialogFragment() {
         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)
@@ -170,26 +173,29 @@ 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 urlSize: String
+
+                    // Get the URL size on the IO thread.
+                    withContext(Dispatchers.IO) {
+                        // Get the URL size.
+                        urlSize = GetUrlSizeHelper.getUrl(requireContext(), URL(urlToSave), userAgentString, cookiesEnabled)
+                    }
+
+                    // Display the updated URL.
+                    fileSizeTextView.text = urlSize
+                }
             }
         })
 
         // Return the alert dialog.
         return alertDialog
     }
-}
\ No newline at end of file
+}