Why Modern Screen Time is Mathematically Flawed

Building a local-first cross-device interval engine in Python and Kotlin

Most screen time tools suffer from a fundamental mathematical flaw: they compute total screen exposure by summing independent device scalars rather than calculating the union of overlapping time intervals.

If you spend two hours working on a laptop while occasionally responding to messages on your phone for 30 minutes, standard digital wellbeing trackers report 2 hours and 30 minutes of screen time. Your biological eyes were exposed to displays for exactly 2 hours, with 30 minutes spent in a state of multi-device fragmentation ("second-screening").

To solve this, I built OmniScreen: an open-source, local-first engine for Windows and Android that synchronizes active screen sessions over your local Wi-Fi network and computes the mathematical interval union with zero cloud servers and zero telemetry.

1. The Mathematical Problem: Scalar Sum vs. Interval Union

Every operating system measures screen activity as discrete active sessions:

Session S = [t_start, t_end]

When aggregating screen time across multiple devices, existing digital wellbeing dashboards compute the naive sum:

Raw Exposure = Duration(Device 1) + Duration(Device 2) + ... + Duration(Device N)

When multiple devices are active simultaneously, this naive scalar addition double-counts the overlapping period.

The Correct Formulation: Interval Union

True screen exposure is the measure of the mathematical union of all session intervals across all devices along a single real timeline:

True Exposure = Measure( Union of all [t_start, t_end] across Device 1 .. N )

Where overlapping intervals are merged into continuous spans so that 1 minute of real wall-clock time never counts as more than 1 minute of physical screen exposure.

From this, we derive the exact duration of concurrent multi-screening:

Second-Screening Duration = Raw Exposure - True Exposure

We also compute an Attention Fragmentation Index (or Second-Screening Index, SSI):

Attention Fragmentation Index (SSI) = ( Second-Screening Duration / True Exposure ) * 100%

A high SSI indicates fragmented cognitive attention, where your focus was repeatedly split across multiple glowing displays simultaneously.

2. Implementing the Interval Union in O(N log N)

To compute the interval union efficiently on a low-power local background daemon, we use a sorted sweep-line interval merge algorithm:

def calculate_interval_union(sessions: list[tuple[float, float]]) -> float:
    """
    Computes the total non-overlapping duration from a list of [start_ts, end_ts] intervals.
    Time Complexity: O(N log N) due to sorting.
    Space Complexity: O(N) to store merged intervals.
    """
    if not sessions:
        return 0.0

    # 1. Sort intervals by start timestamp
    sorted_intervals = sorted(sessions, key=lambda x: x[0])

    merged = []
    current_start, current_end = sorted_intervals[0]

    for start, end in sorted_intervals[1:]:
        if start <= current_end:
            # Overlapping or contiguous interval: extend the current boundary
            current_end = max(current_end, end)
        else:
            # Disjoint interval: commit previous interval and advance
            merged.append((current_start, current_end))
            current_start, current_end = start, end

    merged.append((current_start, current_end))

    # 2. Sum the durations of disjoint merged intervals
    return sum(end - start for start, end in merged)

Running this on thousands of raw session intervals takes less than 2 milliseconds on standard hardware, eliminating CPU overhead on the host machine.

3. System Architecture: Local-First with SQLite WAL

Screen time data is among the most sensitive personal telemetry possible: it records the exact minute you wake up, every application you switch to, and when you go to bed. Storing this on third-party cloud servers poses unacceptable privacy risks.

OmniScreen is architected strictly as a local-first system:

+-------------------------------------------------------------+
|                      Local Wi-Fi Network                    |
|                                                             |
|   +-------------------+              +------------------+   |
|   |   Android Agent   |  UDP Probe   |  Windows PC Hub  |   |
|   |  (Kotlin Sensor)  | -----------> | (FastAPI+SQLite) |   |
|   |                   | <----------- |                  |   |
|   |                   |  HTTP Sync   |                  |   |
|   +-------------------+              +------------------+   |
|                                                |            |
|                                                v            |
|                                         +--------------+    |
|                                         | SQLite (WAL) |    |
|                                         | Local Storage|    |
|                                         +--------------+    |
+-------------------------------------------------------------+

The local PC hub uses SQLite with Write-Ahead Logging (WAL) enabled:

conn.execute("PRAGMA journal_mode=WAL")
conn.execute("PRAGMA synchronous=NORMAL")
conn.execute("PRAGMA busy_timeout=15000")

4. Native Device Sensors

Windows Win32 Agent

The desktop background agent runs quietly in the Windows System Tray with zero terminal windows:

Android Companion Sensor

On Android, query polling drains battery. Instead, OmniScreen uses native Kotlin with UsageStatsManager.queryEvents():

5. Local Wi-Fi Auto-Discovery: Eliminating Cloud Relays

To allow the Android app to communicate with the PC hub without requiring static IP configurations or central accounts, OmniScreen implements a dual-discovery protocol:

  1. UDP Broadcast Auto-Discovery: The Android client broadcasts a lightweight discovery payload over UDP port 8766. The PC hub listens on 0.0.0.0:8766 and responds with its local LAN IP address and HTTP port (8765).
  2. 1-Tap QR Fallback: The desktop dashboard generates a local QR code encoding omniscreen://pair?host=<LAN_IP>:8765. Scanning the QR code deep-links directly into the Android app and establishes pairing in less than 2 seconds.

6. Key Takeaways & Open Source

Building OmniScreen reinforced several systems engineering principles:

OmniScreen is 100% free and open source under the GNU General Public License v3.0 (GPL-3.0).

→ GitHub Repository: https://github.com/aditya8242/omniscreen-agent
→ Windows & Android Release: v0.9-beta Releases

Aditya Chavan Systems & Backend Software Engineer · Pune, India

Undergraduate specializing in Linux internals, distributed systems, and local-first software. Creator of OmniScreen.

Connect ↗
← Back to all essays Portfolio Home →