mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 14:20:30 +05:30
Merge pull request #2868 from Isira-Seneviratne/Check_network_metered
Check if the connected network is metered in NetworkHelper.
This commit is contained in:
commit
2e0c121f58
@ -9,7 +9,7 @@ object DataSaverMode {
|
||||
return when (pref) {
|
||||
"enabled" -> true
|
||||
"disabled" -> false
|
||||
"metered" -> NetworkHelper.isNetworkMobile(context)
|
||||
"metered" -> NetworkHelper.isNetworkMetered(context)
|
||||
else -> throw IllegalArgumentException()
|
||||
}
|
||||
}
|
||||
|
@ -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<ConnectivityManager>()
|
||||
|
||||
// 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<ConnectivityManager>()!!.isActiveNetworkMetered
|
||||
}
|
||||
}
|
||||
|
@ -33,14 +33,9 @@ object PlayerHelper {
|
||||
/**
|
||||
* Get the audio source following the users preferences
|
||||
*/
|
||||
fun getAudioSource(
|
||||
context: Context,
|
||||
audios: List<PipedStream>
|
||||
): String {
|
||||
fun getAudioSource(context: Context, audios: List<PipedStream>): 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 {
|
||||
|
Loading…
Reference in New Issue
Block a user