ForegroundStartPolicy

Decides whether starting MeshService as a foreground service is legal, and which foreground service types it may claim.

Deliberately pure: every platform fact arrives as a parameter so the rules can be unit tested without a device. The Android probes that supply those facts live in ForegroundStartContext.kt.

Two independent platform gates are modelled here.

  1. Background-start restriction (Android 12+). startForegroundService() throws ForegroundServiceStartNotAllowedException unless the caller is in an allowlisted context. Covered by isForegroundStartAllowed.

  2. While-in-use type restriction (Android 14+). A location foreground service cannot be started from the background without ACCESS_BACKGROUND_LOCATION, which this app does not declare; attempting it throws SecurityException. connectedDevice carries no such restriction. Covered by foregroundServiceType.

Functions

Link copied to clipboard
fun fallbackForegroundServiceType(sdkInt: Int = Build.VERSION.SDK_INT): Int

The fallback type used when foregroundServiceType is still rejected at runtime. Always safe to claim when the service may run at all.

Link copied to clipboard
fun foregroundServiceType(hasLocationPermission: Boolean, appInForeground: Boolean, sdkInt: Int = Build.VERSION.SDK_INT): Int

Returns the foreground service type bitmask to pass to startForeground().

Link copied to clipboard
fun isForegroundStartAllowed(trigger: ServiceStartTrigger, appInForeground: Boolean, sdkInt: Int = Build.VERSION.SDK_INT): Boolean

Returns true when Context.startForegroundService() is expected to succeed for trigger.