-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Improve large file download #17169
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
base: master
Are you sure you want to change the base?
Improve large file download #17169
Changes from all commits
0a11e4b
5106a46
c68f1a2
8d0cb4e
3982935
b31523a
da3eca3
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 |
|---|---|---|
|
|
@@ -16,10 +16,9 @@ import com.owncloud.android.R | |
| import com.owncloud.android.operations.DownloadFileOperation | ||
| import com.owncloud.android.ui.notifications.NotificationUtils | ||
| import com.owncloud.android.utils.theme.ViewThemeUtils | ||
| import java.io.File | ||
| import java.security.SecureRandom | ||
|
|
||
| @Suppress("TooManyFunctions") | ||
| @Suppress("TooManyFunctions", "MagicNumber") | ||
| class DownloadNotificationManager(id: Int, private val context: Context, viewThemeUtils: ViewThemeUtils) : | ||
| WorkerNotificationManager( | ||
| id, | ||
|
|
@@ -29,11 +28,8 @@ class DownloadNotificationManager(id: Int, private val context: Context, viewThe | |
| channelId = NotificationUtils.NOTIFICATION_CHANNEL_DOWNLOAD | ||
| ) { | ||
|
|
||
| private var lastPercent = -1 | ||
|
|
||
| @Suppress("MagicNumber") | ||
| fun prepareForStart(operation: DownloadFileOperation) { | ||
| currentOperationTitle = File(operation.savePath).name | ||
| currentOperationTitle = operation.file.fileName | ||
|
Collaborator
Author
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. Uses file name |
||
|
|
||
| notificationBuilder.run { | ||
| setContentTitle(currentOperationTitle) | ||
|
|
@@ -51,20 +47,12 @@ class DownloadNotificationManager(id: Int, private val context: Context, viewThe | |
| .setProgress(0, 0, false) | ||
| } | ||
|
|
||
| @Suppress("MagicNumber") | ||
| fun updateDownloadProgress(percent: Int, totalToTransfer: Long) { | ||
| // If downloads are so fast, no need to notify again. | ||
| if (percent == lastPercent) { | ||
|
Collaborator
Author
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. Already outer call have this |
||
| return | ||
| } | ||
| lastPercent = percent | ||
|
|
||
| val progressText = NumberFormatter.getPercentageText(percent) | ||
| setProgress(percent, progressText, totalToTransfer < 0) | ||
| showNotification() | ||
| } | ||
|
|
||
| @Suppress("MagicNumber") | ||
| fun dismissNotification() { | ||
| dismissNotification(2000) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,8 @@ | ||
| /* | ||
| * Nextcloud - Android Client | ||
| * | ||
| * SPDX-FileCopyrightText: 2023 Alper Ozturk <alper.ozturk@nextcloud.com> | ||
| * SPDX-FileCopyrightText: 2023 Nextcloud GmbH | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only | ||
| * SPDX-FileCopyrightText: 2026 Alper Ozturk <alper.ozturk@nextcloud.com> | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
| package com.nextcloud.client.jobs.download | ||
|
|
||
|
|
@@ -42,6 +41,7 @@ import com.owncloud.android.utils.theme.ViewThemeUtils | |
| import java.util.AbstractList | ||
| import java.util.Optional | ||
| import java.util.Vector | ||
| import java.util.concurrent.ConcurrentHashMap | ||
| import kotlin.random.Random | ||
|
|
||
| @Suppress("LongParameterList", "TooManyFunctions", "TooGenericExceptionCaught") | ||
|
|
@@ -105,8 +105,6 @@ class FileDownloadWorker( | |
|
|
||
| @Suppress("ReturnCount") | ||
| override suspend fun doWork(): Result { | ||
| trySetForeground() | ||
|
|
||
| return try { | ||
| setUser() | ||
| val remotePath = inputData.keyValueMap[FILE_REMOTE_PATH] as? String? ?: return Result.failure() | ||
|
|
@@ -144,20 +142,21 @@ class FileDownloadWorker( | |
| ) | ||
| } | ||
|
|
||
| private suspend fun trySetForeground() { | ||
| private suspend fun trySetForeground(filename: String) { | ||
| try { | ||
| val foregroundInfo = createWorkerForegroundInfo() | ||
| val foregroundInfo = createWorkerForegroundInfo(filename) | ||
|
Collaborator
Author
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. Fixes empty notification |
||
| setForeground(foregroundInfo) | ||
| } catch (e: Exception) { | ||
| Log_OC.w(TAG, "⚠️ Could not set foreground service: ${e.message}") | ||
| } | ||
| } | ||
|
|
||
| private fun createWorkerForegroundInfo(): ForegroundInfo = ForegroundServiceHelper.createWorkerForegroundInfo( | ||
| notificationManager.getId(), | ||
| notificationManager.getNotification(), | ||
| ForegroundServiceType.DataSync | ||
| ) | ||
| private fun createWorkerForegroundInfo(filename: String): ForegroundInfo = | ||
| ForegroundServiceHelper.createWorkerForegroundInfo( | ||
| notificationManager.getId(), | ||
| notificationManager.getNotification(filename), | ||
| ForegroundServiceType.DataSync | ||
| ) | ||
|
|
||
| private fun removePendingDownload(accountName: String?) { | ||
| pendingDownloads.remove(accountName) | ||
|
|
@@ -188,7 +187,6 @@ class FileDownloadWorker( | |
| ) | ||
|
|
||
| operation.addDownloadDataTransferProgressListener(this) | ||
| operation.addDownloadDataTransferProgressListener(downloadProgressListener) | ||
| val (downloadKey, _) = pendingDownloads.putIfAbsent( | ||
| user?.accountName, | ||
| file.remotePath, | ||
|
|
@@ -246,7 +244,7 @@ class FileDownloadWorker( | |
| } | ||
|
|
||
| @Suppress("TooGenericExceptionCaught", "DEPRECATION") | ||
| private fun downloadFile(downloadKey: String) { | ||
| private suspend fun downloadFile(downloadKey: String) { | ||
| currentDownload = pendingDownloads.get(downloadKey) | ||
|
|
||
| if (currentDownload == null) { | ||
|
|
@@ -262,6 +260,7 @@ class FileDownloadWorker( | |
| } | ||
|
|
||
| lastPercent = 0 | ||
| trySetForeground(currentDownload?.file?.fileName ?: "") | ||
| notificationManager.run { | ||
| prepareForStart(currentDownload!!) | ||
| setContentIntent(intents.detailsIntent(currentDownload!!), PendingIntent.FLAG_IMMUTABLE) | ||
|
|
@@ -401,7 +400,7 @@ class FileDownloadWorker( | |
| totalToTransfer: Long, | ||
| filePath: String | ||
| ) { | ||
| val percent: Int = downloadProgressListener.getPercent(totalTransferredSoFar, totalToTransfer) | ||
| val percent: Int = getPercent(totalTransferredSoFar, totalToTransfer) | ||
| val currentTime = System.currentTimeMillis() | ||
|
|
||
| if (percent != lastPercent && (currentTime - lastUpdateTime) >= minProgressUpdateInterval) { | ||
|
|
@@ -413,13 +412,11 @@ class FileDownloadWorker( | |
|
|
||
| lastPercent = percent | ||
| EventBusFactory.downloadProgressEventBus.post(FileDownloadProgressEvent(percent)) | ||
| downloadProgressListener.onTransferProgress(progressRate, totalTransferredSoFar, totalToTransfer, filePath) | ||
| } | ||
|
|
||
| // CHECK: Is this class still needed after conversion from Foreground Services to Worker? | ||
| inner class FileDownloadProgressListener : OnDatatransferProgressListener { | ||
| private val boundListeners: MutableMap<Long, OnDatatransferProgressListener> = HashMap() | ||
|
|
||
| fun isDownloading(user: User?, file: OCFile?): Boolean = FileDownloadHelper.instance().isDownloading(user, file) | ||
| private val boundListeners = ConcurrentHashMap<Long, OnDatatransferProgressListener>() | ||
|
|
||
| fun addDataTransferProgressListener(listener: OnDatatransferProgressListener?, file: OCFile?) { | ||
| if (file == null || listener == null) { | ||
|
|
||
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.
isDownloadingleft over from foreground services no need this since worker is used