presnooze missed readings/pump on startup

This commit is contained in:
AdrianLxM 2017-11-06 20:32:33 +01:00
parent 2f56687e82
commit 2e6a136de6

View file

@ -9,9 +9,7 @@ import android.app.PendingIntent;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.os.PowerManager; import android.os.PowerManager;
import android.preference.PreferenceManager;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -50,7 +48,7 @@ public class KeepAliveReceiver extends BroadcastReceiver {
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, ""); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire(); wl.acquire();
initAlarmTimes(); shortenSnoozeInterval();
checkBg(); checkBg();
checkPump(); checkPump();
@ -117,6 +115,10 @@ public class KeepAliveReceiver extends BroadcastReceiver {
} }
public void setAlarm(Context context) { public void setAlarm(Context context) {
shortenSnoozeInterval();
presnoozeAlarms();
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, KeepAliveReceiver.class); Intent i = new Intent(context, KeepAliveReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0); PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
@ -128,6 +130,18 @@ public class KeepAliveReceiver extends BroadcastReceiver {
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), Constants.keepAliveMsecs, pi); am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), Constants.keepAliveMsecs, pi);
} }
/*Presnoozes the alarms with 5 minutes if no snooze exists.
* Call only at startup!
*/
public void presnoozeAlarms() {
if(SP.getLong("nextMissedReadingsAlarm", 0l) < System.currentTimeMillis()){
SP.putLong("nextMissedReadingsAlarm", System.currentTimeMillis() + 5*60*1000);
}
if(SP.getLong("nextPumpDisconnectedAlarm", 0l) < System.currentTimeMillis()){
SP.putLong("nextPumpDisconnectedAlarm", System.currentTimeMillis() + 5*60*1000);
}
}
public void cancelAlarm(Context context) { public void cancelAlarm(Context context) {
Intent intent = new Intent(context, KeepAliveReceiver.class); Intent intent = new Intent(context, KeepAliveReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0); PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
@ -135,7 +149,7 @@ public class KeepAliveReceiver extends BroadcastReceiver {
alarmManager.cancel(sender); alarmManager.cancel(sender);
} }
static void initAlarmTimes() { static void shortenSnoozeInterval() {
//shortens alarm times in case of setting changes or future data //shortens alarm times in case of setting changes or future data
long nextMissedReadingsAlarm = SP.getLong("nextMissedReadingsAlarm", 0l); long nextMissedReadingsAlarm = SP.getLong("nextMissedReadingsAlarm", 0l);
nextMissedReadingsAlarm = Math.min(System.currentTimeMillis() + missedReadingsThreshold(), nextMissedReadingsAlarm); nextMissedReadingsAlarm = Math.min(System.currentTimeMillis() + missedReadingsThreshold(), nextMissedReadingsAlarm);