From 0d5a56da1c45ba808560ba32b0e14b6b5e321cef Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Sat, 21 Jan 2023 17:44:06 +0530 Subject: [PATCH] Check if the connected network is metered in NetworkHelper. --- .../github/libretube/util/DataSaverMode.kt | 2 +- .../github/libretube/util/NetworkHelper.kt | 28 +++++-------------- .../com/github/libretube/util/PlayerHelper.kt | 22 ++++----------- 3 files changed, 14 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/com/github/libretube/util/DataSaverMode.kt b/app/src/main/java/com/github/libretube/util/DataSaverMode.kt index f7a084d7d..619ce58d2 100644 --- a/app/src/main/java/com/github/libretube/util/DataSaverMode.kt +++ b/app/src/main/java/com/github/libretube/util/DataSaverMode.kt @@ -9,7 +9,7 @@ object DataSaverMode { return when (pref) { "enabled" -> true "disabled" -> false - "metered" -> NetworkHelper.isNetworkMobile(context) + "metered" -> NetworkHelper.isNetworkMetered(context) else -> throw IllegalArgumentException() } } diff --git a/app/src/main/java/com/github/libretube/util/NetworkHelper.kt b/app/src/main/java/com/github/libretube/util/NetworkHelper.kt index ff25b608d..757fc584c 100644 --- a/app/src/main/java/com/github/libretube/util/NetworkHelper.kt +++ b/app/src/main/java/com/github/libretube/util/NetworkHelper.kt @@ -2,16 +2,14 @@ package com.github.libretube.util import android.content.Context import android.net.ConnectivityManager -import android.net.NetworkCapabilities -import android.os.Build +import androidx.core.content.getSystemService object NetworkHelper { /** * Detect whether network is available */ fun isNetworkAvailable(context: Context): Boolean { - val connectivityManager = - context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + val connectivityManager = context.getSystemService() // this seems to not recognize vpn connections /* @@ -37,27 +35,15 @@ object NetworkHelper { */ @Suppress("DEPRECATION") - return connectivityManager.activeNetworkInfo?.isConnected ?: false + return connectivityManager?.activeNetworkInfo?.isConnected ?: false } /** - * Detect whether the current network is mobile data + * Detect whether the current network is metered * @param context Context of the application - * @return isNetworkMobile + * @return whether the network is metered or not */ - @Suppress("DEPRECATION") - fun isNetworkMobile(context: Context): Boolean { - val connectivityManager = - context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - val networkCapabilities = connectivityManager.getNetworkCapabilities( - connectivityManager.activeNetwork ?: return false - ) - return networkCapabilities?.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) - ?: false - } else { - val activeNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) - return activeNetwork != null && activeNetwork.isConnected - } + fun isNetworkMetered(context: Context): Boolean { + return context.getSystemService()!!.isActiveNetworkMetered } } diff --git a/app/src/main/java/com/github/libretube/util/PlayerHelper.kt b/app/src/main/java/com/github/libretube/util/PlayerHelper.kt index 06a8395ac..91bf6b7b5 100644 --- a/app/src/main/java/com/github/libretube/util/PlayerHelper.kt +++ b/app/src/main/java/com/github/libretube/util/PlayerHelper.kt @@ -33,14 +33,9 @@ object PlayerHelper { /** * Get the audio source following the users preferences */ - fun getAudioSource( - context: Context, - audios: List - ): String { + fun getAudioSource(context: Context, audios: List): String { val audioFormat = PreferenceHelper.getString(PreferenceKeys.PLAYER_AUDIO_FORMAT, "all") - val audioPrefKey = if ( - NetworkHelper.isNetworkMobile(context) - ) { + val audioPrefKey = if (NetworkHelper.isNetworkMetered(context)) { PreferenceKeys.PLAYER_AUDIO_QUALITY_MOBILE } else { PreferenceKeys.PLAYER_AUDIO_QUALITY @@ -351,17 +346,12 @@ object PlayerHelper { ) fun getDefaultResolution(context: Context): String { - return if (NetworkHelper.isNetworkMobile(context)) { - PreferenceHelper.getString( - PreferenceKeys.DEFAULT_RESOLUTION_MOBILE, - "" - ) + val prefKey = if (NetworkHelper.isNetworkMetered(context)) { + PreferenceKeys.DEFAULT_RESOLUTION_MOBILE } else { - PreferenceHelper.getString( - PreferenceKeys.DEFAULT_RESOLUTION, - "" - ) + PreferenceKeys.DEFAULT_RESOLUTION } + return PreferenceHelper.getString(prefKey, "") } fun getIntentActon(context: Context): String {