Most app blockers on Android work at the app level: Instagram is either allowed or blocked. I wanted something narrower. Instagram DMs should work and Reels should stop. YouTube tutorials should work and Shorts should stop.
So I built Focsy: Doomscrolling Blocker. You can block a whole app, or only the short-video part of it. Here's how the core works, what kept breaking, and what I'd tell anyone building something similar.
Why AccessibilityService
Android gives a normal app no way to see what's on screen inside another app. The only API that can is AccessibilityService. It receives events when windows change and can read the view tree of the app in front.
That power is exactly why it's sensitive. Three rules I set before shipping:
- Only blocking. The service never stores or sends screen content.
- No third-party SDKs anywhere near it.
- The user turns it on themselves, after a clear explanation of what it does. Google Play also requires you to declare how you use the Accessibility API and to show a prominent disclosure before asking for it.
Declaring the service
The manifest entry:
<service android:name=".blocking.BlockerAccessibilityService" android:exported="false" android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"> <intent-filter> <action android:name="android.accessibilityservice.AccessibilityService" /> </intent-filter> <meta-data android:name="android.accessibilityservice" android:resource="@xml/accessibility_config" /> </service> And the config in res/xml/accessibility_config.xml:
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android" android:accessibilityEventTypes="typeWindowStateChanged|typeWindowContentChanged" android:accessibilityFeedbackType="feedbackGeneric" android:canRetrieveWindowContent="true" android:notificationTimeout="300" android:description="@string/accessibility_description" /> notificationTimeout matters more than it looks. Feeds fire content-change events constantly while you scroll. Without throttling, the service does far too much work and drains the battery.
It also helps to narrow which apps the service listens to. AccessibilityServiceInfo.packageNames can be updated at runtime with setServiceInfo(), so the service only receives events from apps that actually have a rule.
Blocking a screen
Simplified, the core loop looks like this:
class BlockerAccessibilityService : AccessibilityService() { override fun onAccessibilityEvent(event: AccessibilityEvent) { val pkg = event.packageName?.toString() ?: return val rule = rules.forPackage(pkg) ?: return val shouldBlock = when { rule.blocksWholeApp() -> rule.isActiveNow() rule.blocksShortVideoFeed() -> isShortVideoFeed(pkg, rootInActiveWindow) else -> false } if (shouldBlock) block(pkg) } private fun block(pkg: String) { startActivity( Intent(this, BlockActivity::class.java) .putExtra("blocked_package", pkg) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) ) performGlobalAction(GLOBAL_ACTION_HOME) } override fun onInterrupt() = Unit } isActiveNow() covers the three kinds of whole-app rules: always on, on a schedule, or after a daily time limit.
Telling Reels apart from the rest of Instagram
This is the hard part, and the part that keeps breaking. rootInActiveWindow gives you the view tree of the app in front, and you look for signals that you're on the short-video screen.
private fun isShortVideoFeed(pkg: String, root: AccessibilityNodeInfo?): Boolean { root ?: return false return detectors[pkg]?.matches(root) == true } I won't list the exact signals, because they change all the time. But the principles hold:
- Never rely on one signal. Instagram, YouTube and Facebook change their layouts often, sometimes in A/B tests that only some users see. Matching several signals together survives small redesigns.
- Keep detection rules easy to update. You will be shipping fixes for other apps' redesigns.
- Test across app versions, not just the latest. Users don't all update at once.
- Treat every surface separately. A Reel can open from the Reels tab, from Explore, or from a link in DMs. Each one is its own case.
Blocking websites, subdomains and renamed sites
For websites, the service reads the address bar of supported browsers. In Chrome, that's the node with the view ID com.android.chrome:id/url_bar. Other browsers use their own IDs, so each needs its own entry.
Matching needs two details people forget: subdomains, and sites that change their name.
private val aliases = mapOf( "twitter.com" to setOf("x.com"), "x.com" to setOf("twitter.com"), ) fun isBlocked(host: String, blocked: Set<String>): Boolean { val h = host.lowercase().removePrefix("www.") val domains = blocked.flatMap { setOf(it) + aliases[it].orEmpty() } return domains.any { d -> h == d || h.endsWith(".$d") } } Block reddit.com and old.reddit.com is blocked too. Block twitter.com and x.com is blocked too.
Usage stats from Android's own data
For the dashboard and the daily goal, Focsy reads Android's own usage data through UsageStatsManager. The user grants Usage access in Settings, and the app doesn't have to track anything itself.
val usm = getSystemService(UsageStatsManager::class.java) val stats = usm.queryAndAggregateUsageStats(startOfDay, System.currentTimeMillis()) val minutesByApp = stats.mapValues { it.value.totalTimeInForeground / 60_000 } The problems nobody warns you about
- Phone makers kill background services. Xiaomi, Huawei, Samsung and others have aggressive battery managers that can stop an accessibility service. Your onboarding needs to send users to the right battery settings. dontkillmyapp.com is the best reference for each brand.
- Users can always turn the service off. Android lets anyone disable an accessibility service in Settings, and it should. So "hard to turn off" has to come from the app itself. In Focsy that's Strict Mode, where you complete a challenge before blocking turns off and every disable is counted, and friend approval, where a friend has to accept your unlock request.
- Performance. Content-change events arrive many times per second while scrolling. Throttle them, skip packages without rules, and keep the view-tree walk short.
Try it, or tell me what breaks
Focsy is free on Google Play, and blocking Reels and Shorts is free with no limit. If you're building something with AccessibilityService, ask me anything in the comments. And if Focsy misses a Reels or Shorts screen on your phone, tell me your phone model and app version. That's the most useful bug report I can get.
