tj
2025-03-20 90b7acecef96a7b4927301847efbe11a1f0336c2
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 -> {
                // 有权限被拒绝
@@ -190,7 +198,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 +262,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", "历史短信已保存到数据库")