Skip to content

Commit

Permalink
Stop node on low battery (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
desyncr authored Mar 7, 2021
1 parent 990fdea commit 1c18927
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.freenetproject.mobile.receivers;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import org.freenetproject.mobile.services.node.Manager;

public class BatteryLevelReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Log.i("Freenet", "Stopping service from low battery level");
Manager.getInstance().stopService(context);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import androidx.preference.PreferenceManager;

import org.freenetproject.mobile.proxy.Simple;
import org.freenetproject.mobile.receivers.BatteryLevelReceiver;
import org.freenetproject.mobile.receivers.PowerConnectionReceiver;
import org.freenetproject.mobile.ui.notification.Notification;

Expand All @@ -29,12 +30,16 @@ public int onStartCommand(Intent intent, int flags, int startId) {

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
Boolean preserveBattery = sharedPreferences.getBoolean("preserve_battery", true);

// Register power connection receiver
if (preserveBattery) {
IntentFilter powerConnectedFilter = new IntentFilter();
powerConnectedFilter.addAction(Intent.ACTION_POWER_CONNECTED);
powerConnectedFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
registerReceiver(powerConnectionReceiver, powerConnectedFilter);

registerReceiver(new BatteryLevelReceiver(), new IntentFilter(
Intent.ACTION_BATTERY_LOW));
}

Boolean preserveData = sharedPreferences.getBoolean("preserve_data",true);
Expand Down

0 comments on commit 1c18927

Please sign in to comment.