[FIX] StartingAlertService Wakelock (TouchWizCalendar.apk)

Search This thread

rodries

Senior Member
Jul 2, 2011
410
183
Alicante
Xiaomi Mi 10T / 10T Pro
Hi

This is the solution to fix the StartingAlertService Wakelock
To reproduce the problem (or the fix) modify calendar settings to notify events in status bar, create a an event and add a reminder inmediatly, so a notificantion is triggered and you can see it in status bar, open BetterBatteryStats and you can check in partial wakelocks if you have the StartingAlertService Wakelock. You will see that the wakelock is always working, you only need to refresh data and the wakelock time is increased, the wakelock is not released until you close to notification.

The problem is in TouchWizCalendar.apk not releasing the wakelock properly in class ServiceHandler inside class AlertService
The actual code is:
Code:
private final class ServiceHandler extends Handler {
        public ServiceHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            processMessage(msg);
            AlertService.this.stopSelf();
        }
    }

If you check the new android sources you can see the next code:
Code:
 private final class ServiceHandler extends Handler {
        public ServiceHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            processMessage(msg);
            // NOTE: We MUST not call stopSelf() directly, since we need to
            // make sure the wake lock acquired by AlertReceiver is released.
            AlertReceiver.finishStartingService(AlertService.this, msg.arg1);
        }
    }

So the next change will fix the problem. You have to decompile TouchWizCalendar.apk locate com/android/calendar/AlertService$ServiceHandler.smali and search & replace next code.
Search (the last method):
Code:
# virtual methods
.method public handleMessage(Landroid/os/Message;)V
    .registers 3
    .parameter "msg"

    .prologue
    .line 594
    iget-object v0, p0, Lcom/android/calendar/AlertService$ServiceHandler;->this$0:Lcom/android/calendar/AlertService;

    invoke-virtual {v0, p1}, Lcom/android/calendar/AlertService;->processMessage(Landroid/os/Message;)V

    .line 598
    iget-object v0, p0, Lcom/android/calendar/AlertService$ServiceHandler;->this$0:Lcom/android/calendar/AlertService;

    invoke-virtual {v0}, Lcom/android/calendar/AlertService;->stopSelf()V

    .line 601
    return-void
.end method
Replace by:
Code:
# virtual methods
.method public handleMessage(Landroid/os/Message;)V
    .registers 4
    .parameter "msg"

    .prologue

    iget-object v0, p0, Lcom/android/calendar/AlertService$ServiceHandler;->this$0:Lcom/android/calendar/AlertService;

    invoke-virtual {v0, p1}, Lcom/android/calendar/AlertService;->processMessage(Landroid/os/Message;)V

    iget-object v0, p0, Lcom/android/calendar/AlertService$ServiceHandler;->this$0:Lcom/android/calendar/AlertService;

    iget v1, p1, Landroid/os/Message;->arg1:I

    invoke-static {v0, v1}, Lcom/android/calendar/AlertReceiver;->finishStartingService(Landroid/app/Service;I)V

    return-void
.end method

Now recompile TouchWizCalendar.apk and replace it.

I've attached a cwm zip file to create a backup (TouchWizCalendar.apk.bak), replace the apk, and clear battery stats to reset wakelocks counters (reset battery stats doesn't affect to SGS2 battery, the battery chip doesn't need that info). The apk is based on kk5 rom.
If you don't want to use the cwm file you can extract TouchWizCalendar.apk from the zip and use root explorer (or similar) to replace the file, don't forget to make a backup of your original apk.

Edit:
If you get:

Code:
# virtual methods
.method public handleMessage(Landroid/os/Message;)V
    .locals 1
    .parameter "msg"
...
...
...
when decompile then replace with
Code:
# virtual methods
.method public handleMessage(Landroid/os/Message;)V
    .locals 2
    .parameter "msg"
...
...
...

Edit2:
If you use odexed rom delete or rename the odex file (maybe com.android.calendar.odex or TouchWizCalendar.odex, I'm not sure) to be sure classes.dex is used from deodexed apk

Edit3:
I've updated the zip to be sure odex file is not used in odexed roms
 

Attachments

  • calendar_wakelock_fix_CWM.zip
    834.9 KB · Views: 1,441
Last edited:

starbase64

Senior Member
Apr 3, 2007
4,365
1,698
Hi,

this works fine on CheckRom v4, StartingAlertService now 0,3%.

with friendly greet

starbase64
 

rodries

Senior Member
Jul 2, 2011
410
183
Alicante
Xiaomi Mi 10T / 10T Pro
Last edited:

pissyhun

Senior Member
Nov 26, 2011
502
96
Iasi
hi
thx for the answer but i am not a developer or an expert in android commands. i read the post u link but gave me headache:))))
i will save the file and use it with a deodexed rom.
 

hugoxboss

Member
Mar 9, 2008
19
0
Leeuwarden
Love your fix

Yesterday I've installed your fix, and my wake locks for startingalertservice are gone. For your information I'm running a rooted version of the standard i9100XWKK2. Thanks again for your work, my battery time increased quit a bit.
 

ATN24

Senior Member
Jun 7, 2011
377
51
just to make sure, if my wakelocks are in seconds in betterbatterystats, i'm good right?
 

itaykoren

Senior Member
Jul 26, 2010
166
4
Thanks for the instructions!

However, my calendar apk file has a bit of a difference in the code.

For example, it starts with:

# virtual methods
.method public handleMessage(Landroid/os/Message;)V
.locals 1
.parameter "msg"


Can I use the same instructions?
 
Last edited:

cmd512

Senior Member
Aug 25, 2009
3,348
841
Shanghai
Thanks a bunch rodries, this is awesome. Instead of ".registers 3", my AlertService$ServiceHandler.smali had ".locals 1". So, I went ahead and changed that to ".registers 4" as well.

Just revised KK5 TouchWizCalendar.apk, working well. :)
 
Last edited:

TheMatrix2000

Senior Member
Jun 28, 2008
275
56
Herzogenrath
Hello rodies,

i am sorry but i can`t get it to work with my GingerMod v0.7 Rom.

http://xdaforums.com/showthread.php?t=1220283

Neither the cwm-file nor the copy direct option works.

I have the Wakelock also. I am not a programmer. So things like compile decompile are chinese to me.

Is it somehowe possible that you can contact the Developer of GingerMod that he can apply this patch for his Rom.

Many thanks in advanced.
 

rodries

Senior Member
Jul 2, 2011
410
183
Alicante
Xiaomi Mi 10T / 10T Pro
Hello rodies,

i am sorry but i can`t get it to work with my GingerMod v0.7 Rom.

http://xdaforums.com/showthread.php?t=1220283

Neither the cwm-file nor the copy direct option works.

I have the Wakelock also. I am not a programmer. So things like compile decompile are chinese to me.

Is it somehowe possible that you can contact the Developer of GingerMod that he can apply this patch for his Rom.

Many thanks in advanced.

Upload your original TouchWizCalendar.apk, I'll create a cwm file for you
 
  • Like
Reactions: TheMatrix2000

Top Liked Posts

  • There are no posts matching your filters.
  • 33
    Hi

    This is the solution to fix the StartingAlertService Wakelock
    To reproduce the problem (or the fix) modify calendar settings to notify events in status bar, create a an event and add a reminder inmediatly, so a notificantion is triggered and you can see it in status bar, open BetterBatteryStats and you can check in partial wakelocks if you have the StartingAlertService Wakelock. You will see that the wakelock is always working, you only need to refresh data and the wakelock time is increased, the wakelock is not released until you close to notification.

    The problem is in TouchWizCalendar.apk not releasing the wakelock properly in class ServiceHandler inside class AlertService
    The actual code is:
    Code:
    private final class ServiceHandler extends Handler {
            public ServiceHandler(Looper looper) {
                super(looper);
            }
    
            @Override
            public void handleMessage(Message msg) {
                processMessage(msg);
                AlertService.this.stopSelf();
            }
        }

    If you check the new android sources you can see the next code:
    Code:
     private final class ServiceHandler extends Handler {
            public ServiceHandler(Looper looper) {
                super(looper);
            }
    
            @Override
            public void handleMessage(Message msg) {
                processMessage(msg);
                // NOTE: We MUST not call stopSelf() directly, since we need to
                // make sure the wake lock acquired by AlertReceiver is released.
                AlertReceiver.finishStartingService(AlertService.this, msg.arg1);
            }
        }

    So the next change will fix the problem. You have to decompile TouchWizCalendar.apk locate com/android/calendar/AlertService$ServiceHandler.smali and search & replace next code.
    Search (the last method):
    Code:
    # virtual methods
    .method public handleMessage(Landroid/os/Message;)V
        .registers 3
        .parameter "msg"
    
        .prologue
        .line 594
        iget-object v0, p0, Lcom/android/calendar/AlertService$ServiceHandler;->this$0:Lcom/android/calendar/AlertService;
    
        invoke-virtual {v0, p1}, Lcom/android/calendar/AlertService;->processMessage(Landroid/os/Message;)V
    
        .line 598
        iget-object v0, p0, Lcom/android/calendar/AlertService$ServiceHandler;->this$0:Lcom/android/calendar/AlertService;
    
        invoke-virtual {v0}, Lcom/android/calendar/AlertService;->stopSelf()V
    
        .line 601
        return-void
    .end method
    Replace by:
    Code:
    # virtual methods
    .method public handleMessage(Landroid/os/Message;)V
        .registers 4
        .parameter "msg"
    
        .prologue
    
        iget-object v0, p0, Lcom/android/calendar/AlertService$ServiceHandler;->this$0:Lcom/android/calendar/AlertService;
    
        invoke-virtual {v0, p1}, Lcom/android/calendar/AlertService;->processMessage(Landroid/os/Message;)V
    
        iget-object v0, p0, Lcom/android/calendar/AlertService$ServiceHandler;->this$0:Lcom/android/calendar/AlertService;
    
        iget v1, p1, Landroid/os/Message;->arg1:I
    
        invoke-static {v0, v1}, Lcom/android/calendar/AlertReceiver;->finishStartingService(Landroid/app/Service;I)V
    
        return-void
    .end method

    Now recompile TouchWizCalendar.apk and replace it.

    I've attached a cwm zip file to create a backup (TouchWizCalendar.apk.bak), replace the apk, and clear battery stats to reset wakelocks counters (reset battery stats doesn't affect to SGS2 battery, the battery chip doesn't need that info). The apk is based on kk5 rom.
    If you don't want to use the cwm file you can extract TouchWizCalendar.apk from the zip and use root explorer (or similar) to replace the file, don't forget to make a backup of your original apk.

    Edit:
    If you get:

    Code:
    # virtual methods
    .method public handleMessage(Landroid/os/Message;)V
        .locals 1
        .parameter "msg"
    ...
    ...
    ...
    when decompile then replace with
    Code:
    # virtual methods
    .method public handleMessage(Landroid/os/Message;)V
        .locals 2
        .parameter "msg"
    ...
    ...
    ...

    Edit2:
    If you use odexed rom delete or rename the odex file (maybe com.android.calendar.odex or TouchWizCalendar.odex, I'm not sure) to be sure classes.dex is used from deodexed apk

    Edit3:
    I've updated the zip to be sure odex file is not used in odexed roms
    2
    Hello rodies

    i uploaded the whole cmw file from the GingerMod Rom. I didn't extract the apk because there is another file in it. The dev. didn't named it TouchWizCalender.apk but Calendar.apk.

    I think it's the same Programm though because i get the same Wakelock and also battery drain with it.

    Many thanks in advance. :):):)
    Sure thing. Perhaps when rodies agrees, we can give the mod to the GingerMod dev.

    I hope rodies can fix the nasty Bug for us.

    I'm a bit busy now, I'll patch your calendar tomorrow.
    Also I think there is more bugs in calendar code I'll try to fix them also.
    Samsung is using an old and bugged calendar version, S is not upgrading his modified apks with last android version.

    About Maps I'm not using it so I don't know nothing about maps wakelocks, tell me the wakelock name and I'll try to find the problem, but maps is a big apk, so it will be very hard to find the problem, and how to reproduce the problem.
    In calendar I know how to reproduce the problem so I can debug it and find the bug.

    The calendar fix is public so any dev can use it freely.
    If I find more bugs I'll publish how to fix them.
    2
    @rodries.

    After replacing the original TouchWizCalendar.apk by yours from the first post, I'm still getting wakelocks from Agenda (Dutch for Calendar). See the first picture. Batterylife is OK though (see the second picture). Also, I noticed that the APK's (my original and yours) differ almost 200 KB in size (see the third picture). Therefore my kind request: would you be so kind to modify my original TouchWizCalendar.apk (attached to this post)?

    My current firmware versions are (Android 2.3.6):
    - PDA : I9100XWKK2
    - PHONE : I9100XXKI4
    - CSC : I9100XENKJ1
    Build Info : Fri Nov 11 18:05:58 KST 2011

    Try to use this apk and comment
    Remember to reset battery stats to be sure problem is fixed
    The size is not important I've compressed the apk with 9
    1
    hi
    it will work on xwkk5 stock rom odexed?
    1
    I've attached my calendar. Thanks.

    Your apk is fixed
    You haven't the wakelock problem
    If you have the problem try to clean cache & dalvik-cache from recovery, then reboot, the first time you will have to wait a lot (about 5 mins), because you have to wait to recreate cache.