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

Commit 669deaab authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add unit to FixedString metric values" into main

parents 197dc9d6 3b55002f
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
@@ -179,6 +179,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"));

@@ -559,8 +561,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) {