reschedule daily and weekly recurring notifications at the notif

This commit is contained in:
tibbi 2016-09-13 20:41:40 +02:00
parent 234691b0ed
commit 71161651f4
3 changed files with 36 additions and 22 deletions

View File

@ -156,21 +156,13 @@ public class DBHelper extends SQLiteOpenHelper {
}
public Event getEvent(int id) {
final String[] projection = {COL_START_TS, COL_END_TS, COL_TITLE, COL_DESCRIPTION, COL_REMINDER_MINUTES};
final String selection = COL_ID + " = ?";
final String selection = MAIN_TABLE_NAME + "." + COL_ID + " = ?";
final String[] selectionArgs = {String.valueOf(id)};
final Cursor cursor = mDb.query(MAIN_TABLE_NAME, projection, selection, selectionArgs, null, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
final int startTS = cursor.getInt(cursor.getColumnIndex(COL_START_TS));
final int endTS = cursor.getInt(cursor.getColumnIndex(COL_END_TS));
final String title = cursor.getString(cursor.getColumnIndex(COL_TITLE));
final String description = cursor.getString(cursor.getColumnIndex(COL_DESCRIPTION));
final int reminderMinutes = cursor.getInt(cursor.getColumnIndex(COL_REMINDER_MINUTES));
cursor.close();
return new Event(id, startTS, endTS, title, description, reminderMinutes, 0);
}
}
final Cursor cursor = getEventsCursor(selection, selectionArgs);
final List<Event> events = fillEvents(cursor);
if (!events.isEmpty())
return events.get(0);
return null;
}
@ -208,8 +200,8 @@ public class DBHelper extends SQLiteOpenHelper {
newEvents.addAll(getEvents(selection, ts));
// get yearly events
selection = COL_REPEAT_INTERVAL + " = " + Constants.YEAR + " AND " + COL_REPEAT_MONTH + " = " + dateTime.getMonthOfYear() + " AND " +
COL_REPEAT_DAY + " = " + dateTime.getDayOfMonth() + " AND " + COL_REPEAT_START + " <= " + dayEnd;
selection = COL_REPEAT_INTERVAL + " = " + Constants.YEAR + " AND " + COL_REPEAT_MONTH + " = " + dateTime.getMonthOfYear() +
" AND " + COL_REPEAT_DAY + " = " + dateTime.getDayOfMonth() + " AND " + COL_REPEAT_START + " <= " + dayEnd;
newEvents.addAll(getEvents(selection, ts));
return newEvents;

View File

@ -30,19 +30,26 @@ public class Utils {
return;
}
final long delayFromNow = (long) event.getStartTS() * 1000 - event.getReminderMinutes() * 60000 - System.currentTimeMillis();
if (delayFromNow < 0) {
return;
scheduleEventIn(context, event.getStartTS(), event);
}
public static void scheduleEventIn(Context context, int notifTS, Event event) {
final long delayFromNow = (long) notifTS * 1000 - event.getReminderMinutes() * 60000 - System.currentTimeMillis();
if (delayFromNow < 0)
return;
final long notifInMs = SystemClock.elapsedRealtime() + delayFromNow;
final Intent intent = new Intent(context, NotificationReceiver.class);
intent.putExtra(NotificationReceiver.EVENT_ID, event.getId());
final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, event.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
final PendingIntent pendingIntent = getNotificationIntent(context, event.getId());
final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, notifInMs, pendingIntent);
}
public static PendingIntent getNotificationIntent(Context context, int eventId) {
final Intent intent = new Intent(context, NotificationReceiver.class);
intent.putExtra(NotificationReceiver.EVENT_ID, eventId);
return PendingIntent.getBroadcast(context, eventId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
public static int[] getLetterIDs() {
return new int[]{R.string.sunday_letter, R.string.monday_letter, R.string.tuesday_letter, R.string.wednesday_letter,
R.string.thursday_letter, R.string.friday_letter, R.string.saturday_letter};

View File

@ -13,6 +13,7 @@ import com.simplemobiletools.calendar.Constants;
import com.simplemobiletools.calendar.DBHelper;
import com.simplemobiletools.calendar.Formatter;
import com.simplemobiletools.calendar.R;
import com.simplemobiletools.calendar.Utils;
import com.simplemobiletools.calendar.activities.EventActivity;
import com.simplemobiletools.calendar.models.Event;
@ -35,6 +36,20 @@ public class NotificationReceiver extends BroadcastReceiver {
final String title = event.getTitle();
final Notification notification = getNotification(context, pendingIntent, startTime + " - " + endTime + " " + title);
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);
}
}
private PendingIntent getPendingIntent(Context context, Event event) {