语音设置
This commit is contained in:
@@ -8,7 +8,6 @@ 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
|
||||
@@ -20,12 +19,18 @@ import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
//设置页面
|
||||
class SettingsActivity : BaseActivity(), DeviceUpdateEvent.DeviceUpdateListener {
|
||||
class SettingsActivity : BaseActivity() {
|
||||
|
||||
private lateinit var binding: SettingsActivityBinding
|
||||
private val SP_NAME = "DeviceSettings"
|
||||
private val KEY_RECOGNITION_MODE = "recognition_mode"
|
||||
private var alertAudio: String? = null
|
||||
private var audioContent: String? = null
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
fetchDeviceInfo()
|
||||
}
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
binding = SettingsActivityBinding.inflate(layoutInflater)
|
||||
@@ -37,6 +42,8 @@ class SettingsActivity : BaseActivity(), DeviceUpdateEvent.DeviceUpdateListener
|
||||
.init()
|
||||
binding.itemVoiceSetting.setOnClickListener {
|
||||
val intent = Intent(this, VoiceSettingsActivity::class.java)
|
||||
intent.putExtra("alertAudio", alertAudio ?: "1")
|
||||
intent.putExtra("audioContent", audioContent ?: "")
|
||||
startActivity(intent)
|
||||
}
|
||||
binding.ivBack.setOnClickListener {
|
||||
@@ -95,17 +102,6 @@ class SettingsActivity : BaseActivity(), DeviceUpdateEvent.DeviceUpdateListener
|
||||
val intent = Intent(this, WiFiManagementActivity::class.java)
|
||||
startActivity(intent)
|
||||
}
|
||||
fetchDeviceInfo()
|
||||
DeviceUpdateEvent.addListener(this)
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
DeviceUpdateEvent.removeListener(this)
|
||||
}
|
||||
|
||||
override fun onDeviceUpdated(deviceId: String) {
|
||||
fetchDeviceInfo()
|
||||
}
|
||||
|
||||
private fun fetchDeviceInfo() {
|
||||
@@ -138,6 +134,10 @@ class SettingsActivity : BaseActivity(), DeviceUpdateEvent.DeviceUpdateListener
|
||||
binding.tvDeviceName.text = device.deviceName ?: "未设置"
|
||||
binding.tvWifiName.text = device.wifiName ?: "未设置"
|
||||
|
||||
// 保存告警音频和内容数据,用于传递给 VoiceSettingsActivity
|
||||
alertAudio = device.alertAudio
|
||||
audioContent = device.audioContet
|
||||
|
||||
val serverMode = device.patternPerception
|
||||
val cacheMode = getCachedRecognitionMode()
|
||||
|
||||
@@ -192,7 +192,6 @@ class SettingsActivity : BaseActivity(), DeviceUpdateEvent.DeviceUpdateListener
|
||||
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()
|
||||
}
|
||||
@@ -231,7 +230,6 @@ class SettingsActivity : BaseActivity(), DeviceUpdateEvent.DeviceUpdateListener
|
||||
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()
|
||||
}
|
||||
|
||||
@@ -2,51 +2,107 @@ package com.example.defenseapplication.liveVideo
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.view.animation.AccelerateDecelerateInterpolator
|
||||
import android.widget.ImageView
|
||||
import android.util.Log
|
||||
import android.widget.Toast
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.view.ViewCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
import com.example.defenseapplication.R
|
||||
import com.example.defenseapplication.databinding.SettingsActivityBinding
|
||||
import com.example.defenseapplication.bean.DeviceBean
|
||||
import com.example.defenseapplication.databinding.VoiceSettingsActivityBinding
|
||||
import com.example.defenseapplication.utils.ApiService
|
||||
import com.example.defenseapplication.utils.DeviceIdEvent
|
||||
import com.example.defenseapplication.utils.RetrofitNetwork
|
||||
import com.gyf.immersionbar.ImmersionBar
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
//语音设置
|
||||
class VoiceSettingsActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var binding: VoiceSettingsActivityBinding
|
||||
private lateinit var apiService: ApiService
|
||||
private var deviceId: String? = null
|
||||
private var originalAlertAudio: String? = null
|
||||
private var originalAudioContent: String? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
binding = VoiceSettingsActivityBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
|
||||
apiService = RetrofitNetwork.getNetworkService()
|
||||
|
||||
// ImmersionBar核心调用,沉浸式
|
||||
ImmersionBar.with(this)
|
||||
.statusBarDarkFont(true)
|
||||
.titleBar(binding.topBar)
|
||||
.init()
|
||||
|
||||
// 获取设备ID
|
||||
deviceId = DeviceIdEvent.getDeviceId()
|
||||
|
||||
// 从 Intent 获取设备数据
|
||||
originalAlertAudio = intent.getStringExtra("alertAudio") ?: "1"
|
||||
originalAudioContent = intent.getStringExtra("audioContent") ?: ""
|
||||
|
||||
// 设置Switch状态:0->false,1->true
|
||||
binding.swWarningVoice.isChecked = originalAlertAudio == "1"
|
||||
|
||||
binding.swWarningVoice.setOnCheckedChangeListener { _, isChecked ->
|
||||
when(isChecked) {
|
||||
true -> {
|
||||
// 打开开关业务逻辑
|
||||
// 开关状态变化时不立即请求,仅记录状态
|
||||
}
|
||||
|
||||
false -> {
|
||||
// 关闭开关业务逻辑
|
||||
}
|
||||
}
|
||||
}
|
||||
binding.rlWarningContent.setOnClickListener {
|
||||
val intent = Intent(this, WarningVoiceContentActivity::class.java)
|
||||
startActivity(intent)
|
||||
// 传递设备数据给警告语音内容页面
|
||||
intent.putExtra("deviceId", deviceId)
|
||||
intent.putExtra("audioContent", originalAudioContent)
|
||||
startActivityForResult(intent, 1001)
|
||||
}
|
||||
|
||||
binding.ivBack.setOnClickListener {
|
||||
// 点击返回时触发修改设备请求
|
||||
updateDeviceAlertAudio()
|
||||
}
|
||||
}
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
super.onActivityResult(requestCode, resultCode, data)
|
||||
if (requestCode == 1001 && resultCode == RESULT_OK) {
|
||||
// 获取从警告语音内容页面返回的音频内容
|
||||
val newAudioContent = data?.getStringExtra("result")
|
||||
originalAudioContent = newAudioContent
|
||||
Log.d("WarningVoiceContentActivity", "newAudioContent: $newAudioContent")
|
||||
if (newAudioContent != null) {}
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateDeviceAlertAudio() {
|
||||
deviceId?.let { id ->
|
||||
val newAlertAudio = if (binding.swWarningVoice.isChecked) "1" else "0"
|
||||
// 只有状态发生变化时才请求
|
||||
if (newAlertAudio != originalAlertAudio) {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
try {
|
||||
val deviceBean = DeviceBean(id = id, alertAudio = newAlertAudio)
|
||||
val response = apiService.updateDeviceInfo(deviceBean)
|
||||
withContext(Dispatchers.Main) {
|
||||
if (response.code == 200 && response.data != null) {
|
||||
Toast.makeText(this@VoiceSettingsActivity, "修改成功", Toast.LENGTH_SHORT).show()
|
||||
} else {
|
||||
Toast.makeText(this@VoiceSettingsActivity, response.msg, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
finish()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
withContext(Dispatchers.Main) {
|
||||
Toast.makeText(this@VoiceSettingsActivity, "修改失败", Toast.LENGTH_SHORT).show()
|
||||
finish()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
} else {
|
||||
finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,43 +1,100 @@
|
||||
package com.example.defenseapplication.liveVideo
|
||||
|
||||
import android.content.Intent
|
||||
import android.graphics.Color
|
||||
import android.os.Bundle
|
||||
import android.text.Editable
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import android.widget.Toast
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.widget.doAfterTextChanged
|
||||
import com.example.defenseapplication.bean.DeviceBean
|
||||
import com.example.defenseapplication.databinding.WarningVoiceContentActivityBinding
|
||||
import com.example.defenseapplication.utils.ApiService
|
||||
import com.example.defenseapplication.utils.RetrofitNetwork
|
||||
import com.gyf.immersionbar.ImmersionBar
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
//警告语音内容
|
||||
class WarningVoiceContentActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var binding: WarningVoiceContentActivityBinding
|
||||
private lateinit var apiService: ApiService
|
||||
private var deviceId: String? = null
|
||||
private var originalAudioContent: String? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
binding = WarningVoiceContentActivityBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
|
||||
apiService = RetrofitNetwork.getNetworkService()
|
||||
|
||||
// ImmersionBar核心调用,沉浸式
|
||||
ImmersionBar.with(this)
|
||||
.statusBarDarkFont(true)
|
||||
.titleBar(binding.topBar)
|
||||
.init()
|
||||
// 初始化计数显示
|
||||
val initialLength = binding.etWarningContent.text.length
|
||||
updateWordCount(initialLength)
|
||||
|
||||
// 从 Intent 获取设备数据(不再发送网络请求)
|
||||
deviceId = intent.getStringExtra("deviceId")
|
||||
originalAudioContent = intent.getStringExtra("audioContent") ?: ""
|
||||
|
||||
// 设置告警内容
|
||||
binding.etWarningContent.setText(originalAudioContent)
|
||||
// 更新字数统计
|
||||
updateWordCount(originalAudioContent?.length ?: 0)
|
||||
|
||||
// 添加文本变化监听
|
||||
binding.etWarningContent.doAfterTextChanged { text ->
|
||||
val length = text?.length ?: 0
|
||||
updateWordCount(length)
|
||||
}
|
||||
|
||||
binding.btnBack.setOnClickListener {
|
||||
finish()
|
||||
}
|
||||
|
||||
binding.btnSave.setOnClickListener {
|
||||
//保存的逻辑
|
||||
// 点击保存时触发修改设备请求
|
||||
updateDeviceAudioContent()
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateDeviceAudioContent() {
|
||||
deviceId?.let { id ->
|
||||
val newAudioContent = binding.etWarningContent.text.toString().trim()
|
||||
// 只有内容发生变化时才请求
|
||||
if (newAudioContent != originalAudioContent) {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
try {
|
||||
val deviceBean = DeviceBean(id = id, audioContet = newAudioContent)
|
||||
val response = apiService.updateDeviceInfo(deviceBean)
|
||||
withContext(Dispatchers.Main) {
|
||||
if (response.code == 200 && response.data != null) {
|
||||
Toast.makeText(this@WarningVoiceContentActivity, "保存成功", Toast.LENGTH_SHORT).show()
|
||||
} else {
|
||||
Toast.makeText(this@WarningVoiceContentActivity, response.msg, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
val intent = Intent()
|
||||
intent.putExtra("result",newAudioContent)
|
||||
setResult(RESULT_OK, intent)
|
||||
finish()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
withContext(Dispatchers.Main) {
|
||||
Toast.makeText(this@WarningVoiceContentActivity, "保存失败", Toast.LENGTH_SHORT).show()
|
||||
finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateWordCount(current: Int) {
|
||||
binding.tvWordCount.text = "$current/100"
|
||||
//字数达到上限时改变颜色
|
||||
|
||||
Reference in New Issue
Block a user