ソースを参照

Fix FileDownloaderBinder functions

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 1 年間 前
コミット
efe6649b69

+ 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(any(), any()) } returns false
+        every { mockFileDownloaderBinder.isDownloading() } returns false
         every { mockComponentsGetter.fileDownloaderBinder } returns mockFileDownloaderBinder
         every { mockOperationsServiceBinder.isSynchronizing(any(), any()) } returns false
         every { mockComponentsGetter.operationsServiceBinder } returns mockOperationsServiceBinder

+ 11 - 20
app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt

@@ -28,15 +28,17 @@ import android.app.PendingIntent
 import android.content.Context
 import android.os.Binder
 import android.os.IBinder
-import android.util.Pair
 import androidx.localbroadcastmanager.content.LocalBroadcastManager
 import androidx.work.Worker
 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.cancelWork
+import com.nextcloud.utils.extensions.isWorkScheduled
 import com.owncloud.android.datamodel.FileDataStorageManager
 import com.owncloud.android.datamodel.OCFile
 import com.owncloud.android.datamodel.UploadsStorageManager
@@ -338,8 +340,10 @@ class FileDownloadWorker(
         val percent: Int = (100.0 * totalTransferredSoFar.toDouble() / totalToTransfer.toDouble()).toInt()
 
         if (percent != lastPercent) {
-            notificationManager.updateDownloadProgressNotification(filePath, percent, totalToTransfer)
-            notificationManager.showDownloadInProgressNotification()
+            notificationManager.run {
+                updateDownloadProgressNotification(filePath, percent, totalToTransfer)
+                showDownloadInProgressNotification()
+            }
         }
 
         lastPercent = percent
@@ -348,21 +352,8 @@ class FileDownloadWorker(
     inner class FileDownloaderBinder : Binder(), OnDatatransferProgressListener {
         private val boundListeners: MutableMap<Long, OnDatatransferProgressListener> = HashMap()
 
-        fun cancelPendingOrCurrentDownloads(account: Account, file: OCFile) {
-            val removeResult: Pair<DownloadFileOperation, String> =
-                pendingDownloads.remove(account.name, file.remotePath)
-            val download = removeResult.first
-
-            if (download != null) {
-                download.cancel()
-            } else {
-                if (currentUser?.isPresent == true &&
-                    currentDownload?.remotePath?.startsWith(file.remotePath) == true &&
-                    account.name == currentUser.get()?.accountName
-                ) {
-                    currentDownload?.cancel()
-                }
-            }
+        fun cancelPendingOrCurrentDownloads() {
+            context.cancelWork(JOB_FILES_DOWNLOAD)
         }
 
         fun cancelAllDownloadsForAccount(accountName: String?) {
@@ -373,8 +364,8 @@ class FileDownloadWorker(
             cancelPendingDownloads(accountName)
         }
 
-        fun isDownloading(user: User?, file: OCFile?): Boolean {
-            return user != null && file != null && pendingDownloads.contains(user.accountName, file.remotePath)
+        fun isDownloading(): Boolean {
+            return context.isWorkScheduled(JOB_FILES_DOWNLOAD)
         }
 
         fun addDataTransferProgressListener(listener: OnDatatransferProgressListener?, file: OCFile?) {

+ 31 - 0
app/src/main/java/com/nextcloud/utils/extensions/ContextExtensions.kt

@@ -27,7 +27,12 @@ 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? {
@@ -37,3 +42,29 @@ fun Context.registerBroadcastReceiver(receiver: BroadcastReceiver?, filter: Inte
         registerReceiver(receiver, filter)
     }
 }
+
+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
+}
+
+fun Context.cancelWork(tag: String) {
+    WorkManager.getInstance(this).cancelAllWorkByTag(tag)
+}

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

@@ -402,7 +402,7 @@ public class FileMenuFilter {
         boolean downloading = false;
         if (downloaderBinder != null) {
             for (Iterator<OCFile> iterator = files.iterator(); !downloading && iterator.hasNext(); ) {
-                downloading = downloaderBinder.isDownloading(user, iterator.next());
+                downloading = downloaderBinder.isDownloading();
             }
         }
         return downloading;

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

@@ -1884,8 +1884,7 @@ public class FileDisplayActivity extends FileActivity
 
     private void requestForDownload() {
         User user = getUser().orElseThrow(RuntimeException::new);
-        //if (!mWaitingToPreview.isDownloading()) {
-        if (!mDownloaderBinder.isDownloading(user, mWaitingToPreview)) {
+        if (!mDownloaderBinder.isDownloading()) {
             new FileDownloadHelper().downloadFile(user, mWaitingToPreview);
         }
     }
@@ -1957,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(currentUser, mWaitingToPreview)) {
+        if (!mDownloaderBinder.isDownloading()) {
             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(user, file) == true ||
+                fileDownloaderBinder?.isDownloading() == true ||
                 fileUploaderBinder?.isUploading(user, file) == true -> {
                 // synchronizing, downloading or uploading
                 gridViewHolder.localFileIndicator.setImageResource(R.drawable.ic_synchronizing)

+ 2 - 2
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(user, file))
+                || (downloaderBinder != null && downloaderBinder.isDownloading())
                 || (uploaderBinder != null && uploaderBinder.isUploading(user, file))) {
                 setButtonsForTransferring();
 
@@ -662,7 +662,7 @@ public class FileDetailFragment extends FileFragment implements OnClickListener,
             FileDownloadWorker.FileDownloaderBinder downloaderBinder = containerActivity.getFileDownloaderBinder();
             FileUploaderBinder uploaderBinder = containerActivity.getFileUploaderBinder();
             //if (getFile().isDownloading()) {
-            if (downloaderBinder != null && downloaderBinder.isDownloading(user, getFile())) {
+            if (downloaderBinder != null && downloaderBinder.isDownloading()) {
                 binding.progressText.setText(R.string.downloader_download_in_progress_ticker);
             } else {
                 if (uploaderBinder != null && uploaderBinder.isUploading(user, getFile())) {

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

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

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

@@ -411,7 +411,7 @@ public class PreviewImageActivity extends FileActivity implements
         if (mDownloaderBinder == null) {
             Log_OC.d(TAG, "requestForDownload called without binder to download service");
 
-        } else if (!mDownloaderBinder.isDownloading(getUserAccountManager().getUser(), file)) {
+        } else if (!mDownloaderBinder.isDownloading()) {
             final User user = getUser().orElseThrow(RuntimeException::new);
             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(user, getFile())) {
+            if (!containerActivity.getFileDownloaderBinder().isDownloading()) {
                 new FileDownloadHelper().downloadFile(user, getFile());
             }
         }