Skip to content

Commit

Permalink
compat: MotionEvent.AXIS: handle API 24 -> 34
Browse files Browse the repository at this point in the history
  • Loading branch information
david-allison committed Dec 16, 2023
1 parent 7d87b84 commit 9399fc3
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.ichi2.anki.AbstractFlashcardViewer
import com.ichi2.anki.AnkiDroidApp
import com.ichi2.anki.cardviewer.ViewerCommand
import com.ichi2.anki.preferences.sharedPrefs
import com.ichi2.compat.CompatHelper
import timber.log.Timber

/**
Expand Down Expand Up @@ -217,18 +218,29 @@ enum class Axis(val motionEventValue: Int) {
HAT_X(MotionEvent.AXIS_HAT_X),

/** @see MotionEvent.AXIS_HAT_Y */
HAT_Y(MotionEvent.AXIS_HAT_Y)

// API Level 24:
// AXIS_RELATIVE_X,
// AXIS_RELATIVE_Y,

// API Level 34:
// AXIS_GESTURE_X_OFFSET,
// AXIS_GESTURE_Y_OFFSET,
// AXIS_GESTURE_PINCH_SCALE_FACTOR,
// AXIS_GESTURE_SCROLL_X_DISTANCE,
// AXIS_GESTURE_SCROLL_Y_DISTANCE,
HAT_Y(MotionEvent.AXIS_HAT_Y),

/** @see MotionEvent.AXIS_RELATIVE_X */
AXIS_RELATIVE_X(CompatHelper.compat.AXIS_RELATIVE_X),

/** @see MotionEvent.AXIS_RELATIVE_Y */
AXIS_RELATIVE_Y(CompatHelper.compat.AXIS_RELATIVE_Y),

/** @see MotionEvent.AXIS_GESTURE_X_OFFSET */
AXIS_GESTURE_X_OFFSET(CompatHelper.compat.AXIS_GESTURE_X_OFFSET),

/** @see MotionEvent.AXIS_GESTURE_Y_OFFSET */
AXIS_GESTURE_Y_OFFSET(CompatHelper.compat.AXIS_GESTURE_Y_OFFSET),

/** @see MotionEvent.AXIS_GESTURE_PINCH_SCALE_FACTOR */
AXIS_GESTURE_PINCH_SCALE_FACTOR(CompatHelper.compat.AXIS_GESTURE_PINCH_SCALE_FACTOR),

/** @see MotionEvent.AXIS_GESTURE_SCROLL_X_DISTANCE */
AXIS_GESTURE_SCROLL_X_DISTANCE(CompatHelper.compat.AXIS_GESTURE_SCROLL_X_DISTANCE),

/** @see MotionEvent.AXIS_GESTURE_SCROLL_Y_DISTANCE */
AXIS_GESTURE_SCROLL_Y_DISTANCE(CompatHelper.compat.AXIS_GESTURE_SCROLL_Y_DISTANCE)

;

/**
Expand Down
21 changes: 21 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/compat/Compat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,25 @@ interface Compat {
*/
@CheckResult
fun normalize(locale: Locale): Locale

@Suppress("PropertyName")
val AXIS_RELATIVE_X: Int

@Suppress("PropertyName")
val AXIS_RELATIVE_Y: Int

@Suppress("PropertyName")
val AXIS_GESTURE_X_OFFSET: Int

@Suppress("PropertyName")
val AXIS_GESTURE_Y_OFFSET: Int

@Suppress("PropertyName")
val AXIS_GESTURE_PINCH_SCALE_FACTOR: Int

@Suppress("PropertyName")
val AXIS_GESTURE_SCROLL_X_DISTANCE: Int

@Suppress("PropertyName")
val AXIS_GESTURE_SCROLL_Y_DISTANCE: Int
}
1 change: 1 addition & 0 deletions AnkiDroid/src/main/java/com/ichi2/compat/CompatHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class CompatHelper private constructor() {

// Note: Needs ": Compat" or the type system assumes `Compat21`
private val compatValue: Compat = when {
sdkVersion >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE -> CompatV34()
sdkVersion >= Build.VERSION_CODES.TIRAMISU -> CompatV33()
sdkVersion >= Build.VERSION_CODES.S -> CompatV31()
sdkVersion >= Build.VERSION_CODES.Q -> CompatV29()
Expand Down
9 changes: 9 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/compat/CompatV23.kt
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,15 @@ open class CompatV23 : Compat {
// convert back from this key to a two-letter mapping
return twoLetterSystemLocaleMapping[iso3Code] ?: locale
}

override val AXIS_RELATIVE_X: Int = 27
override val AXIS_RELATIVE_Y: Int = 28
override val AXIS_GESTURE_X_OFFSET: Int = 48
override val AXIS_GESTURE_Y_OFFSET: Int = 49
override val AXIS_GESTURE_SCROLL_X_DISTANCE: Int = 50
override val AXIS_GESTURE_SCROLL_Y_DISTANCE: Int = 51
override val AXIS_GESTURE_PINCH_SCALE_FACTOR: Int = 52

companion object {
/**
* Maps from the ISO 3 code of a locale to the locale in
Expand Down
4 changes: 4 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/compat/CompatV24.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.ichi2.compat

import android.annotation.TargetApi
import android.icu.util.ULocale
import android.view.MotionEvent
import com.ichi2.utils.isRobolectric
import timber.log.Timber
import java.util.Locale
Expand All @@ -38,4 +39,7 @@ open class CompatV24 : CompatV23(), Compat {
locale
}
}

override val AXIS_RELATIVE_X: Int = MotionEvent.AXIS_RELATIVE_X
override val AXIS_RELATIVE_Y: Int = MotionEvent.AXIS_RELATIVE_Y
}
29 changes: 29 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/compat/CompatV34.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2023 David Allison <davidallisongithub@gmail.com>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.ichi2.compat

import android.annotation.TargetApi
import android.view.MotionEvent

@TargetApi(34)
open class CompatV34 : CompatV33(), Compat {
override val AXIS_GESTURE_X_OFFSET = MotionEvent.AXIS_GESTURE_X_OFFSET
override val AXIS_GESTURE_Y_OFFSET = MotionEvent.AXIS_GESTURE_Y_OFFSET
override val AXIS_GESTURE_SCROLL_X_DISTANCE = MotionEvent.AXIS_GESTURE_SCROLL_X_DISTANCE
override val AXIS_GESTURE_SCROLL_Y_DISTANCE = MotionEvent.AXIS_GESTURE_SCROLL_Y_DISTANCE
override val AXIS_GESTURE_PINCH_SCALE_FACTOR = MotionEvent.AXIS_GESTURE_PINCH_SCALE_FACTOR
}

0 comments on commit 9399fc3

Please sign in to comment.