mirror of
https://github.com/pppscn/SmsForwarder
synced 2025-08-02 17:07:41 +08:00
新增:监听Screen
事件延迟执行时再次校验
#399
This commit is contained in:
parent
e696125611
commit
b6c98e7f33
@ -12,32 +12,37 @@ data class LockScreenSetting(
|
||||
var timeAfterScreenOn: Int = 5, //开锁后时间
|
||||
var timeAfterScreenLocked: Int = 5, //锁屏后时间
|
||||
var timeAfterScreenUnlocked: Int = 5, //解锁后时间
|
||||
var checkAgain: Boolean = false, //是否再次校验
|
||||
) : Serializable {
|
||||
|
||||
constructor(actionCheckId: Int, timeAfterOff: Int, timeAfterOn: Int, timeAfterLocked: Int, timeAfterUnlocked: Int) : this() {
|
||||
when (actionCheckId) {
|
||||
constructor(actionCheckId: Int, timeAfterOff: Int, timeAfterOn: Int, timeAfterLocked: Int, timeAfterUnlocked: Int, checkAgain: Boolean = false) : this() {
|
||||
val duration = when (actionCheckId) {
|
||||
R.id.rb_action_screen_on -> {
|
||||
val duration = if (timeAfterOn > 0) String.format(getString(R.string.duration_minute), timeAfterOn.toString()) else ""
|
||||
description = String.format(getString(R.string.time_after_screen_on_description), duration)
|
||||
val durationStr = if (timeAfterOn > 0) String.format(getString(R.string.duration_minute), timeAfterOn.toString()) else ""
|
||||
description = String.format(getString(R.string.time_after_screen_on_description), durationStr)
|
||||
action = Intent.ACTION_SCREEN_ON
|
||||
timeAfterOn
|
||||
}
|
||||
|
||||
R.id.rb_action_screen_unlocked -> {
|
||||
val duration = if (timeAfterUnlocked > 0) String.format(getString(R.string.duration_minute), timeAfterUnlocked.toString()) else ""
|
||||
description = String.format(getString(R.string.time_after_screen_unlocked_description), duration)
|
||||
val durationStr = if (timeAfterUnlocked > 0) String.format(getString(R.string.duration_minute), timeAfterUnlocked.toString()) else ""
|
||||
description = String.format(getString(R.string.time_after_screen_unlocked_description), durationStr)
|
||||
action = Intent.ACTION_USER_PRESENT
|
||||
timeAfterUnlocked
|
||||
}
|
||||
|
||||
R.id.rb_action_screen_locked -> {
|
||||
val duration = if (timeAfterLocked > 0) String.format(getString(R.string.duration_minute), timeAfterLocked.toString()) else ""
|
||||
description = String.format(getString(R.string.time_after_screen_locked_description), duration)
|
||||
val durationStr = if (timeAfterLocked > 0) String.format(getString(R.string.duration_minute), timeAfterLocked.toString()) else ""
|
||||
description = String.format(getString(R.string.time_after_screen_locked_description), durationStr)
|
||||
action = Intent.ACTION_SCREEN_OFF + "_LOCKED"
|
||||
timeAfterLocked
|
||||
}
|
||||
|
||||
else -> {
|
||||
val duration = if (timeAfterOff > 0) String.format(getString(R.string.duration_minute), timeAfterOff.toString()) else ""
|
||||
description = String.format(getString(R.string.time_after_screen_off_description), duration)
|
||||
val durationStr = if (timeAfterOff > 0) String.format(getString(R.string.duration_minute), timeAfterOff.toString()) else ""
|
||||
description = String.format(getString(R.string.time_after_screen_off_description), durationStr)
|
||||
action = Intent.ACTION_SCREEN_OFF
|
||||
timeAfterOff
|
||||
}
|
||||
}
|
||||
|
||||
@ -45,6 +50,10 @@ data class LockScreenSetting(
|
||||
timeAfterScreenOn = timeAfterOn
|
||||
timeAfterScreenLocked = timeAfterLocked
|
||||
timeAfterScreenUnlocked = timeAfterUnlocked
|
||||
this.checkAgain = checkAgain
|
||||
if (checkAgain && duration > 0) {
|
||||
description += ", " + getString(R.string.task_condition_check_again)
|
||||
}
|
||||
}
|
||||
|
||||
fun getActionCheckId(): Int {
|
||||
@ -55,4 +64,13 @@ data class LockScreenSetting(
|
||||
else -> R.id.rb_action_screen_locked
|
||||
}
|
||||
}
|
||||
|
||||
fun getDuration(action: String): Int {
|
||||
return when (action) {
|
||||
Intent.ACTION_SCREEN_ON -> timeAfterScreenOn
|
||||
Intent.ACTION_SCREEN_OFF -> timeAfterScreenOff
|
||||
Intent.ACTION_USER_PRESENT -> timeAfterScreenUnlocked
|
||||
else -> timeAfterScreenLocked
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -77,6 +77,7 @@ class LockScreenFragment : BaseFragment<FragmentTasksConditionLockScreenBinding?
|
||||
binding!!.xsbTimeAfterScreenLocked.setDefaultValue(settingVo.timeAfterScreenLocked)
|
||||
binding!!.xsbTimeAfterScreenUnlocked.setDefaultValue(settingVo.timeAfterScreenUnlocked)
|
||||
binding!!.rgAction.check(settingVo.getActionCheckId())
|
||||
binding!!.sbCheckAgain.isChecked = settingVo.checkAgain
|
||||
} else {
|
||||
binding!!.xsbTimeAfterScreenOff.setDefaultValue(0)
|
||||
binding!!.xsbTimeAfterScreenOn.setDefaultValue(0)
|
||||
@ -95,6 +96,15 @@ class LockScreenFragment : BaseFragment<FragmentTasksConditionLockScreenBinding?
|
||||
binding!!.xsbTimeAfterScreenOn.setOnSeekBarListener { _, _ ->
|
||||
checkSetting(true)
|
||||
}
|
||||
binding!!.xsbTimeAfterScreenLocked.setOnSeekBarListener { _, _ ->
|
||||
checkSetting(true)
|
||||
}
|
||||
binding!!.xsbTimeAfterScreenUnlocked.setOnSeekBarListener { _, _ ->
|
||||
checkSetting(true)
|
||||
}
|
||||
binding!!.sbCheckAgain.setOnCheckedChangeListener { _, _ ->
|
||||
checkSetting(true)
|
||||
}
|
||||
}
|
||||
|
||||
@SingleClick
|
||||
@ -132,7 +142,8 @@ class LockScreenFragment : BaseFragment<FragmentTasksConditionLockScreenBinding?
|
||||
val timeAfterScreenOn = binding!!.xsbTimeAfterScreenOn.selectedNumber
|
||||
val timeAferScreenLocked = binding!!.xsbTimeAfterScreenLocked.selectedNumber
|
||||
val timeAfterScreenUnlocked = binding!!.xsbTimeAfterScreenUnlocked.selectedNumber
|
||||
val settingVo = LockScreenSetting(actionCheckId, timeAfterScreenOff, timeAfterScreenOn, timeAferScreenLocked, timeAfterScreenUnlocked)
|
||||
val checkAgain = binding!!.sbCheckAgain.isChecked
|
||||
val settingVo = LockScreenSetting(actionCheckId, timeAfterScreenOff, timeAfterScreenOn, timeAferScreenLocked, timeAfterScreenUnlocked, checkAgain)
|
||||
|
||||
if (updateView) {
|
||||
binding!!.tvDescription.text = settingVo.description
|
||||
|
@ -14,6 +14,7 @@ object Worker {
|
||||
object TaskWorker {
|
||||
const val taskId = "task_id"
|
||||
const val task = "task"
|
||||
const val taskConditions = "task_conditions"
|
||||
const val taskActions = "task_actions"
|
||||
const val conditionType = "condition_type"
|
||||
const val msg = "msg"
|
||||
|
@ -24,6 +24,7 @@ import gatewayapps.crondroid.CronExpression
|
||||
import java.util.Date
|
||||
import kotlin.math.atan2
|
||||
import kotlin.math.cos
|
||||
import kotlin.math.min
|
||||
import kotlin.math.sin
|
||||
import kotlin.math.sqrt
|
||||
|
||||
@ -37,8 +38,9 @@ class ConditionUtils private constructor() {
|
||||
private val TAG: String = ConditionUtils::class.java.simpleName
|
||||
|
||||
//遍历条件列表,判断是否满足条件,默认不校验第一个条件(第一个条件是触发条件)
|
||||
fun checkCondition(taskId: Long, conditionList: MutableList<TaskSetting>, startIndex: Int = 1): Boolean {
|
||||
if (startIndex >= conditionList.size) {
|
||||
fun checkCondition(taskId: Long, conditionList: MutableList<TaskSetting>, beginIndex: Int = 1, endIndex: Int = -1): Boolean {
|
||||
val untilIndex = if (endIndex == -1) conditionList.size else min(endIndex + 1, conditionList.size)
|
||||
if (beginIndex >= untilIndex) {
|
||||
Log.d(TAG, "TASK-$taskId:no condition need to check")
|
||||
return true
|
||||
}
|
||||
@ -46,8 +48,7 @@ class ConditionUtils private constructor() {
|
||||
//注意:触发条件 = SIM卡已准备就绪/网络状态改变时,延迟5秒(给够搜索信号时间)才执行任务
|
||||
val firstCondition = conditionList.firstOrNull()
|
||||
val needDelay = (firstCondition?.type == TASK_CONDITION_SIM && TaskUtils.simState == 5) || (firstCondition?.type == TASK_CONDITION_NETWORK && TaskUtils.networkState != 0)
|
||||
|
||||
for (i in startIndex until conditionList.size) {
|
||||
for (i in beginIndex until untilIndex) { //不包括untilIndex
|
||||
val condition = conditionList[i]
|
||||
when (condition.type) {
|
||||
TASK_CONDITION_CRON -> {
|
||||
|
@ -45,6 +45,7 @@ import com.idormy.sms.forwarder.utils.TASK_ACTION_SENDER
|
||||
import com.idormy.sms.forwarder.utils.TASK_ACTION_SENDSMS
|
||||
import com.idormy.sms.forwarder.utils.TASK_ACTION_SETTINGS
|
||||
import com.idormy.sms.forwarder.utils.TaskWorker
|
||||
import com.idormy.sms.forwarder.utils.task.ConditionUtils
|
||||
import com.jeremyliao.liveeventbus.LiveEventBus
|
||||
import com.xuexiang.xrouter.utils.TextUtils
|
||||
import com.xuexiang.xutil.XUtil
|
||||
@ -61,6 +62,7 @@ class ActionWorker(context: Context, params: WorkerParameters) : CoroutineWorker
|
||||
|
||||
override suspend fun doWork(): Result {
|
||||
taskId = inputData.getLong(TaskWorker.taskId, -1L)
|
||||
val taskConditionsJson = inputData.getString(TaskWorker.taskConditions)
|
||||
val taskActionsJson = inputData.getString(TaskWorker.taskActions)
|
||||
val msgInfoJson = inputData.getString(TaskWorker.msgInfo)
|
||||
Log.d(TAG, "taskId: $taskId, taskActionsJson: $taskActionsJson, msgInfoJson: $msgInfoJson")
|
||||
@ -69,6 +71,19 @@ class ActionWorker(context: Context, params: WorkerParameters) : CoroutineWorker
|
||||
return Result.failure()
|
||||
}
|
||||
|
||||
//TODO: 如果传入的taskConditionsJson不为空,需要再次判断触发条件是否满足
|
||||
if (!taskConditionsJson.isNullOrEmpty()) {
|
||||
val conditionList = Gson().fromJson(taskConditionsJson, Array<TaskSetting>::class.java).toMutableList()
|
||||
if (conditionList.isEmpty()) {
|
||||
writeLog("conditionList is empty")
|
||||
return Result.failure()
|
||||
}
|
||||
if (!ConditionUtils.checkCondition(taskId, conditionList, 0, 0)) {
|
||||
writeLog("recheck condition is not pass", "WARN")
|
||||
return Result.failure()
|
||||
}
|
||||
}
|
||||
|
||||
val actionList = Gson().fromJson(taskActionsJson, Array<TaskSetting>::class.java).toMutableList()
|
||||
if (actionList.isEmpty()) {
|
||||
writeLog("actionList is empty")
|
||||
|
@ -62,21 +62,22 @@ class LockScreenWorker(context: Context, params: WorkerParameters) : CoroutineWo
|
||||
}
|
||||
|
||||
//TODO: 组装消息体 && 执行具体任务
|
||||
val duration = when (action) {
|
||||
Intent.ACTION_SCREEN_ON -> lockScreenSetting.timeAfterScreenOn * 60000L
|
||||
Intent.ACTION_SCREEN_OFF -> lockScreenSetting.timeAfterScreenOff * 60000L
|
||||
Intent.ACTION_USER_PRESENT -> lockScreenSetting.timeAfterScreenUnlocked * 60000L
|
||||
else -> lockScreenSetting.timeAfterScreenLocked * 60000L
|
||||
}
|
||||
Log.d(TAG, "TASK-${task.id}:duration = $duration milliseconds")
|
||||
val msgInfo = MsgInfo("task", task.name, lockScreenSetting.description, Date(), task.description)
|
||||
val actionData = Data.Builder()
|
||||
.putLong(TaskWorker.taskId, task.id)
|
||||
.putString(TaskWorker.taskConditions, if (lockScreenSetting.checkAgain && duration > 0) task.conditions else "")
|
||||
.putString(TaskWorker.taskActions, task.actions)
|
||||
.putString(TaskWorker.msgInfo, Gson().toJson(msgInfo))
|
||||
.build()
|
||||
val duration = when (action) {
|
||||
Intent.ACTION_SCREEN_ON -> lockScreenSetting.timeAfterScreenOn
|
||||
Intent.ACTION_SCREEN_OFF -> lockScreenSetting.timeAfterScreenOff
|
||||
Intent.ACTION_USER_PRESENT -> lockScreenSetting.timeAfterScreenUnlocked
|
||||
else -> lockScreenSetting.timeAfterScreenLocked
|
||||
}
|
||||
Log.d(TAG, "TASK-${task.id}:duration = $duration minutes")
|
||||
val actionRequest = OneTimeWorkRequestBuilder<ActionWorker>()
|
||||
.setInitialDelay(duration.toLong(), TimeUnit.MINUTES)
|
||||
.setInitialDelay(duration, TimeUnit.MILLISECONDS) //TODO: 延迟时间不够精确
|
||||
.setInputData(actionData).build()
|
||||
WorkManager.getInstance().enqueue(actionRequest)
|
||||
}
|
||||
|
@ -162,6 +162,43 @@
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/layout_check_again"
|
||||
style="@style/BarStyle"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/task_condition_check_again"
|
||||
android:textStyle="bold"
|
||||
tools:ignore="RelativeOverlap" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/task_condition_check_again_tips"
|
||||
android:textSize="@dimen/text_size_mini"
|
||||
tools:ignore="SmallSp" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<com.xuexiang.xui.widget.button.switchbutton.SwitchButton
|
||||
android:id="@+id/sb_check_again"
|
||||
style="@style/SwitchButtonStyle"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
tools:ignore="TouchTargetSizeCheck" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</androidx.core.widget.NestedScrollView>
|
||||
|
@ -1196,6 +1196,7 @@
|
||||
<string name="task_rule_tips">Control enabling/disabling of "Forwarding Rules"</string>
|
||||
<string name="task_sender">Channels On/Off</string>
|
||||
<string name="task_sender_tips">Control enabling/disabling of "Sending Channels"</string>
|
||||
<string name="task_alarm">Alarm</string>
|
||||
|
||||
<string name="second">Second</string>
|
||||
<string name="minute">Minute</string>
|
||||
@ -1350,4 +1351,6 @@
|
||||
<string name="chat_id">Chat ID</string>
|
||||
<string name="receive_id">Receive ID</string>
|
||||
<string name="toast_location_not_enabled">Location is not enabled, Please go to system settings and activate it.</string>
|
||||
<string name="task_condition_check_again">Recheck when delaying execution.</string>
|
||||
<string name="task_condition_check_again_tips">When used as a triggering condition, recheck during delayed action execution.</string>
|
||||
</resources>
|
||||
|
@ -1197,6 +1197,7 @@
|
||||
<string name="task_rule_tips">控制【转发规则】的启用/禁用</string>
|
||||
<string name="task_sender">启停通道</string>
|
||||
<string name="task_sender_tips">控制【发送通道】的启用/禁用</string>
|
||||
<string name="task_alarm">声音警报</string>
|
||||
|
||||
<string name="second">秒</string>
|
||||
<string name="minute">分</string>
|
||||
@ -1351,4 +1352,6 @@
|
||||
<string name="chat_id">Chat ID</string>
|
||||
<string name="receive_id">消息接收者ID</string>
|
||||
<string name="toast_location_not_enabled">位置服务未开启,请先前往系统设置中开启!</string>
|
||||
<string name="task_condition_check_again">延迟执行时再次校验</string>
|
||||
<string name="task_condition_check_again_tips">作为触发条件时,在延迟执行动作时再次校验是否满足</string>
|
||||
</resources>
|
||||
|
@ -1197,6 +1197,7 @@
|
||||
<string name="task_rule_tips">控制【轉發規則】的啟用/禁用</string>
|
||||
<string name="task_sender">啟停通道</string>
|
||||
<string name="task_sender_tips">控制【發送通道】的啟用/禁用</string>
|
||||
<string name="task_alarm">聲音警報</string>
|
||||
|
||||
<string name="second">秒</string>
|
||||
<string name="minute">分</string>
|
||||
@ -1352,4 +1353,6 @@
|
||||
<string name="chat_id">Chat ID</string>
|
||||
<string name="receive_id">訊息接收者ID</string>
|
||||
<string name="toast_location_not_enabled">定位服務未開啟,請先前往系統設置中開啟!</string>
|
||||
<string name="task_condition_check_again">延遲執行時再次校驗</string>
|
||||
<string name="task_condition_check_again_tips">作為觸發條件時,在延遲執行動作時再次校驗是否滿足</string>
|
||||
</resources>
|
||||
|
@ -1197,6 +1197,7 @@
|
||||
<string name="task_rule_tips">控制【转发规则】的启用/禁用</string>
|
||||
<string name="task_sender">启停通道</string>
|
||||
<string name="task_sender_tips">控制【发送通道】的启用/禁用</string>
|
||||
<string name="task_alarm">声音警报</string>
|
||||
|
||||
<string name="second">秒</string>
|
||||
<string name="minute">分</string>
|
||||
@ -1351,4 +1352,6 @@
|
||||
<string name="chat_id">Chat ID</string>
|
||||
<string name="receive_id">消息接收者ID</string>
|
||||
<string name="toast_location_not_enabled">位置服务未开启,请先前往系统设置中开启!</string>
|
||||
<string name="task_condition_check_again">延迟执行时再次校验</string>
|
||||
<string name="task_condition_check_again_tips">作为触发条件时,在延迟执行动作时再次校验是否满足</string>
|
||||
</resources>
|
||||
|
Loading…
x
Reference in New Issue
Block a user