Skip to content

Commit

Permalink
Use WorkerState for handling download state
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Dec 29, 2023
1 parent b0a82c5 commit 1ac6eae
Show file tree
Hide file tree
Showing 17 changed files with 151 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class FileMenuFilterIT : AbstractIT() {
private lateinit var mockFileUploaderBinder: FileUploader.FileUploaderBinder

@MockK
private lateinit var mockFileDownloaderBinder: FileDownloadWorker.FileDownloaderBinder
private lateinit var mockFileDownloadProgressListener: FileDownloadWorker.FileDownloadProgressListener

@MockK
private lateinit var mockOperationsServiceBinder: OperationsService.OperationsServiceBinder
Expand All @@ -77,8 +77,8 @@ class FileMenuFilterIT : AbstractIT() {
MockKAnnotations.init(this)
every { mockFileUploaderBinder.isUploading(any(), any()) } returns false
every { mockComponentsGetter.fileUploaderBinder } returns mockFileUploaderBinder
every { mockFileDownloaderBinder.isDownloading(any(), any()) } returns false
every { mockComponentsGetter.fileDownloaderBinder } returns mockFileDownloaderBinder
every { mockFileDownloadProgressListener.isDownloading(any(), any()) } returns false
every { mockComponentsGetter.fileDownloadProgressListener } returns mockFileDownloadProgressListener
every { mockOperationsServiceBinder.isSynchronizing(any(), any()) } returns false
every { mockComponentsGetter.operationsServiceBinder } returns mockOperationsServiceBinder
every { mockStorageManager.getFileById(any()) } returns OCFile("/")
Expand Down
2 changes: 1 addition & 1 deletion app/src/debug/java/com/nextcloud/test/TestActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class TestActivity :
return null
}

override fun getFileDownloaderBinder(): FileDownloadWorker.FileDownloaderBinder? {
override fun getFileDownloadProgressListener(): FileDownloadWorker.FileDownloadProgressListener? {
return null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,90 @@ import com.owncloud.android.MainApp
import com.owncloud.android.datamodel.FileDataStorageManager
import com.owncloud.android.datamodel.OCFile
import com.owncloud.android.datamodel.UploadsStorageManager
import com.owncloud.android.lib.common.network.OnDatatransferProgressListener
import com.owncloud.android.operations.DownloadFileOperation
import com.owncloud.android.operations.DownloadType
import com.owncloud.android.utils.MimeTypeUtil
import java.io.File
import javax.inject.Inject

class FileDownloadHelper {
class FileDownloadHelper : OnDatatransferProgressListener {

@Inject
lateinit var backgroundJobManager: BackgroundJobManager

@Inject
lateinit var uploadsStorageManager: UploadsStorageManager

private val boundListeners: MutableMap<Long, OnDatatransferProgressListener> = HashMap()
private var currentDownload: DownloadFileOperation? = null

init {
MainApp.getAppComponent().inject(this)
}

fun addDataTransferProgressListener(listener: OnDatatransferProgressListener?, file: OCFile?) {
if (file == null || listener == null) {
return
}

boundListeners[file.fileId] = listener
}

fun removeDataTransferProgressListener(listener: OnDatatransferProgressListener?, file: OCFile?) {
if (file == null || listener == null) {
return
}

val fileId = file.fileId
if (boundListeners[fileId] === listener) {
boundListeners.remove(fileId)
}
}

override fun onTransferProgress(
progressRate: Long,
totalTransferredSoFar: Long,
totalToTransfer: Long,
fileName: String
) {
val listener = boundListeners[currentDownload?.file?.fileId]
listener?.onTransferProgress(
progressRate,
totalTransferredSoFar,
totalToTransfer,
fileName
)
}

fun setCurrentDownload(operation: DownloadFileOperation) {
currentDownload = operation
}

fun isDownloading(user: User?, file: OCFile?): Boolean {
return user != null && file != null && backgroundJobManager.isStartFileDownloadJobScheduled(
user,
file
)
}

fun cancelPendingOrCurrentDownloads(user: User?, file: OCFile?) {
if (user == null || file == null) return
backgroundJobManager.cancelFilesDownloadJob(user, file)
}

fun cancelAllDownloadsForAccount(accountName: String?, currentDownload: DownloadFileOperation) {
if (currentDownload.user.nameEquals(accountName)) {
currentDownload.file?.let { file ->
backgroundJobManager.cancelFilesDownloadJob(currentDownload.user, file)
}

currentDownload.cancel()
}

// removePendingDownload(accountName)
}

fun saveFile(
file: OCFile,
currentDownload: DownloadFileOperation?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import android.accounts.AccountManager
import android.accounts.OnAccountsUpdateListener
import android.app.PendingIntent
import android.content.Context
import android.os.Binder
import android.os.IBinder
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.work.Worker
import androidx.work.WorkerParameters
Expand Down Expand Up @@ -97,12 +95,13 @@ class FileDownloadWorker(
private val intents = FileDownloadIntents(context)
private val notificationManager = DownloadNotificationManager(context, viewThemeUtils)
private val pendingDownloads = IndexedForest<DownloadFileOperation>()
private var downloadBinder: IBinder = FileDownloaderBinder()
private var downloadProgressListener = FileDownloadProgressListener()
private var currentUser = Optional.empty<User>()
private val helper = FileDownloadHelper()
private var startedDownload = false
private var storageManager: FileDataStorageManager? = null
private var downloadClient: OwnCloudClient? = null
private var user: User? = null
private val gson = Gson()

@Suppress("TooGenericExceptionCaught")
Expand All @@ -122,11 +121,16 @@ class FileDownloadWorker(
}
}

override fun onStopped() {
super.onStopped()
setIdleWorkerState()
}

private fun getRequestDownloads(): AbstractList<String> {
conflictUploadId = inputData.keyValueMap[CONFLICT_UPLOAD_ID] as Long?
val file = gson.fromJson(inputData.keyValueMap[FILE] as String, OCFile::class.java)
val accountName = inputData.keyValueMap[USER_NAME] as String
val user = accountManager.getUser(accountName).get()
user = accountManager.getUser(accountName).get()
val downloadTypeAsString = inputData.keyValueMap[DOWNLOAD_TYPE] as String?
val downloadType = if (downloadTypeAsString != null) {
if (downloadTypeAsString == DownloadType.DOWNLOAD.toString()) {
Expand All @@ -140,7 +144,6 @@ class FileDownloadWorker(
val behaviour = inputData.keyValueMap[BEHAVIOUR] as String
val activityName = inputData.keyValueMap[ACTIVITY_NAME] as String
val packageName = inputData.keyValueMap[PACKAGE_NAME] as String
setWorkerState(user, file)

val requestedDownloads: AbstractList<String> = Vector()

Expand All @@ -156,7 +159,7 @@ class FileDownloadWorker(
)

operation.addDownloadDataTransferProgressListener(this)
operation.addDownloadDataTransferProgressListener(downloadBinder as FileDownloaderBinder)
operation.addDownloadDataTransferProgressListener(downloadProgressListener)
val putResult = pendingDownloads.putIfAbsent(
user?.accountName,
file.remotePath,
Expand All @@ -176,10 +179,14 @@ class FileDownloadWorker(
}
}

private fun setWorkerState(user: User, file: OCFile) {
private fun setWorkerState(user: User?, file: DownloadFileOperation?) {
WorkerStateLiveData.instance?.setWorkState(WorkerState.Download(user, file))
}

private fun setIdleWorkerState() {
WorkerStateLiveData.instance?.setWorkState(WorkerState.Idle)
}

private fun addAccountUpdateListener() {
val am = AccountManager.get(context)
am.addOnAccountsUpdatedListener(this, null, false)
Expand All @@ -205,6 +212,7 @@ class FileDownloadWorker(
if (currentDownload == null) {
return
}
setWorkerState(user, currentDownload)

val isAccountExist = accountManager.exists(currentDownload?.user?.toPlatformAccount())
if (!isAccountExist) {
Expand Down Expand Up @@ -354,34 +362,11 @@ class FileDownloadWorker(
lastPercent = percent
}

inner class FileDownloaderBinder : Binder(), OnDatatransferProgressListener {
inner class FileDownloadProgressListener : OnDatatransferProgressListener {
private val boundListeners: MutableMap<Long, OnDatatransferProgressListener> = HashMap()

fun cancelPendingOrCurrentDownloads() {
currentDownload?.file?.let { file ->
helper.backgroundJobManager.cancelFilesDownloadJob(currentUser.get(), file)
}
}

fun cancelAllDownloadsForAccount(accountName: String?) {
currentDownload?.user?.let {
if (it.nameEquals(accountName)) {
currentDownload?.file?.let { file ->
helper.backgroundJobManager.cancelFilesDownloadJob(it, file)
}

currentDownload?.cancel()
}
}

removePendingDownload(accountName)
}

fun isDownloading(user: User?, file: OCFile?): Boolean {
return user != null && file != null && helper.backgroundJobManager.isStartFileDownloadJobScheduled(
user,
file
)
return helper.isDownloading(user, file)
}

fun addDataTransferProgressListener(listener: OnDatatransferProgressListener?, file: OCFile?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class DownloadNotificationManager(private val context: Context, private val view
val notifyId = SecureRandom().nextInt()

val contentText = if (result.isSuccess) {
download.file.fileName + " " + context.getString(R.string.downloader_download_succeeded_ticker)
download.file.fileName
} else {
ErrorMessageAdapter.getErrorCauseMessage(
result,
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/nextcloud/model/WorkerState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
package com.nextcloud.model

import com.nextcloud.client.account.User
import com.owncloud.android.datamodel.OCFile
import com.owncloud.android.operations.DownloadFileOperation

sealed class WorkerState {
object Idle
class Download(var user: User, var file: OCFile): WorkerState()
object Idle : WorkerState()
class Download(var user: User?, var currentDownload: DownloadFileOperation?) : WorkerState()
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import com.nextcloud.client.account.User;
import com.nextcloud.client.editimage.EditImageActivity;
import com.nextcloud.client.files.downloader.FileDownloadHelper;
import com.nextcloud.client.files.downloader.FileDownloadWorker;
import com.nextcloud.utils.EditorUtils;
import com.owncloud.android.R;
import com.owncloud.android.datamodel.FileDataStorageManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public interface ComponentsGetter {
* To be invoked when the parent activity is fully created to get a reference
* to the FileDownloadWorker.
*/
public FileDownloadWorker.FileDownloaderBinder getFileDownloaderBinder();
public FileDownloadWorker.FileDownloadProgressListener getFileDownloadProgressListener();



/**
* To be invoked when the parent activity is fully created to get a reference
* to the FileUploader service API.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public abstract class FileActivity extends DrawerActivity
private boolean mResumed;

protected FileDownloadHelper fileDownloadHelper = new FileDownloadHelper();
protected FileDownloadWorker.FileDownloaderBinder mDownloaderBinder;
protected FileDownloadWorker.FileDownloadProgressListener fileDownloadProgressListener;
protected FileUploaderBinder mUploaderBinder;
private ServiceConnection mUploadServiceConnection;

Expand Down Expand Up @@ -613,8 +613,8 @@ public void onServiceDisconnected(ComponentName component) {
}

@Override
public FileDownloadWorker.FileDownloaderBinder getFileDownloaderBinder() {
return mDownloaderBinder;
public FileDownloadWorker.FileDownloadProgressListener getFileDownloadProgressListener() {
return fileDownloadProgressListener;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
import com.nextcloud.client.di.Injectable;
import com.nextcloud.client.editimage.EditImageActivity;
import com.nextcloud.client.files.DeepLinkHandler;
import com.nextcloud.client.files.downloader.FileDownloadHelper;
import com.nextcloud.client.files.downloader.FileDownloadWorker;
import com.nextcloud.client.media.PlayerServiceConnection;
import com.nextcloud.client.network.ClientFactory;
Expand Down Expand Up @@ -162,7 +161,6 @@
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Observer;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import kotlin.Unit;
Expand Down Expand Up @@ -1620,7 +1618,7 @@ public void onServiceConnected(ComponentName component, IBinder service) {
public void onServiceDisconnected(ComponentName component) {
if (component.equals(new ComponentName(FileDisplayActivity.this, FileDownloadWorker.class))) {
Log_OC.d(TAG, "Download service disconnected");
mDownloaderBinder = null;
fileDownloadProgressListener = null;
} else if (component.equals(new ComponentName(FileDisplayActivity.this, FileUploader.class))) {
Log_OC.d(TAG, "Upload service disconnected");
mUploaderBinder = null;
Expand Down
Loading

0 comments on commit 1ac6eae

Please sign in to comment.