自动模式
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package com.example.defenseapplication.home
|
package com.example.defenseapplication.home
|
||||||
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.util.Log
|
||||||
import androidx.activity.enableEdgeToEdge
|
import androidx.activity.enableEdgeToEdge
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.core.view.ViewCompat
|
import androidx.core.view.ViewCompat
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package com.example.defenseapplication.liveVideo
|
package com.example.defenseapplication.liveVideo
|
||||||
|
|
||||||
import TimePickerFragment
|
import android.app.TimePickerDialog
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
@@ -16,18 +16,14 @@ import kotlinx.coroutines.CoroutineScope
|
|||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import java.util.Calendar
|
import java.text.SimpleDateFormat
|
||||||
|
import java.util.Locale
|
||||||
|
|
||||||
//自动模式
|
//自动模式
|
||||||
class AutoModeActivity : AppCompatActivity() {
|
class AutoModeActivity : AppCompatActivity() {
|
||||||
|
|
||||||
private lateinit var binding: AutoModeActivityBinding
|
private lateinit var binding: AutoModeActivityBinding
|
||||||
private lateinit var calendar: Calendar
|
|
||||||
private var selectedMode = "0"
|
private var selectedMode = "0"
|
||||||
private var selectedYear = 0
|
|
||||||
private var selectedMonth = 0
|
|
||||||
private var selectedDay = 0
|
|
||||||
private var selectedHour = 0
|
|
||||||
private var selectedMinute = 0
|
|
||||||
private var selectedStartHour = 21
|
private var selectedStartHour = 21
|
||||||
private var selectedStartMinute = 30
|
private var selectedStartMinute = 30
|
||||||
private var selectedEndHour = 8
|
private var selectedEndHour = 8
|
||||||
@@ -43,28 +39,21 @@ class AutoModeActivity : AppCompatActivity() {
|
|||||||
.titleBar(binding.topBar)
|
.titleBar(binding.topBar)
|
||||||
.init()
|
.init()
|
||||||
|
|
||||||
calendar = Calendar.getInstance()
|
|
||||||
selectedYear = calendar.get(Calendar.YEAR)
|
|
||||||
selectedMonth = calendar.get(Calendar.MONTH)
|
|
||||||
selectedDay = calendar.get(Calendar.DAY_OF_MONTH)
|
|
||||||
|
|
||||||
initListener()
|
initListener()
|
||||||
|
|
||||||
loadCurrentSettings()
|
loadCurrentSettings()
|
||||||
|
|
||||||
showAutoModeNoticeDialog()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun initListener() {
|
private fun initListener() {
|
||||||
binding.ivBack.setOnClickListener {
|
binding.ivBack.setOnClickListener {
|
||||||
finish()
|
saveAutoModeSettings()
|
||||||
}
|
}
|
||||||
|
|
||||||
binding.rbAutoMode.setOnCheckedChangeListener { _, isChecked ->
|
binding.rbAutoMode.setOnCheckedChangeListener { _, isChecked ->
|
||||||
if (isChecked) {
|
if (isChecked) {
|
||||||
selectedMode = "0"
|
selectedMode = "0"
|
||||||
showAutoModeNoticeDialog()
|
|
||||||
binding.time.visibility = android.view.View.VISIBLE
|
binding.time.visibility = android.view.View.VISIBLE
|
||||||
|
showAutoModeNoticeDialog()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,19 +65,19 @@ class AutoModeActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
binding.itemStartTime.setOnClickListener {
|
binding.itemStartTime.setOnClickListener {
|
||||||
showDateTimePicker { year, month, day, hour, minute ->
|
showTimePicker(true) { hour, minute ->
|
||||||
selectedStartHour = hour
|
selectedStartHour = hour
|
||||||
selectedStartMinute = minute
|
selectedStartMinute = minute
|
||||||
val startTimeStr = String.format("%04d-%02d-%02d %02d:%02d", year, month + 1, day, hour, minute)
|
val startTimeStr = String.format("%02d:%02d", hour, minute)
|
||||||
binding.tvStartTime.text = startTimeStr
|
binding.tvStartTime.text = startTimeStr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
binding.itemEndTime.setOnClickListener {
|
binding.itemEndTime.setOnClickListener {
|
||||||
showDateTimePicker { year, month, day, hour, minute ->
|
showTimePicker(false) { hour, minute ->
|
||||||
selectedEndHour = hour
|
selectedEndHour = hour
|
||||||
selectedEndMinute = minute
|
selectedEndMinute = minute
|
||||||
val endTimeStr = String.format("%04d-%02d-%02d %02d:%02d", year, month + 1, day, hour, minute)
|
val endTimeStr = String.format("%02d:%02d", hour, minute)
|
||||||
binding.tvEndTime.text = endTimeStr
|
binding.tvEndTime.text = endTimeStr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -117,30 +106,8 @@ class AutoModeActivity : AppCompatActivity() {
|
|||||||
binding.time.visibility = android.view.View.GONE
|
binding.time.visibility = android.view.View.GONE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
response.data.startTime?.let { startTime ->
|
binding.tvStartTime.text = response.data.startTime?.formatToHourMinute()
|
||||||
binding.tvStartTime.text = if (startTime.length >= 5) {
|
binding.tvEndTime.text=response.data.endTime?.formatToHourMinute()
|
||||||
if (startTime.contains("-")) {
|
|
||||||
startTime
|
|
||||||
} else {
|
|
||||||
String.format("%04d-%02d-%02d %s", selectedYear, selectedMonth + 1, selectedDay, startTime)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
String.format("%04d-%02d-%02d %02d:%02d", selectedYear, selectedMonth + 1, selectedDay, 21, 30)
|
|
||||||
}
|
|
||||||
parseDateTime(startTime, true)
|
|
||||||
}
|
|
||||||
response.data.endTime?.let { endTime ->
|
|
||||||
binding.tvEndTime.text = if (endTime.length >= 5) {
|
|
||||||
if (endTime.contains("-")) {
|
|
||||||
endTime
|
|
||||||
} else {
|
|
||||||
String.format("%04d-%02d-%02d %s", selectedYear, selectedMonth + 1, selectedDay, endTime)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
String.format("%04d-%02d-%02d %02d:%02d", selectedYear, selectedMonth + 1, selectedDay, 8, 30)
|
|
||||||
}
|
|
||||||
parseDateTime(endTime, false)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
@@ -148,60 +115,34 @@ class AutoModeActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
private fun String.formatToHourMinute(): String {
|
||||||
private fun showTimePicker(onTimeSelected: (hour: Int, minute: Int) -> Unit) {
|
return try {
|
||||||
val timePickerFragment = TimePickerFragment { hour, minute ->
|
val inputFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
|
||||||
onTimeSelected(hour, minute)
|
val outputFormat = SimpleDateFormat("HH:mm", Locale.getDefault())
|
||||||
|
val date = inputFormat.parse(this)
|
||||||
|
outputFormat.format(date)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
this // 解析失败返回原字符串
|
||||||
}
|
}
|
||||||
timePickerFragment.show(supportFragmentManager, "timePicker")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun showDateTimePicker(onDateTimeSelected: (year: Int, month: Int, day: Int, hour: Int, minute: Int) -> Unit) {
|
private fun showTimePicker(isStartTime: Boolean, onTimeSelected: (hour: Int, minute: Int) -> Unit) {
|
||||||
|
val currentHour = if (isStartTime) selectedStartHour else selectedEndHour
|
||||||
|
val currentMinute = if (isStartTime) selectedStartMinute else selectedEndMinute
|
||||||
|
|
||||||
}
|
TimePickerDialog(
|
||||||
|
this,
|
||||||
private fun parseDateTime(dateTimeStr: String, isStartTime: Boolean) {
|
{ _, hour, minute ->
|
||||||
if (dateTimeStr.contains("-") && dateTimeStr.contains(" ")) {
|
onTimeSelected(hour, minute)
|
||||||
val dateTimeParts = dateTimeStr.split(" ")
|
},
|
||||||
if (dateTimeParts.size == 2) {
|
currentHour,
|
||||||
val dateParts = dateTimeParts[0].split("-")
|
currentMinute,
|
||||||
val timeParts = dateTimeParts[1].split(":")
|
true
|
||||||
if (dateParts.size == 3 && timeParts.size == 2) {
|
).show()
|
||||||
if (isStartTime) {
|
|
||||||
selectedStartHour = timeParts[0].toIntOrNull() ?: 21
|
|
||||||
selectedStartMinute = timeParts[1].toIntOrNull() ?: 30
|
|
||||||
} else {
|
|
||||||
selectedEndHour = timeParts[0].toIntOrNull() ?: 8
|
|
||||||
selectedEndMinute = timeParts[1].toIntOrNull() ?: 30
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (dateTimeStr.contains(":")) {
|
|
||||||
val timeParts = dateTimeStr.split(":")
|
|
||||||
if (timeParts.size == 2) {
|
|
||||||
if (isStartTime) {
|
|
||||||
selectedStartHour = timeParts[0].toIntOrNull() ?: 21
|
|
||||||
selectedStartMinute = timeParts[1].toIntOrNull() ?: 30
|
|
||||||
} else {
|
|
||||||
selectedEndHour = timeParts[0].toIntOrNull() ?: 8
|
|
||||||
selectedEndMinute = timeParts[1].toIntOrNull() ?: 30
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun showAutoModeNoticeDialog() {
|
private fun showAutoModeNoticeDialog() {
|
||||||
val dialog = AutoModeNoticeDialog(this)
|
val dialog = AutoModeNoticeDialog(this)
|
||||||
dialog.setOnConfirmListener { year, month, day, hour, minute ->
|
|
||||||
selectedYear = year
|
|
||||||
selectedMonth = month
|
|
||||||
selectedDay = day
|
|
||||||
selectedHour = hour
|
|
||||||
selectedMinute = minute
|
|
||||||
saveAutoModeSettings()
|
|
||||||
}
|
|
||||||
dialog.show()
|
dialog.show()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -209,12 +150,12 @@ class AutoModeActivity : AppCompatActivity() {
|
|||||||
val deviceId = DeviceIdEvent.getDeviceId()
|
val deviceId = DeviceIdEvent.getDeviceId()
|
||||||
if (deviceId.isNullOrEmpty()) {
|
if (deviceId.isNullOrEmpty()) {
|
||||||
Toast.makeText(this, "设备ID为空", Toast.LENGTH_SHORT).show()
|
Toast.makeText(this, "设备ID为空", Toast.LENGTH_SHORT).show()
|
||||||
|
finish()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val startTime = String.format("%04d-%02d-%02d %02d:%02d", selectedYear, selectedMonth + 1, selectedDay, selectedStartHour, selectedStartMinute)
|
val startTime = binding.tvStartTime.text.toString()
|
||||||
val endTime = String.format("%04d-%02d-%02d %02d:%02d", selectedYear, selectedMonth + 1, selectedDay, selectedEndHour, selectedEndMinute)
|
val endTime = binding.tvEndTime.text.toString()
|
||||||
val effectiveTime = String.format("%04d-%02d-%02d %02d:%02d", selectedYear, selectedMonth + 1, selectedDay, selectedHour, selectedMinute)
|
|
||||||
|
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
try {
|
try {
|
||||||
@@ -235,18 +176,19 @@ class AutoModeActivity : AppCompatActivity() {
|
|||||||
val resultIntent = Intent()
|
val resultIntent = Intent()
|
||||||
resultIntent.putExtra("auto_mode", modeText)
|
resultIntent.putExtra("auto_mode", modeText)
|
||||||
resultIntent.putExtra("auto_mode_value", selectedMode)
|
resultIntent.putExtra("auto_mode_value", selectedMode)
|
||||||
resultIntent.putExtra("effective_time", effectiveTime)
|
|
||||||
setResult(RESULT_OK, resultIntent)
|
setResult(RESULT_OK, resultIntent)
|
||||||
Toast.makeText(this@AutoModeActivity, "修改成功,生效时间:$effectiveTime", Toast.LENGTH_SHORT).show()
|
Toast.makeText(this@AutoModeActivity, "修改成功", Toast.LENGTH_SHORT).show()
|
||||||
DeviceUpdateEvent.notifyDeviceUpdated(deviceId)
|
DeviceUpdateEvent.notifyDeviceUpdated(deviceId)
|
||||||
} else {
|
} else {
|
||||||
Toast.makeText(this@AutoModeActivity, response.msg, Toast.LENGTH_SHORT).show()
|
Toast.makeText(this@AutoModeActivity, response.msg, Toast.LENGTH_SHORT).show()
|
||||||
}
|
}
|
||||||
|
finish()
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
Toast.makeText(this@AutoModeActivity, "网络请求失败", Toast.LENGTH_SHORT).show()
|
Toast.makeText(this@AutoModeActivity, "网络请求失败", Toast.LENGTH_SHORT).show()
|
||||||
|
finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ import kotlinx.coroutines.Dispatchers
|
|||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
//设置页面
|
//设置页面
|
||||||
class SettingsActivity : BaseActivity() {
|
class SettingsActivity : BaseActivity(), DeviceUpdateEvent.DeviceUpdateListener {
|
||||||
|
|
||||||
private lateinit var binding: SettingsActivityBinding
|
private lateinit var binding: SettingsActivityBinding
|
||||||
private val SP_NAME = "DeviceSettings"
|
private val SP_NAME = "DeviceSettings"
|
||||||
@@ -96,6 +96,16 @@ class SettingsActivity : BaseActivity() {
|
|||||||
startActivity(intent)
|
startActivity(intent)
|
||||||
}
|
}
|
||||||
fetchDeviceInfo()
|
fetchDeviceInfo()
|
||||||
|
DeviceUpdateEvent.addListener(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
super.onDestroy()
|
||||||
|
DeviceUpdateEvent.removeListener(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDeviceUpdated(deviceId: String) {
|
||||||
|
fetchDeviceInfo()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun fetchDeviceInfo() {
|
private fun fetchDeviceInfo() {
|
||||||
@@ -144,6 +154,14 @@ class SettingsActivity : BaseActivity() {
|
|||||||
!cacheMode.isNullOrEmpty() -> cacheMode
|
!cacheMode.isNullOrEmpty() -> cacheMode
|
||||||
else -> getString(R.string.bluetooth_recognition)
|
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? {
|
private fun getCachedRecognitionMode(): String? {
|
||||||
|
|||||||
@@ -71,10 +71,11 @@
|
|||||||
android:background="@drawable/bg_bottom_sheet_rounded"
|
android:background="@drawable/bg_bottom_sheet_rounded"
|
||||||
android:button="@drawable/radio_button_selector"
|
android:button="@drawable/radio_button_selector"
|
||||||
android:checked="true"
|
android:checked="true"
|
||||||
android:layout_marginStart="4dp"
|
android:layout_marginStart="8dp"
|
||||||
android:gravity="center"
|
android:gravity="center"
|
||||||
android:text="@string/full_auto_mode"
|
android:text="@string/full_auto_mode"
|
||||||
android:textColor="@color/black"
|
android:textColor="@color/black"
|
||||||
|
android:paddingVertical="16dp"
|
||||||
android:textSize="14sp" />
|
android:textSize="14sp" />
|
||||||
|
|
||||||
<android.widget.RadioButton
|
<android.widget.RadioButton
|
||||||
@@ -87,6 +88,7 @@
|
|||||||
android:gravity="center"
|
android:gravity="center"
|
||||||
android:text="@string/semi_auto_mode"
|
android:text="@string/semi_auto_mode"
|
||||||
android:textColor="@color/black"
|
android:textColor="@color/black"
|
||||||
|
android:paddingVertical="16dp"
|
||||||
android:textSize="14sp" />
|
android:textSize="14sp" />
|
||||||
|
|
||||||
</RadioGroup>
|
</RadioGroup>
|
||||||
@@ -130,7 +132,6 @@
|
|||||||
android:id="@+id/tvStartTime"
|
android:id="@+id/tvStartTime"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="21:30"
|
|
||||||
android:textColor="@color/gray"
|
android:textColor="@color/gray"
|
||||||
android:textSize="14sp" />
|
android:textSize="14sp" />
|
||||||
|
|
||||||
@@ -175,7 +176,6 @@
|
|||||||
android:id="@+id/tvEndTime"
|
android:id="@+id/tvEndTime"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="08:30"
|
|
||||||
android:textColor="@color/gray"
|
android:textColor="@color/gray"
|
||||||
android:textSize="14sp" />
|
android:textSize="14sp" />
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user