diff --git a/common/src/main/kotlin/com/alfresco/content/network/ConnectivityTracker.kt b/common/src/main/kotlin/com/alfresco/content/network/ConnectivityTracker.kt index 10d458845..c9c8beba9 100644 --- a/common/src/main/kotlin/com/alfresco/content/network/ConnectivityTracker.kt +++ b/common/src/main/kotlin/com/alfresco/content/network/ConnectivityTracker.kt @@ -3,6 +3,7 @@ package com.alfresco.content.network import android.content.Context import android.net.ConnectivityManager import android.net.Network +import android.net.NetworkCapabilities import androidx.annotation.RequiresApi import androidx.core.content.getSystemService import kotlinx.coroutines.flow.MutableStateFlow @@ -31,6 +32,34 @@ object ConnectivityTracker { isTracking = true } + /** + * returns true if network is metered otherwise false + */ fun isActiveNetworkMetered(context: Context): Boolean = context.getSystemService()?.isActiveNetworkMetered == true + + /** + * returns true if network is active otherwise false + */ + fun isActiveNetwork(context: Context): Boolean { + + val connectivityManager = context.getSystemService() ?: return false + + val network = context.getSystemService()?.activeNetwork ?: return false + + val activeNetwork = connectivityManager.getNetworkCapabilities(network) ?: return false + + return when { + // Indicates this network uses a Wi-Fi transport, + // or WiFi has network connectivity + activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true + + // Indicates this network uses a Cellular transport. or + // Cellular has network connectivity + activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true + + // else return false + else -> false + } + } } diff --git a/search/src/main/kotlin/com/alfresco/content/search/SearchViewModel.kt b/search/src/main/kotlin/com/alfresco/content/search/SearchViewModel.kt index cb0e0c999..5035c3b1f 100644 --- a/search/src/main/kotlin/com/alfresco/content/search/SearchViewModel.kt +++ b/search/src/main/kotlin/com/alfresco/content/search/SearchViewModel.kt @@ -18,7 +18,6 @@ import com.alfresco.content.data.SearchFacetQueries import com.alfresco.content.data.SearchFilter import com.alfresco.content.data.SearchFilters import com.alfresco.content.data.SearchRepository -import com.alfresco.content.data.Settings import com.alfresco.content.data.emptyAdvanceFilters import com.alfresco.content.data.emptySearchFacetFields import com.alfresco.content.data.emptySearchFacetIntervals @@ -425,9 +424,7 @@ class SearchViewModel( /** * returns true if device has active network connection otherwise false */ - fun canSearchOverCurrentNetwork() = - Settings(context).canSyncOverMeteredNetwork || - !ConnectivityTracker.isActiveNetworkMetered(context) + fun canSearchOverCurrentNetwork() = ConnectivityTracker.isActiveNetwork(context) override fun emptyMessageArgs(state: ListViewState) = Triple(R.drawable.ic_empty_search, R.string.search_empty_title, R.string.search_empty_message)