Fix service manually restarting

And unregister NetworkCallback
This commit is contained in:
sim 2022-01-17 21:49:38 +01:00
parent 52a956464a
commit 702ce9864c
2 changed files with 37 additions and 21 deletions

View File

@ -1,6 +1,9 @@
package org.unifiedpush.distributor.nextpush.activities
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.util.Log
import android.view.Menu
@ -10,7 +13,6 @@ import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import com.nextcloud.android.sso.AccountImporter
import com.nextcloud.android.sso.ui.UiExceptionManager
import org.unifiedpush.distributor.nextpush.services.StartService
import com.nextcloud.android.sso.AccountImporter.IAccountAccessGranted
import com.nextcloud.android.sso.api.NextcloudAPI.ApiConnectedListener
@ -32,8 +34,7 @@ import org.unifiedpush.distributor.nextpush.api.apiDeleteApp
import org.unifiedpush.distributor.nextpush.api.apiDeleteDevice
import org.unifiedpush.distributor.nextpush.distributor.sendUnregistered
import org.unifiedpush.distributor.nextpush.distributor.getDb
import org.unifiedpush.distributor.nextpush.services.isServiceStarted
import org.unifiedpush.distributor.nextpush.services.startListener
import org.unifiedpush.distributor.nextpush.services.*
import java.lang.String.format
private const val TAG = "NextPush-MainActivity"
@ -145,6 +146,13 @@ class MainActivity : AppCompatActivity() {
private fun restart() {
Log.d(TAG, "Restarting the Listener")
val receiver = object : BroadcastReceiver() {
override fun onReceive(p0: Context?, p1: Intent?) {
startListener(this@MainActivity)
}
}
val intentFilter = IntentFilter(SERVICE_STOPPED_ACTION)
registerReceiver(receiver, intentFilter)
isServiceStarted = false
val serviceIntent = Intent(this, StartService::class.java)
this.stopService(serviceIntent)

View File

@ -26,6 +26,7 @@ import java.lang.Exception
private const val TAG = "StartService"
const val WAKE_LOCK_TAG = "NextPush:StartService:lock"
const val SERVICE_STOPPED_ACTION = "org.unifiedpush.distributor.nextpush.services.STOPPED"
var isServiceStarted = false
var nFails = 0
var wakeLock: PowerManager.WakeLock? = null
@ -78,14 +79,17 @@ class StartService: Service(){
private fun stopService() {
Log.d(TAG, "Stopping Service")
apiDestroy()
isServiceStarted = false
nFails = 0
apiDestroy()
connectivityManager?.unregisterNetworkCallback(networkCallback)
wakeLock?.let {
while (it.isHeld) {
it.release()
}
}
val i = Intent(SERVICE_STOPPED_ACTION)
sendBroadcast(i)
stopSelf()
}
@ -111,24 +115,28 @@ class StartService: Service(){
apiSync(this)
}
private fun registerNetworkCallback() {
try {
val connectivityManager =
this.getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
connectivityManager.registerDefaultNetworkCallback(object : NetworkCallback() {
override fun onAvailable(network: Network) {
Log.d(TAG, "Network is CONNECTED")
startListener(this@StartService)
}
private var connectivityManager = null as ConnectivityManager?
override fun onCapabilitiesChanged(
network: Network,
networkCapabilities: NetworkCapabilities
) {
Log.d(TAG, "Network Capabilities changed")
startListener(this@StartService)
}
})
private val networkCallback = object : NetworkCallback() {
override fun onAvailable(network: Network) {
Log.d(TAG, "Network is CONNECTED")
startListener(this@StartService)
}
override fun onCapabilitiesChanged(
network: Network,
networkCapabilities: NetworkCapabilities
) {
Log.d(TAG, "Network Capabilities changed")
startListener(this@StartService)
}
}
private fun registerNetworkCallback() {
Log.d(TAG, "Registering Network Callback")
try {
connectivityManager = this.getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
connectivityManager!!.registerDefaultNetworkCallback(networkCallback)
} catch (e: Exception) {
e.printStackTrace()
}