android: the scheduler worker leaves Wi-Fi and charging to WorkManager
Build Debug APK / build (push) Failing after 2m47s
Build Debug APK / build (push) Failing after 2m47s
SchedulerConstraints re-checked the unmetered-network and charging constraints the WorkRequest already declares — schedule() rewrites them on every settings change, and WorkManager stops a worker whose constraints stop holding — and its doc still said no Android driver ran the scheduler. The worker now checks only that the scheduler is enabled. (F4i) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
973c427c89
commit
07766080f5
2 files changed
+6
-56
No files matched your search
-45
@@ -1,45 +0,0 @@
|
||||
package com.gregshuflin.synchronicity.scheduler
|
||||
|
||||
import android.content.Context
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.NetworkCapabilities
|
||||
import android.os.BatteryManager
|
||||
import androidx.core.content.getSystemService
|
||||
import uniffi.synchronicity.SchedulerSettings
|
||||
|
||||
/**
|
||||
* Checks Android's network-metered and charging state against a node's [SchedulerSettings] — the
|
||||
* Wi-Fi-only/charging gate `docs/sync-management.md`'s "The sync scheduler" section requires before
|
||||
* an Android driver may run the scheduler's content-fetch pass unattended (`TODO.md`'s "Add a
|
||||
* Wi-Fi-only/charging constraint before the sync scheduler ever runs on Android"). Pulling
|
||||
* arbitrarily large amounts of blob data on a timer with no user action is an accepted cost on
|
||||
* desktop; on a phone, doing it over a cellular connection or while unplugged is exactly the kind
|
||||
* of surprise data/battery use a personal-server app must not inflict by default.
|
||||
*
|
||||
* No driver invokes the scheduler's passes on Android yet (`rust/gui-app/src/scheduler_driver.rs`
|
||||
* is desktop-only) — this exists so one can call [isSatisfied] before doing so, rather than
|
||||
* hand-rolling the check inline once that driver lands.
|
||||
*/
|
||||
object SchedulerConstraints {
|
||||
/** True if every constraint [settings] enables is currently satisfied on this device. */
|
||||
fun isSatisfied(context: Context, settings: SchedulerSettings): Boolean =
|
||||
(!settings.wifiOnly || isOnUnmeteredNetwork(context)) &&
|
||||
(!settings.requireCharging || isCharging(context))
|
||||
|
||||
/**
|
||||
* True if the active network is unmetered. Matches WorkManager's own `NetworkType.UNMETERED`
|
||||
* semantics rather than literally checking for a Wi-Fi transport: a phone on an unmetered
|
||||
* cellular plan shouldn't be blocked, and a metered Wi-Fi hotspot shouldn't be allowed.
|
||||
*/
|
||||
private fun isOnUnmeteredNetwork(context: Context): Boolean {
|
||||
val connectivityManager = context.getSystemService<ConnectivityManager>()
|
||||
val capabilities =
|
||||
connectivityManager?.activeNetwork?.let {
|
||||
connectivityManager.getNetworkCapabilities(it)
|
||||
}
|
||||
return capabilities?.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED) ?: false
|
||||
}
|
||||
|
||||
private fun isCharging(context: Context): Boolean =
|
||||
context.getSystemService<BatteryManager>()?.isCharging ?: false
|
||||
}
|
||||
+6
-11
@@ -44,11 +44,10 @@ class SchedulerWorker(context: Context, params: WorkerParameters) :
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the tick if [SchedulerSettings] is enabled and its constraints are currently satisfied,
|
||||
* or returns `null` (this tick is skipped, not failed) otherwise. The Constraints declared on
|
||||
* the `WorkRequest` itself (see [schedule]) do the real gating; this re-check only covers the
|
||||
* window between WorkManager evaluating them and `doWork()` actually running, and the case
|
||||
* where settings changed since this work was last (re-)enqueued.
|
||||
* Runs the tick if [SchedulerSettings] is enabled, or returns `null` (this tick is skipped, not
|
||||
* failed) otherwise. Wi-Fi-only and charging are not re-checked here: they are the
|
||||
* `WorkRequest`'s own [Constraints] (see [schedule]), which [schedule] rewrites on every
|
||||
* settings change, and WorkManager stops a running worker whose constraints stop holding.
|
||||
*/
|
||||
private fun runTickIfEligible(nativeLib: NativeLib): Result? {
|
||||
val settings =
|
||||
@@ -56,12 +55,8 @@ class SchedulerWorker(context: Context, params: WorkerParameters) :
|
||||
.ffi { settings().getSchedulerSettings() }
|
||||
.onFailure { Log.w(TAG, "Failed to load scheduler settings", it) }
|
||||
.getOrNull()
|
||||
val eligible =
|
||||
settings != null &&
|
||||
settings.enabled &&
|
||||
SchedulerConstraints.isSatisfied(applicationContext, settings)
|
||||
if (!eligible) {
|
||||
Log.d(TAG, "Scheduler disabled or constraints not satisfied; skipping this tick")
|
||||
if (settings?.enabled != true) {
|
||||
Log.d(TAG, "Scheduler disabled; skipping this tick")
|
||||
return null
|
||||
}
|
||||
return nativeLib
|
||||
|
||||
Reference in new issue
Block a user