Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>

Expand Down
32 changes: 22 additions & 10 deletions app/src/main/java/com/mensinator/app/widgets/BootReceiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,35 @@ package com.mensinator.app.widgets
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import androidx.glance.appwidget.updateAll
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.launch

class BootReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
// Use goAsync() to ensure work completes even if receiver would otherwise be killed
val pendingResult = goAsync()
// Use standalone coroutine scope for this one-time operation
CoroutineScope(SupervisorJob() + Dispatchers.IO).launch {
try {
MidnightWorker.scheduleNextMidnight(context)
} finally {
pendingResult.finish()
}
val action = intent.action ?: return
if (action != Intent.ACTION_BOOT_COMPLETED &&
action != "android.intent.action.QUICKBOOT_POWERON" &&
action != "com.htc.intent.action.QUICKBOOT_POWERON"
) return

val pendingResult = goAsync()
val appContext = context.applicationContext
CoroutineScope(SupervisorJob() + Dispatchers.IO).launch {
try {
MidnightWorker.scheduleNextMidnight(appContext)
MidnightTrigger.midnightTrigger.emit(Unit)
WidgetInstances.map { receiver ->
async { receiver.glanceAppWidget.updateAll(appContext) }
}.awaitAll()
} catch (e: Exception) {
android.util.Log.e("BootReceiver", "Failed to refresh widgets on boot", e)
} finally {
pendingResult.finish()
}
}
}
Expand Down
19 changes: 12 additions & 7 deletions app/src/main/java/com/mensinator/app/widgets/MidnightWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,18 @@ class MidnightWorker(val context: Context, params: WorkerParameters) : Coroutine
}

override suspend fun doWork(): Result {
// Wait for 1 second to ensure the worker has time to start
delay(1000)
MidnightTrigger.midnightTrigger.emit(Unit)
WidgetInstances.forEach { it.glanceAppWidget.updateAll(context) }
// Schedule the next update for the following midnight
scheduleNextMidnight(applicationContext)

try {
// Wait for 1 second to ensure the worker has time to start
delay(1000)
MidnightTrigger.midnightTrigger.emit(Unit)
WidgetInstances.forEach { it.glanceAppWidget.updateAll(context) }
} catch (e: Exception) {
android.util.Log.e("MidnightWorker", "Failed to refresh widgets at midnight", e)
} finally {
// Always schedule the next midnight, even if this run failed,
// so the daily cycle keeps going.
scheduleNextMidnight(applicationContext)
}
return Result.success()
}
}
Loading