Fix worker tag

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-01-03 16:01:48 +01:00 committed by Alper Öztürk
parent c131a1237e
commit d661dc2001
3 changed files with 41 additions and 41 deletions

View file

@ -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
@ -64,7 +61,7 @@ class FileDownloadHelper {
}
return if (file.isFolder) {
isFolderDownloading(file)
FileDownloadWorker.isFolderDownloading(file)
} else {
backgroundJobManager.isStartFileDownloadJobScheduled(
user,
@ -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)
}
@ -95,8 +83,6 @@ class FileDownloadHelper {
currentDownload.cancel()
}
removePendingDownload(accountName)
}
fun saveFile(
@ -140,7 +126,6 @@ class FileDownloadHelper {
}
fun downloadFolder(folder: OCFile, user: User, files: List<OCFile>) {
folderDownloadStatusPair[folder.fileId] = true
backgroundJobManager.startFolderDownloadJob(folder, user, files)
}

View file

@ -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
@ -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
@ -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"
@ -89,6 +86,14 @@ 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"
}
@ -96,24 +101,6 @@ class FileDownloadWorker(
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
@ -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 {
@ -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()
}
@ -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()
@ -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))
}

View file

@ -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(
@ -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)
}