249 lines
9.7 KiB
Kotlin
249 lines
9.7 KiB
Kotlin
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(), DeviceUpdateEvent.DeviceUpdateListener {
|
|
|
|
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)
|
|
binding = SettingsActivityBinding.inflate(layoutInflater)
|
|
setContentView(binding.root)
|
|
// ImmersionBar核心调用,沉浸式
|
|
ImmersionBar.with(this)
|
|
.statusBarDarkFont(true)
|
|
.titleBar(binding.topBar)
|
|
.init()
|
|
binding.itemVoiceSetting.setOnClickListener {
|
|
val intent = Intent(this, VoiceSettingsActivity::class.java)
|
|
startActivity(intent)
|
|
}
|
|
binding.ivBack.setOnClickListener {
|
|
finish()
|
|
}
|
|
|
|
binding.itemRecognitionMode.setOnClickListener {
|
|
val dialog = RecognitionModeDialog(this)
|
|
dialog.setOnConfirmListener { mode ->
|
|
updateRecognitionMode(mode)
|
|
}
|
|
dialog.show()
|
|
}
|
|
|
|
binding.itemDeviceName.setOnClickListener {
|
|
val dialog = CustomDialogFragmentText()
|
|
.setTitle(getString(R.string.device_name))
|
|
.setInputText(binding.tvDeviceName.text.toString())
|
|
dialog.setOnConfirmListener { name ->
|
|
updateDeviceName(name)
|
|
}
|
|
dialog.show(supportFragmentManager, "CustomDialogFragment")
|
|
}
|
|
|
|
binding.itemUnbindDevice.setOnClickListener {
|
|
val dialog = CustomDialogFragmentText()
|
|
.setTitle(getString(R.string.tip))
|
|
.setMessage(getString(R.string.logout_confirm))
|
|
.setNegativeText(getString(R.string.cancel))
|
|
.setPositiveText(getString(R.string.confirm))
|
|
.setShowInput(false)
|
|
dialog.setOnConfirmListener {
|
|
finish()
|
|
}
|
|
dialog.show(supportFragmentManager, "CustomDialogFragment")
|
|
}
|
|
binding.itemWaterGunSetting.setOnClickListener {
|
|
val intent = Intent(this, WaterGunSettingsActivity::class.java)
|
|
startActivity(intent)
|
|
}
|
|
binding.itemAutoMode.setOnClickListener {
|
|
val intent = Intent(this, AutoModeActivity::class.java)
|
|
startActivity(intent)
|
|
}
|
|
binding.itemDeviceInfo.setOnClickListener {
|
|
val intent = Intent(this, DeviceInfoActivity::class.java)
|
|
startActivity(intent)
|
|
}
|
|
// 关联用户
|
|
binding.itemRelatedUsers.setOnClickListener {
|
|
val intent = Intent(this, LinkedUserActivity::class.java)
|
|
intent.putExtra(LinkedUserActivity.EXTRA_MODE, LinkedUserActivity.MODE_SETTINGS)
|
|
startActivity( intent)
|
|
}
|
|
binding.itemWifiManage.setOnClickListener {
|
|
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() {
|
|
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)
|
|
}
|
|
|
|
device.workMode?.let { workMode ->
|
|
binding.tvAutoMode.text = when (workMode) {
|
|
"0" -> getString(R.string.full_auto_mode)
|
|
"1" -> getString(R.string.semi_auto_mode)
|
|
else -> getString(R.string.full_auto_mode)
|
|
}
|
|
}
|
|
}
|
|
|
|
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()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|