reschedule the notifications after reboot
This commit is contained in:
parent
0d88e35297
commit
5ea1f1dc52
|
@ -3,6 +3,8 @@
|
|||
package="com.simplemobiletools.calendar"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/launcher"
|
||||
|
@ -60,7 +62,14 @@
|
|||
android:resource="@xml/widget_info"/>
|
||||
</receiver>
|
||||
|
||||
<receiver android:name=".NotificationPublisher"/>
|
||||
<receiver android:name=".receivers.NotificationReceiver"/>
|
||||
|
||||
<receiver android:name=".receivers.BootCompletedReceiver">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.BOOT_COMPLETED"/>
|
||||
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
|
||||
</application>
|
||||
|
||||
|
|
|
@ -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<Event> 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<Event> 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<Event> getEventsAtReboot() {
|
||||
List<Event> 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<Event> fillEvents(Cursor cursor) {
|
||||
final List<Event> 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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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<Event> events = DBHelper.newInstance(context, null).getEventsAtReboot();
|
||||
for (Event event : events) {
|
||||
Utils.scheduleNotification(context, event);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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) {
|
Loading…
Reference in New Issue