diff options
author | Allan Wang <me@allanwang.ca> | 2017-10-24 23:29:55 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-24 23:29:55 -0400 |
commit | c2ca9066c6fd760bd6ef5d2f8f0530a89bfa7b66 (patch) | |
tree | a54665bb873b650b8f6f03b76cd59456ef79e296 /app/src/main/kotlin/com/pitchedapps/frost/utils | |
parent | 64dbf74b7a44a25f41ed7ff2ebfa11db0bc91769 (diff) | |
download | frost-c2ca9066c6fd760bd6ef5d2f8f0530a89bfa7b66.tar.gz frost-c2ca9066c6fd760bd6ef5d2f8f0530a89bfa7b66.tar.bz2 frost-c2ca9066c6fd760bd6ef5d2f8f0530a89bfa7b66.zip |
WIP: Feature/pip video 2 (#405)
* Add dependency
* Test new video view
* Add initial video bindings
* Implement drag to dismiss
* Begin initial integration
* Fix typo
* Fix up url formatter
* Update changelog
* Create first fully integrated video build
* Update translations
* Update translations 2
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/utils')
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/utils/Downloader.kt | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/Downloader.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/Downloader.kt index 53cede18..3e1e1dde 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Downloader.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/Downloader.kt @@ -11,6 +11,7 @@ import ca.allanwang.kau.permissions.kauRequestPermissions import ca.allanwang.kau.utils.string import com.pitchedapps.frost.R import com.pitchedapps.frost.dbflow.loadFbCookie +import com.pitchedapps.frost.facebook.USER_AGENT_BASIC /** @@ -18,23 +19,37 @@ import com.pitchedapps.frost.dbflow.loadFbCookie * * With reference to <a href="https://stackoverflow.com/questions/33434532/android-webview-download-files-like-browsers-do">Stack Overflow</a> */ -fun Context.frostDownload(url: String, userAgent: String, contentDisposition: String, mimeType: String, contentLength: Long) { - L.d("Received download request", "Download $url") - val uri = Uri.parse(url) ?: return +fun Context.frostDownload(url: String?, + userAgent: String = USER_AGENT_BASIC, + contentDisposition: String? = null, + mimeType: String? = null, + contentLength: Long = 0L) { + url ?: return + frostDownload(Uri.parse(url), userAgent, contentDisposition, mimeType, contentLength) +} + +fun Context.frostDownload(uri: Uri?, + userAgent: String = USER_AGENT_BASIC, + contentDisposition: String? = null, + mimeType: String? = null, + contentLength: Long = 0L) { + uri ?: return + L.d("Received download request", "Download $uri") if (uri.scheme != "http" && uri.scheme != "https") - return L.e("Invalid download attempt", url) + return L.e("Invalid download attempt", uri.toString()) kauRequestPermissions(PERMISSION_WRITE_EXTERNAL_STORAGE) { granted, _ -> if (!granted) return@kauRequestPermissions val request = DownloadManager.Request(uri) request.setMimeType(mimeType) val cookie = loadFbCookie(Prefs.userId) ?: return@kauRequestPermissions + val title = URLUtil.guessFileName(uri.toString(), contentDisposition, mimeType) request.addRequestHeader("cookie", cookie.cookie) request.addRequestHeader("User-Agent", userAgent) request.setDescription(string(R.string.downloading)) - request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType)) + request.setTitle(title) request.allowScanningByMediaScanner() request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) - request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Frost/" + URLUtil.guessFileName(url, contentDisposition, mimeType)) + request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Frost/$title") val dm = getSystemService(DOWNLOAD_SERVICE) as DownloadManager dm.enqueue(request) } |