Skip to content

Address code review comments #3669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 12 additions & 17 deletions app/src/main/java/fr/free/nrw/commons/utils/DownloadUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,22 @@ object DownloadUtils {
*/
@JvmStatic
fun downloadMedia(activity: Activity?, m: Media) {
val imageUrl = m.getImageUrl()
var fileName = m.getFilename()
if (imageUrl == null || fileName == null || activity == null
) {
Timber.d(
"Skipping download media as either imageUrl %s or filename %s activity is null",
imageUrl, fileName
)
val imageUrl = m.imageUrl
var fileName = m.filename
if (imageUrl == null || fileName == null || activity == null) {
Timber.d("Skipping download media as either imageUrl $imageUrl or filename $fileName activity is null")
return
}
// Strip 'File:' from beginning of filename, we really shouldn't store it
fileName = fileName.replaceFirst("^File:".toRegex(), "")
fileName = fileName.substringAfter("File:")
val imageUri = Uri.parse(imageUrl)
val req = DownloadManager.Request(imageUri)
//These are not the image title and description fields, they are download descs for notifications
req.setDescription(activity.getString(R.string.app_name))
req.setTitle(m.displayTitle)
req.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)
// Modern Android updates the gallery automatically. Yay!
req.allowScanningByMediaScanner()
req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
val req = DownloadManager.Request(imageUri).apply {
setTitle(m.displayTitle)
setDescription(activity.getString(R.string.app_name))
setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)
allowScanningByMediaScanner()
setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
}
PermissionUtils.checkPermissionsAndPerformAction(
activity,
permission.WRITE_EXTERNAL_STORAGE,
Expand Down