AndroidAPS/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java

60 lines
2.2 KiB
Java
Raw Normal View History

2016-07-07 00:56:31 +02:00
package info.nightscout.androidaps.receivers;
/**
* Created by mike on 07.07.2016.
*/
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2016-07-07 10:34:20 +02:00
import info.nightscout.androidaps.Config;
2016-07-07 00:56:31 +02:00
import info.nightscout.androidaps.Constants;
2016-07-08 13:41:53 +02:00
import info.nightscout.androidaps.MainActivity;
import info.nightscout.androidaps.plugins.DanaR.DanaRFragment;
2016-07-07 10:34:20 +02:00
import info.nightscout.androidaps.plugins.DanaR.Services.DanaRService;
2016-07-07 00:56:31 +02:00
public class KeepAliveReceiver extends BroadcastReceiver {
private static Logger log = LoggerFactory.getLogger(KeepAliveReceiver.class);
@Override
2016-07-07 10:34:20 +02:00
public void onReceive(Context context, Intent rIntent) {
2016-07-07 00:56:31 +02:00
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
log.debug("KeepAlive received");
2016-07-08 13:41:53 +02:00
DanaRFragment danaRFragment = (DanaRFragment) MainActivity.getSpecificPlugin(DanaRFragment.class);
if (Config.DANAR && danaRFragment != null && danaRFragment.isEnabled()) {
2016-07-07 10:34:20 +02:00
Intent intent = new Intent(context, DanaRService.class);
context.startService(intent);
}
2016-07-07 00:56:31 +02:00
wl.release();
}
public void setAlarm(Context context) {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, KeepAliveReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
try {
pi.send();
} catch (PendingIntent.CanceledException e) {
}
am.cancel(pi);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), Constants.keepAliveMsecs, pi);
}
public void cancelAlarm(Context context) {
Intent intent = new Intent(context, KeepAliveReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
}