Skip to content

Commit

Permalink
improve file download worker
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Dec 30, 2024
1 parent d12464a commit 72f37a8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,8 @@ class DownloadNotificationManager(
) : WorkerNotificationManager(id, context, viewThemeUtils, R.string.downloader_download_in_progress_ticker) {

@Suppress("MagicNumber")
fun prepareForStart(operation: DownloadFileOperation, currentDownloadIndex: Int, totalDownloadSize: Int) {
currentOperationTitle = if (totalDownloadSize > 1) {
String.format(
context.getString(R.string.downloader_notification_manager_download_text),
currentDownloadIndex,
totalDownloadSize,
File(operation.savePath).name
)
} else {
File(operation.savePath).name
}
fun prepareForStart(operation: DownloadFileOperation) {
currentOperationTitle = File(operation.savePath).name

notificationBuilder.run {
setContentTitle(currentOperationTitle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import android.util.Pair
import androidx.core.util.component1
import androidx.core.util.component2
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.work.Worker
import androidx.work.CoroutineWorker
import androidx.work.ForegroundInfo
import androidx.work.WorkerParameters
import com.nextcloud.client.account.User
import com.nextcloud.client.account.UserAccountManager
Expand All @@ -38,10 +39,10 @@ import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.operations.DownloadFileOperation
import com.owncloud.android.operations.DownloadType
import com.owncloud.android.utils.theme.ViewThemeUtils
import java.security.SecureRandom
import java.util.AbstractList
import java.util.Optional
import java.util.Vector
import kotlin.random.Random

@Suppress("LongParameterList", "TooManyFunctions")
class FileDownloadWorker(
Expand All @@ -50,7 +51,7 @@ class FileDownloadWorker(
private var localBroadcastManager: LocalBroadcastManager,
private val context: Context,
params: WorkerParameters
) : Worker(context, params), OnAccountsUpdateListener, OnDatatransferProgressListener {
) : CoroutineWorker(context, params), OnAccountsUpdateListener, OnDatatransferProgressListener {

companion object {
private val TAG = FileDownloadWorker::class.java.simpleName
Expand All @@ -74,7 +75,6 @@ class FileDownloadWorker(
const val ACTIVITY_NAME = "ACTIVITY_NAME"
const val PACKAGE_NAME = "PACKAGE_NAME"
const val CONFLICT_UPLOAD_ID = "CONFLICT_UPLOAD_ID"

const val EXTRA_DOWNLOAD_RESULT = "EXTRA_DOWNLOAD_RESULT"
const val EXTRA_REMOTE_PATH = "EXTRA_REMOTE_PATH"
const val EXTRA_LINKED_TO_PATH = "EXTRA_LINKED_TO_PATH"
Expand All @@ -96,7 +96,7 @@ class FileDownloadWorker(

private val intents = FileDownloadIntents(context)
private var notificationManager = DownloadNotificationManager(
SecureRandom().nextInt(),
Random.nextInt(),
context,
viewThemeUtils
)
Expand All @@ -112,47 +112,45 @@ class FileDownloadWorker(
private var downloadError: FileDownloadError? = null

@Suppress("TooGenericExceptionCaught")
override fun doWork(): Result {
override suspend fun doWork(): Result {
val foregroundInfo = createWorkerForegroundInfo()
setForeground(foregroundInfo)

return try {
val requestDownloads = getRequestDownloads()
setUser()
val remotePath = inputData.keyValueMap[FILE_REMOTE_PATH] as String? ?: return Result.failure()
val ocFile = fileDataStorageManager?.getFileByEncryptedRemotePath(remotePath) ?: return Result.failure()
val requestDownloads = getRequestDownloads(ocFile)
addAccountUpdateListener()

val foregroundInfo = ForegroundServiceHelper.createWorkerForegroundInfo(
notificationManager.getId(),
notificationManager.getNotification(),
ForegroundServiceType.DataSync
)
setForegroundAsync(foregroundInfo)

requestDownloads.forEachIndexed { currentDownloadIndex, requestedDownload ->
downloadFile(requestedDownload, currentDownloadIndex, requestDownloads.size)
requestDownloads.forEach {
downloadFile(it)
}

downloadError?.let {
showDownloadErrorNotification(it)
notificationManager.dismissNotification()
}

setIdleWorkerState()

Log_OC.e(TAG, "FilesDownloadWorker successfully completed")
Result.success()
} catch (t: Throwable) {
notificationManager.dismissNotification()
notificationManager.showNewNotification(context.getString(R.string.downloader_unexpected_error))
Log_OC.e(TAG, "Error caught at FilesDownloadWorker(): " + t.localizedMessage)
setIdleWorkerState()
Result.failure()
} finally {
Log_OC.e(TAG, "FilesDownloadWorker cleanup")
notificationManager.dismissNotification()
setIdleWorkerState()
}
}

override fun onStopped() {
Log_OC.e(TAG, "FilesDownloadWorker stopped")

notificationManager.dismissNotification()
setIdleWorkerState()

super.onStopped()
private fun createWorkerForegroundInfo(): ForegroundInfo {
return ForegroundServiceHelper.createWorkerForegroundInfo(
notificationManager.getId(),
notificationManager.getNotification(),
ForegroundServiceType.DataSync
)
}

private fun setWorkerState(user: User?) {
Expand All @@ -167,9 +165,8 @@ class FileDownloadWorker(
pendingDownloads.remove(accountName)
}

private fun getRequestDownloads(): AbstractList<String> {
setUser()
val files = getFiles()
private fun getRequestDownloads(ocFile: OCFile): AbstractList<String> {
val files = getFiles(ocFile)
val downloadType = getDownloadType()

conflictUploadId = inputData.keyValueMap[CONFLICT_UPLOAD_ID] as Long?
Expand Down Expand Up @@ -222,10 +219,7 @@ class FileDownloadWorker(
fileDataStorageManager = FileDataStorageManager(user, context.contentResolver)
}

private fun getFiles(): List<OCFile> {
val remotePath = inputData.keyValueMap[FILE_REMOTE_PATH] as String?
val file = fileDataStorageManager?.getFileByEncryptedRemotePath(remotePath) ?: return listOf()

private fun getFiles(file: OCFile): List<OCFile> {
return if (file.isFolder) {
fileDataStorageManager?.getAllFilesRecursivelyInsideFolder(file) ?: listOf()
} else {
Expand All @@ -252,7 +246,7 @@ class FileDownloadWorker(
}

@Suppress("TooGenericExceptionCaught", "DEPRECATION")
private fun downloadFile(downloadKey: String, currentDownloadIndex: Int, totalDownloadSize: Int) {
private fun downloadFile(downloadKey: String) {
currentDownload = pendingDownloads.get(downloadKey)

if (currentDownload == null) {
Expand All @@ -269,9 +263,8 @@ class FileDownloadWorker(
}

lastPercent = 0

notificationManager.run {
prepareForStart(currentDownload!!, currentDownloadIndex + 1, totalDownloadSize)
prepareForStart(currentDownload!!)
setContentIntent(intents.detailsIntent(currentDownload!!), PendingIntent.FLAG_IMMUTABLE)
}

Expand Down

0 comments on commit 72f37a8

Please sign in to comment.