X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fasynctasks%2FPrepareSaveDialog.java;h=641bbe07da626092faa0987833ab6979f70bc38f;hb=b870c254c26d19749fef9e3dbe9894f8f40c32eb;hp=9b1dcd0974a27b8d011abc3b5ae7423de0327c75;hpb=f52255e6eeb1a6be9f190e733563cc37b5d1f2b5;p=PrivacyBrowserAndroid.git diff --git a/app/src/main/java/com/stoutner/privacybrowser/asynctasks/PrepareSaveDialog.java b/app/src/main/java/com/stoutner/privacybrowser/asynctasks/PrepareSaveDialog.java index 9b1dcd09..641bbe07 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/asynctasks/PrepareSaveDialog.java +++ b/app/src/main/java/com/stoutner/privacybrowser/asynctasks/PrepareSaveDialog.java @@ -24,12 +24,13 @@ import android.content.Context; import android.net.Uri; import android.os.AsyncTask; import android.webkit.CookieManager; +import android.webkit.MimeTypeMap; import androidx.fragment.app.DialogFragment; import androidx.fragment.app.FragmentManager; import com.stoutner.privacybrowser.R; -import com.stoutner.privacybrowser.dialogs.SaveDialog; +import com.stoutner.privacybrowser.dialogs.SaveWebpageDialog; import com.stoutner.privacybrowser.helpers.ProxyHelper; import java.lang.ref.WeakReference; @@ -124,11 +125,18 @@ public class PrepareSaveDialog extends AsyncTask { formattedFileSize = context.getString(R.string.invalid_url); // Set the file name according to the URL. - fileNameString = getFileNameFromUrl(context, urlString); + fileNameString = getFileNameFromUrl(context, urlString, null); } else { // The response code is not an error message. - // Get the content length and disposition headers. + // Get the headers. String contentLengthString = httpUrlConnection.getHeaderField("Content-Length"); String contentDispositionString = httpUrlConnection.getHeaderField("Content-Disposition"); + String contentTypeString = httpUrlConnection.getContentType(); + + // Remove anything after the MIME type in the content type string. + if (contentTypeString.contains(";")) { + // Remove everything beginning with the `;`. + contentTypeString = contentTypeString.substring(0, contentTypeString.indexOf(";")); + } // Only process the content length string if it isn't null. if (contentLengthString != null) { @@ -140,7 +148,7 @@ public class PrepareSaveDialog extends AsyncTask { } // Get the file name string from the content disposition. - fileNameString = getFileNameFromContentDisposition(context, contentDispositionString, urlString); + fileNameString = getFileNameFromHeaders(context, contentDispositionString, contentTypeString, urlString); } } finally { // Disconnect the HTTP URL connection. @@ -151,7 +159,7 @@ public class PrepareSaveDialog extends AsyncTask { formattedFileSize = context.getString(R.string.invalid_url); // Set the file name according to the URL. - fileNameString = getFileNameFromUrl(context, urlString); + fileNameString = getFileNameFromUrl(context, urlString, null); } // Return the formatted file size and name as a string array. @@ -172,7 +180,7 @@ public class PrepareSaveDialog extends AsyncTask { } // Instantiate the save dialog. - DialogFragment saveDialogFragment = SaveDialog.saveUrl(saveType, urlString, fileStringArray[0], fileStringArray[1], userAgent, cookiesEnabled); + DialogFragment saveDialogFragment = SaveWebpageDialog.saveWebpage(saveType, urlString, fileStringArray[0], fileStringArray[1], userAgent, cookiesEnabled); // Show the save dialog. It must be named `save_dialog` so that the file picker can update the file name. saveDialogFragment.show(fragmentManager, activity.getString(R.string.save_dialog)); @@ -180,7 +188,7 @@ public class PrepareSaveDialog extends AsyncTask { // Content dispositions can contain other text besides the file name, and they can be in any order. // Elements are separated by semicolons. Sometimes the file names are contained in quotes. - public static String getFileNameFromContentDisposition(Context context, String contentDispositionString, String urlString) { + public static String getFileNameFromHeaders(Context context, String contentDispositionString, String contentTypeString, String urlString) { // Define a file name string. String fileNameString; @@ -208,20 +216,20 @@ public class PrepareSaveDialog extends AsyncTask { // Remove the last character. fileNameString = fileNameString.substring(0, fileNameString.length() - 1); } - } else { // The content disposition does not contain a filename. + } else { // The headers contain no useful information. // Get the file name string from the URL. - fileNameString = getFileNameFromUrl(context, urlString); + fileNameString = getFileNameFromUrl(context, urlString, contentTypeString); } } else { // The content disposition is null. // Get the file name string from the URL. - fileNameString = getFileNameFromUrl(context, urlString); + fileNameString = getFileNameFromUrl(context, urlString, contentTypeString); } // Return the file name string. return fileNameString; } - private static String getFileNameFromUrl(Context context, String urlString) { + private static String getFileNameFromUrl(Context context, String urlString, String contentTypeString) { // Convert the URL string to a URI. Uri uri = Uri.parse(urlString); @@ -231,6 +239,11 @@ public class PrepareSaveDialog extends AsyncTask { // Use a default file name if the last path segment is null. if (lastPathSegment == null) { lastPathSegment = context.getString(R.string.file); + + if (MimeTypeMap.getSingleton().hasMimeType(contentTypeString)) { // The content type contains a MIME type. + // Add the file extension that matches the MIME type. + lastPathSegment = lastPathSegment + "." + MimeTypeMap.getSingleton().getExtensionFromMimeType(contentTypeString); + } } // Return the last path segment as the file name.