Skip to content

Commit 2616a43

Browse files
committed
Merge branch 'copilot/hinge-closed-open-constraint' into develop
2 parents fa34fd5 + 5de977a commit 2616a43

File tree

16 files changed

+196
-0
lines changed

16 files changed

+196
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- #661 Action to execute shell commands.
2020
- #991 Consolidated volume and stream actions.
2121
- #1066 Action to mute/unmute microphone.
22+
- #985 Constraints for foldable hinge being open/closed.
2223

2324
## Removed
2425

base/src/main/java/io/github/sds100/keymapper/base/constraints/ChooseConstraintViewModel.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ class ChooseConstraintViewModel @Inject constructor(
8989
ConstraintId.CHARGING,
9090
ConstraintId.DISCHARGING,
9191

92+
ConstraintId.HINGE_CLOSED,
93+
ConstraintId.HINGE_OPEN,
94+
9295
ConstraintId.TIME,
9396
)
9497
}
@@ -214,6 +217,12 @@ class ChooseConstraintViewModel @Inject constructor(
214217
ConstraintId.DISCHARGING ->
215218
returnResult.emit(ConstraintData.Discharging)
216219

220+
ConstraintId.HINGE_CLOSED ->
221+
returnResult.emit(ConstraintData.HingeClosed)
222+
223+
ConstraintId.HINGE_OPEN ->
224+
returnResult.emit(ConstraintData.HingeOpen)
225+
217226
ConstraintId.LOCK_SCREEN_SHOWING ->
218227
returnResult.emit(ConstraintData.LockScreenShowing)
219228

base/src/main/java/io/github/sds100/keymapper/base/constraints/Constraint.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,16 @@ sealed class ConstraintData {
200200
override val id: ConstraintId = ConstraintId.DISCHARGING
201201
}
202202

203+
@Serializable
204+
data object HingeClosed : ConstraintData() {
205+
override val id: ConstraintId = ConstraintId.HINGE_CLOSED
206+
}
207+
208+
@Serializable
209+
data object HingeOpen : ConstraintData() {
210+
override val id: ConstraintId = ConstraintId.HINGE_OPEN
211+
}
212+
203213
@Serializable
204214
data class Time(
205215
val startHour: Int,
@@ -364,6 +374,9 @@ object ConstraintEntityMapper {
364374
ConstraintEntity.CHARGING -> ConstraintData.Charging
365375
ConstraintEntity.DISCHARGING -> ConstraintData.Discharging
366376

377+
ConstraintEntity.HINGE_CLOSED -> ConstraintData.HingeClosed
378+
ConstraintEntity.HINGE_OPEN -> ConstraintData.HingeOpen
379+
367380
ConstraintEntity.TIME -> {
368381
val startTime =
369382
entity.extras.getData(ConstraintEntity.EXTRA_START_TIME).valueOrNull()!!
@@ -628,6 +641,16 @@ object ConstraintEntityMapper {
628641
ConstraintEntity.DISCHARGING,
629642
)
630643

644+
is ConstraintData.HingeClosed -> ConstraintEntity(
645+
uid = constraint.uid,
646+
ConstraintEntity.HINGE_CLOSED,
647+
)
648+
649+
is ConstraintData.HingeOpen -> ConstraintEntity(
650+
uid = constraint.uid,
651+
ConstraintEntity.HINGE_OPEN,
652+
)
653+
631654
is ConstraintData.Time -> ConstraintEntity(
632655
uid = constraint.uid,
633656
type = ConstraintEntity.TIME,

base/src/main/java/io/github/sds100/keymapper/base/constraints/ConstraintDependency.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ enum class ConstraintDependency {
1515
LOCK_SCREEN_SHOWING,
1616
PHONE_STATE,
1717
CHARGING_STATE,
18+
HINGE_STATE,
1819
}

base/src/main/java/io/github/sds100/keymapper/base/constraints/ConstraintId.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,8 @@ enum class ConstraintId {
4949
CHARGING,
5050
DISCHARGING,
5151

52+
HINGE_CLOSED,
53+
HINGE_OPEN,
54+
5255
TIME,
5356
}

base/src/main/java/io/github/sds100/keymapper/base/constraints/ConstraintSnapshot.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package io.github.sds100.keymapper.base.constraints
22

33
import android.media.AudioManager
4+
import android.os.Build
45
import io.github.sds100.keymapper.base.system.accessibility.IAccessibilityService
56
import io.github.sds100.keymapper.common.utils.Orientation
67
import io.github.sds100.keymapper.common.utils.firstBlocking
78
import io.github.sds100.keymapper.system.bluetooth.BluetoothDeviceInfo
89
import io.github.sds100.keymapper.system.camera.CameraAdapter
910
import io.github.sds100.keymapper.system.devices.DevicesAdapter
1011
import io.github.sds100.keymapper.system.display.DisplayAdapter
12+
import io.github.sds100.keymapper.system.foldable.FoldableAdapter
13+
import io.github.sds100.keymapper.system.foldable.HingeState
1114
import io.github.sds100.keymapper.system.inputmethod.InputMethodAdapter
1215
import io.github.sds100.keymapper.system.lock.LockScreenAdapter
1316
import io.github.sds100.keymapper.system.media.MediaAdapter
@@ -31,6 +34,7 @@ class LazyConstraintSnapshot(
3134
lockScreenAdapter: LockScreenAdapter,
3235
phoneAdapter: PhoneAdapter,
3336
powerAdapter: PowerAdapter,
37+
private val foldableAdapter: FoldableAdapter,
3438
) : ConstraintSnapshot {
3539
private val appInForeground: String? by lazy { accessibilityService.rootNode?.packageName }
3640
private val connectedBluetoothDevices: Set<BluetoothDeviceInfo> by lazy { devicesAdapter.connectedBluetoothDevices.value }
@@ -141,6 +145,28 @@ class LazyConstraintSnapshot(
141145
is ConstraintData.Charging -> isCharging
142146
is ConstraintData.Discharging -> !isCharging
143147

148+
is ConstraintData.HingeClosed -> {
149+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
150+
when (val state = foldableAdapter.hingeState.value) {
151+
is HingeState.Available -> state.angle < 30f
152+
is HingeState.Unavailable -> false
153+
}
154+
} else {
155+
false
156+
}
157+
}
158+
159+
is ConstraintData.HingeOpen -> {
160+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
161+
when (val state = foldableAdapter.hingeState.value) {
162+
is HingeState.Available -> state.angle >= 150f
163+
is HingeState.Unavailable -> false
164+
}
165+
} else {
166+
false
167+
}
168+
}
169+
144170
// The keyguard manager still reports the lock screen as showing if you are in
145171
// an another activity like the camera app while the phone is locked.
146172
is ConstraintData.LockScreenShowing -> isLockscreenShowing && appInForeground == "com.android.systemui"

base/src/main/java/io/github/sds100/keymapper/base/constraints/ConstraintUiHelper.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ class ConstraintUiHelper(
142142
is ConstraintData.PhoneRinging -> getString(R.string.constraint_phone_ringing)
143143
is ConstraintData.Charging -> getString(R.string.constraint_charging)
144144
is ConstraintData.Discharging -> getString(R.string.constraint_discharging)
145+
is ConstraintData.HingeClosed -> getString(R.string.constraint_hinge_closed_description)
146+
is ConstraintData.HingeOpen -> getString(R.string.constraint_hinge_open_description)
145147
is ConstraintData.LockScreenShowing -> getString(R.string.constraint_lock_screen_showing)
146148
is ConstraintData.LockScreenNotShowing -> getString(R.string.constraint_lock_screen_not_showing)
147149
is ConstraintData.Time -> getString(

base/src/main/java/io/github/sds100/keymapper/base/constraints/ConstraintUtils.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ object ConstraintUtils {
7777

7878
ConstraintId.CHARGING -> ComposeIconInfo.Vector(Icons.Outlined.BatteryChargingFull)
7979
ConstraintId.DISCHARGING -> ComposeIconInfo.Vector(Icons.Outlined.Battery2Bar)
80+
81+
ConstraintId.HINGE_CLOSED -> ComposeIconInfo.Vector(Icons.Outlined.StayCurrentPortrait)
82+
ConstraintId.HINGE_OPEN -> ComposeIconInfo.Vector(Icons.Outlined.StayCurrentLandscape)
83+
8084
ConstraintId.LOCK_SCREEN_SHOWING -> ComposeIconInfo.Vector(Icons.Outlined.ScreenLockPortrait)
8185
ConstraintId.LOCK_SCREEN_NOT_SHOWING -> ComposeIconInfo.Vector(Icons.Outlined.LockOpen)
8286
ConstraintId.TIME -> ComposeIconInfo.Vector(Icons.Outlined.Timer)
@@ -114,6 +118,8 @@ object ConstraintUtils {
114118
ConstraintId.PHONE_RINGING -> R.string.constraint_phone_ringing
115119
ConstraintId.CHARGING -> R.string.constraint_charging
116120
ConstraintId.DISCHARGING -> R.string.constraint_discharging
121+
ConstraintId.HINGE_CLOSED -> R.string.constraint_hinge_closed
122+
ConstraintId.HINGE_OPEN -> R.string.constraint_hinge_open
117123
ConstraintId.LOCK_SCREEN_SHOWING -> R.string.constraint_lock_screen_showing
118124
ConstraintId.LOCK_SCREEN_NOT_SHOWING -> R.string.constraint_lock_screen_not_showing
119125
ConstraintId.TIME -> R.string.constraint_time

base/src/main/java/io/github/sds100/keymapper/base/constraints/CreateConstraintUseCase.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.sds100.keymapper.base.constraints
22

33
import android.content.pm.PackageManager
4+
import android.os.Build
45
import io.github.sds100.keymapper.common.utils.KMError
56
import io.github.sds100.keymapper.data.Keys
67
import io.github.sds100.keymapper.data.repositories.PreferenceRepository
@@ -30,6 +31,11 @@ class CreateConstraintUseCaseImpl @Inject constructor(
3031
return KMError.SystemFeatureNotSupported(PackageManager.FEATURE_CAMERA_FLASH)
3132
}
3233
}
34+
ConstraintId.HINGE_CLOSED, ConstraintId.HINGE_OPEN -> {
35+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
36+
return KMError.SdkVersionTooLow(Build.VERSION_CODES.R)
37+
}
38+
}
3339
else -> Unit
3440
}
3541

base/src/main/java/io/github/sds100/keymapper/base/constraints/DetectConstraintsUseCase.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import io.github.sds100.keymapper.system.camera.CameraAdapter
88
import io.github.sds100.keymapper.system.camera.CameraLens
99
import io.github.sds100.keymapper.system.devices.DevicesAdapter
1010
import io.github.sds100.keymapper.system.display.DisplayAdapter
11+
import io.github.sds100.keymapper.system.foldable.FoldableAdapter
1112
import io.github.sds100.keymapper.system.inputmethod.InputMethodAdapter
1213
import io.github.sds100.keymapper.system.lock.LockScreenAdapter
1314
import io.github.sds100.keymapper.system.media.MediaAdapter
@@ -30,6 +31,7 @@ class DetectConstraintsUseCaseImpl @AssistedInject constructor(
3031
private val lockScreenAdapter: LockScreenAdapter,
3132
private val phoneAdapter: PhoneAdapter,
3233
private val powerAdapter: PowerAdapter,
34+
private val foldableAdapter: FoldableAdapter,
3335
) : DetectConstraintsUseCase {
3436

3537
@AssistedFactory
@@ -50,6 +52,7 @@ class DetectConstraintsUseCaseImpl @AssistedInject constructor(
5052
lockScreenAdapter,
5153
phoneAdapter,
5254
powerAdapter,
55+
foldableAdapter,
5356
)
5457

5558
override fun onDependencyChanged(dependency: ConstraintDependency): Flow<ConstraintDependency> {
@@ -83,6 +86,7 @@ class DetectConstraintsUseCaseImpl @AssistedInject constructor(
8386

8487
ConstraintDependency.PHONE_STATE -> phoneAdapter.callStateFlow.map { dependency }
8588
ConstraintDependency.CHARGING_STATE -> powerAdapter.isCharging.map { dependency }
89+
ConstraintDependency.HINGE_STATE -> foldableAdapter.hingeState.map { dependency }
8690
}
8791
}
8892
}

0 commit comments

Comments
 (0)