新增:自动任务·快捷指令 —— 锁屏解锁 #370

pull/408/head
pppscn 10 months ago
parent b94a25c09d
commit d8553ef793

@ -272,6 +272,14 @@
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
<receiver
android:name=".receiver.LockScreenReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
</receiver>
<receiver
android:name=".receiver.NetworkChangeReceiver"
android:enabled="true"

@ -23,6 +23,7 @@ import com.idormy.sms.forwarder.database.repository.*
import com.idormy.sms.forwarder.entity.SimInfo
import com.idormy.sms.forwarder.receiver.BatteryReceiver
import com.idormy.sms.forwarder.receiver.CactusReceiver
import com.idormy.sms.forwarder.receiver.LockScreenReceiver
import com.idormy.sms.forwarder.receiver.NetworkChangeReceiver
import com.idormy.sms.forwarder.service.ForegroundService
import com.idormy.sms.forwarder.service.HttpServerService
@ -145,6 +146,14 @@ class App : Application(), CactusCallback, Configuration.Provider by Core {
}
registerReceiver(networkReceiver, networkFilter)
//监听锁屏&解锁
val lockScreenReceiver = LockScreenReceiver()
val lockScreenFilter = IntentFilter().apply {
addAction(Intent.ACTION_SCREEN_OFF)
addAction(Intent.ACTION_SCREEN_ON)
}
registerReceiver(lockScreenReceiver, lockScreenFilter)
//Cactus 集成双进程前台服务JobScheduleronePix(一像素)WorkManager无声音乐
if (SettingUtils.enableCactus) {
//注册广播监听器

@ -0,0 +1,36 @@
package com.idormy.sms.forwarder.entity.task
import android.content.Intent
import com.idormy.sms.forwarder.R
import com.xuexiang.xutil.resource.ResUtils.getString
import java.io.Serializable
data class LockScreenSetting(
var description: String = "", //描述
var action: String = Intent.ACTION_SCREEN_OFF, //事件
var timeAfterScreenOff: Int = 5, //锁屏后时间
var timeAfterScreenOn: Int = 5, //解锁后时间
) : Serializable {
constructor(actionCheckId: Int, timeAfterOff: Int, timeAfterOn: Int) : this() {
if (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)
action = Intent.ACTION_SCREEN_ON
} 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)
action = Intent.ACTION_SCREEN_OFF
}
timeAfterScreenOff = timeAfterOff
timeAfterScreenOn = timeAfterOn
}
fun getActionCheckId(): Int {
return when (action) {
Intent.ACTION_SCREEN_ON -> R.id.rb_action_screen_on
else -> R.id.rb_action_screen_off
}
}
}

@ -0,0 +1,159 @@
package com.idormy.sms.forwarder.fragment.condition
import android.annotation.SuppressLint
import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.gson.Gson
import com.idormy.sms.forwarder.R
import com.idormy.sms.forwarder.core.BaseFragment
import com.idormy.sms.forwarder.databinding.FragmentTasksConditionLockScreenBinding
import com.idormy.sms.forwarder.entity.task.LockScreenSetting
import com.idormy.sms.forwarder.utils.KEY_BACK_DATA_CONDITION
import com.idormy.sms.forwarder.utils.KEY_BACK_DESCRIPTION_CONDITION
import com.idormy.sms.forwarder.utils.KEY_EVENT_DATA_CONDITION
import com.idormy.sms.forwarder.utils.KEY_TEST_CONDITION
import com.idormy.sms.forwarder.utils.TASK_CONDITION_LOCK_SCREEN
import com.idormy.sms.forwarder.utils.XToastUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import com.xuexiang.xaop.annotation.SingleClick
import com.xuexiang.xpage.annotation.Page
import com.xuexiang.xrouter.annotation.AutoWired
import com.xuexiang.xrouter.launcher.XRouter
import com.xuexiang.xui.utils.CountDownButtonHelper
import com.xuexiang.xui.widget.actionbar.TitleBar
@Page(name = "LockScreen")
@Suppress("PrivatePropertyName")
class LockScreenFragment : BaseFragment<FragmentTasksConditionLockScreenBinding?>(), View.OnClickListener {
private val TAG: String = LockScreenFragment::class.java.simpleName
var titleBar: TitleBar? = null
private var mCountDownHelper: CountDownButtonHelper? = null
@JvmField
@AutoWired(name = KEY_EVENT_DATA_CONDITION)
var eventData: String? = null
override fun initArgs() {
XRouter.getInstance().inject(this)
}
override fun viewBindingInflate(
inflater: LayoutInflater,
container: ViewGroup,
): FragmentTasksConditionLockScreenBinding {
return FragmentTasksConditionLockScreenBinding.inflate(inflater, container, false)
}
override fun initTitle(): TitleBar? {
titleBar = super.initTitle()!!.setImmersive(false).setTitle(R.string.task_lock_screen)
return titleBar
}
/**
* 初始化控件
*/
override fun initViews() {
//测试按钮增加倒计时,避免重复点击
mCountDownHelper = CountDownButtonHelper(binding!!.btnTest, 3)
mCountDownHelper!!.setOnCountDownListener(object : CountDownButtonHelper.OnCountDownListener {
override fun onCountDown(time: Int) {
binding!!.btnTest.text = String.format(getString(R.string.seconds_n), time)
}
override fun onFinished() {
binding!!.btnTest.text = getString(R.string.test)
}
})
binding!!.rgAction.setOnCheckedChangeListener { _, checkedId ->
if (checkedId == R.id.rb_action_screen_off) {
binding!!.xsbTimeAfterScreenOff.visibility = View.VISIBLE
binding!!.xsbTimeAfterScreenOn.visibility = View.GONE
} else {
binding!!.xsbTimeAfterScreenOff.visibility = View.GONE
binding!!.xsbTimeAfterScreenOn.visibility = View.VISIBLE
}
}
Log.d(TAG, "initViews eventData:$eventData")
if (eventData != null) {
val settingVo = Gson().fromJson(eventData, LockScreenSetting::class.java)
Log.d(TAG, "initViews settingVo:$settingVo")
binding!!.rgAction.check(settingVo.getActionCheckId())
binding!!.xsbTimeAfterScreenOff.setDefaultValue(settingVo.timeAfterScreenOff)
binding!!.xsbTimeAfterScreenOn.setDefaultValue(settingVo.timeAfterScreenOn)
} else {
binding!!.xsbTimeAfterScreenOff.setDefaultValue(0)
binding!!.xsbTimeAfterScreenOn.setDefaultValue(0)
}
}
@SuppressLint("SetTextI18n")
override fun initListeners() {
binding!!.btnTest.setOnClickListener(this)
binding!!.btnDel.setOnClickListener(this)
binding!!.btnSave.setOnClickListener(this)
LiveEventBus.get(KEY_TEST_CONDITION, String::class.java).observe(this) {
mCountDownHelper?.finish()
if (it == "success") {
XToastUtils.success("测试通过", 30000)
} else {
XToastUtils.error(it, 30000)
}
}
}
@SingleClick
override fun onClick(v: View) {
try {
when (v.id) {
R.id.btn_test -> {
mCountDownHelper?.start()
Thread {
try {
val settingVo = checkSetting()
Log.d(TAG, settingVo.toString())
LiveEventBus.get(KEY_TEST_CONDITION, String::class.java).post("success")
} catch (e: Exception) {
LiveEventBus.get(KEY_TEST_CONDITION, String::class.java).post(e.message.toString())
e.printStackTrace()
}
}.start()
return
}
R.id.btn_del -> {
popToBack()
return
}
R.id.btn_save -> {
val settingVo = checkSetting()
val intent = Intent()
intent.putExtra(KEY_BACK_DESCRIPTION_CONDITION, settingVo.description)
intent.putExtra(KEY_BACK_DATA_CONDITION, Gson().toJson(settingVo))
setFragmentResult(TASK_CONDITION_LOCK_SCREEN, intent)
popToBack()
return
}
}
} catch (e: Exception) {
XToastUtils.error(e.message.toString(), 30000)
e.printStackTrace()
}
}
//检查设置
@SuppressLint("SetTextI18n")
private fun checkSetting(): LockScreenSetting {
val actionCheckId = binding!!.rgAction.checkedRadioButtonId
val timeAfterScreenOff = binding!!.xsbTimeAfterScreenOff.selectedNumber
val timeAfterScreenOn = binding!!.xsbTimeAfterScreenOn.selectedNumber
return LockScreenSetting(actionCheckId, timeAfterScreenOff, timeAfterScreenOn)
}
}

@ -0,0 +1,32 @@
package com.idormy.sms.forwarder.receiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.workDataOf
import com.idormy.sms.forwarder.utils.TASK_CONDITION_LOCK_SCREEN
import com.idormy.sms.forwarder.utils.TaskWorker
import com.idormy.sms.forwarder.workers.LockScreenWorker
@Suppress("PropertyName")
class LockScreenReceiver : BroadcastReceiver() {
val TAG: String = LockScreenReceiver::class.java.simpleName
override fun onReceive(context: Context?, intent: Intent?) {
if (context == null || (intent?.action != Intent.ACTION_SCREEN_OFF && intent?.action != Intent.ACTION_SCREEN_ON)) return
Log.d(TAG, "onReceive: ${intent.action}")
val request = OneTimeWorkRequestBuilder<LockScreenWorker>().setInputData(
workDataOf(
TaskWorker.conditionType to TASK_CONDITION_LOCK_SCREEN,
TaskWorker.action to intent.action,
)
).build()
WorkManager.getInstance(context).enqueue(request)
}
}

@ -25,6 +25,7 @@ object TaskWorker {
const val conditionType = "condition_type"
const val msg = "msg"
const val msgInfo = "msg_info"
const val action = "action"
}
//初始化相关

@ -0,0 +1,80 @@
package com.idormy.sms.forwarder.workers
import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.work.CoroutineWorker
import androidx.work.Data
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.WorkerParameters
import com.google.gson.Gson
import com.idormy.sms.forwarder.App
import com.idormy.sms.forwarder.database.AppDatabase
import com.idormy.sms.forwarder.entity.MsgInfo
import com.idormy.sms.forwarder.entity.task.LockScreenSetting
import com.idormy.sms.forwarder.entity.task.TaskSetting
import com.idormy.sms.forwarder.utils.TaskWorker
import java.util.Date
import java.util.concurrent.TimeUnit
@Suppress("PrivatePropertyName", "DEPRECATION")
class LockScreenWorker(context: Context, params: WorkerParameters) : CoroutineWorker(context, params) {
private val TAG: String = LockScreenWorker::class.java.simpleName
override suspend fun doWork(): Result {
val conditionType = inputData.getInt(TaskWorker.conditionType, -1)
val action = inputData.getString(TaskWorker.action)
val taskList = AppDatabase.getInstance(App.context).taskDao().getByType(conditionType)
for (task in taskList) {
Log.d(TAG, "task = $task")
// 根据任务信息执行相应操作
val conditionList = Gson().fromJson(task.conditions, Array<TaskSetting>::class.java).toMutableList()
if (conditionList.isEmpty()) {
Log.d(TAG, "任务${task.id}conditionList is empty")
continue
}
val firstCondition = conditionList.firstOrNull()
if (firstCondition == null) {
Log.d(TAG, "任务${task.id}firstCondition is null")
continue
}
val lockScreenSetting = Gson().fromJson(firstCondition.setting, LockScreenSetting::class.java)
if (lockScreenSetting == null) {
Log.d(TAG, "任务${task.id}lockScreenSetting is null")
continue
}
if (action != lockScreenSetting.action) {
Log.d(TAG, "任务${task.id}action is not match, action = $action, lockScreenSetting = $lockScreenSetting")
continue
}
val duration = if (action == Intent.ACTION_SCREEN_ON) lockScreenSetting.timeAfterScreenOn else lockScreenSetting.timeAfterScreenOff
//TODO判断其他条件是否满足
//TODO: 组装消息体 && 执行具体任务
val msgInfo = MsgInfo("task", task.name, lockScreenSetting.description, Date(), task.description)
val actionData = Data.Builder()
.putLong(TaskWorker.taskId, task.id)
.putString(TaskWorker.taskActions, task.actions)
.putString(TaskWorker.msgInfo, Gson().toJson(msgInfo))
.build()
val actionRequest = OneTimeWorkRequestBuilder<ActionWorker>()
.setInitialDelay(duration.toLong(), TimeUnit.MINUTES)
.setInputData(actionData).build()
WorkManager.getInstance().enqueue(actionRequest)
}
return Result.success()
}
}

@ -65,7 +65,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:xsb_insideRangeLineColor="#0bd97f"
app:xsb_insideRangeLineStrokeWidth="5dp"
app:xsb_insideRangeLineStrokeWidth="10dp"
app:xsb_isShowBubble="true"
app:xsb_isShowRuler="true"
app:xsb_max="100"
@ -73,8 +73,9 @@
app:xsb_numberTextColor="#ffffff"
app:xsb_numberTextSize="15sp"
app:xsb_outsideRangeLineColor="#f0f0f0"
app:xsb_outsideRangeLineStrokeWidth="5dp"
app:xsb_rulerColor="@color/xui_config_color_gray_4" />
app:xsb_outsideRangeLineStrokeWidth="10dp"
app:xsb_rulerColor="@color/xui_config_color_gray_4"
app:xsb_rulerInterval="ten" />
<RadioButton
android:id="@+id/rb_battery_charging"
@ -88,7 +89,7 @@
android:layout_height="wrap_content"
android:visibility="gone"
app:xsb_insideRangeLineColor="#0bd97f"
app:xsb_insideRangeLineStrokeWidth="5dp"
app:xsb_insideRangeLineStrokeWidth="10dp"
app:xsb_isShowBubble="true"
app:xsb_isShowRuler="true"
app:xsb_max="100"
@ -96,8 +97,9 @@
app:xsb_numberTextColor="#ffffff"
app:xsb_numberTextSize="15sp"
app:xsb_outsideRangeLineColor="#f0f0f0"
app:xsb_outsideRangeLineStrokeWidth="5dp"
app:xsb_rulerColor="@color/xui_config_color_gray_4" />
app:xsb_outsideRangeLineStrokeWidth="10dp"
app:xsb_rulerColor="@color/xui_config_color_gray_4"
app:xsb_rulerInterval="ten" />
</RadioGroup>

@ -0,0 +1,151 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/xui_config_color_background"
android:orientation="vertical">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:overScrollMode="never">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center_horizontal"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_margin="10dp"
android:contentDescription="@string/task_lock_screen"
app:srcCompat="@drawable/auto_task_icon_lock_screen"
tools:ignore="ImageContrastCheck" />
<LinearLayout
style="@style/taskBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/task_lock_screen"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="?attr/xui_config_color_separator_light" />
<RadioGroup
android:id="@+id/rg_action"
style="@style/rg_style"
android:orientation="vertical"
android:paddingBottom="@dimen/config_padding_5dp">
<RadioButton
android:id="@+id/rb_action_screen_off"
style="@style/rg_rb_style_match"
android:checked="true"
android:text="@string/time_after_screen_off"
tools:ignore="TouchTargetSizeCheck" />
<com.xuexiang.xui.widget.picker.XSeekBar
android:id="@+id/xsb_time_after_screen_off"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:xsb_insideRangeLineColor="#0bd97f"
app:xsb_insideRangeLineStrokeWidth="10dp"
app:xsb_isShowBubble="true"
app:xsb_isShowRuler="true"
app:xsb_max="30"
app:xsb_min="0"
app:xsb_numberTextColor="#ffffff"
app:xsb_numberTextSize="15sp"
app:xsb_outsideRangeLineColor="#f0f0f0"
app:xsb_outsideRangeLineStrokeWidth="10dp"
app:xsb_rulerColor="@color/xui_config_color_gray_4"
app:xsb_rulerInterval="ten" />
<RadioButton
android:id="@+id/rb_action_screen_on"
style="@style/rg_rb_style_match"
android:text="@string/time_after_screen_on"
tools:ignore="TouchTargetSizeCheck" />
<com.xuexiang.xui.widget.picker.XSeekBar
android:id="@+id/xsb_time_after_screen_on"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
app:xsb_insideRangeLineColor="#0bd97f"
app:xsb_insideRangeLineStrokeWidth="10dp"
app:xsb_isShowBubble="true"
app:xsb_isShowRuler="true"
app:xsb_max="30"
app:xsb_min="0"
app:xsb_numberTextColor="#ffffff"
app:xsb_numberTextSize="15sp"
app:xsb_outsideRangeLineColor="#f0f0f0"
app:xsb_outsideRangeLineStrokeWidth="10dp"
app:xsb_rulerColor="@color/xui_config_color_gray_4"
app:xsb_rulerInterval="ten" />
</RadioGroup>
</LinearLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:padding="10dp">
<com.xuexiang.xui.widget.textview.supertextview.SuperButton
android:id="@+id/btn_del"
style="@style/SuperButton.Gray.Icon"
android:drawableStart="@drawable/icon_delete"
android:paddingStart="15dp"
android:text="@string/discard"
android:textSize="11sp"
tools:ignore="RtlSymmetry,TextContrastCheck,TouchTargetSizeCheck" />
<com.xuexiang.xui.widget.textview.supertextview.SuperButton
android:id="@+id/btn_save"
style="@style/SuperButton.Blue.Icon"
android:layout_marginStart="10dp"
android:drawableStart="@drawable/icon_save"
android:paddingStart="15dp"
android:text="@string/submit"
android:textSize="11sp"
tools:ignore="RtlSymmetry,TextContrastCheck,TouchTargetSizeCheck" />
<com.xuexiang.xui.widget.textview.supertextview.SuperButton
android:id="@+id/btn_test"
style="@style/SuperButton.Green.Icon"
android:layout_marginStart="10dp"
android:drawableStart="@drawable/icon_test"
android:paddingStart="15dp"
android:text="@string/test"
android:textSize="11sp"
android:visibility="gone"
tools:ignore="RtlSymmetry" />
</LinearLayout>
</LinearLayout>

@ -1122,7 +1122,7 @@
<string name="task_sim">SIM Status</string>
<string name="task_battery">Battery</string>
<string name="task_charge">Charge</string>
<string name="task_lock_screen">Lock Screen</string>
<string name="task_lock_screen">Screen Off/On</string>
<string name="task_sendsms">Send Sms</string>
<string name="task_notification">Notification</string>
<string name="task_frpc">Frpc Setting</string>
@ -1200,4 +1200,10 @@
<string name="sim_any">Any SIM</string>
<string name="sim_1">SIM-1</string>
<string name="sim_2">SIM-2</string>
<string name="time_after_screen_off">Time After Screen Off (Minutes)</string>
<string name="time_after_screen_off_description">%sAfter Screen Off</string>
<string name="time_after_screen_on">Time After Screen On (Minutes)</string>
<string name="time_after_screen_on_description">%sAfter Screen On</string>
<string name="duration_minute">%s minutes </string>
</resources>

@ -1123,7 +1123,7 @@
<string name="task_sim">SIM卡状态</string>
<string name="task_battery">电量使用</string>
<string name="task_charge">充电状态</string>
<string name="task_lock_screen">屏幕锁定</string>
<string name="task_lock_screen">锁屏解锁</string>
<string name="task_sendsms">发送短信</string>
<string name="task_notification">通道推送</string>
<string name="task_frpc">Frpc设置</string>
@ -1201,4 +1201,10 @@
<string name="sim_any">不限卡槽</string>
<string name="sim_1">SIM-1</string>
<string name="sim_2">SIM-2</string>
<string name="time_after_screen_off">屏幕锁定后多长时间(分钟)</string>
<string name="time_after_screen_off_description">屏幕锁定%s后</string>
<string name="time_after_screen_on">屏幕解锁后多长时间(分钟)</string>
<string name="time_after_screen_on_description">屏幕解锁%s后</string>
<string name="duration_minute">%s分钟</string>
</resources>

Loading…
Cancel
Save