Skip to content

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Dec 21, 2023
1 parent 256ab51 commit df0be0a
Show file tree
Hide file tree
Showing 7 changed files with 336 additions and 239 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ package com.nextcloud.client.files.downloader
import com.nextcloud.client.account.User
import com.nextcloud.client.jobs.BackgroundJobManager
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.operations.DownloadFileOperation
import com.owncloud.android.operations.DownloadType
import com.owncloud.android.utils.MimeTypeUtil
import java.io.File
import javax.inject.Inject

class FilesDownloadHelper {
Expand All @@ -41,6 +45,40 @@ class FilesDownloadHelper {
MainApp.getAppComponent().inject(this)
}

fun saveFile(
file: OCFile,
currentDownload: DownloadFileOperation?,
storageManager: FileDataStorageManager?
) {
val syncDate = System.currentTimeMillis()

file.apply {
lastSyncDateForProperties = syncDate
lastSyncDateForData = syncDate
isUpdateThumbnailNeeded = true
modificationTimestamp = currentDownload?.modificationTimestamp ?: 0L
modificationTimestampAtLastSyncForData = currentDownload?.modificationTimestamp ?: 0L
etag = currentDownload?.etag
mimeType = currentDownload?.mimeType
storagePath = currentDownload?.savePath

val savePathFile = currentDownload?.savePath?.let { File(it) }
savePathFile?.let {
fileLength = savePathFile.length()
}

remoteId = currentDownload?.file?.remoteId
}

storageManager?.saveFile(file)

if (MimeTypeUtil.isMedia(currentDownload?.mimeType)) {
FileDataStorageManager.triggerMediaScan(file.storagePath, file)
}

storageManager?.saveConflict(file, null)
}

fun downloadFile(user: User, ocFile: OCFile) {
backgroundJobManager.startFilesDownloadJob(
user,
Expand Down Expand Up @@ -89,6 +127,7 @@ class FilesDownloadHelper {
)
}

@Suppress("LongParameterList")
fun downloadFile(
user: User,
ocFile: OCFile,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Nextcloud Android client application
*
* @author Alper Ozturk
* Copyright (C) 2023 Alper Ozturk
* Copyright (C) 2023 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.nextcloud.client.files.downloader

import android.content.Context
import android.content.Intent
import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.operations.DownloadFileOperation
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.preview.PreviewImageActivity
import com.owncloud.android.ui.preview.PreviewImageFragment

class FilesDownloadIntents(private val context: Context) {

fun newDownloadIntent(
download: DownloadFileOperation,
linkedToRemotePath: String
): Intent {
return Intent(FilesDownloadWorker.getDownloadAddedMessage()).apply {
putExtra(FilesDownloadWorker.ACCOUNT_NAME, download.user.accountName)
putExtra(FilesDownloadWorker.EXTRA_REMOTE_PATH, download.remotePath)
putExtra(FilesDownloadWorker.EXTRA_LINKED_TO_PATH, linkedToRemotePath)
setPackage(context.packageName)
}
}

fun downloadFinishedIntent(
download: DownloadFileOperation,
downloadResult: RemoteOperationResult<*>,
unlinkedFromRemotePath: String?
): Intent {
return Intent(FilesDownloadWorker.getDownloadFinishMessage()).apply {
putExtra(FilesDownloadWorker.EXTRA_DOWNLOAD_RESULT, downloadResult.isSuccess)
putExtra(FilesDownloadWorker.ACCOUNT_NAME, download.user.accountName)
putExtra(FilesDownloadWorker.EXTRA_REMOTE_PATH, download.remotePath)
putExtra(OCFileListFragment.DOWNLOAD_BEHAVIOUR, download.behaviour)
putExtra(SendShareDialog.ACTIVITY_NAME, download.activityName)
putExtra(SendShareDialog.PACKAGE_NAME, download.packageName)
if (unlinkedFromRemotePath != null) {
putExtra(FilesDownloadWorker.EXTRA_LINKED_TO_PATH, unlinkedFromRemotePath)
}
setPackage(context.packageName)
}
}

fun detailsIntent(operation: DownloadFileOperation?): Intent {
return if (operation != null) {
if (PreviewImageFragment.canBePreviewed(operation.file)) {
Intent(context, PreviewImageActivity::class.java)
} else {
Intent(context, FileDisplayActivity::class.java)
}.apply {
putExtra(FileActivity.EXTRA_FILE, operation.file)
putExtra(FileActivity.EXTRA_USER, operation.user)
flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
}
} else {
Intent()
}
}
}
Loading

0 comments on commit df0be0a

Please sign in to comment.