From c6b58fbcd843de1453da95a1c6dd61a09768538d Mon Sep 17 00:00:00 2001 From: Tonyo Francis Date: Mon, 17 Aug 2020 12:46:43 -0400 Subject: [PATCH] Added un metered network connection --- .../java/com/tonyodev/fetch2/NetworkType.kt | 6 ++++- .../fetch2/provider/NetworkInfoProvider.kt | 4 ++++ .../tonyodev/fetch2core/AndroidExtentions.kt | 23 +++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/fetch2/src/main/java/com/tonyodev/fetch2/NetworkType.kt b/fetch2/src/main/java/com/tonyodev/fetch2/NetworkType.kt index df20567e..399305da 100644 --- a/fetch2/src/main/java/com/tonyodev/fetch2/NetworkType.kt +++ b/fetch2/src/main/java/com/tonyodev/fetch2/NetworkType.kt @@ -15,7 +15,10 @@ enum class NetworkType(val value: Int) { ALL(0), /** Indicates that a download can be downloaded only on wifi networks.*/ - WIFI_ONLY(1); + WIFI_ONLY(1), + + /** Indicates that a download can be downloaded only on an unmetered connection.*/ + UNMETERED(2); companion object { @@ -25,6 +28,7 @@ enum class NetworkType(val value: Int) { -1 -> GLOBAL_OFF 0 -> ALL 1 -> WIFI_ONLY + 2 -> UNMETERED else -> ALL } } diff --git a/fetch2/src/main/java/com/tonyodev/fetch2/provider/NetworkInfoProvider.kt b/fetch2/src/main/java/com/tonyodev/fetch2/provider/NetworkInfoProvider.kt index 4f87531a..3b08186d 100644 --- a/fetch2/src/main/java/com/tonyodev/fetch2/provider/NetworkInfoProvider.kt +++ b/fetch2/src/main/java/com/tonyodev/fetch2/provider/NetworkInfoProvider.kt @@ -11,6 +11,7 @@ import android.net.NetworkRequest import android.os.Build import com.tonyodev.fetch2.NetworkType import com.tonyodev.fetch2core.isNetworkAvailable +import com.tonyodev.fetch2core.isOnMeteredConnection import com.tonyodev.fetch2core.isOnWiFi import java.net.HttpURLConnection import java.net.URL @@ -102,6 +103,9 @@ class NetworkInfoProvider constructor(private val context: Context, if (networkType == NetworkType.WIFI_ONLY && context.isOnWiFi()) { return true } + if (networkType == NetworkType.UNMETERED && !context.isOnMeteredConnection()) { + return true + } if (networkType == NetworkType.ALL && context.isNetworkAvailable()) { return true } diff --git a/fetch2core/src/main/java/com/tonyodev/fetch2core/AndroidExtentions.kt b/fetch2core/src/main/java/com/tonyodev/fetch2core/AndroidExtentions.kt index 71dbfda5..b8c54438 100644 --- a/fetch2core/src/main/java/com/tonyodev/fetch2core/AndroidExtentions.kt +++ b/fetch2core/src/main/java/com/tonyodev/fetch2core/AndroidExtentions.kt @@ -4,6 +4,8 @@ package com.tonyodev.fetch2core import android.content.Context import android.net.ConnectivityManager +import android.net.NetworkInfo +import android.os.Build fun Context.isOnWiFi(): Boolean { @@ -16,6 +18,27 @@ fun Context.isOnWiFi(): Boolean { } } +fun Context.isOnMeteredConnection(): Boolean { + val cm = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + return if (Build.VERSION.SDK_INT >= 16) { + cm.isActiveNetworkMetered + } else { + val info: NetworkInfo = cm.activeNetworkInfo ?: return true + when (info.type) { + ConnectivityManager.TYPE_MOBILE, + ConnectivityManager.TYPE_MOBILE_DUN, + ConnectivityManager.TYPE_MOBILE_HIPRI, + ConnectivityManager.TYPE_MOBILE_MMS, + ConnectivityManager.TYPE_MOBILE_SUPL, + ConnectivityManager.TYPE_WIMAX -> true + ConnectivityManager.TYPE_WIFI, + ConnectivityManager.TYPE_BLUETOOTH, + ConnectivityManager.TYPE_ETHERNET -> false + else -> true + } + } +} + fun Context.isNetworkAvailable(): Boolean { val manager = this.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager val activeNetworkInfo = manager.activeNetworkInfo