-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Convert download code to kotlin #3665
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package fr.free.nrw.commons.utils | ||
|
|
||
| import android.Manifest.permission | ||
| import android.app.Activity | ||
| import android.app.DownloadManager | ||
| import android.content.Context | ||
| import android.net.Uri | ||
| import android.os.Environment | ||
| import android.widget.Toast | ||
| import fr.free.nrw.commons.Media | ||
| import fr.free.nrw.commons.R | ||
| import timber.log.Timber | ||
|
|
||
| object DownloadUtils { | ||
| /** | ||
| * Start the media file downloading to the local SD card/storage. The file can then be opened in | ||
| * Gallery or other apps. | ||
| * | ||
| * @param m Media file to download | ||
| */ | ||
| @JvmStatic | ||
| fun downloadMedia(activity: Activity?, m: Media) { | ||
| val imageUrl = m.getImageUrl() | ||
| var fileName = m.getFilename() | ||
| if (imageUrl == null || fileName == null || activity == null | ||
| ) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. weird place for a parentheses |
||
| Timber.d( | ||
| "Skipping download media as either imageUrl %s or filename %s activity is null", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use kotlin string interpolation here |
||
| imageUrl, fileName | ||
| ) | ||
| return | ||
| } | ||
| // Strip 'File:' from beginning of filename, we really shouldn't store it | ||
| fileName = fileName.replaceFirst("^File:".toRegex(), "") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| val imageUri = Uri.parse(imageUrl) | ||
| val req = DownloadManager.Request(imageUri) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can use |
||
| //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) | ||
| PermissionUtils.checkPermissionsAndPerformAction( | ||
| activity, | ||
| permission.WRITE_EXTERNAL_STORAGE, | ||
| { enqueueRequest(activity, req) }, | ||
| { | ||
| Toast.makeText( | ||
| activity, | ||
| R.string.download_failed_we_cannot_download_the_file_without_storage_permission, | ||
| Toast.LENGTH_SHORT | ||
| ).show() | ||
| }, | ||
| R.string.storage_permission, | ||
| R.string.write_storage_permission_rationale | ||
| ) | ||
| } | ||
|
|
||
| private fun enqueueRequest(activity: Activity, req: DownloadManager.Request) { | ||
| val systemService = | ||
| activity.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager | ||
| systemService?.enqueue(req) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't the IDE be suggesting using propertyAccess syntax?