Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Jan 21, 2025
1 parent d825d37 commit 4ae3b5e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -584,9 +584,14 @@ internal class BackgroundJobManagerImpl(

val tag = startFileUploadJobTag(user)

val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()

val request = oneTimeRequestBuilder(FileUploadWorker::class, JOB_FILES_UPLOAD, user)
.addTag(tag)
.setInputData(data)
.setConstraints(constraints)
.build()

workManager.enqueueUniqueWork(tag, ExistingWorkPolicy.KEEP, request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package com.nextcloud.client.jobs.upload

import android.app.PendingIntent
import android.content.Context
import android.provider.Settings
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.work.Worker
import androidx.work.WorkerParameters
Expand Down Expand Up @@ -148,6 +149,12 @@ class FileUploadWorker(
return Result.success()
}

if (canExitEarly()) {
Log_OC.d(TAG, "Airplane mode is enabled or no internet connection, stopping worker.")
notificationManager.showConnectionErrorNotification()
return Result.failure()
}

Log_OC.d(TAG, "Handling ${uploadsPerPage.size} uploads for account $accountName")
val lastId = uploadsPerPage.last().uploadId
uploadFiles(totalUploadSize, uploadsPerPage, accountName)
Expand All @@ -163,14 +170,32 @@ class FileUploadWorker(
return Result.success()
}

private fun canExitEarly(): Boolean {
return !connectivityService.isConnected ||
connectivityService.isInternetWalled ||
isStopped ||
isAirplaneModeEnabled()
}

private fun isAirplaneModeEnabled(): Boolean {
return Settings.Global.getInt(context.contentResolver, Settings.Global.AIRPLANE_MODE_ON, 0) != 0
}

@Suppress("NestedBlockDepth")
private fun uploadFiles(totalUploadSize: Int, uploadsPerPage: List<OCUpload>, accountName: String) {
val user = userAccountManager.getUser(accountName)
setWorkerState(user.get(), uploadsPerPage)

if (canExitEarly()) {
Log_OC.d(TAG, "Airplane mode is enabled or no internet connection, stopping worker.")
notificationManager.showConnectionErrorNotification()
return
}

run uploads@{
uploadsPerPage.forEach { upload ->
if (isStopped) {
if (canExitEarly()) {
notificationManager.showConnectionErrorNotification()
return@uploads
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ class UploadNotificationManager(private val context: Context, viewThemeUtils: Vi
)
}

fun showConnectionErrorNotification() {
notificationBuilder.run {
setContentTitle(context.getString(R.string.file_upload_worker_error_notification_title))
setContentText("")
}

notificationManager.notify(
FileUploadWorker.NOTIFICATION_ERROR_ID,
notificationBuilder.build()
)
}

fun dismissOldErrorNotification(operation: UploadFileOperation?) {
if (operation == null) {
return
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@
<string name="upload_list_empty_headline">No uploads available</string>
<string name="upload_list_empty_text_auto_upload">Upload some content or activate auto upload.</string>
<string name="file_list_folder">folder</string>
<string name="file_upload_worker_error_notification_title">Upload failed. No internet connection</string>
<string name="filedetails_download">Download</string>
<string name="filedetails_sync_file">Sync</string>
<string name="filedetails_renamed_in_upload_msg">File renamed %1$s during upload</string>
Expand Down

0 comments on commit 4ae3b5e

Please sign in to comment.