tj
2025-03-25 a8da38115220a0677442899ecf7bf75fd1ef325c
app/src/main/java/com/example/firstapp/MainActivity.kt
@@ -30,10 +30,14 @@
import com.example.firstapp.core.Core
import com.example.firstapp.database.entity.Code
import com.example.firstapp.database.entity.Msg
import com.example.firstapp.database.service.RetrofitClient
import com.example.firstapp.entity.Rule
import com.example.firstapp.ui.home.HomeViewModel
import com.example.firstapp.utils.Log
import com.example.firstapp.workers.KeywordUpdateWorker
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
@@ -43,6 +47,9 @@
import java.util.concurrent.TimeUnit
class MainActivity : AppCompatActivity() {
    // 安全防护关键词数组
    private var securityKeywordsList = emptyList<String>()
    private lateinit var binding: ActivityMainBinding
@@ -57,7 +64,8 @@
                    permissions.getOrDefault(Manifest.permission.READ_SMS, false) -> {
                // 两个权限都获得授权
                registerSmsReceiver()
                syncRecentSms()
//                syncRecentSms()
                initializeSecurityKeywords()
            }
            else -> {
                // 有权限被拒绝
@@ -83,14 +91,7 @@
        val navView = binding.navView
        val navController = findNavController(R.id.nav_host_fragment_activity_main)
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications
            )
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        // 只保留底部导航的设置
        navView.setupWithNavController(navController)
        // 检查权限
@@ -136,10 +137,7 @@
//        }, filter)
    }
    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.nav_host_fragment_activity_main)
        return navController.navigateUp() || super.onSupportNavigateUp()
    }
    private fun registerSmsReceiver() {
//        应用启动时执行 registerSmsReceiver()
//        创建 SmsReceiver 实例
@@ -190,7 +188,29 @@
        finish()
    }
    // 初始化禁用词
    private fun initializeSecurityKeywords() {
        CoroutineScope(Dispatchers.IO).launch {
            try {
                val response = RetrofitClient.apiService.getSecurityList()
                if (response.code == 200) {
                    securityKeywordsList = response.data.map { it.keyword }
                    android.util.Log.d("MainActivity", "securityKeywordsList: $securityKeywordsList")
                    // 确保在主线程中调用 syncRecentSms
                    runOnUiThread {
                        syncRecentSms()
                    }
                } else {
                    android.util.Log.e("MainActivity", "Failed to get security list: ${response.code}")
                }
            } catch (e: Exception) {
                android.util.Log.e("MainActivity", "Error fetching security list", e)
            }
        }
    }
    private fun syncRecentSms() {
        try {
            val calendar = Calendar.getInstance()
            calendar.add(Calendar.DAY_OF_YEAR, -3) // 获取3天前的时间
@@ -232,6 +252,12 @@
                                android.util.Log.d("SmsReceiver", "Received SMS code: ${code}")
                                val msg = Msg(0, "1111", "111111", messageBody.toString(), 1, "111", 1, 1)
                                val msgId = Core.msg.insert(msg)
                                // 禁用关键词拦截,如果有禁用词则不保存在Code里面
                                android.util.Log.d("首页SmsReceiver", "securityKeywordsList: $securityKeywordsList")
                                if (securityKeywordsList.any { it in messageBody.toString() }) {
                                    android.util.Log.d("首页SmsReceiver", "Received SMS code: 禁用关键词拦截,不保存")
                                    continue
                                }
                                val code = Code(0, rule.type, 1, rule.content, 1, 1, msgId, code, dateString, "中通",0,"","")
                                Core.code.insert(code)
                                android.util.Log.d("SMS_DEBUG", "历史短信已保存到数据库")