Skip to content

fix(web-watcher): keep the browser's window after a tolerated keyboard event - #438

Open
TimeToBuildBob wants to merge 2 commits into
ActivityWatch:masterfrom
TimeToBuildBob:fix/434-chromium-split-after-keyboard
Open

TimeToBuildBob wants to merge 2 commits into
ActivityWatch:masterfrom
TimeToBuildBob:fix/434-chromium-split-after-keyboard

Conversation

@TimeToBuildBob

@TimeToBuildBob TimeToBuildBob commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #434.

#342 stopped a keyboard or overlay event from ending a browser visit while the browser keeps focus, but it still moved lastWindowId to the keyboard's window. The browser's next event then counted as a window change. In Chromium-family browsers a page-content event (for example from the edited input) has no url_bar under its source, so the URL is null and the visit ended. Typing into a page field split the visit.

Change (watcher/WebWatcher.kt):

  • A tolerated overlay event no longer advances lastWindowId, so the browser's next event is on the same window and a null URL keeps the visit, as it already did without the keyboard event in between.
  • The tolerated overlay window is remembered, so its further events skip the rootInActiveWindow lookup. That keeps the binder calls per typing burst where they were before; the lookup runs again when a browser event or a different window arrives.
  • browserIsActiveWindow() now uses the same tracked-browser check as onAccessibilityEvent. It only accepted KNOWN_BROWSER_PACKAGES, so a keyboard event in an autodetected browser (feat(android): browser autodetect (rebased from #311) #372) ended its visit outright.
  • Leaving the browser still ends the visit: another app's window is a change against the browser's window, and focus has moved.

Tests (WebWatcherOverlayTest, Robolectric):

  • Chrome page event after a tolerated keyboard event keeps the visit (fails on master: one visit written, session closed).
  • Switching to another app after a keyboard event still ends the visit.
  • A keyboard event in an autodetected browser keeps the visit (fails before the second commit).
  • A keyboard window that takes focus still ends the visit.

Verified: ./gradlew testStandardDebugUnitTest on a JDK 17 host: 485 tests, 0 failures. With master's WebWatcher.kt and the new test file, the first test fails with Typing into a page must not split the visit expected:<0> but was:<1>, as #434 predicted. Not traced on a device.

…d event

A keyboard or overlay event tolerated by ActivityWatch#342 moved lastWindowId to the
overlay's window, so the browser's next event counted as a window change.
A Chromium page event has no url_bar under its source, so the null URL
ended the visit and typing into a page split it.

Keep lastWindowId on the browser's window for tolerated overlay events,
and remember the overlay so its further events skip the active-window
lookup.

Fixes ActivityWatch#434

Git-Session-Id: 574c2071-b503-4d8a-a2d0-56f763debc78
@TimeToBuildBob

Copy link
Copy Markdown
Contributor Author

What changed: 2 files, +133/-4 in mobile/src/test/java/net/activitywatch/android/watcher/, mobile/src/main/java/net/activitywatch/android/watcher/
Riskiest file: mobile/src/test/java/net/activitywatch/android/watcher/WebWatcherOverlayTest.kt (+115/-0)
Test: Run the repo's usual CI-equivalent checks locally

Review guide

Key files

  • mobile/src/test/java/net/activitywatch/android/watcher/WebWatcherOverlayTest.kt (+115/-0, Δ115)
  • mobile/src/main/java/net/activitywatch/android/watcher/WebWatcher.kt (+18/-4, Δ22)

Suggested verification

  • Run the repo's usual CI-equivalent checks locally

Auto-posted by scripts/github/pr-warmup-review-guide.py for #438.

browserIsActiveWindow() only accepted KNOWN_BROWSER_PACKAGES, while
onAccessibilityEvent also tracks autodetected browsers. A keyboard event
in an autodetected browser therefore ended its visit. Share one predicate.

Git-Session-Id: 574c2071-b503-4d8a-a2d0-56f763debc78
@greptile-apps

greptile-apps Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Confidence Score: 4/5RetriggerView in Greptile

[Medium impact] The change is contained to browser visit tracking.

The PR appears safe to merge, with a non-blocking gap in tests for the overlay cache.

Findings

  1. P2 Overlay shortcut lacks tests ▶

Summary

WebWatcher keeps the browser's window after a tolerated keyboard or overlay event.

  • Typing no longer splits a browser visit after a keyboard event.

Review was based on source inspection; tests were not run.

Reviews (1) · Last reviewed commit: "fix(web-watcher): keep the browser's win..." · Reviewed by Greptile

Comment on lines +73 to +79
// Keyboard window event while Chrome stays the active window: tolerated by #342.
activeRoot(chrome)
service.onAccessibilityEvent(event(keyboard, keyboardWindow))
assertEquals(chrome, tracker().currentBrowser)

// The same kind of page event, now arriving after the keyboard's.
service.onAccessibilityEvent(event(chrome, browserWindow))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Overlay shortcut lacks tests

Each test sends only one keyboard event per visit, so none exercises the new toleratedOverlayWindowId shortcut. Count rootInActiveWindow reads in a test: repeated events from the same overlay should skip the lookup, while a browser event or a different overlay should make the next lookup run. Without these checks, the cache can stop working or remain stale without failing the tests.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@TimeToBuildBob

TimeToBuildBob commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor Author

🤖 AI code review

The PR changes WebWatcher.onAccessibilityEvent so that a non-browser (keyboard/overlay) event, arriving while a tracked browser still owns the active window, no longer moves lastWindowId off the browser's window. The overlay's window id is stored in a new toleratedOverlayWindowId field, so later events from that window skip the browserIsActiveWindow() lookup. The tracked-browser condition is extracted into isTrackedBrowser(), which browserIsActiveWindow() now uses too, so autodetected browsers count as well as KNOWN_BROWSER_PACKAGES. A new Robolectric test class, WebWatcherOverlayTest, covers four keyboard/overlay sequences.

Safe to merge — no P0/P1 findings

Confidence 5/5

✅ No findings. The diff looks correct to me on this pass.

Files changed (2) — the diff as I read it
  • mobile/src/main/java/net/activitywatch/android/watcher/WebWatcher.kt — Adds toleratedOverlayWindowId and isTrackedBrowser(), and moves the lastWindowId updates into the browser branch and the session-ending branch only; browserIsActiveWindow() now calls isTrackedBrowser().
  • mobile/src/test/java/net/activitywatch/android/watcher/WebWatcherOverlayTest.kt — New Robolectric tests that replace writer with a counting executor, set lastWindowId/detectedBrowsers/probeMemory via reflection, and send keyboard, Chrome, autodetected-browser and other-app events with chosen window ids.

Reviewed 35ea6a07c7a5 · panel: claude-code:claude-opus-5-5 · subscription engine · 68s · about this reviewer

Maintainer commands

@TimeToBuildBob review (own line) — fresh review · @TimeToBuildBob fix — a worker acts on the findings. Once per comment; 👀 = received.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WebWatcher: Chromium visits still split when typing — the browser event after a keyboard event ends the session

1 participant