Skip to content

Commit

Permalink
Add check for gesture stroke count and gesture duration and trigger p…
Browse files Browse the repository at this point in the history
…roper error
  • Loading branch information
pixel-shock committed Sep 13, 2023
1 parent df711a0 commit 0cc0cb8
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class PinchPickDisplayCoordinateViewModel(
fingerCount ?: return@combine false
duration ?: return@combine false

fingerCount in 2..9 && duration > 0
fingerCount >= 2 && duration > 0
}.stateIn(viewModelScope, SharingStarted.Lazily, false)

val isDoneButtonEnabled: StateFlow<Boolean> = combine(isCoordinatesValid, isOptionsValid) { isCoordinatesValid, isOptionsValid ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class SwipePickDisplayCoordinateViewModel(
fingerCount ?: return@combine false
duration ?: return@combine false

fingerCount in 1..9 && duration > 0
fingerCount > 0 && duration > 0
}.stateIn(viewModelScope, SharingStarted.Lazily, false)

val isDoneButtonEnabled: StateFlow<Boolean> = combine(isCoordinatesValid, isOptionsValid) { isCoordinatesValid, isOptionsValid ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,13 @@ class MyAccessibilityService : AccessibilityService(), LifecycleOwner, IAccessib
override fun swipeScreen(xStart: Int, yStart: Int, xEnd: Int, yEnd: Int, fingerCount: Int, duration: Int, inputEventType: InputEventType): Result<*> {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (fingerCount >= GestureDescription.getMaxStrokeCount()) {
return Error.GestureStrokeCountTooHigh
}
if (duration >= GestureDescription.getMaxGestureDuration()) {
return Error.GestureDurationTooHigh
}

val pStart = Point(xStart, yStart)
val pEnd = Point(xEnd, yEnd)

Expand Down Expand Up @@ -471,6 +478,12 @@ class MyAccessibilityService : AccessibilityService(), LifecycleOwner, IAccessib
inputEventType: InputEventType
): Result<*> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (fingerCount >= GestureDescription.getMaxStrokeCount()) {
return Error.GestureStrokeCountTooHigh
}
if (duration >= GestureDescription.getMaxGestureDuration()) {
return Error.GestureDurationTooHigh
}

val gestureBuilder = GestureDescription.Builder()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ fun Error.getFullMessage(resourceProvider: ResourceProvider) = when (this) {
Error.ShizukuNotStarted -> resourceProvider.getString(R.string.error_shizuku_not_started)
Error.NoFileName -> resourceProvider.getString(R.string.error_no_file_name)
Error.CantDetectKeyEventsInPhoneCall -> resourceProvider.getString(R.string.trigger_error_cant_detect_in_phone_call_explanation)
Error.GestureStrokeCountTooHigh -> resourceProvider.getString(R.string.trigger_error_gesture_stroke_count_too_high)
Error.GestureDurationTooHigh -> resourceProvider.getString(R.string.trigger_error_gesture_duration_too_high)
}

val Error.isFixable: Boolean
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/io/github/sds100/keymapper/util/Result.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ sealed class Error : Result<Nothing>() {

object CantShowImePickerInBackground : Error()
object CantFindImeSettings : Error()
object GestureStrokeCountTooHigh: Error()
object GestureDurationTooHigh: Error()

data class PermissionDenied(val permission: Permission) : Error() {
companion object {
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@
<string name="trigger_error_screen_off_root_permission_denied_short">The option to trigger when the screen is off will not work!</string>
<string name="trigger_error_cant_detect_in_phone_call">This trigger won\'t work while ringing or in a phone call!</string>
<string name="trigger_error_cant_detect_in_phone_call_explanation">Android doesn\'t let accessibility services detect volume button presses while your phone is ringing or it is in a phone call but it does let input method services detect them. Therefore, you must use one of the Key Mapper keyboards if you want this trigger to work.</string>
<string name="trigger_error_gesture_stroke_count_too_high">Too many fingers to perform gesture due to android limitations.</string>
<string name="trigger_error_gesture_duration_too_high">Gesture duration is too high due to android limitations.</string>

<string name="home_error_is_battery_optimised">Your mappings will stop working randomly!</string>
<string name="home_error_key_maps_paused">Your mappings are paused!</string>
Expand Down Expand Up @@ -259,12 +261,12 @@
<string name="extra_label_delay_before_next_action">Delay before next action (ms)</string>
<string name="extra_label_hold_down_duration">Hold down duration (ms)</string>
<string name="extra_label_swipe_duration">Swipe duration (ms)</string>
<string name="extra_label_swipe_finger_count">Finger Count (max. 9)</string>
<string name="extra_label_swipe_finger_count">Finger Count</string>
<string name="extra_label_swipe_select_start_end_label">Coordinates to set with screenshot</string>
<string name="extra_label_swipe_select_start_end_start">Start</string>
<string name="extra_label_swipe_select_start_end_end">End</string>
<string name="extra_label_pinch_duration">Pinch duration (ms)</string>
<string name="extra_label_pinch_finger_count">Finger Count (max. 9)</string>
<string name="extra_label_pinch_finger_count">Finger Count</string>
<!-- Extra labels -->

<!-- Constraints -->
Expand Down

0 comments on commit 0cc0cb8

Please sign in to comment.