Use modern network APIs
This commit is contained in:
parent
210ae35ee0
commit
a66d07ae84
|
@ -9,7 +9,6 @@ package org.moire.ultrasonic.util
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
import android.net.ConnectivityManager
|
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import androidx.preference.PreferenceManager
|
import androidx.preference.PreferenceManager
|
||||||
import java.util.regex.Pattern
|
import java.util.regex.Pattern
|
||||||
|
@ -70,12 +69,9 @@ object Settings {
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
val maxBitRate: Int
|
val maxBitRate: Int
|
||||||
get() {
|
get() {
|
||||||
val manager = Util.getConnectivityManager()
|
|
||||||
val networkInfo = manager.activeNetworkInfo ?: return 0
|
|
||||||
val wifi = networkInfo.type == ConnectivityManager.TYPE_WIFI
|
|
||||||
val preferences = preferences
|
val preferences = preferences
|
||||||
return preferences.getString(
|
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,
|
else Constants.PREFERENCES_KEY_MAX_BITRATE_MOBILE,
|
||||||
"0"
|
"0"
|
||||||
)!!.toInt()
|
)!!.toInt()
|
||||||
|
|
|
@ -22,9 +22,13 @@ import android.graphics.Canvas
|
||||||
import android.graphics.drawable.BitmapDrawable
|
import android.graphics.drawable.BitmapDrawable
|
||||||
import android.graphics.drawable.Drawable
|
import android.graphics.drawable.Drawable
|
||||||
import android.net.ConnectivityManager
|
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.Uri
|
||||||
import android.net.wifi.WifiManager
|
import android.net.wifi.WifiManager
|
||||||
import android.net.wifi.WifiManager.WifiLock
|
import android.net.wifi.WifiManager.WifiLock
|
||||||
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Environment
|
import android.os.Environment
|
||||||
import android.os.Parcelable
|
import android.os.Parcelable
|
||||||
|
@ -374,14 +378,45 @@ object Util {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a usable network for downloading media is available
|
||||||
|
*
|
||||||
|
* @return Boolean
|
||||||
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun isNetworkConnected(): Boolean {
|
fun isNetworkConnected(): Boolean {
|
||||||
val manager = getConnectivityManager()
|
val info = networkInfo()
|
||||||
val networkInfo = manager.activeNetworkInfo
|
val isUnmetered = info.unmetered
|
||||||
val connected = networkInfo != null && networkInfo.isConnected
|
|
||||||
val wifiConnected = connected && networkInfo!!.type == ConnectivityManager.TYPE_WIFI
|
|
||||||
val wifiRequired = Settings.isWifiRequiredForDownload
|
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
|
@JvmStatic
|
||||||
|
@ -921,4 +956,9 @@ object Util {
|
||||||
val context = appContext()
|
val context = appContext()
|
||||||
return context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
return context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data class NetworkInfo(
|
||||||
|
var connected: Boolean = false,
|
||||||
|
var unmetered: Boolean = false
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -339,8 +339,8 @@
|
||||||
<string name="settings.view_refresh_4000">4 seconds</string>
|
<string name="settings.view_refresh_4000">4 seconds</string>
|
||||||
<string name="settings.view_refresh_4500">4.5 seconds</string>
|
<string name="settings.view_refresh_4500">4.5 seconds</string>
|
||||||
<string name="settings.view_refresh_5000">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_summary">Only download media on unmetered connections</string>
|
||||||
<string name="settings.wifi_required_title">Wi-Fi Streaming Only</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.all">%1$s%2$s</string>
|
||||||
<string name="song_details.kbps">%d kbps</string>
|
<string name="song_details.kbps">%d kbps</string>
|
||||||
<string name="util.bytes_format.byte">0 B</string>
|
<string name="util.bytes_format.byte">0 B</string>
|
||||||
|
|
Loading…
Reference in New Issue