Extend FontStyle#size to accept a list of sizes
This updates the existing proto field to accept a list of sizes that
will be used to automatically scale the text to best fit.
The API to set multiple sizes is experimental.
Renderer implementation will be done in a follow up. Currently, it's
only changed to address the change in proto field.
Bug: 302531969
Test: Unit tests for wrappers
Relnote: "We have added an experimental API to automatically scale the text size based on the space it has inside of the parent."
Change-Id: Ibbe632447dcf28a9948a0877f193b4912acb23d1
diff --git a/wear/protolayout/protolayout-proto/src/main/proto/layout.proto b/wear/protolayout/protolayout-proto/src/main/proto/layout.proto
index f248a31..ab3c6c2 100644
--- a/wear/protolayout/protolayout-proto/src/main/proto/layout.proto
+++ b/wear/protolayout/protolayout-proto/src/main/proto/layout.proto
@@ -83,9 +83,29 @@
// The styling of a font (e.g. font size, and metrics).
message FontStyle {
- // The size of the font, in scaled pixels (sp). If not specified, defaults to
- // the size of the system's "body" font.
- SpProp size = 1;
+ // The available sizes of the font, in scaled pixels (sp). If not specified, defaults to the size
+ // of the system's "body" font.
+ //
+ // If more than one size is specified and this FontStyle is applied to a Text element with static
+ // text, the text size will be automatically picked from the provided sizes to try to perfectly
+ // fit within its parent bounds. In other words, the largest size from the specified preset sizes
+ // that can fit the most text within the parent bounds will be used.
+ //
+ // The specified sizes don't have to be sorted. The maximum number of sizes used is limited to 10.
+ //
+ // Note that, if multiple sizes are set, the parent of the Text element this corresponds to
+ // shouldn't have its width and height set to wrapped, as it can lead to unexpected results.
+ //
+ // If this FontStyle is set to any other element besides Text, or that Text element has dynamic
+ // field, only the last added size will be use.
+ //
+ // Any previously added values with this method or #setSize will be cleared.
+ //
+ // While this field is accessible from 1.0 as singular, it only accepts multiple values since
+ // version 1.3 and renderers supporting version 1.3 will use the multiple values to automatically
+ // scale text. Renderers who don't support this version will use the first size among multiple
+ // values.
+ repeated SpProp size = 1;
// Whether the text should be rendered in a italic typeface. If not specified,
// defaults to "false".
diff --git a/wear/protolayout/protolayout-renderer/src/main/java/androidx/wear/protolayout/renderer/inflater/ProtoLayoutInflater.java b/wear/protolayout/protolayout-renderer/src/main/java/androidx/wear/protolayout/renderer/inflater/ProtoLayoutInflater.java
index 1c4789b..6637a31 100644
--- a/wear/protolayout/protolayout-renderer/src/main/java/androidx/wear/protolayout/renderer/inflater/ProtoLayoutInflater.java
+++ b/wear/protolayout/protolayout-renderer/src/main/java/androidx/wear/protolayout/renderer/inflater/ProtoLayoutInflater.java
@@ -1155,8 +1155,11 @@
// flags in Paint if they're not supported by the given typeface).
textView.setTypeface(createTypeface(style), fontStyleToTypefaceStyle(style));
- if (style.hasSize()) {
- textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, style.getSize().getValue());
+ if (fontStyleHasSize(style)) {
+ // TODO(b/302531969): Use all sizes for autosizing.
+ textView.setTextSize(
+ TypedValue.COMPLEX_UNIT_SP,
+ style.getSize(style.getSizeCount() - 1).getValue());
}
if (style.hasLetterSpacing()) {
@@ -1176,10 +1179,16 @@
// flags in Paint if they're not supported by the given typeface).
textView.setTypeface(createTypeface(style), fontStyleToTypefaceStyle(style));
- // underline. We can implement this later by drawing a line under the text ourselves though.
-
- if (style.hasSize()) {
- textView.setTextSize(toPx(style.getSize()));
+ if (fontStyleHasSize(style)) {
+ // We are using the first added size in the FontStyle because ArcText doesn't support
+ // autosizing. This is the same behaviour as it was before size has made repeated.
+ if (style.getSizeList().size() > 1) {
+ Log.w(
+ TAG,
+ "Font size with multiple values has been used on Arc Text. Ignoring "
+ + "all size except the first one.");
+ }
+ textView.setTextSize(toPx(style.getSize(style.getSizeCount() - 1)));
}
}
@@ -2828,8 +2837,17 @@
private void applyStylesToSpan(
SpannableStringBuilder builder, int start, int end, FontStyle fontStyle) {
- if (fontStyle.hasSize()) {
- AbsoluteSizeSpan span = new AbsoluteSizeSpan(round(toPx(fontStyle.getSize())));
+ if (fontStyleHasSize(fontStyle)) {
+ // We are using the first added size in the FontStyle because ArcText doesn't support
+ // autosizing. This is the same behaviour as it was before size has made repeated.
+ if (fontStyle.getSizeList().size() > 1) {
+ Log.w(
+ TAG,
+ "Font size with multiple values has been used on Span Text. Ignoring "
+ + "all size except the first one.");
+ }
+ AbsoluteSizeSpan span = new AbsoluteSizeSpan(round(toPx(
+ fontStyle.getSize(fontStyle.getSizeCount() - 1))));
builder.setSpan(span, start, end, Spanned.SPAN_MARK_MARK);
}
@@ -2858,6 +2876,10 @@
builder.setSpan(colorSpan, start, end, Spanned.SPAN_MARK_MARK);
}
+ private static boolean fontStyleHasSize(FontStyle fontStyle) {
+ return !fontStyle.getSizeList().isEmpty();
+ }
+
private void applyModifiersToSpan(
SpannableStringBuilder builder, int start, int end, SpanModifiers modifiers) {
if (modifiers.hasClickable()) {
diff --git a/wear/protolayout/protolayout-renderer/src/test/java/androidx/wear/protolayout/renderer/helper/TestDsl.java b/wear/protolayout/protolayout-renderer/src/test/java/androidx/wear/protolayout/renderer/helper/TestDsl.java
index a291354..520b0b4 100644
--- a/wear/protolayout/protolayout-renderer/src/test/java/androidx/wear/protolayout/renderer/helper/TestDsl.java
+++ b/wear/protolayout/protolayout-renderer/src/test/java/androidx/wear/protolayout/renderer/helper/TestDsl.java
@@ -124,7 +124,7 @@
private LayoutElementProto.FontStyle toProto() {
return LayoutElementProto.FontStyle.newBuilder()
- .setSize(sp(sizeSp))
+ .addSize(sp(sizeSp))
.setItalic(bool(italic))
.setColor(color(colorArgb))
.build();
diff --git a/wear/protolayout/protolayout/api/current.txt b/wear/protolayout/protolayout/api/current.txt
index 6b9b160..3db6519 100644
--- a/wear/protolayout/protolayout/api/current.txt
+++ b/wear/protolayout/protolayout/api/current.txt
@@ -551,6 +551,7 @@
method public androidx.wear.protolayout.TypeBuilders.BoolProp? getItalic();
method public androidx.wear.protolayout.DimensionBuilders.EmProp? getLetterSpacing();
method public androidx.wear.protolayout.DimensionBuilders.SpProp? getSize();
+ method @SuppressCompatibility @androidx.wear.protolayout.expression.ProtoLayoutExperimental public java.util.List<androidx.wear.protolayout.DimensionBuilders.SpProp!> getSizes();
method public androidx.wear.protolayout.TypeBuilders.BoolProp? getUnderline();
method @SuppressCompatibility @androidx.wear.protolayout.expression.ProtoLayoutExperimental public androidx.wear.protolayout.LayoutElementBuilders.FontVariantProp? getVariant();
method public androidx.wear.protolayout.LayoutElementBuilders.FontWeightProp? getWeight();
@@ -564,6 +565,7 @@
method public androidx.wear.protolayout.LayoutElementBuilders.FontStyle.Builder setItalic(boolean);
method public androidx.wear.protolayout.LayoutElementBuilders.FontStyle.Builder setLetterSpacing(androidx.wear.protolayout.DimensionBuilders.EmProp);
method public androidx.wear.protolayout.LayoutElementBuilders.FontStyle.Builder setSize(androidx.wear.protolayout.DimensionBuilders.SpProp);
+ method @SuppressCompatibility @androidx.wear.protolayout.expression.ProtoLayoutExperimental public androidx.wear.protolayout.LayoutElementBuilders.FontStyle.Builder setSizes(androidx.wear.protolayout.DimensionBuilders.SpProp!...);
method public androidx.wear.protolayout.LayoutElementBuilders.FontStyle.Builder setUnderline(androidx.wear.protolayout.TypeBuilders.BoolProp);
method public androidx.wear.protolayout.LayoutElementBuilders.FontStyle.Builder setUnderline(boolean);
method @SuppressCompatibility @androidx.wear.protolayout.expression.ProtoLayoutExperimental public androidx.wear.protolayout.LayoutElementBuilders.FontStyle.Builder setVariant(androidx.wear.protolayout.LayoutElementBuilders.FontVariantProp);
diff --git a/wear/protolayout/protolayout/api/restricted_current.txt b/wear/protolayout/protolayout/api/restricted_current.txt
index 6b9b160..3db6519 100644
--- a/wear/protolayout/protolayout/api/restricted_current.txt
+++ b/wear/protolayout/protolayout/api/restricted_current.txt
@@ -551,6 +551,7 @@
method public androidx.wear.protolayout.TypeBuilders.BoolProp? getItalic();
method public androidx.wear.protolayout.DimensionBuilders.EmProp? getLetterSpacing();
method public androidx.wear.protolayout.DimensionBuilders.SpProp? getSize();
+ method @SuppressCompatibility @androidx.wear.protolayout.expression.ProtoLayoutExperimental public java.util.List<androidx.wear.protolayout.DimensionBuilders.SpProp!> getSizes();
method public androidx.wear.protolayout.TypeBuilders.BoolProp? getUnderline();
method @SuppressCompatibility @androidx.wear.protolayout.expression.ProtoLayoutExperimental public androidx.wear.protolayout.LayoutElementBuilders.FontVariantProp? getVariant();
method public androidx.wear.protolayout.LayoutElementBuilders.FontWeightProp? getWeight();
@@ -564,6 +565,7 @@
method public androidx.wear.protolayout.LayoutElementBuilders.FontStyle.Builder setItalic(boolean);
method public androidx.wear.protolayout.LayoutElementBuilders.FontStyle.Builder setLetterSpacing(androidx.wear.protolayout.DimensionBuilders.EmProp);
method public androidx.wear.protolayout.LayoutElementBuilders.FontStyle.Builder setSize(androidx.wear.protolayout.DimensionBuilders.SpProp);
+ method @SuppressCompatibility @androidx.wear.protolayout.expression.ProtoLayoutExperimental public androidx.wear.protolayout.LayoutElementBuilders.FontStyle.Builder setSizes(androidx.wear.protolayout.DimensionBuilders.SpProp!...);
method public androidx.wear.protolayout.LayoutElementBuilders.FontStyle.Builder setUnderline(androidx.wear.protolayout.TypeBuilders.BoolProp);
method public androidx.wear.protolayout.LayoutElementBuilders.FontStyle.Builder setUnderline(boolean);
method @SuppressCompatibility @androidx.wear.protolayout.expression.ProtoLayoutExperimental public androidx.wear.protolayout.LayoutElementBuilders.FontStyle.Builder setVariant(androidx.wear.protolayout.LayoutElementBuilders.FontVariantProp);
diff --git a/wear/protolayout/protolayout/src/main/java/androidx/wear/protolayout/LayoutElementBuilders.java b/wear/protolayout/protolayout/src/main/java/androidx/wear/protolayout/LayoutElementBuilders.java
index 80c73df..1cf6bce 100644
--- a/wear/protolayout/protolayout/src/main/java/androidx/wear/protolayout/LayoutElementBuilders.java
+++ b/wear/protolayout/protolayout/src/main/java/androidx/wear/protolayout/LayoutElementBuilders.java
@@ -27,6 +27,7 @@
import androidx.annotation.OptIn;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.Scope;
+import androidx.annotation.VisibleForTesting;
import androidx.wear.protolayout.ColorBuilders.Brush;
import androidx.wear.protolayout.ColorBuilders.ColorProp;
import androidx.wear.protolayout.DeviceParametersBuilders.DeviceParameters;
@@ -608,21 +609,6 @@
}
/**
- * Gets the size of the font, in scaled pixels (sp). If not specified, defaults to the size
- * of the system's "body" font.
- *
- * @since 1.0
- */
- @Nullable
- public SpProp getSize() {
- if (mImpl.hasSize()) {
- return SpProp.fromProto(mImpl.getSize());
- } else {
- return null;
- }
- }
-
- /**
* Gets whether the text should be rendered in a italic typeface. If not specified, defaults
* to "false".
*
@@ -713,6 +699,35 @@
}
}
+ /**
+ * Gets the size of the font, in scaled pixels (sp). If not specified, defaults to the size
+ * of the system's "body" font. If more than one size was originally added, it will
+ * return the last one.
+ *
+ * @since 1.0
+ */
+ @Nullable
+ public SpProp getSize() {
+ List<DimensionProto.SpProp> sizes = mImpl.getSizeList();
+ return !sizes.isEmpty() ? SpProp.fromProto(sizes.get(sizes.size() - 1)) : null;
+ }
+
+ /**
+ * Gets the available sizes of the font, in scaled pixels (sp). If not specified, defaults
+ * to the size of the system's "body" font.
+ *
+ * @since 1.3
+ */
+ @NonNull
+ @ProtoLayoutExperimental
+ public List<SpProp> getSizes() {
+ List<SpProp> list = new ArrayList<>();
+ for (DimensionProto.SpProp item : mImpl.getSizeList()) {
+ list.add(SpProp.fromProto(item));
+ }
+ return Collections.unmodifiableList(list);
+ }
+
/** Get the fingerprint for this object, or null if unknown. */
@RestrictTo(Scope.LIBRARY_GROUP)
@Nullable
@@ -764,6 +779,7 @@
/** Builder for {@link FontStyle} */
public static final class Builder {
+ @VisibleForTesting static final int TEXT_SIZES_LIMIT = 10;
private final LayoutElementProto.FontStyle.Builder mImpl =
LayoutElementProto.FontStyle.newBuilder();
private final Fingerprint mFingerprint = new Fingerprint(-374492482);
@@ -771,16 +787,48 @@
public Builder() {}
/**
- * Sets the size of the font, in scaled pixels (sp). If not specified, defaults to the
- * size of the system's "body" font.
+ * Sets the available sizes of the font, in scaled pixels (sp). If not specified,
+ * defaults to the size of the system's "body" font.
*
- * @since 1.0
+ * <p>If more than one size is specified and this {@link FontStyle} is applied to a
+ * {@link Text} element with static text, the text size will be automatically picked
+ * from the provided sizes to try to perfectly fit within its parent bounds. In other
+ * words, the largest size from the specified preset sizes that can fit the most text
+ * within the parent bounds will be used.
+ *
+ * <p>The specified sizes don't have to be sorted. The maximum number of sizes used is
+ * limited to 10.
+ *
+ * <p>Note that, if multiple sizes are set, the parent of the {@link Text} element this
+ * corresponds to shouldn't have its width and height set to wrapped, as it can lead to
+ * unexpected results.
+ *
+ * <p>If this {@link FontStyle} is set to any other element besides {@link Text} or
+ * that {@link Text} element has dynamic field, only the last added size will be use.
+ *
+ * <p>Any previously added values with this method or {@link #setSize} will be cleared.
+ *
+ * <p>While this field is accessible from 1.0 as singular, it only accepts multiple
+ * values since version 1.3 and renderers supporting version 1.3 will use the multiple
+ * values to automatically scale text. Renderers who don't support this version will
+ * use the last size among multiple values.
+ *
+ * @throws IllegalArgumentException if the number of available sizes is larger than 10.
+ * @since 1.3
*/
@NonNull
- public Builder setSize(@NonNull SpProp size) {
- mImpl.setSize(size.toProto());
- mFingerprint.recordPropertyUpdate(
- 1, checkNotNull(size.getFingerprint()).aggregateValueAsInt());
+ @ProtoLayoutExperimental
+ public Builder setSizes(@NonNull SpProp... sizes) {
+ if (sizes.length > TEXT_SIZES_LIMIT) {
+ throw new IllegalArgumentException(
+ "Number of available sizes can't be larger than 10.");
+ }
+ mImpl.clearSize();
+ for (SpProp size: sizes) {
+ mImpl.addSize(size.toProto());
+ mFingerprint.recordPropertyUpdate(
+ 1, checkNotNull(size.getFingerprint()).aggregateValueAsInt());
+ }
return this;
}
@@ -830,6 +878,23 @@
}
/**
+ * Sets the size of the font, in scaled pixels (sp). If not specified, defaults to the
+ * size of the system's "body" font.
+ *
+ * <p>Any previously added values with this method or {@link #setSizes} will be cleared.
+ *
+ * @since 1.0
+ */
+ @NonNull
+ public Builder setSize(@NonNull SpProp size) {
+ mImpl.clearSize();
+ mImpl.addSize(size.toProto());
+ mFingerprint.recordPropertyUpdate(
+ 1, checkNotNull(size.getFingerprint()).aggregateValueAsInt());
+ return this;
+ }
+
+ /**
* Sets the text color. If not defined, defaults to white.
*
* <p>While this field is statically accessible from 1.0, it's only bindable since
diff --git a/wear/protolayout/protolayout/src/test/java/androidx/wear/protolayout/LayoutElementBuildersTest.java b/wear/protolayout/protolayout/src/test/java/androidx/wear/protolayout/LayoutElementBuildersTest.java
index a30c860..6f10e5a 100644
--- a/wear/protolayout/protolayout/src/test/java/androidx/wear/protolayout/LayoutElementBuildersTest.java
+++ b/wear/protolayout/protolayout/src/test/java/androidx/wear/protolayout/LayoutElementBuildersTest.java
@@ -16,6 +16,8 @@
package androidx.wear.protolayout;
+import static androidx.wear.protolayout.DimensionBuilders.sp;
+
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
@@ -203,6 +205,84 @@
}
@Test
+ public void testFontStyleSetMultipleSizes() {
+ int size1 = 12;
+ int size2 = 30;
+ int lastSize = 20;
+ int[] expectedSizes = {size1, size2, lastSize};
+ LayoutElementBuilders.FontStyle fontStyle =
+ new LayoutElementBuilders.FontStyle.Builder()
+ .setSizes(sp(size1), sp(size2), sp(lastSize))
+ .build();
+
+ LayoutElementProto.FontStyle fontStyleProto = fontStyle.toProto();
+
+ assertThat(
+ fontStyleProto.getSizeList().stream().mapToInt(sp -> (int) sp.getValue()).toArray())
+ .isEqualTo(expectedSizes);
+ // Make sure that if 1 size is used than it's the last one.
+ assertThat(fontStyle.getSize().getValue()).isEqualTo(lastSize);
+ assertThat(
+ fontStyle.getSizes().stream().mapToInt(sp -> (int) sp.getValue()).toArray())
+ .isEqualTo(expectedSizes);
+ }
+
+ @Test
+ public void testFontStyleSetSize_moreTimes_usesLastOne() {
+ int lastSize = 20;
+ LayoutElementBuilders.FontStyle fontStyle =
+ new LayoutElementBuilders.FontStyle.Builder()
+ .setSize(sp(12))
+ .setSize(sp(30))
+ .setSize(sp(lastSize))
+ .build();
+
+ LayoutElementProto.FontStyle fontStyleProto = fontStyle.toProto();
+
+ assertThat(fontStyleProto.getSizeList().size()).isEqualTo(1);
+ assertThat(fontStyleProto.getSizeList().get(0).getValue()).isEqualTo(lastSize);
+ // Make sure that if 1 size is used than it's the last one.
+ assertThat(fontStyle.getSize().getValue()).isEqualTo(lastSize);
+ assertThat(fontStyle.getSizes().size()).isEqualTo(1);
+ assertThat(fontStyle.getSizes().get(0).getValue()).isEqualTo(lastSize);
+ }
+
+ @Test
+ public void testFontStyleSetSize_setSizes_overrides() {
+ int size1 = 12;
+ int size2 = 30;
+ int[] expectedSizes = {size1, size2};
+ LayoutElementBuilders.FontStyle fontStyle =
+ new LayoutElementBuilders.FontStyle.Builder()
+ .setSize(sp(20))
+ .setSizes(sp(size1), sp(size2))
+ .build();
+
+ LayoutElementProto.FontStyle fontStyleProto = fontStyle.toProto();
+
+ assertThat(
+ fontStyleProto.getSizeList().stream().mapToInt(sp -> (int) sp.getValue()).toArray())
+ .isEqualTo(expectedSizes);
+ // Make sure that if 1 size is used than it's the last one.
+ assertThat(fontStyle.getSize().getValue()).isEqualTo(size2);
+ assertThat(
+ fontStyle.getSizes().stream().mapToInt(sp -> (int) sp.getValue()).toArray())
+ .isEqualTo(expectedSizes);
+ }
+
+ @Test
+ public void testFontStyleSetSize_tooManySizes_throws() {
+ DimensionBuilders.SpProp[] sizes =
+ new DimensionBuilders.SpProp[
+ LayoutElementBuilders.FontStyle.Builder.TEXT_SIZES_LIMIT + 1];
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> new LayoutElementBuilders.FontStyle.Builder()
+ .setSizes(sizes)
+ .build());
+ }
+
+ @Test
public void textSetText_withoutLayoutConstraint_throws() {
assertThrows(
IllegalStateException.class,
diff --git a/wear/tiles/tiles/lint-baseline.xml b/wear/tiles/tiles/lint-baseline.xml
index eb4c7b5..faa4a6b 100644
--- a/wear/tiles/tiles/lint-baseline.xml
+++ b/wear/tiles/tiles/lint-baseline.xml
@@ -1803,18 +1803,9 @@
<issue
id="RestrictedApiAndroidX"
- message="FontStyle.hasSize can only be called from within the same library group (referenced groupId=`androidx.wear.protolayout` from groupId=`androidx.wear.tiles`)"
- errorLine1=" if (mImpl.hasSize()) {"
- errorLine2=" ~~~~~~~">
- <location
- file="src/main/java/androidx/wear/tiles/LayoutElementBuilders.java"/>
- </issue>
-
- <issue
- id="RestrictedApiAndroidX"
- message="FontStyle.getSize can only be called from within the same library group (referenced groupId=`androidx.wear.protolayout` from groupId=`androidx.wear.tiles`)"
- errorLine1=" return DimensionBuilders.SpProp.fromProto(mImpl.getSize());"
- errorLine2=" ~~~~~~~">
+ message="FontStyle.getSizeList can only be called from within the same library group (referenced groupId=`androidx.wear.protolayout` from groupId=`androidx.wear.tiles`)"
+ errorLine1=" List<DimensionProto.SpProp> sizes = mImpl.getSizeList();"
+ errorLine2=" ~~~~~~~~~~~">
<location
file="src/main/java/androidx/wear/tiles/LayoutElementBuilders.java"/>
</issue>
@@ -1947,8 +1938,17 @@
<issue
id="RestrictedApiAndroidX"
- message="Builder.setSize can only be called from within the same library group (referenced groupId=`androidx.wear.protolayout` from groupId=`androidx.wear.tiles`)"
- errorLine1=" mImpl.setSize(size.toProto());"
+ message="Builder.clearSize can only be called from within the same library group (referenced groupId=`androidx.wear.protolayout` from groupId=`androidx.wear.tiles`)"
+ errorLine1=" mImpl.clearSize();"
+ errorLine2=" ~~~~~~~~~">
+ <location
+ file="src/main/java/androidx/wear/tiles/LayoutElementBuilders.java"/>
+ </issue>
+
+ <issue
+ id="RestrictedApiAndroidX"
+ message="Builder.addSize can only be called from within the same library group (referenced groupId=`androidx.wear.protolayout` from groupId=`androidx.wear.tiles`)"
+ errorLine1=" mImpl.addSize(size.toProto());"
errorLine2=" ~~~~~~~">
<location
file="src/main/java/androidx/wear/tiles/LayoutElementBuilders.java"/>
diff --git a/wear/tiles/tiles/src/main/java/androidx/wear/tiles/LayoutElementBuilders.java b/wear/tiles/tiles/src/main/java/androidx/wear/tiles/LayoutElementBuilders.java
index a089888..259bddf 100644
--- a/wear/tiles/tiles/src/main/java/androidx/wear/tiles/LayoutElementBuilders.java
+++ b/wear/tiles/tiles/src/main/java/androidx/wear/tiles/LayoutElementBuilders.java
@@ -31,6 +31,7 @@
import androidx.annotation.RestrictTo.Scope;
import androidx.wear.protolayout.expression.Fingerprint;
import androidx.wear.protolayout.proto.AlignmentProto;
+import androidx.wear.protolayout.proto.DimensionProto;
import androidx.wear.protolayout.proto.FingerprintProto;
import androidx.wear.protolayout.proto.FingerprintProto.TreeFingerprint;
import androidx.wear.protolayout.proto.LayoutElementProto;
@@ -622,8 +623,9 @@
*/
@Nullable
public DimensionBuilders.SpProp getSize() {
- if (mImpl.hasSize()) {
- return DimensionBuilders.SpProp.fromProto(mImpl.getSize());
+ List<DimensionProto.SpProp> sizes = mImpl.getSizeList();
+ if (!sizes.isEmpty()) {
+ return DimensionBuilders.SpProp.fromProto(sizes.get(0));
} else {
return null;
}
@@ -744,7 +746,8 @@
*/
@NonNull
public Builder setSize(@NonNull DimensionBuilders.SpProp size) {
- mImpl.setSize(size.toProto());
+ mImpl.clearSize();
+ mImpl.addSize(size.toProto());
mFingerprint.recordPropertyUpdate(
1, checkNotNull(size.getFingerprint()).aggregateValueAsInt());
return this;