[GUIDE] Eliminate Screen Wake Lag

mikeyxda

Inactive Recognized Developer
Jul 2, 2010
3,761
14,262
0
Florida - Gulf Coast
www.ddisoftware.com
This guide shows how to solve the screen wake lag on Samsung devices which can be almost 2 seconds from button press to actual screen-on depending on configuration and whether or not an external SD card is used. You're probably here because you've figured out that all the tricks listed in other threads (like turning off s-voice, ripple effect, and even freezing certain apps) does little to fix the problem. There are things other than wake-from-deep-sleep that can cause some lag, but the majority of the lag is caused by processor and sensor power-up cycles that cannot be improved using any method of modifying settings.

This fix should work on all incarnations of the Note II and potentially other Samsung devices as well. I'll dispense with the usual disclaimers about apktool, decompiling, and recompiling. Obviously this guide assumes some knowledge of those.

The Note II goes into deep sleep within about 1 second of screen off, so to eliminate wake lag, we need to prevent deep sleep. We do this by creating a wake lock. Yes, this means your phone will not go into deep sleep but before jumping to conclusions that this will nail your battery, see this post in my ROM thread for the gory details of what I found. Long story short, many people get better battery life by preventing deep sleep because the phone isn't constantly powering up different parts of the circuitry all the time. And from what I found, your phone is forced to wake out of deep sleep a lot, potentially hundreds of times an hour, to perform regular operations.

Devs, modders, and tweakers: feel free to use this mod in anything you develop and all I ask is that you credit me and link to this thread.

So let's get to it. We'll be making all our changes in android.policy.jar in the KeyguardViewMediator.smali file.

So open KeyguardViewMediator.smali and at the top, add the line in blue to create a ZeroWakeLag wakelock so that end users will be able to see and identify the wakelock that is keeping their phone awake:

Code:
.field private mWakeLock:Landroid/os/PowerManager$WakeLock;

[COLOR="Blue"].field private mZeroWakeLag:Landroid/os/PowerManager$WakeLock;[/COLOR]

.field private mWakelockSequence:I
Next, add these lines in blue in the constructor method to initialize the new wakelock:

Code:
const-string v2, "keyguardWakeAndHandOff"

    invoke-virtual {v0, v1, v2}, Landroid/os/PowerManager;->newWakeLock(ILjava/lang/String;)Landroid/os/PowerManager$WakeLock;

    move-result-object v0

    iput-object v0, p0, Lcom/android/internal/policy/impl/KeyguardViewMediator;->mWakeAndHandOff:Landroid/os/PowerManager$WakeLock;

    .line 314
    iget-object v0, p0, Lcom/android/internal/policy/impl/KeyguardViewMediator;->mWakeAndHandOff:Landroid/os/PowerManager$WakeLock;

    const/4 v1, 0x0

    invoke-virtual {v0, v1}, Landroid/os/PowerManager$WakeLock;->setReferenceCounted(Z)V
	
    [COLOR="Blue"]iget-object v0, p0, Lcom/android/internal/policy/impl/KeyguardViewMediator;->mPM:Landroid/os/PowerManager;

    const/4 v1, 0x1

    const-string v2, "ZeroWakeLag"

    invoke-virtual {v0, v1, v2}, Landroid/os/PowerManager;->newWakeLock(ILjava/lang/String;)Landroid/os/PowerManager$WakeLock;

    move-result-object v0

    iput-object v0, p0, Lcom/android/internal/policy/impl/KeyguardViewMediator;->mZeroWakeLag:Landroid/os/PowerManager$WakeLock;

    iget-object v0, p0, Lcom/android/internal/policy/impl/KeyguardViewMediator;->mZeroWakeLag:Landroid/os/PowerManager$WakeLock;

    const/4 v1, 0x0

    invoke-virtual {v0, v1}, Landroid/os/PowerManager$WakeLock;->setReferenceCounted(Z)V[/COLOR]

    .line 316
    new-instance v7, Landroid/content/IntentFilter;
Now our wakelock is initialized so let's add a wakelock.acquire when the screen turns OFF so there is a permanent wakelock while the screen is off. Just add the lines in blue at the top of the onScreenTurnedOff method:

Code:
.method public onScreenTurnedOff(I)V
    .locals 30
    .parameter "why"

    .prologue
    .line 400
	
    [COLOR="Blue"]move-object/from16 v0, p0
	
    iget-object v1, v0, Lcom/android/internal/policy/impl/KeyguardViewMediator;->mZeroWakeLag:Landroid/os/PowerManager$WakeLock;

    invoke-virtual {v1}, Landroid/os/PowerManager$WakeLock;->acquire()V[/COLOR]
	
    monitor-enter p0

    .line 401
    const/16 v26, 0x0

    :try_start_0
    move/from16 v0, v26

    move-object/from16 v1, p0

    iput-boolean v0, v1, Lcom/android/internal/policy/impl/KeyguardViewMediator;->mScreenOn:Z
Now the wakelock is acquired on screen off, so let's add the lines in blue below to release the wakelock when the screen comes on in the OnScreenTurnedOn event. Technically you could skip the release altogether and it'd do the same thing but it's just good coding practice to release a wakelock when you are done with it rather than driving the reference count up:

Code:
.method public onScreenTurnedOn(Lcom/android/internal/policy/impl/KeyguardViewManager$ShowListener;)V
    .locals 3
    .parameter "showListener"

    .prologue
    .line 545
    monitor-enter p0

    .line 546
	
    [COLOR="Blue"]iget-object v1, p0, Lcom/android/internal/policy/impl/KeyguardViewMediator;->mZeroWakeLag:Landroid/os/PowerManager$WakeLock;

    invoke-virtual {v1}, Landroid/os/PowerManager$WakeLock;->release()V[/COLOR]
	
    const/4 v0, 0x1

    :try_start_0
    iput-boolean v0, p0, Lcom/android/internal/policy/impl/KeyguardViewMediator;->mScreenOn:Z
That's it! Recompile and enjoy the zero wake lag.

Note to devs, tweakers, and modders. This mod works best when combined with some sort of screen-off underclocking and limiting screen-off processing to a single CPU. There are plenty of scripts to do that out there and it can even be done with CPU tuners, so I'll leave that part to you. As I said though, even that is not really necessary because you'd be surprised how much has to be woken up (see the post linked above) when in deep sleep so in many cases, it's better to just let the CPU's idle at-the-ready to avoid all the waking that happens from Google services, push mails, alarms, widget updating, etc. You (or your ROM dev) can also make this mod selectable by using system properties to turn the feature on/off by just jumping around the "acquire" and "release" statements in the OnScreenTurnedOff and OnScreenTurnedOn events.

Sharing

As mentioned above, use this mod in whatever mod, tweak, or ROM you like but please just credit me and link to this thread. Thanks!



Click to Donate
Donations are greatly appreciated. If I've saved you some time and aggravation with this mod, donating a few bucks is the best way to say thanks!
 
Last edited:

mikeyxda

Inactive Recognized Developer
Jul 2, 2010
3,761
14,262
0
Florida - Gulf Coast
www.ddisoftware.com
Guess I'll do some Q&A here...

I read, so if do not go into deep sleep will consume more battery ?
I did a lot of research on that and it's not so easy to answer. Generally, I believe battery will be no worse and possibly better. Google location services, alarms, widgets, push mail, and many other processes end up waking up 99% of phones at least once every 3 minutes anyway and the deep-sleep-->awake-->deep sleep cycles actually consume more power than letting the processor(s) stay awake. On my phone for example, Google location services alone woke the phone 20 times/hr (every 3 minutes) and that's with GPS turned off and no navigation or map activities running! Add a weather widget here, a push mail app there, and I found that my phone was bouncing in and out of deep sleep more than a hundred times an hour! Better (on both battery and circuitry) to just idle at that point. With only one CPU awake with screen off, the radio will draw more power than the CPU idling. Most people don't realize that the screen and radio eat orders of magnitude more power than (especially an idling) CPU, so idling one CPU will have little effect on battery. That said, I suppose if you put your phone in airplane mode and maybe froze a bunch of other apps that wake the phone, maybe battery would be worse with the ZeroWakeLag wakelock and the phone would do better in deep sleep. Reality is: we don't use our phones that way.

Edit: now we should discuss which cpu profile suits best when the screen is off using this Mod. I saw someone suggested to set Max CPU to 600 and Min to 200 Mhz
From my testing, the Note 2 already appears to limit processing to a single CPU when the screen is off. For extra battery savings, you can set up a screen-off max frequency limit of say 800Mhz to 1.0Ghz. I personally use 1.0Ghz because you only have one processor running and it won't ramp up unless it's needed anyway. Going below about 800Mhz can cause issues like bluetooth music skipping, media share server stuttering, etc. I definitely would not recommend setting the max to 200Mhz with the screen off to force the CPU to the lowest frequency all the time. But hey, this is Android... never hurts to try I guess if you really want to. :)

Mike
 
Last edited:

jvillatoro

Member
Aug 4, 2008
24
12
0
I got lost at So open KeyguardViewMediator.smali...
Lol
You need apktool to decompile the android.policy.jar

BTW, I just applied the changes to my phone, will report how it behaves. I am currently using latest Omega Rom V15.

I have the modified android.policy.jar in case anyone wants it

Edit #2: apparently the mod is working, do not notice any lag when turning the screen on. The process shows on better battery stat.





So that means that the cpu will idle at 200 mhz? Ill make a battery test tomorrow under my normal usage.
Ill post results later
 
Last edited:

Nicheuji

Senior Member
May 7, 2011
121
31
0
Malacca
You need apktool to decompile the android.policy.jar

BTW, I just applied the changes to my phone, will report how it behaves. I am currently using latest Omega Rom V15.

I have the modified android.policy.jar in case anyone wants it

Edit #2: apparently the mod is working, do not notice any lag when turning the screen on. The process shows on better battery stat.





So that means that the cpu will idle at 200 mhz? Ill make a battery test tomorrow under my normal usage.
Ill post results later
Can I have the android.policy.jar, please?
So, I just replace it with the one in root/system/framework? (Yes, I'll make a backup first :D)
 

bungknees

Senior Member
Jun 11, 2010
910
276
0
You need apktool to decompile the android.policy.jar

BTW, I just applied the changes to my phone, will report how it behaves. I am currently using latest Omega Rom V15.

I have the modified android.policy.jar in case anyone wants it

Edit #2: apparently the mod is working, do not notice any lag when turning the screen on. The process shows on better battery stat.





So that means that the cpu will idle at 200 mhz? Ill make a battery test tomorrow under my normal usage.
Ill post results later
I don't have access To a computer for the next few days, all I have is my note 2 to keep me entertained. If I upload my android.policy.jar would there be any chance you could edit it for me? Would like to try this mod out. If not that's no biggie.

Cheers.
 

bungknees

Senior Member
Jun 11, 2010
910
276
0
Nicheuji which Rom are you using?

Bungknees, sure, upload it and Ill work on it. Which Rom are you using?

Here, I am uploading my android.policy.jar for OMEGA ROM V15

You just have to replace the one under system/framework. I used Rootz Explorer to do this
I'm using a rom from another forum, and it's odexed, so that may cause a problem. But here it is anyway and I'll try it. Thanks.
 

Attachments

jvillatoro

Member
Aug 4, 2008
24
12
0
I'm using a rom from another forum, and it's odexed, so that may cause a problem. But here it is anyway and I'll try it. Thanks.
That's going to be a problem because I have no experience at all with odexed files. If it was deodexed, I could've help you.

I tried your file with the tools I have and I got a bunch of errors.

Sorry
 
  • Like
Reactions: bungknees
Our Apps
Get our official app!
The best way to access XDA on your phone
Nav Gestures
Add swipe gestures to any Android
One Handed Mode
Eases uses one hand with your phone