- Use ConnectivityManager NetworkCallbacks to detect when network
connectivity becomes available or is lost
This commit is contained in:
@@ -24,6 +24,11 @@ import java.io.File
|
|||||||
import java.nio.charset.StandardCharsets
|
import java.nio.charset.StandardCharsets
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
import android.net.Network
|
||||||
|
import android.net.NetworkRequest
|
||||||
|
import android.content.Context.CONNECTIVITY_SERVICE
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class BaresipService: Service() {
|
class BaresipService: Service() {
|
||||||
|
|
||||||
@@ -33,7 +38,6 @@ class BaresipService: Service() {
|
|||||||
internal lateinit var rt: Ringtone
|
internal lateinit var rt: Ringtone
|
||||||
internal lateinit var nm: NotificationManager
|
internal lateinit var nm: NotificationManager
|
||||||
internal lateinit var snb: NotificationCompat.Builder
|
internal lateinit var snb: NotificationCompat.Builder
|
||||||
internal lateinit var nr: BroadcastReceiver
|
|
||||||
internal lateinit var wl: PowerManager.WakeLock
|
internal lateinit var wl: PowerManager.WakeLock
|
||||||
internal lateinit var fl: WifiManager.WifiLock
|
internal lateinit var fl: WifiManager.WifiLock
|
||||||
|
|
||||||
@@ -60,25 +64,26 @@ class BaresipService: Service() {
|
|||||||
createNotificationChannels()
|
createNotificationChannels()
|
||||||
snb = NotificationCompat.Builder(this, DEFAULT_CHANNEL_ID)
|
snb = NotificationCompat.Builder(this, DEFAULT_CHANNEL_ID)
|
||||||
|
|
||||||
nr = object : BroadcastReceiver() {
|
val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||||
override fun onReceive(context: Context, intent: Intent) {
|
val builder = NetworkRequest.Builder()
|
||||||
if (intent.action == ConnectivityManager.CONNECTIVITY_ACTION) {
|
connectivityManager.registerNetworkCallback(
|
||||||
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
builder.build(),
|
||||||
val activeNetwork: NetworkInfo? = cm.activeNetworkInfo
|
object : ConnectivityManager.NetworkCallback() {
|
||||||
val isConnected: Boolean = activeNetwork?.isConnectedOrConnecting == true
|
override fun onAvailable(network: Network) {
|
||||||
if (isConnected) {
|
super.onAvailable(network)
|
||||||
Log.d(LOG_TAG, "Network is connected/connecting")
|
Log.d(LOG_TAG, "Network $network is available")
|
||||||
if (disconnected) {
|
if (disconnected) {
|
||||||
UserAgent.register()
|
UserAgent.register()
|
||||||
disconnected = false
|
disconnected = false
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
Log.d(LOG_TAG, "Network is NOT connected/connecting")
|
override fun onLost(network: Network) {
|
||||||
|
super.onLost(network)
|
||||||
|
Log.d(LOG_TAG, "Network $network is lost")
|
||||||
disconnected = true
|
disconnected = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
val pm = getSystemService(Context.POWER_SERVICE) as PowerManager
|
val pm = getSystemService(Context.POWER_SERVICE) as PowerManager
|
||||||
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "com.tutpro.baresip:wakelog")
|
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "com.tutpro.baresip:wakelog")
|
||||||
@@ -159,7 +164,6 @@ class BaresipService: Service() {
|
|||||||
wl.acquire()
|
wl.acquire()
|
||||||
Thread(Runnable { baresipStart(filesPath) }).start()
|
Thread(Runnable { baresipStart(filesPath) }).start()
|
||||||
BaresipService.isServiceRunning = true
|
BaresipService.isServiceRunning = true
|
||||||
registerReceiver(nr, IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"))
|
|
||||||
showStatusNotification()
|
showStatusNotification()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -737,11 +741,6 @@ class BaresipService: Service() {
|
|||||||
status.clear()
|
status.clear()
|
||||||
history.clear()
|
history.clear()
|
||||||
messages.clear()
|
messages.clear()
|
||||||
try {
|
|
||||||
unregisterReceiver(nr)
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Log.d(LOG_TAG, "Receiver nr has not been registered")
|
|
||||||
}
|
|
||||||
if (this::nm.isInitialized) nm.cancelAll()
|
if (this::nm.isInitialized) nm.cancelAll()
|
||||||
if (this::wl.isInitialized && wl.isHeld) wl.release()
|
if (this::wl.isInitialized && wl.isHeld) wl.release()
|
||||||
if (this::fl.isInitialized && fl.isHeld) fl.release()
|
if (this::fl.isInitialized && fl.isHeld) fl.release()
|
||||||
|
|||||||
Reference in New Issue
Block a user