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

Avoid checking for walled connection until necessary #10448

Merged
merged 4 commits into from
Jul 5, 2022
Merged
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
74 changes: 43 additions & 31 deletions app/src/main/java/com/nextcloud/client/jobs/OfflineSyncWork.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ package com.nextcloud.client.jobs

import android.content.ContentResolver
import android.content.Context
import android.os.PowerManager.WakeLock
import androidx.work.Worker
import androidx.work.WorkerParameters
import com.nextcloud.client.account.User
Expand Down Expand Up @@ -51,13 +50,10 @@ class OfflineSyncWork constructor(

companion object {
const val TAG = "OfflineSyncJob"
private const val WAKELOCK_TAG_SEPARATION = ":"
private const val WAKELOCK_ACQUISITION_TIMEOUT_MS = 10L * 60L * 1000L
}

override fun doWork(): Result {
val wakeLock: WakeLock? = null
if (!powerManagementService.isPowerSavingEnabled && !connectivityService.isInternetWalled) {
if (!powerManagementService.isPowerSavingEnabled) {
val users = userAccountManager.allUsers
for (user in users) {
val storageManager = FileDataStorageManager(user, contentResolver)
Expand All @@ -67,12 +63,10 @@ class OfflineSyncWork constructor(
}
recursive(File(ocRoot.storagePath), storageManager, user)
}
wakeLock?.release()
}
return Result.success()
}

@Suppress("ReturnCount", "ComplexMethod") // legacy code
private fun recursive(folder: File, storageManager: FileDataStorageManager, user: User) {
val downloadFolder = FileStorageUtils.getSavePath(user.accountName)
val folderName = folder.absolutePath.replaceFirst(downloadFolder.toRegex(), "") + OCFile.PATH_SEPARATOR
Expand All @@ -81,29 +75,9 @@ class OfflineSyncWork constructor(
if (folder.listFiles() == null) {
return
}
val ocFolder = storageManager.getFileByPath(folderName)
Log_OC.d(TAG, folderName + ": currentEtag: " + ocFolder.etag)
// check for etag change, if false, skip
val checkEtagOperation = CheckEtagRemoteOperation(
ocFolder.remotePath,
ocFolder.etagOnServer
)
val result = checkEtagOperation.execute(user, context)
when (result.code) {
ResultCode.ETAG_UNCHANGED -> {
Log_OC.d(TAG, "$folderName: eTag unchanged")
return
}
ResultCode.FILE_NOT_FOUND -> {
val removalResult = storageManager.removeFolder(ocFolder, true, true)
if (!removalResult) {
Log_OC.e(TAG, "removal of " + ocFolder.storagePath + " failed: file not found")
}
return
}
ResultCode.ETAG_CHANGED -> Log_OC.d(TAG, "$folderName: eTag changed")
else -> Log_OC.d(TAG, "$folderName: eTag changed")
}

val updatedEtag = checkEtagChanged(folderName, storageManager, user) ?: return

// iterate over downloaded files
val files = folder.listFiles { obj: File -> obj.isFile }
if (files != null) {
Expand All @@ -129,11 +103,49 @@ class OfflineSyncWork constructor(
// update eTag
@Suppress("TooGenericExceptionCaught") // legacy code
try {
val updatedEtag = result.data[0] as String
val ocFolder = storageManager.getFileByPath(folderName)
ocFolder.etagOnServer = updatedEtag
storageManager.saveFile(ocFolder)
} catch (e: Exception) {
Log_OC.e(TAG, "Failed to update etag on " + folder.absolutePath, e)
}
}

/**
* @return new etag if changed, `null` otherwise
*/
private fun checkEtagChanged(folderName: String, storageManager: FileDataStorageManager, user: User): String? {
val ocFolder = storageManager.getFileByPath(folderName)
Log_OC.d(TAG, folderName + ": currentEtag: " + ocFolder.etag)
// check for etag change, if false, skip
val checkEtagOperation = CheckEtagRemoteOperation(
ocFolder.remotePath,
ocFolder.etagOnServer
)
val result = checkEtagOperation.execute(user, context)
return when (result.code) {
ResultCode.ETAG_UNCHANGED -> {
Log_OC.d(TAG, "$folderName: eTag unchanged")
null
}
ResultCode.FILE_NOT_FOUND -> {
val removalResult = storageManager.removeFolder(ocFolder, true, true)
if (!removalResult) {
Log_OC.e(TAG, "removal of " + ocFolder.storagePath + " failed: file not found")
}
null
}
ResultCode.ETAG_CHANGED -> {
Log_OC.d(TAG, "$folderName: eTag changed")
result.data[0] as String
}
else -> if (connectivityService.isInternetWalled) {
Log_OC.d(TAG, "No connectivity, skipping sync")
null
} else {
Log_OC.d(TAG, "$folderName: eTag changed")
result.data[0] as String
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ public void uploadFile(String uploadKey) {
/**
* Convert current account to user. This is a temporary workaround until
* service is migrated to new user model.
*
*
* @return Optional {@link User}
*/
private Optional<User> getCurrentUser() {
Expand Down Expand Up @@ -1075,7 +1075,7 @@ public static void retryFailedUploads(
}

final Connectivity connectivity = connectivityService.getConnectivity();
final boolean gotNetwork = connectivity.isConnected() && !connectivityService.isInternetWalled();
final boolean gotNetwork = connectivity.isConnected();
final boolean gotWifi = connectivity.isWifi();
final BatteryStatus batteryStatus = powerManagementService.getBattery();
final boolean charging = batteryStatus.isCharging() || batteryStatus.isFull();
Expand All @@ -1094,7 +1094,8 @@ public static void retryFailedUploads(
failedUpload.setLastResult(UploadResult.FILE_NOT_FOUND);
uploadsStorageManager.updateUpload(failedUpload);
}
} else if (!isPowerSaving && gotNetwork && canUploadBeRetried(failedUpload, gotWifi, charging)) {
} else if (!isPowerSaving && gotNetwork &&
canUploadBeRetried(failedUpload, gotWifi, charging) && !connectivityService.isInternetWalled()) {
// 2B. for existing local files, try restarting it if possible
retryUpload(context, uploadUser.get(), failedUpload);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public static void restartJobsIfNeeded(final UploadsStorageManager uploadsStorag
}

new Thread(() -> {
if (connectivityService.getConnectivity().isConnected() && !connectivityService.isInternetWalled()) {
if (connectivityService.getConnectivity().isConnected()) {
FileUploader.retryFailedUploads(
context,
uploadsStorageManager,
Expand Down