From d52fa0a854845de07878f2f312dc8982a9abeba6 Mon Sep 17 00:00:00 2001 From: Abdourahamane Boinaidi Date: Fri, 20 Dec 2024 11:42:35 +0100 Subject: [PATCH] refactor: Update DownloadManagerUtils --- .../infomaniak/core2/DownloadManagerUtils.kt | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Core2/src/main/java/com/infomaniak/core2/DownloadManagerUtils.kt b/Core2/src/main/java/com/infomaniak/core2/DownloadManagerUtils.kt index dadb8ee870..1794498192 100644 --- a/Core2/src/main/java/com/infomaniak/core2/DownloadManagerUtils.kt +++ b/Core2/src/main/java/com/infomaniak/core2/DownloadManagerUtils.kt @@ -18,6 +18,7 @@ package com.infomaniak.core2 import android.app.DownloadManager +import android.app.DownloadManager.Request import android.content.Context import android.database.Cursor import android.net.Uri @@ -40,21 +41,21 @@ object DownloadManagerUtils { url: String, name: String, userAgent: String, - extraHeaders: Iterable> = emptySet() + extraHeaders: Iterable> = emptySet(), ) { val formattedName = name.replace(regexInvalidSystemChar, "_").replace("%", "_").let { // fix IllegalArgumentException only on Android 10 if multi dot if (SDK_INT == 29) it.replace(Regex("\\.{2,}"), ".") else it } - DownloadManager.Request(Uri.parse(url)).apply { - setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE) + Request(Uri.parse(url)).apply { + setAllowedNetworkTypes(Request.NETWORK_WIFI or Request.NETWORK_MOBILE) setTitle(formattedName) setDescription(appName) setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, name) addHeaders(userAgent, extraHeaders) - setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) + setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) val downloadManager = appCtx.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager val downloadReference = downloadManager.enqueue(this) @@ -62,18 +63,16 @@ object DownloadManagerUtils { } } - private fun DownloadManager.Request.addHeaders(userAgent: String, extraHeaders: Iterable>) { + private fun Request.addHeaders(userAgent: String, extraHeaders: Iterable>) { addRequestHeader("Accept-Encoding", "gzip") addRequestHeader("App-Version", "Android $appVersionName") addRequestHeader("User-Agent", userAgent) - extraHeaders.forEach { (key, value) -> - addRequestHeader(key, value) - } + extraHeaders.forEach { (key, value) -> addRequestHeader(key, value) } } private fun handleDownloadManagerErrors(downloadManager: DownloadManager, downloadReference: Long) { scope.launch { - delay(1_000L) + delay(1_000L) // We wait a little to make sure we have the errors from the query DownloadManager.Query().also { query -> query.setFilterById(downloadReference) Dispatchers.IO {