tracker-control-android
tracker-control-android copied to clipboard
Block Internet if in background
This would be a really nice addition to TrackerControl
This explains how to do this:
https://stackoverflow.com/questions/3873659/android-how-can-i-get-the-current-foreground-activity-from-a-service/27642535#27642535
+1 This would be absolutely excellent :pray:
My availability is hit and miss, however please let me know if there's any way I can assist with this outside of me requiring a proficiency in Java. I'd be more than happy to perform testing or give feedback on proposed/implemented UX :)
Thanks for getting in touch. If you have any concrete ideas how to improve the app, I’d love to hear those ideas! :) Maybe, you already have some ideas what the UX could look like?
I’ve already thought out the technical solution, but haven’t implemented it yet.
This is implemented in RethinkApp.
https://github.com/celzero/rethink-app/blob/a4364fd8213b9b4c9dc434c3c974401b802f1fa9/app/src/main/AndroidManifest.xml#L72
https://github.com/celzero/rethink-app/blob/a4364fd8213b9b4c9dc434c3c974401b802f1fa9/app/src/main/java/com/celzero/bravedns/util/BackgroundAccessibilityService.kt#L86
https://github.com/celzero/rethink-app/blob/a4364fd8213b9b4c9dc434c3c974401b802f1fa9/app/src/main/java/com/celzero/bravedns/automaton/FirewallManager.kt#L111
The implementation is still beta, but Apache is compatible with GPL.
There’s also this sample implementation: https://github.com/seguri/GetForegroundActivity
Hi @kasnder
One of the engineers on RethinkDNS here.
We are also looking at the UsageStatsManager
APIs to see if there's a leaner way to implement this feature as using AccessibilityManager
does tend to increase battery usage a bit (though we should filter for interesting events via AndroidManifest.xml
to reduce that hit a bit).
The way we have implemented "block app when background" is to simply listen for apps with window content change events (only these apps are allowed internet connections) until the user navigates to Launcher/Home (and all connections are blocked except for the apps showing Launcher/Home Widgets). This is especially helpful for when folks rapidly switch between apps via gestures and such.
Also, we are in the process of implementing a 10 seconds
grace period where a connection from an app not in the foreground is kept hanging to see if the app comes to foreground within that period (to account for delayed AccessibilityService
events).
We are also looking at the
UsageStatsManager
APIs to see if there's a leaner way to implement this feature as usingAccessibilityManager
does tend to increase battery usage a bit (though we should filter for interesting events viaAndroidManifest.xml
to reduce that hit a bit).
Really great to hear from you! It seems both APIs come with flaws. I heard that UsageStatsManager
comes with a certain delay, and not sure what the battery impact it has.
Finding a robust, root-free solution promises great benefits for users, so I’m very interested in investigating this further.
One of my users suggested that the 'data saver' option rather reliably blocks network traffic in the background. Would be interesting to verify this claim.
Another option might be to take inspiration from Greenify to freeze apps in the background. So, there's the question of how much an app like TC or RethinkDNS needs to offer.
hi, some apps need internet in background, e.g. for background music streaming. If this feature is enabled by default in TC, users would have hard time understanding why their music stream stops when app is in background, they would probably first think this is a bug in their music app.
And TrackerControl's name suggests its main goal is to block trackers, not blocking internet. Currently TC blocks trackers even for apps in background, correct ?
So I think this feature may be a plus for advanced users, but it should be disabled by default in TC, and when enabled the user needs a 'Allow internet when app in background' toggle per app.
One of my users suggested that the 'data saver' option rather reliably blocks network traffic in the background. Would be interesting to verify this claim.
Yes, this works. Apps get sent the dreaded BLOCKED
message when they try to access Internet while in the background when "data-saver" is enabled. On OnePlus, there's an option in Android Settings' app-info pages against every app to disable Background data
which works in a similar manner too, but I don't think non-privileged apps have the necessary permissions to flip that setting programmatically. That said, AccessiblityService
APIs can be used to toggle that preference on/off (that is, perform UI actions on behalf of the user).
Hi kasnder: We've refactored RethinkDNS' implementation (in v053f
) and users have reported it now works error-free (using AccessibilityService of course) so you may want to take a look: com/celzero/bravedns/util/BackgroundAccessibilityService
Handling foreground events for widgets on homescreen (launcher) is the only case that's pending, per our knowledge.
Hi kasnder: We've refactored RethinkDNS' implementation (in
v053f
) and users have reported it now works error-free (using AccessibilityService of course) so you may want to take a look: com/celzero/bravedns/util/BackgroundAccessibilityServiceHandling foreground events for widgets on homescreen (launcher) is the only case that's pending, per our knowledge.
Hi @ignoramous! Thank you so much for this update. Your implementation looks really clean. Well done! I'll definitely check it out, to potentially help TrackerControl users, too, to avoid unwanted tracking in the background.