mirror of
https://github.com/pppscn/SmsForwarder
synced 2024-11-08 01:10:23 +00:00
修复:免打扰时段跨天不成功BUG #493
This commit is contained in:
parent
e3df9bada6
commit
9436e3498b
@ -1,9 +1,13 @@
|
||||
package com.idormy.sms.forwarder.utils
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import com.idormy.sms.forwarder.entity.CallInfo
|
||||
import com.idormy.sms.forwarder.entity.ContactInfo
|
||||
import com.idormy.sms.forwarder.entity.SmsInfo
|
||||
import com.xuexiang.xaop.annotation.MemoryCache
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Calendar
|
||||
import java.util.Date
|
||||
|
||||
@Suppress("SameParameterValue")
|
||||
object DataProvider {
|
||||
@ -71,4 +75,45 @@ object DataProvider {
|
||||
}
|
||||
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.xuexiang.xutil.XUtil
|
||||
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 {
|
||||
private const val TAG = "SendUtils"
|
||||
@ -102,28 +98,10 @@ object SendUtils {
|
||||
return
|
||||
}
|
||||
//免打扰(禁用转发)时间段
|
||||
Log.d(TAG, "silentPeriodStart = ${rule.silentPeriodStart}, silentPeriodEnd = ${rule.silentPeriodEnd}")
|
||||
if (rule.silentPeriodStart != rule.silentPeriodEnd) {
|
||||
val periodStartDay = Date()
|
||||
var periodStartEnd = Date()
|
||||
//跨天了
|
||||
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) {
|
||||
val isSilentPeriod = DataProvider.isCurrentTimeInPeriod(rule.silentPeriodStart, rule.silentPeriodEnd)
|
||||
if (isSilentPeriod) {
|
||||
Log.d(TAG, "免打扰(禁用转发)时间段")
|
||||
updateLogs(logId, 0, getString(R.string.silent_time_period))
|
||||
senderLogic(0, msgInfo, rule, senderIndex, msgId)
|
||||
|
@ -32,10 +32,6 @@ import com.xuexiang.xutil.resource.ResUtils
|
||||
import com.xuexiang.xutil.security.CipherUtils
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.text.ParsePosition
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Calendar
|
||||
import java.util.Date
|
||||
|
||||
@Suppress("PrivatePropertyName", "DEPRECATION")
|
||||
class SendWorker(context: Context, params: WorkerParameters) : CoroutineWorker(context, params) {
|
||||
@ -61,34 +57,13 @@ class SendWorker(context: Context, params: WorkerParameters) : CoroutineWorker(c
|
||||
|
||||
// 免打扰(禁用转发)时间段
|
||||
var isSilentPeriod = false
|
||||
Log.d(TAG, "silentPeriodStart = ${SettingUtils.silentPeriodStart}, silentPeriodEnd = ${SettingUtils.silentPeriodEnd}")
|
||||
if (SettingUtils.silentPeriodStart != SettingUtils.silentPeriodEnd) {
|
||||
val periodStartDay = Date()
|
||||
var periodStartEnd = Date()
|
||||
//跨天了
|
||||
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, "免打扰(禁用转发)时间段")
|
||||
return@withContext Result.failure(workDataOf("send" to "failed"))
|
||||
}
|
||||
isSilentPeriod = DataProvider.isCurrentTimeInPeriod(SettingUtils.silentPeriodStart, SettingUtils.silentPeriodEnd)
|
||||
Log.d(TAG, "isSilentPeriod = $isSilentPeriod, enableSilentPeriodLogs = ${SettingUtils.enableSilentPeriodLogs}")
|
||||
if (isSilentPeriod && !SettingUtils.enableSilentPeriodLogs) {
|
||||
Log.e(TAG, "免打扰(禁用转发)时间段")
|
||||
return@withContext Result.failure(workDataOf("send" to "failed"))
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user