Commit 9664ab31 authored by Aayush Gupta's avatar Aayush Gupta
Browse files

utlis: Common: Switch to NetworkCapabilities to check for internet connection



- Current method has been deprecated

w: /builds/e/apps/apps/app/src/main/java/foundation/e/apps/utils/Common.kt: (47, 53): 'getter for activeNetworkInfo: NetworkInfo!' is deprecated. Deprecated in Java
w: /builds/e/apps/apps/app/src/main/java/foundation/e/apps/utils/Common.kt: (48, 63): 'getter for isConnected: Boolean' is deprecated. Deprecated in Java
Signed-off-by: Aayush Gupta's avatarAayush Gupta <theimpulson@e.email>
parent 5829ff58
Pipeline #109466 passed with stage
in 4 minutes and 19 seconds
...@@ -21,6 +21,7 @@ import android.content.Context ...@@ -21,6 +21,7 @@ import android.content.Context
import android.content.pm.ApplicationInfo import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.net.ConnectivityManager import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Build import android.os.Build
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.core.os.LocaleListCompat import androidx.core.os.LocaleListCompat
...@@ -42,10 +43,22 @@ object Common { ...@@ -42,10 +43,22 @@ object Common {
val EXECUTOR = Executors.newCachedThreadPool()!! val EXECUTOR = Executors.newCachedThreadPool()!!
/*
* Checks if device has internet connection available or not
* @param context current Context
* @return true if internet connection is available, false otherwise
*/
fun isNetworkAvailable(context: Context): Boolean { fun isNetworkAvailable(context: Context): Boolean {
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetworkInfo = connectivityManager.activeNetworkInfo val capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
return activeNetworkInfo != null && activeNetworkInfo.isConnected
if (capabilities != null) {
if (capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
&& capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) {
return true
}
}
return false
} }
fun toMiB(bytes: Int): Double { fun toMiB(bytes: Int): Double {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment