Browse Source

Fix code analytics

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 1 year ago
parent
commit
a6c88a4211

+ 22 - 102
app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadHelper.kt

@@ -27,14 +27,13 @@ import com.owncloud.android.MainApp
 import com.owncloud.android.datamodel.FileDataStorageManager
 import com.owncloud.android.datamodel.OCFile
 import com.owncloud.android.datamodel.UploadsStorageManager
-import com.owncloud.android.lib.common.network.OnDatatransferProgressListener
 import com.owncloud.android.operations.DownloadFileOperation
 import com.owncloud.android.operations.DownloadType
 import com.owncloud.android.utils.MimeTypeUtil
 import java.io.File
 import javax.inject.Inject
 
-class FileDownloadHelper : OnDatatransferProgressListener {
+class FileDownloadHelper {
 
     @Inject
     lateinit var backgroundJobManager: BackgroundJobManager
@@ -42,49 +41,20 @@ class FileDownloadHelper : OnDatatransferProgressListener {
     @Inject
     lateinit var uploadsStorageManager: UploadsStorageManager
 
-    private val boundListeners: MutableMap<Long, OnDatatransferProgressListener> = HashMap()
-    private var currentDownload: DownloadFileOperation? = null
+    companion object {
+        private var instance: FileDownloadHelper? = null
 
-    init {
-        MainApp.getAppComponent().inject(this)
-    }
-
-    fun addDataTransferProgressListener(listener: OnDatatransferProgressListener?, file: OCFile?) {
-        if (file == null || listener == null) {
-            return
-        }
-
-        boundListeners[file.fileId] = listener
-    }
-
-    fun removeDataTransferProgressListener(listener: OnDatatransferProgressListener?, file: OCFile?) {
-        if (file == null || listener == null) {
-            return
-        }
-
-        val fileId = file.fileId
-        if (boundListeners[fileId] === listener) {
-            boundListeners.remove(fileId)
+        fun instance(): FileDownloadHelper {
+            return if (instance == null) {
+                FileDownloadHelper()
+            } else {
+                instance!!
+            }
         }
     }
 
-    override fun onTransferProgress(
-        progressRate: Long,
-        totalTransferredSoFar: Long,
-        totalToTransfer: Long,
-        fileName: String
-    ) {
-        val listener = boundListeners[currentDownload?.file?.fileId]
-        listener?.onTransferProgress(
-            progressRate,
-            totalTransferredSoFar,
-            totalToTransfer,
-            fileName
-        )
-    }
-
-    fun setCurrentDownload(operation: DownloadFileOperation) {
-        currentDownload = operation
+    init {
+        MainApp.getAppComponent().inject(this)
     }
 
     fun isDownloading(user: User?, file: OCFile?): Boolean {
@@ -145,75 +115,25 @@ class FileDownloadHelper : OnDatatransferProgressListener {
         storageManager?.saveConflict(file, null)
     }
 
-    fun downloadFile(user: User, ocFile: OCFile) {
-        backgroundJobManager.startFileDownloadJob(
-            user,
-            ocFile,
-            "",
-            DownloadType.DOWNLOAD,
-            "",
-            "",
-            null
-        )
-    }
-
-    fun downloadFile(user: User, ocFile: OCFile, behaviour: String) {
-        backgroundJobManager.startFileDownloadJob(
-            user,
-            ocFile,
-            behaviour,
-            DownloadType.DOWNLOAD,
-            "",
-            "",
-            null
-        )
-    }
-
-    fun downloadFile(user: User, ocFile: OCFile, behaviour: String, packageName: String, activityName: String) {
-        backgroundJobManager.startFileDownloadJob(
-            user,
-            ocFile,
-            behaviour,
-            DownloadType.DOWNLOAD,
-            packageName,
-            packageName,
-            null
-        )
-    }
-
-    fun downloadFile(user: User, ocFile: OCFile, downloadType: DownloadType) {
-        backgroundJobManager.startFileDownloadJob(
-            user,
-            ocFile,
-            "",
-            downloadType,
-            "",
-            "",
-            null
-        )
+    fun downloadFileIfNotStartedBefore(user: User, file: OCFile) {
+        if (!isDownloading(user, file)) {
+            downloadFile(user, file, downloadType = DownloadType.DOWNLOAD)
+        }
     }
 
-    fun downloadFile(user: User, ocFile: OCFile, conflictUploadId: Long) {
-        backgroundJobManager.startFileDownloadJob(
-            user,
-            ocFile,
-            "",
-            DownloadType.DOWNLOAD,
-            "",
-            "",
-            conflictUploadId
-        )
+    fun downloadFile(user: User, ocFile: OCFile) {
+        downloadFile(user, ocFile, downloadType = DownloadType.DOWNLOAD)
     }
 
     @Suppress("LongParameterList")
     fun downloadFile(
         user: User,
         ocFile: OCFile,
-        behaviour: String,
-        downloadType: DownloadType?,
-        activityName: String,
-        packageName: String,
-        conflictUploadId: Long?
+        behaviour: String = "",
+        downloadType: DownloadType? = DownloadType.DOWNLOAD,
+        activityName: String = "",
+        packageName: String = "",
+        conflictUploadId: Long? = null
     ) {
         backgroundJobManager.startFileDownloadJob(
             user,

+ 2 - 3
app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt

@@ -97,7 +97,6 @@ class FileDownloadWorker(
     private val pendingDownloads = IndexedForest<DownloadFileOperation>()
     private var downloadProgressListener = FileDownloadProgressListener()
     private var currentUser = Optional.empty<User>()
-    private val helper = FileDownloadHelper()
     private var startedDownload = false
     private var storageManager: FileDataStorageManager? = null
     private var downloadClient: OwnCloudClient? = null
@@ -230,7 +229,7 @@ class FileDownloadWorker(
             downloadResult = currentDownload?.execute(downloadClient)
             if (downloadResult?.isSuccess == true && currentDownload?.downloadType === DownloadType.DOWNLOAD) {
                 getCurrentFile()?.let {
-                    helper.saveFile(it, currentDownload, storageManager)
+                    FileDownloadHelper.instance().saveFile(it, currentDownload, storageManager)
                 }
             }
         } catch (e: Exception) {
@@ -366,7 +365,7 @@ class FileDownloadWorker(
         private val boundListeners: MutableMap<Long, OnDatatransferProgressListener> = HashMap()
 
         fun isDownloading(user: User?, file: OCFile?): Boolean {
-            return helper.isDownloading(user, file)
+            return FileDownloadHelper.instance().isDownloading(user, file)
         }
 
         fun addDataTransferProgressListener(listener: OnDatatransferProgressListener?, file: OCFile?) {

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

@@ -111,10 +111,10 @@ class FilesExportWork(
     }
 
     private fun downloadFile(ocFile: OCFile) {
-        FileDownloadHelper().downloadFile(
+        FileDownloadHelper.instance().downloadFile(
             user,
             ocFile,
-            DownloadType.EXPORT
+            downloadType = DownloadType.EXPORT
         )
     }
 

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

@@ -380,9 +380,8 @@ public class FileMenuFilter {
         if (componentsGetter != null && !files.isEmpty() && user != null) {
             OperationsServiceBinder opsBinder = componentsGetter.getOperationsServiceBinder();
             FileUploaderBinder uploaderBinder = componentsGetter.getFileUploaderBinder();
-            FileDownloadHelper fileDownloadHelper = new FileDownloadHelper();
             synchronizing = anyFileSynchronizing(opsBinder) ||      // comparing local and remote
-                            anyFileDownloading(fileDownloadHelper) ||
+                            anyFileDownloading() ||
                             anyFileUploading(uploaderBinder);
         }
         return synchronizing;
@@ -398,9 +397,9 @@ public class FileMenuFilter {
         return synchronizing;
     }
 
-    private boolean anyFileDownloading(FileDownloadHelper downloadHelper) {
+    private boolean anyFileDownloading() {
         for (OCFile file : files) {
-            if (downloadHelper.isDownloading(user, file)) {
+            if (FileDownloadHelper.Companion.instance().isDownloading(user, file)) {
                 return true;
             }
         }

+ 1 - 12
app/src/main/java/com/owncloud/android/operations/SynchronizeFileOperation.java

@@ -309,30 +309,19 @@ public class SynchronizeFileOperation extends SyncOperation {
         mTransferWasRequested = true;
     }
 
-
-    /**
-     * Requests for a download to the FileDownloader service
-     *
-     * @param file OCFile object representing the file to download
-     */
     private void requestForDownload(OCFile file) {
-        FileDownloadHelper downloadHelper = new FileDownloadHelper();
-
-        downloadHelper.downloadFile(
+        FileDownloadHelper.Companion.instance().downloadFile(
             mUser,
             file);
 
         mTransferWasRequested = true;
     }
 
-
     public boolean transferWasRequested() {
         return mTransferWasRequested;
     }
 
-
     public OCFile getLocalFile() {
         return mLocalFile;
     }
-
 }

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

@@ -445,14 +445,12 @@ public class SynchronizeFolderOperation extends SyncOperation {
     }
 
     private void startDirectDownloads() throws OperationCancelledException {
-        FileDownloadHelper downloadHelper = new FileDownloadHelper();
-
         for (OCFile file : mFilesForDirectDownload) {
             synchronized(mCancellationRequested) {
                 if (mCancellationRequested.get()) {
                     throw new OperationCancelledException();
                 }
-                downloadHelper.downloadFile(user, file);
+                FileDownloadHelper.Companion.instance().downloadFile(user, file);
             }
         }
     }

+ 2 - 2
app/src/main/java/com/owncloud/android/ui/activity/ConflictsResolveActivity.kt

@@ -115,10 +115,10 @@ class ConflictsResolveActivity : FileActivity(), OnConflictDecisionMadeListener
                 Decision.KEEP_SERVER -> if (!shouldDeleteLocal()) {
                     // Overwrite local file
                     file?.let {
-                        FileDownloadHelper().downloadFile(
+                        FileDownloadHelper.instance().downloadFile(
                             getUser().orElseThrow { RuntimeException() },
                             file,
-                            conflictUploadId
+                            conflictUploadId = conflictUploadId
                         )
                     }
                 } else {

+ 1 - 2
app/src/main/java/com/owncloud/android/ui/activity/FileActivity.java

@@ -166,7 +166,6 @@ public abstract class FileActivity extends DrawerActivity
 
     private boolean mResumed;
 
-    protected FileDownloadHelper fileDownloadHelper = new FileDownloadHelper();
     protected FileDownloadWorker.FileDownloadProgressListener fileDownloadProgressListener;
     protected FileUploaderBinder mUploaderBinder;
     private ServiceConnection mUploadServiceConnection;
@@ -235,7 +234,7 @@ public abstract class FileActivity extends DrawerActivity
                 Context.BIND_AUTO_CREATE);
 
         if (user != null) {
-            new FileDownloadHelper().downloadFile(user, mFile);
+            FileDownloadHelper.Companion.instance().downloadFile(user, mFile);
         }
 
         mUploadServiceConnection = newTransferenceServiceConnection();

+ 4 - 5
app/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java

@@ -65,6 +65,7 @@ import com.nextcloud.client.core.AsyncRunner;
 import com.nextcloud.client.di.Injectable;
 import com.nextcloud.client.editimage.EditImageActivity;
 import com.nextcloud.client.files.DeepLinkHandler;
+import com.nextcloud.client.files.downloader.FileDownloadHelper;
 import com.nextcloud.client.files.downloader.FileDownloadWorker;
 import com.nextcloud.client.media.PlayerServiceConnection;
 import com.nextcloud.client.network.ClientFactory;
@@ -1896,9 +1897,7 @@ public class FileDisplayActivity extends FileActivity
 
     private void requestForDownload() {
         User user = getUser().orElseThrow(RuntimeException::new);
-        if (!fileDownloadHelper.isDownloading(user, mWaitingToPreview)) {
-            fileDownloadHelper.downloadFile(user, mWaitingToPreview);
-        }
+        FileDownloadHelper.Companion.instance().downloadFileIfNotStartedBefore(user, mWaitingToPreview);
     }
 
     @Override
@@ -1968,8 +1967,8 @@ public class FileDisplayActivity extends FileActivity
 
     private void requestForDownload(OCFile file, String downloadBehaviour, String packageName, String activityName) {
         final User currentUser = getUser().orElseThrow(RuntimeException::new);
-        if (!fileDownloadHelper.isDownloading(currentUser, file)) {
-            fileDownloadHelper.downloadFile(currentUser, file, downloadBehaviour, DownloadType.DOWNLOAD, activityName, packageName, null);
+        if (!FileDownloadHelper.Companion.instance().isDownloading(currentUser, file)) {
+            FileDownloadHelper.Companion.instance().downloadFile(currentUser, file, downloadBehaviour, DownloadType.DOWNLOAD, activityName, packageName, null);
         }
     }
 

+ 3 - 2
app/src/main/java/com/owncloud/android/ui/activity/ManageAccountsActivity.java

@@ -41,6 +41,7 @@ import android.view.View;
 import com.google.common.collect.Sets;
 import com.nextcloud.client.account.User;
 import com.nextcloud.client.account.UserAccountManager;
+import com.nextcloud.client.files.downloader.FileDownloadHelper;
 import com.nextcloud.client.jobs.BackgroundJobManager;
 import com.nextcloud.client.onboarding.FirstRunActivity;
 import com.nextcloud.java.util.Optional;
@@ -341,7 +342,7 @@ public class ManageAccountsActivity extends FileActivity implements UserListAdap
                     mUploaderBinder.cancel(accountName);
                 }
 
-                fileDownloadHelper.cancelAllDownloadsForAccount(workerAccountName, workerCurrentDownload);
+                FileDownloadHelper.Companion.instance().cancelAllDownloadsForAccount(workerAccountName, workerCurrentDownload);
             }
 
             User currentUser = getUserAccountManager().getUser();
@@ -431,7 +432,7 @@ public class ManageAccountsActivity extends FileActivity implements UserListAdap
             mUploaderBinder.cancel(user);
         }
 
-        fileDownloadHelper.cancelAllDownloadsForAccount(workerAccountName, workerCurrentDownload);
+        FileDownloadHelper.Companion.instance().cancelAllDownloadsForAccount(workerAccountName, workerCurrentDownload);
 
         backgroundJobManager.startAccountRemovalJob(user.getAccountName(), false);
 

+ 15 - 11
app/src/main/java/com/owncloud/android/ui/adapter/OCFileListDelegate.kt

@@ -341,27 +341,31 @@ class OCFileListDelegate(
 
     private fun showLocalFileIndicator(file: OCFile, gridViewHolder: ListGridImageViewHolder) {
         val operationsServiceBinder = transferServiceGetter.operationsServiceBinder
-        val fileDownloadHelper = FileDownloadHelper()
         val fileUploaderBinder = transferServiceGetter.fileUploaderBinder
-        when {
+
+        val icon: Int? = when {
             operationsServiceBinder?.isSynchronizing(user, file) == true ||
-                fileDownloadHelper.isDownloading(user, file) ||
+                FileDownloadHelper.instance().isDownloading(user, file) ||
                 fileUploaderBinder?.isUploading(user, file) == true -> {
                 // synchronizing, downloading or uploading
-                gridViewHolder.localFileIndicator.setImageResource(R.drawable.ic_synchronizing)
-                gridViewHolder.localFileIndicator.visibility = View.VISIBLE
+                R.drawable.ic_synchronizing
             }
             file.etagInConflict != null -> {
-                // conflict
-                gridViewHolder.localFileIndicator.setImageResource(R.drawable.ic_synchronizing_error)
-                gridViewHolder.localFileIndicator.visibility = View.VISIBLE
+                R.drawable.ic_synchronizing_error
             }
             file.isDown -> {
-                // downloaded
-                gridViewHolder.localFileIndicator.setImageResource(R.drawable.ic_synced)
-                gridViewHolder.localFileIndicator.visibility = View.VISIBLE
+                R.drawable.ic_synced
             }
+            else -> { null }
         }
+
+        gridViewHolder.localFileIndicator.run {
+            icon?.let {
+                setImageResource(icon)
+                visibility = View.VISIBLE
+            }
+        }
+
     }
 
     private fun showShareIcon(gridViewHolder: ListGridImageViewHolder, file: OCFile) {

+ 2 - 4
app/src/main/java/com/owncloud/android/ui/fragment/FileDetailFragment.java

@@ -534,10 +534,9 @@ public class FileDetailFragment extends FileFragment implements OnClickListener,
             setFavoriteIconStatus(file.isFavorite());
 
             // configure UI for depending upon local state of the file
-            FileDownloadHelper fileDownloadHelper = new FileDownloadHelper();
             FileUploaderBinder uploaderBinder = containerActivity.getFileUploaderBinder();
             if (transferring
-                || (fileDownloadHelper.isDownloading(user, file))
+                || (FileDownloadHelper.Companion.instance().isDownloading(user, file))
                 || (uploaderBinder != null && uploaderBinder.isUploading(user, file))) {
                 setButtonsForTransferring();
 
@@ -659,9 +658,8 @@ public class FileDetailFragment extends FileFragment implements OnClickListener,
             // show the progress bar for the transfer
             binding.progressBlock.setVisibility(View.VISIBLE);
             binding.progressText.setVisibility(View.VISIBLE);
-            FileDownloadHelper fileDownloadHelper = new FileDownloadHelper();
             FileUploaderBinder uploaderBinder = containerActivity.getFileUploaderBinder();
-            if (fileDownloadHelper.isDownloading(user, getFile())) {
+            if (FileDownloadHelper.Companion.instance().isDownloading(user, getFile())) {
                 binding.progressText.setText(R.string.downloader_download_in_progress_ticker);
             } else {
                 if (uploaderBinder != null && uploaderBinder.isUploading(user, getFile())) {

+ 3 - 4
app/src/main/java/com/owncloud/android/ui/helpers/FileOperationsHelper.java

@@ -996,11 +996,10 @@ public class FileOperationsHelper {
             }
         }
 
-        // for both files and folders
-        FileDownloadHelper fileDownloadHelper = new FileDownloadHelper();
-        if (fileDownloadHelper.isDownloading(currentUser, file)) {
-            fileDownloadHelper.cancelPendingOrCurrentDownloads(currentUser, file);
+        if (FileDownloadHelper.Companion.instance().isDownloading(currentUser, file)) {
+            FileDownloadHelper.Companion.instance().cancelPendingOrCurrentDownloads(currentUser, file);
         }
+
         FileUploaderBinder uploaderBinder = fileActivity.getFileUploaderBinder();
         if (uploaderBinder != null && uploaderBinder.isUploading(currentUser, file)) {
             uploaderBinder.cancel(currentUser.toPlatformAccount(), file);

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

@@ -37,6 +37,7 @@ import android.view.View;
 import com.nextcloud.client.account.User;
 import com.nextcloud.client.di.Injectable;
 import com.nextcloud.client.editimage.EditImageActivity;
+import com.nextcloud.client.files.downloader.FileDownloadHelper;
 import com.nextcloud.client.files.downloader.FileDownloadWorker;
 import com.nextcloud.client.preferences.AppPreferences;
 import com.nextcloud.java.util.Optional;
@@ -54,6 +55,7 @@ import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
 import com.owncloud.android.lib.common.operations.RemoteOperation;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.utils.Log_OC;
+import com.owncloud.android.operations.DownloadType;
 import com.owncloud.android.operations.RemoveFileOperation;
 import com.owncloud.android.operations.SynchronizeFileOperation;
 import com.owncloud.android.ui.activity.FileActivity;
@@ -419,8 +421,8 @@ public class PreviewImageActivity extends FileActivity implements
     public void requestForDownload(OCFile file, String downloadBehaviour) {
         final User user = getUser().orElseThrow(RuntimeException::new);
 
-        if (!fileDownloadHelper.isDownloading(user, file)) {
-            fileDownloadHelper.downloadFile(user, file, downloadBehaviour);
+        if (!FileDownloadHelper.Companion.instance().isDownloading(user, file)) {
+            FileDownloadHelper.Companion.instance().downloadFile(user, file, downloadBehaviour, DownloadType.DOWNLOAD, "", "", null);
         }
     }
 

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

@@ -62,6 +62,7 @@ import androidx.media3.ui.PlayerView
 import com.nextcloud.client.account.User
 import com.nextcloud.client.account.UserAccountManager
 import com.nextcloud.client.di.Injectable
+import com.nextcloud.client.files.downloader.FileDownloadHelper
 import com.nextcloud.client.jobs.BackgroundJobManager
 import com.nextcloud.client.media.ExoplayerListener
 import com.nextcloud.client.media.NextcloudExoPlayer.createNextcloudExoplayer
@@ -82,6 +83,7 @@ import com.owncloud.android.lib.common.operations.OnRemoteOperationListener
 import com.owncloud.android.lib.common.operations.RemoteOperation
 import com.owncloud.android.lib.common.operations.RemoteOperationResult
 import com.owncloud.android.lib.common.utils.Log_OC
+import com.owncloud.android.operations.DownloadType
 import com.owncloud.android.operations.RemoveFileOperation
 import com.owncloud.android.operations.SynchronizeFileOperation
 import com.owncloud.android.ui.activity.FileActivity
@@ -574,16 +576,17 @@ class PreviewMediaActivity :
         packageName: String? = null,
         activityName: String? = null
     ) {
-        if (fileDownloadHelper.isDownloading(user, file)) {
+        if (FileDownloadHelper.instance().isDownloading(user, file)) {
             return
         }
 
         user?.let { user ->
             file?.let { file ->
-                fileDownloadHelper.downloadFile(
+                FileDownloadHelper.instance().downloadFile(
                     user,
                     file,
                     downloadBehavior ?: "",
+                    DownloadType.DOWNLOAD,
                     packageName ?: "",
                     activityName ?: ""
                 )

+ 1 - 4
app/src/main/java/com/owncloud/android/ui/preview/PreviewMediaFragment.java

@@ -131,7 +131,6 @@ public class PreviewMediaFragment extends FileFragment implements OnTouchListene
 
     private boolean autoplay;
     private boolean isLivePhoto;
-    private final FileDownloadHelper downloadHelper = new FileDownloadHelper();
     private boolean prepared;
     private PlayerServiceConnection mediaPlayerServiceConnection;
 
@@ -479,9 +478,7 @@ public class PreviewMediaFragment extends FileFragment implements OnTouchListene
                                                                     getView(),
                                                                     backgroundJobManager);
         } else if (itemId == R.id.action_download_file) {
-            if (!downloadHelper.isDownloading(user, getFile())) {
-                downloadHelper.downloadFile(user, getFile());
-            }
+            FileDownloadHelper.Companion.instance().downloadFileIfNotStartedBefore(user, getFile());
         }
     }