We're building ReClaim, a minimalist Android launcher that's on Google Play. An earlier post covered blocking websites by reading only the address bar: find the bar by view id, tell typing from visiting, and move a tab off a site. This post is about what happens after the bar matches a hidden site, where the user gets a challenge before the page opens. That part turned out to be harder than the match.
A hidden site is not a block. The user can pass a challenge (the same kind we use for unlock challenges) and then use the site for that visit. So the gate has three jobs: keep the page unusable until the challenge is up, make sure Cancel never becomes a pass, and handle the case where the address bar disappears because a video went fullscreen.
All of this is new in the two most recent app commits on our master branch, and none of it is in the Play build yet. The live listing still shows version 0.6.6.0.1, and the repo is at 0.6.7.3.1. Stack: Kotlin, Jetpack Compose, Hilt, an AccessibilityService. All code below is copied from the repo.
The gap between the match and the challenge
Starting an activity from an accessibility service is not instant. Between seeing youtube.com in the bar and the challenge being in front, the page is on screen and can be tapped. The commit message for the second change says it plainly: "Catch a hidden site before the page can be used."
The fix is a black window the service puts over everything the moment it decides to gate:
private fun showWebsiteCurtain() {
if (curtainView != null) return
val view = View(this).apply {
setBackgroundColor(Color.BLACK)
importantForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_NO
}
val params = WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.OPAQUE,
).apply {
gravity = Gravity.TOP or Gravity.START
}
val added = runCatching {
(getSystemService(WINDOW_SERVICE) as WindowManager).addView(view, params)
}.isSuccess
if (!added) return
curtainView = view
curtainJob?.cancel()
curtainJob = scope.launch {
delay(CURTAIN_HOLD_MS)
withContext(Dispatchers.Main) {
if (!websiteGatePresence.resumed) hideWebsiteCurtain()
}
}
}
The window type is the important part. The WindowManager.LayoutParams docs describe it as "Windows that are overlaid only by a connected AccessibilityService for interception of user interactions without changing the windows an accessibility service can introspect." That second half matters here. The service keeps reading the browser's address bar under the curtain, so the leave logic below still works while the page is covered.
The curtain can't strand anyone. It comes down when the challenge activity reports onResume, or after CURTAIN_HOLD_MS (2,500 ms) if that never happens.
"The challenge activity reports onResume" needed its own class. ReClaim is a launcher, so Home, Settings and the challenge all live in the same package, and "our package is in front" doesn't mean "the challenge is up." WebsiteGatePresence is a small singleton that the challenge (and the block screen) update from onResume, onPause and onDestroy. Its KDoc: "Home and Settings are the same package, so the package name alone cannot mean the challenge is up."
The other half of catching it early was in the reader. In the earlier post, a focused omnibox showing a bare host had to sit still for 300 ms before it counted, so a highlighted suggestion wouldn't trigger a gate. That's still true for ordinary hosts. A host that already matches a rule now counts at once:
internal fun addressIsOpenPage(focused: Boolean, showsPage: Boolean, matchesRule: Boolean): Boolean =
!focused || showsPage || matchesRule
The KDoc gives the reasoning: "A host that already matches a hide or a block is the page Chrome is opening, so it counts at once." The trade-off is real. If the user types youtube.com and then changes their mind before pressing Enter, they might now see the challenge for a page they never loaded. For a site they chose to hide, we decided that was the better mistake.
Cancel is not a pass
The first version finished the challenge activity on Cancel and let the service move the tab off the site in the background. That uncovered the page while the tab was still on it. A comment in goHome() records what went wrong: "Finishing here revealed the site, and Back from that page skipped the challenge."
So Cancel and Back now keep the activity up, switch it to a plain "still hidden" cover, and arm the leave without bringing the browser forward:
// Dialog disposal calls this again in the same moment as Cancel. A later Back re-arms.
val now = SystemClock.elapsedRealtime()
if (leaving && now - leaveArmedAt < LEAVE_REARM_MS) return
leaving = true
leaveArmedAt = now
covering = true
// Stay on this screen until the tab has left. Finishing here revealed the site, and Back
// from that page skipped the challenge.
websiteDeparture.leaveUnderCover(siteHost, holdCopyHidden(websiteLabel(siteHost)))
The activity then waits for one of two signals from WebsiteDeparture: Left (finish and reveal the hold page) or GaveUp (show the challenge again). There's no third outcome where the page shows up without a pass.
One read is not a leave
How does the service know the tab has left? The earlier version trusted the first address-bar read that was off the host. On a real phone, that read lies. The KDoc on the new code explains: "Chrome shows the hold address, a blank page, or the previous site for a single read while the tab is still on the hidden site, and treating that read as a leave uncovered the page."
The rule is now a small pure function:
internal fun nextTabLeave(
stillOnDepartingHost: Boolean,
readable: Boolean,
focused: Boolean,
offHostStreak: Int,
): TabLeaveResult {
if (!readable) return TabLeaveResult(TabLeave.Unconfirmed, offHostStreak, returned = false)
if (stillOnDepartingHost) {
return TabLeaveResult(TabLeave.StillThere, offHostStreak = 0, returned = offHostStreak > 0)
}
if (focused) return TabLeaveResult(TabLeave.Unconfirmed, offHostStreak, returned = false)
val streak = offHostStreak + 1
val leave = if (streak >= LEAVE_CONFIRM_READS) TabLeave.Left else TabLeave.Unconfirmed
return TabLeaveResult(leave, streak, returned = false)
}
LEAVE_CONFIRM_READS is 2. A blank read changes nothing either way. A focused bar doesn't count either; during a leave, the bar is focused while the service types the hold URL into it. Any read back on the host resets the streak, and returned flags it so the caller can treat it as a new visit instead of a finished leave.
Because it's pure, the exact flicker that caused the bug is a unit test:
@Test fun holdUrlThenHiddenHost_isNotALeave() {
val hold = nextTabLeave(stillOnDepartingHost = false, readable = true, focused = false, offHostStreak = 0)
assertEquals(TabLeave.Unconfirmed, hold.leave)
assertEquals(1, hold.offHostStreak)
val back = nextTabLeave(stillOnDepartingHost = true, readable = true, focused = false, offHostStreak = hold.offHostStreak)
assertEquals(TabLeave.StillThere, back.leave)
assertTrue(back.returned)
assertEquals(
HideNavigation.CoverAndNavigate,
hideNavigation(hideDeparture = true, onHiddenHost = true, allowed = false, back.leave, exhausted = false),
)
assertTrue(shouldAskHiddenGate(onHiddenHost = true, allowed = false))
}
Four outcomes, and none of them is a pass
Each read then goes through one more pure function that decides what the cover does:
internal fun hideNavigation(
hideDeparture: Boolean,
onHiddenHost: Boolean,
allowed: Boolean,
leave: TabLeave,
exhausted: Boolean,
): HideNavigation {
if (!hideDeparture || allowed) return HideNavigation.NotHide
if (leave == TabLeave.Left && !onHiddenHost) return HideNavigation.DismissCover
if (exhausted) return HideNavigation.ShowChallenge
return HideNavigation.CoverAndNavigate
}
exhausted is the same give-up limit from the earlier post: after MAX_DEPART_PHASE (8) steps of focusing the bar and submitting the hold URL, the service stops. For a hidden site, giving up means the challenge comes back. The enum's own comment for that case is "This is not a pass." The service maps the four values straight onto actions: dismiss the cover onto the hold page, return to the challenge, keep driving the bar under the cover, or do nothing.
There was one more way Cancel could turn into a pass, and it's a race. A website reminder that is still running can grant the site an allowance for the rest of that session. If that read was already in flight when the user pressed Cancel, it could write the allowance back after Cancel had cleared it. A one-line guard now runs before the code that restores it:
internal fun mayRestoreHiddenAllowance(departureOpenForHost: Boolean): Boolean =
!departureOpenForHost
It's almost too small to be a function, but that gave the rule a name, a KDoc ("or Back becomes a pass") and a test.
Close goes home, and the first fix was wrong
The "still hidden" cover has a Close button for someone who just wants out. The first version (the commit from the day before) stopped the leave, opened the hold page in the browser with an ACTION_VIEW intent, and then sent the phone Home. The next day that was removed. The KDoc on releaseToHome now says why: "Opening a second tab here left the hidden one in place and raced Home."
Now Close stops the leave and goes straight Home. The tab is still on the hidden site behind Home, so the gate has to stay quiet just long enough for Home to land, and then start asking again:
internal fun homeReleaseBlocksGate(
sameHost: Boolean,
armedAt: Long,
now: Long,
sawHome: Boolean,
settleMs: Long = HOME_RELEASE_SETTLE_MS,
): Boolean {
if (!sameHost || armedAt <= 0L) return false
if (sawHome) return false
return now - armedAt < settleMs
}
The quiet window ends the moment the service sees Home (or another app) in front, or after 600 ms, whichever comes first. The constant's comment says it's "short so the hidden page cannot be used in that gap." Reopening the browser after that is a new visit and gets the challenge again.
When the address bar disappears
Everything above depends on reading the bar. A fullscreen video removes it, and before these commits the service had no fullscreen handling at all. The first commit's message describes the new behavior: "A fullscreen video with the address bar hidden is paused and sent out until the hide challenge is passed."
The fix starts with memory. When no bar can be read, fullscreenHost falls back to the last open page (or the page from before the hold page) for that browser. Then a hidden bar has to stay hidden long enough to mean fullscreen and not a scroll:
internal fun fullscreenShouldClose(
host: String?,
barHidden: Boolean,
hiddenForMs: Long,
mustLeave: Boolean,
confirmMs: Long = FULLSCREEN_CONFIRM_MS,
): Boolean {
if (host.isNullOrEmpty() || !barHidden || !mustLeave) return false
return hiddenForMs >= confirmMs
}
FULLSCREEN_CONFIRM_MS is one second. mustLeave is false for a site whose challenge was passed, so a video you earned keeps playing. One tap that brings the toolbar back for a moment doesn't reset the timer either. It takes two on-screen reads (FULLSCREEN_VISIBLE_RESET).
When it does fire, the service sends a media pause key first. AudioManager.dispatchMediaKeyEvent "Sends a simulated key event for a media button," and the code uses KEYCODE_MEDIA_PAUSE because, as its KDoc says, a play/pause toggle "would start playback if nothing were playing." Then it shows the challenge. Back is only used if the video stayed on top:
internal fun fullscreenNeedsBack(gateCovered: Boolean, addressNodeMissing: Boolean): Boolean =
!gateCovered && addressNodeMissing
A bar node that is gone means real fullscreen. One that only scrolled off screen still exists, and Back there would navigate the page back. The Back itself is GLOBAL_ACTION_BACK through performGlobalAction. The timing is staggered: show the challenge, check 350 ms later whether the video stayed on top, send one Back if it did, and show the challenge again 350 ms after that, so the Back isn't delivered to the challenge. If the video is still up two seconds later, the whole thing runs again.
What the tests cover, and what they don't
HideNavigationTest has 25 JVM tests over these pure functions: the flicker, the focused bar, the blank read, Cancel exhaustion, the 600 ms landing, the one-second fullscreen rule, the Back rule. Pulling the decisions out of a 2,400-line service is what made them testable at all.
What isn't tested:
- The service itself. The wiring between these functions, the curtain, and
WebsiteGatePresencehas no instrumented tests. Every threshold (two reads, 600 ms, one second, 2,500 ms) is a tuned constant, not a measured one. - The browsers. View ids like Chrome's
url_bararen't a public API. The second commit also raised the toolbar node budget from 80 to 250, because "Chrome's tab strip, toolbar, and suggestion list can sit more than 80 nodes above the address field." It added Fenix and Tor Browser to the browser list too. Any browser update can change all of this. - After Close, the tab stays on the hidden site behind Home. The gate asks again on the next open, but the page is still loaded in that tab. The KDoc on
closeCoverHome()still says the tab is sent to the hold page. That's out of date since the second-tab code was removed. - The fallback where the curtain times out before the challenge resumes has not been forced in a device test.
We also didn't compile or run this code while writing the post. The excerpts are copied from the repo, not from a fresh build.
Takeaways
- Matching the URL is half the job. Budget time for the gap between "matched" and "the gate is in front," and cover it.
- On a launcher, "our package is in front" is not "our screen is in front." Track the screen.
- Never treat one address-bar read as a navigation. Ask for two, and ignore blank and focused reads.
- Write the "this is not a pass" rule as pure functions with tests. It's the only way to check it.
- Plan for the bar to disappear. Remember the last page and decide what fullscreen means.
ReClaim is free on Google Play, though this gate is still on the next build. Hidden sites share their levels with hidden apps, and our guide to hiding apps on Android walks through those levels from the user's side.
This post was drafted with AI assistance and reviewed by the ReClaim team.
Top comments (0)