<?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: Madhav Kwatra</title>
    <description>The latest articles on DEV Community by Madhav Kwatra (@madhav_kwatra).</description>
    <link>https://dev.to/madhav_kwatra</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%2F1554573%2F6f5419ab-2686-42c6-937c-0820b54f34d2.jpg</url>
      <title>DEV Community: Madhav Kwatra</title>
      <link>https://dev.to/madhav_kwatra</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/madhav_kwatra"/>
    <language>en</language>
    <item>
      <title>How Android stores your screen time (and how a battery replacement erased mine, twice)</title>
      <dc:creator>Madhav Kwatra</dc:creator>
      <pubDate>Fri, 20 Nov 2026 00:00:00 +0000</pubDate>
      <link>https://dev.to/madhav_kwatra/how-android-stores-your-screen-time-and-how-a-battery-replacement-erased-mine-twice-ged</link>
      <guid>https://dev.to/madhav_kwatra/how-android-stores-your-screen-time-and-how-a-battery-replacement-erased-mine-twice-ged</guid>
      <description>&lt;p&gt;I built PhoneRecap, an Android app that turns your phone usage into a year-in-review story: total hours, top apps, your busiest month, late nights. To do that I had to learn where Android actually keeps your usage data, how long it keeps it, and how easily it loses it. My own phone lost its history &lt;strong&gt;twice&lt;/strong&gt; this year.&lt;/p&gt;

&lt;p&gt;Here is what I learned, from the storage side to the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Where Android keeps your usage data
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://madhavkwatra.in/static/862f8f2658d341c6e14d8f06244ae384/b46c2/memory-2-wide.jpg" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2rsw7v3orjixlxmux493.jpg" alt="Android keeps detail about each app open for about 10 days, weekly totals for 4 weeks, monthly totals for 6 months and yearly totals for about 2 years" width="700" height="394"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Android records usage in two very different forms. Apps read both through the &lt;code&gt;UsageStatsManager&lt;/code&gt; API (application programming interface), the same source Android's own Digital Wellbeing uses:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Events&lt;/strong&gt; are the detail: an app came to the front, went to the back, the screen turned on, the phone was unlocked, a notification arrived. With events you can rebuild sessions, unlocks, the hour you're busiest, and your longest break from the phone.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Summaries&lt;/strong&gt; are totals per app for a time bucket: how long each app was in front, and how often it was opened.&lt;/p&gt;

&lt;p&gt;Android keeps them for very different times. These numbers come from the clean-up rules in AOSP (the Android Open Source Project, the public code Android is built from):&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;What&lt;/th&gt;
&lt;th&gt;Kept for about&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Events (the detail)&lt;/td&gt;
&lt;td&gt;10 days&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Daily summaries&lt;/td&gt;
&lt;td&gt;10 days&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Weekly summaries&lt;/td&gt;
&lt;td&gt;4 weeks&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Monthly summaries&lt;/td&gt;
&lt;td&gt;6 months&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Yearly summaries&lt;/td&gt;
&lt;td&gt;2 years (one AOSP change note says 3)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;So a few days back you can see &lt;em&gt;when&lt;/em&gt; you used an app. Months back you can only see &lt;em&gt;how much&lt;/em&gt;, per month.&lt;/p&gt;

&lt;p&gt;What this means for a year-in-review:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;On the day you install it, a recap app can already show most of your year&lt;/strong&gt; , from Android's monthly totals: total time, top apps, month by month. My sister installed PhoneRecap in October and her recap started in May, 6 months back, exactly the monthly window.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The detail is gone after ~10 days&lt;/strong&gt; , so things like "your late nights" or "your longest phone-free break" only exist for days the app saved itself. PhoneRecap saves every day into its own database on the phone and labels every number "exact" (saved by the app) or "≈" (from Android's totals).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. My phone lost all of it, twice
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://madhavkwatra.in/static/df728b8dd9e2466f488bf537081becda/b46c2/memory-4-wide.jpg" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frt59sot7ebbpxrn7wy51.jpg" alt="Timeline: battery replaced in June, history restarts on 26 June; replaced again in October, everything restarts, even the yearly totals; my sister's phone without a repair keeps months of history" width="700" height="394"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While testing, my recap always started on 26 June, while my sister's went back months further. Android's own files on my phone (read with &lt;code&gt;adb&lt;/code&gt;, the Android Debug Bridge tool for developers) showed why: the daily, weekly, monthly and yearly data &lt;strong&gt;all began at the same millisecond&lt;/strong&gt; , 26 June 12:30. Normal clean-up removes old files one by one; everything starting at once means the history was &lt;strong&gt;re-created from empty&lt;/strong&gt;. That was right after my phone's battery was replaced.&lt;/p&gt;

&lt;p&gt;In October the new battery swelled and was replaced again. Same result: &lt;strong&gt;every bucket restarted at the first boot after the repair&lt;/strong&gt; , the 10 days of detail and the weekly, monthly and yearly totals alike. Android itself had nothing from before that boot any more.&lt;/p&gt;

&lt;p&gt;PhoneRecap survived it because it keeps its own copies: every saved day, and a copy of Android's monthly and yearly totals since 26 June. Only the ~1.5 days it hadn't saved yet were lost, because they existed only in Android's log.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Why: the phone's clock
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://madhavkwatra.in/static/a7ceaf1c0650f3dab3322bbbee5f632b/b46c2/memory-3-wide.jpg" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fq9fczoi7vvm9a67f7qx0.jpg" alt="Two phones: with the battery in, the clock chip keeps time; with the battery out, the clock forgets, and Android then treats your history as from the future and deletes it" width="700" height="394"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A phone keeps time with an &lt;strong&gt;RTC (real-time clock)&lt;/strong&gt;: a tiny circuit that keeps counting even when the phone is off. It sits in the phone's power chip and runs on the &lt;strong&gt;main battery&lt;/strong&gt;. Take the battery out for a repair, and the RTC forgets the time.&lt;/p&gt;

&lt;p&gt;The phone then starts with a &lt;strong&gt;fallback date&lt;/strong&gt; until it gets the real time from the network. Which date depends on the phone and its software: often the date the software was built, sometimes a fixed old date. In October my phone briefly thought it was 1 January 2010.&lt;/p&gt;

&lt;p&gt;Android's usage database doesn't like that. At startup it &lt;strong&gt;deletes files that look like they are from the future&lt;/strong&gt; (it's in the AOSP code), and with the clock in the past, everything you recorded looks like it's from the future. A power cut that corrupts the database's own files can also make Android wipe it and start over. Which one hit my phone can't be proven without system logs (my phone's maker turns them off); the result was the same. I found this rule by going through the AOSP code with Claude Code, an AI coding assistant: it read the relevant Android source files with me and pointed at the "future files" check.&lt;/p&gt;

&lt;p&gt;Computers have the same weak spot. A PC keeps its clock and BIOS (basic input/output system) settings alive with a small coin battery, the CMOS (complementary metal-oxide-semiconductor) battery. On my old PC, a 3 GB RAM (random-access memory) AMD Athlon machine, the date reset on every start and I typed it in by hand. On the PC I bought around 2020, the date kept falling back to around 2022 and the RAM dropped from 3200 to about 2666 MHz (megahertz), because its speed profile lives in the same memory. A new coin battery fixed it. A phone has no separate coin battery, so &lt;strong&gt;every battery replacement is that moment&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://madhavkwatra.in/static/f5b33a2fd2b71b958e8c6c0807e0b408/b46c2/memory-5-wide.jpg" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fehrftqrr5li5zw5ibk83.jpg" alt="A PC motherboard with its small coin battery, which keeps the clock and settings; when it dies, the date and settings like RAM speed reset" width="700" height="394"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. What I built because of it
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Save every day.&lt;/strong&gt; One row per day in SQLite (a small database built into Android) on the phone, from a daily background job plus every time the app opens. A day is safe only once the app has saved it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Honest numbers.&lt;/strong&gt; Each card says where its data starts ("since 26 Jun") and whether it's exact or approximate.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A warning when saving stops.&lt;/strong&gt; Some phones (my vivo; Xiaomi is known for it) stop background work for apps they think are idle. The app shows which days it couldn't save and links to the phone's battery setting. A home-screen widget helps too: on my vivo its 30-minute refresh wakes the app even after the system stopped it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. Counting screen time from events: the traps
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;UsageStatsManager.queryEvents()&lt;/code&gt; gives you the event stream; you build sessions yourself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;usm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;queryEvents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;startMs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;endMs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;UsageEvents&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Event&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hasNextEvent&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="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getNextEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;eventType&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;UsageEvents&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;ACTIVITY_RESUMED&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;openSession&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;packageName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timeStamp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nc"&gt;UsageEvents&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;ACTIVITY_PAUSED&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;closeSession&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;packageName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timeStamp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nc"&gt;UsageEvents&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;SCREEN_NON_INTERACTIVE&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;closeAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timeStamp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nc"&gt;UsageEvents&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;KEYGUARD_HIDDEN&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;unlocks&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What bit me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Screen off must close every session.&lt;/strong&gt; Some phones send "paused" late or never. Wait for it and the whole night counts as Instagram time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;An app can be "resumed" while the screen is off.&lt;/strong&gt; Count nothing until the screen comes on.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A dead battery sends no shutdown event.&lt;/strong&gt; The next startup must close whatever was open.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Total screen time ≠ the sum of app times.&lt;/strong&gt; Split screen puts two apps in front at once; use screen-on time for the total.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;An app already open before your first event&lt;/strong&gt; only shows up as "paused". Start it at the oldest event you have, not at midnight.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Split time at local hour boundaries&lt;/strong&gt; , so daylight-saving days and "busiest hour" charts stay right.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every rule is written at the top of the Kotlin file and has a JUnit test (an automated unit test), because each one was a real bug once.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. No internet permission, for real
&lt;/h2&gt;

&lt;p&gt;PhoneRecap's release build has no INTERNET permission: no backend server, no analytics, no crash-reporting SDK (software development kit). The app is React Native + Expo with a Kotlin module, and debug builds need INTERNET for the dev server, so I remove it only in the release manifest (written by an Expo config plugin):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- android/app/src/release/AndroidManifest.xml --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;manifest&lt;/span&gt; &lt;span class="na"&gt;xmlns:android&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"http://schemas.android.com/apk/res/android"&lt;/span&gt;
          &lt;span class="na"&gt;xmlns:tools&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"http://schemas.android.com/tools"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;uses-permission&lt;/span&gt; &lt;span class="na"&gt;android:name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"android.permission.INTERNET"&lt;/span&gt; &lt;span class="na"&gt;tools:node&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"remove"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/manifest&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then check the built file, not the source: &lt;code&gt;aapt2 dump permissions app-release.apk&lt;/code&gt; (aapt2 is Android's Asset Packaging Tool; the APK is the installable app file) must not list INTERNET (a library can merge it back in). My release script fails the build if it does. Users can check too: the Play page shows no "full network access".&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd tell anyone working with usage data
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://madhavkwatra.in/static/2da34fe5890d77f89feafc2498bc6e7d/b46c2/memory-6-wide.jpg" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1b2u213cr9g4ntxm0cc5.jpg" alt="What helps: expect history to restart after a battery repair, totals stay for months, and an app that saves each day keeps the detail" width="700" height="394"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Android keeps detail for ~10 days and monthly totals for ~6 months. Plan for both.&lt;/li&gt;
&lt;li&gt;Save what you need yourself, every day.&lt;/li&gt;
&lt;li&gt;Expect the history to vanish after a battery replacement or a big clock jump.&lt;/li&gt;
&lt;li&gt;Label approximate numbers honestly.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you've found a reliable way to keep background work alive on Xiaomi, vivo or Samsung, I'd love to hear it in the comments.&lt;/p&gt;

&lt;p&gt;Want to see your own phone year as a story? PhoneRecap is free on Google Play (Android 10+). It works fully offline: no account, no ads, and your data never leaves your phone.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://play.google.com/store/apps/details?id=com.madhavkwatra.phonerecap&amp;amp;referrer=utm_source%3Ddevto%26utm_campaign%3Darticle2" rel="noopener noreferrer"&gt;PhoneRecap on Google Play&lt;/a&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>privacy</category>
      <category>mobile</category>
    </item>
  </channel>
</rss>
