idleFromNanoseconds static method

  1. @visibleForTesting
int idleFromNanoseconds({
  1. required bool succeeded,
  2. required int nanoseconds,
})

The Idle duration implied by one HIDIdleTime reading, including what to report when the walk did not complete.

Truncating division, not rounding. The retired Swift divided integers, so 1.999 ms read as 1 ms; rounding here would be an improvement, and an improvement is a behavior change. A negative reading is clamped — it should be impossible, but a negative idle duration would read to the policy as input arriving in the future.

When succeeded is false the answer is zero — the user is treated as active. The shell catches a thrown read as a transient fault and retries, so throwing on a persistently failing lookup would stall monitoring rather than degrade it.

Implementation

@visibleForTesting
static int idleFromNanoseconds({
  required bool succeeded,
  required int nanoseconds,
}) {
  if (!succeeded || nanoseconds <= 0) return 0;
  return nanoseconds ~/ _nanosecondsPerMillisecond;
}