diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 13947e9..c6172fa 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -45,7 +45,6 @@
@@ -54,24 +53,29 @@
@@ -86,19 +90,26 @@
-
+
-
+
diff --git a/app/src/main/java/com/shuwei/dish/match/base/BaseActivity.kt b/app/src/main/java/com/shuwei/dish/match/base/BaseActivity.kt
index 8cb9e1c..fbc5be1 100644
--- a/app/src/main/java/com/shuwei/dish/match/base/BaseActivity.kt
+++ b/app/src/main/java/com/shuwei/dish/match/base/BaseActivity.kt
@@ -1,6 +1,7 @@
package com.shuwei.dish.match.base
import android.content.Intent
+import android.content.pm.ActivityInfo
import android.os.Bundle
import android.os.Handler
import android.os.Looper
@@ -43,9 +44,16 @@ open class BaseActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
+ // 提前隐藏系统栏,避免窗口创建时导航栏闪烁
+ hideSystemBars()
+ // 主设备竖屏,子设备横屏
+ requestedOrientation = if (GlobalData.deviceRole == DeviceRole.MASTER)
+ ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
+ else
+ ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
ActivityManager.addActivity(this)
binding = ActivityBaseBinding.inflate(layoutInflater)
- if (this !is InitActivity) {
+ if (this !is InitActivity || GlobalData.deviceRole == DeviceRole.SLAVE) {
binding.root.setBackgroundColor(ContextCompat.getColor(this, R.color.bg_color))
}
setContentView(binding.root)
diff --git a/app/src/main/java/com/shuwei/dish/match/scale/ScaleServiceManager.kt b/app/src/main/java/com/shuwei/dish/match/scale/ScaleServiceManager.kt
index ddf1c1a..6eb65fe 100644
--- a/app/src/main/java/com/shuwei/dish/match/scale/ScaleServiceManager.kt
+++ b/app/src/main/java/com/shuwei/dish/match/scale/ScaleServiceManager.kt
@@ -54,6 +54,10 @@ object ScaleServiceManager {
get() = wsServer?.onConnectionChanged
set(value) { wsServer?.onConnectionChanged = value }
+ /** 当前是否有主设备连接(仅子设备使用) */
+ val isMasterConnected: Boolean
+ get() = wsServer?.isConnected == true
+
/**
* 启动所有秤服务,在 Application.onCreate() 中调用
*/
diff --git a/app/src/main/java/com/shuwei/dish/match/scale/ScaleWebSocketServer.kt b/app/src/main/java/com/shuwei/dish/match/scale/ScaleWebSocketServer.kt
index d4363bb..f53289b 100644
--- a/app/src/main/java/com/shuwei/dish/match/scale/ScaleWebSocketServer.kt
+++ b/app/src/main/java/com/shuwei/dish/match/scale/ScaleWebSocketServer.kt
@@ -28,6 +28,10 @@ class ScaleWebSocketServer(private val deviceId: String, private val context: Co
/** 主设备连接状态变化回调:true=已连接,false=已断开;在主线程外调用,需自行切换线程 */
var onConnectionChanged: ((connected: Boolean) -> Unit)? = null
+ /** 当前是否有主设备连接 */
+ val isConnected: Boolean
+ get() = server?.connections?.isNotEmpty() == true
+
/** 每个秤地址的最近一次推送时间,用于节流 */
private val lastPushTime = mutableMapOf()
diff --git a/app/src/main/java/com/shuwei/dish/match/ui/InitActivity.kt b/app/src/main/java/com/shuwei/dish/match/ui/InitActivity.kt
index 7c063a7..1dcecc9 100644
--- a/app/src/main/java/com/shuwei/dish/match/ui/InitActivity.kt
+++ b/app/src/main/java/com/shuwei/dish/match/ui/InitActivity.kt
@@ -2,6 +2,7 @@ package com.shuwei.dish.match.ui
import android.annotation.SuppressLint
import android.content.Intent
+import android.util.Log
import android.graphics.Color
import android.os.Bundle
import android.os.SystemClock
@@ -26,7 +27,6 @@ import com.shuwei.dish.match.utils.ext.startActivity
import com.shuwei.dish.match.viewmodel.AppViewModel
import com.shuwei.dish.match.viewmodel.factory.AppFactory
import kotlinx.coroutines.delay
-import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch
class InitActivity : BaseActivity() {
@@ -63,8 +63,9 @@ class InitActivity : BaseActivity() {
WeightUtil.startContinuousRead()
initViews()
-
-// countDown()
+ val dpi = resources.displayMetrics.density
+ val metrics = resources.displayMetrics.toString()
+ Log.d(TAG, "dpi=$dpi, $metrics")
}
private fun initViewModel() {
@@ -74,24 +75,27 @@ class InitActivity : BaseActivity() {
}
private fun startNextPage() {
- // 子设备:直接进入小屏专属页面,不走原有大屏业务流程
- if (GlobalData.deviceRole == DeviceRole.SLAVE) {
- startActivity()
- return
- }
- // 主设备:走原有路由逻辑
- val launchPageType = SpTool.getInt(SpTool.LAUNCH_PAGE_TYPE, -1)
- when (launchPageType) {
- 0 -> {
- startActivity()
+ lifecycleScope.launch {
+ delay(2000)
+ // 子设备:直接进入小屏专属页面,不走原有大屏业务流程
+ if (GlobalData.deviceRole == DeviceRole.SLAVE) {
+ startActivity()
+ return@launch
}
+ // 主设备:走原有路由逻辑
+ val launchPageType = SpTool.getInt(SpTool.LAUNCH_PAGE_TYPE, -1)
+ when (launchPageType) {
+ 0 -> {
+ startActivity()
+ }
- 1 -> {
- goSampling()
- }
+ 1 -> {
+ goSampling()
+ }
- else -> {
- startActivity()
+ else -> {
+ startActivity()
+ }
}
}
}
@@ -117,25 +121,6 @@ class InitActivity : BaseActivity() {
}
}
- private fun countDown() {
- lifecycleScope.launch {
- flow {
- (2 downTo 1).forEach {
- delay(1000)
- emit(it)
- }
- }.collect {
- // 倒计时结束执行跳转
- if (it == 1) {
- startNextPage()
- //保留初始化页面,不关闭
-// finish()
- }
- }
- }
- }
-
-
override fun onDestroy() {
WeightUtil.stopContinuousRead()
super.onDestroy()
@@ -257,7 +242,7 @@ class InitActivity : BaseActivity() {
*/
private fun openNetworkSettings() {
val intent = Intent(android.provider.Settings.ACTION_WIFI_SETTINGS)
- startActivity(intent)
+ startActivity(intent) {}
}
private fun addBackKeyListener() {
diff --git a/app/src/main/java/com/shuwei/dish/match/ui/SlaveActivity.kt b/app/src/main/java/com/shuwei/dish/match/ui/SlaveActivity.kt
index 45e41ec..3b03a1b 100644
--- a/app/src/main/java/com/shuwei/dish/match/ui/SlaveActivity.kt
+++ b/app/src/main/java/com/shuwei/dish/match/ui/SlaveActivity.kt
@@ -6,6 +6,7 @@ import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
+import androidx.activity.OnBackPressedCallback
import com.shuwei.dish.match.base.BaseActivity
import com.shuwei.dish.match.base.GlobalData
import com.shuwei.dish.match.databinding.ActivitySlaveBinding
@@ -44,9 +45,21 @@ class SlaveActivity : BaseActivity() {
binding.rvScaleList.itemAnimator = null // 关闭默认动画,避免数据频繁更新时出现闪烁错乱
binding.rvScaleList.adapter = adapter
- updateMasterStatus(false)
+ updateMasterStatus(ScaleServiceManager.isMasterConnected)
listenLocalScales()
listenMasterConnection()
+ addBackKeyListener()
+ }
+
+ /**
+ * 禁用返回键,防止退回 InitActivity 后重复打开本页
+ */
+ private fun addBackKeyListener() {
+ onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
+ override fun handleOnBackPressed() {
+ // 禁用返回键,不执行任何操作
+ }
+ })
}
/**
diff --git a/app/src/main/java/com/shuwei/dish/match/utils/WeightUtil.kt b/app/src/main/java/com/shuwei/dish/match/utils/WeightUtil.kt
index e3633ab..8444ae2 100644
--- a/app/src/main/java/com/shuwei/dish/match/utils/WeightUtil.kt
+++ b/app/src/main/java/com/shuwei/dish/match/utils/WeightUtil.kt
@@ -7,6 +7,7 @@ import android.util.Log
import com.shuwei.dish.match.utils.ext.roundedOneDecimalPlace
//import com.aithings.Weigher
import com.wabon.wbintelligenthardwaresdk.api.SensorScale
+import java.util.concurrent.ConcurrentHashMap
typealias WeightCallback = (address: Int, state: Int, weight: Double) -> Unit
@@ -17,8 +18,7 @@ object WeightUtil {
const val STATE_OVER_WEIGHT = SensorScale.STATE_OVER_WEIGHT
var isConnected = false
- var weightFuncMap: MutableMap? =
- null
+ val weightFuncMap: ConcurrentHashMap = ConcurrentHashMap()
fun init() {
Weigher2.init()
@@ -38,7 +38,7 @@ object WeightUtil {
}
val useWeight = (weight*1000).roundedOneDecimalPlace()
Log.d(TAG, "readWeight, address=$address,state=$stateStr, weight=$useWeight")
- weightFuncMap?.forEach { (key, value) ->
+ weightFuncMap.forEach { (key, value) ->
value?.invoke(address, state, useWeight)
}
@@ -82,18 +82,15 @@ object WeightUtil {
}
}
- fun removeWeightListener(weightKey: String){
- weightFuncMap?.remove(weightKey)
+ fun removeWeightListener(weightKey: String) {
+ weightFuncMap.remove(weightKey)
}
fun addWeightListener(
weightKey: String,
getWeight: WeightCallback = { _, _, _ -> }
) {
- if (weightFuncMap == null) {
- weightFuncMap = mutableMapOf()
- }
- weightFuncMap?.put(weightKey, getWeight)
+ weightFuncMap[weightKey] = getWeight
}
}
diff --git a/app/src/main/res/drawable-land/bg_splash.xml b/app/src/main/res/drawable-land/bg_splash.xml
new file mode 100644
index 0000000..15396b0
--- /dev/null
+++ b/app/src/main/res/drawable-land/bg_splash.xml
@@ -0,0 +1,20 @@
+
+
+
+
+
+ -
+
+
+
+
+
+
+ -
+
+
+
+
diff --git a/app/src/main/res/layout-land/activity_init.xml b/app/src/main/res/layout-land/activity_init.xml
new file mode 100644
index 0000000..61954d3
--- /dev/null
+++ b/app/src/main/res/layout-land/activity_init.xml
@@ -0,0 +1,115 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/app/src/main/res/values-night/themes.xml b/app/src/main/res/values-night/themes.xml
index 8f360cb..8da37b6 100644
--- a/app/src/main/res/values-night/themes.xml
+++ b/app/src/main/res/values-night/themes.xml
@@ -11,6 +11,8 @@
- @color/black
- @android:color/transparent
+
+ - @color/bg_color
- false
diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml
index f94c44a..47d4c19 100644
--- a/app/src/main/res/values/themes.xml
+++ b/app/src/main/res/values/themes.xml
@@ -11,6 +11,8 @@
- @color/black
- @android:color/transparent
+
+ - @color/bg_color
- false
@@ -19,6 +21,11 @@
+
+
+