feat: statusBar ui

This commit is contained in:
yangxisong 2025-11-13 17:55:24 +08:00
parent ca99eaa3e4
commit 4193326b92
4 changed files with 75 additions and 12 deletions

View File

@ -35,6 +35,9 @@ android {
kotlinOptions {
jvmTarget = "11"
}
buildFeatures {
viewBinding = true
}
}
dependencies {

View File

@ -6,18 +6,40 @@ import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.yuanxuan.rokid.databinding.ActivityMainBinding
import com.yuanxuan.rokid.dependencies.AppDependencies
import kotlinx.coroutines.launch
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
lifecycleScope.launch {
/**
* 监听电量
*/
AppDependencies.deviceServiceManager.batteryPercentage.collect {
binding.batteryLevel.text = "${it}%"
}
}
/**
* 拦截返回键事件防止返回到桌面
*/
onBackPressedDispatcher.addCallback {
}
}

View File

@ -5,6 +5,7 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import com.rokid.dcg.sprite.service.IInstructListener
import com.rokid.dcg.sprite.service.IInstructService
import com.rokid.dcg.sprite.service.ISpriteMediaService
@ -45,6 +46,12 @@ class DeviceServiceManager(val context: Application) {
private val _instructState = MutableStateFlow<InstructState>(InstructState.None)
val instructState = _instructState.asStateFlow()
/**
* 系统电量
*/
private val _batteryPercentage = MutableStateFlow(-1)
val batteryPercentage = _batteryPercentage.asStateFlow()
lateinit var sn: String
private set
@ -56,13 +63,15 @@ class DeviceServiceManager(val context: Application) {
private var wakeTimeoutJob: Job? = null
init {
val filter = IntentFilter()
filter.addAction(SysKeyAction.SPRITE_BUTTON_DOWN)
filter.addAction(SysKeyAction.SPRITE_BUTTON_UP)
filter.addAction(SysKeyAction.SPRITE_BUTTON_CLICK)
filter.addAction(SysKeyAction.SPRITE_BUTTON_LONG_PRESS)
filter.addAction(SysKeyAction.SPRITE_BUTTON_VERY_VERY_LONG_PRESS)
filter.addAction(SysKeyAction.TOUCH_BAR_LONG_PRESS)
val filter = IntentFilter().apply {
addAction(SysKeyAction.SPRITE_BUTTON_DOWN)
addAction(SysKeyAction.SPRITE_BUTTON_UP)
addAction(SysKeyAction.SPRITE_BUTTON_CLICK)
addAction(SysKeyAction.SPRITE_BUTTON_LONG_PRESS)
addAction(SysKeyAction.SPRITE_BUTTON_VERY_VERY_LONG_PRESS)
addAction(SysKeyAction.TOUCH_BAR_LONG_PRESS)
addAction(Intent.ACTION_BATTERY_CHANGED) // 监听电量
}
context.registerReceiver(SysKeyReceiver(), filter)
scope.launch {
@ -226,6 +235,15 @@ class DeviceServiceManager(val context: Application) {
override fun onReceive(context: Context?, intent: Intent?) {
val action = intent?.action ?: return
when (action) {
Intent.ACTION_BATTERY_CHANGED -> {
val level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
val scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
if (level == -1 || scale == -1)
return
val batteryPercentage = (level / scale.toFloat() * 100).toInt()
_batteryPercentage.update { batteryPercentage }
}
SysKeyAction.SPRITE_BUTTON_DOWN -> {
Timber.d("按下拍照键 SPRITE_BUTTON_DOWN")
}

View File

@ -8,12 +8,32 @@
tools:context=".MainActivity">
<TextView
android:id="@+id/battery_level"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="30.dp"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:textColor="@color/white"
app:layout_constraintBottom_toTopOf="@id/bottom"
app:layout_constraintEnd_toEndOf="parent" />
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30.dp"
android:textColor="@color/white"
app:layout_constraintBottom_toTopOf="@id/bottom"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@id/bottom" />
<Space
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="10.dp"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>