X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fdialogs%2FSaveWebpageDialog.kt;h=3402d4f7861c01231c95b8bb8818dd42d0429164;hb=1d656c562831f535aa33903d44198dd890393f4f;hp=782526d0f761ed94adc7877978f2717c5aa404a1;hpb=725e4a525bed43f46e24ecc97eafcc339a48939c;p=PrivacyBrowserAndroid.git 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 782526d0..3402d4f7 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/dialogs/SaveWebpageDialog.kt +++ b/app/src/main/java/com/stoutner/privacybrowser/dialogs/SaveWebpageDialog.kt @@ -19,12 +19,11 @@ package com.stoutner.privacybrowser.dialogs -import android.annotation.SuppressLint import android.app.Dialog 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 +62,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) { @@ -82,7 +81,7 @@ class SaveWebpageDialog : DialogFragment() { // `@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() @@ -105,35 +104,26 @@ class SaveWebpageDialog : DialogFragment() { } } - // `@SuppressLint("InflateParams")` removes the warning about using null as the parent view group when inflating the alert dialog. - @SuppressLint("InflateParams") 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) // Use an alert dialog builder to create the alert dialog. val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog) - // 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 -> { @@ -141,11 +131,13 @@ class SaveWebpageDialog : DialogFragment() { dialogBuilder.setTitle(R.string.save_archive) // Set the icon according to the theme. - if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) { - dialogBuilder.setIcon(R.drawable.dom_storage_cleared_day) - } else { - dialogBuilder.setIcon(R.drawable.dom_storage_cleared_night) - } + 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 -> { @@ -153,16 +145,18 @@ 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" } } - // Set the view. The parent view is null because it will be assigned by the alert dialog. - dialogBuilder.setView(layoutInflater.inflate(R.layout.save_webpage_dialog, null)) + // Set the view. + dialogBuilder.setView(R.layout.save_webpage_dialog) // Set the cancel button listener. Using `null` as the listener closes the dialog without doing anything else. dialogBuilder.setNegativeButton(R.string.cancel, null) @@ -204,7 +198,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) + "…"