tracker-control-android icon indicating copy to clipboard operation
tracker-control-android copied to clipboard

Block Internet if in background

Open kasnder opened this issue 4 years ago • 13 comments

This would be a really nice addition to TrackerControl

kasnder avatar Jul 28 '20 16:07 kasnder

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

kasnder avatar Sep 19 '20 09:09 kasnder

+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 :)

DrTexx avatar Oct 09 '20 06:10 DrTexx

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.

kasnder avatar Oct 09 '20 12:10 kasnder

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.

kasnder avatar Dec 10 '20 17:12 kasnder

There’s also this sample implementation: https://github.com/seguri/GetForegroundActivity

kasnder avatar Dec 10 '20 17:12 kasnder

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).

ignoramous avatar Dec 12 '20 22:12 ignoramous

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).

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.

kasnder avatar Dec 12 '20 23:12 kasnder

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.

kasnder avatar Dec 15 '20 13:12 kasnder

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.

kasnder avatar Dec 15 '20 13:12 kasnder

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.

iiitomo avatar Mar 22 '21 12:03 iiitomo

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).

ignoramous avatar Mar 22 '21 13:03 ignoramous

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.

ignoramous avatar Aug 27 '21 16:08 ignoramous

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 @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.

kasnder avatar Aug 29 '21 09:08 kasnder