Skip to content

Commit

Permalink
closes #16; fix time zone issue for overseas users.
Browse files Browse the repository at this point in the history
  • Loading branch information
teble committed Apr 12, 2022
1 parent dfc7793 commit 7119874
Show file tree
Hide file tree
Showing 22 changed files with 157 additions and 99 deletions.
69 changes: 39 additions & 30 deletions app/src/main/java/me/teble/xposed/autodaily/hook/CoreServiceHook.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import me.teble.xposed.autodaily.ui.ConfUnit
import me.teble.xposed.autodaily.utils.LogUtil
import me.teble.xposed.autodaily.utils.TimeUtil
import java.time.LocalDateTime
import java.util.*
import java.util.concurrent.TimeUnit
import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.thread
Expand All @@ -37,6 +38,7 @@ class CoreServiceHook : BaseHook() {
private val cronLock = ReentrantLock()
const val EXEC_TASK = 1
const val AUTO_EXEC = 2
const val START_CRON = 3
private val handlerThread by lazy {
HandlerThread("CoreServiceHookThread").apply { start() }
}
Expand All @@ -47,6 +49,7 @@ class CoreServiceHook : BaseHook() {
when (msg.what) {
EXEC_TASK -> runTasks(true)
AUTO_EXEC -> runTasks(false)
START_CRON -> startCorn()
}
}
}
Expand Down Expand Up @@ -74,7 +77,7 @@ class CoreServiceHook : BaseHook() {
}

private fun executorTask(conf: TaskProperties) {
val executeTime = TimeUtil.currentTimeMillis()
val executeTime = System.currentTimeMillis()
val needExecGroups = mutableListOf<TaskGroup>()
lock.withLock {
for (group in conf.taskGroups) {
Expand Down Expand Up @@ -112,7 +115,7 @@ class CoreServiceHook : BaseHook() {
}
threadPool.shutdown()
while (!threadPool.awaitTermination(1, TimeUnit.SECONDS)) {
if (TimeUtil.currentTimeMillis() - executeTime > 20 * 60 * 1000) {
if (System.currentTimeMillis() - executeTime > 20 * 60 * 1000) {
// 关闭运行时间超过20分钟的任务
threadPool.shutdownNow()
}
Expand All @@ -125,41 +128,47 @@ class CoreServiceHook : BaseHook() {
}
}
}

private fun startCorn() {
val scheduler = CronUtil.scheduler
if (scheduler.isStarted) {
return
}
TimeUtil.init()
CronUtil.setMatchSecond(true)
scheduler.setTimeZone(TimeZone.getTimeZone("GMT+8"))
try {
CronUtil.start()
} catch (e: Throwable) {
LogUtil.e(e, "CronUtil.start error: ")
ToastUtil.send("任务调度器启动失败,详情请查看日志")
}
LogUtil.d("CoreService 进程已经启动,启动时间 -> ${LocalDateTime.now()}")
LogUtil.d("任务调度器启动成功")
CronUtil.schedule(
"task_timer",
"0 */10 * * * *"
) {
handler.sendEmptyMessage(AUTO_EXEC)
}
CronUtil.schedule(
"check_update_task",
"0 0 9/3 * * *"
) {
if (ConfigUtil.checkUpdate(false)) {
ConfUnit.needUpdate = true
}
}
LogUtil.d("任务调度器存在任务:${scheduler.taskTable.ids}")
}
}

@MethodHook("代理 service hook")
fun coreServiceHook() {
XposedHelpers.findAndHookMethod(load(CoreService),
"onCreate", object : XC_MethodHook() {
override fun beforeHookedMethod(param: MethodHookParam) {
val scheduler = CronUtil.scheduler
if (scheduler.isStarted) {
return
}
CronUtil.setMatchSecond(true)
try {
CronUtil.start()
} catch (e: Throwable) {
LogUtil.e(e, "CronUtil.start error: ")
ToastUtil.send("任务调度器启动失败,详情请查看日志")
}
logd("CoreService 进程已经启动,启动时间 -> ${LocalDateTime.now()}")
LogUtil.d("任务调度器启动成功")
CronUtil.schedule(
"task_timer",
"0 */10 * * * *"
) {
handler.sendEmptyMessage(AUTO_EXEC)
}
CronUtil.schedule(
"check_update_task",
"0 0 9/3 * * *"
) {
if (ConfigUtil.checkUpdate(false)) {
ConfUnit.needUpdate = true
}
}
LogUtil.d("任务调度器存在任务:${scheduler.taskTable.ids}")
handler.sendEmptyMessage(START_CRON)
}
}
)
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/java/me/teble/xposed/autodaily/hook/MainHook.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import me.teble.xposed.autodaily.hook.proxy.ProxyManager
import me.teble.xposed.autodaily.hook.proxy.activity.ResInjectUtil
import me.teble.xposed.autodaily.hook.utils.ToastUtil
import me.teble.xposed.autodaily.utils.LogUtil
import me.teble.xposed.autodaily.utils.TimeUtil
import me.teble.xposed.autodaily.utils.fieldValueAs
import me.teble.xposed.autodaily.utils.new
import java.lang.reflect.Method
Expand Down Expand Up @@ -180,9 +179,9 @@ class MainHook : IXposedHookLoadPackage, IXposedHookZygoteInit {
LogUtil.log("needLocateClasses -> $needLocateClasses")
ToastUtil.send("正在尝试定位QQ混淆类")
val info = needLocateClasses.associateWith { confuseInfo[it] }
val startTime = TimeUtil.currentTimeMillis()
val startTime = System.currentTimeMillis()
val locateRes = locateClasses(info)
val useTime = TimeUtil.currentTimeMillis() - startTime
val useTime = System.currentTimeMillis() - startTime
var locateNum = 0
locateRes.forEach {
if (it.value.size == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import me.teble.xposed.autodaily.ui.AppUpdateLayout
import me.teble.xposed.autodaily.ui.ConfUnit
import me.teble.xposed.autodaily.ui.ConfigUpdateLayout
import me.teble.xposed.autodaily.ui.CustomDialog
import me.teble.xposed.autodaily.utils.TimeUtil
import java.util.*
import kotlin.concurrent.thread

Expand All @@ -38,7 +37,6 @@ class SplashActivityHook : BaseHook() {
findMethod(SplashActivity) { name == "doOnCreate" }.hookAfter {
val context = it.thisObject as Activity
thread {
TimeUtil.init()
loadSaveConf()
if (ConfigUtil.checkUpdate(false)) {
ConfUnit.needUpdate = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ open class FavoriteManager : BaseFunction(

open fun syncGetVoterList(page: Int, pageSize: Int): List<VoterInfo>? {
LogUtil.d("正在获取点赞列表 page: $page, pageSize: $pageSize")
val startTime = TimeUtil.currentTimeMillis()
val startTime = System.currentTimeMillis()
val id = "syncGetVoterList"
FromServiceMsgHook.resMap[id] = null
getVoterList(page, pageSize)
while (TimeUtil.currentTimeMillis() - startTime < 10_000) {
while (System.currentTimeMillis() - startTime < 10_000) {
Thread.sleep(120)
@Suppress("UNCHECKED_CAST")
val tmp = FromServiceMsgHook.resMap[id] as List<VoterInfo>?
Expand All @@ -57,7 +57,7 @@ open class FavoriteManager : BaseFunction(

open fun getAllYesterdayVoter(maxPage: Int): List<VoterInfo>? {
LogUtil.d("正在获取前${maxPage}页的点赞列表")
val beginOfYesterday = DateUtil.beginOfDay(Date(TimeUtil.currentTimeMillis())).time / 1000 - 24 * 60 * 60
val beginOfYesterday = DateUtil.beginOfDay(Date(TimeUtil.cnTimeMillis())).time / 1000 - 24 * 60 * 60
val mutableList = mutableListOf<VoterInfo>()
for (i in 1..maxPage) {
val list = syncGetVoterList(i, 30)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import me.teble.xposed.autodaily.hook.utils.QApplicationUtil
import me.teble.xposed.autodaily.hook.utils.QApplicationUtil.currentUin
import me.teble.xposed.autodaily.hook.utils.WupUtil
import me.teble.xposed.autodaily.utils.LogUtil
import me.teble.xposed.autodaily.utils.TimeUtil
import me.teble.xposed.autodaily.utils.invokeAs
import me.teble.xposed.autodaily.utils.new
import mqq.app.Packet
Expand All @@ -30,11 +29,11 @@ open class MiniLoginManager : BaseFunction(
}

open fun syncGetLoginCode(miniAppId: String): String? {
val startTime = TimeUtil.currentTimeMillis()
val startTime = System.currentTimeMillis()
val id = "syncGetLoginCode"
FromServiceMsgHook.resMap[id] = null
sendLoginRequest(miniAppId)
while (TimeUtil.currentTimeMillis() - startTime < 10_000) {
while (System.currentTimeMillis() - startTime < 10_000) {
Thread.sleep(120)
val tmp = FromServiceMsgHook.resMap[id] as String?
tmp?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import me.teble.xposed.autodaily.hook.utils.QApplicationUtil.currentUin
import me.teble.xposed.autodaily.hook.utils.WupUtil
import me.teble.xposed.autodaily.task.model.MiniProfile
import me.teble.xposed.autodaily.utils.LogUtil
import me.teble.xposed.autodaily.utils.TimeUtil
import me.teble.xposed.autodaily.utils.invokeAs
import me.teble.xposed.autodaily.utils.new
import mqq.app.Packet
Expand All @@ -31,11 +30,11 @@ open class MiniProfileManager : BaseFunction(
}

open fun syncGetProfile(miniAppId: String): MiniProfile? {
val startTime = TimeUtil.currentTimeMillis()
val startTime = System.currentTimeMillis()
val id = "syncGetProfile"
FromServiceMsgHook.resMap[id] = null
sendGetProfileRequest(miniAppId)
while (TimeUtil.currentTimeMillis() - startTime < 10_000) {
while (System.currentTimeMillis() - startTime < 10_000) {
Thread.sleep(120)
val tmp = FromServiceMsgHook.resMap[id] as MiniProfile?
tmp?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ object CalculationUtil {
}

val microsecondTime: Long
get() = TimeUtil.currentTimeMillis()
get() = TimeUtil.cnTimeMillis()

val secondTime: Int
get() = (TimeUtil.currentTimeMillis() / 1000).toInt()
get() = (TimeUtil.cnTimeMillis() / 1000).toInt()

val random: Double
get() = Random().nextDouble()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ object ServletUtil {
append(currentUin)
append("_")
append(sdf.format(Date()))
append(TimeUtil.currentTimeMillis() % 1000)
append(TimeUtil.cnTimeMillis() % 1000)
append("_")
append(
Random().apply {
setSeed(TimeUtil.currentTimeMillis())
setSeed(TimeUtil.cnTimeMillis())
}.nextInt(90000) + 10000
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,25 @@ class CronTimer
private var isStop = false
override fun run() {
val timerUnit = if (scheduler.config.matchSecond) TIMER_UNIT_SECOND else TIMER_UNIT_MINUTE
var thisTime = TimeUtil.currentTimeMillis()
var thisTime = TimeUtil.cnTimeMillis()
var nextTime: Long
var sleep: Long
while (false == isStop) {
//下一时间计算是按照上一个执行点开始时间计算的
//此处除以定时单位是为了清零单位以下部分,例如单位是分则秒和毫秒清零
nextTime = (thisTime / timerUnit + 1) * timerUnit
sleep = nextTime - TimeUtil.currentTimeMillis()
sleep = nextTime - TimeUtil.cnTimeMillis()
if (isValidSleepMillis(sleep, timerUnit)) {
if (!ThreadUtil.safeSleep(sleep)) {
//等待直到下一个时间点,如果被中断直接退出Timer
break
}
//执行点,时间记录为执行开始的时间,而非结束时间
thisTime = TimeUtil.currentTimeMillis()
thisTime = TimeUtil.cnTimeMillis()
spawnLauncher(thisTime)
} else {
// 非正常时间重新计算(issue#1224@Github)
thisTime = TimeUtil.currentTimeMillis()
thisTime = TimeUtil.cnTimeMillis()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ object CronPatternUtil {
} else null
}

fun nextDateAfter(timezone: TimeZone?, pattern: CronPattern, start: Date, isMatchSecond: Boolean): Date? {
val matchedDates: List<Date> = matchedDates(
timezone,
pattern,
start.time,
DateUtil.endOfYear(start).time,
1,
isMatchSecond
)
return if (CollUtil.isNotEmpty(matchedDates)) {
matchedDates[0]
} else null
}

/**
* 列举指定日期之后(到开始日期对应年年底)内所有匹配表达式的日期
*
Expand Down Expand Up @@ -123,4 +137,28 @@ object CronPatternUtil {
}
return result
}

fun matchedDates(
timezone: TimeZone?,
pattern: CronPattern,
start: Long,
end: Long,
count: Int,
isMatchSecond: Boolean
): List<Date> {
Assert.isTrue(start < end, "Start date is later than end !")
val result: MutableList<Date> = ArrayList(count)
val step = if (isMatchSecond) DateUnit.SECOND.millis else DateUnit.MINUTE.millis
var i = start
while (i < end) {
if (pattern.match(timezone, i, isMatchSecond)) {
result.add(DateUtil.date(i))
if (result.size >= count) {
break
}
}
i += step
}
return result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ open class TimerTask(task: Runnable?, delayMs: Long) {
/**
* 延迟时间
*/
val delayMs: Long = TimeUtil.currentTimeMillis() + delayMs
val delayMs: Long = TimeUtil.cnTimeMillis() + delayMs

/**
* 任务
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class TimerTaskList : Delayed {
override fun getDelay(unit: TimeUnit): Long {
return max(
0,
unit.convert(expire.get() - TimeUtil.currentTimeMillis(), TimeUnit.MILLISECONDS)
unit.convert(expire.get() - TimeUtil.cnTimeMillis(), TimeUnit.MILLISECONDS)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class TimingWheel(
constructor(tickMs: Long, wheelSize: Int, consumer: Consumer<TimerTaskList?>) : this(
tickMs,
wheelSize,
TimeUtil.currentTimeMillis(),
TimeUtil.cnTimeMillis(),
consumer
) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class GroupTaskFilterChain(
filter.doFilter(relayTaskMap, taskList, env, this)
} else {
val reqType = ReqType.getType(taskGroup.type.split("|")[0])
val currentDate = Date(TimeUtil.currentTimeMillis()).formatDate()
val currentDate = Date(TimeUtil.localTimeMillis()).formatDate()
for (task in taskList) {
var errCount = 0
var date = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ object CalculationUtil {
}

fun getMicrosecondTime(): Long {
return TimeUtil.currentTimeMillis()
return TimeUtil.cnTimeMillis()
}

fun getSecondTime(): Int {
return (TimeUtil.currentTimeMillis() / 1000).toInt()
return (TimeUtil.cnTimeMillis() / 1000).toInt()
}

fun getRandom(): Double {
Expand Down
Loading

0 comments on commit 7119874

Please sign in to comment.