Skip to content

Commit

Permalink
Fix worker tag
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Jan 3, 2024
1 parent d1a44bf commit 1ca5641
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
package com.nextcloud.client.files.downloader

import com.nextcloud.client.account.User
import com.nextcloud.client.files.downloader.FileDownloadWorker.Companion.cancelAllDownloads
import com.nextcloud.client.files.downloader.FileDownloadWorker.Companion.folderDownloadStatusPair
import com.nextcloud.client.files.downloader.FileDownloadWorker.Companion.removePendingDownload
import com.nextcloud.client.jobs.BackgroundJobManager
import com.owncloud.android.MainApp
import com.owncloud.android.datamodel.FileDataStorageManager
Expand Down Expand Up @@ -64,7 +61,7 @@ class FileDownloadHelper {
}

return if (file.isFolder) {
isFolderDownloading(file)
FileDownloadWorker.isFolderDownloading(file)
} else {
backgroundJobManager.isStartFileDownloadJobScheduled(
user,
Expand All @@ -73,17 +70,8 @@ class FileDownloadHelper {
}
}

private fun isFolderDownloading(folder: OCFile): Boolean {
for ((id, status) in folderDownloadStatusPair) {
return id == folder.fileId && status
}

return false
}

fun cancelPendingOrCurrentDownloads(user: User?, file: OCFile?) {
if (user == null || file == null) return
cancelAllDownloads()
backgroundJobManager.cancelFilesDownloadJob(user, file)
}

Expand All @@ -95,8 +83,6 @@ class FileDownloadHelper {

currentDownload.cancel()
}

removePendingDownload(accountName)
}

fun saveFile(
Expand Down Expand Up @@ -140,7 +126,6 @@ class FileDownloadHelper {
}

fun downloadFolder(folder: OCFile, user: User, files: List<OCFile>) {
folderDownloadStatusPair[folder.fileId] = true
backgroundJobManager.startFolderDownloadJob(folder, user, files)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ package com.nextcloud.client.files.downloader
import android.accounts.Account
import android.accounts.AccountManager
import android.accounts.OnAccountsUpdateListener
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import androidx.core.util.component1
Expand All @@ -39,8 +38,6 @@ import com.nextcloud.client.account.UserAccountManager
import com.nextcloud.java.util.Optional
import com.nextcloud.model.WorkerState
import com.nextcloud.model.WorkerStateLiveData
import com.owncloud.android.MainApp
import com.owncloud.android.R
import com.owncloud.android.datamodel.FileDataStorageManager
import com.owncloud.android.datamodel.OCFile
import com.owncloud.android.datamodel.UploadsStorageManager
Expand Down Expand Up @@ -71,7 +68,7 @@ class FileDownloadWorker(
companion object {
private val TAG = FileDownloadWorker::class.java.simpleName

var folderDownloadStatusPair = HashMap<Long, Boolean>()
private var folderDownloadStatusPair = HashMap<Long, Boolean>()

const val FOLDER_ID = "FOLDER_ID"
const val USER_NAME = "USER"
Expand All @@ -89,31 +86,21 @@ class FileDownloadWorker(
const val EXTRA_LINKED_TO_PATH = "LINKED_TO"
const val ACCOUNT_NAME = "ACCOUNT_NAME"

fun isFolderDownloading(folder: OCFile): Boolean {
for ((id, status) in folderDownloadStatusPair) {
return id == folder.fileId && status
}

return false
}

fun getDownloadAddedMessage(): String {
return FileDownloadWorker::class.java.name + "DOWNLOAD_ADDED"
}

fun getDownloadFinishMessage(): String {
return FileDownloadWorker::class.java.name + "DOWNLOAD_FINISH"
}

private val pendingDownloads = IndexedForest<DownloadFileOperation>()

fun removePendingDownload(accountName: String?) {
pendingDownloads.remove(accountName)
}

fun cancelAllDownloads() {
pendingDownloads.all.forEach {
it.value.payload?.cancel()
}

val notificationManager =
MainApp.getAppContext().getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.cancel(R.string.downloader_download_in_progress_ticker)

pendingDownloads.all.clear()
}
}

private var currentDownload: DownloadFileOperation? = null
Expand All @@ -127,6 +114,7 @@ class FileDownloadWorker(
private var downloadClient: OwnCloudClient? = null
private var user: User? = null
private val gson = Gson()
private val pendingDownloads = IndexedForest<DownloadFileOperation>()

@Suppress("TooGenericExceptionCaught")
override fun doWork(): Result {
Expand All @@ -151,7 +139,13 @@ class FileDownloadWorker(
}

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

cancelAllDownloads()
notificationManager.dismissDownloadInProgressNotification()
removePendingDownload(currentDownload?.user?.accountName)
setIdleWorkerState()

super.onStopped()
}

Expand Down Expand Up @@ -200,6 +194,17 @@ class FileDownloadWorker(
}
}

private fun removePendingDownload(accountName: String?) {
pendingDownloads.remove(accountName)
}

private fun cancelAllDownloads() {
pendingDownloads.all.forEach {
it.value.payload?.cancel()
}
pendingDownloads.all.clear()
}

private fun setUser() {
val accountName = inputData.keyValueMap[USER_NAME] as String
user = accountManager.getUser(accountName).get()
Expand Down Expand Up @@ -231,6 +236,11 @@ class FileDownloadWorker(
}

private fun setWorkerState(user: User?, file: DownloadFileOperation?) {
val folderId = inputData.keyValueMap[FOLDER_ID] as Long?
folderId?.let {
folderDownloadStatusPair[folderId] = true
}

WorkerStateLiveData.instance().setWorkState(WorkerState.Download(user, file))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,12 +527,15 @@ internal class BackgroundJobManagerImpl(
FileDownloadWorker.DOWNLOAD_TYPE to DownloadType.DOWNLOAD.toString()
)

val tag = startFileDownloadJobTag(user, folder)

val request = oneTimeRequestBuilder(FileDownloadWorker::class, JOB_FILES_DOWNLOAD, user)
.addTag(tag)
.setInputData(data)
.build()

val tag = startFileDownloadJobTag(user, folder)
workManager.enqueueUniqueWork(tag, ExistingWorkPolicy.REPLACE, request)
workManager
.enqueueUniqueWork(tag, ExistingWorkPolicy.REPLACE, request)
}

override fun startFileDownloadJob(
Expand All @@ -554,11 +557,13 @@ internal class BackgroundJobManagerImpl(
FileDownloadWorker.CONFLICT_UPLOAD_ID to conflictUploadId
)

val tag = startFileDownloadJobTag(user, file)

val request = oneTimeRequestBuilder(FileDownloadWorker::class, JOB_FILES_DOWNLOAD, user)
.addTag(tag)
.setInputData(data)
.build()

val tag = startFileDownloadJobTag(user, file)
workManager.enqueueUniqueWork(tag, ExistingWorkPolicy.REPLACE, request)
}

Expand Down

0 comments on commit 1ca5641

Please sign in to comment.