Modern Apps git

← Modern-Apps.git

battery: stop maps' redundant network fixes; hourly subscriptions

Commit
5401cfc72ef5396918f12c03b33393fca3127c2f
Author
Vayun Mathur <[email protected]>
When
2026-11-20 21:34 UTC (15 days ago)
Parents
bdf73f7
FrameworkLocationManager registered GPS and NETWORK both at
1s/0m while its acceptance policy is GPS-first - network fixes
were discarded unless GPS had been silent 10s, so the second
stream was near-pure Wi-Fi/cell scan drain. NETWORK drops to
5s/10m when GPS is on (stays 1s when it's the only source) and a
zero-cost PASSIVE stream joins the same acceptance policy.
Navigation uses its own location manager and is untouched.

youpipe's subscription fetch (full per-channel network sweep)
ran every 15 minutes - up to 96 sweeps a day. It's hourly now
as its name always said, with UPDATE policy so existing
installs actually migrate cadences.

email set forkEvery=1 without maxParallelForks (Robolectric
sandbox isolation, like the Rust modules); it gets the same
availableProcessors/2 clamped 1..6 formula.

Verified: maps/youpipe compile; email smoke class 13/13.

Diff

email/build.gradle.kts
diff --git a/email/build.gradle.kts b/email/build.gradle.ktsindex 81978e5150..83d93ff790 100644--- a/email/build.gradle.kts+++ b/email/build.gradle.kts@@ -33,6 +33,13 @@ android {         unitTests.all {             it.maxHeapSize = "2g"             it.forkEvery = 1+            // forkEvery = 1 alone would run those per-class JVMs one at a+            // time (Gradle's default maxParallelForks = 1). Run several+            // class-forks concurrently instead, with the same+            // memory-bounded formula as build-logic's RustNative wiring:+            // each Robolectric fork costs roughly 1-2 GB, so the cap is+            // half the processors, clamped to 1..6.+            it.maxParallelForks = (Runtime.getRuntime().availableProcessors() / 2).coerceIn(1, 6)         }     } 
maps/src/main/java/com/vayunmathur/maps/platform/FrameworkLocationManager.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/platform/FrameworkLocationManager.kt b/maps/src/main/java/com/vayunmathur/maps/platform/FrameworkLocationManager.ktindex bc747d20b3..5f0ad549b5 100644--- a/maps/src/main/java/com/vayunmathur/maps/platform/FrameworkLocationManager.kt+++ b/maps/src/main/java/com/vayunmathur/maps/platform/FrameworkLocationManager.kt@@ -19,6 +19,19 @@ class FrameworkLocationManager(context: Context) : SensorEventListener {         private const val LOCATION_MIN_TIME_MS = 1000L         /** No distance floor: the fix-acceptance policy filters, not the OS. */         private const val LOCATION_MIN_DISTANCE_M = 0f+        /**+         * Relaxed cadence for NETWORK while GPS is live. The acceptance+         * policy is GPS-first — a network fix only lands when GPS has been+         * silent for 10 s or is 2× less accurate — so a 1 Hz network stream+         * is almost pure waste: Wi-Fi/cell scans firing every second for+         * fixes that get thrown away (the biggest non-navigation location+         * drain). At 5 s / 10 m the network provider still feeds the+         * stale-GPS and indoor-accuracy escapes, just without the constant+         * scanning. When GPS is off entirely, NETWORK keeps the fast+         * cadence: it is the only fix source the map has.+         */+        private const val NETWORK_MIN_TIME_MS = 5000L+        private const val NETWORK_MIN_DISTANCE_M = 10f     }     private val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager     private val sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager@@ -112,7 +125,8 @@ class FrameworkLocationManager(context: Context) : SensorEventListener {         }         registeredLocationListener = locationListener -        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {+        val gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)+        if (gpsEnabled) {             locationManager.requestLocationUpdates(                 LocationManager.GPS_PROVIDER,                 LOCATION_MIN_TIME_MS,@@ -121,8 +135,25 @@ class FrameworkLocationManager(context: Context) : SensorEventListener {             )         }         if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {+            // GPS live: NETWORK is only a fallback (see acceptsFix), so it+            // runs relaxed. GPS off: NETWORK is the sole source, keep 1 Hz.+            val minTimeMs = if (gpsEnabled) NETWORK_MIN_TIME_MS else LOCATION_MIN_TIME_MS+            val minDistanceM = if (gpsEnabled) NETWORK_MIN_DISTANCE_M else LOCATION_MIN_DISTANCE_M             locationManager.requestLocationUpdates(                 LocationManager.NETWORK_PROVIDER,+                minTimeMs,+                minDistanceM,+                locationListener,+            )+        }+        // Passive fallback: fixes other apps already paid to compute arrive+        // here at zero extra radio cost and flow through the same GPS-first+        // acceptance policy, so a coarse passive fix can still rescue the+        // puck when GPS goes quiet (indoors, tunnels) between the relaxed+        // network deliveries.+        if (locationManager.isProviderEnabled(LocationManager.PASSIVE_PROVIDER)) {+            locationManager.requestLocationUpdates(+                LocationManager.PASSIVE_PROVIDER,                 LOCATION_MIN_TIME_MS,                 LOCATION_MIN_DISTANCE_M,                 locationListener,
youpipe/src/main/java/com/vayunmathur/youpipe/platform/SubscriptionFetchTask.kt
diff --git a/youpipe/src/main/java/com/vayunmathur/youpipe/platform/SubscriptionFetchTask.kt b/youpipe/src/main/java/com/vayunmathur/youpipe/platform/SubscriptionFetchTask.ktindex 09e833f671..7088d1af3c 100644--- a/youpipe/src/main/java/com/vayunmathur/youpipe/platform/SubscriptionFetchTask.kt+++ b/youpipe/src/main/java/com/vayunmathur/youpipe/platform/SubscriptionFetchTask.kt@@ -6,9 +6,10 @@ import androidx.work.CoroutineWorker import androidx.work.WorkerParameters import androidx.work.workDataOf import com.vayunmathur.library.work.startRepeatedTask+import androidx.work.ExistingPeriodicWorkPolicy import com.vayunmathur.youpipe.data.SubscriptionRepository import com.vayunmathur.youpipe.data.SubscriptionVideo-import kotlin.time.Duration.Companion.minutes+import kotlin.time.Duration.Companion.hours import kotlinx.coroutines.CancellationException import com.vayunmathur.youpipe.network.getChannelInfo import com.vayunmathur.youpipe.network.getChannelVideos@@ -91,7 +92,20 @@ private suspend fun fetchAndStoreChannelVideos( }  fun setupHourlyTask(context: Context) {-    startRepeatedTask<SubscriptionFetchTask>(context, "subscription_fetch", 15.minutes)+    // This worker does real sync work — a network fetch per subscribed+    // channel plus Room writes — so the inherited 15-minute cadence (the+    // WorkManager periodic floor) meant up to 96 full subscription sweeps+    // a day. Hourly matches the feed's freshness needs at a quarter of the+    // wakeups; startRepeatedTask already constrains the work to+    // NetworkType.CONNECTED. UPDATE (not the KEEP default) so installs+    // already carrying the 15-minute periodic work migrate to the new+    // interval instead of keeping the old cadence forever.+    startRepeatedTask<SubscriptionFetchTask>(+        context,+        "subscription_fetch",+        1.hours,+        periodicWorkPolicy = ExistingPeriodicWorkPolicy.UPDATE,+    ) }  private fun offlineFetchException(channelName: String, cause: java.io.IOException): java.io.IOException {