setForeground method

void setForeground(
  1. bool foreground
)

The app entered (true) or left (false) the foreground. A backgrounded watchdog pauses with its remaining budget; foregrounding re-arms it. iOS wins: a game opened behind a lock screen is never failed for a skin that never got to run. Never throws.

Implementation

void setForeground(bool foreground) {
  try {
    if (foreground == _foreground) return;
    _foreground = foreground;
    if (!foreground) {
      if (_watchdog == null) return;
      final elapsed = _clock.nowMs() - _watchdogArmedAtMs;
      _watchdogRemainingMs -= elapsed;
      if (_watchdogRemainingMs < 0) _watchdogRemainingMs = 0;
      _watchdog?.cancel();
      _watchdog = null;
      return;
    }
    // Foregrounding with a paused budget re-arms it; with a running timer
    // or nothing armed there is nothing to do.
    if (_watchdog != null || _watchdogRemainingMs <= 0) return;
    final remaining = _watchdogRemainingMs;
    _armWatchdog(remaining);
  } catch (_) {}
}