Skip to content

Commit 0cfb0af

Browse files
committed
wip
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent fef91e8 commit 0cfb0af

11 files changed

Lines changed: 98 additions & 49 deletions

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/download/FileDownloadBroadcastManager.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,28 @@ class FileDownloadBroadcastManager(
2727
const val DOWNLOAD_FINISHED = "DOWNLOAD_FINISHED"
2828
}
2929

30-
fun sendAdded(accountName: String, remotePath: String, packageName: String, linkedToRemotePath: String?) {
30+
fun sendAdded(
31+
accountName: String,
32+
remotePath: String,
33+
packageName: String,
34+
fileId: Long?,
35+
linkedToRemotePath: String?,
36+
currentDownloadAccountName: String?
37+
) {
3138
Log_OC.d(TAG, "download added broadcast sent")
3239

3340
val intent = Intent(DOWNLOAD_ADDED).apply {
3441
putExtra(FileDownloadWorker.EXTRA_ACCOUNT_NAME, accountName)
3542
putExtra(FileDownloadWorker.EXTRA_REMOTE_PATH, remotePath)
3643

44+
fileId?.let {
45+
putExtra(FileDownloadWorker.EXTRA_CURRENT_DOWNLOAD_FILE_ID, fileId)
46+
}
47+
48+
currentDownloadAccountName?.let {
49+
putExtra(FileDownloadWorker.EXTRA_CURRENT_DOWNLOAD_ACCOUNT_NAME, currentDownloadAccountName)
50+
}
51+
3752
linkedToRemotePath?.let {
3853
putExtra(FileDownloadWorker.EXTRA_LINKED_TO_PATH, linkedToRemotePath)
3954
}

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/FileDownloadWorker.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ class FileDownloadWorker(
8383
const val EXTRA_REMOTE_PATH = "EXTRA_REMOTE_PATH"
8484
const val EXTRA_LINKED_TO_PATH = "EXTRA_LINKED_TO_PATH"
8585
const val EXTRA_ACCOUNT_NAME = "EXTRA_ACCOUNT_NAME"
86+
const val EXTRA_CURRENT_DOWNLOAD_ACCOUNT_NAME = "EXTRA_CURRENT_DOWNLOAD_ACCOUNT_NAME"
87+
const val EXTRA_CURRENT_DOWNLOAD_FILE_ID = "EXTRA_CURRENT_DOWNLOAD_FILE_ID"
8688
}
8789

8890
private var currentDownload: DownloadFileOperation? = null
@@ -218,7 +220,9 @@ class FileDownloadWorker(
218220
operation.user.accountName,
219221
operation.remotePath,
220222
context.packageName,
221-
linkedToRemotePath
223+
operation.file.fileId,
224+
linkedToRemotePath,
225+
operation.user.accountName
222226
)
223227
}
224228

app/src/main/java/com/owncloud/android/operations/SynchronizeFolderOperation.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,4 +580,16 @@ private void startSyncFolderOperation(String path){
580580
public String getRemotePath() {
581581
return mRemotePath;
582582
}
583+
584+
public String getAccountName() {
585+
return user.getAccountName();
586+
}
587+
588+
public Long getFolderId() {
589+
if (mLocalFolder == null) {
590+
return null;
591+
}
592+
593+
return mLocalFolder.getFileId();
594+
}
583595
}

app/src/main/java/com/owncloud/android/services/SyncFolderHandler.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,12 @@ public void add(Account account, String remotePath,
124124
SynchronizeFolderOperation syncFolderOperation){
125125
Pair<String, String> putResult = mPendingOperations.putIfAbsent(account.name, remotePath, syncFolderOperation);
126126
if (putResult != null) {
127-
fileDownloadBroadcastManager.sendAdded(account.name, remotePath, mService.getPackageName(), null);
127+
fileDownloadBroadcastManager.sendAdded(account.name,
128+
remotePath,
129+
mService.getPackageName(),
130+
syncFolderOperation.getFolderId(),
131+
null,
132+
syncFolderOperation.getAccountName());
128133
}
129134
}
130135

app/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,9 @@ import com.nextcloud.client.core.Clock
6363
import com.nextcloud.client.di.Injectable
6464
import com.nextcloud.client.editimage.EditImageActivity
6565
import com.nextcloud.client.files.DeepLinkHandler
66+
import com.nextcloud.client.jobs.download.FileDownloadBroadcastManager
6667
import com.nextcloud.client.jobs.download.FileDownloadHelper
6768
import com.nextcloud.client.jobs.download.FileDownloadWorker
68-
import com.nextcloud.client.jobs.download.FileDownloadWorker.Companion.getDownloadAddedMessage
69-
import com.nextcloud.client.jobs.download.FileDownloadWorker.Companion.getDownloadFinishMessage
7069
import com.nextcloud.client.jobs.upload.FileUploadBroadcastManager
7170
import com.nextcloud.client.jobs.upload.FileUploadHelper
7271
import com.nextcloud.client.jobs.upload.FileUploadWorker
@@ -76,7 +75,6 @@ import com.nextcloud.client.preferences.AppPreferences
7675
import com.nextcloud.client.utils.IntentUtil
7776
import com.nextcloud.model.WorkerState
7877
import com.nextcloud.model.WorkerState.FileDownloadCompleted
79-
import com.nextcloud.model.WorkerState.FileDownloadStarted
8078
import com.nextcloud.model.WorkerState.OfflineOperationsCompleted
8179
import com.nextcloud.ui.composeActivity.ComposeProcessTextAlias
8280
import com.nextcloud.utils.extensions.getParcelableArgument
@@ -841,11 +839,11 @@ class FileDisplayActivity :
841839
if (fileInFragment != null && downloadedRemotePath != fileInFragment.remotePath) {
842840
// the user browsed to other file ; forget the automatic preview
843841
mWaitingToPreview = null
844-
} else if (downloadEvent == getDownloadAddedMessage()) {
842+
} else if (downloadEvent == FileDownloadBroadcastManager.DOWNLOAD_ADDED) {
845843
// grant that the details fragment updates the progress bar
846844
leftFragment.listenForTransferProgress()
847845
leftFragment.updateFileDetails(true, false)
848-
} else if (downloadEvent == getDownloadFinishMessage()) {
846+
} else if (downloadEvent == FileDownloadBroadcastManager.DOWNLOAD_FINISHED) {
849847
// update the details panel
850848
var detailsFragmentChanged = false
851849
if (waitedPreview) {
@@ -1431,8 +1429,8 @@ class FileDisplayActivity :
14311429
}
14321430

14331431
private fun registerDownloadFinishReceiver() {
1434-
val filter = IntentFilter(getDownloadAddedMessage()).apply {
1435-
addAction(getDownloadFinishMessage())
1432+
val filter = IntentFilter(FileDownloadBroadcastManager.DOWNLOAD_ADDED).apply {
1433+
addAction(FileDownloadBroadcastManager.DOWNLOAD_FINISHED)
14361434
}
14371435
mDownloadFinishReceiver = DownloadFinishReceiver()
14381436
mDownloadFinishReceiver?.let {
@@ -1756,6 +1754,8 @@ class FileDisplayActivity :
17561754
*/
17571755
private inner class DownloadFinishReceiver : BroadcastReceiver() {
17581756
override fun onReceive(context: Context?, intent: Intent) {
1757+
Log_OC.d(TAG, "DownloadFinishReceiver: download finish received broadcast")
1758+
17591759
val sameAccount = isSameAccount(intent)
17601760
val downloadedRemotePath = intent.getStringExtra(FileDownloadWorker.EXTRA_REMOTE_PATH)
17611761
val downloadBehaviour = intent.getStringExtra(OCFileListFragment.DOWNLOAD_BEHAVIOUR)
@@ -1904,6 +1904,7 @@ class FileDisplayActivity :
19041904
private fun observeWorkerState() {
19051905
observeWorker { state ->
19061906
when (state) {
1907+
/*
19071908
is FileDownloadStarted -> {
19081909
Log_OC.d(TAG, "Download worker started")
19091910
handleDownloadWorkerState()
@@ -1913,6 +1914,8 @@ class FileDisplayActivity :
19131914
fileDownloadProgressListener = null
19141915
previewFile(state)
19151916
}
1917+
*/
1918+
19161919

19171920
is OfflineOperationsCompleted -> {
19181921
refreshCurrentDirectory()

app/src/main/java/com/owncloud/android/ui/activity/InternalTwoWaySyncActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class InternalTwoWaySyncActivity :
120120
val folders = fileDataStorageManager.getInternalTwoWaySyncFolders(currentUser)
121121
folders.forEach { folder ->
122122
FileDownloadWorker.cancelOperation(currentUser.accountName, folder.fileId)
123-
backgroundJobManager.cancelFilesDownloadJob(currentUser, folder.fileId)
123+
backgroundJobManager.cancelFilesDownloadJob(currentUser.accountName, folder.fileId)
124124

125125
folder.internalFolderSyncTimestamp = -1L
126126
fileDataStorageManager.saveFile(folder)

app/src/main/java/com/owncloud/android/ui/activity/ManageAccountsActivity.kt

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import android.accounts.AccountManagerCallback
1616
import android.accounts.AccountManagerFuture
1717
import android.accounts.OperationCanceledException
1818
import android.annotation.SuppressLint
19+
import android.content.BroadcastReceiver
20+
import android.content.Context
1921
import android.content.Intent
2022
import android.os.Bundle
2123
import android.os.Handler
@@ -31,11 +33,9 @@ import com.google.common.collect.Sets
3133
import com.nextcloud.client.account.User
3234
import com.nextcloud.client.account.UserAccountManager
3335
import com.nextcloud.client.jobs.download.FileDownloadHelper
36+
import com.nextcloud.client.jobs.download.FileDownloadWorker
3437
import com.nextcloud.client.onboarding.FirstRunActivity
35-
import com.nextcloud.model.WorkerState
36-
import com.nextcloud.model.WorkerState.FileDownloadStarted
3738
import com.nextcloud.utils.extensions.getParcelableArgument
38-
import com.nextcloud.utils.extensions.observeWorker
3939
import com.nextcloud.utils.mdm.MDMConfig.multiAccountSupport
4040
import com.owncloud.android.MainApp
4141
import com.owncloud.android.R
@@ -45,7 +45,6 @@ import com.owncloud.android.datamodel.ArbitraryDataProviderImpl
4545
import com.owncloud.android.datamodel.FileDataStorageManager
4646
import com.owncloud.android.lib.common.UserInfo
4747
import com.owncloud.android.lib.common.utils.Log_OC
48-
import com.owncloud.android.operations.DownloadFileOperation
4948
import com.owncloud.android.services.OperationsService.OperationsServiceBinder
5049
import com.owncloud.android.ui.adapter.UserListAdapter
5150
import com.owncloud.android.ui.adapter.UserListItem
@@ -74,8 +73,6 @@ class ManageAccountsActivity :
7473

7574
private var multipleAccountsSupported = false
7675

77-
private var workerAccountName: String? = null
78-
private var workerCurrentDownload: DownloadFileOperation? = null
7976

8077
override fun onCreate(savedInstanceState: Bundle?) {
8178
super.onCreate(savedInstanceState)
@@ -303,7 +300,7 @@ class ManageAccountsActivity :
303300

304301
if (!user.isPresent) {
305302
fileUploadHelper.cancel(it)
306-
FileDownloadHelper.instance().cancelAllDownloadsForAccount(workerAccountName, workerCurrentDownload)
303+
cancelAllDownloadsForAccount()
307304
}
308305
}
309306

@@ -364,7 +361,7 @@ class ManageAccountsActivity :
364361
val arbitraryDataProvider: ArbitraryDataProvider = ArbitraryDataProviderImpl(this)
365362
arbitraryDataProvider.storeOrUpdateKeyValue(user.accountName, PENDING_FOR_REMOVAL, true.toString())
366363

367-
FileDownloadHelper.instance().cancelAllDownloadsForAccount(workerAccountName, workerCurrentDownload)
364+
cancelAllDownloadsForAccount()
368365
fileUploadHelper.cancel(user.accountName)
369366
backgroundJobManager.startAccountRemovalJob(user.accountName, false)
370367

@@ -397,6 +394,20 @@ class ManageAccountsActivity :
397394
}
398395
}
399396

397+
private fun cancelAllDownloadsForAccount() {
398+
workerAccountName?.let { accountName ->
399+
workerCurrentDownloadAccountName?.let { currentDownloadAccountName ->
400+
if (workerFileId != -1L) {
401+
FileDownloadHelper.instance().cancelAllDownloadsForAccount(
402+
accountName,
403+
currentDownloadAccountName,
404+
workerFileId
405+
)
406+
}
407+
}
408+
}
409+
}
410+
400411
@Suppress("DEPRECATION")
401412
private fun openAccount(user: User) {
402413
val intent = Intent(this, UserInfoActivity::class.java).apply {
@@ -455,20 +466,21 @@ class ManageAccountsActivity :
455466
}
456467
}
457468

458-
private fun observeWorkerState() {
459-
observeWorker { state: WorkerState? ->
460-
if (state is FileDownloadStarted) {
461-
Log_OC.d(TAG, "Download worker started")
462-
workerAccountName = state.user?.accountName
463-
workerCurrentDownload = state.currentDownload
464-
}
465-
}
466-
}
467-
468469
override fun onAccountClicked(user: User) {
469470
openAccount(user)
470471
}
471472

473+
private class DownloadReceiver : BroadcastReceiver() {
474+
override fun onReceive(context: Context, intent: Intent) {
475+
Log_OC.d(TAG, "download received")
476+
477+
workerAccountName = intent.getStringExtra(FileDownloadWorker.EXTRA_ACCOUNT_NAME)
478+
workerCurrentDownloadAccountName =
479+
intent.getStringExtra(FileDownloadWorker.EXTRA_CURRENT_DOWNLOAD_ACCOUNT_NAME)
480+
workerFileId = intent.getLongExtra(FileDownloadWorker.EXTRA_CURRENT_DOWNLOAD_FILE_ID, -1L)
481+
}
482+
}
483+
472484
companion object {
473485
private val TAG: String = ManageAccountsActivity::class.java.simpleName
474486

@@ -480,6 +492,11 @@ class ManageAccountsActivity :
480492
private const val SINGLE_ACCOUNT = 1
481493
private const val MIN_MULTI_ACCOUNT_SIZE = 2
482494

495+
private var workerAccountName: String? = null
496+
private var workerCurrentDownloadAccountName: String? = null
497+
private var workerFileId: Long = -1L
498+
499+
483500
private fun toAccountNames(users: Collection<User>): Set<String> {
484501
val accountNames: MutableSet<String> = Sets.newHashSetWithExpectedSize(users.size)
485502
for (user in users) {

0 commit comments

Comments
 (0)