Use network detection logic from AntennaPod

This commit is contained in:
tzugen 2022-12-11 00:18:30 +01:00
parent d56bbd819d
commit 1aa0b0dda2
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
6 changed files with 45 additions and 53 deletions

View File

@ -124,7 +124,7 @@ class DownloadService : Service(), KoinComponent {
fun processNextTracks() { fun processNextTracks() {
retrying = false retrying = false
if ( if (
!Util.isNetworkConnected() || !Util.hasUsableNetwork() ||
!Util.isExternalStoragePresent() || !Util.isExternalStoragePresent() ||
!storageMonitor.isExternalStorageAvailable !storageMonitor.isExternalStorageAvailable
) { ) {

View File

@ -12,7 +12,7 @@ class NetworkAndStorageChecker(val context: Context) {
fun warnIfNetworkOrStorageUnavailable() { fun warnIfNetworkOrStorageUnavailable() {
if (!Util.isExternalStoragePresent()) { if (!Util.isExternalStoragePresent()) {
Util.toast(context, R.string.select_album_no_sdcard) Util.toast(context, R.string.select_album_no_sdcard)
} else if (!isOffline() && !Util.isNetworkConnected()) { } else if (!isOffline() && !Util.hasUsableNetwork()) {
Util.toast(context, R.string.select_album_no_network) Util.toast(context, R.string.select_album_no_network)
} }
} }

View File

@ -15,7 +15,7 @@ import org.moire.ultrasonic.util.Util
class VideoPlayer { class VideoPlayer {
companion object { companion object {
fun playVideo(context: Context, track: Track?) { fun playVideo(context: Context, track: Track?) {
if (!Util.isNetworkConnected() || track == null) { if (!Util.hasUsableNetwork() || track == null) {
Util.toast(context, R.string.select_album_no_network) Util.toast(context, R.string.select_album_no_network)
return return
} }

View File

@ -54,7 +54,7 @@ object CommunicationError {
@Suppress("ReturnCount") @Suppress("ReturnCount")
fun getErrorMessage(error: Throwable, context: Context?): String { fun getErrorMessage(error: Throwable, context: Context?): String {
if (context == null) return "Couldn't get Error message, Context is null" if (context == null) return "Couldn't get Error message, Context is null"
if (error is IOException && !Util.isNetworkConnected()) { if (error is IOException && !Util.hasUsableNetwork()) {
return context.resources.getString(R.string.background_task_no_network) return context.resources.getString(R.string.background_task_no_network)
} else if (error is FileNotFoundException) { } else if (error is FileNotFoundException) {
return context.resources.getString(R.string.background_task_not_found) return context.resources.getString(R.string.background_task_not_found)

View File

@ -32,14 +32,10 @@ object Settings {
@JvmStatic @JvmStatic
val maxBitRate: Int val maxBitRate: Int
get() { get() {
val network = Util.networkInfo() return if (Util.isNetworkRestricted()) {
if (!network.connected) return 0
return if (network.unmetered) {
maxWifiBitRate
} else {
maxMobileBitRate maxMobileBitRate
} else {
maxWifiBitRate
} }
} }

View File

@ -17,13 +17,10 @@ import android.content.ContentResolver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.media.MediaScannerConnection import android.media.MediaScannerConnection
import android.net.ConnectivityManager import android.net.ConnectivityManager
import android.net.Network import android.net.NetworkCapabilities
import android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET
import android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED
import android.net.Uri import android.net.Uri
import android.net.wifi.WifiManager import android.net.wifi.WifiManager
import android.net.wifi.WifiManager.WifiLock import android.net.wifi.WifiManager.WifiLock
@ -317,39 +314,50 @@ object Util {
* @return Boolean * @return Boolean
*/ */
@JvmStatic @JvmStatic
fun isNetworkConnected(): Boolean { fun hasUsableNetwork(): Boolean {
val info = networkInfo() val isUnmetered = !isNetworkRestricted()
val isUnmetered = info.unmetered
val wifiRequired = Settings.isWifiRequiredForDownload val wifiRequired = Settings.isWifiRequiredForDownload
return info.connected && (!wifiRequired || isUnmetered) return (!wifiRequired || isUnmetered)
} }
/** fun isNetworkRestricted(): Boolean {
* Query connectivity status return isNetworkMetered() || isNetworkCellular()
* }
* @return NetworkInfo object
*/
@Suppress("DEPRECATION")
fun networkInfo(): NetworkInfo {
val manager = connectivityManager
val info = NetworkInfo()
private fun isNetworkMetered(): Boolean {
val connManager = connectivityManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val network: Network? = manager.activeNetwork val capabilities = connManager.getNetworkCapabilities(
val capabilities = manager.getNetworkCapabilities(network) connManager.activeNetwork
)
if (capabilities != null) { if (capabilities != null &&
info.unmetered = capabilities.hasCapability(NET_CAPABILITY_NOT_METERED) capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) &&
info.connected = capabilities.hasCapability(NET_CAPABILITY_INTERNET) capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)
} ) {
} else { return false
val networkInfo = manager.activeNetworkInfo
if (networkInfo != null) {
info.unmetered = networkInfo.type == ConnectivityManager.TYPE_WIFI
info.connected = networkInfo.isConnected
} }
} }
return info return connManager.isActiveNetworkMetered
}
@Suppress("DEPRECATION")
private fun isNetworkCellular(): Boolean {
val connManager = connectivityManager
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val network = connManager.activeNetwork
?: return false // Nothing connected
connManager.getNetworkInfo(network)
?: return true // Better be safe than sorry
val capabilities = connManager.getNetworkCapabilities(network)
?: return true // Better be safe than sorry
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
} else {
// if the default network is a VPN,
// this method will return the NetworkInfo for one of its underlying networks
val info = connManager.activeNetworkInfo
?: return false // Nothing connected
info.type == ConnectivityManager.TYPE_MOBILE
}
} }
@JvmStatic @JvmStatic
@ -380,10 +388,6 @@ object Util {
return (newWidth * aspectRatio).roundToInt() return (newWidth * aspectRatio).roundToInt()
} }
private fun getScaledHeight(bitmap: Bitmap, width: Int): Int {
return getScaledHeight(bitmap.height.toDouble(), bitmap.width.toDouble(), width)
}
fun getSongsFromSearchResult(searchResult: SearchResult): MusicDirectory { fun getSongsFromSearchResult(searchResult: SearchResult): MusicDirectory {
val musicDirectory = MusicDirectory() val musicDirectory = MusicDirectory()
for (entry in searchResult.songs) { for (entry in searchResult.songs) {
@ -752,14 +756,6 @@ object Util {
return this?.let(block) return this?.let(block)
} }
/**
* Small data class to store information about the current network
**/
data class NetworkInfo(
var connected: Boolean = false,
var unmetered: Boolean = false
)
/** /**
* Closes a Closeable while ignoring any errors. * Closes a Closeable while ignoring any errors.
**/ **/