Skip to content

Commit

Permalink
Fix code analytics
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 1ac6eae commit 172e9a7
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,64 +27,34 @@ 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 : OnDatatransferProgressListener {
class FileDownloadHelper {

@Inject
lateinit var backgroundJobManager: BackgroundJobManager

@Inject
lateinit var uploadsStorageManager: UploadsStorageManager

private val boundListeners: MutableMap<Long, OnDatatransferProgressListener> = HashMap()
private var currentDownload: DownloadFileOperation? = null
companion object {
private var instance: FileDownloadHelper? = 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)
fun instance(): FileDownloadHelper {
return if (instance == null) {
FileDownloadHelper()
} else {
instance!!
}
}
}

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
init {
MainApp.getAppComponent().inject(this)
}

fun isDownloading(user: User?, file: OCFile?): Boolean {
Expand Down Expand Up @@ -145,75 +115,25 @@ class FileDownloadHelper : OnDatatransferProgressListener {
storageManager?.saveConflict(file, null)
}

fun downloadFile(user: User, ocFile: OCFile) {
backgroundJobManager.startFileDownloadJob(
user,
ocFile,
"",
DownloadType.DOWNLOAD,
"",
"",
null
)
}

fun downloadFile(user: User, ocFile: OCFile, behaviour: String) {
backgroundJobManager.startFileDownloadJob(
user,
ocFile,
behaviour,
DownloadType.DOWNLOAD,
"",
"",
null
)
}

fun downloadFile(user: User, ocFile: OCFile, behaviour: String, packageName: String, activityName: String) {
backgroundJobManager.startFileDownloadJob(
user,
ocFile,
behaviour,
DownloadType.DOWNLOAD,
packageName,
packageName,
null
)
}

fun downloadFile(user: User, ocFile: OCFile, downloadType: DownloadType) {
backgroundJobManager.startFileDownloadJob(
user,
ocFile,
"",
downloadType,
"",
"",
null
)
fun downloadFileIfNotStartedBefore(user: User, file: OCFile) {
if (!isDownloading(user, file)) {
downloadFile(user, file, downloadType = DownloadType.DOWNLOAD)
}
}

fun downloadFile(user: User, ocFile: OCFile, conflictUploadId: Long) {
backgroundJobManager.startFileDownloadJob(
user,
ocFile,
"",
DownloadType.DOWNLOAD,
"",
"",
conflictUploadId
)
fun downloadFile(user: User, ocFile: OCFile) {
downloadFile(user, ocFile, downloadType = DownloadType.DOWNLOAD)
}

@Suppress("LongParameterList")
fun downloadFile(
user: User,
ocFile: OCFile,
behaviour: String,
downloadType: DownloadType?,
activityName: String,
packageName: String,
conflictUploadId: Long?
behaviour: String = "",
downloadType: DownloadType? = DownloadType.DOWNLOAD,
activityName: String = "",
packageName: String = "",
conflictUploadId: Long? = null
) {
backgroundJobManager.startFileDownloadJob(
user,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ class FileDownloadWorker(
private val pendingDownloads = IndexedForest<DownloadFileOperation>()
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
Expand Down Expand Up @@ -230,7 +229,7 @@ class FileDownloadWorker(
downloadResult = currentDownload?.execute(downloadClient)
if (downloadResult?.isSuccess == true && currentDownload?.downloadType === DownloadType.DOWNLOAD) {
getCurrentFile()?.let {
helper.saveFile(it, currentDownload, storageManager)
FileDownloadHelper.instance().saveFile(it, currentDownload, storageManager)
}
}
} catch (e: Exception) {
Expand Down Expand Up @@ -366,7 +365,7 @@ class FileDownloadWorker(
private val boundListeners: MutableMap<Long, OnDatatransferProgressListener> = HashMap()

fun isDownloading(user: User?, file: OCFile?): Boolean {
return helper.isDownloading(user, file)
return FileDownloadHelper.instance().isDownloading(user, file)
}

fun addDataTransferProgressListener(listener: OnDatatransferProgressListener?, file: OCFile?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ class FilesExportWork(
}

private fun downloadFile(ocFile: OCFile) {
FileDownloadHelper().downloadFile(
FileDownloadHelper.instance().downloadFile(
user,
ocFile,
DownloadType.EXPORT
downloadType = DownloadType.EXPORT
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,8 @@ private boolean anyFileSynchronizing() {
if (componentsGetter != null && !files.isEmpty() && user != null) {
OperationsServiceBinder opsBinder = componentsGetter.getOperationsServiceBinder();
FileUploaderBinder uploaderBinder = componentsGetter.getFileUploaderBinder();
FileDownloadHelper fileDownloadHelper = new FileDownloadHelper();
synchronizing = anyFileSynchronizing(opsBinder) || // comparing local and remote
anyFileDownloading(fileDownloadHelper) ||
anyFileDownloading() ||
anyFileUploading(uploaderBinder);
}
return synchronizing;
Expand All @@ -398,9 +397,9 @@ private boolean anyFileSynchronizing(OperationsServiceBinder opsBinder) {
return synchronizing;
}

private boolean anyFileDownloading(FileDownloadHelper downloadHelper) {
private boolean anyFileDownloading() {
for (OCFile file : files) {
if (downloadHelper.isDownloading(user, file)) {
if (FileDownloadHelper.Companion.instance().isDownloading(user, file)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,30 +309,19 @@ private void requestForUpload(OCFile file) {
mTransferWasRequested = true;
}


/**
* Requests for a download to the FileDownloader service
*
* @param file OCFile object representing the file to download
*/
private void requestForDownload(OCFile file) {
FileDownloadHelper downloadHelper = new FileDownloadHelper();

downloadHelper.downloadFile(
FileDownloadHelper.Companion.instance().downloadFile(
mUser,
file);

mTransferWasRequested = true;
}


public boolean transferWasRequested() {
return mTransferWasRequested;
}


public OCFile getLocalFile() {
return mLocalFile;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -445,14 +445,12 @@ private void syncContents() throws OperationCancelledException {
}

private void startDirectDownloads() throws OperationCancelledException {
FileDownloadHelper downloadHelper = new FileDownloadHelper();

for (OCFile file : mFilesForDirectDownload) {
synchronized(mCancellationRequested) {
if (mCancellationRequested.get()) {
throw new OperationCancelledException();
}
downloadHelper.downloadFile(user, file);
FileDownloadHelper.Companion.instance().downloadFile(user, file);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ class ConflictsResolveActivity : FileActivity(), OnConflictDecisionMadeListener
Decision.KEEP_SERVER -> if (!shouldDeleteLocal()) {
// Overwrite local file
file?.let {
FileDownloadHelper().downloadFile(
FileDownloadHelper.instance().downloadFile(
getUser().orElseThrow { RuntimeException() },
file,
conflictUploadId
conflictUploadId = conflictUploadId
)
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ public abstract class FileActivity extends DrawerActivity

private boolean mResumed;

protected FileDownloadHelper fileDownloadHelper = new FileDownloadHelper();
protected FileDownloadWorker.FileDownloadProgressListener fileDownloadProgressListener;
protected FileUploaderBinder mUploaderBinder;
private ServiceConnection mUploadServiceConnection;
Expand Down Expand Up @@ -235,7 +234,7 @@ protected void onCreate(Bundle savedInstanceState) {
Context.BIND_AUTO_CREATE);

if (user != null) {
new FileDownloadHelper().downloadFile(user, mFile);
FileDownloadHelper.Companion.instance().downloadFile(user, mFile);
}

mUploadServiceConnection = newTransferenceServiceConnection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
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 @@ -1896,9 +1897,7 @@ public void onTransferStateChanged(OCFile file, boolean downloading, boolean upl

private void requestForDownload() {
User user = getUser().orElseThrow(RuntimeException::new);
if (!fileDownloadHelper.isDownloading(user, mWaitingToPreview)) {
fileDownloadHelper.downloadFile(user, mWaitingToPreview);
}
FileDownloadHelper.Companion.instance().downloadFileIfNotStartedBefore(user, mWaitingToPreview);
}

@Override
Expand Down Expand Up @@ -1968,8 +1967,8 @@ public void run() {

private void requestForDownload(OCFile file, String downloadBehaviour, String packageName, String activityName) {
final User currentUser = getUser().orElseThrow(RuntimeException::new);
if (!fileDownloadHelper.isDownloading(currentUser, file)) {
fileDownloadHelper.downloadFile(currentUser, file, downloadBehaviour, DownloadType.DOWNLOAD, activityName, packageName, null);
if (!FileDownloadHelper.Companion.instance().isDownloading(currentUser, file)) {
FileDownloadHelper.Companion.instance().downloadFile(currentUser, file, downloadBehaviour, DownloadType.DOWNLOAD, activityName, packageName, null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.google.common.collect.Sets;
import com.nextcloud.client.account.User;
import com.nextcloud.client.account.UserAccountManager;
import com.nextcloud.client.files.downloader.FileDownloadHelper;
import com.nextcloud.client.jobs.BackgroundJobManager;
import com.nextcloud.client.onboarding.FirstRunActivity;
import com.nextcloud.java.util.Optional;
Expand Down Expand Up @@ -341,7 +342,7 @@ public void run(AccountManagerFuture<Boolean> future) {
mUploaderBinder.cancel(accountName);
}

fileDownloadHelper.cancelAllDownloadsForAccount(workerAccountName, workerCurrentDownload);
FileDownloadHelper.Companion.instance().cancelAllDownloadsForAccount(workerAccountName, workerCurrentDownload);
}

User currentUser = getUserAccountManager().getUser();
Expand Down Expand Up @@ -431,7 +432,7 @@ private void performAccountRemoval(User user) {
mUploaderBinder.cancel(user);
}

fileDownloadHelper.cancelAllDownloadsForAccount(workerAccountName, workerCurrentDownload);
FileDownloadHelper.Companion.instance().cancelAllDownloadsForAccount(workerAccountName, workerCurrentDownload);

backgroundJobManager.startAccountRemovalJob(user.getAccountName(), false);

Expand Down
Loading

0 comments on commit 172e9a7

Please sign in to comment.