aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2017-07-07 11:46:53 -0700
committerAllan Wang <me@allanwang.ca>2017-07-07 11:46:53 -0700
commit70d6719ca3798b0be30a860683e4bf2c5813d37a (patch)
tree3d62cc222fd1785261b1f7151812d539108bbb9c
parenta8f0c6ddba0e002032d6fa78a2f4635c44db6bbc (diff)
downloadkau-70d6719ca3798b0be30a860683e4bf2c5813d37a.tar.gz
kau-70d6719ca3798b0be30a860683e4bf2c5813d37a.tar.bz2
kau-70d6719ca3798b0be30a860683e4bf2c5813d37a.zip
Add network utils
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt7
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/NetworkUtils.kt28
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