123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- /*
- *
- * Nextcloud Android client application
- *
- * @author Tobias Kaminsky
- * Copyright (C) 2022 Tobias Kaminsky
- * Copyright (C) 2022 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.owncloud.android.utils
- import com.nextcloud.client.account.User
- import com.nextcloud.client.jobs.BackgroundJobManager
- import com.owncloud.android.MainApp
- import com.owncloud.android.datamodel.OCFile
- import com.owncloud.android.datamodel.UploadsStorageManager
- import com.owncloud.android.db.OCUpload
- import com.owncloud.android.files.services.FileUploader.FileUploaderBinder
- import com.owncloud.android.files.services.NameCollisionPolicy
- import com.owncloud.android.lib.common.network.OnDatatransferProgressListener
- import com.owncloud.android.lib.common.utils.Log_OC
- import javax.inject.Inject
- class FilesUploadHelper {
- @Inject
- lateinit var backgroundJobManager: BackgroundJobManager
- @Inject
- lateinit var uploadsStorageManager: UploadsStorageManager
- init {
- MainApp.getAppComponent().inject(this)
- }
- @Suppress("LongParameterList")
- fun uploadNewFiles(
- user: User,
- localPaths: Array<String>,
- remotePaths: Array<String>,
- createRemoteFolder: Boolean,
- createdBy: Int,
- requiresWifi: Boolean,
- requiresCharging: Boolean,
- nameCollisionPolicy: NameCollisionPolicy,
- localBehavior: Int
- ) {
- val uploads = localPaths.mapIndexed { index, localPath ->
- OCUpload(localPath, remotePaths[index], user.accountName).apply {
- this.nameCollisionPolicy = nameCollisionPolicy
- isUseWifiOnly = requiresWifi
- isWhileChargingOnly = requiresCharging
- uploadStatus = UploadsStorageManager.UploadStatus.UPLOAD_IN_PROGRESS
- this.createdBy = createdBy
- isCreateRemoteFolder = createRemoteFolder
- localAction = localBehavior
- }
- }
- uploadsStorageManager.storeUploads(uploads)
- backgroundJobManager.startFilesUploadJob(user)
- }
- fun cancelFileUpload(remotePath: String, user: User) {
- // need to update now table in mUploadsStorageManager,
- // since the operation will not get to be run by FileUploader#uploadFile
- uploadsStorageManager.removeUpload(user.accountName, remotePath)
- restartUploadJob(user)
- }
- fun restartUploadJob(user: User) {
- backgroundJobManager.cancelFilesUploadJob(user)
- backgroundJobManager.startFilesUploadJob(user)
- }
- fun uploadUpdatedFile(
- user: User,
- existingFiles: Array<OCFile>,
- behaviour: Int,
- nameCollisionPolicy: NameCollisionPolicy
- ) {
- Log_OC.d(this, "upload updated file")
- val uploads = existingFiles.map { file ->
- OCUpload(file, user).apply {
- fileSize = file.fileLength
- this.nameCollisionPolicy = nameCollisionPolicy
- isCreateRemoteFolder = true
- this.localAction = behaviour
- isUseWifiOnly = false
- isWhileChargingOnly = false
- uploadStatus = UploadsStorageManager.UploadStatus.UPLOAD_IN_PROGRESS
- }
- }
- uploadsStorageManager.storeUploads(uploads)
- backgroundJobManager.startFilesUploadJob(user)
- }
- fun retryUpload(upload: OCUpload, user: User) {
- Log_OC.d(this, "retry upload")
- upload.uploadStatus = UploadsStorageManager.UploadStatus.UPLOAD_IN_PROGRESS
- uploadsStorageManager.updateUpload(upload)
- backgroundJobManager.startFilesUploadJob(user)
- }
- fun addDatatransferProgressListener(
- listener: OnDatatransferProgressListener,
- targetKey: String
- ) {
- mBoundListeners[targetKey] = listener
- }
- fun removeDatatransferProgressListener(
- listener: OnDatatransferProgressListener,
- targetKey: String
- ) {
- if (mBoundListeners[targetKey] === listener) {
- mBoundListeners.remove(targetKey)
- }
- }
- companion object Progress {
- val mBoundListeners = HashMap<String, OnDatatransferProgressListener>()
- fun onTransferProgress(
- accountName: String?,
- remotePath: String?,
- progressRate: Long,
- totalTransferredSoFar: Long,
- totalToTransfer: Long,
- fileName: String?
- ) {
- if (accountName == null || remotePath == null) return
- val key: String =
- FileUploaderBinder.buildRemoteName(accountName, remotePath)
- val boundListener = mBoundListeners[key]
- boundListener?.onTransferProgress(progressRate, totalTransferredSoFar, totalToTransfer, fileName)
- Log_OC.d("TAG", "Hello")
- }
- }
- }
|