Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3d7a8b5f3d | |||
| 1d6a80e43d | |||
| a9322d3214 | |||
| 5f45a344b7 | |||
| e237555222 | |||
| d6220b7bd7 | |||
| c8e50ac17e |
@@ -7,17 +7,28 @@ import androidx.activity.enableEdgeToEdge
|
|||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.biometric.BiometricManager
|
import androidx.biometric.BiometricManager
|
||||||
import androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_STRONG
|
import androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_STRONG
|
||||||
|
import androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_WEAK
|
||||||
import androidx.biometric.BiometricManager.Authenticators.DEVICE_CREDENTIAL
|
import androidx.biometric.BiometricManager.Authenticators.DEVICE_CREDENTIAL
|
||||||
import androidx.biometric.BiometricPrompt
|
import androidx.biometric.BiometricPrompt
|
||||||
import androidx.compose.foundation.background
|
import androidx.compose.foundation.background
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Box
|
import androidx.compose.foundation.layout.Box
|
||||||
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.Spacer
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.material3.CircularProgressIndicator
|
import androidx.compose.foundation.layout.height
|
||||||
|
import androidx.compose.foundation.layout.size
|
||||||
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.filled.Lock
|
||||||
|
import androidx.compose.material3.Button
|
||||||
|
import androidx.compose.material3.Icon
|
||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.material3.Surface
|
import androidx.compose.material3.Surface
|
||||||
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.*
|
import androidx.compose.runtime.*
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
@@ -36,6 +47,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
@Inject lateinit var appPreferences: AppPreferences
|
@Inject lateinit var appPreferences: AppPreferences
|
||||||
|
|
||||||
private var isLocked by mutableStateOf(false)
|
private var isLocked by mutableStateOf(false)
|
||||||
|
private var showRetry by mutableStateOf(false)
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
installSplashScreen()
|
installSplashScreen()
|
||||||
@@ -47,13 +59,18 @@ class MainActivity : AppCompatActivity() {
|
|||||||
SyncFlowNavGraph(rememberNavController())
|
SyncFlowNavGraph(rememberNavController())
|
||||||
}
|
}
|
||||||
if (isLocked) {
|
if (isLocked) {
|
||||||
LockOverlay()
|
LockOverlay(
|
||||||
LaunchedEffect(Unit) {
|
showRetry = showRetry,
|
||||||
showBiometricPrompt(onSuccess = { isLocked = false })
|
onRetry = { triggerBiometric() },
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onResume() {
|
||||||
|
super.onResume()
|
||||||
|
if (isLocked) triggerBiometric()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStop() {
|
override fun onStop() {
|
||||||
@@ -62,51 +79,86 @@ class MainActivity : AppCompatActivity() {
|
|||||||
lifecycleScope.launch {
|
lifecycleScope.launch {
|
||||||
if (appPreferences.biometricLockEnabled.first() && canAuthenticate()) {
|
if (appPreferences.biometricLockEnabled.first() && canAuthenticate()) {
|
||||||
isLocked = true
|
isLocked = true
|
||||||
|
showRetry = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun canAuthenticate(): Boolean {
|
private fun triggerBiometric() {
|
||||||
val authenticators = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
|
showRetry = false
|
||||||
BIOMETRIC_STRONG or DEVICE_CREDENTIAL
|
val authenticators = bestAuthenticators()
|
||||||
else
|
|
||||||
BIOMETRIC_STRONG
|
|
||||||
return BiometricManager.from(this).canAuthenticate(authenticators) ==
|
|
||||||
BiometricManager.BIOMETRIC_SUCCESS
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun showBiometricPrompt(onSuccess: () -> Unit) {
|
|
||||||
val executor = ContextCompat.getMainExecutor(this)
|
val executor = ContextCompat.getMainExecutor(this)
|
||||||
val prompt = BiometricPrompt(this, executor, object : BiometricPrompt.AuthenticationCallback() {
|
val prompt = BiometricPrompt(this, executor, object : BiometricPrompt.AuthenticationCallback() {
|
||||||
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
|
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
|
||||||
onSuccess()
|
isLocked = false
|
||||||
|
showRetry = false
|
||||||
|
}
|
||||||
|
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
|
||||||
|
// Show the Unlock button so the user can tap to retry manually
|
||||||
|
showRetry = true
|
||||||
|
}
|
||||||
|
override fun onAuthenticationFailed() {
|
||||||
|
// Wrong biometric — BiometricPrompt retries automatically
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
val promptInfo = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
val promptInfo = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||||
BiometricPrompt.PromptInfo.Builder()
|
BiometricPrompt.PromptInfo.Builder()
|
||||||
.setTitle("Unlock SyncFlow")
|
.setTitle("Unlock SyncFlow")
|
||||||
.setSubtitle("Confirm your identity to continue")
|
.setSubtitle("Use fingerprint or PIN")
|
||||||
.setAllowedAuthenticators(BIOMETRIC_STRONG or DEVICE_CREDENTIAL)
|
.setAllowedAuthenticators(authenticators)
|
||||||
.build()
|
.build()
|
||||||
} else {
|
} else {
|
||||||
BiometricPrompt.PromptInfo.Builder()
|
BiometricPrompt.PromptInfo.Builder()
|
||||||
.setTitle("Unlock SyncFlow")
|
.setTitle("Unlock SyncFlow")
|
||||||
.setSubtitle("Confirm your identity to continue")
|
.setSubtitle("Use fingerprint")
|
||||||
.setNegativeButtonText("Cancel")
|
.setNegativeButtonText("Cancel")
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
prompt.authenticate(promptInfo)
|
prompt.authenticate(promptInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun bestAuthenticators(): Int {
|
||||||
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) return BIOMETRIC_STRONG
|
||||||
|
val bm = BiometricManager.from(this)
|
||||||
|
// Prefer strong+credential; fall back to weak+credential so side-sensor phones work
|
||||||
|
return if (bm.canAuthenticate(BIOMETRIC_STRONG or DEVICE_CREDENTIAL) == BiometricManager.BIOMETRIC_SUCCESS)
|
||||||
|
BIOMETRIC_STRONG or DEVICE_CREDENTIAL
|
||||||
|
else
|
||||||
|
BIOMETRIC_WEAK or DEVICE_CREDENTIAL
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun canAuthenticate(): Boolean {
|
||||||
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R)
|
||||||
|
return BiometricManager.from(this).canAuthenticate(BIOMETRIC_STRONG) == BiometricManager.BIOMETRIC_SUCCESS
|
||||||
|
val bm = BiometricManager.from(this)
|
||||||
|
return bm.canAuthenticate(BIOMETRIC_STRONG or DEVICE_CREDENTIAL) == BiometricManager.BIOMETRIC_SUCCESS ||
|
||||||
|
bm.canAuthenticate(BIOMETRIC_WEAK or DEVICE_CREDENTIAL) == BiometricManager.BIOMETRIC_SUCCESS
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
private fun LockOverlay() {
|
private fun LockOverlay(showRetry: Boolean, onRetry: () -> Unit) {
|
||||||
Box(
|
Box(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxSize()
|
.fillMaxSize()
|
||||||
.background(MaterialTheme.colorScheme.background),
|
.background(MaterialTheme.colorScheme.background),
|
||||||
contentAlignment = Alignment.Center,
|
contentAlignment = Alignment.Center,
|
||||||
) {
|
) {
|
||||||
CircularProgressIndicator()
|
Column(
|
||||||
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
|
verticalArrangement = Arrangement.spacedBy(12.dp),
|
||||||
|
) {
|
||||||
|
Icon(Icons.Default.Lock, null, modifier = Modifier.size(56.dp), tint = MaterialTheme.colorScheme.primary)
|
||||||
|
Text("SyncFlow is locked", style = MaterialTheme.typography.titleMedium)
|
||||||
|
if (showRetry) {
|
||||||
|
Button(onClick = onRetry) { Text("Unlock") }
|
||||||
|
} else {
|
||||||
|
Text(
|
||||||
|
"Use fingerprint or PIN to unlock",
|
||||||
|
style = MaterialTheme.typography.bodySmall,
|
||||||
|
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,6 @@ import androidx.room.TypeConverter
|
|||||||
import java.time.Instant
|
import java.time.Instant
|
||||||
|
|
||||||
class DbConverters {
|
class DbConverters {
|
||||||
@TypeConverter fun fromInstant(v: Instant?): Long? = v?.epochSecond
|
@TypeConverter fun fromInstant(v: Instant?): Long? = v?.toEpochMilli()
|
||||||
@TypeConverter fun toInstant(v: Long?): Instant? = v?.let { Instant.ofEpochSecond(it) }
|
@TypeConverter fun toInstant(v: Long?): Instant? = v?.let { Instant.ofEpochMilli(it) }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package com.syncflow.data.db
|
|||||||
import androidx.room.Database
|
import androidx.room.Database
|
||||||
import androidx.room.RoomDatabase
|
import androidx.room.RoomDatabase
|
||||||
import androidx.room.TypeConverters
|
import androidx.room.TypeConverters
|
||||||
|
import androidx.room.migration.Migration
|
||||||
|
import androidx.sqlite.db.SupportSQLiteDatabase
|
||||||
import com.syncflow.data.db.entities.*
|
import com.syncflow.data.db.entities.*
|
||||||
|
|
||||||
@Database(
|
@Database(
|
||||||
@@ -13,11 +15,21 @@ import com.syncflow.data.db.entities.*
|
|||||||
SyncConflictEntity::class,
|
SyncConflictEntity::class,
|
||||||
SyncEventEntity::class,
|
SyncEventEntity::class,
|
||||||
],
|
],
|
||||||
version = 2,
|
version = 3,
|
||||||
exportSchema = true,
|
exportSchema = true,
|
||||||
)
|
)
|
||||||
@TypeConverters(DbConverters::class)
|
@TypeConverters(DbConverters::class)
|
||||||
abstract class SyncDatabase : RoomDatabase() {
|
abstract class SyncDatabase : RoomDatabase() {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
// Wipe file states: timestamps were stored as epoch-seconds, now epoch-millis.
|
||||||
|
// All previously saved states are wrong so we drop and re-learn on next sync.
|
||||||
|
val MIGRATION_2_3 = object : Migration(2, 3) {
|
||||||
|
override fun migrate(db: SupportSQLiteDatabase) {
|
||||||
|
db.execSQL("DELETE FROM sync_file_states")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
abstract fun cloudAccountDao(): CloudAccountDao
|
abstract fun cloudAccountDao(): CloudAccountDao
|
||||||
abstract fun syncPairDao(): SyncPairDao
|
abstract fun syncPairDao(): SyncPairDao
|
||||||
abstract fun syncFileStateDao(): SyncFileStateDao
|
abstract fun syncFileStateDao(): SyncFileStateDao
|
||||||
|
|||||||
@@ -132,7 +132,10 @@ open class WebDavProvider(protected val account: CloudAccount) : CloudProvider {
|
|||||||
val req = Request.Builder().url(url(remotePath)).method("PROPFIND", PROPFIND_BODY).header("Depth", "0").build()
|
val req = Request.Builder().url(url(remotePath)).method("PROPFIND", PROPFIND_BODY).header("Depth", "0").build()
|
||||||
client.newCall(req).execute().use { resp ->
|
client.newCall(req).execute().use { resp ->
|
||||||
if (resp.code != 207) throw Exception("HTTP ${resp.code}")
|
if (resp.code != 207) throw Exception("HTTP ${resp.code}")
|
||||||
parsePropfind(resp.body?.string() ?: "", remotePath.substringBeforeLast('/'))
|
// Depth:0 returns exactly the requested resource as the single response entry.
|
||||||
|
// parsePropfind normally drops the first entry (the parent dir) for Depth:1
|
||||||
|
// directory listings, so pass dropFirst=false here.
|
||||||
|
parsePropfind(resp.body?.string() ?: "", remotePath.substringBeforeLast('/'), dropFirst = false)
|
||||||
.firstOrNull() ?: throw Exception("File not found")
|
.firstOrNull() ?: throw Exception("File not found")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -153,7 +156,7 @@ open class WebDavProvider(protected val account: CloudAccount) : CloudProvider {
|
|||||||
|
|
||||||
protected fun url(path: String) = "$baseUrl/${path.trimStart('/')}"
|
protected fun url(path: String) = "$baseUrl/${path.trimStart('/')}"
|
||||||
|
|
||||||
private fun parsePropfind(xml: String, parentPath: String): List<RemoteFile> {
|
private fun parsePropfind(xml: String, parentPath: String, dropFirst: Boolean = true): List<RemoteFile> {
|
||||||
val results = mutableListOf<RemoteFile>()
|
val results = mutableListOf<RemoteFile>()
|
||||||
try {
|
try {
|
||||||
val factory = XmlPullParserFactory.newInstance()
|
val factory = XmlPullParserFactory.newInstance()
|
||||||
@@ -192,7 +195,7 @@ open class WebDavProvider(protected val account: CloudAccount) : CloudProvider {
|
|||||||
eventType = parser.next()
|
eventType = parser.next()
|
||||||
}
|
}
|
||||||
} catch (_: Exception) {}
|
} catch (_: Exception) {}
|
||||||
return results.drop(1) // drop the parent folder itself
|
return if (dropFirst) results.drop(1) else results
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun parseHttpDate(value: String): Instant = try {
|
private fun parseHttpDate(value: String): Instant = try {
|
||||||
|
|||||||
@@ -21,9 +21,8 @@ object AppModule {
|
|||||||
@Provides @Singleton
|
@Provides @Singleton
|
||||||
fun provideDatabase(@ApplicationContext ctx: Context): SyncDatabase =
|
fun provideDatabase(@ApplicationContext ctx: Context): SyncDatabase =
|
||||||
Room.databaseBuilder(ctx, SyncDatabase::class.java, "syncflow.db")
|
Room.databaseBuilder(ctx, SyncDatabase::class.java, "syncflow.db")
|
||||||
// Only fall back to destructive migration for very old dev builds (v1).
|
|
||||||
// All future version bumps must include a proper Migration object.
|
|
||||||
.fallbackToDestructiveMigrationFrom(1)
|
.fallbackToDestructiveMigrationFrom(1)
|
||||||
|
.addMigrations(SyncDatabase.MIGRATION_2_3)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
@Provides fun provideCloudAccountDao(db: SyncDatabase): CloudAccountDao = db.cloudAccountDao()
|
@Provides fun provideCloudAccountDao(db: SyncDatabase): CloudAccountDao = db.cloudAccountDao()
|
||||||
|
|||||||
@@ -63,7 +63,13 @@ sealed class LocalAccessor {
|
|||||||
|
|
||||||
class Saf(private val treeUri: Uri, private val resolver: ContentResolver) : LocalAccessor() {
|
class Saf(private val treeUri: Uri, private val resolver: ContentResolver) : LocalAccessor() {
|
||||||
|
|
||||||
|
// Populated by walkFiles so openInputStream can skip the re-query for files that
|
||||||
|
// already exist locally (uploads). Root-level files are the common failure case
|
||||||
|
// when findDocUri re-queries: the cache sidesteps the issue entirely.
|
||||||
|
private val docIdCache = mutableMapOf<String, String>()
|
||||||
|
|
||||||
override fun walkFiles(pair: SyncPair): Map<String, LocalFileInfo> {
|
override fun walkFiles(pair: SyncPair): Map<String, LocalFileInfo> {
|
||||||
|
docIdCache.clear()
|
||||||
val rootDocId = DocumentsContract.getTreeDocumentId(treeUri)
|
val rootDocId = DocumentsContract.getTreeDocumentId(treeUri)
|
||||||
val childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(treeUri, rootDocId)
|
val childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(treeUri, rootDocId)
|
||||||
return cursorWalk(childrenUri, "", pair)
|
return cursorWalk(childrenUri, "", pair)
|
||||||
@@ -110,6 +116,7 @@ sealed class LocalAccessor {
|
|||||||
if (ext in excludeExts) continue
|
if (ext in excludeExts) continue
|
||||||
if (size !in minBytes..maxBytes) continue
|
if (size !in minBytes..maxBytes) continue
|
||||||
result[rel] = LocalFileInfo(rel, size, modified)
|
result[rel] = LocalFileInfo(rel, size, modified)
|
||||||
|
docIdCache[rel] = docId
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -117,7 +124,10 @@ sealed class LocalAccessor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun openInputStream(relativePath: String): InputStream? {
|
override fun openInputStream(relativePath: String): InputStream? {
|
||||||
val docUri = findDocUri(relativePath) ?: return null
|
val docUri = docIdCache[relativePath]
|
||||||
|
?.let { DocumentsContract.buildDocumentUriUsingTree(treeUri, it) }
|
||||||
|
?: findDocUri(relativePath)
|
||||||
|
?: return null
|
||||||
return resolver.openInputStream(docUri)
|
return resolver.openInputStream(docUri)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -155,8 +165,15 @@ sealed class LocalAccessor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun delete(relativePath: String): Boolean {
|
override fun delete(relativePath: String): Boolean {
|
||||||
val docUri = findDocUri(relativePath) ?: return false
|
val docUri = docIdCache[relativePath]
|
||||||
return DocumentsContract.deleteDocument(resolver, docUri)
|
?.let { DocumentsContract.buildDocumentUriUsingTree(treeUri, it) }
|
||||||
|
?: findDocUri(relativePath)
|
||||||
|
?: return false
|
||||||
|
return try {
|
||||||
|
DocumentsContract.deleteDocument(resolver, docUri)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun lastModifiedMs(relativePath: String): Long {
|
override fun lastModifiedMs(relativePath: String): Long {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import com.syncflow.data.db.SyncFileStateDao
|
|||||||
import com.syncflow.data.db.SyncPairDao
|
import com.syncflow.data.db.SyncPairDao
|
||||||
import com.syncflow.data.db.entities.SyncConflictEntity
|
import com.syncflow.data.db.entities.SyncConflictEntity
|
||||||
import com.syncflow.data.db.entities.SyncEventEntity
|
import com.syncflow.data.db.entities.SyncEventEntity
|
||||||
|
import com.syncflow.data.db.entities.SyncFileStateEntity
|
||||||
import com.syncflow.data.providers.CloudProvider
|
import com.syncflow.data.providers.CloudProvider
|
||||||
import com.syncflow.domain.model.ConflictStrategy
|
import com.syncflow.domain.model.ConflictStrategy
|
||||||
import com.syncflow.domain.model.DeleteBehavior
|
import com.syncflow.domain.model.DeleteBehavior
|
||||||
@@ -68,39 +69,44 @@ class SyncEngine @Inject constructor(
|
|||||||
.associateBy { it.path.removePrefix(pair.remotePath).trimStart('/') }
|
.associateBy { it.path.removePrefix(pair.remotePath).trimStart('/') }
|
||||||
val localFiles = accessor.walkFiles(pair)
|
val localFiles = accessor.walkFiles(pair)
|
||||||
|
|
||||||
var uploaded = 0; var downloaded = 0; var deleted = 0; var skipped = 0; var failed = 0; var conflicts = 0
|
|
||||||
var bytesTransferred = 0L
|
|
||||||
val newStates = mutableListOf<com.syncflow.data.db.entities.SyncFileStateEntity>()
|
|
||||||
|
|
||||||
val allPaths = (localFiles.keys + remoteFiles.keys + knownStates.keys).toSet()
|
val allPaths = (localFiles.keys + remoteFiles.keys + knownStates.keys).toSet()
|
||||||
|
val hasPriorSyncState = knownStates.isNotEmpty()
|
||||||
val semaphore = Semaphore(4)
|
val semaphore = Semaphore(4)
|
||||||
|
|
||||||
coroutineScope {
|
// Each async block returns its outcome; no shared mutable state across coroutines.
|
||||||
|
data class FileOutcome(
|
||||||
|
val uploaded: Int = 0, val downloaded: Int = 0, val deleted: Int = 0,
|
||||||
|
val skipped: Int = 0, val failed: Int = 0, val conflicts: Int = 0,
|
||||||
|
val bytesTransferred: Long = 0L,
|
||||||
|
val newState: SyncFileStateEntity? = null,
|
||||||
|
)
|
||||||
|
|
||||||
|
val outcomes: List<FileOutcome> = coroutineScope {
|
||||||
allPaths.map { rel ->
|
allPaths.map { rel ->
|
||||||
async {
|
async {
|
||||||
semaphore.withPermit {
|
semaphore.withPermit {
|
||||||
val local = localFiles[rel]
|
val local = localFiles[rel]
|
||||||
val remote = remoteFiles[rel]
|
val remote = remoteFiles[rel]
|
||||||
val known = knownStates[rel]
|
val known = knownStates[rel]
|
||||||
val decision = decide(pair.syncDirection, pair.conflictStrategy, pair.deleteBehavior, local, remote, known)
|
val decision = syncDecide(pair.syncDirection, pair.conflictStrategy, pair.deleteBehavior, local, remote, known, hasPriorSyncState)
|
||||||
|
|
||||||
when (decision) {
|
when (decision) {
|
||||||
SyncDecision.UPLOAD -> {
|
SyncDecision.UPLOAD -> {
|
||||||
|
var uploadedRemoteFile: RemoteFile? = null
|
||||||
val bytes = runCatching {
|
val bytes = runCatching {
|
||||||
|
ensureRemoteDirs(provider, pair.remotePath, rel)
|
||||||
accessor.openInputStream(rel)?.use { stream ->
|
accessor.openInputStream(rel)?.use { stream ->
|
||||||
provider.uploadFile(stream, "${pair.remotePath}/$rel", local!!.sizeBytes) { }
|
uploadedRemoteFile = provider.uploadFile(stream, "${pair.remotePath}/$rel", local!!.sizeBytes) { }.getOrThrow()
|
||||||
}
|
}
|
||||||
local!!.sizeBytes
|
local!!.sizeBytes
|
||||||
}.getOrElse { e ->
|
}.getOrElse { e ->
|
||||||
Timber.e(e, "Upload failed: $rel")
|
Timber.e(e, "Upload failed: $rel")
|
||||||
failed++
|
|
||||||
logEvent(pair.id, SyncEventType.FILE_SKIPPED, rel, e.message, 0)
|
logEvent(pair.id, SyncEventType.FILE_SKIPPED, rel, e.message, 0)
|
||||||
return@withPermit
|
return@withPermit FileOutcome(failed = 1)
|
||||||
}
|
}
|
||||||
uploaded++
|
|
||||||
bytesTransferred += bytes
|
|
||||||
newStates += buildState(pair.id, rel, local!!, remote)
|
|
||||||
logEvent(pair.id, SyncEventType.FILE_UPLOADED, rel, null, bytes)
|
logEvent(pair.id, SyncEventType.FILE_UPLOADED, rel, null, bytes)
|
||||||
|
FileOutcome(uploaded = 1, bytesTransferred = bytes,
|
||||||
|
newState = buildState(pair.id, rel, local!!, remoteAfterTransfer = uploadedRemoteFile))
|
||||||
}
|
}
|
||||||
SyncDecision.DOWNLOAD -> {
|
SyncDecision.DOWNLOAD -> {
|
||||||
val bytes = runCatching {
|
val bytes = runCatching {
|
||||||
@@ -110,29 +116,31 @@ class SyncEngine @Inject constructor(
|
|||||||
remote!!.sizeBytes
|
remote!!.sizeBytes
|
||||||
}.getOrElse { e ->
|
}.getOrElse { e ->
|
||||||
Timber.e(e, "Download failed: $rel")
|
Timber.e(e, "Download failed: $rel")
|
||||||
failed++
|
|
||||||
logEvent(pair.id, SyncEventType.FILE_SKIPPED, rel, e.message, 0)
|
logEvent(pair.id, SyncEventType.FILE_SKIPPED, rel, e.message, 0)
|
||||||
return@withPermit
|
return@withPermit FileOutcome(failed = 1)
|
||||||
}
|
}
|
||||||
downloaded++
|
// Read the actual local mtime written by the OS/SAF after download.
|
||||||
bytesTransferred += bytes
|
val localMtime = runCatching { accessor.lastModifiedMs(rel) }
|
||||||
newStates += buildState(pair.id, rel, null, remote)
|
.getOrDefault(System.currentTimeMillis()).takeIf { it > 0L }
|
||||||
|
?: System.currentTimeMillis()
|
||||||
logEvent(pair.id, SyncEventType.FILE_DOWNLOADED, rel, null, bytes)
|
logEvent(pair.id, SyncEventType.FILE_DOWNLOADED, rel, null, bytes)
|
||||||
|
FileOutcome(downloaded = 1, bytesTransferred = bytes,
|
||||||
|
newState = buildState(pair.id, rel,
|
||||||
|
LocalFileInfo(rel, remote!!.sizeBytes, localMtime), remoteAfterTransfer = remote))
|
||||||
}
|
}
|
||||||
SyncDecision.DELETE_LOCAL -> {
|
SyncDecision.DELETE_LOCAL -> {
|
||||||
accessor.delete(rel)
|
accessor.delete(rel)
|
||||||
fileStateDao.delete(pair.id, rel)
|
fileStateDao.delete(pair.id, rel)
|
||||||
deleted++
|
|
||||||
logEvent(pair.id, SyncEventType.FILE_DELETED, rel, "local", 0)
|
logEvent(pair.id, SyncEventType.FILE_DELETED, rel, "local", 0)
|
||||||
|
FileOutcome(deleted = 1)
|
||||||
}
|
}
|
||||||
SyncDecision.DELETE_REMOTE -> {
|
SyncDecision.DELETE_REMOTE -> {
|
||||||
provider.deleteFile("${pair.remotePath}/$rel")
|
provider.deleteFile("${pair.remotePath}/$rel")
|
||||||
fileStateDao.delete(pair.id, rel)
|
fileStateDao.delete(pair.id, rel)
|
||||||
deleted++
|
|
||||||
logEvent(pair.id, SyncEventType.FILE_DELETED, rel, "remote", 0)
|
logEvent(pair.id, SyncEventType.FILE_DELETED, rel, "remote", 0)
|
||||||
|
FileOutcome(deleted = 1)
|
||||||
}
|
}
|
||||||
SyncDecision.CONFLICT -> {
|
SyncDecision.CONFLICT -> {
|
||||||
conflicts++
|
|
||||||
conflictDao.insert(SyncConflictEntity(
|
conflictDao.insert(SyncConflictEntity(
|
||||||
syncPairId = pair.id,
|
syncPairId = pair.id,
|
||||||
relativePath = rel,
|
relativePath = rel,
|
||||||
@@ -144,31 +152,96 @@ class SyncEngine @Inject constructor(
|
|||||||
detectedAt = Instant.now(),
|
detectedAt = Instant.now(),
|
||||||
))
|
))
|
||||||
logEvent(pair.id, SyncEventType.CONFLICT_DETECTED, rel, null, 0)
|
logEvent(pair.id, SyncEventType.CONFLICT_DETECTED, rel, null, 0)
|
||||||
|
FileOutcome(conflicts = 1)
|
||||||
|
}
|
||||||
|
SyncDecision.SKIP -> {
|
||||||
|
// Save state whenever both sides are present and state is absent or
|
||||||
|
// incomplete (post-upload null metadata). Without a baseline record,
|
||||||
|
// a subsequent local deletion would look like an unseen remote file
|
||||||
|
// and be re-downloaded instead of triggering DELETE_REMOTE.
|
||||||
|
val saveState = local != null && remote != null && (
|
||||||
|
known == null ||
|
||||||
|
known.remoteModifiedAt == null || known.localModifiedAt == null
|
||||||
|
)
|
||||||
|
if (saveState) {
|
||||||
|
FileOutcome(skipped = 1, newState = buildState(pair.id, rel, local, remoteAfterTransfer = remote))
|
||||||
|
} else {
|
||||||
|
FileOutcome(skipped = 1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
SyncDecision.SKIP -> skipped++
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.awaitAll()
|
}.awaitAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
fileStateDao.upsertAll(newStates)
|
fileStateDao.upsertAll(outcomes.mapNotNull { it.newState })
|
||||||
return SyncResult(uploaded, downloaded, deleted, skipped, failed, conflicts, bytesTransferred)
|
return SyncResult(
|
||||||
|
uploaded = outcomes.sumOf { it.uploaded },
|
||||||
|
downloaded = outcomes.sumOf { it.downloaded },
|
||||||
|
deleted = outcomes.sumOf { it.deleted },
|
||||||
|
skipped = outcomes.sumOf { it.skipped },
|
||||||
|
failedFiles = outcomes.sumOf { it.failed },
|
||||||
|
conflicts = outcomes.sumOf { it.conflicts },
|
||||||
|
bytesTransferred = outcomes.sumOf { it.bytesTransferred },
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun decide(
|
private suspend fun ensureRemoteDirs(provider: CloudProvider, remotePairPath: String, rel: String) {
|
||||||
|
val parts = rel.replace('\\', '/').split('/')
|
||||||
|
var currentPath = remotePairPath
|
||||||
|
for (part in parts.dropLast(1)) {
|
||||||
|
currentPath = "$currentPath/$part"
|
||||||
|
provider.createDirectory(currentPath).onFailure { e ->
|
||||||
|
Timber.w("MKCOL $currentPath: ${e.message}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun buildState(
|
||||||
|
pairId: Long,
|
||||||
|
rel: String,
|
||||||
|
local: LocalFileInfo?,
|
||||||
|
remoteAfterTransfer: RemoteFile?,
|
||||||
|
) = SyncFileStateEntity(
|
||||||
|
syncPairId = pairId,
|
||||||
|
relativePath = rel,
|
||||||
|
localModifiedAt = local?.lastModifiedMs?.let { Instant.ofEpochMilli(it) },
|
||||||
|
localSizeBytes = local?.sizeBytes ?: 0L,
|
||||||
|
localHash = null,
|
||||||
|
remoteModifiedAt = remoteAfterTransfer?.modifiedAt,
|
||||||
|
remoteSizeBytes = remoteAfterTransfer?.sizeBytes ?: 0L,
|
||||||
|
remoteEtag = remoteAfterTransfer?.etag,
|
||||||
|
lastSyncedAt = Instant.now(),
|
||||||
|
syncedHash = null,
|
||||||
|
)
|
||||||
|
|
||||||
|
private suspend fun logEvent(pairId: Long, type: SyncEventType, file: String?, msg: String?, bytes: Long) {
|
||||||
|
eventDao.insert(SyncEventEntity(syncPairId = pairId, timestamp = Instant.now(), eventType = type, filePath = file, message = msg, bytesTransferred = bytes))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Top-level so unit tests can call it directly without instantiating SyncEngine.
|
||||||
|
internal fun syncDecide(
|
||||||
direction: SyncDirection,
|
direction: SyncDirection,
|
||||||
conflictStrategy: ConflictStrategy,
|
conflictStrategy: ConflictStrategy,
|
||||||
deleteBehavior: DeleteBehavior,
|
deleteBehavior: DeleteBehavior,
|
||||||
local: LocalFileInfo?,
|
local: LocalFileInfo?,
|
||||||
remote: RemoteFile?,
|
remote: RemoteFile?,
|
||||||
known: com.syncflow.data.db.entities.SyncFileStateEntity?,
|
known: SyncFileStateEntity?,
|
||||||
|
hasPriorSyncState: Boolean = false,
|
||||||
): SyncDecision {
|
): SyncDecision {
|
||||||
val localExists = local != null
|
val localExists = local != null
|
||||||
val remoteExists = remote != null
|
val remoteExists = remote != null
|
||||||
|
|
||||||
val localChanged = known == null || (localExists && local!!.lastModifiedMs != known.localModifiedAt?.toEpochMilli())
|
// Treat null known timestamps as "not yet recorded" — don't treat as changed.
|
||||||
val remoteChanged = known == null || (remoteExists && remote!!.etag != known.remoteEtag && remote.modifiedAt != known.remoteModifiedAt)
|
// The SKIP reconciliation pass will fill them in on the next sync.
|
||||||
|
val localChanged = known == null ||
|
||||||
|
(localExists && known.localModifiedAt != null &&
|
||||||
|
local!!.lastModifiedMs != known.localModifiedAt.toEpochMilli())
|
||||||
|
val remoteChanged = known == null ||
|
||||||
|
(remoteExists && known.remoteModifiedAt != null &&
|
||||||
|
(remote!!.etag != known.remoteEtag || remote.modifiedAt != known.remoteModifiedAt))
|
||||||
|
|
||||||
return when {
|
return when {
|
||||||
!localExists && !remoteExists -> SyncDecision.SKIP
|
!localExists && !remoteExists -> SyncDecision.SKIP
|
||||||
@@ -186,10 +259,22 @@ class SyncEngine @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
!localExists && remoteExists -> when {
|
!localExists && remoteExists -> when {
|
||||||
known == null -> when (direction) {
|
known == null -> if (!hasPriorSyncState) {
|
||||||
|
// Initial sync: no history at all — remote files are new, download them.
|
||||||
|
when (direction) {
|
||||||
SyncDirection.DOWNLOAD_ONLY, SyncDirection.TWO_WAY -> SyncDecision.DOWNLOAD
|
SyncDirection.DOWNLOAD_ONLY, SyncDirection.TWO_WAY -> SyncDecision.DOWNLOAD
|
||||||
else -> SyncDecision.SKIP
|
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 {
|
else -> when {
|
||||||
deleteBehavior == DeleteBehavior.KEEP -> SyncDecision.SKIP
|
deleteBehavior == DeleteBehavior.KEEP -> SyncDecision.SKIP
|
||||||
direction == SyncDirection.UPLOAD_ONLY || direction == SyncDirection.TWO_WAY -> SyncDecision.DELETE_REMOTE
|
direction == SyncDirection.UPLOAD_ONLY || direction == SyncDirection.TWO_WAY -> SyncDecision.DELETE_REMOTE
|
||||||
@@ -222,29 +307,6 @@ class SyncEngine @Inject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun buildState(
|
|
||||||
pairId: Long,
|
|
||||||
rel: String,
|
|
||||||
local: LocalFileInfo?,
|
|
||||||
remote: RemoteFile?,
|
|
||||||
) = com.syncflow.data.db.entities.SyncFileStateEntity(
|
|
||||||
syncPairId = pairId,
|
|
||||||
relativePath = rel,
|
|
||||||
localModifiedAt = local?.lastModifiedMs?.let { Instant.ofEpochMilli(it) },
|
|
||||||
localSizeBytes = local?.sizeBytes ?: 0L,
|
|
||||||
localHash = null,
|
|
||||||
remoteModifiedAt = remote?.modifiedAt,
|
|
||||||
remoteSizeBytes = remote?.sizeBytes ?: 0L,
|
|
||||||
remoteEtag = remote?.etag,
|
|
||||||
lastSyncedAt = Instant.now(),
|
|
||||||
syncedHash = null,
|
|
||||||
)
|
|
||||||
|
|
||||||
private suspend fun logEvent(pairId: Long, type: SyncEventType, file: String?, msg: String?, bytes: Long) {
|
|
||||||
eventDao.insert(SyncEventEntity(syncPairId = pairId, timestamp = Instant.now(), eventType = type, filePath = file, message = msg, bytesTransferred = bytes))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
enum class SyncDecision { UPLOAD, DOWNLOAD, DELETE_LOCAL, DELETE_REMOTE, CONFLICT, SKIP }
|
enum class SyncDecision { UPLOAD, DOWNLOAD, DELETE_LOCAL, DELETE_REMOTE, CONFLICT, SKIP }
|
||||||
|
|
||||||
data class SyncResult(
|
data class SyncResult(
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.syncflow.ui.addpair
|
package com.syncflow.ui.addpair
|
||||||
|
|
||||||
|
import android.content.Intent
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
@@ -14,6 +15,7 @@ import androidx.compose.material3.*
|
|||||||
import androidx.compose.runtime.*
|
import androidx.compose.runtime.*
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.text.font.FontWeight
|
import androidx.compose.ui.text.font.FontWeight
|
||||||
import androidx.compose.ui.text.input.ImeAction
|
import androidx.compose.ui.text.input.ImeAction
|
||||||
import androidx.compose.ui.text.input.KeyboardType
|
import androidx.compose.ui.text.input.KeyboardType
|
||||||
@@ -29,10 +31,17 @@ fun AddPairScreen(onDone: () -> Unit, vm: AddPairViewModel = hiltViewModel()) {
|
|||||||
val s by vm.state.collectAsState()
|
val s by vm.state.collectAsState()
|
||||||
LaunchedEffect(s.done) { if (s.done) onDone() }
|
LaunchedEffect(s.done) { if (s.done) onDone() }
|
||||||
|
|
||||||
|
val context = LocalContext.current
|
||||||
var showRemoteBrowser by remember { mutableStateOf(false) }
|
var showRemoteBrowser by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
val dirPicker = rememberLauncherForActivityResult(ActivityResultContracts.OpenDocumentTree()) { uri: Uri? ->
|
val dirPicker = rememberLauncherForActivityResult(ActivityResultContracts.OpenDocumentTree()) { uri: Uri? ->
|
||||||
uri?.let { vm.update { copy(localPath = it.toString()) } }
|
uri?.let {
|
||||||
|
context.contentResolver.takePersistableUriPermission(
|
||||||
|
it,
|
||||||
|
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION,
|
||||||
|
)
|
||||||
|
vm.update { copy(localPath = it.toString()) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (showRemoteBrowser && s.selectedAccountId != -1L) {
|
if (showRemoteBrowser && s.selectedAccountId != -1L) {
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ class HomeViewModel @Inject constructor(
|
|||||||
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList())
|
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList())
|
||||||
|
|
||||||
fun triggerSync(pair: SyncPairEntity) {
|
fun triggerSync(pair: SyncPairEntity) {
|
||||||
val req = SyncWorker.buildOneTimeRequest(pair.id, pair.wifiOnly, pair.chargingOnly)
|
val req = SyncWorker.buildOneTimeRequest(pair.id, wifiOnly = false, chargingOnly = false)
|
||||||
workManager.enqueue(req)
|
workManager.enqueue(req)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ fun SyncFlowNavGraph(navController: NavHostController) {
|
|||||||
) {
|
) {
|
||||||
PairDetailScreen(
|
PairDetailScreen(
|
||||||
onBack = { navController.popBackStack() },
|
onBack = { navController.popBackStack() },
|
||||||
|
onEdit = { id -> navController.navigate(Screen.AddPair.route(id)) },
|
||||||
onConflicts = { id -> navController.navigate(Screen.Conflicts.route(id)) },
|
onConflicts = { id -> navController.navigate(Screen.Conflicts.route(id)) },
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import java.time.format.FormatStyle
|
|||||||
@Composable
|
@Composable
|
||||||
fun PairDetailScreen(
|
fun PairDetailScreen(
|
||||||
onBack: () -> Unit,
|
onBack: () -> Unit,
|
||||||
|
onEdit: (Long) -> Unit,
|
||||||
onConflicts: (Long) -> Unit,
|
onConflicts: (Long) -> Unit,
|
||||||
vm: PairDetailViewModel = hiltViewModel(),
|
vm: PairDetailViewModel = hiltViewModel(),
|
||||||
) {
|
) {
|
||||||
@@ -49,6 +50,7 @@ fun PairDetailScreen(
|
|||||||
title = { Text(pair?.name ?: "…") },
|
title = { Text(pair?.name ?: "…") },
|
||||||
navigationIcon = { IconButton(onClick = onBack) { Icon(Icons.Default.ArrowBack, "Back") } },
|
navigationIcon = { IconButton(onClick = onBack) { Icon(Icons.Default.ArrowBack, "Back") } },
|
||||||
actions = {
|
actions = {
|
||||||
|
IconButton(onClick = { pair?.let { onEdit(it.id) } }) { Icon(Icons.Default.Edit, "Edit") }
|
||||||
IconButton(onClick = { vm.syncNow() }) { Icon(Icons.Default.Sync, "Sync now") }
|
IconButton(onClick = { vm.syncNow() }) { Icon(Icons.Default.Sync, "Sync now") }
|
||||||
IconButton(onClick = { showDelete = true }) { Icon(Icons.Default.Delete, "Delete") }
|
IconButton(onClick = { showDelete = true }) { Icon(Icons.Default.Delete, "Delete") }
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ class PairDetailViewModel @Inject constructor(
|
|||||||
|
|
||||||
fun syncNow() {
|
fun syncNow() {
|
||||||
val p = pair.value ?: return
|
val p = pair.value ?: return
|
||||||
workManager.enqueue(SyncWorker.buildOneTimeRequest(p.id, p.wifiOnly, p.chargingOnly))
|
workManager.enqueue(SyncWorker.buildOneTimeRequest(p.id, wifiOnly = false, chargingOnly = false))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun delete() {
|
fun delete() {
|
||||||
|
|||||||
@@ -0,0 +1,164 @@
|
|||||||
|
package com.syncflow.domain.sync
|
||||||
|
|
||||||
|
import com.syncflow.data.db.entities.SyncFileStateEntity
|
||||||
|
import com.syncflow.domain.model.*
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Test
|
||||||
|
import java.time.Instant
|
||||||
|
|
||||||
|
class SyncDecideTest {
|
||||||
|
|
||||||
|
private val MS = 1_716_000_000_000L
|
||||||
|
private val MS2 = MS + 5_000L
|
||||||
|
|
||||||
|
private fun local(ms: Long = MS, size: Long = 100L) = LocalFileInfo("test.txt", size, ms)
|
||||||
|
|
||||||
|
private fun remote(ms: Long = MS, etag: String? = "abc", size: Long = 100L) =
|
||||||
|
RemoteFile(
|
||||||
|
path = "path/test.txt", name = "test.txt", isDirectory = false,
|
||||||
|
sizeBytes = size, modifiedAt = Instant.ofEpochMilli(ms),
|
||||||
|
etag = etag, mimeType = null,
|
||||||
|
)
|
||||||
|
|
||||||
|
private fun state(localMs: Long? = MS, remoteMs: Long? = MS, etag: String? = "abc") =
|
||||||
|
SyncFileStateEntity(
|
||||||
|
syncPairId = 1L, relativePath = "test.txt",
|
||||||
|
localModifiedAt = localMs?.let { Instant.ofEpochMilli(it) },
|
||||||
|
localSizeBytes = 100L, localHash = null,
|
||||||
|
remoteModifiedAt = remoteMs?.let { Instant.ofEpochMilli(it) },
|
||||||
|
remoteSizeBytes = 100L, remoteEtag = etag,
|
||||||
|
lastSyncedAt = Instant.now(), syncedHash = null,
|
||||||
|
)
|
||||||
|
|
||||||
|
private fun decide(
|
||||||
|
local: LocalFileInfo?, remote: RemoteFile?, known: SyncFileStateEntity? = null,
|
||||||
|
dir: SyncDirection = SyncDirection.TWO_WAY,
|
||||||
|
conflict: ConflictStrategy = ConflictStrategy.KEEP_NEWEST,
|
||||||
|
delete: DeleteBehavior = DeleteBehavior.MIRROR,
|
||||||
|
hasPriorState: Boolean = known != null,
|
||||||
|
) = syncDecide(dir, conflict, delete, local, remote, known, hasPriorState)
|
||||||
|
|
||||||
|
// ── first sync (no known state) ───────────────────────────────────────────
|
||||||
|
|
||||||
|
@Test fun `first sync both exist local newer uploads`() =
|
||||||
|
assertEquals(SyncDecision.UPLOAD, decide(local(MS2), remote(MS)))
|
||||||
|
|
||||||
|
@Test fun `first sync both exist remote newer downloads`() =
|
||||||
|
assertEquals(SyncDecision.DOWNLOAD, decide(local(MS), remote(MS2)))
|
||||||
|
|
||||||
|
@Test fun `first sync local only TWO_WAY uploads`() =
|
||||||
|
assertEquals(SyncDecision.UPLOAD, decide(local(), null))
|
||||||
|
|
||||||
|
@Test fun `first sync remote only TWO_WAY downloads`() =
|
||||||
|
assertEquals(SyncDecision.DOWNLOAD, decide(null, remote()))
|
||||||
|
|
||||||
|
// ── after upload: remote metadata null in state ───────────────────────────
|
||||||
|
|
||||||
|
@Test fun `second sync after upload remote metadata null skips`() {
|
||||||
|
// State saved after upload: local mtime known, remote unknown (null).
|
||||||
|
val known = state(localMs = MS, remoteMs = null, etag = null)
|
||||||
|
// Remote listing shows a new mtime (server assigned), but we treat null as "no change".
|
||||||
|
assertEquals(SyncDecision.SKIP, decide(local(MS), remote(MS2), known))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun `after upload local changed again re-uploads`() {
|
||||||
|
val known = state(localMs = MS, remoteMs = null, etag = null)
|
||||||
|
assertEquals(SyncDecision.UPLOAD, decide(local(MS2), remote(MS2), known))
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── after download: local mtime recorded ─────────────────────────────────
|
||||||
|
|
||||||
|
@Test fun `second sync fully recorded skips`() {
|
||||||
|
val known = state(localMs = MS, remoteMs = MS, etag = "abc")
|
||||||
|
assertEquals(SyncDecision.SKIP, decide(local(MS), remote(MS, etag = "abc"), known))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun `remote changed after download downloads`() {
|
||||||
|
val known = state(localMs = MS, remoteMs = MS, etag = "abc")
|
||||||
|
assertEquals(SyncDecision.DOWNLOAD, decide(local(MS), remote(MS2, etag = "xyz"), known))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun `local changed after download uploads`() {
|
||||||
|
val known = state(localMs = MS, remoteMs = MS, etag = "abc")
|
||||||
|
assertEquals(SyncDecision.UPLOAD, decide(local(MS2), remote(MS, etag = "abc"), known))
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── epoch-millis precision ────────────────────────────────────────────────
|
||||||
|
|
||||||
|
@Test fun `same millisecond timestamp treated as unchanged`() {
|
||||||
|
val ts = 1_716_393_136_789L
|
||||||
|
assertEquals(SyncDecision.SKIP,
|
||||||
|
decide(local(ts), remote(ts, etag = "e"), state(localMs = ts, remoteMs = ts, etag = "e")))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun `1ms difference detected as local change`() {
|
||||||
|
val ts = 1_716_393_136_789L
|
||||||
|
assertEquals(SyncDecision.UPLOAD,
|
||||||
|
decide(local(ts + 1), remote(ts, etag = "e"), state(localMs = ts, remoteMs = ts, etag = "e")))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun `epoch-second stored value differs from millis comparison`() {
|
||||||
|
// If we stored 1716393136 (seconds) and compare to 1716393136000 (millis) they differ →
|
||||||
|
// This was the original bug — now we store millis so they should match.
|
||||||
|
val ms = 1_716_393_136_000L // exact second boundary, no sub-second component
|
||||||
|
assertEquals(SyncDecision.SKIP,
|
||||||
|
decide(local(ms), remote(ms, etag = "e"), state(localMs = ms, remoteMs = ms, etag = "e")))
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── delete behaviour ──────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
@Test fun `local exists remote deleted TWO_WAY MIRROR deletes local`() =
|
||||||
|
assertEquals(SyncDecision.DELETE_LOCAL, decide(local(), null, state(), delete = DeleteBehavior.MIRROR))
|
||||||
|
|
||||||
|
@Test fun `local exists remote deleted KEEP skips`() =
|
||||||
|
assertEquals(SyncDecision.SKIP, decide(local(), null, state(), delete = DeleteBehavior.KEEP))
|
||||||
|
|
||||||
|
@Test fun `remote deleted UPLOAD_ONLY skips local deletion`() =
|
||||||
|
assertEquals(SyncDecision.SKIP,
|
||||||
|
decide(local(), null, state(), dir = SyncDirection.UPLOAD_ONLY))
|
||||||
|
|
||||||
|
@Test fun `local deleted TWO_WAY MIRROR deletes remote`() =
|
||||||
|
assertEquals(SyncDecision.DELETE_REMOTE, decide(null, remote(), state(), delete = DeleteBehavior.MIRROR))
|
||||||
|
|
||||||
|
@Test fun `local deleted TWO_WAY KEEP skips`() =
|
||||||
|
assertEquals(SyncDecision.SKIP, decide(null, remote(), state(), delete = DeleteBehavior.KEEP))
|
||||||
|
|
||||||
|
@Test fun `local deleted DOWNLOAD_ONLY skips remote deletion`() =
|
||||||
|
assertEquals(SyncDecision.SKIP,
|
||||||
|
decide(null, remote(), state(), dir = SyncDirection.DOWNLOAD_ONLY))
|
||||||
|
|
||||||
|
// ── local deleted, no state record (uploaded in broken version) ──────────
|
||||||
|
|
||||||
|
@Test fun `local deleted no known state but pair has prior history deletes remote`() =
|
||||||
|
// hasPriorState=true means the pair has been synced before; file has no state
|
||||||
|
// because it was uploaded when getFileMetadata was broken. Should still mirror deletion.
|
||||||
|
assertEquals(SyncDecision.DELETE_REMOTE,
|
||||||
|
decide(null, remote(), known = null, delete = DeleteBehavior.MIRROR, hasPriorState = true))
|
||||||
|
|
||||||
|
@Test fun `initial sync remote only no prior state downloads`() =
|
||||||
|
assertEquals(SyncDecision.DOWNLOAD,
|
||||||
|
decide(null, remote(), known = null, hasPriorState = false))
|
||||||
|
|
||||||
|
// ── first-seen SKIP saves baseline so later deletions are detected ────────
|
||||||
|
|
||||||
|
@Test fun `first sync both exist same mtime uploads local wins tie`() =
|
||||||
|
assertEquals(SyncDecision.UPLOAD, decide(local(MS), remote(MS, etag = "abc")))
|
||||||
|
|
||||||
|
@Test fun `after first-seen skip local deleted deletes remote`() {
|
||||||
|
// Simulate: first sync saw both sides identical → SKIP (state saved by engine).
|
||||||
|
// Then local file deleted → known is now present → DELETE_REMOTE.
|
||||||
|
val known = state(localMs = MS, remoteMs = MS, etag = "abc")
|
||||||
|
assertEquals(SyncDecision.DELETE_REMOTE,
|
||||||
|
decide(null, remote(MS, etag = "abc"), known, delete = DeleteBehavior.MIRROR))
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── directions ────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
@Test fun `UPLOAD_ONLY ignores remote changes`() =
|
||||||
|
assertEquals(SyncDecision.SKIP,
|
||||||
|
decide(local(MS), remote(MS2, etag = "new"), state(), dir = SyncDirection.UPLOAD_ONLY))
|
||||||
|
|
||||||
|
@Test fun `DOWNLOAD_ONLY ignores local changes`() =
|
||||||
|
assertEquals(SyncDecision.SKIP,
|
||||||
|
decide(local(MS2), remote(MS, etag = "abc"), state(), dir = SyncDirection.DOWNLOAD_ONLY))
|
||||||
|
}
|
||||||
+2
-2
@@ -1,2 +1,2 @@
|
|||||||
VERSION_NAME=1.0.1
|
VERSION_NAME=1.0.12
|
||||||
VERSION_CODE=2
|
VERSION_CODE=13
|
||||||
|
|||||||
Reference in New Issue
Block a user