taler-android

Android apps for GNU Taler (wallet, PoS, cashier)
Log | Files | Refs | README | LICENSE

NetworkManager.kt (2535B)


      1 /*
      2  * This file is part of GNU Taler
      3  * (C) 2023 Taler Systems S.A.
      4  *
      5  * GNU Taler is free software; you can redistribute it and/or modify it under the
      6  * terms of the GNU General Public License as published by the Free Software
      7  * Foundation; either version 3, or (at your option) any later version.
      8  *
      9  * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
     10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11  * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License along with
     14  * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15  */
     16 
     17 package net.taler.wallet
     18 
     19 import android.content.Context
     20 import android.content.Context.CONNECTIVITY_SERVICE
     21 import android.net.ConnectivityManager
     22 import android.net.Network
     23 import android.net.NetworkCapabilities
     24 import android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET
     25 import android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED
     26 import androidx.annotation.UiThread
     27 import androidx.lifecycle.LiveData
     28 import androidx.lifecycle.MutableLiveData
     29 
     30 class NetworkManager(context: Context) : ConnectivityManager.NetworkCallback() {
     31     private val connectivityManager: ConnectivityManager
     32 
     33     private val _networkStatus: MutableLiveData<Boolean>
     34     val networkStatus: LiveData<Boolean>
     35 
     36     init {
     37         // careful, the order below is important, should probably get simplified
     38         connectivityManager = context.getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
     39         _networkStatus = MutableLiveData(getCurrentStatus())
     40         networkStatus = _networkStatus
     41         connectivityManager.registerDefaultNetworkCallback(this)
     42     }
     43 
     44     @UiThread
     45     override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
     46         super.onCapabilitiesChanged(network, networkCapabilities)
     47         _networkStatus.postValue(networkCapabilities.isOnline())
     48     }
     49 
     50     override fun onLost(network: Network) {
     51         super.onLost(network)
     52         _networkStatus.postValue(getCurrentStatus())
     53     }
     54 
     55     private fun getCurrentStatus(): Boolean {
     56         return connectivityManager.activeNetwork?.let { network ->
     57             connectivityManager.getNetworkCapabilities(network)?.isOnline()
     58         } ?: false
     59     }
     60 
     61     private fun NetworkCapabilities.isOnline(): Boolean {
     62         return hasCapability(NET_CAPABILITY_INTERNET) && hasCapability(NET_CAPABILITY_VALIDATED)
     63     }
     64 }