KeepAliveReceiver

This commit is contained in:
Milos Kozak 2016-07-07 00:56:31 +02:00
parent 725db13776
commit 47e130d246
4 changed files with 62 additions and 1 deletions

View file

@ -59,6 +59,9 @@
</intent-filter> </intent-filter>
</receiver> </receiver>
<!-- Receiver keepalive, scheduled every 30 min -->
<receiver android:name=".receivers.KeepAliveReceiver"></receiver>
<!-- Service processing incomming data --> <!-- Service processing incomming data -->
<service <service
android:name=".Services.DataService" android:name=".Services.DataService"

View file

@ -20,4 +20,6 @@ public class Constants {
public static final Integer notificationID = 556677; public static final Integer notificationID = 556677;
public static final int hoursToKeepInDatabase = 24; public static final int hoursToKeepInDatabase = 24;
public static final long keepAliveMsecs = 30 * 60 * 1000L;
} }

View file

@ -40,6 +40,7 @@ import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripFragment;
import info.nightscout.androidaps.plugins.TempBasals.TempBasalsFragment; import info.nightscout.androidaps.plugins.TempBasals.TempBasalsFragment;
import info.nightscout.androidaps.plugins.Treatments.TreatmentsFragment; import info.nightscout.androidaps.plugins.Treatments.TreatmentsFragment;
import info.nightscout.androidaps.plugins.VirtualPump.VirtualPumpFragment; import info.nightscout.androidaps.plugins.VirtualPump.VirtualPumpFragment;
import info.nightscout.androidaps.receivers.KeepAliveReceiver;
import info.nightscout.androidaps.tabs.*; import info.nightscout.androidaps.tabs.*;
import info.nightscout.androidaps.plugins.Objectives.ObjectivesFragment; import info.nightscout.androidaps.plugins.Objectives.ObjectivesFragment;
import info.nightscout.utils.ImportExportPrefs; import info.nightscout.utils.ImportExportPrefs;
@ -52,6 +53,7 @@ public class MainActivity extends AppCompatActivity {
private SlidingTabLayout mTabs; private SlidingTabLayout mTabs;
private ViewPager mPager; private ViewPager mPager;
private static TabPageAdapter pageAdapter; private static TabPageAdapter pageAdapter;
private static KeepAliveReceiver keepAliveReceiver;
private static ArrayList<PluginBase> pluginsList = null; private static ArrayList<PluginBase> pluginsList = null;
@ -94,6 +96,8 @@ public class MainActivity extends AppCompatActivity {
pluginsList.add(configBuilderFragment = ConfigBuilderFragment.newInstance()); pluginsList.add(configBuilderFragment = ConfigBuilderFragment.newInstance());
registerBus(); registerBus();
keepAliveReceiver = new KeepAliveReceiver();
keepAliveReceiver.setAlarm(this);
configBuilderFragment.initialize(); configBuilderFragment.initialize();
MainApp.setConfigBuilder(configBuilderFragment); MainApp.setConfigBuilder(configBuilderFragment);
@ -169,7 +173,7 @@ public class MainActivity extends AppCompatActivity {
break; break;
case R.id.nav_exit: case R.id.nav_exit:
log.debug("Exiting"); log.debug("Exiting");
//chancelAlarmManager(); keepAliveReceiver.cancelAlarm(this);
//MainApp.bus().post(new StopEvent()); //MainApp.bus().post(new StopEvent());
MainApp.closeDbHelper(); MainApp.closeDbHelper();

View file

@ -0,0 +1,52 @@
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;
import info.nightscout.androidaps.Constants;
public class KeepAliveReceiver extends BroadcastReceiver {
private static Logger log = LoggerFactory.getLogger(KeepAliveReceiver.class);
@Override
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
log.debug("KeepAlive received");
// TODO: Start service here
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);
}
}