mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 22:30: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) {
|
return when (pref) {
|
||||||
"enabled" -> true
|
"enabled" -> true
|
||||||
"disabled" -> false
|
"disabled" -> false
|
||||||
"metered" -> NetworkHelper.isNetworkMobile(context)
|
"metered" -> NetworkHelper.isNetworkMetered(context)
|
||||||
else -> throw IllegalArgumentException()
|
else -> throw IllegalArgumentException()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,16 +2,14 @@ package com.github.libretube.util
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.net.ConnectivityManager
|
import android.net.ConnectivityManager
|
||||||
import android.net.NetworkCapabilities
|
import androidx.core.content.getSystemService
|
||||||
import android.os.Build
|
|
||||||
|
|
||||||
object NetworkHelper {
|
object NetworkHelper {
|
||||||
/**
|
/**
|
||||||
* Detect whether network is available
|
* Detect whether network is available
|
||||||
*/
|
*/
|
||||||
fun isNetworkAvailable(context: Context): Boolean {
|
fun isNetworkAvailable(context: Context): Boolean {
|
||||||
val connectivityManager =
|
val connectivityManager = context.getSystemService<ConnectivityManager>()
|
||||||
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
|
||||||
|
|
||||||
// this seems to not recognize vpn connections
|
// this seems to not recognize vpn connections
|
||||||
/*
|
/*
|
||||||
@ -37,27 +35,15 @@ object NetworkHelper {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
@Suppress("DEPRECATION")
|
@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
|
* @param context Context of the application
|
||||||
* @return isNetworkMobile
|
* @return whether the network is metered or not
|
||||||
*/
|
*/
|
||||||
@Suppress("DEPRECATION")
|
fun isNetworkMetered(context: Context): Boolean {
|
||||||
fun isNetworkMobile(context: Context): Boolean {
|
return context.getSystemService<ConnectivityManager>()!!.isActiveNetworkMetered
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,14 +33,9 @@ object PlayerHelper {
|
|||||||
/**
|
/**
|
||||||
* Get the audio source following the users preferences
|
* Get the audio source following the users preferences
|
||||||
*/
|
*/
|
||||||
fun getAudioSource(
|
fun getAudioSource(context: Context, audios: List<PipedStream>): String {
|
||||||
context: Context,
|
|
||||||
audios: List<PipedStream>
|
|
||||||
): String {
|
|
||||||
val audioFormat = PreferenceHelper.getString(PreferenceKeys.PLAYER_AUDIO_FORMAT, "all")
|
val audioFormat = PreferenceHelper.getString(PreferenceKeys.PLAYER_AUDIO_FORMAT, "all")
|
||||||
val audioPrefKey = if (
|
val audioPrefKey = if (NetworkHelper.isNetworkMetered(context)) {
|
||||||
NetworkHelper.isNetworkMobile(context)
|
|
||||||
) {
|
|
||||||
PreferenceKeys.PLAYER_AUDIO_QUALITY_MOBILE
|
PreferenceKeys.PLAYER_AUDIO_QUALITY_MOBILE
|
||||||
} else {
|
} else {
|
||||||
PreferenceKeys.PLAYER_AUDIO_QUALITY
|
PreferenceKeys.PLAYER_AUDIO_QUALITY
|
||||||
@ -351,17 +346,12 @@ object PlayerHelper {
|
|||||||
)
|
)
|
||||||
|
|
||||||
fun getDefaultResolution(context: Context): String {
|
fun getDefaultResolution(context: Context): String {
|
||||||
return if (NetworkHelper.isNetworkMobile(context)) {
|
val prefKey = if (NetworkHelper.isNetworkMetered(context)) {
|
||||||
PreferenceHelper.getString(
|
PreferenceKeys.DEFAULT_RESOLUTION_MOBILE
|
||||||
PreferenceKeys.DEFAULT_RESOLUTION_MOBILE,
|
|
||||||
""
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
PreferenceHelper.getString(
|
PreferenceKeys.DEFAULT_RESOLUTION
|
||||||
PreferenceKeys.DEFAULT_RESOLUTION,
|
|
||||||
""
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
return PreferenceHelper.getString(prefKey, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getIntentActon(context: Context): String {
|
fun getIntentActon(context: Context): String {
|
||||||
|
Loading…
Reference in New Issue
Block a user