Afterword: Kindle highlights on phone home screen
Contents
I highlight a lot on my Kindle, and I almost never look at those highlights again. They pile up in a file on the device, a line here and a paragraph there, while I move on to the next book.
I wanted to view them without my having to go looking. So I built Afterword, a small Android app that puts one highlight on my home screen and swaps it for another every hour. The code is at github.com/manasmehrotra3/afterword, MIT licensed, so build it for yourself or take pieces of it.
There's no Kindle highlights API
Amazon doesn't offer one. That leaves three ways to get your highlights out:
- Amazon's notebook page. Every highlight from books you bought is listed at read.amazon.com/notebook, and the Kindle export browser extensions I looked at all read that page. Pulling it from an app means signing in inside a WebView and scraping it, which Amazon's terms most likely forbid and which breaks whenever the page changes.
- Readwise. It syncs Kindle highlights reliably and has a real API, but it's a paid subscription for something I wanted on one widget.
- My Clippings.txt. The Kindle e-reader appends every highlight, note and bookmark to a plain text file in its
documentsfolder. It's free, it includes sideloaded books that the notebook page never shows, and nothing has to leave the phone.
I went with the my clippings.txt approach. Importing is manual, so new highlights only show up when I import again.
Getting the file onto the phone
I planned to plug the Kindle into the phone with a USB-C cable and pick the file. With USB-C on both ends, though, the phone often charges the Kindle instead of reading it. The Kindle has no setting to change that, because it only ever acts as the device being read, so the phone has to take the host role.
Three things work:
- On the phone, open the USB notification and set USB controlled by to This device and Use USB for to File transfer.
- Use a USB-C to USB-A OTG adapter on the phone with a normal Kindle cable. The adapter tells the phone it's the host, so there's nothing to negotiate. Some phones also hide an OTG toggle in settings that switches itself off after a while.
- Skip the cable. Copy
My Clippings.txtto a computer and send it to the phone withadb push, or through Google Drive, then pick it from there.
Whichever way it arrives, the app's import button opens Android's file picker, and it reads the file you choose.
Reading My Clippings.txt
Each entry is a title line, a metadata line, a blank line, the highlighted text and a row of equals signs:
The Sample Programmer (Doe, Jane;Roe, Richard)
- Your Highlight on page 23 | Location 345-347 | Added on Tuesday, January 2, 2024 9:12:44 PM
Small, careful changes add up.
==========
The parser is about 120 lines of TypeScript, and most of them handle cases the format doesn't make obvious:
- The file is append-only. Extend a highlight on the Kindle and the old version stays, with a new entry added below it. Two entries from the same book with overlapping locations, where one text contains the other, are one highlight, and the newer version wins.
- Notes, bookmarks and the clipping limit. Notes and bookmarks are skipped and counted, so the import can say what it left out. Some publishers cap how much you can clip, and past that point the Kindle writes a placeholder line instead of your text, which gets skipped too.
- Old Kindles shorten locations. "Loc. 1234-56" means 1234 to 1256.
- Dates come in the Kindle's regional format. A US Kindle writes "January 2, 2024 9:12:44 PM" and a UK one writes "2 January 2024 21:12:44".
- Authors are "Last, First". "Doe, Jane;Roe, Richard" becomes Jane Doe, Richard Roe. A title can have parentheses of its own, so only the last pair is the author.
- A byte order mark sits at the start of the file and sometimes before individual entries.
Each highlight gets an id hashed from its book and text, so importing the same file again only adds what's new and leaves the widget's rotation history alone. Thirteen unit tests cover the cases above.
The widget
Widgets can't be written in JavaScript, so the app is Expo and React Native for the screens and Kotlin for the widget. An Expo config plugin writes the Kotlin provider and its layout into the generated Android project at build time. The widget opens the app's SQLite database itself, so it can change highlights without starting the app.
Every time Android asks it to draw, it picks the next highlight. Ones it has never shown come first, then whichever it showed longest ago, and it writes down when it showed each one. That means it works through everything once before repeating. Android redraws it every hour, and tapping the quote redraws it straight away, which is how "next" works. Tapping the book line opens the app.
Sizing the text took three tries:
- Fixed sizes by length. Short quotes at 18sp, long ones at 13sp. A one-line quote left most of the widget empty.
- Android's auto-size text. It grows text to fill its box, but only a box of fixed height, which pinned the book line to the bottom with the gap still above it.
- Measuring in Kotlin. The provider reads the widget's actual size, lays the quote out with
StaticLayoutat each size from 32sp down to 13sp, and uses the largest one that fits, with the book line right under it. Resizing the widget refits the same highlight instead of moving on.
While testing on Android 17, I tried to fake the hourly redraw by sending the widget's update broadcast from adb. The system blocks that broadcast from the shell now, which also means no other app can force the widget to change.
Building it yourself
With Node and the Android SDK installed:
git clone https://github.com/manasmehrotra3/afterword.git
cd afterword
npm install
npm run android
npm run android puts a debug build on a connected phone or emulator. For a copy to keep, npm run apk builds a signed release APK into dist/, using your own signing key from ~/.gradle/gradle.properties, and the README lists the four lines it needs. Sign every update with the same key: Android refuses an update signed with a different one, and uninstalling deletes your highlights.
The release APK is 11.6 MB. It's arm64 only and minified, it leaves out the GIF and WebP decoders React Native would otherwise ship, and it doesn't ask for the internet permission.
What it doesn't do
- Highlights from the Kindle phone app. Those never reach the e-reader's clippings file.
- iPhone. An iPhone can't read a Kindle's files over USB, and the whole import depends on that.
- Other languages. A Kindle set to a language other than English writes a metadata line the parser doesn't read yet. Those entries are counted as skipped.
- Daily sync. New highlights arrive when I import again. Re-importing is safe, so I do it whenever the Kindle is near the phone.
Questions people ask
Does Afterword work with highlights from the Kindle phone app? No. It reads My Clippings.txt, which only the Kindle e-reader writes. Highlights you make in the Kindle app on a phone or tablet never land in that file.
Is it on the Play Store? No. It's sideloaded: build a signed APK from the code on GitHub, then install it with adb or copy the file to the phone and open it.
Does it work on iPhone? No. The whole import depends on the phone reading the Kindle's files over USB, which an iPhone can't do.
Does any of my reading data leave the phone? No. The highlights go from the Kindle into a SQLite file on the phone, and the widget reads them from there. The app makes no network requests, and release builds don't ask for the internet permission.