package eu.friedcutlet.yokan import android.app.Activity import android.app.Application import android.content.Context import eu.friedcutlet.yokan.app.YokanRuntime import eu.friedcutlet.yokan.services.scheduling.ProfileWorkResult import eu.friedcutlet.yokan.services.download.DownloadService import eu.friedcutlet.yokan.services.download.DownloadTaskHost import eu.friedcutlet.yokan.services.scheduling.BackgroundTaskHost import eu.friedcutlet.yokan.services.scheduling.PeriodicTaskRunner import eu.friedcutlet.yokan.services.scheduling.PeriodicTaskOutcome /** * Process-wide application [Context], set by [YokanApplication] before any * component (activity, WorkManager worker, receiver) runs. */ object AndroidAppContext { @Volatile var appContext: Context? = null } /** * The currently resumed-or-created [MainActivity], for APIs that need an * activity window (system bars, permission prompts). Cleared in `onDestroy`. */ object AndroidForegroundActivity { @Volatile var activity: Activity? = null } class YokanApplication : Application(), BackgroundTaskHost, DownloadTaskHost { override fun onCreate() { super.onCreate() AndroidAppContext.appContext = applicationContext } override suspend fun runBackgroundTask( owner: String?, task: suspend PeriodicTaskRunner.() -> PeriodicTaskOutcome, ): ProfileWorkResult = YokanRuntime.runBackgroundTask(owner, allowLegacyDefault = true, task = task) override suspend fun runBackgroundDownloads( owner: String?, task: suspend DownloadService.() -> Unit, ): ProfileWorkResult = YokanRuntime.runBackgroundDownloads(owner, allowLegacyDefault = true, task = task) }