Skip to content

Commit 87a9dae

Browse files
criticalAYdavid-allison
authored andcommitted
refactor: extract 'KeyUtils' to :common:android
Simple KeyEvent digit helpers with no project dependencies. Lives in :common:android since it uses android.view.KeyEvent.
1 parent cf4d62c commit 87a9dae

4 files changed

Lines changed: 29 additions & 103 deletions

File tree

AnkiDroid/src/androidTest/java/com/ichi2/anki/utils/KeyUtilsTest.kt

Lines changed: 0 additions & 65 deletions
This file was deleted.

AnkiDroid/src/main/java/com/ichi2/anki/NoteEditorFragment.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ import com.ichi2.anki.android.input.shortcut
9292
import com.ichi2.anki.common.annotations.NeedsTest
9393
import com.ichi2.anki.common.crashreporting.CrashReportService
9494
import com.ichi2.anki.common.utils.HashUtil
95+
import com.ichi2.anki.common.utils.android.digit
9596
import com.ichi2.anki.common.utils.android.showThemedToast
9697
import com.ichi2.anki.common.utils.annotation.KotlinCleanup
9798
import com.ichi2.anki.common.utils.ext.ifZero
@@ -170,7 +171,6 @@ import com.ichi2.utils.ClipboardUtil.hasMedia
170171
import com.ichi2.utils.ClipboardUtil.items
171172
import com.ichi2.utils.ImportUtils
172173
import com.ichi2.utils.IntentUtil.resolveMimeType
173-
import com.ichi2.utils.KeyUtils
174174
import com.ichi2.utils.NoteFieldDecorator
175175
import com.ichi2.utils.TextViewUtil
176176
import com.ichi2.utils.configureView
@@ -984,7 +984,6 @@ class NoteEditorFragment :
984984
textBox.setSelection(start + newStart, start + newEnd)
985985
}
986986

987-
@KotlinCleanup("convert KeyUtils to extension functions")
988987
override fun dispatchKeyEvent(event: KeyEvent): Boolean {
989988
// We want to behave as onKeyUp and thus only react to ACTION_UP
990989
if (event.action != KeyEvent.ACTION_UP) return false
@@ -1055,7 +1054,7 @@ class NoteEditorFragment :
10551054

10561055
// 7573: Ctrl+Shift+[Num] to select a field
10571056
if (event.isCtrlPressed && event.isShiftPressed) {
1058-
val digit = KeyUtils.getDigit(event) ?: return false
1057+
val digit = event.digit ?: return false
10591058
// '0' is after '9' on the keyboard, so a user expects '10'
10601059
val humanReadableDigit = if (digit == 0) 10 else digit
10611060
// Subtract 1 to map to field index. '1' is the first field (index 0)

AnkiDroid/src/main/java/com/ichi2/utils/KeyUtils.kt renamed to common/android/src/main/java/com/ichi2/anki/common/utils/android/KeyUtils.kt

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,16 @@
1313
* You should have received a copy of the GNU General Public License along with
1414
* this program. If not, see <http://www.gnu.org/licenses/>.
1515
*/
16-
package com.ichi2.utils
16+
package com.ichi2.anki.common.utils.android
1717

1818
import android.view.KeyEvent
19-
import androidx.annotation.VisibleForTesting
2019

21-
object KeyUtils {
22-
@VisibleForTesting
23-
fun isDigit(event: KeyEvent): Boolean {
24-
val unicodeChar = event.getUnicodeChar(0)
25-
return unicodeChar >= '0'.code && unicodeChar <= '9'.code
26-
}
27-
28-
fun getDigit(event: KeyEvent): Int? {
29-
val unicodeChar = event.getUnicodeChar(0)
30-
return if (isDigit(event)) unicodeChar - '0'.code else null
20+
/**
21+
* The decimal digit (0..9) represented by this [KeyEvent], or `null` if the event
22+
* does not represent a digit.
23+
*/
24+
val KeyEvent.digit: Int?
25+
get() {
26+
val unicodeChar = getUnicodeChar(0)
27+
return if (unicodeChar in '0'.code..'9'.code) unicodeChar - '0'.code else null
3128
}
32-
}

AnkiDroid/src/test/java/com/ichi2/utils/KeyUtilsTest.kt renamed to common/android/src/test/java/com/ichi2/anki/common/utils/android/KeyUtilsTest.kt

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,40 @@
1414
* this program. If not, see <http://www.gnu.org/licenses/>.
1515
*/
1616

17-
package com.ichi2.utils
17+
package com.ichi2.anki.common.utils.android
1818

1919
import android.view.KeyEvent
2020
import android.view.KeyEvent.ACTION_UP
2121
import android.view.KeyEvent.KEYCODE_0
22+
import android.view.KeyEvent.KEYCODE_5
2223
import android.view.KeyEvent.KEYCODE_9
24+
import android.view.KeyEvent.KEYCODE_BACK
2325
import android.view.KeyEvent.KEYCODE_ENDCALL
2426
import android.view.KeyEvent.KEYCODE_STAR
2527
import androidx.test.ext.junit.runners.AndroidJUnit4
26-
import com.ichi2.anki.EmptyApplicationCategory
27-
import com.ichi2.testutils.EmptyApplication
28-
import com.ichi2.utils.KeyUtils.getDigit
29-
import com.ichi2.utils.KeyUtils.isDigit
30-
import org.junit.Assert.assertEquals
31-
import org.junit.Assert.assertFalse
32-
import org.junit.Assert.assertTrue
3328
import org.junit.Test
34-
import org.junit.experimental.categories.Category
3529
import org.junit.runner.RunWith
36-
import org.robolectric.annotation.Config
30+
import kotlin.test.assertEquals
31+
import kotlin.test.assertNull
3732

33+
/** Tests for [KeyEvent.digit]. */
3834
@RunWith(AndroidJUnit4::class)
39-
@Config(application = EmptyApplication::class)
40-
@Category(EmptyApplicationCategory::class)
4135
class KeyUtilsTest {
4236
@Test
43-
fun testIsDigit() {
44-
assertTrue(isDigit(KeyEvent(ACTION_UP, KEYCODE_0)))
45-
assertTrue(isDigit(KeyEvent(ACTION_UP, KEYCODE_9)))
46-
assertFalse(isDigit(KeyEvent(ACTION_UP, KEYCODE_STAR)))
47-
assertFalse(isDigit(KeyEvent(ACTION_UP, KEYCODE_ENDCALL)))
37+
fun `digit returns the digit for 0-9 keys`() {
38+
assertEquals(0, KeyEvent(ACTION_UP, KEYCODE_0).digit)
39+
assertEquals(5, KeyEvent(ACTION_UP, KEYCODE_5).digit)
40+
assertEquals(9, KeyEvent(ACTION_UP, KEYCODE_9).digit)
4841
}
4942

5043
@Test
51-
fun testGetDigit() {
52-
assertEquals(0, getDigit(KeyEvent(ACTION_UP, KEYCODE_0)))
53-
assertEquals(9, getDigit(KeyEvent(ACTION_UP, KEYCODE_9)))
54-
assertEquals(null, getDigit(KeyEvent(ACTION_UP, KEYCODE_STAR)))
55-
assertEquals(null, getDigit(KeyEvent(ACTION_UP, KEYCODE_ENDCALL)))
44+
fun `digit returns null for non-digit printable keys`() {
45+
assertNull(KeyEvent(ACTION_UP, KEYCODE_STAR).digit)
46+
}
47+
48+
@Test
49+
fun `digit returns null for non-printable keys`() {
50+
assertNull(KeyEvent(ACTION_UP, KEYCODE_ENDCALL).digit)
51+
assertNull(KeyEvent(ACTION_UP, KEYCODE_BACK).digit)
5652
}
5753
}

0 commit comments

Comments
 (0)