manager: fix download state
This commit is contained in:
@@ -4,6 +4,7 @@ import android.app.Activity.RESULT_OK
|
|||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import android.widget.Toast
|
||||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.compose.foundation.layout.*
|
import androidx.compose.foundation.layout.*
|
||||||
@@ -230,12 +231,16 @@ private fun ModuleList(
|
|||||||
}
|
}
|
||||||
}, onUpdate = {
|
}, onUpdate = {
|
||||||
|
|
||||||
|
val downloading = downloadingText.format(module.name)
|
||||||
download(
|
download(
|
||||||
context,
|
context,
|
||||||
Uri.parse(updateUrl),
|
updateUrl,
|
||||||
module.name,
|
|
||||||
"${module.name}-${module.version}.zip",
|
"${module.name}-${module.version}.zip",
|
||||||
downloadingText.format(module.name)
|
downloading,
|
||||||
|
onDownloaded = onInstallModule,
|
||||||
|
onDownloading = {
|
||||||
|
Toast.makeText(context, downloading, Toast.LENGTH_SHORT).show()
|
||||||
|
}
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -15,18 +15,46 @@ import androidx.compose.runtime.DisposableEffect
|
|||||||
* @author weishu
|
* @author weishu
|
||||||
* @date 2023/6/22.
|
* @date 2023/6/22.
|
||||||
*/
|
*/
|
||||||
|
@SuppressLint("Range")
|
||||||
fun download(context: Context, uri: Uri, title: String, fileName: String, description: String) {
|
fun download(
|
||||||
|
context: Context,
|
||||||
|
url: String,
|
||||||
|
fileName: String,
|
||||||
|
description: String,
|
||||||
|
onDownloaded: (Uri) -> Unit = {},
|
||||||
|
onDownloading: () -> Unit = {}
|
||||||
|
) {
|
||||||
val downloadManager =
|
val downloadManager =
|
||||||
context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
|
context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
|
||||||
val request = DownloadManager.Request(uri)
|
|
||||||
|
val query = DownloadManager.Query()
|
||||||
|
query.setFilterByStatus(DownloadManager.STATUS_RUNNING or DownloadManager.STATUS_PAUSED or DownloadManager.STATUS_PENDING)
|
||||||
|
downloadManager.query(query).use { cursor ->
|
||||||
|
while (cursor.moveToNext()) {
|
||||||
|
val uri = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI))
|
||||||
|
val localUri = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))
|
||||||
|
val status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))
|
||||||
|
val columnTitle = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TITLE))
|
||||||
|
if (url == uri || fileName == columnTitle) {
|
||||||
|
if (status == DownloadManager.STATUS_RUNNING || status == DownloadManager.STATUS_PENDING) {
|
||||||
|
onDownloading()
|
||||||
|
return
|
||||||
|
} else if (status == DownloadManager.STATUS_SUCCESSFUL) {
|
||||||
|
onDownloaded(Uri.parse(localUri))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val request = DownloadManager.Request(Uri.parse(url))
|
||||||
.setDestinationInExternalPublicDir(
|
.setDestinationInExternalPublicDir(
|
||||||
Environment.DIRECTORY_DOWNLOADS,
|
Environment.DIRECTORY_DOWNLOADS,
|
||||||
fileName
|
fileName
|
||||||
)
|
)
|
||||||
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
|
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
|
||||||
.setMimeType("application/zip")
|
.setMimeType("application/zip")
|
||||||
.setTitle(title)
|
.setTitle(fileName)
|
||||||
.setDescription(description)
|
.setDescription(description)
|
||||||
|
|
||||||
downloadManager.enqueue(request)
|
downloadManager.enqueue(request)
|
||||||
|
|||||||
Reference in New Issue
Block a user