From 7e9146950d8349dd6d6c9534af50844d299ddfea Mon Sep 17 00:00:00 2001 From: Andrew Sapperstein Date: Tue, 30 Aug 2016 12:54:52 -0700 Subject: [PATCH] Use LinkTextView for support disclaimer text. LinkTextView uses LinkAccessibilityHelper to properly handle making links accessible. Deleted Settings' outdated fork for the version in setupwizardlib. Test: manual Change-Id: I38c031f96b5be0d45e4cc1aa358f95b229f17480 Fixes: 31077012 --- res/layout/support_disclaimer_content.xml | 2 +- .../widget/LinkAccessibilityHelper.java | 169 ------------------ .../android/settings/widget/LinkTextView.java | 4 +- 3 files changed, 4 insertions(+), 171 deletions(-) delete mode 100644 src/com/android/settings/widget/LinkAccessibilityHelper.java diff --git a/res/layout/support_disclaimer_content.xml b/res/layout/support_disclaimer_content.xml index 4fed865138f..44e5bcd0184 100644 --- a/res/layout/support_disclaimer_content.xml +++ b/res/layout/support_disclaimer_content.xml @@ -20,7 +20,7 @@ android:orientation="vertical" android:padding="24dp"> - virtualViewIds) { - final CharSequence text = mView.getText(); - if (text instanceof Spanned) { - final Spanned spannedText = (Spanned) text; - ClickableSpan[] linkSpans = spannedText.getSpans(0, spannedText.length(), - ClickableSpan.class); - for (ClickableSpan span : linkSpans) { - virtualViewIds.add(spannedText.getSpanStart(span)); - } - } - } - - @Override - protected void onPopulateEventForVirtualView(int virtualViewId, AccessibilityEvent event) { - final ClickableSpan span = getSpanForOffset(virtualViewId); - if (span != null) { - event.setContentDescription(getTextForSpan(span)); - } else { - Log.e(TAG, "ClickableSpan is null for offset: " + virtualViewId); - event.setContentDescription(mView.getText()); - } - } - - @Override - protected void onPopulateNodeForVirtualView(int virtualViewId, AccessibilityNodeInfo info) { - final ClickableSpan span = getSpanForOffset(virtualViewId); - if (span != null) { - info.setContentDescription(getTextForSpan(span)); - } else { - Log.e(TAG, "ClickableSpan is null for offset: " + virtualViewId); - info.setContentDescription(mView.getText()); - } - info.setFocusable(true); - info.setClickable(true); - getBoundsForSpan(span, mTempRect); - if (!mTempRect.isEmpty()) { - info.setBoundsInParent(getBoundsForSpan(span, mTempRect)); - } else { - Log.e(TAG, "LinkSpan bounds is empty for: " + virtualViewId); - mTempRect.set(0, 0, 1, 1); - info.setBoundsInParent(mTempRect); - } - info.addAction(AccessibilityNodeInfo.ACTION_CLICK); - } - - @Override - protected boolean onPerformActionForVirtualView(int virtualViewId, int action, - Bundle arguments) { - if (action == AccessibilityNodeInfo.ACTION_CLICK) { - ClickableSpan span = getSpanForOffset(virtualViewId); - if (span != null) { - span.onClick(mView); - return true; - } else { - Log.e(TAG, "LinkSpan is null for offset: " + virtualViewId); - } - } - return false; - } - - private ClickableSpan getSpanForOffset(int offset) { - CharSequence text = mView.getText(); - if (text instanceof Spanned) { - Spanned spannedText = (Spanned) text; - ClickableSpan[] spans = spannedText.getSpans(offset, offset, ClickableSpan.class); - if (spans.length == 1) { - return spans[0]; - } - } - return null; - } - - private CharSequence getTextForSpan(ClickableSpan span) { - CharSequence text = mView.getText(); - if (text instanceof Spanned) { - Spanned spannedText = (Spanned) text; - return spannedText.subSequence(spannedText.getSpanStart(span), - spannedText.getSpanEnd(span)); - } - return text; - } - - // Find the bounds of a span. If it spans multiple lines, it will only return the bounds for the - // section on the first line. - private Rect getBoundsForSpan(ClickableSpan span, Rect outRect) { - CharSequence text = mView.getText(); - outRect.setEmpty(); - if (text instanceof Spanned) { - Spanned spannedText = (Spanned) text; - final int spanStart = spannedText.getSpanStart(span); - final int spanEnd = spannedText.getSpanEnd(span); - final Layout layout = mView.getLayout(); - final float xStart = layout.getPrimaryHorizontal(spanStart); - final float xEnd = layout.getPrimaryHorizontal(spanEnd); - final int lineStart = layout.getLineForOffset(spanStart); - final int lineEnd = layout.getLineForOffset(spanEnd); - layout.getLineBounds(lineStart, outRect); - outRect.left = (int) xStart; - if (lineEnd == lineStart) { - outRect.right = (int) xEnd; - } // otherwise just leave it at the end of the start line - - // Offset for padding - outRect.offset(mView.getTotalPaddingLeft(), mView.getTotalPaddingTop()); - } - return outRect; - } -} diff --git a/src/com/android/settings/widget/LinkTextView.java b/src/com/android/settings/widget/LinkTextView.java index 9142e39b008..e0fa72328ca 100644 --- a/src/com/android/settings/widget/LinkTextView.java +++ b/src/com/android/settings/widget/LinkTextView.java @@ -18,12 +18,14 @@ package com.android.settings.widget; import android.content.Context; import android.support.annotation.NonNull; +import android.support.v13.view.ViewCompat; import android.text.Spanned; import android.text.method.LinkMovementMethod; import android.text.style.ClickableSpan; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.TextView; +import com.android.setupwizardlib.util.LinkAccessibilityHelper; /** * Copied from setup wizard. @@ -39,7 +41,7 @@ public class LinkTextView extends TextView { public LinkTextView(Context context, AttributeSet attrs) { super(context, attrs); mAccessibilityHelper = new LinkAccessibilityHelper(this); - setAccessibilityDelegate(mAccessibilityHelper); + ViewCompat.setAccessibilityDelegate(this, mAccessibilityHelper); } @Override