Skip to content

Commit

Permalink
Merge pull request #13918 from nextcloud/backport/13884/stable-3.30
Browse files Browse the repository at this point in the history
[stable-3.30] Improve Two Way Sync Behaviour
  • Loading branch information
tobiasKaminsky authored Oct 31, 2024
2 parents bf0a150 + 6ed5be9 commit b4a68cf
Show file tree
Hide file tree
Showing 16 changed files with 280 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ class BackgroundJobFactory @Inject constructor(
params,
accountManager,
powerManagementService,
connectivityService
connectivityService,
preferences
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,5 @@ interface BackgroundJobManager {
fun schedulePeriodicHealthStatus()
fun startHealthStatus()
fun bothFilesSyncJobsRunning(syncedFolderID: Long): Boolean
fun scheduleInternal2WaySync()
fun scheduleInternal2WaySync(intervalMinutes: Long)
}
Original file line number Diff line number Diff line change
Expand Up @@ -654,12 +654,13 @@ internal class BackgroundJobManagerImpl(
)
}

override fun scheduleInternal2WaySync() {
override fun scheduleInternal2WaySync(intervalMinutes: Long) {
val request = periodicRequestBuilder(
jobClass = InternalTwoWaySyncWork::class,
jobName = JOB_INTERNAL_TWO_WAY_SYNC
jobName = JOB_INTERNAL_TWO_WAY_SYNC,
intervalMins = intervalMinutes
).build()

workManager.enqueueUniquePeriodicWork(JOB_INTERNAL_TWO_WAY_SYNC, ExistingPeriodicWorkPolicy.KEEP, request)
workManager.enqueueUniquePeriodicWork(JOB_INTERNAL_TWO_WAY_SYNC, ExistingPeriodicWorkPolicy.UPDATE, request)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.work.WorkerParameters
import com.nextcloud.client.account.UserAccountManager
import com.nextcloud.client.device.PowerManagementService
import com.nextcloud.client.network.ConnectivityService
import com.nextcloud.client.preferences.AppPreferences
import com.owncloud.android.MainApp
import com.owncloud.android.datamodel.FileDataStorageManager
import com.owncloud.android.datamodel.OCFile
Expand All @@ -21,13 +22,14 @@ import com.owncloud.android.operations.SynchronizeFolderOperation
import com.owncloud.android.utils.FileStorageUtils
import java.io.File

@Suppress("Detekt.NestedBlockDepth", "ReturnCount")
@Suppress("Detekt.NestedBlockDepth", "ReturnCount", "LongParameterList")
class InternalTwoWaySyncWork(
private val context: Context,
params: WorkerParameters,
private val userAccountManager: UserAccountManager,
private val powerManagementService: PowerManagementService,
private val connectivityService: ConnectivityService
private val connectivityService: ConnectivityService,
private val appPreferences: AppPreferences
) : Worker(context, params) {
private var shouldRun = true

Expand All @@ -36,7 +38,9 @@ class InternalTwoWaySyncWork(

var result = true

if (powerManagementService.isPowerSavingEnabled ||
@Suppress("ComplexCondition")
if (!appPreferences.isTwoWaySyncEnabled ||
powerManagementService.isPowerSavingEnabled ||
!connectivityService.isConnected ||
connectivityService.isInternetWalled ||
!connectivityService.connectivity.isWifi
Expand All @@ -61,13 +65,6 @@ class InternalTwoWaySyncWork(
return checkFreeSpaceResult
}

// do not attempt to sync root folder
if (folder.remotePath == OCFile.ROOT_PATH) {
folder.internalFolderSyncTimestamp = -1L
fileDataStorageManager.saveFile(folder)
continue
}

Log_OC.d(TAG, "Folder ${folder.remotePath}: started!")
val operation = SynchronizeFolderOperation(context, folder.remotePath, user, fileDataStorageManager)
.execute(context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,4 +391,10 @@ default void onDarkThemeModeChanged(DarkMode mode) {

@NonNull
String getLastSelectedMediaFolder();

void setTwoWaySyncStatus(boolean value);
boolean isTwoWaySyncEnabled();

void setTwoWaySyncInterval(Long value);
Long getTwoWaySyncInterval();
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ public final class AppPreferencesImpl implements AppPreferences {
private static final String PREF__STORAGE_PERMISSION_REQUESTED = "storage_permission_requested";
private static final String PREF__IN_APP_REVIEW_DATA = "in_app_review_data";

private static final String PREF__TWO_WAY_STATUS = "two_way_sync_status";
private static final String PREF__TWO_WAY_SYNC_INTERVAL = "two_way_sync_interval";

private static final String LOG_ENTRY = "log_entry";

private final Context context;
Expand Down Expand Up @@ -789,4 +792,24 @@ public void setLastSelectedMediaFolder(@NonNull String path) {
public String getLastSelectedMediaFolder() {
return preferences.getString(PREF__MEDIA_FOLDER_LAST_PATH, OCFile.ROOT_PATH);
}

@Override
public void setTwoWaySyncStatus(boolean value) {
preferences.edit().putBoolean(PREF__TWO_WAY_STATUS, value).apply();
}

@Override
public boolean isTwoWaySyncEnabled() {
return preferences.getBoolean(PREF__TWO_WAY_STATUS, true);
}

@Override
public void setTwoWaySyncInterval(Long value) {
preferences.edit().putLong(PREF__TWO_WAY_SYNC_INTERVAL, value).apply();
}

@Override
public Long getTwoWaySyncInterval() {
return preferences.getLong(PREF__TWO_WAY_SYNC_INTERVAL, 15L);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ import android.os.Handler
import android.os.Looper
import android.widget.Toast
import com.google.common.io.Resources
import com.owncloud.android.R
import com.owncloud.android.datamodel.ReceiverFlag

fun Context.hourPlural(hour: Int): String = resources.getQuantityString(R.plurals.hours, hour, hour)

fun Context.minPlural(min: Int): String = resources.getQuantityString(R.plurals.minutes, min, min)

@SuppressLint("UnspecifiedRegisterReceiverFlag")
fun Context.registerBroadcastReceiver(receiver: BroadcastReceiver?, filter: IntentFilter, flag: ReceiverFlag): Intent? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import android.util.TypedValue
import android.view.View
import android.view.ViewOutlineProvider

fun View?.setVisibleIf(condition: Boolean) {
if (this == null) return
visibility = if (condition) View.VISIBLE else View.GONE
}

fun createRoundedOutline(context: Context, cornerRadiusValue: Float): ViewOutlineProvider {
return object : ViewOutlineProvider() {
override fun getOutline(view: View, outline: Outline) {
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/com/owncloud/android/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,10 @@ public void onCreate() {
backgroundJobManager.scheduleMediaFoldersDetectionJob();
backgroundJobManager.startMediaFoldersDetectionJob();
backgroundJobManager.schedulePeriodicHealthStatus();
backgroundJobManager.scheduleInternal2WaySync();

if (preferences.isTwoWaySyncEnabled()) {
backgroundJobManager.scheduleInternal2WaySync(preferences.getTwoWaySyncInterval());
}
}

registerGlobalPassCodeProtection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2493,7 +2493,10 @@ public List<OCFile> getInternalTwoWaySyncFolders(User user) {
List<OCFile> files = new ArrayList<>(fileEntities.size());

for (FileEntity fileEntity : fileEntities) {
files.add(createFileInstance(fileEntity));
OCFile file = createFileInstance(fileEntity);
if (file.isFolder() && !file.isRootDirectory()) {
files.add(file);
}
}

return files;
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/owncloud/android/datamodel/OCFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,10 @@ public boolean isSharedWithSharee() {
return this.sharedWithSharee;
}

public boolean isRootDirectory() {
return ROOT_PATH.equals(decryptedRemotePath);
}

public boolean isFavorite() {
return this.favorite;
}
Expand Down
Loading

0 comments on commit b4a68cf

Please sign in to comment.