Skip to content

Instantly share code, notes, and snippets.

@iamriajul
Created April 26, 2021 05:19
Show Gist options
  • Save iamriajul/5d0618f4767f2d6ec9395f2cf637845d to your computer and use it in GitHub Desktop.
Save iamriajul/5d0618f4767f2d6ec9395f2cf637845d to your computer and use it in GitHub Desktop.
Android Kotlin utility class for checking device's network connectivity and speed and internet availability. - You may use this with DI system easily.
package org.dailyislam.android.utilities
import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.telephony.TelephonyManager
import java.net.InetAddress
class ConnectivityUtil(private val applicationContext: Context) {
/**
* Get the network info
*/
fun getNetworkInfo(): NetworkInfo {
val cm = applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
return cm.activeNetworkInfo
}
/**
* Check if there is any connectivity
*/
fun isConnected(): Boolean {
val info = getNetworkInfo()
return info != null && info.isConnected
}
/**
* Check if there is any connectivity to a Wifi network
*/
fun isConnectedWifi(): Boolean {
val info = getNetworkInfo()
return info != null && info.isConnected && info.type == ConnectivityManager.TYPE_WIFI
}
/**
* Check if there is any connectivity to a mobile network
* @return
*/
fun isConnectedMobile(): Boolean {
val info = getNetworkInfo()
return info != null && info.isConnected && info.type == ConnectivityManager.TYPE_MOBILE
}
/**
* Check if there is fast connectivity
*/
fun isConnectedFast(): Boolean {
val info = getNetworkInfo()
return info != null && info.isConnected && isConnectionFast(info.type, info.subtype)
}
/**
* Check if the connection is fast
* @param type
* @param subType
* @return
*/
fun isConnectionFast(type: Int, subType: Int): Boolean {
return if (type == ConnectivityManager.TYPE_WIFI) {
true
} else if (type == ConnectivityManager.TYPE_MOBILE) {
when (subType) {
TelephonyManager.NETWORK_TYPE_1xRTT -> false // ~ 50-100 kbps
TelephonyManager.NETWORK_TYPE_CDMA -> false // ~ 14-64 kbps
TelephonyManager.NETWORK_TYPE_EDGE -> false // ~ 50-100 kbps
TelephonyManager.NETWORK_TYPE_EVDO_0 -> true // ~ 400-1000 kbps
TelephonyManager.NETWORK_TYPE_EVDO_A -> true // ~ 600-1400 kbps
TelephonyManager.NETWORK_TYPE_GPRS -> false // ~ 100 kbps
TelephonyManager.NETWORK_TYPE_HSDPA -> true // ~ 2-14 Mbps
TelephonyManager.NETWORK_TYPE_HSPA -> true // ~ 700-1700 kbps
TelephonyManager.NETWORK_TYPE_HSUPA -> true // ~ 1-23 Mbps
TelephonyManager.NETWORK_TYPE_UMTS -> true // ~ 400-7000 kbps
TelephonyManager.NETWORK_TYPE_EHRPD -> true // ~ 1-2 Mbps
TelephonyManager.NETWORK_TYPE_EVDO_B -> true // ~ 5 Mbps
TelephonyManager.NETWORK_TYPE_HSPAP -> true // ~ 10-20 Mbps
TelephonyManager.NETWORK_TYPE_IDEN -> false // ~25 kbps
TelephonyManager.NETWORK_TYPE_LTE -> true // ~ 10+ Mbps
TelephonyManager.NETWORK_TYPE_UNKNOWN -> false
else -> false
}
} else {
false
}
}
fun isInternetAvailable(): Boolean {
return try {
val address : InetAddress = InetAddress.getByName("google.com")
//You can replace it with your name
!address.equals("")
} catch (e: Exception) {
false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment