Commit ae9c73aa authored by Robert Hitt's avatar Robert Hitt

Notifications now restored on boot

parent 5323c2ad
Pipeline #815 failed with stages
in 4 minutes and 42 seconds
......@@ -3,6 +3,8 @@
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:name=".MyApplication"
android:allowBackup="true"
......@@ -37,7 +39,13 @@
<receiver android:name=".util.NotificationReceiver"
android:process=":remote" >
</receiver>
<receiver android:name=".util.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service android:name=".util.NotificationService"
......
......@@ -113,7 +113,7 @@ public class NotificationPresenter {
bgRealm -> bgRealm.copyToRealmOrUpdate(n),
() -> {
// TODO: make async
createAlarmsForFacility(n);
createAlarmsForFacility(mNotificationView.getContext(), n);
Toast.makeText(mNotificationView.getContext(),
message, Toast.LENGTH_SHORT).show();
......@@ -130,7 +130,8 @@ public class NotificationPresenter {
}
// Sets Alarms for the Facility with the given name
private void createAlarmsForFacility(NotificationSettings notificationSettings) {
public static void createAlarmsForFacility(Context context,
NotificationSettings notificationSettings) {
Realm realm = Realm.getDefaultInstance();
Facility facility = realm.where(Facility.class)
.equalTo("mName", notificationSettings.name).findFirst();
......@@ -140,13 +141,13 @@ public class NotificationPresenter {
return;
for(OpenTimes o : openTimesList) {
setAlarmsFromOpenTimes(o, notificationSettings);
setAlarmsFromOpenTimes(context, o, notificationSettings);
}
realm.close();
}
private void setAlarmsFromOpenTimes(OpenTimes openTimes,
private static void setAlarmsFromOpenTimes(Context context, OpenTimes openTimes,
NotificationSettings n) {
String name = n.name;
......@@ -156,38 +157,38 @@ public class NotificationPresenter {
if(n.opening) {
if(n.interval_on)
setAlarm(name, day, "Op_on", 0, openTimes.getStartTime(),
setAlarm(context, name, day, "Op_on", 0, openTimes.getStartTime(),
"Opens now");
if(n.interval_15)
setAlarm(name, day, "Op_15", 15, openTimes.getStartTime(),
setAlarm(context, name, day, "Op_15", 15, openTimes.getStartTime(),
"Opens in 15 minutes");
if(n.interval_30)
setAlarm(name, day, "Op_30", 30, openTimes.getStartTime(),
setAlarm(context, name, day, "Op_30", 30, openTimes.getStartTime(),
"Opens in 30 minutes");
if(n.interval_hour)
setAlarm(name, day, "Op_hour", 60, openTimes.getStartTime(),
setAlarm(context, name, day, "Op_hour", 60, openTimes.getStartTime(),
"Opens in an hour");
}
if(n.closing) {
if(n.interval_on)
setAlarm(name, day, "Cl_on", 0, openTimes.getEndTime(),
setAlarm(context, name, day, "Cl_on", 0, openTimes.getEndTime(),
"Closes now");
if(n.interval_15)
setAlarm(name, day, "Cl_15", 15, openTimes.getEndTime(),
setAlarm(context, name, day, "Cl_15", 15, openTimes.getEndTime(),
"Closes in 15 minutes");
if(n.interval_30)
setAlarm(name, day, "Cl_30", 30, openTimes.getEndTime(),
setAlarm(context, name, day, "Cl_30", 30, openTimes.getEndTime(),
"Closes in 30 minutes");
if(n.interval_hour)
setAlarm(name, day, "Cl_hour", 60, openTimes.getEndTime(),
setAlarm(context, name, day, "Cl_hour", 60, openTimes.getEndTime(),
"Closes in an hour");
}
}
}
private void setAlarm(String name, int day, String type, int intervalMin, String time,
String message) {
private static void setAlarm(Context context, String name, int day, String type,
int intervalMin, String time, String message) {
Long alarmTime = parseTimeStringToMs(time, day, Calendar.getInstance());
......@@ -198,17 +199,16 @@ public class NotificationPresenter {
Log.i("Set hash for " + name + day + type, ""+id);
// Construct an Intent to execute the NotificationReceiver
Intent intent = new Intent(mNotificationView.getContext(), NotificationReceiver.class);
Intent intent = new Intent(context, NotificationReceiver.class);
intent.putExtra("title", name);
intent.putExtra("text", message);
// Create a PendingIntent that will be triggered when the alarm goes off
PendingIntent pendingIntent = PendingIntent.getBroadcast(mNotificationView.getContext(),
id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Set up alarm to repeat every week
AlarmManager alarm = (AlarmManager)
mNotificationView.getContext().getSystemService(Context.ALARM_SERVICE);
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, 7*1440*60000, pendingIntent);
}
......@@ -262,7 +262,7 @@ public class NotificationPresenter {
// Returns the time in ms from epoch for the given time on the next Day
// of the week relative to the given Calendar
private Long parseTimeStringToMs(String timeString, int day, Calendar alarmCalendar) {
private static Long parseTimeStringToMs(String timeString, int day, Calendar alarmCalendar) {
// Determine if the time is in the past
boolean hasPassed = timeHasPassed(timeString, day, alarmCalendar);
......@@ -299,7 +299,7 @@ public class NotificationPresenter {
}
// Returns the active schedule given the current date
private RealmList<OpenTimes> getActiveSchedule(Facility facility, Calendar now) {
private static RealmList<OpenTimes> getActiveSchedule(Facility facility, Calendar now) {
RealmList<OpenTimes> openTimesList = facility.getMainSchedule().getOpenTimesList();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
......@@ -324,7 +324,7 @@ public class NotificationPresenter {
}
// Determines if the given time is earlier in the current day
public boolean timeHasPassed(String time, int day, Calendar now) {
public static boolean timeHasPassed(String time, int day, Calendar now) {
if(now.get(Calendar.DAY_OF_WEEK) != day)
return false;
......
package srct.whatsopen.util;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import io.realm.Realm;
import io.realm.RealmResults;
import srct.whatsopen.model.NotificationSettings;
import srct.whatsopen.presenters.NotificationPresenter;
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Resets Notifications on boot
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Realm realm = Realm.getDefaultInstance();
RealmResults<NotificationSettings> results = realm.where(NotificationSettings.class)
.findAll();
for(NotificationSettings n : results) {
if(n != null) {
NotificationPresenter.createAlarmsForFacility(context, n);
}
}
realm.close();
}
}
}
package srct.whatsopen.util;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
public class NotificationReceiver extends BroadcastReceiver {
public class NotificationReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
......@@ -14,6 +14,7 @@ public class NotificationReceiver extends BroadcastReceiver {
i.putExtra("title", intent.getStringExtra("title"));
i.putExtra("text", intent.getStringExtra("text"));
context.startService(i);
//context.startService(i);
startWakefulService(context, i);
}
}
......@@ -5,6 +5,7 @@ import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.WakefulBroadcastReceiver;
import srct.whatsopen.R;
......@@ -31,5 +32,6 @@ public class NotificationService extends IntentService {
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(id, builder.build());
WakefulBroadcastReceiver.completeWakefulIntent(intent);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment