diff options
-rw-r--r-- | core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt | 7 | ||||
-rw-r--r-- | core/src/main/kotlin/ca/allanwang/kau/utils/NetworkUtils.kt | 28 |
2 files changed, 28 insertions, 7 deletions
diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt index bc01919..ca3e90b 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt @@ -140,13 +140,6 @@ inline fun Context.materialDialog(action: MaterialDialog.Builder.() -> Unit): Ma return builder.show() } -inline val Context.isNetworkAvailable: Boolean - get() { - val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager - val activeNetworkInfo = connectivityManager.activeNetworkInfo - return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting - } - fun Context.getDip(value: Float): Float = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, resources.displayMetrics) inline val Context.isRtl: Boolean diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/NetworkUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/NetworkUtils.kt new file mode 100644 index 0000000..53016be --- /dev/null +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/NetworkUtils.kt @@ -0,0 +1,28 @@ +package ca.allanwang.kau.utils + +import android.content.Context +import android.net.ConnectivityManager + +/** + * Created by Allan Wang on 2017-07-07. + */ +inline val Context.isNetworkAvailable: Boolean + get() { + val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + val activeNetworkInfo = connectivityManager.activeNetworkInfo + return activeNetworkInfo?.isConnectedOrConnecting ?: false + } + +inline val Context.isWifiConnected: Boolean + get() { + val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + val activeNetworkInfo = connectivityManager.activeNetworkInfo + return (activeNetworkInfo?.type ?: -1) == ConnectivityManager.TYPE_WIFI + } + +inline val Context.isMobileDataConnected: Boolean + get() { + val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + val activeNetworkInfo = connectivityManager.activeNetworkInfo + return (activeNetworkInfo?.type ?: -1) == ConnectivityManager.TYPE_MOBILE + }
\ No newline at end of file |