+ override fun saveWithAndroidDownloadManager(dialogFragment: DialogFragment) {
+ // Get the dialog.
+ val dialog = dialogFragment.dialog!!
+
+ // Get handles for the dialog views.
+ val dialogUrlEditText = dialog.findViewById<EditText>(R.id.url_edittext)
+ val downloadDirectoryRadioGroup = dialog.findViewById<RadioGroup>(R.id.download_directory_radiogroup)
+ val dialogFileNameEditText = dialog.findViewById<EditText>(R.id.file_name_edittext)
+
+ // Get the string from the edit texts, which may have been modified by the user.
+ val saveUrlString = dialogUrlEditText.text.toString()
+ val fileNameString = dialogFileNameEditText.text.toString()
+
+ // Get a handle for the system download service.
+ val downloadManager = getSystemService(DOWNLOAD_SERVICE) as DownloadManager
+
+ // Parse the URL.
+ val downloadRequest = DownloadManager.Request(Uri.parse(saveUrlString))
+
+ // Pass cookies to download manager if cookies are enabled. This is required to download files from websites that require a login.
+ // Code contributed 2017 Hendrik Knackstedt. Copyright assigned to Soren Stoutner <soren@stoutner.com>.
+ if (cookieManager.acceptCookie()) {
+ // Get the cookies for the URL.
+ val cookiesString = cookieManager.getCookie(saveUrlString)
+
+ // Add the cookies to the download request. In the HTTP request header, cookies are named `Cookie`.
+ downloadRequest.addRequestHeader("Cookie", cookiesString)
+ }
+
+ // Get the download directory.
+ val downloadDirectory = when (downloadDirectoryRadioGroup.checkedRadioButtonId) {
+ R.id.downloads_radiobutton -> Environment.DIRECTORY_DOWNLOADS
+ R.id.documents_radiobutton -> Environment.DIRECTORY_DOCUMENTS
+ R.id.pictures_radiobutton -> Environment.DIRECTORY_PICTURES
+ else -> Environment.DIRECTORY_MUSIC
+ }
+
+ // Set the download destination.
+ downloadRequest.setDestinationInExternalPublicDir(downloadDirectory, fileNameString)
+
+ // Allow media scanner to index the download if it is a media file. This is automatic for API >= 29.
+ @Suppress("DEPRECATION")
+ if (Build.VERSION.SDK_INT <= 28)
+ downloadRequest.allowScanningByMediaScanner()
+
+ // Add the URL as the description for the download.
+ downloadRequest.setDescription(saveUrlString)
+
+ // Show the download notification after the download is completed.
+ downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
+
+ // Initiate the download.
+ downloadManager.enqueue(downloadRequest)
+ }
+
+ private fun saveWithExternalApp(url: String) {
+ // Create a download intent. Not specifying the action type will display the maximum number of options.
+ val downloadIntent = Intent()
+
+ // Set the URI and the mime type.
+ downloadIntent.setDataAndType(Uri.parse(url), "text/html")
+
+ // Flag the intent to open in a new task.
+ downloadIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
+
+ // Show the chooser.
+ startActivity(Intent.createChooser(downloadIntent, getString(R.string.download_with_external_app)))
+ }
+
+ override fun saveWithPrivacyBrowser(originalUrlString: String, fileNameString: String, dialogFragment: DialogFragment) {