mirror of
https://github.com/pppscn/SmsForwarder
synced 2025-08-03 01:17:41 +08:00
修复:免打扰时段跨天不成功BUG #493
This commit is contained in:
parent
e3df9bada6
commit
9436e3498b
@ -1,9 +1,13 @@
|
|||||||
package com.idormy.sms.forwarder.utils
|
package com.idormy.sms.forwarder.utils
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
import com.idormy.sms.forwarder.entity.CallInfo
|
import com.idormy.sms.forwarder.entity.CallInfo
|
||||||
import com.idormy.sms.forwarder.entity.ContactInfo
|
import com.idormy.sms.forwarder.entity.ContactInfo
|
||||||
import com.idormy.sms.forwarder.entity.SmsInfo
|
import com.idormy.sms.forwarder.entity.SmsInfo
|
||||||
import com.xuexiang.xaop.annotation.MemoryCache
|
import com.xuexiang.xaop.annotation.MemoryCache
|
||||||
|
import java.text.SimpleDateFormat
|
||||||
|
import java.util.Calendar
|
||||||
|
import java.util.Date
|
||||||
|
|
||||||
@Suppress("SameParameterValue")
|
@Suppress("SameParameterValue")
|
||||||
object DataProvider {
|
object DataProvider {
|
||||||
@ -71,4 +75,45 @@ object DataProvider {
|
|||||||
}
|
}
|
||||||
return list
|
return list
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断当前时间是否在时间段内
|
||||||
|
*/
|
||||||
|
@SuppressLint("SimpleDateFormat")
|
||||||
|
fun isCurrentTimeInPeriod(periodStartIndex: Int, periodEndIndex: Int): Boolean {
|
||||||
|
val periodStartStr = timePeriodOption[periodStartIndex]
|
||||||
|
val periodEndStr = timePeriodOption[periodEndIndex]
|
||||||
|
|
||||||
|
// 定义时间格式
|
||||||
|
val formatter = SimpleDateFormat("HH:mm")
|
||||||
|
|
||||||
|
// 解析时间字符串
|
||||||
|
val periodStart = Calendar.getInstance().apply {
|
||||||
|
time = formatter.parse(periodStartStr) as Date
|
||||||
|
set(Calendar.SECOND, 0)
|
||||||
|
set(Calendar.MILLISECOND, 0)
|
||||||
|
}
|
||||||
|
val periodEnd = Calendar.getInstance().apply {
|
||||||
|
time = formatter.parse(periodEndStr) as Date
|
||||||
|
set(Calendar.SECOND, 0)
|
||||||
|
set(Calendar.MILLISECOND, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取当前时间
|
||||||
|
val currentTime = Calendar.getInstance()
|
||||||
|
val currentHour = currentTime.get(Calendar.HOUR_OF_DAY)
|
||||||
|
val currentMinute = currentTime.get(Calendar.MINUTE)
|
||||||
|
|
||||||
|
// 判断是否跨天
|
||||||
|
return if (periodEnd.before(periodStart)) {
|
||||||
|
// 跨天的情况
|
||||||
|
(currentHour > periodStart.get(Calendar.HOUR_OF_DAY) || (currentHour == periodStart.get(Calendar.HOUR_OF_DAY) && currentMinute >= periodStart.get(Calendar.MINUTE))) ||
|
||||||
|
(currentHour < periodEnd.get(Calendar.HOUR_OF_DAY) || (currentHour == periodEnd.get(Calendar.HOUR_OF_DAY) && currentMinute < periodEnd.get(Calendar.MINUTE)))
|
||||||
|
} else {
|
||||||
|
// 不跨天的情况
|
||||||
|
(currentHour > periodStart.get(Calendar.HOUR_OF_DAY) || (currentHour == periodStart.get(Calendar.HOUR_OF_DAY) && currentMinute >= periodStart.get(Calendar.MINUTE))) &&
|
||||||
|
(currentHour < periodEnd.get(Calendar.HOUR_OF_DAY) || (currentHour == periodEnd.get(Calendar.HOUR_OF_DAY) && currentMinute < periodEnd.get(Calendar.MINUTE)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -49,10 +49,6 @@ import com.idormy.sms.forwarder.workers.UpdateLogsWorker
|
|||||||
import com.jeremyliao.liveeventbus.LiveEventBus
|
import com.jeremyliao.liveeventbus.LiveEventBus
|
||||||
import com.xuexiang.xutil.XUtil
|
import com.xuexiang.xutil.XUtil
|
||||||
import com.xuexiang.xutil.resource.ResUtils.getString
|
import com.xuexiang.xutil.resource.ResUtils.getString
|
||||||
import java.text.ParsePosition
|
|
||||||
import java.text.SimpleDateFormat
|
|
||||||
import java.util.Calendar
|
|
||||||
import java.util.Date
|
|
||||||
|
|
||||||
object SendUtils {
|
object SendUtils {
|
||||||
private const val TAG = "SendUtils"
|
private const val TAG = "SendUtils"
|
||||||
@ -102,28 +98,10 @@ object SendUtils {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
//免打扰(禁用转发)时间段
|
//免打扰(禁用转发)时间段
|
||||||
|
Log.d(TAG, "silentPeriodStart = ${rule.silentPeriodStart}, silentPeriodEnd = ${rule.silentPeriodEnd}")
|
||||||
if (rule.silentPeriodStart != rule.silentPeriodEnd) {
|
if (rule.silentPeriodStart != rule.silentPeriodEnd) {
|
||||||
val periodStartDay = Date()
|
val isSilentPeriod = DataProvider.isCurrentTimeInPeriod(rule.silentPeriodStart, rule.silentPeriodEnd)
|
||||||
var periodStartEnd = Date()
|
if (isSilentPeriod) {
|
||||||
//跨天了
|
|
||||||
if (rule.silentPeriodStart > rule.silentPeriodEnd) {
|
|
||||||
val c: Calendar = Calendar.getInstance()
|
|
||||||
c.time = periodStartEnd
|
|
||||||
c.add(Calendar.DAY_OF_MONTH, 1)
|
|
||||||
periodStartEnd = c.time
|
|
||||||
}
|
|
||||||
|
|
||||||
val dateFmt = SimpleDateFormat("yyyy-MM-dd")
|
|
||||||
val mTimeOption = DataProvider.timePeriodOption
|
|
||||||
val periodStartStr = dateFmt.format(periodStartDay) + " " + mTimeOption[rule.silentPeriodStart] + ":00"
|
|
||||||
val periodEndStr = dateFmt.format(periodStartEnd) + " " + mTimeOption[rule.silentPeriodEnd] + ":00"
|
|
||||||
|
|
||||||
val timeFmt = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
|
|
||||||
val periodStart = timeFmt.parse(periodStartStr, ParsePosition(0))?.time
|
|
||||||
val periodEnd = timeFmt.parse(periodEndStr, ParsePosition(0))?.time
|
|
||||||
|
|
||||||
val now = System.currentTimeMillis()
|
|
||||||
if (periodStart != null && periodEnd != null && now in periodStart..periodEnd) {
|
|
||||||
Log.d(TAG, "免打扰(禁用转发)时间段")
|
Log.d(TAG, "免打扰(禁用转发)时间段")
|
||||||
updateLogs(logId, 0, getString(R.string.silent_time_period))
|
updateLogs(logId, 0, getString(R.string.silent_time_period))
|
||||||
senderLogic(0, msgInfo, rule, senderIndex, msgId)
|
senderLogic(0, msgInfo, rule, senderIndex, msgId)
|
||||||
|
@ -32,10 +32,6 @@ import com.xuexiang.xutil.resource.ResUtils
|
|||||||
import com.xuexiang.xutil.security.CipherUtils
|
import com.xuexiang.xutil.security.CipherUtils
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import java.text.ParsePosition
|
|
||||||
import java.text.SimpleDateFormat
|
|
||||||
import java.util.Calendar
|
|
||||||
import java.util.Date
|
|
||||||
|
|
||||||
@Suppress("PrivatePropertyName", "DEPRECATION")
|
@Suppress("PrivatePropertyName", "DEPRECATION")
|
||||||
class SendWorker(context: Context, params: WorkerParameters) : CoroutineWorker(context, params) {
|
class SendWorker(context: Context, params: WorkerParameters) : CoroutineWorker(context, params) {
|
||||||
@ -61,36 +57,15 @@ class SendWorker(context: Context, params: WorkerParameters) : CoroutineWorker(c
|
|||||||
|
|
||||||
// 免打扰(禁用转发)时间段
|
// 免打扰(禁用转发)时间段
|
||||||
var isSilentPeriod = false
|
var isSilentPeriod = false
|
||||||
|
Log.d(TAG, "silentPeriodStart = ${SettingUtils.silentPeriodStart}, silentPeriodEnd = ${SettingUtils.silentPeriodEnd}")
|
||||||
if (SettingUtils.silentPeriodStart != SettingUtils.silentPeriodEnd) {
|
if (SettingUtils.silentPeriodStart != SettingUtils.silentPeriodEnd) {
|
||||||
val periodStartDay = Date()
|
isSilentPeriod = DataProvider.isCurrentTimeInPeriod(SettingUtils.silentPeriodStart, SettingUtils.silentPeriodEnd)
|
||||||
var periodStartEnd = Date()
|
Log.d(TAG, "isSilentPeriod = $isSilentPeriod, enableSilentPeriodLogs = ${SettingUtils.enableSilentPeriodLogs}")
|
||||||
//跨天了
|
if (isSilentPeriod && !SettingUtils.enableSilentPeriodLogs) {
|
||||||
if (SettingUtils.silentPeriodStart > SettingUtils.silentPeriodEnd) {
|
|
||||||
val c: Calendar = Calendar.getInstance()
|
|
||||||
c.time = periodStartEnd
|
|
||||||
c.add(Calendar.DAY_OF_MONTH, 1)
|
|
||||||
periodStartEnd = c.time
|
|
||||||
}
|
|
||||||
|
|
||||||
val dateFmt = SimpleDateFormat("yyyy-MM-dd")
|
|
||||||
val mTimeOption = DataProvider.timePeriodOption
|
|
||||||
val periodStartStr = dateFmt.format(periodStartDay) + " " + mTimeOption[SettingUtils.silentPeriodStart] + ":00"
|
|
||||||
val periodEndStr = dateFmt.format(periodStartEnd) + " " + mTimeOption[SettingUtils.silentPeriodEnd] + ":00"
|
|
||||||
|
|
||||||
val timeFmt = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
|
|
||||||
val periodStart = timeFmt.parse(periodStartStr, ParsePosition(0))?.time
|
|
||||||
val periodEnd = timeFmt.parse(periodEndStr, ParsePosition(0))?.time
|
|
||||||
|
|
||||||
val now = System.currentTimeMillis()
|
|
||||||
if (periodStart != null && periodEnd != null && now in periodStart..periodEnd) {
|
|
||||||
if (SettingUtils.enableSilentPeriodLogs) {
|
|
||||||
isSilentPeriod = true
|
|
||||||
} else {
|
|
||||||
Log.e(TAG, "免打扰(禁用转发)时间段")
|
Log.e(TAG, "免打扰(禁用转发)时间段")
|
||||||
return@withContext Result.failure(workDataOf("send" to "failed"))
|
return@withContext Result.failure(workDataOf("send" to "failed"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// 过滤重复消息机制
|
// 过滤重复消息机制
|
||||||
val duplicateMessagesLimits = SettingUtils.duplicateMessagesLimits * 1000L
|
val duplicateMessagesLimits = SettingUtils.duplicateMessagesLimits * 1000L
|
||||||
|
Loading…
x
Reference in New Issue
Block a user