From 1ed7efce4189ddbb424104d5d3ddf12dc898f9a1 Mon Sep 17 00:00:00 2001 From: mcarare Date: Thu, 12 Nov 2020 16:39:57 +0200 Subject: [PATCH] [fenix] For https://github.com/mozilla-mobile/fenix/issues/16477: Switch to using an extension to update a11y collection info. --- .../main/java/org/mozilla/fenix/ext/View.kt | 32 +++++++++++++++++-- .../org/mozilla/fenix/tabtray/TabTrayView.kt | 24 +++----------- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/ext/View.kt b/app/src/main/java/org/mozilla/fenix/ext/View.kt index 44f24e5501..03a0a6258d 100644 --- a/app/src/main/java/org/mozilla/fenix/ext/View.kt +++ b/app/src/main/java/org/mozilla/fenix/ext/View.kt @@ -2,6 +2,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +@file:Suppress("TooManyFunctions") + package org.mozilla.fenix.ext import android.graphics.Rect @@ -95,18 +97,42 @@ fun View.updateAccessibilityCollectionItemInfo( } } +/** + * Updates the a11y collection info for a list. + */ +fun View.updateAccessibilityCollectionInfo( + rowCount: Int, + columnCount: Int +) { + this.accessibilityDelegate = object : View.AccessibilityDelegate() { + override fun onInitializeAccessibilityNodeInfo( + host: View?, + info: AccessibilityNodeInfo? + ) { + super.onInitializeAccessibilityNodeInfo(host, info) + info?.collectionInfo = AccessibilityNodeInfo.CollectionInfo.obtain( + rowCount, + columnCount, + false + ) + } + } +} + /** * Fills a [Rect] with data about a view's location in the screen. * * @see View.getLocationOnScreen * @see View.getRectWithViewLocation for a version of this that is relative to a window -*/ + */ fun View.getRectWithScreenLocation(): Rect { val locationOnScreen = IntArray(2).apply { getLocationOnScreen(this) } - return Rect(locationOnScreen[0], + return Rect( + locationOnScreen[0], locationOnScreen[1], locationOnScreen[0] + width, - locationOnScreen[1] + height) + locationOnScreen[1] + height + ) } /** diff --git a/app/src/main/java/org/mozilla/fenix/tabtray/TabTrayView.kt b/app/src/main/java/org/mozilla/fenix/tabtray/TabTrayView.kt index faa0e8f9c4..0dff1c3a4d 100644 --- a/app/src/main/java/org/mozilla/fenix/tabtray/TabTrayView.kt +++ b/app/src/main/java/org/mozilla/fenix/tabtray/TabTrayView.kt @@ -9,8 +9,6 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.view.accessibility.AccessibilityEvent -import android.view.accessibility.AccessibilityNodeInfo -import android.view.accessibility.AccessibilityNodeInfo.CollectionInfo import androidx.annotation.IdRes import androidx.cardview.widget.CardView import androidx.constraintlayout.widget.ConstraintLayout @@ -52,6 +50,7 @@ import org.mozilla.fenix.components.toolbar.TabCounter.Companion.MAX_VISIBLE_TAB import org.mozilla.fenix.components.toolbar.TabCounter.Companion.SO_MANY_TABS_OPEN import org.mozilla.fenix.ext.components import org.mozilla.fenix.ext.settings +import org.mozilla.fenix.ext.updateAccessibilityCollectionInfo import org.mozilla.fenix.tabtray.SaveToCollectionsButtonAdapter.MultiselectModeChange import org.mozilla.fenix.tabtray.TabTrayDialogFragmentState.Mode import java.text.NumberFormat @@ -673,24 +672,11 @@ class TabTrayView( String.format(view.context.getString(R.string.open_tab_tray_plural), count.toString()) } - view.tabsTray.accessibilityDelegate = object : View.AccessibilityDelegate() { - override fun onInitializeAccessibilityNodeInfo( - host: View?, - info: AccessibilityNodeInfo? - ) { - super.onInitializeAccessibilityNodeInfo(host, info) - val isListTabView = view.context.settings().listTabView + val isListTabView = view.context.settings().listTabView + val columnCount = if (isListTabView) 1 else getNumberOfGridColumns(view.context) + val rowCount = count.toDouble().div(columnCount).roundToInt() - val columnCount = if (isListTabView) 1 else getNumberOfGridColumns(view.context) - val rowCount = count.toDouble().div(columnCount).roundToInt() - - info?.collectionInfo = CollectionInfo.obtain( - rowCount, - columnCount, - false - ) - } - } + view.tabsTray.updateAccessibilityCollectionInfo(rowCount, columnCount) } private fun updateTabCounter(count: Int): String {