From 9755d9eefb12f95fa45c785e526038e10c9c7115 Mon Sep 17 00:00:00 2001
From: cloudroam <cloudroam>
Date: 星期五, 25 四月 2025 18:15:51 +0800
Subject: [PATCH] 短信fix

---
 app/src/main/java/com/example/firstapp/MainActivity.kt |  300 ++++++++++++++---------------------------------------------
 1 files changed, 71 insertions(+), 229 deletions(-)

diff --git a/app/src/main/java/com/example/firstapp/MainActivity.kt b/app/src/main/java/com/example/firstapp/MainActivity.kt
index 585212b..f0be7b9 100644
--- a/app/src/main/java/com/example/firstapp/MainActivity.kt
+++ b/app/src/main/java/com/example/firstapp/MainActivity.kt
@@ -33,6 +33,7 @@
 import java.util.Date
 import java.util.Locale
 import android.app.AlertDialog
+import com.example.firstapp.utils.CodeUtils
 
 class MainActivity : AppCompatActivity() {
     // 安全防护关键词数组
@@ -64,9 +65,9 @@
                 }
             }
         }
-        
+
     // 通知权限请求
-    private val notificationPermissionRequest = 
+    private val notificationPermissionRequest =
         registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
             if (isGranted) {
                 // 权限已授予
@@ -96,25 +97,25 @@
 
         // 重置提醒计划并检查是否有错过的提醒
         resetReminders()
-        
+
         // 开始权限请求流程
         startPermissionsFlow()
     }
 
     override fun onResume() {
         super.onResume()
-        
+
         // 每次恢复活动时检查短信权限
         checkAndHandleSmsPermissions(showDialog = false)
     }
-    
+
     // 权限请求主流程
     private fun startPermissionsFlow() {
         // 先请求通知权限,再请求短信权限
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
             // Android 13+ 需要通知权限
             if (ContextCompat.checkSelfPermission(
-                    this, 
+                    this,
                     Manifest.permission.POST_NOTIFICATIONS
                 ) != PackageManager.PERMISSION_GRANTED
             ) {
@@ -128,7 +129,7 @@
             requestSmsPermissions()
         }
     }
-    
+
     // 请求短信权限
     private fun requestSmsPermissions() {
         if (!hasSmsPermissions()) {
@@ -147,7 +148,7 @@
             }
         }
     }
-    
+
     // 检查并处理短信权限(可选是否显示对话框)
     private fun checkAndHandleSmsPermissions(showDialog: Boolean = true) {
         if (!hasSmsPermissions()) {
@@ -161,17 +162,17 @@
             }
         }
     }
-    
+
     // 检查是否有短信权限
     private fun hasSmsPermissions(): Boolean {
         return ContextCompat.checkSelfPermission(
             this, Manifest.permission.RECEIVE_SMS
-        ) == PackageManager.PERMISSION_GRANTED && 
-        ContextCompat.checkSelfPermission(
-            this, Manifest.permission.READ_SMS
-        ) == PackageManager.PERMISSION_GRANTED
+        ) == PackageManager.PERMISSION_GRANTED &&
+                ContextCompat.checkSelfPermission(
+                    this, Manifest.permission.READ_SMS
+                ) == PackageManager.PERMISSION_GRANTED
     }
-    
+
     // 显示短信权限解释对话框
     private fun showSmsPermissionExplanationDialog() {
         AlertDialog.Builder(this)
@@ -187,13 +188,13 @@
             .setCancelable(false)
             .show()
     }
-    
+
     // 提供给外部调用的请求通知权限方法
     fun requestNotificationPermission() {
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
             // 判断是否已经有通知权限
             if (ContextCompat.checkSelfPermission(
-                    this, 
+                    this,
                     Manifest.permission.POST_NOTIFICATIONS
                 ) != PackageManager.PERMISSION_GRANTED
             ) {
@@ -210,7 +211,7 @@
     private fun registerSmsReceiver() {
         // 确保不重复注册
         if (smsReceiver != null) return
-        
+
         Log.d("SMS_DEBUG", "MainActivity注册短信接收器")
         smsReceiver = SmsReceiver()
         val filter = IntentFilter(Telephony.Sms.Intents.SMS_RECEIVED_ACTION)
@@ -286,12 +287,6 @@
                     val msg = Msg(0, "1111", "111111", messageBody, 1, "111", 1, 1)
                     val msgId = Core.msg.insert(msg)
 
-                    // 禁用关键词拦截
-//                    if (securityKeywordsList.any { it in messageBody }) {
-//                        android.util.Log.d("MainActivity", "历史短信含有禁用关键词,跳过处理")
-//                        continue
-//                    }
-
                     // 使用协程处理API调用和数据库操作
                     CoroutineScope(Dispatchers.IO).launch {
                         try {
@@ -302,224 +297,71 @@
                                 if (response.status == "success") {
                                     when (response.data.category) {
                                         "快递" -> {
-                                            val pickupCode = response.data.details.pickupCode ?: ""
-                                            if (pickupCode.isNotEmpty()) {
-                                                val existingCode =
-                                                    Core.code.queryByTypeAndCodeAndDate(
-                                                        response.data.category,
-                                                        pickupCode,
-                                                        dateString
-                                                    )
-
-                                                if (existingCode == null) {
-                                                    val code = Code(
-                                                        id = 0,
-                                                        category = response.data.category,
-                                                        categoryId = 1,
-                                                        typeId = 1,
-                                                        ruleId = 1,
-                                                        msgId = msgId,
-                                                        createTime = dateString,
-                                                        oneLevel = response.data.details.post ?: "",
-                                                        secondLevel = response.data.details.company
-                                                            ?: "未知",
-                                                        code = pickupCode,
-                                                        pickup = 0,
-                                                        pickupTime = "",
-                                                        overTime = "",
-                                                        address = response.data.details.address
-                                                            ?: "",
-                                                        remarks = response.data.details.time ?: "",
-                                                    )
-//                                                    if(code.oneLevel.isNotEmpty() && code.secondLevel.isNotEmpty() && code.code.isNotEmpty()) {
-                                                    if (code.oneLevel.isNotEmpty() && code.code.isNotEmpty()) {
-                                                        Core.code.insert(code)
-                                                        android.util.Log.d(
-                                                            "MainActivity",
-                                                            "历史快递短信已保存: $pickupCode"
-                                                        )
-                                                    }
-                                                } else {
-                                                    android.util.Log.d(
-                                                        "MainActivity",
-                                                        "发现重复快递短信,跳过保存: $pickupCode"
-                                                    )
-                                                }
-                                            }
+                                            val code = CodeUtils.createExpressCode(
+                                                msgId = msgId,
+                                                createTime = dateString,
+                                                post = response.data.details.post,
+                                                company = response.data.details.company,
+                                                pickupCode = response.data.details.pickupCode,
+                                                address = response.data.details.address,
+                                                time = response.data.details.time
+                                            )
+                                            CodeUtils.saveCode(code)
                                         }
 
                                         "还款" -> {
-                                            val amount = response.data.details.amount ?: ""
-                                            if (amount.isNotEmpty()) {
-                                                val existingCode =
-                                                    Core.code.queryByTypeAndCodeAndDate(
-                                                        response.data.category, amount, dateString
-                                                    )
-
-                                                if (existingCode == null) {
-                                                    val code = Code(
-                                                        id = 0,
-                                                        category = response.data.category,
-                                                        categoryId = 2,
-                                                        typeId = 1,
-                                                        ruleId = 2,
-                                                        msgId = msgId,
-                                                        createTime = dateString,
-                                                        oneLevel = response.data.details.type ?: "",
-                                                        secondLevel = response.data.details.bank
-                                                            ?: "未知",
-                                                        code = amount,
-                                                        pickup = 0,
-                                                        pickupTime = "",
-                                                        overTime = response.data.details.date ?: "",
-                                                        address = response.data.details.address
-                                                            ?: "",
-                                                        remarks = "最小还款金额${response.data.details.min_amount}还款卡号${response.data.details.number}"
-                                                    )
-                                                    if (code.oneLevel.isNotEmpty() && code.secondLevel.isNotEmpty() && code.code.isNotEmpty()) {
-                                                        Core.code.insert(code)
-                                                        android.util.Log.d(
-                                                            "MainActivity",
-                                                            "历史还款短信已保存: $amount"
-                                                        )
-                                                    }
-                                                } else {
-                                                    android.util.Log.d(
-                                                        "MainActivity",
-                                                        "发现重复还款短信,跳过保存: $amount"
-                                                    )
-                                                }
-                                            }
+                                            val code = CodeUtils.createRepaymentCode(
+                                                msgId = msgId,
+                                                createTime = dateString,
+                                                type = response.data.details.type,
+                                                bank = response.data.details.bank,
+                                                amount = response.data.details.amount,
+                                                date = response.data.details.date,
+                                                address = response.data.details.address,
+                                                minAmount = response.data.details.min_amount,
+                                                number = response.data.details.number
+                                            )
+                                            CodeUtils.saveCode(code)
                                         }
 
                                         "收入" -> {
-                                            val amount = response.data.details.amount ?: ""
-                                            if (amount.isNotEmpty()) {
-                                                val existingCode =
-                                                    Core.code.queryByTypeAndCodeAndDate(
-                                                        response.data.category, amount, dateString
-                                                    )
-
-                                                if (existingCode == null) {
-                                                    val code = Code(
-                                                        id = 0,
-                                                        category = response.data.category,
-                                                        categoryId = 3, // 3-收入类型
-                                                        typeId = 1,     //暂时没有根据type分类
-                                                        ruleId = 2,     //1-还款类型
-                                                        msgId = msgId,
-                                                        createTime = dateString,
-                                                        oneLevel = response.data.details.bank ?: "",
-                                                        secondLevel = response.data.details.bank
-                                                            ?: "未知",
-                                                        code = amount,
-                                                        pickup = 0, // 0-未取件,1-已取件
-                                                        pickupTime = "", // 取件时间为空
-                                                        overTime = response.data.details.datetime
-                                                            ?: "",  // 超时时间为空,暂时没有这块处理逻辑
-                                                        address = response.data.details.address
-                                                            ?: "",
-                                                        remarks = "余额" + response.data.details.balance
-                                                            ?: "",
-                                                    )
-                                                    if (code.oneLevel.isNotEmpty() && code.secondLevel.isNotEmpty() && code.code.isNotEmpty()) {
-                                                        Core.code.insert(code)
-                                                        android.util.Log.d(
-                                                            "MainActivity",
-                                                            "历史还款短信已保存: $amount"
-                                                        )
-                                                    }
-                                                } else {
-                                                    android.util.Log.d(
-                                                        "MainActivity",
-                                                        "发现重复还款短信,跳过保存: $amount"
-                                                    )
-                                                }
-                                            }
+                                            val code = CodeUtils.createIncomeCode(
+                                                msgId = msgId,
+                                                createTime = dateString,
+                                                bank = response.data.details.bank,
+                                                amount = response.data.details.amount,
+                                                datetime = response.data.details.datetime,
+                                                address = response.data.details.address,
+                                                balance = response.data.details.balance
+                                            )
+                                            CodeUtils.saveCode(code)
                                         }
 
                                         "航班" -> {
-                                            val flight = response.data.details.flight ?: ""
-                                            if (flight.isNotEmpty()) {
-                                                val existingCode =
-                                                    Core.code.queryByTypeAndCodeAndDate(
-                                                        response.data.category, flight, dateString
-                                                    )
-
-                                                if (existingCode == null) {
-                                                    val code = Code(
-                                                        id = 0,
-                                                        category = response.data.category,
-                                                        categoryId = 4, // 4-航班类型
-                                                        typeId = 1,     //暂时没有根据type分类
-                                                        ruleId = 2,     //1-还款类型
-                                                        msgId = msgId,
-                                                        createTime = dateString,
-                                                        oneLevel = response.data.details.company
-                                                            ?: "未知",
-                                                        secondLevel = response.data.details.start + response.data.details.end
-                                                            ?: "",
-                                                        code = flight,
-                                                        pickup = 0, // 0-未取件,1-已取件
-                                                        pickupTime = "", // 取件时间为空
-                                                        overTime = response.data.details.time
-                                                            ?: "",  // 超时时间为空,暂时没有这块处理逻辑
-                                                        address = response.data.details.address
-                                                            ?: "",
-                                                        remarks = response.data.details.seat ?: "",
-                                                    )
-                                                    if (code.oneLevel != "" && code.secondLevel != "" && code.code != "") {
-                                                        Core.code.insert(code)
-                                                    }
-                                                } else {
-                                                    android.util.Log.d(
-                                                        "MainActivity",
-                                                        "发现重复还款短信,跳过保存: $flight"
-                                                    )
-                                                }
-                                            }
+                                            val code = CodeUtils.createFlightCode(
+                                                msgId = msgId,
+                                                createTime = dateString,
+                                                company = response.data.details.company,
+                                                start = response.data.details.start,
+                                                end = response.data.details.end,
+                                                seat = response.data.details.seat,
+                                                time = response.data.details.time,
+                                                address = response.data.details.address
+                                            )
+                                            CodeUtils.saveCode(code)
                                         }
 
                                         "火车票" -> {
-                                            val seat = response.data.details.seat ?: ""
-                                            if (seat.isNotEmpty()) {
-                                                val existingCode =
-                                                    Core.code.queryByTypeAndCodeAndDate(
-                                                        response.data.category, seat, dateString
-                                                    )
-
-                                                if (existingCode == null) {
-                                                    val code = Code(
-                                                        id = 0,
-                                                        category = response.data.category,
-                                                        categoryId = 5, // 5-火车票类型
-                                                        typeId = 1,     //暂时没有根据type分类
-                                                        ruleId = 2,     //1-还款类型
-                                                        msgId = msgId,
-                                                        createTime = dateString,
-                                                        oneLevel = response.data.details.company
-                                                            ?: "",
-                                                        secondLevel = response.data.details.company
-                                                            ?: "未知",
-                                                        code = seat,
-                                                        pickup = 0, // 0-未取件,1-已取件
-                                                        pickupTime = "", // 取件时间为空
-                                                        overTime = response.data.details.time
-                                                            ?: "",  // 超时时间为空,暂时没有这块处理逻辑
-                                                        address = response.data.details.address
-                                                            ?: "",
-                                                        remarks = response.data.details.trips ?: "",
-                                                    )
-                                                    if (code.oneLevel != "" && code.secondLevel != "" && code.code != "") {
-                                                        Core.code.insert(code)
-                                                    }
-                                                } else {
-                                                    android.util.Log.d(
-                                                        "MainActivity",
-                                                        "发现重复还款短信,跳过保存: $seat"
-                                                    )
-                                                }
-                                            }
+                                            val code = CodeUtils.createTrainTicketCode(
+                                                msgId = msgId,
+                                                createTime = dateString,
+                                                company = response.data.details.company,
+                                                seat = response.data.details.seat,
+                                                time = response.data.details.time,
+                                                address = response.data.details.address,
+                                                trips = response.data.details.trips
+                                            )
+                                            CodeUtils.saveCode(code)
                                         }
                                     }
 

--
Gitblit v1.9.3