Skip to content

Commit

Permalink
优化:监听Screen事件细分On/Off/Locked/Unlocked #399
Browse files Browse the repository at this point in the history
  • Loading branch information
pppscn committed Feb 16, 2024
1 parent d662390 commit 098a8f1
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 36 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.ACTION_USER_PRESENT" />
</intent-filter>
</receiver>
<receiver
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/idormy/sms/forwarder/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ class App : Application(), CactusCallback, Configuration.Provider by Core {
val lockScreenFilter = IntentFilter().apply {
addAction(Intent.ACTION_SCREEN_OFF)
addAction(Intent.ACTION_SCREEN_ON)
addAction(Intent.ACTION_USER_PRESENT)
}
registerReceiver(lockScreenReceiver, lockScreenFilter)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,37 @@ 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, //解锁后时间
var timeAfterScreenOff: Int = 5, //熄屏后时间
var timeAfterScreenOn: Int = 5, //开锁后时间
var timeAfterScreenLocked: Int = 5, //锁屏后时间
var timeAfterScreenUnlocked: 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
constructor(actionCheckId: Int, timeAfterOff: Int, timeAfterOn: Int, timeAfterLocked: Int, timeAfterUnlocked: Int) : this() {
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)
action = Intent.ACTION_SCREEN_ON
}

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)
action = Intent.ACTION_USER_PRESENT
}

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)
action = Intent.ACTION_SCREEN_OFF + "_LOCKED"
}

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
Expand All @@ -30,7 +48,9 @@ data class LockScreenSetting(
fun getActionCheckId(): Int {
return when (action) {
Intent.ACTION_SCREEN_ON -> R.id.rb_action_screen_on
else -> R.id.rb_action_screen_off
Intent.ACTION_SCREEN_OFF -> R.id.rb_action_screen_off
Intent.ACTION_USER_PRESENT -> R.id.rb_action_screen_unlocked
else -> R.id.rb_action_screen_locked
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,15 @@ class LockScreenFragment : BaseFragment<FragmentTasksConditionLockScreenBinding?
*/
override fun initViews() {
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
binding!!.xsbTimeAfterScreenOff.visibility = View.GONE
binding!!.xsbTimeAfterScreenLocked.visibility = View.GONE
binding!!.xsbTimeAfterScreenOn.visibility = View.GONE
binding!!.xsbTimeAfterScreenUnlocked.visibility = View.GONE
when (checkedId) {
R.id.rb_action_screen_on -> binding!!.xsbTimeAfterScreenOn.visibility = View.VISIBLE
R.id.rb_action_screen_unlocked -> binding!!.xsbTimeAfterScreenUnlocked.visibility = View.VISIBLE
R.id.rb_action_screen_locked -> binding!!.xsbTimeAfterScreenLocked.visibility = View.VISIBLE
else -> binding!!.xsbTimeAfterScreenOff.visibility = View.VISIBLE
}
checkSetting(true)
}
Expand All @@ -71,10 +74,14 @@ class LockScreenFragment : BaseFragment<FragmentTasksConditionLockScreenBinding?
binding!!.tvDescription.text = settingVo.description
binding!!.xsbTimeAfterScreenOff.setDefaultValue(settingVo.timeAfterScreenOff)
binding!!.xsbTimeAfterScreenOn.setDefaultValue(settingVo.timeAfterScreenOn)
binding!!.xsbTimeAfterScreenLocked.setDefaultValue(settingVo.timeAfterScreenLocked)
binding!!.xsbTimeAfterScreenUnlocked.setDefaultValue(settingVo.timeAfterScreenUnlocked)
binding!!.rgAction.check(settingVo.getActionCheckId())
} else {
binding!!.xsbTimeAfterScreenOff.setDefaultValue(0)
binding!!.xsbTimeAfterScreenOn.setDefaultValue(0)
binding!!.xsbTimeAfterScreenLocked.setDefaultValue(0)
binding!!.xsbTimeAfterScreenUnlocked.setDefaultValue(0)
}
}

Expand Down Expand Up @@ -123,7 +130,9 @@ class LockScreenFragment : BaseFragment<FragmentTasksConditionLockScreenBinding?
val actionCheckId = binding!!.rgAction.checkedRadioButtonId
val timeAfterScreenOff = binding!!.xsbTimeAfterScreenOff.selectedNumber
val timeAfterScreenOn = binding!!.xsbTimeAfterScreenOn.selectedNumber
val settingVo = LockScreenSetting(actionCheckId, timeAfterScreenOff, timeAfterScreenOn)
val timeAferScreenLocked = binding!!.xsbTimeAfterScreenLocked.selectedNumber
val timeAfterScreenUnlocked = binding!!.xsbTimeAfterScreenUnlocked.selectedNumber
val settingVo = LockScreenSetting(actionCheckId, timeAfterScreenOff, timeAfterScreenOn, timeAferScreenLocked, timeAfterScreenUnlocked)

if (updateView) {
binding!!.tvDescription.text = settingVo.description
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.idormy.sms.forwarder.receiver

import android.app.KeyguardManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.idormy.sms.forwarder.utils.Log
import android.os.Build
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.workDataOf
import com.idormy.sms.forwarder.utils.Log
import com.idormy.sms.forwarder.utils.TASK_CONDITION_LOCK_SCREEN
import com.idormy.sms.forwarder.utils.TaskWorker
import com.idormy.sms.forwarder.utils.task.TaskUtils
Expand All @@ -19,16 +21,31 @@ class LockScreenReceiver : BroadcastReceiver() {

override fun onReceive(context: Context?, intent: Intent?) {

if (context == null || (intent?.action != Intent.ACTION_SCREEN_OFF && intent?.action != Intent.ACTION_SCREEN_ON)) return
if (context == null || (intent?.action != Intent.ACTION_SCREEN_OFF && intent?.action != Intent.ACTION_SCREEN_ON && intent?.action != Intent.ACTION_USER_PRESENT)) return

var action = intent.action.toString()
if (action == Intent.ACTION_SCREEN_OFF && isDeviceLocked(context)) {
action += "_LOCKED"
}

Log.d(TAG, "onReceive: ${intent.action}")
TaskUtils.lockScreenAction = intent.action.toString()
Log.d(TAG, "onReceive: action=$action")
TaskUtils.lockScreenAction = action
val request = OneTimeWorkRequestBuilder<LockScreenWorker>().setInputData(
workDataOf(
TaskWorker.conditionType to TASK_CONDITION_LOCK_SCREEN,
TaskWorker.action to intent.action,
TaskWorker.action to action,
)
).build()
WorkManager.getInstance(context).enqueue(request)
}

private fun isDeviceLocked(context: Context?): Boolean {
val keyguardManager = context?.getSystemService(Context.KEYGUARD_SERVICE) as? KeyguardManager
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
keyguardManager?.isDeviceLocked ?: false
} else {
// 对于较早版本的 Android,无法直接检查设备锁定状态
false
}
}
}
48 changes: 48 additions & 0 deletions app/src/main/res/layout/fragment_tasks_condition_lock_screen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,30 @@
app:xsb_rulerColor="@color/xui_config_color_gray_4"
app:xsb_rulerInterval="ten" />

<RadioButton
android:id="@+id/rb_action_screen_locked"
style="@style/rg_rb_style_match"
android:text="@string/time_after_screen_locked"
tools:ignore="TouchTargetSizeCheck" />

<com.xuexiang.xui.widget.picker.XSeekBar
android:id="@+id/xsb_time_after_screen_locked"
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="@dimen/text_size_small"
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"
Expand All @@ -110,6 +134,30 @@
app:xsb_rulerColor="@color/xui_config_color_gray_4"
app:xsb_rulerInterval="ten" />

<RadioButton
android:id="@+id/rb_action_screen_unlocked"
style="@style/rg_rb_style_match"
android:text="@string/time_after_screen_unlocked"
tools:ignore="TouchTargetSizeCheck" />

<com.xuexiang.xui.widget.picker.XSeekBar
android:id="@+id/xsb_time_after_screen_unlocked"
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="@dimen/text_size_small"
app:xsb_outsideRangeLineColor="#f0f0f0"
app:xsb_outsideRangeLineStrokeWidth="10dp"
app:xsb_rulerColor="@color/xui_config_color_gray_4"
app:xsb_rulerInterval="ten" />

</RadioGroup>

</LinearLayout>
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values-en/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,10 @@
<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="time_after_screen_locked">Time After Screen Locked (Minutes)</string>
<string name="time_after_screen_locked_description">%sAfter Screen Locked</string>
<string name="time_after_screen_unlocked">Time After Screen Unlocked (Minutes)</string>
<string name="time_after_screen_unlocked_description">%sAfter Screen Unlocked</string>
<string name="duration_minute">%s minutes </string>

<string name="calc_type_distance">Calculate distance based on GPS coordinates.</string>
Expand Down
12 changes: 8 additions & 4 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1265,10 +1265,14 @@
<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="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="time_after_screen_locked">屏幕锁定后多长时间(分钟)</string>
<string name="time_after_screen_locked_description">屏幕锁定%s后</string>
<string name="time_after_screen_unlocked">屏幕解锁后多长时间(分钟)</string>
<string name="time_after_screen_unlocked_description">屏幕解锁%s后</string>
<string name="duration_minute">%s分钟</string>

<string name="calc_type_distance">根据GPS坐标计算距离</string>
Expand Down
12 changes: 8 additions & 4 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1265,10 +1265,14 @@
<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="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="time_after_screen_locked">屏幕鎖定後多長時間(分鐘)</string>
<string name="time_after_screen_locked_description">屏幕鎖定%s后</string>
<string name="time_after_screen_unlocked">屏幕解鎖後多長時間(分鐘)</string>
<string name="time_after_screen_unlocked_description">屏幕解鎖%s后</string>
<string name="duration_minute">%s分鐘</string>

<string name="calc_type_distance">根據GPS坐標計算距離</string>
Expand Down
12 changes: 8 additions & 4 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1265,10 +1265,14 @@
<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="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="time_after_screen_locked">屏幕锁定后多长时间(分钟)</string>
<string name="time_after_screen_locked_description">屏幕锁定%s后</string>
<string name="time_after_screen_unlocked">屏幕解锁后多长时间(分钟)</string>
<string name="time_after_screen_unlocked_description">屏幕解锁%s后</string>
<string name="duration_minute">%s分钟</string>

<string name="calc_type_distance">根据GPS坐标计算距离</string>
Expand Down

0 comments on commit 098a8f1

Please sign in to comment.