]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/DownloadFile.java
Make file download names more accurate. Updated German translation provided by Aaron...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / DownloadFile.java
index 627335df711dc8c5528999c01b6091c5d7d10e01..0a1d1dcab81a713c2a0b7f090f85f109453867e6 100644 (file)
@@ -48,20 +48,17 @@ public class DownloadFile extends DialogFragment {
         Bundle argumentsBundle = new Bundle();
 
         String fileNameString;
-        if (!contentDisposition.isEmpty()) {  // Extract `fileNameString` from `contentDisposition` using the substring beginning after `filename="` and ending one character before the end of `contentDisposition`.
-            fileNameString = contentDisposition.substring(contentDisposition.indexOf("filename=\"") + 10, contentDisposition.length() - 1);
+        if (!contentDisposition.isEmpty()) {  // Extract `fileNameString` from `contentDisposition` using the substring beginning after `filename="` and ending with the next `"`.
+            fileNameString = contentDisposition.substring(contentDisposition.indexOf("filename=\"") + 10, contentDisposition.indexOf("\"", contentDisposition.indexOf("filename=\"") + 10));
         } else {  // `contentDisposition` is empty, so use the last path segment of the URL as the file name.
             Uri downloadUri = Uri.parse(urlString);
             fileNameString = downloadUri.getLastPathSegment();
         }
 
-        // Convert `contentLength` to MB and store it in `fileSizeString`.  `%.3g` displays the three most significant digits.
-        String fileSizeString = String.format(Locale.getDefault(), "%.3g", (float) contentLength / 1048576) + " MB";
-
         // Store the variables in the `Bundle`.
         argumentsBundle.putString("URL", urlString);
         argumentsBundle.putString("File_Name", fileNameString);
-        argumentsBundle.putString("File_Size", fileSizeString);
+        argumentsBundle.putLong("File_Size", contentLength);
 
         // Add `argumentsBundle` to this instance of `DownloadFile`.
         DownloadFile thisDownloadFileDialog = new DownloadFile();
@@ -76,7 +73,16 @@ public class DownloadFile extends DialogFragment {
         // Store the strings in the local class variables.
         downloadUrl = getArguments().getString("URL");
         downloadFileName = getArguments().getString("File_Name");
-        fileSize = getArguments().getString("File_Size");
+
+        // Get the `File_Size`.
+        long fileSizeLong = getArguments().getLong("File_Size");
+
+        // Convert `fileSizeLong` to a String.
+        if (fileSizeLong == -1) {  // We don't know the file size.
+            fileSize = getString(R.string.unknown_size);
+        } else {  // Convert `fileSize` to MB and store it in `fileSizeString`.  `%.3g` displays the three most significant digits.
+            fileSize = String.format(Locale.getDefault(), "%.3g", (float) fileSizeLong / 1048576) + " MB";
+        }
     }
 
     // The public interface is used to send information back to the parent activity.