Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Very high battery usage #15

Closed
bcsti opened this issue Jul 31, 2023 · 20 comments
Closed

Very high battery usage #15

bcsti opened this issue Jul 31, 2023 · 20 comments
Assignees
Labels
enhancement New feature or request

Comments

@bcsti
Copy link

bcsti commented Jul 31, 2023

The app uses about 30-40% of my battery in the background. I can see this happening on three different phones.
The app runs on the default optimized battery mode.
Is there something that can be done or is it not fixable?

@zaneschepke
Copy link
Owner

Thank you for reporting this as well. I'll take a look and see what can be put in place to lower battery usage. I think there is a way this could be more optimized.

@zaneschepke zaneschepke self-assigned this Jul 31, 2023
@zaneschepke zaneschepke added the enhancement New feature or request label Jul 31, 2023
@zaneschepke
Copy link
Owner

The latest release, 2.3.2, I made some changes that should hopefully help the battery usage. This is especially true when VPN is active. I change the statistics monitoring (handshake and packets) from a 1 second to 10 second interval which should help battery usage. I made another change to how VPN auto tunneling works that could potentially have a minor improvement to battery usage as well (especially for a device that is constantly changing network connectivity). Please let me know if you notice any change in battery usage performance in this new release. I will also monitor it for myself. Thanks again!

@zaneschepke
Copy link
Owner

So even after these changes in 2.3.2 I am still seeing high battery usage. I believe I found a solution that involves switching from a foreground service to a broadcast receiver for listening to the connectivity changes. If I can get this working it would massively improve battery performance. I'll keep you posted on my progress.

@zilexa
Copy link

zilexa commented Aug 12, 2023

You may want to have a look at how the Automate app (a free alternative to Tasker) monitors for connection changes, using my Automate flow.
It needs the following permissions:
Control tunnels
Execute shell commands
Location - all the time
Nearby devices (not sure if this is really needed)

Also, I needed to set "battery usage" of Automate app to "Unrestricted". But battery usage is rarely more than 0%.

I use this in combination with the official Wireguard app. But it requires the Automate app, my flow, several configurations (see the flow description) and additionally, you have to remember to reboot your phone after every Automate app update.
Also, everytime you reboot your phone, you have to open the Automate app and stop/start the flow.

I can't wait for you to fix the battery usage, me, my wife and some others will definitely switch to WG Tunnel and get rid of tis cumbersome setup with Automate and the official app.

Offtopic:
It is shocking to me that the official app for iOS does have the same feature as what you created, but for Android they push you to Tasker, for such a basic feature. Actually, there used to be an unofficial app that also supported automatic switching when you leave a trusted WiFi network. When that developer joined the official app devteam, he rescinded his app and said that the same functionality would become available in the official app. That was a couple of years ago!
I am really happy to have found your app!

@zaneschepke
Copy link
Owner

You may want to have a look at how the Automate app (a free alternative to Tasker) monitors for connection changes, using my Automate flow. It needs the following permissions: Control tunnels Execute shell commands Location - all the time Nearby devices (not sure if this is really needed)

Also, I needed to set "battery usage" of Automate app to "Unrestricted". But battery usage is rarely more than 0%.

I use this in combination with the official Wireguard app. But it requires the Automate app, my flow, several configurations (see the flow description) and additionally, you have to remember to reboot your phone after every Automate app update. Also, everytime you reboot your phone, you have to open the Automate app and stop/start the flow.

I can't wait for you to fix the battery usage, me, my wife and some others will definitely switch to WG Tunnel and get rid of tis cumbersome setup with Automate and the official app.

Offtopic: It is shocking to me that the official app for iOS does have the same feature as what you created, but for Android they push you to Tasker, for such a basic feature. Actually, there used to be an unofficial app that also supported automatic switching when you leave a trusted WiFi network. When that developer joined the official app devteam, he rescinded his app and said that the same functionality would become available in the official app. That was a couple of years ago! I am really happy to have found your app!

Hi, thanks for the detailed background and information on other potential solutions. Is Automate open source? I can't find the source code to see how they are accomplishing monitoring of network changes with low battery usage.

I think I might be able to give some background in the off topic bit. I know this feature seems like a pretty basic feature but Android has actually made it extremely difficult to accomplish the monitoring of the network SSID changes (especially in the background) as their APIs/Android have progressed. They have removed system wide broadcasts of network changes and have further restricted the access of reading the wifi SSID. This is likely why you see this feature implement on all of the other WireGuard apps but not on Android. This is the reason I was pushed to use a more power consuming foreground service approach for this feature.

You can see the list of system level broadcasts events here:
https://developer.android.com/about/versions/11/reference/broadcast-intents-30

I have come up with an potential alternative solution but it has some quirks. There is a way to receive a broadcast from the system when network changes happen. From my testing, this functionality is only available to an app that is currently open or backgrounded (unlike the system level broadcasts list above). So, essentially, if you use this battery saving feature you would have to background the app and not close it for the auto tunneling to still work. I haven't full tested this idea but it should reduce the battery consumption. What are your thoughts?

@zilexa
Copy link

zilexa commented Aug 13, 2023

I think Automate is closed source but there is an official forum and Reddit page:
https://llamalab.com/automate/

Regarding your proposed solution: I think that's exactly how for example Automate but also FolderSync work, they are always active in the background. The notification is not necessary or disabled. On Android 13 it looks like this:

Screenshot_20230813-161216_Microsoft_Launcher

I just rebooted my device, before that it said 175 hours for both. Battery consumption is basically 0.

@zilexa
Copy link

zilexa commented Aug 15, 2023

Just FYI:
On my Asus Zenfone 9, Android 13 with latest security updates:

  • with App Setting > Battery Usage set to "Unrestricted" and "Background app management" turned off
  • the app opened for the last time almost an hour ago
  • Battery usage shows 0%

The tunnel does not auto-turn on when I leave the house/wifi. The app is also not shown anymore in the notification list of "Active apps". It was present there for a while though in the past hour.

Once I open the app, go back home, connect to wifi then disable wifi, it does autoconnect again.

So I guess the current solution to monitor for network changes/stay active is indeed a bit unreliable on some devices/latest Android?

@zaneschepke
Copy link
Owner

Just FYI: On my Asus Zenfone 9, Android 13 with latest security updates:

  • with App Setting > Battery Usage set to "Unrestricted" and "Background app management" turned off
  • the app opened for the last time almost an hour ago
  • Battery usage shows 0%

The tunnel does not auto-turn on when I leave the house/wifi. The app is also not shown anymore in the notification list of "Active apps". It was present there for a while though in the past hour.

Once I open the app, go back home, connect to wifi then disable wifi, it does autoconnect again.

So I guess the current solution to monitor for network changes/stay active is indeed a bit unreliable on some devices/latest Android?

The only thing I can think of is that the system killed the app. I'll investigate and see if I can reproduce.

@bcsti
Copy link
Author

bcsti commented Aug 15, 2023

I have this issue conistently one one of my devices. The app somehow gets killed even if the vpn is currently connected. On another device this happens less often, about 2-3 times a week.

@zaneschepke
Copy link
Owner

I have this issue conistently one one of my devices. The app somehow gets killed even if the vpn is currently connected. On another device this happens less often, about 2-3 times a week.

That's for the additional info! Could you tell me the model and version of your device that it happens less often on?

@bcsti
Copy link
Author

bcsti commented Aug 15, 2023

Yeah, thats a Pixel 5 with Android 13 latest Patch.

@zilexa
Copy link

zilexa commented Aug 15, 2023

In my case, it was a human error! I had set battery usage to Restricted instead of Unrestricted.

@zilexa
Copy link

zilexa commented Sep 1, 2023

I can confirm, with:

  • battery usage set to Unrestricted
  • Battery management optimization Disabled

The app still does not stay active.. it disappeared from the list of active apps..

@mich2k
Copy link

mich2k commented Sep 1, 2023

I can confirm, with:

  • battery usage set to Unrestricted
  • Battery management optimization Disabled

The app still does not stay active.. it disappeared from the list of active apps..

Screenshot_20230901_034542_Device care.jpg

Oh my device I can't avoid suspension since it doesn't appear in the app list

@zaneschepke
Copy link
Owner

Thanks for the additional details. I'm doing more testing to see if I can replicate and remediate. I have a few different changes I'm currently testing out. In dev, I did get it working where you can now start VPN from the tile even if the app is closed and auto tunneling service is not active (this does not work on the official app). This would also relaunch the auto tunneling service if it had been killed by the OS. I still want to get to the bottom of why the OS is killing the service but we'll get there with time I am sure. I also invite you both @zilexa @mich2k to join the Discord if you're interested and have not already joined. It is a bit easier to go back and forth on ideas, improvements and discoveries there.

@zilexa
Copy link

zilexa commented Sep 2, 2023

@mich2k I don't think you are in the right place. You just go to your app drawer where all your apps are and long press. You need to get to "App Info".
See a little video here (will expire in a few days): https://streamable.com/ky0an3

Additionally I did a test, I didn't charge my phone last night. It had enough battery to not automatically switch to an extreme battery save mode.

Now I see both apps that should stay active, stayed active:

Screenshot_20230902-104118_Microsoft_Launcher
Extra info: 2% battery usage since last charge for WGtunnel and 0% for FolderSync. Last charge was 1 day and 1 hour ago (25 hours).

Not sure why yesterday (while not at home) wg-tunnel did stop. Not sure what triggers that. I will pay attention the next few days to see what happens :)

And will join Discord!

@zilexa
Copy link

zilexa commented Sep 2, 2023

@zaneschepke maybe there is no issue: I don't have the updated version yet that re-enables auto-tunnel via the tile. Maybe I disabled/paused the tunnel via the tile and enabled it again via the tile, but then auto-tunnel stays disabled with the version that I have.
So maybe there is no issue.. at least not with my devices on Android 13.
I'll keep an eye on it.

@mich2k
Copy link

mich2k commented Sep 2, 2023

@mich2k I don't think you are in the right place. You just go to your app drawer where all your apps are and long press. You need to get to "App Info".
See a little video here (will expire in a few days): https://streamable.com/ky0an3

Additionally I did a test, I didn't charge my phone last night. It had enough battery to not automatically switch to an extreme battery save mode.

Now I see both apps that should stay active, stayed active:

Screenshot_20230902-104118_Microsoft_Launcher
Extra info: 2% battery usage since last charge for WGtunnel and 0% for FolderSync. Last charge was 1 day and 1 hour ago (25 hours).

Not sure why yesterday (while not at home) wg-tunnel did stop. Not sure what triggers that. I will pay attention the next few days to see what happens :)

And will join Discord!

In order to avoid suspension on Samsung you have to tweak two menus, the one you are saying "Unrestricted" and this other one that states "app never in suspension" (this last one allows the app to run on battery saving etc)

I even managed to find the background consumption (is in the multitasking panel and the consumption is always around 1 and 3%

zaneschepke added a commit that referenced this issue Oct 21, 2023
Added the ability to turn on and off tunnels via intents to the shortcut activity.
#44

Added a setting to enable or disable shortcut/intent control of tunnels.

Added an experimental battery saver setting to auto-tunneling to fix the battery drain issue caused by wakelock.
#15

Fixes a bug where sometimes the config screen could crash if there are issues parsing the tunnel config data.
@zaneschepke
Copy link
Owner

I'm sorry it has taken me a long time to address this issue.

In version 3.1.6, I've added an experimental feature in the settings screen to address the battery drain. With this feature enabled, the app will no longer hold a wake lock on the CPU for longer than 10 minutes. This change will prevent the battery drain.

I have done some light testing with this new feature. I did not see an impact on auto-tunneling, but I only tested a few scenarios. Any feedback on this would be greatly appreciated!

@zaneschepke
Copy link
Owner

So far, I have seen positive results and positive feedback from users on the new battery saver setting. With that, I am going to mark this one as closed. Any future feedback on the battery saver/power usage can be opened as a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants