Browse Source

Rebase master

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

+ 1 - 1
app/src/androidTest/java/com/owncloud/android/files/FileMenuFilterIT.kt

@@ -77,7 +77,7 @@ class FileMenuFilterIT : AbstractIT() {
         MockKAnnotations.init(this)
         every { mockFileUploaderBinder.isUploading(any(), any()) } returns false
         every { mockComponentsGetter.fileUploaderBinder } returns mockFileUploaderBinder
-        every { mockFileDownloaderBinder.isDownloading() } returns false
+        every { mockFileDownloaderBinder.isDownloading(any(), any()) } returns false
         every { mockComponentsGetter.fileDownloaderBinder } returns mockFileDownloaderBinder
         every { mockOperationsServiceBinder.isSynchronizing(any(), any()) } returns false
         every { mockComponentsGetter.operationsServiceBinder } returns mockOperationsServiceBinder

+ 6 - 4
app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt

@@ -34,10 +34,8 @@ import androidx.work.WorkerParameters
 import com.google.gson.Gson
 import com.nextcloud.client.account.User
 import com.nextcloud.client.account.UserAccountManager
-import com.nextcloud.client.jobs.BackgroundJobManagerImpl.Companion.JOB_FILES_DOWNLOAD
 import com.nextcloud.client.notifications.download.DownloadNotificationManager
 import com.nextcloud.java.util.Optional
-import com.nextcloud.utils.extensions.isWorkScheduled
 import com.owncloud.android.datamodel.FileDataStorageManager
 import com.owncloud.android.datamodel.OCFile
 import com.owncloud.android.datamodel.UploadsStorageManager
@@ -122,6 +120,7 @@ class FileDownloadWorker(
         }
     }
 
+    // FIXME not returning multiple string for folder
     private fun getRequestDownloads(): AbstractList<String> {
         conflictUploadId = inputData.keyValueMap[CONFLICT_UPLOAD_ID] as Long?
         val file = gson.fromJson(inputData.keyValueMap[FILE] as String, OCFile::class.java)
@@ -366,8 +365,11 @@ class FileDownloadWorker(
             removePendingDownload(accountName)
         }
 
-        fun isDownloading(): Boolean {
-            return context.isWorkScheduled(JOB_FILES_DOWNLOAD)
+        fun isDownloading(user: User?, file: OCFile?): Boolean {
+            return user != null && file != null && helper.backgroundJobManager.isStartFileDownloadJobScheduled(
+                user,
+                file
+            )
         }
 
         fun addDataTransferProgressListener(listener: OnDatatransferProgressListener?, file: OCFile?) {

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

@@ -148,6 +148,8 @@ interface BackgroundJobManager {
 
     fun cancelFilesDownloadJob(user: User)
 
+    fun isStartFileDownloadJobScheduled(user: User, file: OCFile): Boolean
+
     @Suppress("LongParameterList")
     fun startFilesDownloadJob(
         user: User,

+ 11 - 1
app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt

@@ -41,6 +41,7 @@ import com.nextcloud.client.di.Injectable
 import com.nextcloud.client.documentscan.GeneratePdfFromImagesWork
 import com.nextcloud.client.files.downloader.FileDownloadWorker
 import com.nextcloud.client.preferences.AppPreferences
+import com.nextcloud.utils.extensions.isWorkScheduled
 import com.owncloud.android.datamodel.OCFile
 import com.owncloud.android.lib.common.operations.OperationCancelledException
 import com.owncloud.android.operations.DownloadType
@@ -538,6 +539,14 @@ internal class BackgroundJobManagerImpl(
             .build()
     }
 
+    private fun startFileDownloadJobTag(user: User, file: OCFile): String {
+        return JOB_FILES_DOWNLOAD + user.accountName + file.fileId
+    }
+
+    override fun isStartFileDownloadJobScheduled(user: User, file: OCFile): Boolean {
+        return workManager.isWorkScheduled(startFileDownloadJobTag(user, file))
+    }
+
     @Throws(OperationCancelledException::class)
     override fun startFilesDownloadJob(
         user: User,
@@ -597,7 +606,8 @@ internal class BackgroundJobManagerImpl(
             conflictUploadId
         )
 
-        workManager.enqueueUniqueWork(JOB_FILES_DOWNLOAD + user.accountName, ExistingWorkPolicy.REPLACE, request)
+        val tag = startFileDownloadJobTag(user, ocFile)
+        workManager.enqueueUniqueWork(tag, ExistingWorkPolicy.REPLACE, request)
     }
 
     override fun getFileUploads(user: User): LiveData<List<JobInfo>> {

+ 1 - 23
app/src/main/java/com/nextcloud/utils/extensions/ContextExtensions.kt

@@ -27,12 +27,8 @@ import android.content.Context
 import android.content.Intent
 import android.content.IntentFilter
 import android.os.Build
-import androidx.work.WorkInfo
 import androidx.work.WorkManager
-import com.google.common.util.concurrent.ListenableFuture
 import com.owncloud.android.datamodel.ReceiverFlag
-import com.owncloud.android.lib.common.utils.Log_OC
-import java.util.concurrent.ExecutionException
 
 @SuppressLint("UnspecifiedRegisterReceiverFlag")
 fun Context.registerBroadcastReceiver(receiver: BroadcastReceiver?, filter: IntentFilter, flag: ReceiverFlag): Intent? {
@@ -44,23 +40,5 @@ fun Context.registerBroadcastReceiver(receiver: BroadcastReceiver?, filter: Inte
 }
 
 fun Context.isWorkScheduled(tag: String): Boolean {
-    val instance = WorkManager.getInstance(this)
-    val statuses: ListenableFuture<List<WorkInfo>> = instance.getWorkInfosByTag(tag)
-    var running = false
-    var workInfoList: List<WorkInfo> = emptyList()
-
-    try {
-        workInfoList = statuses.get()
-    } catch (e: ExecutionException) {
-        Log_OC.d("Worker", "ExecutionException in isWorkScheduled: $e")
-    } catch (e: InterruptedException) {
-        Log_OC.d("Worker", "InterruptedException in isWorkScheduled: $e")
-    }
-
-    for (workInfo in workInfoList) {
-        val state = workInfo.state
-        running = running || (state == WorkInfo.State.RUNNING || state == WorkInfo.State.ENQUEUED)
-    }
-
-    return running
+    return WorkManager.getInstance(this).isWorkScheduled(tag)
 }

+ 49 - 0
app/src/main/java/com/nextcloud/utils/extensions/WorkManagerExtensions.kt

@@ -0,0 +1,49 @@
+/*
+ * Nextcloud Android client application
+ *
+ * @author Alper Ozturk
+ * Copyright (C) 2023 Alper Ozturk
+ * Copyright (C) 2023 Nextcloud GmbH
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package com.nextcloud.utils.extensions
+
+import androidx.work.WorkInfo
+import androidx.work.WorkManager
+import com.google.common.util.concurrent.ListenableFuture
+import com.owncloud.android.lib.common.utils.Log_OC
+import java.util.concurrent.ExecutionException
+
+fun WorkManager.isWorkScheduled(tag: String): Boolean {
+    val statuses: ListenableFuture<List<WorkInfo>> = this.getWorkInfosByTag(tag)
+    var running = false
+    var workInfoList: List<WorkInfo> = emptyList()
+
+    try {
+        workInfoList = statuses.get()
+    } catch (e: ExecutionException) {
+        Log_OC.d("Worker", "ExecutionException in isWorkScheduled: $e")
+    } catch (e: InterruptedException) {
+        Log_OC.d("Worker", "InterruptedException in isWorkScheduled: $e")
+    }
+
+    for (workInfo in workInfoList) {
+        val state = workInfo.state
+        running = running || (state == WorkInfo.State.RUNNING || state == WorkInfo.State.ENQUEUED)
+    }
+
+    return running
+}

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

@@ -400,9 +400,10 @@ public class FileMenuFilter {
 
     private boolean anyFileDownloading(FileDownloadWorker.FileDownloaderBinder downloaderBinder) {
         boolean downloading = false;
+
         if (downloaderBinder != null) {
             for (Iterator<OCFile> iterator = files.iterator(); !downloading && iterator.hasNext(); ) {
-                downloading = downloaderBinder.isDownloading();
+                downloading = downloaderBinder.isDownloading(user, iterator.next());
             }
         }
         return downloading;

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

@@ -1884,7 +1884,7 @@ public class FileDisplayActivity extends FileActivity
 
     private void requestForDownload() {
         User user = getUser().orElseThrow(RuntimeException::new);
-        if (!mDownloaderBinder.isDownloading()) {
+        if (!mDownloaderBinder.isDownloading(user, mWaitingToPreview)) {
             new FileDownloadHelper().downloadFile(user, mWaitingToPreview);
         }
     }
@@ -1956,7 +1956,7 @@ public class FileDisplayActivity extends FileActivity
 
     private void requestForDownload(OCFile file, String downloadBehaviour, String packageName, String activityName) {
         final User currentUser = getUser().orElseThrow(RuntimeException::new);
-        if (!mDownloaderBinder.isDownloading()) {
+        if (!mDownloaderBinder.isDownloading(currentUser, file)) {
             new FileDownloadHelper().downloadFile(currentUser, file, downloadBehaviour, DownloadType.DOWNLOAD, activityName, packageName, null);
         }
     }

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

@@ -344,7 +344,7 @@ class OCFileListDelegate(
         val fileUploaderBinder = transferServiceGetter.fileUploaderBinder
         when {
             operationsServiceBinder?.isSynchronizing(user, file) == true ||
-                fileDownloaderBinder?.isDownloading() == true ||
+                fileDownloaderBinder?.isDownloading(user, file) == true ||
                 fileUploaderBinder?.isUploading(user, file) == true -> {
                 // synchronizing, downloading or uploading
                 gridViewHolder.localFileIndicator.setImageResource(R.drawable.ic_synchronizing)

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

@@ -537,7 +537,7 @@ public class FileDetailFragment extends FileFragment implements OnClickListener,
             FileDownloadWorker.FileDownloaderBinder downloaderBinder = containerActivity.getFileDownloaderBinder();
             FileUploaderBinder uploaderBinder = containerActivity.getFileUploaderBinder();
             if (transferring
-                || (downloaderBinder != null && downloaderBinder.isDownloading())
+                || (downloaderBinder != null && downloaderBinder.isDownloading(user, file))
                 || (uploaderBinder != null && uploaderBinder.isUploading(user, file))) {
                 setButtonsForTransferring();
 
@@ -661,8 +661,7 @@ public class FileDetailFragment extends FileFragment implements OnClickListener,
             binding.progressText.setVisibility(View.VISIBLE);
             FileDownloadWorker.FileDownloaderBinder downloaderBinder = containerActivity.getFileDownloaderBinder();
             FileUploaderBinder uploaderBinder = containerActivity.getFileUploaderBinder();
-            //if (getFile().isDownloading()) {
-            if (downloaderBinder != null && downloaderBinder.isDownloading()) {
+            if (downloaderBinder != null && downloaderBinder.isDownloading(user, getFile())) {
                 binding.progressText.setText(R.string.downloader_download_in_progress_ticker);
             } else {
                 if (uploaderBinder != null && uploaderBinder.isUploading(user, getFile())) {

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

@@ -998,7 +998,7 @@ public class FileOperationsHelper {
 
         // for both files and folders
         FileDownloadWorker.FileDownloaderBinder downloaderBinder = fileActivity.getFileDownloaderBinder();
-        if (downloaderBinder != null && downloaderBinder.isDownloading()) {
+        if (downloaderBinder != null && downloaderBinder.isDownloading(currentUser, file)) {
             downloaderBinder.cancelPendingOrCurrentDownloads();
         }
         FileUploaderBinder uploaderBinder = fileActivity.getFileUploaderBinder();

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

@@ -408,11 +408,11 @@ public class PreviewImageActivity extends FileActivity implements
     }
 
     public void requestForDownload(OCFile file, String downloadBehaviour) {
+        final User user = getUser().orElseThrow(RuntimeException::new);
+
         if (mDownloaderBinder == null) {
             Log_OC.d(TAG, "requestForDownload called without binder to download service");
-
-        } else if (!mDownloaderBinder.isDownloading()) {
-            final User user = getUser().orElseThrow(RuntimeException::new);
+        } else if (!mDownloaderBinder.isDownloading(user, file)) {
             new FileDownloadHelper().downloadFile(user, file, downloadBehaviour);
         }
     }

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

@@ -478,7 +478,7 @@ public class PreviewMediaFragment extends FileFragment implements OnTouchListene
                                                                     getView(),
                                                                     backgroundJobManager);
         } else if (itemId == R.id.action_download_file) {
-            if (!containerActivity.getFileDownloaderBinder().isDownloading()) {
+            if (!containerActivity.getFileDownloaderBinder().isDownloading(user, getFile())) {
                 new FileDownloadHelper().downloadFile(user, getFile());
             }
         }