Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a9c4305e authored by Sally Yuen's avatar Sally Yuen Committed by Android (Google) Code Review
Browse files

Merge "Use CollectionInfo and CollectionItemInfo api in RadioGroup and RadioButton"

parents 9f784592 69a146ca
Loading
Loading
Loading
Loading
+23 −6
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package android.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.accessibility.AccessibilityNodeInfo;


/**
@@ -81,4 +82,20 @@ public class RadioButton extends CompoundButton {
    public CharSequence getAccessibilityClassName() {
        return RadioButton.class.getName();
    }

    @Override
    public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
        super.onInitializeAccessibilityNodeInfo(info);
        if (getParent() instanceof RadioGroup) {
            RadioGroup radioGroup = (RadioGroup) getParent();
            if (radioGroup.getOrientation() == LinearLayout.HORIZONTAL) {
                info.setCollectionItemInfo(AccessibilityNodeInfo.CollectionItemInfo.obtain(0, 1,
                        radioGroup.getIndexWithinVisibleButtons(this), 1, false, isChecked()));
            } else {
                info.setCollectionItemInfo(AccessibilityNodeInfo.CollectionItemInfo.obtain(
                        radioGroup.getIndexWithinVisibleButtons(this), 1, 0, 1,
                        false, isChecked()));
            }
        }
    }
}
+50 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package android.widget;

import android.annotation.IdRes;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UnsupportedAppUsage;
import android.content.Context;
import android.content.res.TypedArray;
@@ -26,6 +27,7 @@ import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStructure;
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.autofill.AutofillManager;
import android.view.autofill.AutofillValue;

@@ -93,6 +95,7 @@ public class RadioGroup extends LinearLayout {
        if (getImportantForAutofill() == IMPORTANT_FOR_AUTOFILL_AUTO) {
            setImportantForAutofill(IMPORTANT_FOR_AUTOFILL_YES);
        }
        setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);

        // retrieve selected radio button as requested by the user in the
        // XML layout file
@@ -475,4 +478,50 @@ public class RadioGroup extends LinearLayout {
        }
        return null;
    }

    @Override
    public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
        super.onInitializeAccessibilityNodeInfo(info);
        if (this.getOrientation() == HORIZONTAL) {
            info.setCollectionInfo(AccessibilityNodeInfo.CollectionInfo.obtain(1,
                    getVisibleChildCount(), false,
                    AccessibilityNodeInfo.CollectionInfo.SELECTION_MODE_SINGLE));
        } else {
            info.setCollectionInfo(
                    AccessibilityNodeInfo.CollectionInfo.obtain(getVisibleChildCount(),
                    1, false,
                    AccessibilityNodeInfo.CollectionInfo.SELECTION_MODE_SINGLE));
        }
    }

    private int getVisibleChildCount() {
        int count = 0;
        for (int i = 0; i < getChildCount(); i++) {
            if (this.getChildAt(i) instanceof RadioButton) {
                if (((RadioButton) this.getChildAt(i)).getVisibility() == VISIBLE) {
                    count++;
                }
            }
        }
        return count;
    }

    int getIndexWithinVisibleButtons(@Nullable View child) {
        if (!(child instanceof RadioButton)) {
            return -1;
        }
        int index = 0;
        for (int i = 0; i < getChildCount(); i++) {
            if (this.getChildAt(i) instanceof RadioButton) {
                RadioButton radioButton = (RadioButton) this.getChildAt(i);
                if (radioButton == child) {
                    return index;
                }
                if (radioButton.getVisibility() == VISIBLE) {
                    index++;
                }
            }
        }
        return -1;
    }
}
 No newline at end of file