Merge "Allow customization on the learn more text in FooterPreference"

This commit is contained in:
TreeHugger Robot
2022-01-28 01:59:06 +00:00
committed by Android (Google) Code Review
2 changed files with 33 additions and 1 deletions

View File

@@ -42,6 +42,7 @@ public class FooterPreference extends Preference {
@VisibleForTesting
View.OnClickListener mLearnMoreListener;
private CharSequence mContentDescription;
private CharSequence mLearnMoreText;
private CharSequence mLearnMoreContentDescription;
private FooterLearnMoreSpan mLearnMoreSpan;
@@ -69,7 +70,12 @@ public class FooterPreference extends Preference {
TextView learnMore = holder.itemView.findViewById(R.id.settingslib_learn_more);
if (learnMore != null && mLearnMoreListener != null) {
learnMore.setVisibility(View.VISIBLE);
SpannableString learnMoreText = new SpannableString(learnMore.getText());
if (TextUtils.isEmpty(mLearnMoreText)) {
mLearnMoreText = learnMore.getText();
} else {
learnMore.setText(mLearnMoreText);
}
SpannableString learnMoreText = new SpannableString(mLearnMoreText);
if (mLearnMoreSpan != null) {
learnMoreText.removeSpan(mLearnMoreSpan);
}
@@ -122,6 +128,18 @@ public class FooterPreference extends Preference {
return mContentDescription;
}
/**
* Sets the learn more text.
*
* @param learnMoreText The string of the learn more text.
*/
public void setLearnMoreText(CharSequence learnMoreText) {
if (!TextUtils.equals(mLearnMoreText, learnMoreText)) {
mLearnMoreText = learnMoreText;
notifyChanged();
}
}
/**
* To set content description of the learn more text. This can use for talkback
* environment if developer wants to have a customization content.

View File

@@ -63,6 +63,20 @@ public class FooterPreferenceTest {
assertThat(mFooterPreference.getTitle()).isEqualTo("summary");
}
@Test
public void setLearnMoreText_shouldSetAsTextInLearnMore() {
final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(
LayoutInflater.from(mContext).inflate(R.layout.preference_footer, null));
mFooterPreference.setLearnMoreText("Custom learn more");
mFooterPreference.setLearnMoreAction(view -> { /* do nothing */ } /* listener */);
mFooterPreference.onBindViewHolder(holder);
assertThat(((TextView) holder.findViewById(
R.id.settingslib_learn_more)).getText().toString())
.isEqualTo("Custom learn more");
}
@Test
public void setContentDescription_contentSet_shouldGetSameContentDescription() {
mFooterPreference.setContentDescription("test");