| | |
| | | registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions -> |
| | | when { |
| | | permissions.getOrDefault( |
| | | Manifest.permission.RECEIVE_SMS, |
| | | false |
| | | Manifest.permission.RECEIVE_SMS, false |
| | | ) && permissions.getOrDefault(Manifest.permission.READ_SMS, false) -> { |
| | | // 两个权限都获得授权 |
| | | registerSmsReceiver() |
| | |
| | | |
| | | // 重置提醒计划并检查是否有错过的提醒 |
| | | resetReminders() |
| | | |
| | | |
| | | // 检查权限 |
| | | if (ContextCompat.checkSelfPermission( |
| | | this, Manifest.permission.RECEIVE_SMS |
| | |
| | | while (cursor.moveToNext()) { |
| | | val messageBody = cursor.getString(cursor.getColumnIndexOrThrow("body")) |
| | | val datetime = cursor.getLong(cursor.getColumnIndexOrThrow("date")) |
| | | |
| | | |
| | | // 转换为 Date 对象 |
| | | val date = Date(datetime) |
| | | val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()) |
| | |
| | | // 使用协程处理API调用和数据库操作 |
| | | CoroutineScope(Dispatchers.IO).launch { |
| | | try { |
| | | val response = RetrofitModelClient.modelService.processSms(mapOf("content" to messageBody)) |
| | | |
| | | val response = |
| | | RetrofitModelClient.modelService.processSms(mapOf("content" to messageBody)) |
| | | |
| | | synchronized(syncLock) { |
| | | 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 |
| | | ) |
| | | val existingCode = |
| | | Core.code.queryByTypeAndCodeAndDate( |
| | | response.data.category, |
| | | pickupCode, |
| | | dateString |
| | | ) |
| | | |
| | | if (existingCode == null) { |
| | | val code = Code( |
| | |
| | | msgId = msgId, |
| | | createTime = dateString, |
| | | oneLevel = response.data.details.post ?: "", |
| | | secondLevel = response.data.details.company ?: "", |
| | | secondLevel = response.data.details.company |
| | | ?: "", |
| | | code = pickupCode, |
| | | pickup = 0, |
| | | pickupTime = "", |
| | | overTime = "", |
| | | address = response.data.details.address ?: "", |
| | | 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()) { |
| | | if (code.oneLevel.isNotEmpty() && code.code.isNotEmpty()) { |
| | | Core.code.insert(code) |
| | | android.util.Log.d("MainActivity", "历史快递短信已保存: $pickupCode") |
| | | android.util.Log.d( |
| | | "MainActivity", |
| | | "历史快递短信已保存: $pickupCode" |
| | | ) |
| | | } |
| | | } else { |
| | | android.util.Log.d("MainActivity", "发现重复快递短信,跳过保存: $pickupCode") |
| | | android.util.Log.d( |
| | | "MainActivity", |
| | | "发现重复快递短信,跳过保存: $pickupCode" |
| | | ) |
| | | } |
| | | } |
| | | } |
| | | |
| | | "还款" -> { |
| | | val amount = response.data.details.amount ?: "" |
| | | if (amount.isNotEmpty()) { |
| | | val existingCode = Core.code.queryByTypeAndCodeAndDate( |
| | | response.data.category, |
| | | amount, |
| | | dateString |
| | | ) |
| | | val existingCode = |
| | | Core.code.queryByTypeAndCodeAndDate( |
| | | response.data.category, amount, dateString |
| | | ) |
| | | |
| | | if (existingCode == null) { |
| | | val code = Code( |
| | |
| | | msgId = msgId, |
| | | createTime = dateString, |
| | | oneLevel = response.data.details.type ?: "", |
| | | secondLevel = response.data.details.bank ?: "", |
| | | secondLevel = response.data.details.bank |
| | | ?: "", |
| | | code = amount, |
| | | pickup = 0, |
| | | pickupTime = "", |
| | | overTime = response.data.details.date ?: "", |
| | | address = response.data.details.address ?: "", |
| | | 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()) { |
| | | if (code.oneLevel.isNotEmpty() && code.secondLevel.isNotEmpty() && code.code.isNotEmpty()) { |
| | | Core.code.insert(code) |
| | | android.util.Log.d("MainActivity", "历史还款短信已保存: $amount") |
| | | android.util.Log.d( |
| | | "MainActivity", |
| | | "历史还款短信已保存: $amount" |
| | | ) |
| | | } |
| | | } else { |
| | | android.util.Log.d("MainActivity", "发现重复还款短信,跳过保存: $amount") |
| | | android.util.Log.d( |
| | | "MainActivity", |
| | | "发现重复还款短信,跳过保存: $amount" |
| | | ) |
| | | } |
| | | } |
| | | } |
| | | |
| | | "收入" -> { |
| | | val amount = response.data.details.amount ?: "" |
| | | if (amount.isNotEmpty()) { |
| | | val existingCode = Core.code.queryByTypeAndCodeAndDate( |
| | | response.data.category, |
| | | amount, |
| | | dateString |
| | | ) |
| | | val existingCode = |
| | | Core.code.queryByTypeAndCodeAndDate( |
| | | response.data.category, amount, dateString |
| | | ) |
| | | |
| | | if (existingCode == null) { |
| | | val code = Code( |
| | |
| | | msgId = msgId, |
| | | createTime = dateString, |
| | | oneLevel = response.data.details.bank ?: "", |
| | | secondLevel = 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 ?: "", |
| | | address = response.data.details.address |
| | | ?: "", |
| | | remarks = "余额" + response.data.details.balance |
| | | ?: "", |
| | | ) |
| | | if(code.oneLevel.isNotEmpty() && code.secondLevel.isNotEmpty() && code.code.isNotEmpty()) { |
| | | if (code.oneLevel.isNotEmpty() && code.secondLevel.isNotEmpty() && code.code.isNotEmpty()) { |
| | | Core.code.insert(code) |
| | | android.util.Log.d("MainActivity", "历史还款短信已保存: $amount") |
| | | android.util.Log.d( |
| | | "MainActivity", |
| | | "历史还款短信已保存: $amount" |
| | | ) |
| | | } |
| | | } else { |
| | | android.util.Log.d("MainActivity", "发现重复还款短信,跳过保存: $amount") |
| | | android.util.Log.d( |
| | | "MainActivity", |
| | | "发现重复还款短信,跳过保存: $amount" |
| | | ) |
| | | } |
| | | } |
| | | } |
| | | |
| | | "航班" -> { |
| | | 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 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" |
| | | ) |
| | | } |
| | | } |
| | | } |
| | |
| | | try { |
| | | // 取消所有现有的提醒任务 |
| | | androidx.work.WorkManager.getInstance(this).cancelAllWorkByTag("reminder_worker") |
| | | |
| | | |
| | | // 清除旧的提醒偏好设置 |
| | | getSharedPreferences("reminder_prefs", Context.MODE_PRIVATE).edit().clear().apply() |
| | | |
| | | |
| | | // 重新设置提醒任务 |
| | | (application as App).setupReminderWorker() |
| | | |
| | | |
| | | // 检查是否有错过的提醒 |
| | | com.example.firstapp.service.ReminderWorker.checkMissedReminders(this) |
| | | |
| | | |
| | | android.util.Log.d("MainActivity", "已重置提醒计划") |
| | | } catch (e: Exception) { |
| | | android.util.Log.e("MainActivity", "重置提醒计划失败: ${e.message}") |