Add content description to TitleChip
Since TitleChip can have an icon, we should add the public API
for developer to provide a custom content description.
Bug: 320438882
Test: Added
Relnote: "Add option to set content description to TitleChip."
Change-Id: I5d21fef571a06d600b849d509738bd4dd279506d
diff --git a/wear/protolayout/protolayout-material/api/current.txt b/wear/protolayout/protolayout-material/api/current.txt
index 2c6baf1..fda934c 100644
--- a/wear/protolayout/protolayout-material/api/current.txt
+++ b/wear/protolayout/protolayout-material/api/current.txt
@@ -203,6 +203,7 @@
method public static androidx.wear.protolayout.material.TitleChip? fromLayoutElement(androidx.wear.protolayout.LayoutElementBuilders.LayoutElement);
method public androidx.wear.protolayout.material.ChipColors getChipColors();
method public androidx.wear.protolayout.ModifiersBuilders.Clickable getClickable();
+ method public androidx.wear.protolayout.TypeBuilders.StringProp? getContentDescription();
method public int getHorizontalAlignment();
method public String? getIconContent();
method public String getText();
@@ -213,6 +214,8 @@
ctor public TitleChip.Builder(android.content.Context, String, androidx.wear.protolayout.ModifiersBuilders.Clickable, androidx.wear.protolayout.DeviceParametersBuilders.DeviceParameters);
method public androidx.wear.protolayout.material.TitleChip build();
method public androidx.wear.protolayout.material.TitleChip.Builder setChipColors(androidx.wear.protolayout.material.ChipColors);
+ method public androidx.wear.protolayout.material.TitleChip.Builder setContentDescription(androidx.wear.protolayout.TypeBuilders.StringProp);
+ method public androidx.wear.protolayout.material.TitleChip.Builder setContentDescription(CharSequence);
method public androidx.wear.protolayout.material.TitleChip.Builder setHorizontalAlignment(int);
method public androidx.wear.protolayout.material.TitleChip.Builder setIconContent(String);
method public androidx.wear.protolayout.material.TitleChip.Builder setWidth(androidx.wear.protolayout.DimensionBuilders.ContainerDimension);
diff --git a/wear/protolayout/protolayout-material/api/restricted_current.txt b/wear/protolayout/protolayout-material/api/restricted_current.txt
index 2c6baf1..fda934c 100644
--- a/wear/protolayout/protolayout-material/api/restricted_current.txt
+++ b/wear/protolayout/protolayout-material/api/restricted_current.txt
@@ -203,6 +203,7 @@
method public static androidx.wear.protolayout.material.TitleChip? fromLayoutElement(androidx.wear.protolayout.LayoutElementBuilders.LayoutElement);
method public androidx.wear.protolayout.material.ChipColors getChipColors();
method public androidx.wear.protolayout.ModifiersBuilders.Clickable getClickable();
+ method public androidx.wear.protolayout.TypeBuilders.StringProp? getContentDescription();
method public int getHorizontalAlignment();
method public String? getIconContent();
method public String getText();
@@ -213,6 +214,8 @@
ctor public TitleChip.Builder(android.content.Context, String, androidx.wear.protolayout.ModifiersBuilders.Clickable, androidx.wear.protolayout.DeviceParametersBuilders.DeviceParameters);
method public androidx.wear.protolayout.material.TitleChip build();
method public androidx.wear.protolayout.material.TitleChip.Builder setChipColors(androidx.wear.protolayout.material.ChipColors);
+ method public androidx.wear.protolayout.material.TitleChip.Builder setContentDescription(androidx.wear.protolayout.TypeBuilders.StringProp);
+ method public androidx.wear.protolayout.material.TitleChip.Builder setContentDescription(CharSequence);
method public androidx.wear.protolayout.material.TitleChip.Builder setHorizontalAlignment(int);
method public androidx.wear.protolayout.material.TitleChip.Builder setIconContent(String);
method public androidx.wear.protolayout.material.TitleChip.Builder setWidth(androidx.wear.protolayout.DimensionBuilders.ContainerDimension);
diff --git a/wear/protolayout/protolayout-material/src/main/java/androidx/wear/protolayout/material/TitleChip.java b/wear/protolayout/protolayout-material/src/main/java/androidx/wear/protolayout/material/TitleChip.java
index 25de00b..92140a6 100644
--- a/wear/protolayout/protolayout-material/src/main/java/androidx/wear/protolayout/material/TitleChip.java
+++ b/wear/protolayout/protolayout-material/src/main/java/androidx/wear/protolayout/material/TitleChip.java
@@ -24,6 +24,7 @@
import static androidx.wear.protolayout.material.ChipDefaults.TITLE_HORIZONTAL_PADDING;
import static androidx.wear.protolayout.material.ChipDefaults.TITLE_PRIMARY_COLORS;
import static androidx.wear.protolayout.materialcore.Helper.checkNotNull;
+import static androidx.wear.protolayout.materialcore.Helper.staticString;
import android.content.Context;
@@ -38,6 +39,7 @@
import androidx.wear.protolayout.LayoutElementBuilders.HorizontalAlignment;
import androidx.wear.protolayout.LayoutElementBuilders.LayoutElement;
import androidx.wear.protolayout.ModifiersBuilders.Clickable;
+import androidx.wear.protolayout.TypeBuilders.StringProp;
import androidx.wear.protolayout.expression.Fingerprint;
import androidx.wear.protolayout.expression.ProtoLayoutExperimental;
import androidx.wear.protolayout.proto.LayoutElementProto;
@@ -89,6 +91,7 @@
@NonNull private final DeviceParameters mDeviceParameters;
@NonNull private ChipColors mChipColors = TITLE_PRIMARY_COLORS;
@HorizontalAlignment private int mHorizontalAlign = HORIZONTAL_ALIGN_UNDEFINED;
+ @Nullable private StringProp mContentDescription = null;
// Indicates that the width isn't set, so it will be automatically set by Chip.Builder
// constructor.
@@ -166,6 +169,28 @@
return this;
}
+ /**
+ * Sets the static content description for the {@link TitleChip}. It is highly recommended
+ * to provide this for chip containing an icon.
+ */
+ @NonNull
+ public Builder setContentDescription(@NonNull CharSequence contentDescription) {
+ return setContentDescription(staticString(contentDescription.toString()));
+ }
+
+ /**
+ * Sets the content description for the {@link TitleChip}. It is highly recommended to
+ * provide this for chip containing an icon.
+ *
+ * <p>While this field is statically accessible from 1.0, it's only bindable since version
+ * 1.2 and renderers supporting version 1.2 will use the dynamic value (if set).
+ */
+ @NonNull
+ public Builder setContentDescription(@NonNull StringProp contentDescription) {
+ this.mContentDescription = contentDescription;
+ return this;
+ }
+
/** Constructs and returns {@link TitleChip} with the provided content and look. */
@NonNull
@Override
@@ -174,7 +199,10 @@
Chip.Builder chipBuilder =
new Chip.Builder(mContext, mClickable, mDeviceParameters)
.setChipColors(mChipColors)
- .setContentDescription(mText)
+ .setContentDescription(
+ mContentDescription == null
+ ? staticString(mText)
+ : mContentDescription)
.setHeight(TITLE_HEIGHT)
.setMaxLines(1)
.setHorizontalPadding(TITLE_HORIZONTAL_PADDING)
@@ -255,6 +283,12 @@
return coreChip == null ? null : new TitleChip(new Chip(coreChip));
}
+ /** Returns content description of this TitleChip. */
+ @Nullable
+ public StringProp getContentDescription() {
+ return mElement.getContentDescription();
+ }
+
@RestrictTo(Scope.LIBRARY_GROUP)
@NonNull
@Override
diff --git a/wear/protolayout/protolayout-material/src/test/java/androidx/wear/protolayout/material/TitleChipTest.java b/wear/protolayout/protolayout-material/src/test/java/androidx/wear/protolayout/material/TitleChipTest.java
index 3463ba3..4570ab7 100644
--- a/wear/protolayout/protolayout-material/src/test/java/androidx/wear/protolayout/material/TitleChipTest.java
+++ b/wear/protolayout/protolayout-material/src/test/java/androidx/wear/protolayout/material/TitleChipTest.java
@@ -75,13 +75,16 @@
@Test
public void testTitleChipCustom() {
DpProp width = dp(150);
+ String description = "Test description";
TitleChip titleChip =
new TitleChip.Builder(CONTEXT, MAIN_TEXT, CLICKABLE, DEVICE_PARAMETERS)
.setChipColors(COLORS)
.setWidth(width)
+ .setContentDescription(description)
.build();
assertChip(titleChip, COLORS, width, /* iconId= */ null);
+ assertThat(titleChip.getContentDescription().getValue()).isEqualTo(description);
}
@Test