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

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

Disallow MetricStyle with zero metrics

Throw on Builder.build() if that happens.

Bug: 433727001
Test: atest NotificationMetricStyleTest (CTS)
Flag: android.app.api_metric_style
Change-Id: I58775c798cc8e84384ed6f97e86ccc49375be18c
parent 5a90495a
Loading
Loading
Loading
Loading
+17 −2
Original line number Diff line number Diff line
@@ -11621,6 +11621,9 @@ public class Notification implements Parcelable
     *           Metric.TimeDifference.forStopwatch(startTime, FORMAT_CHRONOMETER_AUTOMATIC),
     *           "Time elapsed", MEANING_CHRONOMETER_STOPWATCH)))
     * </pre>
     *
     * <p>A MetricStyle must contain at least one {@link Metric} object to be valid; an invalid
     * style will be rejected when {@link Builder#build()} is called.
     */
    @FlaggedApi(Flags.FLAG_API_METRIC_STYLE)
    public static final class MetricStyle extends Style {
@@ -11669,10 +11672,13 @@ public class Notification implements Parcelable
            return this;
        }
        /** Returns the list of {@link Metric} instances in this {@link MetricStyle}. */
        /**
         * Returns an immutable view of the list of {@link Metric} instances in this
         * {@link MetricStyle}.
         */
        @NonNull
        public List<Metric> getMetrics() {
            return mMetrics;
            return Collections.unmodifiableList(mMetrics);
        }
        /** @hide */
@@ -11731,6 +11737,15 @@ public class Notification implements Parcelable
            }
        }
        /** @hide */
        @Override
        public void validate(@NonNull Context context) {
            super.validate(context);
            if (mMetrics.isEmpty()) {
                throw new IllegalArgumentException("A MetricStyle must have at least one Metric");
            }
        }
        /** @hide */
        @Override
        public boolean displayCustomViewInline() {
+4 −1
Original line number Diff line number Diff line
@@ -399,7 +399,10 @@ public class NotificationTest {
    public void testGetNotificationStyle_metricStyle_withApiFlagEnabled() {
        // FIRST -- check that this works if you use the constructor
        Notification n = new Notification.Builder(mContext, "test")
                .setStyle(new Notification.MetricStyle())
                .setStyle(new Notification.MetricStyle()
                        .addMetric(new Notification.Metric(
                                new Notification.Metric.FixedInt(1), "Int",
                                Notification.Metric.MEANING_UNKNOWN)))
                .setSmallIcon(android.R.drawable.sym_def_app_icon)
                .build();
        assertThat(n.extras.getString(Notification.EXTRA_TEMPLATE))