Commit e00467af authored by Sebastián Katzer's avatar Sebastián Katzer
Browse files

Fix & simplify repeating on Android

parent 30f8f6aa
Loading
Loading
Loading
Loading
+9 −27
Changes for src/android/notification/Notification.java: 9 added lines, 27 removed lines.
Original line number Diff line number Diff line
@@ -45,7 +45,7 @@ import java.util.Date;
public class Notification {

    // Used to differ notifications by their life cycle state
    public static enum Type {
    public enum Type {
        ALL, SCHEDULED, TRIGGERED
    }

@@ -162,7 +162,7 @@ public class Notification {
     * Schedule the local notification.
     */
    public void schedule() {
        long triggerTime = getNextTriggerTime();
        long triggerTime = options.getTriggerTime();

        persist();

@@ -174,15 +174,11 @@ public class Notification {
        PendingIntent pi = PendingIntent.getBroadcast(
                context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        getAlarmMgr().set(AlarmManager.RTC_WAKEUP, triggerTime, pi);
    }

    /**
     * Re-schedule the local notification if repeating.
     */
    void reschedule () {
        if (isRepeating()) {
            schedule();
            getAlarmMgr().setRepeating(AlarmManager.RTC_WAKEUP,
                    triggerTime, options.getRepeatInterval(), pi);
        } else {
            getAlarmMgr().set(AlarmManager.RTC_WAKEUP, triggerTime, pi);
        }
    }

@@ -250,27 +246,12 @@ public class Notification {
        // TODO
    }

    /**
     * Next trigger time.
     */
    public long getNextTriggerTime() {
        long triggerTime = options.getTriggerTime();

        if (!isRepeating() || !isTriggered())
            return triggerTime;

        long interval    = options.getRepeatInterval();
        int triggerCount = getTriggerCountSinceSchedule();

        return triggerTime + (triggerCount + 1) * interval;
    }

    /**
     * Count of triggers since schedule.
     */
    public int getTriggerCountSinceSchedule() {
        long now = System.currentTimeMillis();
        long initTriggerTime = options.getTriggerTime();
        long triggerTime = options.getTriggerTime();

        if (!wasInThePast())
            return 0;
@@ -278,7 +259,7 @@ public class Notification {
        if (!isRepeating())
            return 1;

        return (int) ((now - initTriggerTime) / options.getRepeatInterval());
        return (int) ((now - triggerTime) / options.getRepeatInterval());
    }

    /**
@@ -294,6 +275,7 @@ public class Notification {
            e.printStackTrace();
        }

        json.remove("firstAt");
        json.remove("updatedAt");
        json.remove("soundUri");
        json.remove("iconUri");
+15 −12
Changes for src/android/notification/Options.java: 15 added lines, 12 removed lines.
Original line number Diff line number Diff line
@@ -189,31 +189,34 @@ public class Options {
    }

    /**
     * Trigger date in milliseconds.
     * ID for the local notification as a number.
     */
    public long getTriggerTime() {
        return options.optLong("at", 0) * 1000;
    public Integer getId() {
        return options.optInt("id", 0);
    }

    /**
     * Trigger date.
     * ID for the local notification as a string.
     */
    public Date getTriggerDate() {
        return new Date(getTriggerTime());
    public String getIdStr() {
        return getId().toString();
    }

    /**
     * ID for the local notification as a number.
     * Trigger date.
     */
    public Integer getId() {
        return options.optInt("id", 0);
    public Date getTriggerDate() {
        return new Date(getTriggerTime());
    }

    /**
     * ID for the local notification as a string.
     * Trigger date in milliseconds.
     */
    public String getIdStr() {
        return getId().toString();
    public long getTriggerTime() {
        return Math.max(
                System.currentTimeMillis(),
                options.optLong("at", 0) * 1000
        );
    }

    /**
+0 −5
Changes for src/android/notification/TriggerReceiver.java: 0 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -42,11 +42,6 @@ public class TriggerReceiver extends AbstractTriggerReceiver {
     */
    @Override
    public void onTrigger (Notification notification, boolean updated) {

        if (notification.isRepeating()) {
            notification.reschedule();
        }

        notification.show();
    }