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

fix: Screen turns off while patching due to wrong WakeLock #2147

Open
wants to merge 1 commit into
base: compose-dev
Choose a base branch
from

Conversation

kitadai31
Copy link
Contributor

@kitadai31 kitadai31 commented Aug 18, 2024

Fix a issue that screen timeout is activated during patching.
This is caused by WakeLock not being set correctly.

The current code is passing WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON to newWakeLock().
But this is wrong.

It is true that the document says that PowerManager.FULL_WAKE_LOCK is deprecated and use WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON instead.

Most applications should use WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON instead of this type of wake lock
https://developer.android.com/reference/android/os/PowerManager#FULL_WAKE_LOCK

However, this does not mean passing the FLAG_KEEP_SCREEN_ON flag to newWakeLock().
The flag have to be passed to window.addFlags().
https://developer.android.com/develop/background-work/background-tasks/scheduling/wakelock#screen

Therefore, I removed WakeLock and add window.addFlags().

Since the window requires Activity context, it can't be handled from PatcherWorker, so I added it to the UI code.

I've checked this sets KEEP_SCREEN_ON correctly at the start of patching, and clears the flag correctly when succeeded or failed to patching, or patching was canceled.

@validcube validcube added the ReVanced Manager Compose Regarding the Compose rewrite of ReVanced Manager label Aug 18, 2024
@Axelen123
Copy link
Member

The incorrect use of FLAG_KEEP_SCREEN_ON for the PowerManager seems to originate from the old compose manager. The reason for the wakelock is to try to discourage Android from killing Manager while patching in the background, so just using FLAG_KEEP_SCREEN_ON is not sufficient. I believe the correct thing to do is to use FULL_WAKE_LOCK and suppress the deprecation, which is fine because it does not seem to be deprecated for our use case.

@kitadai31
Copy link
Contributor Author

The reason for the wakelock is to try to discourage Android from killing Manager while patching in the background

Isn't this purpose already achieved by foreground service?

@Axelen123
Copy link
Member

Axelen123 commented Aug 18, 2024

That was what I heard from the previous maintainer, but it was a while ago so I might be wrong. Also, it appears PARTIAL_WAKE_LOCK would definitely be better than FULL_WAKE_LOCK since we only need the CPU for patching, not the screen and it doesn't get silently released if the user presses the power button.

@kitadai31
Copy link
Contributor Author

I could not find any information in the official documentation that foreground services are also limited without WakeLock, but I did find such information on StackOverflow.
https://stackoverflow.com/questions/55170819/android-slows-down-foreground-service-when-device-sleeps
https://stackoverflow.com/questions/52002533/does-service-startforeground-imply-wakelock
According to this information, WakeLock may affect the CPU power when the screen is off.

But WakeLock is not related to background task killing by Android.
Also, this PR fixes the problem that screen turns off during patching.
Therefore I don't think WakeLock is needed since the screen won't be turned off while patching.

What's your opinion?
If you think it's needed, I'll restore the WakeLock and change it to PARTIAL_WAKE_LOCK.

@Axelen123
Copy link
Member

Axelen123 commented Aug 20, 2024

Keeping the CPU on while patching might be desirable. I am not sure if we should use FLAG_KEEP_SCREEN_ON, PARTIAL_WAKE_LOCK or both.

@Ushie and @validcube may have ideas here.

@oSumAtrIX
Copy link
Member

What's the progress of this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ReVanced Manager Compose Regarding the Compose rewrite of ReVanced Manager
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants