From be5829165f7cd49216971d22e39b8eae16fad915 Mon Sep 17 00:00:00 2001 From: tj <1378534974@qq.com> Date: 星期二, 25 三月 2025 14:06:19 +0800 Subject: [PATCH] 1.阿里支付 --- app/src/main/java/com/example/firstapp/MainActivity.kt | 52 +++++++++++++++++++++++++++++++++++++++------------- 1 files changed, 39 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/example/firstapp/MainActivity.kt b/app/src/main/java/com/example/firstapp/MainActivity.kt index 52a8e13..7d01490 100644 --- a/app/src/main/java/com/example/firstapp/MainActivity.kt +++ b/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", "历史短信已保存到数据库") -- Gitblit v1.9.3