diff --git a/app/src/main/java/ch/dissem/apps/abit/service/BitmessageService.kt b/app/src/main/java/ch/dissem/apps/abit/service/BitmessageService.kt index 78850eb..631d3a3 100644 --- a/app/src/main/java/ch/dissem/apps/abit/service/BitmessageService.kt +++ b/app/src/main/java/ch/dissem/apps/abit/service/BitmessageService.kt @@ -17,12 +17,17 @@ package ch.dissem.apps.abit.service import android.app.Service +import android.content.BroadcastReceiver +import android.content.Context import android.content.Intent +import android.content.IntentFilter +import android.net.ConnectivityManager import android.os.Handler import ch.dissem.apps.abit.notification.NetworkNotification import ch.dissem.apps.abit.notification.NetworkNotification.Companion.NETWORK_NOTIFICATION_ID import ch.dissem.bitmessage.BitmessageContext import ch.dissem.bitmessage.utils.Property +import org.jetbrains.anko.connectivityManager /** * Define a Service that returns an IBinder for the @@ -34,6 +39,14 @@ class BitmessageService : Service() { private val bmc: BitmessageContext by lazy { Singleton.getBitmessageContext(this) } private lateinit var notification: NetworkNotification + private val connectivityReceiver: BroadcastReceiver = object: BroadcastReceiver() { + override fun onReceive(context: Context?, intent: Intent?) { + if (bmc.isRunning() && connectivityManager.isActiveNetworkMetered){ + bmc.shutdown() + } + } + } + private val cleanupHandler = Handler() private val cleanupTask: Runnable = object : Runnable { override fun run() { @@ -45,6 +58,7 @@ class BitmessageService : Service() { } override fun onCreate() { + registerReceiver(connectivityReceiver, IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)) notification = NetworkNotification(this) running = false } diff --git a/app/src/main/java/ch/dissem/apps/abit/service/StartupNodeOnWifiService.kt b/app/src/main/java/ch/dissem/apps/abit/service/StartupNodeOnWifiService.kt index 535bc3b..ab52baa 100644 --- a/app/src/main/java/ch/dissem/apps/abit/service/StartupNodeOnWifiService.kt +++ b/app/src/main/java/ch/dissem/apps/abit/service/StartupNodeOnWifiService.kt @@ -26,7 +26,8 @@ class StartupNodeOnWifiService : JobService() { override fun onStopJob(params: JobParameters?): Boolean { if (Preferences.isWifiOnly(this)) { - Singleton.getBitmessageContext(this).shutdown() + // Don't actually stop the service, otherwise it will be stopped after 1 or 10 minutes + // depending on Android version. return Preferences.isFullNodeActive(this) } else { return false