Skip to content

Commit 37df0dc

Browse files
committed
fix: download progress listener
Signed-off-by: alperozturk <[email protected]>
1 parent 05e83cb commit 37df0dc

File tree

6 files changed

+68
-19
lines changed

6 files changed

+68
-19
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ import java.security.SecureRandom
2222
class DownloadNotificationManager(id: Int, private val context: Context, viewThemeUtils: ViewThemeUtils) :
2323
WorkerNotificationManager(id, context, viewThemeUtils, R.string.downloader_download_in_progress_ticker) {
2424

25+
private var lastPercent = -1
26+
27+
init {
28+
notificationBuilder.apply {
29+
setSound(null)
30+
setVibrate(null)
31+
setOnlyAlertOnce(true)
32+
setSilent(true)
33+
}
34+
}
35+
2536
@Suppress("MagicNumber")
2637
fun prepareForStart(operation: DownloadFileOperation) {
2738
currentOperationTitle = File(operation.savePath).name
@@ -44,6 +55,12 @@ class DownloadNotificationManager(id: Int, private val context: Context, viewThe
4455

4556
@Suppress("MagicNumber")
4657
fun updateDownloadProgress(percent: Int, totalToTransfer: Long) {
58+
// If downloads are so fast, no need to notify again.
59+
if (percent == lastPercent) {
60+
return
61+
}
62+
lastPercent = percent
63+
4764
val progressText = NumberFormatter.getPercentageText(percent)
4865
setProgress(percent, progressText, totalToTransfer < 0)
4966
showNotification()

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCo
3838
import com.owncloud.android.lib.common.utils.Log_OC
3939
import com.owncloud.android.operations.DownloadFileOperation
4040
import com.owncloud.android.operations.DownloadType
41+
import com.owncloud.android.ui.events.EventBusFactory
42+
import com.owncloud.android.ui.events.FileDownloadProgressEvent
4143
import com.owncloud.android.utils.theme.ViewThemeUtils
4244
import java.util.AbstractList
4345
import java.util.Optional
@@ -149,8 +151,8 @@ class FileDownloadWorker(
149151
ForegroundServiceType.DataSync
150152
)
151153

152-
private fun setWorkerState(user: User?, percent: Int) {
153-
WorkerStateLiveData.instance().setWorkState(WorkerState.DownloadStarted(user, currentDownload, percent))
154+
private fun setWorkerState(user: User?) {
155+
WorkerStateLiveData.instance().setWorkState(WorkerState.DownloadStarted(user, currentDownload))
154156
}
155157

156158
private fun setIdleWorkerState() {
@@ -247,7 +249,7 @@ class FileDownloadWorker(
247249
return
248250
}
249251

250-
setWorkerState(user, 0)
252+
setWorkerState(user)
251253
Log_OC.e(TAG, "FilesDownloadWorker downloading: $downloadKey")
252254

253255
val isAccountExist = accountManager.exists(currentDownload?.user?.toPlatformAccount())
@@ -410,7 +412,7 @@ class FileDownloadWorker(
410412
}
411413

412414
lastPercent = percent
413-
setWorkerState(user, lastPercent)
415+
EventBusFactory.downloadProgressEventBus.post(FileDownloadProgressEvent(percent))
414416
}
415417

416418
// CHECK: Is this class still needed after conversion from Foreground Services to Worker?

app/src/main/java/com/nextcloud/model/WorkerState.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import com.owncloud.android.operations.DownloadFileOperation
1313

1414
sealed class WorkerState {
1515
data class DownloadFinished(var currentFile: OCFile?) : WorkerState()
16-
data class DownloadStarted(var user: User?, var currentDownload: DownloadFileOperation?, var percent: Int) :
17-
WorkerState()
16+
data class DownloadStarted(var user: User?, var currentDownload: DownloadFileOperation?) : WorkerState()
1817
data class UploadFinished(var currentFile: OCFile?) : WorkerState()
1918
data class UploadStarted(var user: User?) : WorkerState()
2019
data object OfflineOperationsCompleted : WorkerState()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Nextcloud - Android Client
3+
*
4+
* SPDX-FileCopyrightText: 2025 Alper Ozturk <[email protected]>
5+
* SPDX-License-Identifier: AGPL-3.0-or-later
6+
*/
7+
8+
package com.owncloud.android.ui.events
9+
10+
import org.greenrobot.eventbus.EventBus
11+
12+
object EventBusFactory {
13+
14+
val downloadProgressEventBus: EventBus = EventBus.builder()
15+
.logNoSubscriberMessages(false)
16+
.sendNoSubscriberEvent(false)
17+
.build()
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* Nextcloud - Android Client
3+
*
4+
* SPDX-FileCopyrightText: 2025 Alper Ozturk <[email protected]>
5+
* SPDX-License-Identifier: AGPL-3.0-or-later
6+
*/
7+
8+
package com.owncloud.android.ui.events
9+
10+
data class FileDownloadProgressEvent(val percent: Int)

app/src/main/java/com/owncloud/android/ui/fragment/FileDetailFragment.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@
6161
import com.owncloud.android.ui.adapter.progressListener.DownloadProgressListener;
6262
import com.owncloud.android.ui.dialog.RemoveFilesDialogFragment;
6363
import com.owncloud.android.ui.dialog.RenameFileDialogFragment;
64+
import com.owncloud.android.ui.events.EventBusFactory;
6465
import com.owncloud.android.ui.events.FavoriteEvent;
66+
import com.owncloud.android.ui.events.FileDownloadProgressEvent;
6567
import com.owncloud.android.utils.DisplayUtils;
6668
import com.owncloud.android.utils.EncryptionUtils;
6769
import com.owncloud.android.utils.MimeTypeUtil;
@@ -394,6 +396,7 @@ public void onStart() {
394396
super.onStart();
395397
listenForTransferProgress();
396398
EventBus.getDefault().register(this);
399+
EventBusFactory.INSTANCE.getDownloadProgressEventBus().register(this);
397400
}
398401

399402
@Override
@@ -417,6 +420,7 @@ public void onStop() {
417420
}
418421

419422
EventBus.getDefault().unregister(this);
423+
EventBusFactory.INSTANCE.getDownloadProgressEventBus().unregister(this);
420424
super.onStop();
421425
}
422426

@@ -609,28 +613,27 @@ public void updateFileDetails(boolean transferring, boolean refresh) {
609613
observeWorkerState();
610614
}
611615

616+
@Subscribe(threadMode = ThreadMode.MAIN)
617+
public void onDownloadProgress(FileDownloadProgressEvent event) {
618+
if (binding.progressBlock.getVisibility() != View.VISIBLE) {
619+
binding.progressBlock.setVisibility(View.VISIBLE);
620+
}
621+
622+
binding.progressText.setText(R.string.downloader_download_in_progress_ticker);
623+
binding.progressBar.setProgress(event.getPercent());
624+
binding.progressBar.invalidate();
625+
}
626+
612627
private void observeWorkerState() {
613628
WorkerStateLiveData.Companion.instance().observe(getViewLifecycleOwner(), state -> {
614-
if (state instanceof WorkerState.DownloadStarted downloadState) {
615-
updateProgressBar(downloadState);
616-
} else if (state instanceof WorkerState.UploadStarted) {
629+
if (state instanceof WorkerState.UploadStarted) {
617630
binding.progressText.setText(R.string.uploader_upload_in_progress_ticker);
618631
} else {
619632
binding.progressBlock.setVisibility(View.GONE);
620633
}
621634
});
622635
}
623636

624-
private void updateProgressBar(WorkerState.DownloadStarted downloadState) {
625-
if (binding.progressBlock.getVisibility() != View.VISIBLE) {
626-
binding.progressBlock.setVisibility(View.VISIBLE);
627-
}
628-
629-
binding.progressText.setText(R.string.downloader_download_in_progress_ticker);
630-
binding.progressBar.setProgress(downloadState.getPercent());
631-
binding.progressBar.invalidate();
632-
}
633-
634637
private void setFileModificationTimestamp(OCFile file, boolean showDetailedTimestamp) {
635638
if (showDetailedTimestamp) {
636639
binding.lastModificationTimestamp.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));

0 commit comments

Comments
 (0)