From 5ea1f1dc52d1830ba6b52dc173142a244b2259f5 Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 7 Jul 2016 11:35:37 +0200 Subject: [PATCH] reschedule the notifications after reboot --- app/src/main/AndroidManifest.xml | 11 ++- .../simplemobiletools/calendar/DBHelper.java | 74 ++++++++++++------- .../com/simplemobiletools/calendar/Utils.java | 25 +++++++ .../calendar/activities/EventActivity.java | 26 +------ .../receivers/BootCompletedReceiver.java | 22 ++++++ .../NotificationReceiver.java} | 8 +- 6 files changed, 114 insertions(+), 52 deletions(-) create mode 100644 app/src/main/java/com/simplemobiletools/calendar/receivers/BootCompletedReceiver.java rename app/src/main/java/com/simplemobiletools/calendar/{NotificationPublisher.java => receivers/NotificationReceiver.java} (88%) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index bdc5c011c..076d25c92 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,6 +3,8 @@ package="com.simplemobiletools.calendar" xmlns:android="http://schemas.android.com/apk/res/android"> + + - + + + + + + + + diff --git a/app/src/main/java/com/simplemobiletools/calendar/DBHelper.java b/app/src/main/java/com/simplemobiletools/calendar/DBHelper.java index 2c6964be0..37580b01d 100644 --- a/app/src/main/java/com/simplemobiletools/calendar/DBHelper.java +++ b/app/src/main/java/com/simplemobiletools/calendar/DBHelper.java @@ -8,6 +8,8 @@ import android.database.sqlite.SQLiteOpenHelper; import com.simplemobiletools.calendar.models.Event; +import org.joda.time.DateTime; + import java.util.ArrayList; import java.util.List; @@ -92,31 +94,6 @@ public class DBHelper extends SQLiteOpenHelper { mCallback.eventsDeleted(ids.length); } - public void getEvents(int fromTS, int toTS) { - final String[] projection = {COL_ID, COL_START_TS, COL_END_TS, COL_TITLE, COL_DESCRIPTION, COL_REMINDER_MINUTES}; - List events = new ArrayList<>(); - final String selection = COL_START_TS + " <= ? AND " + COL_END_TS + " >= ?"; - final String[] selectionArgs = {String.valueOf(toTS), String.valueOf(fromTS)}; - final Cursor cursor = mDb.query(TABLE_NAME, projection, selection, selectionArgs, null, null, COL_START_TS); - if (cursor != null) { - if (cursor.moveToFirst()) { - do { - final int id = cursor.getInt(cursor.getColumnIndex(COL_ID)); - 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)); - events.add(new Event(id, startTS, endTS, title, description, reminderMinutes)); - } while (cursor.moveToNext()); - } - cursor.close(); - } - - if (mCallback != null) - mCallback.gotEvents(events); - } - 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 + " = ?"; @@ -136,6 +113,53 @@ public class DBHelper extends SQLiteOpenHelper { return null; } + public void getEvents(int fromTS, int toTS) { + List events = new ArrayList<>(); + final String[] projection = getAllColumns(); + final String selection = COL_START_TS + " <= ? AND " + COL_END_TS + " >= ?"; + final String[] selectionArgs = {String.valueOf(toTS), String.valueOf(fromTS)}; + final Cursor cursor = mDb.query(TABLE_NAME, projection, selection, selectionArgs, null, null, COL_START_TS); + if (cursor != null) { + events = fillEvents(cursor); + } + + if (mCallback != null) + mCallback.gotEvents(events); + } + + public List getEventsAtReboot() { + List events = new ArrayList<>(); + final String[] projection = getAllColumns(); + final String selection = COL_START_TS + " > ? AND " + COL_REMINDER_MINUTES + " != ?"; + final String[] selectionArgs = {String.valueOf(DateTime.now().getMillis() / 1000), "-1"}; + final Cursor cursor = mDb.query(TABLE_NAME, projection, selection, selectionArgs, null, null, null); + if (cursor != null) { + events = fillEvents(cursor); + } + return events; + } + + private String[] getAllColumns() { + return new String[]{COL_ID, COL_START_TS, COL_END_TS, COL_TITLE, COL_DESCRIPTION, COL_REMINDER_MINUTES}; + } + + private List fillEvents(Cursor cursor) { + final List events = new ArrayList<>(); + if (cursor.moveToFirst()) { + do { + final int id = cursor.getInt(cursor.getColumnIndex(COL_ID)); + 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)); + events.add(new Event(id, startTS, endTS, title, description, reminderMinutes)); + } while (cursor.moveToNext()); + } + cursor.close(); + return events; + } + public interface DBOperationsListener { void eventInserted(Event event); diff --git a/app/src/main/java/com/simplemobiletools/calendar/Utils.java b/app/src/main/java/com/simplemobiletools/calendar/Utils.java index 168f93079..012b5f982 100644 --- a/app/src/main/java/com/simplemobiletools/calendar/Utils.java +++ b/app/src/main/java/com/simplemobiletools/calendar/Utils.java @@ -1,9 +1,16 @@ package com.simplemobiletools.calendar; +import android.app.AlarmManager; +import android.app.PendingIntent; import android.content.Context; +import android.content.Intent; import android.graphics.Color; +import android.os.SystemClock; import android.widget.Toast; +import com.simplemobiletools.calendar.models.Event; +import com.simplemobiletools.calendar.receivers.NotificationReceiver; + public class Utils { public static int adjustAlpha(int color, float factor) { @@ -17,4 +24,22 @@ public class Utils { public static void showToast(Context context, int resId) { Toast.makeText(context, context.getResources().getString(resId), Toast.LENGTH_SHORT).show(); } + + public static void scheduleNotification(Context context, Event event) { + if (event.getReminderMinutes() == -1) { + return; + } + + final long delayFromNow = (long) event.getStartTS() * 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 AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); + alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, notifInMs, pendingIntent); + } } diff --git a/app/src/main/java/com/simplemobiletools/calendar/activities/EventActivity.java b/app/src/main/java/com/simplemobiletools/calendar/activities/EventActivity.java index 1d155fcce..010582a7a 100644 --- a/app/src/main/java/com/simplemobiletools/calendar/activities/EventActivity.java +++ b/app/src/main/java/com/simplemobiletools/calendar/activities/EventActivity.java @@ -1,13 +1,10 @@ package com.simplemobiletools.calendar.activities; -import android.app.AlarmManager; import android.app.DatePickerDialog; -import android.app.PendingIntent; import android.app.TimePickerDialog; import android.content.Context; import android.content.Intent; import android.os.Bundle; -import android.os.SystemClock; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.AppCompatSpinner; import android.view.Menu; @@ -23,7 +20,6 @@ import android.widget.TimePicker; import com.simplemobiletools.calendar.Constants; import com.simplemobiletools.calendar.DBHelper; import com.simplemobiletools.calendar.Formatter; -import com.simplemobiletools.calendar.NotificationPublisher; import com.simplemobiletools.calendar.R; import com.simplemobiletools.calendar.Utils; import com.simplemobiletools.calendar.models.Event; @@ -310,34 +306,16 @@ public class EventActivity extends AppCompatActivity implements DBHelper.DBOpera } } - private void handleNotification(Event event) { - if (event.getReminderMinutes() == -1) { - return; - } - - final long delayFromNow = (long) event.getStartTS() * 1000 - event.getReminderMinutes() * 60000 - System.currentTimeMillis(); - if (delayFromNow < 0) { - return; - } - - final long notifInMs = SystemClock.elapsedRealtime() + delayFromNow; - final Intent intent = new Intent(this, NotificationPublisher.class); - intent.putExtra(NotificationPublisher.EVENT_ID, event.getId()); - final PendingIntent pendingIntent = PendingIntent.getBroadcast(this, event.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT); - final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); - alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, notifInMs, pendingIntent); - } - @Override public void eventInserted(Event event) { - handleNotification(event); + Utils.scheduleNotification(getApplicationContext(), event); Utils.showToast(getApplicationContext(), R.string.event_added); finish(); } @Override public void eventUpdated(Event event) { - handleNotification(event); + Utils.scheduleNotification(getApplicationContext(), event); Utils.showToast(getApplicationContext(), R.string.event_updated); finish(); } diff --git a/app/src/main/java/com/simplemobiletools/calendar/receivers/BootCompletedReceiver.java b/app/src/main/java/com/simplemobiletools/calendar/receivers/BootCompletedReceiver.java new file mode 100644 index 000000000..f4cc39f27 --- /dev/null +++ b/app/src/main/java/com/simplemobiletools/calendar/receivers/BootCompletedReceiver.java @@ -0,0 +1,22 @@ +package com.simplemobiletools.calendar.receivers; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; + +import com.simplemobiletools.calendar.DBHelper; +import com.simplemobiletools.calendar.Utils; +import com.simplemobiletools.calendar.models.Event; + +import java.util.List; + +public class BootCompletedReceiver extends BroadcastReceiver { + + @Override + public void onReceive(Context context, Intent arg1) { + final List events = DBHelper.newInstance(context, null).getEventsAtReboot(); + for (Event event : events) { + Utils.scheduleNotification(context, event); + } + } +} diff --git a/app/src/main/java/com/simplemobiletools/calendar/NotificationPublisher.java b/app/src/main/java/com/simplemobiletools/calendar/receivers/NotificationReceiver.java similarity index 88% rename from app/src/main/java/com/simplemobiletools/calendar/NotificationPublisher.java rename to app/src/main/java/com/simplemobiletools/calendar/receivers/NotificationReceiver.java index ebee4d139..1deb1bd6d 100644 --- a/app/src/main/java/com/simplemobiletools/calendar/NotificationPublisher.java +++ b/app/src/main/java/com/simplemobiletools/calendar/receivers/NotificationReceiver.java @@ -1,4 +1,4 @@ -package com.simplemobiletools.calendar; +package com.simplemobiletools.calendar.receivers; import android.app.Notification; import android.app.NotificationManager; @@ -9,10 +9,14 @@ import android.content.Intent; import android.media.RingtoneManager; import android.net.Uri; +import com.simplemobiletools.calendar.Constants; +import com.simplemobiletools.calendar.DBHelper; +import com.simplemobiletools.calendar.Formatter; +import com.simplemobiletools.calendar.R; import com.simplemobiletools.calendar.activities.EventActivity; import com.simplemobiletools.calendar.models.Event; -public class NotificationPublisher extends BroadcastReceiver { +public class NotificationReceiver extends BroadcastReceiver { public static String EVENT_ID = "event_id"; public void onReceive(Context context, Intent intent) {