修改识别模式

This commit is contained in:
2026-05-19 15:22:44 +08:00
parent 45828c7e9b
commit 066334932f
11 changed files with 535 additions and 165 deletions

View File

@@ -1,19 +1,30 @@
package com.example.defenseapplication.liveVideo
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import com.example.defenseapplication.R
import com.example.defenseapplication.base.BaseActivity
import com.example.defenseapplication.databinding.SettingsActivityBinding
import com.example.defenseapplication.utils.DeviceIdEvent
import com.example.defenseapplication.utils.DeviceUpdateEvent
import com.example.defenseapplication.utils.LanguageUtil
import com.example.defenseapplication.utils.RetrofitNetwork
import com.example.defenseapplication.view.CustomDialogFragmentText
import com.example.defenseapplication.view.LanguageDialog
import com.example.defenseapplication.view.RecognitionModeDialog
import com.gyf.immersionbar.ImmersionBar
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
//设置页面
class SettingsActivity : BaseActivity() {
private lateinit var binding: SettingsActivityBinding
private val SP_NAME = "DeviceSettings"
private val KEY_RECOGNITION_MODE = "recognition_mode"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@@ -35,7 +46,7 @@ class SettingsActivity : BaseActivity() {
binding.itemRecognitionMode.setOnClickListener {
val dialog = RecognitionModeDialog(this)
dialog.setOnConfirmListener { mode ->
binding.tvRecognitionMode.text = mode
updateRecognitionMode(mode)
}
dialog.show()
}
@@ -45,7 +56,7 @@ class SettingsActivity : BaseActivity() {
.setTitle(getString(R.string.device_name))
.setInputText(binding.tvDeviceName.text.toString())
dialog.setOnConfirmListener { name ->
binding.tvDeviceName.text = name
updateDeviceName(name)
}
dialog.show(supportFragmentManager, "CustomDialogFragment")
}
@@ -84,6 +95,136 @@ class SettingsActivity : BaseActivity() {
val intent = Intent(this, WiFiManagementActivity::class.java)
startActivity(intent)
}
fetchDeviceInfo()
}
private fun fetchDeviceInfo() {
val deviceId = DeviceIdEvent.getDeviceId()
if (deviceId.isNullOrEmpty()) {
Toast.makeText(this, "设备ID为空", Toast.LENGTH_SHORT).show()
return
}
CoroutineScope(Dispatchers.IO).launch {
try {
val response = RetrofitNetwork.getNetworkService().getDeviceInfo(deviceId)
withContext(Dispatchers.Main) {
if (response.code == 200 && response.data != null) {
updateDeviceInfo(response.data)
} else {
Toast.makeText(this@SettingsActivity, response.msg, Toast.LENGTH_SHORT).show()
}
}
} catch (e: Exception) {
e.printStackTrace()
withContext(Dispatchers.Main) {
Toast.makeText(this@SettingsActivity, "网络请求失败", Toast.LENGTH_SHORT).show()
}
}
}
}
private fun updateDeviceInfo(device: com.example.defenseapplication.bean.DeviceBean) {
binding.tvDeviceName.text = device.deviceName ?: "未设置"
binding.tvWifiName.text = device.wifiName ?: "未设置"
val serverMode = device.patternPerception
val cacheMode = getCachedRecognitionMode()
binding.tvRecognitionMode.text = when {
!serverMode.isNullOrEmpty() -> {
val displayMode = when (serverMode) {
"0" -> getString(R.string.bluetooth_recognition)
"1" -> getString(R.string.bluetooth_face_recognition)
else -> serverMode
}
saveRecognitionMode(displayMode)
displayMode
}
!cacheMode.isNullOrEmpty() -> cacheMode
else -> getString(R.string.bluetooth_recognition)
}
}
private fun getCachedRecognitionMode(): String? {
val sp = getSharedPreferences("DeviceSettings", Context.MODE_PRIVATE)
return sp.getString("recognition_mode", null)
}
private fun saveRecognitionMode(mode: String) {
val sp = getSharedPreferences("DeviceSettings", Context.MODE_PRIVATE)
sp.edit().putString("recognition_mode", mode).apply()
}
private fun updateDeviceName(name: String) {
val deviceId = DeviceIdEvent.getDeviceId()
if (deviceId.isNullOrEmpty()) {
Toast.makeText(this, "设备ID为空", Toast.LENGTH_SHORT).show()
return
}
CoroutineScope(Dispatchers.IO).launch {
try {
val deviceBean = com.example.defenseapplication.bean.DeviceBean(
id = deviceId,
deviceName = name,
)
val response = RetrofitNetwork.getNetworkService().updateDeviceInfo(deviceBean)
withContext(Dispatchers.Main) {
if (response.code == 200) {
binding.tvDeviceName.text = name
Toast.makeText(this@SettingsActivity, "修改成功", Toast.LENGTH_SHORT).show()
DeviceUpdateEvent.notifyDeviceUpdated(deviceId)
} else {
Toast.makeText(this@SettingsActivity, response.msg, Toast.LENGTH_SHORT).show()
}
}
} catch (e: Exception) {
e.printStackTrace()
withContext(Dispatchers.Main) {
Toast.makeText(this@SettingsActivity, "网络请求失败", Toast.LENGTH_SHORT).show()
}
}
}
}
private fun updateRecognitionMode(mode: String) {
val deviceId = DeviceIdEvent.getDeviceId()
if (deviceId.isNullOrEmpty()) {
Toast.makeText(this, "设备ID为空", Toast.LENGTH_SHORT).show()
return
}
val patternPerception = when (mode) {
getString(R.string.bluetooth_recognition) -> "0"
getString(R.string.bluetooth_face_recognition) -> "1"
else -> "0"
}
CoroutineScope(Dispatchers.IO).launch {
try {
val deviceBean = com.example.defenseapplication.bean.DeviceBean(
id = deviceId,
patternPerception = patternPerception,
)
val response = RetrofitNetwork.getNetworkService().updateDeviceInfo(deviceBean)
withContext(Dispatchers.Main) {
if (response.code == 200) {
binding.tvRecognitionMode.text = mode
saveRecognitionMode(mode)
Toast.makeText(this@SettingsActivity, "修改成功", Toast.LENGTH_SHORT).show()
DeviceUpdateEvent.notifyDeviceUpdated(deviceId)
} else {
Toast.makeText(this@SettingsActivity, response.msg, Toast.LENGTH_SHORT).show()
}
}
} catch (e: Exception) {
e.printStackTrace()
withContext(Dispatchers.Main) {
Toast.makeText(this@SettingsActivity, "网络请求失败", Toast.LENGTH_SHORT).show()
}
}
}
}
}