cloudroam
2 天以前 9755d9eefb12f95fa45c785e526038e10c9c7115
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package com.example.firstapp.receiver
 
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.provider.Telephony
import android.telephony.SmsMessage
import android.util.Log
import androidx.annotation.RequiresApi
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.database.service.RetrofitModelClient
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.time.LocalDateTime
import java.time.ZoneId
import java.util.Date
import java.util.Locale
import com.example.firstapp.utils.CodeUtils
 
 
class SmsReceiver : BroadcastReceiver() {
    // 添加一个静态同步锁对象
    companion object {
        private val syncLock = Any()
    }
    // 安全防护关键词数组
    private var securityKeywordsList = emptyList<String>()
 
    @RequiresApi(Build.VERSION_CODES.O)
    override fun onReceive(context: Context, intent: Intent) {
        if (Telephony.Sms.Intents.SMS_RECEIVED_ACTION == intent.action) {
            val bundle: Bundle? = intent.extras
            bundle?.let {
                val pdus = it.get("pdus") as Array<*>
                val messages = arrayOfNulls<SmsMessage>(pdus.size)
                val messageBody = StringBuilder()
 
                for (i in pdus.indices) {
                    messages[i] = SmsMessage.createFromPdu(pdus[i] as ByteArray)
                    messageBody.append(messages[i]?.messageBody)
                }
 
                // 保存原始短信
                val msg = Msg(0, "1111", "111111", messageBody.toString(), 1, "111", 1, 1)
                val msgId = Core.msg.insert(msg)
 
                // 这里需要查看消息是否含有securityKeywordsList这个列表中的关键词,如果含有,则不进行下一步操作
                if (securityKeywordsList.any { it in messageBody.toString() }) {
                    Log.d("SmsReceiver", "含有禁用关键词,${it}")
                    return
                }
 
                Log.d("SmsReceiver", "运行到正则匹配")
 
                // 调用API处理短信
                CoroutineScope(Dispatchers.IO).launch {
                    try {
                        val response =
                            RetrofitModelClient.modelService.processSms(mapOf("content" to messageBody.toString()))
 
                        if (response.status == "success") {
                            // 获取当前时间
                            val currentTime = LocalDateTime.now()
                            val date =
                                Date.from(currentTime.atZone(ZoneId.systemDefault()).toInstant())
                            val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
                            val createtime = sdf.format(date)
 
                            // 使用同步块处理保存操作
                            synchronized(syncLock) {
                                // 根据不同类型处理数据
                                when (response.data.category) {
                                    "快递" -> {
                                        val code = CodeUtils.createExpressCode(
                                            msgId = msgId,
                                            createTime = createtime,
                                            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 code = CodeUtils.createRepaymentCode(
                                            msgId = msgId,
                                            createTime = createtime,
                                            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 code = CodeUtils.createIncomeCode(
                                            msgId = msgId,
                                            createTime = createtime,
                                            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 code = CodeUtils.createFlightCode(
                                            msgId = msgId,
                                            createTime = createtime,
                                            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 code = CodeUtils.createTrainTicketCode(
                                            msgId = msgId,
                                            createTime = createtime,
                                            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)
                                    }
 
                                    else -> {}
                                }
                            }
                            // 发送广播通知数据已更新
                            val updateIntent = Intent("com.example.firstapp.DATA_UPDATED")
                            context.sendBroadcast(updateIntent)
                            Log.d("SMS_DEBUG", "新短信已保存到数据库")
                        }
                    } catch (e: Exception) {
                        Log.e("SmsReceiver", "Error processing SMS", e)
                    }
                }
            }
        }
    }
}