mirror of
https://github.com/nextcloud/android.git
synced 2024-11-26 07:05:49 +03:00
Stop background job when cancelling two-way sync
Signed-off-by: ZetaTom <70907959+ZetaTom@users.noreply.github.com>
This commit is contained in:
parent
dd7b08ff7a
commit
a78607a6e9
4 changed files with 25 additions and 0 deletions
|
@ -130,6 +130,11 @@ interface BackgroundJobManager {
|
|||
changedFiles: Array<String> = arrayOf<String>()
|
||||
)
|
||||
|
||||
/**
|
||||
* Cancel two-way sync. Existing tasks might finish, but no new invocations will occur.
|
||||
*/
|
||||
fun cancelTwoWaySyncJob(user: User)
|
||||
|
||||
fun scheduleOfflineSync()
|
||||
|
||||
fun scheduleMediaFoldersDetectionJob()
|
||||
|
|
|
@ -503,6 +503,10 @@ internal class BackgroundJobManagerImpl(
|
|||
)
|
||||
}
|
||||
|
||||
override fun cancelTwoWaySyncJob(user: User) {
|
||||
workManager.cancelJob(JOB_INTERNAL_TWO_WAY_SYNC, user)
|
||||
}
|
||||
|
||||
override fun scheduleOfflineSync() {
|
||||
val constrains = Constraints.Builder()
|
||||
.setRequiredNetworkType(NetworkType.UNMETERED)
|
||||
|
|
|
@ -29,6 +29,8 @@ class InternalTwoWaySyncWork(
|
|||
private val powerManagementService: PowerManagementService,
|
||||
private val connectivityService: ConnectivityService
|
||||
) : Worker(context, params) {
|
||||
private var shouldRun = true
|
||||
|
||||
override fun doWork(): Result {
|
||||
Log_OC.d(TAG, "Worker started!")
|
||||
|
||||
|
@ -50,6 +52,11 @@ class InternalTwoWaySyncWork(
|
|||
val folders = fileDataStorageManager.getInternalTwoWaySyncFolders(user)
|
||||
|
||||
for (folder in folders) {
|
||||
if (!shouldRun) {
|
||||
Log_OC.d(TAG, "Worker was stopped!")
|
||||
return Result.failure()
|
||||
}
|
||||
|
||||
checkFreeSpace(folder)?.let { checkFreeSpaceResult ->
|
||||
return checkFreeSpaceResult
|
||||
}
|
||||
|
@ -90,6 +97,12 @@ class InternalTwoWaySyncWork(
|
|||
}
|
||||
}
|
||||
|
||||
override fun onStopped() {
|
||||
Log_OC.d(TAG, "OnStopped of worker called!")
|
||||
shouldRun = false
|
||||
super.onStopped()
|
||||
}
|
||||
|
||||
@Suppress("TooGenericExceptionCaught")
|
||||
private fun checkFreeSpace(folder: OCFile): Result? {
|
||||
val storagePath = folder.storagePath ?: MainApp.getStoragePath()
|
||||
|
|
|
@ -96,6 +96,9 @@ class InternalTwoWaySyncActivity : DrawerActivity(), Injectable {
|
|||
*/
|
||||
private fun removeAllFolders() {
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
// cancel main worker
|
||||
backgroundJobManager.cancelTwoWaySyncJob(user.get())
|
||||
|
||||
val folders = fileDataStorageManager.getInternalTwoWaySyncFolders(user.get())
|
||||
folders.forEach { folder ->
|
||||
// update database to ignore folder
|
||||
|
|
Loading…
Reference in a new issue