Skip to content
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

App is focusing on non-readable ViewGroup #54

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class FocusInterceptor : Interceptor {

if (nodeInfo.isAccessibilityFocused) return

if (!NodeValidator.isValidForAccessible(nodeInfo)) return
if (!NodeValidator.isValidForAccessibility(nodeInfo)) return

when (event.eventType) {
AccessibilityEvent.TYPE_VIEW_HOVER_ENTER -> {
Expand All @@ -59,16 +59,16 @@ class FocusInterceptor : Interceptor {

private fun getFocusableNode(nodeInfo: NodeInfo): NodeInfo? {

if (NodeValidator.isRequiredFocus(nodeInfo)) {
if (NodeValidator.mustFocus(nodeInfo)) {
return nodeInfo
}

val ancestor = nodeInfo.getNearestAncestor(
NodeValidator::isRequiredFocus
NodeValidator::mustFocus
)

return ancestor ?: when {
NodeValidator.isReadable(nodeInfo) -> nodeInfo
NodeValidator.hasContentToRead(nodeInfo) -> nodeInfo
else -> null
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/neo/speaktouch/model/Reader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Reader(
is Level.Children -> buildList {
for (child in node) {

if (!NodeValidator.isValidForAccessible(child)) continue
if (!NodeValidator.isValidForAccessibility(child)) continue
if (!NodeValidator.isReadableAsChild(child)) continue

add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ fun NodeInfo.getLog(vararg extra: String) = buildList {
add("isScreenReaderFocusable: $isScreenReaderFocusable")

add("\nVALIDATOR")
add("isValidForAccessible: ${NodeValidator.isValidForAccessible(this@getLog)}")
add("isReadable: ${NodeValidator.isReadable(this@getLog)}")
add("isRequestFocus: ${NodeValidator.isRequiredFocus(this@getLog)}")
add("isValidForAccessible: ${NodeValidator.isValidForAccessibility(this@getLog)}")
add("isReadable: ${NodeValidator.hasContentToRead(this@getLog)}")
add("isRequestFocus: ${NodeValidator.mustFocus(this@getLog)}")
add("isReadableAsChild: ${NodeValidator.isReadableAsChild(this@getLog)}")

add("\nHIERARCHY")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ fun <T : List<CharSequence?>> T.filterNotNullOrEmpty() = filterNot { it.isNullOr

infix fun CharSequence.`is`(childClass: Class<*>): Boolean {
if (equals(childClass.name)) return true
if (isEmpty()) return false

return runCatching {
childClass.isAssignableFrom(Class.forName(toString()))
Expand Down
70 changes: 55 additions & 15 deletions app/src/main/java/com/neo/speaktouch/utils/object/NodeValidator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,86 @@ import com.neo.speaktouch.utils.`typealias`.NodeInfo

object NodeValidator {

/**
* @return true if [node] has any action
*/
private fun isClickable(node: NodeInfo): Boolean {

return node.isClickable || node.isLongClickable
}

private fun hasText(node: NodeInfo): Boolean {
/**
* @return true if [node] has some text to read
*/
private fun hasTextToRead(node: NodeInfo): Boolean {

return listOf(
node.text,
node.hintText,
node.contentDescription
).any { it.isNotNullOrEmpty() }
}

private fun isRequiredRead(node: NodeInfo): Boolean {
return node.isFocusable && isReadable(node)
/**
* @return true if [node] should be read directly
*/
private fun mustReadContent(node: NodeInfo): Boolean {

return hasContentToRead(node) && node.isFocusable
}

private fun hasReadableChild(nodeInfo: NodeInfo): Boolean {
for (child in nodeInfo) {
/**
* @return true if [node] has a readable child
*/
private fun hasReadableChild(node: NodeInfo): Boolean {

for (child in node) {
if (!isValidForAccessibility(child)) continue
if (isReadableAsChild(child)) return true
if (hasReadableChild(child)) return true
}

return false
}

fun isValidForAccessible(node: NodeInfo): Boolean {
/**
* @return true if [node] must be ignored
*/
fun isValidForAccessibility(node: NodeInfo): Boolean {

return node.isImportantForAccessibility && node.isVisibleToUser
}

fun isRequiredFocus(node: NodeInfo): Boolean {
return isRequiredRead(node) ||
isClickable(node) &&
hasReadableChild(node)
/**
* @return true if is mandatory to focus on [node]
*/
fun mustFocus(node: NodeInfo): Boolean {

return mustReadContent(node) || mustReadChildren(node)
}

/**
* @return true if is mandatory read [node]'s children
*/
fun mustReadChildren(node: NodeInfo) : Boolean {

if (mustReadContent(node)) return false

return isClickable(node) && hasReadableChild(node)
}

fun isReadable(node: NodeInfo): Boolean {
return hasText(node) || node.isCheckable
/**
* @return true if [node] has content (text or state)
*/
fun hasContentToRead(node: NodeInfo): Boolean {

return hasTextToRead(node) || node.isCheckable
}

fun isReadableAsChild(nodeInfo: NodeInfo): Boolean {
return isReadable(nodeInfo) && !isRequiredFocus(nodeInfo)
/**
* @return true if [node] cannot be read directly
*/
fun isReadableAsChild(node: NodeInfo): Boolean {

return hasContentToRead(node) && !mustFocus(node)
}
}