Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop Uploads When the Device Doesn’t Have an Internet Connection #14364

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,38 @@ class FileUploadWorker(
return Result.success()
}

private fun canExitEarly(): Boolean {
val result = !connectivityService.isConnected ||
connectivityService.isInternetWalled ||
isStopped ||
isAirplaneModeEnabled()

if (!result) {
notificationManager.dismissErrorNotification()
}

return result
}

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,20 @@ class UploadNotificationManager(private val context: Context, viewThemeUtils: Vi
)
}

fun showConnectionErrorNotification() {
notificationManager.cancel(getId())

notificationBuilder.run {
setContentTitle(context.getString(R.string.file_upload_worker_error_notification_title))
setContentText("")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it retry once a network connection is detected?
If so, we can add this info here as text.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the FileUploadWorker is triggered for auto-upload, it will run again. However, if it is triggered for manual file upload, it will not. We could indicate this detail, but it might be too much information for the user.

}

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

fun dismissOldErrorNotification(operation: UploadFileOperation?) {
if (operation == null) {
return
Expand All @@ -155,6 +169,8 @@ class UploadNotificationManager(private val context: Context, viewThemeUtils: Vi
}
}

fun dismissErrorNotification() = notificationManager.cancel(FileUploadWorker.NOTIFICATION_ERROR_ID)

fun dismissOldErrorNotification(remotePath: String, localPath: String) {
notificationManager.cancel(
NotificationUtils.createUploadNotificationTag(remotePath, localPath),
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 @@ -196,6 +196,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
Loading