Skip to content

Fix: Ensure BlockingActivity always launches to foreground when rapidly switching to the blocked app#18

Open
jamesdean268 wants to merge 6 commits into
cajdata:mainfrom
jamesdean268:main
Open

Fix: Ensure BlockingActivity always launches to foreground when rapidly switching to the blocked app#18
jamesdean268 wants to merge 6 commits into
cajdata:mainfrom
jamesdean268:main

Conversation

@jamesdean268

Copy link
Copy Markdown

This PR addresses two bugs where the BlockingActivity would fail to appear if a user navigated to a blocked app via the Recents menu multiple times in quick succession, or navigated to the blocked app by swiping across the bottom bar quickly to get from the TapBlok screen to the blocked app (called "Quick Switch").

It resolves these bugs by:

  • Adding Intent.FLAG_ACTIVITY_CLEAR_TASK, forcing the Android system to recreate the activity and clear any existing task state, ensuring the blocking screen always takes over the foreground, and
  • Switching from UsageStatsManager.queryUsageStats to queryEvents to accurately determine the foreground app in real-time. Tracking the ACTIVITY_RESUMED event allows TapBlok to instantly catch when a user switches to a blocked app via gestures like Quick Switch or the Recents menu, closing the bypass window created by aggregated stats delays.

google-labs-jules Bot and others added 6 commits May 9, 2026 03:33
Added Intent.FLAG_ACTIVITY_CLEAR_TASK when launching BlockingActivity from AppMonitoringService to ensure it properly comes to the foreground, especially when launched successively from the Recents menu.

Co-authored-by: jamesdean268 <27984001+jamesdean268@users.noreply.github.com>
Switched from `UsageStatsManager.queryUsageStats` to `queryEvents` to accurately determine the foreground app in real-time. Tracking the `ACTIVITY_RESUMED` event allows TapBlok to instantly catch when a user switches to a blocked app via gestures like Quick Switch or the Recents menu, closing the bypass window created by aggregated stats delays.

Co-authored-by: jamesdean268 <27984001+jamesdean268@users.noreply.github.com>
…flags-9955110494792391912

Fix: Ensure BlockingActivity always launches to foreground
…ssion

Android 13+ (API 33+) requires the POST_NOTIFICATIONS runtime permission
to display the foreground service persistent notification. Without this
notification being visible, the OS considers the foreground service a
background process and terminates it randomly to reclaim resources.

This commit adds the POST_NOTIFICATIONS permission to AndroidManifest.xml,
prompts the user to grant it in MainActivity on API 33+, and validates
its presence before attempting to start the AppMonitoringService from any
entry point (UI, shortcuts, NFC intent).

Co-authored-by: jamesdean268 <27984001+jamesdean268@users.noreply.github.com>
When an active break ends, TapBlok is supposed to immediately kick the user out of any
currently active blocked app. However, TapBlok polls for `ACTIVITY_RESUMED` events in the last
1 minute to determine the foreground app. Since the break is 5 minutes, if the user stays in the
app without switching, no new resume events are logged. Thus, `getForegroundApp()` would return
null and fail to block the app.

This commit updates `getForegroundApp()` to cache the `lastKnownForegroundApp` when it detects
a valid app, and returns that fallback when the UsageStats query is empty.

Co-authored-by: jamesdean268 <27984001+jamesdean268@users.noreply.github.com>
…-2751649071894736556

Fix monitoring session randomly stopping by requesting Notification permissions
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.

1 participant