Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 897b685c70 | |||
| 4b20697bb1 | |||
| 66d28761a8 |
@@ -146,13 +146,15 @@ class SyncEngine @Inject constructor(
|
||||
storeLocalMtime = false))
|
||||
}
|
||||
SyncDecision.DELETE_LOCAL -> {
|
||||
accessor.delete(rel)
|
||||
val deleted = accessor.delete(rel)
|
||||
if (!deleted) Timber.w("SyncEngine: DELETE_LOCAL failed (silent) for $rel")
|
||||
fileStateDao.delete(pair.id, rel)
|
||||
logEvent(pair.id, SyncEventType.FILE_DELETED, rel, "local", 0)
|
||||
FileOutcome(deleted = 1)
|
||||
}
|
||||
SyncDecision.DELETE_REMOTE -> {
|
||||
provider.deleteFile("${pair.remotePath}/$rel")
|
||||
runCatching { provider.deleteFile("${pair.remotePath}/$rel") }
|
||||
.onFailure { e -> Timber.e(e, "SyncEngine: DELETE_REMOTE failed for $rel") }
|
||||
fileStateDao.delete(pair.id, rel)
|
||||
logEvent(pair.id, SyncEventType.FILE_DELETED, rel, "remote", 0)
|
||||
FileOutcome(deleted = 1)
|
||||
@@ -283,21 +285,15 @@ internal fun syncDecide(
|
||||
}
|
||||
|
||||
!localExists && remoteExists -> when {
|
||||
known == null -> if (!hasPriorSyncState) {
|
||||
// Initial sync: no history at all — remote files are new, download them.
|
||||
known == null -> {
|
||||
// No state record: could be a new remote file OR a file whose state was lost.
|
||||
// Downloading is always safer than deleting — if the user deleted the local
|
||||
// copy intentionally, the state record will still exist (known != null) and
|
||||
// the else-branch below correctly deletes the remote copy.
|
||||
when (direction) {
|
||||
SyncDirection.DOWNLOAD_ONLY, SyncDirection.TWO_WAY -> SyncDecision.DOWNLOAD
|
||||
else -> SyncDecision.SKIP
|
||||
}
|
||||
} else {
|
||||
// Pair has been synced before but this file has no state record
|
||||
// (e.g. uploaded before state-tracking was fixed). Treat the same
|
||||
// as a known remote-deletion: apply mirror/keep behavior.
|
||||
when {
|
||||
deleteBehavior == DeleteBehavior.KEEP -> SyncDecision.SKIP
|
||||
direction == SyncDirection.UPLOAD_ONLY || direction == SyncDirection.TWO_WAY -> SyncDecision.DELETE_REMOTE
|
||||
else -> SyncDecision.SKIP
|
||||
}
|
||||
}
|
||||
else -> when {
|
||||
deleteBehavior == DeleteBehavior.KEEP -> SyncDecision.SKIP
|
||||
|
||||
@@ -20,7 +20,9 @@ import com.syncflow.MainActivity
|
||||
import com.syncflow.R
|
||||
import com.syncflow.data.db.SyncFileStateDao
|
||||
import com.syncflow.data.db.SyncPairDao
|
||||
import com.syncflow.data.db.entities.toDomain
|
||||
import com.syncflow.domain.model.ScheduleType
|
||||
import com.syncflow.domain.sync.LocalAccessor
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
@@ -44,8 +46,10 @@ class FileWatchService : Service() {
|
||||
private val fileObservers = mutableMapOf<Long, MutableList<FileObserver>>()
|
||||
private val contentObservers = mutableMapOf<Long, ContentObserver>()
|
||||
private val debounceJobs = mutableMapOf<Long, Job>()
|
||||
// After a watcher-triggered sync completes, suppress FileObserver events for this long
|
||||
// to stop the feedback loop: sync writes files → FileObserver fires → another sync → repeat.
|
||||
// Persistent monitors that watch WorkManager for ANY sync (manual, catchup, onchange)
|
||||
// so the cooldown is set regardless of who triggered the sync.
|
||||
private val syncMonitorJobs = mutableMapOf<Long, Job>()
|
||||
// After a sync completes, suppress FileObserver events for this long.
|
||||
private val syncCooldownUntil = mutableMapOf<Long, Long>()
|
||||
|
||||
companion object {
|
||||
@@ -149,11 +153,41 @@ class FileWatchService : Service() {
|
||||
return
|
||||
}
|
||||
fileObservers[pairId] = mutableListOf()
|
||||
// Set startup cooldown BEFORE registering watchers so inotify events that fire
|
||||
// immediately on registration don't trigger the debounce before catchupScan runs.
|
||||
syncCooldownUntil[pairId] = System.currentTimeMillis() + 15_000
|
||||
watchDirRecursive(dir, pairId, wifiOnly, chargingOnly)
|
||||
Timber.d("FileWatchService: watching pair $pairId at $path (${fileObservers[pairId]?.size} dirs)")
|
||||
startSyncMonitor(pairId)
|
||||
scope.launch { catchupScan(pairId, dir, wifiOnly, chargingOnly) }
|
||||
}
|
||||
|
||||
// Watches WorkManager for ANY sync tagged sync_$pairId (manual, catchup, onchange).
|
||||
// Sets cooldown while running and for 60s after, so FileObserver events from our
|
||||
// own file writes never trigger a re-sync regardless of what started the sync.
|
||||
private fun startSyncMonitor(pairId: Long) {
|
||||
syncMonitorJobs[pairId]?.cancel()
|
||||
syncMonitorJobs[pairId] = scope.launch {
|
||||
var wasSyncing = false
|
||||
WorkManager.getInstance(applicationContext)
|
||||
.getWorkInfosByTagFlow("sync_$pairId")
|
||||
.collect { infos ->
|
||||
val isSyncing = infos.any {
|
||||
it.state == WorkInfo.State.RUNNING || it.state == WorkInfo.State.ENQUEUED
|
||||
}
|
||||
if (isSyncing) {
|
||||
Timber.d("FileWatchService: sync active for pair $pairId — cooldown extended")
|
||||
syncCooldownUntil[pairId] = System.currentTimeMillis() + 120_000
|
||||
wasSyncing = true
|
||||
} else if (wasSyncing) {
|
||||
Timber.d("FileWatchService: sync finished for pair $pairId — 60s settle cooldown")
|
||||
syncCooldownUntil[pairId] = System.currentTimeMillis() + 60_000
|
||||
wasSyncing = false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun watchDirRecursive(dir: File, pairId: Long, wifiOnly: Boolean, chargingOnly: Boolean) {
|
||||
if (!dir.isDirectory) return
|
||||
val mask = FileObserver.CREATE or FileObserver.DELETE or FileObserver.MODIFY or
|
||||
@@ -192,22 +226,29 @@ class FileWatchService : Service() {
|
||||
val known = fileStateDao.getForPair(pairId).associateBy { it.relativePath }
|
||||
if (known.isEmpty()) return // Never synced — first sync will be triggered manually
|
||||
|
||||
val current = mutableMapOf<String, Long>()
|
||||
dir.walk().filter { it.isFile }.forEach { f ->
|
||||
current[f.relativeTo(dir).path.replace('\\', '/')] = f.lastModified()
|
||||
}
|
||||
val pairEntity = syncPairDao.getById(pairId) ?: return
|
||||
val pair = pairEntity.toDomain()
|
||||
// Use the same accessor + filters as SyncEngine so hidden/excluded/size-filtered files
|
||||
// don't appear as "new" in the catchup scan and trigger a perpetual sync loop.
|
||||
val accessor = if (pair.localPath.startsWith("content://"))
|
||||
LocalAccessor.Saf(Uri.parse(pair.localPath), contentResolver)
|
||||
else
|
||||
LocalAccessor.JavaFile(dir)
|
||||
val current = accessor.walkFiles(pair)
|
||||
|
||||
val hasNew = current.any { (rel, _) -> rel !in known }
|
||||
val hasModified = current.any { (rel, mtime) ->
|
||||
val hasModified = current.any { (rel, info) ->
|
||||
val s = known[rel]; s != null && s.localModifiedAt != null &&
|
||||
s.localModifiedAt.toEpochMilli() != mtime
|
||||
s.localModifiedAt.epochSecond != info.lastModifiedMs / 1000
|
||||
}
|
||||
val hasDeleted = known.keys.any { rel -> rel !in current }
|
||||
|
||||
if (hasNew || hasModified || hasDeleted) {
|
||||
Timber.d("FileWatchService: catchup detected changes for pair $pairId, scheduling sync")
|
||||
val pair = syncPairDao.getById(pairId) ?: return
|
||||
// Set cooldown so file writes during this sync don't immediately re-trigger
|
||||
// Cancel any debounce that started before our startup cooldown was set
|
||||
debounceJobs[pairId]?.cancel()
|
||||
debounceJobs.remove(pairId)
|
||||
// Hold cooldown for duration of sync + 60s settle
|
||||
syncCooldownUntil[pairId] = System.currentTimeMillis() + 120_000
|
||||
val req = SyncWorker.buildOneTimeRequest(pairId, wifiOnly, chargingOnly)
|
||||
WorkManager.getInstance(applicationContext)
|
||||
@@ -238,6 +279,12 @@ class FileWatchService : Service() {
|
||||
debounceJobs[pairId]?.cancel()
|
||||
debounceJobs[pairId] = scope.launch {
|
||||
delay(5_000)
|
||||
// Re-check: catchupScan or another path may have already set a cooldown
|
||||
// and handled this sync while we were waiting.
|
||||
if (System.currentTimeMillis() < (syncCooldownUntil[pairId] ?: 0L)) {
|
||||
Timber.d("FileWatchService: debounce fired but cooldown active for pair $pairId, skipping")
|
||||
return@launch
|
||||
}
|
||||
val pair = syncPairDao.getById(pairId)
|
||||
if (pair == null || !pair.isEnabled) return@launch
|
||||
Timber.d("FileWatchService: triggering sync for pair $pairId after debounce")
|
||||
@@ -285,6 +332,8 @@ class FileWatchService : Service() {
|
||||
contentObservers.clear()
|
||||
debounceJobs.values.forEach { it.cancel() }
|
||||
debounceJobs.clear()
|
||||
syncMonitorJobs.values.forEach { it.cancel() }
|
||||
syncMonitorJobs.clear()
|
||||
syncCooldownUntil.clear()
|
||||
}
|
||||
|
||||
|
||||
@@ -6,71 +6,79 @@
|
||||
android:viewportHeight="108">
|
||||
|
||||
<!--
|
||||
Four interlocked ring arcs (blue/red/green/orange), each a thick rounded band
|
||||
arranged in a 2x2 offset so they interweave. A white sync-arrow circle sits
|
||||
at the center. Designed to match the braided-knot reference icon.
|
||||
Four thick arcs arranged as an interlocked pinwheel.
|
||||
Each arc sweeps ~210 degrees, rounded caps, radius 18 from center (54,54).
|
||||
Draw order creates natural over/under at the four crossing points:
|
||||
blue under green, green under red, red under orange, orange under blue (re-draw blue tip).
|
||||
|
||||
Arc endpoints computed at radius 18, sweep 210 deg clockwise:
|
||||
start angle end angle start point end point
|
||||
270 (top) 120 (54, 36) (45, 70)
|
||||
0 (right) 210 (72, 54) (39, 45)
|
||||
90 (bot) 300 (54, 72) (63, 38)
|
||||
180 (left) 390=30 (36, 54) (69, 63)
|
||||
-->
|
||||
|
||||
<!-- Blue ring - top-left -->
|
||||
<!-- Blue — starts at top, sweeps clockwise to lower-left -->
|
||||
<path
|
||||
android:strokeColor="#2979FF"
|
||||
android:strokeWidth="7"
|
||||
android:strokeWidth="8.5"
|
||||
android:fillColor="#00000000"
|
||||
android:strokeLineCap="round"
|
||||
android:pathData="M 38,36
|
||||
A 16,16 0 1,1 54,52
|
||||
A 16,16 0 1,1 38,36 Z"/>
|
||||
android:pathData="M 54,36 A 18,18 0 1,1 45,70"/>
|
||||
|
||||
<!-- Red ring - top-right -->
|
||||
<path
|
||||
android:strokeColor="#F44336"
|
||||
android:strokeWidth="7"
|
||||
android:fillColor="#00000000"
|
||||
android:strokeLineCap="round"
|
||||
android:pathData="M 54,36
|
||||
A 16,16 0 1,1 70,52
|
||||
A 16,16 0 1,1 54,36 Z"/>
|
||||
|
||||
<!-- Green ring - bottom-left -->
|
||||
<!-- Green — starts at bottom, sweeps clockwise to upper-right -->
|
||||
<path
|
||||
android:strokeColor="#00C853"
|
||||
android:strokeWidth="7"
|
||||
android:strokeWidth="8.5"
|
||||
android:fillColor="#00000000"
|
||||
android:strokeLineCap="round"
|
||||
android:pathData="M 38,52
|
||||
A 16,16 0 1,1 54,68
|
||||
A 16,16 0 1,1 38,52 Z"/>
|
||||
android:pathData="M 54,72 A 18,18 0 1,1 63,38"/>
|
||||
|
||||
<!-- Orange ring - bottom-right -->
|
||||
<!-- Red — starts at right, sweeps clockwise to lower-left -->
|
||||
<path
|
||||
android:strokeColor="#E53935"
|
||||
android:strokeWidth="8.5"
|
||||
android:fillColor="#00000000"
|
||||
android:strokeLineCap="round"
|
||||
android:pathData="M 72,54 A 18,18 0 1,1 39,45"/>
|
||||
|
||||
<!-- Orange — starts at left, sweeps clockwise to upper-right -->
|
||||
<path
|
||||
android:strokeColor="#FF6D00"
|
||||
android:strokeWidth="7"
|
||||
android:strokeWidth="8.5"
|
||||
android:fillColor="#00000000"
|
||||
android:strokeLineCap="round"
|
||||
android:pathData="M 54,52
|
||||
A 16,16 0 1,1 70,68
|
||||
A 16,16 0 1,1 54,52 Z"/>
|
||||
android:pathData="M 36,54 A 18,18 0 1,1 69,63"/>
|
||||
|
||||
<!-- White filled circle at center to create interlock illusion -->
|
||||
<!-- Re-draw blue start cap on top so it goes OVER orange end -->
|
||||
<path
|
||||
android:strokeColor="#2979FF"
|
||||
android:strokeWidth="8.5"
|
||||
android:fillColor="#00000000"
|
||||
android:strokeLineCap="round"
|
||||
android:pathData="M 54,36 A 18,18 0 0,1 62,37.5"/>
|
||||
|
||||
<!-- White sync circle at center -->
|
||||
<path
|
||||
android:fillColor="#000000"
|
||||
android:pathData="M 46,52 A 8,8 0 1,0 62,52 A 8,8 0 1,0 46,52 Z"/>
|
||||
android:pathData="M 45,54 A 9,9 0 1,0 63,54 A 9,9 0 1,0 45,54 Z"/>
|
||||
|
||||
<!-- Sync arrow ring (outer white circle) -->
|
||||
<!-- Sync ring -->
|
||||
<path
|
||||
android:strokeColor="#FFFFFF"
|
||||
android:strokeWidth="3.5"
|
||||
android:strokeWidth="2.5"
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M 47,52 A 7,7 0 1,0 61,52 A 7,7 0 1,0 47,52 Z"/>
|
||||
android:pathData="M 46.5,54 A 7.5,7.5 0 1,0 61.5,54 A 7.5,7.5 0 1,0 46.5,54 Z"/>
|
||||
|
||||
<!-- Sync arrow head top -->
|
||||
<!-- Top arrow head (pointing up) -->
|
||||
<path
|
||||
android:fillColor="#FFFFFF"
|
||||
android:pathData="M 54,45 L 57,49 L 51,49 Z"/>
|
||||
android:pathData="M 54,46.5 L 57,50.5 L 51,50.5 Z"/>
|
||||
|
||||
<!-- Sync arrow head bottom -->
|
||||
<!-- Bottom arrow head (pointing down) -->
|
||||
<path
|
||||
android:fillColor="#FFFFFF"
|
||||
android:pathData="M 54,59 L 51,55 L 57,55 Z"/>
|
||||
android:pathData="M 54,61.5 L 51,57.5 L 57,57.5 Z"/>
|
||||
|
||||
</vector>
|
||||
|
||||
+2
-2
@@ -1,2 +1,2 @@
|
||||
VERSION_NAME=1.0.30
|
||||
VERSION_CODE=31
|
||||
VERSION_NAME=1.0.37
|
||||
VERSION_CODE=38
|
||||
|
||||
Reference in New Issue
Block a user