Browse Source

Merge pull request #13920 from nextcloud/dont-check-singleton-instance-of-file-download-helper-in-loops

Don't Check or Create Singleton FileDownloadHelper Instance Inside Loops
Tobias Kaminsky 5 months ago
parent
commit
90ba36aeb2

+ 7 - 9
app/src/main/java/com/nextcloud/client/jobs/FilesExportWork.kt

@@ -57,6 +57,8 @@ class FilesExportWork(
     }
 
     private fun exportFiles(fileIDs: LongArray): Int {
+        val fileDownloadHelper = FileDownloadHelper.instance()
+
         var successfulExports = 0
         fileIDs
             .asSequence()
@@ -76,7 +78,11 @@ class FilesExportWork(
                         showErrorNotification(successfulExports)
                     }
                 } else {
-                    downloadFile(ocFile)
+                    fileDownloadHelper.downloadFile(
+                        user,
+                        ocFile,
+                        downloadType = DownloadType.EXPORT
+                    )
                 }
 
                 successfulExports++
@@ -95,14 +101,6 @@ class FilesExportWork(
         )
     }
 
-    private fun downloadFile(ocFile: OCFile) {
-        FileDownloadHelper.instance().downloadFile(
-            user,
-            ocFile,
-            downloadType = DownloadType.EXPORT
-        )
-    }
-
     private fun showErrorNotification(successfulExports: Int) {
         val message = if (successfulExports == 0) {
             appContext.resources.getQuantityString(R.plurals.export_failed, successfulExports, successfulExports)

+ 3 - 1
app/src/main/java/com/owncloud/android/files/FileMenuFilter.java

@@ -397,8 +397,10 @@ public class FileMenuFilter {
     }
 
     private boolean anyFileDownloading() {
+        final var fileDownloadHelper = FileDownloadHelper.Companion.instance();
+
         for (OCFile file : files) {
-            if (FileDownloadHelper.Companion.instance().isDownloading(user, file)) {
+            if (fileDownloadHelper.isDownloading(user, file)) {
                 return true;
             }
         }

+ 2 - 1
app/src/main/java/com/owncloud/android/operations/SynchronizeFolderOperation.java

@@ -441,7 +441,8 @@ public class SynchronizeFolderOperation extends SyncOperation {
     }
 
     private void startDirectDownloads() {
-        mFilesForDirectDownload.forEach(file -> FileDownloadHelper.Companion.instance().downloadFile(user, file));
+        final var fileDownloadHelper = FileDownloadHelper.Companion.instance();
+        mFilesForDirectDownload.forEach(file -> fileDownloadHelper.downloadFile(user, file));
     }
 
     /**

+ 2 - 1
app/src/main/java/com/owncloud/android/ui/adapter/OCFileListDelegate.kt

@@ -335,10 +335,11 @@ class OCFileListDelegate(
 
     private fun showLocalFileIndicator(file: OCFile, gridViewHolder: ListViewHolder) {
         val operationsServiceBinder = transferServiceGetter.operationsServiceBinder
+        val fileDownloadHelper = FileDownloadHelper.instance()
 
         val icon: Int? = when {
             operationsServiceBinder?.isSynchronizing(user, file) == true ||
-                FileDownloadHelper.instance().isDownloading(user, file) ||
+                fileDownloadHelper.isDownloading(user, file) ||
                 fileUploadHelper.isUploading(user, file) -> {
                 // synchronizing, downloading or uploading
                 R.drawable.ic_synchronizing

+ 4 - 2
app/src/main/java/com/owncloud/android/ui/preview/PreviewMediaActivity.kt

@@ -653,13 +653,15 @@ class PreviewMediaActivity :
         packageName: String? = null,
         activityName: String? = null
     ) {
-        if (FileDownloadHelper.instance().isDownloading(user, file)) {
+        val fileDownloadHelper = FileDownloadHelper.instance()
+
+        if (fileDownloadHelper.isDownloading(user, file)) {
             return
         }
 
         user?.let { user ->
             file?.let { file ->
-                FileDownloadHelper.instance().downloadFile(
+                fileDownloadHelper.downloadFile(
                     user,
                     file,
                     downloadBehavior ?: "",