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

Commit 3b55002f authored by Matías Hernández's avatar Matías Hernández
Browse files

Add unit to FixedString metric values

FixedString can be used to represent quantities that cannot be expressed in a single number, and it makes sense for it to have a unit in those cases.

Bug: 415827681
Test: atest NotificationMetricStyleTest
Flag: android.app.api_metric_style
Change-Id: Idd733e64ab35481c5ed03309a67d74cef12f8665
parent 9889cd23
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -7027,6 +7027,8 @@ package android.app {
  public static final class Notification.Metric.FixedString extends android.app.Notification.Metric.MetricValue {
    ctor public Notification.Metric.FixedString(@NonNull String);
    ctor public Notification.Metric.FixedString(@NonNull String, @Nullable String);
    method @Nullable public String getUnit();
    method @NonNull public String getValue();
  }
+39 −6
Original line number Diff line number Diff line
@@ -12893,55 +12893,88 @@ public class Notification implements Parcelable
        public static final class FixedString extends MetricValue {
            private static final String KEY_VALUE = "value";
            private static final String KEY_UNIT = "unit";
            private final String mValue;
            private final String mUnit;
            /**
             * Creates a {@link FixedString} instance with the specified String.
             */
            public FixedString(@NonNull String value) {
                this(value, null);
            }
            /**
             * Creates a {@link FixedString} instance with the specified String.
             *
             * @param unit optional unit for the value. Limit this to a few characters.
             */
            public FixedString(@NonNull String value, @Nullable String unit) {
                mValue = safeString(requireNonNull(value));
                mUnit = safeString(unit);
            }
            @NonNull
            private static FixedString fromBundle(Bundle bundle) {
                return new FixedString(bundle.getString(KEY_VALUE, ""));
                return new FixedString(
                        bundle.getString(KEY_VALUE, ""),
                        bundle.getString(KEY_UNIT));
            }
            /** @hide */
            @Override
            protected void toBundle(Bundle bundle) {
                bundle.putString(KEY_VALUE, mValue);
                bundle.putString(KEY_UNIT, mUnit);
            }
            @Override
            public boolean equals(Object obj) {
                if (!(obj instanceof FixedString that)) return false;
                if (this == that) return true;
                return Objects.equals(this.mValue, that.mValue);
                return Objects.equals(this.mValue, that.mValue)
                        && Objects.equals(this.mUnit, that.mUnit);
            }
            @Override
            public int hashCode() {
                return mValue.hashCode();
                return Objects.hash(mValue, mUnit);
            }
            @Override
            public String toString() {
                return getClass().getSimpleName() + "{" + mValue + "}";
                return getClass().getSimpleName() + "{"
                        + "mValue=" + mValue
                        + ", mUnit=" + mUnit
                        + "}";
            }
            /** The string value. */
            @NonNull public String getValue() {
            @NonNull
            public String getValue() {
                return mValue;
            }
            /**
             * A unit for the value.
             *
             * <p>This may not be shown to the user in all views.
             *
             * <p>The space allocated to this will be limited. It's recommended to limit
             * this to just a few characters.
             */
            @Nullable
            public String getUnit() {
                return mUnit;
            }
            /** @hide */
            @Override
            @NonNull
            @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
            public ValueString toValueString(Context context) {
                return new ValueString(mValue, null);
                return new ValueString(mValue, mUnit);
            }
        }
    }
+7 −2
Original line number Diff line number Diff line
@@ -175,6 +175,8 @@ public class NotificationMetricStyleTest {
                .addMetric(new Metric(
                        new FixedFloat(12.345f, null, 0, 3),
                        "Active time"))
                .addMetric(new Metric(
                        new FixedString("A LOT", "things"), "With unit"))
                .addMetric(new Metric(
                        new FixedString("This is the last"), "Last"));

@@ -555,8 +557,11 @@ public class NotificationMetricStyleTest {

    @Test
    public void valueToString_fixedString() {
        FixedString str = new FixedString("Boring");
        assertThat(str.toValueString(mContext)).isEqualTo(new ValueString("Boring", null));
        FixedString withUnit = new FixedString("120/80", "mmHg");
        assertThat(withUnit.toValueString(mContext)).isEqualTo(new ValueString("120/80", "mmHg"));

        FixedString noUnit = new FixedString("Boring");
        assertThat(noUnit.toValueString(mContext)).isEqualTo(new ValueString("Boring", null));
    }

    private void withLocale(Locale locale, Runnable r) {