feat: statusBar ui

This commit is contained in:
2025-11-14 11:44:49 +08:00
parent 189ffa71bf
commit 6b234f86e7
16 changed files with 253 additions and 3 deletions
@@ -5,6 +5,11 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import android.net.wifi.WifiManager
import android.os.BatteryManager
import com.rokid.dcg.sprite.service.IInstructListener
import com.rokid.dcg.sprite.service.IInstructService
@@ -29,7 +34,7 @@ import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
class DeviceServiceManager(val context: Application) {
class DeviceServiceManager(val context: Application) : ConnectivityManager.NetworkCallback() {
companion object {
/**
@@ -52,6 +57,15 @@ class DeviceServiceManager(val context: Application) {
private val _batteryPercentage = MutableStateFlow(-1)
val batteryPercentage = _batteryPercentage.asStateFlow()
/**
* wifi
*/
private val _wifiState: MutableStateFlow<WifiState> = MutableStateFlow(WifiState.Unconnected)
val wifiState = _wifiState.asStateFlow()
private val wifiManager by lazy {
context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
}
lateinit var sn: String
private set
@@ -86,6 +100,16 @@ class DeviceServiceManager(val context: Application) {
}
}
}
/**
* 监听wifi状态
*/
val connectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val request = NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.build()
connectivityManager.registerNetworkCallback(request, this)
}
val instructListener = object : IInstructListener.Stub() {
@@ -274,10 +298,29 @@ class DeviceServiceManager(val context: Application) {
}
}
override fun onLost(network: Network) {
_wifiState.update { WifiState.Unconnected }
}
override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
_wifiState.update {
val level = wifiManager.calculateSignalLevel(networkCapabilities.signalStrength)
WifiState.Connected(
// maxSignalLevel是4 level区间应该是 0-3 这里却返回了4 限制一下
level = level.coerceAtMost(wifiManager.maxSignalLevel - 1)
)
}
}
sealed interface InstructState {
data object None : InstructState
data object WaitingInstructState : InstructState
}
sealed interface WifiState {
data object Unconnected : WifiState
data class Connected(val level: Int) : WifiState
}
}