package com.pitchedapps.frost.utils import android.app.DownloadManager import android.content.Context import android.content.Context.DOWNLOAD_SERVICE import android.net.Uri import android.os.Environment import android.webkit.URLUtil import ca.allanwang.kau.permissions.PERMISSION_WRITE_EXTERNAL_STORAGE import ca.allanwang.kau.permissions.kauRequestPermissions import ca.allanwang.kau.utils.string import com.pitchedapps.frost.R import com.pitchedapps.frost.dbflow.loadFbCookie /** * Created by Allan Wang on 2017-08-04. * * With reference to Stack Overflow */ 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 if (uri.scheme != "http" && uri.scheme != "https") return L.e("Invalid download attempt", url) 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 request.addRequestHeader("cookie", cookie.cookie) request.addRequestHeader("User-Agent", userAgent) request.setDescription(string(R.string.downloading)) request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType)) request.allowScanningByMediaScanner() request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Frost/" + URLUtil.guessFileName(url, contentDisposition, mimeType)) val dm = getSystemService(DOWNLOAD_SERVICE) as DownloadManager dm.enqueue(request) } }