Skip to content

Commit 7d7ddc0

Browse files
committed
#1392 feat: add action to enable/disable/toggle night shift
1 parent e21ccff commit 7d7ddc0

File tree

9 files changed

+113
-3
lines changed

9 files changed

+113
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
- [#1990](https://github.com/keymapperorg/KeyMapper/issues/1990) Passthrough the device id of the trigger to the key event action if one is not manually specified
2121
- [#1982](https://github.com/keymapperorg/KeyMapper/issues/1982) Text action does not need Key Mapper input method on Android 13+.
2222
- [#1989](https://github.com/keymapperorg/KeyMapper/issues/1989) center the "Trigger and actions" and "Constraint and more" tabs.
23+
- [#1392](https://github.com/keymapperorg/KeyMapper/issues/1392) Add action to enable/disable/toggle night shift.
2324
- Bugs with expert mode auto starting time.
2425

2526
## [4.0.0 Beta 6](https://github.com/sds100/KeyMapper/releases/tag/v4.0.0-beta.06)

base/src/main/java/io/github/sds100/keymapper/base/actions/ActionData.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,24 @@ sealed class ActionData : Comparable<ActionData> {
714714
}
715715
}
716716

717+
@Serializable
718+
sealed class NightShift : ActionData() {
719+
@Serializable
720+
data object Enable : NightShift() {
721+
override val id = ActionId.ENABLE_NIGHT_SHIFT
722+
}
723+
724+
@Serializable
725+
data object Disable : NightShift() {
726+
override val id = ActionId.DISABLE_NIGHT_SHIFT
727+
}
728+
729+
@Serializable
730+
data object Toggle : NightShift() {
731+
override val id = ActionId.TOGGLE_NIGHT_SHIFT
732+
}
733+
}
734+
717735
@Serializable
718736
sealed class StatusBar : ActionData() {
719737
@Serializable

base/src/main/java/io/github/sds100/keymapper/base/actions/ActionDataEntityMapper.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,10 @@ object ActionDataEntityMapper {
498498
ActionId.INCREASE_BRIGHTNESS -> ActionData.Brightness.Increase
499499
ActionId.DECREASE_BRIGHTNESS -> ActionData.Brightness.Decrease
500500

501+
ActionId.TOGGLE_NIGHT_SHIFT -> ActionData.NightShift.Toggle
502+
ActionId.ENABLE_NIGHT_SHIFT -> ActionData.NightShift.Enable
503+
ActionId.DISABLE_NIGHT_SHIFT -> ActionData.NightShift.Disable
504+
501505
ActionId.TOGGLE_AUTO_ROTATE -> ActionData.Rotation.ToggleAuto
502506
ActionId.ENABLE_AUTO_ROTATE -> ActionData.Rotation.EnableAuto
503507
ActionId.DISABLE_AUTO_ROTATE -> ActionData.Rotation.DisableAuto
@@ -1238,6 +1242,10 @@ object ActionDataEntityMapper {
12381242
ActionId.INCREASE_BRIGHTNESS to "increase_brightness",
12391243
ActionId.DECREASE_BRIGHTNESS to "decrease_brightness",
12401244

1245+
ActionId.TOGGLE_NIGHT_SHIFT to "toggle_night_shift",
1246+
ActionId.ENABLE_NIGHT_SHIFT to "enable_night_shift",
1247+
ActionId.DISABLE_NIGHT_SHIFT to "disable_night_shift",
1248+
12411249
ActionId.TOGGLE_AUTO_ROTATE to "toggle_auto_rotate",
12421250
ActionId.ENABLE_AUTO_ROTATE to "enable_auto_rotate",
12431251
ActionId.DISABLE_AUTO_ROTATE to "disable_auto_rotate",

base/src/main/java/io/github/sds100/keymapper/base/actions/ActionId.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ enum class ActionId {
3838
INCREASE_BRIGHTNESS,
3939
DECREASE_BRIGHTNESS,
4040

41+
TOGGLE_NIGHT_SHIFT,
42+
ENABLE_NIGHT_SHIFT,
43+
DISABLE_NIGHT_SHIFT,
44+
4145
TOGGLE_AUTO_ROTATE,
4246
ENABLE_AUTO_ROTATE,
4347
DISABLE_AUTO_ROTATE,

base/src/main/java/io/github/sds100/keymapper/base/actions/ActionUiHelper.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,10 @@ class ActionUiHelper(
475475
ActionData.Brightness.Increase -> getString(R.string.action_increase_brightness)
476476
ActionData.Brightness.ToggleAuto -> getString(R.string.action_toggle_auto_brightness)
477477

478+
ActionData.NightShift.Disable -> getString(R.string.action_disable_night_shift)
479+
ActionData.NightShift.Enable -> getString(R.string.action_enable_night_shift)
480+
ActionData.NightShift.Toggle -> getString(R.string.action_toggle_night_shift)
481+
478482
ActionData.ConsumeKeyEvent -> getString(R.string.action_consume_keyevent)
479483

480484
ActionData.ControlMedia.FastForward -> getString(R.string.action_fast_forward)

base/src/main/java/io/github/sds100/keymapper/base/actions/ActionUtils.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import androidx.compose.material.icons.outlined.Mic
4545
import androidx.compose.material.icons.outlined.MicOff
4646
import androidx.compose.material.icons.outlined.MoreVert
4747
import androidx.compose.material.icons.outlined.Nfc
48+
import androidx.compose.material.icons.outlined.Nightlight
4849
import androidx.compose.material.icons.outlined.NotStarted
4950
import androidx.compose.material.icons.outlined.Pause
5051
import androidx.compose.material.icons.outlined.PhonelinkRing
@@ -148,6 +149,9 @@ object ActionUtils {
148149
ActionId.ENABLE_AUTO_BRIGHTNESS -> ActionCategory.DISPLAY
149150
ActionId.INCREASE_BRIGHTNESS -> ActionCategory.DISPLAY
150151
ActionId.DECREASE_BRIGHTNESS -> ActionCategory.DISPLAY
152+
ActionId.TOGGLE_NIGHT_SHIFT -> ActionCategory.DISPLAY
153+
ActionId.ENABLE_NIGHT_SHIFT -> ActionCategory.DISPLAY
154+
ActionId.DISABLE_NIGHT_SHIFT -> ActionCategory.DISPLAY
151155
ActionId.SCREENSHOT -> ActionCategory.DISPLAY
152156
ActionId.TOGGLE_AUTO_ROTATE -> ActionCategory.INTERFACE
153157
ActionId.ENABLE_AUTO_ROTATE -> ActionCategory.INTERFACE
@@ -275,6 +279,12 @@ object ActionUtils {
275279

276280
ActionId.DECREASE_BRIGHTNESS -> R.string.action_decrease_brightness
277281

282+
ActionId.TOGGLE_NIGHT_SHIFT -> R.string.action_toggle_night_shift
283+
284+
ActionId.ENABLE_NIGHT_SHIFT -> R.string.action_enable_night_shift
285+
286+
ActionId.DISABLE_NIGHT_SHIFT -> R.string.action_disable_night_shift
287+
278288
ActionId.TOGGLE_AUTO_ROTATE -> R.string.action_toggle_auto_rotate
279289

280290
ActionId.ENABLE_AUTO_ROTATE -> R.string.action_enable_auto_rotate
@@ -681,6 +691,11 @@ object ActionUtils {
681691
ActionId.DISABLE_HOTSPOT,
682692
-> Build.VERSION_CODES.R
683693

694+
ActionId.TOGGLE_NIGHT_SHIFT,
695+
ActionId.ENABLE_NIGHT_SHIFT,
696+
ActionId.DISABLE_NIGHT_SHIFT,
697+
-> Build.VERSION_CODES.Q
698+
684699
else -> Constants.MIN_API
685700
}
686701

@@ -840,6 +855,11 @@ object ActionUtils {
840855
ActionId.DECREASE_BRIGHTNESS,
841856
-> return listOf(Permission.WRITE_SETTINGS)
842857

858+
ActionId.TOGGLE_NIGHT_SHIFT,
859+
ActionId.ENABLE_NIGHT_SHIFT,
860+
ActionId.DISABLE_NIGHT_SHIFT,
861+
-> return listOf(Permission.WRITE_SECURE_SETTINGS)
862+
843863
ActionId.TOGGLE_FLASHLIGHT,
844864
ActionId.ENABLE_FLASHLIGHT,
845865
ActionId.DISABLE_FLASHLIGHT,
@@ -935,6 +955,9 @@ object ActionUtils {
935955
ActionId.ENABLE_AUTO_BRIGHTNESS -> Icons.Outlined.BrightnessAuto
936956
ActionId.INCREASE_BRIGHTNESS -> Icons.Outlined.BrightnessHigh
937957
ActionId.DECREASE_BRIGHTNESS -> Icons.Outlined.BrightnessLow
958+
ActionId.TOGGLE_NIGHT_SHIFT -> Icons.Outlined.Nightlight
959+
ActionId.ENABLE_NIGHT_SHIFT -> Icons.Outlined.Nightlight
960+
ActionId.DISABLE_NIGHT_SHIFT -> Icons.Outlined.Nightlight
938961
ActionId.TOGGLE_AUTO_ROTATE -> Icons.Outlined.ScreenRotation
939962
ActionId.ENABLE_AUTO_ROTATE -> Icons.Outlined.ScreenRotation
940963
ActionId.DISABLE_AUTO_ROTATE -> Icons.Outlined.ScreenLockRotation

base/src/main/java/io/github/sds100/keymapper/base/actions/CreateActionDelegate.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,12 @@ class CreateActionDelegate(
986986

987987
ActionId.DECREASE_BRIGHTNESS -> return ActionData.Brightness.Decrease
988988

989+
ActionId.TOGGLE_NIGHT_SHIFT -> return ActionData.NightShift.Toggle
990+
991+
ActionId.ENABLE_NIGHT_SHIFT -> return ActionData.NightShift.Enable
992+
993+
ActionId.DISABLE_NIGHT_SHIFT -> return ActionData.NightShift.Disable
994+
989995
ActionId.TOGGLE_AUTO_ROTATE -> return ActionData.Rotation.ToggleAuto
990996

991997
ActionId.ENABLE_AUTO_ROTATE -> return ActionData.Rotation.EnableAuto

base/src/main/java/io/github/sds100/keymapper/base/actions/PerformActionsUseCase.kt

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ import io.github.sds100.keymapper.system.phone.PhoneAdapter
6565
import io.github.sds100.keymapper.system.popup.ToastAdapter
6666
import io.github.sds100.keymapper.system.ringtones.RingtoneAdapter
6767
import io.github.sds100.keymapper.system.root.SuAdapter
68+
import io.github.sds100.keymapper.system.settings.SettingType
69+
import io.github.sds100.keymapper.system.settings.SettingsAdapter
6870
import io.github.sds100.keymapper.system.shell.ShellAdapter
6971
import io.github.sds100.keymapper.system.url.OpenUrlAdapter
7072
import io.github.sds100.keymapper.system.volume.RingerMode
@@ -118,9 +120,13 @@ class PerformActionsUseCaseImpl @AssistedInject constructor(
118120
private val settingsRepository: PreferenceRepository,
119121
private val inputEventHub: InputEventHub,
120122
private val systemBridgeConnectionManager: SystemBridgeConnectionManager,
121-
private val settingsAdapter: io.github.sds100.keymapper.system.settings.SettingsAdapter,
123+
private val settingsAdapter: SettingsAdapter,
122124
) : PerformActionsUseCase {
123125

126+
companion object {
127+
private const val SETTING_NIGHT_DISPLAY_ACTIVATED = "night_display_activated"
128+
}
129+
124130
@AssistedFactory
125131
interface Factory {
126132
fun create(
@@ -463,11 +469,19 @@ class PerformActionsUseCaseImpl @AssistedInject constructor(
463469
}
464470

465471
is ActionData.Hotspot.Enable -> {
466-
result = networkAdapter.enableHotspot()
472+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
473+
result = networkAdapter.enableHotspot()
474+
} else {
475+
result = SdkVersionTooLow(minSdk = Build.VERSION_CODES.R)
476+
}
467477
}
468478

469479
is ActionData.Hotspot.Disable -> {
470-
result = networkAdapter.disableHotspot()
480+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
481+
result = networkAdapter.disableHotspot()
482+
} else {
483+
result = SdkVersionTooLow(minSdk = Build.VERSION_CODES.R)
484+
}
471485
}
472486

473487
is ActionData.Brightness.ToggleAuto -> {
@@ -1043,6 +1057,35 @@ class PerformActionsUseCaseImpl @AssistedInject constructor(
10431057
action.value,
10441058
)
10451059
}
1060+
1061+
is ActionData.NightShift.Enable -> {
1062+
result = settingsAdapter.setValue(
1063+
SettingType.SECURE,
1064+
SETTING_NIGHT_DISPLAY_ACTIVATED,
1065+
"1",
1066+
)
1067+
}
1068+
1069+
is ActionData.NightShift.Disable -> {
1070+
result = settingsAdapter.setValue(
1071+
SettingType.SECURE,
1072+
SETTING_NIGHT_DISPLAY_ACTIVATED,
1073+
"0",
1074+
)
1075+
}
1076+
1077+
is ActionData.NightShift.Toggle -> {
1078+
val currentValue = settingsAdapter.getValue(
1079+
SettingType.SECURE,
1080+
SETTING_NIGHT_DISPLAY_ACTIVATED,
1081+
)
1082+
val newValue = if (currentValue == "1") "0" else "1"
1083+
result = settingsAdapter.setValue(
1084+
SettingType.SECURE,
1085+
SETTING_NIGHT_DISPLAY_ACTIVATED,
1086+
newValue,
1087+
)
1088+
}
10461089
}
10471090

10481091
when (result) {

base/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,9 @@
961961
<string name="action_enable_auto_brightness">Enable auto brightness</string>
962962
<string name="action_increase_brightness">Increase display brightness</string>
963963
<string name="action_decrease_brightness">Decrease display brightness</string>
964+
<string name="action_toggle_night_shift">Toggle night shift</string>
965+
<string name="action_enable_night_shift">Enable night shift</string>
966+
<string name="action_disable_night_shift">Disable night shift</string>
964967

965968
<string name="action_expand_notification_drawer">Expand notification drawer</string>
966969
<string name="action_toggle_notification_drawer">Toggle notification drawer</string>

0 commit comments

Comments
 (0)