| | |
| | | import android.net.Uri |
| | | import android.os.Build |
| | | import androidx.annotation.RequiresApi |
| | | import androidx.work.ExistingPeriodicWorkPolicy |
| | | import androidx.work.PeriodicWorkRequestBuilder |
| | | import androidx.work.WorkManager |
| | | import com.example.firstapp.activity.LoginActivity |
| | | import com.example.firstapp.adapter.MyAdapter |
| | | 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 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.util.Calendar |
| | | import java.util.Date |
| | | import java.util.Locale |
| | | import java.util.concurrent.TimeUnit |
| | | import java.time.ZoneId |
| | | |
| | | class MainActivity : AppCompatActivity() { |
| | | // 安全防护关键词数组 |
| | |
| | | private lateinit var binding: ActivityMainBinding |
| | | |
| | | private var smsReceiver: SmsReceiver? = null |
| | | |
| | | private lateinit var adapter: MyAdapter |
| | | private lateinit var homeViewModel: HomeViewModel |
| | | |
| | | private val multiplePermissionRequest = |
| | | registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions -> |
| | |
| | | ) && permissions.getOrDefault(Manifest.permission.READ_SMS, false) -> { |
| | | // 两个权限都获得授权 |
| | | registerSmsReceiver() |
| | | // syncRecentSms() |
| | | // initializeSecurityKeywords() |
| | | } |
| | | |
| | | else -> { |
| | |
| | | binding = ActivityMainBinding.inflate(layoutInflater) |
| | | setContentView(binding.root) |
| | | setupViews() |
| | | // binding.btnLogout.setOnClickListener { |
| | | // logout() |
| | | // } |
| | | // 在此位置初始化 homeViewModel |
| | | // homeViewModel = ViewModelProvider(this).get(HomeViewModel::class.java) |
| | | // |
| | | // val navView: BottomNavigationView = binding.navView |
| | | val navView = binding.navView |
| | | val navController = findNavController(R.id.nav_host_fragment_activity_main) |
| | | |
| | |
| | | registerSmsReceiver() |
| | | syncRecentSms() |
| | | } |
| | | // val recyclerView = findViewById<RecyclerView>(R.id.recyclerView) |
| | | // recyclerView.layoutManager = LinearLayoutManager(this) |
| | | // |
| | | // // 初始化适配器 |
| | | // adapter = MyAdapter() |
| | | // recyclerView.adapter = adapter |
| | | // |
| | | // // 观察 LiveData 数据 |
| | | // homeViewModel.codeList.observe(this) { codeList -> |
| | | // // 如果 codeList 为 null,避免闪退 |
| | | // if (codeList != null) { |
| | | // adapter.submitList(codeList) |
| | | // // 滚动到顶部 |
| | | // recyclerView.scrollToPosition(0) |
| | | // } else { |
| | | // // 如果数据为空,可以显示空列表或其他处理 |
| | | // Toast.makeText(this, "No data available", Toast.LENGTH_SHORT).show() |
| | | // } |
| | | // } |
| | | |
| | | // // 注册广播接收器来监听数据更新 |
| | | // val filter = IntentFilter("com.example.firstapp.DATA_UPDATED") |
| | | // registerReceiver(object : BroadcastReceiver() { |
| | | // override fun onReceive(context: Context, intent: Intent) { |
| | | // // 数据已更新,刷新 LiveData |
| | | // homeViewModel.loadData() |
| | | // } |
| | | // }, filter) |
| | | |
| | | } |
| | | |
| | | private fun registerSmsReceiver() { |
| | |
| | | registerReceiver(smsReceiver, filter) |
| | | } |
| | | |
| | | private fun setupKeywordUpdate() { |
| | | val updateRequest = PeriodicWorkRequestBuilder<KeywordUpdateWorker>( |
| | | 1, TimeUnit.HOURS, // 每小时更新一次 |
| | | 15, TimeUnit.MINUTES // 灵活时间窗口 |
| | | ).build() |
| | | |
| | | WorkManager.getInstance(this).enqueueUniquePeriodicWork( |
| | | "keyword_update", ExistingPeriodicWorkPolicy.REPLACE, updateRequest |
| | | ) |
| | | } |
| | | |
| | | private fun setupViews() { |
| | | // 获取并显示当前登录的手机号 |
| | | val phone = |
| | | getSharedPreferences("user_info", Context.MODE_PRIVATE).getString("phone", "") ?: "" |
| | | |
| | | // binding.apply { |
| | | // tvPhone.text = "当前登录手机号:$phone" |
| | | // } |
| | | getSharedPreferences("user_info", Context.MODE_PRIVATE).getString("phone", "") ?: "" |
| | | } |
| | | |
| | | private fun logout() { |
| | |
| | | val msgId = Core.msg.insert(msg) |
| | | |
| | | // 禁用关键词拦截 |
| | | if (securityKeywordsList.any { it in messageBody }) { |
| | | android.util.Log.d("MainActivity", "历史短信含有禁用关键词,跳过处理") |
| | | continue |
| | | } |
| | | // if (securityKeywordsList.any { it in messageBody }) { |
| | | // android.util.Log.d("MainActivity", "历史短信含有禁用关键词,跳过处理") |
| | | // continue |
| | | // } |
| | | |
| | | // 使用协程处理API调用和数据库操作 |
| | | CoroutineScope(Dispatchers.IO).launch { |
| | | try { |
| | | // API调用移到synchronized块外 |
| | | val response = RetrofitModelClient.modelService.processSms(mapOf("content" to messageBody)) |
| | | |
| | | // 数据库操作放在synchronized块内 |
| | | synchronized(syncLock) { |
| | | if (response.status == "success") { |
| | | when (response.data.category) { |
| | | "快递" -> { |
| | | val existingCode = Core.code.queryByTypeAndCodeAndDate( |
| | | response.data.category, |
| | | response.data.details.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 = response.data.details.pickupCode ?: "", |
| | | pickup = 0, |
| | | pickupTime = "", |
| | | overTime = "", |
| | | address = response.data.details.address ?: "", |
| | | remarks = response.data.details.time ?: "", |
| | | val pickupCode = response.data.details.pickupCode ?: "" |
| | | if (pickupCode.isNotEmpty()) { |
| | | val existingCode = Core.code.queryByTypeAndCodeAndDate( |
| | | response.data.category, |
| | | pickupCode, |
| | | dateString |
| | | ) |
| | | if(code.oneLevel!="" && code.secondLevel!="" && code.code!="") { |
| | | Core.code.insert(code) |
| | | |
| | | 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") |
| | | } |
| | | android.util.Log.d("MainActivity", "历史快递短信已保存: ${response.data.details.pickupCode}") |
| | | } else { |
| | | android.util.Log.d("MainActivity", "发现重复快递短信,跳过保存: ${response.data.details.pickupCode}") |
| | | } |
| | | } |
| | | "还款" -> { |
| | | val existingCode = Core.code.queryByTypeAndCodeAndDate( |
| | | response.data.category, |
| | | response.data.details.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 = response.data.details.amount ?: "", |
| | | pickup = 0, |
| | | pickupTime = "", |
| | | overTime = response.data.details.date ?: "", |
| | | address = response.data.details.address ?: "", |
| | | remarks = "最小还款金额${response.data.details.min_amount}还款卡号${response.data.details.number}" |
| | | val amount = response.data.details.amount ?: "" |
| | | if (amount.isNotEmpty()) { |
| | | val existingCode = Core.code.queryByTypeAndCodeAndDate( |
| | | response.data.category, |
| | | amount, |
| | | dateString |
| | | ) |
| | | if(code.oneLevel!="" && code.secondLevel!="" && code.code!="") { |
| | | Core.code.insert(code) |
| | | |
| | | 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") |
| | | } |
| | | android.util.Log.d("MainActivity", "历史还款短信已保存: ${response.data.details.amount}") |
| | | } else { |
| | | android.util.Log.d("MainActivity", "发现重复还款短信,跳过保存: ${response.data.details.amount}") |
| | | } |
| | | } |
| | | |
| | | "收入" -> { |
| | | val existingCode = Core.code.queryByTypeAndCodeAndDate( |
| | | response.data.category, |
| | | response.data.details.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 = response.data.details.amount ?: "", |
| | | pickup = 0, // 0-未取件,1-已取件 |
| | | pickupTime = "", // 取件时间为空 |
| | | overTime = response.data.details.datetime |
| | | ?: "", // 超时时间为空,暂时没有这块处理逻辑 |
| | | address = response.data.details.address ?: "", |
| | | remarks = "余额" + response.data.details.balance ?: "", |
| | | val amount = response.data.details.amount ?: "" |
| | | if (amount.isNotEmpty()) { |
| | | val existingCode = Core.code.queryByTypeAndCodeAndDate( |
| | | response.data.category, |
| | | amount, |
| | | dateString |
| | | ) |
| | | if(code.oneLevel!="" && code.secondLevel!="" && code.code!="") { |
| | | Core.code.insert(code) |
| | | |
| | | 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") |
| | | } |
| | | android.util.Log.d("MainActivity", "历史还款短信已保存: ${response.data.details.amount}") |
| | | } else { |
| | | android.util.Log.d("MainActivity", "发现重复还款短信,跳过保存: ${response.data.details.amount}") |
| | | } |
| | | } |
| | | } |