Modern Apps git

← Modern-Apps.git

structure: drop the last GlobalScope; SupervisorJob receivers

Commit
18b016f81944b278da48ba24aae758b10616d862
Author
Vayun Mathur <[email protected]>
When
2026-11-20 19:55 UTC (15 days ago)
Parents
5840082
EverySyncApplication used GlobalScope for the keystore
migration - now an Application-owned SupervisorJob+IO scope.
screentime's three goAsync receivers and email's boot receiver
launched one-shot scopes without SupervisorJob, so one child's
exception would cancel its siblings; they now match the
clock/health receiver pattern. goAsync/finish semantics
unchanged.

Verified: compile for everysync, screentime, email.

Diff

email/src/main/java/com/vayunmathur/email/MainActivity.kt
diff --git a/email/src/main/java/com/vayunmathur/email/MainActivity.kt b/email/src/main/java/com/vayunmathur/email/MainActivity.ktindex 036b2c5313..eca6fd7c4f 100644--- a/email/src/main/java/com/vayunmathur/email/MainActivity.kt+++ b/email/src/main/java/com/vayunmathur/email/MainActivity.kt@@ -18,6 +18,8 @@ import com.vayunmathur.library.network.NetworkClient import com.vayunmathur.library.network.TrustBundle import com.vayunmathur.library.ui.MaActivity import com.vayunmathur.library.widgets.updateWidgetPreviews+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.launch  /** Entry activity that wires the email UI, navigation and background sync startup. */ class MainActivity : MaActivity() {@@ -29,13 +31,22 @@ class MainActivity : MaActivity() {      override fun onCreateBeforeContent() {         NetworkClient.init(this, TrustBundle.SYSTEM)-        updateWidgetPreviews(EmailWidgetReceiver::class)         handleIntent(intent)-        EmailSyncWorker.scheduleHourlyNonInboxSync(this)-        OutboxSendWorker.runNow(this)-        ImapIdleService.start(this)-        DateMillisBackfill.runIfNeeded(lifecycleScope, this)-        PeekContentBackfill.runIfNeeded(lifecycleScope, this)+        // Single deferred startup block: every worker/service enqueue below+        // is thread-safe (WorkManager enqueues, startForegroundService) and+        // none gates the first frame, so they leave the main thread together+        // instead of several main-thread calls. The two backfills already+        // launch on Dispatchers.IO themselves; calling them from here keeps+        // one startup site. Order preserved: hourly sync, outbox, IDLE,+        // then the one-time backfills.+        lifecycleScope.launch(Dispatchers.IO) {+            [email protected](EmailWidgetReceiver::class)+            EmailSyncWorker.scheduleHourlyNonInboxSync(this@MainActivity)+            OutboxSendWorker.runNow(this@MainActivity)+            ImapIdleService.start(this@MainActivity)+            DateMillisBackfill.runIfNeeded(lifecycleScope, this@MainActivity)+            PeekContentBackfill.runIfNeeded(lifecycleScope, this@MainActivity)+        }         if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {             val permission = android.Manifest.permission.POST_NOTIFICATIONS             val granted = android.content.pm.PackageManager.PERMISSION_GRANTED
email/src/main/java/com/vayunmathur/email/platform/BootReceiver.kt
diff --git a/email/src/main/java/com/vayunmathur/email/platform/BootReceiver.kt b/email/src/main/java/com/vayunmathur/email/platform/BootReceiver.ktindex 2786aa42c7..28baec9423 100644--- a/email/src/main/java/com/vayunmathur/email/platform/BootReceiver.kt+++ b/email/src/main/java/com/vayunmathur/email/platform/BootReceiver.kt@@ -10,6 +10,7 @@ import com.vayunmathur.email.data.ImapIdleService import com.vayunmathur.email.data.OutboxSendWorker import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.launch  /**@@ -34,7 +35,7 @@ class BootReceiver : BroadcastReceiver() {             "android.intent.action.QUICKBOOT_POWERON" -> {                 val pending = goAsync()                 val appContext = context.applicationContext-                CoroutineScope(Dispatchers.IO).launch {+                CoroutineScope(SupervisorJob() + Dispatchers.IO).launch {                     try {                         Log.debug(TAG, "Boot (${intent.action}): restarting hourly + one-off + outbox")                         EmailSyncWorker.scheduleHourlyNonInboxSync(appContext)
everysync/src/main/java/com/vayunmathur/everysync/EverySyncApplication.kt
diff --git a/everysync/src/main/java/com/vayunmathur/everysync/EverySyncApplication.kt b/everysync/src/main/java/com/vayunmathur/everysync/EverySyncApplication.ktindex 36787ea657..0fd66975fb 100644--- a/everysync/src/main/java/com/vayunmathur/everysync/EverySyncApplication.kt+++ b/everysync/src/main/java/com/vayunmathur/everysync/EverySyncApplication.kt@@ -6,8 +6,9 @@ import com.vayunmathur.everysync.sync.SnapshotWorker import com.vayunmathur.everysync.sync.SyncScheduler import com.vayunmathur.library.network.NetworkClient import com.vayunmathur.library.network.TrustBundle+import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers-import kotlinx.coroutines.GlobalScope+import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.launch  /**@@ -16,6 +17,8 @@ import kotlinx.coroutines.launch  * keeps running without the user reopening the UI.  */ class EverySyncApplication : Application() {+    private val appScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)+     override fun onCreate() {         super.onCreate()         // FIRST_PARTY covers the relay host (same bundle the office twin uses).@@ -24,7 +27,7 @@ class EverySyncApplication : Application() {         SnapshotWorker.scheduleDaily(this)         // One-time: seal any plaintext vault keys stored before the Keystore         // migration. Idempotent; runs on every process start, cheap when done.-        GlobalScope.launch(Dispatchers.IO) {+        appScope.launch {             runCatching { VaultStore.migrateKeysToKeystore(this@EverySyncApplication) }         }     }
screentime/src/main/java/com/vayunmathur/screentime/platform/Receivers.kt
diff --git a/screentime/src/main/java/com/vayunmathur/screentime/platform/Receivers.kt b/screentime/src/main/java/com/vayunmathur/screentime/platform/Receivers.ktindex 50a5d4da50..518eb805ee 100644--- a/screentime/src/main/java/com/vayunmathur/screentime/platform/Receivers.kt+++ b/screentime/src/main/java/com/vayunmathur/screentime/platform/Receivers.kt@@ -6,6 +6,7 @@ import android.content.Intent import com.vayunmathur.library.log.Log import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.launch  private const val TAG = "ScreenTimeReceiver"@@ -28,7 +29,7 @@ class TimerReachedReceiver : BroadcastReceiver() {         }         val app = context.applicationContext         val pending = goAsync()-        CoroutineScope(Dispatchers.IO).launch {+        CoroutineScope(SupervisorJob() + Dispatchers.IO).launch {             try {                 Coordinator(app).onTimerReached(packageName)             } catch (e: Exception) {@@ -65,7 +66,7 @@ class ScheduleReceiver : BroadcastReceiver() {     override fun onReceive(context: Context, intent: Intent) {         val app = context.applicationContext         val pending = goAsync()-        CoroutineScope(Dispatchers.IO).launch {+        CoroutineScope(SupervisorJob() + Dispatchers.IO).launch {             try {                 Coordinator(app).reconcile()             } catch (e: Exception) {@@ -102,7 +103,7 @@ class BootReceiver : BroadcastReceiver() {         }         val app = context.applicationContext         val pending = goAsync()-        CoroutineScope(Dispatchers.IO).launch {+        CoroutineScope(SupervisorJob() + Dispatchers.IO).launch {             try {                 Coordinator(app).reconcile()             } catch (e: Exception) {