From 34e2e0673b77719652c46f4a5ac478ca6c86e311 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Sat, 16 Sep 2017 06:57:59 +0200 Subject: [PATCH] Fixed the timeout issue with the JobScheduler --- .../dissem/apps/abit/service/BitmessageService.kt | 14 ++++++++++++++ .../apps/abit/service/StartupNodeOnWifiService.kt | 3 ++- 2 files changed, 16 insertions(+), 1 deletion(-) 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