Эх сурвалжийг харах

Remove FileDownloader Service

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 1 жил өмнө
parent
commit
c419604ae5
24 өөрчлөгдсөн 146 нэмэгдсэн , 825 устгасан
  1. 2 2
      app/src/androidTest/java/com/owncloud/android/files/FileMenuFilterIT.kt
  2. 2 2
      app/src/debug/java/com/nextcloud/test/TestActivity.kt
  3. 0 4
      app/src/main/java/com/nextcloud/client/di/ComponentsModule.java
  4. 2 2
      app/src/main/java/com/nextcloud/client/files/downloader/FilesDownloadHelper.kt
  5. 25 8
      app/src/main/java/com/nextcloud/client/files/downloader/FilesDownloadWorker.kt
  6. 2 2
      app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt
  7. 2 3
      app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt
  8. 10 10
      app/src/main/java/com/nextcloud/client/jobs/FilesExportWork.kt
  9. 3 3
      app/src/main/java/com/owncloud/android/files/FileMenuFilter.java
  10. 0 686
      app/src/main/java/com/owncloud/android/files/services/FileDownloader.java
  11. 11 6
      app/src/main/java/com/owncloud/android/operations/SynchronizeFileOperation.java
  12. 12 9
      app/src/main/java/com/owncloud/android/operations/SynchronizeFolderOperation.java
  13. 1 2
      app/src/main/java/com/owncloud/android/providers/DocumentsStorageProvider.java
  14. 8 8
      app/src/main/java/com/owncloud/android/services/SyncFolderHandler.java
  15. 2 2
      app/src/main/java/com/owncloud/android/ui/activity/ComponentsGetter.java
  16. 12 7
      app/src/main/java/com/owncloud/android/ui/activity/ConflictsResolveActivity.kt
  17. 9 7
      app/src/main/java/com/owncloud/android/ui/activity/FileActivity.java
  18. 16 25
      app/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java
  19. 6 6
      app/src/main/java/com/owncloud/android/ui/activity/ManageAccountsActivity.java
  20. 4 4
      app/src/main/java/com/owncloud/android/ui/fragment/FileDetailFragment.java
  21. 2 3
      app/src/main/java/com/owncloud/android/ui/fragment/FileFragment.java
  22. 2 2
      app/src/main/java/com/owncloud/android/ui/helpers/FileOperationsHelper.java
  23. 11 17
      app/src/main/java/com/owncloud/android/ui/preview/PreviewImageActivity.java
  24. 2 5
      app/src/main/java/com/owncloud/android/ui/preview/PreviewMediaFragment.java

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

@@ -23,6 +23,7 @@ package com.owncloud.android.files
 import androidx.test.core.app.launchActivity
 import androidx.test.core.app.launchActivity
 import androidx.test.ext.junit.runners.AndroidJUnit4
 import androidx.test.ext.junit.runners.AndroidJUnit4
 import com.nextcloud.client.account.User
 import com.nextcloud.client.account.User
+import com.nextcloud.client.files.downloader.FilesDownloadWorker
 import com.nextcloud.test.TestActivity
 import com.nextcloud.test.TestActivity
 import com.nextcloud.utils.EditorUtils
 import com.nextcloud.utils.EditorUtils
 import com.owncloud.android.AbstractIT
 import com.owncloud.android.AbstractIT
@@ -30,7 +31,6 @@ import com.owncloud.android.R
 import com.owncloud.android.datamodel.ArbitraryDataProvider
 import com.owncloud.android.datamodel.ArbitraryDataProvider
 import com.owncloud.android.datamodel.FileDataStorageManager
 import com.owncloud.android.datamodel.FileDataStorageManager
 import com.owncloud.android.datamodel.OCFile
 import com.owncloud.android.datamodel.OCFile
-import com.owncloud.android.files.services.FileDownloader
 import com.owncloud.android.files.services.FileUploader
 import com.owncloud.android.files.services.FileUploader
 import com.owncloud.android.lib.resources.files.model.FileLockType
 import com.owncloud.android.lib.resources.files.model.FileLockType
 import com.owncloud.android.lib.resources.status.CapabilityBooleanType
 import com.owncloud.android.lib.resources.status.CapabilityBooleanType
@@ -62,7 +62,7 @@ class FileMenuFilterIT : AbstractIT() {
     private lateinit var mockFileUploaderBinder: FileUploader.FileUploaderBinder
     private lateinit var mockFileUploaderBinder: FileUploader.FileUploaderBinder
 
 
     @MockK
     @MockK
-    private lateinit var mockFileDownloaderBinder: FileDownloader.FileDownloaderBinder
+    private lateinit var mockFileDownloaderBinder: FilesDownloadWorker.FileDownloaderBinder
 
 
     @MockK
     @MockK
     private lateinit var mockOperationsServiceBinder: OperationsService.OperationsServiceBinder
     private lateinit var mockOperationsServiceBinder: OperationsService.OperationsServiceBinder

+ 2 - 2
app/src/debug/java/com/nextcloud/test/TestActivity.kt

@@ -25,6 +25,7 @@ import android.os.Bundle
 import android.view.View
 import android.view.View
 import androidx.fragment.app.Fragment
 import androidx.fragment.app.Fragment
 import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
 import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
+import com.nextcloud.client.files.downloader.FilesDownloadWorker
 import com.nextcloud.client.network.Connectivity
 import com.nextcloud.client.network.Connectivity
 import com.nextcloud.client.network.ConnectivityService
 import com.nextcloud.client.network.ConnectivityService
 import com.nextcloud.utils.EditorUtils
 import com.nextcloud.utils.EditorUtils
@@ -33,7 +34,6 @@ import com.owncloud.android.databinding.TestLayoutBinding
 import com.owncloud.android.datamodel.ArbitraryDataProviderImpl
 import com.owncloud.android.datamodel.ArbitraryDataProviderImpl
 import com.owncloud.android.datamodel.FileDataStorageManager
 import com.owncloud.android.datamodel.FileDataStorageManager
 import com.owncloud.android.datamodel.OCFile
 import com.owncloud.android.datamodel.OCFile
-import com.owncloud.android.files.services.FileDownloader
 import com.owncloud.android.files.services.FileUploader
 import com.owncloud.android.files.services.FileUploader
 import com.owncloud.android.lib.resources.status.OCCapability
 import com.owncloud.android.lib.resources.status.OCCapability
 import com.owncloud.android.lib.resources.status.OwnCloudVersion
 import com.owncloud.android.lib.resources.status.OwnCloudVersion
@@ -130,7 +130,7 @@ class TestActivity :
         return null
         return null
     }
     }
 
 
-    override fun getFileDownloaderBinder(): FileDownloader.FileDownloaderBinder? {
+    override fun getFileDownloaderBinder(): FilesDownloadWorker.FileDownloaderBinder? {
         return null
         return null
     }
     }
 
 

+ 0 - 4
app/src/main/java/com/nextcloud/client/di/ComponentsModule.java

@@ -46,7 +46,6 @@ import com.owncloud.android.MainApp;
 import com.owncloud.android.authentication.AuthenticatorActivity;
 import com.owncloud.android.authentication.AuthenticatorActivity;
 import com.owncloud.android.authentication.DeepLinkLoginActivity;
 import com.owncloud.android.authentication.DeepLinkLoginActivity;
 import com.owncloud.android.files.BootupBroadcastReceiver;
 import com.owncloud.android.files.BootupBroadcastReceiver;
-import com.owncloud.android.files.services.FileDownloader;
 import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.providers.DiskLruImageCacheFileProvider;
 import com.owncloud.android.providers.DiskLruImageCacheFileProvider;
 import com.owncloud.android.providers.DocumentsStorageProvider;
 import com.owncloud.android.providers.DocumentsStorageProvider;
@@ -326,9 +325,6 @@ abstract class ComponentsModule {
     @ContributesAndroidInjector
     @ContributesAndroidInjector
     abstract FileUploader fileUploader();
     abstract FileUploader fileUploader();
 
 
-    @ContributesAndroidInjector
-    abstract FileDownloader fileDownloader();
-
     @ContributesAndroidInjector
     @ContributesAndroidInjector
     abstract BootupBroadcastReceiver bootupBroadcastReceiver();
     abstract BootupBroadcastReceiver bootupBroadcastReceiver();
 
 

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

@@ -45,10 +45,10 @@ class FilesDownloadHelper {
         user: User,
         user: User,
         ocFile: OCFile,
         ocFile: OCFile,
         behaviour: String,
         behaviour: String,
-        downloadType: DownloadType,
+        downloadType: DownloadType?,
         activityName: String,
         activityName: String,
         packageName: String,
         packageName: String,
-        conflictUploadId: Long
+        conflictUploadId: Long?
     ) {
     ) {
         backgroundJobManager.startFilesDownloadJob(
         backgroundJobManager.startFilesDownloadJob(
             user,
             user,

+ 25 - 8
app/src/main/java/com/nextcloud/client/files/downloader/FilesDownloadWorker.kt

@@ -99,6 +99,14 @@ class FilesDownloadWorker(
         const val EXTRA_REMOTE_PATH = "REMOTE_PATH"
         const val EXTRA_REMOTE_PATH = "REMOTE_PATH"
         const val EXTRA_LINKED_TO_PATH = "LINKED_TO"
         const val EXTRA_LINKED_TO_PATH = "LINKED_TO"
         const val ACCOUNT_NAME = "ACCOUNT_NAME"
         const val ACCOUNT_NAME = "ACCOUNT_NAME"
+
+        fun getDownloadAddedMessage(): String {
+            return FilesDownloadWorker::class.java.name + "DOWNLOAD_ADDED"
+        }
+
+        fun getDownloadFinishMessage(): String {
+            return FilesDownloadWorker::class.java.name + "DOWNLOAD_FINISH"
+        }
     }
     }
 
 
     private var notification: Notification? = null
     private var notification: Notification? = null
@@ -436,14 +444,6 @@ class FilesDownloadWorker(
         pendingDownloads.remove(accountName)
         pendingDownloads.remove(accountName)
     }
     }
 
 
-    private fun getDownloadAddedMessage(): String {
-        return FilesDownloadWorker::class.java.name + "DOWNLOAD_ADDED"
-    }
-
-    private fun getDownloadFinishMessage(): String {
-        return FilesDownloadWorker::class.java.name + "DOWNLOAD_FINISH"
-    }
-
     private fun sendBroadcastNewDownload(
     private fun sendBroadcastNewDownload(
         download: DownloadFileOperation,
         download: DownloadFileOperation,
         linkedToRemotePath: String
         linkedToRemotePath: String
@@ -533,6 +533,23 @@ class FilesDownloadWorker(
             return user != null && file != null && pendingDownloads.contains(user.accountName, file.remotePath)
             return user != null && file != null && pendingDownloads.contains(user.accountName, file.remotePath)
         }
         }
 
 
+        fun addDatatransferProgressListener(listener: OnDatatransferProgressListener?, file: OCFile?) {
+            if (file == null || listener == null) {
+                return
+            }
+            mBoundListeners[file.fileId] = listener
+        }
+
+        fun removeDatatransferProgressListener(listener: OnDatatransferProgressListener?, file: OCFile?) {
+            if (file == null || listener == null) {
+                return
+            }
+            val fileId = file.fileId
+            if (mBoundListeners[fileId] === listener) {
+                mBoundListeners.remove(fileId)
+            }
+        }
+
         override fun onTransferProgress(
         override fun onTransferProgress(
             progressRate: Long, totalTransferredSoFar: Long,
             progressRate: Long, totalTransferredSoFar: Long,
             totalToTransfer: Long, fileName: String
             totalToTransfer: Long, fileName: String

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

@@ -149,10 +149,10 @@ interface BackgroundJobManager {
         user: User,
         user: User,
         ocFile: OCFile,
         ocFile: OCFile,
         behaviour: String,
         behaviour: String,
-        downloadType: DownloadType,
+        downloadType: DownloadType?,
         activityName: String,
         activityName: String,
         packageName: String,
         packageName: String,
-        conflictUploadId: Long
+        conflictUploadId: Long?
     )
     )
 
 
     fun startPdfGenerateAndUploadWork(user: User, uploadFolder: String, imagePaths: List<String>, pdfPath: String)
     fun startPdfGenerateAndUploadWork(user: User, uploadFolder: String, imagePaths: List<String>, pdfPath: String)

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

@@ -42,7 +42,6 @@ import com.nextcloud.client.files.downloader.FilesDownloadWorker
 import com.nextcloud.client.preferences.AppPreferences
 import com.nextcloud.client.preferences.AppPreferences
 import com.owncloud.android.datamodel.OCFile
 import com.owncloud.android.datamodel.OCFile
 import com.owncloud.android.operations.DownloadType
 import com.owncloud.android.operations.DownloadType
-import java.io.File
 import java.util.Date
 import java.util.Date
 import java.util.UUID
 import java.util.UUID
 import java.util.concurrent.TimeUnit
 import java.util.concurrent.TimeUnit
@@ -513,10 +512,10 @@ internal class BackgroundJobManagerImpl(
         user: User,
         user: User,
         ocFile: OCFile,
         ocFile: OCFile,
         behaviour: String,
         behaviour: String,
-        downloadType: DownloadType,
+        downloadType: DownloadType?,
         activityName: String,
         activityName: String,
         packageName: String,
         packageName: String,
-        conflictUploadId: Long
+        conflictUploadId: Long?
     ) {
     ) {
         val data = workDataOf(
         val data = workDataOf(
             FilesDownloadWorker.USER to user,
             FilesDownloadWorker.USER to user,

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

@@ -33,13 +33,12 @@ import androidx.core.app.NotificationCompat
 import androidx.work.Worker
 import androidx.work.Worker
 import androidx.work.WorkerParameters
 import androidx.work.WorkerParameters
 import com.nextcloud.client.account.User
 import com.nextcloud.client.account.User
+import com.nextcloud.client.files.downloader.FilesDownloadHelper
 import com.owncloud.android.R
 import com.owncloud.android.R
 import com.owncloud.android.datamodel.FileDataStorageManager
 import com.owncloud.android.datamodel.FileDataStorageManager
 import com.owncloud.android.datamodel.OCFile
 import com.owncloud.android.datamodel.OCFile
-import com.owncloud.android.files.services.FileDownloader
 import com.owncloud.android.lib.common.utils.Log_OC
 import com.owncloud.android.lib.common.utils.Log_OC
 import com.owncloud.android.operations.DownloadType
 import com.owncloud.android.operations.DownloadType
-import com.owncloud.android.ui.dialog.SendShareDialog
 import com.owncloud.android.ui.notifications.NotificationUtils
 import com.owncloud.android.ui.notifications.NotificationUtils
 import com.owncloud.android.utils.FileExportUtils
 import com.owncloud.android.utils.FileExportUtils
 import com.owncloud.android.utils.FileStorageUtils
 import com.owncloud.android.utils.FileStorageUtils
@@ -112,14 +111,15 @@ class FilesExportWork(
     }
     }
 
 
     private fun downloadFile(ocFile: OCFile) {
     private fun downloadFile(ocFile: OCFile) {
-        val i = Intent(appContext, FileDownloader::class.java)
-        i.putExtra(FileDownloader.EXTRA_USER, user)
-        i.putExtra(FileDownloader.EXTRA_FILE, ocFile)
-        i.putExtra(SendShareDialog.PACKAGE_NAME, "")
-        i.putExtra(SendShareDialog.ACTIVITY_NAME, "")
-        i.putExtra(FileDownloader.DOWNLOAD_TYPE, DownloadType.EXPORT)
-
-        FileDownloader(i)
+        FilesDownloadHelper().downloadFile(
+            user,
+            ocFile,
+            behaviour = "",
+            packageName = "",
+            activityName = "",
+            conflictUploadId = 0L,
+            downloadType = DownloadType.EXPORT
+        )
     }
     }
 
 
     private fun showErrorNotification(successfulExports: Int) {
     private fun showErrorNotification(successfulExports: Int) {

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

@@ -31,11 +31,11 @@ import android.view.Menu;
 import com.nextcloud.android.files.FileLockingHelper;
 import com.nextcloud.android.files.FileLockingHelper;
 import com.nextcloud.client.account.User;
 import com.nextcloud.client.account.User;
 import com.nextcloud.client.editimage.EditImageActivity;
 import com.nextcloud.client.editimage.EditImageActivity;
+import com.nextcloud.client.files.downloader.FilesDownloadWorker;
 import com.nextcloud.utils.EditorUtils;
 import com.nextcloud.utils.EditorUtils;
 import com.owncloud.android.R;
 import com.owncloud.android.R;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.datamodel.OCFile;
-import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
 import com.owncloud.android.lib.resources.status.OCCapability;
 import com.owncloud.android.lib.resources.status.OCCapability;
 import com.owncloud.android.services.OperationsService.OperationsServiceBinder;
 import com.owncloud.android.services.OperationsService.OperationsServiceBinder;
@@ -380,7 +380,7 @@ public class FileMenuFilter {
         if (componentsGetter != null && !files.isEmpty() && user != null) {
         if (componentsGetter != null && !files.isEmpty() && user != null) {
             OperationsServiceBinder opsBinder = componentsGetter.getOperationsServiceBinder();
             OperationsServiceBinder opsBinder = componentsGetter.getOperationsServiceBinder();
             FileUploaderBinder uploaderBinder = componentsGetter.getFileUploaderBinder();
             FileUploaderBinder uploaderBinder = componentsGetter.getFileUploaderBinder();
-            FileDownloaderBinder downloaderBinder = componentsGetter.getFileDownloaderBinder();
+            FilesDownloadWorker.FileDownloaderBinder downloaderBinder = componentsGetter.getFileDownloaderBinder();
             synchronizing = anyFileSynchronizing(opsBinder) ||      // comparing local and remote
             synchronizing = anyFileSynchronizing(opsBinder) ||      // comparing local and remote
                             anyFileDownloading(downloaderBinder) ||
                             anyFileDownloading(downloaderBinder) ||
                             anyFileUploading(uploaderBinder);
                             anyFileUploading(uploaderBinder);
@@ -398,7 +398,7 @@ public class FileMenuFilter {
         return synchronizing;
         return synchronizing;
     }
     }
 
 
-    private boolean anyFileDownloading(FileDownloaderBinder downloaderBinder) {
+    private boolean anyFileDownloading(FilesDownloadWorker.FileDownloaderBinder downloaderBinder) {
         boolean downloading = false;
         boolean downloading = false;
         if (downloaderBinder != null) {
         if (downloaderBinder != null) {
             for (Iterator<OCFile> iterator = files.iterator(); !downloading && iterator.hasNext(); ) {
             for (Iterator<OCFile> iterator = files.iterator(); !downloading && iterator.hasNext(); ) {

+ 0 - 686
app/src/main/java/com/owncloud/android/files/services/FileDownloader.java

@@ -1,686 +0,0 @@
-/*
- *   ownCloud Android client application
- *
- *   Copyright (C) 2012 Bartek Przybylski
- *   Copyright (C) 2012-2016 ownCloud Inc.
- *
- *   This program is free software: you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License version 2,
- *   as published by the Free Software Foundation.
- *
- *   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 General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-package com.owncloud.android.files.services;
-
-import android.accounts.Account;
-import android.accounts.AccountManager;
-import android.accounts.OnAccountsUpdateListener;
-import android.app.Notification;
-import android.app.NotificationManager;
-import android.app.PendingIntent;
-import android.content.Context;
-import android.content.Intent;
-import android.graphics.BitmapFactory;
-import android.os.Binder;
-import android.os.Handler;
-import android.os.HandlerThread;
-import android.os.IBinder;
-import android.os.Looper;
-import android.os.Message;
-import android.os.Process;
-import android.util.Pair;
-
-import com.nextcloud.client.account.User;
-import com.nextcloud.client.account.UserAccountManager;
-import com.nextcloud.client.files.downloader.DownloadTask;
-import com.nextcloud.client.files.downloader.FilesDownloadWorker;
-import com.nextcloud.java.util.Optional;
-import com.nextcloud.utils.extensions.IntentExtensionsKt;
-import com.owncloud.android.MainApp;
-import com.owncloud.android.R;
-import com.owncloud.android.authentication.AuthenticatorActivity;
-import com.owncloud.android.datamodel.FileDataStorageManager;
-import com.owncloud.android.datamodel.OCFile;
-import com.owncloud.android.datamodel.UploadsStorageManager;
-import com.owncloud.android.lib.common.OwnCloudAccount;
-import com.owncloud.android.lib.common.OwnCloudClient;
-import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
-import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
-import com.owncloud.android.lib.common.operations.RemoteOperationResult;
-import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
-import com.owncloud.android.lib.common.utils.Log_OC;
-import com.owncloud.android.lib.resources.files.FileUtils;
-import com.owncloud.android.operations.DownloadFileOperation;
-import com.owncloud.android.operations.DownloadType;
-import com.owncloud.android.providers.DocumentsStorageProvider;
-import com.owncloud.android.ui.activity.ConflictsResolveActivity;
-import com.owncloud.android.ui.activity.FileActivity;
-import com.owncloud.android.ui.activity.FileDisplayActivity;
-import com.owncloud.android.ui.dialog.SendShareDialog;
-import com.owncloud.android.ui.fragment.OCFileListFragment;
-import com.owncloud.android.ui.notifications.NotificationUtils;
-import com.owncloud.android.ui.preview.PreviewImageActivity;
-import com.owncloud.android.ui.preview.PreviewImageFragment;
-import com.owncloud.android.utils.ErrorMessageAdapter;
-import com.owncloud.android.utils.MimeTypeUtil;
-import com.owncloud.android.utils.theme.ViewThemeUtils;
-
-import java.io.File;
-import java.security.SecureRandom;
-import java.util.AbstractList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Vector;
-
-import javax.inject.Inject;
-
-import androidx.core.app.NotificationCompat;
-import androidx.localbroadcastmanager.content.LocalBroadcastManager;
-import androidx.work.OneTimeWorkRequest;
-import androidx.work.WorkManager;
-import androidx.work.WorkRequest;
-import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
-
-import static android.content.Context.NOTIFICATION_SERVICE;
-
-public class FileDownloader implements OnDatatransferProgressListener, OnAccountsUpdateListener {
-
-    private final Context context = MainApp.getAppContext();
-    private final Intent intent;
-
-    private static final String DOWNLOAD_ADDED_MESSAGE = "DOWNLOAD_ADDED";
-    private static final String DOWNLOAD_FINISH_MESSAGE = "DOWNLOAD_FINISH";
-
-    public static final String EXTRA_USER = "USER";
-    public static final String EXTRA_FILE = "FILE";
-    public static final String EXTRA_DOWNLOAD_RESULT = "RESULT";
-    public static final String EXTRA_REMOTE_PATH = "REMOTE_PATH";
-    public static final String EXTRA_LINKED_TO_PATH = "LINKED_TO";
-    public static final String ACCOUNT_NAME = "ACCOUNT_NAME";
-    public static final String DOWNLOAD_TYPE = "DOWNLOAD_TYPE";
-
-    private static final int FOREGROUND_SERVICE_ID = 412;
-
-    private static final String TAG = FileDownloader.class.getSimpleName();
-
-    private Looper mServiceLooper;
-    private ServiceHandler mServiceHandler;
-    private IBinder mBinder;
-    private OwnCloudClient mDownloadClient;
-    private Optional<User> currentUser = Optional.empty();
-    private FileDataStorageManager mStorageManager;
-
-    private IndexedForest<DownloadFileOperation> mPendingDownloads = new IndexedForest<>();
-
-    private DownloadFileOperation mCurrentDownload;
-
-    private NotificationManager mNotificationManager;
-    private NotificationCompat.Builder mNotificationBuilder;
-    private int mLastPercent;
-
-    private Notification mNotification;
-
-    private long conflictUploadId;
-
-    public boolean mStartedDownload = false;
-
-    @Inject UserAccountManager accountManager;
-    @Inject UploadsStorageManager uploadsStorageManager;
-    @Inject LocalBroadcastManager localBroadcastManager;
-    @Inject ViewThemeUtils viewThemeUtils;
-
-    public static String getDownloadAddedMessage() {
-        return FileDownloader.class.getName() + DOWNLOAD_ADDED_MESSAGE;
-    }
-
-    public static String getDownloadFinishMessage() {
-        return FileDownloader.class.getName() + DOWNLOAD_FINISH_MESSAGE;
-    }
-
-    public FileDownloader(Intent intent) {
-        this.intent = intent;
-        Log_OC.d(TAG, "Creating service");
-        mNotificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
-        HandlerThread thread = new HandlerThread("FileDownloaderThread", Process.THREAD_PRIORITY_BACKGROUND);
-        thread.start();
-        mServiceLooper = thread.getLooper();
-        mServiceHandler = new ServiceHandler(mServiceLooper, this);
-        mBinder = new FileDownloaderBinder();
-
-        NotificationCompat.Builder builder = NotificationUtils.newNotificationBuilder(context, viewThemeUtils).setContentTitle(
-                context.getResources().getString(R.string.app_name))
-            .setContentText(context.getResources().getString(R.string.foreground_service_download))
-            .setSmallIcon(R.drawable.notification_icon)
-            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.notification_icon));
-
-
-        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
-            builder.setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_DOWNLOAD);
-        }
-
-        mNotification = builder.build();
-
-        // add AccountsUpdatedListener
-        AccountManager am = AccountManager.get(context);
-        am.addOnAccountsUpdatedListener(this, null, false);
-    }
-
-    @Override
-    protected void finalize() throws Throwable {
-        Log_OC.v(TAG, "Destroying service");
-        mBinder = null;
-        mServiceHandler = null;
-        mServiceLooper.quit();
-        mServiceLooper = null;
-        mNotificationManager = null;
-
-        // remove AccountsUpdatedListener
-        AccountManager am = AccountManager.get(context);
-        am.removeOnAccountsUpdatedListener(this);
-        super.finalize();
-    }
-
-    public void download() {
-        final User user = IntentExtensionsKt.getParcelableArgument(intent, EXTRA_USER, User.class);
-        final OCFile file = IntentExtensionsKt.getParcelableArgument(intent, EXTRA_FILE, OCFile.class);
-        final String behaviour = intent.getStringExtra(OCFileListFragment.DOWNLOAD_BEHAVIOUR);
-
-        DownloadType downloadType = DownloadType.DOWNLOAD;
-        if (intent.hasExtra(DOWNLOAD_TYPE)) {
-            downloadType = IntentExtensionsKt.getSerializableArgument(intent, DOWNLOAD_TYPE, DownloadType.class);
-        }
-        String activityName = intent.getStringExtra(SendShareDialog.ACTIVITY_NAME);
-        String packageName = intent.getStringExtra(SendShareDialog.PACKAGE_NAME);
-        conflictUploadId = intent.getLongExtra(ConflictsResolveActivity.EXTRA_CONFLICT_UPLOAD_ID, -1);
-        AbstractList<String> requestedDownloads = new Vector<String>();
-        try {
-            DownloadFileOperation newDownload = new DownloadFileOperation(user,
-                                                                          file,
-                                                                          behaviour,
-                                                                          activityName,
-                                                                          packageName,
-                                                                          context,
-                                                                          downloadType);
-            newDownload.addDatatransferProgressListener(this);
-            newDownload.addDatatransferProgressListener((FileDownloaderBinder) mBinder);
-            Pair<String, String> putResult = mPendingDownloads.putIfAbsent(user.getAccountName(),
-                                                                           file.getRemotePath(),
-                                                                           newDownload);
-            if (putResult != null) {
-                String downloadKey = putResult.first;
-                requestedDownloads.add(downloadKey);
-                sendBroadcastNewDownload(newDownload, putResult.second);
-            }   // else, file already in the queue of downloads; don't repeat the request
-
-        } catch (IllegalArgumentException e) {
-            Log_OC.e(TAG, "Not enough information provided in intent: " + e.getMessage());
-        }
-
-        if (requestedDownloads.size() > 0) {
-            Message msg = mServiceHandler.obtainMessage();
-            // msg.arg1 = startId;
-            msg.obj = requestedDownloads;
-            mServiceHandler.sendMessage(msg);
-        }
-    }
-
-    @Override
-    public void onAccountsUpdated(Account[] accounts) {
-         //review the current download and cancel it if its account doesn't exist
-        if (mCurrentDownload != null && !accountManager.exists(mCurrentDownload.getUser().toPlatformAccount())) {
-            mCurrentDownload.cancel();
-        }
-        // The rest of downloads are cancelled when they try to start
-    }
-
-    /**
-     * Binder to let client components to perform operations on the queue of downloads.
-     * <p/>
-     * It provides by itself the available operations.
-     */
-    public class FileDownloaderBinder2 extends Binder implements OnDatatransferProgressListener {
-
-        /**
-         * Map of listeners that will be reported about progress of downloads from a
-         * {@link FileDownloaderBinder}
-         * instance.
-         */
-        private Map<Long, OnDatatransferProgressListener> mBoundListeners =
-                new HashMap<Long, OnDatatransferProgressListener>();
-
-
-        /**
-         * Cancels a pending or current download of a remote file.
-         *
-         * @param account ownCloud account where the remote file is stored.
-         * @param file    A file in the queue of pending downloads
-         */
-        public void cancel(Account account, OCFile file) {
-            Pair<DownloadFileOperation, String> removeResult =
-                mPendingDownloads.remove(account.name, file.getRemotePath());
-            DownloadFileOperation download = removeResult.first;
-            if (download != null) {
-                download.cancel();
-            } else {
-                if (mCurrentDownload != null && currentUser.isPresent() &&
-                    mCurrentDownload.getRemotePath().startsWith(file.getRemotePath()) &&
-                        account.name.equals(currentUser.get().getAccountName())) {
-                    mCurrentDownload.cancel();
-                }
-            }
-        }
-
-        /**
-         * Cancels all the downloads for an account
-         */
-        public void cancel(String accountName) {
-            if (mCurrentDownload != null && mCurrentDownload.getUser().nameEquals(accountName)) {
-                mCurrentDownload.cancel();
-            }
-            // Cancel pending downloads
-            cancelPendingDownloads(accountName);
-        }
-
-        /**
-         * Returns True when the file described by 'file' in the ownCloud account 'account'
-         * is downloading or waiting to download.
-         *
-         * If 'file' is a directory, returns 'true' if any of its descendant files is downloading or
-         * waiting to download.
-         *
-         * @param user    user where the remote file is stored.
-         * @param file    A file that could be in the queue of downloads.
-         */
-        public boolean isDownloading(User user, OCFile file) {
-            return user != null && file != null && mPendingDownloads.contains(user.getAccountName(), file.getRemotePath());
-        }
-
-
-        /**
-         * Adds a listener interested in the progress of the download for a concrete file.
-         *
-         * @param listener Object to notify about progress of transfer.
-         * @param file     {@link OCFile} of interest for listener.
-         */
-        public void addDatatransferProgressListener(OnDatatransferProgressListener listener, OCFile file) {
-            if (file == null || listener == null) {
-                return;
-            }
-            mBoundListeners.put(file.getFileId(), listener);
-        }
-
-
-        /**
-         * Removes a listener interested in the progress of the download for a concrete file.
-         *
-         * @param listener      Object to notify about progress of transfer.
-         * @param file          {@link OCFile} of interest for listener.
-         */
-        public void removeDatatransferProgressListener(OnDatatransferProgressListener listener, OCFile file) {
-            if (file == null || listener == null) {
-                return;
-            }
-            Long fileId = file.getFileId();
-            if (mBoundListeners.get(fileId) == listener) {
-                mBoundListeners.remove(fileId);
-            }
-        }
-
-        @Override
-        public void onTransferProgress(long progressRate, long totalTransferredSoFar,
-                                       long totalToTransfer, String fileName) {
-            OnDatatransferProgressListener boundListener =
-                    mBoundListeners.get(mCurrentDownload.getFile().getFileId());
-            if (boundListener != null) {
-                boundListener.onTransferProgress(progressRate, totalTransferredSoFar,
-                                                 totalToTransfer, fileName);
-            }
-        }
-
-    }
-
-    /**
-     * Download worker. Performs the pending downloads in the order they were requested.
-
-     * Created with the Looper of a new thread, started in {@link FileUploader#onCreate()}.
-     */
-    private static class ServiceHandler extends Handler {
-        // don't make it a final class, and don't remove the static ; lint will warn about a
-        // possible memory leak
-        FileDownloader mService;
-
-        public ServiceHandler(Looper looper, FileDownloader service) {
-            super(looper);
-            if (service == null) {
-                throw new IllegalArgumentException("Received invalid NULL in parameter 'service'");
-            }
-            mService = service;
-        }
-
-        @Override
-        public void handleMessage(Message msg) {
-            @SuppressWarnings("unchecked")
-            AbstractList<String> requestedDownloads = (AbstractList<String>) msg.obj;
-            if (msg.obj != null) {
-                Iterator<String> it = requestedDownloads.iterator();
-                while (it.hasNext()) {
-                    String next = it.next();
-                    mService.downloadFile(next);
-                }
-            }
-            mService.mStartedDownload=false;
-
-            (new Handler()).postDelayed(() -> {
-                if(!mService.mStartedDownload){
-                    mService.mNotificationManager.cancel(R.string.downloader_download_in_progress_ticker);
-                }
-                Log_OC.d(TAG, "Stopping after command with id " + msg.arg1);
-                mService.mNotificationManager.cancel(FOREGROUND_SERVICE_ID);
-            }, 2000);
-        }
-    }
-
-
-    void downloadFile(String downloadKey) {
-
-        mStartedDownload = true;
-        mCurrentDownload = mPendingDownloads.get(downloadKey);
-
-        if (mCurrentDownload != null) {
-            // Detect if the account exists
-            if (accountManager.exists(mCurrentDownload.getUser().toPlatformAccount())) {
-                notifyDownloadStart(mCurrentDownload);
-                RemoteOperationResult downloadResult = null;
-                try {
-                    /// prepare client object to send the request to the ownCloud server
-                    Account currentDownloadAccount = mCurrentDownload.getUser().toPlatformAccount();
-                    Optional<User> currentDownloadUser = accountManager.getUser(currentDownloadAccount.name);
-                    if (!currentUser.equals(currentDownloadUser)) {
-                        currentUser = currentDownloadUser;
-                        mStorageManager = new FileDataStorageManager(currentUser.get(), context.getContentResolver());
-                    }   // else, reuse storage manager from previous operation
-
-                    // always get client from client manager, to get fresh credentials in case
-                    // of update
-                    OwnCloudAccount ocAccount = currentDownloadUser.get().toOwnCloudAccount();
-                    mDownloadClient = OwnCloudClientManagerFactory.getDefaultSingleton().
-                            getClientFor(ocAccount, context);
-
-
-                    /// perform the download
-                    downloadResult = mCurrentDownload.execute(mDownloadClient);
-                    if (downloadResult.isSuccess() && mCurrentDownload.getDownloadType() == DownloadType.DOWNLOAD) {
-                        saveDownloadedFile();
-                    }
-
-                } catch (Exception e) {
-                    Log_OC.e(TAG, "Error downloading", e);
-                    downloadResult = new RemoteOperationResult(e);
-
-                } finally {
-                    Pair<DownloadFileOperation, String> removeResult = mPendingDownloads.removePayload(
-                        mCurrentDownload.getUser().getAccountName(), mCurrentDownload.getRemotePath());
-
-                    if (downloadResult == null) {
-                        downloadResult = new RemoteOperationResult(new RuntimeException("Error downloading…"));
-                    }
-
-                    /// notify result
-                    notifyDownloadResult(mCurrentDownload, downloadResult);
-                    sendBroadcastDownloadFinished(mCurrentDownload, downloadResult, removeResult.second);
-                }
-            } else {
-                cancelPendingDownloads(mCurrentDownload.getUser().getAccountName());
-            }
-        }
-    }
-
-
-    /**
-     * Updates the OC File after a successful download.
-     *
-     * TODO move to DownloadFileOperation
-     *  unify with code from {@link DocumentsStorageProvider} and {@link DownloadTask}.
-     */
-    private void saveDownloadedFile() {
-        OCFile file = mStorageManager.getFileById(mCurrentDownload.getFile().getFileId());
-
-        if (file == null) {
-            // try to get file via path, needed for overwriting existing files on conflict dialog
-            file = mStorageManager.getFileByDecryptedRemotePath(mCurrentDownload.getFile().getRemotePath());
-        }
-
-        if (file == null) {
-            Log_OC.e(this, "Could not save " + mCurrentDownload.getFile().getRemotePath());
-            return;
-        }
-
-        long syncDate = System.currentTimeMillis();
-        file.setLastSyncDateForProperties(syncDate);
-        file.setLastSyncDateForData(syncDate);
-        file.setUpdateThumbnailNeeded(true);
-        file.setModificationTimestamp(mCurrentDownload.getModificationTimestamp());
-        file.setModificationTimestampAtLastSyncForData(mCurrentDownload.getModificationTimestamp());
-        file.setEtag(mCurrentDownload.getEtag());
-        file.setMimeType(mCurrentDownload.getMimeType());
-        file.setStoragePath(mCurrentDownload.getSavePath());
-        file.setFileLength(new File(mCurrentDownload.getSavePath()).length());
-        file.setRemoteId(mCurrentDownload.getFile().getRemoteId());
-        mStorageManager.saveFile(file);
-        if (MimeTypeUtil.isMedia(mCurrentDownload.getMimeType())) {
-            FileDataStorageManager.triggerMediaScan(file.getStoragePath(), file);
-        }
-        mStorageManager.saveConflict(file, null);
-    }
-
-    /**
-     * Creates a status notification to show the download progress
-     *
-     * @param download Download operation starting.
-     */
-    private void notifyDownloadStart(DownloadFileOperation download) {
-        /// create status notification with a progress bar
-        mLastPercent = 0;
-        mNotificationBuilder = NotificationUtils.newNotificationBuilder(context, viewThemeUtils);
-        mNotificationBuilder
-            .setSmallIcon(R.drawable.notification_icon)
-            .setTicker(context.getString(R.string.downloader_download_in_progress_ticker))
-            .setContentTitle(context.getString(R.string.downloader_download_in_progress_ticker))
-            .setOngoing(true)
-            .setProgress(100, 0, download.getSize() < 0)
-            .setContentText(
-                String.format(context.getString(R.string.downloader_download_in_progress_content), 0,
-                              new File(download.getSavePath()).getName()));
-
-        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
-            mNotificationBuilder.setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_DOWNLOAD);
-        }
-
-        /// includes a pending intent in the notification showing the details view of the file
-        Intent showDetailsIntent = null;
-        if (PreviewImageFragment.canBePreviewed(download.getFile())) {
-            showDetailsIntent = new Intent(context, PreviewImageActivity.class);
-        } else {
-            showDetailsIntent = new Intent(context, FileDisplayActivity.class);
-        }
-        showDetailsIntent.putExtra(FileActivity.EXTRA_FILE, download.getFile());
-        showDetailsIntent.putExtra(FileActivity.EXTRA_USER, download.getUser());
-        showDetailsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
-
-        mNotificationBuilder.setContentIntent(PendingIntent.getActivity(context, (int) System.currentTimeMillis(),
-                                                                        showDetailsIntent, PendingIntent.FLAG_IMMUTABLE));
-
-
-        if (mNotificationManager == null) {
-            mNotificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
-        }
-        if (mNotificationManager != null) {
-            mNotificationManager.notify(R.string.downloader_download_in_progress_ticker, mNotificationBuilder.build());
-        }
-    }
-
-
-    /**
-     * Callback method to update the progress bar in the status notification.
-     */
-    @Override
-    public void onTransferProgress(long progressRate, long totalTransferredSoFar,
-                                   long totalToTransfer, String filePath) {
-        int percent = (int) (100.0 * ((double) totalTransferredSoFar) / ((double) totalToTransfer));
-        if (percent != mLastPercent) {
-            mNotificationBuilder.setProgress(100, percent, totalToTransfer < 0);
-            String fileName = filePath.substring(filePath.lastIndexOf(FileUtils.PATH_SEPARATOR) + 1);
-            String text = String.format(context.getString(R.string.downloader_download_in_progress_content), percent, fileName);
-            mNotificationBuilder.setContentText(text);
-
-            if (mNotificationManager == null) {
-                mNotificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
-            }
-
-            if (mNotificationManager != null) {
-                mNotificationManager.notify(R.string.downloader_download_in_progress_ticker,
-                        mNotificationBuilder.build());
-            }
-        }
-        mLastPercent = percent;
-    }
-
-
-    /**
-     * Updates the status notification with the result of a download operation.
-     *
-     * @param downloadResult Result of the download operation.
-     * @param download       Finished download operation
-     */
-    @SuppressFBWarnings("DMI")
-    private void notifyDownloadResult(DownloadFileOperation download,
-                                      RemoteOperationResult downloadResult) {
-        if (mNotificationManager == null) {
-            mNotificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
-        }
-
-        if (!downloadResult.isCancelled()) {
-            if (downloadResult.isSuccess()) {
-                if (conflictUploadId > 0) {
-                    uploadsStorageManager.removeUpload(conflictUploadId);
-                }
-                // Dont show notification except an error has occured.
-                return;
-            }
-            int tickerId = downloadResult.isSuccess() ?
-                    R.string.downloader_download_succeeded_ticker : R.string.downloader_download_failed_ticker;
-
-            boolean needsToUpdateCredentials = ResultCode.UNAUTHORIZED == downloadResult.getCode();
-            tickerId = needsToUpdateCredentials ?
-                    R.string.downloader_download_failed_credentials_error : tickerId;
-
-            mNotificationBuilder
-                    .setTicker(context.getString(tickerId))
-                    .setContentTitle(context.getString(tickerId))
-                    .setAutoCancel(true)
-                    .setOngoing(false)
-                    .setProgress(0, 0, false);
-
-            if (needsToUpdateCredentials) {
-                configureUpdateCredentialsNotification(download.getUser());
-
-            } else {
-                // TODO put something smart in showDetailsIntent
-                Intent showDetailsIntent = new Intent();
-                mNotificationBuilder.setContentIntent(PendingIntent.getActivity(context, (int) System.currentTimeMillis(),
-                                                                                showDetailsIntent, PendingIntent.FLAG_IMMUTABLE));
-            }
-
-            mNotificationBuilder.setContentText(ErrorMessageAdapter.getErrorCauseMessage(downloadResult,
-                                                                                         download, context.getResources()));
-
-            if (mNotificationManager != null) {
-                mNotificationManager.notify((new SecureRandom()).nextInt(), mNotificationBuilder.build());
-
-                // Remove success notification
-                if (downloadResult.isSuccess()) {
-                    // Sleep 2 seconds, so show the notification before remove it
-                    NotificationUtils.cancelWithDelay(mNotificationManager,
-                                                      R.string.downloader_download_succeeded_ticker, 2000);
-                }
-            }
-        }
-    }
-
-    private void configureUpdateCredentialsNotification(User user) {
-        // let the user update credentials with one click
-        Intent updateAccountCredentials = new Intent(context, AuthenticatorActivity.class);
-        updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, user.toPlatformAccount());
-        updateAccountCredentials.putExtra(
-                AuthenticatorActivity.EXTRA_ACTION,
-                AuthenticatorActivity.ACTION_UPDATE_EXPIRED_TOKEN
-        );
-        updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-        updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
-        updateAccountCredentials.addFlags(Intent.FLAG_FROM_BACKGROUND);
-        mNotificationBuilder.setContentIntent(
-            PendingIntent.getActivity(context,
-                                      (int) System.currentTimeMillis(),
-                                      updateAccountCredentials,
-                                      PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE)
-                                             );
-    }
-
-
-    /**
-     * Sends a broadcast when a download finishes in order to the interested activities can
-     * update their view
-     *
-     * @param download               Finished download operation
-     * @param downloadResult         Result of the download operation
-     * @param unlinkedFromRemotePath Path in the downloads tree where the download was unlinked from
-     */
-    private void sendBroadcastDownloadFinished(
-            DownloadFileOperation download,
-            RemoteOperationResult downloadResult,
-            String unlinkedFromRemotePath) {
-
-        Intent end = new Intent(getDownloadFinishMessage());
-        end.putExtra(EXTRA_DOWNLOAD_RESULT, downloadResult.isSuccess());
-        end.putExtra(ACCOUNT_NAME, download.getUser().getAccountName());
-        end.putExtra(EXTRA_REMOTE_PATH, download.getRemotePath());
-        end.putExtra(OCFileListFragment.DOWNLOAD_BEHAVIOUR, download.getBehaviour());
-        end.putExtra(SendShareDialog.ACTIVITY_NAME, download.getActivityName());
-        end.putExtra(SendShareDialog.PACKAGE_NAME, download.getPackageName());
-        if (unlinkedFromRemotePath != null) {
-            end.putExtra(EXTRA_LINKED_TO_PATH, unlinkedFromRemotePath);
-        }
-        end.setPackage(context.getPackageName());
-        localBroadcastManager.sendBroadcast(end);
-    }
-
-
-    /**
-     * Sends a broadcast when a new download is added to the queue.
-     *
-     * @param download           Added download operation
-     * @param linkedToRemotePath Path in the downloads tree where the download was linked to
-     */
-    private void sendBroadcastNewDownload(DownloadFileOperation download,
-                                          String linkedToRemotePath) {
-        Intent added = new Intent(getDownloadAddedMessage());
-        added.putExtra(ACCOUNT_NAME, download.getUser().getAccountName());
-        added.putExtra(EXTRA_REMOTE_PATH, download.getRemotePath());
-        added.putExtra(EXTRA_LINKED_TO_PATH, linkedToRemotePath);
-        added.setPackage(context.getPackageName());
-        localBroadcastManager.sendBroadcast(added);
-    }
-
-    private void cancelPendingDownloads(String accountName) {
-        mPendingDownloads.remove(accountName);
-    }
-}

+ 11 - 6
app/src/main/java/com/owncloud/android/operations/SynchronizeFileOperation.java

@@ -22,13 +22,12 @@
 package com.owncloud.android.operations;
 package com.owncloud.android.operations;
 
 
 import android.content.Context;
 import android.content.Context;
-import android.content.Intent;
 import android.text.TextUtils;
 import android.text.TextUtils;
 
 
 import com.nextcloud.client.account.User;
 import com.nextcloud.client.account.User;
+import com.nextcloud.client.files.downloader.FilesDownloadHelper;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.datamodel.OCFile;
-import com.owncloud.android.files.services.FileDownloader;
 import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.files.services.NameCollisionPolicy;
 import com.owncloud.android.files.services.NameCollisionPolicy;
 import com.owncloud.android.lib.common.OwnCloudClient;
 import com.owncloud.android.lib.common.OwnCloudClient;
@@ -317,11 +316,17 @@ public class SynchronizeFileOperation extends SyncOperation {
      * @param file OCFile object representing the file to download
      * @param file OCFile object representing the file to download
      */
      */
     private void requestForDownload(OCFile file) {
     private void requestForDownload(OCFile file) {
-        Intent i = new Intent(mContext, FileDownloader.class);
-        i.putExtra(FileDownloader.EXTRA_USER, mUser);
-        i.putExtra(FileDownloader.EXTRA_FILE, file);
+        FilesDownloadHelper downloadHelper = new FilesDownloadHelper();
+
+        downloadHelper.downloadFile(
+            mUser,
+            file,
+            "",
+            null,
+            "",
+            "",
+            null);
 
 
-        new FileDownloader(i);
         mTransferWasRequested = true;
         mTransferWasRequested = true;
     }
     }
 
 

+ 12 - 9
app/src/main/java/com/owncloud/android/operations/SynchronizeFolderOperation.java

@@ -25,10 +25,10 @@ import android.content.Intent;
 import android.text.TextUtils;
 import android.text.TextUtils;
 
 
 import com.nextcloud.client.account.User;
 import com.nextcloud.client.account.User;
+import com.nextcloud.client.files.downloader.FilesDownloadHelper;
 import com.owncloud.android.datamodel.DecryptedFolderMetadata;
 import com.owncloud.android.datamodel.DecryptedFolderMetadata;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.datamodel.OCFile;
-import com.owncloud.android.files.services.FileDownloader;
 import com.owncloud.android.lib.common.OwnCloudClient;
 import com.owncloud.android.lib.common.OwnCloudClient;
 import com.owncloud.android.lib.common.operations.OperationCancelledException;
 import com.owncloud.android.lib.common.operations.OperationCancelledException;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
@@ -451,14 +451,17 @@ public class SynchronizeFolderOperation extends SyncOperation {
                 if (mCancellationRequested.get()) {
                 if (mCancellationRequested.get()) {
                     throw new OperationCancelledException();
                     throw new OperationCancelledException();
                 }
                 }
-                Intent i = new Intent(mContext, FileDownloader.class);
-                i.putExtra(FileDownloader.EXTRA_USER, user);
-                i.putExtra(FileDownloader.EXTRA_FILE, file);
-                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
-                    mContext.startForegroundService(i);
-                } else {
-                    mContext.startService(i);
-                }
+
+                FilesDownloadHelper downloadHelper = new FilesDownloadHelper();
+
+                downloadHelper.downloadFile(
+                    user,
+                    file,
+                    "",
+                    null,
+                    "",
+                    "",
+                    null);
             }
             }
         }
         }
     }
     }

+ 1 - 2
app/src/main/java/com/owncloud/android/providers/DocumentsStorageProvider.java

@@ -52,7 +52,6 @@ import com.owncloud.android.R;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
-import com.owncloud.android.files.services.FileDownloader;
 import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.files.services.NameCollisionPolicy;
 import com.owncloud.android.files.services.NameCollisionPolicy;
 import com.owncloud.android.lib.common.OwnCloudAccount;
 import com.owncloud.android.lib.common.OwnCloudAccount;
@@ -308,7 +307,7 @@ public class DocumentsStorageProvider extends DocumentsProvider {
     /**
     /**
      * Updates the OC File after a successful download.
      * Updates the OC File after a successful download.
      *
      *
-     * TODO unify with code from {@link FileDownloader} and {@link DownloadTask}.
+     * TODO unify with code from {@link com.nextcloud.client.files.downloader.FilesDownloadWorker} and {@link DownloadTask}.
      */
      */
     private void saveDownloadedFile(FileDataStorageManager storageManager, DownloadFileOperation dfo, OCFile file) {
     private void saveDownloadedFile(FileDataStorageManager storageManager, DownloadFileOperation dfo, OCFile file) {
         long syncDate = System.currentTimeMillis();
         long syncDate = System.currentTimeMillis();

+ 8 - 8
app/src/main/java/com/owncloud/android/services/SyncFolderHandler.java

@@ -28,8 +28,8 @@ import android.os.Message;
 import android.util.Pair;
 import android.util.Pair;
 
 
 import com.nextcloud.client.account.User;
 import com.nextcloud.client.account.User;
+import com.nextcloud.client.files.downloader.FilesDownloadWorker;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.datamodel.OCFile;
-import com.owncloud.android.files.services.FileDownloader;
 import com.owncloud.android.files.services.IndexedForest;
 import com.owncloud.android.files.services.IndexedForest;
 import com.owncloud.android.lib.common.OwnCloudAccount;
 import com.owncloud.android.lib.common.OwnCloudAccount;
 import com.owncloud.android.lib.common.OwnCloudClient;
 import com.owncloud.android.lib.common.OwnCloudClient;
@@ -169,9 +169,9 @@ class SyncFolderHandler extends Handler {
      * this is a fast and ugly patch.
      * this is a fast and ugly patch.
      */
      */
     private void sendBroadcastNewSyncFolder(Account account, String remotePath) {
     private void sendBroadcastNewSyncFolder(Account account, String remotePath) {
-        Intent added = new Intent(FileDownloader.getDownloadAddedMessage());
-        added.putExtra(FileDownloader.ACCOUNT_NAME, account.name);
-        added.putExtra(FileDownloader.EXTRA_REMOTE_PATH, remotePath);
+        Intent added = new Intent(FilesDownloadWorker.Companion.getDownloadAddedMessage());
+        added.putExtra(FilesDownloadWorker.ACCOUNT_NAME, account.name);
+        added.putExtra(FilesDownloadWorker.EXTRA_REMOTE_PATH, remotePath);
         added.setPackage(mService.getPackageName());
         added.setPackage(mService.getPackageName());
         LocalBroadcastManager.getInstance(mService.getApplicationContext()).sendBroadcast(added);
         LocalBroadcastManager.getInstance(mService.getApplicationContext()).sendBroadcast(added);
     }
     }
@@ -182,10 +182,10 @@ class SyncFolderHandler extends Handler {
      */
      */
     private void sendBroadcastFinishedSyncFolder(Account account, String remotePath,
     private void sendBroadcastFinishedSyncFolder(Account account, String remotePath,
                                                  boolean success) {
                                                  boolean success) {
-        Intent finished = new Intent(FileDownloader.getDownloadFinishMessage());
-        finished.putExtra(FileDownloader.ACCOUNT_NAME, account.name);
-        finished.putExtra(FileDownloader.EXTRA_REMOTE_PATH, remotePath);
-        finished.putExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, success);
+        Intent finished = new Intent(FilesDownloadWorker.Companion.getDownloadFinishMessage());
+        finished.putExtra(FilesDownloadWorker.ACCOUNT_NAME, account.name);
+        finished.putExtra(FilesDownloadWorker.EXTRA_REMOTE_PATH, remotePath);
+        finished.putExtra(FilesDownloadWorker.EXTRA_DOWNLOAD_RESULT, success);
         finished.setPackage(mService.getPackageName());
         finished.setPackage(mService.getPackageName());
         LocalBroadcastManager.getInstance(mService.getApplicationContext()).sendBroadcast(finished);
         LocalBroadcastManager.getInstance(mService.getApplicationContext()).sendBroadcast(finished);
     }
     }

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

@@ -20,8 +20,8 @@
 
 
 package com.owncloud.android.ui.activity;
 package com.owncloud.android.ui.activity;
 
 
+import com.nextcloud.client.files.downloader.FilesDownloadWorker;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.FileDataStorageManager;
-import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
 import com.owncloud.android.services.OperationsService.OperationsServiceBinder;
 import com.owncloud.android.services.OperationsService.OperationsServiceBinder;
 import com.owncloud.android.ui.helpers.FileOperationsHelper;
 import com.owncloud.android.ui.helpers.FileOperationsHelper;
@@ -32,7 +32,7 @@ public interface ComponentsGetter {
      * To be invoked when the parent activity is fully created to get a reference
      * To be invoked when the parent activity is fully created to get a reference
      * to the FileDownloader service API.
      * to the FileDownloader service API.
      */
      */
-    public FileDownloaderBinder getFileDownloaderBinder();
+    public FilesDownloadWorker.FileDownloaderBinder getFileDownloaderBinder();
 
 
     
     
     /**
     /**

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

@@ -21,13 +21,13 @@ import android.content.Intent
 import android.os.Bundle
 import android.os.Bundle
 import android.widget.Toast
 import android.widget.Toast
 import com.nextcloud.client.account.User
 import com.nextcloud.client.account.User
+import com.nextcloud.client.files.downloader.FilesDownloadHelper
 import com.nextcloud.model.HTTPStatusCodes
 import com.nextcloud.model.HTTPStatusCodes
 import com.nextcloud.utils.extensions.getParcelableArgument
 import com.nextcloud.utils.extensions.getParcelableArgument
 import com.owncloud.android.R
 import com.owncloud.android.R
 import com.owncloud.android.datamodel.OCFile
 import com.owncloud.android.datamodel.OCFile
 import com.owncloud.android.datamodel.UploadsStorageManager
 import com.owncloud.android.datamodel.UploadsStorageManager
 import com.owncloud.android.db.OCUpload
 import com.owncloud.android.db.OCUpload
-import com.owncloud.android.files.services.FileDownloader
 import com.owncloud.android.files.services.FileUploader
 import com.owncloud.android.files.services.FileUploader
 import com.owncloud.android.files.services.NameCollisionPolicy
 import com.owncloud.android.files.services.NameCollisionPolicy
 import com.owncloud.android.lib.common.utils.Log_OC
 import com.owncloud.android.lib.common.utils.Log_OC
@@ -114,12 +114,17 @@ class ConflictsResolveActivity : FileActivity(), OnConflictDecisionMadeListener
 
 
                 Decision.KEEP_SERVER -> if (!shouldDeleteLocal()) {
                 Decision.KEEP_SERVER -> if (!shouldDeleteLocal()) {
                     // Overwrite local file
                     // Overwrite local file
-                    val intent = Intent(baseContext, FileDownloader::class.java)
-                    intent.putExtra(FileDownloader.EXTRA_USER, getUser().orElseThrow { RuntimeException() })
-                    intent.putExtra(FileDownloader.EXTRA_FILE, file)
-                    intent.putExtra(EXTRA_CONFLICT_UPLOAD_ID, conflictUploadId)
-
-                    FileDownloader(intent)
+                    file?.let {
+                        FilesDownloadHelper().downloadFile(
+                            user = getUser().orElseThrow { RuntimeException() },
+                            ocFile = file,
+                            conflictUploadId = conflictUploadId,
+                            behaviour = "",
+                            packageName = "",
+                            activityName = "",
+                            downloadType = null
+                        )
+                    }
                 } else {
                 } else {
                     uploadsStorageManager!!.removeUpload(upload)
                     uploadsStorageManager!!.removeUpload(upload)
                 }
                 }

+ 9 - 7
app/src/main/java/com/owncloud/android/ui/activity/FileActivity.java

@@ -43,6 +43,8 @@ import android.text.TextUtils;
 import com.google.android.material.snackbar.Snackbar;
 import com.google.android.material.snackbar.Snackbar;
 import com.nextcloud.client.account.User;
 import com.nextcloud.client.account.User;
 import com.nextcloud.client.account.UserAccountManager;
 import com.nextcloud.client.account.UserAccountManager;
+import com.nextcloud.client.files.downloader.FilesDownloadHelper;
+import com.nextcloud.client.files.downloader.FilesDownloadWorker;
 import com.nextcloud.client.jobs.BackgroundJobManager;
 import com.nextcloud.client.jobs.BackgroundJobManager;
 import com.nextcloud.client.network.ConnectivityService;
 import com.nextcloud.client.network.ConnectivityService;
 import com.nextcloud.utils.EditorUtils;
 import com.nextcloud.utils.EditorUtils;
@@ -54,8 +56,6 @@ import com.owncloud.android.authentication.AuthenticatorActivity;
 import com.owncloud.android.datamodel.ArbitraryDataProvider;
 import com.owncloud.android.datamodel.ArbitraryDataProvider;
 import com.owncloud.android.datamodel.ArbitraryDataProviderImpl;
 import com.owncloud.android.datamodel.ArbitraryDataProviderImpl;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.datamodel.OCFile;
-import com.owncloud.android.files.services.FileDownloader;
-import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
 import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
 import com.owncloud.android.lib.common.OwnCloudAccount;
 import com.owncloud.android.lib.common.OwnCloudAccount;
@@ -166,7 +166,7 @@ public abstract class FileActivity extends DrawerActivity
 
 
     private boolean mResumed;
     private boolean mResumed;
 
 
-    protected FileDownloaderBinder mDownloaderBinder;
+    protected FilesDownloadWorker.FileDownloaderBinder mDownloaderBinder;
     protected FileUploaderBinder mUploaderBinder;
     protected FileUploaderBinder mUploaderBinder;
     private ServiceConnection mDownloadServiceConnection;
     private ServiceConnection mDownloadServiceConnection;
     private ServiceConnection mUploadServiceConnection;
     private ServiceConnection mUploadServiceConnection;
@@ -206,6 +206,7 @@ public abstract class FileActivity extends DrawerActivity
         super.onCreate(savedInstanceState);
         super.onCreate(savedInstanceState);
         mHandler = new Handler();
         mHandler = new Handler();
         mFileOperationsHelper = new FileOperationsHelper(this, getUserAccountManager(), connectivityService, editorUtils);
         mFileOperationsHelper = new FileOperationsHelper(this, getUserAccountManager(), connectivityService, editorUtils);
+        User user = null;
 
 
         if (savedInstanceState != null) {
         if (savedInstanceState != null) {
             mFile = BundleExtensionsKt.getParcelableArgument(savedInstanceState, FileActivity.EXTRA_FILE, OCFile.class);
             mFile = BundleExtensionsKt.getParcelableArgument(savedInstanceState, FileActivity.EXTRA_FILE, OCFile.class);
@@ -218,10 +219,11 @@ public abstract class FileActivity extends DrawerActivity
                 viewThemeUtils.files.themeActionBar(this, actionBar, savedInstanceState.getString(KEY_ACTION_BAR_TITLE));
                 viewThemeUtils.files.themeActionBar(this, actionBar, savedInstanceState.getString(KEY_ACTION_BAR_TITLE));
             }
             }
         } else {
         } else {
-            User user = IntentExtensionsKt.getParcelableArgument(getIntent(), FileActivity.EXTRA_USER, User.class);
+            user = IntentExtensionsKt.getParcelableArgument(getIntent(), FileActivity.EXTRA_USER, User.class);
             mFile = IntentExtensionsKt.getParcelableArgument(getIntent(), FileActivity.EXTRA_FILE, OCFile.class);
             mFile = IntentExtensionsKt.getParcelableArgument(getIntent(), FileActivity.EXTRA_FILE, OCFile.class);
             mFromNotification = getIntent().getBooleanExtra(FileActivity.EXTRA_FROM_NOTIFICATION,
             mFromNotification = getIntent().getBooleanExtra(FileActivity.EXTRA_FROM_NOTIFICATION,
                     false);
                     false);
+
             if (user != null) {
             if (user != null) {
                 setUser(user);
                 setUser(user);
             }
             }
@@ -233,8 +235,8 @@ public abstract class FileActivity extends DrawerActivity
                 Context.BIND_AUTO_CREATE);
                 Context.BIND_AUTO_CREATE);
 
 
         mDownloadServiceConnection = newTransferenceServiceConnection();
         mDownloadServiceConnection = newTransferenceServiceConnection();
-        if (mDownloadServiceConnection != null) {
-            new FileDownloader(new Intent(this, FileDownloader.class));
+        if (mDownloadServiceConnection != null && user != null) {
+            new FilesDownloadHelper().downloadFile(user, mFile, "", null, "", "", null);
         }
         }
         mUploadServiceConnection = newTransferenceServiceConnection();
         mUploadServiceConnection = newTransferenceServiceConnection();
         if (mUploadServiceConnection != null) {
         if (mUploadServiceConnection != null) {
@@ -615,7 +617,7 @@ public abstract class FileActivity extends DrawerActivity
     }
     }
 
 
     @Override
     @Override
-    public FileDownloaderBinder getFileDownloaderBinder() {
+    public FilesDownloadWorker.FileDownloaderBinder getFileDownloaderBinder() {
         return mDownloaderBinder;
         return mDownloaderBinder;
     }
     }
 
 

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

@@ -65,6 +65,8 @@ import com.nextcloud.client.core.AsyncRunner;
 import com.nextcloud.client.di.Injectable;
 import com.nextcloud.client.di.Injectable;
 import com.nextcloud.client.editimage.EditImageActivity;
 import com.nextcloud.client.editimage.EditImageActivity;
 import com.nextcloud.client.files.DeepLinkHandler;
 import com.nextcloud.client.files.DeepLinkHandler;
+import com.nextcloud.client.files.downloader.FilesDownloadHelper;
+import com.nextcloud.client.files.downloader.FilesDownloadWorker;
 import com.nextcloud.client.media.PlayerServiceConnection;
 import com.nextcloud.client.media.PlayerServiceConnection;
 import com.nextcloud.client.network.ClientFactory;
 import com.nextcloud.client.network.ClientFactory;
 import com.nextcloud.client.network.ConnectivityService;
 import com.nextcloud.client.network.ConnectivityService;
@@ -80,8 +82,6 @@ import com.owncloud.android.databinding.FilesBinding;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.datamodel.VirtualFolderType;
 import com.owncloud.android.datamodel.VirtualFolderType;
-import com.owncloud.android.files.services.FileDownloader;
-import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
 import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
 import com.owncloud.android.files.services.NameCollisionPolicy;
 import com.owncloud.android.files.services.NameCollisionPolicy;
@@ -684,12 +684,12 @@ public class FileDisplayActivity extends FileActivity
                 // the user browsed to other file ; forget the automatic preview
                 // the user browsed to other file ; forget the automatic preview
                 mWaitingToPreview = null;
                 mWaitingToPreview = null;
 
 
-            } else if (downloadEvent.equals(FileDownloader.getDownloadAddedMessage())) {
+            } else if (downloadEvent.equals(FilesDownloadWorker.Companion.getDownloadAddedMessage())) {
                 // grant that the details fragment updates the progress bar
                 // grant that the details fragment updates the progress bar
                 detailsFragment.listenForTransferProgress();
                 detailsFragment.listenForTransferProgress();
                 detailsFragment.updateFileDetails(true, false);
                 detailsFragment.updateFileDetails(true, false);
 
 
-            } else if (downloadEvent.equals(FileDownloader.getDownloadFinishMessage())) {
+            } else if (downloadEvent.equals(FilesDownloadWorker.Companion.getDownloadFinishMessage())) {
                 //  update the details panel
                 //  update the details panel
                 boolean detailsFragmentChanged = false;
                 boolean detailsFragmentChanged = false;
                 if (waitedPreview) {
                 if (waitedPreview) {
@@ -1115,8 +1115,8 @@ public class FileDisplayActivity extends FileActivity
         localBroadcastManager.registerReceiver(mUploadFinishReceiver, uploadIntentFilter);
         localBroadcastManager.registerReceiver(mUploadFinishReceiver, uploadIntentFilter);
 
 
         // Listen for download messages
         // Listen for download messages
-        IntentFilter downloadIntentFilter = new IntentFilter(FileDownloader.getDownloadAddedMessage());
-        downloadIntentFilter.addAction(FileDownloader.getDownloadFinishMessage());
+        IntentFilter downloadIntentFilter = new IntentFilter(FilesDownloadWorker.Companion.getDownloadAddedMessage());
+        downloadIntentFilter.addAction(FilesDownloadWorker.Companion.getDownloadFinishMessage());
         mDownloadFinishReceiver = new DownloadFinishReceiver();
         mDownloadFinishReceiver = new DownloadFinishReceiver();
         localBroadcastManager.registerReceiver(mDownloadFinishReceiver, downloadIntentFilter);
         localBroadcastManager.registerReceiver(mDownloadFinishReceiver, downloadIntentFilter);
 
 
@@ -1417,7 +1417,7 @@ public class FileDisplayActivity extends FileActivity
 
 
 
 
     /**
     /**
-     * Class waiting for broadcast events from the {@link FileDownloader} service.
+     * Class waiting for broadcast events from the {@link FilesDownloadWorker} service.
      * <p>
      * <p>
      * Updates the UI when a download is started or finished, provided that it is relevant for the current folder.
      * Updates the UI when a download is started or finished, provided that it is relevant for the current folder.
      */
      */
@@ -1426,16 +1426,16 @@ public class FileDisplayActivity extends FileActivity
         @Override
         @Override
         public void onReceive(Context context, Intent intent) {
         public void onReceive(Context context, Intent intent) {
             boolean sameAccount = isSameAccount(intent);
             boolean sameAccount = isSameAccount(intent);
-            String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
+            String downloadedRemotePath = intent.getStringExtra(FilesDownloadWorker.EXTRA_REMOTE_PATH);
             String downloadBehaviour = intent.getStringExtra(OCFileListFragment.DOWNLOAD_BEHAVIOUR);
             String downloadBehaviour = intent.getStringExtra(OCFileListFragment.DOWNLOAD_BEHAVIOUR);
             boolean isDescendant = isDescendant(downloadedRemotePath);
             boolean isDescendant = isDescendant(downloadedRemotePath);
 
 
             if (sameAccount && isDescendant) {
             if (sameAccount && isDescendant) {
-                String linkedToRemotePath = intent.getStringExtra(FileDownloader.EXTRA_LINKED_TO_PATH);
+                String linkedToRemotePath = intent.getStringExtra(FilesDownloadWorker.EXTRA_LINKED_TO_PATH);
                 if (linkedToRemotePath == null || isAscendant(linkedToRemotePath)) {
                 if (linkedToRemotePath == null || isAscendant(linkedToRemotePath)) {
                     updateListOfFilesFragment(false);
                     updateListOfFilesFragment(false);
                 }
                 }
-                refreshDetailsFragmentIfVisible(intent.getAction(), downloadedRemotePath, intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false));
+                refreshDetailsFragmentIfVisible(intent.getAction(), downloadedRemotePath, intent.getBooleanExtra(FilesDownloadWorker.EXTRA_DOWNLOAD_RESULT, false));
             }
             }
 
 
             if (mWaitingToSend != null) {
             if (mWaitingToSend != null) {
@@ -1468,7 +1468,7 @@ public class FileDisplayActivity extends FileActivity
         }
         }
 
 
         private boolean isSameAccount(Intent intent) {
         private boolean isSameAccount(Intent intent) {
-            String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);
+            String accountName = intent.getStringExtra(FilesDownloadWorker.ACCOUNT_NAME);
             return accountName != null && getAccount() != null && accountName.equals(getAccount().name);
             return accountName != null && getAccount() != null && accountName.equals(getAccount().name);
         }
         }
     }
     }
@@ -1570,9 +1570,9 @@ public class FileDisplayActivity extends FileActivity
 
 
         @Override
         @Override
         public void onServiceConnected(ComponentName component, IBinder service) {
         public void onServiceConnected(ComponentName component, IBinder service) {
-            if (component.equals(new ComponentName(FileDisplayActivity.this, FileDownloader.class))) {
+            if (component.equals(new ComponentName(FileDisplayActivity.this, FilesDownloadWorker.class))) {
                 Log_OC.d(TAG, "Download service connected");
                 Log_OC.d(TAG, "Download service connected");
-                mDownloaderBinder = (FileDownloaderBinder) service;
+                mDownloaderBinder = (FilesDownloadWorker.FileDownloaderBinder) service;
                 if (mWaitingToPreview != null && getStorageManager() != null) {
                 if (mWaitingToPreview != null && getStorageManager() != null) {
                     // update the file
                     // update the file
                     mWaitingToPreview = getStorageManager().getFileById(mWaitingToPreview.getFileId());
                     mWaitingToPreview = getStorageManager().getFileById(mWaitingToPreview.getFileId());
@@ -1603,7 +1603,7 @@ public class FileDisplayActivity extends FileActivity
 
 
         @Override
         @Override
         public void onServiceDisconnected(ComponentName component) {
         public void onServiceDisconnected(ComponentName component) {
-            if (component.equals(new ComponentName(FileDisplayActivity.this, FileDownloader.class))) {
+            if (component.equals(new ComponentName(FileDisplayActivity.this, FilesDownloadWorker.class))) {
                 Log_OC.d(TAG, "Download service disconnected");
                 Log_OC.d(TAG, "Download service disconnected");
                 mDownloaderBinder = null;
                 mDownloaderBinder = null;
             } else if (component.equals(new ComponentName(FileDisplayActivity.this, FileUploader.class))) {
             } else if (component.equals(new ComponentName(FileDisplayActivity.this, FileUploader.class))) {
@@ -1885,10 +1885,7 @@ public class FileDisplayActivity extends FileActivity
         User user = getUser().orElseThrow(RuntimeException::new);
         User user = getUser().orElseThrow(RuntimeException::new);
         //if (!mWaitingToPreview.isDownloading()) {
         //if (!mWaitingToPreview.isDownloading()) {
         if (!mDownloaderBinder.isDownloading(user, mWaitingToPreview)) {
         if (!mDownloaderBinder.isDownloading(user, mWaitingToPreview)) {
-            Intent i = new Intent(this, FileDownloader.class);
-            i.putExtra(FileDownloader.EXTRA_USER, user);
-            i.putExtra(FileDownloader.EXTRA_FILE, mWaitingToPreview);
-            startService(i);
+            new FilesDownloadHelper().downloadFile(user, mWaitingToPreview, "", null, "", "", null);
         }
         }
     }
     }
 
 
@@ -1960,13 +1957,7 @@ public class FileDisplayActivity extends FileActivity
     private void requestForDownload(OCFile file, String downloadBehaviour, String packageName, String activityName) {
     private void requestForDownload(OCFile file, String downloadBehaviour, String packageName, String activityName) {
         final User currentUser = getUser().orElseThrow(RuntimeException::new);
         final User currentUser = getUser().orElseThrow(RuntimeException::new);
         if (!mDownloaderBinder.isDownloading(currentUser, mWaitingToPreview)) {
         if (!mDownloaderBinder.isDownloading(currentUser, mWaitingToPreview)) {
-            Intent i = new Intent(this, FileDownloader.class);
-            i.putExtra(FileDownloader.EXTRA_USER, currentUser);
-            i.putExtra(FileDownloader.EXTRA_FILE, file);
-            i.putExtra(SendShareDialog.PACKAGE_NAME, packageName);
-            i.putExtra(SendShareDialog.ACTIVITY_NAME, activityName);
-            i.putExtra(OCFileListFragment.DOWNLOAD_BEHAVIOUR, downloadBehaviour);
-            startService(i);
+            new FilesDownloadHelper().downloadFile(currentUser, file, downloadBehaviour, null, activityName, packageName, null);
         }
         }
     }
     }
 
 

+ 6 - 6
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.google.common.collect.Sets;
 import com.nextcloud.client.account.User;
 import com.nextcloud.client.account.User;
 import com.nextcloud.client.account.UserAccountManager;
 import com.nextcloud.client.account.UserAccountManager;
+import com.nextcloud.client.files.downloader.FilesDownloadWorker;
 import com.nextcloud.client.jobs.BackgroundJobManager;
 import com.nextcloud.client.jobs.BackgroundJobManager;
 import com.nextcloud.client.onboarding.FirstRunActivity;
 import com.nextcloud.client.onboarding.FirstRunActivity;
 import com.nextcloud.java.util.Optional;
 import com.nextcloud.java.util.Optional;
@@ -51,7 +52,6 @@ import com.owncloud.android.authentication.AuthenticatorActivity;
 import com.owncloud.android.datamodel.ArbitraryDataProvider;
 import com.owncloud.android.datamodel.ArbitraryDataProvider;
 import com.owncloud.android.datamodel.ArbitraryDataProviderImpl;
 import com.owncloud.android.datamodel.ArbitraryDataProviderImpl;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.FileDataStorageManager;
-import com.owncloud.android.files.services.FileDownloader;
 import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.lib.common.OwnCloudAccount;
 import com.owncloud.android.lib.common.OwnCloudAccount;
 import com.owncloud.android.lib.common.UserInfo;
 import com.owncloud.android.lib.common.UserInfo;
@@ -243,8 +243,8 @@ public class ManageAccountsActivity extends FileActivity implements UserListAdap
     private void initializeComponentGetters() {
     private void initializeComponentGetters() {
         downloadServiceConnection = newTransferenceServiceConnection();
         downloadServiceConnection = newTransferenceServiceConnection();
         if (downloadServiceConnection != null) {
         if (downloadServiceConnection != null) {
-            bindService(new Intent(this, FileDownloader.class), downloadServiceConnection,
-                        Context.BIND_AUTO_CREATE);
+            // FIXME check this usage
+            // bindService(new Intent(this, FileDownloader.class), downloadServiceConnection, Context.BIND_AUTO_CREATE);
         }
         }
         uploadServiceConnection = newTransferenceServiceConnection();
         uploadServiceConnection = newTransferenceServiceConnection();
         if (uploadServiceConnection != null) {
         if (uploadServiceConnection != null) {
@@ -535,8 +535,8 @@ public class ManageAccountsActivity extends FileActivity implements UserListAdap
         @Override
         @Override
         public void onServiceConnected(ComponentName component, IBinder service) {
         public void onServiceConnected(ComponentName component, IBinder service) {
 
 
-            if (component.equals(new ComponentName(ManageAccountsActivity.this, FileDownloader.class))) {
-                mDownloaderBinder = (FileDownloader.FileDownloaderBinder) service;
+            if (component.equals(new ComponentName(ManageAccountsActivity.this, FilesDownloadWorker.class))) {
+                mDownloaderBinder = (FilesDownloadWorker.FileDownloaderBinder) service;
 
 
             } else if (component.equals(new ComponentName(ManageAccountsActivity.this, FileUploader.class))) {
             } else if (component.equals(new ComponentName(ManageAccountsActivity.this, FileUploader.class))) {
                 Log_OC.d(TAG, "Upload service connected");
                 Log_OC.d(TAG, "Upload service connected");
@@ -546,7 +546,7 @@ public class ManageAccountsActivity extends FileActivity implements UserListAdap
 
 
         @Override
         @Override
         public void onServiceDisconnected(ComponentName component) {
         public void onServiceDisconnected(ComponentName component) {
-            if (component.equals(new ComponentName(ManageAccountsActivity.this, FileDownloader.class))) {
+            if (component.equals(new ComponentName(ManageAccountsActivity.this, FilesDownloadWorker.class))) {
                 Log_OC.d(TAG, "Download service suddenly disconnected");
                 Log_OC.d(TAG, "Download service suddenly disconnected");
                 mDownloaderBinder = null;
                 mDownloaderBinder = null;
             } else if (component.equals(new ComponentName(ManageAccountsActivity.this, FileUploader.class))) {
             } else if (component.equals(new ComponentName(ManageAccountsActivity.this, FileUploader.class))) {

+ 4 - 4
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.User;
 import com.nextcloud.client.account.UserAccountManager;
 import com.nextcloud.client.account.UserAccountManager;
 import com.nextcloud.client.di.Injectable;
 import com.nextcloud.client.di.Injectable;
+import com.nextcloud.client.files.downloader.FilesDownloadWorker;
 import com.nextcloud.client.jobs.BackgroundJobManager;
 import com.nextcloud.client.jobs.BackgroundJobManager;
 import com.nextcloud.client.network.ClientFactory;
 import com.nextcloud.client.network.ClientFactory;
 import com.nextcloud.client.network.ConnectivityService;
 import com.nextcloud.client.network.ConnectivityService;
@@ -57,7 +58,6 @@ import com.owncloud.android.databinding.FileDetailsFragmentBinding;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
-import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
 import com.owncloud.android.lib.common.OwnCloudClient;
 import com.owncloud.android.lib.common.OwnCloudClient;
 import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
 import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
@@ -502,7 +502,7 @@ public class FileDetailFragment extends FileFragment implements OnClickListener,
      * TODO Remove parameter when the transferring state of files is kept in database.
      * 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
      * @param transferring Flag signaling if the file should be considered as downloading or uploading, although
-     *                     {@link FileDownloaderBinder#isDownloading(User, OCFile)}  and
+     *                     {@link com.nextcloud.client.files.downloader.FilesDownloadWorker.FileDownloaderBinder#isDownloading(User, OCFile)}  and
      *                     {@link FileUploaderBinder#isUploading(User, OCFile)} return false.
      *                     {@link FileUploaderBinder#isUploading(User, OCFile)} return false.
      * @param refresh      If 'true', try to refresh the whole file from the database
      * @param refresh      If 'true', try to refresh the whole file from the database
      */
      */
@@ -534,7 +534,7 @@ public class FileDetailFragment extends FileFragment implements OnClickListener,
             setFavoriteIconStatus(file.isFavorite());
             setFavoriteIconStatus(file.isFavorite());
 
 
             // configure UI for depending upon local state of the file
             // configure UI for depending upon local state of the file
-            FileDownloaderBinder downloaderBinder = containerActivity.getFileDownloaderBinder();
+            FilesDownloadWorker.FileDownloaderBinder downloaderBinder = containerActivity.getFileDownloaderBinder();
             FileUploaderBinder uploaderBinder = containerActivity.getFileUploaderBinder();
             FileUploaderBinder uploaderBinder = containerActivity.getFileUploaderBinder();
             if (transferring
             if (transferring
                 || (downloaderBinder != null && downloaderBinder.isDownloading(user, file))
                 || (downloaderBinder != null && downloaderBinder.isDownloading(user, file))
@@ -659,7 +659,7 @@ public class FileDetailFragment extends FileFragment implements OnClickListener,
             // show the progress bar for the transfer
             // show the progress bar for the transfer
             binding.progressBlock.setVisibility(View.VISIBLE);
             binding.progressBlock.setVisibility(View.VISIBLE);
             binding.progressText.setVisibility(View.VISIBLE);
             binding.progressText.setVisibility(View.VISIBLE);
-            FileDownloaderBinder downloaderBinder = containerActivity.getFileDownloaderBinder();
+            FilesDownloadWorker.FileDownloaderBinder downloaderBinder = containerActivity.getFileDownloaderBinder();
             FileUploaderBinder uploaderBinder = containerActivity.getFileUploaderBinder();
             FileUploaderBinder uploaderBinder = containerActivity.getFileUploaderBinder();
             //if (getFile().isDownloading()) {
             //if (getFile().isDownloading()) {
             if (downloaderBinder != null && downloaderBinder.isDownloading(user, getFile())) {
             if (downloaderBinder != null && downloaderBinder.isDownloading(user, getFile())) {

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

@@ -27,7 +27,6 @@ import android.os.Bundle;
 
 
 import com.nextcloud.utils.extensions.BundleExtensionsKt;
 import com.nextcloud.utils.extensions.BundleExtensionsKt;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.datamodel.OCFile;
-import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
 import com.owncloud.android.ui.activity.ComponentsGetter;
 import com.owncloud.android.ui.activity.ComponentsGetter;
 
 
@@ -161,8 +160,8 @@ public class FileFragment extends Fragment {
          * This happens when a download or upload is started or ended for a file.
          * This happens when a download or upload is started or ended for a file.
          *
          *
          * This method is necessary by now to update the user interface of the double-pane layout
          * This method is necessary by now to update the user interface of the double-pane layout
-         * in tablets because methods {@link FileDownloaderBinder#isDownloading(Account, OCFile)}
-         * and {@link FileUploaderBinder#isUploading(Account, OCFile)}
+         * in tablets because methods {@link //FileDownloaderBinder # isDownloading(Account, OCFile)}
+         * and {@link FileUploaderBinder# isUploading(Account, OCFile)}
          * won't provide the needed response before the method where this is called finishes.
          * won't provide the needed response before the method where this is called finishes.
          *
          *
          * TODO Remove this when the transfer state of a file is kept in the database
          * TODO Remove this when the transfer state of a file is kept in the database

+ 2 - 2
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.CurrentAccountProvider;
 import com.nextcloud.client.account.User;
 import com.nextcloud.client.account.User;
+import com.nextcloud.client.files.downloader.FilesDownloadWorker;
 import com.nextcloud.client.jobs.BackgroundJobManager;
 import com.nextcloud.client.jobs.BackgroundJobManager;
 import com.nextcloud.client.network.ConnectivityService;
 import com.nextcloud.client.network.ConnectivityService;
 import com.nextcloud.java.util.Optional;
 import com.nextcloud.java.util.Optional;
@@ -58,7 +59,6 @@ import com.owncloud.android.datamodel.ArbitraryDataProviderImpl;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.files.StreamMediaFileOperation;
 import com.owncloud.android.files.StreamMediaFileOperation;
-import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.lib.common.utils.Log_OC;
@@ -997,7 +997,7 @@ public class FileOperationsHelper {
         }
         }
 
 
         // for both files and folders
         // for both files and folders
-        FileDownloaderBinder downloaderBinder = fileActivity.getFileDownloaderBinder();
+        FilesDownloadWorker.FileDownloaderBinder downloaderBinder = fileActivity.getFileDownloaderBinder();
         if (downloaderBinder != null && downloaderBinder.isDownloading(currentUser, file)) {
         if (downloaderBinder != null && downloaderBinder.isDownloading(currentUser, file)) {
             downloaderBinder.cancel(currentUser.toPlatformAccount(), file);
             downloaderBinder.cancel(currentUser.toPlatformAccount(), file);
         }
         }

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

@@ -37,6 +37,8 @@ import android.view.View;
 import com.nextcloud.client.account.User;
 import com.nextcloud.client.account.User;
 import com.nextcloud.client.di.Injectable;
 import com.nextcloud.client.di.Injectable;
 import com.nextcloud.client.editimage.EditImageActivity;
 import com.nextcloud.client.editimage.EditImageActivity;
+import com.nextcloud.client.files.downloader.FilesDownloadHelper;
+import com.nextcloud.client.files.downloader.FilesDownloadWorker;
 import com.nextcloud.client.preferences.AppPreferences;
 import com.nextcloud.client.preferences.AppPreferences;
 import com.nextcloud.java.util.Optional;
 import com.nextcloud.java.util.Optional;
 import com.nextcloud.utils.extensions.IntentExtensionsKt;
 import com.nextcloud.utils.extensions.IntentExtensionsKt;
@@ -45,8 +47,6 @@ import com.owncloud.android.R;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.datamodel.VirtualFolderType;
 import com.owncloud.android.datamodel.VirtualFolderType;
-import com.owncloud.android.files.services.FileDownloader;
-import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
 import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
 import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
 import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
@@ -311,8 +311,8 @@ public class PreviewImageActivity extends FileActivity implements
         public void onServiceConnected(ComponentName component, IBinder service) {
         public void onServiceConnected(ComponentName component, IBinder service) {
 
 
             if (component.equals(new ComponentName(PreviewImageActivity.this,
             if (component.equals(new ComponentName(PreviewImageActivity.this,
-                    FileDownloader.class))) {
-                mDownloaderBinder = (FileDownloaderBinder) service;
+                    FilesDownloadWorker.class))) {
+                mDownloaderBinder = (FilesDownloadWorker.FileDownloaderBinder) service;
                 if (mRequestWaitingForBinder) {
                 if (mRequestWaitingForBinder) {
                     mRequestWaitingForBinder = false;
                     mRequestWaitingForBinder = false;
                     Log_OC.d(TAG, "Simulating reselection of current page after connection " +
                     Log_OC.d(TAG, "Simulating reselection of current page after connection " +
@@ -331,7 +331,7 @@ public class PreviewImageActivity extends FileActivity implements
         @Override
         @Override
         public void onServiceDisconnected(ComponentName component) {
         public void onServiceDisconnected(ComponentName component) {
             if (component.equals(new ComponentName(PreviewImageActivity.this,
             if (component.equals(new ComponentName(PreviewImageActivity.this,
-                    FileDownloader.class))) {
+                    FilesDownloadWorker.class))) {
                 Log_OC.d(TAG, "Download service suddenly disconnected");
                 Log_OC.d(TAG, "Download service suddenly disconnected");
                 mDownloaderBinder = null;
                 mDownloaderBinder = null;
             } else if (component.equals(new ComponentName(PreviewImageActivity.this,
             } else if (component.equals(new ComponentName(PreviewImageActivity.this,
@@ -359,7 +359,7 @@ public class PreviewImageActivity extends FileActivity implements
         super.onResume();
         super.onResume();
 
 
         mDownloadFinishReceiver = new DownloadFinishReceiver();
         mDownloadFinishReceiver = new DownloadFinishReceiver();
-        IntentFilter downloadIntentFilter = new IntentFilter(FileDownloader.getDownloadFinishMessage());
+        IntentFilter downloadIntentFilter = new IntentFilter(FilesDownloadWorker.Companion.getDownloadFinishMessage());
         localBroadcastManager.registerReceiver(mDownloadFinishReceiver, downloadIntentFilter);
         localBroadcastManager.registerReceiver(mDownloadFinishReceiver, downloadIntentFilter);
 
 
         mUploadFinishReceiver = new UploadFinishReceiver();
         mUploadFinishReceiver = new UploadFinishReceiver();
@@ -413,13 +413,7 @@ public class PreviewImageActivity extends FileActivity implements
 
 
         } else if (!mDownloaderBinder.isDownloading(getUserAccountManager().getUser(), file)) {
         } else if (!mDownloaderBinder.isDownloading(getUserAccountManager().getUser(), file)) {
             final User user = getUser().orElseThrow(RuntimeException::new);
             final User user = getUser().orElseThrow(RuntimeException::new);
-            Intent i = new Intent(this, FileDownloader.class);
-            i.putExtra(FileDownloader.EXTRA_USER, user);
-            i.putExtra(FileDownloader.EXTRA_FILE, file);
-            if (downloadBehaviour != null) {
-                i.putExtra(OCFileListFragment.DOWNLOAD_BEHAVIOUR, downloadBehaviour);
-            }
-            new FileDownloader(i);
+            new FilesDownloadHelper().downloadFile(user, file, downloadBehaviour, null, "", "", null);
         }
         }
     }
     }
 
 
@@ -484,7 +478,7 @@ public class PreviewImageActivity extends FileActivity implements
     }
     }
 
 
     /**
     /**
-     * Class waiting for broadcast events from the {@link FileDownloader} service.
+     * Class waiting for broadcast events from the {@link FilesDownloadWorker} service.
      *
      *
      * Updates the UI when a download is started or finished, provided that it is relevant for the
      * Updates the UI when a download is started or finished, provided that it is relevant for the
      * folder displayed in the gallery.
      * folder displayed in the gallery.
@@ -504,12 +498,12 @@ public class PreviewImageActivity extends FileActivity implements
     }
     }
 
 
     private void previewNewImage(Intent intent) {
     private void previewNewImage(Intent intent) {
-        String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);
-        String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
+        String accountName = intent.getStringExtra(FilesDownloadWorker.ACCOUNT_NAME);
+        String downloadedRemotePath = intent.getStringExtra(FilesDownloadWorker.EXTRA_REMOTE_PATH);
         String downloadBehaviour = intent.getStringExtra(OCFileListFragment.DOWNLOAD_BEHAVIOUR);
         String downloadBehaviour = intent.getStringExtra(OCFileListFragment.DOWNLOAD_BEHAVIOUR);
         if (getAccount().name.equals(accountName) && downloadedRemotePath != null) {
         if (getAccount().name.equals(accountName) && downloadedRemotePath != null) {
             OCFile file = getStorageManager().getFileByPath(downloadedRemotePath);
             OCFile file = getStorageManager().getFileByPath(downloadedRemotePath);
-            boolean downloadWasFine = intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false);
+            boolean downloadWasFine = intent.getBooleanExtra(FilesDownloadWorker.EXTRA_DOWNLOAD_RESULT, false);
 
 
             if (EditImageActivity.OPEN_IMAGE_EDITOR.equals(downloadBehaviour)) {
             if (EditImageActivity.OPEN_IMAGE_EDITOR.equals(downloadBehaviour)) {
                 startImageEditor(file);
                 startImageEditor(file);

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

@@ -53,6 +53,7 @@ import android.view.ViewGroup;
 import com.nextcloud.client.account.User;
 import com.nextcloud.client.account.User;
 import com.nextcloud.client.account.UserAccountManager;
 import com.nextcloud.client.account.UserAccountManager;
 import com.nextcloud.client.di.Injectable;
 import com.nextcloud.client.di.Injectable;
+import com.nextcloud.client.files.downloader.FilesDownloadHelper;
 import com.nextcloud.client.jobs.BackgroundJobManager;
 import com.nextcloud.client.jobs.BackgroundJobManager;
 import com.nextcloud.client.media.ExoplayerListener;
 import com.nextcloud.client.media.ExoplayerListener;
 import com.nextcloud.client.media.NextcloudExoPlayer;
 import com.nextcloud.client.media.NextcloudExoPlayer;
@@ -66,7 +67,6 @@ import com.owncloud.android.databinding.FragmentPreviewMediaBinding;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
 import com.owncloud.android.files.StreamMediaFileOperation;
 import com.owncloud.android.files.StreamMediaFileOperation;
-import com.owncloud.android.files.services.FileDownloader;
 import com.owncloud.android.lib.common.OwnCloudClient;
 import com.owncloud.android.lib.common.OwnCloudClient;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.lib.common.utils.Log_OC;
@@ -479,10 +479,7 @@ public class PreviewMediaFragment extends FileFragment implements OnTouchListene
                                                                     backgroundJobManager);
                                                                     backgroundJobManager);
         } else if (itemId == R.id.action_download_file) {
         } else if (itemId == R.id.action_download_file) {
             if (!containerActivity.getFileDownloaderBinder().isDownloading(user, getFile())) {
             if (!containerActivity.getFileDownloaderBinder().isDownloading(user, getFile())) {
-                Intent i = new Intent(requireActivity(), FileDownloader.class);
-                i.putExtra(FileDownloader.EXTRA_USER, user);
-                i.putExtra(FileDownloader.EXTRA_FILE, getFile());
-                new FileDownloader(i);
+                new FilesDownloadHelper().downloadFile(user, getFile(), "", null, "", "", null);
             }
             }
         }
         }
     }
     }