Case study · Android device-owner app
The phone that deleted
my alarms
My life was honestly miserable. My sleep was bad, and bad sleep was messing up every other part of my life. I needed something that would force me out of the habit, without being a cage.
What I built
- A Kotlin device-owner app that runs the phone on a schedule: app locks, hotspot and network rules, and a home-screen widget
- Traced TECNO HiOS's freezer silently removing AlarmManager alarms, proved four standard fixes fail, and found the widget exemption
- Redesigned for alarms that don't fire: re-check the schedule on the first sign of life, and trust PowerManager over screen-on broadcasts
01
One week in: wait, this works
So I built a device-owner app, the highest level of control Android gives an app. At night, it locks the phone down so I actually sleep. I used it for a week and I was like, wait, this works. I was sleeping well. I was reading every single day.
So why not do this for every other part of my life where I was relying on willpower? Doing my work when I'm meant to, so I don't end up with a backlog. Even my own excitement: I'm the type that gets an idea and can't sleep until I build it. But sleep is important, so the app manages that too.
02
Then it just stopped working
Each time the app runs, it sets an alarm for the next run, so the schedule is a chain. After a restart, it worked. Then it stopped. My phone was still awake at midnight, and I could use it whenever I wanted, which was the whole thing it was meant to stop. No crash, no error, nothing in my logs.
03
The answer was in someone else's log tag
Reading all of logcat around each run, not just my own lines, turned up one I hadn't written: Hiber/hiber: proxy alarm (uid=…, pkgName=…), about five seconds after every run. Hiber is HiOS's background freezer. It freezes apps that aren't in front and takes their alarms away.
When it unfreezes the app, it gives back the services, the jobs and the sensors. Not the alarms. So the next link in the chain never fires.
I was basically using a phone that was fighting my app.
The schedule is a chain of alarms. Hiber cuts it a few seconds after each run, unless the app has a widget on the home screen.
04
Four fixes that should have worked
These are the standard ways to keep an Android app alive. I tried every one and watched for that proxy alarm line.
A foreground service. Frozen anyway, with the service running.
The battery-optimisation exemption. Frozen anyway.
Device-owner status, the strongest privilege an app can have. Frozen anyway.
The EXEMPTED standby bucket. Frozen anyway.
Moving the work to WorkManager didn't help either. Hiber freezes scheduled jobs the same way.
05
The one thing Hiber respects
A widget on the home screen. With one placed, Hiber logs can't transition from R to F, importantcase is : widget and leaves the app alone. So the widget became part of the app, not decoration. It shows what the schedule is doing, and it's what keeps the schedule alive.
06
Then it broke again with the screen off
The widget only protects the app while the screen is on. Screen off, Hiber freezes it again and holds messages meant for it in a queue that might never deliver them.
There was nothing left to exempt, so I changed the design. The first sign of life after a freeze now makes the app check the schedule again, and it assumes an alarm might not fire instead of assuming it will.
07
Everybody wants one
I've ranted about this app to my whole family and all my friends. My sisters, my mum, my friends, they all said they want it, because it would keep them disciplined. One of my aunts even said I should sell it to parents for their kids.
I made a tiny version for my grandpa's phone. It just locks his phone from 8pm to 8am, because he really needs his sleep. That's it.
Selling it is harder than it sounds, though. It's a device owner, so it takes over the whole phone. If I put it on the Play Store, it would come off as a virus.