Skip to content

Commit 6cfd9ba

Browse files
Merge pull request #16631 from nextcloud/fix/receivers
fix: receivers
2 parents 612824f + 57296e0 commit 6cfd9ba

23 files changed

Lines changed: 448 additions & 385 deletions

app/src/main/java/com/nextcloud/client/jobs/BackgroundJobFactory.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ class BackgroundJobFactory @Inject constructor(
300300
accountManager,
301301
context,
302302
viewThemeUtils.get(),
303+
localBroadcastManager.get(),
303304
params
304305
)
305306
}

app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ interface BackgroundJobManager {
136136
fun cancelFilesUploadJob(user: User)
137137
fun isStartFileUploadJobScheduled(accountName: String): Boolean
138138

139-
fun cancelFilesDownloadJob(user: User, fileId: Long)
139+
fun cancelFilesDownloadJob(accountName: String, fileId: Long)
140140

141141
@Suppress("LongParameterList")
142142
fun startFileDownloadJob(

app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import com.nextcloud.client.jobs.offlineOperations.OfflineOperationsWorker
3535
import com.nextcloud.client.jobs.upload.FileUploadHelper
3636
import com.nextcloud.client.jobs.upload.FileUploadWorker
3737
import com.nextcloud.client.preferences.AppPreferences
38-
import com.nextcloud.utils.extensions.isWorkRunning
3938
import com.nextcloud.utils.extensions.isWorkScheduled
4039
import com.owncloud.android.datamodel.OCFile
4140
import com.owncloud.android.datamodel.SyncedFolder
@@ -662,8 +661,8 @@ internal class BackgroundJobManagerImpl(
662661
}
663662
}
664663

665-
private fun startFileDownloadJobTag(user: User, fileId: Long): String =
666-
JOB_FOLDER_DOWNLOAD + user.accountName + fileId
664+
private fun startFileDownloadJobTag(accountName: String, fileId: Long): String =
665+
JOB_FOLDER_DOWNLOAD + accountName + fileId
667666

668667
override fun startFileDownloadJob(
669668
user: User,
@@ -674,7 +673,7 @@ internal class BackgroundJobManagerImpl(
674673
packageName: String,
675674
conflictUploadId: Long?
676675
) {
677-
val tag = startFileDownloadJobTag(user, file.fileId)
676+
val tag = startFileDownloadJobTag(user.accountName, file.fileId)
678677

679678
val data = workDataOf(
680679
FileDownloadWorker.ACCOUNT_NAME to user.accountName,
@@ -706,8 +705,8 @@ internal class BackgroundJobManagerImpl(
706705
workManager.cancelJob(JOB_FILES_UPLOAD, user)
707706
}
708707

709-
override fun cancelFilesDownloadJob(user: User, fileId: Long) {
710-
workManager.cancelAllWorkByTag(startFileDownloadJobTag(user, fileId))
708+
override fun cancelFilesDownloadJob(accountName: String, fileId: Long) {
709+
workManager.cancelAllWorkByTag(startFileDownloadJobTag(accountName, fileId))
711710
}
712711

713712
override fun startPdfGenerateAndUploadWork(

app/src/main/java/com/nextcloud/client/jobs/autoUpload/AutoUploadWorker.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import com.nextcloud.client.account.UserAccountManager
1818
import com.nextcloud.client.database.entity.toOCUpload
1919
import com.nextcloud.client.database.entity.toUploadEntity
2020
import com.nextcloud.client.device.PowerManagementService
21-
import com.nextcloud.client.jobs.upload.FileUploadBroadcastManager
21+
import com.nextcloud.client.jobs.upload.FileUploadEventBroadcaster
2222
import com.nextcloud.client.jobs.upload.FileUploadWorker
2323
import com.nextcloud.client.jobs.utils.UploadErrorNotificationManager
2424
import com.nextcloud.client.network.ConnectivityService
@@ -70,7 +70,7 @@ class AutoUploadWorker(
7070
}
7171

7272
private val syncFolderHelper = SyncFolderHelper(context)
73-
private val fileUploadBroadcastManager = FileUploadBroadcastManager(localBroadcastManager)
73+
private val fileUploadEventBroadcaster = FileUploadEventBroadcaster(localBroadcastManager)
7474
private lateinit var syncedFolder: SyncedFolder
7575
private val notificationManager = AutoUploadNotificationManager(context, viewThemeUtils, NOTIFICATION_ID)
7676

@@ -287,12 +287,12 @@ class AutoUploadWorker(
287287
uploadEntity = uploadEntity.copy(id = generatedId.toInt())
288288
upload.uploadId = generatedId
289289

290-
fileUploadBroadcastManager.sendAdded(context)
290+
fileUploadEventBroadcaster.sendUploadEnqueued(context)
291291
val operation = createUploadFileOperation(upload, user)
292292
Log_OC.d(TAG, "🕒 uploading: $localPath, id: $generatedId")
293293

294294
val result = operation.execute(client)
295-
fileUploadBroadcastManager.sendStarted(operation, context)
295+
fileUploadEventBroadcaster.sendUploadStarted(operation, context)
296296

297297
UploadErrorNotificationManager.handleResult(
298298
context,
@@ -427,7 +427,7 @@ class AutoUploadWorker(
427427
)
428428

429429
private fun sendUploadFinishEvent(operation: UploadFileOperation, result: RemoteOperationResult<*>) {
430-
fileUploadBroadcastManager.sendFinished(
430+
fileUploadEventBroadcaster.sendUploadCompleted(
431431
operation,
432432
result,
433433
context
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Nextcloud - Android Client
3+
*
4+
* SPDX-FileCopyrightText: 2026 Alper Ozturk <alper.ozturk@nextcloud.com>
5+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH
6+
* SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only
7+
*/
8+
package com.nextcloud.client.jobs.download
9+
10+
import android.content.Context
11+
import android.content.Intent
12+
import androidx.localbroadcastmanager.content.LocalBroadcastManager
13+
import com.owncloud.android.lib.common.operations.RemoteOperationResult
14+
import com.owncloud.android.lib.common.utils.Log_OC
15+
import com.owncloud.android.operations.DownloadFileOperation
16+
17+
class FileDownloadEventBroadcaster(private val context: Context, private val broadcastManager: LocalBroadcastManager) {
18+
companion object {
19+
private const val TAG = "📣" + "FileDownloadBroadcastManager"
20+
21+
private const val PREFIX = "com.nextcloud.client.download."
22+
23+
const val ACTION_DOWNLOAD_ENQUEUED = PREFIX + "ACTION_DOWNLOAD_ENQUEUED"
24+
const val ACTION_DOWNLOAD_COMPLETED = PREFIX + "ACTION_DOWNLOAD_COMPLETED"
25+
26+
const val EXTRA_DOWNLOAD_RESULT = PREFIX + "EXTRA_DOWNLOAD_RESULT"
27+
const val EXTRA_REMOTE_PATH = PREFIX + "EXTRA_REMOTE_PATH"
28+
const val EXTRA_ACCOUNT_NAME = PREFIX + "EXTRA_ACCOUNT_NAME"
29+
const val EXTRA_CURRENT_DOWNLOAD_ACCOUNT_NAME = PREFIX + "EXTRA_CURRENT_DOWNLOAD_ACCOUNT_NAME"
30+
const val EXTRA_CURRENT_DOWNLOAD_FILE_ID = PREFIX + "EXTRA_CURRENT_DOWNLOAD_FILE_ID"
31+
const val EXTRA_PACKAGE_NAME = PREFIX + "PACKAGE_NAME"
32+
const val EXTRA_ACTIVITY_NAME = PREFIX + "ACTIVITY_NAME"
33+
const val EXTRA_DOWNLOAD_BEHAVIOUR = PREFIX + "DOWNLOAD_BEHAVIOUR"
34+
}
35+
36+
fun sendDownloadEnqueued(
37+
accountName: String,
38+
remotePath: String,
39+
packageName: String,
40+
fileId: Long?,
41+
currentDownloadAccountName: String?
42+
) {
43+
Log_OC.d(TAG, "Download enqueued broadcast sent")
44+
45+
val intent = Intent(ACTION_DOWNLOAD_ENQUEUED).apply {
46+
putExtra(EXTRA_ACCOUNT_NAME, accountName)
47+
putExtra(EXTRA_REMOTE_PATH, remotePath)
48+
49+
fileId?.let {
50+
putExtra(EXTRA_CURRENT_DOWNLOAD_FILE_ID, fileId)
51+
}
52+
53+
currentDownloadAccountName?.let {
54+
putExtra(EXTRA_CURRENT_DOWNLOAD_ACCOUNT_NAME, currentDownloadAccountName)
55+
}
56+
setPackage(packageName)
57+
}
58+
59+
broadcastManager.sendBroadcast(intent)
60+
}
61+
62+
fun sendDownloadCompleted(download: DownloadFileOperation, downloadResult: RemoteOperationResult<*>) {
63+
Log_OC.d(TAG, "Download completed broadcast sent")
64+
65+
val intent = Intent(ACTION_DOWNLOAD_COMPLETED).apply {
66+
putExtra(EXTRA_DOWNLOAD_RESULT, downloadResult.isSuccess)
67+
putExtra(EXTRA_ACCOUNT_NAME, download.user.accountName)
68+
putExtra(EXTRA_REMOTE_PATH, download.remotePath)
69+
putExtra(EXTRA_DOWNLOAD_BEHAVIOUR, download.behaviour)
70+
putExtra(EXTRA_ACTIVITY_NAME, download.activityName)
71+
putExtra(EXTRA_PACKAGE_NAME, download.packageName)
72+
setPackage(context.packageName)
73+
}
74+
75+
broadcastManager.sendBroadcast(intent)
76+
}
77+
78+
fun sendDownloadCompleted(accountName: String, remotePath: String?, packageName: String, success: Boolean) {
79+
Log_OC.d(TAG, "Download completed broadcast sent")
80+
81+
val intent = Intent(ACTION_DOWNLOAD_COMPLETED).apply {
82+
putExtra(EXTRA_ACCOUNT_NAME, accountName)
83+
putExtra(EXTRA_REMOTE_PATH, remotePath)
84+
putExtra(EXTRA_DOWNLOAD_RESULT, success)
85+
setPackage(packageName)
86+
}
87+
88+
broadcastManager.sendBroadcast(intent)
89+
}
90+
}

app/src/main/java/com/nextcloud/client/jobs/download/FileDownloadHelper.kt

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,17 @@ class FileDownloadHelper {
5959

6060
files.forEach { file ->
6161
FileDownloadWorker.cancelOperation(user.accountName, file.fileId)
62-
backgroundJobManager.cancelFilesDownloadJob(user, file.fileId)
62+
backgroundJobManager.cancelFilesDownloadJob(user.accountName, file.fileId)
6363
}
6464
}
6565

66-
fun cancelAllDownloadsForAccount(accountName: String?, currentDownload: DownloadFileOperation?) {
67-
if (accountName == null || currentDownload == null) return
68-
69-
val currentUser = currentDownload.user
70-
val currentFile = currentDownload.file
71-
72-
if (!currentUser.nameEquals(accountName)) {
66+
fun cancelAllDownloadsForAccount(accountName: String, currentDownloadAccountName: String, fileId: Long) {
67+
if (!accountName.equals(currentDownloadAccountName, true)) {
7368
return
7469
}
7570

76-
currentDownload.cancel()
77-
FileDownloadWorker.cancelOperation(currentUser.accountName, currentFile.fileId)
78-
backgroundJobManager.cancelFilesDownloadJob(currentUser, currentFile.fileId)
71+
FileDownloadWorker.cancelOperation(currentDownloadAccountName, fileId)
72+
backgroundJobManager.cancelFilesDownloadJob(currentDownloadAccountName, fileId)
7973
}
8074

8175
fun saveFile(file: OCFile, currentDownload: DownloadFileOperation?, storageManager: FileDataStorageManager?) {

app/src/main/java/com/nextcloud/client/jobs/download/FileDownloadIntents.kt

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,24 @@
11
/*
22
* Nextcloud - Android Client
33
*
4-
* SPDX-FileCopyrightText: 2023 Alper Ozturk <alper.ozturk@nextcloud.com>
5-
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH
6-
* SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only
4+
* SPDX-FileCopyrightText: 2026 Alper Ozturk <alper.ozturk@nextcloud.com>
5+
* SPDX-License-Identifier: AGPL-3.0-or-later
76
*/
7+
88
package com.nextcloud.client.jobs.download
99

1010
import android.content.Context
1111
import android.content.Intent
1212
import com.nextcloud.client.account.User
1313
import com.owncloud.android.authentication.AuthenticatorActivity
14-
import com.owncloud.android.lib.common.operations.RemoteOperationResult
1514
import com.owncloud.android.operations.DownloadFileOperation
1615
import com.owncloud.android.ui.activity.FileActivity
1716
import com.owncloud.android.ui.activity.FileDisplayActivity
18-
import com.owncloud.android.ui.dialog.SendShareDialog
19-
import com.owncloud.android.ui.fragment.OCFileListFragment
2017
import com.owncloud.android.ui.preview.PreviewImageActivity
2118
import com.owncloud.android.ui.preview.PreviewImageFragment
2219

2320
class FileDownloadIntents(private val context: Context) {
2421

25-
fun newDownloadIntent(download: DownloadFileOperation, linkedToRemotePath: String): Intent =
26-
Intent(FileDownloadWorker.getDownloadAddedMessage()).apply {
27-
putExtra(FileDownloadWorker.EXTRA_ACCOUNT_NAME, download.user.accountName)
28-
putExtra(FileDownloadWorker.EXTRA_REMOTE_PATH, download.remotePath)
29-
putExtra(FileDownloadWorker.EXTRA_LINKED_TO_PATH, linkedToRemotePath)
30-
setPackage(context.packageName)
31-
}
32-
33-
fun downloadFinishedIntent(
34-
download: DownloadFileOperation,
35-
downloadResult: RemoteOperationResult<*>,
36-
unlinkedFromRemotePath: String?
37-
): Intent = Intent(FileDownloadWorker.getDownloadFinishMessage()).apply {
38-
putExtra(FileDownloadWorker.EXTRA_DOWNLOAD_RESULT, downloadResult.isSuccess)
39-
putExtra(FileDownloadWorker.EXTRA_ACCOUNT_NAME, download.user.accountName)
40-
putExtra(FileDownloadWorker.EXTRA_REMOTE_PATH, download.remotePath)
41-
putExtra(OCFileListFragment.DOWNLOAD_BEHAVIOUR, download.behaviour)
42-
putExtra(SendShareDialog.ACTIVITY_NAME, download.activityName)
43-
putExtra(SendShareDialog.PACKAGE_NAME, download.packageName)
44-
if (unlinkedFromRemotePath != null) {
45-
putExtra(FileDownloadWorker.EXTRA_LINKED_TO_PATH, unlinkedFromRemotePath)
46-
}
47-
setPackage(context.packageName)
48-
}
49-
5022
fun credentialContentIntent(user: User): Intent = Intent(context, AuthenticatorActivity::class.java).apply {
5123
putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, user.toPlatformAccount())
5224
putExtra(

app/src/main/java/com/nextcloud/client/jobs/download/FileDownloadWorker.kt

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import androidx.work.ForegroundInfo
2121
import androidx.work.WorkerParameters
2222
import com.nextcloud.client.account.User
2323
import com.nextcloud.client.account.UserAccountManager
24-
import com.nextcloud.model.WorkerState
25-
import com.nextcloud.model.WorkerStateObserver
2624
import com.nextcloud.utils.ForegroundServiceHelper
2725
import com.nextcloud.utils.extensions.getPercent
2826
import com.owncloud.android.R
@@ -50,7 +48,7 @@ import kotlin.random.Random
5048
class FileDownloadWorker(
5149
viewThemeUtils: ViewThemeUtils,
5250
private val accountManager: UserAccountManager,
53-
private var localBroadcastManager: LocalBroadcastManager,
51+
localBroadcastManager: LocalBroadcastManager,
5452
private val context: Context,
5553
params: WorkerParameters
5654
) : CoroutineWorker(context, params),
@@ -79,22 +77,16 @@ class FileDownloadWorker(
7977
const val ACTIVITY_NAME = "ACTIVITY_NAME"
8078
const val PACKAGE_NAME = "PACKAGE_NAME"
8179
const val CONFLICT_UPLOAD_ID = "CONFLICT_UPLOAD_ID"
82-
const val EXTRA_DOWNLOAD_RESULT = "EXTRA_DOWNLOAD_RESULT"
83-
const val EXTRA_REMOTE_PATH = "EXTRA_REMOTE_PATH"
84-
const val EXTRA_LINKED_TO_PATH = "EXTRA_LINKED_TO_PATH"
85-
const val EXTRA_ACCOUNT_NAME = "EXTRA_ACCOUNT_NAME"
86-
87-
fun getDownloadAddedMessage(): String = FileDownloadWorker::class.java.name + "DOWNLOAD_ADDED"
88-
89-
fun getDownloadFinishMessage(): String = FileDownloadWorker::class.java.name + "DOWNLOAD_FINISH"
9080
}
9181

9282
private var currentDownload: DownloadFileOperation? = null
9383

9484
private var conflictUploadId: Long? = null
9585
private var lastPercent = 0
9686

87+
private val fileDownloadEventBroadcaster = FileDownloadEventBroadcaster(context, localBroadcastManager)
9788
private val intents = FileDownloadIntents(context)
89+
9890
private var notificationManager = DownloadNotificationManager(
9991
Random.nextInt(),
10092
context,
@@ -140,7 +132,6 @@ class FileDownloadWorker(
140132
} finally {
141133
Log_OC.d(TAG, "cleanup")
142134
notificationManager.dismissNotification()
143-
setIdleWorkerState()
144135
}
145136
}
146137

@@ -168,14 +159,6 @@ class FileDownloadWorker(
168159
ForegroundServiceType.DataSync
169160
)
170161

171-
private fun setWorkerState(user: User?) {
172-
WorkerStateObserver.send(WorkerState.FileDownloadStarted(user, currentDownload))
173-
}
174-
175-
private fun setIdleWorkerState() {
176-
WorkerStateObserver.send(WorkerState.FileDownloadCompleted(getCurrentFile()))
177-
}
178-
179162
private fun removePendingDownload(accountName: String?) {
180163
pendingDownloads.remove(accountName)
181164
}
@@ -206,7 +189,7 @@ class FileDownloadWorker(
206189

207190
operation.addDownloadDataTransferProgressListener(this)
208191
operation.addDownloadDataTransferProgressListener(downloadProgressListener)
209-
val (downloadKey, linkedToRemotePath) = pendingDownloads.putIfAbsent(
192+
val (downloadKey, _) = pendingDownloads.putIfAbsent(
210193
user?.accountName,
211194
file.remotePath,
212195
operation
@@ -216,9 +199,13 @@ class FileDownloadWorker(
216199
requestedDownloads.add(downloadKey)
217200
}
218201

219-
linkedToRemotePath?.let {
220-
localBroadcastManager.sendBroadcast(intents.newDownloadIntent(operation, linkedToRemotePath))
221-
}
202+
fileDownloadEventBroadcaster.sendDownloadEnqueued(
203+
operation.user.accountName,
204+
operation.remotePath,
205+
context.packageName,
206+
operation.file.fileId,
207+
operation.user.accountName
208+
)
222209
}
223210

224211
requestedDownloads
@@ -266,7 +253,6 @@ class FileDownloadWorker(
266253
return
267254
}
268255

269-
setWorkerState(user)
270256
Log_OC.d(TAG, "downloading: $downloadKey")
271257

272258
val isAccountExist = accountManager.exists(currentDownload?.user?.toPlatformAccount())
@@ -342,13 +328,10 @@ class FileDownloadWorker(
342328
currentDownload?.run {
343329
notifyDownloadResult(this, downloadResult)
344330

345-
val downloadFinishedIntent = intents.downloadFinishedIntent(
331+
fileDownloadEventBroadcaster.sendDownloadCompleted(
346332
this,
347-
downloadResult,
348-
removeResult.second
333+
downloadResult
349334
)
350-
351-
localBroadcastManager.sendBroadcast(downloadFinishedIntent)
352335
}
353336
}
354337

0 commit comments

Comments
 (0)