Repository navigation
feat(keypad): support the Sundial keypad's physical layout and holds #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
manuel-rtsch
wants to merge
4
commits into
HarlenPostill:main
Choose a base branch
from
manuel-rtsch:feat/sundial-keypad
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0792cfd
Support the Sundial keypad's detection, holds and real layout
manuel-rtsch 3ad32a4
fix(input): follow the system long-press timeout and trust only modul…
manuel-rtsch f1e0a91
test(input): move HoldToActTest beside the code it tests
manuel-rtsch 298fa22
fix(input): satisfy lint for the Sundial Back hold
manuel-rtsch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
app/src/test/java/hrln/interactive/sidekit/design/input/HoldToActTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package hrln.interactive.sidekit.design.input | ||
|
|
||
| import android.view.KeyEvent | ||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
|
|
||
| /** A tap is delivered on release; a hold fires while the key is still down and never also taps. */ | ||
| @RunWith(AndroidJUnit4::class) | ||
| class HoldToActTest { | ||
| private class FakeScheduler : HoldToAct.Scheduler { | ||
| var scheduledDelay = -1L | ||
| private var action: (() -> Unit)? = null | ||
| override fun schedule(delayMs: Long, action: () -> Unit): HoldToAct.Cancellable { | ||
| scheduledDelay = delayMs | ||
| this.action = action | ||
| return HoldToAct.Cancellable { this.action = null } | ||
| } | ||
| fun fire() { | ||
| action?.invoke() | ||
| } | ||
| } | ||
|
|
||
| private val holds = mutableListOf<Pair<Int, KeyEvent>>() | ||
| private val scheduler = FakeScheduler() | ||
|
|
||
| private fun hold() = HoldToAct( | ||
| holdMs = 600, | ||
| onHold = { action, source -> holds += action to source }, | ||
| scheduler = scheduler | ||
| ) | ||
|
|
||
| private fun event(action: Int, code: Int, time: Long, repeat: Int = 0) = KeyEvent(0L, time, action, code, repeat, 0, 1, 0) | ||
|
|
||
| private fun action(code: Int): Int? = when (code) { | ||
| KeyEvent.KEYCODE_TAB -> KeyEvent.KEYCODE_BACK | ||
| KeyEvent.KEYCODE_ENTER -> KeyEvent.KEYCODE_ENDCALL | ||
| else -> null | ||
| } | ||
|
|
||
| @Test fun shortTapIsDeliveredOnRelease() { | ||
| val hold = hold() | ||
| assertEquals(HoldToAct.Result.Consume, hold.handle(event(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_TAB, 0), ::action)) | ||
| assertEquals(600L, scheduler.scheduledDelay) | ||
| val result = hold.handle(event(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_TAB, 100), ::action) | ||
| assertTrue(result is HoldToAct.Result.Deliver) | ||
| result as HoldToAct.Result.Deliver | ||
| assertEquals(listOf(KeyEvent.KEYCODE_TAB, KeyEvent.KEYCODE_TAB), result.events.map { it.keyCode }) | ||
| assertEquals(listOf(KeyEvent.ACTION_DOWN, KeyEvent.ACTION_UP), result.events.map { it.action }) | ||
| } | ||
|
|
||
| @Test fun holdFiresWhileHeldAndSwallowsTheRelease() { | ||
| val hold = hold() | ||
| assertEquals(HoldToAct.Result.Consume, hold.handle(event(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_TAB, 0), ::action)) | ||
| scheduler.fire() | ||
| assertEquals(listOf(KeyEvent.KEYCODE_BACK), holds.map { it.first }) | ||
| assertEquals(HoldToAct.Result.Consume, hold.handle(event(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_TAB, 500), ::action)) | ||
| } | ||
|
|
||
| @Test fun holdFiresOnReleaseWhenTheTimerDidNotRun() { | ||
| val hold = hold() | ||
| assertEquals(HoldToAct.Result.Consume, hold.handle(event(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER, 0), ::action)) | ||
| assertEquals( | ||
| HoldToAct.Result.Consume, | ||
| hold.handle(event(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_ENTER, 700), ::action) | ||
| ) | ||
| assertEquals(listOf(KeyEvent.KEYCODE_ENDCALL), holds.map { it.first }) | ||
| } | ||
|
|
||
| @Test fun otherKeysPassAndDropAPendingTap() { | ||
| val hold = hold() | ||
| assertEquals(HoldToAct.Result.Pass, hold.handle(event(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_UP, 0), ::action)) | ||
| assertEquals(HoldToAct.Result.Consume, hold.handle(event(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_TAB, 100), ::action)) | ||
| assertEquals(HoldToAct.Result.Pass, hold.handle(event(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_UP, 150), ::action)) | ||
| assertEquals(HoldToAct.Result.Consume, hold.handle(event(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_TAB, 200), ::action)) | ||
| assertEquals(emptyList<Int>(), holds.map { it.first }) | ||
| } | ||
|
|
||
| @Test fun resetCancelsAPendingHold() { | ||
| val hold = hold() | ||
| assertEquals(HoldToAct.Result.Consume, hold.handle(event(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_TAB, 0), ::action)) | ||
| hold.reset() | ||
| scheduler.fire() | ||
| assertEquals(emptyList<Int>(), holds.map { it.first }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.