+ // `onProgressUpdate()` operates on the UI thread.
+ @Override
+ protected void onProgressUpdate(Long... numberOfBytesDownloaded) {
+ // Get a handle for the activity.
+ Activity activity = activityWeakReference.get();
+
+ // Abort if the activity is gone.
+ if ((activity == null) || activity.isFinishing()) {
+ return;
+ }
+
+ // Format the number of bytes downloaded.
+ String formattedNumberOfBytesDownloaded = NumberFormat.getInstance().format(numberOfBytesDownloaded[0]);
+
+ // Check to see if the file size is known.
+ if (fileSize == -1) { // The size of the download file is not known.
+ // Update the snackbar.
+ savingFileSnackbar.setText(activity.getString(R.string.saving_file) + " " + formattedNumberOfBytesDownloaded + " " + activity.getString(R.string.bytes) + " - " + urlString);
+ } else { // The size of the download file is known.
+ // Calculate the download percentage.
+ long downloadPercentage = (numberOfBytesDownloaded[0] * 100) / fileSize;
+
+ // Update the snackbar.
+ savingFileSnackbar.setText(activity.getString(R.string.saving_file) + " " + downloadPercentage + "% - " + formattedNumberOfBytesDownloaded + " " + activity.getString(R.string.bytes) + " / " + formattedFileSize + " " +
+ activity.getString(R.string.bytes) + " - " + urlString);
+ }
+ }
+