<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aditya</title>
    <description>The latest articles on DEV Community by Aditya (@ad1tya_24).</description>
    <link>https://dev.to/ad1tya_24</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4255045%2F16a2e228-9ecd-4236-871d-c7b3a47c8a5e.png</url>
      <title>DEV Community: Aditya</title>
      <link>https://dev.to/ad1tya_24</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ad1tya_24"/>
    <language>en</language>
    <item>
      <title>Why Modern Screen Time is Mathematically Flawed: Building a Local-First Cross-Device Interval Engine in Python and Kotlin</title>
      <dc:creator>Aditya</dc:creator>
      <pubDate>Fri, 20 Nov 2026 07:43:33 +0000</pubDate>
      <link>https://dev.to/ad1tya_24/why-modern-screen-time-is-mathematically-flawed-building-a-local-first-cross-device-interval-4nmf</link>
      <guid>https://dev.to/ad1tya_24/why-modern-screen-time-is-mathematically-flawed-building-a-local-first-cross-device-interval-4nmf</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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").&lt;/p&gt;

&lt;p&gt;To solve this, I built &lt;strong&gt;OmniScreen&lt;/strong&gt;: 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.&lt;/p&gt;

&lt;p&gt;Here is a breakdown of the mathematics, the local-first architecture, and the systems engineering behind it.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. The Mathematical Problem: Scalar Sum vs. Interval Union
&lt;/h2&gt;

&lt;p&gt;Every operating system measures screen activity as discrete active sessions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Session S = [t_start, t_end]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When aggregating screen time across multiple devices, existing digital wellbeing dashboards compute the naive sum:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Raw Exposure = Duration(Device 1) + Duration(Device 2) + ... + Duration(Device N)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When multiple devices are active simultaneously (for example, your laptop display is active while you check notifications on your phone), this naive scalar addition double-counts the overlapping period.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Correct Formulation: Interval Union
&lt;/h3&gt;

&lt;p&gt;True screen exposure is the measure of the mathematical union of all session intervals across all devices along a single real timeline:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;True Exposure = Measure( Union of all [t_start, t_end] across Device 1 .. N )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;From this, we can derive the exact duration of concurrent multi-screening:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Second-Screening Duration = Raw Exposure - True Exposure
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We also compute an &lt;strong&gt;Attention Fragmentation Index&lt;/strong&gt; (or Second-Screening Index, SSI):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Attention Fragmentation Index (SSI) = ( Second-Screening Duration / True Exposure ) * 100%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A high SSI indicates fragmented cognitive attention, where your focus was repeatedly split across multiple glowing displays simultaneously.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Implementing the Interval Union in O(N log N)
&lt;/h2&gt;

&lt;p&gt;To compute the interval union efficiently on a low-power local background daemon, we use a sorted sweep-line interval merge algorithm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;calculate_interval_union&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sessions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;tuple&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""
    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.
    """&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;sessions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;

    &lt;span class="c1"&gt;# 1. Sort intervals by start timestamp&lt;/span&gt;
    &lt;span class="n"&gt;sorted_intervals&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sessions&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;merged&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;current_start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current_end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sorted_intervals&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;sorted_intervals&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;current_end&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="c1"&gt;# Overlapping or contiguous interval: extend the current boundary&lt;/span&gt;
            &lt;span class="n"&gt;current_end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current_end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="c1"&gt;# Disjoint interval: commit previous interval and advance&lt;/span&gt;
            &lt;span class="n"&gt;merged&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current_start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current_end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;current_start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current_end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;

    &lt;span class="n"&gt;merged&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current_start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current_end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# 2. Sum the durations of disjoint merged intervals&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;merged&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running this on thousands of raw session intervals takes less than 2 milliseconds on standard hardware, eliminating any CPU overhead on the host machine.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. System Architecture: Local-First with SQLite WAL
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;OmniScreen is architected strictly as a &lt;strong&gt;local-first system&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+-------------------------------------------------------------+
|                      Local Wi-Fi Network                    |
|                                                             |
|   +-------------------+              +------------------+   |
|   |   Android Agent   |  UDP Probe   |  Windows PC Hub  |   |
|   |  (Kotlin Sensor)  | -----------&amp;gt; | (FastAPI+SQLite) |   |
|   |                   | &amp;lt;----------- |                  |   |
|   |                   |  HTTP Sync   |                  |   |
|   +-------------------+              +------------------+   |
|                                                |            |
|                                                v            |
|                                         +--------------+    |
|                                         | SQLite (WAL) |    |
|                                         | Local Storage|    |
|                                         +--------------+    |
+-------------------------------------------------------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Database Concurrency
&lt;/h3&gt;

&lt;p&gt;The local PC hub uses SQLite with &lt;strong&gt;Write-Ahead Logging (WAL)&lt;/strong&gt; enabled:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"PRAGMA journal_mode=WAL"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"PRAGMA synchronous=NORMAL"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"PRAGMA busy_timeout=15000"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;WAL mode allows concurrent readers (such as the web dashboard querying metrics) while background agent syncs write incoming sessions without locking the database file.&lt;/li&gt;
&lt;li&gt;All administrative and mutating endpoints (&lt;code&gt;/api/device/delete&lt;/code&gt;, &lt;code&gt;/api/device/rename&lt;/code&gt;, &lt;code&gt;/api/tv/add&lt;/code&gt;) enforce loopback checks (&lt;code&gt;require_localhost&lt;/code&gt;), ensuring external network requests cannot modify device records even on an open Wi-Fi network.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  4. Native Device Sensors
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Windows Win32 Agent
&lt;/h3&gt;

&lt;p&gt;The desktop background agent runs quietly in the Windows System Tray with zero terminal windows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Uses &lt;code&gt;ctypes&lt;/code&gt; to call Win32 APIs: &lt;code&gt;GetForegroundWindow()&lt;/code&gt;, &lt;code&gt;GetWindowTextW()&lt;/code&gt;, and &lt;code&gt;GetWindowThreadProcessId()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Implements tick-gap detection: rather than assuming continuous usage, the polling loop calculates elapsed wall-clock time between iterations. If the system sleeps, hibernates, or locks, the gap is automatically detected and excluded from active session recording.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Android Companion Sensor
&lt;/h3&gt;

&lt;p&gt;On Android, query polling drains battery. Instead, OmniScreen uses native Kotlin with &lt;code&gt;UsageStatsManager.queryEvents()&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Queries raw OS events (&lt;code&gt;UsageEvents.Event.ACTIVITY_RESUMED&lt;/code&gt; and &lt;code&gt;UsageEvents.Event.ACTIVITY_PAUSED&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Clamps event boundaries strictly to local midnight (&lt;code&gt;00:00:00&lt;/code&gt;) to prevent multi-day leakage.&lt;/li&gt;
&lt;li&gt;Filters out system launchers and navigation overlays, tracking only genuine foreground applications.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  5. Local Wi-Fi Auto-Discovery: Eliminating Cloud Relays
&lt;/h2&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;UDP Broadcast Auto-Discovery:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;The Android client broadcasts a lightweight discovery payload over UDP port &lt;code&gt;8766&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The PC hub listens on &lt;code&gt;0.0.0.0:8766&lt;/code&gt; and responds with its local LAN IP address and HTTP port (&lt;code&gt;8765&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;The Android client parses the response and initiates automated local syncing.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;1-Tap QR Fallback:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;The desktop dashboard generates a local QR code encoding &lt;code&gt;omniscreen://pair?host=&amp;lt;LAN_IP&amp;gt;:8765&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Scanning the QR code with the phone camera deep-links directly into the Android app and establishes pairing in less than 2 seconds.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  6. Open Source &amp;amp; Key Takeaways
&lt;/h2&gt;

&lt;p&gt;Building OmniScreen reinforced several systems engineering principles:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Always question basic domain math:&lt;/strong&gt; The double-counting error in digital wellbeing metrics had existed unchallenged simply because naive summation is easier to compute than interval unions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Local-first is performant and viable:&lt;/strong&gt; Complex multi-device synchronization does not require cloud infrastructure or subscriptions. Local networks and SQLite WAL handle modern workloads effortlessly with zero latency.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security through minimal surface area:&lt;/strong&gt; By refusing external cloud relays, all data remains on your physical hardware under your control.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;OmniScreen is 100% free and open source under the &lt;strong&gt;GNU General Public License v3.0 (GPL-3.0)&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;GitHub Repository:&lt;/strong&gt; &lt;a href="https://github.com/aditya8242/omniscreen-agent" rel="noopener noreferrer"&gt;https://github.com/aditya8242/omniscreen-agent&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Windows &amp;amp; Android Release:&lt;/strong&gt; &lt;a href="https://github.com/aditya8242/omniscreen-agent/releases/tag/v0.9-beta" rel="noopener noreferrer"&gt;v0.9-beta Releases&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Contributions, feedback on the interval math, and hardware compatibility reports are welcome on the GitHub issue tracker.&lt;/p&gt;

</description>
      <category>python</category>
      <category>android</category>
      <category>opensource</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
