Skip to content

Instantly share code, notes, and snippets.

Avatar

MiguelHincapieC miguelhincapie

View GitHub Profile
@miguelhincapie
miguelhincapie / CarouselKeyEventDelegate.kt
Created Apr 8, 2020
Key event delegate to navigate through the carousel using TalkBack
View CarouselKeyEventDelegate.kt
class CarouselKeyEventDelegate : BaseKeyEventDelegate() {
init {
keyEventActionMap.let {
it.put(
createKey(
R.id.carousel_container,
KEYCODE_DPAD_DOWN
),
this::consumeDownKeyOnCarouselContainer
@miguelhincapie
miguelhincapie / GridKeyEventDelegateGist.kt
Created Apr 6, 2020
Snippet to navigate through recyclerView using accessibility
View GridKeyEventDelegateGist.kt
private fun onDownKeyPressed(currentFocus: View): Boolean = with(currentFocus) {
getNextElementPosition().let { nextElementPosition ->
if (isPositionInbound(nextElementPosition)) {
sendFocusToListItem(nextElementPosition)
} else {
rootView.findViewById<View>(R.id.carouselRV)?.sendAccessibilityFocus()
}
}
return true
}
@miguelhincapie
miguelhincapie / AccessibilityFocus.kt
Created Apr 6, 2020
Extension functions to enable/disable accessibility focus.
View AccessibilityFocus.kt
/**
* Extension function to set touch values for accessibility
*/
fun List<View>.enableAccessibilityFocus() {
this.forEach {
it.setAccessibilityFocus()
}
}
/**
@miguelhincapie
miguelhincapie / GeneralKeyEventDelegate.kt
Created Apr 6, 2020
Delegate implementation for general accessibility navigation using TalkBack.
View GeneralKeyEventDelegate.kt
class GeneralKeyEventDelegate : BaseKeyEventDelegate() {
init {
keyEventActionMap.let {
it.put(
createKey(
R.id.accessibility_state,
KEYCODE_DPAD_DOWN,
ACTION_DOWN
),
@miguelhincapie
miguelhincapie / KeyEventHandler.kt
Created Apr 6, 2020
Class designated to process TalkBack accessibility key events using delegate pattern.
View KeyEventHandler.kt
const val KEYCODE_CHANGE_ACCESSIBILITY_STATE = KeyEvent.KEYCODE_S
class KeyEventHandler @Inject constructor() {
/**
* Contains all possible accessibility actions available for the current Activity.
*/
private var keyEventActionMap = SparseArrayCompat<KeyEventAction>()
fun addKeyEventDelegate(keyEventDelegateImpl: BaseKeyEventDelegate) {
@miguelhincapie
miguelhincapie / DelegatingKeyEventFromActivityToViewModel.kt
Created Apr 6, 2020
Snippet showing how to send an accessibility event to viewModel.
View DelegatingKeyEventFromActivityToViewModel.kt
override fun dispatchKeyEvent(event: KeyEvent?): Boolean {
return event?.let {
currentFocus?.let { focusView ->
talkBackViewModel.dispatchKeyEvent(it, WeakReference(focusView))
}
} ?: super.dispatchKeyEvent(event)
}
@miguelhincapie
miguelhincapie / ButtonExtFun.kt
Created Apr 5, 2020
Extension functions to deal with enable/disable buttons with accessibility service ON.
View ButtonExtFun.kt
/**
* Extension function to disable a Button.
* Workaround due we can't just set enable to false because it will lose focusable ability.
* Check canTakeFocus() function inside View.java file.
*/
fun Button.disable(@DrawableRes drawableForAccessibility: Int) {
if (TalkBackState.value == true) {
isEnabled = true
isClickable = false
contentDescription = String.format(context.getString(R.string.button_disabled_accessibility), text)
@miguelhincapie
miguelhincapie / PostDelayedUtil.kt
Created Apr 4, 2020
Util extension function to send an event after X time.
View PostDelayedUtil.kt
const val DELAY_AFTER_TALK_BACK_GETS_STARTED = 400L
fun View.weakPostDelayed(delay: Long = DELAY_AFTER_TALK_BACK_GETS_STARTED, code: (View) -> Unit) {
postDelayed(WeakRunnable(this) {
code(it)
}, delay)
}
class WeakRunnable<T>(instance: T?, private val action: (T) -> Unit) : Runnable {
private val weakReference: WeakReference<T?> = WeakReference(instance)
@miguelhincapie
miguelhincapie / SendFocusAfterTalkBackStart.kt
Created Apr 4, 2020
Snippet showing how to send accessibility focus to an specific UI element after TalkBack started.
View SendFocusAfterTalkBackStart.kt
/**
* With this function we are observing a specific accessibility service started/stopped: TalkBack
*/
private fun onTalkBackStateChanged(isOn: Boolean) {
if (isOn) {
accessibility_state.text = getString(R.string.accessibility_state_on)
accessibility_state.weakPostDelayed { sendFocusToTitle() }
} else {
accessibility_state.text = getString(R.string.accessibility_state_off)
}
@miguelhincapie
miguelhincapie / AccessibilityServiceListener.kt
Created Apr 4, 2020
Generic way to listen for any change in accessibility services state
View AccessibilityServiceListener.kt
/**
* Observe for any accessibility service state change. Use [AccessibilityManager] to list
* current accessibility service ON or send [AccessibilityEvent] like a speech out.
*/
private fun listenForAccessibilityServiceStatus(context: Context) {
val accessibilityManager =
context.getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager
accessibilityManager.addAccessibilityStateChangeListener { enabled ->
// Do your stuff
}