Use modern network APIs

This commit is contained in:
tzugen 2021-11-01 15:09:53 +01:00
parent 210ae35ee0
commit a66d07ae84
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
3 changed files with 48 additions and 12 deletions

View File

@ -9,7 +9,6 @@ package org.moire.ultrasonic.util
import android.content.Context
import android.content.SharedPreferences
import android.net.ConnectivityManager
import android.os.Build
import androidx.preference.PreferenceManager
import java.util.regex.Pattern
@ -70,12 +69,9 @@ object Settings {
@JvmStatic
val maxBitRate: Int
get() {
val manager = Util.getConnectivityManager()
val networkInfo = manager.activeNetworkInfo ?: return 0
val wifi = networkInfo.type == ConnectivityManager.TYPE_WIFI
val preferences = preferences
return preferences.getString(
if (wifi) Constants.PREFERENCES_KEY_MAX_BITRATE_WIFI
if (Util.networkInfo().unmetered) Constants.PREFERENCES_KEY_MAX_BITRATE_WIFI
else Constants.PREFERENCES_KEY_MAX_BITRATE_MOBILE,
"0"
)!!.toInt()

View File

@ -22,9 +22,13 @@ import android.graphics.Canvas
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET
import android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED
import android.net.Uri
import android.net.wifi.WifiManager
import android.net.wifi.WifiManager.WifiLock
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.os.Parcelable
@ -374,14 +378,45 @@ object Util {
return null
}
/**
* Check if a usable network for downloading media is available
*
* @return Boolean
*/
@JvmStatic
fun isNetworkConnected(): Boolean {
val manager = getConnectivityManager()
val networkInfo = manager.activeNetworkInfo
val connected = networkInfo != null && networkInfo.isConnected
val wifiConnected = connected && networkInfo!!.type == ConnectivityManager.TYPE_WIFI
val info = networkInfo()
val isUnmetered = info.unmetered
val wifiRequired = Settings.isWifiRequiredForDownload
return connected && (!wifiRequired || wifiConnected)
return info.connected && (!wifiRequired || isUnmetered)
}
/**
* Query connectivity status
*
* @return NetworkInfo object
*/
@Suppress("DEPRECATION")
fun networkInfo(): NetworkInfo {
val manager = getConnectivityManager()
val info = NetworkInfo()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val network: Network? = manager.activeNetwork
val capabilities = manager.getNetworkCapabilities(network)
if (capabilities != null) {
info.unmetered = capabilities.hasCapability(NET_CAPABILITY_NOT_METERED)
info.connected = capabilities.hasCapability(NET_CAPABILITY_INTERNET)
}
} else {
val networkInfo = manager.activeNetworkInfo
if (networkInfo != null) {
info.unmetered = networkInfo.type == ConnectivityManager.TYPE_WIFI
info.connected = networkInfo.isConnected
}
}
return info
}
@JvmStatic
@ -921,4 +956,9 @@ object Util {
val context = appContext()
return context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
}
data class NetworkInfo(
var connected: Boolean = false,
var unmetered: Boolean = false
)
}

View File

@ -339,8 +339,8 @@
<string name="settings.view_refresh_4000">4 seconds</string>
<string name="settings.view_refresh_4500">4.5 seconds</string>
<string name="settings.view_refresh_5000">5 seconds</string>
<string name="settings.wifi_required_summary">Only stream media if connected to Wi-Fi</string>
<string name="settings.wifi_required_title">Wi-Fi Streaming Only</string>
<string name="settings.wifi_required_summary">Only download media on unmetered connections</string>
<string name="settings.wifi_required_title">Download on Wi-Fi only</string>
<string name="song_details.all">%1$s%2$s</string>
<string name="song_details.kbps">%d kbps</string>
<string name="util.bytes_format.byte">0 B</string>