Skip to content

Commit

Permalink
feat(reader): improve content reader
Browse files Browse the repository at this point in the history
  • Loading branch information
Irineu333 committed Nov 29, 2023
1 parent 31a6f04 commit 3d9c9eb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import android.speech.tts.TextToSpeech
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat
import com.neo.speaktouch.utils.Reader
import com.neo.speaktouch.model.Text
import com.neo.speaktouch.utils.extension.getLog
import timber.log.Timber

class SpeechController(
Expand All @@ -50,7 +49,7 @@ class SpeechController(
}

fun speak(nodeInfo: AccessibilityNodeInfoCompat) {
speak(reader.readContent(nodeInfo))
speak(reader.read(nodeInfo))
}

fun stop() {
Expand Down
28 changes: 12 additions & 16 deletions app/src/main/java/com/neo/speaktouch/utils/Reader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import android.content.Context
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat
import com.neo.speaktouch.model.Type
import com.neo.speaktouch.model.toTypeText
import com.neo.speaktouch.utils.extension.ifEmptyOrNull
import com.neo.speaktouch.utils.extension.getContent
import com.neo.speaktouch.utils.extension.isNotNullOrEmpty
import com.neo.speaktouch.utils.extension.iterator
import com.neo.speaktouch.utils.extension.toStateText
import javax.inject.Inject
Expand All @@ -31,23 +32,20 @@ class Reader @Inject constructor(
private val context: Context
) {

fun readContent(
fun read(
node: AccessibilityNodeInfoCompat,
options: Options = Options()
) = with(node) {

val content = contentDescription.ifEmptyOrNull {
text.ifEmptyOrNull {
hintText.ifEmptyOrNull {
readChildren(node)
}
}
}
val type = Type.get(node)

val content = getContent(type) ?: readChildren(node)

buildList {
add(content)

val type = Type.get(node)
if (content.isNotNullOrEmpty()) {
add(content)
}

if (options.mustReadType && type != null) {
type.toTypeText()?.let {
Expand All @@ -61,8 +59,7 @@ class Reader @Inject constructor(
}
}
}.joinToString(
separator = ", ",
postfix = "."
separator = ", "
)
}

Expand All @@ -78,7 +75,7 @@ class Reader @Inject constructor(
val isCheckable = Type.get(child) is Type.Checkable

add(
readContent(
read(
node = child,
options = Options(
mustReadState = isCheckable,
Expand All @@ -88,8 +85,7 @@ class Reader @Inject constructor(
)
}
}.joinToString(
separator = ", ",
postfix = "."
separator = ", "
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.Accessibilit
import com.neo.speaktouch.R
import com.neo.speaktouch.model.Type
import com.neo.speaktouch.model.Text
import com.neo.speaktouch.model.toTypeText
import com.neo.speaktouch.utils.NodeValidator

fun AccessibilityNodeInfoCompat.getNearestAncestor(
Expand Down Expand Up @@ -264,3 +263,29 @@ fun AccessibilityNodeInfoCompat.toCheckableStateText(
}
}
}

fun AccessibilityNodeInfoCompat.getContent(
type: Type? = null
): CharSequence? {

if (type is Type.EditField) {

// Deliberately different behavior from talkback
// https://github.com/NeoA11y/SpeakTouch/discussions/119
return when {
text.isNotNullOrEmpty() -> text
hintText.isNotNullOrEmpty() -> hintText
else -> null
}
}

if (contentDescription.isNotNullOrEmpty()) {
return contentDescription
}

if (text.isNotNullOrEmpty()) {
return text
}

return null
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ inline fun <T : CharSequence> T?.ifEmptyOrNull(
}
}



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

0 comments on commit 3d9c9eb

Please sign in to comment.