refactor(activity): 优化启动页跳转延迟及子设备页面稳定性,修复导航栏闪烁问题
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -45,7 +45,6 @@
|
||||
<activity
|
||||
android:name=".ui.InitActivity"
|
||||
android:exported="true"
|
||||
android:screenOrientation="portrait"
|
||||
tools:ignore="DiscouragedApi,LockedOrientationActivity">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
@@ -54,24 +53,29 @@
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity android:name=".ui.HomeActivity"
|
||||
android:theme="@style/Theme.DishMatch.NoSplash"
|
||||
android:screenOrientation="portrait"
|
||||
tools:ignore="DiscouragedApi,LockedOrientationActivity" />
|
||||
|
||||
<activity
|
||||
android:name=".ui.SelectDishActivity"
|
||||
android:theme="@style/Theme.DishMatch.NoSplash"
|
||||
android:launchMode="singleTask"
|
||||
android:screenOrientation="portrait"
|
||||
tools:ignore="DiscouragedApi,LockedOrientationActivity" />
|
||||
<activity
|
||||
android:name=".ui.PrepareCookActivity"
|
||||
android:theme="@style/Theme.DishMatch.NoSplash"
|
||||
android:screenOrientation="portrait"
|
||||
tools:ignore="DiscouragedApi,LockedOrientationActivity" />
|
||||
<activity
|
||||
android:name=".ui.DishSamplingActivity"
|
||||
android:theme="@style/Theme.DishMatch.NoSplash"
|
||||
android:screenOrientation="portrait"
|
||||
tools:ignore="DiscouragedApi,LockedOrientationActivity" />
|
||||
<activity
|
||||
android:name=".ui.SamplingListActivity"
|
||||
android:theme="@style/Theme.DishMatch.NoSplash"
|
||||
android:launchMode="singleTask"
|
||||
android:screenOrientation="portrait"
|
||||
tools:ignore="DiscouragedApi,LockedOrientationActivity" />
|
||||
@@ -86,19 +90,26 @@
|
||||
<!-- tools:ignore="DiscouragedApi,LockedOrientationActivity" />-->
|
||||
<activity
|
||||
android:name=".ui.FoodSearchActivity"
|
||||
android:theme="@style/Theme.DishMatch.NoSplash"
|
||||
android:screenOrientation="portrait"
|
||||
tools:ignore="DiscouragedApi,LockedOrientationActivity" />
|
||||
<activity android:name="com.shuwei.dish.match.ui.DeviceConfigActivity" />
|
||||
<activity android:name="com.shuwei.dish.match.ui.DeviceConfigActivity"
|
||||
android:theme="@style/Theme.DishMatch.NoSplash" />
|
||||
<activity android:name="com.shuwei.dish.match.ui.SettingActivity"
|
||||
android:theme="@style/Theme.DishMatch.NoSplash"
|
||||
android:screenOrientation="portrait"
|
||||
tools:ignore="DiscouragedApi,LockedOrientationActivity" />
|
||||
<activity android:name="com.shuwei.dish.match.ui.SubmitFoodActivity" />
|
||||
<activity android:name="com.shuwei.dish.match.ui.SubmitFoodActivity"
|
||||
android:theme="@style/Theme.DishMatch.NoSplash" />
|
||||
<activity
|
||||
android:name="com.shuwei.dish.match.ui.MasterScaleActivity"
|
||||
android:theme="@style/Theme.DishMatch.NoSplash"
|
||||
android:screenOrientation="portrait"
|
||||
tools:ignore="DiscouragedApi,LockedOrientationActivity" />
|
||||
<activity
|
||||
android:name="com.shuwei.dish.match.ui.SlaveActivity"
|
||||
android:theme="@style/Theme.DishMatch.NoSplash"
|
||||
android:launchMode="singleTask"
|
||||
android:screenOrientation="landscape"
|
||||
tools:ignore="DiscouragedApi,LockedOrientationActivity" />
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -54,6 +54,10 @@ object ScaleServiceManager {
|
||||
get() = wsServer?.onConnectionChanged
|
||||
set(value) { wsServer?.onConnectionChanged = value }
|
||||
|
||||
/** 当前是否有主设备连接(仅子设备使用) */
|
||||
val isMasterConnected: Boolean
|
||||
get() = wsServer?.isConnected == true
|
||||
|
||||
/**
|
||||
* 启动所有秤服务,在 Application.onCreate() 中调用
|
||||
*/
|
||||
|
||||
@@ -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<Int, Long>()
|
||||
|
||||
|
||||
@@ -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<SlaveActivity>()
|
||||
return
|
||||
}
|
||||
// 主设备:走原有路由逻辑
|
||||
val launchPageType = SpTool.getInt(SpTool.LAUNCH_PAGE_TYPE, -1)
|
||||
when (launchPageType) {
|
||||
0 -> {
|
||||
startActivity<SelectDishActivity>()
|
||||
lifecycleScope.launch {
|
||||
delay(2000)
|
||||
// 子设备:直接进入小屏专属页面,不走原有大屏业务流程
|
||||
if (GlobalData.deviceRole == DeviceRole.SLAVE) {
|
||||
startActivity<SlaveActivity>()
|
||||
return@launch
|
||||
}
|
||||
// 主设备:走原有路由逻辑
|
||||
val launchPageType = SpTool.getInt(SpTool.LAUNCH_PAGE_TYPE, -1)
|
||||
when (launchPageType) {
|
||||
0 -> {
|
||||
startActivity<SelectDishActivity>()
|
||||
}
|
||||
|
||||
1 -> {
|
||||
goSampling()
|
||||
}
|
||||
1 -> {
|
||||
goSampling()
|
||||
}
|
||||
|
||||
else -> {
|
||||
startActivity<HomeActivity>()
|
||||
else -> {
|
||||
startActivity<HomeActivity>()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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() {
|
||||
|
||||
@@ -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() {
|
||||
// 禁用返回键,不执行任何操作
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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<String, WeightCallback?>? =
|
||||
null
|
||||
val weightFuncMap: ConcurrentHashMap<String, WeightCallback?> = 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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- 横屏专用启动背景,适配子设备 800×480 屏幕 -->
|
||||
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<!-- 背景色铺满全屏 -->
|
||||
<item>
|
||||
<shape android:shape="rectangle">
|
||||
<solid android:color="@color/bg_color"/>
|
||||
</shape>
|
||||
</item>
|
||||
|
||||
<!-- 图片居中显示,适配横屏比例 -->
|
||||
<item
|
||||
android:width="200dp"
|
||||
android:height="190dp"
|
||||
android:gravity="center">
|
||||
<bitmap android:src="@drawable/img_init" android:scaleType="fitCenter"/>
|
||||
</item>
|
||||
|
||||
</layer-list>
|
||||
@@ -0,0 +1,115 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- 横屏 800×480 专用布局,View ID 与 activity_init.xml 完全一致,供子设备使用 -->
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="@color/bg_color">
|
||||
|
||||
<!-- 主内容区:水平居中排列加载区和网络错误区 -->
|
||||
<LinearLayout
|
||||
android:id="@+id/llNetwork"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="horizontal"
|
||||
android:gravity="center">
|
||||
|
||||
<!-- 加载动画容器 -->
|
||||
<LinearLayout
|
||||
android:id="@+id/llLoading"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:gravity="center"
|
||||
android:visibility="visible">
|
||||
|
||||
<!-- 加载动画 -->
|
||||
<ImageView
|
||||
android:id="@+id/ivLoading"
|
||||
android:layout_width="100dp"
|
||||
android:layout_height="100dp"
|
||||
android:src="@drawable/loading_spinner"
|
||||
android:contentDescription="加载中" />
|
||||
|
||||
<!-- 倒计时文字 -->
|
||||
<TextView
|
||||
android:id="@+id/tvCountdown"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="60秒"
|
||||
android:textSize="32sp"
|
||||
android:textColor="@color/black"
|
||||
android:textStyle="bold"
|
||||
android:layout_marginTop="16dp" />
|
||||
|
||||
<!-- 提示文字 -->
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="网络连接检测中,请稍后……"
|
||||
android:textSize="16sp"
|
||||
android:textColor="#555555"
|
||||
android:layout_marginTop="10dp"
|
||||
tools:ignore="HardcodedText" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<!-- 连接网络按钮容器 -->
|
||||
<LinearLayout
|
||||
android:id="@+id/llNetworkButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:gravity="center"
|
||||
android:visibility="gone">
|
||||
|
||||
<!-- 网络错误图标 -->
|
||||
<ImageView
|
||||
android:layout_width="100dp"
|
||||
android:layout_height="100dp"
|
||||
android:src="@drawable/ic_network_error"
|
||||
android:contentDescription="网络连接失败"
|
||||
android:layout_marginBottom="16dp" />
|
||||
|
||||
<!-- 连接失败提示 -->
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="网络连接失败"
|
||||
android:textSize="28sp"
|
||||
android:textColor="@color/black"
|
||||
android:textStyle="bold"
|
||||
android:layout_marginBottom="20dp"
|
||||
tools:ignore="HardcodedText" />
|
||||
|
||||
<!-- 连接网络按钮 -->
|
||||
<Button
|
||||
android:id="@+id/btnConnectNetwork"
|
||||
android:layout_width="260dp"
|
||||
android:layout_height="56dp"
|
||||
android:text="连接网络"
|
||||
android:textSize="20sp"
|
||||
android:textStyle="bold"
|
||||
android:textColor="@android:color/white"
|
||||
android:background="@drawable/btn_connect_network"
|
||||
tools:ignore="HardcodedText" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<!-- 以下 View 在此布局中不使用,保留 ID 确保 ActivityInitBinding.bind() 不崩溃 -->
|
||||
<TextView
|
||||
android:id="@+id/btnInit"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="0dp"
|
||||
android:visibility="gone" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/ivQrCode"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="0dp"
|
||||
android:visibility="gone"
|
||||
tools:ignore="ContentDescription" />
|
||||
|
||||
</FrameLayout>
|
||||
@@ -11,6 +11,8 @@
|
||||
<item name="colorOnSecondary">@color/black</item>
|
||||
<!-- Status bar color. -->
|
||||
<item name="android:statusBarColor">@android:color/transparent</item>
|
||||
<!-- 导航栏颜色与背景一致,避免启动时黑色闪烁;上滑 transient 导航栏由系统控制不受影响 -->
|
||||
<item name="android:navigationBarColor">@color/bg_color</item>
|
||||
<!-- Customize your theme here. -->
|
||||
<!-- <item name="android:layout_width">828dp</item>-->
|
||||
<item name="android:fitsSystemWindows">false</item>
|
||||
|
||||
@@ -11,6 +11,8 @@
|
||||
<item name="colorOnSecondary">@color/black</item>
|
||||
<!-- Status bar color. -->
|
||||
<item name="android:statusBarColor">@android:color/transparent</item>
|
||||
<!-- 导航栏颜色与背景一致,避免启动时黑色闪烁;上滑 transient 导航栏由系统控制不受影响 -->
|
||||
<item name="android:navigationBarColor">@color/bg_color</item>
|
||||
<!-- Customize your theme here. -->
|
||||
<!-- <item name="android:layout_width">828dp</item>-->
|
||||
<item name="android:fitsSystemWindows">false</item>
|
||||
@@ -19,6 +21,11 @@
|
||||
|
||||
</style>
|
||||
|
||||
<!-- 非启动页主题:windowBackground 为纯色,避免 bg_splash 闪烁 -->
|
||||
<style name="Theme.DishMatch.NoSplash" parent="Theme.DishMatch">
|
||||
<item name="android:windowBackground">@color/bg_color</item>
|
||||
</style>
|
||||
|
||||
<style name="CookButtonStyle" parent="Widget.MaterialComponents.Button">
|
||||
<!-- 禁用默认背景色 -->
|
||||
<item name="backgroundTint">@null</item>
|
||||
|
||||
Reference in New Issue
Block a user