Clean how startService is stopped
This commit is contained in:
parent
842e011b31
commit
2e6672db4f
@ -30,6 +30,7 @@ import org.unifiedpush.distributor.nextpush.account.AccountUtils.ssoAccount
|
||||
import org.unifiedpush.distributor.nextpush.distributor.Distributor.deleteApp
|
||||
import org.unifiedpush.distributor.nextpush.distributor.Distributor.deleteDevice
|
||||
import org.unifiedpush.distributor.nextpush.distributor.Distributor.getDb
|
||||
import org.unifiedpush.distributor.nextpush.services.FailureHandler
|
||||
import org.unifiedpush.distributor.nextpush.services.RestartWorker
|
||||
import org.unifiedpush.distributor.nextpush.services.StartService
|
||||
import org.unifiedpush.distributor.nextpush.utils.TAG
|
||||
@ -158,7 +159,8 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
private fun restart() {
|
||||
Log.d(TAG, "Restarting the Listener")
|
||||
StartService.service?.stopService {
|
||||
FailureHandler.clearFails()
|
||||
StartService.stopService {
|
||||
RestartWorker.start(this, delay = 0)
|
||||
}
|
||||
}
|
||||
|
@ -41,6 +41,7 @@ object Api {
|
||||
}
|
||||
|
||||
fun apiDestroy() {
|
||||
Log.d(TAG, "Destroying API")
|
||||
provider?.destroyProvider()
|
||||
provider = null
|
||||
source?.cancel()
|
||||
|
@ -10,7 +10,7 @@ import org.unifiedpush.distributor.nextpush.utils.TAG
|
||||
import java.lang.Exception
|
||||
|
||||
class RestartNetworkCallback(val context: Context) : ConnectivityManager.NetworkCallback() {
|
||||
var connectivityManager: ConnectivityManager? = null
|
||||
private var connectivityManager: ConnectivityManager? = null
|
||||
|
||||
override fun onAvailable(network: Network) {
|
||||
Log.d(TAG, "Network is CONNECTED")
|
||||
@ -32,8 +32,10 @@ class RestartNetworkCallback(val context: Context) : ConnectivityManager.Network
|
||||
}
|
||||
|
||||
fun register() {
|
||||
Log.d(TAG, "Registering Network Callback")
|
||||
connectivityManager ?: run {
|
||||
connectivityManager?.let {
|
||||
Log.d(TAG, "ConnectivityManager already registered")
|
||||
} ?: run {
|
||||
Log.d(TAG, "Registering new ConnectivityManager")
|
||||
try {
|
||||
connectivityManager = (
|
||||
context.getSystemService(Service.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||
@ -47,6 +49,7 @@ class RestartNetworkCallback(val context: Context) : ConnectivityManager.Network
|
||||
}
|
||||
|
||||
fun unregister() {
|
||||
Log.d(TAG, "Unregistering ConnectivityManager")
|
||||
connectivityManager?.unregisterNetworkCallback(this)
|
||||
connectivityManager = null
|
||||
}
|
||||
|
@ -10,7 +10,6 @@ import android.util.Log
|
||||
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException
|
||||
import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException
|
||||
import com.nextcloud.android.sso.helper.SingleAccountHelper
|
||||
import okhttp3.sse.EventSource
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountUtils.nextcloudAppNotInstalledDialog
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountUtils.ssoAccount
|
||||
import org.unifiedpush.distributor.nextpush.api.Api.apiDestroy
|
||||
@ -21,18 +20,20 @@ import org.unifiedpush.distributor.nextpush.utils.TAG
|
||||
|
||||
class StartService : Service() {
|
||||
|
||||
companion object {
|
||||
companion object StartServiceCompanion {
|
||||
private const val WAKE_LOCK_TAG = "NextPush:StartService:lock"
|
||||
|
||||
var service: StartService? = null
|
||||
private var service: StartService? = null
|
||||
var isServiceStarted = false
|
||||
private set
|
||||
var wakeLock: PowerManager.WakeLock? = null
|
||||
private set
|
||||
|
||||
fun startListener(context: Context) {
|
||||
if (isServiceStarted && !FailureHandler.hasFailed()) return
|
||||
Log.d(TAG, "Starting the Listener")
|
||||
Log.d(TAG, "Service is started: $isServiceStarted")
|
||||
Log.d(TAG, "nFails: $FailureHandler.nFails")
|
||||
Log.d(TAG, "nFails: ${FailureHandler.nFails}")
|
||||
val serviceIntent = Intent(context, StartService::class.java)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
context.startForegroundService(serviceIntent)
|
||||
@ -40,6 +41,13 @@ class StartService : Service() {
|
||||
context.startService(serviceIntent)
|
||||
}
|
||||
}
|
||||
|
||||
fun stopService(block: () -> Unit = {}) {
|
||||
Log.d(TAG, "Stopping Service")
|
||||
isServiceStarted = false
|
||||
service?.stopSelf()
|
||||
block()
|
||||
}
|
||||
}
|
||||
|
||||
private val networkCallback = RestartNetworkCallback(this)
|
||||
@ -65,32 +73,22 @@ class StartService : Service() {
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
Log.d(TAG, "Destroyed")
|
||||
if (isServiceStarted) {
|
||||
apiDestroy()
|
||||
Log.d(TAG, "onDestroy: restarting worker")
|
||||
RestartWorker.start(this, delay = 0)
|
||||
} else {
|
||||
stopService()
|
||||
}
|
||||
}
|
||||
|
||||
fun stopService(block: () -> Unit = {}) {
|
||||
Log.d(TAG, "Stopping Service")
|
||||
isServiceStarted = false
|
||||
FailureHandler.clearFails()
|
||||
Log.d(TAG, "Destroying Service")
|
||||
apiDestroy()
|
||||
networkCallback.unregister()
|
||||
wakeLock?.let {
|
||||
while (it.isHeld) {
|
||||
it.release()
|
||||
}
|
||||
}
|
||||
stopSelf()
|
||||
block()
|
||||
if (isServiceStarted) {
|
||||
Log.d(TAG, "onDestroy: restarting worker")
|
||||
RestartWorker.start(this, delay = 0)
|
||||
}
|
||||
}
|
||||
|
||||
private fun startService() {
|
||||
// If the service is running and we don't have any fail
|
||||
// In case somehow startService is called when everything is fine
|
||||
if (isServiceStarted && !FailureHandler.hasFailed()) return
|
||||
isServiceStarted = true
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user