The following code easily demonstrates the accumulating RTC_WAKEUP bug in com.android.deskclock:
Code:
$ watch -n 2 sh -c 'clear;dumpsys alarm|grep RTC_WAKEUP'
Now open Clock, add an alarm and repeatedly turn it on and off. Every time you interact with the alarms in Clock the number of RTC_WAKEUP events for com.android.deskclock increases. These wakeup events are only removed when Clock is forcibly closed or the zygote is restarted.
The following patch to DeskClock fixes this bug. Once it is applied, com.android.deskclock only ever has one outstanding RTC_WAKEUP event. This event is updated when the related alarm expires or is disabled before expiry.
Code:
diff --git a/src/com/android/deskclock/Alarms.java b/src/com/android/deskclock/Alarms.java
index 9dbe63a..4f0b488 100644
--- a/src/com/android/deskclock/Alarms.java
+++ b/src/com/android/deskclock/Alarms.java
@@ -425,7 +425,7 @@ public class Alarms {
intent.putExtra(ALARM_RAW_DATA, out.marshall());
PendingIntent sender = PendingIntent.getBroadcast(
- context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
+ context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, atTimeInMillis, sender);
@@ -451,7 +451,7 @@ public class Alarms {
context.getSystemService(Context.ALARM_SERVICE);
PendingIntent sender = PendingIntent.getBroadcast(
context, 0, new Intent(ALARM_ALERT_ACTION),
- PendingIntent.FLAG_CANCEL_CURRENT);
+ PendingIntent.FLAG_UPDATE_CURRENT);
am.cancel(sender);
setStatusBarIcon(context, false);
saveNextAlarm(context, "");
A patched version of DeskClock.apk is attached to this posting. To test it, simply replace the version on your phone with this one, taking care to set the file permissions and ownership correctly. Force close Clock and clear its cache and data before you replace the apk. Test the clock with several subsequent alarms, have some repeating alarms and check whether all alarms go off. I have tested the patched version for about half an hour, it seems to work. I have not been able to test whether repeating alarms work as that would take at least a day. I have no reason to believe they would not work but given the nature of the beast it might be prudent to have a backup alarm in case you depend on being alerted by your phone. Insert standard disclaimer, objects in the mirror are closer than they appear, not suitable for children of 45 or younger, etc.
I submitted this bug and patch to the Android bug tracker:
#52601. With a bit of luck this problem will be patched upstream. I have not submitted it to Cyanogenmod yet but might do so if the response on the Android bug tracker is tardy or lacking.