新增:自动任务·快捷指令 —— 锁屏解锁 #370
parent
b94a25c09d
commit
d8553ef793
@ -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)
|
||||
}
|
||||
}
|
@ -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>
|
Loading…
Reference in New Issue