From cb20b3ef4c9cdbcdc9b8e6d845314f6e1374fd05 Mon Sep 17 00:00:00 2001 From: Tsung-Mao Fang Date: Wed, 12 May 2021 16:28:58 +0800 Subject: [PATCH] Add ability of setting content description for footer Test: Rebuilt lib and confirmed to set a content description for a footer in Settings app. Then, talkback can say correct description. Fix: 188009297 Change-Id: I7af451a553a13d5108ab279d149e4cdfdfcb620f Change-Id: I06c5fbdfca25e287988ea8ef8eca56fcce10857a --- .../settingslib/widget/FooterPreference.java | 52 +++++++++++++++++++ .../widget/FooterPreferenceTest.java | 7 +++ 2 files changed, 59 insertions(+) diff --git a/packages/SettingsLib/FooterPreference/src/com/android/settingslib/widget/FooterPreference.java b/packages/SettingsLib/FooterPreference/src/com/android/settingslib/widget/FooterPreference.java index 5ba1082e91ef7..feb3b011fe638 100644 --- a/packages/SettingsLib/FooterPreference/src/com/android/settingslib/widget/FooterPreference.java +++ b/packages/SettingsLib/FooterPreference/src/com/android/settingslib/widget/FooterPreference.java @@ -35,6 +35,7 @@ public class FooterPreference extends Preference { public static final String KEY_FOOTER = "footer_preference"; static final int ORDER_FOOTER = Integer.MAX_VALUE - 1; + private CharSequence mContentDescription; public FooterPreference(Context context, AttributeSet attrs) { super(context, attrs, R.attr.footerPreferenceStyle); @@ -52,6 +53,7 @@ public class FooterPreference extends Preference { title.setMovementMethod(new LinkMovementMethod()); title.setClickable(false); title.setLongClickable(false); + title.setContentDescription(mContentDescription); } @Override @@ -69,6 +71,26 @@ public class FooterPreference extends Preference { return getTitle(); } + /** + * To set content description of the {@link FooterPreference}. This can use for talkback + * environment if developer wants to have a customization content. + * + * @param contentDescription The resource id of the content description. + */ + public void setContentDescription(CharSequence contentDescription) { + if (!TextUtils.equals(mContentDescription, contentDescription)) { + mContentDescription = contentDescription; + notifyChanged(); + } + } + + /** + * Return the content description of footer preference. + */ + public CharSequence getContentDescription() { + return mContentDescription; + } + private void init() { setLayoutResource(R.layout.preference_footer); if (getIcon() == null) { @@ -87,6 +109,7 @@ public class FooterPreference extends Preference { private Context mContext; private String mKey; private CharSequence mTitle; + private CharSequence mContentDescription; public Builder(@NonNull Context context) { mContext = context; @@ -94,6 +117,7 @@ public class FooterPreference extends Preference { /** * To set the key value of the {@link FooterPreference}. + * * @param key The key value. */ public Builder setKey(@NonNull String key) { @@ -103,6 +127,7 @@ public class FooterPreference extends Preference { /** * To set the title of the {@link FooterPreference}. + * * @param title The title. */ public Builder setTitle(CharSequence title) { @@ -112,6 +137,7 @@ public class FooterPreference extends Preference { /** * To set the title of the {@link FooterPreference}. + * * @param titleResId The resource id of the title. */ public Builder setTitle(@StringRes int titleResId) { @@ -119,6 +145,28 @@ public class FooterPreference extends Preference { return this; } + /** + * To set content description of the {@link FooterPreference}. This can use for talkback + * environment if developer wants to have a customization content. + * + * @param contentDescription The resource id of the content description. + */ + public Builder setContentDescription(CharSequence contentDescription) { + mContentDescription = contentDescription; + return this; + } + + /** + * To set content description of the {@link FooterPreference}. This can use for talkback + * environment if developer wants to have a customization content. + * + * @param contentDescriptionResId The resource id of the content description. + */ + public Builder setContentDescription(@StringRes int contentDescriptionResId) { + mContentDescription = mContext.getText(contentDescriptionResId); + return this; + } + /** * To generate the {@link FooterPreference}. */ @@ -132,6 +180,10 @@ public class FooterPreference extends Preference { if (!TextUtils.isEmpty(mKey)) { footerPreference.setKey(mKey); } + + if (!TextUtils.isEmpty(mContentDescription)) { + footerPreference.setContentDescription(mContentDescription); + } return footerPreference; } } diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/FooterPreferenceTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/FooterPreferenceTest.java index 1a4f0efd3834a..4503669f59017 100644 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/FooterPreferenceTest.java +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/FooterPreferenceTest.java @@ -62,4 +62,11 @@ public class FooterPreferenceTest { assertThat(mFooterPreference.getTitle()).isEqualTo("summary"); } + + @Test + public void setContentDescription_contentSet_shouldGetSameContentDescription() { + mFooterPreference.setContentDescription("test"); + + assertThat(mFooterPreference.getContentDescription()).isEqualTo("test"); + } }