Skip to content

Commit

Permalink
ANDROID-13687 Update. Improve announcement behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
haynlo committed Jun 12, 2024
1 parent 47639c6 commit d66962f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package com.telefonica.mistica.feedback

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.ColorStateList
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.View
import android.view.accessibility.AccessibilityManager
import androidx.annotation.AttrRes
import androidx.annotation.StringRes
import com.google.android.material.snackbar.BaseTransientBottomBar.BaseCallback
import com.google.android.material.snackbar.Snackbar
import com.telefonica.mistica.R
import com.telefonica.mistica.feedback.SnackBarBehaviorConfig.areSticky
import com.telefonica.mistica.feedback.snackbar.CustomSnackbarLayout
import com.telefonica.mistica.util.AccessibilityWrapper
import com.telefonica.mistica.util.getThemeColor

open class SnackbarBuilder(view: View?, text: String) {
Expand All @@ -26,7 +27,7 @@ open class SnackbarBuilder(view: View?, text: String) {
private var actionListener: View.OnClickListener? = null
private var callback: Snackbar.Callback? = null
private var withDismiss = false
private val accessibilityWrapper: AccessibilityWrapper
private val accessibilityManager: AccessibilityManager

private val hasAction: Boolean
get() = actionText != null
Expand All @@ -37,7 +38,7 @@ open class SnackbarBuilder(view: View?, text: String) {
requireNotNull(view) { "View cannot be null" }
this.text = text
this.view = view
this.accessibilityWrapper = AccessibilityWrapper(view.context)
this.accessibilityManager = view.context.getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager
}

open fun withAction(text: String, contentDescription: String? = null, listener: View.OnClickListener?): SnackbarBuilder = apply {
Expand Down Expand Up @@ -73,6 +74,7 @@ open class SnackbarBuilder(view: View?, text: String) {
val snackbar = createSnackbar(spannable, snackbarLength, isCritical = true)
setBackgroundColor(snackbar, R.attr.colorFeedbackErrorBackground)
setActionTextColor(snackbar, R.attr.colorTextPrimaryInverse)
interruptPreviousAccessibilityAnnouncement(snackbar)
snackbar.show()
return snackbar
}
Expand All @@ -86,6 +88,14 @@ open class SnackbarBuilder(view: View?, text: String) {
ColorStateList.valueOf(view.context.getThemeColor(colorRes))
}

private fun interruptPreviousAccessibilityAnnouncement(snackbar: Snackbar) {
snackbar.addCallback(object : BaseCallback<Snackbar>() {
override fun onShown(snackbar: Snackbar) {
accessibilityManager.interrupt()
}
})
}

private fun getSpannable(@AttrRes colorRes: Int): Spannable {
val spannable: Spannable = SpannableString(text)
val foregroundColorSpan = ForegroundColorSpan(view.context.getThemeColor(colorRes))
Expand All @@ -112,11 +122,6 @@ open class SnackbarBuilder(view: View?, text: String) {
snackbar.setCustomAction()
snackbar.showDismissActionIfNeeded(hasInfiniteDuration = snackbarLength == SnackbarLength.INDEFINITE)
snackbar.addCallbackIfNeeded()
snackbar.addCallback(object : BaseCallback<Snackbar>() {
override fun onShown(snackbar: Snackbar) {
accessibilityWrapper.sendAccessibilityAnnouncement(announcement = text.toString(), interrupt = isCritical)
}
})

return snackbar
}
Expand Down

This file was deleted.

0 comments on commit d66962f

Please sign in to comment.