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

Commit 6ac7259f authored by Matías Hernández's avatar Matías Hernández
Browse files

Support setting a specific content description for the widget in SelectorWithWidgetPreference

Bug: 368124250
Test: atest SelectorWithWidgetPreferenceTest
Flag: EXEMPT minor addition
Change-Id: Iff0f1f7df27c15fb1a1d48322b2694cc640a053b
parent 6f273d49
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -68,6 +68,7 @@ public class SelectorWithWidgetPreference extends CheckBoxPreference {

    private View mExtraWidgetContainer;
    private ImageView mExtraWidget;
    @Nullable private String mExtraWidgetContentDescription;
    private boolean mIsCheckBox = false;  // whether to display this button as a checkbox

    private View.OnClickListener mExtraWidgetOnClickListener;
@@ -173,6 +174,12 @@ public class SelectorWithWidgetPreference extends CheckBoxPreference {

        setExtraWidgetOnClickListener(mExtraWidgetOnClickListener);

        if (mExtraWidget != null) {
            mExtraWidget.setContentDescription(mExtraWidgetContentDescription != null
                    ? mExtraWidgetContentDescription
                    : getContext().getString(R.string.settings_label));
        }

        if (Flags.allowSetTitleMaxLines()) {
            TextView title = (TextView) holder.findViewById(android.R.id.title);
            title.setMaxLines(mTitleMaxLines);
@@ -209,6 +216,17 @@ public class SelectorWithWidgetPreference extends CheckBoxPreference {
                ? View.VISIBLE : View.GONE);
    }

    /**
     * Sets the content description of the extra widget. If {@code null}, a default content
     * description will be used ("Settings").
     */
    public void setExtraWidgetContentDescription(@Nullable String contentDescription) {
        if (!TextUtils.equals(mExtraWidgetContentDescription, contentDescription)) {
            mExtraWidgetContentDescription = contentDescription;
            notifyChanged();
        }
    }

    /**
     * Returns whether this preference is a checkbox.
     */
+20 −0
Original line number Diff line number Diff line
@@ -183,6 +183,26 @@ public class SelectorWithWidgetPreferenceTest {
        assertThat(title.getMaxLines()).isEqualTo(titleMaxLines);
    }

    @Test
    public void onBindViewHolder_appliesWidgetContentDescription() {
        mPreference = new SelectorWithWidgetPreference(mContext);
        View view = LayoutInflater.from(mContext)
                .inflate(mPreference.getLayoutResource(), /* root= */ null);
        PreferenceViewHolder preferenceViewHolder =
                PreferenceViewHolder.createInstanceForTests(view);

        mPreference.setExtraWidgetContentDescription("this is clearer");
        mPreference.onBindViewHolder(preferenceViewHolder);

        View widget = preferenceViewHolder.findViewById(R.id.selector_extra_widget);
        assertThat(widget.getContentDescription().toString()).isEqualTo("this is clearer");

        mPreference.setExtraWidgetContentDescription(null);
        mPreference.onBindViewHolder(preferenceViewHolder);

        assertThat(widget.getContentDescription().toString()).isEqualTo("Settings");
    }

    @Test
    public void nullSummary_containerShouldBeGone() {
        mPreference.setSummary(null);