package com.sw.scalefusion.shelf import android.content.Intent import android.graphics.Color import android.os.Bundle import android.os.Handler import android.os.Looper import android.os.SystemClock import android.util.Log import android.view.animation.RotateAnimation import androidx.activity.addCallback import androidx.core.graphics.toColorInt import com.shuwei.intelligent.shelves.App import com.shuwei.intelligent.shelves.GlobalData import com.shuwei.intelligent.shelves.base.BaseActivity import com.shuwei.intelligent.shelves.databinding.ActivityInitBinding import com.shuwei.intelligent.shelves.utils.AppUtil import com.shuwei.intelligent.shelves.utils.NetworkUtils import com.shuwei.intelligent.shelves.utils.SpTool import com.shuwei.intelligent.shelves.utils.ext.invisible import com.shuwei.intelligent.shelves.utils.ext.startActivity class InitActivity : BaseActivity() { companion object { const val TAG = "InitActivity" } private lateinit var binding: ActivityInitBinding private val handler = Handler(Looper.getMainLooper()) private var startTime = 0L private var lastNetworkCheckTime = 0L override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityInitBinding.inflate(layoutInflater) setContentView(binding.root) App.deviceId = AppUtil.getUDID(this) // TODO: 测试设备号 // App.deviceId = "d369f9b0-1c5b-3066-ba72-988e1449cecc" // App.deviceId = "25b777a9-6298-30d3-a5f8-3b3eef2a50f7" // App.deviceId = "505ce1b1-facc-3eb2-855a-3dadda5ba358" Log.d(TAG, "onCreate: deviceId = ${App.deviceId}") SpTool.put(SpTool.DEVICE_ID, App.deviceId) // val appBaseUrl = SpTool.baseUrl // if (!appBaseUrl.isBlank()) { // GlobalData.appBaseUrl = appBaseUrl // } else { // GlobalData.appBaseUrl = GlobalData.PROD_BASE_URL // } GlobalData.appBaseUrl = GlobalData.TEST_BASE_URL App.canteenId = "0" binding.ivQrCode.invisible() binding.btnInit.invisible() initViews() // 禁用返回键 onBackPressedDispatcher.addCallback(this) {} } /** * 初始化控件事件 */ private fun initViews() { binding.btnConnectNetwork.setOnClickListener { openNetworkSettings() } startLoadingAnimation() } /** * 启动加载动画 - ImageView 持续旋转 */ private fun startLoadingAnimation() { val rotateAnimation = RotateAnimation( 0f, 360f, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f ).apply { duration = 1200 repeatCount = RotateAnimation.INFINITE repeatMode = RotateAnimation.RESTART } binding.ivLoading.startAnimation(rotateAnimation) } override fun onResume() { super.onResume() if (NetworkUtils.isNetworkConnected(this)) { navigateToHome() return } startCountdown() } override fun onPause() { super.onPause() handler.removeCallbacksAndMessages(null) } /** * 启动 60 秒倒计时 */ private fun startCountdown() { startTime = SystemClock.elapsedRealtime() lastNetworkCheckTime = 0 binding.llNetwork.setBackgroundColor(Color.TRANSPARENT) binding.llLoading.visibility = android.view.View.VISIBLE binding.llNetworkButton.visibility = android.view.View.GONE binding.tvCountdown.text = "60秒" scheduleCountdown() } /** * 定时更新倒计时 */ private fun scheduleCountdown() { handler.postDelayed({ val elapsedTime = SystemClock.elapsedRealtime() - startTime val remainingTime = 60 - (elapsedTime / 1000).toInt() val currentNetworkCheckTime = (elapsedTime / 1000).toInt() if (currentNetworkCheckTime > 0 && currentNetworkCheckTime % 10 == 0 && currentNetworkCheckTime != lastNetworkCheckTime.toInt()) { lastNetworkCheckTime = currentNetworkCheckTime.toLong() checkNetworkConnection() } if (remainingTime > 0) { binding.tvCountdown.text = "${remainingTime}秒" scheduleCountdown() } else { if (NetworkUtils.isNetworkConnected(this)) { navigateToHome() } else { showNetworkButton() } } }, 100) } /** * 检测网络连接 */ private fun checkNetworkConnection() { if (NetworkUtils.isNetworkConnected(this)) { handler.removeCallbacksAndMessages(null) navigateToHome() } } /** * 跳转到 HomeV3Activity */ private fun navigateToHome() { startActivity() finish() } /** * 显示"连接网络"按钮 */ private fun showNetworkButton() { binding.llNetwork.setBackgroundColor("#5C77F7".toColorInt()) binding.llLoading.visibility = android.view.View.GONE binding.llNetworkButton.visibility = android.view.View.VISIBLE } /** * 打开系统网络设置页面 */ private fun openNetworkSettings() { val intent = Intent(android.provider.Settings.ACTION_WIFI_SETTINGS) startActivity(intent) } }