X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fdialogs%2FSaveWebpageDialog.kt;h=15b9c85e0fc9e2b21552f3ab5d92c842baa58bc2;hp=b27e1703b6537b04022560eada2122b9f4556f6d;hb=568ac6354411aa26c42077d2b80f8770f6e64695;hpb=031def95c6d9bfc14113fe86b4a5690233d93ce2 diff --git a/app/src/main/java/com/stoutner/privacybrowser/dialogs/SaveWebpageDialog.kt b/app/src/main/java/com/stoutner/privacybrowser/dialogs/SaveWebpageDialog.kt index b27e1703..15b9c85e 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/dialogs/SaveWebpageDialog.kt +++ b/app/src/main/java/com/stoutner/privacybrowser/dialogs/SaveWebpageDialog.kt @@ -25,6 +25,7 @@ import android.content.Context import android.content.DialogInterface import android.content.Intent import android.content.res.Configuration +import android.net.Uri import android.os.AsyncTask import android.os.Bundle import android.text.Editable @@ -63,7 +64,7 @@ class SaveWebpageDialog : DialogFragment() { // The public interface is used to send information back to the parent activity. interface SaveWebpageListener { - fun onSaveWebpage(saveType: Int, originalUrlString: String?, dialogFragment: DialogFragment) + fun onSaveWebpage(saveType: Int, originalUrlString: String, dialogFragment: DialogFragment) } override fun onAttach(context: Context) { @@ -77,11 +78,12 @@ class SaveWebpageDialog : DialogFragment() { companion object { // Define the companion object constants. These can be moved to class constants once all of the code has transitioned to Kotlin. const val SAVE_URL = 0 - const val SAVE_IMAGE = 1 + const val SAVE_ARCHIVE = 1 + const val SAVE_IMAGE = 2 // `@JvmStatic` will no longer be required once all the code has transitioned to Kotlin. @JvmStatic - fun saveWebpage(saveType: Int, urlString: String?, fileSizeString: String?, fileNameString: String, userAgentString: String?, cookiesEnabled: Boolean): SaveWebpageDialog { + fun saveWebpage(saveType: Int, urlString: String, fileSizeString: String?, fileNameString: String?, userAgentString: String?, cookiesEnabled: Boolean): SaveWebpageDialog { // Create an arguments bundle. val argumentsBundle = Bundle() @@ -109,9 +111,9 @@ class SaveWebpageDialog : DialogFragment() { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { // Get the arguments from the bundle. val saveType = requireArguments().getInt(SAVE_TYPE) - val originalUrlString = requireArguments().getString(URL_STRING) + val originalUrlString = requireArguments().getString(URL_STRING)!! val fileSizeString = requireArguments().getString(FILE_SIZE_STRING) - val fileNameString = requireArguments().getString(FILE_NAME_STRING)!! + var fileNameString = requireArguments().getString(FILE_NAME_STRING) val userAgentString = requireArguments().getString(USER_AGENT_STRING) val cookiesEnabled = requireArguments().getBoolean(COOKIES_ENABLED) @@ -121,18 +123,28 @@ class SaveWebpageDialog : DialogFragment() { // Get the current theme status. val currentThemeStatus = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK - // Set the title and icon according to the save type. + // Configure the dialog according to the save type. when (saveType) { SAVE_URL -> { // Set the title. dialogBuilder.setTitle(R.string.save_url) // Set the icon according to the theme. - if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) { - dialogBuilder.setIcon(R.drawable.copy_enabled_day) - } else { - dialogBuilder.setIcon(R.drawable.copy_enabled_night) - } + dialogBuilder.setIconAttribute(R.attr.copyBlueIcon) + } + + SAVE_ARCHIVE -> { + // Set the title. + dialogBuilder.setTitle(R.string.save_archive) + + // Set the icon according to the theme. + dialogBuilder.setIconAttribute(R.attr.domStorageBlueIcon) + + // Convert the URL to a URI. + val uri = Uri.parse(originalUrlString) + + // Build a file name string based on the host from the URI. + fileNameString = uri.host + ".mht" } SAVE_IMAGE -> { @@ -140,11 +152,13 @@ class SaveWebpageDialog : DialogFragment() { dialogBuilder.setTitle(R.string.save_image) // Set the icon according to the theme. - if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) { - dialogBuilder.setIcon(R.drawable.images_enabled_day) - } else { - dialogBuilder.setIcon(R.drawable.images_enabled_night) - } + dialogBuilder.setIconAttribute(R.attr.imagesBlueIcon) + + // Convert the URL to a URI. + val uri = Uri.parse(originalUrlString) + + // Build a file name string based on the host from the URI. + fileNameString = uri.host + ".png" } } @@ -191,7 +205,7 @@ class SaveWebpageDialog : DialogFragment() { // Modify the layout based on the save type. if (saveType == SAVE_URL) { // A URL is being saved. // 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. - if (originalUrlString!!.startsWith("data:")) { // The URL contains the entire data of an image. + if (originalUrlString.startsWith("data:")) { // The URL contains the entire data of an image. // 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. val urlSubstring = originalUrlString.substring(0, 100) + "…"