Advertisement
Vassa007

NetworkConnection

Jun 22nd, 2024
840
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.85 KB | None | 0 0
  1. class NetworkConnection(private val context: Context) : LiveData<Boolean>() {
  2.  
  3.     private val connectionManager: ConnectivityManager =
  4.         context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
  5.  
  6.     private var networkCallback: ConnectivityManager.NetworkCallback? = null
  7.  
  8.     override fun onActive() {
  9.         super.onActive()
  10.         updateConnection()
  11.         when {
  12.             Build.VERSION.SDK_INT >= Build.VERSION_CODES.N -> {
  13.                 connectionManager.registerDefaultNetworkCallback(connectivityManagerCallback())
  14.             }
  15.  
  16.             Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP -> {
  17.                 lollipopNetwork()
  18.             }
  19.  
  20.             else -> context.registerReceiver(
  21.                 networkReceiver,
  22.                 IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)
  23.             )
  24.         }
  25.     }
  26.  
  27.     override fun onInactive() {
  28.         super.onInactive()
  29.         if  (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  30.             connectionManager.unregisterNetworkCallback(connectivityManagerCallback())
  31.         } else {
  32.             context.unregisterReceiver(networkReceiver)
  33.         }
  34.     }
  35.  
  36.     @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  37.     private fun lollipopNetwork() {
  38.         val requestBuilder = NetworkRequest.Builder()
  39.             .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
  40.             .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
  41.             .addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET)
  42.         connectionManager.registerNetworkCallback(
  43.             requestBuilder.build(),
  44.             connectivityManagerCallback()
  45.         )
  46.     }
  47.  
  48.     private fun connectivityManagerCallback() : ConnectivityManager.NetworkCallback {
  49.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  50.             if (networkCallback == null) {
  51.                 networkCallback = object : ConnectivityManager.NetworkCallback() {
  52.                     override fun onLost(network: Network) {
  53.                         super.onLost(network)
  54.                         postValue(false)
  55.                     }
  56.  
  57.                     override fun onAvailable(network: Network) {
  58.                         super.onAvailable(network)
  59.                         postValue(true)
  60.                     }
  61.                 }
  62.             }
  63.  
  64.             return networkCallback as ConnectivityManager.NetworkCallback
  65.         } else {
  66.             throw IllegalAccessError("Error")
  67.         }
  68.     }
  69.  
  70.     private val networkReceiver = object : BroadcastReceiver() {
  71.         override fun onReceive(context: Context?, intent: Intent?) {
  72.             updateConnection()
  73.         }
  74.  
  75.     }
  76.  
  77.     private fun updateConnection() {
  78.         val activeNetwork: NetworkInfo? = connectionManager.activeNetworkInfo
  79.         postValue(activeNetwork?.isConnected == true)
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement