浏览代码

Fix isDownloading, cancel, success notification

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 1 年之前
父节点
当前提交
d166035dda

+ 19 - 0
app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadHelper.kt

@@ -45,6 +45,13 @@ class FileDownloadHelper {
         MainApp.getAppComponent().inject(this)
     }
 
+    fun isDownloading(user: User?, file: OCFile?): Boolean {
+        return user != null && file != null && backgroundJobManager.isStartFileDownloadJobScheduled(
+            user,
+            file
+        )
+    }
+
     fun saveFile(
         file: OCFile,
         currentDownload: DownloadFileOperation?,
@@ -103,6 +110,18 @@ class FileDownloadHelper {
         )
     }
 
+    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,

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

@@ -351,13 +351,18 @@ class FileDownloadWorker(
         private val boundListeners: MutableMap<Long, OnDatatransferProgressListener> = HashMap()
 
         fun cancelPendingOrCurrentDownloads() {
-            helper.backgroundJobManager.cancelFilesDownloadJob(currentUser.get())
+            currentDownload?.file?.let { file ->
+                helper.backgroundJobManager.cancelFilesDownloadJob(currentUser.get(), file)
+            }
         }
 
         fun cancelAllDownloadsForAccount(accountName: String?) {
             currentDownload?.user?.let {
                 if (it.nameEquals(accountName)) {
-                    helper.backgroundJobManager.cancelFilesDownloadJob(it)
+                    currentDownload?.file?.let { file ->
+                        helper.backgroundJobManager.cancelFilesDownloadJob(it, file)
+                    }
+
                     currentDownload?.cancel()
                 }
             }

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

@@ -145,7 +145,7 @@ interface BackgroundJobManager {
     fun getFileUploads(user: User): LiveData<List<JobInfo>>
     fun cancelFilesUploadJob(user: User)
 
-    fun cancelFilesDownloadJob(user: User)
+    fun cancelFilesDownloadJob(user: User, file: OCFile)
 
     fun isStartFileDownloadJobScheduled(user: User, file: OCFile): Boolean
 

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

@@ -577,8 +577,8 @@ internal class BackgroundJobManagerImpl(
         workManager.cancelJob(JOB_FILES_UPLOAD, user)
     }
 
-    override fun cancelFilesDownloadJob(user: User) {
-        workManager.cancelJob(JOB_FILES_DOWNLOAD, user)
+    override fun cancelFilesDownloadJob(user: User, file: OCFile) {
+        workManager.cancelAllWorkByTag(startFileDownloadJobTag(user, file))
     }
 
     override fun startPdfGenerateAndUploadWork(

+ 1 - 1
app/src/main/java/com/nextcloud/client/notifications/download/DownloadNotificationManager.kt

@@ -89,7 +89,7 @@ class DownloadNotificationManager(private val context: Context, private val view
         val notifyId = SecureRandom().nextInt()
 
         val contentText = if (result.isSuccess) {
-            context.getString(R.string.downloader_download_succeeded_ticker)
+            download.file.fileName + " " + context.getString(R.string.downloader_download_succeeded_ticker)
         } else {
             ErrorMessageAdapter.getErrorCauseMessage(
                 result,

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

@@ -31,6 +31,7 @@ import android.view.Menu;
 import com.nextcloud.android.files.FileLockingHelper;
 import com.nextcloud.client.account.User;
 import com.nextcloud.client.editimage.EditImageActivity;
+import com.nextcloud.client.files.downloader.FileDownloadHelper;
 import com.nextcloud.client.files.downloader.FileDownloadWorker;
 import com.nextcloud.utils.EditorUtils;
 import com.owncloud.android.R;
@@ -380,9 +381,9 @@ public class FileMenuFilter {
         if (componentsGetter != null && !files.isEmpty() && user != null) {
             OperationsServiceBinder opsBinder = componentsGetter.getOperationsServiceBinder();
             FileUploaderBinder uploaderBinder = componentsGetter.getFileUploaderBinder();
-            FileDownloadWorker.FileDownloaderBinder downloaderBinder = componentsGetter.getFileDownloaderBinder();
+            FileDownloadHelper fileDownloadHelper = new FileDownloadHelper();
             synchronizing = anyFileSynchronizing(opsBinder) ||      // comparing local and remote
-                            anyFileDownloading(downloaderBinder) ||
+                            anyFileDownloading(fileDownloadHelper) ||
                             anyFileUploading(uploaderBinder);
         }
         return synchronizing;
@@ -398,15 +399,14 @@ public class FileMenuFilter {
         return synchronizing;
     }
 
-    private boolean anyFileDownloading(FileDownloadWorker.FileDownloaderBinder downloaderBinder) {
-        boolean downloading = false;
-
-        if (downloaderBinder != null) {
-            for (Iterator<OCFile> iterator = files.iterator(); !downloading && iterator.hasNext(); ) {
-                downloading = downloaderBinder.isDownloading(user, iterator.next());
+    private boolean anyFileDownloading(FileDownloadHelper downloadHelper) {
+        for (OCFile file : files) {
+            if (downloadHelper.isDownloading(user, file)) {
+                return true;
             }
         }
-        return downloading;
+
+        return false;
     }
 
     private boolean anyFileUploading(FileUploaderBinder uploaderBinder) {

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

@@ -166,6 +166,7 @@ public abstract class FileActivity extends DrawerActivity
 
     private boolean mResumed;
 
+    protected FileDownloadHelper fileDownloadHelper = new FileDownloadHelper();
     protected FileDownloadWorker.FileDownloaderBinder mDownloaderBinder;
     protected FileUploaderBinder mUploaderBinder;
     private ServiceConnection mUploadServiceConnection;

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

@@ -1564,6 +1564,7 @@ public class FileDisplayActivity extends FileActivity
         return new ListServiceConnection();
     }
 
+    // FIXME ServiceConnection will not trigger anymore
     /**
      * Defines callbacks for service binding, passed to bindService()
      */
@@ -1885,8 +1886,8 @@ public class FileDisplayActivity extends FileActivity
 
     private void requestForDownload() {
         User user = getUser().orElseThrow(RuntimeException::new);
-        if (!mDownloaderBinder.isDownloading(user, mWaitingToPreview)) {
-            new FileDownloadHelper().downloadFile(user, mWaitingToPreview);
+        if (!fileDownloadHelper.isDownloading(user, mWaitingToPreview)) {
+            fileDownloadHelper.downloadFile(user, mWaitingToPreview);
         }
     }
 
@@ -1957,8 +1958,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 (!mDownloaderBinder.isDownloading(currentUser, file)) {
-            new FileDownloadHelper().downloadFile(currentUser, file, downloadBehaviour, DownloadType.DOWNLOAD, activityName, packageName, null);
+        if (!fileDownloadHelper.isDownloading(currentUser, file)) {
+            fileDownloadHelper.downloadFile(currentUser, file, downloadBehaviour, DownloadType.DOWNLOAD, activityName, packageName, null);
         }
     }
 

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

@@ -32,6 +32,7 @@ import androidx.core.content.res.ResourcesCompat
 import com.elyeproj.loaderviewlibrary.LoaderImageView
 import com.nextcloud.android.common.ui.theme.utils.ColorRole
 import com.nextcloud.client.account.User
+import com.nextcloud.client.files.downloader.FileDownloadHelper
 import com.nextcloud.client.preferences.AppPreferences
 import com.nextcloud.utils.extensions.createRoundedOutline
 import com.owncloud.android.R
@@ -340,11 +341,11 @@ class OCFileListDelegate(
 
     private fun showLocalFileIndicator(file: OCFile, gridViewHolder: ListGridImageViewHolder) {
         val operationsServiceBinder = transferServiceGetter.operationsServiceBinder
-        val fileDownloaderBinder = transferServiceGetter.fileDownloaderBinder
+        val fileDownloadHelper = FileDownloadHelper()
         val fileUploaderBinder = transferServiceGetter.fileUploaderBinder
         when {
             operationsServiceBinder?.isSynchronizing(user, file) == true ||
-                fileDownloaderBinder?.isDownloading(user, file) == true ||
+                fileDownloadHelper.isDownloading(user, file) ||
                 fileUploaderBinder?.isUploading(user, file) == true -> {
                 // synchronizing, downloading or uploading
                 gridViewHolder.localFileIndicator.setImageResource(R.drawable.ic_synchronizing)

+ 6 - 5
app/src/main/java/com/owncloud/android/ui/fragment/FileDetailFragment.java

@@ -44,6 +44,7 @@ import com.google.android.material.tabs.TabLayout;
 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.files.downloader.FileDownloadWorker;
 import com.nextcloud.client.jobs.BackgroundJobManager;
 import com.nextcloud.client.network.ClientFactory;
@@ -502,7 +503,7 @@ public class FileDetailFragment extends FileFragment implements OnClickListener,
      * TODO Remove parameter when the transferring state of files is kept in database.
      *
      * @param transferring Flag signaling if the file should be considered as downloading or uploading, although
-     *                     {@link com.nextcloud.client.files.downloader.FileDownloadWorker.FileDownloaderBinder#isDownloading(User, OCFile)}  and
+     *                     {@link FileDownloadHelper#isDownloading(User, OCFile)}  and
      *                     {@link FileUploaderBinder#isUploading(User, OCFile)} return false.
      * @param refresh      If 'true', try to refresh the whole file from the database
      */
@@ -534,10 +535,10 @@ public class FileDetailFragment extends FileFragment implements OnClickListener,
             setFavoriteIconStatus(file.isFavorite());
 
             // configure UI for depending upon local state of the file
-            FileDownloadWorker.FileDownloaderBinder downloaderBinder = containerActivity.getFileDownloaderBinder();
+            FileDownloadHelper fileDownloadHelper = new FileDownloadHelper();
             FileUploaderBinder uploaderBinder = containerActivity.getFileUploaderBinder();
             if (transferring
-                || (downloaderBinder != null && downloaderBinder.isDownloading(user, file))
+                || (fileDownloadHelper.isDownloading(user, file))
                 || (uploaderBinder != null && uploaderBinder.isUploading(user, file))) {
                 setButtonsForTransferring();
 
@@ -659,9 +660,9 @@ public class FileDetailFragment extends FileFragment implements OnClickListener,
             // show the progress bar for the transfer
             binding.progressBlock.setVisibility(View.VISIBLE);
             binding.progressText.setVisibility(View.VISIBLE);
-            FileDownloadWorker.FileDownloaderBinder downloaderBinder = containerActivity.getFileDownloaderBinder();
+            FileDownloadHelper fileDownloadHelper = new FileDownloadHelper();
             FileUploaderBinder uploaderBinder = containerActivity.getFileUploaderBinder();
-            if (downloaderBinder != null && downloaderBinder.isDownloading(user, getFile())) {
+            if (fileDownloadHelper.isDownloading(user, getFile())) {
                 binding.progressText.setText(R.string.downloader_download_in_progress_ticker);
             } else {
                 if (uploaderBinder != null && uploaderBinder.isUploading(user, getFile())) {

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

@@ -47,6 +47,7 @@ import android.webkit.MimeTypeMap;
 
 import com.nextcloud.client.account.CurrentAccountProvider;
 import com.nextcloud.client.account.User;
+import com.nextcloud.client.files.downloader.FileDownloadHelper;
 import com.nextcloud.client.files.downloader.FileDownloadWorker;
 import com.nextcloud.client.jobs.BackgroundJobManager;
 import com.nextcloud.client.network.ConnectivityService;

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

@@ -410,10 +410,8 @@ 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(user, file)) {
-            new FileDownloadHelper().downloadFile(user, file, downloadBehaviour);
+        if (!fileDownloadHelper.isDownloading(user, file)) {
+            fileDownloadHelper.downloadFile(user, file, downloadBehaviour);
         }
     }
 

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

@@ -65,6 +65,8 @@ 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.files.downloader.FileDownloadWorker
 import com.nextcloud.client.jobs.BackgroundJobManager
 import com.nextcloud.client.media.ExoplayerListener
 import com.nextcloud.client.media.NextcloudExoPlayer.createNextcloudExoplayer
@@ -80,8 +82,6 @@ import com.owncloud.android.databinding.ActivityPreviewMediaBinding
 import com.owncloud.android.datamodel.OCFile
 import com.owncloud.android.datamodel.ThumbnailsCacheManager
 import com.owncloud.android.files.StreamMediaFileOperation
-import com.owncloud.android.files.services.FileDownloader
-import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder
 import com.owncloud.android.lib.common.OwnCloudClient
 import com.owncloud.android.lib.common.operations.OnRemoteOperationListener
 import com.owncloud.android.lib.common.operations.RemoteOperation
@@ -576,14 +576,14 @@ class PreviewMediaActivity :
     private inner class PreviewMediaServiceConnection : ServiceConnection {
         override fun onServiceConnected(componentName: ComponentName?, service: IBinder?) {
             componentName?.let {
-                if (it == ComponentName(this@PreviewMediaActivity, FileDownloader::class.java)) {
-                    mDownloaderBinder = service as FileDownloaderBinder
+                if (it == ComponentName(this@PreviewMediaActivity, FileDownloadWorker::class.java)) {
+                    mDownloaderBinder = service as FileDownloadWorker.FileDownloaderBinder
                 }
             }
         }
 
         override fun onServiceDisconnected(componentName: ComponentName?) {
-            if (componentName == ComponentName(this@PreviewMediaActivity, FileDownloader::class.java)) {
+            if (componentName == ComponentName(this@PreviewMediaActivity, FileDownloadWorker::class.java)) {
                 Log_OC.d(PreviewImageActivity.TAG, "Download service suddenly disconnected")
                 mDownloaderBinder = null
             }
@@ -600,21 +600,21 @@ class PreviewMediaActivity :
         packageName: String? = null,
         activityName: String? = null
     ) {
-        if (fileDownloaderBinder.isDownloading(user, file)) {
+        if (fileDownloadHelper.isDownloading(user, file)) {
             return
         }
 
-        val intent = Intent(this, FileDownloader::class.java).apply {
-            putExtra(FileDownloader.EXTRA_USER, user)
-            putExtra(FileDownloader.EXTRA_FILE, file)
-            downloadBehavior?.let { behavior ->
-                putExtra(OCFileListFragment.DOWNLOAD_BEHAVIOUR, behavior)
+        user?.let { user ->
+            file?.let { file ->
+                fileDownloadHelper.downloadFile(
+                    user,
+                    file,
+                    downloadBehavior ?: "",
+                    packageName ?: "",
+                    activityName ?: ""
+                )
             }
-            putExtra(SendShareDialog.PACKAGE_NAME, packageName)
-            putExtra(SendShareDialog.ACTIVITY_NAME, activityName)
         }
-
-        startService(intent)
     }
 
     private fun seeDetails() {

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

@@ -131,6 +131,7 @@ 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;
 
@@ -478,8 +479,8 @@ public class PreviewMediaFragment extends FileFragment implements OnTouchListene
                                                                     getView(),
                                                                     backgroundJobManager);
         } else if (itemId == R.id.action_download_file) {
-            if (!containerActivity.getFileDownloaderBinder().isDownloading(user, getFile())) {
-                new FileDownloadHelper().downloadFile(user, getFile());
+            if (!downloadHelper.isDownloading(user, getFile())) {
+                downloadHelper.downloadFile(user, getFile());
             }
         }
     }