From 9399fc3f84a6765f0476ea3419ba3b41f1fd1dee Mon Sep 17 00:00:00 2001 From: David Allison <62114487+david-allison@users.noreply.github.com> Date: Fri, 15 Dec 2023 19:57:47 +0000 Subject: [PATCH] compat: MotionEvent.AXIS: handle API 24 -> 34 --- .../ichi2/anki/reviewer/MotionEventHandler.kt | 36 ++++++++++++------- .../src/main/java/com/ichi2/compat/Compat.kt | 21 +++++++++++ .../java/com/ichi2/compat/CompatHelper.kt | 1 + .../main/java/com/ichi2/compat/CompatV23.kt | 9 +++++ .../main/java/com/ichi2/compat/CompatV24.kt | 4 +++ .../main/java/com/ichi2/compat/CompatV34.kt | 29 +++++++++++++++ 6 files changed, 88 insertions(+), 12 deletions(-) create mode 100644 AnkiDroid/src/main/java/com/ichi2/compat/CompatV34.kt diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/reviewer/MotionEventHandler.kt b/AnkiDroid/src/main/java/com/ichi2/anki/reviewer/MotionEventHandler.kt index 6f3516a36501..f5a44fa94200 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/reviewer/MotionEventHandler.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/reviewer/MotionEventHandler.kt @@ -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 /** @@ -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) + ; /** diff --git a/AnkiDroid/src/main/java/com/ichi2/compat/Compat.kt b/AnkiDroid/src/main/java/com/ichi2/compat/Compat.kt index 05511efcfd41..76c6f7c2acf5 100644 --- a/AnkiDroid/src/main/java/com/ichi2/compat/Compat.kt +++ b/AnkiDroid/src/main/java/com/ichi2/compat/Compat.kt @@ -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 } diff --git a/AnkiDroid/src/main/java/com/ichi2/compat/CompatHelper.kt b/AnkiDroid/src/main/java/com/ichi2/compat/CompatHelper.kt index f29024d5d6fc..7ba2f8d40e5b 100644 --- a/AnkiDroid/src/main/java/com/ichi2/compat/CompatHelper.kt +++ b/AnkiDroid/src/main/java/com/ichi2/compat/CompatHelper.kt @@ -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() diff --git a/AnkiDroid/src/main/java/com/ichi2/compat/CompatV23.kt b/AnkiDroid/src/main/java/com/ichi2/compat/CompatV23.kt index 0314bc31f0a1..318e4211cbca 100644 --- a/AnkiDroid/src/main/java/com/ichi2/compat/CompatV23.kt +++ b/AnkiDroid/src/main/java/com/ichi2/compat/CompatV23.kt @@ -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 diff --git a/AnkiDroid/src/main/java/com/ichi2/compat/CompatV24.kt b/AnkiDroid/src/main/java/com/ichi2/compat/CompatV24.kt index 9278d24ce52c..8ddae07042e6 100644 --- a/AnkiDroid/src/main/java/com/ichi2/compat/CompatV24.kt +++ b/AnkiDroid/src/main/java/com/ichi2/compat/CompatV24.kt @@ -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 @@ -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 } diff --git a/AnkiDroid/src/main/java/com/ichi2/compat/CompatV34.kt b/AnkiDroid/src/main/java/com/ichi2/compat/CompatV34.kt new file mode 100644 index 000000000000..a8d4dd2aca04 --- /dev/null +++ b/AnkiDroid/src/main/java/com/ichi2/compat/CompatV34.kt @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 David Allison + * + * 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 . + */ + +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 +}