add rescheduling monthly and yearly events

This commit is contained in:
tibbi 2016-09-13 21:40:51 +02:00
parent 71161651f4
commit 1d65ad305e
5 changed files with 36 additions and 19 deletions

View File

@ -9,7 +9,7 @@ public class Constants {
public static final int DAY = 86400;
public static final int WEEK = 604800;
public static final int MONTH = 2592000;
public static final int MONTH = 2592000; // exact value not taken into account, Joda is used for adding months and years
public static final int YEAR = 31536000;
// Shared Preferences

View File

@ -217,6 +217,7 @@ public class DBHelper extends SQLiteOpenHelper {
}
events.addAll(currEvents);
}
return events;
}

View File

@ -11,6 +11,8 @@ import android.widget.Toast;
import com.simplemobiletools.calendar.models.Event;
import com.simplemobiletools.calendar.receivers.NotificationReceiver;
import org.joda.time.DateTime;
public class Utils {
public static int adjustAlpha(int color, float factor) {
@ -25,16 +27,41 @@ public class Utils {
Toast.makeText(context, context.getResources().getString(resId), Toast.LENGTH_SHORT).show();
}
public static void scheduleNotification(Context context, Event event) {
if (event.getReminderMinutes() == -1) {
return;
public static void scheduleNextEvent(Context context, Event event) {
int startTS = event.getStartTS() - event.getReminderMinutes() * 60;
int newTS = 0;
if (event.getRepeatInterval() == Constants.DAY || event.getRepeatInterval() == Constants.WEEK) {
while (startTS < System.currentTimeMillis() / 1000 + 5) {
startTS += event.getRepeatInterval();
}
newTS = startTS;
} else if (event.getRepeatInterval() == Constants.MONTH) {
newTS = getNewTS(startTS, true);
} else if (event.getRepeatInterval() == Constants.YEAR) {
newTS = getNewTS(startTS, false);
}
scheduleEventIn(context, event.getStartTS(), event);
if (newTS != 0)
Utils.scheduleEventIn(context, newTS, event);
}
private static int getNewTS(int ts, boolean isMonthly) {
DateTime dateTime = Formatter.getDateTimeFromTS(ts);
while (dateTime.isBeforeNow()) {
dateTime = isMonthly ? dateTime.plusMonths(1) : dateTime.plusYears(1);
}
return (int) (dateTime.getMillis() / 1000);
}
public static void scheduleNotification(Context context, Event event) {
if (event.getReminderMinutes() == -1)
return;
scheduleNextEvent(context, event);
}
public static void scheduleEventIn(Context context, int notifTS, Event event) {
final long delayFromNow = (long) notifTS * 1000 - event.getReminderMinutes() * 60000 - System.currentTimeMillis();
final long delayFromNow = (long) notifTS * 1000 - System.currentTimeMillis();
if (delayFromNow < 0)
return;

View File

@ -355,9 +355,9 @@ public class EventActivity extends SimpleActivity implements DBHelper.DBOperatio
if (DateTime.now().isAfter(mEventStartDateTime.getMillis())) {
Utils.showToast(getApplicationContext(), R.string.past_event_added);
} else {
Utils.scheduleNotification(getApplicationContext(), event);
Utils.showToast(getApplicationContext(), R.string.event_added);
}
Utils.scheduleNotification(getApplicationContext(), event);
finish();
}

View File

@ -38,18 +38,7 @@ public class NotificationReceiver extends BroadcastReceiver {
notificationManager.notify(id, notification);
if (event.getRepeatInterval() != 0)
scheduleNextEvent(context, event);
}
private void scheduleNextEvent(Context context, Event event) {
if (event.getRepeatInterval() == Constants.DAY || event.getRepeatInterval() == Constants.WEEK) {
int startTS = event.getStartTS();
while (startTS < System.currentTimeMillis() / 1000 + 5) {
startTS += event.getRepeatInterval();
}
Utils.scheduleEventIn(context, startTS, event);
}
Utils.scheduleNextEvent(context, event);
}
private PendingIntent getPendingIntent(Context context, Event event) {