feat: statusBar ui
This commit is contained in:
parent
ca99eaa3e4
commit
4193326b92
|
|
@ -35,6 +35,9 @@ android {
|
||||||
kotlinOptions {
|
kotlinOptions {
|
||||||
jvmTarget = "11"
|
jvmTarget = "11"
|
||||||
}
|
}
|
||||||
|
buildFeatures {
|
||||||
|
viewBinding = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
|
|
||||||
|
|
@ -6,18 +6,40 @@ import androidx.activity.enableEdgeToEdge
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.core.view.ViewCompat
|
import androidx.core.view.ViewCompat
|
||||||
import androidx.core.view.WindowInsetsCompat
|
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() {
|
class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
|
private lateinit var binding: ActivityMainBinding
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
enableEdgeToEdge()
|
enableEdgeToEdge()
|
||||||
setContentView(R.layout.activity_main)
|
|
||||||
|
binding = ActivityMainBinding.inflate(layoutInflater)
|
||||||
|
|
||||||
|
setContentView(binding.root)
|
||||||
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
|
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
|
||||||
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
|
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
|
||||||
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
|
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
|
||||||
insets
|
insets
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lifecycleScope.launch {
|
||||||
|
/**
|
||||||
|
* 监听电量
|
||||||
|
*/
|
||||||
|
AppDependencies.deviceServiceManager.batteryPercentage.collect {
|
||||||
|
binding.batteryLevel.text = "${it}%"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 拦截返回键事件,防止返回到桌面
|
||||||
|
*/
|
||||||
onBackPressedDispatcher.addCallback {
|
onBackPressedDispatcher.addCallback {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import android.content.BroadcastReceiver
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.IntentFilter
|
import android.content.IntentFilter
|
||||||
|
import android.os.BatteryManager
|
||||||
import com.rokid.dcg.sprite.service.IInstructListener
|
import com.rokid.dcg.sprite.service.IInstructListener
|
||||||
import com.rokid.dcg.sprite.service.IInstructService
|
import com.rokid.dcg.sprite.service.IInstructService
|
||||||
import com.rokid.dcg.sprite.service.ISpriteMediaService
|
import com.rokid.dcg.sprite.service.ISpriteMediaService
|
||||||
|
|
@ -45,6 +46,12 @@ class DeviceServiceManager(val context: Application) {
|
||||||
private val _instructState = MutableStateFlow<InstructState>(InstructState.None)
|
private val _instructState = MutableStateFlow<InstructState>(InstructState.None)
|
||||||
val instructState = _instructState.asStateFlow()
|
val instructState = _instructState.asStateFlow()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 系统电量
|
||||||
|
*/
|
||||||
|
private val _batteryPercentage = MutableStateFlow(-1)
|
||||||
|
val batteryPercentage = _batteryPercentage.asStateFlow()
|
||||||
|
|
||||||
lateinit var sn: String
|
lateinit var sn: String
|
||||||
private set
|
private set
|
||||||
|
|
||||||
|
|
@ -56,13 +63,15 @@ class DeviceServiceManager(val context: Application) {
|
||||||
private var wakeTimeoutJob: Job? = null
|
private var wakeTimeoutJob: Job? = null
|
||||||
|
|
||||||
init {
|
init {
|
||||||
val filter = IntentFilter()
|
val filter = IntentFilter().apply {
|
||||||
filter.addAction(SysKeyAction.SPRITE_BUTTON_DOWN)
|
addAction(SysKeyAction.SPRITE_BUTTON_DOWN)
|
||||||
filter.addAction(SysKeyAction.SPRITE_BUTTON_UP)
|
addAction(SysKeyAction.SPRITE_BUTTON_UP)
|
||||||
filter.addAction(SysKeyAction.SPRITE_BUTTON_CLICK)
|
addAction(SysKeyAction.SPRITE_BUTTON_CLICK)
|
||||||
filter.addAction(SysKeyAction.SPRITE_BUTTON_LONG_PRESS)
|
addAction(SysKeyAction.SPRITE_BUTTON_LONG_PRESS)
|
||||||
filter.addAction(SysKeyAction.SPRITE_BUTTON_VERY_VERY_LONG_PRESS)
|
addAction(SysKeyAction.SPRITE_BUTTON_VERY_VERY_LONG_PRESS)
|
||||||
filter.addAction(SysKeyAction.TOUCH_BAR_LONG_PRESS)
|
addAction(SysKeyAction.TOUCH_BAR_LONG_PRESS)
|
||||||
|
addAction(Intent.ACTION_BATTERY_CHANGED) // 监听电量
|
||||||
|
}
|
||||||
context.registerReceiver(SysKeyReceiver(), filter)
|
context.registerReceiver(SysKeyReceiver(), filter)
|
||||||
|
|
||||||
scope.launch {
|
scope.launch {
|
||||||
|
|
@ -226,6 +235,15 @@ class DeviceServiceManager(val context: Application) {
|
||||||
override fun onReceive(context: Context?, intent: Intent?) {
|
override fun onReceive(context: Context?, intent: Intent?) {
|
||||||
val action = intent?.action ?: return
|
val action = intent?.action ?: return
|
||||||
when (action) {
|
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 -> {
|
SysKeyAction.SPRITE_BUTTON_DOWN -> {
|
||||||
Timber.d("按下拍照键 SPRITE_BUTTON_DOWN")
|
Timber.d("按下拍照键 SPRITE_BUTTON_DOWN")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,12 +8,32 @@
|
||||||
tools:context=".MainActivity">
|
tools:context=".MainActivity">
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
|
android:id="@+id/battery_level"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginEnd="30.dp"
|
||||||
android:text="Hello World!"
|
android:text="Hello World!"
|
||||||
app:layout_constraintBottom_toBottomOf="parent"
|
android:textColor="@color/white"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintBottom_toTopOf="@id/bottom"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintEnd_toEndOf="parent" />
|
||||||
app:layout_constraintTop_toTopOf="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>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
Loading…
Reference in New Issue