…lines 1–18 unchanged (18 lines)
19import com.nextcloud.client.device.PowerManagementService
20import com.nextcloud.client.jobs.BackgroundJobManager
21import com.nextcloud.client.jobs.BackgroundJobManagerImpl
22import com.nextcloud.client.jobs.utils.UploadErrorNotificationManager
23import com.nextcloud.client.network.ConnectivityService
24import com.nextcloud.client.preferences.AppPreferences
25import com.nextcloud.model.WorkerState
26import com.nextcloud.model.WorkerStateObserver
27import com.nextcloud.utils.ForegroundServiceHelper
28import com.nextcloud.utils.extensions.getPercent
29import com.nextcloud.utils.extensions.updateStatus
30import com.owncloud.android.R
31import com.owncloud.android.datamodel.FileDataStorageManager
32import com.owncloud.android.datamodel.ForegroundServiceType
…lines 33–73 unchanged (41 lines)
74 const val CURRENT_BATCH_INDEX = "batch_index"
75 const val TOTAL_UPLOAD_SIZE = "total_upload_size"
76 const val SHOW_SAME_FILE_ALREADY_EXISTS_NOTIFICATION = "show_same_file_already_exists_notification"
77
78 var currentUploadFileOperation: UploadFileOperation? = null
79
80 private const val UPLOADS_ADDED_MESSAGE = "UPLOADS_ADDED"
81 private const val UPLOAD_START_MESSAGE = "UPLOAD_START"
82 private const val UPLOAD_FINISH_MESSAGE = "UPLOAD_FINISH"
83
84 private const val BATCH_SIZE = 100
85
86 const val EXTRA_UPLOAD_RESULT = "RESULT"
87 const val EXTRA_REMOTE_PATH = "REMOTE_PATH"
88 const val EXTRA_OLD_REMOTE_PATH = "OLD_REMOTE_PATH"
…lines 89–92 unchanged (4 lines)
93 const val ACTION_CANCEL_BROADCAST = "CANCEL"
94 const val LOCAL_BEHAVIOUR_COPY = 0
95 const val LOCAL_BEHAVIOUR_MOVE = 1
96 const val LOCAL_BEHAVIOUR_FORGET = 2
97 const val LOCAL_BEHAVIOUR_DELETE = 3
98
99 fun getUploadsAddedMessage(): String = FileUploadWorker::class.java.name + UPLOADS_ADDED_MESSAGE
100
101 fun getUploadStartMessage(): String = FileUploadWorker::class.java.name + UPLOAD_START_MESSAGE
102
103 fun getUploadFinishMessage(): String = FileUploadWorker::class.java.name + UPLOAD_FINISH_MESSAGE
104
105 fun cancelCurrentUpload(remotePath: String, accountName: String, onCompleted: () -> Unit) {
106 currentUploadFileOperation?.let {
107 if (it.remotePath == remotePath && it.user.accountName == accountName) {
108 it.cancel(ResultCode.USER_CANCELLED)
109 onCompleted()
…lines 110–139 unchanged (30 lines)
140
141 trySetForeground()
142
143 val result = uploadFiles()
144 backgroundJobManager.logEndOfWorker(workerName, result)
145 notificationManager.dismissNotification()
146 if (result == Result.success()) {
147 setIdleWorkerState()
148 }
149 result
150 } catch (t: Throwable) {
151 Log_OC.e(TAG, "Error caught at FileUploadWorker $t")
152 cleanup()
153 Result.failure()
154 }
…lines 155–197 unchanged (43 lines)
198 .setSilent(true)
199 .build()
200
201 private fun cleanup() {
202 Log_OC.e(TAG, "FileUploadWorker stopped")
203
204 setIdleWorkerState()
205 currentUploadFileOperation?.cancel(null)
206 notificationManager.dismissNotification()
207 }
208
209 private fun setIdleWorkerState() {
210 WorkerStateObserver.send(WorkerState.FileUploadCompleted(currentUploadFileOperation?.file))
211 }
212
213 @Suppress("ReturnCount", "LongMethod", "DEPRECATION")
214 private suspend fun uploadFiles(): Result = withContext(Dispatchers.IO) {
215 val accountName = inputData.getString(ACCOUNT)
216 if (accountName == null) {
217 Log_OC.e(TAG, "accountName is null")
…lines 218–260 unchanged (43 lines)
261 }
262
263 if (canExitEarly()) {
264 notificationManager.showConnectionErrorNotification()
265 return@withContext Result.failure()
266 }
267
268 val operation = createUploadFileOperation(upload, user)
269 currentUploadFileOperation = operation
270 fileUploadBroadcastManager.sendStarted(operation, context)
271
272 val currentIndex = (index + 1)
273 val currentUploadIndex = (currentIndex + previouslyUploadedFileSize)
…lines 274–303 unchanged (30 lines)
304 result: RemoteOperationResult<*>
305 ) {
306 val shouldBroadcast =
307 (totalUploadSize > BATCH_SIZE && currentUploadIndex > 0) && currentUploadIndex % BATCH_SIZE == 0
308
309 if (shouldBroadcast) {
310 // delay broadcast
311 fileUploadBroadcastManager.sendFinished(
312 operation,
313 result,
314 operation.oldFile?.storagePath,
315 context
316 )
…lines 317–439 unchanged (123 lines)