@@ -5,27 +5,68 @@ import android.content.Context
5
5
import android.content.Intent
6
6
import android.content.IntentFilter
7
7
import android.net.ConnectivityManager
8
+ import android.net.ConnectivityManager.NetworkCallback
9
+ import android.net.Network
10
+ import android.net.NetworkCapabilities
11
+ import android.os.Build
8
12
import kotlinx.coroutines.flow.MutableStateFlow
9
13
import kotlinx.coroutines.flow.StateFlow
10
- import kotlinx.coroutines.flow.asStateFlow
11
14
12
- class NetworkConnectionStateHolder (context : Context ) {
15
+ class NetworkConnectionStateHolder (private val context : Context ) {
13
16
14
- private val _networkState = MutableStateFlow <NetworkState >(NetworkState .UnInitialized )
15
- val networkState : StateFlow <NetworkState > get() = _networkState .asStateFlow()
17
+ private val _networkConnectionState = MutableStateFlow <NetworkState >(NetworkState .UnInitialized )
18
+ val networkConnectionState : StateFlow <NetworkState > = _networkConnectionState
16
19
17
- private val networkReceiver = object : BroadcastReceiver () {
18
- override fun onReceive (context : Context , intent : Intent ) {
19
- _networkState .value =
20
- if (context.isConnected) NetworkState .Connection else NetworkState .DisConnection
20
+ private val connectivityManager =
21
+ context.getSystemService(Context .CONNECTIVITY_SERVICE ) as ConnectivityManager
22
+ private val connectivityCallback = object : NetworkCallback () {
23
+ override fun onAvailable (network : Network ) {
24
+ emitNetworkConnectionState(NetworkState .Connection )
25
+ }
26
+
27
+ override fun onLost (network : Network ) {
28
+ emitNetworkConnectionState(NetworkState .DisConnection )
29
+ }
30
+ }
31
+ private val connectivityReceiver = object : BroadcastReceiver () {
32
+ override fun onReceive (context : Context ? , intent : Intent ? ) {
33
+ if (context != null && context.isConnected) {
34
+ emitNetworkConnectionState(NetworkState .Connection )
35
+ } else {
36
+ emitNetworkConnectionState(NetworkState .DisConnection )
37
+ }
38
+ }
39
+ }
40
+
41
+ fun startObservingState () {
42
+ val networkCapabilities =
43
+ connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
44
+ if (networkCapabilities?.hasCapability(NetworkCapabilities .NET_CAPABILITY_INTERNET ) == true ) {
45
+ emitNetworkConnectionState(NetworkState .Connection )
46
+ } else {
47
+ emitNetworkConnectionState(NetworkState .DisConnection )
48
+ }
49
+
50
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .N ) {
51
+ connectivityManager.registerDefaultNetworkCallback(connectivityCallback)
52
+ } else {
53
+ context.registerReceiver(
54
+ connectivityReceiver,
55
+ IntentFilter (ConnectivityManager .CONNECTIVITY_ACTION )
56
+ )
57
+ }
58
+ }
59
+
60
+ fun finishObservingState () {
61
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .N ) {
62
+ connectivityManager.unregisterNetworkCallback(connectivityCallback)
63
+ } else {
64
+ context.unregisterReceiver(connectivityReceiver)
21
65
}
22
66
}
23
67
24
- init {
25
- context.registerReceiver(
26
- networkReceiver,
27
- IntentFilter (ConnectivityManager .CONNECTIVITY_ACTION )
28
- )
68
+ private fun emitNetworkConnectionState (currentState : NetworkState ) {
69
+ _networkConnectionState .value = currentState
29
70
}
30
71
}
31
72
0 commit comments